Author: Henrich Lauko Date: 2026-11-27T14:09:22+01:00 New Revision: 7bcdf0a78c1ec663dff64dedc421fd0823002005
URL: https://github.com/llvm/llvm-project/commit/7bcdf0a78c1ec663dff64dedc421fd0823002005 DIFF: https://github.com/llvm/llvm-project/commit/7bcdf0a78c1ec663dff64dedc421fd0823002005.diff LOG: [CIR] Use one check for ignorable FP exceptions (#245985) Speculation and lowering each decided on their own whether a constrained operation can ignore floating-point exceptions, and they disagreed. An operation with `fenv<strict_except = false>` was speculatable but lowered to `fpexcept.maytrap`, and one with masked but strict exceptions was not speculatable but lowered to `fpexcept.ignore`. `cir.vec.fcmp` never folded a constrained comparison. This adds `FenvAttr::canIgnoreExceptions()`, true when exceptions are masked and not strict, and uses it in all three places: - Speculation already used this condition and does not change. - Lowering emits `fpexcept.ignore` when it holds, and otherwise `fpexcept.strict` or `fpexcept.maytrap` by `strict_except`. Three hand-written forms change: `fenv<strict_except = false>` goes from maytrap to ignore, masked exceptions with `strict_except = true` from ignore to strict, and unmasked or unknown exceptions without `strict_except` from ignore to maytrap. The first changes an existing check in `clang/test/CIR/Lowering/fenv.cir`. Lowering now applies the documented `masked` default of an absent `except_mode`, as speculation did. - A constrained `cir.vec.fcmp` folds like an unconstrained one when it holds. CIRGen emits masked exceptions that are not strict, and unknown exceptions that are strict or not. The lowering of these is unchanged. Assisted-by: Claude Code (Fable 5.2). Added: Modified: clang/include/clang/CIR/Dialect/IR/CIRAttrs.td clang/include/clang/CIR/Dialect/IR/CIRDialect.h clang/lib/CIR/Dialect/IR/CIRDialect.cpp clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp clang/test/CIR/Lowering/fenv.cir clang/test/CIR/Transforms/vector-cmp-fold.cir Removed: ################################################################################ diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 33360b773093dd..12e7fbdaa2c01a 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -972,6 +972,13 @@ def CIR_FenvAttr : CIR_Attr<"Fenv", "fenv"> { mlir::BoolAttr strictExcept = getStrictExcept(); return strictExcept && strictExcept.getValue(); } + + /// Whether exceptions are masked and not strict. The operation then + /// cannot trap and need not raise its exception flags. + bool canIgnoreExceptions() const { + return getExceptionModeOrDefault() == cir::FPExceptionMode::Masked && + !getStrictExceptOrDefault(); + } }]; let canHaveIllegalCXXABIType = 0; diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h index c128b79e11c23a..7df1588c979b9c 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h @@ -52,11 +52,11 @@ template <typename ConcreteType> class FenvOpTrait : public mlir::OpTrait::TraitBase<ConcreteType, FenvOpTrait> { public: mlir::Speculation::Speculatability getSpeculatability() { - // Masked exceptions cannot trap. When strict_except is false, exception - // side effects are non-deterministic, so speculation is still safe. - FPEnvConstrainedOpInterface fenvOp = getFenvOp(); - if (fenvOp.getFenvExceptionMode() == cir::FPExceptionMode::Masked && - !fenvOp.getFenvStrictExcept()) + // An unconstrained operation, and one that can ignore exceptions, cannot + // trap, and nothing may rely on the exception flags it raises, so + // speculation is safe. + cir::FenvAttr fenv = getFenvOp().getFenvAttr(); + if (!fenv || fenv.canIgnoreExceptions()) return mlir::Speculation::Speculatable; return mlir::Speculation::NotSpeculatable; @@ -66,6 +66,9 @@ class FenvOpTrait : public mlir::OpTrait::TraitBase<ConcreteType, FenvOpTrait> { llvm::SmallVectorImpl<mlir::MemoryEffects::EffectInstance> &effects) { if (!getFenvOp().getFenvAttr()) return; + // A constrained operation is modelled as reading the floating-point + // control state and writing the status flags. The write keeps it ordered + // against environment changes even when its exceptions can be ignored. effects.emplace_back(mlir::MemoryEffects::Read::get(), FloatingPointEnvironmentResource::get()); effects.emplace_back(mlir::MemoryEffects::Write::get(), diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index af5fdadefbcb40..28bcda81bc3db6 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -4573,12 +4573,13 @@ OpFoldResult cir::VecCmpOp::fold(FoldAdaptor adaptor) { //===----------------------------------------------------------------------===// OpFoldResult cir::VecFCmpOp::fold(FoldAdaptor adaptor) { - // Do not fold when fenv is present. - // TODO(cir): This is overly conservative. We could fold comparisons as long - // as we can prove that the operation wouldn't raise exceptions or - // when the fenv attribute does not require strict exception - // semantics. - if (getFenvAttr()) + // Folding drops the exceptions the comparison would raise, such as invalid + // for a signaling NaN, or for any NaN in a signaling comparison, so a + // constrained comparison folds only when it can ignore exceptions. The + // rounding mode does not affect a comparison. + // TODO(cir): This is still conservative. We could also fold when we can + // prove that the operation wouldn't raise exceptions. + if (cir::FenvAttr fenv = getFenvAttr(); fenv && !fenv.canIgnoreExceptions()) return {}; cir::FCmpOpKind kind = getKind(); diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 00799c5d1e2a7a..a3940f9c158cbc 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -645,13 +645,10 @@ static llvm::StringRef getConstrainedRoundingMetadata(cir::FenvAttr fenv) { } static llvm::StringRef getConstrainedExceptMetadata(cir::FenvAttr fenv) { - std::optional<cir::FPExceptionMode> exceptMode = fenv.getExceptMode(); - if (exceptMode == cir::FPExceptionMode::Masked) + if (fenv.canIgnoreExceptions()) return "fpexcept.ignore"; - mlir::BoolAttr strictExcept = fenv.getStrictExcept(); - if (!strictExcept) - return "fpexcept.ignore"; - return strictExcept.getValue() ? "fpexcept.strict" : "fpexcept.maytrap"; + return fenv.getStrictExceptOrDefault() ? "fpexcept.strict" + : "fpexcept.maytrap"; } static mlir::Value diff --git a/clang/test/CIR/Lowering/fenv.cir b/clang/test/CIR/Lowering/fenv.cir index ad815e18b874a3..e3d039cdc22418 100644 --- a/clang/test/CIR/Lowering/fenv.cir +++ b/clang/test/CIR/Lowering/fenv.cir @@ -49,7 +49,7 @@ module { // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.strict"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" %0 = cir.fadd %a, %b : !cir.float fenv<strict_except = true> - // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.maytrap"> + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" %1 = cir.fadd %a, %b : !cir.float fenv<strict_except = false> // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> @@ -58,6 +58,26 @@ module { // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" %3 = cir.fadd %a, %b : !cir.float fenv<dynamic_rounding_mode = tonearest> + // Strict exceptions stay strict when masked. + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.strict"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %4 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked, strict_except = true> + // Exceptions that are unmasked or unknown, and not strict, may trap. + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.maytrap"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %5 = cir.fadd %a, %b : !cir.float fenv<except_mode = unmasked> + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.maytrap"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %6 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown> + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.maytrap"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %7 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown, strict_except = false> + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.strict"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %8 = cir.fadd %a, %b : !cir.float fenv<except_mode = unmasked, strict_except = true> + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.strict"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %9 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown, strict_except = true> cir.return } diff --git a/clang/test/CIR/Transforms/vector-cmp-fold.cir b/clang/test/CIR/Transforms/vector-cmp-fold.cir index dc5f4118587a59..8971a1da73acf2 100644 --- a/clang/test/CIR/Transforms/vector-cmp-fold.cir +++ b/clang/test/CIR/Transforms/vector-cmp-fold.cir @@ -514,3 +514,148 @@ module { // CHECK-SAME: !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> // CHECK-SAME: fenv<strict_except = true> } + +// ----- + +!s32i = !cir.int<s, 32> + +module { + cir.func @fold_fcmp_vector_with_ignored_exceptions() -> !cir.vector<4 x !s32i> { + %vec_1 = cir.const #cir.const_vector<[ + #cir.fp<0x7FC00000> : !cir.float, + #cir.fp<2.000000e+00> : !cir.float, + #cir.fp<3.000000e+00> : !cir.float, + #cir.fp<4.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %vec_2 = cir.const #cir.const_vector<[ + #cir.fp<5.000000e+00> : !cir.float, + #cir.fp<6.000000e+00> : !cir.float, + #cir.fp<7.000000e+00> : !cir.float, + #cir.fp<8.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : + !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> + fenv<dynamic_rounding_mode = upward, except_mode = masked> + cir.return %new_vec : !cir.vector<4 x !s32i> + } + + // Masked, non-strict exceptions can be ignored, so the comparison folds. + // CHECK-LABEL: cir.func{{.*}} @fold_fcmp_vector_with_ignored_exceptions + // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<0> : !s32i, #cir.int<-1> : !s32i, + // CHECK-SAME: #cir.int<-1> : !s32i, #cir.int<-1> : !s32i]> : !cir.vector<4 x !s32i> + // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i> +} + +// ----- + +!s32i = !cir.int<s, 32> + +module { + cir.func @no_fold_fcmp_vector_with_unknown_exceptions() -> !cir.vector<4 x !s32i> { + %vec_1 = cir.const #cir.const_vector<[ + #cir.fp<0x7FC00000> : !cir.float, + #cir.fp<2.000000e+00> : !cir.float, + #cir.fp<3.000000e+00> : !cir.float, + #cir.fp<4.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %vec_2 = cir.const #cir.const_vector<[ + #cir.fp<5.000000e+00> : !cir.float, + #cir.fp<6.000000e+00> : !cir.float, + #cir.fp<7.000000e+00> : !cir.float, + #cir.fp<8.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : + !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> + fenv<except_mode = unknown> + cir.return %new_vec : !cir.vector<4 x !s32i> + } + + // Exceptions that are not masked may trap, so the comparison stays. + // CHECK-LABEL: cir.func{{.*}} @no_fold_fcmp_vector_with_unknown_exceptions + // CHECK: cir.vec.fcmp(olt, %{{.*}}, %{{.*}}) : + // CHECK-SAME: !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> + // CHECK-SAME: fenv<except_mode = unknown> +} + +// ----- + +!s32i = !cir.int<s, 32> + +module { + cir.func @fold_fcmp_vector_with_default_fenv() -> !cir.vector<4 x !s32i> { + %vec_1 = cir.const #cir.const_vector<[ + #cir.fp<0x7FC00000> : !cir.float, + #cir.fp<2.000000e+00> : !cir.float, + #cir.fp<3.000000e+00> : !cir.float, + #cir.fp<4.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %vec_2 = cir.const #cir.const_vector<[ + #cir.fp<5.000000e+00> : !cir.float, + #cir.fp<6.000000e+00> : !cir.float, + #cir.fp<7.000000e+00> : !cir.float, + #cir.fp<8.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : + !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<> + cir.return %new_vec : !cir.vector<4 x !s32i> + } + + // The default environment can ignore exceptions, so the comparison folds. + // CHECK-LABEL: cir.func{{.*}} @fold_fcmp_vector_with_default_fenv + // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<0> : !s32i, #cir.int<-1> : !s32i, + // CHECK-SAME: #cir.int<-1> : !s32i, #cir.int<-1> : !s32i]> : !cir.vector<4 x !s32i> + // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i> +} + +// ----- + +!s32i = !cir.int<s, 32> + +module { + cir.func @fold_signaling_fcmp_vector_with_non_strict_fenv() -> !cir.vector<4 x !s32i> { + %vec_1 = cir.const #cir.const_vector<[ + #cir.fp<0x7FC00000> : !cir.float, + #cir.fp<2.000000e+00> : !cir.float, + #cir.fp<3.000000e+00> : !cir.float, + #cir.fp<4.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %vec_2 = cir.const #cir.const_vector<[ + #cir.fp<5.000000e+00> : !cir.float, + #cir.fp<6.000000e+00> : !cir.float, + #cir.fp<7.000000e+00> : !cir.float, + #cir.fp<8.000000e+00> : !cir.float + ]> : !cir.vector<4 x !cir.float> + %new_vec = cir.vec.fcmp(olt, %vec_1, %vec_2) : + !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> signaling + fenv<strict_except = false> + cir.return %new_vec : !cir.vector<4 x !s32i> + } + + // The NaN operand makes the signaling comparison raise an invalid exception, + // which folding drops. + // CHECK-LABEL: cir.func{{.*}} @fold_signaling_fcmp_vector_with_non_strict_fenv + // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<0> : !s32i, #cir.int<-1> : !s32i, + // CHECK-SAME: #cir.int<-1> : !s32i, #cir.int<-1> : !s32i]> : !cir.vector<4 x !s32i> + // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i> +} + +// ----- + +!s32i = !cir.int<s, 32> + +module { + cir.func @fold_true_fcmp_vector_with_default_fenv( + %a: !cir.vector<4 x !cir.float>, %b: !cir.vector<4 x !cir.float>) + -> !cir.vector<4 x !s32i> { + %new_vec = cir.vec.fcmp(true, %a, %b) : + !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<> + cir.return %new_vec : !cir.vector<4 x !s32i> + } + + // A `true` comparison does not depend on its operands, so it folds without + // constant ones. + // CHECK-LABEL: cir.func{{.*}} @fold_true_fcmp_vector_with_default_fenv + // CHECK-NEXT: %[[RES:.*]] = cir.const #cir.const_vector<[#cir.int<-1> : !s32i, #cir.int<-1> : !s32i, + // CHECK-SAME: #cir.int<-1> : !s32i, #cir.int<-1> : !s32i]> : !cir.vector<4 x !s32i> + // CHECK-NEXT: cir.return %[[RES]] : !cir.vector<4 x !s32i> +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
