[llvm] [DirectX] Implement `memcpy` in DXIL CBuffer Access pass (PR #144436)

Deric C. via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 17 13:43:00 PDT 2025


Icohedron wrote:

While the implementation appears to be correct via opt, usage via clang when compiling a shader can fail to produce correct output in some cases. I am currently investigating why this may be the case.

Example shader:
```hlsl
// File: Reproduce.hlsl
uint32_t foo(uint32_t2 v[2], uint32_t i) { return v[i]; }
RWStructuredBuffer<uint32_t> output;
cbuffer Constants {
  uint32_t2 v[2];
}
[numthreads(1, 1, 1)] 
void CSMain(uint3 Tid : SV_DispatchThreadID) {
  output[Tid.x] = foo(v, Tid.x);
}
```

Comparing the output of clang vs opt when running the dxil-cbuffer-access pass:
```
$ clang-dxc -E CSMain -T cs_6_2 -enable-16bit-types Reproduce.hlsl -mllvm -stop-before=dxil-data-scalarization > clang-result.ll
$ clang-dxc -E CSMain -T cs_6_2 -enable-16bit-types Reproduce.hlsl -mllvm -stop-before=dxil-cbuffer-access | opt -S -dxil-cbuffer-access -mtriple=dxil--shadermodel6.2-compute > opt-result.ll
$ diff clang-result.ll opt-result.ll | rg cbufferrow
<   %2 = call { i32, i32, i32, i32 } @llvm.dx.resource.load.cbufferrow.4.i32.i32.i32.i32.tdx.CBuffer_tdx.Layout_s___cblayout_Constantss_24_0tt(target("dx.CBuffer", target("dx.Layout", %__cblayout_Constants, 24, 0)) %Constants.cb, i32 0)
<   %8 = call { i32, i32, i32, i32 } @llvm.dx.resource.load.cbufferrow.4.i32.i32.i32.i32.tdx.CBuffer_tdx.Layout_s___cblayout_Constantss_24_0tt(target("dx.CBuffer", target("dx.Layout", %__cblayout_Constants, 24, 0)) %Constants.cb1, i32 0)
>   %memcpy.agg.tmp3.i.v.load = call { i32, i32, i32, i32 } @llvm.dx.resource.load.cbufferrow.4.i32.i32.i32.i32.tdx.CBuffer_tdx.Layout_s___cblayout_Constantss_24_0tt(target("dx.CBuffer", target("dx.Layout", %__cblayout_Constants, 24, 0)) %Constants.cb, i32 0)
>   %memcpy.agg.tmp3.i.v.load2 = call { i32, i32, i32, i32 } @llvm.dx.resource.load.cbufferrow.4.i32.i32.i32.i32.tdx.CBuffer_tdx.Layout_s___cblayout_Constantss_24_0tt(target("dx.CBuffer", target("dx.Layout", %__cblayout_Constants, 24, 0)) %Constants.cb, i32 1)
< uselistorder ptr @llvm.dx.resource.load.cbufferrow.4.i32.i32.i32.i32.tdx.CBuffer_tdx.Layout_s___cblayout_Constantss_24_0tt, { 1, 0 }
```

https://github.com/llvm/llvm-project/pull/144436


More information about the llvm-commits mailing list