/* * Copyright (C) 2020 Intel Corporation * * SPDX-License-Identifier: MIT * */ #include "shared/source/program/program_initialization.h" #include "shared/test/unit_test/compiler_interface/linker_mock.h" #include "opencl/test/unit_test/mocks/mock_device.h" #include "opencl/test/unit_test/mocks/mock_memory_manager.h" #include "opencl/test/unit_test/mocks/mock_svm_manager.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreNotExportedThenMemoryIsAllocatedAsNonSvmAllocation) { MockDevice device; if (0 == device.getDeviceInfo().svmCapabilities) { return; } MockSVMAllocsManager svmAllocsManager(device.getMemoryManager()); WhiteBox emptyLinkerInput; LinkerInput *linkerInput; std::vector initData; initData.resize(64, 7U); bool constant = true; GraphicsAllocation *alloc = nullptr; alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = true, linkerInput = nullptr, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_EQ(GraphicsAllocation::AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = false, linkerInput = nullptr, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = true, linkerInput = &emptyLinkerInput, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_EQ(GraphicsAllocation::AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = false, linkerInput = &emptyLinkerInput, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); } TEST(AllocateGlobalSurfaceTest, GivenSvmAllocsManagerWhenGlobalsAreExportedThenMemoryIsAllocatedAsSvmAllocation) { MockDevice device; if (0 == device.getDeviceInfo().svmCapabilities) { return; } MockMemoryManager memoryManager; MockSVMAllocsManager svmAllocsManager(&memoryManager); WhiteBox linkerInputExportGlobalVariables; WhiteBox linkerInputExportGlobalConstants; linkerInputExportGlobalVariables.traits.exportsGlobalVariables = true; linkerInputExportGlobalConstants.traits.exportsGlobalConstants = true; LinkerInput *linkerInput; std::vector initData; initData.resize(64, 7U); bool constant = true; GraphicsAllocation *alloc = nullptr; alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = true, linkerInput = &linkerInputExportGlobalConstants, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); ASSERT_NE(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_FALSE(alloc->isMemObjectsAllocationWithWritableFlags()); svmAllocsManager.freeSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress()))); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = true, linkerInput = &linkerInputExportGlobalVariables, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = false, linkerInput = &linkerInputExportGlobalConstants, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = false, linkerInput = &linkerInputExportGlobalVariables, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_NE(nullptr, svmAllocsManager.getSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress())))); EXPECT_TRUE(alloc->isMemObjectsAllocationWithWritableFlags()); svmAllocsManager.freeSVMAlloc(reinterpret_cast(static_cast(alloc->getGpuAddress()))); } TEST(AllocateGlobalSurfaceTest, GivenNullSvmAllocsManagerWhenGlobalsAreExportedThenMemoryIsAllocatedAsNonSvmAllocation) { MockDevice device; WhiteBox linkerInputExportGlobalVariables; WhiteBox linkerInputExportGlobalConstants; linkerInputExportGlobalVariables.traits.exportsGlobalVariables = true; linkerInputExportGlobalConstants.traits.exportsGlobalConstants = true; LinkerInput *linkerInput; std::vector initData; initData.resize(64, 7U); bool constant = true; GraphicsAllocation *alloc = nullptr; alloc = allocateGlobalsSurface(nullptr, device, initData.size(), constant = true, linkerInput = &linkerInputExportGlobalConstants, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(GraphicsAllocation::AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(nullptr, device, initData.size(), constant = true, linkerInput = &linkerInputExportGlobalVariables, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(GraphicsAllocation::AllocationType::CONSTANT_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(nullptr, device, initData.size(), constant = false, linkerInput = &linkerInputExportGlobalConstants, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); alloc = allocateGlobalsSurface(nullptr, device, initData.size(), constant = false, linkerInput = &linkerInputExportGlobalVariables, initData.data()); ASSERT_NE(nullptr, alloc); ASSERT_EQ(initData.size(), alloc->getUnderlyingBufferSize()); EXPECT_EQ(0, memcmp(alloc->getUnderlyingBuffer(), initData.data(), initData.size())); EXPECT_EQ(GraphicsAllocation::AllocationType::GLOBAL_SURFACE, alloc->getAllocationType()); device.getMemoryManager()->freeGraphicsMemory(alloc); } TEST(AllocateGlobalSurfaceTest, WhenGlobalsAreNotExportedAndAllocationFailsThenGracefullyReturnsNullptr) { MockDevice device; auto memoryManager = std::make_unique(); memoryManager->failInAllocateWithSizeAndAlignment = true; device.injectMemoryManager(memoryManager.release()); MockSVMAllocsManager mockSvmAllocsManager(device.getMemoryManager()); WhiteBox emptyLinkerInput; LinkerInput *linkerInput; SVMAllocsManager *svmAllocsManager = nullptr; std::vector initData; initData.resize(64, 7U); bool constant = true; GraphicsAllocation *alloc = nullptr; alloc = allocateGlobalsSurface(svmAllocsManager = &mockSvmAllocsManager, device, initData.size(), constant = true, linkerInput = nullptr, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = &mockSvmAllocsManager, device, initData.size(), constant = false, linkerInput = nullptr, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = &mockSvmAllocsManager, device, initData.size(), constant = true, linkerInput = &emptyLinkerInput, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = &mockSvmAllocsManager, device, initData.size(), constant = false, linkerInput = &emptyLinkerInput, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = nullptr, device, initData.size(), constant = true, linkerInput = nullptr, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = nullptr, device, initData.size(), constant = false, linkerInput = nullptr, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = nullptr, device, initData.size(), constant = true, linkerInput = &emptyLinkerInput, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(svmAllocsManager = nullptr, device, initData.size(), constant = false, linkerInput = &emptyLinkerInput, initData.data()); EXPECT_EQ(nullptr, alloc); } TEST(AllocateGlobalSurfaceTest, WhenGlobalsAreExportedAndAllocationFailsThenGracefullyReturnsNullptr) { MockDevice device; MockMemoryManager memoryManager; MockSVMAllocsManager svmAllocsManager(&memoryManager); memoryManager.failInAllocateWithSizeAndAlignment = true; WhiteBox linkerInputExportGlobalVariables; WhiteBox linkerInputExportGlobalConstants; linkerInputExportGlobalVariables.traits.exportsGlobalVariables = true; linkerInputExportGlobalConstants.traits.exportsGlobalConstants = true; LinkerInput *linkerInput; std::vector initData; initData.resize(64, 7U); bool constant = true; GraphicsAllocation *alloc = nullptr; alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = true, linkerInput = &linkerInputExportGlobalConstants, initData.data()); EXPECT_EQ(nullptr, alloc); alloc = allocateGlobalsSurface(&svmAllocsManager, device, initData.size(), constant = false, linkerInput = &linkerInputExportGlobalVariables, initData.data()); EXPECT_EQ(nullptr, alloc); }