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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `DispatchRaysIndex` 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 `DispatchRaysIndex` clang builtin,
- [ ] Link `DispatchRaysIndex` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `DispatchRaysIndex` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `DispatchRaysIndex` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/DispatchRaysIndex.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/DispatchRaysIndex-errors.hlsl`
- [ ] Create the `int_dx_DispatchRaysIndex` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_DispatchRaysIndex` to  `145` in `DXIL.td`
- [ ] Create the  `DispatchRaysIndex.ll` and `DispatchRaysIndex_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_DispatchRaysIndex` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `DispatchRaysIndex` lowering and map  it to `int_spv_DispatchRaysIndex` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/DispatchRaysIndex.ll`

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 145 | DispatchRaysIndex | 6.3 | ('library', 'raygeneration', 'intersection', 'anyhit', 'closesthit', 'miss', 'callable') |

## SPIR-V

<div id="header">

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

## Short Description

<div class="sectionbody">

LaunchIdKHR - Launch Id for ray shaders

</div>

</div>

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

<div class="sect1">

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

<div class="sectionbody">

<div class="dlist">

` LaunchIdKHR `  
A variable decorated with the ` LaunchIdKHR ` decoration will specify
the index of the work item being processed. One work item is generated
for each of the ` width ` Ã— ` height ` Ã— ` depth ` items dispatched by
a [vkCmdTraceRaysKHR](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/None) command. All shader
invocations inherit the same value for variables decorated with
` LaunchIdKHR ` .

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

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

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

- <a href="#VUID-LaunchIdKHR-LaunchIdKHR-04268"
  id="VUID-LaunchIdKHR-LaunchIdKHR-04268"></a> <span class="vuid">
  VUID-LaunchIdKHR-LaunchIdKHR-04268 </span>  
  The variable decorated with ` LaunchIdKHR ` **must** be declared as a
  three-component vector of 32-bit integer values

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

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

export uint3 fn() {
    return DispatchRaysIndex();
}
```
 ### SPIRV Example(s):

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

[shader("intersection")]
void fn() {
uint3   ret = DispatchRaysIndex();
}
```
## HLSL:

Gets the current location within the width, height, and depth obtained with the [**DispatchRaysDimensions**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dispatchraysdimensions.md) system value intrinsic.

## Syntax

```syntax
uint3 DispatchRaysIndex();
```

## 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)
* [**Callable Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/callable-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)
* [**Miss Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/miss-shader.md)
* [**Ray Generation Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/ray-generation-shader.md)

## See also

* [Direct3D 12 Raytracing HLSL Reference](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/direct3d-12-raytracing-hlsl-reference.md)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWV1v4rrW_jXujRUUHKBwwQWFMoPemT2v2tmjfVeZeEF86tiR7dByfv3RskMILaXTs8_sqZCKl-318awPLxvunNxqgCkZ3pDh4orXvjB2uuH230arq7UR-2lCyfCG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