<rss version="2.0">
  <channel>
    <title>Ali Cheraghi</title>
    <link>https://alichraghi.github.io</link>
    <description>Ali Cheraghi</description>
    <language>en-US</language>
    <lastBuildDate>Thu, 18 Jun 2026 00:35:32 +0000</lastBuildDate>
    
      <item>
        <title>Zig and GPUs</title>
        <description>&lt;p&gt;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 &lt;a href=&quot;https://rust-gpu.github.io/&quot; target=&quot;_blank&quot;&gt;Rust&lt;/a&gt; and &lt;a href=&quot;https://ziglang.org&quot; target=&quot;_blank&quot;&gt;Zig&lt;/a&gt; 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.&lt;/p&gt;&lt;h3&gt;SPIR-V, PTX and AMDGCN&lt;/h3&gt;&lt;p&gt;Zig’s GPU backend support has been steadily evolving. After &lt;a href=&quot;https://github.com/ziglang/zig/pull/7827&quot; target=&quot;_blank&quot;&gt;roughly 4 years&lt;/a&gt;, 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 &lt;a href=&quot;https://devblogs.microsoft.com/directx/directx-adopting-spir-v/&quot; target=&quot;_blank&quot;&gt;soon DirectX&lt;/a&gt;. Zig can also use &lt;a href=&quot;https://llvm.org&quot; target=&quot;_blank&quot;&gt;LLVM&lt;/a&gt; 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.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;https://alichraghi.github.io/blog/zig-gpu/graph.svg&quot;&gt; &lt;pre&gt;&lt;code class=&quot;zig&quot;&gt;&lt;span class=&quot;comment_documentation comment spell&quot;&gt;// SPIR-V:&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     zig build-obj -target spirv64-vulkan-none -mcpu vulkan_v1_2+int64 \&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     -ofmt=spirv -fno-llvm kernel.zig&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     zig build-obj -target spirv64-opencl-none -mcpu opencl_v2+int64 \&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     -ofmt=spirv -fno-llvm kernel.zig&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;// PTX:&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     zig build-lib -dynamic -target nvptx64-cuda-none -mcpu &amp;lt;gpu-model&amp;gt; \&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     -femit-asm -fno-emit-bin -fno-ubsan-rt kernel.zig&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;// AMDGCN:&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     zig build-lib -dynamic -target amdgcn-amdhsa-none -mcpu &amp;lt;gpu-model&amp;gt; \&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//     -fno-compiler-rt kernel.zig&lt;/span&gt;
&lt;span class=&quot;comment_documentation comment spell&quot;&gt;//&lt;/span&gt;

&lt;span class=&quot;keyword_import&quot;&gt;export&lt;/span&gt; &lt;span class=&quot;keyword_function&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;constant variable_builtin type variable function&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword_modifier&quot;&gt;callconv&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation_delimiter&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;constant variable_builtin type variable&quot;&gt;kernel&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;type_builtin&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;punctuation_bracket&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;punctuation_bracket&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;For a complete example, checkout &lt;a href=&quot;http://github.com/snektron/shallenge/&quot; target=&quot;_blank&quot;&gt;snektron/shallenge&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;Vulkan vs. OpenCL&lt;/h3&gt;&lt;p&gt;Today, Vulkan and OpenCL are the two major SPIR-V environments — but they diverge in a few aspects. in OpenCL, capabilities like &lt;code&gt;Kernel&lt;/code&gt; and &lt;code&gt;Addresses&lt;/code&gt; 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 &lt;code&gt;50%&lt;/code&gt; of &lt;a href=&quot;https://github.com/ziglang/zig/tree/master/test/behavior&quot; target=&quot;_blank&quot;&gt;Zig’s behavior tests&lt;/a&gt; and &lt;code&gt;~75%&lt;/code&gt; 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.&lt;/p&gt;&lt;h3&gt;Challenges&lt;/h3&gt;&lt;p&gt;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 &lt;code&gt;addrspace&lt;/code&gt; 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 &lt;code&gt;OpPtrCastToGeneric&lt;/code&gt;. Unfortunately, Vulkan doesn’t support that, so as a temporary workaround, all pointers are assumed to be local (&lt;code&gt;Function&lt;/code&gt;) memory.&lt;/p&gt;&lt;p&gt;Both Vulkan and OpenCL expose hardware-accelerated math instructions via &lt;code&gt;OpExtInst&lt;/code&gt;, which sometimes can be mapped directly to Zig’s &lt;code&gt;@builtin&lt;/code&gt;s target-specific instructions when available. However, subtle differences exist. For example, unlike Zig or OpenCL, some Vulkan instructions (&lt;code&gt;fma&lt;/code&gt;, &lt;code&gt;sqrt&lt;/code&gt;, &lt;code&gt;exp&lt;/code&gt;, &lt;code&gt;log&lt;/code&gt;) do &lt;em&gt;NOT&lt;/em&gt; guarantee correctly rounded results.&lt;/p&gt;&lt;h3&gt;What’s next?&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Composite integers&lt;/li&gt;&lt;li&gt;&lt;code&gt;spirv-val&lt;/code&gt; should never fail on a binary emitted by Zig. As Zen says, “Compile errors are better than runtime crashes”.&lt;/li&gt;&lt;li&gt;Increase passing behavior tests.&lt;/li&gt;&lt;li&gt;Provide bindings to CUDA/HIP runtime.&lt;/li&gt;&lt;li&gt;Add or extend a set of common algorithms in stdlib to work under GPUs (prefix sum, reduction, matmul, etc).&lt;/li&gt;&lt;li&gt;You tell!&lt;/li&gt;&lt;/ul&gt;</description>
        <link>https://alichraghi.github.io/blog/zig-gpu/</link>
        <pubDate>Thu, 17 Apr 2025 00:00:00 +0000</pubDate>
        <guid>https://alichraghi.github.io/blog/zig-gpu/</guid>
      </item>
    
      <item>
        <title>Donate</title>
        <description>&lt;p&gt;If you like my work, please consider donating to one of these addreses! Don’t hesitate to reach me out via &lt;a href=&quot;mailto:alichraghi@proton.me&quot; target=&quot;_blank&quot;&gt;email&lt;/a&gt; or by the &lt;code&gt;alichraghi&lt;/code&gt; username in discord and telegram to let me know who you are or if you have any questions!&lt;/p&gt;&lt;h3&gt;Monero&lt;/h3&gt;&lt;p&gt;&lt;code&gt;43S2tCrmsURechR5n1jdGyRG951roMReZNdvGYX9V1T5huT2dgM9JLKCCCXpNXox2BFz3HsScDDQeLLTbPSrxYX8HRvwCSW&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;USDT (BEP20)&lt;/h3&gt;&lt;p&gt;&lt;code&gt;0x55948246A13181863b4Db18330f87438Fb403b26&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Contact me if you couldn’t find your preferred crypto-currency.&lt;/h3&gt;</description>
        <link>https://alichraghi.github.io/donate/</link>
        <pubDate>Wed, 16 Apr 2025 00:00:00 +0000</pubDate>
        <guid>https://alichraghi.github.io/donate/</guid>
      </item>
    
      <item>
        <title>Art</title>
        <description></description>
        <link>https://alichraghi.github.io/art/</link>
        <pubDate>Wed, 16 Apr 2025 00:00:00 +0000</pubDate>
        <guid>https://alichraghi.github.io/art/</guid>
      </item>
    
  </channel>
</rss>
