https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/236704
>From cb18e063967b790fae1e52171783f9f08a1f3c1c Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> Date: Thu, 12 Nov 2026 13:28:25 +0000 Subject: [PATCH] [CIR] Reject the empty fenv attribute `#cir.fenv<>` with no parameter set was accepted, printed as `fenv<>` and lowered to a constrained intrinsic, so an unconstrained operation had two spellings. Give `#cir.fenv` a verifier that requires at least one parameter, leaving a missing `fenv` attribute as the only way to say an operation is unconstrained. An absent parameter takes its value in the default FP environment (tonearest, masked, non-strict). Speculation and lowering share one FenvAttr::canIgnoreExceptions() check (masked and not strict, lowered to fpexcept.ignore), so a strict op stays fpexcept.strict even when masked. Reduce FPEnvConstrainedOpInterface to getFenvAttr() and add a CIR_FenvOp base class carrying the fenv traits and argument, which the fenv ops prepend their operands to. Assisted-by: Claude Code (Opus 5.5). --- .../include/clang/CIR/Dialect/IR/CIRAttrs.td | 35 +++---- .../include/clang/CIR/Dialect/IR/CIRDialect.h | 5 +- clang/include/clang/CIR/Dialect/IR/CIROps.td | 95 +++++++++---------- .../clang/CIR/Interfaces/CIROpInterfaces.td | 31 +----- clang/lib/CIR/Dialect/IR/CIRAttrs.cpp | 14 +++ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 15 +-- .../CIR/Analysis/alias-analysis-modref.cir | 2 +- clang/test/CIR/IR/fenv.cir | 18 ++-- clang/test/CIR/IR/invalid-cast.cir | 10 +- clang/test/CIR/IR/invalid-cmp.cir | 6 +- clang/test/CIR/IR/invalid-fenv.cir | 7 ++ clang/test/CIR/Lowering/fenv.cir | 14 ++- clang/unittests/CIR/FenvOpTest.cpp | 44 ++++----- 13 files changed, 142 insertions(+), 154 deletions(-) create mode 100644 clang/test/CIR/IR/invalid-fenv.cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td index 9841e66fec2eb9..65818952d47cf2 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td @@ -920,17 +920,21 @@ def CIR_FenvAttr : CIR_Attr<"Fenv", "fenv"> { parameters are optional. - `dynamic_rounding_mode`: the known dynamic rounding mode at the point the - instruction is executed. Otherwise the behavior is undefined. + instruction is executed. If the actual mode differs, the behavior is + undefined. - `except_mode`: the known exception mode at the point the instruction is - executed. Otherwise the behavior is undefined. + executed. If the actual mode differs, the behavior is undefined. - `strict_except`: if `false`, any case that would produce an FP exception produces it non-deterministically instead (i.e. it may or may not occur). This means the FP status is written non-deterministically, and, if exceptions are unmasked, the instruction traps non-deterministically. - An absent `dynamic_rounding_mode` defaults to `unknown`, an absent - `except_mode` defaults to `masked`, and an absent `strict_except` defaults - to `false`. + An absent parameter takes its value in the default floating-point + environment: `dynamic_rounding_mode` defaults to `tonearest`, `except_mode` + to `masked`, and `strict_except` to `false`. + + At least one parameter must be set. An operation without a `fenv` + attribute is not constrained. Example: ``` @@ -950,30 +954,29 @@ def CIR_FenvAttr : CIR_Attr<"Fenv", "fenv"> { }]; let extraClassDeclaration = [{ - static constexpr cir::FPDynamicRoundingMode - getDefaultDynamicRoundingMode() { - return cir::FPDynamicRoundingMode::Unknown; - } - - static constexpr cir::FPExceptionMode getDefaultExceptionMode() { - return cir::FPExceptionMode::Masked; - } - cir::FPDynamicRoundingMode getDynamicRoundingModeOrDefault() const { return getDynamicRoundingMode().value_or( - getDefaultDynamicRoundingMode()); + cir::FPDynamicRoundingMode::ToNearest); } cir::FPExceptionMode getExceptionModeOrDefault() const { - return getExceptMode().value_or(getDefaultExceptionMode()); + return getExceptMode().value_or(cir::FPExceptionMode::Masked); } bool getStrictExceptOrDefault() const { mlir::BoolAttr strictExcept = getStrictExcept(); return strictExcept && strictExcept.getValue(); } + + /// Whether exceptions are masked and not strict, so the operation can + /// neither trap nor has to keep the floating-point status exact. + bool canIgnoreExceptions() const { + return getExceptionModeOrDefault() == cir::FPExceptionMode::Masked && + !getStrictExceptOrDefault(); + } }]; + let genVerifyDecl = 1; let canHaveIllegalCXXABIType = 0; } diff --git a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h index 04abe65453993f..bbc548c04e2f98 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRDialect.h +++ b/clang/include/clang/CIR/Dialect/IR/CIRDialect.h @@ -54,9 +54,8 @@ class FenvOpTrait : public mlir::OpTrait::TraitBase<ConcreteType, FenvOpTrait> { 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()) + cir::FenvAttr fenv = getFenvOp().getFenvAttr(); + if (!fenv || fenv.canIgnoreExceptions()) return mlir::Speculation::Speculatable; return mlir::Speculation::NotSpeculatable; diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 0e540588b45f97..92094ac05b940f 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -220,6 +220,14 @@ defvar CIR_FenvOpTraits = [ CIR_FenvOpTrait ]; +// Base class for operations constrained by an optional `fenv` attribute. +// Subclasses prepend their own arguments. +class CIR_FenvOp<string mnemonic, list<Trait> traits = []> + : CIR_Op<mnemonic, traits> { + let append traits = CIR_FenvOpTraits; + let arguments = (ins OptionalAttr<CIR_FenvAttr>:$fenv); +} + // Passed as CIR_BinaryOp's hasNoMemoryEffect: fenv ops compute effects // dynamically via FenvOpTrait, so they must not inherit NoMemoryEffect. defvar CIR_DynamicMemoryEffects = 0; @@ -305,9 +313,9 @@ def CIR_CastKind : CIR_I32Enum<"CastKind", "cast kind", [ def CIR_CastKindAttr : CIR_EnumAttr<CIR_CastKind, "cast">; -def CIR_CastOp : CIR_Op<"cast", !listconcat([ +def CIR_CastOp : CIR_FenvOp<"cast", [ DeclareOpInterfaceMethods<PromotableOpInterface> -], CIR_FenvOpTraits)> { +]> { let summary = "Conversion between values of different types"; let description = [{ Apply the usual C/C++ conversion rules between values. This operation models @@ -357,12 +365,12 @@ def CIR_CastOp : CIR_Op<"cast", !listconcat([ ... %x = cir.cast array_to_ptrdecay %0 : !cir.ptr<!cir.array<!s32i x 10>> -> !cir.ptr<!s32i> - %y = cir.cast floating %f : !cir.double -> !cir.float fenv<> + %y = cir.cast floating %f : !cir.double -> !cir.float + fenv<except_mode = unknown, strict_except = true> ``` }]; - let arguments = (ins CIR_CastKindAttr:$kind, CIR_AnyType:$src, - OptionalAttr<CIR_FenvAttr>:$fenv); + let prepend arguments = (ins CIR_CastKindAttr:$kind, CIR_AnyType:$src); let results = (outs CIR_AnyType:$result); let assemblyFormat = [{ @@ -2664,8 +2672,7 @@ def CIR_CmpOpKind : CIR_I32Enum<"CmpOpKind", "compare operation kind", [ def CIR_CmpOpKindAttr : CIR_EnumAttr<CIR_CmpOpKind, "cmp">; -def CIR_CmpOp : CIR_Op<"cmp", - !listconcat([SameTypeOperands], CIR_FenvOpTraits)> { +def CIR_CmpOp : CIR_FenvOp<"cmp", [SameTypeOperands]> { let summary = "Compare two values and produce a boolean result"; let description = [{ The `cir.cmp` operation compares two operands of the same type and produces @@ -2696,15 +2703,15 @@ def CIR_CmpOp : CIR_Op<"cmp", ``` %0 = cir.cmp gt %1, %2 : !s32i %1 = cir.cmp eq %a, %b : !cir.ptr<!u8i> - %2 = cir.cmp lt %x, %y : !cir.float fenv<> + %2 = cir.cmp lt %x, %y : !cir.float + fenv<except_mode = unknown, strict_except = true> ``` }]; - let arguments = (ins + let prepend arguments = (ins CIR_CmpOpKindAttr:$kind, CIR_ComparableType:$lhs, - CIR_ComparableType:$rhs, - OptionalAttr<CIR_FenvAttr>:$fenv + CIR_ComparableType:$rhs ); let results = (outs CIR_BoolType:$result); @@ -3055,9 +3062,10 @@ class CIR_FPBinaryOp<string mnemonic, list<Trait> traits = [], : CIR_BinaryOp<mnemonic, type, !listconcat(CIR_FenvOpTraits, traits), CIR_DynamicMemoryEffects> { - let arguments = !con(commonArgs, (ins - OptionalAttr<CIR_FenvAttr>:$fenv, - OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags)); + let append arguments = (ins + OptionalAttr<CIR_FenvAttr>:$fenv, + OptionalAttr<CIR_FastMathFlagsAttr>:$fastmath_flags + ); let assemblyFormat = [{ $lhs `,` $rhs `:` type($lhs) (`fenv` `` $fenv^)? prop-dict attr-dict }]; @@ -6656,8 +6664,7 @@ def CIR_VecReduceFMulOp : CIR_VecReduceAccFPOp<"fmul", "vector_reduce_fmul">; // VecCmpOp //===----------------------------------------------------------------------===// -def CIR_VecCmpOp : CIR_Op<"vec.cmp", - !listconcat([SameTypeOperands], CIR_FenvOpTraits)> { +def CIR_VecCmpOp : CIR_FenvOp<"vec.cmp", [SameTypeOperands]> { let summary = "Compare two vectors"; let description = [{ The `cir.vec.cmp` operation does an element-wise comparison of two vectors @@ -6672,15 +6679,15 @@ def CIR_VecCmpOp : CIR_Op<"vec.cmp", ``` %eq = cir.vec.cmp(eq, %vec_a, %vec_b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i> %lt = cir.vec.cmp(lt, %vec_a, %vec_b) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i> - %gt = cir.vec.cmp(gt, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<> + %gt = cir.vec.cmp(gt, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> + fenv<except_mode = unknown, strict_except = true> ``` }]; - let arguments = (ins + let prepend arguments = (ins CIR_CmpOpKindAttr:$kind, CIR_VectorType:$lhs, - CIR_VectorType:$rhs, - OptionalAttr<CIR_FenvAttr>:$fenv + CIR_VectorType:$rhs ); let results = (outs CIR_VectorType:$result); @@ -6726,8 +6733,7 @@ def CIR_FCmpOpKind : CIR_I32Enum<"FCmpOpKind", "floating compare operation kind" def CIR_FCmpOpKindAttr : CIR_EnumAttr<CIR_FCmpOpKind, "fcmp">; -def CIR_VecFCmpOp : CIR_Op<"vec.fcmp", - !listconcat([SameTypeOperands], CIR_FenvOpTraits)> { +def CIR_VecFCmpOp : CIR_FenvOp<"vec.fcmp", [SameTypeOperands]> { let summary = "Compare two floating point vectors"; let description = [{ The `cir.vec.fcmp` operation does an element-wise comparison of two vectors @@ -6744,16 +6750,15 @@ def CIR_VecFCmpOp : CIR_Op<"vec.fcmp", ``` %ogt = cir.vec.fcmp(ogt, %va, %vb) : !cir.vector<4 x !cir.float>, - !cir.vector<4 x !s32i> signaling fenv<> + !cir.vector<4 x !s32i> signaling fenv<strict_except = true> ``` }]; - let arguments = (ins + let prepend arguments = (ins CIR_FCmpOpKindAttr:$kind, CIR_VectorOfFloatType:$lhs, CIR_VectorOfFloatType:$rhs, - UnitAttr:$signaling, - OptionalAttr<CIR_FenvAttr>:$fenv + UnitAttr:$signaling ); let results = (outs CIR_VectorType:$result); @@ -8473,11 +8478,8 @@ def CIR_PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> { // The optional `fenv` attribute describes constraints on the floating-point // handling of the operation. class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName> - : CIR_Op<mnemonic, - !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)> -{ - let arguments = (ins CIR_AnyFloatOrVecOfFloatType:$src, - OptionalAttr<CIR_FenvAttr>:$fenv); + : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> { + let prepend arguments = (ins CIR_AnyFloatOrVecOfFloatType:$src); let results = (outs CIR_AnyFloatOrVecOfFloatType:$result); let assemblyFormat = "$src `:` type($src) (`fenv` `` $fenv^)? attr-dict"; @@ -8799,10 +8801,8 @@ def CIR_FloorOp : CIR_UnaryFPToFPBuiltinOp<"floor", "FFloorOp"> { } class CIR_UnaryFPToIntBuiltinOp<string mnemonic, string llvmOpName> - : CIR_Op<mnemonic, CIR_FenvOpTraits> -{ - let arguments = (ins CIR_AnyFloatType:$src, - OptionalAttr<CIR_FenvAttr>:$fenv); + : CIR_FenvOp<mnemonic> { + let prepend arguments = (ins CIR_AnyFloatType:$src); let results = (outs CIR_IntType:$result); let summary = [{ @@ -8863,16 +8863,14 @@ def CIR_LlrintOp : CIR_UnaryFPToIntBuiltinOp<"llrint", "LlrintOp"> { } class CIR_BinaryFPToFPBuiltinOp<string mnemonic, string llvmOpName> - : CIR_Op<mnemonic, - !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)> { + : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> { let summary = [{ libc builtin equivalent ignoring floating-point exceptions and errno. }]; - let arguments = (ins + let prepend arguments = (ins CIR_AnyFloatOrVecOfFloatType:$lhs, - CIR_AnyFloatOrVecOfFloatType:$rhs, - OptionalAttr<CIR_FenvAttr>:$fenv + CIR_AnyFloatOrVecOfFloatType:$rhs ); let results = (outs CIR_AnyFloatOrVecOfFloatType:$result); @@ -8975,8 +8973,7 @@ def CIR_ATan2Op : CIR_BinaryFPToFPBuiltinOp<"atan2", "ATan2Op"> { let constrainedLLVMIntrinsic = "atan2"; } -def CIR_LdexpOp : CIR_Op<"ldexp", - !listconcat([AllTypesMatch<["src", "result"]>], CIR_FenvOpTraits)> { +def CIR_LdexpOp : CIR_FenvOp<"ldexp", [AllTypesMatch<["src", "result"]>]> { let summary = "Multiplies a floating-point value by an integral power of two"; let description = [{ `cir.ldexp` computes `src * 2^exp`. @@ -8988,10 +8985,9 @@ def CIR_LdexpOp : CIR_Op<"ldexp", %r = cir.ldexp %x, %e : !cir.float, !s32i }]; - let arguments = (ins + let prepend arguments = (ins CIR_AnyFloatOrVecOfFloatType:$src, - CIR_AnyIntOrVecOfIntType:$exp, - OptionalAttr<CIR_FenvAttr>:$fenv + CIR_AnyIntOrVecOfIntType:$exp ); let results = (outs CIR_AnyFloatOrVecOfFloatType:$result); @@ -9055,14 +9051,11 @@ def CIR_ModfOp : CIR_Op<"modf", [Pure]> { } class CIR_TernaryFPToFPBuiltinOp<string mnemonic, string llvmOpName> - : CIR_Op<mnemonic, - !listconcat([SameOperandsAndResultType], CIR_FenvOpTraits)> -{ - let arguments = (ins + : CIR_FenvOp<mnemonic, [SameOperandsAndResultType]> { + let prepend arguments = (ins CIR_AnyFloatOrVecOfFloatType:$a, CIR_AnyFloatOrVecOfFloatType:$b, - CIR_AnyFloatOrVecOfFloatType:$c, - OptionalAttr<CIR_FenvAttr>:$fenv + CIR_AnyFloatOrVecOfFloatType:$c ); let results = (outs CIR_AnyFloatOrVecOfFloatType:$result); diff --git a/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td b/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td index ba172abcc5ec63..ab91774ba9a7da 100644 --- a/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td +++ b/clang/include/clang/CIR/Interfaces/CIROpInterfaces.td @@ -274,40 +274,13 @@ let cppNamespace = "::cir" in { : OpInterface<"FPEnvConstrainedOpInterface"> { let description = [{ Interface for floating-point operations that carry an optional - `#cir.fenv` attribute. It provides effective values for each field, - substituting the floating-point environment defaults for absent fields. + `#cir.fenv` attribute. An operation without one is not constrained. }]; let methods = [ InterfaceMethod< "Return the `#cir.fenv` attribute, or a null attribute if absent.", - "cir::FenvAttr", "getFenvAttr", (ins), [{}], - /*defaultImplementation=*/[{ - return $_op.getFenvAttr(); - }]>, - InterfaceMethod< - "Return the effective dynamic rounding mode.", - "cir::FPDynamicRoundingMode", "getFenvDynamicRoundingMode", (ins), - [{}], /*defaultImplementation=*/[{ - cir::FenvAttr fenv = $_op.getFenvAttr(); - return fenv ? fenv.getDynamicRoundingModeOrDefault() - : cir::FenvAttr::getDefaultDynamicRoundingMode(); - }]>, - InterfaceMethod< - "Return the effective floating-point exception mode.", - "cir::FPExceptionMode", "getFenvExceptionMode", (ins), [{}], - /*defaultImplementation=*/[{ - cir::FenvAttr fenv = $_op.getFenvAttr(); - return fenv ? fenv.getExceptionModeOrDefault() - : cir::FenvAttr::getDefaultExceptionMode(); - }]>, - InterfaceMethod< - "Return the effective strict-exception flag.", - "bool", "getFenvStrictExcept", (ins), [{}], - /*defaultImplementation=*/[{ - cir::FenvAttr fenv = $_op.getFenvAttr(); - return fenv ? fenv.getStrictExceptOrDefault() : false; - }]>, + "cir::FenvAttr", "getFenvAttr">, ]; } diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp index 96f9f82e6ad869..bec82555493a47 100644 --- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp @@ -319,6 +319,20 @@ LogicalResult OptInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError, return success(); } +//===----------------------------------------------------------------------===// +// FenvAttr definitions +//===----------------------------------------------------------------------===// + +LogicalResult +FenvAttr::verify(function_ref<InFlightDiagnostic()> emitError, + std::optional<FPDynamicRoundingMode> dynamicRoundingMode, + std::optional<FPExceptionMode> exceptMode, + mlir::BoolAttr strictExcept) { + if (!dynamicRoundingMode && !exceptMode && !strictExcept) + return emitError() << "at least one parameter must be set"; + return success(); +} + //===----------------------------------------------------------------------===// // ConstPtrAttr definitions //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index fd38367ff959dd..f26349cdf53bd4 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -618,11 +618,7 @@ static mlir::LLVM::CallIntrinsicOp replaceOpWithCallLLVMIntrinsicOp( } static llvm::StringRef getConstrainedRoundingMetadata(cir::FenvAttr fenv) { - std::optional<cir::FPDynamicRoundingMode> rounding = - fenv.getDynamicRoundingMode(); - if (!rounding) - return "round.tonearest"; - switch (*rounding) { + switch (fenv.getDynamicRoundingModeOrDefault()) { case cir::FPDynamicRoundingMode::ToNearest: return "round.tonearest"; case cir::FPDynamicRoundingMode::Downward: @@ -640,13 +636,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) - return "fpexcept.ignore"; - mlir::BoolAttr strictExcept = fenv.getStrictExcept(); - if (!strictExcept) + if (fenv.canIgnoreExceptions()) 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/Analysis/alias-analysis-modref.cir b/clang/test/CIR/Analysis/alias-analysis-modref.cir index 781d3bd3baa60f..386e1eccae3b14 100644 --- a/clang/test/CIR/Analysis/alias-analysis-modref.cir +++ b/clang/test/CIR/Analysis/alias-analysis-modref.cir @@ -51,6 +51,6 @@ cir.func @modref_load() { !s32i = !cir.int<s, 32> cir.func @modref_fenv(%a: !cir.float, %b: !cir.float) { %x = cir.alloca "x" align(4) : !cir.ptr<!s32i> {test.ptr = "x"} - %0 = cir.fadd %a, %b : !cir.float fenv<> {test.ptr = "fadd_op"} + %0 = cir.fadd %a, %b : !cir.float fenv<strict_except = true> {test.ptr = "fadd_op"} cir.return } diff --git a/clang/test/CIR/IR/fenv.cir b/clang/test/CIR/IR/fenv.cir index 2c938c2c74c70b..f66d49d0081716 100644 --- a/clang/test/CIR/IR/fenv.cir +++ b/clang/test/CIR/IR/fenv.cir @@ -13,9 +13,9 @@ cir.func @fadd_fenv(%a: !cir.float, %b: !cir.float) { // CHECK-NOT: fenv %1 = cir.fadd %a, %b : !cir.float - // Empty fenv attribute. - // CHECK: cir.fadd %{{.*}}, %{{.*}} : !cir.float fenv<> - %2 = cir.fadd %a, %b : !cir.float fenv<> + // Fields set to their default value are kept. + // CHECK: cir.fadd %{{.*}}, %{{.*}} : !cir.float fenv<dynamic_rounding_mode = unknown, except_mode = masked, strict_except = false> + %2 = cir.fadd %a, %b : !cir.float fenv<dynamic_rounding_mode = unknown, except_mode = masked, strict_except = false> cir.return } @@ -73,8 +73,8 @@ cir.func @unary_fp_to_int_builtin_fenv(%a: !cir.float) -> !s32i { %0 = cir.lround %a : !cir.float -> !s32i fenv<dynamic_rounding_mode = tonearest> // CHECK: cir.llround %{{.*}} : !cir.float -> !s32i fenv<except_mode = unmasked> %1 = cir.llround %a : !cir.float -> !s32i fenv<except_mode = unmasked> - // CHECK: cir.lrint %{{.*}} : !cir.float -> !s32i fenv<> - %2 = cir.lrint %a : !cir.float -> !s32i fenv<> + // CHECK: cir.lrint %{{.*}} : !cir.float -> !s32i fenv<except_mode = unknown> + %2 = cir.lrint %a : !cir.float -> !s32i fenv<except_mode = unknown> // CHECK: cir.llrint %{{.*}} : !cir.float -> !s32i // CHECK-NOT: fenv %3 = cir.llrint %a : !cir.float -> !s32i @@ -110,8 +110,8 @@ cir.func @ternary_fp_builtin_fenv(%a: !cir.float, %b: !cir.float, %c: !cir.float cir.func @cast_fenv(%a: !cir.float, %b: !cir.double, %i: !s32i) { // CHECK: cir.cast floating %{{.*}} : !cir.double -> !cir.float fenv<dynamic_rounding_mode = tonearest> %0 = cir.cast floating %b : !cir.double -> !cir.float fenv<dynamic_rounding_mode = tonearest> - // CHECK: cir.cast int_to_float %{{.*}} : !s32i -> !cir.float fenv<> - %1 = cir.cast int_to_float %i : !s32i -> !cir.float fenv<> + // CHECK: cir.cast int_to_float %{{.*}} : !s32i -> !cir.float fenv<except_mode = unknown> + %1 = cir.cast int_to_float %i : !s32i -> !cir.float fenv<except_mode = unknown> // CHECK: cir.cast float_to_int %{{.*}} : !cir.float -> !s32i fenv<except_mode = unmasked, strict_except = true> %2 = cir.cast float_to_int %a : !cir.float -> !s32i fenv<except_mode = unmasked, strict_except = true> // CHECK: cir.cast float_to_bool %{{.*}} : !cir.float -> !cir.bool @@ -126,8 +126,8 @@ cir.func @cmp_fenv(%a: !cir.float, %b: !cir.float, %vb: !cir.vector<4 x !cir.float>) { // CHECK: cir.cmp lt %{{.*}}, %{{.*}} : !cir.float fenv<except_mode = unmasked> %0 = cir.cmp lt %a, %b : !cir.float fenv<except_mode = unmasked> - // CHECK: cir.cmp eq %{{.*}}, %{{.*}} : !cir.float fenv<> - %1 = cir.cmp eq %a, %b : !cir.float fenv<> + // CHECK: cir.cmp eq %{{.*}}, %{{.*}} : !cir.float fenv<except_mode = unknown> + %1 = cir.cmp eq %a, %b : !cir.float fenv<except_mode = unknown> // CHECK: cir.vec.cmp(gt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<strict_except = true> %2 = cir.vec.cmp(gt, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<strict_except = true> diff --git a/clang/test/CIR/IR/invalid-cast.cir b/clang/test/CIR/IR/invalid-cast.cir index 587cb7791aa0ee..54b621f45edcfa 100644 --- a/clang/test/CIR/IR/invalid-cast.cir +++ b/clang/test/CIR/IR/invalid-cast.cir @@ -57,7 +57,7 @@ module { module { cir.func @fenv_on_integral(%i: !s32i) { // expected-error@+1 {{'fenv' is only valid for floating-point cast kinds}} - %0 = cir.cast integral %i : !s32i -> !s32i fenv<> + %0 = cir.cast integral %i : !s32i -> !s32i fenv<strict_except = true> cir.return } } @@ -69,7 +69,7 @@ module { module { cir.func @fenv_on_int_to_bool(%i: !s32i) { // expected-error@+1 {{'fenv' is only valid for floating-point cast kinds}} - %0 = cir.cast int_to_bool %i : !s32i -> !cir.bool fenv<> + %0 = cir.cast int_to_bool %i : !s32i -> !cir.bool fenv<strict_except = true> cir.return } } @@ -81,7 +81,7 @@ module { module { cir.func @fenv_on_ptr_to_int(%p: !cir.ptr<!s32i>) { // expected-error@+1 {{'fenv' is only valid for floating-point cast kinds}} - %0 = cir.cast ptr_to_int %p : !cir.ptr<!s32i> -> !s32i fenv<> + %0 = cir.cast ptr_to_int %p : !cir.ptr<!s32i> -> !s32i fenv<strict_except = true> cir.return } } @@ -93,7 +93,7 @@ module { module { cir.func @fenv_on_bitcast(%p: !cir.ptr<!s32i>) { // expected-error@+1 {{'fenv' is only valid for floating-point cast kinds}} - %0 = cir.cast bitcast %p : !cir.ptr<!s32i> -> !cir.ptr<!cir.float> fenv<> + %0 = cir.cast bitcast %p : !cir.ptr<!s32i> -> !cir.ptr<!cir.float> fenv<strict_except = true> cir.return } } @@ -105,7 +105,7 @@ module { module { cir.func @fenv_on_int_complex(%c: !cir.complex<!s32i>) { // expected-error@+1 {{'fenv' is only valid for floating-point cast kinds}} - %0 = cir.cast int_complex %c : !cir.complex<!s32i> -> !cir.complex<!s32i> fenv<> + %0 = cir.cast int_complex %c : !cir.complex<!s32i> -> !cir.complex<!s32i> fenv<strict_except = true> cir.return } } diff --git a/clang/test/CIR/IR/invalid-cmp.cir b/clang/test/CIR/IR/invalid-cmp.cir index 25e09700c2d72f..337becafc1b525 100644 --- a/clang/test/CIR/IR/invalid-cmp.cir +++ b/clang/test/CIR/IR/invalid-cmp.cir @@ -5,7 +5,7 @@ module { cir.func @fenv_on_int_cmp(%a: !s32i, %b: !s32i) { // expected-error@+1 {{'fenv' is only valid for floating-point comparisons}} - %0 = cir.cmp eq %a, %b : !s32i fenv<> + %0 = cir.cmp eq %a, %b : !s32i fenv<strict_except = true> cir.return } } @@ -17,7 +17,7 @@ module { module { cir.func @fenv_on_ptr_cmp(%a: !cir.ptr<!s32i>, %b: !cir.ptr<!s32i>) { // expected-error@+1 {{'fenv' is only valid for floating-point comparisons}} - %0 = cir.cmp ne %a, %b : !cir.ptr<!s32i> fenv<> + %0 = cir.cmp ne %a, %b : !cir.ptr<!s32i> fenv<strict_except = true> cir.return } } @@ -31,7 +31,7 @@ module { %b: !cir.vector<4 x !s32i>) { // expected-error@+1 {{'fenv' is only valid for floating-point comparisons}} %0 = cir.vec.cmp(eq, %a, %b) : !cir.vector<4 x !s32i>, - !cir.vector<4 x !s32i> fenv<> + !cir.vector<4 x !s32i> fenv<strict_except = true> cir.return } } diff --git a/clang/test/CIR/IR/invalid-fenv.cir b/clang/test/CIR/IR/invalid-fenv.cir new file mode 100644 index 00000000000000..13c74d2eef3c8c --- /dev/null +++ b/clang/test/CIR/IR/invalid-fenv.cir @@ -0,0 +1,7 @@ +// RUN: cir-opt %s -verify-diagnostics -split-input-file + +cir.func @empty_fenv(%a: !cir.float, %b: !cir.float) { + // expected-error @below {{at least one parameter must be set}} + %0 = cir.fadd %a, %b : !cir.float fenv<> + cir.return +} diff --git a/clang/test/CIR/Lowering/fenv.cir b/clang/test/CIR/Lowering/fenv.cir index 5d0c39245164de..33aa1da90fb837 100644 --- a/clang/test/CIR/Lowering/fenv.cir +++ b/clang/test/CIR/Lowering/fenv.cir @@ -17,7 +17,7 @@ module { // CHECK: %[[RM2:.*]] = llvm.mlir.metadata_as_value #llvm.md_string<"round.tonearest"> // CHECK: %[[EB2:.*]] = llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd"(%{{.*}}, %{{.*}}, %[[RM2]], %[[EB2]]) : (f32, f32, !llvm.metadata, !llvm.metadata) -> f32 - %2 = cir.fadd %a, %b : !cir.float fenv<> + %2 = cir.fadd %a, %b : !cir.float fenv<dynamic_rounding_mode = tonearest, except_mode = masked, strict_except = false> cir.return } @@ -51,13 +51,21 @@ module { %0 = cir.fadd %a, %b : !cir.float fenv<strict_except = true> // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.maytrap"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" - %1 = cir.fadd %a, %b : !cir.float fenv<strict_except = false> + %1 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown, strict_except = false> // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" %2 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked, strict_except = false> // 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> + // An absent except_mode is masked. + // CHECK: llvm.mlir.metadata_as_value #llvm.md_string<"fpexcept.ignore"> + // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd" + %4 = cir.fadd %a, %b : !cir.float fenv<strict_except = false> + // 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" + %5 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked, strict_except = true> cir.return } @@ -277,7 +285,7 @@ module { // CHECK: llvm.fmul %{{.*}}, %{{.*}} fastmath<contract> : f32 %0 = cir.fmul %a, %b : !cir.float <fastmath_flags = [contract]> // CHECK: llvm.call_intrinsic "llvm.experimental.constrained.fadd"(%{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {fastmathFlags = #llvm.fastmath<contract>} : (f32, f32, !llvm.metadata, !llvm.metadata) -> f32 - %1 = cir.fadd %0, %b : !cir.float fenv<> <fastmath_flags = [contract]> + %1 = cir.fadd %0, %b : !cir.float fenv<except_mode = masked> <fastmath_flags = [contract]> cir.return } } diff --git a/clang/unittests/CIR/FenvOpTest.cpp b/clang/unittests/CIR/FenvOpTest.cpp index b0ff67b9167010..bbb034df5bff4d 100644 --- a/clang/unittests/CIR/FenvOpTest.cpp +++ b/clang/unittests/CIR/FenvOpTest.cpp @@ -68,19 +68,19 @@ TEST_F(CIRFenvOpTest, MemoryEffects) { %va: !cir.vector<4 x !cir.float>, %vb: !cir.vector<4 x !cir.float>) { %0 = cir.fadd %a, %b : !cir.float - %1 = cir.fadd %a, %b : !cir.float fenv<> - %2 = cir.sqrt %a : !cir.float fenv<> - %3 = cir.pow %a, %b : !cir.float fenv<> + %1 = cir.fadd %a, %b : !cir.float fenv<strict_except = true> + %2 = cir.sqrt %a : !cir.float fenv<strict_except = true> + %3 = cir.pow %a, %b : !cir.float fenv<strict_except = true> %4 = cir.fma %a, %b, %c : !cir.float - %5 = cir.fma %a, %b, %c : !cir.float fenv<> + %5 = cir.fma %a, %b, %c : !cir.float fenv<strict_except = true> %6 = cir.lround %a : !cir.float -> !s32i - %7 = cir.lround %a : !cir.float -> !s32i fenv<> + %7 = cir.lround %a : !cir.float -> !s32i fenv<strict_except = true> %8 = cir.cast floating %a : !cir.float -> !cir.double - %9 = cir.cast floating %a : !cir.float -> !cir.double fenv<> + %9 = cir.cast floating %a : !cir.float -> !cir.double fenv<strict_except = true> %10 = cir.cmp lt %a, %b : !cir.float - %11 = cir.cmp lt %a, %b : !cir.float fenv<> + %11 = cir.cmp lt %a, %b : !cir.float fenv<strict_except = true> %12 = cir.vec.cmp(eq, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> - %13 = cir.vec.cmp(eq, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<> + %13 = cir.vec.cmp(eq, %va, %vb) : !cir.vector<4 x !cir.float>, !cir.vector<4 x !s32i> fenv<strict_except = true> cir.return } )CIR"); @@ -141,34 +141,32 @@ TEST_F(CIRFenvOpTest, Speculatability) { OwningOpRef<ModuleOp> module = parse(R"CIR( cir.func @f(%a: !cir.float, %b: !cir.float) { %0 = cir.fadd %a, %b : !cir.float - %1 = cir.fadd %a, %b : !cir.float fenv<> - %2 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked> - %3 = cir.fadd %a, %b : !cir.float fenv<strict_except = false> - %4 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked, strict_except = true> - %5 = cir.fadd %a, %b : !cir.float fenv<except_mode = unmasked, strict_except = false> - %6 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown, strict_except = false> + %1 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked> + %2 = cir.fadd %a, %b : !cir.float fenv<strict_except = false> + %3 = cir.fadd %a, %b : !cir.float fenv<except_mode = masked, strict_except = true> + %4 = cir.fadd %a, %b : !cir.float fenv<except_mode = unmasked, strict_except = false> + %5 = cir.fadd %a, %b : !cir.float fenv<except_mode = unknown, strict_except = false> cir.return } )CIR"); ASSERT_TRUE(module); SmallVector<cir::FAddOp> ops = findOps<cir::FAddOp>(*module); - ASSERT_EQ(ops.size(), 7u); + ASSERT_EQ(ops.size(), 6u); // Missing fenv fields use the defaults: masked exceptions and non-strict // exception behavior. - for (unsigned i = 0; i != 4; ++i) + for (unsigned i = 0; i != 3; ++i) EXPECT_TRUE(isSpeculatable(ops[i])) << "operation " << i; - for (unsigned i = 4; i != ops.size(); ++i) + for (unsigned i = 3; i != ops.size(); ++i) EXPECT_FALSE(isSpeculatable(ops[i])) << "operation " << i; - cir::FPEnvConstrainedOpInterface fenvOp = - cast<cir::FPEnvConstrainedOpInterface>(ops[1].getOperation()); - EXPECT_EQ(fenvOp.getFenvDynamicRoundingMode(), - cir::FPDynamicRoundingMode::Unknown); - EXPECT_EQ(fenvOp.getFenvExceptionMode(), cir::FPExceptionMode::Masked); - EXPECT_FALSE(fenvOp.getFenvStrictExcept()); + cir::FenvAttr fenv = ops[2].getFenvAttr(); + EXPECT_EQ(fenv.getDynamicRoundingModeOrDefault(), + cir::FPDynamicRoundingMode::ToNearest); + EXPECT_EQ(fenv.getExceptionModeOrDefault(), cir::FPExceptionMode::Masked); + EXPECT_FALSE(fenv.getStrictExceptOrDefault()); } } // namespace _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
