<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/122553>122553</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [HLSL] cbuffer: Create host layout struct and add resource handle to AST
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            HLSL
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          hekota
      </td>
    </tr>
</table>

<pre>
    The `cbuffer` language syntax allows declarations inside `cbuffer` context that do not contribute to the constant buffer layout, such as static variables, function definitions, declarations of classes and namespaces, resources, or empty struct and zero-sized arrays. As part of semantic analysis we need to create a "layout struct" for the constant buffer that will only include elements that affect the buffer memory layout. This layout struct will be used as the contained type of the `cbuffer` resource handle type and for layout calculations in codegen.

If the constant buffer includes a struct that contains any of the above undesirable declarations, a new version of this struct should be created with these declarations filtered out as well.

The buffer layout struct will be declared within the HLSLBufferDecl context and will be followed by 'cbuffer` resource handle declaration that will reference the layout struct as its contained type.

For example:
```
struct Something {
  int a;
  float f[0];
};

cbuffer CB {
    float x;
    RWBuffer<float> buf;
    Something s;
}
```
The buffer layout should look like this:
```
;   struct hostlayout.CB
;   {
; float x;                                      ; Offset:    0
; struct hostlayout.struct.Something
;       {
;           int a; ; Offset:   16
;       } s; ; Offset:   16
;   } CB; ; Offset:    0 Size:    20
;
```
And the resource handle added to the `HLSLBufferDecl` will look like this:
```
__hlsl_resource_t [[hlsl::resource_class(CBuffer)]] [[hlsl::contained_type(hostlayout.CB)] __handle;
```

https://godbolt.org/z/j37r1Tede
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVl1v6jgT_jXmZlQUnAbaCy74eNG70pFW2lbaSzSJJ8SnxkYep23661d2EjhAdXajSk3G8_E8z4xtkFkfLNFSFGtRbCfYhsb5ZUNvLuCkdKpbvjYEYp5VZVvX5MU8A4P20OKBgDsb8BPQGPfBoK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