Skip to content

Commit 6a873f5

Browse files
committed
[Clang] Add "extend lifetime" flags and release note
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.
1 parent c5cd1e9 commit 6a873f5

File tree

7 files changed

+51
-0
lines changed

7 files changed

+51
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ New Compiler Flags
412412
only for thread-local variables, and none (which corresponds to the
413413
existing ``-fno-c++-static-destructors`` flag) skips all static
414414
destructors registration.
415+
- The ``-fextend-lifetimes`` and ``-fextend-this-ptr`` flags have been added to
416+
allow for improved debugging of optimized code. Using ``-fextend-lifetimes``
417+
will cause Clang to generate code that tries to preserve the lifetimes of
418+
source variables, meaning that variables will typically be visible in a
419+
debugger more often. The ``-fextend-this-ptr`` flag has the same behaviour,
420+
but applies only to the ``this`` variable in C++ class member functions. Note
421+
that this flag modifies the optimizations that Clang performs, which will
422+
result in reduced performance in generated code; however, this feature will
423+
not extend the lifetime of some variables in cases where doing so would have
424+
too severe an impact on generated code performance.
415425

416426
Deprecated Compiler Flags
417427
-------------------------

clang/include/clang/Basic/CodeGenOptions.def

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

396+
/// Whether to extend the live range of the `this` pointer.
397+
CODEGENOPT(ExtendThisPtr, 1, 0)
398+
399+
/// Whether to extend the live ranges of all local variables.
400+
CODEGENOPT(ExtendLifetimes, 1, 0)
401+
396402
/// The default stack protector guard offset to use.
397403
VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX)
398404

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4298,6 +4298,15 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">,
42984298
Visibility<[CC1Option]>,
42994299
HelpText<"Filename (or -) to write stack usage output to">,
43004300
MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>;
4301+
def fextend_this_ptr : Flag <["-"], "fextend-this-ptr">, Group<f_Group>,
4302+
MarshallingInfoFlag<CodeGenOpts<"ExtendThisPtr">>,
4303+
HelpText<"Extend the lifetime of the 'this' pointer to improve visibility "
4304+
"in optimized debugging">, Visibility<[ClangOption, CC1Option]>;
4305+
def fextend_lifetimes : Flag <["-"], "fextend-lifetimes">, Group<f_Group>,
4306+
MarshallingInfoFlag<CodeGenOpts<"ExtendLifetimes">>,
4307+
HelpText<"Extend the lifetimes of local variables and parameters to improve "
4308+
"visibility in optimized debugging">,
4309+
Visibility<[ClangOption, CC1Option]>;
43014310

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

clang/lib/CodeGen/CGCall.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,10 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
25672567
if (shouldDisableTailCalls())
25682568
FuncAttrs.addAttribute("disable-tail-calls", "true");
25692569

2570+
// Mark the function as having fake uses when -fextend-lifetimes is on.
2571+
if (CodeGenOpts.ExtendLifetimes || CodeGenOpts.ExtendThisPtr)
2572+
FuncAttrs.addAttribute(llvm::Attribute::HasFakeUses);
2573+
25702574
// CPU/feature overrides. addDefaultFunctionDefinitionAttributes
25712575
// handles these separately to set them based on the global defaults.
25722576
GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7654,6 +7654,11 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
76547654
if (Args.hasArg(options::OPT_fretain_comments_from_system_headers))
76557655
CmdArgs.push_back("-fretain-comments-from-system-headers");
76567656

7657+
if (Args.hasArg(options::OPT_fextend_this_ptr))
7658+
CmdArgs.push_back("-fextend-this-ptr");
7659+
if (Args.hasArg(options::OPT_fextend_lifetimes))
7660+
CmdArgs.push_back("-fextend-lifetimes");
7661+
76577662
// Forward -fcomment-block-commands to -cc1.
76587663
Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
76597664
// Forward -fparse-all-comments to -cc1.

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,6 +2245,11 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
22452245
Args.getAllArgValues(OPT_fsanitize_trap_EQ), Diags,
22462246
Opts.SanitizeTrap);
22472247

2248+
Opts.ExtendThisPtr =
2249+
Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_this_ptr);
2250+
Opts.ExtendLifetimes =
2251+
Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_lifetimes);
2252+
22482253
Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true);
22492254

22502255
if (!LangOpts->CUDAIsDevice)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 %s -emit-llvm -O2 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O2 %s
2+
// RUN: %clang_cc1 %s -emit-llvm -O0 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O0 %s
3+
4+
// Checks that we emit the function attribute has_fake_uses when
5+
// -fextend-lifetimes is on and optimizations are enabled, and that it does not
6+
// when optimizations are disabled.
7+
8+
// CHECK-ALL: define {{.*}}void @foo
9+
// CHECK-O2: attributes #0 = {{{.*}}has_fake_uses
10+
// CHECK-O0-NOT: attributes #0 = {{{.*}}has_fake_uses
11+
12+
void foo() {}

0 commit comments

Comments
 (0)