Skip to content

[Clang][RISCV] Add missing support for __builtin_riscv_cpop_32/64 #76256

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/BuiltinsRISCV.def
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ TARGET_BUILTIN(__builtin_riscv_clz_32, "UiUi", "nc", "zbb|xtheadbb")
TARGET_BUILTIN(__builtin_riscv_clz_64, "UiUWi", "nc", "zbb|xtheadbb,64bit")
TARGET_BUILTIN(__builtin_riscv_ctz_32, "UiUi", "nc", "zbb")
TARGET_BUILTIN(__builtin_riscv_ctz_64, "UiUWi", "nc", "zbb,64bit")
TARGET_BUILTIN(__builtin_riscv_cpop_32, "UiUi", "nc", "zbb")
TARGET_BUILTIN(__builtin_riscv_cpop_64, "UiUWi", "nc", "zbb,64bit")

// Zbc or Zbkc extension
TARGET_BUILTIN(__builtin_riscv_clmul_32, "UiUiUi", "nc", "zbc|zbkc")
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20696,6 +20696,8 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
case RISCV::BI__builtin_riscv_clz_64:
case RISCV::BI__builtin_riscv_ctz_32:
case RISCV::BI__builtin_riscv_ctz_64:
case RISCV::BI__builtin_riscv_cpop_32:
case RISCV::BI__builtin_riscv_cpop_64:
case RISCV::BI__builtin_riscv_clmul_32:
case RISCV::BI__builtin_riscv_clmul_64:
case RISCV::BI__builtin_riscv_clmulh_32:
Expand Down Expand Up @@ -20735,6 +20737,14 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
"cast");
return Result;
}
case RISCV::BI__builtin_riscv_cpop_32:
case RISCV::BI__builtin_riscv_cpop_64: {
Value *Result = Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, Ops[0]);
if (Result->getType() != ResultType)
Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/ true,
"cast");
return Result;
}

// Zbc
case RISCV::BI__builtin_riscv_clmul_32:
Expand Down
26 changes: 26 additions & 0 deletions clang/test/CodeGen/RISCV/rvb-intrinsics/zbb.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,29 @@ unsigned int ctz_64(unsigned long a) {
return __builtin_riscv_ctz_64(a);
}
#endif

// RV32ZBB-LABEL: @cpop_32(
// RV32ZBB-NEXT: entry:
// RV32ZBB-NEXT: [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[A:%.*]])
// RV32ZBB-NEXT: ret i32 [[TMP0]]
//
// RV64ZBB-LABEL: @cpop_32(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[A:%.*]])
// RV64ZBB-NEXT: ret i32 [[TMP0]]
//
unsigned int cpop_32(unsigned int a) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use uint32_t and uint64_t. I already updated some of the tests to use those types but I somehow missed this one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be using the intrinsic from riscv_bitmanip.h

return __builtin_riscv_cpop_32(a);
}

#if __riscv_xlen == 64
// RV64ZBB-LABEL: @cpop_64(
// RV64ZBB-NEXT: entry:
// RV64ZBB-NEXT: [[TMP0:%.*]] = call i64 @llvm.ctpop.i64(i64 [[A:%.*]])
// RV64ZBB-NEXT: [[CAST:%.*]] = trunc i64 [[TMP0]] to i32
// RV64ZBB-NEXT: ret i32 [[CAST]]
//
unsigned int cpop_64(unsigned long a) {
return __builtin_riscv_cpop_64(a);
}
#endif