Skip to content

Commit e47359a

Browse files
authored
Inline: Fix handling of byval using non-alloca addrspace (llvm#97306)
Use the address space of the original pointer argument instead of querying the datalayout. This avoids producing a verifier error since this was changing the address space for the user instructions. Fixes llvm#97086
1 parent 95e823e commit e47359a

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,8 +1674,9 @@ static Value *HandleByValArgument(Type *ByValType, Value *Arg,
16741674
if (ByValAlignment)
16751675
Alignment = std::max(Alignment, *ByValAlignment);
16761676

1677-
AllocaInst *NewAlloca = new AllocaInst(ByValType, DL.getAllocaAddrSpace(),
1678-
nullptr, Alignment, Arg->getName());
1677+
AllocaInst *NewAlloca =
1678+
new AllocaInst(ByValType, Arg->getType()->getPointerAddressSpace(),
1679+
nullptr, Alignment, Arg->getName());
16791680
NewAlloca->insertBefore(Caller->begin()->begin());
16801681
IFI.StaticAllocas.push_back(NewAlloca);
16811682

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: --version 5
2+
; RUN: opt -S -passes=always-inline %s | FileCheck %s
3+
4+
; The byval parameters use a different address space from the alloca
5+
; address space. Make sure this is gracefully handled by using the
6+
; original byval pointer type's address space instead of what the
7+
; datalayout says to use.
8+
9+
target datalayout = "A5"
10+
11+
%struct = type { i64, i64 }
12+
13+
define i64 @bar(ptr byval(%struct) %a) alwaysinline {
14+
; CHECK-LABEL: define i64 @bar(
15+
; CHECK-SAME: ptr byval([[STRUCT:%.*]]) [[A:%.*]]) #[[ATTR0:[0-9]+]] {
16+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [[STRUCT]], ptr [[A]], i64 0, i32 1
17+
; CHECK-NEXT: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
18+
; CHECK-NEXT: ret i64 0
19+
;
20+
%1 = getelementptr %struct, ptr %a, i64 0, i32 1
21+
%2 = load i64, ptr %1, align 4
22+
ret i64 0
23+
}
24+
25+
define i64 @foo(ptr %arg) {
26+
; CHECK-LABEL: define i64 @foo(
27+
; CHECK-SAME: ptr [[ARG:%.*]]) {
28+
; CHECK-NEXT: [[ARG1:%.*]] = alloca [[STRUCT:%.*]], align 8
29+
; CHECK-NEXT: call void @llvm.lifetime.start.p0(i64 16, ptr [[ARG1]])
30+
; CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 [[ARG1]], ptr align 1 [[ARG]], i64 16, i1 false)
31+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr [[STRUCT]], ptr [[ARG1]], i64 0, i32 1
32+
; CHECK-NEXT: [[TMP2:%.*]] = load i64, ptr [[TMP1]], align 4
33+
; CHECK-NEXT: call void @llvm.lifetime.end.p0(i64 16, ptr [[ARG1]])
34+
; CHECK-NEXT: ret i64 0
35+
;
36+
%1 = call i64 @bar(ptr byval(%struct) align 8 %arg)
37+
ret i64 0
38+
}

0 commit comments

Comments
 (0)