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

IntelPython / dpctl / 28771100431

06 Jul 2026 05:59AM UTC coverage: 75.205% (-0.5%) from 75.677%
28771100431

Pull #2304

github

web-flow
Merge bbb322af3 into 7e85c9dfb
Pull Request #2304: Add support for specialization constants

870 of 1222 branches covered (71.19%)

Branch coverage included in aggregate %.

115 of 172 new or added lines in 4 files covered. (66.86%)

3358 of 4400 relevant lines covered (76.32%)

265.45 hits per line

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

45.69
/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
    cl_int create_err_code = CL_SUCCESS;
66✔
285
    cl_program clProgram =
66✔
286
        clCreateProgramWithILF(clContext, IL, il_length, &create_err_code);
66✔
287

288
    if (create_err_code != CL_SUCCESS) {
66!
289
        error_handler("OpenCL program could not be created from the SPIR-V "
×
290
                      "binary. OpenCL Error " +
×
291
                          _GetErrorCode_ocl_impl(create_err_code),
×
292
                      __FILE__, __func__, __LINE__);
×
293
        return nullptr;
×
294
    }
×
295

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

NEW
312
                    auto clReleaseProgramF = get_clReleaseProgram();
×
NEW
313
                    if (clReleaseProgramF) {
×
NEW
314
                        clReleaseProgramF(clProgram);
×
NEW
315
                    }
×
316

NEW
317
                    return nullptr;
×
NEW
318
                }
×
319
            }
4✔
320
        }
2✔
NEW
321
        else {
×
NEW
322
            error_handler("clSetProgramSpecializationConstant is not available "
×
NEW
323
                          "in the OpenCL implementation.",
×
NEW
324
                          __FILE__, __func__, __LINE__);
×
325

NEW
326
            auto clReleaseProgramF = get_clReleaseProgram();
×
NEW
327
            if (clReleaseProgramF) {
×
NEW
328
                clReleaseProgramF(clProgram);
×
NEW
329
            }
×
330

NEW
331
            return nullptr;
×
NEW
332
        }
×
333
    }
2✔
334

335
    return _CreateKernelBundle_common_ocl_impl(clProgram, ctx, dev,
66✔
336
                                               CompileOpts);
66✔
337
}
66✔
338

339
bool _HasKernel_ocl_impl(const kernel_bundle<bundle_state::executable> &kb,
340
                         const char *kernel_name)
341
{
62✔
342
    auto clCreateKernelF = get_clCreateKernel();
62✔
343
    if (clCreateKernelF == nullptr) {
62!
344
        return false;
×
345
    }
×
346

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

349
    bool found = false;
62✔
350
    for (auto &cl_pr : oclKB) {
62!
351
        cl_int create_kernel_err_code = CL_SUCCESS;
62✔
352
        [[maybe_unused]] cl_kernel try_kern =
62✔
353
            clCreateKernelF(cl_pr, kernel_name, &create_kernel_err_code);
62✔
354
        if (create_kernel_err_code == CL_SUCCESS) {
62!
355
            found = true;
62✔
356
            break;
62✔
357
        }
62✔
358
    }
62✔
359
    return found;
62✔
360
}
62✔
361

362
__dpctl_give DPCTLSyclKernelRef
363
_GetKernel_ocl_impl(const kernel_bundle<bundle_state::executable> &kb,
364
                    const char *kernel_name)
