[clang] 71ab44a - [Clang] Add "extend lifetime" flags and release note (#110000)
Stephen Tozer via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 28 02:15:29 PST 2025
Author: Wolfgang Pieb
Date: 2025-01-28T10:08:02Z
New Revision: 71ab44a8193c56e4ef9aede4d9bac8e14760c6c6
URL: https://github.com/llvm/llvm-project/commit/71ab44a8193c56e4ef9aede4d9bac8e14760c6c6
DIFF: https://github.com/llvm/llvm-project/commit/71ab44a8193c56e4ef9aede4d9bac8e14760c6c6.diff
LOG: [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 <stephen.tozer at sony.com>
Added:
clang/test/Driver/extend-variable-liveness.c
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index de24ab9cab5c6e..ad524ad3c5bbba 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -519,6 +519,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-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 liveness of some variables in cases where doing so would likely
+ have a severe impact on generated code performance.
- The ``-Warray-compare`` warning has been added to warn about array comparison
on versions older than C++20.
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index 1ab8c7fb4d3c33..259972bdf8f001 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -393,6 +393,9 @@ CODEGENOPT(EnableTLSDESC, 1, 0)
/// Bit size of immediate TLS offsets (0 == use the default).
VALUE_CODEGENOPT(TLSSize, 8, 0)
+/// The types of variables that we will extend the live ranges of.
+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 b64ad74d711c60..c531c656f42b7e 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 ExtendVariableLivenessKind {
+ 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 75f2487143ee8b..6fa8a8273aca34 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4349,6 +4349,26 @@ def stack_usage_file : Separate<["-"], "stack-usage-file">,
Visibility<[CC1Option]>,
HelpText<"Filename (or -) to write stack usage output to">,
MarshallingInfoString<CodeGenOpts<"StackUsageOutput">>;
+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.">,
+ Values<"all,this,none">,
+ 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"]>,
+ HelpText<"Alias for -fextend-variable-liveness=all.">;
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 518113e20cb063..589de953be5be1 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7706,6 +7706,8 @@ 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_variable_liveness_EQ);
+
// Forward -fcomment-block-commands to -cc1.
Args.AddAllArgs(CmdArgs, options::OPT_fcomment_block_commands);
// Forward -fparse-all-comments to -cc1.
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"
More information about the cfe-commits
mailing list