Skip to content

Commit

Permalink
[SROA] Protect against calling the alloca ptr
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davemgreen committed Dec 17, 2024
1 parent 3508d8f commit 2a7ed2c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/Transforms/SROA/readonlynocapture.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2a7ed2c

Please sign in to comment.