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

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

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

## Description:
  
A bitwise xor [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>361</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 WaveActiveBitXor_test.hlsl -T lib_6_8 -enable-16bit-types -O0

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

Returns the bitwise XOR 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> WaveActiveBitXor(
   <int_type> expr
);
```

## Parameters

<dl> <dt>

*expr* 
</dt> <dd>

The expression to evaluate.

</dd> </dl>

## Return value

The bitwise XOR 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/eJzsWd9u47bSfxrmhrAgU2s5vsiF7MT9DOw2xWa73buAEscWv1CkQFJO0qc_GFK2ZcdJ2p6T9lycIuiaM8P585shOaS4c3KjAa7IZE4m1xe887WxV2tufzdaXZRGPF-NKJnMKZlc01XTKmhAe0ry9De-haLycgtz6X8YS_KUVorrDS07qbzUhC1IWhxmf5b64Q9NpI_S1yhZK6fupfZWaicrl9QkT49UFkJQBw2nVQ3Vg6NrY1-z4A1yFij3f5_vPs-jqWWnKy-NXnClUEpqlLqDhgdJqTdJ1bbnzFZGwAb0-yZvGukHFm-eWnuwtPipJ79nxoPzrtcYwCJsiTTClgsj4CfQaIOwZY-hI2x56lOCeL4K4RsWEI9efXB3dVb9CKw11p21srDAPVBfA6qX2t-Lp_tzmO2z3QO02mf_Wlqo_I_Ei7eVX_9Yfb5tv_C2lXqDKs36HZveUJQYs_SQGNTytqlzWU9iFXEtznHve4CiUMQ7WlNq27xIKGHLPmbClu8D6trtn0T07pfV1-9nglxpGlgr7bztwvq4AwWVNxbLlFZHts_ZVOYRrNSbgETDW0ql70vrbVfD6nvFNskKkhUujPZBkDxNzgGDSkbfacmrB9AigE0r7uAdwINtwpZYw6PDznNuMam-yPGPZYRldJetSJsuKBYRvW1xDdPB-GfexPFdzQVY-sUIUEPCnecbcEjpFY0O_9F3xi8Jo4GiMUsD_yiaQMmTyCHskrCpkqXl9pmwKWFIm1amaTsPBwJvWiXXsuKYogO5AVcfRq18AnUYbsF6eDqM604NuMI0XA5UbcA04IdOWP68AQ32xKbUHqyD6pjK9XMt_SACZRw4f0RrpHMDCa4UL9UgSG1EHM12GO6THQtsT8L6u21_sqZrfzb6Vy3XxjZz6R-lA6zuyTVhl7X3rcMyZkvClhY20nn7nDzU1mjjEmM3fQmOsAZdC6HyOi3XEsR4V55J7RtFWPaWOTZDM0fFCa6ysg0YBRalJC0KW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