From f0ec01f6570c3358be05091b37cad2a3df529619 Mon Sep 17 00:00:00 2001 From: jujiang Date: Tue, 13 Jul 2021 19:44:02 -0400 Subject: [PATCH] SWDEV-291787 - Fix persistent direct map Change-Id: Ic1507cc6d63e9ed574e8e169bce7bf56f4792c19 [ROCm/clr commit: 84b971c7c17be7c338a94c9b931190015530dc08] --- projects/clr/rocclr/device/device.hpp | 3 ++- projects/clr/rocclr/device/pal/palmemory.cpp | 14 ++++++++++---- projects/clr/rocclr/device/pal/palmemory.hpp | 10 ++++++++++ projects/clr/rocclr/device/pal/palresource.cpp | 6 +++--- projects/clr/rocclr/device/pal/palresource.hpp | 3 ++- projects/clr/rocclr/device/pal/palvirtual.cpp | 7 +++++-- 6 files changed, 32 insertions(+), 11 deletions(-) diff --git a/projects/clr/rocclr/device/device.hpp b/projects/clr/rocclr/device/device.hpp index 120e03ea71..115176f5ef 100644 --- a/projects/clr/rocclr/device/device.hpp +++ b/projects/clr/rocclr/device/device.hpp @@ -841,7 +841,8 @@ class Memory : public amd::HeapObject { SubMemoryObject = 0x00000008, //!< Memory is sub-memory HostMemoryRegistered = 0x00000010, //!< Host memory was registered MemoryCpuUncached = 0x00000020, //!< Memory is uncached on CPU access(slow read) - AllowedPeerAccess = 0x00000040 //!< Memory can be accessed from peer + AllowedPeerAccess = 0x00000040, //!< Memory can be accessed from peer + PersistentMap = 0x00000080 //!< Map Peristent memory }; uint flags_; //!< Memory object flags diff --git a/projects/clr/rocclr/device/pal/palmemory.cpp b/projects/clr/rocclr/device/pal/palmemory.cpp index 8a5940d1ac..29a4fe2010 100644 --- a/projects/clr/rocclr/device/pal/palmemory.cpp +++ b/projects/clr/rocclr/device/pal/palmemory.cpp @@ -786,12 +786,16 @@ void* Memory::allocMapTarget(const amd::Coord3D& origin, const amd::Coord3D& reg mapAddress = reinterpret_cast
(owner()->getHostMem()); } // If resource is a persistent allocation, we can use it directly - else if (isPersistentDirectMap()) { + else if ((isPersistentDirectMap(mapFlags & CL_MAP_WRITE) && (getMapCount() == 0)) || + isPersistentMapped()) { if (nullptr == map(nullptr)) { LogError("Could not map target persistent resource"); decIndMapCount(); return nullptr; } + if (getMapCount() == 1) { + setPersistentMapFlag(true); + } mapAddress = data(); } // Otherwise we can use a remote resource: @@ -1046,14 +1050,17 @@ void* Image::allocMapTarget(const amd::Coord3D& origin, const amd::Coord3D& regi //! runtime can't use it directly, //! because CAL volume map doesn't work properly. //! @todo arrays can be added for persistent lock with some CAL changes - else if (isPersistentDirectMap()) { + else if((isPersistentDirectMap(mapFlags & CL_MAP_WRITE) && (getMapCount() == 0)) || + isPersistentMapped()) { if (nullptr == map(nullptr)) { useRemoteResource = true; LogError("Could not map target persistent resource, try remote resource"); } else { useRemoteResource = false; mapAddress = data(); - + if (getMapCount() == 1) { + setPersistentMapFlag(true); + } // Calculate the offset in bytes offset *= elementSize(); @@ -1075,7 +1082,6 @@ void* Image::allocMapTarget(const amd::Coord3D& origin, const amd::Coord3D& regi const static bool SysMem = true; bool failed = false; amd::Memory* memory; - // Search for a possible indirect resource memory = dev().findMapTarget(owner()->getSize()); diff --git a/projects/clr/rocclr/device/pal/palmemory.hpp b/projects/clr/rocclr/device/pal/palmemory.hpp index bc49c0f65a..0c1cbd46a6 100644 --- a/projects/clr/rocclr/device/pal/palmemory.hpp +++ b/projects/clr/rocclr/device/pal/palmemory.hpp @@ -97,6 +97,16 @@ class Memory : public device::Memory, public Resource { size_t* slicePitch = NULL //!< Slice for the mapped memory ); + virtual bool isPersistentMapped() const { return (flags_ & PersistentMap) ? true : false; } + virtual void setPersistentMapFlag(bool persistentMapped) { + if (persistentMapped == true) { + flags_ |= PersistentMap; + } + else { + flags_ &= ~PersistentMap; + } + } + //! Pins system memory associated with this memory object virtual bool pinSystemMemory(void* hostPtr, //!< System memory address size_t size //!< Size of allocated system memory diff --git a/projects/clr/rocclr/device/pal/palresource.cpp b/projects/clr/rocclr/device/pal/palresource.cpp index c084c0d150..a3de8b86e3 100644 --- a/projects/clr/rocclr/device/pal/palresource.cpp +++ b/projects/clr/rocclr/device/pal/palresource.cpp @@ -1824,9 +1824,9 @@ bool Resource::isMemoryType(MemoryType memType) const { } // ================================================================================================ -bool Resource::isPersistentDirectMap() const { - bool directMap = - ((memoryType() == Resource::Persistent) && (desc().dimSize_ < 3) && !desc().imageArray_); +bool Resource::isPersistentDirectMap(bool writeMap) const { + bool directMap = ((memoryType() == Resource::Persistent) && + (desc().dimSize_ < 3) && !desc().imageArray_ && writeMap); // If direct map is possible, then validate it with the current tiling if (directMap && desc().tiled_) { diff --git a/projects/clr/rocclr/device/pal/palresource.hpp b/projects/clr/rocclr/device/pal/palresource.hpp index e6da7725da..c7108368bc 100644 --- a/projects/clr/rocclr/device/pal/palresource.hpp +++ b/projects/clr/rocclr/device/pal/palresource.hpp @@ -296,7 +296,8 @@ class Resource : public amd::HeapObject { bool mipMapped() const { return (desc().mipLevels_ > 1) ? true : false; } //! Checks if persistent memory can have a direct map - bool isPersistentDirectMap() const; + bool isPersistentDirectMap(bool writeMap = true) const; + int getMapCount() const { return mapCount_; } /*! \brief Locks the resource and returns a physical pointer * diff --git a/projects/clr/rocclr/device/pal/palvirtual.cpp b/projects/clr/rocclr/device/pal/palvirtual.cpp index ea6d87a512..47ea8b4f2b 100644 --- a/projects/clr/rocclr/device/pal/palvirtual.cpp +++ b/projects/clr/rocclr/device/pal/palvirtual.cpp @@ -1620,7 +1620,7 @@ void VirtualGPU::submitMapMemory(amd::MapMemoryCommand& vcmd) { // Add memory to VA cache, so rutnime can detect direct access to VA dev().addVACache(memory); - } else if (memory->isPersistentDirectMap()) { + } else if (memory->isPersistentMapped()) { // Nothing to do here } else if (memory->mapMemory() != nullptr) { // Target is a remote resource, so copy @@ -1721,10 +1721,13 @@ void VirtualGPU::submitUnmapMemory(amd::UnmapMemoryCommand& vcmd) { } // data check was added for persistent memory that failed to get aperture // and therefore are treated like a remote resource - else if (memory->isPersistentDirectMap() && (memory->data() != nullptr)) { + else if (memory->isPersistentMapped()) { // Map/unmap must be serialized amd::ScopedLock lock(owner->lockMemoryOps()); memory->unmap(this); + if (memory->getMapount() == 0) { + memory->setPersistentMapFlag(false); + } } else if (memory->mapMemory() != nullptr) { if (writeMapInfo->isUnmapWrite()) { amd::Coord3D srcOrigin(0, 0, 0);