Skip to content

Implement the countbits HLSL Function #99094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
9 tasks
Tracked by #99235 ...
farzonl opened this issue Jul 16, 2024 · 3 comments · Fixed by #108121
Closed
9 tasks
Tracked by #99235 ...

Implement the countbits HLSL Function #99094

farzonl opened this issue Jul 16, 2024 · 3 comments · Fixed by #108121
Assignees
Labels
backend:DirectX backend:SPIR-V bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.

Comments

@farzonl
Copy link
Member

farzonl commented Jul 16, 2024

Note The SPIRV Work is completed https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L481C3-L482C63
We just need to add the test case.

  • Implement countbits clang builtin,
  • Link countbits clang builtin with hlsl_intrinsics.h
  • Add sema checks for countbits to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for countbits to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/countbits.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/countbits-errors.hlsl
  • Create the DXILOpMapping of int_ctpop to 31 in DXIL.td
  • Create the countbits.ll and countbits_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/countbits.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
31 Countbits 6.0 ()

SPIR-V

OpBitCount:

Description:

Count the number of set bits in an object.

Results are computed per component.

Result Type must be a scalar or vector of integer type.
The components must be wide enough to hold the unsigned Width of
Base as an unsigned value. That is, no sign bit is needed or counted
when checking for a wide enough result width.

Base must be a scalar or vector of integer type. It must
have the same number of components as Result Type.

The result is the unsigned value that is the number of bits in Base
that are 1.

Word Count Opcode Results Operands

4

205

<id>
Result Type

Result <id>

<id>
Base

Test Case(s)

Example 1

//dxc countbits_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(int4 p1) {
    return countbits(p1);
}

HLSL:

Counts the number of bits (per component) set in the input integer.

Syntax

uint countbits(
  in uint value
);

Parameters

value [in]

Type: uint

The input value.

Return value

Type: uint

The number of bits.

Remarks

The following overloaded versions are also available:

uint count_bits(uint value);
uint2 count_bits(uint2 value);
uint3 count_bits(uint3 value);
uint4 count_bits(uint4 value);

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 5 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

Intrinsic Functions

Shader Model 5

@farzonl farzonl added backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues. labels Jul 16, 2024
@spall
Copy link
Contributor

spall commented Sep 5, 2024

looking at this

@spall spall closed this as completed in 67518a4 Sep 18, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. labels Sep 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

@llvm/issue-subscribers-clang-frontend

Author: Farzon Lotfi (farzonl)

> Note The SPIRV Work is completed https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L481C3-L482C63 > We just need to add the test case.
  • Implement countbits clang builtin,
  • Link countbits clang builtin with hlsl_intrinsics.h
  • Add sema checks for countbits to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for countbits to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/countbits.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/countbits-errors.hlsl
  • Create the DXILOpMapping of int_ctpop to 31 in DXIL.td
  • Create the countbits.ll and countbits_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/countbits.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
31 Countbits 6.0 ()

SPIR-V

OpBitCount:

Description:

Count the number of set bits in an object.

Results are computed per component.

Result Type must be a scalar or vector of integer type.
The components must be wide enough to hold the unsigned Width of
Base as an unsigned value. That is, no sign bit is needed or counted
when checking for a wide enough result width.

Base must be a scalar or vector of integer type. It must
have the same number of components as Result Type.

The result is the unsigned value that is the number of bits in Base
that are 1.

<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>Word Count</th>
<th>Opcode</th>
<th>Results</th>
<th>Operands</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>4</p></td>
<td class="tableblock halign-left valign-top"><p>205</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><em>&lt;id&gt;</em><br />
<em>Base</em></p></td>
</tr>
</tbody>
</table>

Test Case(s)

Example 1

//dxc countbits_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(int4 p1) {
    return countbits(p1);
}

HLSL:

Counts the number of bits (per component) set in the input integer.

Syntax

uint countbits(
  in uint value
);

Parameters

<dl> <dt>

value [in]
</dt> <dd>

Type: uint

The input value.

</dd> </dl>

Return value

Type: uint

The number of bits.

Remarks

The following overloaded versions are also available:

uint count_bits(uint value);
uint2 count_bits(uint2 value);
uint3 count_bits(uint3 value);
uint4 count_bits(uint4 value);

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 5 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
</dt> <dt>

Shader Model 5
</dt> </dl>

Sorry, something went wrong.

@llvmbot
Copy link
Member

llvmbot commented Sep 18, 2024

@llvm/issue-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

> Note The SPIRV Work is completed https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L481C3-L482C63 > We just need to add the test case.
  • Implement countbits clang builtin,
  • Link countbits clang builtin with hlsl_intrinsics.h
  • Add sema checks for countbits to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for countbits to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/countbits.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/countbits-errors.hlsl
  • Create the DXILOpMapping of int_ctpop to 31 in DXIL.td
  • Create the countbits.ll and countbits_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/countbits.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
31 Countbits 6.0 ()

SPIR-V

OpBitCount:

Description:

Count the number of set bits in an object.

Results are computed per component.

Result Type must be a scalar or vector of integer type.
The components must be wide enough to hold the unsigned Width of
Base as an unsigned value. That is, no sign bit is needed or counted
when checking for a wide enough result width.

Base must be a scalar or vector of integer type. It must
have the same number of components as Result Type.

The result is the unsigned value that is the number of bits in Base
that are 1.

<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>Word Count</th>
<th>Opcode</th>
<th>Results</th>
<th>Operands</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>4</p></td>
<td class="tableblock halign-left valign-top"><p>205</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><em>&lt;id&gt;</em><br />
<em>Base</em></p></td>
</tr>
</tbody>
</table>

Test Case(s)

Example 1

//dxc countbits_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(int4 p1) {
    return countbits(p1);
}

HLSL:

Counts the number of bits (per component) set in the input integer.

Syntax

uint countbits(
  in uint value
);

Parameters

<dl> <dt>

value [in]
</dt> <dd>

Type: uint

The input value.

</dd> </dl>

Return value

Type: uint

The number of bits.

Remarks

The following overloaded versions are also available:

uint count_bits(uint value);
uint2 count_bits(uint2 value);
uint3 count_bits(uint3 value);
uint4 count_bits(uint4 value);

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 5 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
</dt> <dt>

Shader Model 5
</dt> </dl>

Sorry, something went wrong.

tmsri pushed a commit to tmsri/llvm-project that referenced this issue Sep 19, 2024

Verified

This commit was signed with the committer’s verified signature.
apoelstra Andrew Poelstra
Add new elementwise popcount builtin to support HLSL function
'countbits'.
elementwise popcount only accepts integer types.
Add hlsl intrinsic 'countbits'
Closes llvm#99094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:SPIR-V bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants