Testing HLSL with the LLVM Offload Test Suite in WSL 2

Search for a command to run...

No comments yet. Be the first to comment.
Today we are deconstructing the 4-Star Dragon Ball shader (available on Shadertoy here). This shader is a good example of using 2D math to drive 3D optical effects. To understand it, we must reference

In Part 1, I talked about the high-level "magic" happening on your wrist. How Apple uses neural networks and math to guess your VO2Max even when you’re just walking around the block. But if you're lik
I am a data junkie. I track, measure, and optimize. So, when a severe ankle injury recently sidelined me, the hardest part wasn't the physical pain it was watching my hard earned fitness metrics plumm

In my previous post, I detailed how our move from the Android Emulator to standalone Android-x86 VHDX images gave us impressive cost saving wins for our fuzzing budget. With any infrastructure pivot,

When I was the head of Microsoft's Edge security for the US market, we ran into an infrastructure challenge that forced us to completely rethink how we fuzzed Android. The project we built no longer e
If you are a developer working with HLSL or GPU compilers you likely prefer working in Linux. You might have felt sidelined because Linux lacks a dedicated DirectX device. However, if you are on Windows, you have a powerful tool at your disposal: Windows Subsystem for Linux (WSL2).
Since 2020, WSL 2 has supported DirectX, exposing the DxCore (libdxcore.so) and D3D12 (libd3d12.so) APIs. This bridge allows you to run the Offload test suite directly within your Linux environment by leveraging the underlying Windows hardware.
The LLVM Offload Test Suite is an experimental runtime test suite designed specifically for HLSL (High-Level Shader Language). It provides a framework for verifying the correctness of GPU code generated by compilers like Clang and DXC.
By using a YAML-based pipeline description, it allows developers to define GPU workloads, buffers, and expected results, which are then executed across various backends including DirectX 12, Vulkan, and Metal. It is a critical tool for ensuring that HLSL features function correctly on real hardware or software emulators.
Before diving in, ensure you are using a WSL 2 distribution (glibc-based, such as Ubuntu).
You need the WSL-specific headers and libraries to bridge the gap between Linux and Windows D3D12. Specifically, you need libDirectX-Guids.a and libd3dx12-format-properties.a.
directx-headers-dev package.Follow the standard LLVM instructions to add the experimental runtime test suite for HLSL to your LLVM build. Ensure your environment can see the pre-compiled user-mode binaries shipped by Windows at: /usr/lib/wsl/lib
Developing in a bridged environment like WSL isn't without its quirks. Here are the common hurdles and how to resolve them:
Currently, the D3D12 Debug Layer appears to be incompatible with WSL. Calling D3D12GetDebugInterface typically results in a segmentation fault.
Tip: You will have to rely on standard logging, as the debug layer is currently unavailable in this environment.
If your Pipeline State Objects (PSOs) fail to create, it is likely because the shaders are unsigned.
dxv.double free or corruption)You might encounter a double free or corruption (!prev) error when api-query or offloader exit. This often leads to an infinite stall rather than a clean termination.
This issue stems from how Microsoft::WRL::ComPtr::InternalRelease behaves in WSL when more than one device is present.
Workarounds:
DeviceContext::Devices before the program exits usually eliminates the crash.lib/API/DX/Device.cpp to force the system to select only one device. Add a break; at the end of the first iteration of the for-loop in initializeDXDevices().api-query first to find the index of your preferred GPU, then edit the AdapterIndex in the for-loop to target that specific device index.By utilizing the DirectX bridge in WSL 2, you can effectively test HLSL offloading for the DirectX target without ever needing to interact with the host Windows OS. While the memory management in the WSL/D3D12 bridge can be finicky, these workarounds provide a stable path for your experimental compiler development.
Happy testing!
First published April 14th at blog.farzon.org