Issue 239023
Summary [hlsl][spirv] Incorrect lowering of [[vk::push_constant]] ConstantBuffer<T>
Labels new issue
Assignees
Reporter rj123-nv
    In DXC, a declaration like the following is allowed and is treated as a pointer to push constant memory, in other words a Block *ptr where the Block lives in push constant address space.

```
struct Block {
    uint a;
    float b;
};

[[vk::push_constant]] ConstantBuffer<Block> pc_handle;
```

This is treated by DXC pretty much equivalently to

```
[[vk::push_constant]] Block pc;
```

The 1st declaration would allow a programmer to pass pc_handle somewhere a ConstantBuffer<Block> is normally accepted, for example as a function arg. However, for SPIR-V codegen, this may force inlining/specialization/other restrictions since PushConstant and Uniform storage classes are not interchangeable.

Currently Clang treats "pc_handle" as if it the handle lives in push constant address space rather than the pointee Block data, and fails in backend compilation.

We could add a new intrinsic along the lines of 'handlefrombinding' but for push constants. Then after inlining/other legalization, we could propagate push constant address space to all pointers derived from the handle. But that is just one idea.

Here's an example that shows the need for specialization/inlining/some kind of legalization.

```
  struct Block {
    uint a;
    float b;
  };

  ConstantBuffer<Block> ubo : register(b0);
  [[vk::push_constant]] ConstantBuffer<Block> pc;

  float read_buffer(ConstantBuffer<Block> buffer) {
    return float(buffer.a) + buffer.b;
  }

  float2 main() : SV_Target {
    return float2(read_buffer(ubo), read_buffer(pc));
  }
```

DXC codegen for above:
https://godbolt.org/z/PjeKd3rT9

Clang arguments:  clang --driver-mode=dxc -spirv -T ps_6_0 -E main -O3 -fspv-target-env=vulkan1.3 constantbuffer-push-constant-parameter.hlsl -Fo /tmp/repro.spv
Output (with debug/asserts if that matters, at 762bb48c07b7):
```
  error: store into a read-only SPIR-V storage class is not allowed
  fatal error: error in backend: cannot select: G_STORE %20:pid(s64), %23:pid(p0) :: (store (s64) into %ir.7, addrspace 13) (in function: main)
```

Originally found that this reproduces in 
https://github.com/google/bigwheels/blob/main/assets/basic/shaders/VertexColorsPushConstants.hlsl
Test in dxc using this syntax:
https://github.com/microsoft/DirectXShaderCompiler/blob/main/tools/clang/test/CodeGenSPIRV/vk.push-constant.constantbuffer.hlsl

AI disclosure: I used codex to investigate/find reproducers but wrote this description myself based on own understanding.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to