1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "shared/source/device_binary_format/patchtokens_decoder.h"
9 #include "shared/source/kernel/kernel_descriptor_from_patchtokens.h"
10 #include "shared/source/program/kernel_info.h"
11 #include "shared/source/program/kernel_info_from_patchtokens.h"
12 #include "shared/test/common/helpers/unit_test_helper.h"
13 #include "shared/test/common/mocks/mock_compilers.h"
14 #include "shared/test/common/mocks/mock_elf.h"
15 #include "shared/test/common/test_macros/matchers.h"
16 #include "shared/test/common/test_macros/test.h"
17 #include "shared/test/unit_test/compiler_interface/linker_mock.h"
18 
19 #include "level_zero/core/source/module/module_imp.h"
20 #include "level_zero/core/test/unit_tests/fixtures/module_fixture.h"
21 #include "level_zero/core/test/unit_tests/mocks/mock_l0_debugger.h"
22 #include "level_zero/core/test/unit_tests/mocks/mock_module.h"
23 #include "level_zero/core/test/unit_tests/sources/debugger/l0_debugger_fixture.h"
24 
25 #include "active_debugger_fixture.h"
26 
27 namespace L0 {
28 namespace ult {
29 using DeviceWithDebuggerEnabledTest = Test<ActiveDebuggerFixture>;
TEST_F(DeviceWithDebuggerEnabledTest,givenDebuggingEnabledWhenModuleIsCreatedThenDebugOptionsAreUsed)30 TEST_F(DeviceWithDebuggerEnabledTest, givenDebuggingEnabledWhenModuleIsCreatedThenDebugOptionsAreUsed) {
31     NEO::MockCompilerEnableGuard mock(true);
32     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
33     device->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
34     debugger->isOptDisabled = true;
35 
36     uint8_t binary[10];
37     ze_module_desc_t moduleDesc = {};
38     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
39     moduleDesc.pInputModule = binary;
40     moduleDesc.inputSize = 10;
41 
42     ModuleBuildLog *moduleBuildLog = nullptr;
43 
44     auto module = std::unique_ptr<L0::ModuleImp>(new L0::ModuleImp(deviceL0, moduleBuildLog, ModuleType::User));
45     ASSERT_NE(nullptr, module.get());
46     module->initialize(&moduleDesc, device);
47 
48     EXPECT_TRUE(CompilerOptions::contains(cip->buildInternalOptions, L0::BuildOptions::debugKernelEnable));
49     EXPECT_TRUE(CompilerOptions::contains(cip->buildOptions, NEO::CompilerOptions::generateDebugInfo));
50     EXPECT_TRUE(CompilerOptions::contains(cip->buildOptions, L0::BuildOptions::optDisable));
51 };
52 
TEST_F(DeviceWithDebuggerEnabledTest,GivenDebugVarDebuggerOptDisableZeroWhenOptDisableIsTrueFromDebuggerThenOptDisableIsNotAdded)53 TEST_F(DeviceWithDebuggerEnabledTest, GivenDebugVarDebuggerOptDisableZeroWhenOptDisableIsTrueFromDebuggerThenOptDisableIsNotAdded) {
54     DebugManagerStateRestore dgbRestorer;
55     NEO::DebugManager.flags.DebuggerOptDisable.set(0);
56 
57     NEO::MockCompilerEnableGuard mock(true);
58     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
59     device->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
60     debugger->isOptDisabled = true;
61 
62     uint8_t binary[10];
63     ze_module_desc_t moduleDesc = {};
64     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
65     moduleDesc.pInputModule = binary;
66     moduleDesc.inputSize = 10;
67 
68     ModuleBuildLog *moduleBuildLog = nullptr;
69 
70     auto module = std::unique_ptr<L0::ModuleImp>(new L0::ModuleImp(deviceL0, moduleBuildLog, ModuleType::User));
71     ASSERT_NE(nullptr, module.get());
72     module->initialize(&moduleDesc, device);
73 
74     EXPECT_FALSE(CompilerOptions::contains(cip->buildOptions, L0::BuildOptions::optDisable));
75 };
76 
TEST_F(DeviceWithDebuggerEnabledTest,GivenDebugVarDebuggerOptDisableOneWhenOptDisableIsFalseFromDebuggerThenOptDisableIsAdded)77 TEST_F(DeviceWithDebuggerEnabledTest, GivenDebugVarDebuggerOptDisableOneWhenOptDisableIsFalseFromDebuggerThenOptDisableIsAdded) {
78     DebugManagerStateRestore dgbRestorer;
79     NEO::DebugManager.flags.DebuggerOptDisable.set(1);
80 
81     NEO::MockCompilerEnableGuard mock(true);
82     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
83     device->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
84     debugger->isOptDisabled = false;
85 
86     uint8_t binary[10];
87     ze_module_desc_t moduleDesc = {};
88     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
89     moduleDesc.pInputModule = binary;
90     moduleDesc.inputSize = 10;
91 
92     ModuleBuildLog *moduleBuildLog = nullptr;
93 
94     auto module = std::unique_ptr<L0::ModuleImp>(new L0::ModuleImp(deviceL0, moduleBuildLog, ModuleType::User));
95     ASSERT_NE(nullptr, module.get());
96     module->initialize(&moduleDesc, device);
97 
98     EXPECT_TRUE(CompilerOptions::contains(cip->buildOptions, L0::BuildOptions::optDisable));
99 };
100 
TEST_F(DeviceWithDebuggerEnabledTest,GivenDebuggeableKernelWhenModuleIsInitializedThenDebugEnabledIsTrue)101 TEST_F(DeviceWithDebuggerEnabledTest, GivenDebuggeableKernelWhenModuleIsInitializedThenDebugEnabledIsTrue) {
102     NEO::MockCompilerEnableGuard mock(true);
103     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
104     device->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
105 
106     uint8_t binary[10];
107     ze_module_desc_t moduleDesc = {};
108     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
109     moduleDesc.pInputModule = binary;
110     moduleDesc.inputSize = 10;
111 
112     ModuleBuildLog *moduleBuildLog = nullptr;
113 
114     auto module = std::make_unique<WhiteBox<::L0::Module>>(deviceL0, moduleBuildLog, ModuleType::User);
115     ASSERT_NE(nullptr, module.get());
116 
117     NEO::PatchTokenBinary::KernelFromPatchtokens kernelTokens;
118     iOpenCL::SKernelBinaryHeaderCommon kernelHeader;
119     kernelTokens.header = &kernelHeader;
120     iOpenCL::SPatchAllocateSystemThreadSurface systemThreadSurface = {};
121     systemThreadSurface.Offset = 2;
122     systemThreadSurface.PerThreadSystemThreadSurfaceSize = 3;
123     kernelTokens.tokens.allocateSystemThreadSurface = &systemThreadSurface;
124 
125     auto kernelInfo = std::make_unique<KernelInfo>();
126     populateKernelInfo(*kernelInfo, kernelTokens, sizeof(size_t));
127 
128     module->translationUnit->programInfo.kernelInfos.push_back(kernelInfo.release());
129     module->initialize(&moduleDesc, device);
130 
131     EXPECT_TRUE(module->isDebugEnabled());
132 }
133 
TEST_F(DeviceWithDebuggerEnabledTest,GivenNonDebuggeableKernelWhenModuleIsInitializedThenDebugEnabledIsFalse)134 TEST_F(DeviceWithDebuggerEnabledTest, GivenNonDebuggeableKernelWhenModuleIsInitializedThenDebugEnabledIsFalse) {
135     NEO::MockCompilerEnableGuard mock(true);
136     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
137     device->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
138 
139     uint8_t binary[10];
140     ze_module_desc_t moduleDesc = {};
141     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
142     moduleDesc.pInputModule = binary;
143     moduleDesc.inputSize = 10;
144 
145     ModuleBuildLog *moduleBuildLog = nullptr;
146 
147     auto module = std::make_unique<WhiteBox<::L0::Module>>(deviceL0, moduleBuildLog, ModuleType::User);
148     ASSERT_NE(nullptr, module.get());
149 
150     NEO::PatchTokenBinary::KernelFromPatchtokens kernelTokens;
151     iOpenCL::SKernelBinaryHeaderCommon kernelHeader;
152     kernelTokens.header = &kernelHeader;
153     kernelTokens.tokens.allocateSystemThreadSurface = nullptr;
154 
155     auto kernelInfo = std::make_unique<KernelInfo>();
156     populateKernelInfo(*kernelInfo, kernelTokens, sizeof(size_t));
157 
158     module->translationUnit->programInfo.kernelInfos.push_back(kernelInfo.release());
159     module->initialize(&moduleDesc, device);
160 
161     EXPECT_FALSE(module->isDebugEnabled());
162 }
163 
164 using ModuleWithSLDTest = Test<ModuleFixture>;
165 
TEST_F(ModuleWithSLDTest,GivenNoDebugDataWhenInitializingModuleThenRelocatedDebugDataIsNotCreated)166 TEST_F(ModuleWithSLDTest, GivenNoDebugDataWhenInitializingModuleThenRelocatedDebugDataIsNotCreated) {
167     NEO::MockCompilerEnableGuard mock(true);
168     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
169     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
170     auto debugger = new MockActiveSourceLevelDebugger(new MockOsLibrary);
171     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
172 
173     uint8_t binary[10];
174     ze_module_desc_t moduleDesc = {};
175     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
176     moduleDesc.pInputModule = binary;
177     moduleDesc.inputSize = 10;
178     ModuleBuildLog *moduleBuildLog = nullptr;
179 
180     std::unique_ptr<MockModule> module = std::make_unique<MockModule>(device,
181                                                                       moduleBuildLog,
182                                                                       ModuleType::User);
183     module->translationUnit = std::make_unique<MockModuleTranslationUnit>(device);
184 
185     uint32_t kernelHeap = 0;
186     auto kernelInfo = new KernelInfo();
187     kernelInfo->heapInfo.KernelHeapSize = 1;
188     kernelInfo->heapInfo.pKernelHeap = &kernelHeap;
189 
190     Mock<::L0::Kernel> kernel;
191     kernel.module = module.get();
192     kernel.immutableData.kernelInfo = kernelInfo;
193 
194     kernel.immutableData.surfaceStateHeapSize = 64;
195     kernel.immutableData.surfaceStateHeapTemplate.reset(new uint8_t[64]);
196     kernelInfo->kernelDescriptor.payloadMappings.implicitArgs.systemThreadSurfaceAddress.bindful = 0;
197 
198     module->kernelImmData = &kernel.immutableData;
199     module->translationUnit->programInfo.kernelInfos.push_back(kernelInfo);
200 
201     EXPECT_EQ(nullptr, module->translationUnit->debugData.get());
202     auto result = module->initialize(&moduleDesc, neoDevice);
203     EXPECT_TRUE(result);
204 
205     EXPECT_EQ(nullptr, kernelInfo->kernelDescriptor.external.relocatedDebugData);
206 }
207 
TEST_F(ModuleWithSLDTest,GivenDebugDataWithSingleRelocationWhenInitializingModuleThenRelocatedDebugDataIsNotCreated)208 TEST_F(ModuleWithSLDTest, GivenDebugDataWithSingleRelocationWhenInitializingModuleThenRelocatedDebugDataIsNotCreated) {
209     NEO::MockCompilerEnableGuard mock(true);
210     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
211     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
212     auto debugger = new MockActiveSourceLevelDebugger(new MockOsLibrary);
213     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
214 
215     createKernel();
216 
217     uint8_t binary[10];
218     ze_module_desc_t moduleDesc = {};
219     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
220     moduleDesc.pInputModule = binary;
221     moduleDesc.inputSize = 10;
222     ModuleBuildLog *moduleBuildLog = nullptr;
223 
224     std::unique_ptr<MockModule> moduleMock = std::make_unique<MockModule>(device, moduleBuildLog, ModuleType::User);
225     moduleMock->translationUnit = std::make_unique<MockModuleTranslationUnit>(device);
226 
227     uint32_t kernelHeap = 0;
228     auto kernelInfo = new KernelInfo();
229     kernelInfo->heapInfo.KernelHeapSize = 1;
230     kernelInfo->heapInfo.pKernelHeap = &kernelHeap;
231 
232     Mock<::L0::Kernel> kernelMock;
233     kernelMock.module = moduleMock.get();
234     kernelMock.immutableData.kernelInfo = kernelInfo;
235 
236     kernelMock.immutableData.surfaceStateHeapSize = 64;
237     kernelMock.immutableData.surfaceStateHeapTemplate.reset(new uint8_t[64]);
238     kernelInfo->kernelDescriptor.payloadMappings.implicitArgs.systemThreadSurfaceAddress.bindful = 0;
239 
240     moduleMock->kernelImmData = &kernelMock.immutableData;
241     moduleMock->translationUnit->programInfo.kernelInfos.push_back(kernelInfo);
242 
243     kernelInfo->kernelDescriptor.external.debugData = std::make_unique<NEO::DebugData>();
244     kernelInfo->kernelDescriptor.external.debugData->vIsa = kernel->getKernelDescriptor().external.debugData->vIsa;
245     kernelInfo->kernelDescriptor.external.debugData->vIsaSize = kernel->getKernelDescriptor().external.debugData->vIsaSize;
246     kernelInfo->kernelDescriptor.external.debugData->genIsa = nullptr;
247     kernelInfo->kernelDescriptor.external.debugData->genIsaSize = 0;
248 
249     auto result = moduleMock->initialize(&moduleDesc, neoDevice);
250     EXPECT_TRUE(result);
251 
252     EXPECT_EQ(nullptr, kernelInfo->kernelDescriptor.external.relocatedDebugData);
253 }
254 
TEST_F(ModuleWithSLDTest,GivenDebugDataWithMultipleRelocationsWhenInitializingModuleThenRelocatedDebugDataIsCreated)255 TEST_F(ModuleWithSLDTest, GivenDebugDataWithMultipleRelocationsWhenInitializingModuleThenRelocatedDebugDataIsCreated) {
256     NEO::MockCompilerEnableGuard mock(true);
257     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
258     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->compilerInterface.reset(cip);
259     auto debugger = new MockActiveSourceLevelDebugger(new MockOsLibrary);
260     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
261 
262     uint8_t binary[10];
263     ze_module_desc_t moduleDesc = {};
264     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
265     moduleDesc.pInputModule = binary;
266     moduleDesc.inputSize = 10;
267     ModuleBuildLog *moduleBuildLog = nullptr;
268 
269     std::unique_ptr<MockModule> moduleMock = std::make_unique<MockModule>(device, moduleBuildLog, ModuleType::User);
270     moduleMock->translationUnit = std::make_unique<MockModuleTranslationUnit>(device);
271 
272     uint32_t kernelHeap = 0;
273     auto kernelInfo = new KernelInfo();
274     kernelInfo->heapInfo.KernelHeapSize = 1;
275     kernelInfo->heapInfo.pKernelHeap = &kernelHeap;
276 
277     Mock<::L0::Kernel> kernelMock;
278     kernelMock.module = moduleMock.get();
279     kernelMock.immutableData.kernelInfo = kernelInfo;
280     kernelInfo->kernelDescriptor.payloadMappings.implicitArgs.systemThreadSurfaceAddress.bindful = 0;
281 
282     moduleMock->kernelImmData = &kernelMock.immutableData;
283     moduleMock->translationUnit->programInfo.kernelInfos.push_back(kernelInfo);
284 
285     kernelInfo->kernelDescriptor.external.debugData = std::make_unique<NEO::DebugData>();
286 
287     auto debugData = MockElfEncoder<>::createRelocateableDebugDataElf();
288     kernelInfo->kernelDescriptor.external.debugData->vIsaSize = static_cast<uint32_t>(debugData.size());
289     kernelInfo->kernelDescriptor.external.debugData->vIsa = reinterpret_cast<char *>(debugData.data());
290     kernelInfo->kernelDescriptor.external.debugData->genIsa = nullptr;
291     kernelInfo->kernelDescriptor.external.debugData->genIsaSize = 0;
292 
293     EXPECT_EQ(nullptr, kernelInfo->kernelDescriptor.external.relocatedDebugData);
294 
295     auto result = moduleMock->initialize(&moduleDesc, neoDevice);
296     EXPECT_TRUE(result);
297 
298     EXPECT_NE(nullptr, kernelInfo->kernelDescriptor.external.relocatedDebugData);
299 }
300 
301 using ModuleWithZebinAndSLDTest = Test<ModuleWithZebinFixture>;
TEST_F(ModuleWithZebinAndSLDTest,GivenZebinThenCreateDebugZebinAndPassToSLD)302 TEST_F(ModuleWithZebinAndSLDTest, GivenZebinThenCreateDebugZebinAndPassToSLD) {
303     module->addEmptyZebin();
304 
305     auto debugger = new MockActiveSourceLevelDebugger(new MockOsLibrary);
306     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
307     module->passDebugData();
308 
309     EXPECT_TRUE(module->translationUnit->debugData);
310 }
311 using KernelDebugSurfaceTest = Test<ModuleFixture>;
312 
HWTEST_F(KernelDebugSurfaceTest,givenDebuggerAndBindfulKernelWhenAppendingKernelToCommandListThenBindfulSurfaceStateForDebugSurfaceIsProgrammed)313 HWTEST_F(KernelDebugSurfaceTest, givenDebuggerAndBindfulKernelWhenAppendingKernelToCommandListThenBindfulSurfaceStateForDebugSurfaceIsProgrammed) {
314     NEO::MockCompilerEnableGuard mock(true);
315     using RENDER_SURFACE_STATE = typename FamilyType::RENDER_SURFACE_STATE;
316 
317     auto debugger = MockDebuggerL0Hw<FamilyType>::allocate(neoDevice);
318 
319     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
320 
321     auto debugSurface = neoDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties(
322         {device->getRootDeviceIndex(), true,
323          NEO::SipKernel::maxDbgSurfaceSize,
324          NEO::GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA,
325          false,
326          false,
327          device->getNEODevice()->getDeviceBitfield()});
328     static_cast<L0::DeviceImp *>(device)->setDebugSurface(debugSurface);
329 
330     uint8_t binary[10];
331     ze_module_desc_t moduleDesc = {};
332     moduleDesc.format = ZE_MODULE_FORMAT_NATIVE;
333     moduleDesc.pInputModule = binary;
334     moduleDesc.inputSize = 10;
335     ModuleBuildLog *moduleBuildLog = nullptr;
336 
337     std::unique_ptr<MockModule> module = std::make_unique<MockModule>(device,
338                                                                       moduleBuildLog,
339                                                                       ModuleType::User);
340 
341     module->debugEnabled = true;
342 
343     uint32_t kernelHeap = 0;
344     KernelInfo kernelInfo;
345     kernelInfo.heapInfo.KernelHeapSize = 1;
346     kernelInfo.heapInfo.pKernelHeap = &kernelHeap;
347 
348     Mock<::L0::Kernel> kernel;
349     kernel.module = module.get();
350     kernel.immutableData.kernelInfo = &kernelInfo;
351 
352     ze_kernel_desc_t desc = {};
353 
354     kernel.immutableData.kernelDescriptor->payloadMappings.implicitArgs.systemThreadSurfaceAddress.bindful = sizeof(RENDER_SURFACE_STATE);
355     kernel.immutableData.surfaceStateHeapSize = 2 * sizeof(RENDER_SURFACE_STATE);
356     kernel.immutableData.surfaceStateHeapTemplate.reset(new uint8_t[2 * sizeof(RENDER_SURFACE_STATE)]);
357     module->kernelImmData = &kernel.immutableData;
358 
359     kernel.initialize(&desc);
360 
361     auto debugSurfaceState = reinterpret_cast<RENDER_SURFACE_STATE *>(kernel.surfaceStateHeapData.get());
362     debugSurfaceState = ptrOffset(debugSurfaceState, sizeof(RENDER_SURFACE_STATE));
363 
364     SURFACE_STATE_BUFFER_LENGTH length;
365     length.Length = static_cast<uint32_t>(debugSurface->getUnderlyingBufferSize() - 1);
366 
367     EXPECT_EQ(length.SurfaceState.Depth + 1u, debugSurfaceState->getDepth());
368     EXPECT_EQ(length.SurfaceState.Width + 1u, debugSurfaceState->getWidth());
369     EXPECT_EQ(length.SurfaceState.Height + 1u, debugSurfaceState->getHeight());
370     EXPECT_EQ(debugSurface->getGpuAddress(), debugSurfaceState->getSurfaceBaseAddress());
371 
372     EXPECT_EQ(RENDER_SURFACE_STATE::SURFACE_TYPE_SURFTYPE_BUFFER, debugSurfaceState->getSurfaceType());
373 }
374 
375 using ModuleWithDebuggerL0Test = Test<L0DebuggerHwFixture>;
TEST_F(ModuleWithDebuggerL0Test,givenDebuggingEnabledWhenModuleIsCreatedThenDebugOptionsAreNotUsed)376 TEST_F(ModuleWithDebuggerL0Test, givenDebuggingEnabledWhenModuleIsCreatedThenDebugOptionsAreNotUsed) {
377     NEO::MockCompilerEnableGuard mock(true);
378     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
379     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[neoDevice->getRootDeviceIndex()]->compilerInterface.reset(cip);
380 
381     uint8_t binary[10];
382     ze_module_desc_t moduleDesc = {};
383     moduleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
384     moduleDesc.pInputModule = binary;
385     moduleDesc.inputSize = 10;
386 
387     ModuleBuildLog *moduleBuildLog = nullptr;
388 
389     auto module = std::unique_ptr<L0::ModuleImp>(new L0::ModuleImp(device, moduleBuildLog, ModuleType::User));
390     ASSERT_NE(nullptr, module.get());
391     module->initialize(&moduleDesc, neoDevice);
392 
393     EXPECT_TRUE(module->isDebugEnabled());
394 
395     EXPECT_FALSE(CompilerOptions::contains(cip->buildInternalOptions, L0::BuildOptions::debugKernelEnable));
396     EXPECT_FALSE(CompilerOptions::contains(cip->buildOptions, NEO::CompilerOptions::generateDebugInfo));
397     EXPECT_FALSE(CompilerOptions::contains(cip->buildOptions, L0::BuildOptions::optDisable));
398 }
399 
TEST_F(ModuleWithDebuggerL0Test,givenDebuggingEnabledWhenKernelsAreInitializedThenAllocationsAreNotResidentAndNotCopied)400 TEST_F(ModuleWithDebuggerL0Test, givenDebuggingEnabledWhenKernelsAreInitializedThenAllocationsAreNotResidentAndNotCopied) {
401     uint32_t kernelHeap = 0xDEAD;
402     KernelInfo kernelInfo;
403     kernelInfo.heapInfo.KernelHeapSize = 4;
404     kernelInfo.heapInfo.pKernelHeap = &kernelHeap;
405 
406     KernelImmutableData kernelImmutableData(device);
407 
408     memoryOperationsHandler->makeResidentCalledCount = 0;
409     kernelImmutableData.initialize(&kernelInfo, device, 0, nullptr, nullptr, false);
410     EXPECT_EQ(0, memoryOperationsHandler->makeResidentCalledCount);
411 
412     auto isa = kernelImmutableData.getIsaGraphicsAllocation()->getUnderlyingBuffer();
413     EXPECT_THAT(isa, testing::Not(MemCompare(&kernelHeap, sizeof(kernelHeap))));
414 };
415 
416 using ModuleTest = Test<ModuleFixture>;
417 
HWTEST_F(ModuleTest,givenDebuggingEnabledWhenModuleIsCreatedAndFullyLinkedThenIsaAllocationsAreCopiedAndResident)418 HWTEST_F(ModuleTest, givenDebuggingEnabledWhenModuleIsCreatedAndFullyLinkedThenIsaAllocationsAreCopiedAndResident) {
419     NEO::MockCompilerEnableGuard mock(true);
420     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
421     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[neoDevice->getRootDeviceIndex()]->compilerInterface.reset(cip);
422 
423     auto memoryOperationsHandler = new NEO::MockMemoryOperations();
424     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->memoryOperationsInterface.reset(memoryOperationsHandler);
425 
426     auto debugger = MockDebuggerL0Hw<FamilyType>::allocate(neoDevice);
427     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
428 
429     std::string testFile;
430     retrieveBinaryKernelFilenameNoRevision(testFile, binaryFilename + "_", ".bin");
431 
432     size_t size = 0;
433     auto src = loadDataFromFile(testFile.c_str(), size);
434 
435     ASSERT_NE(0u, size);
436     ASSERT_NE(nullptr, src);
437 
438     ze_module_desc_t moduleDesc = {};
439     moduleDesc.format = ZE_MODULE_FORMAT_NATIVE;
440     moduleDesc.pInputModule = reinterpret_cast<const uint8_t *>(src.get());
441     moduleDesc.inputSize = size;
442 
443     ModuleBuildLog *moduleBuildLog = nullptr;
444 
445     auto module = std::unique_ptr<L0::ModuleImp>(new L0::ModuleImp(device, moduleBuildLog, ModuleType::User));
446     ASSERT_NE(nullptr, module.get());
447 
448     memoryOperationsHandler->makeResidentCalledCount = 0;
449 
450     module->initialize(&moduleDesc, neoDevice);
451 
452     EXPECT_EQ(4, memoryOperationsHandler->makeResidentCalledCount);
453 
454     for (auto &ki : module->getKernelImmutableDataVector()) {
455         EXPECT_TRUE(ki->isIsaCopiedToAllocation());
456     }
457 }
458 
HWTEST_F(ModuleTest,givenDebuggingEnabledWhenModuleWithUnresolvedSymbolsIsCreatedThenIsaAllocationsAreNotCopiedAndNotResident)459 HWTEST_F(ModuleTest, givenDebuggingEnabledWhenModuleWithUnresolvedSymbolsIsCreatedThenIsaAllocationsAreNotCopiedAndNotResident) {
460     NEO::MockCompilerEnableGuard mock(true);
461     auto cip = new NEO::MockCompilerInterfaceCaptureBuildOptions();
462     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[neoDevice->getRootDeviceIndex()]->compilerInterface.reset(cip);
463 
464     auto memoryOperationsHandler = new NEO::MockMemoryOperations();
465     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->memoryOperationsInterface.reset(memoryOperationsHandler);
466 
467     auto debugger = MockDebuggerL0Hw<FamilyType>::allocate(neoDevice);
468     neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[0]->debugger.reset(debugger);
469 
470     std::string testFile;
471     retrieveBinaryKernelFilenameNoRevision(testFile, binaryFilename + "_", ".bin");
472 
473     size_t size = 0;
474     auto src = loadDataFromFile(testFile.c_str(), size);
475 
476     ASSERT_NE(0u, size);
477     ASSERT_NE(nullptr, src);
478 
479     ze_module_desc_t moduleDesc = {};
480     moduleDesc.format = ZE_MODULE_FORMAT_NATIVE;
481     moduleDesc.pInputModule = reinterpret_cast<const uint8_t *>(src.get());
482     moduleDesc.inputSize = size;
483 
484     ModuleBuildLog *moduleBuildLog = nullptr;
485 
486     auto module = std::unique_ptr<Module>(new Module(device, moduleBuildLog, ModuleType::User));
487     ASSERT_NE(nullptr, module.get());
488 
489     NEO::Linker::RelocationInfo unresolvedRelocation;
490     unresolvedRelocation.symbolName = "unresolved";
491 
492     auto linkerInput = std::make_unique<::WhiteBox<NEO::LinkerInput>>();
493     linkerInput->dataRelocations.push_back(unresolvedRelocation);
494     linkerInput->traits.requiresPatchingOfGlobalVariablesBuffer = true;
495 
496     module->unresolvedExternalsInfo.push_back({unresolvedRelocation});
497     module->translationUnit->programInfo.linkerInput = std::move(linkerInput);
498 
499     memoryOperationsHandler->makeResidentCalledCount = 0;
500 
501     module->initialize(&moduleDesc, neoDevice);
502 
503     EXPECT_EQ(0, memoryOperationsHandler->makeResidentCalledCount);
504 
505     for (auto &ki : module->getKernelImmutableDataVector()) {
506         EXPECT_FALSE(ki->isIsaCopiedToAllocation());
507     }
508 
509     EXPECT_FALSE(module->isFullyLinked);
510 }
511 
512 } // namespace ult
513 } // namespace L0
514