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

    <tr>
        <th>Summary</th>
        <td>
            [HLSL][SPIRV] Generate sign extended image instructions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            HLSL,
            backend:SPIR-V
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            s-perron
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          s-perron
      </td>
    </tr>
</table>

<pre>
    In HLSL, a RWBuffer<int> is suppose to be loading value as if they are signed integers. This is important because if the format of the image is 8-bit, then the value on a load is implicitly sign extended. The problem in the current Clang implementation is that the signedness of the sampled type is lost because llvm-ir interpreted whether or not an integer is sign or not based on the instruction that is using it.

Without knowing the signedness of the sample type, SPIR-V generates all code assuming the integers are unsigned. This causes problems.

There are two possible solutions in the SPIR-V:

1. Pass information along so that the sampled type can be modified to be a signed integer.
2. Pass information along so that the `OpImage*` instructions can get the `signExtend` image operand when necessary.


The difficulty with 1 is that the sampled type of the image must match the Texel components of the `OpImage*` instructions. This means that the type information will have to propagate further.

Seems like 2 is the best solution. It is a more local change, making the code generation easier.

Now we still need to figure out how to pass the information from the FE to the BE. There are again a few options

1. Use a new target type that has a bit for `isSigned` as is done for DXIL.
2. Incorporate the sign into the type for the `spirv.Image` type.
3. Add a target attribute to the "gethandlefrombinding" call.
4. Add an annotation to the loads and stores.

Option 4 does not work. The annotation would have to be metadata, which the optimizer is allowed to drop.

Option 2 does not work well either. It would require special handling to make sure the sampled type matches other integers. Not a reasonable option.

To know if 1 will work, I'll have to see if the SPIR-V backend has easy access to the lllvm type when generating the load and stores. The SPIR-V type will have lost the signedness informations.

If that does not work, we will have to go with option 3. However, that has it own uglyness.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVt1ynDgTfRrmpstTIDyOczEX_v0yVakkFedL9raBBrQWEisJk9mn3-oWjMfZqtRe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