Index: clang/lib/CodeGen/CGCall.cpp =================================================================== --- clang/lib/CodeGen/CGCall.cpp +++ clang/lib/CodeGen/CGCall.cpp @@ -1862,7 +1862,11 @@ FuncAttrs.addAttribute("no-nans-fp-math", "true"); if (LangOpts.ApproxFunc) FuncAttrs.addAttribute("approx-func-fp-math", "true"); - if (LangOpts.UnsafeFPMath) + if ((LangOpts.FastMath || + !LangOpts.FastMath && LangOpts.AllowFPReassoc && LangOpts.AllowRecip && + !LangOpts.FiniteMathOnly && LangOpts.NoSignedZero && + LangOpts.ApproxFunc) && + LangOpts.getDefaultFPContractMode() != LangOptions::FPModeKind::FPM_Off) FuncAttrs.addAttribute("unsafe-fp-math", "true"); if (CodeGenOpts.SoftFloat) FuncAttrs.addAttribute("use-soft-float", "true"); Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -3099,10 +3099,6 @@ if (MathErrno) CmdArgs.push_back("-fmath-errno"); - if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros && - ApproxFunc && !TrappingMath) - CmdArgs.push_back("-menable-unsafe-fp-math"); - if (!SignedZeros) CmdArgs.push_back("-fno-signed-zeros"); Index: clang/test/CodeGen/func-attr.c =================================================================== --- /dev/null +++ clang/test/CodeGen/func-attr.c @@ -0,0 +1,12 @@ +// RUN: %clang -c -ffast-math -emit-llvm -S -o - %s \ +// RUN: | FileCheck %s + +// RUN: %clang -c -funsafe-math-optimizations -emit-llvm -S -o - %s \ +// RUN: | FileCheck %s + +float foo(float a, float b) { + return a+b; +} + +// CHECK: define{{.*}} float @foo(float noundef %a, float noundef %b) [[FAST_ATTRS:#[0-9]+]] +// CHECK: attributes [[FAST_ATTRS]] = { {{.*}} "approx-func-fp-math"="true" {{.*}} "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" {{.*}} "unsafe-fp-math"="true" Index: clang/test/Driver/fast-math.c =================================================================== --- clang/test/Driver/fast-math.c +++ clang/test/Driver/fast-math.c @@ -151,14 +151,12 @@ // RUN: -fno-signed-zeros -fno-trapping-math -fapprox-func -c %s 2>&1 \ // RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s // CHECK-UNSAFE-MATH: "-cc1" -// CHECK-UNSAFE-MATH: "-menable-unsafe-fp-math" // CHECK-UNSAFE-MATH: "-mreassociate" // // RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \ // RUN: -fno-signed-zeros -fno-trapping-math -fapprox-func -c %s 2>&1 \ // RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH-UNSAFE-MATH %s // CHECK-NO-FAST-MATH-UNSAFE-MATH: "-cc1" -// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-menable-unsafe-fp-math" // CHECK-NO-FAST-MATH-UNSAFE-MATH: "-mreassociate" // The 2nd -fno-fast-math overrides -fassociative-math. @@ -167,7 +165,6 @@ // RUN: -fno-fast-math -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \ // RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH-NO-FAST-MATH %s // CHECK-UNSAFE-MATH-NO-FAST-MATH: "-cc1" -// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-mreassociate" // // Check that various umbrella flags also enable these frontend options. @@ -280,7 +277,6 @@ // RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s // CHECK-NO-UNSAFE-MATH: "-cc1" -// CHECK-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-NO_UNSAFE-MATH-NOT: "-mreassociate" // CHECK-NO-UNSAFE-MATH: "-o" @@ -292,9 +288,7 @@ // RUN: | FileCheck --check-prefix=CHECK-REASSOC-NO-UNSAFE-MATH %s // CHECK-REASSOC-NO-UNSAFE-MATH: "-cc1" -// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-REASSOC-NO_UNSAFE-MATH: "-mreassociate" -// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-REASSOC-NO-UNSAFE-MATH: "-o" @@ -309,9 +303,7 @@ // RUN: | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s // CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-cc1" -// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-mreassociate" -// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math" // CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-o" Index: clang/test/Driver/fp-model.c =================================================================== --- clang/test/Driver/fp-model.c +++ clang/test/Driver/fp-model.c @@ -86,7 +86,6 @@ // CHECK-FPM-FAST: "-cc1" // CHECK-FPM-FAST: "-menable-no-infs" // CHECK-FPM-FAST: "-menable-no-nans" -// CHECK-FPM-FAST: "-menable-unsafe-fp-math" // CHECK-FPM-FAST: "-fno-signed-zeros" // CHECK-FPM-FAST: "-mreassociate" // CHECK-FPM-FAST: "-freciprocal-math"