• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IntelPython / dpctl / 28956617038

08 Jul 2026 03:56PM UTC coverage: 74.758% (-0.9%) from 75.677%
28956617038

push

github

web-flow
Merge pull request #2304 from IntelPython/feature/specialization-constants

Add support for specialization constants

880 of 1248 branches covered (70.51%)

Branch coverage included in aggregate %.

133 of 222 new or added lines in 4 files covered. (59.91%)

1 existing line in 1 file now uncovered.

3370 of 4437 relevant lines covered (75.95%)

262.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

45.09
/libsyclinterface/source/dpctl_sycl_kernel_bundle_interface.cpp
1
//===- dpctl_sycl_kernel_bundle_interface.cpp - Implements C API for
2
//    sycl::kernel_bundle<sycl::bundle_state::executable>  ---------------===//
3
//
4
//                      Data Parallel Control (dpctl)
5
//
6
// Copyright 2020 Intel Corporation
7
//
8
// Licensed under the Apache License, Version 2.0 (the "License");
9
// you may not use this file except in compliance with the License.
10
// You may obtain a copy of the License at
11
//
12
//    http://www.apache.org/licenses/LICENSE-2.0
13
//
14
// Unless required by applicable law or agreed to in writing, software
15
// distributed under the License is distributed on an "AS IS" BASIS,
16
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
// See the License for the specific language governing permissions and
18
// limitations under the License.
19
//
20
//===----------------------------------------------------------------------===//
21
///
22
/// \file
23
/// This file implements the functions declared in
24
/// dpctl_sycl_kernel_bundle_interface.h.
25
///
26
//===----------------------------------------------------------------------===//
27

28
#include "dpctl_sycl_kernel_bundle_interface.h"
29
#include "Config/dpctl_config.h"
30
#include "dpctl_dynamic_lib_helper.h"
31
#include "dpctl_error_handlers.h"
32
#include "dpctl_sycl_type_casters.hpp"
33
#include <CL/cl.h> /* OpenCL headers     */
34
#include <cstdint>
35
#include <sstream>
36
#include <stddef.h>
37
#include <sycl/backend/opencl.hpp>
38
#include <sycl/sycl.hpp> /* Sycl headers       */
39
#include <utility>
40

41
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
42
// Note: include ze_api.h before level_zero.hpp. Make sure clang-format does
43
// not reorder the includes.
44
// clang-format off
45
#include "ze_api.h" /* Level Zero headers */
46
#include <sycl/ext/oneapi/backend/level_zero.hpp>
47
// clang-format on
48
#endif
49

50
using namespace sycl;
51

