Skip to content

Commit 835a1da

Browse files
Update kernel allocation when substitute kernel heap
Change-Id: Iee02a93d4e10c7b32fae56ffa61c90d8617d6ec9
1 parent 9bc11a7 commit 835a1da

File tree

8 files changed

+219
-16
lines changed

8 files changed

+219
-16
lines changed

runtime/kernel/kernel.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,16 @@ void Kernel::substituteKernelHeap(void *newKernelHeap, size_t newKernelHeapSize)
697697
SKernelBinaryHeaderCommon *pHeader = const_cast<SKernelBinaryHeaderCommon *>(pKernelInfo->heapInfo.pKernelHeader);
698698
pHeader->KernelHeapSize = static_cast<uint32_t>(newKernelHeapSize);
699699
pKernelInfo->isKernelHeapSubstituted = true;
700+
701+
auto currentAllocationSize = pKernelInfo->kernelAllocation->getUnderlyingBufferSize();
702+
if (currentAllocationSize >= newKernelHeapSize) {
703+
memcpy_s(pKernelInfo->kernelAllocation->getUnderlyingBuffer(), newKernelHeapSize, newKernelHeap, newKernelHeapSize);
704+
} else {
705+
auto memoryManager = device.getMemoryManager();
706+
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(pKernelInfo->kernelAllocation);
707+
pKernelInfo->kernelAllocation = nullptr;
708+
pKernelInfo->createKernelAllocation(memoryManager);
709+
}
700710
}
701711

