forked from oneapi-src/unified-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.cpp
310 lines (267 loc) · 10.6 KB
/
program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//===--------- program.cpp - Native CPU Adapter ---------------------------===//
//
// Copyright (C) 2023 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ur/ur.hpp"
#include "ur_api.h"
#include "common.hpp"
#include "common/ur_util.hpp"
#include "program.hpp"
#include <cstdint>
#include <memory>
UR_APIEXPORT ur_result_t UR_APICALL
urProgramCreateWithIL(ur_context_handle_t hContext, const void *pIL,
size_t length, const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {
std::ignore = hContext;
std::ignore = pIL;
std::ignore = length;
std::ignore = pProperties;
std::ignore = phProgram;
DIE_NO_IMPLEMENTATION
}
static ur_result_t
deserializeWGMetadata(const ur_program_metadata_t &MetadataElement,
native_cpu::WGSize_t &res, std::uint32_t DefaultVal) {
size_t MDElemsSize = MetadataElement.size - sizeof(std::uint64_t);
// Expect between 1 and 3 32-bit integer values.
UR_ASSERT(MDElemsSize == sizeof(std::uint32_t) ||
MDElemsSize == sizeof(std::uint32_t) * 2 ||
MDElemsSize == sizeof(std::uint32_t) * 3,
UR_RESULT_ERROR_INVALID_WORK_GROUP_SIZE);
// Get pointer to data, skipping 64-bit size at the start of the data.
const char *ValuePtr =
reinterpret_cast<const char *>(MetadataElement.value.pData) +
sizeof(std::uint64_t);
// Read values and pad with a default value for missing elements.
std::uint32_t WorkGroupElements[] = {DefaultVal, DefaultVal, DefaultVal};
std::memcpy(WorkGroupElements, ValuePtr, MDElemsSize);
std::get<0>(res) = WorkGroupElements[0];
std::get<1>(res) = WorkGroupElements[1];
std::get<2>(res) = WorkGroupElements[2];
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary(
ur_context_handle_t hContext, uint32_t numDevices,
ur_device_handle_t *phDevices, size_t *pLengths, const uint8_t **ppBinaries,
const ur_program_properties_t *pProperties,
ur_program_handle_t *phProgram) {
if (numDevices > 1)
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
auto hDevice = phDevices[0];
auto pBinary = ppBinaries[0];
std::ignore = pLengths;
std::ignore = pProperties;
UR_ASSERT(hContext, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
UR_ASSERT(hDevice, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
UR_ASSERT(phProgram, UR_RESULT_ERROR_INVALID_NULL_POINTER);
UR_ASSERT(pBinary != nullptr, UR_RESULT_ERROR_INVALID_NULL_POINTER);
auto hProgram = std::make_unique<ur_program_handle_t_>(
hContext, reinterpret_cast<const unsigned char *>(pBinary));
if (pProperties != nullptr) {
for (uint32_t i = 0; i < pProperties->count; i++) {
const auto &mdNode = pProperties->pMetadatas[i];
std::string mdName(mdNode.pName);
auto [Prefix, Tag] = splitMetadataName(mdName);
if (Tag == __SYCL_UR_PROGRAM_METADATA_TAG_REQD_WORK_GROUP_SIZE ||
Tag == __SYCL_UR_PROGRAM_METADATA_TAG_MAX_WORK_GROUP_SIZE) {
bool isReqd =
Tag == __SYCL_UR_PROGRAM_METADATA_TAG_REQD_WORK_GROUP_SIZE;
native_cpu::WGSize_t wgSizeProp;
auto res = deserializeWGMetadata(
mdNode, wgSizeProp,
isReqd ? 1 : std::numeric_limits<std::uint32_t>::max());
if (res != UR_RESULT_SUCCESS) {
return res;
}
(isReqd ? hProgram->KernelReqdWorkGroupSizeMD
: hProgram->KernelMaxWorkGroupSizeMD)[Prefix] =
std::move(wgSizeProp);
} else if (Tag ==
__SYCL_UR_PROGRAM_METADATA_TAG_MAX_LINEAR_WORK_GROUP_SIZE) {
hProgram->KernelMaxLinearWorkGroupSizeMD[Prefix] = mdNode.value.data64;
}
}
}
const nativecpu_program *program =
reinterpret_cast<const nativecpu_program *>(pBinary);
const nativecpu_entry *nativecpu_it = program->entries;
while (nativecpu_it->kernel_ptr != nullptr) {
hProgram->_kernels.insert(
std::make_pair(nativecpu_it->kernelname, nativecpu_it->kernel_ptr));
nativecpu_it++;
}
// Process Native CPU specific properties
const _pi_device_binary_property_set_struct *props = program->properties;
for (auto prop = props->PropertiesBegin; prop != props->PropertiesEnd;
prop++) {
auto [Prefix, Tag] = splitMetadataName(prop->Name);
if (Tag == "@is_nd_range") {
hProgram->KernelIsNDRangeMD[Prefix] = prop->ValSize;
}
}
*phProgram = hProgram.release();
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinaryExp(
ur_context_handle_t, uint32_t, ur_device_handle_t *, size_t *,
const uint8_t **, const ur_program_properties_t *, ur_program_handle_t *) {
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramBuild(ur_context_handle_t hContext,
ur_program_handle_t hProgram,
const char *pOptions) {
std::ignore = hContext;
std::ignore = hProgram;
std::ignore = pOptions;
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramCompile(ur_context_handle_t hContext, ur_program_handle_t hProgram,
const char *pOptions) {
std::ignore = hContext;
std::ignore = hProgram;
std::ignore = pOptions;
// Currently for Native CPU the program is offline compiled, so
// urProgramCompile is a no-op.
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramLink(ur_context_handle_t hContext, uint32_t count,
const ur_program_handle_t *phPrograms, const char *pOptions,
ur_program_handle_t *phProgram) {
if (nullptr != phProgram) {
*phProgram = nullptr;
}
std::ignore = hContext;
std::ignore = count;
std::ignore = phPrograms;
std::ignore = pOptions;
// Currently for Native CPU the program is already linked and all its
// symbols are resolved, so this is a no-op.
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramCompileExp(ur_program_handle_t,
uint32_t,
ur_device_handle_t *,
const char *) {
// Currently for Native CPU the program is offline compiled, so
// urProgramCompile is a no-op.
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramBuildExp(ur_program_handle_t,
uint32_t,
ur_device_handle_t *,
const char *) {
// Currently for Native CPU the program is offline compiled and linked,
// so urProgramBuild is a no-op.
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramLinkExp(
ur_context_handle_t, uint32_t, ur_device_handle_t *, uint32_t,
const ur_program_handle_t *, const char *, ur_program_handle_t *phProgram) {
if (nullptr != phProgram) {
*phProgram = nullptr;
}
// Currently for Native CPU the program is already linked and all its
// symbols are resolved, so this is a no-op.
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramRetain(ur_program_handle_t hProgram) {
hProgram->incrementReferenceCount();
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramRelease(ur_program_handle_t hProgram) {
decrementOrDelete(hProgram);
return UR_RESULT_SUCCESS;
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
ur_device_handle_t hDevice, ur_program_handle_t hProgram,
const char *pFunctionName, void **ppFunctionPointer) {
std::ignore = hDevice;
std::ignore = hProgram;
std::ignore = pFunctionName;
std::ignore = ppFunctionPointer;
DIE_NO_IMPLEMENTATION
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetGlobalVariablePointer(
ur_device_handle_t, ur_program_handle_t hProgram,
const char *pGlobalVariableName, size_t *pGlobalVariableSizeRet,
void **ppGlobalVariablePointerRet) {
std::ignore = hProgram;
std::ignore = pGlobalVariableName;
std::ignore = pGlobalVariableSizeRet;
std::ignore = ppGlobalVariablePointerRet;
DIE_NO_IMPLEMENTATION
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName,
size_t propSize, void *pPropValue, size_t *pPropSizeRet) {
UR_ASSERT(hProgram, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
UrReturnHelper returnValue(propSize, pPropValue, pPropSizeRet);
switch (propName) {
case UR_PROGRAM_INFO_REFERENCE_COUNT:
return returnValue(hProgram->getReferenceCount());
case UR_PROGRAM_INFO_CONTEXT:
return returnValue(nullptr);
case UR_PROGRAM_INFO_NUM_DEVICES:
return returnValue(1u);
case UR_PROGRAM_INFO_DEVICES:
return returnValue(hProgram->_ctx->_device);
case UR_PROGRAM_INFO_BINARY_SIZES:
return returnValue("foo");
case UR_PROGRAM_INFO_BINARIES:
return returnValue("foo");
case UR_PROGRAM_INFO_KERNEL_NAMES: {
return returnValue("foo");
}
case UR_PROGRAM_INFO_IL:
return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
default:
break;
}
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
UR_APIEXPORT ur_result_t UR_APICALL
urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice,
ur_program_build_info_t propName, size_t propSize,
void *pPropValue, size_t *pPropSizeRet) {
std::ignore = hProgram;
std::ignore = hDevice;
std::ignore = propName;
std::ignore = propSize;
std::ignore = pPropValue;
std::ignore = pPropSizeRet;
CONTINUE_NO_IMPLEMENTATION
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants(
ur_program_handle_t hProgram, uint32_t count,
const ur_specialization_constant_info_t *pSpecConstants) {
std::ignore = hProgram;
std::ignore = count;
std::ignore = pSpecConstants;
DIE_NO_IMPLEMENTATION
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramGetNativeHandle(
ur_program_handle_t hProgram, ur_native_handle_t *phNativeProgram) {
std::ignore = hProgram;
std::ignore = phNativeProgram;
DIE_NO_IMPLEMENTATION
}
UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle(
ur_native_handle_t hNativeProgram, ur_context_handle_t hContext,
const ur_program_native_properties_t *pProperties,
ur_program_handle_t *phProgram) {
std::ignore = hNativeProgram;
std::ignore = hContext;
std::ignore = pProperties;
std::ignore = phProgram;
DIE_NO_IMPLEMENTATION
}