52
namespace
53
{
54
static_assert(__SYCL_COMPILER_VERSION >= __SYCL_COMPILER_VERSION_REQUIRED,
55
              "The compiler does not meet minimum version requirement");
56

57
using namespace dpctl::syclinterface;
58

59
#ifdef __linux__
60
static const char *clLoaderName = DPCTL_LIBCL_LOADER_FILENAME;
61
static const int clLibLoadFlags = RTLD_NOLOAD | RTLD_NOW | RTLD_LOCAL;
62
#elif defined(_WIN64)
63
static const char *clLoaderName = "OpenCL.dll";
64
static const int clLibLoadFlags = 0;
65
#else
66
#error "OpenCL program compilation is unavailable for this platform"
67
#endif
68

69
#define CodeStringSuffix(code)                                                 \
70
    std::string(" (code=") + std::to_string(static_cast<int>(code)) + ")"
6✔
71

72
#define EnumCaseString(code)                                                   \
73
    case code:                                                                 \
2✔
74
        return std::string(#code) + CodeStringSuffix(code)
2✔
75

76
static constexpr backend cl_be = backend::opencl;
77

78
struct cl_loader
79
{
80
public:
81
    static cl_loader &get()
82
    {
11✔
83
        static cl_loader _loader;
11✔
84
        return _loader;
11✔
85
    }
11✔
86

87
    template <typename retTy> retTy getSymbol(const char *name)
88
    {
11✔
89
        if (!opened) {
11!
90
            error_handler("The OpenCL loader dynamic library could not "
×
91
                          "be opened.",
×
92
                          __FILE__, __func__, __LINE__);
×
93

94
            return nullptr;
×
95
        }
×
96
        return clLib.getSymbol<retTy>(name);
11✔
97
    }
11✔
98

99
private:
100
    dpctl::DynamicLibHelper clLib;
101
    bool opened;
102
    cl_loader() : clLib(clLoaderName, clLibLoadFlags), opened(clLib.opened()) {}
2✔
103
};
104

105
typedef cl_program (*clCreateProgramWithSourceFT)(cl_context,
106
                                                  cl_uint,
107
                                                  const char **,
108
                                                  const size_t *,
109
                                                  cl_int *);
110
const char *clCreateProgramWithSource_Name = "clCreateProgramWithSource";
111
clCreateProgramWithSourceFT get_clCreateProgramWithSource()
112
{
28✔
113
    static auto st_clCreateProgramWithSourceF =
28✔
114
        cl_loader::get().getSymbol<clCreateProgramWithSourceFT>(
28✔
115
            clCreateProgramWithSource_Name);
28✔
116

117
    return st_clCreateProgramWithSourceF;
28✔
118
}
28✔
119

120
typedef cl_program (*clCreateProgramWithILFT)(cl_context,
121
                                              const void *,
122
                                              size_t,
123
                                              cl_int *);
124
const char *clCreateProgramWithIL_Name = "clCreateProgramWithIL";
125
clCreateProgramWithILFT get_clCreateProgramWithIL()
126
{
66✔
127
    static auto st_clCreateProgramWithILF =
66✔
128
        cl_loader::get().getSymbol<clCreateProgramWithILFT>(
66✔
129
            clCreateProgramWithIL_Name);
66✔
130

131
    return st_clCreateProgramWithILF;
66✔
132
}
66✔
133
typedef cl_int (*clBuildProgramFT)(cl_program,
134
                                   cl_uint,
135
                                   const cl_device_id *,
136
                                   const char *,
137
                                   void (*)(cl_program, void *),
138
                                   void *);
139
const char *clBuildProgram_Name = "clBuildProgram";
140
clBuildProgramFT get_clBuildProgram()
141
{
94✔
142
    static auto st_clBuildProgramF =
94✔
143
        cl_loader::get().getSymbol<clBuildProgramFT>(clBuildProgram_Name);
94✔
144

145
    return st_clBuildProgramF;
94✔
146
}
94✔
147

148
typedef cl_kernel (*clCreateKernelFT)(cl_program, const char *, cl_int *);
149
const char *clCreateKernel_Name = "clCreateKernel";
150
clCreateKernelFT get_clCreateKernel()
151
{
153✔
152
    static auto st_clCreateKernelF =
153✔
153
        cl_loader::get().getSymbol<clCreateKernelFT>(clCreateKernel_Name);
153✔
154

155
    return st_clCreateKernelF;
153✔
156
}
153✔
157

158
std::string _GetErrorCode_ocl_impl(cl_int code)
159
{
2✔
160
    switch (code) {
2✔
161
        EnumCaseString(CL_BUILD_PROGRAM_FAILURE);
2!
162
        EnumCaseString(CL_INVALID_CONTEXT);
×
163
        EnumCaseString(CL_INVALID_DEVICE);
×
164
        EnumCaseString(CL_INVALID_VALUE);
×
165
        EnumCaseString(CL_OUT_OF_RESOURCES);
×
166
        EnumCaseString(CL_OUT_OF_HOST_MEMORY);
×
167
        EnumCaseString(CL_INVALID_OPERATION);
×
168
        EnumCaseString(CL_INVALID_BINARY);
×
169
    default:
×
170
        return "<< ERROR CODE UNRECOGNIZED >>" + CodeStringSuffix(code);
×
171
    }
2✔
172
}
2✔
173

174
typedef cl_int (*clSetProgramSpecializationConstantFT)(cl_program,
175
                                                       cl_uint,
176
                                                       size_t,
177
                                                       const void *);
178
const char *clSetProgramSpecializationConstant_Name =
179
    "clSetProgramSpecializationConstant";
180
clSetProgramSpecializationConstantFT get_clSetProgramSpecializationConstant()
181
{
2✔
182
    static auto st_clSetProgramSpecializationConstantF =
2✔
183
        cl_loader::get().getSymbol<clSetProgramSpecializationConstantFT>(
2✔
184
            clSetProgramSpecializationConstant_Name);
2✔
185

186
    return st_clSetProgramSpecializationConstantF;
2✔
187
}
2✔
188

189
typedef cl_int (*clReleaseProgramFT)(cl_program);
190
const char *clReleaseProgram_Name = "clReleaseProgram";
191
clReleaseProgramFT get_clReleaseProgram()
192
{
2✔
193
    static auto st_clReleaseProgramF =
2✔
194
        cl_loader::get().getSymbol<clReleaseProgramFT>(clReleaseProgram_Name);
2✔
195

196
    return st_clReleaseProgramF;
2✔
197
}
2✔
198

199
DPCTLSyclKernelBundleRef
200
_CreateKernelBundle_common_ocl_impl(cl_program clProgram,
201
                                    const context &ctx,
202
                                    const device &dev,
203
                                    const char *CompileOpts)
204
{
94✔
205
    backend_traits<cl_be>::return_type<device> clDevice;
94✔
206
    clDevice = get_native<cl_be>(dev);
94✔
207

208
    // Last two pointers are notification function pointer and user-data pointer
209
    // that can be passed to the notification function.
210
    auto clBuildProgramF = get_clBuildProgram();
94✔
211
    if (clBuildProgramF == nullptr) {
94!
NEW
212
        auto clReleaseProgramF = get_clReleaseProgram();
×
NEW
213
        if (clReleaseProgramF) {
×
NEW
214
            clReleaseProgramF(clProgram);
×
NEW
215
        }
×
216
        return nullptr;
×
217
    }
×
218
    cl_int build_status =
94✔
219
        clBuildProgramF(clProgram, 1, &clDevice, CompileOpts, nullptr, nullptr);
94✔
220

221
    if (build_status != CL_SUCCESS) {
94✔
222
        error_handler("clBuildProgram failed: " +
2✔
223
                          _GetErrorCode_ocl_impl(build_status),
2✔
224
                      __FILE__, __func__, __LINE__);
2✔
225
        auto clReleaseProgramF = get_clReleaseProgram();
2✔
226
        if (clReleaseProgramF) {
2!
227
            clReleaseProgramF(clProgram);
2✔
228
        }
2✔
229
        return nullptr;
2✔
230
    }
2✔
231

232
    using ekbTy = kernel_bundle<bundle_state::executable>;
92✔
233
    const ekbTy &kb =
92✔
234
        make_kernel_bundle<cl_be, bundle_state::executable>(clProgram, ctx);
92✔
235
    return wrap<ekbTy>(new ekbTy(kb));
92✔
236
}
94✔
237

238
DPCTLSyclKernelBundleRef
239
_CreateKernelBundleWithOCLSource_ocl_impl(const context &ctx,
240
                                          const device &dev,
241
                                          const char *oclSrc,
242
                                          const char *CompileOpts)
243
{
28✔
244
    auto clCreateProgramWithSourceF = get_clCreateProgramWithSource();
28✔
245
    if (clCreateProgramWithSourceF == nullptr) {
28!
246
        return nullptr;
×
247
    }
×
248

249
    backend_traits<cl_be>::return_type<context> clContext;
28✔
250
    clContext = get_native<cl_be>(ctx);
28✔
251

252
    cl_int build_with_source_err_code = CL_SUCCESS;
28✔
253
    cl_program clProgram = clCreateProgramWithSourceF(
28✔
254
        clContext, 1, &oclSrc, nullptr, &build_with_source_err_code);
28✔
255

256
    if (build_with_source_err_code != CL_SUCCESS) {
28!
257
        error_handler("clPCreateProgramWithSource failed with " +
×
258
                          _GetErrorCode_ocl_impl(build_with_source_err_code),
×
259
                      __FILE__, __func__, __LINE__);
×
260
        return nullptr;
×
261
    }
×
262

263
    return _CreateKernelBundle_common_ocl_impl(clProgram, ctx, dev,
28✔
264
                                               CompileOpts);
28✔
265
}
28✔
266

267
DPCTLSyclKernelBundleRef
268
_CreateKernelBundleWithIL_ocl_impl(const context &ctx,
269
                                   const device &dev,
270
                                   const void *IL,
271
                                   size_t il_length,
272
                                   const char *CompileOpts,
273
                                   size_t NumSpecConsts,
274
                                   const DPCTLSpecConst *SpecConsts)
275
{
66✔
276
    auto clCreateProgramWithILF = get_clCreateProgramWithIL();
66✔
277
    if (clCreateProgramWithILF == nullptr) {
66!
278
        return nullptr;
×
279
    }
×
280

281
    backend_traits<cl_be>::return_type<context> clContext;
66✔
282
    clContext = get_native<cl_be>(ctx);
66✔
283

284
    if (NumSpecConsts > 0 && SpecConsts == nullptr) {
66!
NEW
285
        error_handler("NumSpecConsts > 0 but SpecConsts is NULL.", __FILE__,
×
NEW
286
                      __func__, __LINE__);
×
NEW
287
        return nullptr;
×
NEW
288
    }
×
289

290
    cl_int create_err_code = CL_SUCCESS;
66✔
291
    cl_program clProgram =
66✔
292
        clCreateProgramWithILF(clContext, IL, il_length, &create_err_code);
66✔
293

294
    if (create_err_code != CL_SUCCESS) {
66!
295
        error_handler("OpenCL program could not be created from the SPIR-V "
×
296
                      "binary. OpenCL Error " +
×
297
                          _GetErrorCode_ocl_impl(create_err_code),
×
298
                      __FILE__, __func__, __LINE__);
×
299
        return nullptr;
×
300
    }
×
301

302
    if (SpecConsts != nullptr && NumSpecConsts > 0) {
66!
303
        auto clSetProgramSpecConstF = get_clSetProgramSpecializationConstant();
2✔
304
        if (clSetProgramSpecConstF) {
2!
305
            for (size_t i = 0; i < NumSpecConsts; ++i) {
6✔
306
                cl_int spec_err = clSetProgramSpecConstF(
4✔
307
                    clProgram, SpecConsts[i].id, SpecConsts[i].size,
4✔
308
                    SpecConsts[i].value);
4✔
309
                if (spec_err != CL_SUCCESS) {
4!
NEW
310
                    error_handler(
×
NEW
311
                        "clSetProgramSpecializationConstant failed for "
×
NEW
312
                        "spec constant id " +
×
NEW
313
                            std::to_string(SpecConsts[i].id) +
×
NEW
314
                            ". OpenCL Error " +
×
NEW
315
                            _GetErrorCode_ocl_impl(spec_err),
×
NEW
316
                        __FILE__, __func__, __LINE__);
×
317

NEW
318
                    auto clReleaseProgramF = get_clReleaseProgram();
×
NEW
319
                    if (clReleaseProgramF) {
×
NEW
320
                        clReleaseProgramF(clProgram);
×
NEW
321
                    }
×
322

NEW
323
                    return nullptr;
×
NEW
324
                }
×
325
            }
4✔
326
        }
2✔
NEW
327
        else {
×
NEW
328
            error_handler("clSetProgramSpecializationConstant is not available "
×
NEW
329
                          "in the OpenCL implementation.",
×
NEW
330
                          __FILE__, __func__, __LINE__);
×
331

NEW
332
            auto clReleaseProgramF = get_clReleaseProgram();
×
NEW
333
            if (clReleaseProgramF) {
×
NEW
334
                clReleaseProgramF(clProgram);
×
NEW
335
            }
×
336

NEW
337
            return nullptr;
×
NEW
338
        }
×
339
    }
2✔
340

341
    return _CreateKernelBundle_common_ocl_impl(clProgram, ctx, dev,
66✔
342
                                               CompileOpts);
66✔
343
}
66✔
344

345
bool _HasKernel_ocl_impl(const kernel_bundle<bundle_state::executable> &kb,
346
                         const char *kernel_name)
347
{
62✔
348
    auto clCreateKernelF = get_clCreateKernel();
62✔
349
    if (clCreateKernelF == nullptr) {
62!
350
        return false;
×
351
    }
×
352

353
    std::vector<cl_program> oclKB = get_native<cl_be>(kb);
62✔
354

355
    bool found = false;
62✔
356
    for (auto &cl_pr : oclKB) {
62!
357
        cl_int create_kernel_err_code = CL_SUCCESS;
62✔
358
        [[maybe_unused]] cl_kernel try_kern =
62✔
359
            clCreateKernelF(cl_pr, kernel_name, &create_kernel_err_code);
62✔
360
        if (create_kernel_err_code == CL_SUCCESS) {
62!
361
            found = true;
62✔
362
            break;
62✔
363
        }
62✔
364
    }
62✔
365
    return found;
62✔
366
}
62✔
367

368
__dpctl_give DPCTLSyclKernelRef
369
_GetKernel_ocl_impl(const kernel_bundle<bundle_state::executable> &kb,
370
                    const char *kernel_name)
371
{
91✔
372
    auto clCreateKernelF = get_clCreateKernel();
91✔
373
    if (clCreateKernelF == nullptr) {
91!
374
        return nullptr;
×
375
    }
×
376

377
    std::vector<cl_program> oclKB = get_native<cl_be>(kb);
91✔
378

379
    bool found = false;
91✔
380
    cl_kernel ocl_kernel_from_kb;
91✔
381
    for (auto &cl_pr : oclKB) {
91✔
382
        cl_int create_kernel_err_code = CL_SUCCESS;
91✔
383
        cl_kernel try_kern =
91✔
384
            clCreateKernelF(cl_pr, kernel_name, &create_kernel_err_code);
91✔
385
        if (create_kernel_err_code == CL_SUCCESS) {
91✔
386
            found = true;
90✔
387
            ocl_kernel_from_kb = try_kern;
90✔
388
            break;
90✔
389
        }
90✔
390
    }
91✔
391
    if (found) {
91✔
392
        try {
90✔
393
            context ctx = kb.get_context();
90✔
394

395
            const kernel &interop_kernel =
90✔
396
                make_kernel<cl_be>(ocl_kernel_from_kb, ctx);
90✔
397

398
            return wrap<kernel>(new kernel(interop_kernel));
90✔
399
        } catch (std::exception const &e) {
90✔
400
            error_handler(e, __FILE__, __func__, __LINE__);
×
401
            return nullptr;
×
402
        }
×
403
    }
90✔
404
    else {
1✔
405
        error_handler("Kernel " + std::string(kernel_name) + " not found.",
1✔
406
                      __FILE__, __func__, __LINE__);
1✔
407
        return nullptr;
1✔
408
    }
1✔
409
}
91✔
410

411
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
412

413
#ifdef __linux__
414
static const char *zeLoaderName = DPCTL_LIBZE_LOADER_FILENAME;
415
static const int zeLibLoadFlags = RTLD_NOLOAD | RTLD_NOW | RTLD_LOCAL;
416
#elif defined(_WIN64)
417
static const char *zeLoaderName = "ze_loader.dll";
418
static const int zeLibLoadFlags = 0;
419
#else
420
#error "Level Zero program compilation is unavailable for this platform"
421
#endif
422

423
static constexpr sycl::backend ze_be = sycl::backend::ext_oneapi_level_zero;
424

425
struct ze_loader
426
{
427
public:
428
    static ze_loader &get()
429
    {
×
430
        static ze_loader _loader;
×
431
        return _loader;
×
432
    }
×
433

434
    template <typename retTy> retTy getSymbol(const char *name)
435
    {
×
436
        if (!opened) {
×
437
            error_handler("The Level-Zero loader dynamic library could not "
×
438
                          "be opened.",
×
439
                          __FILE__, __func__, __LINE__);
×
440

441
            return nullptr;
×
442
        }
×
443
        return zeLib.getSymbol<retTy>(name);
×
444
    }
×
445

446
private:
447
    dpctl::DynamicLibHelper zeLib;
448
    bool opened;
449
    ze_loader() : zeLib(zeLoaderName, zeLibLoadFlags), opened(zeLib.opened()) {}
×
450
};
451

452
typedef ze_result_t (*zeModuleCreateFT)(ze_context_handle_t,
453
                                        ze_device_handle_t,
454
                                        const ze_module_desc_t *,
455
                                        ze_module_handle_t *,
456
                                        ze_module_build_log_handle_t *);
457
const char *zeModuleCreate_Name = "zeModuleCreate";
458
zeModuleCreateFT get_zeModuleCreate()
459
{
×
460
    static auto st_zeModuleCreateF =
×
461
        ze_loader::get().getSymbol<zeModuleCreateFT>(zeModuleCreate_Name);
×
462

463
    return st_zeModuleCreateF;
×
464
}
×
465

466
typedef ze_result_t (*zeModuleDestroyFT)(ze_module_handle_t);
467
const char *zeModuleDestroy_Name = "zeModuleDestroy";
468
zeModuleDestroyFT get_zeModuleDestroy()
469
{
×
470
    static auto st_zeModuleDestroyF =
×
471
        ze_loader::get().getSymbol<zeModuleDestroyFT>(zeModuleDestroy_Name);
×
472

473
    return st_zeModuleDestroyF;
×
474
}
×
475

476
typedef ze_result_t (*zeKernelCreateFT)(ze_module_handle_t,
477
                                        const ze_kernel_desc_t *,
478
                                        ze_kernel_handle_t *);
479
const char *zeKernelCreate_Name = "zeKernelCreate";
480
zeKernelCreateFT get_zeKernelCreate()
481
{
×
482
    static auto st_zeKernelCreateF =
×
483
        ze_loader::get().getSymbol<zeKernelCreateFT>(zeKernelCreate_Name);
×
484

485
    return st_zeKernelCreateF;
×
486
}
×
487

488
std::string _GetErrorCode_ze_impl(ze_result_t code)
489
{
×
490
    switch (code) {
×
491
        EnumCaseString(ZE_RESULT_ERROR_UNINITIALIZED);
×
492
        EnumCaseString(ZE_RESULT_ERROR_DEVICE_LOST);
×
493
        EnumCaseString(ZE_RESULT_ERROR_INVALID_NULL_HANDLE);
×
494
        EnumCaseString(ZE_RESULT_ERROR_INVALID_NULL_POINTER);
×
495
        EnumCaseString(ZE_RESULT_ERROR_INVALID_ENUMERATION);
×
496
        EnumCaseString(ZE_RESULT_ERROR_INVALID_NATIVE_BINARY);
×
497
        EnumCaseString(ZE_RESULT_ERROR_INVALID_SIZE);
×
498
        EnumCaseString(ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY);
×
499
        EnumCaseString(ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY);
×
500
        EnumCaseString(ZE_RESULT_ERROR_MODULE_BUILD_FAILURE);
×
501
        EnumCaseString(ZE_RESULT_ERROR_INVALID_MODULE_UNLINKED);
×
502
    default:
×
503
        return "<< UNRECOGNIZED ZE_RESULT_T CODE >> " + CodeStringSuffix(code);
×
504
    }
×
505
}
×
506

507
__dpctl_give DPCTLSyclKernelBundleRef
508
_CreateKernelBundleWithIL_ze_impl(const context &SyclCtx,
509
                                  const device &SyclDev,
510
                                  const void *IL,
511
                                  size_t il_length,
512
                                  const char *CompileOpts,
513
                                  size_t NumSpecConsts,
514
                                  const DPCTLSpecConst *SpecConsts)
515
{
×
516
    auto zeModuleCreateFn = get_zeModuleCreate();
×
517
    if (zeModuleCreateFn == nullptr) {
×
518
        error_handler("ZeModuleCreateFn is invalid.", __FILE__, __func__,
×
519
                      __LINE__);
×
520
        return nullptr;
×
521
    }
×
522

523
    backend_traits<ze_be>::return_type<context> ZeContext;
×
524
    ZeContext = get_native<ze_be>(SyclCtx);
×
525

526
    backend_traits<ze_be>::return_type<device> ZeDevice;
×
527
    ZeDevice = get_native<ze_be>(SyclDev);
×
528

NEW
529
    if (NumSpecConsts > 0 && SpecConsts == nullptr) {
×
NEW
530
        error_handler("NumSpecConsts > 0 but SpecConsts is NULL.", __FILE__,
×
NEW
531
                      __func__, __LINE__);
×
NEW
532
        return nullptr;
×
NEW
533
    }
×
534

NEW
535
    std::vector<std::uint32_t> spec_ids;
×
NEW
536
    std::vector<const void *> spec_values;
×
537

NEW
538
    if (SpecConsts != nullptr && NumSpecConsts > 0) {
×
NEW
539
        spec_ids.reserve(NumSpecConsts);
×
NEW
540
        spec_values.reserve(NumSpecConsts);
×
NEW
541
        for (size_t i = 0; i < NumSpecConsts; ++i) {
×
NEW
542
            spec_ids.push_back(SpecConsts[i].id);
×
NEW
543
            spec_values.push_back(SpecConsts[i].value);
×
NEW
544
        }
×
NEW
545
    }
×
546
    ze_module_constants_t ZeSpecConstants = {};
×
NEW
547
    ZeSpecConstants.numConstants = static_cast<std::uint32_t>(spec_ids.size());
×
NEW
548
    ZeSpecConstants.pConstantIds = spec_ids.empty() ? nullptr : spec_ids.data();
×
NEW
549
    ZeSpecConstants.pConstantValues =
×
NEW
550
        spec_values.empty() ? nullptr : spec_values.data();
×
551

552
    // Populate the Level Zero module descriptions
553
    ze_module_desc_t ZeModuleDesc = {};
×
554
    ZeModuleDesc.stype = ZE_STRUCTURE_TYPE_MODULE_DESC;
×
555
    ZeModuleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
×
556
    ZeModuleDesc.inputSize = il_length;
×
557
    ZeModuleDesc.pInputModule = (uint8_t *)IL;
×
558
    ZeModuleDesc.pBuildFlags = CompileOpts;
×
559
    ZeModuleDesc.pConstants = &ZeSpecConstants;
×
560

561
    ze_module_handle_t ZeModule;
×
562

563
    auto ret_code = zeModuleCreateFn(ZeContext, ZeDevice, &ZeModuleDesc,
×
564
                                     &ZeModule, nullptr);
×
565
    if (ret_code != ZE_RESULT_SUCCESS) {
×
566
        error_handler("Module creation failed " +
×
567
                          _GetErrorCode_ze_impl(ret_code),
×
568
                      __FILE__, __func__, __LINE__);
×
569
        return nullptr;
×
570
    }
×
571

572
    try {
×
573
        const auto &kb = make_kernel_bundle<ze_be, bundle_state::executable>(
×
574
            {ZeModule, ext::oneapi::level_zero::ownership::keep}, SyclCtx);
×
575

576
        return wrap<kernel_bundle<bundle_state::executable>>(
×
577
            new kernel_bundle<bundle_state::executable>(kb));
×
578
    } catch (std::exception const &e) {
×
579
        error_handler(e, __FILE__, __func__, __LINE__);
×
580
        auto zeModuleDestroyFn = get_zeModuleDestroy();
×
581
        if (zeModuleDestroyFn) {
×
582
            zeModuleDestroyFn(ZeModule);
×
583
        }
×
584
        return nullptr;
×
585
    }
×
586
}
×
587

588
__dpctl_give DPCTLSyclKernelRef
589
_GetKernel_ze_impl(const kernel_bundle<bundle_state::executable> &kb,
590
                   const char *kernel_name)
591
{
×
592
    auto zeKernelCreateFn = get_zeKernelCreate();
×
593
    if (zeKernelCreateFn == nullptr) {
×
594
        error_handler("Could not load zeKernelCreate function.", __FILE__,
×
595
                      __func__, __LINE__);
×
596
        return nullptr;
×
597
    }
×
598

599
    auto ZeKernelBundle = sycl::get_native<ze_be>(kb);
×
600
    bool found = false;
×
601

602
    // Populate the Level Zero kernel descriptions
603
    ze_kernel_desc_t ZeKernelDescr = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr,
×
604
                                      0, // flags
×
605
                                      kernel_name};
×
606

607
    std::unique_ptr<sycl::kernel> syclInteropKern_ptr;
×
608
    ze_kernel_handle_t ZeKern;
×
609
    for (auto &ZeM : ZeKernelBundle) {
×
610
        ze_result_t ze_status = zeKernelCreateFn(ZeM, &ZeKernelDescr, &ZeKern);
×
611

612
        if (ze_status == ZE_RESULT_SUCCESS) {
×
613
            found = true;
×
614
            const auto &ctx = kb.get_context();
×
615
            const auto &k = make_kernel<ze_be>(
×
616
                {kb, ZeKern, ext::oneapi::level_zero::ownership::keep}, ctx);
×
617
            syclInteropKern_ptr = std::unique_ptr<kernel>(new kernel(k));
×
618
            break;
×
619
        }
×
620
        else {
×
621
            if (ze_status != ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
×
622
                error_handler("zeKernelCreate failed: " +
×
623
                                  _GetErrorCode_ze_impl(ze_status),
×
624
                              __FILE__, __func__, __LINE__);
×
625
                return nullptr;
×
626
            }
×
627
        }
×
628
    }
×
629

630
    if (found) {
×
631
        return wrap<kernel>(new kernel(*syclInteropKern_ptr));
×
632
    }
×
633
    else {
×
634
        error_handler("Kernel named " + std::string(kernel_name) +
×
635
                          " could not be found.",
×
636
                      __FILE__, __func__, __LINE__, error_level::error);
×
637
        return nullptr;
×
638
    }
×
639
}
×
640

641
bool _HasKernel_ze_impl(const kernel_bundle<bundle_state::executable> &kb,
642
                        const char *kernel_name)
643
{
×
644
    auto zeKernelCreateFn = get_zeKernelCreate();
×
645
    if (zeKernelCreateFn == nullptr) {
×
646
        error_handler("Could not load zeKernelCreate function.", __FILE__,
×
647
                      __func__, __LINE__, error_level::error);
×
648
        return false;
×
649
    }
×
650

651
    auto ZeKernelBundle = sycl::get_native<ze_be>(kb);
×
652

653
    // Populate the Level Zero kernel descriptions
654
    ze_kernel_desc_t ZeKernelDescr = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr,
×
655
                                      0, // flags
×
656
                                      kernel_name};
×
657

658
    std::unique_ptr<sycl::kernel> syclInteropKern_ptr;
×
659
    ze_kernel_handle_t ZeKern;
×
660
    for (auto &ZeM : ZeKernelBundle) {
×
661
        ze_result_t ze_status = zeKernelCreateFn(ZeM, &ZeKernelDescr, &ZeKern);
×
662

663
        if (ze_status == ZE_RESULT_SUCCESS) {
×
664
            return true;
×
665
        }
×
666
        else {
×
667
            if (ze_status != ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
×
668
                error_handler("zeKernelCreate failed: " +
×
669
                                  _GetErrorCode_ze_impl(ze_status),
×
670
                              __FILE__, __func__, __LINE__, error_level::error);
×
671
                return false;
×
672
            }
×
673
        }
×
674
    }
×
675

676
    return false;
×
677
}
×
678

