https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/183002
>From 4d91aede8e582bf08b284a526dae119bb68a9d52 Mon Sep 17 00:00:00 2001 From: Sam Elliott <[email protected]> Date: Mon, 23 Feb 2026 22:35:43 -0800 Subject: [PATCH 1/5] [clang] Improve Ofast Warning It was not clear from the deprecation warning or from the documentation that Ofast has an effect on strict aliasing as well. This seeks to make the warning more accurate, for developers trying to move away from using Ofast --- clang/docs/CommandGuide/clang.rst | 15 ++++++++------- .../include/clang/Basic/DiagnosticDriverKinds.td | 4 ++-- clang/test/Driver/Ofast.c | 10 +++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index 4c1f8e47ce381..0563420121e9f 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -445,14 +445,15 @@ 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 + :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. + that :option:`-O3` in combination with :option:`-ffast-math` and + :option:`-fstrict-aliasing` 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:`-Os` Like :option:`-O2` with extra optimizations to reduce code size. diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 90a92b1602231..260d97b7a7ba6 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -490,8 +490,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 -ffast-math -fstrict-aliasing' " + "for the same behavior, or '-O3' to enable only conforming optimizations">, 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/test/Driver/Ofast.c b/clang/test/Driver/Ofast.c index e04ce036638f9..22e2c42f53b23 100644 --- a/clang/test/Driver/Ofast.c +++ b/clang/test/Driver/Ofast.c @@ -10,14 +10,14 @@ // 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 -// CHECK-OFAST: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST: use '-O3 -ffast-math -fstrict-aliasing' 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 -// Lack of warning about '-Ofast' deprecation is checked via -Werror +// Lack of warning about '-Ofast -fstrict-aliasing' deprecation is checked via -Werror // CHECK-OFAST-O2: -cc1 // CHECK-OFAST-O2-NOT: -Ofast // CHECK-OFAST-O2-ALIASING-NOT: -relaxed-aliasing @@ -25,21 +25,21 @@ // CHECK-OFAST-O2-NOT: -ffast-math // CHECK-OFAST-O2: -vectorize-loops -// 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: use '-O3 -ffast-math -fstrict-aliasing' 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 -// 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: use '-O3 -ffast-math -fstrict-aliasing' 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 -// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast-math -fstrict-aliasing' 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 >From a4763856c19d67983189c00063e3efff832724a5 Mon Sep 17 00:00:00 2001 From: Sam Elliott <[email protected]> Date: Mon, 23 Feb 2026 22:46:43 -0800 Subject: [PATCH 2/5] Fix docs build --- clang/docs/CommandGuide/clang.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index 0563420121e9f..c970bb1179747 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -449,7 +449,7 @@ Code Generation Options 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` and - :option:`-fstrict-aliasing` should be used instead if the request for + ``-fstrict-aliasing`` 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 >From 0e9f15746f4ab754125db22db5e57931b40fba06 Mon Sep 17 00:00:00 2001 From: Sam Elliott <[email protected]> Date: Thu, 26 Feb 2026 11:36:53 -0800 Subject: [PATCH 3/5] Update warning + docs --- clang/docs/CommandGuide/clang.rst | 12 ++++++------ clang/include/clang/Basic/DiagnosticDriverKinds.td | 5 +++-- clang/test/Driver/Ofast.c | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index c970bb1179747..ef175fdea1c09 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -448,12 +448,12 @@ Code Generation Options :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` and - ``-fstrict-aliasing`` 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. + that :option:`-O3` in combination with :option:`-ffast-math` (and + ``-fstrict-aliasing`` if your platform is not MSVC Windows or UEFI) 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:`-Os` Like :option:`-O2` with extra optimizations to reduce code size. diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 0b4c1fec7b288..30e758dacc409 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -490,8 +490,9 @@ 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 -fstrict-aliasing' " - "for the same behavior, or '-O3' to enable only conforming optimizations">, + "argument '-Ofast' is deprecated; use '-O3' to enable only conforming " + "optimizations, or see the clang docs for which flags to use to get 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/test/Driver/Ofast.c b/clang/test/Driver/Ofast.c index 22e2c42f53b23..1516344fd5282 100644 --- a/clang/test/Driver/Ofast.c +++ b/clang/test/Driver/Ofast.c @@ -10,7 +10,7 @@ // 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 -// CHECK-OFAST: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST: warning: argument '-Ofast' is deprecated; use '-O3' to enable only conforming optimizations // CHECK-OFAST: -cc1 // CHECK-OFAST: -Ofast // CHECK-OFAST-NOT: -relaxed-aliasing @@ -25,21 +25,21 @@ // CHECK-OFAST-O2-NOT: -ffast-math // CHECK-OFAST-O2: -vectorize-loops -// CHECK-OFAST-NO-FAST-MATH: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST-NO-FAST-MATH: warning: argument '-Ofast' is deprecated; use '-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 -// CHECK-OFAST-NO-STRICT-ALIASING: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST-NO-STRICT-ALIASING: warning: argument '-Ofast' is deprecated; use '-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 -// CHECK-OFAST-NO-VECTORIZE: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations +// CHECK-OFAST-NO-VECTORIZE: warning: argument '-Ofast' is deprecated; use '-O3' to enable only conforming optimizations // CHECK-OFAST-NO-VECTORIZE: -cc1 // CHECK-OFAST-NO-VECTORIZE: -Ofast // CHECK-OFAST-NO-VECTORIZE-NOT: -relaxed-aliasing >From bd537084f527528c8d6bb37968f0ced52cc4cc2f Mon Sep 17 00:00:00 2001 From: Sam Elliott <[email protected]> Date: Fri, 27 Feb 2026 11:36:23 -0800 Subject: [PATCH 4/5] Two separate warnings --- clang/docs/CommandGuide/clang.rst | 10 +- .../clang/Basic/DiagnosticDriverKinds.td | 9 +- clang/lib/Driver/ToolChains/Clang.cpp | 8 +- clang/test/Driver/Ofast.c | 99 ++++++++++--------- 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index ef175fdea1c09..3bd5139ceb002 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -449,11 +449,11 @@ Code Generation Options 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` (and - ``-fstrict-aliasing`` if your platform is not MSVC Windows or UEFI) 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. + ::ref```-fstrict-aliasing``<_strict_aliasing>` on platforms that are not + Windows or UEFI) 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:`-Os` Like :option:`-O2` with extra optimizations to reduce code size. diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index 30e758dacc409..a7ff56dedbba3 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -490,9 +490,12 @@ 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' to enable only conforming " - "optimizations, or see the clang docs for which flags to use to get the same " - "behavior">, + "argument '-Ofast' is deprecated; use '-O3 -ffast-math -fstrict-aliasing' " + "for the same behavior, or '-O3' to enable only conforming optimizations">, + InGroup<DeprecatedOFast>; +def warn_drv_deprecated_arg_ofast_windows : Warning< + "argument '-Ofast' is deprecated; use '-O3 -ffast-math' " + "for the same behavior, or '-O3' to enable only conforming optimizations">, 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 0aa93e2e46814..8a0cc3fc9834e 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5825,8 +5825,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_zero_initialized_in_bss); bool OFastEnabled = isOptimizationLevelFast(Args); - if (OFastEnabled) - D.Diag(diag::warn_drv_deprecated_arg_ofast); + if (OFastEnabled) { + if (IsWindowsMSVC || IsUEFI) + D.Diag(diag::warn_drv_deprecated_arg_ofast_windows); + else + 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. OptSpecifier StrictAliasingAliasOption = diff --git a/clang/test/Driver/Ofast.c b/clang/test/Driver/Ofast.c index 1516344fd5282..74a308b73409a 100644 --- a/clang/test/Driver/Ofast.c +++ b/clang/test/Driver/Ofast.c @@ -1,47 +1,56 @@ -// 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 - -// CHECK-OFAST: warning: argument '-Ofast' is deprecated; use '-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-unknown-linux-gnu -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN-NO-WIN %s +// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-NO-WIN %s + +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING-MSVC %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN-WIN %s +// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-WIN %s + + +// WARN-NO-WIN: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations +// WARN-WIN: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations + + +// OFAST: -cc1 +// OFAST: -Ofast +// OFAST-NOT: -relaxed-aliasing +// OFAST: -ffast-math +// OFAST: -vectorize-loops // Lack of warning about '-Ofast -fstrict-aliasing' 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 - -// CHECK-OFAST-NO-FAST-MATH: warning: argument '-Ofast' is deprecated; use '-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 - -// CHECK-OFAST-NO-STRICT-ALIASING: warning: argument '-Ofast' is deprecated; use '-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 - -// CHECK-OFAST-NO-VECTORIZE: warning: argument '-Ofast' is deprecated; use '-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 +// O2: -cc1 +// O2-NOT: -Ofast +// O2-ALIASING-NOT: -relaxed-aliasing +// O2-ALIASING-MSVC: -relaxed-aliasing +// O2-NOT: -ffast-math +// O2: -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 >From 288515c4ec2310ae3752242f3deaa5bc6b528da3 Mon Sep 17 00:00:00 2001 From: Sam Elliott <[email protected]> Date: Tue, 7 Apr 2026 18:33:18 -0700 Subject: [PATCH 5/5] Update docs, warning refers to docs --- clang/docs/CommandGuide/clang.rst | 18 +++++--- clang/docs/UsersManual.rst | 12 +++-- .../clang/Basic/DiagnosticDriverKinds.td | 8 +--- clang/lib/Driver/ToolChains/Clang.cpp | 8 +--- clang/test/Driver/Ofast.c | 46 +++++++++---------- 5 files changed, 44 insertions(+), 48 deletions(-) diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst index 3bd5139ceb002..6064abcd015a6 100644 --- a/clang/docs/CommandGuide/clang.rst +++ b/clang/docs/CommandGuide/clang.rst @@ -447,13 +447,17 @@ Code Generation Options :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` (and - ::ref```-fstrict-aliasing``<_strict_aliasing>` on platforms that are not - Windows or UEFI) 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. + 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 optimisation + 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 + optimisation 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..5812c685caa4e 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 different 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 118ec4b62e6b2..0ed07fdad4431 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -492,12 +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 -fstrict-aliasing' " - "for the same behavior, or '-O3' to enable only conforming optimizations">, - InGroup<DeprecatedOFast>; -def warn_drv_deprecated_arg_ofast_windows : 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 dc223931745b5..386459306f5da 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5888,12 +5888,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fno_zero_initialized_in_bss); bool OFastEnabled = isOptimizationLevelFast(Args); - if (OFastEnabled) { - if (IsWindowsMSVC || IsUEFI) - D.Diag(diag::warn_drv_deprecated_arg_ofast_windows); - else - D.Diag(diag::warn_drv_deprecated_arg_ofast); - } + 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. OptSpecifier StrictAliasingAliasOption = diff --git a/clang/test/Driver/Ofast.c b/clang/test/Driver/Ofast.c index 74a308b73409a..785291ff8fad8 100644 --- a/clang/test/Driver/Ofast.c +++ b/clang/test/Driver/Ofast.c @@ -1,27 +1,24 @@ -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN-NO-WIN %s -// RUN: %clang --target=x86_64-unknown-linux-gnu -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-NO-WIN %s - -// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -O2 -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -fno-fast-math -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -fno-strict-aliasing -Ofast -### %s 2>&1 | FileCheck -check-prefixes=OFAST,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -fno-vectorize -Ofast -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -O2 -### -Werror %s 2>&1 | FileCheck -check-prefixes=O2,O2-ALIASING-MSVC %s -// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-fast-math -### %s 2>&1 | FileCheck -check-prefixes=NO-FAST-MATH,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-strict-aliasing -### %s 2>&1 | FileCheck -check-prefixes=NO-STRICT-ALIASING,WARN-WIN %s -// RUN: %clang --target=x86_64-windows-msvc -c -Ofast -fno-vectorize -### %s 2>&1 | FileCheck -check-prefixes=NO-VECTORIZE,WARN-WIN %s - - -// WARN-NO-WIN: use '-O3 -ffast-math -fstrict-aliasing' for the same behavior, or '-O3' to enable only conforming optimizations -// WARN-WIN: use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations - +// 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 + +// 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 + +// WARN: use '-O3' to enable only conforming optimizations, or consult the documentation for the same behavior // OFAST: -cc1 // OFAST: -Ofast @@ -29,7 +26,6 @@ // OFAST: -ffast-math // OFAST: -vectorize-loops -// Lack of warning about '-Ofast -fstrict-aliasing' deprecation is checked via -Werror // O2: -cc1 // O2-NOT: -Ofast // O2-ALIASING-NOT: -relaxed-aliasing _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
