Author: Sam Elliott Date: 2026-04-10T13:11:10-07:00 New Revision: 5b5a2ad6d29e86cf0fd8a6cf9657505c2a1ad5a4
URL: https://github.com/llvm/llvm-project/commit/5b5a2ad6d29e86cf0fd8a6cf9657505c2a1ad5a4 DIFF: https://github.com/llvm/llvm-project/commit/5b5a2ad6d29e86cf0fd8a6cf9657505c2a1ad5a4.diff LOG: [clang] Improve Ofast Warning (#183002) `-Ofast` has an effect on the defaults for `-ffast-math` (documented before this patch), and `-fstrict-aliasing` (not documented before this patch). On some platforms, `-Ofast` cannot be replaced with `-O3 -ffast-math`, because the strict aliasing default would change. `-Ofast` can only be replaced (in the exact same position) with `-O3 -ffast-math -fstrict-aliasing` if `-Ofast` is the effective optimization level (i.e., it is not followed by another `-O<value>` flag). Otherwise, the `-Ofast` flag should just be deleted as it is having no effect. This is all too difficult to summarise in a warning message, so this PR mostly updates the docs. We keep the message about "use `-O3` to get conforming optimizations" in the hope this encourages people to adopt `-O3` alone. The warning message is now emitted any time there is `-Ofast` in the command-line string, rather than only when `-Ofast` is the effective optimization level. Added: Modified: clang/docs/CommandGuide/clang.rst clang/docs/UsersManual.rst clang/include/clang/Basic/DiagnosticDriverKinds.td clang/lib/Driver/ToolChains/Clang.cpp clang/test/Driver/Ofast.c Removed: ################################################################################ diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index 4c1f8e47ce381..d8c70bdc43b18 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -445,14 +445,19 @@ Code Generation Options take longer to perform or that may generate larger code (in an attempt to make the program run faster). - :option:`-Ofast` Enables all the optimizations from :option:`-O3` along - with other aggressive optimizations that may violate strict compliance with - language standards. This is deprecated in Clang 19 and a warning is emitted - that :option:`-O3` in combination with :option:`-ffast-math` should be used - instead if the request for non-standard math behavior is intended. There - is no timeline yet for removal; the aim is to discourage use of - :option:`-Ofast` due to the surprising behavior of an optimization flag - changing the observable behavior of correct code. + :option:`-Ofast` Enables all the optimizations from :option:`-O3` along with + other aggressive optimizations that may violate strict compliance with + language standards. This has been deprecated since Clang 19. There is no + timeline yet for removal; the aim is to discourage use of :option:`-Ofast` + due to the surprising behavior of an optimization flag changing the + observable behavior of correct code. + + If :option:`-Ofast` has been specified and is the effective optimization + level (i.e. there is no later `-O` option specified), then the option can be + replaced in the option string with `-O3 -ffast-math -fstrict-aliasing`. + (:option:`-fstrict-aliasing` is the default on non-Windows, non-UEFI + platforms). If :option:`-Ofast` has been specified but is not the effective + optimization level, then it can be removed or replaced with :option:`-O3`. :option:`-Os` Like :option:`-O2` with extra optimizations to reduce code size. diff --git a/clang/docs/UsersManual.rst b/clang/docs/UsersManual.rst index 41f9f2896bae1..cb3042d437e97 100644 --- a/clang/docs/UsersManual.rst +++ b/clang/docs/UsersManual.rst @@ -2749,9 +2749,13 @@ violates the strict aliasing rules. For example: if Clang is able to inline this function. */ } -Strict aliasing can be explicitly enabled with ``-fstrict-aliasing`` and -disabled with ``-fno-strict-aliasing``. ``clang-cl`` defaults to -``-fno-strict-aliasing``. Otherwise, Clang defaults to ``-fstrict-aliasing``. +.. option:: -fstrict-aliasing, -fno-strict-aliasing + +Strict aliasing can be explicitly enabled with :option:`-fstrict-aliasing` and +disabled with :option:`-fno-strict-aliasing`. Windows MSVC platforms and UEFI +platforms default to :option:`-fno-strict-aliasing`. Otherwise, Clang defaults +to :option:`-fstrict-aliasing`. These options may also be affected by +:option:`-Ofast`. C and C++ specify slightly diff erent rules for strict aliasing. To improve language interoperability, Clang allows two types to alias if either language @@ -2769,7 +2773,7 @@ works in one version of Clang may not work in another because of changes to the optimizer. Clang provides a :doc:`TypeSanitizer` to help detect violations of the strict aliasing rules, but it is currently still experimental. Code that is known to violate strict aliasing should generally be built with -``-fno-strict-aliasing`` if the violation cannot be fixed. +:option:`-fno-strict-aliasing` if the violation cannot be fixed. Clang supports several ways to fix a violation of strict aliasing: diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index ed6a9107002af..0ed07fdad4431 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -492,8 +492,8 @@ def warn_drv_clang_unsupported : Warning< def warn_drv_deprecated_arg : Warning< "argument '%0' is deprecated%select{|, use '%2' instead}1">, InGroup<Deprecated>; def warn_drv_deprecated_arg_ofast : Warning< - "argument '-Ofast' is deprecated; use '-O3 -ffast-math' for the same behavior," - " or '-O3' to enable only conforming optimizations">, + "argument '-Ofast' is deprecated; use '-O3' to enable only conforming optimizations, " + "or consult the documentation for the same behavior">, InGroup<DeprecatedOFast>; def warn_drv_deprecated_arg_ofast_for_flang : Warning< "argument '-Ofast' is deprecated; use '-O3 -ffast-math -fstack-arrays -fno-protect-parens' for the same behavior," diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 701c344ac047a..f685abe9dad35 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5902,7 +5902,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_zero_initialized_in_bss); bool OFastEnabled = isOptimizationLevelFast(Args); - if (OFastEnabled) + if (Args.hasArg(options::OPT_Ofast)) D.Diag(diag::warn_drv_deprecated_arg_ofast); // If -Ofast is the optimization level, then -fstrict-aliasing should be // enabled. This alias option is being used to simplify the hasFlag logic. diff --git a/clang/test/Driver/Ofast.c b/clang/test/Driver/Ofast.c index e04ce036638f9..785291ff8fad8 100644 --- a/clang/test/Driver/Ofast.c +++ b/clang/test/Driver/Ofast.c @@ -1,47 +1,52 @@ -// RUN: %clang -c -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s -// RUN: %clang -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s -// RUN: %clang -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s -// RUN: %clang -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST %s -// RUN: %clang -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-VECTORIZE %s -// RUN: %clang -c -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-O2 \ -// RUN: %if target={{.*-windows-msvc.*}} %{ --check-prefix=CHECK-OFAST-O2-ALIASING-MSVC %} \ -// RUN: %else %{ --check-prefix=CHECK-OFAST-O2-ALIASING %} %s -// RUN: %clang -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-FAST-MATH %s -// RUN: %clang -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-STRICT-ALIASING %s -// RUN: %clang -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefix=CHECK-OFAST-NO-VECTORIZE %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -O2 -### %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN %s -// CHECK-OFAST: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations -// CHECK-OFAST: -cc1 -// CHECK-OFAST: -Ofast -// CHECK-OFAST-NOT: -relaxed-aliasing -// CHECK-OFAST: -ffast-math -// CHECK-OFAST: -vectorize-loops +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -O2 -### %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING-MSVC,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN %s -// Lack of warning about '-Ofast' deprecation is checked via -Werror -// CHECK-OFAST-O2: -cc1 -// CHECK-OFAST-O2-NOT: -Ofast -// CHECK-OFAST-O2-ALIASING-NOT: -relaxed-aliasing -// CHECK-OFAST-O2-ALIASING-MSVC: -relaxed-aliasing -// CHECK-OFAST-O2-NOT: -ffast-math -// CHECK-OFAST-O2: -vectorize-loops +// WARN: use '-O3' to enable only conforming optimizations, or consult the documentation for the same behavior -// CHECK-OFAST-NO-FAST-MATH: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations -// CHECK-OFAST-NO-FAST-MATH: -cc1 -// CHECK-OFAST-NO-FAST-MATH: -Ofast -// CHECK-OFAST-NO-FAST-MATH-NOT: -relaxed-aliasing -// CHECK-OFAST-NO-FAST-MATH-NOT: -ffast-math -// CHECK-OFAST-NO-FAST-MATH: -vectorize-loops +// OFAST: -cc1 +// OFAST: -Ofast +// OFAST-NOT: -relaxed-aliasing +// OFAST: -ffast-math +// OFAST: -vectorize-loops -// CHECK-OFAST-NO-STRICT-ALIASING: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations -// CHECK-OFAST-NO-STRICT-ALIASING: -cc1 -// CHECK-OFAST-NO-STRICT-ALIASING: -Ofast -// CHECK-OFAST-NO-STRICT-ALIASING: -relaxed-aliasing -// CHECK-OFAST-NO-STRICT-ALIASING: -ffast-math -// CHECK-OFAST-NO-STRICT-ALIASING: -vectorize-loops +// O2: -cc1 +// O2-NOT: -Ofast +// O2-ALIASING-NOT: -relaxed-aliasing +// O2-ALIASING-MSVC: -relaxed-aliasing +// O2-NOT: -ffast-math +// O2: -vectorize-loops -// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations -// CHECK-OFAST-NO-VECTORIZE: -cc1 -// CHECK-OFAST-NO-VECTORIZE: -Ofast -// CHECK-OFAST-NO-VECTORIZE-NOT: -relaxed-aliasing -// CHECK-OFAST-NO-VECTORIZE: -ffast-math -// CHECK-OFAST-NO-VECTORIZE-NOT: -vectorize-loops +// NO-FAST-MATH: -cc1 +// NO-FAST-MATH: -Ofast +// NO-FAST-MATH-NOT: -relaxed-aliasing +// NO-FAST-MATH-NOT: -ffast-math +// NO-FAST-MATH: -vectorize-loops + +// NO-STRICT-ALIASING: -cc1 +// NO-STRICT-ALIASING: -Ofast +// NO-STRICT-ALIASING: -relaxed-aliasing +// NO-STRICT-ALIASING: -ffast-math +// NO-STRICT-ALIASING: -vectorize-loops + +// NO-VECTORIZE: -cc1 +// NO-VECTORIZE: -Ofast +// NO-VECTORIZE-NOT: -relaxed-aliasing +// NO-VECTORIZE: -ffast-math +// NO-VECTORIZE-NOT: -vectorize-loops _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