365
{
91✔
366
    auto clCreateKernelF = get_clCreateKernel();
91✔
367
    if (clCreateKernelF == nullptr) {
91!
368
        return nullptr;
×
369
    }
×
370

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

373
    bool found = false;
91✔
374
    cl_kernel ocl_kernel_from_kb;
91✔
375
    for (auto &cl_pr : oclKB) {
91✔
376
        cl_int create_kernel_err_code = CL_SUCCESS;
91✔
377
        cl_kernel try_kern =
91✔
378
            clCreateKernelF(cl_pr, kernel_name, &create_kernel_err_code);
91✔
379
        if (create_kernel_err_code == CL_SUCCESS) {
91✔
380
            found = true;
90✔
381
            ocl_kernel_from_kb = try_kern;
90✔
382
            break;
90✔
383
        }
90✔
384
    }
91✔
385
    if (found) {
91✔
386
        try {
90✔
387
            context ctx = kb.get_context();
90✔
388

389
            const kernel &interop_kernel =
90✔
390
                make_kernel<cl_be>(ocl_kernel_from_kb, ctx);
90✔
391

392
            return wrap<kernel>(new kernel(interop_kernel));
90✔
393
        } catch (std::exception const &e) {
90✔
394
            error_handler(e, __FILE__, __func__, __LINE__);
×
395
            return nullptr;
×
396
        }
×
397
    }
90✔
398
    else {
1✔
399
        error_handler("Kernel " + std::string(kernel_name) + " not found.",
1✔
400
                      __FILE__, __func__, __LINE__);
1✔
401
        return nullptr;
1✔
402
    }
1✔
403
}
91✔
404

405
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
406

407
#ifdef __linux__
408
static const char *zeLoaderName = DPCTL_LIBZE_LOADER_FILENAME;
409
static const int zeLibLoadFlags = RTLD_NOLOAD | RTLD_NOW | RTLD_LOCAL;
410
#elif defined(_WIN64)
411
static const char *zeLoaderName = "ze_loader.dll";
412
static const int zeLibLoadFlags = 0;
413
#else
414
#error "Level Zero program compilation is unavailable for this platform"
415
#endif
416

417
static constexpr sycl::backend ze_be = sycl::backend::ext_oneapi_level_zero;
418

419
struct ze_loader
420
{
421
public:
422
    static ze_loader &get()
423
    {
×
424
        static ze_loader _loader;
×
425
        return _loader;
×
426
    }
×
427

428
    template <typename retTy> retTy getSymbol(const char *name)
429
    {
×
430
        if (!opened) {
×
431
            error_handler("The Level-Zero loader dynamic library could not "
×
432
                          "be opened.",
×
433
                          __FILE__, __func__, __LINE__);
×
434

435
            return nullptr;
×
436
        }
×
437
        return zeLib.getSymbol<retTy>(name);
×
438
    }
×
439

440
private:
441
    dpctl::DynamicLibHelper zeLib;
442
    bool opened;
443
    ze_loader() : zeLib(zeLoaderName, zeLibLoadFlags), opened(zeLib.opened()) {}
×
444
};
445

446
typedef ze_result_t (*zeModuleCreateFT)(ze_context_handle_t,
447
                                        ze_device_handle_t,
448
                                        const ze_module_desc_t *,
449
                                        ze_module_handle_t *,
450
                                        ze_module_build_log_handle_t *);
451
const char *zeModuleCreate_Name = "zeModuleCreate";
452
zeModuleCreateFT get_zeModuleCreate()
453
{
×
454
    static auto st_zeModuleCreateF =
×
455
        ze_loader::get().getSymbol<zeModuleCreateFT>(zeModuleCreate_Name);
×
456

457
    return st_zeModuleCreateF;
×
458
}
×
459

460
typedef ze_result_t (*zeModuleDestroyFT)(ze_module_handle_t);
461
const char *zeModuleDestroy_Name = "zeModuleDestroy";
462
zeModuleDestroyFT get_zeModuleDestroy()
463
{
×
464
    static auto st_zeModuleDestroyF =
×
465
        ze_loader::get().getSymbol<zeModuleDestroyFT>(zeModuleDestroy_Name);
×
466

467
    return st_zeModuleDestroyF;
×
468
}
×
469

470
typedef ze_result_t (*zeKernelCreateFT)(ze_module_handle_t,
471
                                        const ze_kernel_desc_t *,
472
                                        ze_kernel_handle_t *);
473
const char *zeKernelCreate_Name = "zeKernelCreate";
474
zeKernelCreateFT get_zeKernelCreate()
475
{
×
476
    static auto st_zeKernelCreateF =
×
477
        ze_loader::get().getSymbol<zeKernelCreateFT>(zeKernelCreate_Name);
×
478

479
    return st_zeKernelCreateF;
×
480
}
×
481

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

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

517
    backend_traits<ze_be>::return_type<context> ZeContext;
