This patch adds a toolchain (TC) for SPIR-V. The TC is not complete but it is functional enough for producing SPIR-V assembly*1 and object code directly via clang from a language of choice, for example:
clang -target spirv64 foo.cl -c -o foo.spv clang -target spirv64 baz.clcpp -c -o baz.spv clang -target spirv64 bar.c -S -o bar.spt
The SPIR-V code is generated by the SPIRV-LLVM translator tool named llvm-spirv that is sought in PATH.
Linking of multiple input files is currently not supported but can be added separately. The options are:
- Link using LLVM IR linker and then invoke llvm-spirv on the final linked module.
- Linking using spirv-link of SPIR-V object files generated from llvm-spirv. Creates another dependency on external tools.
Feedback would be greatly appreciated...
Changes in the Driver and base ToolChain and Tool:
Added a mechanism to work with the lack of SPIR-V backend in LLVM for SPIR-V TC. Until SPIR-V backend lands on LLVM, compilation phases/actions should be bound for SPIR-V in the meantime as following:
- compile -> tools::Clang
- backend -> tools::SPIRV::Translator
- assemble -> tools::SPIRV::Translator
However, Driver’s ToolSelector collapses compile-backend-assemble and compile-backend sequences to tools::Clang. To prevent this, added new {use,has}IntegratedBackend properties in ToolChain and Tool to which the ToolSelector reacts on, and which SPIR-V TC overrides.
This is contributed originally by @linjamaki along with the previous changes in the stack (D112404 - [SPIR-V] Add translator tool) to address feedback in (https://reviews.llvm.org/D110618#3062078) and other concerns around the SPIR-V toolchain adoption for languages other than HIP.
[1]: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/wiki/SPIRV-Toolchain-for-Clang
This change relies on the following changes to llvm-spirv:
- Accepting SPIR-V as a valid triple: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/pull/1305
- [Optional but desirable] Generation of standard SPIR-V assembly syntax: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/pull/1294