<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/97624>97624</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Implement the `InstanceID` HLSL Function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:DirectX,
HLSL,
backend:SPIR-V,
HLSL_Bot
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
farzonl
</td>
</tr>
</table>
<pre>
- [ ] Implement `InstanceID` clang builtin,
- [ ] Link `InstanceID` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `InstanceID` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `InstanceID` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/InstanceID.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/InstanceID-errors.hlsl`
- [ ] Create the `int_dx_InstanceID` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_InstanceID` to `141` in `DXIL.td`
- [ ] Create the `InstanceID.ll` and `InstanceID_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_InstanceID` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `InstanceID` lowering and map it to `int_spv_InstanceID` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/InstanceID.ll`
## DirectX
| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 141 | InstanceID | 6.3 | ('library', 'intersection', 'anyhit', 'closesthit') |
## SPIR-V
See https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/InstanceCustomIndexKHR.html
## Test Case(s)
### Example 1
```hlsl
//dxc InstanceID_test.hlsl -T lib_6_8 -enable-16bit-types -O0
export uint fn() {
return InstanceID();
}
```
### SPIRV Example(s):
### Example 2
```hlsl
//dxc InstanceID_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 = InstanceID();
}
```
## HLSL:
The user-provided identifier for the instance on the bottom-level acceleration structure instance within the top-level structure.
## Syntax
```
uint InstanceID();
```
## Remarks
This function can be called from the following raytracing shader types:
* [**Any Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3d12/any-hit-shader.md)
* [**Closest Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3d12/closest-hit-shader.md)
* [**Intersection Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3d12/intersection-shader.md)
## See also
<dl> <dt>
[Direct3D 12 Raytracing HLSL Reference](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3d12/direct3d-12-raytracing-hlsl-reference.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV19v6joS_zTmxUoUbAjlgQcK5R60PXtW7dHVfUNOMhBvHTuyHQr76VfjBBJO_52ne6tUxOPxzPx-Mx47wjl50AALMr0n0_VINL40drEX9n9Gq1FmivMiomR6T8l0TbdVraAC7SlJk612XugctmuSJjRXQh9o1kjlpSZsRZJlv-5R6pcvltBX6UvUKZVTO6m9ldrJ3MUlSZMbY8uioA4qQfMS8hdH98a-te0Nylao8e3x-fG-dbJpdO6l0SuhFGpJjVrPUImgKfUhzuv6PYe5KeAA-jNnD5X0A18Pp9r2PlZ_dOKvHHhw3nUWA0GEbVBG2GZlCvgDNPogbNPx5gjb9NHEyN6HhH1iGznoDIdAt78YjsBaY9279lcWhAfqS0DDUvtdcdrdMnTNZ0fH9prftbSQ-79iX3xudv3X9vFH_V3UtdQHNGn2H3rzhuLceDLuE4DrP3dym9e4rR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