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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 32 | FirstbitLo | 6.0 | () |

## SPIR-V

# FindILsb:

## Description:
**FindILsb**  
  
Integer least-significant bit.  
  
Results in the bit number of the least-significant 1-bit in the binary
representation of *Value*. If *Value* is 0, 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.

<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>73</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 firstbitlow_test.hlsl -T lib_6_8 -enable-16bit-types -O0

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

Returns the location of the first set bit starting from the lowest order bit and working upward, per component.

## Syntax

``` syntax
int firstbitlow(
  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

The following overloaded versions are also available:

``` syntax
uint2 firstbitlow(uint2 value);
uint3 firstbitlow(uint3 value);
uint4 firstbitlow(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/eJzUGF1v6jry15gXKyhxGgoPPFAo5yL13HN0enT2vlVObMBbx45sB-j--tXYIR-U0t29q6tdhCCeGc-3Z8ah1oqd4nyOsgeUrUa0dntt5ltq_qGVHOWavc0jjLIHjLIV3pSV5CVXDqNJvBXGulw4qY9oEuNCUrXDeS2kEwqRJYoX3cYnoV4_24OPwu2BaC-tfBHKGaGsKOx4jybxgNuCMWx5SXGx58WrxVttrjB3GoBLIPnt6fnpIUhZ16pwQqsllRKohAKqZ15STynUblxU1TWJhWZ8x9VNaY-lcD1hj6fKdEKWXxrwZxIct842HL2LEFkDDJH1UjP-hSuQgci68ZxFZN1TZwwO_NBnN5iDFxrOXtPNJeeIG6ONvSpgaTh1HLs9B85CuRd2erlwUhvUxiObNsgrYXjh_hg7dpvv6o_N07fqK60qoXbAUm8_Fuc0BmRKuhjA_ttCLmI7DmlCFbtAvDS-CPjg1SBDykP5LmKIrBsbEVl_7jtbHf515z1_3_z4dcWqjcIetVHWmdqn_TOXvHDaQAriYiD2QpzUR26E2nnTS1phLFyTNh8q6I_SBxJRukDpwvpVqzqaxONrngAm0S-c0-KVK-a9iwtq-Sce9rIRWUN-Rl0FuTgessld-JIUkRSfIxNg90sMaYK_VXAgcW_9Oy3D-nlPGTf4q2Zc9gHPju64BUjDKOo--JP1e0DUY5QSj143djxpv5yMY_-PyBSR2Zm8tSu4sQXhtVBs82RziMXAfm4LIyoIV4Mi8G3J_QpjFIefjXJ8xw2WnFoXQQMRW1FQ5XAu3Lij-8FtLcOxgBzLhcOqLnNu4NQC5D2DJAKqdoOi5g3FC8Mrwy1XjoKK_syTxS8qa47IYow3gzUWFseILD0L41UAUJT0NE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