×
518
    ZeContext = get_native<ze_be>(SyclCtx);
×
519

520
    backend_traits<ze_be>::return_type<device> ZeDevice;
×
521
    ZeDevice = get_native<ze_be>(SyclDev);
×
522

NEW
523
    std::vector<std::uint32_t> spec_ids;
×
NEW
524
    std::vector<const void *> spec_values;
×
525

NEW
526
    if (SpecConsts != nullptr && NumSpecConsts > 0) {
×
NEW
527
        spec_ids.reserve(NumSpecConsts);
×
NEW
528
        spec_values.reserve(NumSpecConsts);
×
NEW
529
        for (size_t i = 0; i < NumSpecConsts; ++i) {
×
NEW
530
            spec_ids.push_back(SpecConsts[i].id);
×
NEW
531
            spec_values.push_back(SpecConsts[i].value);
×
NEW
532
        }
×
NEW
533
    }
×
534
    ze_module_constants_t ZeSpecConstants = {};
×
NEW
535
    ZeSpecConstants.numConstants = static_cast<std::uint32_t>(NumSpecConsts);
×
NEW
536
    ZeSpecConstants.pConstantIds = spec_ids.empty() ? nullptr : spec_ids.data();
×
NEW
537
    ZeSpecConstants.pConstantValues =
×
NEW
538
        spec_values.empty() ? nullptr : spec_values.data();
×
539

540
    // Populate the Level Zero module descriptions
541
    ze_module_desc_t ZeModuleDesc = {};
×
542
    ZeModuleDesc.stype = ZE_STRUCTURE_TYPE_MODULE_DESC;
×
543
    ZeModuleDesc.format = ZE_MODULE_FORMAT_IL_SPIRV;
×
544
    ZeModuleDesc.inputSize = il_length;
×
545
    ZeModuleDesc.pInputModule = (uint8_t *)IL;
×
546
    ZeModuleDesc.pBuildFlags = CompileOpts;
×
547
    ZeModuleDesc.pConstants = &ZeSpecConstants;
×
548

549
    ze_module_handle_t ZeModule;
×
550

551
    auto ret_code = zeModuleCreateFn(ZeContext, ZeDevice, &ZeModuleDesc,
×
552
                                     &ZeModule, nullptr);
×
553
    if (ret_code != ZE_RESULT_SUCCESS) {
×
554
        error_handler("Module creation failed " +
×
555
                          _GetErrorCode_ze_impl(ret_code),
×
556
                      __FILE__, __func__, __LINE__);
×
557
        return nullptr;
×
558
    }
×
559

560
    try {
×
561
        const auto &kb = make_kernel_bundle<ze_be, bundle_state::executable>(
×
562
            {ZeModule, ext::oneapi::level_zero::ownership::keep}, SyclCtx);
×
563

564
        return wrap<kernel_bundle<bundle_state::executable>>(
×
565
            new kernel_bundle<bundle_state::executable>(kb));
×
566
    } catch (std::exception const &e) {
×
567
        error_handler(e, __FILE__, __func__, __LINE__);
×
568
        auto zeModuleDestroyFn = get_zeModuleDestroy();
×
569
        if (zeModuleDestroyFn) {
×
570
            zeModuleDestroyFn(ZeModule);
×
571
        }
×
572
        return nullptr;
×
573
    }
×
574
}
×
575

576
__dpctl_give DPCTLSyclKernelRef
577
_GetKernel_ze_impl(const kernel_bundle<bundle_state::executable> &kb,
578
                   const char *kernel_name)
579
{
×
580
    auto zeKernelCreateFn = get_zeKernelCreate();
×
581
    if (zeKernelCreateFn == nullptr) {
×
582
        error_handler("Could not load zeKernelCreate function.", __FILE__,
×
583
                      __func__, __LINE__);
×
584
        return nullptr;
×
585
    }
×
586

587
    auto ZeKernelBundle = sycl::get_native<ze_be>(kb);
×
588
    bool found = false;
×
589

590
    // Populate the Level Zero kernel descriptions
591
    ze_kernel_desc_t ZeKernelDescr = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr,
×
592
                                      0, // flags
×
593
                                      kernel_name};
