From a5950500a3420381b5874a69714cab1c83cfe193 Mon Sep 17 00:00:00 2001 From: "Dunajski, Bartosz" Date: Mon, 13 Aug 2018 12:24:17 +0200 Subject: [PATCH] Aux translation [4/n]: Lock BuiltIn Kernel + refactor BuiltIns locking Change-Id: Ic7dc9b86a4aa5f93f1c4bcdf80b9598ecdff9713 Signed-off-by: Dunajski, Bartosz --- runtime/built_ins/built_ins.cpp | 34 ++++--- runtime/built_ins/built_ins.h | 13 +++ runtime/built_ins/builtins_dispatch_builder.h | 3 +- runtime/command_queue/enqueue_common.h | 3 + runtime/command_queue/enqueue_copy_buffer.h | 4 +- .../command_queue/enqueue_copy_buffer_rect.h | 4 +- .../enqueue_copy_buffer_to_image.h | 4 +- runtime/command_queue/enqueue_copy_image.h | 4 +- .../enqueue_copy_image_to_buffer.h | 4 +- runtime/command_queue/enqueue_fill_buffer.h | 4 +- runtime/command_queue/enqueue_fill_image.h | 4 +- runtime/command_queue/enqueue_read_buffer.h | 4 +- .../command_queue/enqueue_read_buffer_rect.h | 5 +- runtime/command_queue/enqueue_read_image.h | 5 +- runtime/command_queue/enqueue_svm.h | 8 +- runtime/command_queue/enqueue_write_buffer.h | 5 +- .../command_queue/enqueue_write_buffer_rect.h | 5 +- runtime/command_queue/enqueue_write_image.h | 5 +- runtime/helpers/properties_helper.h | 10 +++ unit_tests/built_ins/built_in_tests.cpp | 89 ++++++++++++++++--- unit_tests/mocks/mock_kernel.h | 12 +++ 21 files changed, 152 insertions(+), 77 deletions(-) diff --git a/runtime/built_ins/built_ins.cpp b/runtime/built_ins/built_ins.cpp index fdab19186d..52f50f456f 100644 --- a/runtime/built_ins/built_ins.cpp +++ b/runtime/built_ins/built_ins.cpp @@ -224,20 +224,6 @@ Program *BuiltIns::createBuiltInProgram( return pBuiltInProgram; } -void BuiltinDispatchInfoBuilder::takeOwnership(Context *context) { - for (auto &k : usedKernels) { - k->takeOwnership(true); - k->setContext(context); - } -} - -void BuiltinDispatchInfoBuilder::releaseOwnership() { - for (auto &k : usedKernels) { - k->setContext(nullptr); - k->releaseOwnership(); - } -} - template class BuiltInOp : public BuiltinDispatchInfoBuilder { public: @@ -827,4 +813,24 @@ std::unique_ptr BuiltIns::setBuiltinDispatchInfoBuil return builder; } +BuiltInOwnershipWrapper::BuiltInOwnershipWrapper(BuiltinDispatchInfoBuilder &inputBuilder, Context *context) { + takeOwnership(inputBuilder, context); +} +BuiltInOwnershipWrapper::~BuiltInOwnershipWrapper() { + if (builder) { + for (auto &kernel : builder->peekUsedKernels()) { + kernel->setContext(nullptr); + kernel->releaseOwnership(); + } + } +} +void BuiltInOwnershipWrapper::takeOwnership(BuiltinDispatchInfoBuilder &inputBuilder, Context *context) { + UNRECOVERABLE_IF(builder); + builder = &inputBuilder; + for (auto &kernel : builder->peekUsedKernels()) { + kernel->takeOwnership(true); + kernel->setContext(context); + } +} + } // namespace OCLRT diff --git a/runtime/built_ins/built_ins.h b/runtime/built_ins/built_ins.h index 2051cec633..17f5c81551 100644 --- a/runtime/built_ins/built_ins.h +++ b/runtime/built_ins/built_ins.h @@ -22,6 +22,7 @@ #pragma once #include "CL/cl.h" +#include "runtime/helpers/properties_helper.h" #include "runtime/built_ins/sip.h" #include "runtime/utilities/vec.h" @@ -238,6 +239,18 @@ class BuiltIns { bool enableCacheing = true; }; +class BuiltInOwnershipWrapper : public NonCopyableOrMovableClass { + public: + BuiltInOwnershipWrapper() = default; + BuiltInOwnershipWrapper(BuiltinDispatchInfoBuilder &inputBuilder, Context *context); + ~BuiltInOwnershipWrapper(); + + void takeOwnership(BuiltinDispatchInfoBuilder &inputBuilder, Context *context); + + protected: + BuiltinDispatchInfoBuilder *builder = nullptr; +}; + template class BuiltInOp; diff --git a/runtime/built_ins/builtins_dispatch_builder.h b/runtime/built_ins/builtins_dispatch_builder.h index ed262b1719..07aa3fa74c 100644 --- a/runtime/built_ins/builtins_dispatch_builder.h +++ b/runtime/built_ins/builtins_dispatch_builder.h @@ -90,8 +90,7 @@ class BuiltinDispatchInfoBuilder { return true; } - void takeOwnership(Context *context); - void releaseOwnership(); + std::vector> &peekUsedKernels() { return usedKernels; } protected: template diff --git a/runtime/command_queue/enqueue_common.h b/runtime/command_queue/enqueue_common.h index af8ea5e64b..23fddc6df1 100644 --- a/runtime/command_queue/enqueue_common.h +++ b/runtime/command_queue/enqueue_common.h @@ -78,6 +78,7 @@ void CommandQueueHw::enqueueHandler(Surface *(&surfaces)[surfaceCount if (kernel == nullptr) { enqueueHandler(surfaces, blocking, MultiDispatchInfo(), numEventsInWaitList, eventWaitList, event); } else { + BuiltInOwnershipWrapper builtInLock; MultiDispatchInfo multiDispatchInfo; if (DebugManager.flags.ForceDispatchScheduler.get()) { @@ -85,6 +86,8 @@ void CommandQueueHw::enqueueHandler(Surface *(&surfaces)[surfaceCount } else { BuffersForAuxTranslation buffersForAuxTranslation; if (kernel->isAuxTranslationRequired()) { + auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::AuxTranslation, getContext(), getDevice()); + builtInLock.takeOwnership(builder, this->context); kernel->fillWithBuffersForAuxTranslation(buffersForAuxTranslation); dispatchAuxTranslation(multiDispatchInfo, buffersForAuxTranslation, AuxTranslationDirection::AuxToNonAux); } diff --git a/runtime/command_queue/enqueue_copy_buffer.h b/runtime/command_queue/enqueue_copy_buffer.h index f83d565af6..7916b7d368 100644 --- a/runtime/command_queue/enqueue_copy_buffer.h +++ b/runtime/command_queue/enqueue_copy_buffer.h @@ -48,7 +48,7 @@ cl_int CommandQueueHw::enqueueCopyBuffer( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); BuiltinDispatchInfoBuilder::BuiltinOpParams dc; dc.srcMemObj = srcBuffer; @@ -70,8 +70,6 @@ cl_int CommandQueueHw::enqueueCopyBuffer( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_copy_buffer_rect.h b/runtime/command_queue/enqueue_copy_buffer_rect.h index a120258861..9cdd694172 100644 --- a/runtime/command_queue/enqueue_copy_buffer_rect.h +++ b/runtime/command_queue/enqueue_copy_buffer_rect.h @@ -50,7 +50,7 @@ cl_int CommandQueueHw::enqueueCopyBufferRect( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferRect, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); MemObjSurface srcBufferSurf(srcBuffer); MemObjSurface dstBufferSurf(dstBuffer); @@ -76,8 +76,6 @@ cl_int CommandQueueHw::enqueueCopyBufferRect( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_copy_buffer_to_image.h b/runtime/command_queue/enqueue_copy_buffer_to_image.h index 8064696817..b741557b15 100644 --- a/runtime/command_queue/enqueue_copy_buffer_to_image.h +++ b/runtime/command_queue/enqueue_copy_buffer_to_image.h @@ -50,7 +50,7 @@ cl_int CommandQueueHw::enqueueCopyBufferToImage( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToImage3d, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); MemObjSurface srcBufferSurf(srcBuffer); MemObjSurface dstImgSurf(dstImage); @@ -75,8 +75,6 @@ cl_int CommandQueueHw::enqueueCopyBufferToImage( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_copy_image.h b/runtime/command_queue/enqueue_copy_image.h index 691d9f7afb..dd69627cf8 100644 --- a/runtime/command_queue/enqueue_copy_image.h +++ b/runtime/command_queue/enqueue_copy_image.h @@ -50,7 +50,7 @@ cl_int CommandQueueHw::enqueueCopyImage( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyImageToImage3d, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); MemObjSurface srcImgSurf(srcImage); MemObjSurface dstImgSurf(dstImage); @@ -78,8 +78,6 @@ cl_int CommandQueueHw::enqueueCopyImage( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_copy_image_to_buffer.h b/runtime/command_queue/enqueue_copy_image_to_buffer.h index c2a0ca520e..55648d5aba 100644 --- a/runtime/command_queue/enqueue_copy_image_to_buffer.h +++ b/runtime/command_queue/enqueue_copy_image_to_buffer.h @@ -50,7 +50,7 @@ cl_int CommandQueueHw::enqueueCopyImageToBuffer( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyImage3dToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); MemObjSurface srcImgSurf(srcImage); MemObjSurface dstBufferSurf(dstBuffer); @@ -75,8 +75,6 @@ cl_int CommandQueueHw::enqueueCopyImageToBuffer( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_fill_buffer.h b/runtime/command_queue/enqueue_fill_buffer.h index 32bd941daf..e2ad5eb2db 100644 --- a/runtime/command_queue/enqueue_fill_buffer.h +++ b/runtime/command_queue/enqueue_fill_buffer.h @@ -64,7 +64,7 @@ cl_int CommandQueueHw::enqueueFillBuffer( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::FillBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); BuiltinDispatchInfoBuilder::BuiltinOpParams dc; MemObj patternMemObj(this->context, 0, 0, alignUp(patternSize, 4), patternAllocation->getUnderlyingBuffer(), @@ -89,8 +89,6 @@ cl_int CommandQueueHw::enqueueFillBuffer( memoryManager->storeAllocation(std::unique_ptr(patternAllocation), TEMPORARY_ALLOCATION, taskCount); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_fill_image.h b/runtime/command_queue/enqueue_fill_image.h index 0421af4c86..38e709c85e 100644 --- a/runtime/command_queue/enqueue_fill_image.h +++ b/runtime/command_queue/enqueue_fill_image.h @@ -49,7 +49,7 @@ cl_int CommandQueueHw::enqueueFillImage( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::FillImage3d, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); MemObjSurface dstImgSurf(image); Surface *surfaces[] = {&dstImgSurf}; @@ -70,8 +70,6 @@ cl_int CommandQueueHw::enqueueFillImage( eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_read_buffer.h b/runtime/command_queue/enqueue_read_buffer.h index 33734e2254..07ce43bff0 100644 --- a/runtime/command_queue/enqueue_read_buffer.h +++ b/runtime/command_queue/enqueue_read_buffer.h @@ -94,7 +94,7 @@ cl_int CommandQueueHw::enqueueReadBuffer( } auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); void *dstPtr = ptr; @@ -105,7 +105,6 @@ cl_int CommandQueueHw::enqueueReadBuffer( if (size != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } dstPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -131,7 +130,6 @@ cl_int CommandQueueHw::enqueueReadBuffer( numEventsInWaitList, eventWaitList, event); - builder.releaseOwnership(); return CL_SUCCESS; } diff --git a/runtime/command_queue/enqueue_read_buffer_rect.h b/runtime/command_queue/enqueue_read_buffer_rect.h index 4190379469..34bc9a3f60 100644 --- a/runtime/command_queue/enqueue_read_buffer_rect.h +++ b/runtime/command_queue/enqueue_read_buffer_rect.h @@ -79,7 +79,7 @@ cl_int CommandQueueHw::enqueueReadBufferRect( } auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferRect, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); size_t hostPtrSize = Buffer::calculateHostPtrSize(hostOrigin, region, hostRowPitch, hostSlicePitch); void *dstPtr = ptr; @@ -93,7 +93,6 @@ cl_int CommandQueueHw::enqueueReadBufferRect( region[2] != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } dstPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -119,8 +118,6 @@ cl_int CommandQueueHw::enqueueReadBufferRect( eventWaitList, event); - builder.releaseOwnership(); - if (context->isProvidingPerformanceHints()) { context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL, CL_ENQUEUE_READ_BUFFER_RECT_REQUIRES_COPY_DATA, static_cast(buffer), ptr); if (!isL3Capable(ptr, hostPtrSize)) { diff --git a/runtime/command_queue/enqueue_read_image.h b/runtime/command_queue/enqueue_read_image.h index d52b031327..98cddf0df2 100644 --- a/runtime/command_queue/enqueue_read_image.h +++ b/runtime/command_queue/enqueue_read_image.h @@ -82,7 +82,7 @@ cl_int CommandQueueHw::enqueueReadImage( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyImage3dToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); size_t hostPtrSize = calculateHostPtrSizeForImage(const_cast(region), inputRowPitch, inputSlicePitch, srcImage); void *dstPtr = ptr; @@ -96,7 +96,6 @@ cl_int CommandQueueHw::enqueueReadImage( region[2] != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } dstPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -122,8 +121,6 @@ cl_int CommandQueueHw::enqueueReadImage( eventWaitList, event); - builder.releaseOwnership(); - if (context->isProvidingPerformanceHints()) { if (!isL3Capable(ptr, hostPtrSize)) { context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_BAD_INTEL, CL_ENQUEUE_READ_IMAGE_DOESNT_MEET_ALIGNMENT_RESTRICTIONS, ptr, hostPtrSize, MemoryConstants::pageSize, MemoryConstants::pageSize); diff --git a/runtime/command_queue/enqueue_svm.h b/runtime/command_queue/enqueue_svm.h index 26ee5267b2..6287129004 100644 --- a/runtime/command_queue/enqueue_svm.h +++ b/runtime/command_queue/enqueue_svm.h @@ -194,7 +194,7 @@ cl_int CommandQueueHw::enqueueSVMMemcpy(cl_bool blockingCopy, auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); BuiltinDispatchInfoBuilder::BuiltinOpParams operationParams; operationParams.srcPtr = const_cast(srcPtr); @@ -217,8 +217,6 @@ cl_int CommandQueueHw::enqueueSVMMemcpy(cl_bool blockingCopy, eventWaitList, event); - builder.releaseOwnership(); - return CL_SUCCESS; } @@ -264,7 +262,7 @@ cl_int CommandQueueHw::enqueueSVMMemFill(void *svmPtr, auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::FillBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); BuiltinDispatchInfoBuilder::BuiltinOpParams operationParams; MemObj patternMemObj(this->context, 0, 0, alignUp(patternSize, 4), patternAllocation->getUnderlyingBuffer(), @@ -290,8 +288,6 @@ cl_int CommandQueueHw::enqueueSVMMemFill(void *svmPtr, memoryManager->storeAllocation(std::unique_ptr(patternAllocation), REUSABLE_ALLOCATION, taskCount); - builder.releaseOwnership(); - return CL_SUCCESS; } diff --git a/runtime/command_queue/enqueue_write_buffer.h b/runtime/command_queue/enqueue_write_buffer.h index f8ed271e64..e681b80a5e 100644 --- a/runtime/command_queue/enqueue_write_buffer.h +++ b/runtime/command_queue/enqueue_write_buffer.h @@ -95,7 +95,7 @@ cl_int CommandQueueHw::enqueueWriteBuffer( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToBuffer, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); void *srcPtr = const_cast(ptr); @@ -106,7 +106,6 @@ cl_int CommandQueueHw::enqueueWriteBuffer( if (size != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } srcPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -131,8 +130,6 @@ cl_int CommandQueueHw::enqueueWriteBuffer( context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL, CL_ENQUEUE_WRITE_BUFFER_REQUIRES_COPY_DATA, static_cast(buffer)); } - builder.releaseOwnership(); - return CL_SUCCESS; } } // namespace OCLRT diff --git a/runtime/command_queue/enqueue_write_buffer_rect.h b/runtime/command_queue/enqueue_write_buffer_rect.h index 00dd8c9a77..bf90da875b 100644 --- a/runtime/command_queue/enqueue_write_buffer_rect.h +++ b/runtime/command_queue/enqueue_write_buffer_rect.h @@ -78,7 +78,7 @@ cl_int CommandQueueHw::enqueueWriteBufferRect( } auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferRect, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper builtInLock(builder, this->context); size_t hostPtrSize = Buffer::calculateHostPtrSize(hostOrigin, region, hostRowPitch, hostSlicePitch); void *srcPtr = const_cast(ptr); @@ -92,7 +92,6 @@ cl_int CommandQueueHw::enqueueWriteBufferRect( region[2] != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } srcPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -118,8 +117,6 @@ cl_int CommandQueueHw::enqueueWriteBufferRect( eventWaitList, event); - builder.releaseOwnership(); - if (context->isProvidingPerformanceHints()) { context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL, CL_ENQUEUE_WRITE_BUFFER_RECT_REQUIRES_COPY_DATA, static_cast(buffer)); } diff --git a/runtime/command_queue/enqueue_write_image.h b/runtime/command_queue/enqueue_write_image.h index 48fc480879..f398cf8fb5 100644 --- a/runtime/command_queue/enqueue_write_image.h +++ b/runtime/command_queue/enqueue_write_image.h @@ -79,7 +79,7 @@ cl_int CommandQueueHw::enqueueWriteImage( auto &builder = BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToImage3d, this->getContext(), this->getDevice()); - builder.takeOwnership(this->context); + BuiltInOwnershipWrapper lock(builder, this->context); size_t hostPtrSize = calculateHostPtrSizeForImage(const_cast(region), inputRowPitch, inputSlicePitch, dstImage); void *srcPtr = const_cast(ptr); @@ -93,7 +93,6 @@ cl_int CommandQueueHw::enqueueWriteImage( region[2] != 0) { bool status = createAllocationForHostSurface(hostPtrSurf); if (!status) { - builder.releaseOwnership(); return CL_OUT_OF_RESOURCES; } srcPtr = reinterpret_cast(hostPtrSurf.getAllocation()->getGpuAddressToPatch()); @@ -120,8 +119,6 @@ cl_int CommandQueueHw::enqueueWriteImage( eventWaitList, event); - builder.releaseOwnership(); - if (context->isProvidingPerformanceHints()) { context->providePerformanceHint(CL_CONTEXT_DIAGNOSTICS_LEVEL_NEUTRAL_INTEL, CL_ENQUEUE_WRITE_IMAGE_REQUIRES_COPY_DATA, static_cast(dstImage)); } diff --git a/runtime/helpers/properties_helper.h b/runtime/helpers/properties_helper.h index 22be53d9e6..6991c5b9dd 100644 --- a/runtime/helpers/properties_helper.h +++ b/runtime/helpers/properties_helper.h @@ -87,4 +87,14 @@ struct MapInfo { bool readOnly = false; uint32_t mipLevel = 0; }; + +class NonCopyableOrMovableClass { + public: + NonCopyableOrMovableClass() = default; + NonCopyableOrMovableClass(const NonCopyableOrMovableClass &) = delete; + NonCopyableOrMovableClass &operator=(const NonCopyableOrMovableClass &) = delete; + + NonCopyableOrMovableClass(NonCopyableOrMovableClass &&) = delete; + NonCopyableOrMovableClass &operator=(NonCopyableOrMovableClass &&) = delete; +}; } // namespace OCLRT diff --git a/unit_tests/built_ins/built_in_tests.cpp b/unit_tests/built_ins/built_in_tests.cpp index 36816c2aef..9f58333888 100644 --- a/unit_tests/built_ins/built_in_tests.cpp +++ b/unit_tests/built_ins/built_in_tests.cpp @@ -41,6 +41,7 @@ #include "unit_tests/global_environment.h" #include "unit_tests/helpers/debug_manager_state_restore.h" #include "unit_tests/mocks/mock_buffer.h" +#include "unit_tests/mocks/mock_command_queue.h" #include "unit_tests/mocks/mock_builtins.h" #include "unit_tests/mocks/mock_compilers.h" #include "unit_tests/mocks/mock_kernel.h" @@ -204,11 +205,6 @@ TEST_F(BuiltInTests, BuiltinDispatchInfoBuilderCopyBufferToBuffer) { builtinOpsParams.size = {dst.getSize(), 0, 0}; ASSERT_TRUE(builder.buildDispatchInfos(multiDispatchInfo, builtinOpsParams)); - builder.takeOwnership(pContext); - - for (auto &dispatchInfo : multiDispatchInfo) { - EXPECT_TRUE(dispatchInfo.getKernel()->hasOwnership()); - } size_t leftSize = reinterpret_cast(dst.getCpuAddress()) % MemoryConstants::cacheLineSize; if (leftSize > 0) { @@ -249,11 +245,6 @@ TEST_F(BuiltInTests, BuiltinDispatchInfoBuilderCopyBufferToBuffer) { i++; } - builder.releaseOwnership(); - for (auto &dispatchInfo : multiDispatchInfo) { - EXPECT_FALSE(dispatchInfo.getKernel()->hasOwnership()); - } - delete srcPtr; delete dstPtr; } @@ -413,6 +404,7 @@ class MockAuxBuilInOp : public BuiltInOp { using BaseClass::convertToAuxKernel; using BaseClass::convertToNonAuxKernel; using BaseClass::resizeKernelInstances; + using BaseClass::usedKernels; MockAuxBuilInOp(BuiltIns &kernelsLib, Context &context, Device &device) : BaseClass(kernelsLib, context, device) {} }; @@ -447,7 +439,6 @@ HWTEST_F(BuiltInTests, givenMoreBuffersForAuxTranslationThanKernelInstancesWhenD HWTEST_F(BuiltInTests, givenkAuxBuiltInWhenResizeIsCalledThenCloneAllNewInstancesFromBaseKernel) { MockAuxBuilInOp mockAuxBuiltInOp(*pBuiltIns, *pContext, *pDevice); - size_t newSize = mockAuxBuiltInOp.convertToAuxKernel.size() + 3; mockAuxBuiltInOp.resizeKernelInstances(newSize); @@ -462,6 +453,39 @@ HWTEST_F(BuiltInTests, givenkAuxBuiltInWhenResizeIsCalledThenCloneAllNewInstance } } +HWTEST_F(BuiltInTests, givenKernelWithAuxTranslationRequiredWhenEnqueueCalledThenLockOnBuiltin) { + BuiltIns::getInstance().getBuiltinDispatchInfoBuilder(EBuiltInOps::AuxTranslation, *pContext, *pDevice); + auto mockAuxBuiltInOp = new MockAuxBuilInOp(*pBuiltIns, *pContext, *pDevice); + pBuiltIns->BuiltinOpsBuilders[static_cast(EBuiltInOps::AuxTranslation)].first.reset(mockAuxBuiltInOp); + + MockProgram mockProgram; + auto mockBuiltinKernel = MockKernel::create(*pDevice, &mockProgram); + mockAuxBuiltInOp->usedKernels.at(0).reset(mockBuiltinKernel); + + MockKernelWithInternals mockKernel(*pDevice, pContext); + MockCommandQueueHw cmdQ(pContext, pDevice, nullptr); + size_t gws[3] = {1, 0, 0}; + MockBuffer buffer; + cl_mem clMem = &buffer; + + buffer.getGraphicsAllocation()->setAllocationType(GraphicsAllocation::AllocationType::BUFFER_COMPRESSED); + mockKernel.kernelInfo.kernelArgInfo.resize(1); + mockKernel.kernelInfo.kernelArgInfo.at(0).kernelArgPatchInfoVector.resize(1); + mockKernel.kernelInfo.kernelArgInfo.at(0).pureStatefulBufferAccess = false; + mockKernel.mockKernel->initialize(); + mockKernel.mockKernel->setArgBuffer(0, sizeof(cl_mem *), &clMem); + + mockKernel.mockKernel->auxTranslationRequired = false; + cmdQ.enqueueKernel(mockKernel.mockKernel, 1, nullptr, gws, nullptr, 0, nullptr, nullptr); + EXPECT_EQ(0u, mockBuiltinKernel->takeOwnershipCalls); + EXPECT_EQ(0u, mockBuiltinKernel->releaseOwnershipCalls); + + mockKernel.mockKernel->auxTranslationRequired = true; + cmdQ.enqueueKernel(mockKernel.mockKernel, 1, nullptr, gws, nullptr, 0, nullptr, nullptr); + EXPECT_EQ(1u, mockBuiltinKernel->takeOwnershipCalls); + EXPECT_EQ(1u, mockBuiltinKernel->releaseOwnershipCalls); +} + TEST_F(BuiltInTests, BuiltinDispatchInfoBuilderCopyBufferToBufferAligned) { BuiltinDispatchInfoBuilder &builder = pBuiltIns->getBuiltinDispatchInfoBuilder(EBuiltInOps::CopyBufferToBuffer, *pContext, *pDevice); @@ -1740,3 +1764,46 @@ TEST_F(BuiltInTests, givenDebugFlagForceUseSourceWhenArgIsAnyThenReturnBuiltinCo EXPECT_NE(0u, code.resource.size()); EXPECT_EQ(pDevice, code.targetDevice); } + +using BuiltInOwnershipWrapperTests = BuiltInTests; + +HWTEST_F(BuiltInOwnershipWrapperTests, givenBuiltinWhenConstructedThenLockAndUnlockOnDestruction) { + MockAuxBuilInOp mockAuxBuiltInOp(*pBuiltIns, *pContext, *pDevice); + MockContext mockContext; + + { + BuiltInOwnershipWrapper lock(mockAuxBuiltInOp, &mockContext); + EXPECT_TRUE(mockAuxBuiltInOp.baseKernel->hasOwnership()); + EXPECT_EQ(&mockContext, &mockAuxBuiltInOp.baseKernel->getContext()); + } + EXPECT_FALSE(mockAuxBuiltInOp.baseKernel->hasOwnership()); + EXPECT_EQ(pContext, &mockAuxBuiltInOp.baseKernel->getContext()); +} + +HWTEST_F(BuiltInOwnershipWrapperTests, givenLockWithoutParametersWhenConstructingThenLockOnlyWhenRequested) { + MockAuxBuilInOp mockAuxBuiltInOp(*pBuiltIns, *pContext, *pDevice); + MockContext mockContext; + + { + BuiltInOwnershipWrapper lock; + lock.takeOwnership(mockAuxBuiltInOp, &mockContext); + EXPECT_TRUE(mockAuxBuiltInOp.baseKernel->hasOwnership()); + EXPECT_EQ(&mockContext, &mockAuxBuiltInOp.baseKernel->getContext()); + } + EXPECT_FALSE(mockAuxBuiltInOp.baseKernel->hasOwnership()); + EXPECT_EQ(pContext, &mockAuxBuiltInOp.baseKernel->getContext()); +} + +HWTEST_F(BuiltInOwnershipWrapperTests, givenLockWithAcquiredOwnershipWhenTakeOwnershipCalledThenAbort) { + MockAuxBuilInOp mockAuxBuiltInOp1(*pBuiltIns, *pContext, *pDevice); + MockAuxBuilInOp mockAuxBuiltInOp2(*pBuiltIns, *pContext, *pDevice); + + BuiltInOwnershipWrapper lock(mockAuxBuiltInOp1, pContext); + EXPECT_THROW(lock.takeOwnership(mockAuxBuiltInOp1, pContext), std::exception); + EXPECT_THROW(lock.takeOwnership(mockAuxBuiltInOp2, pContext), std::exception); +} + +HWTEST_F(BuiltInOwnershipWrapperTests, givenBuiltInOwnershipWrapperWhenAskedForTypeTraitsThenDisableCopyConstructorAndOperator) { + EXPECT_FALSE(std::is_copy_constructible::value); + EXPECT_FALSE(std::is_copy_assignable::value); +} diff --git a/unit_tests/mocks/mock_kernel.h b/unit_tests/mocks/mock_kernel.h index e56afa60eb..1b6c9a62e0 100644 --- a/unit_tests/mocks/mock_kernel.h +++ b/unit_tests/mocks/mock_kernel.h @@ -227,9 +227,21 @@ class MockKernel : public Kernel { void makeResident(CommandStreamReceiver &commandStreamReceiver) override; void getResidency(std::vector &dst) override; + bool takeOwnership(bool lock) const override { + auto retVal = Kernel::takeOwnership(lock); + takeOwnershipCalls++; + return retVal; + } + + void releaseOwnership() const override { + releaseOwnershipCalls++; + Kernel::releaseOwnership(); + } uint32_t makeResidentCalls = 0; uint32_t getResidencyCalls = 0; + mutable uint32_t takeOwnershipCalls = 0; + mutable uint32_t releaseOwnershipCalls = 0; bool canKernelTransformImages = true;