www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [GSoC 2026] DCompute Vulkan Backend

reply Quang Ha <ha.phamquang.dev gmail.com> writes:
Hi everyone,

I am making this thread to share my weekly updates for the 
**DCompute Vulkan Backend** project.

Here are the 3 main parts of my project (Mentored by Nicholas 
Wilson):

* **LDC Compiler:** Connect D's data types with Vulkan's memory 
rules by adding a wrapper kernel. I will also fix pointer sizes 
to make correct SPIR-V files.
* **Testing:** Check the compiled `.spv` files with Vulkan tools 
to make sure the memory works right.
* **DCompute Runtime:** Build the host-side system to start the 
Vulkan device and run the code.

I will post my progress, problems, and questions as replies to 
this thread every week. This helps keep everything in one place.

I am excited to share my work with you all!

GitHub: 
[https://github.com/gnavdev28](https://github.com/gnavdev28)
May 09
parent reply Quang Ha <ha.phamquang.dev gmail.com> writes:
Hi everyone, here is my progress update for **Week 1** of the 
DCompute Vulkan Backend project.


   * Squashed [Nicholas's 
PR](https://github.com/ldc-developers/ldc/pull/4958) to 
thoroughly analyze the required implementation details in 
`targetVulkan.cpp` and `dcomputecodegenerator.cpp`.

   * Investigated the `TODO` in `dcomputecodegenerator.cpp` and 
implemented support for parsing the `vulkan-{version}` target 
value by adapting the existing OpenCL (OCL) and CUDA logic.

   * Updated `cl_options.cpp` to include Vulkan in the 
`--mdcompute-targets` flag's description.

   * Addressed the hardcoded 64-bit pointer assignment in 
`targetVulkan.cpp` to fix the layout issue for different pointer 
sizes.

   * Refactored relevant files by updating the LLVM version macro 
checks from `LLVM_LDC_VER >= 2100` to `LLVM_VERSION_MAJOR >= 23`.


   * **The .spv file output problem:** I used the 
`--mdcompute-targets` flag to make a `.spv` file, but it was in 
COFF format, not real SPIR-V. It was very strange. When I added a 
logging line just to print things out, it made correct SPIR-V 
code. After that, I built the newest LLVM 23 (on May 24), and it 
went back to COFF format. Nicholas and I didn't understand why. 
Finally, I fixed it. I copied the "HACK" part from 
`DComputeTarget::emit` to `DComputeTarget::writeModule`. Now it 
works normally.

   * **Build error:** I got a linker error when building. But it 
was easy to fix. I just changed the paths in the `CMakeLists.txt` 
file.

   * **The TODO part:** There is a `TODO` for `spirv.Layout` in 
`targetVulkan.cpp`. It is very difficult to understand. So, I 
haven't touched it yet. I need more time to study it.


   * **Look into spirv.Layout:** I will spend time studying the 
`TODO` about `spirv.Layout` in `targetVulkan.cpp` because I 
didn't finish it this week.

   * **Write more tests:** I will write some more test files to 
make sure the compiler always makes the correct `.spv` files.

   * **Update my PR:** I will wait for Nicholas to finish 
reviewing my PR. Then, I will update the code and fix any issues 
he points out.


* Commits/PRs: https://github.com/ldc-developers/ldc/pull/5132
* My Personal Blog: Will update soon!

Feel free to leave any feedback or suggestions!
May 24
parent reply Quang Ha <ha.phamquang.dev gmail.com> writes:


Hi everyone, here is my progress update for **Week 2** of the 
DCompute Vulkan Backend project.


    * **Updated `target.cpp`:** I added `gABI = abi; gIR = _ir; 
gTargetMachine = targetMachine;` to `writeModule` to help the 
compiler write the correct file format.

    * **Added missing tags:** I added ` CompileFor.deviceOnly` to 
`test_vulkan.d` so it can emit the module. I also added the 
` kernel()` tag to the test functions so the compiler knows the 
entry point.

    * **Fixed 3 big crashes in `targetVulkan.cpp`:** This was my 
most important work this week. I updated the LLVM intrinsic calls:
       * Deleted `i1false` from the `handlefrombinding` call to 
fix the bad signature.
       * Added `i32zero->getType()` and changed `i32one` to 
`i32zero` to fix the `getpointer` invalid index.
       * Wrote a `CreateStructGEP` loop to get each `fieldTy` and 
load them one by one. This replaced the old `CreateAlignedLoad` 
for the big struct and fixed the element type error.
    * **Checked `isWritable`:** I used `spirv-dis` to check the 
`TargetExtType`. I saw `OpMemberDecorate %_struct_20 0 
NonWritable`. I am researching if the number should be `0` or `1`.


    * **The COFF file problem:** At first, my compiler made a COFF 
file, not a `.spv` file. Sometimes it was right, sometimes wrong. 
It was very strange until I updated `writeModule`.
    * **Missing Kernel and "not set" spam:** The `.spv` file did 
not have `OpEntryPoint` and `_kernel`. Also, when I put a log 
inside `addKernelMetadata`, my screen was full of `"-> 
dcomputetarget: not set"`. I found out I just missed the 
` CompileFor.deviceOnly` and ` kernel()` tags.
    * **The Compiler Crashes:** Right after the file emitted, the 
compiler crashed 3 times in a row! First, it had a bad signature. 
Second, it had an invalid index. Third, LLVM did not let me load 
a big struct at the same time. I had to debug and fix all of them 
in `targetVulkan.cpp`.


    * **Update my PR:** I will wait for Nicholas to check my pull 
request and I will update my code.
    * **Find `isWritable` answer:** I will find the final answer 
for the `isWritable` number (0 or 1).
    * **Write more tests:** I will write more test files to make 
sure everything works perfectly.


* Commits/PRs: https://github.com/ldc-developers/ldc/pull/5132
* My Personal Blog: Updating soon!
Jun 01
parent Quang Ha <ha.phamquang.dev gmail.com> writes:


Hi everyone, this is my update for Week 3. This week, I did not 
write a lot of new code for the compiler. Instead, I focused on 
testing to make sure everything works correctly in real life!


    * **Added a new test file:** I wrote another test file for the 
compiler to generate the `.spv` file.
    * **Cloned a test project:** I cloned a repository called 
`vulkan_minimal_compute` from GitHub.
    * **Tested the Vulkan API:** I changed some code in the 
`vulkan_minimal_compute` project. I want to use it to run my 
generated `.spv` file. This helps me check if the Vulkan API can 
read and use my file correctly.


    * **Understanding the real environment:** It takes time to 
read and understand the `vulkan_minimal_compute` repo. Setting up 
a real Vulkan environment to test the `.spv` file is a bit slow 
and requires a lot of checking.


    * **Finish the API test:** I will finish modifying the 
`vulkan_minimal_compute` project and fully test my `.spv` file.
    * **Fix bugs (if any):** If the Vulkan API finds any problems 
with my `.spv` file, I will go back to the compiler and fix them.
    * **Wait for PR review:** I am still waiting for Nicholas to 
review my pull request so I can update my code.


* Commits/PRs: https://github.com/ldc-developers/ldc/pull/5132
* My Personal Blog: https://gnavdev28.github.io (nothing special 
yet :D)
Jun 09