[clang] [Clang] Add "extend lifetime" flags and release note (PR #110000)

Stephen Tozer via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 03:06:49 PST 2024


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.tozer at sony.com>
Date: Wed, 25 Sep 2024 15:08:39 +0100
Subject: [PATCH 1/4] [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.tozer at sony.com>
Date: Thu, 14 Nov 2024 11:35:45 +0000
Subject: [PATCH 2/4] 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.tozer at sony.com>
Date: Wed, 27 Nov 2024 18:14:54 +0000
Subject: [PATCH 3/4] 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.tozer at sony.com>
Date: Thu, 5 Dec 2024 11:06:15 +0000
Subject: [PATCH 4/4] 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);



More information about the cfe-commits mailing list