[llvm] [CodeGen][WinEH] Update saved esp for inlined inallocas (PR #116585)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 19 09:42:46 PST 2024


================
@@ -774,3 +778,27 @@ void WinEHStatePass::insertStateNumberStore(Instruction *IP, int State) {
                                               RegNode, StateFieldIndex);
   Builder.CreateStore(Builder.getInt32(State), StateField);
 }
+
+void WinEHStatePass::updateEspForInAllocas(Function &F) {
+  for (BasicBlock &BB : F) {
+    for (Instruction &I : BB) {
+      if (auto *Alloca = dyn_cast<AllocaInst>(&I)) {
+        if (!Alloca->isUsedWithInAlloca())
----------------
rnk wrote:

> But what is more problematic in my opinion is that GlobalOpt does strip the inalloca attribute of argmem inside the CallBase but doesnt strip the attribute for the alloca.

That is a good point!

I would lean away from messing with lifetime attribute placement. There are many aspects of exception handling and optimizations that make it hard to use these reliably.

A low-cost idea that comes to mind is to have a local pass that looks for inalloca allocas that are not used in conjunction with an inalloca call and promote them to static allocas. There are many passes beyond inlining that might delete the consuming inalloca call, so there are good reasons to do this separately from GlobalOpt. However, inalloca allocas are often captured by constructors and destructors, which is why we're using inalloca in the first place, and that makes it hard to prove the alloca is not used later by an inalloca call.

I previously attempted to design a more complete solution to this problem with the `preallocated` attribute which uses tokens to make it possible to statically identify which calls use the argument memory, but the reality is that it's not a priority for my employer, so I wasn't able to get this work prioritized.

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


More information about the llvm-commits mailing list