Skip to content

Commit bdf1014

Browse files
committed
Report build log in exception message
1 parent 22802fb commit bdf1014

File tree

6 files changed

+124
-13
lines changed

6 files changed

+124
-13
lines changed

dpctl/_backend.pxd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,11 @@ cdef extern from "syclinterface/dpctl_sycl_kernel_bundle_interface.h":
458458
cdef struct DPCTLBuildOptionList
459459
cdef struct DPCTLKernelNameList
460460
cdef struct DPCTLVirtualHeaderList
461+
cdef struct DPCTLKernelBuildLog
461462
ctypedef DPCTLBuildOptionList* DPCTLBuildOptionListRef
462463
ctypedef DPCTLKernelNameList* DPCTLKernelNameListRef
463464
ctypedef DPCTLVirtualHeaderList* DPCTLVirtualHeaderListRef
465+
ctypedef DPCTLKernelBuildLog* DPCTLKernelBuildLogRef
464466

465467
cdef DPCTLBuildOptionListRef DPCTLBuildOptionList_Create()
466468
cdef void DPCTLBuildOptionList_Delete(DPCTLBuildOptionListRef Ref)
@@ -478,13 +480,18 @@ cdef extern from "syclinterface/dpctl_sycl_kernel_bundle_interface.h":
478480
const char *Name,
479481
const char *Content)
480482

483+
cdef DPCTLKernelBuildLogRef DPCTLKernelBuildLog_Create()
484+
cdef void DPCTLKernelBuildLog_Delete(DPCTLKernelBuildLogRef Ref)
485+
cdef const char *DPCTLKernelBuildLog_Get(DPCTLKernelBuildLogRef)
486+
481487
cdef DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
482488
const DPCTLSyclContextRef Ctx,
483489
const DPCTLSyclDeviceRef Dev,
484490
const char *Source,
485491
DPCTLVirtualHeaderListRef Headers,
486492
DPCTLKernelNameListRef Names,
487-
DPCTLBuildOptionListRef BuildOptions)
493+
DPCTLBuildOptionListRef BuildOptions,
494+
DPCTLKernelBuildLogRef BuildLog)
488495