×
594

595
    std::unique_ptr<sycl::kernel> syclInteropKern_ptr;
×
596
    ze_kernel_handle_t ZeKern;
×
597
    for (auto &ZeM : ZeKernelBundle) {
×
598
        ze_result_t ze_status = zeKernelCreateFn(ZeM, &ZeKernelDescr, &ZeKern);
×
599

600
        if (ze_status == ZE_RESULT_SUCCESS) {
×
601
            found = true;
×
602
            const auto &ctx = kb.get_context();
×
603
            const auto &k = make_kernel<ze_be>(
×
604
                {kb, ZeKern, ext::oneapi::level_zero::ownership::keep}, ctx);
×
605
            syclInteropKern_ptr = std::unique_ptr<kernel>(new kernel(k));
×
606
            break;
×
607
        }
×
608
        else {
×
609
            if (ze_status != ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
×
610
                error_handler("zeKernelCreate failed: " +
×
611
                                  _GetErrorCode_ze_impl(ze_status),
×
612
                              __FILE__, __func__, __LINE__);
×
613
                return nullptr;
×
614
            }
×
615
        }
×
616
    }
×
617

618
    if (found) {
×
619
        return wrap<kernel>(new kernel(*syclInteropKern_ptr));
×
620
    }
×
621
    else {
×
622
        error_handler("Kernel named " + std::string(kernel_name) +
×
623
                          " could not be found.",
×
624
                      __FILE__, __func__, __LINE__, error_level::error);
×
625
        return nullptr;
×
626
    }
×
627
}
×
628

629
bool _HasKernel_ze_impl(const kernel_bundle<bundle_state::executable> &kb,
630
                        const char *kernel_name)
631
{
×
632
    auto zeKernelCreateFn = get_zeKernelCreate();
×
633
    if (zeKernelCreateFn == nullptr) {
×
634
        error_handler("Could not load zeKernelCreate function.", __FILE__,
×
635
                      __func__, __LINE__, error_level::error);
×
636
        return false;
×
637
    }
×
638

639
    auto ZeKernelBundle = sycl::get_native<ze_be>(kb);
×
640

641
    // Populate the Level Zero kernel descriptions
642
    ze_kernel_desc_t ZeKernelDescr = {ZE_STRUCTURE_TYPE_KERNEL_DESC, nullptr,
×
643
                                      0, // flags
×
644
                                      kernel_name};
×
645

646
    std::unique_ptr<sycl::kernel> syclInteropKern_ptr;
×
647
    ze_kernel_handle_t ZeKern;
×
648
    for (auto &ZeM : ZeKernelBundle) {
×
649
        ze_result_t ze_status = zeKernelCreateFn(ZeM, &ZeKernelDescr, &ZeKern);
×
650

651
        if (ze_status == ZE_RESULT_SUCCESS) {
×
652
            return true;
×
653
        }
×
654
        else {
×
655
            if (ze_status != ZE_RESULT_ERROR_INVALID_KERNEL_NAME) {
×
656
                error_handler("zeKernelCreate failed: " +
×
657
                                  _GetErrorCode_ze_impl(ze_status),
×
658
                              __FILE__, __func__, __LINE__, error_level::error);
×
659
                return false;
×
660
            }
×
661
        }
×
662
    }
×
663

664
    return false;
×
665
}
×
666

667
#endif /* #ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION */
668

669
} /* end of anonymous namespace */
670

671
__dpctl_give DPCTLSyclKernelBundleRef
672
DPCTLKernelBundle_CreateFromSpirv(__dpctl_keep const DPCTLSyclContextRef CtxRef,
673
                                  __dpctl_keep const DPCTLSyclDeviceRef DevRef,
674
                                  __dpctl_keep const void *IL,
675
                                  size_t length,
676
                                  const char *CompileOpts,
677
                                  size_t NumSpecConsts,
678
                                  const DPCTLSpecConst *SpecConsts)
