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

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

## DirectX

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

## SPIR-V

<div id="header">

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

## Short Description

<div class="sectionbody">

SubgroupSize - Size of a subgroup

</div>

</div>

<div id="content" class="loadable">

<div class="sect1">

## <a href="#_description" class="anchor"></a> Description

<div class="sectionbody">

<div class="dlist">

` SubgroupSize `  
<div class="paragraph">

Decorating a variable with the ` SubgroupSize ` builtin decoration will
make that variable contain the implementation-dependent <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#limits-subgroupSize"
rel="noopener" target="_blank">number of invocations in a subgroup</a> .
This value **must** be a power-of-two integer.

</div>

<div class="paragraph">

If the pipeline was created with the
` VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT ` flag
set, or the shader object was created with the
` VK_SHADER_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT ` flag set, or
the SPIR-V ` module ` is at least version 1.6, the ` SubgroupSize `
decorated variable will contain the subgroup size for each subgroup that
gets dispatched. This value **must** be between <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#limits-minSubgroupSize"
rel="noopener" target="_blank"><code> minSubgroupSize </code></a> and <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#limits-maxSubgroupSize"
rel="noopener" target="_blank"><code> maxSubgroupSize </code></a> and
**must** be uniform with <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#shaders-scope-subgroup"
rel="noopener" target="_blank">subgroup scope</a> . The value **may**
vary across a single draw call, and for fragment shaders **may** vary
across a single primitive. In compute dispatches, ` SubgroupSize `
**must** be uniform with <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#shaders-scope-command"
rel="noopener" target="_blank">command scope</a> .

</div>

<div class="paragraph">

If the pipeline was created with a chained
[VkPipelineShaderStageRequiredSubgroupSizeCreateInfo](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/None)
structure, or the shader object was created with a chained
[VkShaderRequiredSubgroupSizeCreateInfoEXT](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/None)
structure, the ` SubgroupSize ` decorated variable will match <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#pipelines-required-subgroup-size"
rel="noopener" target="_blank"><code> requiredSubgroupSize </code></a> .

</div>

<div class="paragraph">

If SPIR-V ` module ` is less than version 1.6 and the pipeline was not
created with the
` VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT ` flag
set and no
[VkPipelineShaderStageRequiredSubgroupSizeCreateInfo](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/None)
structure was chained, and the shader was not created with the
` VK_SHADER_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT ` flag set and no
[VkShaderRequiredSubgroupSizeCreateInfoEXT](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/None)
structure was chained, the variable decorated with ` SubgroupSize ` will
match <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#limits-subgroupSize"
rel="noopener" target="_blank"><code> subgroupSize </code></a> .

</div>

<div class="paragraph">

The maximum number of invocations that an implementation can support per
subgroup is 128.

</div>

</div>

<div class="admonitionblock note">

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<tbody>
<tr>
<td class="icon"><em></em></td>
<td class="content"><div class="title">
Note
</div>
<div class="paragraph">
<p>The old behavior for <code> SubgroupSize </code> is considered
deprecated as certain compute algorithms cannot be easily implemented
without the guarantees of
<code> VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT </code>
and
<code> VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT </code>
.</p>
</div></td>
</tr>
</tbody>
</table>

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

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

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

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

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

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

 ### Example 2
```hlsl
//dxc WaveGetLaneCount_spirv_test.hlsl -T ps_6_8 -E fn -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0


uint fn( ) : SV_Target {

        return WaveGetLaneCount();
}
```
## HLSL:

Returns the number of lanes in a wave on this architecture.

## Syntax

``` syntax
uint WaveGetLaneCount(void);
```

## Parameters

This function has no parameters.

## Return value

The result will be between 4 and 128, and includes all waves: active, inactive, and/or helper lanes. The result returned from this function may vary significantly depending on the driver implementation.

## Remarks

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



 

## Examples

``` syntax
 uint laneCount = WaveGetLaneCount();    // number of lanes in wave
```

## 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/eJzUWttz4yqT_2vICyWXLMVO8pAHxZf5XJszMxtn8s3uiwtLbYsTBFpATnL--q0GWZJvSeZ6zjeVqjEIuulfX-gGmDF8LQGuyeCGDMZnrLK50tcrpv9SUpwtVfZyHVAyuKFkMKazohRQgLSUDMN_sw18AHvLJIxUJS0ZhjQVTK7psuLCckmiEQmTdvYtl4_vmkifuM1xZC6MWHBpNZeGp6aXk2G4QzLJMmqgYDTNIX00dKX0KQ5W4ZcRjvvX7fz2xrOaVjK1XMkREwJHcYmj5lAwN5LLdS8ty2NsU5XBGuTbLCcFtx2Ok-dSt5xGH-rut9hYMNbUFB1YJJpiH4mmI5XBB5DIg0TTGkNDoun-mnqI50kIX-GAeNTk3XJnR8kHoLXS5iiXkQZmgdockDyXdpE9L45h1mi7BmjWaH_MNaT2a89mrxMff53dfir_YGXJ5RpJqtUbPK2iOKLfj1rFIJXXWR3Tes9bEZPZsa-LGiA_yOPtuQmxKQ4USqJpLTOJpm8DasrNNyI6_zy7ezgi5ExS92kmjdWV8485CEit0mimNN3hfYynUE-guVw7JApWUsptbVqvL9V53wneJE5InBjXaoQgw7B3DBgkEjzQJUsfQWYObJoyA28A7niTaIo2HLSR55gzidrI8S-KSRTTrbZ838WIohHRTyX6MO20P7LCt-c5y0DTP1QGotsxt2wNBntqQkH7j77RPuwIOoT6_ch935fGdQ57ofufRJckuhB8qZl-IdEFibDvIlVFWVloO1hRCr7iKUMttd0FmLxtlfwZRNvcgLbw3LbzSnS-ZqpgvENqDaoA212EZi9rkKD3eHJpQRtId3uZfMm57UgglAFjd_oKbkxnBBOCLUVHSKky37rawtjo29tY3RWPMr6hPCPxmERRDqhIEkUknjRz0Ebn1XKtVVXO-V9ABmMSXebWlgZtO5qSaKphzY3VL73HXCupTE9pDMSbSjwyNFBTgjPHfi8O4NmCNFy5aFy4z7ktBJpxh0vP913tLj1X2tIxmFTz0oHWlSIVzBgvSI0pJgJdaboMaEDdf2p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