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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `InstanceIndex` HLSL Function
        </td>
    </tr>

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

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

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

<pre>
    - [ ] Implement `InstanceIndex` clang builtin,
- [ ] Link `InstanceIndex` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `InstanceIndex` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `InstanceIndex` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/InstanceIndex.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/InstanceIndex-errors.hlsl`
- [ ] Create the `int_dx_InstanceIndex` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_InstanceIndex` to  `142` in `DXIL.td`
- [ ] Create the  `InstanceIndex.ll` and `InstanceIndex_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_InstanceIndex` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `InstanceIndex` lowering and map  it to `int_spv_InstanceIndex` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/InstanceIndex.ll`

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 142 | InstanceIndex | 6.3 | ('library', 'intersection', 'anyhit', 'closesthit') |

## SPIR-V

<div id="header">

# [InstanceId](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/InstanceId.html)

## Short Description

<div class="sectionbody">

InstanceId - Id associated with an intersected instance

</div>

</div>

<div id="content" class="loadable">

<div class="sect1">

## <a href="#_description" class="anchor"></a> Description

<div class="sectionbody">

<div class="dlist">

` InstanceId ` 
Decorating a variable in an intersection, any-hit, or closest hit shader
with the ` InstanceId ` decoration will make that variable contain the
index of the instance that intersects the current ray.

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

- <a href="#VUID-InstanceId-InstanceId-04254"
 id="VUID-InstanceId-InstanceId-04254"></a> <span class="vuid">
 VUID-InstanceId-InstanceId-04254 </span>  
  The ` InstanceId ` decoration **must** be used only within the
  ` IntersectionKHR ` , ` AnyHitKHR ` , or ` ClosestHitKHR `
  ` Execution ` ` Model `

- <a href="#VUID-InstanceId-InstanceId-04255"
 id="VUID-InstanceId-InstanceId-04255"></a> <span class="vuid">
 VUID-InstanceId-InstanceId-04255 </span>  
  The variable decorated with ` InstanceId ` **must** be declared using
  the ` Input ` ` Storage ` ` Class `

- <a href="#VUID-InstanceId-InstanceId-04256"
 id="VUID-InstanceId-InstanceId-04256"></a> <span class="vuid">
 VUID-InstanceId-InstanceId-04256 </span>  
  The variable decorated with ` InstanceId ` **must** be declared as a
  scalar 32-bit integer value

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

 
 ### Example 1
```hlsl
//dxc InstanceIndex_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint fn() {
    return InstanceIndex();
}
```
 ### SPIRV Example(s):

 ### Example 2
```hlsl
//dxc InstanceIndex_spirv_test.hlsl -T ps_6_8 -E fn -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0


uint fn( ) : SV_Target {

        return InstanceIndex();
}
```
## HLSL:

The autogenerated index of the current instance in the top-level raytracing acceleration structure.

## Syntax

```
uint InstanceIndex();

```


## Remarks

This function can be called from the following raytracing shader types:

* [**Any Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/any-hit-shader.md)
* [**Closest Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/closest-hit-shader.md)
* [**Intersection Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/intersection-shader.md)





## See also

<dl> <dt>

[Direct3D 12 Raytracing HLSL Reference](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/direct3d-12-raytracing-hlsl-reference.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWF9z4jgS_zTipcuUkYGEBx4IhB3qZm6uktnUvqVkqwFdhOSSZCbcp7-SLGwTSLjczt2mXBXcavefX3erW2LWio1CnJLRHRkteqxyW22ma2b-pZXs5ZofpgmQ0R2Q0QJWu1LiDpUDMk5XyjqmClwpjq9knEIhmdpAXgnphCJ0TtJZ--lXoV6ufwU_hdt6tq208lkoZ4SyorD9LRmnJ_JmnIPFHYNii8WLhbU2F8U77clzz_Tl6-PXu1rPslKFE1rNmZSeSyjP9Yg7FjiF2vSLsryks9AcN6iu6LvfCddRd_9amlbN_LdIvqbDoXU2SgwwEbr0NEKXc83xN1ReB6HLiJ4ldHliUN_D-C5yH4j3SETZwdbVuewEjdHGXlQxN8gcgtuily2Ue-avz2dQNeGNuKyacC-EwcL90Xf8Y8mLP1Zfv5ffWFkKtfEi9fojhU6DXx4MaRsML-JjPWdh7tc5wxQ_W3qOoNQcNcC1Hin3u7PwEbqMrhK6vA6iLfefQfHxH6uHpwu-rRSEJS_KVKEOHlFi4bTxGQnFieIzhVL_RCPUJgCwYyWAcDGLPjAy1Nc7Wkk2I9nMhrfGfDJO-5fw8EKSJ8hZ8YKKB4yhYBav4Bx0E7r06Zq0G8tZxciYzP6hGaEZHCNU027m4FMGvpe-SqHz_ne2q98ft4yjgW-ao-wSHh3boPWUKChp_-DK-zkh6QgaDG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