Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -1923,7 +1923,7 @@ MarshallingInfoFlag>, Normalizer<"makeFlagToValueNormalizer(llvm::RoundingMode::Dynamic)">; def fno_rounding_math : Flag<["-"], "fno-rounding-math">, Group, Flags<[CC1Option]>; -def ftrapping_math : Flag<["-"], "ftrapping-math">, Group; +def ftrapping_math : Flag<["-"], "ftrapping-math">, Group, Flags<[FC1Option]>; def fno_trapping_math : Flag<["-"], "fno-trapping-math">, Group; def ffp_contract : Joined<["-"], "ffp-contract=">, Group, Flags<[CC1Option, FC1Option, FlangOption]>, Index: clang/lib/Driver/ToolChains/Flang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Flang.cpp +++ clang/lib/Driver/ToolChains/Flang.cpp @@ -90,6 +90,7 @@ bool AssociativeMath = false; bool ReciprocalMath = false; bool RoundingFPMath = false; + bool TrappingMath = false; if (const Arg *A = Args.getLastArg(options::OPT_ffp_contract)) { const StringRef Val = A->getValue(); @@ -157,6 +158,12 @@ case options::OPT_fno_rounding_math: RoundingFPMath = false; break; + case options::OPT_ftrapping_math: + TrappingMath = true; + break; + case options::OPT_fno_trapping_math: + TrappingMath = false; + break; } // If we handled this option claim it @@ -178,7 +185,7 @@ if (!SignedZeros) CmdArgs.push_back("-fno-signed-zeros"); - if (AssociativeMath && !SignedZeros) + if (AssociativeMath && !SignedZeros && !TrappingMath) CmdArgs.push_back("-mreassociate"); if (ReciprocalMath) @@ -186,6 +193,9 @@ if (RoundingFPMath) CmdArgs.push_back("-frounding-math"); + + if (TrappingMath) + CmdArgs.push_back("-ftrapping-math"); } void Flang::ConstructJob(Compilation &C, const JobAction &JA, Index: flang/include/flang/Common/MathOptionsBase.def =================================================================== --- flang/include/flang/Common/MathOptionsBase.def +++ flang/include/flang/Common/MathOptionsBase.def @@ -41,4 +41,7 @@ /// Do not assume default floating-point rounding behavior ENUM_MATHOPT(RoundingMath, unsigned, 1, 0) +/// Tells the compiler whether to assume that floating point operations can trap +ENUM_MATHOPT(TrappingMath, unsigned, 1, 0) + #undef ENUM_MATHOPT Index: flang/include/flang/Frontend/LangOptions.def =================================================================== --- flang/include/flang/Frontend/LangOptions.def +++ flang/include/flang/Frontend/LangOptions.def @@ -36,6 +36,8 @@ LANGOPT(ReciprocalMath, 1, false) /// Do not assume default floating-point rounding behavior LANGOPT(RoundingMath, 1, false) +/// Tells the compiler whether to assume that floating point operations can trap +LANGOPT(TrappingMath, 1, false) #undef LANGOPT #undef ENUM_LANGOPT Index: flang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- flang/lib/Frontend/CompilerInvocation.cpp +++ flang/lib/Frontend/CompilerInvocation.cpp @@ -741,6 +741,12 @@ opts.RoundingMath = true; } + if (const llvm::opt::Arg *a = + args.getLastArg(clang::driver::options::OPT_ftrapping_math)) { + diags.Report(diagUnimplemented) << a->getOption().getName(); + opts.TrappingMath = true; + } + return true; } @@ -972,5 +978,6 @@ .setNoSignedZeros(langOptions.NoSignedZeros) .setAssociativeMath(langOptions.AssociativeMath) .setReciprocalMath(langOptions.ReciprocalMath) - .setRoundingMath(langOptions.RoundingMath); + .setRoundingMath(langOptions.RoundingMath) + .setTrappingMath(langOptions.TrappingMath); } Index: flang/lib/Optimizer/Builder/FIRBuilder.cpp =================================================================== --- flang/lib/Optimizer/Builder/FIRBuilder.cpp +++ flang/lib/Optimizer/Builder/FIRBuilder.cpp @@ -619,7 +619,7 @@ if (options.getReciprocalMath()) { arithFMF = arithFMF | mlir::arith::FastMathFlags::arcp; } - // TODO: getRoundingMath() + // TODO: getRoundingMath(), getTrappingMath() setFastMathFlags(arithFMF); } Index: flang/test/Driver/flang_fp_opts.f90 =================================================================== --- flang/test/Driver/flang_fp_opts.f90 +++ flang/test/Driver/flang_fp_opts.f90 @@ -9,6 +9,7 @@ ! RUN: -mreassociate \ ! RUN: -freciprocal-math \ ! RUN: -frounding-math \ +! RUN: -ftrapping-math \ ! RUN: %s 2>&1 | FileCheck %s ! CHECK: ffp-contract= is not currently implemented ! CHECK: menable-no-infs is not currently implemented @@ -18,3 +19,4 @@ ! CHECK: mreassociate is not currently implemented ! CHECK: freciprocal-math is not currently implemented ! CHECK: frounding-math is not currently implemented +! CHECK: ftrapping-math is not currently implemented Index: flang/test/Driver/frontend-forwarding.f90 =================================================================== --- flang/test/Driver/frontend-forwarding.f90 +++ flang/test/Driver/frontend-forwarding.f90 @@ -38,3 +38,10 @@ ! CHECK: "-fconvert=little-endian" ! CHECK: "-fpass-plugin=Bye ! CHECK: "-mllvm" "-print-before-all" + +! Check -ftrapping-math. This has to be done separately because it prevents +! -mreassociate from being forwarded +! RUN: %flang -fsyntax-only -### %s -o %t 2>&1 \ +! RUN: -ftrapping-math \ +! RUN: | FileCheck --check-prefix=CHECK-TRAP %s +! CHECK-TRAP: "-ftrapping-math"