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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 213 | GeometryIndex | 6.5 | ('library', 'intersection', 'anyhit', 'closesthit') |

## SPIR-V

<div id="header">

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

## Short Description

<div class="sectionbody">

RayGeometryIndexKHR - Geometry index in a ray shader

</div>

</div>

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

<div class="sect1">

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

<div class="sectionbody">

<div class="dlist">

` RayGeometryIndexKHR `  
A variable decorated with the ` RayGeometryIndexKHR ` decoration will
contain the <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#acceleration-structure-geometry-index"
rel="noopener" target="_blank">geometry index</a> for the acceleration
structure geometry currently being shaded.

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

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

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

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

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

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

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

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

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

The autogenerated index of the current geometry in the bottom-level acceleration structure.

## Syntax


```C++
uint GeometryIndex();
```

## Remarks

This method is only available on [Tier 1.1](https://devblogs.microsoft.com/directx/dxr-1-1/) implementations.

The MultiplierForGeometryContributionToHitGroupIndex parameter to [TraceRay()](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/traceray-function.md) can be set to 0 to stop the geometry index from contributing to shader table indexing if the shader just wants to rely on GeometryIndex() to distinguish geometries.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8WN2S2joSfhpxo7LLyGMzXHDBwJBQm2y2Zuakzt2UbDVGB1lySTKBffotyT-YwDAnJ9mkqMq41eqf72tJLVFjeCEBZih5QMlyRGu7VXq2ofq_SopRpthxFmCUPGCULPG6rASUIC1GafQBVAlWH9eSwQGlEc4FlQXOai4sl4gsUDQ_Tf3E5e79Wfgbt1unthVGvHJpNZeG5ybcojQ6szdnDBsoKc63kO8M3ih91bxVTrxwSh8_PX96aPysaplbruSCCuG0uHRaz1BSr8llEeZVdc1nrhgUIN_x91hyO3D3eKj0yc3iQyt-z4cFY01r0cOEyMrJEFktFIMPIJ0PRFYtegaR1VlAoYPxTeRumHdItLZ9rOtL2wForbS56mKhgVrAdgvONpf2lR1eL6Dq6W1xWfd0L7mG3P4ZWnbb8vLP9acv1WdaVVwWzqTa3HJoFXbDZByfyHAmbvu5oDlsaoZKdjH02oLSaDQAN36E2JcX9CGyalNFZPU-iKba_wiKz_9ZP329kttaYj-0lsbq2q-DZxCQW6VdReL8zPGFQ6G-geay8ACUtMKY27aKbgTp19cbXlE8R_Hc-K8-fJRG4TU8nJHgK85ov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