[NNNN] Add proposal for HLSL ConstantBuffer<T>#413
[NNNN] Add proposal for HLSL ConstantBuffer<T>#413
Conversation
This proposal details the implementation of the `ConstantBuffer<T>` resource type in Clang and LLVM for HLSL. Unlike the legacy `cbuffer`, `ConstantBuffer<T>` behaves as a standard type, supporting instantiation, arrays, function parameters, and assignments. It acts as a drop-in replacement for `T` via an implicit conversion operator to `const T &`. The proposal details the Sema member lookup interception required and the corresponding CodeGen which targets the appropriate constant address spaces via resource intrinsics.
|
@hekota @bogner @llvm-beanz Hey, here is a draft design for I still have to figure out the implicit conversion to |
hekota
left a comment
There was a problem hiding this comment.
Looks good, I just wonder whether we need to add the new llvm intrinsic.
We are also going to need to decide if we are going to be supporting resources inside T. DXC seems to support it unless ConstantBuffer<T> is in an array: https://godbolt.org/z/11dTdhzh1
That is an awkward one. It never worked for SPIR-V: https://godbolt.org/z/Mqvq4cd6Y. I would like to disallow it, but we can talk about this more. I'll update the proposal to disallows resources in ConstantBuffers, and then we can have a discussion in the appropriate meeting. |
| 4. **Sema Constraints:** Enforce that `T` must be a user-defined struct or | ||
| class, and reject primitive types, vectors, arrays, or matrices as `T`. |
There was a problem hiding this comment.
What about methods? Should we start enforcing those have a const this?
This somehow is accepted but should IMO not: https://godbolt.org/z/avE8zTKb7
There was a problem hiding this comment.
As I wrote the implementation, your example would be rejected because the member expression would add a cast to const hlsl_constant& T. Since the call to foo is not const it will create an error.
However that is a good example that I need to add. If DXC allowed call to non-const member function, should we allow them too? At what point do we issue an error? Note that your example fails when generating DXIL. I'm not sure why it works for spir-v. We probably have a bad optimization removing the invalid store because the spir-v is invalid.
There was a problem hiding this comment.
I think we had a couple issues open in DXC for this weirdness,IMO the fact dxc allows this is an oversight, and we should not support this. Maybe something we can specify in hlsl 202x
There was a problem hiding this comment.
I left this as a open question. We should get more people to chime in on. With a good compiler error it might not be too hard for devs to add const where needed if we do require const.
I would also be in favor of disallowing resources in |
You definitely weren't ever supposed to be allowed to put a resource in |
This proposal details the implementation of the
ConstantBuffer<T>resource type in Clang and LLVM for HLSL.
Unlike the legacy
cbuffer,ConstantBuffer<T>behaves as a standardtype, supporting instantiation, arrays, function parameters, and
assignments. It acts as a drop-in replacement for
Tvia an implicitconversion operator to
const T &. The proposal details the Sema memberlookup interception required and the corresponding CodeGen which targets
the appropriate constant address spaces via resource intrinsics.