https://github.com/SLTozer updated https://github.com/llvm/llvm-project/pull/110000
>From 6a873f5c487a936344f6cd226b7d525b406f34b2 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Wed, 25 Sep 2024 15:08:39 +0100 Subject: [PATCH 1/9] [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. --- clang/docs/ReleaseNotes.rst | 10 ++++++++++ clang/include/clang/Basic/CodeGenOptions.def | 6 ++++++ clang/include/clang/Driver/Options.td | 9 +++++++++ clang/lib/CodeGen/CGCall.cpp | 4 ++++ clang/lib/Driver/ToolChains/Clang.cpp | 5 +++++ clang/lib/Frontend/CompilerInvocation.cpp | 5 +++++ clang/test/CodeGen/extend-lifetimes-hasfakeuses.c | 12 ++++++++++++ 7 files changed, 51 insertions(+) create mode 100644 clang/test/CodeGen/extend-lifetimes-hasfakeuses.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 601a233b81904f..f88fcef9bdade7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -412,6 +412,16 @@ New Compiler Flags only for thread-local variables, and none (which corresponds to the existing ``-fno-c++-static-destructors`` flag) skips all static destructors registration. +- The ``-fextend-lifetimes`` and ``-fextend-this-ptr`` flags have been added to + allow for improved debugging of optimized code. Using ``-fextend-lifetimes`` + will cause Clang to generate code that tries to preserve the lifetimes of + source variables, meaning that variables will typically be visible in a + debugger more often. The ``-fextend-this-ptr`` flag has the same behaviour, + but applies only to the ``this`` variable in C++ class member functions. Note + that this flag modifies the optimizations that Clang performs, which will + result in reduced performance in generated code; however, this feature will + not extend the lifetime of some variables in cases where doing so would have + too severe an impact on generated code performance. Deprecated Compiler Flags ------------------------- diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index 4cf22c4ee08ce0..2f155f7df49a3a 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -393,6 +393,12 @@ CODEGENOPT(EnableTLSDESC, 1, 0) /// Bit size of immediate TLS offsets (0 == use the default). VALUE_CODEGENOPT(TLSSize, 8, 0) +/// Whether to extend the live range of the `this` pointer. +CODEGENOPT(ExtendThisPtr, 1, 0) + +/// Whether to extend the live ranges of all local variables. +CODEGENOPT(ExtendLifetimes, 1, 0) + /// The default stack protector guard offset to use. VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9c356c9d2ea4ef..b2e22c9cb6bd95 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4298,6 +4298,15 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, Visibility<[CC1Option]>, HelpText<"Filename (or -) to write stack usage output to">, MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>; +def fextend_this_ptr : Flag <["-"], "fextend-this-ptr">, Group<f_Group>, + MarshallingInfoFlag<CodeGenOpts<"ExtendThisPtr">>, + HelpText<"Extend the lifetime of the 'this' pointer to improve visibility " + "in optimized debugging">, Visibility<[ClangOption, CC1Option]>; +def fextend_lifetimes : Flag <["-"], "fextend-lifetimes">, Group<f_Group>, + MarshallingInfoFlag<CodeGenOpts<"ExtendLifetimes">>, + HelpText<"Extend the lifetimes of local variables and parameters to improve " + "visibility in optimized debugging">, + Visibility<[ClangOption, CC1Option]>; defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names", CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse, diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 20455dbb820914..41195953c9a221 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2567,6 +2567,10 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (shouldDisableTailCalls()) FuncAttrs.addAttribute("disable-tail-calls", "true"); + // Mark the function as having fake uses when -fextend-lifetimes is on. + if (CodeGenOpts.ExtendLifetimes || CodeGenOpts.ExtendThisPtr) + FuncAttrs.addAttribute(llvm::Attribute::HasFakeUses); + // CPU/feature overrides. addDefaultFunctionDefinitionAttributes // handles these separately to set them based on the global defaults. GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 217c1a845f0a47..1068b0300b9c55 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7654,6 +7654,11 @@ 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"); + if (Args.hasArg(options::OPT_fextend_this_ptr)) + CmdArgs.push_back("-fextend-this-ptr"); + if (Args.hasArg(options::OPT_fextend_lifetimes)) + CmdArgs.push_back("-fextend-lifetimes"); + // Forward -fcomment-block-commands to -cc1. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); // Forward -fparse-all-comments to -cc1. diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 3dd94c31b2bc7a..a3e609ff1ffc3e 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2245,6 +2245,11 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, Args.getAllArgValues(OPT_fsanitize_trap_EQ), Diags, Opts.SanitizeTrap); + Opts.ExtendThisPtr = + Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_this_ptr); + Opts.ExtendLifetimes = + Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_lifetimes); + Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true); if (!LangOpts->CUDAIsDevice) diff --git a/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c b/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c new file mode 100644 index 00000000000000..081bace3dc7cce --- /dev/null +++ b/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 %s -emit-llvm -O2 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O2 %s +// RUN: %clang_cc1 %s -emit-llvm -O0 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O0 %s + +// Checks that we emit the function attribute has_fake_uses when +// -fextend-lifetimes is on and optimizations are enabled, and that it does not +// when optimizations are disabled. + +// CHECK-ALL: define {{.*}}void @foo +// CHECK-O2: attributes #0 = {{{.*}}has_fake_uses +// CHECK-O0-NOT: attributes #0 = {{{.*}}has_fake_uses + +void foo() {} >From 957a46887580334a0128bfabbfca6b98e5fb433a Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 14 Nov 2024 11:35:45 +0000 Subject: [PATCH 2/9] Make options into BoolF types, remove added attribute --- clang/docs/ReleaseNotes.rst | 11 +++++----- clang/include/clang/Driver/Options.td | 21 +++++++++++-------- clang/lib/CodeGen/CGCall.cpp | 4 ---- clang/lib/Driver/ToolChains/Clang.cpp | 8 +++---- clang/lib/Frontend/CompilerInvocation.cpp | 5 ----- .../CodeGen/extend-lifetimes-hasfakeuses.c | 12 ----------- 6 files changed, 22 insertions(+), 39 deletions(-) delete mode 100644 clang/test/CodeGen/extend-lifetimes-hasfakeuses.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f88fcef9bdade7..78696bfd9b2c82 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -417,11 +417,12 @@ New Compiler Flags will cause Clang to generate code that tries to preserve the lifetimes of source variables, meaning that variables will typically be visible in a debugger more often. The ``-fextend-this-ptr`` flag has the same behaviour, - but applies only to the ``this`` variable in C++ class member functions. Note - that this flag modifies the optimizations that Clang performs, which will - result in reduced performance in generated code; however, this feature will - not extend the lifetime of some variables in cases where doing so would have - too severe an impact on generated code performance. + but applies only to the ``this`` variable in C++ class member functions, + meaning its effect is a strict subset of ``-fextend-lifetimes``. Note that + this flag modifies the optimizations that Clang performs, which will result + in reduced performance in generated code; however, this feature will not + extend the lifetime of some variables in cases where doing so would have too + severe of an impact on generated code performance. Deprecated Compiler Flags ------------------------- diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index b2e22c9cb6bd95..76ec03a01e8f28 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4298,15 +4298,18 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, Visibility<[CC1Option]>, HelpText<"Filename (or -) to write stack usage output to">, MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>; -def fextend_this_ptr : Flag <["-"], "fextend-this-ptr">, Group<f_Group>, - MarshallingInfoFlag<CodeGenOpts<"ExtendThisPtr">>, - HelpText<"Extend the lifetime of the 'this' pointer to improve visibility " - "in optimized debugging">, Visibility<[ClangOption, CC1Option]>; -def fextend_lifetimes : Flag <["-"], "fextend-lifetimes">, Group<f_Group>, - MarshallingInfoFlag<CodeGenOpts<"ExtendLifetimes">>, - HelpText<"Extend the lifetimes of local variables and parameters to improve " - "visibility in optimized debugging">, - Visibility<[ClangOption, CC1Option]>; +defm extend_this_ptr : BoolFOption<"extend-this-ptr", + CodeGenOpts<"ExtendThisPtr">, DefaultFalse, + PosFlag<SetTrue, [], [ClangOption, CC1Option], + "Extend the lifetime of the 'this' pointer to improve visibility " + "in optimized debugging">, + NegFlag<SetFalse>>; +defm extend_lifetimes : BoolFOption<"extend-lifetimes", + CodeGenOpts<"ExtendLifetimes">, DefaultFalse, + PosFlag<SetTrue, [], [ClangOption, CC1Option], + "Extend the lifetimes of local variables and parameters to improve " + "visibility in optimized debugging">, + NegFlag<SetFalse>>; defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names", CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse, diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 41195953c9a221..20455dbb820914 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -2567,10 +2567,6 @@ void CodeGenModule::ConstructAttributeList(StringRef Name, if (shouldDisableTailCalls()) FuncAttrs.addAttribute("disable-tail-calls", "true"); - // Mark the function as having fake uses when -fextend-lifetimes is on. - if (CodeGenOpts.ExtendLifetimes || CodeGenOpts.ExtendThisPtr) - FuncAttrs.addAttribute(llvm::Attribute::HasFakeUses); - // CPU/feature overrides. addDefaultFunctionDefinitionAttributes // handles these separately to set them based on the global defaults. GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 1068b0300b9c55..5a9a57eed0ba0e 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7654,10 +7654,10 @@ 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"); - if (Args.hasArg(options::OPT_fextend_this_ptr)) - CmdArgs.push_back("-fextend-this-ptr"); - if (Args.hasArg(options::OPT_fextend_lifetimes)) - CmdArgs.push_back("-fextend-lifetimes"); + Args.addOptInFlag(CmdArgs, options::OPT_fextend_this_ptr, + options::OPT_fno_extend_this_ptr); + Args.addOptInFlag(CmdArgs, options::OPT_fextend_lifetimes, + options::OPT_fno_extend_lifetimes); // Forward -fcomment-block-commands to -cc1. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index a3e609ff1ffc3e..3dd94c31b2bc7a 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2245,11 +2245,6 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, Args.getAllArgValues(OPT_fsanitize_trap_EQ), Diags, Opts.SanitizeTrap); - Opts.ExtendThisPtr = - Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_this_ptr); - Opts.ExtendLifetimes = - Opts.OptimizationLevel > 0 && Args.hasArg(OPT_fextend_lifetimes); - Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true); if (!LangOpts->CUDAIsDevice) diff --git a/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c b/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c deleted file mode 100644 index 081bace3dc7cce..00000000000000 --- a/clang/test/CodeGen/extend-lifetimes-hasfakeuses.c +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %clang_cc1 %s -emit-llvm -O2 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O2 %s -// RUN: %clang_cc1 %s -emit-llvm -O0 -fextend-lifetimes -o - | FileCheck --check-prefixes=CHECK-ALL,CHECK-O0 %s - -// Checks that we emit the function attribute has_fake_uses when -// -fextend-lifetimes is on and optimizations are enabled, and that it does not -// when optimizations are disabled. - -// CHECK-ALL: define {{.*}}void @foo -// CHECK-O2: attributes #0 = {{{.*}}has_fake_uses -// CHECK-O0-NOT: attributes #0 = {{{.*}}has_fake_uses - -void foo() {} >From 0901017098d5f6d8161d609dbe6846ae746a7ff4 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Wed, 27 Nov 2024 18:14:54 +0000 Subject: [PATCH 3/9] Make extend lifetimes an = option --- clang/include/clang/Basic/CodeGenOptions.def | 7 ++--- clang/include/clang/Basic/CodeGenOptions.h | 6 +++++ clang/include/clang/Driver/Options.td | 28 +++++++++++--------- clang/lib/Driver/ToolChains/Clang.cpp | 5 +--- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index 2f155f7df49a3a..2e6a99949647a5 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -393,11 +393,8 @@ CODEGENOPT(EnableTLSDESC, 1, 0) /// Bit size of immediate TLS offsets (0 == use the default). VALUE_CODEGENOPT(TLSSize, 8, 0) -/// Whether to extend the live range of the `this` pointer. -CODEGENOPT(ExtendThisPtr, 1, 0) - -/// Whether to extend the live ranges of all local variables. -CODEGENOPT(ExtendLifetimes, 1, 0) +/// The types of variables that we will extend the live ranges of. +ENUM_CODEGENOPT(ExtendLifetimes, ExtendLifetimesKind, 2, ExtendLifetimesKind::None) /// The default stack protector guard offset to use. VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX) diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 2dcf98b465661e..7c4f9f6c534a01 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -95,6 +95,12 @@ class CodeGenOptions : public CodeGenOptionsBase { Embed_Marker // Embed a marker as a placeholder for bitcode. }; + enum class ExtendLifetimesKind { + None, + This, + All, + }; + enum InlineAsmDialectKind { IAD_ATT, IAD_Intel, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 76ec03a01e8f28..b5be9d891b1514 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4298,18 +4298,22 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, Visibility<[CC1Option]>, HelpText<"Filename (or -) to write stack usage output to">, MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>; -defm extend_this_ptr : BoolFOption<"extend-this-ptr", - CodeGenOpts<"ExtendThisPtr">, DefaultFalse, - PosFlag<SetTrue, [], [ClangOption, CC1Option], - "Extend the lifetime of the 'this' pointer to improve visibility " - "in optimized debugging">, - NegFlag<SetFalse>>; -defm extend_lifetimes : BoolFOption<"extend-lifetimes", - CodeGenOpts<"ExtendLifetimes">, DefaultFalse, - PosFlag<SetTrue, [], [ClangOption, CC1Option], - "Extend the lifetimes of local variables and parameters to improve " - "visibility in optimized debugging">, - NegFlag<SetFalse>>; +def fextend_lifetimes_EQ : Joined<["-"], "fextend-lifetimes=">, Group<f_Group>, + Visibility<[ClangOption, CC1Option]>, + HelpText<"Extend the lifetimes of variables to improve visibility in " + "optimized debugging">, + Values<"all,this,none">, + NormalizedValues<["All", "This", "None"]>, + NormalizedValuesScope<"CodeGenOptions::ExtendLifetimesKind">, + MarshallingInfoEnum<CodeGenOpts<"ExtendLifetimes">, "None">; +def fextend_this_ptr : Flag<["-"], "fextend-this-ptr">, + Alias<fextend_lifetimes_EQ>, AliasArgs<["this"]>, + HelpText<"Extend the lifetime of the 'this' pointer to improve visibility " + "in optimized debugging">; +def fextend_lifetimes : Flag<["-"], "fextend-lifetimes">, + Alias<fextend_lifetimes_EQ>, AliasArgs<["all"]>, + HelpText<"Extend the lifetimes of local variables and parameters to improve " + "visibility in optimized debugging">; defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names", CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 5a9a57eed0ba0e..cedf64ba34e0d3 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7654,10 +7654,7 @@ 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.addOptInFlag(CmdArgs, options::OPT_fextend_this_ptr, - options::OPT_fno_extend_this_ptr); - Args.addOptInFlag(CmdArgs, options::OPT_fextend_lifetimes, - options::OPT_fno_extend_lifetimes); + Args.AddLastArg(CmdArgs, options::OPT_fextend_lifetimes_EQ); // Forward -fcomment-block-commands to -cc1. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); >From 984e0505c644244bb55474ea67c9b4c27fc3b0a4 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 5 Dec 2024 11:06:15 +0000 Subject: [PATCH 4/9] Rename to -fextend-variable-liveness --- clang/docs/ReleaseNotes.rst | 23 +++++++++------- clang/include/clang/Basic/CodeGenOptions.def | 2 +- clang/include/clang/Basic/CodeGenOptions.h | 2 +- clang/include/clang/Driver/Options.td | 29 ++++++++++---------- clang/lib/Driver/ToolChains/Clang.cpp | 2 +- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 78696bfd9b2c82..19a40d4666c727 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -412,17 +412,20 @@ New Compiler Flags only for thread-local variables, and none (which corresponds to the existing ``-fno-c++-static-destructors`` flag) skips all static destructors registration. -- The ``-fextend-lifetimes`` and ``-fextend-this-ptr`` flags have been added to - allow for improved debugging of optimized code. Using ``-fextend-lifetimes`` - will cause Clang to generate code that tries to preserve the lifetimes of - source variables, meaning that variables will typically be visible in a - debugger more often. The ``-fextend-this-ptr`` flag has the same behaviour, - but applies only to the ``this`` variable in C++ class member functions, - meaning its effect is a strict subset of ``-fextend-lifetimes``. Note that - this flag modifies the optimizations that Clang performs, which will result +- The ``-fextend-variable-liveness`` flag has been added to allow for improved + debugging of optimized code. Using ``-fextend-variable-liveness`` will cause + Clang to generate code that tries to preserve the liveness of source variables + through optimizations, meaning that variables will typically be visible in a + debugger more often. The flag has two levels: ``-fextend-variable-liveness``, + or ``-fextend-variable-liveness=all``, extendes the liveness of all user + variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or + ``-fextend-variable-liveness=this``, has the same behaviour but applies only + to the ``this`` variable in C++ class member functions, meaning its effect is + a strict subset of ``-fextend-variable-liveness``. Note that this flag + modifies the results of optimizations that Clang performs, which will result in reduced performance in generated code; however, this feature will not - extend the lifetime of some variables in cases where doing so would have too - severe of an impact on generated code performance. + extend the liveness of some variables in cases where doing so would likely + have a severe impact on generated code performance. Deprecated Compiler Flags ------------------------- diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index 2e6a99949647a5..8d181c96bc9641 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -394,7 +394,7 @@ CODEGENOPT(EnableTLSDESC, 1, 0) VALUE_CODEGENOPT(TLSSize, 8, 0) /// The types of variables that we will extend the live ranges of. -ENUM_CODEGENOPT(ExtendLifetimes, ExtendLifetimesKind, 2, ExtendLifetimesKind::None) +ENUM_CODEGENOPT(ExtendVariableLiveness, ExtendVariableLivenessKind, 2, ExtendVariableLivenessKind::None) /// The default stack protector guard offset to use. VALUE_CODEGENOPT(StackProtectorGuardOffset, 32, INT_MAX) diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h index 7c4f9f6c534a01..f55d9513443e8c 100644 --- a/clang/include/clang/Basic/CodeGenOptions.h +++ b/clang/include/clang/Basic/CodeGenOptions.h @@ -95,7 +95,7 @@ class CodeGenOptions : public CodeGenOptionsBase { Embed_Marker // Embed a marker as a placeholder for bitcode. }; - enum class ExtendLifetimesKind { + enum class ExtendVariableLivenessKind { None, This, All, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index b5be9d891b1514..9ebf9f5a11f420 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4298,22 +4298,23 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, Visibility<[CC1Option]>, HelpText<"Filename (or -) to write stack usage output to">, MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>; -def fextend_lifetimes_EQ : Joined<["-"], "fextend-lifetimes=">, Group<f_Group>, - Visibility<[ClangOption, CC1Option]>, - HelpText<"Extend the lifetimes of variables to improve visibility in " - "optimized debugging">, +def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, + Group<f_Group>, Visibility<[ClangOption, CC1Option]>, + HelpText<"Extend the liveness of user variables through optimizations to " + "prevent stale or optimized-out variable values when debugging.">, Values<"all,this,none">, NormalizedValues<["All", "This", "None"]>, - NormalizedValuesScope<"CodeGenOptions::ExtendLifetimesKind">, - MarshallingInfoEnum<CodeGenOpts<"ExtendLifetimes">, "None">; -def fextend_this_ptr : Flag<["-"], "fextend-this-ptr">, - Alias<fextend_lifetimes_EQ>, AliasArgs<["this"]>, - HelpText<"Extend the lifetime of the 'this' pointer to improve visibility " - "in optimized debugging">; -def fextend_lifetimes : Flag<["-"], "fextend-lifetimes">, - Alias<fextend_lifetimes_EQ>, AliasArgs<["all"]>, - HelpText<"Extend the lifetimes of local variables and parameters to improve " - "visibility in optimized debugging">; + NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">, + MarshallingInfoEnum<CodeGenOpts<"ExtendVariableLiveness">, "None">; +def fextend_this_ptr_liveness : Flag<["-"], "fextend-this-ptr-liveness">, + Alias<fextend_variable_liveness_EQ>, AliasArgs<["this"]>, + HelpText<"Extend the liveness of the `this` pointer through optimizations to " + "prevent a stale or optimized-out value when debugging.">; +def fextend_variable_liveness : Flag<["-"], "fextend-variable-liveness">, + Alias<fextend_variable_liveness_EQ>, AliasArgs<["all"]>, + HelpText<"Extend the liveness of all local variables and parameters through " + "optimizations to prevent stale or optimized-out variable values " + "when debugging.">; defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names", CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse, diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index cedf64ba34e0d3..ece14d1e3cbf7d 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7654,7 +7654,7 @@ 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_lifetimes_EQ); + Args.AddLastArg(CmdArgs, options::OPT_fextend_variable_liveness_EQ); // Forward -fcomment-block-commands to -cc1. Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands); >From 8fdaaf7cd753dd38d9567a8246ba619ede946648 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 5 Dec 2024 11:37:33 +0000 Subject: [PATCH 5/9] Add visibility to alias --- clang/include/clang/Driver/Options.td | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9ebf9f5a11f420..d6e2c6e4732206 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4307,10 +4307,12 @@ def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">, MarshallingInfoEnum<CodeGenOpts<"ExtendVariableLiveness">, "None">; def fextend_this_ptr_liveness : Flag<["-"], "fextend-this-ptr-liveness">, + Visibility<[ClangOption, CC1Option]>, Alias<fextend_variable_liveness_EQ>, AliasArgs<["this"]>, HelpText<"Extend the liveness of the `this` pointer through optimizations to " "prevent a stale or optimized-out value when debugging.">; def fextend_variable_liveness : Flag<["-"], "fextend-variable-liveness">, + Visibility<[ClangOption, CC1Option]>, Alias<fextend_variable_liveness_EQ>, AliasArgs<["all"]>, HelpText<"Extend the liveness of all local variables and parameters through " "optimizations to prevent stale or optimized-out variable values " >From 6835e13e8bf908e10ce9e6a485ad2d90367c9354 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 12 Dec 2024 14:01:35 +0000 Subject: [PATCH 6/9] Add Driver test for flag --- clang/test/Driver/extend-variable-liveness.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 clang/test/Driver/extend-variable-liveness.c diff --git a/clang/test/Driver/extend-variable-liveness.c b/clang/test/Driver/extend-variable-liveness.c new file mode 100644 index 00000000000000..bdd89d6f7721ce --- /dev/null +++ b/clang/test/Driver/extend-variable-liveness.c @@ -0,0 +1,15 @@ +// Tests that -fextend-variable-liveness and its aliases are correctly passed +// by the driver. + +// RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT +// 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-this-ptr-liveness -### -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 +// RUN: %clang -fextend-variable-liveness -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL + +// CHECK: "-cc1" +// DEFAULT-NOT: -fextend-variable-liveness +// NONE-SAME: "-fextend-variable-liveness=none" +// THIS-SAME: "-fextend-variable-liveness=this" +// ALL-SAME: "-fextend-variable-liveness=all" >From 7ff39f83e4d46721891f332244803cd40bd7a938 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 12 Dec 2024 15:12:54 +0000 Subject: [PATCH 7/9] Improve help text for new flags --- clang/include/clang/Driver/Options.td | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index d6e2c6e4732206..bd9e5407d3843b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4301,7 +4301,11 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, Group<f_Group>, Visibility<[ClangOption, CC1Option]>, HelpText<"Extend the liveness of user variables through optimizations to " - "prevent stale or optimized-out variable values when debugging.">, + "prevent stale or optimized-out variable values when debugging. Can " + "be applied to all user variables, or just to the C++ 'this' ptr. " + "May choose not to extend the liveness of some variables, such as " + "non-scalars larger than 4 unsigned ints, or variables in any " + "inlined functions.">, Values<"all,this,none">, NormalizedValues<["All", "This", "None"]>, NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">, @@ -4309,14 +4313,11 @@ def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, def fextend_this_ptr_liveness : Flag<["-"], "fextend-this-ptr-liveness">, Visibility<[ClangOption, CC1Option]>, Alias<fextend_variable_liveness_EQ>, AliasArgs<["this"]>, - HelpText<"Extend the liveness of the `this` pointer through optimizations to " - "prevent a stale or optimized-out value when debugging.">; + HelpText<"Alias for -fextend-variable-liveness=this.">; def fextend_variable_liveness : Flag<["-"], "fextend-variable-liveness">, Visibility<[ClangOption, CC1Option]>, Alias<fextend_variable_liveness_EQ>, AliasArgs<["all"]>, - HelpText<"Extend the liveness of all local variables and parameters through " - "optimizations to prevent stale or optimized-out variable values " - "when debugging.">; + HelpText<"Alias for -fextend-variable-liveness=all.">; defm unique_basic_block_section_names : BoolFOption<"unique-basic-block-section-names", CodeGenOpts<"UniqueBasicBlockSectionNames">, DefaultFalse, >From 5580215ee5227520ba31d7d51a4c947ccc47bfb8 Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Thu, 9 Jan 2025 15:25:59 +0000 Subject: [PATCH 8/9] Remove -fextend-this-ptr alias --- clang/docs/ReleaseNotes.rst | 16 ++++++++-------- clang/include/clang/Driver/Options.td | 4 ---- clang/test/Driver/extend-variable-liveness.c | 1 - 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 19a40d4666c727..1feaa835861292 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -418,14 +418,14 @@ New Compiler Flags through optimizations, meaning that variables will typically be visible in a debugger more often. The flag has two levels: ``-fextend-variable-liveness``, or ``-fextend-variable-liveness=all``, extendes the liveness of all user - variables and the ``this`` pointer. Alternatively ``-fextend-this-ptr``, or - ``-fextend-variable-liveness=this``, has the same behaviour but applies only - to the ``this`` variable in C++ class member functions, meaning its effect is - a strict subset of ``-fextend-variable-liveness``. Note that this flag - modifies the results of optimizations that Clang performs, which will result - in reduced performance in generated code; however, this feature will not - extend the liveness of some variables in cases where doing so would likely - have a severe impact on generated code performance. + variables and the ``this`` pointer. Alternatively + ``-fextend-variable-liveness=this`` has the same behaviour but applies only to + the ``this`` variable in C++ class member functions, meaning its effect is a + strict subset of ``-fextend-variable-liveness``. Note that this flag modifies + the results of optimizations that Clang performs, which will result in reduced + performance in generated code; however, this feature will not extend the + liveness of some variables in cases where doing so would likely have a severe + impact on generated code performance. Deprecated Compiler Flags ------------------------- diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index bd9e5407d3843b..b5b41ab3ea2a4a 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4310,10 +4310,6 @@ def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, NormalizedValues<["All", "This", "None"]>, NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">, MarshallingInfoEnum<CodeGenOpts<"ExtendVariableLiveness">, "None">; -def fextend_this_ptr_liveness : Flag<["-"], "fextend-this-ptr-liveness">, - Visibility<[ClangOption, CC1Option]>, - Alias<fextend_variable_liveness_EQ>, AliasArgs<["this"]>, - HelpText<"Alias for -fextend-variable-liveness=this.">; def fextend_variable_liveness : Flag<["-"], "fextend-variable-liveness">, Visibility<[ClangOption, CC1Option]>, Alias<fextend_variable_liveness_EQ>, AliasArgs<["all"]>, diff --git a/clang/test/Driver/extend-variable-liveness.c b/clang/test/Driver/extend-variable-liveness.c index bdd89d6f7721ce..bbfb2ece6f297e 100644 --- a/clang/test/Driver/extend-variable-liveness.c +++ b/clang/test/Driver/extend-variable-liveness.c @@ -4,7 +4,6 @@ // RUN: %clang -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,DEFAULT // 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-this-ptr-liveness -### -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 // RUN: %clang -fextend-variable-liveness -### -c %s 2>&1 | FileCheck %s --check-prefixes=CHECK,ALL >From 7a26d4e53fa1643af212def09247a32950101d6c Mon Sep 17 00:00:00 2001 From: Stephen Tozer <stephen.to...@sony.com> Date: Fri, 24 Jan 2025 18:53:32 +0000 Subject: [PATCH 9/9] Shorten help text --- clang/include/clang/Driver/Options.td | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index b5b41ab3ea2a4a..1088f67e083e22 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4301,11 +4301,7 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">, def fextend_variable_liveness_EQ : Joined<["-"], "fextend-variable-liveness=">, Group<f_Group>, Visibility<[ClangOption, CC1Option]>, HelpText<"Extend the liveness of user variables through optimizations to " - "prevent stale or optimized-out variable values when debugging. Can " - "be applied to all user variables, or just to the C++ 'this' ptr. " - "May choose not to extend the liveness of some variables, such as " - "non-scalars larger than 4 unsigned ints, or variables in any " - "inlined functions.">, + "prevent stale or optimized-out variable values when debugging." Values<"all,this,none">, NormalizedValues<["All", "This", "None"]>, NormalizedValuesScope<"CodeGenOptions::ExtendVariableLivenessKind">, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits