1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "program_initialization.h"
9 
10 #include "shared/source/compiler_interface/linker.h"
11 #include "shared/source/device/device.h"
12 #include "shared/source/helpers/blit_commands_helper.h"
13 #include "shared/source/helpers/hw_helper.h"
14 #include "shared/source/helpers/string.h"
15 #include "shared/source/memory_manager/memory_manager.h"
16 #include "shared/source/memory_manager/unified_memory_manager.h"
17 #include "shared/source/program/program_info.h"
18 
19 namespace NEO {
20 
allocateGlobalsSurface(NEO::SVMAllocsManager * const svmAllocManager,NEO::Device & device,size_t size,bool constant,LinkerInput * const linkerInput,const void * initData)21 GraphicsAllocation *allocateGlobalsSurface(NEO::SVMAllocsManager *const svmAllocManager, NEO::Device &device, size_t size, bool constant,
22                                            LinkerInput *const linkerInput, const void *initData) {
23     bool globalsAreExported = false;
24     GraphicsAllocation *gpuAllocation = nullptr;
25     auto rootDeviceIndex = device.getRootDeviceIndex();
26     auto deviceBitfield = device.getDeviceBitfield();
27 
28     if (linkerInput != nullptr) {
29         globalsAreExported = constant ? linkerInput->getTraits().exportsGlobalConstants : linkerInput->getTraits().exportsGlobalVariables;
30     }
31 
32     if (globalsAreExported && (svmAllocManager != nullptr)) {
33         NEO::SVMAllocsManager::SvmAllocationProperties svmProps = {};
34         svmProps.coherent = false;
35         svmProps.readOnly = constant;
36         svmProps.hostPtrReadOnly = constant;
37 
38         std::set<uint32_t> rootDeviceIndices;
39         rootDeviceIndices.insert(rootDeviceIndex);
40         std::map<uint32_t, DeviceBitfield> subDeviceBitfields;
41         subDeviceBitfields.insert({rootDeviceIndex, deviceBitfield});
42         auto ptr = svmAllocManager->createSVMAlloc(size, svmProps, rootDeviceIndices, subDeviceBitfields);
43         DEBUG_BREAK_IF(ptr == nullptr);
44         if (ptr == nullptr) {
45             return nullptr;
46         }
47         auto svmAlloc = svmAllocManager->getSVMAlloc(ptr);
48         UNRECOVERABLE_IF(svmAlloc == nullptr);
49         gpuAllocation = svmAlloc->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
50     } else {
51         auto allocationType = constant ? GraphicsAllocation::AllocationType::CONSTANT_SURFACE : GraphicsAllocation::AllocationType::GLOBAL_SURFACE;
52         gpuAllocation = device.getMemoryManager()->allocateGraphicsMemoryWithProperties({rootDeviceIndex,
53                                                                                          true, // allocateMemory
54                                                                                          size, allocationType,
55                                                                                          false, // isMultiStorageAllocation
56                                                                                          deviceBitfield});
57     }
58 
59     if (!gpuAllocation) {
60         return nullptr;
61     }
62 
63     auto &hwInfo = device.getHardwareInfo();
64     auto &helper = HwHelper::get(hwInfo.platform.eRenderCoreFamily);
65 
66     auto success = MemoryTransferHelper::transferMemoryToAllocation(helper.isBlitCopyRequiredForLocalMemory(hwInfo, *gpuAllocation),
67                                                                     device, gpuAllocation, 0, initData, size);
68 
69     UNRECOVERABLE_IF(!success);
70 
71     return gpuAllocation;
72 }
73 
74 } // namespace NEO
75