Skip to content

Implement the countbits HLSL Function #99094

Closed
Listed in 2 issues
Closed
@farzonl

Description

@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.

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

Activity

spall

spall commented on Sep 5, 2024

@spall
Contributor

looking at this

added a commit that references this issue on Sep 18, 2024
67518a4

6 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportbackend:DirectXbackend:SPIR-Vbot:HLSLclang:codegenIR generation bugs: mangling, exceptions, etc.clang:frontendLanguage frontend issues, e.g. anything involving "Sema"metabugIssue to collect references to a group of similar or related issues.

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @spall@farzonl@EugeneZelenko@llvmbot

    Issue actions

      Implement the `countbits` HLSL Function · Issue #99094 · llvm/llvm-project