From 7a3b2e583bebef13531aeccfa23a9461e1cd6edd Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Fri, 21 Jun 2019 10:35:05 +0200 Subject: [PATCH] Use GPU pointer when programming constant/global surfaces Related-To: NEO-3127 Change-Id: I29fd5e3f3f370c21a20f403f66c0a3604be884fd Signed-off-by: Mateusz Jablonski --- runtime/kernel/kernel.cpp | 2 +- runtime/program/process_gen_binary.cpp | 4 +- unit_tests/program/program_data_tests.cpp | 58 +++++++++++++---------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/runtime/kernel/kernel.cpp b/runtime/kernel/kernel.cpp index 276f94c4e8..91d8fce617 100644 --- a/runtime/kernel/kernel.cpp +++ b/runtime/kernel/kernel.cpp @@ -163,7 +163,7 @@ void Kernel::patchWithImplicitSurface(void *ptrToPatchInCrossThreadData, Graphic if (ssh) { auto surfaceState = ptrOffset(ssh, sshOffset); - void *addressToPatch = reinterpret_cast(allocation.getUnderlyingBuffer()); + void *addressToPatch = reinterpret_cast(allocation.getGpuAddressToPatch()); size_t sizeToPatch = allocation.getUnderlyingBufferSize(); Buffer::setSurfaceState(&getDevice(), surfaceState, sizeToPatch, addressToPatch, &allocation); } diff --git a/runtime/program/process_gen_binary.cpp b/runtime/program/process_gen_binary.cpp index 225a6b2d23..861d82d525 100644 --- a/runtime/program/process_gen_binary.cpp +++ b/runtime/program/process_gen_binary.cpp @@ -899,7 +899,7 @@ cl_int Program::parseProgramScopePatchList() { if (globalSurface->is32BitAllocation()) { *reinterpret_cast(pPtr) += static_cast(globalSurface->getGpuAddressToPatch()); } else { - *reinterpret_cast(pPtr) += reinterpret_cast(globalSurface->getUnderlyingBuffer()); + *reinterpret_cast(pPtr) += static_cast(globalSurface->getGpuAddressToPatch()); } } else { printDebugString(DebugManager.flags.PrintDebugMessages.get(), stderr, "Program::parseProgramScopePatchList. Unhandled Data parameter: %d\n", pPatch->Token); @@ -922,7 +922,7 @@ cl_int Program::parseProgramScopePatchList() { if (constantSurface->is32BitAllocation()) { *reinterpret_cast(pPtr) += static_cast(constantSurface->getGpuAddressToPatch()); } else { - *reinterpret_cast(pPtr) += reinterpret_cast(constantSurface->getUnderlyingBuffer()); + *reinterpret_cast(pPtr) += static_cast(constantSurface->getGpuAddressToPatch()); } } else { diff --git a/unit_tests/program/program_data_tests.cpp b/unit_tests/program/program_data_tests.cpp index f47e054891..d772a9b755 100644 --- a/unit_tests/program/program_data_tests.cpp +++ b/unit_tests/program/program_data_tests.cpp @@ -276,7 +276,12 @@ TEST_F(ProgramDataTest, GlobalPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); EXPECT_NE(nullptr, pProgram->getGlobalSurface()); - EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); + + auto globalSurface = pProgram->getGlobalSurface(); + + globalSurface->setCpuPtrAndGpuAddress(globalSurface->getUnderlyingBuffer(), globalSurface->getGpuAddress() + 1); + EXPECT_NE(reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalSurface->getGpuAddress()); + EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); delete[] pAllocateGlobalMemorySurface; @@ -299,7 +304,7 @@ TEST_F(ProgramDataTest, GlobalPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); + EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); delete[] pGlobalPointer; @@ -322,7 +327,7 @@ TEST_F(ProgramDataTest, GlobalPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); + EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); delete[] pGlobalPointer; @@ -345,7 +350,7 @@ TEST_F(ProgramDataTest, GlobalPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); + EXPECT_EQ(0, memcmp(&pGlobalPointerValue, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); delete[] pGlobalPointer; @@ -368,12 +373,10 @@ TEST_F(ProgramDataTest, GlobalPointerProgramBinaryInfo) { programPatchListSize = globalPointer.Size; buildAndDecodeProgramPatchList(); - auto globalSurface = pProgram->getGlobalSurface(); - if (!globalSurface->is32BitAllocation()) { - EXPECT_NE(0, memcmp(&pGlobalPointerValue, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); - ptr = pGlobalPointerValue + reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()); - EXPECT_EQ(0, memcmp(&ptr, reinterpret_cast(pProgram->getGlobalSurface()->getUnderlyingBuffer()), globalPointerSize)); + EXPECT_NE(0, memcmp(&pGlobalPointerValue, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); + ptr = pGlobalPointerValue + (globalSurface->getGpuAddressToPatch()); + EXPECT_EQ(0, memcmp(&ptr, reinterpret_cast(globalSurface->getUnderlyingBuffer()), globalPointerSize)); } delete[] pGlobalPointer; } @@ -472,18 +475,23 @@ TEST_F(ProgramDataTest, ConstantPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); EXPECT_NE(nullptr, pProgram->getConstantSurface()); - EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()), constantDataLen)); + + auto constantSurface = pProgram->getConstantSurface(); + constantSurface->setCpuPtrAndGpuAddress(constantSurface->getUnderlyingBuffer(), constantSurface->getGpuAddress() + 1); + EXPECT_NE(reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantSurface->getGpuAddress()); + + EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantDataLen)); // there was no PATCH_TOKEN_CONSTANT_POINTER_PROGRAM_BINARY_INFO, so constant buffer offset should be still 0 - EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); + EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(constantSurface->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); // once finally constant buffer offset gets patched - the patch value depends on the bitness of the compute kernel auto patchOffsetValueStorage = std::unique_ptr(new uint64_t); // 4bytes for 32-bit compute kernel, full 8byte for 64-bit compute kernel uint64_t *patchOffsetValue = patchOffsetValueStorage.get(); - if (pProgram->getConstantSurface()->is32BitAllocation() || (sizeof(void *) == 4)) { - reinterpret_cast(patchOffsetValue)[0] = static_cast(pProgram->getConstantSurface()->getGpuAddressToPatch()); + if (constantSurface->is32BitAllocation() || (sizeof(void *) == 4)) { + reinterpret_cast(patchOffsetValue)[0] = static_cast(constantSurface->getGpuAddressToPatch()); reinterpret_cast(patchOffsetValue)[1] = 0; // just pad with 0 } else { // full 8 bytes - *reinterpret_cast(patchOffsetValue) = reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()); + *reinterpret_cast(patchOffsetValue) = constantSurface->getGpuAddressToPatch(); } // constant pointer to constant surface - simulate invalid GlobalBufferIndex @@ -503,11 +511,11 @@ TEST_F(ProgramDataTest, ConstantPointerProgramBinaryInfo) { programPatchListSize = constantPointer.Size; buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()), constantDataLen)); + EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantDataLen)); // check that constant pointer offset was not patched - EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); + EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(constantSurface->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); // reset the constant pointer offset - *(uint64_t *)((char *)pProgram->getConstantSurface()->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; + *(uint64_t *)((char *)constantSurface->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; delete[] pConstantPointer; @@ -528,11 +536,11 @@ TEST_F(ProgramDataTest, ConstantPointerProgramBinaryInfo) { programPatchListSize = constantPointer.Size; buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()), constantDataLen)); + EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantDataLen)); // check that constant pointer offset was not patched - EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); + EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(constantSurface->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); // reset the constant pointer offset - *(uint64_t *)((char *)pProgram->getConstantSurface()->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; + *(uint64_t *)((char *)constantSurface->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; delete[] pConstantPointer; @@ -553,11 +561,11 @@ TEST_F(ProgramDataTest, ConstantPointerProgramBinaryInfo) { programPatchListSize = constantPointer.Size; buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()), constantDataLen)); + EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantDataLen)); // check that constant pointer offset was not patched - EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); + EXPECT_EQ(0U, *(uint64_t *)(reinterpret_cast(constantSurface->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); // reset the constant pointer offset - *(uint64_t *)((char *)pProgram->getConstantSurface()->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; + *(uint64_t *)((char *)constantSurface->getUnderlyingBuffer() + constantBufferOffsetPatchOffset) = 0U; delete[] pConstantPointer; @@ -579,9 +587,9 @@ TEST_F(ProgramDataTest, ConstantPointerProgramBinaryInfo) { buildAndDecodeProgramPatchList(); - EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()), constantDataLen)); + EXPECT_EQ(0, memcmp(pConstantData, reinterpret_cast(constantSurface->getUnderlyingBuffer()), constantDataLen)); // check that constant pointer offset was patched - EXPECT_EQ(*reinterpret_cast(patchOffsetValue), *(uint64_t *)(reinterpret_cast(pProgram->getConstantSurface()->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); + EXPECT_EQ(*reinterpret_cast(patchOffsetValue), *(uint64_t *)(reinterpret_cast(constantSurface->getUnderlyingBuffer()) + constantBufferOffsetPatchOffset)); delete[] pConstantPointer; }