679
#endif /* #ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION */
680

681
} /* end of anonymous namespace */
682

683
__dpctl_give DPCTLSyclKernelBundleRef
684
DPCTLKernelBundle_CreateFromSpirv(__dpctl_keep const DPCTLSyclContextRef CtxRef,
685
                                  __dpctl_keep const DPCTLSyclDeviceRef DevRef,
686
                                  __dpctl_keep const void *IL,
687
                                  size_t length,
688
                                  const char *CompileOpts,
689
                                  size_t NumSpecConsts,
690
                                  const DPCTLSpecConst *SpecConsts)
691
{
75✔
692
    DPCTLSyclKernelBundleRef KBRef = nullptr;
75✔
693
    if (!CtxRef) {
75✔
694
        error_handler("Cannot create program from SPIR-V as the supplied SYCL "
3✔
695
                      "context is NULL.",
3✔
696
                      __FILE__, __func__, __LINE__);
3✔
697
        return KBRef;
3✔
698
    }
3✔
699
    if (!DevRef) {
72✔
700
        error_handler("Cannot create program from SPIR-V as the supplied SYCL "
3✔
701
                      "device is NULL.",
3✔
702
                      __FILE__, __func__, __LINE__);
3✔
703
        return KBRef;
3✔
704
    }
3✔
705
    if ((!IL) || (length == 0)) {
69!
706
        error_handler("Cannot create program from null SPIR-V buffer.",
3✔
707
                      __FILE__, __func__, __LINE__);
3✔
708
        return KBRef;
3✔
709
    }
3✔
710

711
    context *SyclCtx = unwrap<context>(CtxRef);
66✔
712
    device *SyclDev = unwrap<device>(DevRef);
66✔
713
    // get the backend type
714
    try {
66✔
715
        auto BE = SyclCtx->get_platform().get_backend();
66✔
716
        switch (BE) {
66✔
717
        case backend::opencl:
66!
718
            KBRef = _CreateKernelBundleWithIL_ocl_impl(
66✔
719
                *SyclCtx, *SyclDev, IL, length, CompileOpts, NumSpecConsts,
66✔
720
                SpecConsts);
66✔
721
            break;
66✔
NEW
722
        case backend::ext_oneapi_level_zero:
×
723
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
NEW
724
            KBRef = _CreateKernelBundleWithIL_ze_impl(
×
NEW
725
                *SyclCtx, *SyclDev, IL, length, CompileOpts, NumSpecConsts,
×
NEW
726
                SpecConsts);
×
NEW
727
            break;
×
728
#endif
×
NEW
729
        default:
×
NEW
730
            std::ostringstream os;
×
NEW
731
            os << "Backend " << BE << " is not supported";
×
NEW
732
            error_handler(os.str(), __FILE__, __func__, __LINE__);
×
NEW
733
            break;
×
734
        }
66✔
735
    } catch (std::exception const &e) {
66✔
NEW
736
        error_handler(e, __FILE__, __func__, __LINE__);
×
NEW
737
        return nullptr;
×
UNCOV
738
    }
×
739
    return KBRef;
66✔
740
}
66✔
741

