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

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

## DirectX

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

## SPIR-V

<div id="header">

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

## Short Description

<div class="sectionbody">

HitKindKHR - Kind of hit that triggered an any-hit or closest hit 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">

` HitKindKHR `  
A variable decorated with the ` HitKindKHR ` decoration will describe
the intersection that triggered the execution of the current shader. The
values are determined by the intersection shader. For user-defined
intersection shaders this is the value that was passed to the “Hit Kind”
operand of ` OpReportIntersectionKHR ` . For triangle intersection
candidates, this will be one of ` HitKindFrontFacingTriangleKHR ` or
` HitKindBackFacingTriangleKHR ` .

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

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

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

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

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

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

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

 ### Example 2
```hlsl
//dxc HitKind_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 = HitKind();
}
```
## HLSL:

Returns the value passed as the **HitKind** parameter to [**ReportHit**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/reporthit-function.md).

## Syntax

```
uint HitKind();

```



## Remarks

If the intersection was reported by fixed-function triangle intersection, **HitKind** will be one of **HIT\_KIND\_TRIANGLE\_FRONT\_FACE** (254) or **HIT\_KIND\_TRIANGLE\_BACK\_FACE** (255).

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/eJzEWVtz4rgS_jXKi8qUkYGEBx4cCBNqbqeS7NS-pYTd2DoRkkuSmXB-_amWZWMIuczZObtbbMVutfvydUv6pOHWikIBzMj4mowXF7x2pTazDTf_0UperHW-n0WUjK8pGS_oaltJ2IJylEziW-E-C5WTSUwzyVVB17WQTijC5iRODx99EerpLX36U7gSFUpp5aNQzghlRWYHJZnER5bSPKcWtpxmJWRPlm60OTHsNArmOHz75f7LdeNhWavMCa3mXErUEgq17mHLvaZQxSCrqnPeMp1DAepVTzdb4XqObp4rc3Aw_xTE71l3YJ0NFj00hC1RRthyrnP4BAp9ELYMiFnCliGUAYL2Kk5vGMbsg1Uf5apvNQJjtLFnjc8NcAfUlYBWhXKP-fNjD5iugAGFVVfQhTCQuT8HLn_b5uLP1Zfv1VdeVUIVaFJvzrtymuLAcJQcQMeP3_bQK-Sg6Qeu8p7wMST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