<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/111566>111566</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[HLSL] Use constructors for resource handle initialization
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hekota
</td>
</tr>
</table>
<pre>
Resource handle initialization should be done via resource class constructors. If a resource is declared in a global variable, the compiler should make sure to call a constructor that takes binding information, which will later be translated to `dx.handle.fromBinding`.
Resources declared as local variables would use a default constructor that will initialize the handle to 'empty' or 'null', or simply leave it undefined. Another resource can be assigned to these local variables and a copy constructor and/or assignment operator will take care of copying the resource handle.
This should greatly simplify handling of resources, including when they are dynamically bound, used as function in/out parameters, declared in unbounded arrays or used defined types.
Resource classes should look like this:
```
struct Binding;
template <typename T> struct RWBuffer {
private:
using handle_t = __hlsl_resource_t
[[hlsl::contained_type(T)]] [[hlsl::resource_class(UAV)]];
handle_t h;
public:
// For uninitialized handles
RWBuffer() { h = __builtin_hlsl_create_poison_handle(); }
// Resources are copyable.
RWBuffer(RWBuffer &LHS) = default;
// For handles constructed as globals with binding information, or from the
// descriptor heap.
RWBuffer(Binding &B) { h = __builtin_create_handle_from_binding(B); }
~RWBuffer() = default;
RWBuffer &operator=(RWBuffer &LHS) {
h = LHS.h;
return *this
}
...
}
```
Follow-up from #105076.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VkuPqzgT_TXOpnQRMUlIFlkkN1_Un9Sr231nlpGBInja2MiPZDKL-e2jMpBXP9SiG-GqOufUqQLhnDxqxDWbb9l8NxHBN8auG_wwXkwKU13Wv9CZYEuERuhKIUgtvRRK_iO8NBpcY4KqoECojEY4SQF2jCiVcA5Ko523ofTGugT-X8PdCemgwlIJixVIDQKOyhRCwUlYKQqFjP8E3yCUpu2kQjuWa8UHggsWwRsohVIg7uuAb4QH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