Index: clang/include/clang/Basic/DiagnosticFrontendKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticFrontendKinds.td +++ clang/include/clang/Basic/DiagnosticFrontendKinds.td @@ -48,6 +48,10 @@ "overriding currently unsupported use of floating point exceptions " "on this target">, InGroup; +def err_incompatible_fp_eval_method_options : Error< + "option 'ffp-eval-method' cannot be used with" + " %select{option 'fapprox-func'|option 'mreassociate'|option 'freciprocal'}0.">; + def remark_fe_backend_optimization_remark : Remark<"%0">, BackendInfo, InGroup; def remark_fe_backend_optimization_remark_missed : Remark<"%0">, BackendInfo, Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6475,6 +6475,11 @@ "comparing floating point with == or != is unsafe">, InGroup>, DefaultIgnore; +def err_setting_eval_method_used_in_unsafe_context : Error < + "%select{'#pragma clang fp eval_method'|option 'ffp-eval-method'}0 cannot be used when" + " %select{option 'fapprox_func'|option 'mreassociate'|option 'freciprocal'|option 'ffp-eval-method'|'#pragma clang fp reassociate'}1" + " is enabled.">; + def warn_remainder_division_by_zero : Warning< "%select{remainder|division}0 by zero is undefined">, InGroup; Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -503,6 +503,22 @@ Diags.Report(diag::warn_ignored_hip_only_option) << Args.getLastArg(OPT_gpu_max_threads_per_block_EQ)->getAsString(Args); + // When these options are used, the compiler is allowed to apply + // optimizations that may affect the final result. For example + // (x+y)+z is transformed to x+(y+z) but may not give the same + // final result; it's not value safe. + // Another example can be to simplify x/x to 1.0 but x could be 0.0, INF + // or NaN. Final result may then differ. An error is issued when the eval + // method is set with one of these options. + if (Args.hasArg(OPT_ffp_eval_method_EQ)) { + if (LangOpts.ApproxFunc) + Diags.Report(diag::err_incompatible_fp_eval_method_options) << 0; + if (LangOpts.AllowFPReassoc) + Diags.Report(diag::err_incompatible_fp_eval_method_options) << 1; + if (LangOpts.AllowRecip) + Diags.Report(diag::err_incompatible_fp_eval_method_options) << 2; + } + // -cl-strict-aliasing needs to emit diagnostic in the case where CL > 1.0. // This option should be deprecated for CL > 1.0 because // this option was added for compatibility with OpenCL 1.0. Index: clang/lib/Sema/SemaAttr.cpp =================================================================== --- clang/lib/Sema/SemaAttr.cpp +++ clang/lib/Sema/SemaAttr.cpp @@ -486,6 +486,12 @@ NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Extended); break; } + if (getLangOpts().ApproxFunc) + Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 0; + if (getLangOpts().AllowFPReassoc) + Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 1; + if (getLangOpts().AllowRecip) + Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 2; FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); PP.setCurrentFPEvalMethod(Loc, Value); @@ -1136,6 +1142,23 @@ } void Sema::ActOnPragmaFPReassociate(SourceLocation Loc, bool IsEnabled) { + if (IsEnabled) { + // For value unsafe context, combining this pragma with eval method + // setting is not recommended. See comment in function FixupInvocation#506. + int Reason = -1; + if (getLangOpts().getFPEvalMethod() != LangOptions::FEM_UnsetOnCommandLine) + // Eval method set using the option 'ffp-eval-method'. + Reason = 1; + if (PP.getLastFPEvalPragmaLocation().isValid()) + // Eval method set using the '#pragma clang fp eval_method'. + // We could have both an option and a pragma used to the set the eval + // method. The pragma overrides the option in the command line. The Reason + // of the diagnostic is overriden too. + Reason = 0; + if (Reason != -1) + Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) + << Reason << 4; + } FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); NewFPFeatures.setAllowFPReassociateOverride(IsEnabled); FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); Index: clang/test/Sema/eval-method-with-unsafe-math.c =================================================================== --- /dev/null +++ clang/test/Sema/eval-method-with-unsafe-math.c @@ -0,0 +1,82 @@ +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-RECPR,CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -mreassociate \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-ASSOC,CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -fapprox-func \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FUNC,CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math -mreassociate -verify \ +// RUN: %s 2>&1 | FileCheck %s --check-prefixes=CHECK-ASSOC,CHECK-RECPR,CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math -mreassociate -fapprox-func \ +// RUN: -verify %s 2>&1 \ +// RUN: | FileCheck %s --check-prefixes=CHECK-FUNC,CHECK-ASSOC,CHECK-RECPR,CHECK-PRGM + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -fapprox-func -ffp-eval-method=source \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FE-FUNC + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -mreassociate -ffp-eval-method=source \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FE-ASSOC + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math -ffp-eval-method=source \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FE-RECPR + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math -mreassociate \ +// RUN: -ffp-eval-method=source \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FE-ASSOC,CHECK-FE-RECPR + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -freciprocal-math -mreassociate -fapprox-func \ +// RUN: -ffp-eval-method=source -verify %s 2>&1 \ +// RUN: | FileCheck %s --check-prefixes=CHECK-FE-ASSOC,CHECK-FE-RECPR,CHECK-FE-FUNC + +// RUN: not %clang_cc1 -fexperimental-strict-floating-point \ +// RUN: -triple x86_64-linux-gnu -ffp-eval-method=source \ +// RUN: -verify %s 2>&1 | FileCheck %s --check-prefixes=CHECK-FFP-OPT,CHECK-PRGM + +// CHECK-FE-FUNC: (frontend): option 'ffp-eval-method' cannot be used with option 'fapprox-func'. +// CHECK-FE-ASSOC: (frontend): option 'ffp-eval-method' cannot be used with option 'mreassociate'. +// CHECK-FE-RECPR: (frontend): option 'ffp-eval-method' cannot be used with option 'freciprocal'. + +// expected-no-diagnostics + +float f1(float a, float b, float c) { + a = b + c; + return a * b + c; +} + +float f2(float a, float b, float c) { + // CHECK-FFP-OPT: option 'ffp-eval-method' cannot be used when '#pragma clang fp reassociate' is enabled. +#pragma clang fp reassociate(on) + return (a + b) + c; +} + +float f3(float a, float b, float c) { +#pragma clang fp reassociate(off) + return (a - b) - c; +} + +float f4(float a, float b, float c) { +#pragma clang fp eval_method (double) + // CHECK-FUNC: '#pragma clang fp eval_method' cannot be used when option 'fapprox_func' is enabled. + // CHECK-ASSOC: '#pragma clang fp eval_method' cannot be used when option 'mreassociate' is enabled. + // CHECK-RECPR: '#pragma clang fp eval_method' cannot be used when option 'freciprocal' is enabled. + // CHECK-PRGM: '#pragma clang fp eval_method' cannot be used when '#pragma clang fp reassociate' is enabled. +#pragma clang fp reassociate(on) + return (a * c) - (b * c); +}