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

    <tr>
        <th>Summary</th>
        <td>
            [HLSL] Collect explicit resource binding information
        </td>
    </tr>

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

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

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

<pre>
    This is the explicit bindings part of the bindings approach originally described in #58051.

Binding information is recorded in HLSLResourceBinding attributes on declarations. We currently have logic to diagnose these bindings, but we don't store this information to later use in lowering to `llvm.dx.handle.fromBinding` intrinsics. We need to gather all of the explicit resource binding information and create 1:1 mapping of FieldDecl or VarDecl to explicit. HLSLResourceBindingAttr.

In order to share code with the binding diagnostics, we need a couple of adjustments:

1. `SemaHLSL::handleResourceBindingAttr` can continue to diagnose the simple/local errors:
   - `err_hlsl_binding_type_invalid` - the type isn't valid at all, like `x0`
   - `warn_hlsl_deprecated_register_type_i` - the type is deprecated/removed and ignored
   - `warn_hlsl_deprecated_register_type_b` - the type is deprecated/removed and ignored
   - `warn_hlsl_register_type_c_packoffset` - the type is in the wrong place (in a `cbuffer`)
   - `warn_hlsl_overlapping_binding` - the same binding type is applied more than once

   These shouldn't need any traversal of types to diagnose, so keeping them here is simplest.

2. Add a data member, `Bindings`, to `SemaHLSL` to track the ranges that are already bound and the list of `Decl`s that still need to be bound.

3. In `Sema::ActOnVariableDeclarator` (in `SemaDecl.cpp`) after the call to `ProcessDeclAttributes`, call into `SemaHLSL`:
   ```c++ 
 if (getLangOpts().HLSL && NewVD->hasGlobalStorage()) {
 HLSL()->handleBindings(NewVD);
   ```

   This is where the recursive walk that diagnoses binding type mismatches should move. It can fill in the binding map as it goes.

3. The bindings for cbuffer/tbuffer can be diagnosed and applied in `SemaHLSL::ActOnFinishBuffer`.

   Note that doing this here simplifies the parts of the design that need to differentiate between `HLSLBufferDecl ` and `VarDecl`, since the two code paths are separate. 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v47oR_TX0yyCCRCf-ePCDvanbBRb3Ft3F9jEYUWOJG4oUyJG9-fcFScmON1ugBS4QxLY0H4cz58wQQ9CtJdqJp4N4el7gyJ3zu9q1lvyids3b7lunA-gA3BHQz8FopRlqbRtt2wADegZ3Sm-vD3EYvEPVgfO61RaNeYOGgvK6pga0BSGXT5vyqSpE-SzKff5_yO6g7cn5Hlk7G_N6Us432e8fX75--RcFN3pFszkye12PTAGchYaUQZ-cQwH_JlCj92TZvEGHZwLjWq2AHTQaW-sCReThhl3IT1CPDBeCxlkh1wyBnY9msQzvoLEDg0wexkARm3EX8hEQOxCr0phzXzQ_iw5tY6g4eddPiMWqBG3Zaxu0yiAtURP9WuSOPKAxc02vFffTsWekd1jQNqA8IRNUYrmvoMdhiEbuBEdNpnkmZcB5-I4-fWV3jVz8rqp7Zn_XnM8WYhd89AwdegLlGoKL5u597-eyslaplJfpbAjKjYOhCAibH2PgniwHsdy_T1IVsXJfqccIKb5c7nP9fgMvllGhBeUsazvSr02FoPvBkJBH4xQaIO-dv2UEgIeYjbx_6UwwL9MJXvhtoBdtz2h0E3M8pGDxKeiQGZHeAXJsVDyl0a8UY_0sxaq8D39Bb3P8hgZPCpmaF0-tDkx-yvUhC9xshTx66t051tA2oFvrPDX_d476r8txH1i9DKhe3ekUiD_m0Db9vHhnWxgMKgIhN9oCxpiqHk8nio0Ucvvf0rkzeZPZPLfolidgf6PenBSHwWhqoM-yRQvOKnrPMwD4llQfOjeaJjc189S-AXs8kw-YNfg2UHhPrdjv4OCVKAmMO-qhI58yZ8YFvlOOLGDfRAU0yAg99TX5GESsysM8dGIFPk2D40r_VRmfsEf1mg7r0bYRSxeJ5wnQeMLmDWo32ty5aGV0SANZrMqodLEqJ5fA2pjrpKkp-91BXRbw2c4Ysvz2iv-039FrrA09T8PVJfHlTk7W8VWhhiE3E_AUJ2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