From 101c914b1fc06a540a0d37f79cec998e3c245c1c Mon Sep 17 00:00:00 2001 From: Kamil Kopryk Date: Mon, 20 May 2024 13:26:09 +0000 Subject: [PATCH] test: add missing ult for checking patch address in heapless mode OCL Signed-off-by: Kamil Kopryk --- .../unit_tests/sources/module/test_module.cpp | 16 +-- .../unit_test/program/program_data_tests.cpp | 103 ++++++++++-------- shared/test/common/mocks/CMakeLists.txt | 1 + .../mocks/mock_compiler_product_helper.h | 22 ++++ 4 files changed, 83 insertions(+), 59 deletions(-) create mode 100644 shared/test/common/mocks/mock_compiler_product_helper.h diff --git a/level_zero/core/test/unit_tests/sources/module/test_module.cpp b/level_zero/core/test/unit_tests/sources/module/test_module.cpp index 64cbeb65aa..fa3f825031 100644 --- a/level_zero/core/test/unit_tests/sources/module/test_module.cpp +++ b/level_zero/core/test/unit_tests/sources/module/test_module.cpp @@ -25,6 +25,7 @@ #include "shared/test/common/helpers/debug_manager_state_restore.h" #include "shared/test/common/helpers/mock_file_io.h" #include "shared/test/common/libult/ult_command_stream_receiver.h" +#include "shared/test/common/mocks/mock_compiler_product_helper.h" #include "shared/test/common/mocks/mock_device.h" #include "shared/test/common/mocks/mock_elf.h" #include "shared/test/common/mocks/mock_graphics_allocation.h" @@ -1879,22 +1880,11 @@ TEST_F(ModuleDynamicLinkTests, givenModuleWithInternalRelocationAndUnresolvedExt EXPECT_EQ(externalSymbolAddress, *reinterpret_cast(ptrOffset(isaPtr, externalRelocationOffset))); } -template -class MockCompilerProductHelperHw : public CompilerProductHelperHw { - public: - bool isHeaplessModeEnabled() const override { - return heaplessModeEnabled; - } - - MockCompilerProductHelperHw(bool heaplessModeEnabled) : CompilerProductHelperHw(), heaplessModeEnabled(heaplessModeEnabled) {} - bool heaplessModeEnabled = false; -}; - HWTEST2_F(ModuleDynamicLinkTests, givenHeaplessAndModuleWithInternalRelocationAndUnresolvedExternalSymbolWhenLinkModuleThenPatchedAddressesAreCorrect, MatchAny) { for (bool heaplessModeEnabled : {true, false}) { - auto backup = std::unique_ptr(new MockCompilerProductHelperHw(heaplessModeEnabled)); + auto backup = std::unique_ptr(new MockCompilerProductHelperHeaplessHw(heaplessModeEnabled)); neoDevice->getRootDeviceEnvironmentRef().compilerProductHelper.swap(backup); auto linkerInput = std::make_unique<::WhiteBox>(); @@ -1941,7 +1931,7 @@ HWTEST2_F(ModuleDynamicLinkTests, givenHeaplessAndModuleWithInternalRelocationAn for (bool heaplessModeEnabled : {true, false}) { - auto backup = std::unique_ptr(new MockCompilerProductHelperHw(heaplessModeEnabled)); + auto backup = std::unique_ptr(new MockCompilerProductHelperHeaplessHw(heaplessModeEnabled)); neoDevice->getRootDeviceEnvironmentRef().compilerProductHelper.swap(backup); auto linkerInput = std::make_unique<::WhiteBox>(); diff --git a/opencl/test/unit_test/program/program_data_tests.cpp b/opencl/test/unit_test/program/program_data_tests.cpp index 9b95200701..65ed107c0f 100644 --- a/opencl/test/unit_test/program/program_data_tests.cpp +++ b/opencl/test/unit_test/program/program_data_tests.cpp @@ -16,6 +16,7 @@ #include "shared/test/common/helpers/debug_manager_state_restore.h" #include "shared/test/common/helpers/gtest_helpers.h" #include "shared/test/common/mocks/mock_bindless_heaps_helper.h" +#include "shared/test/common/mocks/mock_compiler_product_helper.h" #include "shared/test/common/mocks/mock_csr.h" #include "shared/test/common/mocks/mock_execution_environment.h" #include "shared/test/common/mocks/mock_memory_manager.h" @@ -705,59 +706,69 @@ TEST(ProgramLinkBinaryTest, whenLinkerUnresolvedExternalThenLinkFailedAndBuildLo device->getMemoryManager()->freeGraphicsMemory(kernelInfo.getGraphicsAllocation()); } -TEST_F(ProgramDataTest, whenLinkerInputValidThenIsaIsProperlyPatched) { - auto linkerInput = std::make_unique>(); - linkerInput->symbols["A"] = NEO::SymbolInfo{4U, 4U, NEO::SegmentType::globalVariables, std::numeric_limits::max(), true}; - linkerInput->symbols["B"] = NEO::SymbolInfo{8U, 4U, NEO::SegmentType::globalConstants, std::numeric_limits::max(), true}; - linkerInput->symbols["C"] = NEO::SymbolInfo{16U, 4U, NEO::SegmentType::instructions, std::numeric_limits::max(), true}; +HWTEST2_F(ProgramDataTest, whenLinkerInputValidThenIsaIsProperlyPatched, MatchAny) { - auto relocationType = NEO::LinkerInput::RelocationInfo::Type::address; - linkerInput->textRelocations.push_back({NEO::LinkerInput::RelocationInfo{"A", 8U, relocationType}, - NEO::LinkerInput::RelocationInfo{"B", 16U, relocationType}, - NEO::LinkerInput::RelocationInfo{"C", 24U, relocationType}}); - linkerInput->traits.requiresPatchingOfInstructionSegments = true; - linkerInput->exportedFunctionsSegmentId = 0; - auto device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); - MockProgram program{nullptr, false, toClDeviceVector(*device)}; - auto &buildInfo = program.buildInfos[device->getRootDeviceIndex()]; - KernelInfo kernelInfo = {}; - kernelInfo.kernelDescriptor.kernelMetadata.kernelName = "onlyKernel"; - std::vector kernelHeap; - kernelHeap.resize(32, 7); - kernelInfo.heapInfo.pKernelHeap = kernelHeap.data(); - kernelInfo.heapInfo.kernelHeapSize = static_cast(kernelHeap.size()); - MockGraphicsAllocation kernelIsa(kernelHeap.data(), kernelHeap.size()); - kernelInfo.kernelAllocation = &kernelIsa; - program.getKernelInfoArray(rootDeviceIndex).push_back(&kernelInfo); - program.setLinkerInput(device->getRootDeviceIndex(), std::move(linkerInput)); + for (bool heaplessModeEnabled : {false, true}) { - buildInfo.exportedFunctionsSurface = kernelInfo.kernelAllocation; - std::vector globalVariablesBuffer; - globalVariablesBuffer.resize(32, 7); - std::vector globalConstantsBuffer; - globalConstantsBuffer.resize(32, 7); - std::vector globalVariablesInitData{32, 0}; - std::vector globalConstantsInitData{32, 0}; - buildInfo.globalSurface = new MockGraphicsAllocation(globalVariablesBuffer.data(), globalVariablesBuffer.size()); - buildInfo.constantSurface = new MockGraphicsAllocation(globalConstantsBuffer.data(), globalConstantsBuffer.size()); + auto device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); + auto backup = std::unique_ptr(new MockCompilerProductHelperHeaplessHw(heaplessModeEnabled)); + device->device.getRootDeviceEnvironmentRef().compilerProductHelper.swap(backup); - auto ret = program.linkBinary(&pClDevice->getDevice(), globalConstantsInitData.data(), globalConstantsInitData.size(), globalVariablesInitData.data(), globalVariablesInitData.size(), {}, program.externalFunctions); - EXPECT_EQ(CL_SUCCESS, ret); + auto linkerInput = std::make_unique>(); + linkerInput->symbols["A"] = NEO::SymbolInfo{4U, 4U, NEO::SegmentType::globalVariables, std::numeric_limits::max(), true}; + linkerInput->symbols["B"] = NEO::SymbolInfo{8U, 4U, NEO::SegmentType::globalConstants, std::numeric_limits::max(), true}; + linkerInput->symbols["C"] = NEO::SymbolInfo{16U, 4U, NEO::SegmentType::instructions, std::numeric_limits::max(), true}; - linkerInput.reset(static_cast *>(buildInfo.linkerInput.release())); + auto relocationType = NEO::LinkerInput::RelocationInfo::Type::address; + linkerInput->textRelocations.push_back({NEO::LinkerInput::RelocationInfo{"A", 8U, relocationType}, + NEO::LinkerInput::RelocationInfo{"B", 16U, relocationType}, + NEO::LinkerInput::RelocationInfo{"C", 24U, relocationType}}); + linkerInput->traits.requiresPatchingOfInstructionSegments = true; + linkerInput->exportedFunctionsSegmentId = 0; - for (size_t i = 0; i < linkerInput->textRelocations.size(); ++i) { - auto expectedPatch = buildInfo.globalSurface->getGpuAddress() + linkerInput->symbols[linkerInput->textRelocations[0][0].symbolName].offset; - auto relocationAddress = kernelHeap.data() + linkerInput->textRelocations[0][0].offset; + MockProgram program{nullptr, false, toClDeviceVector(*device)}; + auto &buildInfo = program.buildInfos[device->getRootDeviceIndex()]; + KernelInfo kernelInfo = {}; + kernelInfo.kernelDescriptor.kernelMetadata.kernelName = "onlyKernel"; + std::vector kernelHeap; + kernelHeap.resize(32, 7); + kernelInfo.heapInfo.pKernelHeap = kernelHeap.data(); + kernelInfo.heapInfo.kernelHeapSize = static_cast(kernelHeap.size()); + MockGraphicsAllocation kernelIsa(kernelHeap.data(), kernelHeap.size()); + kernelInfo.kernelAllocation = &kernelIsa; + program.getKernelInfoArray(rootDeviceIndex).push_back(&kernelInfo); + program.setLinkerInput(device->getRootDeviceIndex(), std::move(linkerInput)); - EXPECT_EQ(static_cast(expectedPatch), *reinterpret_cast(relocationAddress)) << i; + buildInfo.exportedFunctionsSurface = kernelInfo.kernelAllocation; + std::vector globalVariablesBuffer; + globalVariablesBuffer.resize(32, 7); + std::vector globalConstantsBuffer; + globalConstantsBuffer.resize(32, 7); + std::vector globalVariablesInitData{32, 0}; + std::vector globalConstantsInitData{32, 0}; + auto globalSurface = std::make_unique(globalVariablesBuffer.data(), globalVariablesBuffer.size()); + auto constantSurface = std::make_unique(globalConstantsBuffer.data(), globalConstantsBuffer.size()); + + buildInfo.globalSurface = globalSurface.get(); + buildInfo.constantSurface = constantSurface.get(); + + auto ret = program.linkBinary(&pClDevice->getDevice(), globalConstantsInitData.data(), globalConstantsInitData.size(), globalVariablesInitData.data(), globalVariablesInitData.size(), {}, program.externalFunctions); + EXPECT_EQ(CL_SUCCESS, ret); + + linkerInput.reset(static_cast *>(buildInfo.linkerInput.release())); + + for (size_t i = 0; i < linkerInput->textRelocations.size(); ++i) { + auto expectedPatch = buildInfo.globalSurface->getGpuAddress() + linkerInput->symbols[linkerInput->textRelocations[0][0].symbolName].offset; + auto relocationAddress = kernelHeap.data() + linkerInput->textRelocations[0][0].offset; + + EXPECT_EQ(static_cast(expectedPatch), *reinterpret_cast(relocationAddress)) << i; + } + + program.getKernelInfoArray(rootDeviceIndex).clear(); + buildInfo.globalSurface = nullptr; + buildInfo.constantSurface = nullptr; + device->device.getRootDeviceEnvironmentRef().compilerProductHelper.swap(backup); } - - program.getKernelInfoArray(rootDeviceIndex).clear(); - delete buildInfo.globalSurface; - buildInfo.globalSurface = nullptr; - delete buildInfo.constantSurface; - buildInfo.constantSurface = nullptr; } TEST_F(ProgramDataTest, whenRelocationsAreNotNeededThenIsaIsPreserved) { diff --git a/shared/test/common/mocks/CMakeLists.txt b/shared/test/common/mocks/CMakeLists.txt index 35559f8d04..a10bb4989b 100644 --- a/shared/test/common/mocks/CMakeLists.txt +++ b/shared/test/common/mocks/CMakeLists.txt @@ -36,6 +36,7 @@ set(NEO_CORE_tests_mocks ${CMAKE_CURRENT_SOURCE_DIR}/mock_compiler_cache.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_compiler_interface_spirv.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_compiler_interface_spirv.h + ${CMAKE_CURRENT_SOURCE_DIR}/mock_compiler_product_helper.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_compilers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_compilers.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_cpu_page_fault_manager.h diff --git a/shared/test/common/mocks/mock_compiler_product_helper.h b/shared/test/common/mocks/mock_compiler_product_helper.h new file mode 100644 index 0000000000..0fae440968 --- /dev/null +++ b/shared/test/common/mocks/mock_compiler_product_helper.h @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2024 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/source/helpers/compiler_product_helper.h" + +namespace NEO { +template +class MockCompilerProductHelperHeaplessHw : public CompilerProductHelperHw { + public: + bool isHeaplessModeEnabled() const override { + return heaplessModeEnabled; + } + + MockCompilerProductHelperHeaplessHw(bool heaplessModeEnabled) : CompilerProductHelperHw(), heaplessModeEnabled(heaplessModeEnabled) {} + bool heaplessModeEnabled = false; +}; + +} // namespace NEO