[clang] [Clang] Add pragma to selectively disable variable lifetime extension (PR #241298)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 03:51:50 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Katya Romanova (romanova-ekaterina)

<details>
<summary>Changes</summary>

This patch adds #pragma clang extend_lifetimes disable and #pragma clang extend_lifetimes enable to selectively suppress lifetime extension for subsequent function definitions, including lambdas.

Developers can use these pragmas to avoid the potential performance overhead of lifetime extension in functions they do not need to debug. Place disable before those functions and enable afterward to restore the behavior selected by -fextend-lifetimes or -fextend-this-ptr.

This is useful for development builds with repeated edit-compile-debug cycles. The pragmas can remain in the source for release builds: they have no effect unless one of those command-line options is enabled. In particular, enable does not independently turn on lifetime extension.

This patch also documents the pragma and add tests for ordinary functions and lambdas with pragma enabled and disabled.

This patch is based on work by Wolfgang Pieb. I will take the ownership for this PR and address the code review comments.

---
Full diff: https://github.com/llvm/llvm-project/pull/241298.diff


12 Files Affected:

- (modified) clang/include/clang/Basic/Attr.td (+6) 
- (modified) clang/include/clang/Basic/AttrDocs.td (+28) 
- (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+6) 
- (modified) clang/include/clang/Parse/Parser.h (+1) 
- (modified) clang/include/clang/Sema/Sema.h (+24) 
- (modified) clang/lib/CodeGen/CGDecl.cpp (+3) 
- (modified) clang/lib/Parse/ParsePragma.cpp (+60) 
- (modified) clang/lib/Sema/SemaAttr.cpp (+20) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+4) 
- (modified) clang/lib/Sema/SemaLambda.cpp (+3) 
- (added) clang/test/CodeGen/pragma-extend-lifetimes-lambda.cpp (+105) 
- (added) clang/test/CodeGen/pragma-extend-lifetimes.cpp (+87) 


``````````diff
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 24fd853a2384af..f7f225bc59f502 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -2397,6 +2397,12 @@ def NoCommon : InheritableAttr {
   let SimpleHandler = 1;
 }
 
+def ExtendLifetimesDisable : InheritableAttr {
+  let Spellings = [];
+  let Subjects = SubjectList<[Function]>;
+  let Documentation = [ExtendLifetimesDisableDocs];
+}
+
 def NoDebug : InheritableAttr {
   let Spellings = [GCC<"nodebug">];
   let Subjects = SubjectList<[TypedefName, FunctionLike, ObjCMethod, NonParmVar]>;
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 2300c8456ef164..844a3979eefa51 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -5105,6 +5105,34 @@ function table. It is only supported when using the Microsoft C++ ABI.
   }];
 }
 