679
{
75✔
680
    DPCTLSyclKernelBundleRef KBRef = nullptr;
75✔
681
    if (!CtxRef) {
75✔
682
        error_handler("Cannot create program from SPIR-V as the supplied SYCL "
3✔
683
                      "context is NULL.",
3✔
684
                      __FILE__, __func__, __LINE__);
3✔
685
        return KBRef;
3✔
686
    }
3✔
687
    if (!DevRef) {
72✔
688
        error_handler("Cannot create program from SPIR-V as the supplied SYCL "
3✔
689
                      "device is NULL.",
3✔
690
                      __FILE__, __func__, __LINE__);
3✔
691
        return KBRef;
3✔
692
    }
3✔
693
    if ((!IL) || (length == 0)) {
69!
694
        error_handler("Cannot create program from null SPIR-V buffer.",
3✔
695
                      __FILE__, __func__, __LINE__);
3✔
696
        return KBRef;
3✔
697
    }
3✔
698

699
    context *SyclCtx = unwrap<context>(CtxRef);
66✔
700
    device *SyclDev = unwrap<device>(DevRef);
66✔
701
    // get the backend type
702
    auto BE = SyclCtx->get_platform().get_backend();
66✔
703
    switch (BE) {
66✔
704
    case backend::opencl:
66!
705
        KBRef = _CreateKernelBundleWithIL_ocl_impl(*SyclCtx, *SyclDev, IL,
66✔
706
                                                   length, CompileOpts,
66✔
707
                                                   NumSpecConsts, SpecConsts);
66✔
708
        break;
66✔
709
    case backend::ext_oneapi_level_zero:
×
710
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
711
        KBRef = _CreateKernelBundleWithIL_ze_impl(*SyclCtx, *SyclDev, IL,
×
NEW
712
                                                  length, CompileOpts,
×
NEW
713
                                                  NumSpecConsts, SpecConsts);
×
714
        break;
×
715
#endif
×
716
    default:
×
717
        std::ostringstream os;
×
718
        os << "Backend " << BE << " is not supported";
×
719
        error_handler(os.str(), __FILE__, __func__, __LINE__);
×
720
        break;
×
721
    }
66✔
722
    return KBRef;
66✔
723
}
66✔
724

725
__dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromOCLSource(
726
    __dpctl_keep const DPCTLSyclContextRef Ctx,
727
    __dpctl_keep const DPCTLSyclDeviceRef Dev,
728
    __dpctl_keep const char *Source,
729
    __dpctl_keep const char *CompileOpts)
730
{
39✔
731
    context *SyclCtx = nullptr;
39✔
732
    device *SyclDev = nullptr;
39✔
733

734
    if (!Ctx) {
39✔
735
        error_handler("Input Ctx is nullptr.", __FILE__, __func__, __LINE__);
10✔
736
        return nullptr;
10✔
737
    }
10✔
738
    if (!Dev) {
29✔
739
        error_handler("Input Dev is nullptr.", __FILE__, __func__, __LINE__);
1✔
740
        return nullptr;
1✔
741
    }
1✔
742
    if (!Source) {
28!
743
        error_handler("Input Source is nullptr.", __FILE__, __func__, __LINE__);
×
744
        return nullptr;
×
745
    }
×
746

747
    SyclCtx = unwrap<context>(Ctx);
28✔
748
    SyclDev = unwrap<device>(Dev);
28✔
749

750
    // get the backend type
751
    auto BE = SyclCtx->get_platform().get_backend();
28✔
752
    switch (BE) {
28✔
753
    case backend::opencl:
28!
754
        try {
28✔
755
            return _CreateKernelBundleWithOCLSource_ocl_impl(
28✔
756
                *SyclCtx, *SyclDev, Source, CompileOpts);
28✔
757
        } catch (std::exception const &e) {
28✔
758
            error_handler(e, __FILE__, __func__, __LINE__);
×
759
            return nullptr;
×
760
        }
×
761
        break;
×
762
    case backend::ext_oneapi_level_zero:
×
763
        error_handler(
×
764
            "CreateFromSource is not supported for Level Zero backend.",
×
765
            __FILE__, __func__, __LINE__);
×
766
        return nullptr;
×
767
    default:
×
768
        error_handler("CreateFromSource is not supported in unknown backend.",
×
769
                      __FILE__, __func__, __LINE__);
×
770
        return nullptr;
×
771
    }
28✔
772
}
28✔
773

