[clang] 3d9e24b - [CIR] Support __builtin_elementwise_ctzg and __builtin_elementwise_clzg (#246181)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 08:56:12 PST 2026
Author: Aman Maurya
Date: 2026-12-04T22:26:06+05:30
New Revision: 3d9e24b3b3a709a0a3ab3715d022ab58f28dba5f
URL: https://github.com/llvm/llvm-project/commit/3d9e24b3b3a709a0a3ab3715d022ab58f28dba5f
DIFF: https://github.com/llvm/llvm-project/commit/3d9e24b3b3a709a0a3ab3715d022ab58f28dba5f.diff
LOG: [CIR] Support __builtin_elementwise_ctzg and __builtin_elementwise_clzg (#246181)
### Description
This patch adds support for `__builtin_elementwise_ctzg` and
`__builtin_elementwise_clzg` in `CIRGenFunction::emitBuiltinExpr`,
establishing parity with classic Clang CodeGen
(`clang/lib/CodeGen/CGBuiltin.cpp:3438-3505`).
This change:
1. Extends `CIR_BitClzOp` and `CIR_BitCtzOp` in `CIROps.td` to accept
`CIR_ScalarOrVectorOf<CIR_UIntOfWidths<[8, 16, 32, 64, 128]>>`.
2. Casts signed scalar/vector operands to unsigned in
`createBuiltinBitOp` for `cir::BitCtzOp` / `cir::BitClzOp` and casts the
result back to the expression type.
3. Adds `isElementwise` (forcing `poisonZero = true`) and vector
zero-fallback selection (`cir::VecCmpOp` + `cir::VecTernaryOp`) to
`emitBuiltinBitOpWithFallback`.
4. Adds CIR, LLVM IR, and classic CodeGen (`OGCG`) parity tests in
`clang/test/CIR/CodeGenBuiltins/builtin-bit.cpp`.
Fixes #246134.
Assisted-by: Antigravity (Gemini 4 Hydrogen)
Added:
Modified:
clang/include/clang/CIR/Dialect/IR/CIROps.td
clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
Removed:
################################################################################
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index ac320dfc9c3b3d..25903bf075b557 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -8158,15 +8158,15 @@ def CIR_BitClrsbOp : CIR_BitOpBase<"clrsb", CIR_SIntOfWidths<[32, 64]>> {
}
def CIR_BitClzOp : CIR_BitZeroCountOpBase<"clz",
- CIR_UIntOfWidths<[8, 16, 32, 64, 128]>
+ CIR_ScalarOrVectorOf<CIR_AnyUIntType>
> {
let summary = "Get the number of leading 0-bits in the input";
let description = [{
Compute the number of leading 0-bits in the input.
- The input integer must be an unsigned integer. The `cir.clz` operation
- returns the number of consecutive 0-bits at the most significant bit
- position in the input.
+ The input must be an unsigned integer or a vector of unsigned integers. The
+ `cir.clz` operation returns the number of consecutive 0-bits at the most
+ significant bit position in the input.
If the `poison_zero` attribute is present, this operation will have
undefined behavior if the input value is 0.
@@ -8185,15 +8185,15 @@ def CIR_BitClzOp : CIR_BitZeroCountOpBase<"clz",
}
def CIR_BitCtzOp : CIR_BitZeroCountOpBase<"ctz",
- CIR_UIntOfWidths<[8, 16, 32, 64, 128]>
+ CIR_ScalarOrVectorOf<CIR_AnyUIntType>
> {
let summary = "Get the number of trailing 0-bits in the input";
let description = [{
Compute the number of trailing 0-bits in the input.
- The input integer must be an unsigned integer. The `cir.ctz` operation
- counts the number of consecutive 0-bits starting from the least significant
- bit.
+ The input must be an unsigned integer or a vector of unsigned integers. The
+ `cir.ctz` operation counts the number of consecutive 0-bits starting from
+ the least significant bit.
If the `poison_zero` attribute is present, this operation will have
undefined behavior if the input value is 0.
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 8ca0343966535c..1a3eb85f8b8a26 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -95,30 +95,44 @@ static RValue emitBuiltinVectorReduction(CIRGenFunction &cgf,
.getResult());
}
-/// Emit a clz/ctz bit op with optional fallback for __builtin_c[lt]zg.
+/// Emit a clz/ctz bit op with optional fallback for __builtin_c[lt]zg and
+/// __builtin_elementwise_c[lt]zg.
/// When a fallback is present, the result is the fallback value if the input is
/// zero, otherwise the bit count.
template <typename Op>
static RValue emitBuiltinBitOpWithFallback(CIRGenFunction &cgf,
- const CallExpr *e) {
+ const CallExpr *e,
+ bool isElementwise = false) {
bool hasFallback = e->getNumArgs() > 1;
- bool poisonZero = hasFallback || cgf.getTarget().isCLZForZeroUndef();
+ bool poisonZero =
+ hasFallback || isElementwise || cgf.getTarget().isCLZForZeroUndef();
- if (!hasFallback) {
+ if (!hasFallback)
assert(!cir::MissingFeatures::builtinCheckKind());
- return emitBuiltinBitOp<Op>(cgf, e, poisonZero);
- }
+ else
+ assert(!cir::MissingFeatures::builtinBitCountExpr());
- assert(!cir::MissingFeatures::builtinBitCountExpr());
- mlir::Value arg = cgf.emitScalarExpr(e->getArg(0));
+ CIRGenBuilderTy &builder = cgf.getBuilder();
+ QualType argTy = e->getArg(0)->getType();
+ mlir::Value arg = builder.createIntCast(
+ cgf.emitScalarExpr(e->getArg(0)),
+ cgf.convertType(cgf.getContext().getCorrespondingUnsignedType(argTy)));
mlir::Value result = createBuiltinBitOp<Op>(cgf, e, arg, poisonZero);
+ if (!hasFallback)
+ return RValue::get(result);
- CIRGenBuilderTy &builder = cgf.getBuilder();
mlir::Location loc = cgf.getLoc(e->getSourceRange());
mlir::Value zero = builder.getNullValue(arg.getType(), loc);
- mlir::Value isZero =
- builder.createCompare(loc, cir::CmpOpKind::eq, arg, zero);
mlir::Value fallbackValue = cgf.emitScalarExpr(e->getArg(1));
+ mlir::Value isZero;
+ if (auto vecTy = mlir::dyn_cast<cir::VectorType>(arg.getType())) {
+ auto boolVecTy = cir::VectorType::get(builder.getBoolTy(), vecTy.getSize(),
+ vecTy.getKind());
+ isZero =
+ builder.createVecCompare(loc, boolVecTy, cir::CmpOpKind::eq, arg, zero);
+ } else {
+ isZero = builder.createCompare(loc, cir::CmpOpKind::eq, arg, zero);
+ }
return RValue::get(builder.createSelect(loc, isZero, fallbackValue, result));
}
@@ -1462,8 +1476,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__builtin_ctzg:
return emitBuiltinBitOpWithFallback<cir::BitCtzOp>(*this, e);
case Builtin::BI__builtin_elementwise_ctzg:
- cgm.errorNYI(e->getSourceRange(), "__builtin_elementwise_ctzg");
- return RValue::get(nullptr);
+ return emitBuiltinBitOpWithFallback<cir::BitCtzOp>(*this, e,
+ /*isElementwise=*/true);
case Builtin::BI__builtin_clzs:
case Builtin::BI__builtin_clz:
@@ -1475,8 +1489,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
case Builtin::BI__builtin_clzg:
return emitBuiltinBitOpWithFallback<cir::BitClzOp>(*this, e);
case Builtin::BI__builtin_elementwise_clzg:
- cgm.errorNYI(e->getSourceRange(), "__builtin_elementwise_clzg");
- return RValue::get(nullptr);
+ return emitBuiltinBitOpWithFallback<cir::BitClzOp>(*this, e,
+ /*isElementwise=*/true);
case Builtin::BI__builtin_ffs:
case Builtin::BI__builtin_ffsl:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c b/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
index f712dfcd8e0394..3f4c52812eab3f 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtins-elementwise.c
@@ -1373,3 +1373,43 @@ void test_builtin_elementwise_maximum(float f1, float f2, double d1, double d2,
// LLVM-NEXT: call <4 x float> @llvm.maximum.v4f32(<4 x float> [[VF2]], <4 x float> [[CVF1]])
vf1 = __builtin_elementwise_maximum(vf2, cvf1);
}
+
+void test_builtin_elementwise_ctzg_clzg(unsigned u, int s, int fallback,
+ vuint4 vu4, vint4 vi4,
+ vint4 vfallback,
+ unsigned _BitInt(31) bi31) {
+ // CIR-LABEL: test_builtin_elementwise_ctzg_clzg
+ // LLVM-LABEL: test_builtin_elementwise_ctzg_clzg
+
+ // CIR: cir.ctz %{{.+}} poison_zero : !u32i
+ // LLVM: call i32 @llvm.cttz.i32(i32 %{{.+}}, i1 true)
+ u = __builtin_elementwise_ctzg(u);
+
+ // CIR: %[[U:.+]] = cir.cast integral %{{.+}} : !s32i -> !u32i
+ // CIR: %[[CTZ:.+]] = cir.ctz %[[U]] poison_zero : !u32i
+ // CIR: %[[RES:.+]] = cir.cast integral %[[CTZ]] : !u32i -> !s32i
+ // CIR: %[[CMP:.+]] = cir.cmp eq %[[U]], %{{.+}} : !u32i
+ // CIR: cir.select if %[[CMP]] then %{{.+}} else %[[RES]] : (!cir.bool, !s32i, !s32i) -> !s32i
+ // LLVM: %[[CTZ:.+]] = call i32 @llvm.cttz.i32(i32 %{{.+}}, i1 true)
+ // LLVM: %[[ISZERO:.+]] = icmp eq i32 %{{.+}}, 0
+ // LLVM: select i1 %[[ISZERO]], i32 %{{.+}}, i32 %[[CTZ]]
+ s = __builtin_elementwise_ctzg(s, fallback);
+
+ // CIR: cir.ctz %{{.+}} poison_zero : !cir.vector<4 x !u32i>
+ // LLVM: call <4 x i32> @llvm.cttz.v4i32(<4 x i32> %{{.+}}, i1 true)
+ vu4 = __builtin_elementwise_ctzg(vu4);
+
+ // CIR: %[[UV:.+]] = cir.cast integral %{{.+}} : !cir.vector<4 x !s32i> -> !cir.vector<4 x !u32i>
+ // CIR: %[[CLZ:.+]] = cir.clz %[[UV]] poison_zero : !cir.vector<4 x !u32i>
+ // CIR: %[[VRES:.+]] = cir.cast integral %[[CLZ]] : !cir.vector<4 x !u32i> -> !cir.vector<4 x !s32i>
+ // CIR: %[[VCMP:.+]] = cir.vec.cmp(eq, %[[UV]], %{{.+}}) : !cir.vector<4 x !u32i>, !cir.vector<4 x !cir.bool>
+ // CIR: cir.select if %[[VCMP]] then %{{.+}} else %[[VRES]] : (!cir.vector<4 x !cir.bool>, !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>) -> !cir.vector<4 x !s32i>
+ // LLVM: %[[VCLZ:.+]] = call <4 x i32> @llvm.ctlz.v4i32(<4 x i32> %{{.+}}, i1 true)
+ // LLVM: %[[VISZERO:.+]] = icmp eq <4 x i32> %{{.+}}, zeroinitializer
+ // LLVM: select <4 x i1> %[[VISZERO]], <4 x i32> %{{.+}}, <4 x i32> %[[VCLZ]]
+ vi4 = __builtin_elementwise_clzg(vi4, vfallback);
+
+ // CIR: cir.ctz %{{.+}} poison_zero : !cir.int<u, 31, bitint>
+ // LLVM: call i31 @llvm.cttz.i31(i31 %{{.+}}, i1 true)
+ bi31 = __builtin_elementwise_ctzg(bi31);
+}
More information about the cfe-commits
mailing list