+def ExtendLifetimesDisableDocs : Documentation {
+  let Category = DocCatFunction;
+  let Heading = "#pragma clang extend_lifetimes disable, #pragma clang extend_lifetimes enable";
+  let Content = [{
+The ``#pragma clang extend_lifetimes disable`` directive suppresses lifetime
+extension enabled by ``-fextend-variable-liveness=all`` or ``-fextend-variable-liveness=this`` for
+subsequent function definitions. This can be useful when lifetime extension
+causes excessive overhead in particular functions.
+
+The ``#pragma clang extend_lifetimes enable`` directive restores the behavior
+selected by the command-line options for subsequent function definitions.
+
+.. code-block:: c++
+
+  #pragma clang extend_lifetimes disable
+  void foo(...) {} // Lifetime extension disabled.
+  void bar(...) {} // Lifetime extension remains disabled.
+  #pragma clang extend_lifetimes enable
+  void baz(...) {} // Lifetime extension follows the command-line options.
+
+A directive inside a function does not affect that function, but does affect
+subsequent function definitions.
+
+These directives have no effect if neither ``-fextend-variable-liveness=all`` nor
+``-fextend-variable-liveness=this`` is enabled.
+  }];
+}
+
 def OptnoneDocs : Documentation {
   let Category = DocCatFunction;
   let Content = [{
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 2be6ff8344cd6d..1d7fd993cbed9e 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1226,6 +1226,12 @@ def err_pragma_optimize_invalid_argument : Error<
   "expected 'on' or 'off'">;
 def err_pragma_optimize_extra_argument : Error<
   "unexpected extra argument '%0' to '#pragma clang optimize'">;
+// - #pragma clang extend_lifetimes enable/disable
+def err_pragma_extend_lifetimes_invalid_argument : Error<
+  "unexpected argument '%0' to '#pragma clang extend_lifetimes'; "
+  "expected 'enable' or 'disable'">;
+def err_pragma_extend_lifetimes_extra_argument : Error<
+  "unexpected extra argument '%0' to '#pragma clang extend_lifetimes'">;
 // - #pragma clang attribute
 def err_pragma_attribute_expected_push_pop_paren : Error<
   "expected 'push', 'pop', or '(' after '#pragma clang attribute'">;
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index e4fa25ebc427f6..7df2109e662c13 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -7184,6 +7184,7 @@ class Parser : public CodeCompletionHandler {
   std::unique_ptr<PragmaHandler> MSAllocText;
   std::unique_ptr<PragmaHandler> CUDAForceHostDeviceHandler;
   std::unique_ptr<PragmaHandler> OptimizeHandler;
+  std::unique_ptr<PragmaHandler> ExtendLifetimesHandler;
   std::unique_ptr<PragmaHandler> LoopHintHandler;
   std::unique_ptr<PragmaHandler> UnrollHintHandler;
   std::unique_ptr<PragmaHandler> NoUnrollHintHandler;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 3abbaf6b1941ec..ef0eee2c0e2bf2 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2184,6 +2184,19 @@ class Sema final : public SemaBase {
     return OptimizeOffPragmaLocation;
   }
 
+  /// This represents the last location of a "#pragma clang extend_lifetimes
+  /// disable" directive if such a directive has not been closed by an "enable"
+  /// yet. If extend_lifetimes is currently enabled, this is set to an invalid
+  /// location.
+  SourceLocation ExtendLifetimesDisablePragmaLocation;
+
+  /// Get the location for the currently active "\#pragma extend_lifetimes
+  /// disable". If this location is invalid, then the state of the pragma is
+  /// "enabled".
+  SourceLocation getExtendLifetimesDisablePragmaLocation() const {
+    return ExtendLifetimesDisablePragmaLocation;
+  }
+
   /// The "on" or "off" argument passed by \#pragma optimize, that denotes
   /// whether the optimizations in the list passed to the pragma should be
   /// turned off or on. This boolean is true by default because command line
@@ -2371,6 +2384,9 @@ class Sema final : public SemaBase {
   /// Called on well formed \#pragma clang optimize.
   void ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc);
 
+  /// Called on well formed \#pragma clang extend_lifetimes.
+  void ActOnPragmaExtendLifetimes(bool Enable, SourceLocation PragmaLoc);
+
   /// #pragma optimize("[optimization-list]", on | off).
   void ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn);
 
@@ -2411,6 +2427,14 @@ class Sema final : public SemaBase {
   /// attribute to be added (usually because of a pragma).
   void AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, SourceLocation Loc);
 
+  /// For function definitions, if there is a pragma in scope with the effect
+  /// of disabling extend_lifetimes, mark the function with attribute
+  /// ExtendLifetimesDisable.
+  void AddRangeBasedExtendLifetimesDisable(FunctionDecl *FD);
+
+  /// Adds the ExtendLifetimesDisable attribute to the function declaration.
+  void AddExtendLifetimesDisableAttribute(FunctionDecl *FD, SourceLocation Loc);
+
   /// Only called on function definitions; if there is a MSVC #pragma optimize
   /// in scope, consider changing the function's attributes based on the
   /// optimization list passed to the pragma.
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index b425392ebbe41c..3fb2422b9ec032 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -1485,6 +1485,9 @@ static bool shouldExtendLifetime(const ASTContext &Context,
   // Do not extend variables in nodebug or optnone functions.
   if (FuncDecl->hasAttr<NoDebugAttr>() || FuncDecl->hasAttr<OptimizeNoneAttr>())
     return false;
+  // pragma clang extend_lifetimes disable has been seen.
+  if (FuncDecl->hasAttr<ExtendLifetimesDisableAttr>())
+    return false;
   return true;
 }
 
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index dd05bd3c21442a..ee79da656e4cc7 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -280,6 +280,18 @@ struct PragmaOptimizeHandler : public PragmaHandler {
   Sema &Actions;
 };
 
+/// PragmaExtendLifetimesHandler - "\#pragma clang extend_lifetimes
+/// enable/disable".
+struct PragmaExtendLifetimesHandler : public PragmaHandler {
+  PragmaExtendLifetimesHandler(Sema &S)
+      : PragmaHandler("extend_lifetimes"), Actions(S) {}
+  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
+                    Token &FirstToken) override;
+
+private:
+  Sema &Actions;
+};
+
 struct PragmaLoopHintHandler : public PragmaHandler {
   PragmaLoopHintHandler() : PragmaHandler("loop") {}
   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
@@ -527,6 +539,10 @@ void Parser::initializePragmaHandlers() {
   OptimizeHandler = std::make_unique<PragmaOptimizeHandler>(Actions);
   PP.AddPragmaHandler("clang", OptimizeHandler.get());
 
+  ExtendLifetimesHandler =
+      std::make_unique<PragmaExtendLifetimesHandler>(Actions);
+  PP.AddPragmaHandler("clang", ExtendLifetimesHandler.get());
+
   LoopHintHandler = std::make_unique<PragmaLoopHintHandler>();
   PP.AddPragmaHandler("clang", LoopHintHandler.get());
 
@@ -670,6 +686,9 @@ void Parser::resetPragmaHandlers() {
   PP.RemovePragmaHandler("clang", OptimizeHandler.get());
   OptimizeHandler.reset();
 
+  PP.RemovePragmaHandler("clang", ExtendLifetimesHandler.get());
+  ExtendLifetimesHandler.reset();
+
   PP.RemovePragmaHandler("clang", LoopHintHandler.get());
   LoopHintHandler.reset();
 
@@ -3375,6 +3394,47 @@ void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
   Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
 }
 
+// #pragma clang extend_lifetimes disable
+// #pragma clang extend_lifetimes enable
+void PragmaExtendLifetimesHandler::HandlePragma(Preprocessor &PP,
+                                                PragmaIntroducer Introducer,
+                                                Token &FirstToken) {
+  Token Tok;
+  PP.Lex(Tok);
+  if (Tok.is(tok::eod)) {
+    PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
+        << "clang extend_lifetimes" << /*Expected=*/true
+        << "'enable' or 'disable'";
+    return;
+  }
+  if (Tok.isNot(tok::identifier)) {
+    PP.Diag(Tok.getLocation(),
+            diag::err_pragma_extend_lifetimes_invalid_argument)
+        << PP.getSpelling(Tok);
+    return;
+  }
+  const IdentifierInfo *II = Tok.getIdentifierInfo();
+  // The only accepted values are 'enable' or 'disable'.
+  bool IsEnabled = false;
+  if (II->isStr("enable")) {
+    IsEnabled = true;
+  } else if (!II->isStr("disable")) {
+    PP.Diag(Tok.getLocation(),
+            diag::err_pragma_extend_lifetimes_invalid_argument)
+        << PP.getSpelling(Tok);
+    return;
+  }
+  PP.Lex(Tok);
+
+  if (Tok.isNot(tok::eod)) {
+    PP.Diag(Tok.getLocation(), diag::err_pragma_extend_lifetimes_extra_argument)
+        << PP.getSpelling(Tok);
+    return;
+  }
+
+  Actions.ActOnPragmaExtendLifetimes(IsEnabled, FirstToken.getLocation());
+}
+
 namespace {
 /// Used as the annotation value for tok::annot_pragma_fp.
 struct TokFPAnnotValue {
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index e5253e2a311747..42872844e59795 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1326,6 +1326,13 @@ void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
     OptimizeOffPragmaLocation = PragmaLoc;
 }
 
+void Sema::ActOnPragmaExtendLifetimes(bool Enable, SourceLocation PragmaLoc) {
+  if (Enable)
+    ExtendLifetimesDisablePragmaLocation = SourceLocation();
+  else
+    ExtendLifetimesDisablePragmaLocation = PragmaLoc;
+}
+
 void Sema::ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn) {
   if (!CurContext->getRedeclContext()->isFileContext()) {
     Diag(Loc, diag::err_pragma_expected_file_scope) << "optimize";
@@ -1389,6 +1396,19 @@ void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
     FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
 }
 
+void Sema::AddRangeBasedExtendLifetimesDisable(FunctionDecl *FD) {
+  if (ExtendLifetimesDisablePragmaLocation.isValid())
+    AddExtendLifetimesDisableAttribute(FD,
+                                       ExtendLifetimesDisablePragmaLocation);
+}
+
+void Sema::AddExtendLifetimesDisableAttribute(FunctionDecl *FD,
+                                              SourceLocation Loc) {
+  // Add the attribute only if it is not already present.
+  if (!FD->hasAttr<ExtendLifetimesDisableAttr>())
+    FD->addAttr(ExtendLifetimesDisableAttr::CreateImplicit(Context, Loc));
+}
+
 void Sema::AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl *FD) {
   if (FD->isDeleted() || FD->isDefaulted())
     return;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d4fbf04ee284af..d65dd7509a21db 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -11325,6 +11325,10 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
     ModifyFnAttributesMSPragmaOptimize(NewFD);
   }
 
+  // Check if we have to apply the extend_lifetimes disable pragma.
+  if (D.isFunctionDefinition())
+    AddRangeBasedExtendLifetimesDisable(NewFD);
+
   // If this is the first declaration of an extern C variable, update
   // the map of such variables.
   if (NewFD->isFirstDecl() && !NewFD->isInvalidDecl() &&
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index 2a74f59a1d3833..7c4dd1804fa1c5 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -1518,6 +1518,9 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
 
   CheckCXXDefaultArguments(Method);
 
+  // Check if we have to apply the extend_lifetimes disable pragma.
+  AddRangeBasedExtendLifetimesDisable(Method);
+
   // code_seg attribute on lambda apply to the method.
   if (Attr *A = getImplicitCodeSegOrSectionAttrForFunction(
           Method, /*IsDefinition=*/true))
diff --git a/clang/test/CodeGen/pragma-extend-lifetimes-lambda.cpp b/clang/test/CodeGen/pragma-extend-lifetimes-lambda.cpp
new file mode 100644
index 00000000000000..9e759e578e380c
--- /dev/null
+++ b/clang/test/CodeGen/pragma-extend-lifetimes-lambda.cpp
@@ -0,0 +1,105 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -O1 -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=NO-EXTENSION --implicit-check-not="call void (...) @llvm.fake.use("
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -O1 -disable-llvm-passes -emit-llvm -fextend-variable-liveness=this -o - %s | FileCheck %s --check-prefixes=CHECK,THIS
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++11 -O1 -disable-llvm-passes -emit-llvm -fextend-variable-liveness=all -o - %s | FileCheck %s --check-prefixes=CHECK,ALL
+
+// Keep the lambda call operators intact without adding the automatic optnone
+// attribute used at -O0.
+// NO-EXTENSION: define
+
+// Lifetime extension is initially enabled by the command-line option.
+void foo() {
+  auto lambda = [](int value) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}foo{{.*}}clEi
+// THIS-NOT: @llvm.fake.use
+// ALL: call void (...) @llvm.fake.use(i32 %
+// CHECK: call void (...) @llvm.fake.use(ptr %
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+#pragma clang extend_lifetimes disable
+// Disable lifetime extension.
+void bar() {
+  auto lambda = [](int value) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}bar{{.*}}clEi
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+// The disabling pragma spans multiple lambda definitions.
+void bat() {
+  auto lambda = [](int value) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}bat{{.*}}clEi
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+#pragma clang extend_lifetimes enable
+// Enable lifetime extension.
+void baz() {
+  auto lambda = [](int value) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}baz{{.*}}clEi
+// THIS-NOT: @llvm.fake.use
+// ALL: call void (...) @llvm.fake.use(i32 %
+// CHECK: call void (...) @llvm.fake.use(ptr %
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+// Disabling inside a lambda leaves that lambda enabled.
+void goo() {
+  auto lambda = [](int value) {
+#pragma clang extend_lifetimes disable
+  };
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}goo{{.*}}clEi
+// THIS-NOT: @llvm.fake.use
+// ALL: call void (...) @llvm.fake.use(i32 %
+// CHECK: call void (...) @llvm.fake.use(ptr %
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+// The previous disable applies here; enabling inside affects later lambdas.
+void moo() {
+  auto lambda = [](int value) {
+#pragma clang extend_lifetimes enable
+  };
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}moo{{.*}}clEi
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+// The enable in the previous lambda applies to this lambda.
+void zoo() {
+  auto lambda = [](int value) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}zoo{{.*}}clEi
+// THIS-NOT: @llvm.fake.use
+// ALL: call void (...) @llvm.fake.use(i32 %
+// CHECK: call void (...) @llvm.fake.use(ptr %
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
+
+// The nodebug attribute suppresses extension on the call operator.
+void fie() {
+  auto lambda = [](int value) __attribute__((nodebug)) {};
+  lambda(42);
+}
+
+// CHECK-LABEL: define internal{{.*}}fie{{.*}}clEi
+// CHECK-NOT: @llvm.fake.use
+// CHECK: ret void
\ No newline at end of file
diff --git a/clang/test/CodeGen/pragma-extend-lifetimes.cpp b/clang/test/CodeGen/pragma-extend-lifetimes.cpp
new file mode 100644
index 00000000000000..5f446126202141
--- /dev/null
+++ b/clang/test/CodeGen/pragma-extend-lifetimes.cpp
@@ -0,0 +1,87 @@
+// RUN: %clang %s --target=%itanium_abi_triple -S -O2 -emit-llvm -o - \
+// RUN:   | FileCheck %s --check-prefix=NO-EXTENSION --implicit-check-not="call void (...) @llvm.fake.use("
+// Check that we do not generate fake_use instructions without -fextend-variable-liveness=this
+// or -fextend-variable-liveness=all, even with `#pragma clang extend_lifetimes enable`
+// declared.
+
+// RUN: %clang %s --target=%itanium_abi_triple -S -O2 -emit-llvm -fextend-variable-liveness=this -o - \
+// RUN:   | FileCheck %s --implicit-check-not="call void (...) @llvm.fake.use("
+// Check that we generate a fake_use instruction for this 'this' ptr in foo()
+// and baz(), but not for bar().
+
+// RUN: %clang %s --target=%itanium_abi_triple -S -O2 -emit-llvm -fextend-variable-liveness=all -o - \
+// RUN:   | FileCheck %s --check-prefixes CHECK,ALL --implicit-check-not="call void (...) @llvm.fake.use("
+// Check that we generate a fake_use instruction for all parameters (including
+// the 'this' ptr) in foo() and baz(), but not for bar().
+
+class A {
+  void foo(int i);
+  void bar(int i);
+  void bat(int i);
+  void baz(int i);
+  void goo(int i);
+  void moo(int i);
+  void zoo(int i);
+  void fie(int i);
+};
+
+// NO-EXTENSION: define{{.*}}foo
+// Lifetime extension is initially enabled by the command-line option.
+void A::foo(int i) {
+  // CHECK-LABEL: define{{.*}}foo
+  // ALL:         call void (...) @llvm.fake.use(i32 %
+  // CHECK:       call void (...) @llvm.fake.use(ptr nonnull %
+}
+
+#pragma clang extend_lifetimes disable
+// Disable lifetime extension for subsequent function definitions.
+void A::bar(int i) {
+  // CHECK-LABEL: define{{.*}}bar
+  // CHECK-NOT:   fake.use
+  // CHECK:       ret
+}
+
+// The disabling pragma spans multiple function definitions.
+void A::bat(int i) {
+  // CHECK-LABEL: define{{.*}}bat
+  // CHECK-NOT:   fake.use
+  // CHECK:       ret
+}
+
+#pragma clang extend_lifetimes enable
+// Enable lifetime extension for subsequent function definitions.
+void A::baz(int i) {
+  // CHECK-LABEL: define{{.*}}baz
+  // ALL:         call void (...) @llvm.fake.use(i32 %
+  // CHECK:       call void (...) @llvm.fake.use(ptr nonnull %
+}
+
+// Disabling inside a function does not affect that function, only subsequent ones.
+void A::goo(int i) {
+#pragma clang extend_lifetimes disable
+  // CHECK-LABEL: define{{.*}}goo
+  // ALL:         call void (...) @llvm.fake.use(i32 %
+  // CHECK:       call void (...) @llvm.fake.use(ptr nonnull %
+}
+
+// The previous disable applies here; enabling inside affects subsequent functions.
+void A::moo(int i) {
+#pragma clang extend_lifetimes enable
+  // CHECK-LABEL: define{{.*}}moo
+  // CHECK-NOT:   fake.use
+  // CHECK:       ret
+}
+
+// The enable in the previous function applies to this function.
+void A::zoo(int i) {
+  // CHECK-LABEL: define{{.*}}zoo
+  // ALL:         call void (...) @llvm.fake.use(i32 %
+  // CHECK:       call void (...) @llvm.fake.use(ptr nonnull %
+}
+
+// The nodebug attribute suppresses lifetime extension.
+void __attribute__((nodebug)) A::fie(int i) {
+  int j = 0;
+  // CHECK-LABEL: define{{.*}}fie
+  // CHECK-NOT:   fake.use
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/241298


More information about the cfe-commits mailing list