Skip to content

Commit 2ef7cbf

Browse files
authored
[clang] Add deprecation warning for -Ofast driver option (#98736)
This patch implements consensus on the corresponding RFC documented here: https://discourse.llvm.org/t/rfc-deprecate-ofast/78687/72 Specifically, I added a deprecation warning for `-Ofast`, that suggests to use `-O3` or `-O3` with `-ffast-math`, and a new diagnostic group for aforementioned warning. Deprecation period is going to be lengthy, so I hope this PR can be merged in time for Clang 19.
1 parent 09cbb45 commit 2ef7cbf

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

clang/docs/CommandGuide/clang.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ Code Generation Options
429429

430430
:option:`-Ofast` Enables all the optimizations from :option:`-O3` along
431431
with other aggressive optimizations that may violate strict compliance with
432-
language standards.
432+
language standards. This is deprecated in favor of :option:`-O3`
433+
in combination with :option:`-ffast-math`.
433434

434435
:option:`-Os` Like :option:`-O2` with extra optimizations to reduce code
435436
size.

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ New Compiler Flags
473473
Deprecated Compiler Flags
474474
-------------------------
475475

476+
- The ``-Ofast`` command-line option has been deprecated. This option both
477+
enables the ``-O3`` optimization-level, as well as enabling non-standard
478+
``-ffast-math`` behaviors. As such, it is somewhat misleading as an
479+
"optimization level". Users are advised to switch to ``-O3 -ffast-math`` if
480+
the use of non-standard math behavior is intended, and ``-O3`` otherwise.
481+
See `RFC <https://discourse.llvm.org/t/rfc-deprecate-ofast/78687>`_ for details.
482+
476483
Modified Compiler Flags
477484
-----------------------
478485
- Added a new diagnostic flag ``-Wreturn-mismatch`` which is grouped under

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ def warn_drv_deprecated_arg : Warning<
442442
def warn_drv_deprecated_arg_no_relaxed_template_template_args : Warning<
443443
"argument '-fno-relaxed-template-template-args' is deprecated">,
444444
InGroup<DeprecatedNoRelaxedTemplateTemplateArgs>;
445+
def warn_drv_deprecated_arg_ofast : Warning<
446+
"argument '-Ofast' is deprecated; use '-O3 -ffast math' for the same behavior,"
447+
" or '-O3' to enable only conforming optimizations">,
448+
InGroup<DeprecatedOFast>;
445449
def warn_drv_deprecated_custom : Warning<
446450
"argument '%0' is deprecated, %1">, InGroup<Deprecated>;
447451
def warn_drv_assuming_mfloat_abi_is : Warning<

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def EnumConversion : DiagGroup<"enum-conversion",
103103
EnumFloatConversion,
104104
EnumCompareConditional]>;
105105
def DeprecatedNoRelaxedTemplateTemplateArgs : DiagGroup<"deprecated-no-relaxed-template-template-args">;
106+
def DeprecatedOFast : DiagGroup<"deprecated-ofast">;
106107
def ObjCSignedCharBoolImplicitIntConversion :
107108
DiagGroup<"objc-signed-char-bool-implicit-int-conversion">;
108109
def Shorten64To32 : DiagGroup<"shorten-64-to-32">;
@@ -228,6 +229,7 @@ def Deprecated : DiagGroup<"deprecated", [DeprecatedAnonEnumEnumConversion,
228229
DeprecatedPragma,
229230
DeprecatedRegister,
230231
DeprecatedNoRelaxedTemplateTemplateArgs,
232+
DeprecatedOFast,
231233
DeprecatedThisCapture,
232234
DeprecatedType,
233235
DeprecatedVolatile,

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ def O : Joined<["-"], "O">, Group<O_Group>,
931931
def O_flag : Flag<["-"], "O">, Visibility<[ClangOption, CC1Option, FC1Option]>,
932932
Alias<O>, AliasArgs<["1"]>;
933933
def Ofast : Joined<["-"], "Ofast">, Group<O_Group>,
934-
Visibility<[ClangOption, CC1Option, FlangOption]>;
934+
Visibility<[ClangOption, CC1Option, FlangOption]>,
935+
HelpText<"Deprecated; use '-O3 -ffast math' for the same behavior,"
936+
" or '-O3' to enable only conforming optimizations">;
935937
def P : Flag<["-"], "P">,
936938
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
937939
Group<Preprocessor_Group>,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5725,6 +5725,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
57255725
options::OPT_fno_zero_initialized_in_bss);
57265726