742
__dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromOCLSource(
743
    __dpctl_keep const DPCTLSyclContextRef Ctx,
744
    __dpctl_keep const DPCTLSyclDeviceRef Dev,
745
    __dpctl_keep const char *Source,
746
    __dpctl_keep const char *CompileOpts)
747
{
39✔
748
    context *SyclCtx = nullptr;
39✔
749
    device *SyclDev = nullptr;
39✔
750

751
    if (!Ctx) {
39✔
752
        error_handler("Input Ctx is nullptr.", __FILE__, __func__, __LINE__);
10✔
753
        return nullptr;
10✔
754
    }
10✔
755
    if (!Dev) {
29✔
756
        error_handler("Input Dev is nullptr.", __FILE__, __func__, __LINE__);
1✔
757
        return nullptr;
1✔
758
    }
1✔
759
    if (!Source) {
28!
760
        error_handler("Input Source is nullptr.", __FILE__, __func__, __LINE__);
×
761
        return nullptr;
×
762
    }
×
763

764
    SyclCtx = unwrap<context>(Ctx);
28✔
765
    SyclDev = unwrap<device>(Dev);
28✔
766

767
    // get the backend type
768
    auto BE = SyclCtx->get_platform().get_backend();
28✔
769
    switch (BE) {
28✔
770
    case backend::opencl:
28!
771
        try {
28✔
772
            return _CreateKernelBundleWithOCLSource_ocl_impl(
28✔
773
                *SyclCtx, *SyclDev, Source, CompileOpts);
28✔
774
        } catch (std::exception const &e) {
28✔
775
            error_handler(e, __FILE__, __func__, __LINE__);
×
776
            return nullptr;
×
777
        }
×
778
        break;
×
779
    case backend::ext_oneapi_level_zero:
×
780
        error_handler(
×
781
            "CreateFromSource is not supported for Level Zero backend.",
×
782
            __FILE__, __func__, __LINE__);
×
783
        return nullptr;
×
784
    default:
×
785
        error_handler("CreateFromSource is not supported in unknown backend.",
×
786
                      __FILE__, __func__, __LINE__);
×
787
        return nullptr;
×
788
    }
28✔
789
}
28✔
790