774
__dpctl_give DPCTLSyclKernelRef
775
DPCTLKernelBundle_GetKernel(__dpctl_keep DPCTLSyclKernelBundleRef KBRef,
776
                            __dpctl_keep const char *KernelName)
777
{
115✔
778
    if (!KBRef) {
115✔
779
        error_handler("Input KBRef is nullptr", __FILE__, __func__, __LINE__);
21✔
780
        return nullptr;
21✔
781
    }
21✔
782
    if (!KernelName) {
94✔
783
        error_handler("Input KernelName is nullptr", __FILE__, __func__,
3✔
784
                      __LINE__);
3✔
785
        return nullptr;
3✔
786
    }
3✔
787
    auto SyclKB = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
91✔
788
    sycl::backend be = SyclKB->get_backend();
91✔
789
    switch (be) {
91✔
790
    case sycl::backend::opencl:
91!
791
        return _GetKernel_ocl_impl(*SyclKB, KernelName);
91✔
792
    case sycl::backend::ext_oneapi_level_zero:
×
793
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
794
        return _GetKernel_ze_impl(*SyclKB, KernelName);
×
795
#endif
×
796
    default:
×
797
        std::ostringstream os;
×
798
        os << "Backend " << be << " is not supported";
×
799
        error_handler(os.str(), __FILE__, __func__, __LINE__);
×
800
        return nullptr;
×
801
    }
91✔
802
}
91✔
803

804
bool DPCTLKernelBundle_HasKernel(__dpctl_keep DPCTLSyclKernelBundleRef KBRef,
805
                                 __dpctl_keep const char *KernelName)
806
{
68✔
807
    if (!KBRef) {
68✔
808
        error_handler("Input KBRef is nullptr", __FILE__, __func__, __LINE__);
3✔
809
        return false;
3✔
810
    }
3✔
811
    if (!KernelName) {
65✔
812
        error_handler("Input KernelName is nullptr", __FILE__, __func__,
3✔
813
                      __LINE__);
3✔
814
        return false;
3✔
815
    }
3✔
816

817
    auto SyclKB = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
62✔
818
    sycl::backend be = SyclKB->get_backend();
62✔
819
    switch (be) {
62✔
820
    case sycl::backend::opencl:
62!
821
        return _HasKernel_ocl_impl(*SyclKB, KernelName);
62✔
822
    case sycl::backend::ext_oneapi_level_zero:
×
823
#ifdef DPCTL_ENABLE_L0_PROGRAM_CREATION
×
824
        return _HasKernel_ze_impl(*SyclKB, KernelName);
×
825
#endif
×
826
    default:
×
827
        std::ostringstream os;
×
828
        os << "Backend " << be << " is not supported";
×
829
        error_handler(os.str(), __FILE__, __func__, __LINE__);
×
830
        return false;
×
831
    }
62✔
832
}
62✔
833

834
void DPCTLKernelBundle_Delete(__dpctl_take DPCTLSyclKernelBundleRef KBRef)
835
{
106✔
836
    delete unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
106✔
837
}
106✔
838

839
__dpctl_give DPCTLSyclKernelBundleRef
840
DPCTLKernelBundle_Copy(__dpctl_keep const DPCTLSyclKernelBundleRef KBRef)
841
{
8✔
842
    auto Bundle = unwrap<kernel_bundle<bundle_state::executable>>(KBRef);
8✔
843
    if (!Bundle) {
8✔
844
        error_handler(
3✔
845
            "Cannot copy DPCTLSyclKernelBundleRef as input is a nullptr",
3✔
846
            __FILE__, __func__, __LINE__);
3✔
847
        return nullptr;
3✔
848
    }
3✔
849
    try {
5✔
850
        auto CopiedBundle =
5✔
851
            new kernel_bundle<bundle_state::executable>(*Bundle);
5✔
852
        return wrap<kernel_bundle<bundle_state::executable>>(CopiedBundle);
5✔
853
    } catch (std::exception const &e) {
5✔
854
        error_handler(e, __FILE__, __func__, __LINE__);
×
855
        return nullptr;
×
856
    }
×
857
}
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