57275727
bool OFastEnabled = isOptimizationLevelFast(Args);
5728+
if (OFastEnabled)
5729+
D.Diag(diag::warn_drv_deprecated_arg_ofast);
57285730
// If -Ofast is the optimization level, then -fstrict-aliasing should be
57295731
// enabled. This alias option is being used to simplify the hasFlag logic.
57305732
OptSpecifier StrictAliasingAliasOption =

clang/test/Driver/Ofast.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,43 @@
33
// RUN: %clang -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
44
// RUN: %clang -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
55
// RUN: %clang -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s
6-
// RUN: %clang -Ofast -O2 -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-O2 \
6+
// RUN: %clang -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-O2 \
77
// RUN: %if target={{.*-windows-msvc.*}} %{ --check-prefix=CHECK-OFAST-O2-ALIASING-MSVC %} \
88
// RUN: %else %{ --check-prefix=CHECK-OFAST-O2-ALIASING %} %s
99
// RUN: %clang -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-FAST-MATH %s
1010
// RUN: %clang -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-STRICT-ALIASING %s
1111
// RUN: %clang -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-VECTORIZE %s
1212

13+
// CHECK-OFAST: use '-O3 -ffast math' for the same behavior, or '-O3' to enable only conforming optimizations
1314
// CHECK-OFAST: -cc1
1415
// CHECK-OFAST-NOT: -relaxed-aliasing
1516
// CHECK-OFAST: -ffast-math
1617
// CHECK-OFAST: -Ofast
1718
// CHECK-OFAST: -vectorize-loops
1819

20+
// Lack of warning about '-Ofast' deprecation is checked via -Werror
1921
// CHECK-OFAST-O2: -cc1
2022
// CHECK-OFAST-O2-ALIASING-NOT: -relaxed-aliasing
2123
// CHECK-OFAST-O2-ALIASING-MSVC: -relaxed-aliasing
2224
// CHECK-OFAST-O2-NOT: -ffast-math
2325
// CHECK-OFAST-O2-NOT: -Ofast
2426
// CHECK-OFAST-O2: -vectorize-loops
2527

28+
// CHECK-OFAST-NO-FAST-MATH: use '-O3 -ffast math' for the same behavior, or '-O3' to enable only conforming optimizations
2629
// CHECK-OFAST-NO-FAST-MATH: -cc1
2730
// CHECK-OFAST-NO-FAST-MATH-NOT: -relaxed-aliasing
2831
// CHECK-OFAST-NO-FAST-MATH-NOT: -ffast-math
2932
// CHECK-OFAST-NO-FAST-MATH: -Ofast
3033
// CHECK-OFAST-NO-FAST-MATH: -vectorize-loops
3134

35+
// CHECK-OFAST-NO-STRICT-ALIASING: use '-O3 -ffast math' for the same behavior, or '-O3' to enable only conforming optimizations
3236
// CHECK-OFAST-NO-STRICT-ALIASING: -cc1
3337
// CHECK-OFAST-NO-STRICT-ALIASING: -relaxed-aliasing
3438
// CHECK-OFAST-NO-STRICT-ALIASING: -ffast-math
3539
// CHECK-OFAST-NO-STRICT-ALIASING: -Ofast
3640
// CHECK-OFAST-NO-STRICT-ALIASING: -vectorize-loops
3741

42+
// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast math' for the same behavior, or '-O3' to enable only conforming optimizations
3843
// CHECK-OFAST-NO-VECTORIZE: -cc1
3944
// CHECK-OFAST-NO-VECTORIZE-NOT: -relaxed-aliasing
4045
// CHECK-OFAST-NO-VECTORIZE: -ffast-math

0 commit comments

Comments
 (0)