Skip to content

Add a C API - #27

Merged
maleadt merged 3 commits into
mainfrom
tb/c-api
Oct 20, 2026
Merged

maleadt merged 3 commits into
mainfrom
tb/c-api

Conversation

@maleadt

@maleadt maleadt commented Oct 20, 2026

Copy link
Copy Markdown
Member

OpenCL.jl runs the spirv2clc tool once per kernel: it writes the SPIR-V to a temporary file, runs the tool, and reads back stdout and stderr merged. This PR adds a C API so it can translate in-process with a ccall instead.

The API follows the llvm-c conventions of the other back-end libraries GPUCompiler.jl loads (libspirv, libllvm_spirv, libnvptx):

int SPIRV2CLCTranslate(const char *SPIRV, size_t Length,
                       const SPIRV2CLCTranslateOptions *Options, /* --spirv-version, --cl-std */
                       SPIRV2CLCDiagnosticCallback Handler, void *HandlerContext,
                       char **OutSource, char **OutMessage);
void SPIRV2CLCDisposeMessage(char *Message);
  • Status and error: the return value is the status (0 = success). On failure the error comes back through OutMessage.
  • Ownership: both returned strings are freed by the library, which avoids freeing across C runtimes.
  • Diagnostics: an optional callback receives them, including warnings. With NULL they go to stderr.
  • Robustness: no C++ exception crosses the boundary.
  • Threads: calls are independent of each other and may run concurrently.

It is built as a shared libspirv2clc that exports only these two functions. The translator and SPIRV-Tools are linked in and hidden, and no C++ type crosses the interface, so the JLL can keep shipping a single build per platform. It installs as its own c_api component, so it can be packaged next to the tool.

Two problems had to be fixed first, because they would hurt more in-process than they do in a separate process:

  • Crashes on invalid input. SPIR-V assembly that doesn't parse dereferenced a null module. Separately, the tool read binaries whose size isn't a multiple of 4 past the end of its buffer. Both segfaulted.
  • Uncapturable diagnostics. The translator wrote its errors to std::cerr and validation errors went through spvDiagnosticPrint. SPIRV-Tools' parse messages were printf'd to stdout without a newline, which put them in the middle of the source OpenCL.jl compiles. All of these now go through a diagnostic handler on the C++ translator. The default handler prints to stderr with an error:/warning: prefix.

Testing:

  • New C test: run by ctest. It covers translation, errors, diagnostics, invalid input and invalid options, and checks that the library exports only the C API on Linux.
  • Sanitizers: the lit suite and ctest pass under ASan+UBSan. TSan reports no races with 8 threads concurrently translating every test module.
  • Shared C++ library: the lit suite and ctest pass with BUILD_SHARED_LIBS=ON.
  • From Julia: I called the library through ccall on all 148 test modules at OpenCL C 1.2, 2.0 and 3.0. All 444 results match the tool's output, including the error messages of the 75 that fail.

Not tested: GCC 10 and macOS, which CI covers, and Windows and FreeBSD, which the Yggdrasil build will be the first to exercise.

Follow-ups: add a LibraryProduct to the Yggdrasil recipe, use the library in OpenCL.jl, and point the downstream CI job at the build with a libspirv2clc_path preference.

Translating SPIR-V assembly that doesn't parse dereferenced the null module
BuildModule returns, and the tool read a binary whose size isn't a multiple
of 4 bytes past the end of its word buffer. Both crashed instead of failing
the translation. Check for a module before using it in both entry points,
and reject such files in the tool.
The translator wrote its errors to std::cerr, validation errors went through
spvDiagnosticPrint, and SPIRV-Tools' parse messages were printf'd to stdout
without a newline, where they ended up in the middle of the emitted source.
A caller embedding the library could capture none of them.

Report every message through a diagnostic handler on the translator, one
call per message, with a severity. The default handler prints to stderr
prefixed by the severity, so the tool's output is unchanged apart from that
prefix and SPIRV-Tools' messages moving to stderr.
Users of the translator that can't call C++, like OpenCL.jl, run the
spirv2clc tool on a file for every kernel. Provide a C API to translate in
process instead, following the llvm-c conventions of the other back-end
libraries GPUCompiler.jl loads: SPIRV2CLCTranslate takes the binary and an
options struct mirroring --spirv-version and --cl-std, returns a status, and
hands back the source or an error message through out-pointers, freed with
SPIRV2CLCDisposeMessage. Diagnostics go to an optional callback.

It is built as a shared library, libspirv2clc, that exports only the C API:
the translator and SPIRV-Tools are linked in and hidden, so it can share a
process with other copies of them, and no C++ type crosses its interface.
It installs as its own component for packaging next to the tool.
@maleadt
maleadt merged commit 5dbda63 into main Oct 20, 2026
5 checks passed
@maleadt
maleadt deleted the tb/c-api branch October 20, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant