[clang] [clang-tools-extra] Reapply "[clang] Limit lifetimes of temporaries to the full expression (#170517)" (PR #175816)

2026-04-08 Thread Paul Kirth via cfe-commits

https://github.com/ilovepi updated 
https://github.com/llvm/llvm-project/pull/175816

>From 97a30af46c02440f2498ff90f9befab99d6eca9b Mon Sep 17 00:00:00 2001
From: Paul Kirth 
Date: Fri, 12 Dec 2025 09:03:44 -0800
Subject: [PATCH 1/8] Reapply "[clang] Limit lifetimes of temporaries to the
 full expression (#170517)"

This reverts commit 6d38c876478dac4a42f9d6e37692348deabf6a25. The
current version only works when exceptions are not enabled until we
determine how to resolve issues around broken dominance relationships
with the def-use chain.
---
 clang/docs/ReleaseNotes.rst   | 11 +++
 clang/include/clang/Basic/CodeGenOptions.def  |  4 +
 clang/include/clang/Options/Options.td|  5 +
 clang/lib/CodeGen/CGCall.cpp  | 25 -
 clang/lib/CodeGen/CGCleanup.cpp   |  7 +-
 clang/test/CodeGen/lifetime-bug-2.c   | 58 +++
 clang/test/CodeGen/lifetime-bug.cpp   | 58 +++
 clang/test/CodeGen/lifetime-call-temp.c   | 98 +++
 clang/test/CodeGen/lifetime-invoke-c.c| 36 +++
 .../CodeGenCXX/aggregate-lifetime-invoke.cpp  | 40 
 .../CodeGenCXX/amdgcn-call-with-aggarg.cc | 19 
 .../CodeGenCXX/stack-reuse-miscompile.cpp |  4 +
 clang/test/CodeGenCoroutines/pr59181.cpp  |  4 +
 13 files changed, 365 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/CodeGen/lifetime-bug-2.c
 create mode 100644 clang/test/CodeGen/lifetime-bug.cpp
 create mode 100644 clang/test/CodeGen/lifetime-call-temp.c
 create mode 100644 clang/test/CodeGen/lifetime-invoke-c.c
 create mode 100644 clang/test/CodeGenCXX/aggregate-lifetime-invoke.cpp
 create mode 100644 clang/test/CodeGenCXX/amdgcn-call-with-aggarg.cc

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2da7175b51ea3..9f13c8329dd73 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -36,6 +36,17 @@ latest release, please see the `Clang Web Site 
`_ or the
 
 Potentially Breaking Changes
 
+- When exceptions are disabled, Clang is now more precise with regards to the
+  lifetime of temporary objects such as when aggregates are passed by value to
+  a function, resulting in better sharing of stack slots and reduced stack
+  usage. This change can lead to use-after-scope related issues in code that
+  unintentionally relied on the previous behavior. If recompiling with
+  ``-fsanitize=address`` shows a use-after-scope warning, then this is likely
+  the case, and the report printed should be able to help users pinpoint where
+  the use-after-scope is occurring. Users can use ``-Xclang
+  -sloppy-temporary-lifetimes`` to retain the old behavior until they are able
+  to find and resolve issues in their code.
+
 
 C/C++ Language Potentially Breaking Changes
 ---
diff --git a/clang/include/clang/Basic/CodeGenOptions.def 
b/clang/include/clang/Basic/CodeGenOptions.def
index 6cee3e8acda3c..4999f3eb32d77 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -498,6 +498,10 @@ ENUM_CODEGENOPT(ZeroCallUsedRegs, ZeroCallUsedRegsKind,
 /// non-deleting destructors. (No effect on Microsoft ABI.)
 CODEGENOPT(CtorDtorReturnThis, 1, 0, Benign)
 
+/// Set via -Xclang -sloppy-temporary-lifetimes to disable emission of lifetime
+/// marker intrinsic calls.
+CODEGENOPT(NoLifetimeMarkersForTemporaries, 1, 0, Benign)
+
 /// Enables emitting Import Call sections on supported targets that can be used
 /// by the Windows kernel to enable import call optimization.
 CODEGENOPT(ImportCallOptimization, 1, 0, Benign)
diff --git a/clang/include/clang/Options/Options.td 
b/clang/include/clang/Options/Options.td
index bffb3dfb27485..e5098d1957faa 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -8496,6 +8496,11 @@ def import_call_optimization : Flag<["-"], 
"import-call-optimization">,
 def replaceable_function: Joined<["-"], "loader-replaceable-function=">,
   MarshallingInfoStringVector>;
 
+def sloppy_temporary_lifetimes
+: Flag<["-"], "sloppy-temporary-lifetimes">,
+  HelpText<"Don't emit lifetime markers for temporary objects">,
+  MarshallingInfoFlag>;
+
 } // let Visibility = [CC1Option]
 
 
//===--===//
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index b7b79e7051181..6f09d5a9aadbf 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -5067,7 +5067,30 @@ void CodeGenFunction::EmitCallArg(CallArgList &args, 
const Expr *E,
 return;
   }
 
-  args.add(EmitAnyExprToTemp(E), type);
+  AggValueSlot ArgSlot = AggValueSlot::ignored();
+  // For arguments with aggregate type, create an alloca to store
+  // the value.  If the argument's type has a destructor, that destructor
+  // will run at the en

[clang] [clang-tools-extra] Reapply "[clang] Limit lifetimes of temporaries to the full expression (#170517)" (PR #175816)

2026-04-08 Thread Eli Friedman via cfe-commits

efriedma-quic wrote:

> @efriedma-quic can you do another pass on this, please?

I replied a couple days ago.

https://github.com/llvm/llvm-project/pull/175816
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] Reapply "[clang] Limit lifetimes of temporaries to the full expression (#170517)" (PR #175816)

2026-04-06 Thread Eli Friedman via cfe-commits


@@ -2664,6 +2664,10 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool 
UseGlobal,
 }
   }
 
+  if (OperatorDelete && !OperatorDelete->isReservedGlobalPlacementOperator() &&
+  Initializer && (getLangOpts().Exceptions || getLangOpts().CXXExceptions))

efriedma-quic wrote:

This seems roughly right... but the side-effects seem significant enough that 
we should land it separately.

Do we really need the check for whether exceptions are enabled here?  I'd 
prefer to keep the AST as similar as possible in different modes.

https://github.com/llvm/llvm-project/pull/175816
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] Reapply "[clang] Limit lifetimes of temporaries to the full expression (#170517)" (PR #175816)

2026-04-03 Thread Paul Kirth via cfe-commits

https://github.com/ilovepi edited 
https://github.com/llvm/llvm-project/pull/175816
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits