Skip to content

Commit fe42e70

Browse files
committed
Propagate build errors to user facing exceptions
1 parent 7d66613 commit fe42e70

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/backend/opencl/compile_module.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using cl::Error;
2929
using cl::Program;
3030
using common::loggerFactory;
31+
using fmt::format;
3132
using opencl::getActiveDeviceId;
3233
using opencl::getDevice;
3334
using opencl::Kernel;
@@ -49,31 +50,23 @@ logger *getLogger() {
4950
return logger.get();
5051
}
5152

52-
#define SHOW_DEBUG_BUILD_INFO(PROG) \
53-
do { \
54-
cl_uint numDevices = PROG.getInfo<CL_PROGRAM_NUM_DEVICES>(); \
55-
for (unsigned int i = 0; i < numDevices; ++i) { \
56-
printf("%s\n", PROG.getBuildInfo<CL_PROGRAM_BUILD_LOG>( \
57-
PROG.getInfo<CL_PROGRAM_DEVICES>()[i]) \
58-
.c_str()); \
59-
printf("%s\n", PROG.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>( \
60-
PROG.getInfo<CL_PROGRAM_DEVICES>()[i]) \
61-
.c_str()); \
62-
} \
53+
#define THROW_BUILD_LOG_EXCEPTION(PROG) \
54+
do { \
55+
string build_error; \
56+
build_error.reserve(4096); \
57+
auto devices = PROG.getInfo<CL_PROGRAM_DEVICES>(); \
58+
for (auto &device : PROG.getInfo<CL_PROGRAM_DEVICES>()) { \
59+
build_error += \
60+
format("OpenCL Device: {}\n\tOptions: {}\n\tLog:\n{}\n", \
61+
device.getInfo<CL_DEVICE_NAME>(), \
62+
PROG.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(device), \
63+
PROG.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device)); \
64+
} \
65+
string info = getEnvVar("AF_OPENCL_SHOW_BUILD_INFO"); \
66+
if (!info.empty() && info != "0") puts(build_error.c_str()); \
67+
AF_ERROR(build_error, AF_ERR_INTERNAL); \
6368
} while (0)
6469

65-
#if defined(NDEBUG)
66-
67-
#define SHOW_BUILD_INFO(PROG) \
68-
do { \
69-
string info = getEnvVar("AF_OPENCL_SHOW_BUILD_INFO"); \
70-
if (!info.empty() && info != "0") { SHOW_DEBUG_BUILD_INFO(PROG); } \
71-
} while (0)
72-
73-
#else
74-
#define SHOW_BUILD_INFO(PROG) SHOW_DEBUG_BUILD_INFO(PROG)
75-
#endif
76-
7770
namespace opencl {
7871

7972
const static string DEFAULT_MACROS_STR(
@@ -116,7 +109,9 @@ Program buildProgram(const vector<string> &kernelSources,
116109

117110
retVal.build({device}, (cl_std + defaults + options.str()).c_str());
118111
} catch (Error &err) {
119-
if (err.err() == CL_BUILD_PROGRAM_FAILURE) { SHOW_BUILD_INFO(retVal); }
112+
if (err.err() == CL_BUILD_PROGRAM_FAILURE) {
113+
THROW_BUILD_LOG_EXCEPTION(retVal);
114+
}
120115
throw;
121116
}
122117
return retVal;

0 commit comments

Comments
 (0)