Skip to content

Commit 7f43d4a

Browse files
author
Diptorup Deb
committed
Refactoring the backend functions to prep for changes to add L0 support.
1 parent 45959f9 commit 7f43d4a

File tree

2 files changed

+83
-39
lines changed

2 files changed

+83
-39
lines changed

dpctl-capi/include/dpctl_sycl_program_interface.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ DPCTL_C_EXTERN_C_BEGIN
4141
* @brief Create a Sycl program from an OpenCL SPIR-V binary file.
4242
*
4343
* Sycl 1.2 does not expose any method to create a sycl::program from a SPIR-V
44-
* IL file. To get around this limitation, we need to use the Sycl feature to
45-
* create an interoperability kernel from an OpenCL kernel. This function first
46-
* creates an OpenCL program and kernel from the SPIR-V binary and then using
47-
* the Sycl-OpenCL interoperability feature creates a Sycl kernel from the
48-
* OpenCL kernel.
44+
* IL file. To get around this limitation, we first creare a SYCL
45+
* interoperability program and then create a SYCL program from the
46+
* interoperability program. Currently, interoperability programs can be created
47+
* for OpenCL and Level-0 backends.
4948
*
5049
* The feature to create a Sycl kernel from a SPIR-V IL binary will be available
5150
* in Sycl 2.0 at which point this function may become deprecated.

dpctl-capi/source/dpctl_sycl_program_interface.cpp

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,21 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPCTLSyclContextRef)
3838
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(program, DPCTLSyclProgramRef)
3939
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(kernel, DPCTLSyclKernelRef)
4040