791
__dpctl_give DPCTLSyclKernelRef
792
DPCTLKernelBundle_GetKernel(__dpctl_keep DPCTLSyclKernelBundleRef KBRef,
793
                            __dpctl_keep const char *KernelName)
794
{
115✔
795
    if (!KBRef) {
115✔
796
        error_handler("Input KBRef is nullptr", __FILE__, __func__, __LINE__);
21✔
797
        return nullptr;
21✔
798
    }
21✔
799
    if (!KernelName) {
94✔
800
        error_handler("Input KernelName is nullptr", __FILE__, __func__,
3✔
801
                      __LINE__);
3✔
802
        return nullptr;
3✔
803
    }
3✔
804
    auto SyclKB = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
91✔
805
    sycl::backend be = SyclKB->get_backend();
91✔
806
    switch (be) {
91✔
807
    case sycl::backend::opencl:
91!
808
        return _GetKernel_ocl_impl(*SyclKB, KernelName);
91✔
809
    case sycl::backend::ext_oneapi_level_zero:
×
810
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
811
        return _GetKernel_ze_impl(*SyclKB, KernelName);
×
812
#endif
×
813
    default:
×
814
        std::ostringstream os;
×
815
        os << "Backend " << be << " is not supported";
×
816
        error_handler(os.str(), __FILE__, __func__, __LINE__);
×
817
        return nullptr;
×
818
    }
91✔
819
}
91✔
820

