[PATCH] D149563: [TRE] Do not mark alloca's as escaped if the function only writes to argmem

Joshua Cao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 7 18:25:15 PDT 2023


caojoshua updated this revision to Diff 520231.
caojoshua retitled this revision from "[TRE] Mark alloca's as escaped if their call does not write memory" to "[TRE] Do not mark alloca's as escaped if the function only writes to argmem".
caojoshua edited the summary of this revision.
caojoshua added a comment.

Rework the patch and commit message completey based on discussion. Don't escape allocas that write only to argmems.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149563/new/

https://reviews.llvm.org/D149563

Files:
  llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
  llvm/test/Transforms/TailCallElim/function_readwrite_attrs.ll


Index: llvm/test/Transforms/TailCallElim/function_readwrite_attrs.ll
===================================================================
--- llvm/test/Transforms/TailCallElim/function_readwrite_attrs.ll
+++ llvm/test/Transforms/TailCallElim/function_readwrite_attrs.ll
@@ -62,7 +62,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[X:%.*]] = alloca i32, align 4
 ; CHECK-NEXT:    call void @use_memory_argmem_none(ptr [[X]])
-; CHECK-NEXT:    call void @noarg()
+; CHECK-NEXT:    tail call void @noarg()
 ; CHECK-NEXT:    ret void
 ;
 entry:
@@ -77,7 +77,7 @@
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[X:%.*]] = alloca i32, align 4
 ; CHECK-NEXT:    call void @use_memory_argmem_read(ptr [[X]])
-; CHECK-NEXT:    call void @noarg()
+; CHECK-NEXT:    tail call void @noarg()
 ; CHECK-NEXT:    ret void
 ;
 entry:
Index: llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
+++ llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
@@ -133,14 +133,16 @@
         // beyond the lifetime of the current frame.
         if (CB.isArgOperand(U) && CB.isByValArgument(CB.getArgOperandNo(U)))
           continue;
-        bool IsNocapture =
-            CB.isDataOperand(U) && CB.doesNotCapture(CB.getDataOperandNo(U));
-        callUsesLocalStack(CB, IsNocapture);
-        if (IsNocapture) {
+        AllocaUsers.insert(&CB);
+        if (!CB.isDataOperand(U) || CB.doesNotCapture(CB.getDataOperandNo(U)))
           // If the alloca-derived argument is passed in as nocapture, then it
           // can't propagate to the call's return. That would be capturing.
           continue;
-        }
+        if ((CB.getMemoryEffects().getModRef(MemoryEffects::ArgMem) &
+             ModRefInfo::Mod) != ModRefInfo::NoModRef)
+          // The alloca escapes if the function writes to non argument memory,
+          // such as globals.
+          EscapePoints.insert(&CB);
         break;
       }
       case Instruction::Load: {
@@ -168,19 +170,6 @@
     }
   }
 
-  void callUsesLocalStack(CallBase &CB, bool IsNocapture) {
-    // Add it to the list of alloca users.
-    AllocaUsers.insert(&CB);
-
-    // If it's nocapture then it can't capture this alloca.
-    if (IsNocapture)
-      return;
-
-    // If it can write to memory, it can leak the alloca value.
-    if (!CB.onlyReadsMemory())
-      EscapePoints.insert(&CB);
-  }
-
   SmallPtrSet<Instruction *, 32> AllocaUsers;
   SmallPtrSet<Instruction *, 32> EscapePoints;
 };


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149563.520231.patch
Type: text/x-patch
Size: 2562 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230508/9cbd8fad/attachment.bin>


More information about the llvm-commits mailing list