702712
bool Kernel::isKernelHeapSubstituted() const {

runtime/program/kernel_info.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "runtime/helpers/ptr_math.h"
2828
#include "runtime/mem_obj/buffer.h"
2929
#include "runtime/mem_obj/image.h"
30+
#include "runtime/memory_manager/memory_manager.h"
3031
#include "runtime/kernel/kernel.h"
3132
#include "runtime/sampler/sampler.h"
3233
#include "runtime/helpers/string.h"
@@ -499,4 +500,17 @@ size_t KernelInfo::getBorderColorOffset() const {
499500
uint32_t KernelInfo::getConstantBufferSize() const {
500501
return patchInfo.dataParameterStream ? patchInfo.dataParameterStream->DataParameterStreamSize : 0;
501502
}
503+
504+
bool KernelInfo::createKernelAllocation(MemoryManager *memoryManager) {
505+
UNRECOVERABLE_IF(kernelAllocation);
506+
auto kernelIsaSize = heapInfo.pKernelHeader->KernelHeapSize;
507+
kernelAllocation = memoryManager->createInternalGraphicsAllocation(nullptr, kernelIsaSize);
508+
if (kernelAllocation) {
509+
memcpy_s(kernelAllocation->getUnderlyingBuffer(), kernelIsaSize, heapInfo.pKernelHeap, kernelIsaSize);
510+
} else {
511+
return false;
512+
}
513+
return true;
514+
}
515+
502516
} // namespace OCLRT

runtime/program/kernel_info.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct KernelInfo;
4343
class DispatchInfo;
4444
struct KernelArgumentType;
4545
class GraphicsAllocation;
46+
class MemoryManager;
4647

4748
extern std::unordered_map<std::string, uint32_t> accessQualifierMap;
4849
extern std::unordered_map<std::string, uint32_t> addressQualifierMap;
@@ -219,6 +220,8 @@ struct KernelInfo {
219220
return -1;
220221
}
221222

223+
bool createKernelAllocation(MemoryManager *memoryManager);
224+
222225
std::string name;
223226
std::string attributes;
224227
HeapInfo heapInfo;

runtime/program/process_gen_binary.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -807,15 +807,7 @@ cl_int Program::parsePatchList(KernelInfo &kernelInfo) {
807807
}
808808

809809
if (kernelInfo.heapInfo.pKernelHeader->KernelHeapSize && this->pDevice) {
810-
auto memoryManager = this->pDevice->getMemoryManager();
811-
auto kernelIsaSize = kernelInfo.heapInfo.pKernelHeader->KernelHeapSize;
812-
auto kernelAllocation = memoryManager->createInternalGraphicsAllocation(nullptr, kernelIsaSize);
813-
if (kernelAllocation) {
814-
memcpy_s(kernelAllocation->getUnderlyingBuffer(), kernelIsaSize, kernelInfo.heapInfo.pKernelHeap, kernelIsaSize);
815-
kernelInfo.kernelAllocation = kernelAllocation;
816-
} else {
817-
retVal = CL_OUT_OF_HOST_MEMORY;
818-
}
810+
retVal = kernelInfo.createKernelAllocation(this->pDevice->getMemoryManager()) ? CL_SUCCESS : CL_OUT_OF_HOST_MEMORY;
819811
}
820812

821813
DEBUG_BREAK_IF(kernelInfo.heapInfo.pKernelHeader->KernelHeapSize && !this->pDevice);

unit_tests/gtpin/gtpin_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ TEST_F(GTPinTests, givenKernelThenVerifyThatKernelCodeSubstitutionWorksWell) {
20442044

20452045
// Substitute new kernel code
20462046
constexpr size_t newCodeSize = 64;
2047-
uint8_t newCode[newCodeSize];
2047+
uint8_t newCode[newCodeSize] = {0x0, 0x1, 0x2, 0x3, 0x4};
20482048
pKernel->substituteKernelHeap(&newCode[0], newCodeSize);
20492049

20502050
// Verify that substitution went properly
@@ -2053,6 +2053,10 @@ TEST_F(GTPinTests, givenKernelThenVerifyThatKernelCodeSubstitutionWorksWell) {
20532053
uint8_t *pBin2 = reinterpret_cast<uint8_t *>(const_cast<void *>(pKernel->getKernelHeap()));
20542054
EXPECT_EQ(pBin2, &newCode[0]);
20552055

2056+
auto kernelIsa = pKernel->getKernelInfo().kernelAllocation->getUnderlyingBuffer();
2057+
2058+
EXPECT_EQ(0, memcmp(kernelIsa, newCode, newCodeSize));
2059+
20562060
// Cleanup
20572061
retVal = clReleaseKernel(kernel);
20582062
EXPECT_EQ(CL_SUCCESS, retVal);

unit_tests/kernel/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ set(IGDRCL_SRCS_tests_kernel
4040
${CMAKE_CURRENT_SOURCE_DIR}/kernel_transformable_tests.cpp
4141
${CMAKE_CURRENT_SOURCE_DIR}/debug_kernel_tests.cpp
4242
${CMAKE_CURRENT_SOURCE_DIR}/parent_kernel_tests.cpp
43+
${CMAKE_CURRENT_SOURCE_DIR}/substitute_kernel_heap_tests.cpp
4344
)
4445
target_sources(igdrcl_tests PRIVATE ${IGDRCL_SRCS_tests_kernel})
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include "unit_tests/fixtures/device_fixture.h"
24+
#include "unit_tests/mocks/mock_kernel.h"
25+
#include "test.h"
26+
27+
using namespace OCLRT;
28+
29+
typedef Test<DeviceFixture> KernelSubstituteTest;
30+
31+
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithGreaterSizeThenAllocatesNewKernelAllocation) {
32+
MockKernelWithInternals kernel(*pDevice);
33+
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
34+
const size_t initialHeapSize = 0x40;
35+
pHeader->KernelHeapSize = initialHeapSize;
36+
37+
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
38+
kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager());
39+
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
40+
EXPECT_NE(nullptr, firstAllocation);
41+
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
42+
EXPECT_EQ(initialHeapSize, firstAllocationSize);
43+
44+
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
45+
46+
const size_t newHeapSize = initialHeapSize + 1;
47+
char newHeap[newHeapSize];
48+
49+
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
50+
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
51+
EXPECT_NE(nullptr, secondAllocation);
52+
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
53+
EXPECT_NE(initialHeapSize, secondAllocationSize);
54+
EXPECT_EQ(newHeapSize, secondAllocationSize);
55+
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
56+
57+
EXPECT_NE(firstAllocationId, secondAllocationId);
58+
59+
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
60+
}
61+
62+
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSameSizeThenDoesNotAllocateNewKernelAllocation) {
63+
MockKernelWithInternals kernel(*pDevice);
64+
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
65+
const size_t initialHeapSize = 0x40;
66+
pHeader->KernelHeapSize = initialHeapSize;
67+
68+
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
69+
kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager());
70+
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
71+
EXPECT_NE(nullptr, firstAllocation);
72+
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
73+
EXPECT_EQ(initialHeapSize, firstAllocationSize);
74+
75+
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
76+
77+
const size_t newHeapSize = initialHeapSize;
78+
char newHeap[newHeapSize];
79+
80+
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
81+
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
82+
EXPECT_NE(nullptr, secondAllocation);
83+
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
84+
EXPECT_EQ(initialHeapSize, secondAllocationSize);
85+
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
86+
87+
EXPECT_EQ(firstAllocationId, secondAllocationId);
88+
89+
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
90+
}
91+
92+
TEST_F(KernelSubstituteTest, givenKernelWhenSubstituteKernelHeapWithSmallerSizeThenDoesNotAllocateNewKernelAllocation) {
93+
MockKernelWithInternals kernel(*pDevice);
94+
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
95+
const size_t initialHeapSize = 0x40;
96+
pHeader->KernelHeapSize = initialHeapSize;
97+
98+
EXPECT_EQ(nullptr, kernel.kernelInfo.kernelAllocation);
99+
kernel.kernelInfo.createKernelAllocation(pDevice->getMemoryManager());
100+
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
101+
EXPECT_NE(nullptr, firstAllocation);
102+
auto firstAllocationSize = firstAllocation->getUnderlyingBufferSize();
103+
EXPECT_EQ(initialHeapSize, firstAllocationSize);
104+
105+
auto firstAllocationId = static_cast<MemoryAllocation *>(firstAllocation)->id;
106+
107+
const size_t newHeapSize = initialHeapSize - 1;
108+
char newHeap[newHeapSize];
109+
110+
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
111+
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
112+
EXPECT_NE(nullptr, secondAllocation);
113+
auto secondAllocationSize = secondAllocation->getUnderlyingBufferSize();
114+
EXPECT_EQ(initialHeapSize, secondAllocationSize);
115+
auto secondAllocationId = static_cast<MemoryAllocation *>(secondAllocation)->id;
116+
117+
EXPECT_EQ(firstAllocationId, secondAllocationId);
118+
119+
pDevice->getMemoryManager()->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
120+
}
121+
122+
TEST_F(KernelSubstituteTest, givenKernelWithUsedKernelAllocationWhenSubstituteKernelHeapAndAllocateNewMemoryThenStoreOldAllocationOnTemporaryList) {
123+
MockKernelWithInternals kernel(*pDevice);
124+
auto pHeader = const_cast<SKernelBinaryHeaderCommon *>(kernel.kernelInfo.heapInfo.pKernelHeader);
125+
auto memoryManager = pDevice->getMemoryManager();
126+
127+
const size_t initialHeapSize = 0x40;
128+
pHeader->KernelHeapSize = initialHeapSize;
129+
130+
kernel.kernelInfo.createKernelAllocation(memoryManager);
131+
auto firstAllocation = kernel.kernelInfo.kernelAllocation;
132+
133+
firstAllocation->taskCount = ObjectNotUsed - 1;
134+
135+
const size_t newHeapSize = initialHeapSize + 1;
136+
char newHeap[newHeapSize];
137+
138+
EXPECT_TRUE(memoryManager->graphicsAllocations.peekIsEmpty());
139+
140+
kernel.mockKernel->substituteKernelHeap(newHeap, newHeapSize);
141+
auto secondAllocation = kernel.kernelInfo.kernelAllocation;
142+
143+
EXPECT_FALSE(memoryManager->graphicsAllocations.peekIsEmpty());
144+
EXPECT_EQ(memoryManager->graphicsAllocations.peekHead(), firstAllocation);
145+
memoryManager->checkGpuUsageAndDestroyGraphicsAllocations(secondAllocation);
146+
memoryManager->cleanAllocationList(firstAllocation->taskCount, TEMPORARY_ALLOCATION);
147+
}

unit_tests/program/kernel_info_tests.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,12 @@
2121
*/
2222

2323
#include "runtime/program/kernel_info.h"
24+
#include "runtime/memory_manager/os_agnostic_memory_manager.h"
2425
#include "gtest/gtest.h"
2526
#include <type_traits>
2627
#include <memory>
2728

28-
using OCLRT::KernelInfo;
29-
using OCLRT::SPatchStatelessConstantMemoryObjectKernelArgument;
30-
using OCLRT::SPatchStatelessGlobalMemoryObjectKernelArgument;
31-
using OCLRT::SPatchGlobalMemoryObjectKernelArgument;
32-
using OCLRT::SPatchImageMemoryObjectKernelArgument;
33-
using OCLRT::SPatchSamplerKernelArgument;
29+
using namespace OCLRT;
3430

3531
TEST(KernelInfo, NonCopyable) {
3632
EXPECT_FALSE(std::is_move_constructible<KernelInfo>::value);
@@ -129,6 +125,42 @@ TEST(KernelInfo, decodeImageKernelArgument) {
129125
delete pKernelInfo;
130126
}
131127

128+
TEST(KernelInfoTest, givenKernelInfoWhenCreateKernelAllocationThenCopyWholeKernelHeapToKernelAllocation) {
129+
KernelInfo kernelInfo;
130+
OsAgnosticMemoryManager memoryManager;
131+
SKernelBinaryHeaderCommon kernelHeader;
132+
const size_t heapSize = 0x40;
133+
char heap[heapSize];
134+
kernelHeader.KernelHeapSize = heapSize;
135+
kernelInfo.heapInfo.pKernelHeader = &kernelHeader;
136+
kernelInfo.heapInfo.pKernelHeap = &heap;
137+
138+
for (size_t i = 0; i < heapSize; i++) {
139+
heap[i] = static_cast<char>(i);
140+
}
141+
142+
auto retVal = kernelInfo.createKernelAllocation(&memoryManager);
143+
EXPECT_TRUE(retVal);
144+
auto allocation = kernelInfo.kernelAllocation;
145+
EXPECT_EQ(0, memcmp(allocation->getUnderlyingBuffer(), heap, heapSize));
146+
EXPECT_EQ(heapSize, allocation->getUnderlyingBufferSize());
147+
memoryManager.checkGpuUsageAndDestroyGraphicsAllocations(allocation);
148+
}
149+
150+
class MyMemoryManager : public OsAgnosticMemoryManager {
151+
public:
152+
GraphicsAllocation *createInternalGraphicsAllocation(const void *ptr, size_t allocationSize) override { return nullptr; }
153+
};
154+
155+
TEST(KernelInfoTest, givenKernelInfoWhenCreateKernelAllocationAndCannotAllocateMemoryThenReturnsFalse) {
156+
KernelInfo kernelInfo;
157+
MyMemoryManager memoryManager;
158+
SKernelBinaryHeaderCommon kernelHeader;
159+
kernelInfo.heapInfo.pKernelHeader = &kernelHeader;
160+
auto retVal = kernelInfo.createKernelAllocation(&memoryManager);
161+
EXPECT_FALSE(retVal);
162+
}
163+
132164
TEST(KernelInfo, decodeGlobalMemObjectKernelArgument) {
133165
uint32_t argumentNumber = 1;
134166
KernelInfo *pKernelInfo = KernelInfo::create();

0 commit comments

Comments
 (0)