489496
cdef DPCTLSyclKernelRef DPCTLKernelBundle_GetSyclKernel(
490497
DPCTLSyclKernelBundleRef KBRef,

dpctl/program/_program.pyx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ from dpctl._backend cimport ( # noqa: E211, E402;
4242
DPCTLKernel_GetPreferredWorkGroupSizeMultiple,
4343
DPCTLKernel_GetPrivateMemSize,
4444
DPCTLKernel_GetWorkGroupSize,
45+
DPCTLKernelBuildLog_Create,
46+
DPCTLKernelBuildLog_Delete,
47+
DPCTLKernelBuildLog_Get,
48+
DPCTLKernelBuildLogRef,
4549
DPCTLKernelBundle_Copy,
4650
DPCTLKernelBundle_CreateFromOCLSource,
4751
DPCTLKernelBundle_CreateFromSpirv,
@@ -383,7 +387,8 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source,
383387
384388
Raises:
385389
SyclProgramCompilationError
386-
If a SYCL kernel bundle could not be created.
390+
If a SYCL kernel bundle could not be created. The exception
391+
message contains the build log for more details.
387392
"""
388393
cdef DPCTLSyclKernelBundleRef KBref
389394
cdef DPCTLSyclContextRef CRef = q.get_sycl_context().get_context_ref()
@@ -397,6 +402,7 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source,
397402
cdef const char* sName
398403
cdef bytes bContent
399404
cdef const char* sContent
405+
cdef const char* buildLogContent
400406
for opt in copts:
401407
if not isinstance(opt, unicode):
402408
DPCTLBuildOptionList_Delete(BuildOpts)
@@ -430,19 +436,26 @@ cpdef create_program_from_sycl_source(SyclQueue q, unicode source,
430436
sContent = <const char*>bContent
431437
DPCTLVirtualHeaderList_Append(VirtualHeaders, sName, sContent)
432438

439+
cdef DPCTLKernelBuildLogRef BuildLog
440+
BuildLog = DPCTLKernelBuildLog_Create()
441+
433442
KBref = DPCTLKernelBundle_CreateFromSYCLSource(CRef, DRef, Src,
434443
VirtualHeaders, KernelNames,
435-
BuildOpts)
444+
BuildOpts, BuildLog)
436445

437446
if KBref is NULL:
447+
buildLogContent = DPCTLKernelBuildLog_Get(BuildLog)
448+
buildLogStr = str(buildLogContent, "utf-8")
438449
DPCTLBuildOptionList_Delete(BuildOpts)
439450
DPCTLKernelNameList_Delete(KernelNames)
440451
DPCTLVirtualHeaderList_Delete(VirtualHeaders)
441-
raise SyclProgramCompilationError()
452+
DPCTLKernelBuildLog_Delete(BuildLog)
453+
raise SyclProgramCompilationError(buildLogStr)
442454

443455
DPCTLBuildOptionList_Delete(BuildOpts)
444456
DPCTLKernelNameList_Delete(KernelNames)
445457
DPCTLVirtualHeaderList_Delete(VirtualHeaders)
458+
DPCTLKernelBuildLog_Delete(BuildLog)
446459

447460
return SyclProgram._create(KBref, True)
448461

dpctl/tests/test_sycl_program.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,9 @@ def test_create_program_from_invalid_src_ocl():
268268

269269
def test_create_program_from_sycl_source():
270270
try:
271-
q = dpctl.SyclQueue("level_zero")
271+
q = dpctl.SyclQueue("opencl")
272272
except dpctl.SyclQueueCreationError:
273-
pytest.skip("No Level-zero queue is available")
273+
pytest.skip("No OpenCL queue is available")
274274

275275
if not q.get_sycl_device().can_compile("sycl"):
276276
pytest.skip("SYCL source compilation not supported")
@@ -374,3 +374,40 @@ def test_create_program_from_sycl_source():
374374
assert type(cmsgsz) is int
375375

376376
_check_cpython_api_SyclProgram_GetKernelBundleRef(prog)
377+
378+
379+
def test_create_program_from_invalid_src_sycl():
380+
try:
381+
q = dpctl.SyclQueue("opencl")
382+
except dpctl.SyclQueueCreationError:
383+
pytest.skip("No OpenCL queue is available")
384+
385+
if not q.get_sycl_device().can_compile("sycl"):
386+
pytest.skip("SYCL source compilation not supported")
387+
388+
sycl_source = """
389+
#include <sycl/sycl.hpp>
390+
391+
namespace syclext = sycl::ext::oneapi::experimental;
392+
393+
extern "C" SYCL_EXTERNAL
394+
SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclext::nd_range_kernel<1>))
395+
void vector_add(int* in1, int* in2, int* out){
396+
sycl::nd_item<1> item =
397+
sycl::ext::oneapi::this_work_item::get_nd_item<1>();
398+
size_t globalID = item.get_global_linear_id()
399+
out[globalID] = in1[globalID] + in2[globalID];
400+
}
401+
"""
402+
try:
403+
_ = dpctl.program.create_program_from_sycl_source(
404+
q,
405+
sycl_source,
406+
headers=[],
407+
registered_names=[],
408+
copts=[],
409+
)
410+
assert False
411+
except dpctl_prog.SyclProgramCompilationError as prog_error:
412+
print(str(prog_error))
413+
assert "error: expected ';' at end of declaration" in str(prog_error)

libsyclinterface/include/syclinterface/dpctl_sycl_kernel_bundle_interface.h

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ DPCTLKernelBundle_Copy(__dpctl_keep const DPCTLSyclKernelBundleRef KBRef);
132132
typedef struct DPCTLBuildOptionList *DPCTLBuildOptionListRef;
133133
typedef struct DPCTLKernelNameList *DPCTLKernelNameListRef;
134134
typedef struct DPCTLVirtualHeaderList *DPCTLVirtualHeaderListRef;
135+
typedef struct DPCTLKernelBuildLog *DPCTLKernelBuildLogRef;
135136

136137
/*!
137138
* @brief Create an empty list of build options.
@@ -145,7 +146,7 @@ __dpctl_give DPCTLBuildOptionListRef DPCTLBuildOptionList_Create();
145146
/*!
146147
* @brief Frees the DPCTLBuildOptionListRef pointer.
147148
*
148-
* @param KBRef Opaque pointer to a list of build options
149+
* @param Ref Opaque pointer to a list of build options
149150
* @ingroup KernelBundleInterface
150151
*/
151152
DPCTL_API void
@@ -173,7 +174,7 @@ __dpctl_give DPCTLKernelNameListRef DPCTLKernelNameList_Create();
173174
/*!
174175
* @brief Frees the DPCTLKernelNameListRef pointer.
175176
*
176-
* @param KBRef Opaque pointer to a list of kernels to register
177+
* @param Ref Opaque pointer to a list of kernels to register
177178
* @ingroup KernelBundleInterface
178179
*/
179180
DPCTL_API void
@@ -200,7 +201,7 @@ __dpctl_give DPCTLVirtualHeaderListRef DPCTLVirtualHeaderList_Create();
200201
/*!
201202
* @brief Frees the DPCTLVirtualHeaderListRef pointer.
202203
*
203-
* @param KBRef Opaque pointer to a list of virtual headers
204+
* @param Ref Opaque pointer to a list of virtual headers
204205
* @ingroup KernelBundleInterface
205206
*/
206207
DPCTL_API void
@@ -218,6 +219,33 @@ void DPCTLVirtualHeaderList_Append(__dpctl_keep DPCTLVirtualHeaderListRef Ref,
218219
__dpctl_keep const char *Name,
219220
__dpctl_keep const char *Content);
220221

222+
/*!
223+
* @brief Create an empty kernel build log.
224+
*
225+
* @return Opaque pointer to the kernel build log.
226+
* @ingroup KernelBundleInterface
227+
*/
228+
DPCTL_API __dpctl_give DPCTLKernelBuildLogRef DPCTLKernelBuildLog_Create();
229+
230+
/*!
231+
* @brief Frees the DPCTLKernelBuildLogRef pointer.
232+
*
233+
* @param Ref Opaque pointer to a kernel build log.
234+
* @ingroup KernelBundleInterface
235+
*/
236+
DPCTL_API
237+
void DPCTLKernelBuildLog_Delete(__dpctl_take DPCTLKernelBuildLogRef Ref);
238+
239+
/*!
240+
* @brief Get the content of the build log.
241+
*
242+
* @param Ref Opaque pointer to the kernel build log.
243+
* @return Content of the build log
244+
* @ingroup KernelBundleInterface
245+
*/
246+
DPCTL_API const char *
247+
DPCTLKernelBuildLog_Get(__dpctl_keep DPCTLKernelBuildLogRef);
248+
221249
/*!
222250
* @brief Create a SYCL kernel bundle from an SYCL kernel source string.
223251
*
@@ -238,7 +266,8 @@ __dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
238266
__dpctl_keep const char *Source,
239267
__dpctl_keep DPCTLVirtualHeaderListRef Headers,
240268
__dpctl_keep DPCTLKernelNameListRef Names,
241-
__dpctl_keep DPCTLBuildOptionListRef BuildOptions);
269+
__dpctl_keep DPCTLBuildOptionListRef BuildOptions,
270+
__dpctl_keep DPCTLKernelBuildLogRef BuildLog);
242271

243272
/*!
244273
* @brief Returns the SyclKernel with given name from the program compiled from

libsyclinterface/source/dpctl_sycl_kernel_bundle_interface.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,27 @@ void DPCTLVirtualHeaderList_Append(__dpctl_keep DPCTLVirtualHeaderListRef Ref,
833833
reinterpret_cast<virtual_header_list_t *>(Ref)->push_back(Header);
834834
}
835835

836+
using kernel_build_log_t = std::string;
837+
838+
__dpctl_give DPCTLKernelBuildLogRef DPCTLKernelBuildLog_Create()
839+
{
840+
auto BuildLog =
841+
std::unique_ptr<kernel_build_log_t>(new kernel_build_log_t(""));
842+
auto *RetVal = reinterpret_cast<DPCTLKernelBuildLogRef>(BuildLog.get());
843+
BuildLog.release();
844+
return RetVal;
845+
}
846+
847+
void DPCTLKernelBuildLog_Delete(__dpctl_take DPCTLKernelBuildLogRef Ref)
848+
{
849+
delete reinterpret_cast<kernel_build_log_t *>(Ref);
850+
}
851+
852+
const char *DPCTLKernelBuildLog_Get(__dpctl_keep DPCTLKernelBuildLogRef Ref)
853+
{
854+
return reinterpret_cast<kernel_build_log_t *>(Ref)->data();
855+
}
856+
836857
namespace syclex = sycl::ext::oneapi::experimental;
837858

838859
#if defined(SYCL_EXT_ONEAPI_KERNEL_COMPILER) && \
@@ -880,7 +901,8 @@ __dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
880901
__dpctl_keep const char *Source,
881902
__dpctl_keep DPCTLVirtualHeaderListRef Headers,
882903
__dpctl_keep DPCTLKernelNameListRef Names,
883-
__dpctl_keep DPCTLBuildOptionListRef BuildOptions)
904+
__dpctl_keep DPCTLBuildOptionListRef BuildOptions,
905+
__dpctl_keep DPCTLKernelBuildLogRef BuildLog)
884906
{
885907
#if (SUPPORTS_SYCL_COMPILATION > 0)
886908
context *SyclCtx = unwrap<context>(Ctx);
@@ -941,7 +963,8 @@ __dpctl_give DPCTLSyclKernelBundleRef DPCTLKernelBundle_CreateFromSYCLSource(
941963
return wrap<kernel_bundle<bundle_state::executable>>(
942964
ResultBundle.release());
943965
} catch (const std::exception &e) {
944-
error_handler(e, __FILE__, __func__, __LINE__);
966+
auto *RawBuildLog = reinterpret_cast<kernel_build_log_t *>(BuildLog);
967+
*RawBuildLog = e.what();
945968
return nullptr;
946969
}
947970
#else

libsyclinterface/tests/test_sycl_kernel_bundle_interface.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,13 @@ struct TestSYCLKernelBundleFromSource
335335
DPCTLVirtualHeaderListRef VHRef = DPCTLVirtualHeaderList_Create();
336336
DPCTLVirtualHeaderList_Append(VHRef, Header1Name, header1_content);
337337
DPCTLVirtualHeaderList_Append(VHRef, Header2Name, header2_content);
338+
DPCTLKernelBuildLogRef KBLRef = DPCTLKernelBuildLog_Create();
338339
KBRef = DPCTLKernelBundle_CreateFromSYCLSource(
339-
CRef, DRef, sycl_source, VHRef, KNRef, BORef);
340+
CRef, DRef, sycl_source, VHRef, KNRef, BORef, KBLRef);
340341
DPCTLVirtualHeaderList_Delete(VHRef);
341342
DPCTLKernelNameList_Delete(KNRef);
342343
DPCTLBuildOptionList_Delete(BORef);
344+
DPCTLKernelBuildLog_Delete(KBLRef);
343345
}
344346
}
345347

0 commit comments

Comments
 (0)