[llvm] 2a7ed2c - [SROA] Protect against calling the alloca ptr

David Green via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 17 01:22:01 PST 2024


Author: David Green
Date: 2024-12-17T09:21:15Z
New Revision: 2a7ed2c1aaf5c84280d947eea56daaf302eb83d1

URL: https://github.com/llvm/llvm-project/commit/2a7ed2c1aaf5c84280d947eea56daaf302eb83d1
DIFF: https://github.com/llvm/llvm-project/commit/2a7ed2c1aaf5c84280d947eea56daaf302eb83d1.diff

LOG: [SROA] Protect against calling the alloca ptr

In case we are calling the alloca ptr directly, check that the Use is a normal
operand to the call. Fortran is a funny language.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/SROA.cpp
    llvm/test/Transforms/SROA/readonlynocapture.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 3e46f0d35ee057..f6179cadab4254 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -1399,7 +1399,8 @@ class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
   void visitCallBase(CallBase &CB) {
     // If the call operand is NoCapture ReadOnly, then we mark it as
     // EscapedReadOnly.
-    if (CB.doesNotCapture(U->getOperandNo()) &&
+    if (CB.isDataOperand(U) &&
+        CB.doesNotCapture(U->getOperandNo()) &&
         CB.onlyReadsMemory(U->getOperandNo())) {
       PI.setEscapedReadOnly(&CB);
       return;

diff  --git a/llvm/test/Transforms/SROA/readonlynocapture.ll b/llvm/test/Transforms/SROA/readonlynocapture.ll
index 9c12a4b1a4909e..2c21624f3ea51a 100644
--- a/llvm/test/Transforms/SROA/readonlynocapture.ll
+++ b/llvm/test/Transforms/SROA/readonlynocapture.ll
@@ -375,4 +375,19 @@ define i32 @sixteenload() {
   ret i32 %a2
 }
 
+define i32 @testcallalloca() {
+; CHECK-LABEL: @testcallalloca(
+; CHECK-NEXT:    [[A:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    store i32 0, ptr [[A]], align 4
+; CHECK-NEXT:    call void [[A]]()
+; CHECK-NEXT:    [[L1:%.*]] = load i32, ptr [[A]], align 4
+; CHECK-NEXT:    ret i32 [[L1]]
+;
+  %a = alloca i32
+  store i32 0, ptr %a
+  call void %a()
+  %l1 = load i32, ptr %a
+  ret i32 %l1
+}
+
 declare void @llvm.memcpy.p0.p0.i64(ptr, ptr, i64, i1)


        


More information about the llvm-commits mailing list