41-
} /* end of anonymous namespace */
42-
43-
__dpctl_give DPCTLSyclProgramRef
44-
DPCTLProgram_CreateFromOCLSpirv (__dpctl_keep const DPCTLSyclContextRef CtxRef,
45-
__dpctl_keep const void *IL,
46-
size_t length)
41+
__dppl_give DPCTLSyclProgramRef
42+
createOpenCLInterOpProgram (const context &SyclCtx,
43+
__dppl_keep const void *IL,
44+
size_t length)
4745
{
4846
cl_int err;
49-
context *SyclCtx;
50-
if(!CtxRef) {
51-
// \todo handle error
52-
return nullptr;
53-
}
54-
55-
SyclCtx = unwrap(CtxRef);
56-
auto CLCtx = SyclCtx->get();
47+
auto CLCtx = SyclCtx.get();
5748
auto CLProgram = clCreateProgramWithIL(CLCtx, IL, length, &err);
5849
if (err) {
5950
// \todo: record the error string and any other information.
6051
std::cerr << "OpenCL program could not be created from the SPIR-V "
6152
"binary. OpenCL Error " << err << ".\n";
6253
return nullptr;
6354
}
64-
auto SyclDevices = SyclCtx->get_devices();
55+
auto SyclDevices = SyclCtx.get_devices();
6556

6657
// Get a list of CL Devices from the Sycl devices
6758
auto CLDevices = new cl_device_id[SyclDevices.size()];
@@ -83,18 +74,50 @@ DPCTLProgram_CreateFromOCLSpirv (__dpctl_keep const DPCTLSyclContextRef CtxRef,
8374

8475
// Create the Sycl program from OpenCL program
8576
try {
86-
auto SyclProgram = new program(*SyclCtx, CLProgram);
77+
auto SyclProgram = new program(SyclCtx, CLProgram);
8778
return wrap(SyclProgram);
88-
} catch (invalid_object_error) {
79+
} catch (invalid_object_error &e) {
8980
// \todo record error
81+
std::cerr << e.what() << '\n';
9082
return nullptr;
9183
}
9284
}
9385

94-
__dpctl_give DPCTLSyclProgramRef
95-
DPCTLProgram_CreateFromOCLSource (__dpctl_keep const DPCTLSyclContextRef Ctx,
96-
__dpctl_keep const char *Source,
97-
__dpctl_keep const char *CompileOpts)
86+
} /* end of anonymous namespace */
87+
88+
__dppl_give DPCTLSyclProgramRef
89+
DPCTLProgram_CreateFromOCLSpirv (__dppl_keep const DPPLSyclContextRef CtxRef,
90+
__dppl_keep const void *IL,
91+
size_t length)
92+
{
93+
DPPLSyclProgramRef Pref = nullptr;
94+
context *SyclCtx = nullptr;
95+
if(!CtxRef) {
96+
// \todo handle error
97+
return Pref;
98+
}
99+
100+
SyclCtx = unwrap(CtxRef);
101+
// get the backend type
102+
auto BE = SyclCtx->get_platform().get_backend();
103+
switch (BE)
104+
{
105+
case backend::opencl:
106+
Pref = createOpenCLInterOpProgram(*SyclCtx, IL, length);
107+
break;
108+
case backend::level_zero:
109+
break;
110+
default:
111+
break;
112+
}
113+
114+
return Pref;
115+
}
116+
117+
__dppl_give DPCTLSyclProgramRef
118+
DPCTLProgram_CreateFromOCLSource (__dppl_keep const DPPLSyclContextRef Ctx,
119+
__dppl_keep const char *Source,
120+
__dppl_keep const char *CompileOpts)
98121
{
99122
std::string compileOpts;
100123
context *SyclCtx = nullptr;
@@ -118,23 +141,43 @@ DPCTLProgram_CreateFromOCLSource (__dpctl_keep const DPCTLSyclContextRef Ctx,
118141
compileOpts = CompileOpts;
119142
}
120143

121-
try{
122-
SyclProgram->build_with_source(source, compileOpts);
123-
return wrap(SyclProgram);
124-
} catch (compile_program_error) {
125-
delete SyclProgram;
126-
// \todo record error
144+
// get the backend type
145+
auto BE = SyclCtx->get_platform().get_backend();
146+
switch (BE)
147+
{
148+
case backend::opencl:
149+
try {
150+
SyclProgram->build_with_source(source, compileOpts);
151+
return wrap(SyclProgram);
152+
} catch (compile_program_error &e) {
153+
std::cerr << e.what() << '\n';
154+
delete SyclProgram;
155+
// \todo record error
156+
return nullptr;
157+
} catch (feature_not_supported &e) {
158+
std::cerr << e.what() << '\n';
159+
delete SyclProgram;
160+
// \todo record error
161+
return nullptr;
162+
} catch (runtime_error &e) {
163+
std::cerr << e.what() << '\n';
164+
delete SyclProgram;
165+
// \todo record error
166+
return nullptr;
167+
}
168+
break;
169+
case backend::level_zero:
170+
std::cerr << "CreateFromSource is not supported in Level Zero.\n";
127171
return nullptr;
128-
} catch (feature_not_supported) {
129-
delete SyclProgram;
130-
// \todo record error
172+
default:
173+
std::cerr << "CreateFromSource is not supported in unknown backend.\n";
131174
return nullptr;
132175
}
133176
}
134177

135178
__dpctl_give DPCTLSyclKernelRef
136179
DPCTLProgram_GetKernel (__dpctl_keep DPCTLSyclProgramRef PRef,
137-
__dpctl_keep const char *KernelName)
180+
__dpctl_keep const char *KernelName)
138181
{
139182
if(!PRef) {
140183
// \todo record error
@@ -149,15 +192,16 @@ DPCTLProgram_GetKernel (__dpctl_keep DPCTLSyclProgramRef PRef,
149192
try {
150193
auto SyclKernel = new kernel(SyclProgram->get_kernel(name));
151194
return wrap(SyclKernel);
152-
} catch (invalid_object_error) {
195+
} catch (invalid_object_error &e) {
153196
// \todo record error
197+
std::cerr << e.what() << '\n';
154198
return nullptr;
155199
}
156200
}
157201

158202
bool
159203
DPCTLProgram_HasKernel (__dpctl_keep DPCTLSyclProgramRef PRef,
160-
__dpctl_keep const char *KernelName)
204+
__dpctl_keep const char *KernelName)
161205
{
162206
if(!PRef) {
163207
// \todo handle error
@@ -167,7 +211,8 @@ DPCTLProgram_HasKernel (__dpctl_keep DPCTLSyclProgramRef PRef,
167211
auto SyclProgram = unwrap(PRef);
168212
try {
169213
return SyclProgram->has_kernel(KernelName);
170-
} catch (invalid_object_error) {
214+
} catch (invalid_object_error &e) {
215+
std::cerr << e.what() << '\n';
171216
return false;
172217
}
173218
}

0 commit comments

Comments
 (0)