Skip to content

Commit 68799e7

Browse files
aeubankststellar
authored andcommitted
[GlobalOpt] Don't remove inalloca from varargs functions
Varargs and inalloca have a weird interaction where varargs are actually passed via the inalloca alloca. Removing inalloca breaks the varargs because they're still not passed as separate arguments. Fixes #58718 Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D137182 (cherry picked from commit 8c49b01)
1 parent 11c3a21 commit 68799e7

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ OptimizeFunctions(Module &M,
20032003
// FIXME: We should also hoist alloca affected by this to the entry
20042004
// block if possible.
20052005
if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca) &&
2006-
!F.hasAddressTaken() && !hasMustTailCallers(&F)) {
2006+
!F.hasAddressTaken() && !hasMustTailCallers(&F) && !F.isVarArg()) {
20072007
RemoveAttribute(&F, Attribute::InAlloca);
20082008
Changed = true;
20092009
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature
2+
; RUN: opt -passes=globalopt -S < %s | FileCheck %s
3+
4+
define i32 @main(ptr %a) {
5+
; CHECK-LABEL: define {{[^@]+}}@main
6+
; CHECK-SAME: (ptr [[A:%.*]]) local_unnamed_addr {
7+
; CHECK-NEXT: [[ARGMEM:%.*]] = alloca inalloca <{ ptr, i32 }>, align 4
8+
; CHECK-NEXT: store ptr [[A]], ptr [[ARGMEM]], align 8
9+
; CHECK-NEXT: [[G0:%.*]] = getelementptr inbounds <{ ptr, i32 }>, ptr [[ARGMEM]], i32 0, i32 1
10+
; CHECK-NEXT: store i32 5, ptr [[G0]], align 4
11+
; CHECK-NEXT: [[CALL3:%.*]] = call i32 (ptr, ...) @i(ptr inalloca(ptr) [[ARGMEM]])
12+
; CHECK-NEXT: ret i32 [[CALL3]]
13+
;
14+
%argmem = alloca inalloca <{ ptr, i32 }>, align 4
15+
store ptr %a, ptr %argmem, align 8
16+
%g0 = getelementptr inbounds <{ ptr, i32 }>, ptr %argmem, i32 0, i32 1
17+
store i32 5, ptr %g0, align 4
18+
%call3 = call i32 (ptr, ...) @i(ptr inalloca(ptr) %argmem)
19+
ret i32 %call3
20+
}
21+
22+
define internal i32 @i(ptr inalloca(ptr) %a, ...) {
23+
; CHECK-LABEL: define {{[^@]+}}@i
24+
; CHECK-SAME: (ptr inalloca(ptr) [[A:%.*]], ...) unnamed_addr {
25+
; CHECK-NEXT: [[AP:%.*]] = alloca ptr, align 4
26+
; CHECK-NEXT: call void @llvm.va_start(ptr [[AP]])
27+
; CHECK-NEXT: [[ARGP_CUR:%.*]] = load ptr, ptr [[AP]], align 4
28+
; CHECK-NEXT: [[L:%.*]] = load i32, ptr [[ARGP_CUR]], align 4
29+
; CHECK-NEXT: ret i32 [[L]]
30+
;
31+
%ap = alloca ptr, align 4
32+
call void @llvm.va_start(ptr %ap)
33+
%argp.cur = load ptr, ptr %ap, align 4
34+
%l = load i32, ptr %argp.cur, align 4
35+
ret i32 %l
36+
}
37+
38+
declare void @llvm.va_start(ptr)

0 commit comments

Comments
 (0)