821
bool DPCTLKernelBundle_HasKernel(__dpctl_keep DPCTLSyclKernelBundleRef KBRef,
822
                                 __dpctl_keep const char *KernelName)
823
{
68✔
824
    if (!KBRef) {
68✔
825
        error_handler("Input KBRef is nullptr", __FILE__, __func__, __LINE__);
3✔
826
        return false;
3✔
827
    }
3✔
828
    if (!KernelName) {
65✔
829
        error_handler("Input KernelName is nullptr", __FILE__, __func__,
3✔
830
                      __LINE__);
3✔
831
        return false;
3✔
832
    }
3✔
833

834
    auto SyclKB = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
62✔
835
    sycl::backend be = SyclKB->get_backend();
62✔
836
    switch (be) {
62✔
837
    case sycl::backend::opencl:
62!
838
        return _HasKernel_ocl_impl(*SyclKB, KernelName);
62✔
839
    case sycl::backend::ext_oneapi_level_zero:
×
840
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
841
        return _HasKernel_ze_impl(*SyclKB, KernelName);
×
842
#endif
×
843
    default:
×
844
        std::ostringstream os;
×
845
        os << "Backend " << be << " is not supported";
×
846
        error_handler(os.str(), __FILE__, __func__, __LINE__);
×
847
        return false;
×
848
    }
62✔
849
}
62✔
850

851
void DPCTLKernelBundle_Delete(__dpctl_take DPCTLSyclKernelBundleRef KBRef)
852
{
106✔
853
    delete unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
106✔
854
}
106✔
855

856
__dpctl_give DPCTLSyclKernelBundleRef
857
DPCTLKernelBundle_Copy(__dpctl_keep const DPCTLSyclKernelBundleRef KBRef)
858
{
8✔
859
    auto Bundle = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
8✔
860
    if (!Bundle) {
8✔
861
        error_handler(
3✔
862
            "Cannot copy DPCTLSyclKernelBundleRef as input is a nullptr",
3✔
863
            __FILE__, __func__, __LINE__);
3✔
864
        return nullptr;
3✔
865
    }
3✔
866
    try {
5✔
867
        auto CopiedBundle =
5✔
868
            new kernel_bundle<bundle_state::executable>(*Bundle);
5✔
869
        return wrap<kernel_bundle<bundle_state::executable>>(CopiedBundle);
5✔
870
    } catch (std::exception const &e) {
5✔
871
        error_handler(e, __FILE__, __func__, __LINE__);
×
872
        return nullptr;
×
873
    }
×
874
}
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc