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

    <tr>
        <th>Summary</th>
        <td>
            [HLSL] Enable protection code for destruction of local statics
        </td>
    </tr>

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

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

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

<pre>
    The usual way to set up a destructor call for a local static is to use atexit, but HLSL doesn't have one of those. Instead, we currently add the destructor to the global destructor vector and generate it at the bottom of the entry function: https://github.com/llvm/llvm-project/blob/f05145cd31e92c73301e308a6e13c581af3076ce/clang/lib/CodeGen/MicrosoftCXXABI.cpp#L2385-L2387

The flaw in this is that it will be called regardless of whether the construtor was encountered at all. This is inconsistent with how C++ operates and potentially damaging. Instead, we need to reuse the guard variable and bit offset that is used to ensure the constructor only gets called once: https://github.com/llvm/llvm-project/blob/f05145cd31e92c73301e308a6e13c581af3076ce/clang/lib/CodeGen/MicrosoftCXXABI.cpp#L2704-L2708

To do this, we'll have to keep some map of the variable and offset to the associated destructor call. That could either be added to the CXXGLobalDtorsOrStermFinalizers vector or stored in a map within CGHLSLRuntime for use when generating the global destructor calls. 

This is specific to the version of HLSL that adds destructors, which looks to be 202x.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVM1u4zYQfhr6MlhDoiTLPuiQOHW6QIoC3T3kSpEjiQ3NEciRve7TF6RiNFmgh956oQCbM5z5_lSMdvSInWgeRfO0UQtPFLqZrlK7t01P5tZ9nxCWuCgHV3UDJojIsMygwGDksGimAFo5BwMFUOBIKweRFVsNNqaKJSIoxh-WhTxCvzD8-vLtBQxh9EK2DJO6IJBHoAF4oohb-OojozKp4IqglxDQs7uBMgZ4wo-PM-VfRke9ch__uGD-KG9gRI9BMYJlUJzv98RM5_VJBPQcbjAsXrMlL6oHmJj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