You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
acceptedThis proposal is planned.arch-spirvKhronos Group SPIR-VproposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
What would be involved in adding SPIR-V as a target for Zig? There's a translator from (some subset of?) LLVM to SPIR-V; as I understand it, Zig compiles first to LLVM, so this seems reasonable. There's been some discussion of adding SPIR-V as a Clang target which I'm not sure has materialized further, but I think this would be interesting.
The most straightforward way for this to work would be if LLVM supported it directly. However it's still open for discussion to do this even if that scenario does not happen.
It has nvptx64 and amdgcn targets, and can emit LLVM bitcode for them, and the LLVM to SPIR-V translator is also based on libllvm I think, so probably somebody who knows more than me could figure it out. Seems complicated though.
Some advice from @paniq about implementing such a backend:
<lritter> ...there's https://www.khronos.org/registry/spir-v/specs/1.0/SPIRV.html, but also have a look at SpvBuilder in glslang - i made a copy of that one and expanded it a little
<lritter> also, you will need SPIRV Tools for the validator and other stuff. SPIRV Cross can then convert your SPIR-V to GLSL, which is also great for seeing if your stuff produces the code you have in mind
Reacted by Bodie Solomon, Florian Hoenig and johan0A
Hi @andrewrk -- I think LLVM is well on its way to supporting this, but ISTM that it might be necessary or useful to use MLIR. I'm currently exploring this on my own and it definitely seems like a cool project! Far from groking it just yet, but it should be possible to target other GPU backends besides SPIR-V/Vulkan this way, too. IREE is taking this approach to compile from TensorFlow through LLVM.
How would this work with defining images, buffers, input/output variables and all the binding slots and stuff? Or would this just be for the OpenCL use case? (Or maybe all of this can be solved 'easily'?)
I see they want to compile C++ to SPIR-V 🤦♂ RUN AWAY! 😱
Reacted by Florian Hoenig and João Marcos Mororo Costa
I think the focus here should be on generating MLIR / SPIR-V. Tooling and other stuff (like whether / how to use OpenCL types, how to launch and schedule kernels, native GPU types for components, and all that) isn't out of the question, but can hopefully be done as library layers on top of the core functionality. (Counterargument: if the heterogeneous system is all modeled in one IR, LLVM can optimize across the CPU/GPU boundary, as IREE does.)
Otherwise it runs the risk of being too much work to implement or to become useful in a reasonable amount of time.
If adding a SPIR-V backend requires too many {GPU,SPIR-V}-specific features into the core language / compiler, then my proposal is to instead add a feature something like a "Zig @dialect" that can be imported by the build tooling as a library (implemented as MLIR dialects?)
Actually, rust-gpu does not use LLVM's spirv target at all, it manually codegens direct to spirv (the primary motivation for doing it that way is because LLVM's spirv target only supports an opencl/cuda-like execution model, not shader execution models)
Reacted by sobe, Bodie Solomon, Tastaturtaste, Samuel and Liam Germain
I'm not sure if a SPIR-V target is the best option, since you never actually
run the binary. The binary is (from my experience) usually embedded into an
program at compile time and sent to GPU at runtime, or rarely given to a
program at runtime and sent to GPU at runtime.
My workflow with SPIR-V is always something like this:
GLSL -> SPIR-V -> .o -> binary -> SPIR-V sent to GPU at runtime
^
other sources -> .o ----+
which makes some sense in this case, since you need extra compiler for the GLSL -> SPIR-V step.
If Zig adds a SPIR-V backend, the workflow would be the same. But I think we
can do better.
I propose that there would be a builtin function that takes a Zig function and
creates a const array of SPIR-V bytecode at compile time.
This would mean that the same functions can be used by CPU and GPU code, in
contrast to the regular approach where the codebase is split into GPU and CPU
side (since they are two different languages and are compiled with two
different compilers...)
It also frees the programmer of the cumbersome task of embedding files into
executables. Although I have to admit that I have not yet explored the Zig
build system and don't know if this problem is already solved.
I imagine it could look something like this:
fnadd(a: u32, b: u32) u32 {
returna+b;
}
fncompute(gl_GlobalInvocationID: [3]u32, buf: []u32) {
constidx=gl_GlobalInvocationID[0];
buf[idx] =add(idx, idx);
}
constspirv: [_]u32=@spirv(compute);
fnmain() {
// I can call compute()/add() here on the cpu and at the same time can send// them to GPU as a compute shader
}
In this case the OpEntryPoint is constructed from the compute function
signature but I am not sure this is the best way. Further thinking required :D
Alternatively Zig could just expand the Type.Fn struct to also include the
function AST (or one of the IRs). This could entirely offload the SPIR-V
compilation to an external library that would construct SPIR-V bytecode from Type.Fn struct. The external library could then have a more flexible way of
specifying entry point interfaces, spirv target version, optimalisation, ...
It could then look like this:
constspirvc=@import("...");
fnadd(a: u32, b: u32) u32 {
returna+b;
}
fncompute(gl_GlobalInvocationID: [3]u32, buf: []u32) {
constidx=gl_GlobalInvocationID[0];
buf[idx] =add(idx, idx);
}
constspirv: [_]u32=spirvc.compile(@typeInfo(@TypeOf(compute)), ...moreargumentsspecifiedbythelibrary...);
fnmain() {
// I can call compute()/add() here on the cpu and at the same time can send// them to GPU as a compute shader
}
Should I create a new issue for this proposal?
I think it's neat :D
This would mean that the same functions can be used by CPU and GPU code
This is already possible because of Zig's lazy compilation. Just dont reference any of the host functions on the device and vice versa and youre good. The only thing that is required really is conditionally exporting entry points, both for the device and host side:
@ProkopRandacek Host is usually referring to the CPU, while device refers to GPU.
Robin is saying that if you write code that should only run on the CPU, then the final spirv output will not contain this code as long as you don't call it from any of your GPU code
acceptedThis proposal is planned.arch-spirvKhronos Group SPIR-VproposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
Activity
emekoi commentedon Jun 15, 2019
adding a custom spir-v target might lay the groundwork for adding other backends to zig.
andrewrk commentedon Jun 16, 2019
The most straightforward way for this to work would be if LLVM supported it directly. However it's still open for discussion to do this even if that scenario does not happen.
jbsolomon commentedon Jun 17, 2019
One potential fruitful direction could be to look at how clang does this:
https://clang.llvm.org/docs/UsersManual.html#opencl-features
It has
nvptx64andamdgcntargets, and can emit LLVM bitcode for them, and the LLVM to SPIR-V translator is also based on libllvm I think, so probably somebody who knows more than me could figure it out. Seems complicated though.jbsolomon commentedon Jun 17, 2019
Sorry, this article appears to have some references to the current state of the art:
andrewrk commentedon Aug 28, 2019
Some advice from @paniq about implementing such a backend:
jbsolomon commentedon Mar 5, 2020
Hi @andrewrk -- I think LLVM is well on its way to supporting this, but ISTM that it might be necessary or useful to use MLIR. I'm currently exploring this on my own and it definitely seems like a cool project! Far from groking it just yet, but it should be possible to target other GPU backends besides SPIR-V/Vulkan this way, too. IREE is taking this approach to compile from TensorFlow through LLVM.
BarabasGitHub commentedon Mar 5, 2020
How would this work with defining images, buffers, input/output variables and all the binding slots and stuff? Or would this just be for the OpenCL use case? (Or maybe all of this can be solved 'easily'?)
I see they want to compile C++ to SPIR-V 🤦♂ RUN AWAY! 😱
jbsolomon commentedon Mar 6, 2020
I think the focus here should be on generating MLIR / SPIR-V. Tooling and other stuff (like whether / how to use OpenCL types, how to launch and schedule kernels, native GPU types for components, and all that) isn't out of the question, but can hopefully be done as library layers on top of the core functionality. (Counterargument: if the heterogeneous system is all modeled in one IR, LLVM can optimize across the CPU/GPU boundary, as IREE does.)
Otherwise it runs the risk of being too much work to implement or to become useful in a reasonable amount of time.
If adding a SPIR-V backend requires too many
{GPU,SPIR-V}-specific features into the core language / compiler, then my proposal is to instead add a feature something like a "Zig@dialect" that can be imported by the build tooling as a library (implemented as MLIR dialects?)andrewrk commentedon Mar 19, 2020
With inline assembly, or with target-specific builtin functions.
2 remaining items
fu5ha commentedon Dec 13, 2020
Actually, rust-gpu does not use LLVM's spirv target at all, it manually codegens direct to spirv (the primary motivation for doing it that way is because LLVM's spirv target only supports an opencl/cuda-like execution model, not shader execution models)
ProkopRandacek commentedon Oct 24, 2022
I'm not sure if a SPIR-V target is the best option, since you never actually
run the binary. The binary is (from my experience) usually embedded into an
program at compile time and sent to GPU at runtime, or rarely given to a
program at runtime and sent to GPU at runtime.
My workflow with SPIR-V is always something like this:
which makes some sense in this case, since you need extra compiler for the
GLSL -> SPIR-Vstep.If Zig adds a SPIR-V backend, the workflow would be the same. But I think we
can do better.
I propose that there would be a builtin function that takes a Zig function and
creates a const array of SPIR-V bytecode at compile time.
This would mean that the same functions can be used by CPU and GPU code, in
contrast to the regular approach where the codebase is split into GPU and CPU
side (since they are two different languages and are compiled with two
different compilers...)
It also frees the programmer of the cumbersome task of embedding files into
executables. Although I have to admit that I have not yet explored the Zig
build system and don't know if this problem is already solved.
I imagine it could look something like this:
In this case the OpEntryPoint is constructed from the compute function
signature but I am not sure this is the best way. Further thinking required :D
Alternatively Zig could just expand the
Type.Fnstruct to also include thefunction AST (or one of the IRs). This could entirely offload the SPIR-V
compilation to an external library that would construct SPIR-V bytecode from
Type.Fnstruct. The external library could then have a more flexible way ofspecifying entry point interfaces, spirv target version, optimalisation, ...
It could then look like this:
Should I create a new issue for this proposal?
I think it's neat :D
Snektron commentedon Oct 24, 2022
This is already possible because of Zig's lazy compilation. Just dont reference any of the host functions on the device and vice versa and youre good. The only thing that is required really is conditionally exporting entry points, both for the device and host side:
(instead of conditionally exporting entry points you could also have multiple files that all
@importshared code)ProkopRandacek commentedon Oct 24, 2022
Yeah you are right. Didn't think of that.
@embedFilelooks amazing :D+1
What do you mean by host functions? I didn't find anything in the documentation.
Avokadoen commentedon Oct 24, 2022
@ProkopRandacek Host is usually referring to the CPU, while device refers to GPU.
Robin is saying that if you write code that should only run on the CPU, then the final spirv output will not contain this code as long as you don't call it from any of your GPU code
andrewrk commentedon Apr 9, 2023
This is implemented. Separate issues can be filed for follow-up enhancements.