Skip to content

Commit 21b452e

Browse files
committed
fix: poolbuffer check refcount issue when using clang compiler
The issue was traced to a subtle difference in std::unique_ptr's destruction process between Clang (libc++) and GCC (libstdc++). Clang's unique_ptr destructor first nullifies its internal pointer before calling the deleter, meaning that within the managed object's destructor, the owning pointer is already nullptr. In contrast, GCC's unique_ptr directly calls the deleter without first setting the internal pointer to nullptr. This difference caused a specific dependency or interaction within the managed object's destructor to behave incorrectly under GCC because it still saw a non-null owning pointer, leading to unexpected behavior during cleanup. PR is using flag-based system to replace the problematic pointer comparison ref #744 Signed-off-by: Mi, Yanfeng <yanfeng.mi@intel.com>
1 parent 6aa5610 commit 21b452e

File tree

6 files changed

+17
-1
lines changed

6 files changed

+17
-1
lines changed

opencl/source/context/context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ Context::BufferPool::BufferPool(Context *context) : BaseType(context->memoryMana
574574
bufferCreateArgs,
575575
errcodeRet));
576576
if (this->mainStorage) {
577+
this->mainStorage->setAsPoolBuffer(true);
577578
this->chunkAllocator.reset(new HeapAllocator(params.startingOffset,
578579
context->getBufferPoolAllocator().getParams().aggregatedSmallBuffersPoolSize,
579580
context->getBufferPoolAllocator().getParams().chunkAlignment));
@@ -602,6 +603,7 @@ Buffer *Context::BufferPool::allocate(const MemoryProperties &memoryProperties,
602603
auto bufferFromPool = this->mainStorage->createSubBuffer(flags, flagsIntel, &bufferRegion, errcodeRet);
603604
bufferFromPool->createFunction = this->mainStorage->createFunction;
604605
bufferFromPool->setSizeInPoolAllocator(actualSize);
606+
bufferFromPool->setAsPoolBuffer(true);
605607
return bufferFromPool;
606608
}
607609

opencl/source/mem_obj/buffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Buffer : public MemObj {
6666
constexpr static cl_ulong maskMagic = 0xFFFFFFFFFFFFFFFFLL;
6767
constexpr static cl_ulong objectMagic = MemObj::objectMagic | 0x02;
6868
bool forceDisallowCPUCopy = false;
69+
bool poolBuffer = false;
6970

7071
~Buffer() override;
7172

@@ -158,6 +159,8 @@ class Buffer : public MemObj {
158159
BufferCreateFunc createFunction = nullptr;
159160
bool isSubBuffer();
160161
bool isValidSubBufferOffset(size_t offset);
162+
void setAsPoolBuffer(bool value) { this->poolBuffer = value; }
163+
bool isPoolBuffer() const { return poolBuffer; }
161164
uint64_t setArgStateless(void *memory, uint32_t patchSize, uint32_t rootDeviceIndex, bool set32BitAddressing);
162165
virtual void setArgStateful(void *memory, bool forceNonAuxMode, bool disableL3, bool alignSizeForAuxTranslation,
163166
bool isReadOnly, const Device &device, bool areMultipleSubDevicesInContext) = 0;

shared/source/memory_manager/graphics_allocation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation>, NEO::NonCopyableAn
313313
bool isAllocatedInLocalMemoryPool() const { return (this->memoryPool == MemoryPool::localMemory); }
314314
bool isAllocationLockable() const;
315315

316+
void setAsPoolBuffer(bool value) { this->poolBuffer = value; }
317+
bool isPoolBuffer() const { return poolBuffer; }
318+
316319
const AubInfo &getAubInfo() const { return aubInfo; }
317320

318321
bool isCompressionEnabled() const;
@@ -438,6 +441,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation>, NEO::NonCopyableAn
438441
std::atomic<uint32_t> registeredContextsNum{0};
439442
bool shareableHostMemory = false;
440443
bool cantBeReadOnly = false;
444+
bool poolBuffer = false;
441445
bool explicitlyMadeResident = false;
442446
bool isImported = false;
443447
};

shared/source/utilities/buffer_pool_allocator.inl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ template <typename PoolT, typename BufferType, typename BufferParentType>
4848
bool AbstractBuffersPool<PoolT, BufferType, BufferParentType>::isPoolBuffer(const BufferParentType *buffer) const {
4949
static_assert(std::is_base_of_v<BufferParentType, BufferType>);
5050

51-
return (buffer && this->mainStorage.get() == buffer);
51+
const auto *bufferObj = static_cast<const BufferType *>(buffer);
52+
return bufferObj && bufferObj->isPoolBuffer();
5253
}
5354

5455
template <typename PoolT, typename BufferType, typename BufferParentType>

shared/source/utilities/generic_pool_allocator.inl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ GenericPool<Traits>::GenericPool(Device *device, size_t poolSize)
2828
MemoryConstants::pageSize,
2929
0u));
3030
this->mainStorage.reset(graphicsAllocation);
31+
if (this->mainStorage) {
32+
this->mainStorage->setAsPoolBuffer(true);
33+
}
3134
this->mtx = std::make_unique<std::mutex>();
3235
stackVec.push_back(graphicsAllocation);
3336
}

shared/source/utilities/isa_pool_allocator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ ISAPool::ISAPool(Device *device, bool isBuiltin, size_t storageSize)
2828
MemoryConstants::pageSize,
2929
0u));
3030
this->mainStorage.reset(graphicsAllocation);
31+
if (this->mainStorage) {
32+
this->mainStorage->setAsPoolBuffer(true);
33+
}
3134
this->mtx = std::make_unique<std::mutex>();
3235
this->stackVec.push_back(graphicsAllocation);
3336
}

0 commit comments

Comments
 (0)