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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 34 | FirstbitSHi | 6.0 | () |

## SPIR-V

# FindSMsb:

## Description:
**FindSMsb**  
  
Signed-integer most-significant bit, with *Value* interpreted as a
signed integer.  
  
For positive numbers, the result will be the bit number of the most
significant 1-bit. For negative numbers, the result will be the bit
number of the most significant 0-bit. For a *Value* of 0 or -1, the
result is -1.  
  
*Result Type* and the type of *Value* must both be integer scalar or
integer vector types. *Result Type* and operand types must have the same
number of components with the same component width. Results are computed
per component.  
  
This instruction is currently limited to 32-bit width components.

<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr>
<th>Number</th>
<th>Operand 1</th>
<th>Operand 2</th>
<th>Operand 3</th>
<th>Operand 4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>74</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Value</em></p></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>



## Test Case(s)

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

export uint4 fn(int4 p1) {
    return firstbithigh(p1);
}
```
## HLSL:

Gets the location of the first set bit starting from the highest order bit and working downward, per component.

## Syntax

``` syntax
int firstbithigh(
  in int value
);
```

## Parameters

<dl> <dt>

*value* \[in\]
</dt> <dd>

Type: **[**int**](/windows/desktop/WinProg/windows-data-types)**

The input value.

</dd> </dl>

## Return value

Type: **[**int**](/windows/desktop/WinProg/windows-data-types)**

The location of the first set bit.

## Remarks

For a signed integer, the first significant bit is zero for a negative number.

The following overloaded versions are also available:

``` syntax
int2 firstbithigh(int2 value);
int3 firstbithigh(int3 value);
int4 firstbithigh(int4 value);
uint firstbithigh(uint value);
uint2 firstbithigh(uint2 value);
uint3 firstbithigh(uint3 value);
uint4 firstbithigh(uint4 value);
```

### Minimum Shader Model

This function is supported in the following shader models.



| Shader Model                                                                | Supported |
|-----------------------------------------------------------------------------|-----------|
| [Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md) and higher shader models | yes       |



 

This function is supported in the following types of shaders:



| Vertex | Hull | Domain | Geometry | Pixel | Compute |
|--------|------|--------|----------|-------|---------|
| x      | x    | x      | x        | x     | x       |



 

## See also

<dl> <dt>

[Intrinsic Functions](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
</dt> <dt>

[Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUGF2PGjny13herEbdbmCGBx4YGJKRkk2UiXL7NnK3De0bt92y3cDsrz-V3Z9AyO3taXWHENhV5fq2q2xqrdgrzpdo9ohmmztau0Kb5Y6aP7SSd5lm78sIo9kjRrMNfi4ryUuuHEbzeCeMdZlwhdgXaB7jXFK1x1ktpBMKkTWKV_3KT0K9_XIRPgpXAFUhrXwVyhmhrMjtBEhH7FaMYctLivOC528W77S5xt1pgK6B5uOnl0-PQcy2VrkTWq2plEAlFFC98JJ6SqH2k7yqronMNeN7rm6LeyqFG0h7OlWml7L-0IB_JcJx62zD0TsJkS3AENmuNeMfuAIZiGwb31lEtkN9JuDDn7rtBnfwQ8Paq_p8wTrixmhjr0pYG04dx67gwFoo98pOr-d-6iLbOOW5i_RGGJ673yeO3Wa8-f3505fqM60qofbAUu9uyHMaAzad9nEABrelnAd4EpKFKnaOeW38EQiCa4MUKQ_lRdwQ2TZmIrL9tf9sdfgTDnz5-vztxxXDnhX2qGdlnal9-r9wyXOnDWQizkdyz-VJfeRGqL23vqQVxsI12fNzFf2m-olMlK5QurJ-1imP5vHkmjOASfQDZzR_44p5B-OcWv4LJ3vZiGwhTaP-MDnfJ7LJYfiSFJEUt9EJsPs1hmTBXyrYmngw_42WYf5SUMYN_qwZl0PAi6N7bgHSMIr6D_7F_BIQDRilU4_eNoa8fBR-Pp_E_h-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