Skip to content

Commit a9dff35

Browse files
authored
[Clang] Enable -fextend-lifetimes at -Og (#118026)
Recently, a new flag -fextend-variable-liveness was added that prevents optimizations from removing the values of source variables in some cases, improving the quality of debugging for optimized builds where it is enabled. Following the inclusion of the flag, this patch enables it by default when `-Og` is set. Currently, `-Og` is equivalent to `-O1` - it is effectively just an alias. By enabling `-fextend-lifetimes`, this patch changes the code generated by Clang with `-Og` to have reduced optimization and greater debuggability than `-O1`, differentiating the two according to their respective purposes. This idea was discussed previously on Discourse where there was general agreement with the principle of this change: https://discourse.llvm.org/t/rfc-redefine-og-o1-and-add-a-new-level-of-og
1 parent 70e2acf commit a9dff35

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

clang/docs/CommandGuide/clang.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,11 @@ Code Generation Options
443443
:option:`-Oz` Like :option:`-Os` (and thus :option:`-O2`), but reduces code
444444
size further.
445445

446-
:option:`-Og` Like :option:`-O1`. In future versions, this option might
447-
disable different optimizations in order to improve debuggability.
446+
:option:`-Og` Similar to :option:`-O1`, but with slightly reduced
447+
optimization and better variable visibility. The same optimizations are run
448+
as at :option:`-O1`, but the ``-fextend-variable-liveness`` flag is
449+
also set, which tries to prevent optimizations from reducing the liveness of
450+
user variables, improving their availability when debugging.
448451

449452
:option:`-O` Equivalent to :option:`-O1`.
450453

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ Modified Compiler Flags
221221

222222
- The ``-mexecute-only`` and ``-mpure-code`` flags are now accepted for AArch64 targets. (#GH125688)
223223

224+
- The ``-Og`` optimization flag now sets ``-fextend-variable-liveness``,
225+
reducing performance slightly while reducing the number of optimized-out
226+
variables.
227+
224228
Removed Compiler Flags
225229
-------------------------
226230

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7681,7 +7681,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
76817681
if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
76827682
CmdArgs.push_back("-fretain-comments-from-system-headers");
76837683

7684-
Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
7684+
if (Arg *A = Args.getLastArg(options::OPT_fextend_variable_liveness_EQ)) {
7685+
A->render(Args, CmdArgs);
7686+
} else if (Arg *A = Args.getLastArg(options::OPT_O_Group);
7687+
A && A->containsValue("g")) {
7688+
// Set -fextend-variable-liveness=all by default at -Og.
7689+
CmdArgs.push_back("-fextend-variable-liveness=all");
7690+
}
76857691

76867692
// Forward -fcomment-block-commands to -cc1.
76877693
Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);

clang/test/Driver/extend-variable-liveness.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Tests that -fextend-variable-liveness and its aliases are correctly passed
2-
// by the driver.
2+
// by the driver, and are set by default at -Og.
33

44
// RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
5+
// RUN: %clang -### -Og -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
56
// RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NONE
67
// RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,THIS
78
// RUN: %clang -fextend-variable-liveness=all -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL

0 commit comments

Comments
 (0)