Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
OpenCL.jl runs the
spirv2clctool 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 accallinstead.The API follows the llvm-c conventions of the other back-end libraries GPUCompiler.jl loads (
libspirv,libllvm_spirv,libnvptx):OutMessage.It is built as a shared
libspirv2clcthat 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 ownc_apicomponent, 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:
std::cerrand validation errors went throughspvDiagnosticPrint. SPIRV-Tools' parse messages wereprintf'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 anerror:/warning:prefix.Testing:
BUILD_SHARED_LIBS=ON.ccallon 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
LibraryProductto the Yggdrasil recipe, use the library in OpenCL.jl, and point the downstream CI job at the build with alibspirv2clc_pathpreference.