[clang] [Clang] Enable -fextend-lifetimes at -Og (PR #118026)
Stephen Tozer via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 07:15:26 PDT 2025
https://github.com/SLTozer updated https://github.com/llvm/llvm-project/pull/118026
>From 351971bbff4e77b0f36cd92cd1a881584d17a9e7 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Thu, 28 Nov 2024 13:53:20 +0000
Subject: [PATCH 1/2] Enable -fextend-lifetimes at -Og
---
clang/docs/CommandGuide/clang.rst | 7 +++++--
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/Driver/ToolChains/Clang.cpp | 8 +++++++-
clang/test/Driver/extend-variable-liveness.c | 3 ++-
4 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst
index 42aac7b25d93c..68e9e07ed0005 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -443,8 +443,11 @@ Code Generation Options
:option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
size further.
- :option:`-Og` Like :option:`-O1`. In future versions, this option might
- disable different optimizations in order to improve debuggability.
+ :option:`-Og` Similar to :option:`-O1`, but with slightly reduced
+ optimization and better variable visibility. The same optimizations are run
+ as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+ also set, which tries to prevent optimizations from reducing the liveness of
+ user variables, improving their availability when debugging.
:option:`-O` Equivalent to :option:`-O1`.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4f640697e1817..23a3d481e66c2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,6 +219,10 @@ Modified Compiler Flags
- `-Wpadded` option implemented for the `x86_64-windows-msvc` target. Fixes #61702
+- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``, a new
+ compiler flag which trades a small amount of optimization in exchange for
+ improved variable visibility.
+
Removed Compiler Flags
-------------------------
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 8506a5c00e7bc..b2dd4b3b54869 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7681,7 +7681,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
CmdArgs.push_back("-fretain-comments-from-system-headers");
- Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
+ if (Arg *A = Args.getLastArg(options::OPT_fextend_variable_liveness_EQ)) {
+ A->render(Args, CmdArgs);
+ } else if (Arg *A = Args.getLastArg(options::OPT_O_Group);
+ A && A->containsValue("g")) {
+ // Set -fextend-variable-liveness=all by default at -Og.
+ CmdArgs.push_back("-fextend-variable-liveness=all");
+ }
// Forward -fcomment-block-commands to -cc1.
Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
diff --git a/clang/test/Driver/extend-variable-liveness.c b/clang/test/Driver/extend-variable-liveness.c
index bbfb2ece6f297..99a5409ceccea 100644
--- a/clang/test/Driver/extend-variable-liveness.c
+++ b/clang/test/Driver/extend-variable-liveness.c
@@ -1,7 +1,8 @@
// Tests that -fextend-variable-liveness and its aliases are correctly passed
-// by the driver.
+// by the driver, and are set by default at -Og.
// RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
+// RUN: %clang -### -Og -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
// RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NONE
// RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,THIS
// RUN: %clang -fextend-variable-liveness=all -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
>From e5037472fd98a7f4a2d9f8298799f46a8b647f87 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Thu, 17 Apr 2025 15:14:55 +0100
Subject: [PATCH 2/2] Update docs to fit current compiler/doc state
---
clang/docs/CommandGuide/clang.rst | 2 +-
clang/docs/ReleaseNotes.rst | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/docs/CommandGuide/clang.rst b/clang/docs/CommandGuide/clang.rst
index 68e9e07ed0005..a1e738f1afec8 100644
--- a/clang/docs/CommandGuide/clang.rst
+++ b/clang/docs/CommandGuide/clang.rst
@@ -445,7 +445,7 @@ Code Generation Options
:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
optimization and better variable visibility. The same optimizations are run
- as at :option:`-O1`, but the :option:`-fextend-variable-liveness` flag is
+ as at :option:`-O1`, but the ``-fextend-variable-liveness`` flag is
also set, which tries to prevent optimizations from reducing the liveness of
user variables, improving their availability when debugging.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 23a3d481e66c2..9a533410f1f3a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -219,9 +219,9 @@ Modified Compiler Flags
- `-Wpadded` option implemented for the `x86_64-windows-msvc` target. Fixes #61702
-- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``, a new
- compiler flag which trades a small amount of optimization in exchange for
- improved variable visibility.
+- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``,
+ reducing performance slightly while reducing the number of optimized-out
+ variables.
Removed Compiler Flags
-------------------------
More information about the cfe-commits
mailing list