Skip to content

Commit 71ab44a

Browse files
wolfy1961SLTozer
andcommitted
[Clang] Add "extend lifetime" flags and release note (#110000)
Following the commit that added the fake use intrinsic to LLVM, this patch adds a pair of flags for the clang frontend that emit fake use intrinsics, for the purpose of extending the lifetime of variables (either all source variables, or just the `this` pointer). This patch does not implement the fake use intrinsic emission of the flags themselves, it simply adds the flags, the corresponding release note, and the attachment of the `has_fake_uses` attribute to affected functions; the remaining functionality appears in the next patch. Co-authored-by: Stephen Tozer <[email protected]>
1 parent 8fe7860 commit 71ab44a

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,20 @@ New Compiler Flags
519519
only for thread-local variables, and none (which corresponds to the
520520
existing ``-fno-c++-static-destructors`` flag) skips all static
521521
destructors registration.
522+
- The ``-fextend-variable-liveness`` flag has been added to allow for improved
523+
debugging of optimized code. Using ``-fextend-variable-liveness`` will cause
524+
Clang to generate code that tries to preserve the liveness of source variables
525+
through optimizations, meaning that variables will typically be visible in a
526+
debugger more often. The flag has two levels: ``-fextend-variable-liveness``,
527+
or ``-fextend-variable-liveness=all``, extendes the liveness of all user
528+
variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or
529+
``-fextend-variable-liveness=this``, has the same behaviour but applies only
530+
to the ``this`` variable in C++ class member functions, meaning its effect is
531+
a strict subset of ``-fextend-variable-liveness``. Note that this flag
532+
modifies the results of optimizations that Clang performs, which will result
533+
in reduced performance in generated code; however, this feature will not
534+
extend the liveness of some variables in cases where doing so would likely
535+
have a severe impact on generated code performance.
522536

523537
- The ``-Warray-compare`` warning has been added to warn about array comparison
524538
on versions older than C++20.

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ CODEGENOPT(EnableTLSDESC, 1, 0)
393393
/// Bit size of immediate TLS offsets (0 == use the default).
394394
VALUE_CODEGENOPT(TLSSize, 8, 0)
395395

396+
/// The types of variables that we will extend the live ranges of.
397+
ENUM_CODEGENOPT(ExtendVariableLiveness, ExtendVariableLivenessKind, 2, ExtendVariableLivenessKind::None)
398+
396399
/// The default stack protector guard offset to use.
397400
VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX)
398401

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class CodeGenOptions : public CodeGenOptionsBase {
9595
Embed_Marker // Embed a marker as a placeholder for bitcode.
9696
};
9797

98+
enum class ExtendVariableLivenessKind {
99+
None,
100+
This,
101+
All,
102+
};
103+
98104
enum InlineAsmDialectKind {
99105
IAD_ATT,
100106
IAD_Intel,

clang/include/clang/Driver/Options.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4349,6 +4349,26 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">,
43494349
Visibility<[CC1Option]>,
43504350
HelpText<"Filename (or -) to write stack usage output to">,
43514351
MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>;
4352+
def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">,
4353+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
4354+
HelpText<"Extend the liveness of user variables through optimizations to "
4355+
"prevent stale or optimized-out variable values when debugging. Can "
4356+
"be applied to all user variables, or just to the C++ 'this' ptr. "
4357+
"May choose not to extend the liveness of some variables, such as "
4358+
"non-scalars larger than 4 unsigned ints, or variables in any "
4359+
"inlined functions.">,
4360+
Values<"all,this,none">,
4361+
NormalizedValues<["All", "This", "None"]>,
4362+
NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">,
4363+
MarshallingInfoEnum<CodeGenOpts<"ExtendVariableLiveness">, "None">;
4364+
def fextend_this_ptr_liveness : Flag<["-"], "fextend-this-ptr-liveness">,
4365+
Visibility<[ClangOption, CC1Option]>,
4366+
Alias<fextend_variable_liveness_EQ>, AliasArgs<["this"]>,
4367+
HelpText<"Alias for -fextend-variable-liveness=this.">;
4368+
def fextend_variable_liveness : Flag<["-"], "fextend-variable-liveness">,
4369+
Visibility<[ClangOption, CC1Option]>,
4370+
Alias<fextend_variable_liveness_EQ>, AliasArgs<["all"]>,
4371+
HelpText<"Alias for -fextend-variable-liveness=all.">;
43524372

43534373
defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names",
43544374
CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7706,6 +7706,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
77067706
if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
77077707
CmdArgs.push_back("-fretain-comments-from-system-headers");
77087708

7709+
Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ);
7710+
77097711
// Forward -fcomment-block-commands to -cc1.
77107712
Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
77117713
// Forward -fparse-all-comments to -cc1.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Tests that -fextend-variable-liveness and its aliases are correctly passed
2+
// by the driver.
3+
4+
// RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT
5+
// RUN: %clang -fextend-variable-liveness=none -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,NONE
6+
// RUN: %clang -fextend-variable-liveness=this -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,THIS
7+
// RUN: %clang -fextend-this-ptr-liveness -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,THIS
8+
// RUN: %clang -fextend-variable-liveness=all -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
9+
// RUN: %clang -fextend-variable-liveness -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL
10+
11+
// CHECK: "-cc1"
12+
// DEFAULT-NOT: -fextend-variable-liveness
13+
// NONE-SAME: "-fextend-variable-liveness=none"
14+
// THIS-SAME: "-fextend-variable-liveness=this"
15+
// ALL-SAME: "-fextend-variable-liveness=all"

0 commit comments

Comments
 (0)