https://github.com/Fznamznon created https://github.com/llvm/llvm-project/pull/244577
This adds a dedicated CIR op for floating scalar comparisons and converts a couple of obvious fp comparison places to use the new op. >From 9a9108c2297582e207bc7f07ba36466df868277c Mon Sep 17 00:00:00 2001 From: Mariya Podchishchaeva <[email protected]> Date: Fri, 20 Nov 2026 06:21:09 -0600 Subject: [PATCH] [CIR] Introduce `cir.fcmp` op This adds a dedicated CIR op for floating scalar comparisons and converts a couple of obvious fp comparison places to use the new op. --- .../CIR/Dialect/Builder/CIRBaseBuilder.h | 10 +- clang/include/clang/CIR/Dialect/IR/CIROps.td | 116 ++++++++++++++---- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 12 +- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 22 ++++ .../CodeGen/complex-compound-assignment.cpp | 4 +- clang/test/CIR/CodeGen/complex-mul-div.cpp | 4 +- .../test/CIR/CodeGenBuiltins/builtin-float.c | 38 +++--- 7 files changed, 155 insertions(+), 51 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h index 967a826274602b..b44fb5f842b9b1 100644 --- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h +++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h @@ -957,6 +957,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { return cir::CmpOp::create(*this, loc, kind, lhs, rhs, fenv); } + cir::FCmpOp createFCompare(mlir::Location loc, cir::FCmpOpKind kind, + mlir::Value lhs, mlir::Value rhs, + bool signaling = false) { + cir::FenvAttr fenv; + fenv = getConstrainedFPAttr(); + return cir::FCmpOp::create(*this, loc, kind, lhs, rhs, signaling, fenv); + } + /// Compares \p lhs and \p rhs element-wise. Each result element is a /// signed integer of the operand element's bit width. cir::VecCmpOp createVecCompare(mlir::Location loc, cir::CmpOpKind kind, @@ -1020,7 +1028,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder { } mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand) { - return createCompare(loc, cir::CmpOpKind::ne, operand, operand); + return createFCompare(loc, cir::FCmpOpKind::une, operand, operand); } mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 87842e875e7874..5a66d0c2acc0ed 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -2758,6 +2758,101 @@ def CIR_CmpOp : CIR_Op<"cmp", let hasCXXABILowering = true; } +//===----------------------------------------------------------------------===// +// FCmpOp +//===----------------------------------------------------------------------===// + +def CIR_FCmpOpKind : CIR_I32Enum<"FCmpOpKind", "floating compare operation kind", [ + I32EnumCase<"_false", 0, "false">, + I32EnumCase<"oeq", 1>, + I32EnumCase<"ogt", 2>, + I32EnumCase<"oge", 3>, + I32EnumCase<"olt", 4>, + I32EnumCase<"ole", 5>, + I32EnumCase<"one", 6>, + I32EnumCase<"ord", 7>, + I32EnumCase<"ueq", 8>, + I32EnumCase<"ugt", 9>, + I32EnumCase<"uge", 10>, + I32EnumCase<"ult", 11>, + I32EnumCase<"ule", 12>, + I32EnumCase<"une", 13>, + I32EnumCase<"uno", 14>, + I32EnumCase<"_true", 15, "true"> +]>; + +def CIR_FCmpOpKindAttr : CIR_EnumAttr<CIR_FCmpOpKind, "fcmp">; + +def CIR_FCmpOp : CIR_Op<"fcmp", + !listconcat([SameTypeOperands], CIR_FenvOpTraits)> { + let summary = "Compare two floating point values and produce a boolean result"; + let description = [{ + The `cir.cmp` operation compares two operands of the same floating type and + produces a `!cir.bool` result. + + The following comparison predicates are available: + + - `false`: no comparison, always returns false + - `oeq`: ordered and equal + - `ogt`: ordered and greater than + - `oge`: ordered and greater than or equal + - `olt`: ordered and less than + - `ole`: ordered and less than or equal + - `one`: ordered and not equal + - `ord`: ordered (no nans) + - `ueq`: unordered or equal + - `ugt`: unordered or greater than + - `uge`: unordered or greater than or equal + - `ult`: unordered or less than + - `ule`: unordered or less than or equal + - `une`: unordered or not equal + - `uno`: unordered (either nans) + - `true`: no comparison, always returns true + + The optional `fenv` attribute describes constraints on the floating-point + handling of the operation. It is only valid for floating-point + comparisons. + + `signaling` attribute overrides whether a floating-point + comparison raises an invalid exception for quiet NaN operands if fenv + attribute is present. + + ``` + %0 = cir.fcmp olt %x, %y : !cir.float fenv<> + ``` + }]; + + let arguments = (ins + CIR_FCmpOpKindAttr:$kind, + CIR_AnyFloatType:$lhs, + CIR_AnyFloatType:$rhs, + UnitAttr:$signaling, + OptionalAttr<CIR_FenvAttr>:$fenv + ); + + let results = (outs CIR_BoolType:$result); + + let assemblyFormat = [{ + enum($kind) $lhs `,` $rhs `:` type($lhs) + (`signaling` $signaling^)? + (`fenv` `` $fenv^)? attr-dict + }]; + + let builders = [ + OpBuilder<(ins "cir::FCmpOpKind":$kind, "mlir::Value":$lhs, + "mlir::Value":$rhs), [{ + build($_builder, $_state, kind, lhs, rhs, /*signaling=*/false, + cir::FenvAttr{}); + }]>, + OpBuilder<(ins "mlir::Type":$result, "cir::FCmpOpKind":$kind, + "mlir::Value":$lhs, "mlir::Value":$rhs), [{ + build($_builder, $_state, result, kind, lhs, rhs, /*signaling=*/false, + cir::FenvAttr{}); + }]> + ]; + +} + //===----------------------------------------------------------------------===// // Checked Binary Arithmetic Operations (with overflow) //===----------------------------------------------------------------------===// @@ -6889,27 +6984,6 @@ def CIR_VecCmpOp : CIR_Op<"vec.cmp", [Pure, SameTypeOperands]> { // VecFCmpOp //===----------------------------------------------------------------------===// -def CIR_FCmpOpKind : CIR_I32Enum<"FCmpOpKind", "floating compare operation kind", [ - I32EnumCase<"_false", 0, "false">, - I32EnumCase<"oeq", 1>, - I32EnumCase<"ogt", 2>, - I32EnumCase<"oge", 3>, - I32EnumCase<"olt", 4>, - I32EnumCase<"ole", 5>, - I32EnumCase<"one", 6>, - I32EnumCase<"ord", 7>, - I32EnumCase<"ueq", 8>, - I32EnumCase<"ugt", 9>, - I32EnumCase<"uge", 10>, - I32EnumCase<"ult", 11>, - I32EnumCase<"ule", 12>, - I32EnumCase<"une", 13>, - I32EnumCase<"uno", 14>, - I32EnumCase<"_true", 15, "true"> -]>; - -def CIR_FCmpOpKindAttr : CIR_EnumAttr<CIR_FCmpOpKind, "fcmp">; - def CIR_VecFCmpOp : CIR_Op<"vec.fcmp", !listconcat([SameTypeOperands], CIR_FenvOpTraits)> { let summary = "Compare two floating point vectors"; diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 2fd24edd626a69..10230d29b2545b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1980,22 +1980,22 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, mlir::Value cmpResult; switch (builtinID) { case Builtin::BI__builtin_isgreater: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::gt, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::ogt, lhs, rhs); break; case Builtin::BI__builtin_isgreaterequal: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::ge, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::oge, lhs, rhs); break; case Builtin::BI__builtin_isless: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::lt, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::olt, lhs, rhs); break; case Builtin::BI__builtin_islessequal: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::le, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::ole, lhs, rhs); break; case Builtin::BI__builtin_islessgreater: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::one, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::one, lhs, rhs); break; case Builtin::BI__builtin_isunordered: - cmpResult = builder.createCompare(loc, cir::CmpOpKind::uno, lhs, rhs); + cmpResult = builder.createFCompare(loc, cir::FCmpOpKind::uno, lhs, rhs); break; default: llvm_unreachable("Unknown ordered comparison"); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 3980709ed14879..f3ce3a723370c0 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -4324,6 +4324,28 @@ mlir::LogicalResult CIRToLLVMCmpOpLowering::matchAndRewrite( return cmpOp.emitError() << "unsupported type for CmpOp: " << type; } +mlir::LogicalResult CIRToLLVMFCmpOpLowering::matchAndRewrite( + cir::FCmpOp FCmpOp, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + mlir::Type type = FCmpOp.getLhs().getType(); + if (!mlir::isa<cir::FPTypeInterface>(type)) + return FCmpOp.emitError() << "unsupported type for FCmpOp: " << type; + + mlir::Type llvmResTy = getTypeConverter()->convertType(FCmpOp.getType()); + if (cir::FenvAttr fenv = FCmpOp.getFenvAttr()) { + mlir::LLVM::CallIntrinsicOp call = createConstrainedFCmpCall( + rewriter, FCmpOp.getLoc(), adaptor.getLhs(), adaptor.getRhs(), + FCmpOp.getKind(), FCmpOp.getSignaling(), fenv, llvmResTy); + rewriter.replaceOp(FCmpOp, call.getResult(0)); + return mlir::success(); + } + mlir::LLVM::FCmpPredicate kind = + convertCmpKindToFCmpPredicate(FCmpOp.getKind()); + rewriter.replaceOpWithNewOp<mlir::LLVM::FCmpOp>(FCmpOp, kind, adaptor.getLhs(), + adaptor.getRhs()); + return mlir::success(); +} + /// Shared lowering logic for checked binary arithmetic overflow operations. /// The \p opStr parameter specifies the arithmetic operation name used in the /// LLVM intrinsic (e.g., "add", "sub", "mul"). diff --git a/clang/test/CIR/CodeGen/complex-compound-assignment.cpp b/clang/test/CIR/CodeGen/complex-compound-assignment.cpp index ed4afa9b54cff1..d16f3e3f580ab2 100644 --- a/clang/test/CIR/CodeGen/complex-compound-assignment.cpp +++ b/clang/test/CIR/CodeGen/complex-compound-assignment.cpp @@ -408,8 +408,8 @@ void foo7() { // CIR: %[[C_REAL:.*]] = cir.fsub %[[MUL_BR_AR]], %[[MUL_BI_AI]] : !cir.float // CIR: %[[C_IMAG:.*]] = cir.fadd %[[MUL_BR_AI]], %[[MUL_BI_AR]] : !cir.float // CIR: %[[COMPLEX:.*]] = cir.complex.create %[[C_REAL]], %[[C_IMAG]] : !cir.float -> !cir.complex<!cir.float> -// CIR: %[[IS_C_REAL_NAN:.*]] = cir.cmp ne %[[C_REAL]], %[[C_REAL]] : !cir.float -// CIR: %[[IS_C_IMAG_NAN:.*]] = cir.cmp ne %[[C_IMAG]], %[[C_IMAG]] : !cir.float +// CIR: %[[IS_C_REAL_NAN:.*]] = cir.fcmp une %[[C_REAL]], %[[C_REAL]] : !cir.float +// CIR: %[[IS_C_IMAG_NAN:.*]] = cir.fcmp une %[[C_IMAG]], %[[C_IMAG]] : !cir.float // CIR: %[[CONST_FALSE:.*]] = cir.const #false // CIR: %[[SELECT_CONDITION:.*]] = cir.select if %[[IS_C_REAL_NAN]] then %[[IS_C_IMAG_NAN]] else %[[CONST_FALSE]] : (!cir.bool, !cir.bool, !cir.bool) -> !cir.bool // CIR: %[[RESULT:.*]] = cir.ternary(%[[SELECT_CONDITION]], true { diff --git a/clang/test/CIR/CodeGen/complex-mul-div.cpp b/clang/test/CIR/CodeGen/complex-mul-div.cpp index e0f7bfd1a218c0..7c75b790e2cbd1 100644 --- a/clang/test/CIR/CodeGen/complex-mul-div.cpp +++ b/clang/test/CIR/CodeGen/complex-mul-div.cpp @@ -123,8 +123,8 @@ void foo() { // CIR-AFTER-FULL: %[[C_REAL:.*]] = cir.fsub %[[MUL_AR_BR]], %[[MUL_AI_BI]] : !cir.float // CIR-AFTER-FULL: %[[C_IMAG:.*]] = cir.fadd %[[MUL_AR_BI]], %[[MUL_AI_BR]] : !cir.float // CIR-AFTER-FULL: %[[COMPLEX:.*]] = cir.complex.create %[[C_REAL]], %[[C_IMAG]] : !cir.float -> !cir.complex<!cir.float> -// CIR-AFTER-FULL: %[[IS_C_REAL_NAN:.*]] = cir.cmp ne %[[C_REAL]], %[[C_REAL]] : !cir.float -// CIR-AFTER-FULL: %[[IS_C_IMAG_NAN:.*]] = cir.cmp ne %[[C_IMAG]], %[[C_IMAG]] : !cir.float +// CIR-AFTER-FULL: %[[IS_C_REAL_NAN:.*]] = cir.fcmp une %[[C_REAL]], %[[C_REAL]] : !cir.float +// CIR-AFTER-FULL: %[[IS_C_IMAG_NAN:.*]] = cir.fcmp une %[[C_IMAG]], %[[C_IMAG]] : !cir.float // CIR-AFTER-FULL: %[[CONST_FALSE:.*]] = cir.const #false // CIR-AFTER-FULL: %[[SELECT_CONDITION:.*]] = cir.select if %[[IS_C_REAL_NAN]] then %[[IS_C_IMAG_NAN]] else %[[CONST_FALSE]] : (!cir.bool, !cir.bool, !cir.bool) -> !cir.bool // CIR-AFTER-FULL: %[[RESULT:.*]] = cir.ternary(%[[SELECT_CONDITION]], true { diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-float.c b/clang/test/CIR/CodeGenBuiltins/builtin-float.c index 64d06a9220f9f5..26efb5fb9dcfe7 100644 --- a/clang/test/CIR/CodeGenBuiltins/builtin-float.c +++ b/clang/test/CIR/CodeGenBuiltins/builtin-float.c @@ -300,32 +300,32 @@ __float128 test_ldexpf128(__float128 x, int e) { void test_floats(float x, float y, int *out) { out[0] = __builtin_isgreater(x, y); - // CIR: cir.cmp gt %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp ogt %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp ogt float // OGCG: fcmp ogt float out[1] = __builtin_isgreaterequal(x, y); - // CIR: cir.cmp ge %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp oge %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp oge float // OGCG: fcmp oge float out[2] = __builtin_isless(x, y); - // CIR: cir.cmp lt %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp olt %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp olt float // OGCG: fcmp olt float out[3] = __builtin_islessequal(x, y); - // CIR: cir.cmp le %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp ole %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp ole float // OGCG: fcmp ole float out[4] = __builtin_islessgreater(x, y); - // CIR: cir.cmp one %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp one %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp one float // OGCG: fcmp one float out[5] = __builtin_isunordered(x, y); - // CIR: cir.cmp uno %{{.*}}, %{{.*}} : !cir.float + // CIR: cir.fcmp uno %{{.*}}, %{{.*}} : !cir.float // LLVM: fcmp uno float // OGCG: fcmp uno float } @@ -334,32 +334,32 @@ void test_floats(float x, float y, int *out) { void test_doubles(double x, double y, int *out) { out[0] = __builtin_isgreater(x, y); - // CIR: cir.cmp gt %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp ogt %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp ogt double // OGCG: fcmp ogt double out[1] = __builtin_isgreaterequal(x, y); - // CIR: cir.cmp ge %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp oge %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp oge double // OGCG: fcmp oge double out[2] = __builtin_isless(x, y); - // CIR: cir.cmp lt %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp olt %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp olt double // OGCG: fcmp olt double out[3] = __builtin_islessequal(x, y); - // CIR: cir.cmp le %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp ole %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp ole double // OGCG: fcmp ole double out[4] = __builtin_islessgreater(x, y); - // CIR: cir.cmp one %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp one %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp one double // OGCG: fcmp one double out[5] = __builtin_isunordered(x, y); - // CIR: cir.cmp uno %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp uno %{{.*}}, %{{.*}} : !cir.double // LLVM: fcmp uno double // OGCG: fcmp uno double } @@ -369,7 +369,7 @@ void test_doubles(double x, double y, int *out) { void test_mixed(double x, float y, int *out) { out[0] = __builtin_isgreater(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp gt %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp ogt %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp ogt double // OGCG: fpext float %{{.*}} to double @@ -377,7 +377,7 @@ void test_mixed(double x, float y, int *out) { out[1] = __builtin_isgreaterequal(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp ge %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp oge %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp oge double // OGCG: fpext float %{{.*}} to double @@ -385,7 +385,7 @@ void test_mixed(double x, float y, int *out) { out[2] = __builtin_isless(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp lt %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp olt %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp olt double // OGCG: fpext float %{{.*}} to double @@ -393,7 +393,7 @@ void test_mixed(double x, float y, int *out) { out[3] = __builtin_islessequal(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp le %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp ole %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp ole double // OGCG: fpext float %{{.*}} to double @@ -401,7 +401,7 @@ void test_mixed(double x, float y, int *out) { out[4] = __builtin_islessgreater(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp one %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp one %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp one double // OGCG: fpext float %{{.*}} to double @@ -409,7 +409,7 @@ void test_mixed(double x, float y, int *out) { out[5] = __builtin_isunordered(x, y); // CIR: cir.cast floating %{{.*}} : !cir.float -> !cir.double - // CIR: cir.cmp uno %{{.*}}, %{{.*}} : !cir.double + // CIR: cir.fcmp uno %{{.*}}, %{{.*}} : !cir.double // LLVM: fpext float %{{.*}} to double // LLVM: fcmp uno double // OGCG: fpext float %{{.*}} to double @@ -425,7 +425,7 @@ int test_isgreater_half(__fp16 *a, __fp16 *b) { // CIR-LABEL: cir.func {{.*}} @test_isgreater_half // CIR: cir.cast floating %{{.*}} : !cir.f16 -> !cir.float // CIR: cir.cast floating %{{.*}} : !cir.f16 -> !cir.float -// CIR: cir.cmp gt %{{.*}}, %{{.*}} : !cir.float +// CIR: cir.fcmp ogt %{{.*}}, %{{.*}} : !cir.float // LLVM-LABEL: define {{.*}} i32 @test_isgreater_half // LLVM: fpext half %{{.*}} to float _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
