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

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

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 120 | WaveActiveBit | 6.0 | ('library', 'compute', 'amplification', 'mesh', 'pixel', 'vertex', 'hull', 'domain', 'geometry', 'raygeneration', 'intersection', 'anyhit', 'closesthit', 'miss', 'callable', 'node') |

## SPIR-V

# [OpGroupNonUniformBitwiseAnd](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpGroupNonUniformBitwiseAnd):

## Description:
  
A bitwise and [group operation](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Group_Operation) of all *Value*
operands contributed by active [invocations](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Invocation) in the
[group](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Group).  
  
*Result Type* must be a scalar or vector of [*integer
type*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Integer).  
  
*Execution* is a [*Scope*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Scope_-id-) that identifies the group of
invocations affected by this command. It must be **Subgroup**.  
  
The identity *I* for *Operation* is ~0. If *Operation* is
**ClusteredReduce**, *ClusterSize* must be present.  
  
The type of *Value* must be the same as *Result Type*.  
  
*ClusterSize* is the size of cluster to use. *ClusterSize* must be a
scalar of [*integer type*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Integer), whose *Signedness* operand is 0.
*ClusterSize* must come from a [*constant
instruction*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#ConstantInstruction). Behavior is undefined unless
*ClusterSize* is at least 1 and a power of 2. If *ClusterSize* is
greater than the size of the [group](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Group), executing this instruction
results in undefined behavior.

[Capability](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Capability):  
**GroupNonUniformArithmetic**, **GroupNonUniformClustered**,
**GroupNonUniformPartitionedNV**  
  
[Missing before](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Unified) **version 1.3**.

<table>
<colgroup>
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
<col style="width: 12%" />
</colgroup>
<thead>
<tr>
<th>Word Count</th>
<th>Opcode</th>
<th>Results</th>
<th>Operands</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>6 + variable</p></td>
<td class="tableblock halign-left valign-top"><p>359</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Result Type</em></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#ResultId"><em>Result &lt;id&gt;</em></a></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#Scope_-id-"><em>Scope &lt;id&gt;</em></a><br />
<em>Execution</em></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#Group_Operation"><em>Group Operation</em></a><br />
<em>Operation</em></p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Value</em></p></td>
<td class="tableblock halign-left valign-top"><p>Optional<br />
<em>&lt;id&gt;</em><br />
<em>ClusterSize</em></p></td>
</tr>
</tbody>
</table>



## Test Case(s)

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

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

Returns the bitwise AND of all the values of the expression across all active lanes in the current wave and replicates it back to all active lanes.

## Syntax


``` syntax
<int_type> WaveActiveBitAnd(
   <int_type> expr
);
```



## Parameters

<dl> <dt>

*expr* 
</dt> <dd>

The expression to evaluate.

</dd> </dl>

## Return value

The bitwise AND value.

## Remarks

This function is supported from shader model 6.0 in all shader stages. 



 

## See also

<dl> <dt>

[Overview of Shader Model 6](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/hlsl-shader-model-6-0-features-for-direct3d-12.md)
</dt> <dt>

[Shader Model 6](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/shader-model-6-0.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWV9v4zYS_zTMC2FBpmIneciD7MQ9A7ubYrPd9i2gxJHFC0UKJOXEfbjPfhhStmXHSdreZXsPVwRdc2Y4f34zJIcUd06uNMA1mczI5OaMd7429rri9nej1VlhxOZ6RMlkRsnkhi6bVkED2lMyTX_la8hLL9cwkz7XgkxTWiquV7TopPJSEzYnab6f_Unqxz80kT5JX6NkrZx6kNpbqZ0sXVKTaXqgMheCOmg4LWsoHx2tjH3NgjfImaPcPz7df5pFU4tOl14aPedKoZTUKHUPDQ-SUq-Ssm1PmS2NgBXo903eNtIPLN4-t3Zvaf5TT37PjAfnXa8xgEXYAmmELeZGwE-g0QZhix5DR9ji2KcE8XwVwjcsIB69-uDu8qT6EVhrrDtpZW6Be6C-BlQvtX8Qzw-nMNtluwdoucv-jbRQ-t8SL95WfvPb8tNd-5m3rdQrVGmqd2x6Q1FizNJ9YlDL26ZOZT2JVcS1OMV96AGKQhHvaE2pdfMioYQt-pgJW7wPqGvXfxLR-5-XX7-fCHKpaWAttfO2C-vjHhSU3lgsU1oe2D5lU5knsFKvAhINbymVvi-tt10Nq-8V2yTLSZa7MNoFQaZpcgoYVDL6TgtePoIWAWxacgfvAB5sE7bAGh7td55Ti0n1RY5_LCMso9tsRdrFnGIR0bsW1zAdjL_wJo7vay7A0s9GgBoS7j1fgUNKr2i0_4--M35JGA0UjVka-AfRBMo0iRzCLgm7ULKw3G4IuyAMaReladrOw57Am1bJSpYcU7QnN-Dq_aiVz6D2wzVYD8_7cd2pAVeYhsuBqhWYBvzQCcs3K9Bgj2xK7cE6KA-pXG9q6QcRKOPA-QNaI50bSHCleKEGQWoj4uhqi-Eu2bHAdiSsv7v2J2u69ovRv2hZGdvMpH-SDrC6JzeEXdbetw7LmC0IW1hYSeftJnmsrdHGJcau-hIcYQ26FkLldVpWEsR4W55J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