Zig 和 GPU
Zig and GPUs

原始链接: https://alichraghi.github.io/blog/zig-gpu/

Zig 正在成为一种可行的 GPU 编程方案,为传统的 C++ 提供了一种现代替代方案。其自托管的 SPIR-V 后端正在成熟,允许对 Vulkan 和 OpenCL 的着色器和计算内核进行实验。Zig 还可以利用 LLVM 生成原生 PTX(NVIDIA)和 AMDGCN(AMD)代码,从而绕过 CUDA、HIP 或 HLSL。 Vulkan 和 OpenCL 虽然都使用 SPIR-V,但在功能可用性方面存在差异。OpenCL 保证了诸如内核和地址之类的功能,从而可以使用指针运算。Vulkan 的基本功能更为有限。目前,Zig 的 SPIR-V 后端在 Vulkan 1.2 的行为测试中通过率为 50%,在 OpenCL 中为 75%。 挑战包括由于 SPIR-V 的要求而必须显式指定地址空间,这需要针对 Vulkan 兼容性进行变通处理。Zig、OpenCL 和 Vulkan 之间硬件加速数学指令的精度也存在差异。 未来的计划包括改进 SPIR-V 验证,提高通过测试率,提供 CUDA/HIP 运行时绑定,向标准库添加兼容 GPU 的算法,以及整合社区反馈。

Hacker News 上关于使用 Zig 进行 GPU 编程的讨论,重点突出了其挑战和潜力。一位用户指出,虽然业余项目很有趣,但在 GPU 供应商直接支持像 Zig 这样的新语言之前,C/C++ 仍然可能是主要的选项。目前的真正性能依赖于 CUDA 和 Metal 等厂商特定的工具包。 另一条评论询问了 AMD GPU 架构(AMDGCN 与 RDNA)和 ISA 的可靠性。文章作者回应说,LLVM 使用“amdgcn”作为通用的架构名称,具体的型号由标志来处理。虽然 NVIDIA 的 PTX 规范是公开的,但实际的硬件汇编指令并非如此,尽管存在查看它的工具。该主题还链接到相关的 Rust GPU 项目,包括 CUDA 和通用 GPU 编程。

原文

GPU programming used to mean wrangling C++ compilers, bloated SDKs, and vendor-specific toolchains. That’s changing. You can now write GPU code in modern languages like Rust and Zig with fewer layers. This post walks through the current state of Zig’s GPU backends and how they stack up across Vulkan, OpenCL, and native ISAs.

SPIR-V, PTX and AMDGCN

Zig’s GPU backend support has been steadily evolving. After roughly 4 years, the self-hosted SPIR-V backend is now mature enough to experiment with and port basic shaders and compute kernels. In case you don’t know, SPIR-V is an intermediate typed IR used by Vulkan, OpenCL, and soon DirectX. Zig can also use LLVM to directly generate PTX (NVIDIA) and AMDGCN (AMD) code, producing native binaries that can be loaded at runtime. This opens the door to writing high-performance GPU code without needing to touch CUDA, HIP, or HLSL.
















export fn kernel() callconv(.kernel) void {}

For a complete example, checkout snektron/shallenge.

Vulkan vs. OpenCL

Today, Vulkan and OpenCL are the two major SPIR-V environments — but they diverge in a few aspects. in OpenCL, capabilities like Kernel and Addresses are guaranteed to be available. As a result, pointer arithmetic and casting are permitted. Whereas the baseline features for Vulkan and OpenGL environments tend to be more limited. This is why when targeting baseline Vulkan 1.2 features, Zig’s SPIR-V backend passes about 50% of Zig’s behavior tests and ~75% for the OpenCL target. Note that these numbers are not expected to get significantly higher since a lot of these tests are not meant to be working in GPUs anyway.

Challenges

One core challenge in targeting SPIR-V from a general-purpose language like Zig is the need to explicitly specify address spaces (a.k.a. storage classes). Zig supports this via the addrspace keyword, but nearly all Zig code assumes pointers live in the generic address space. That works fine until you need to lower to SPIR-V, where we sometimes rely on OpPtrCastToGeneric. Unfortunately, Vulkan doesn’t support that, so as a temporary workaround, all pointers are assumed to be local (Function) memory.

Both Vulkan and OpenCL expose hardware-accelerated math instructions via OpExtInst, which sometimes can be mapped directly to Zig’s @builtins target-specific instructions when available. However, subtle differences exist. For example, unlike Zig or OpenCL, some Vulkan instructions (fma, sqrt, exp, log) do NOT guarantee correctly rounded results.

What’s next?

  • Composite integers
  • spirv-val should never fail on a binary emitted by Zig. As Zen says, “Compile errors are better than runtime crashes”.
  • Increase passing behavior tests.
  • Provide bindings to CUDA/HIP runtime.
  • Add or extend a set of common algorithms in stdlib to work under GPUs (prefix sum, reduction, matmul, etc).
  • You tell!
联系我们 contact @ memedata.com