Skip to content

Commit e39b419

Browse files
committed
[Attributor][FIX] Honor alloca address space in AAPrivatizablePtr
When we privatize a pointer (~argument promotion) we introduce new private allocas as replacement. These need to be placed in the alloca address space as later passes cannot properly deal with them otherwise. Fixes #53725
1 parent 24b30ec commit e39b419

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
236236
}
237237

238238
// Ensure the result has the requested type.
239-
Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
239+
Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, ResTy,
240+
Ptr->getName() + ".cast");
240241

241242
LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
242243
return Ptr;
@@ -6748,8 +6749,8 @@ struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
67486749

67496750
Type *PrivPtrType = PrivType->getPointerTo();
67506751
if (Base->getType() != PrivPtrType)
6751-
Base = BitCastInst::CreateBitOrPointerCast(Base, PrivPtrType, "",
6752-
ACS.getInstruction());
6752+
Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6753+
Base, PrivPtrType, "", ACS.getInstruction());
67536754

67546755
// Traverse the type, build GEPs and loads.
67556756
if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
@@ -6816,14 +6817,16 @@ struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
68166817
Function &ReplacementFn, Function::arg_iterator ArgIt) {
68176818
BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
68186819
Instruction *IP = &*EntryBB.getFirstInsertionPt();
6819-
Instruction *AI = new AllocaInst(PrivatizableType.getValue(), 0,
6820+
const DataLayout &DL = IP->getModule()->getDataLayout();
6821+
unsigned AS = DL.getAllocaAddrSpace();
6822+
Instruction *AI = new AllocaInst(PrivatizableType.getValue(), AS,
68206823
Arg->getName() + ".priv", IP);
68216824
createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
68226825
ArgIt->getArgNo(), *IP);
68236826

68246827
if (AI->getType() != Arg->getType())
6825-
AI =
6826-
BitCastInst::CreateBitOrPointerCast(AI, Arg->getType(), "", IP);
6828+
AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
6829+
AI, Arg->getType(), "", IP);
68276830
Arg->replaceAllUsesWith(AI);
68286831

68296832
for (CallInst *CI : TailCalls)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
2+
; RUN: opt -attributor -enable-new-pm=0 -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM
3+
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM
4+
; RUN: opt -attributor-cgscc -enable-new-pm=0 -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM
5+
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM
6+
7+
target datalayout = "A7"
8+
9+
; Make sure we create allocas in AS 7 and cast them properly.
10+
11+
define i32 @bar(i32 %arg) {
12+
; IS________OPM-LABEL: define {{[^@]+}}@bar
13+
; IS________OPM-SAME: (i32 [[ARG:%.*]]) {
14+
; IS________OPM-NEXT: entry:
15+
; IS________OPM-NEXT: [[STACK:%.*]] = alloca i32, align 4
16+
; IS________OPM-NEXT: store i32 [[ARG]], i32* [[STACK]], align 4
17+
; IS________OPM-NEXT: [[CALL:%.*]] = call i32 @foo(i32* noalias nocapture nofree noundef nonnull readonly align 4 dereferenceable(4) [[STACK]])
18+
; IS________OPM-NEXT: ret i32 [[CALL]]
19+
;
20+
; IS__TUNIT_NPM-LABEL: define {{[^@]+}}@bar
21+
; IS__TUNIT_NPM-SAME: (i32 [[ARG:%.*]]) {
22+
; IS__TUNIT_NPM-NEXT: entry:
23+
; IS__TUNIT_NPM-NEXT: [[STACK:%.*]] = alloca i32, align 4
24+
; IS__TUNIT_NPM-NEXT: store i32 [[ARG]], i32* [[STACK]], align 4
25+
; IS__TUNIT_NPM-NEXT: [[TMP0:%.*]] = load i32, i32* [[STACK]], align 4
26+
; IS__TUNIT_NPM-NEXT: [[CALL:%.*]] = call i32 @foo(i32 [[TMP0]])
27+
; IS__TUNIT_NPM-NEXT: ret i32 [[CALL]]
28+
;
29+
; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@bar
30+
; IS__CGSCC_NPM-SAME: (i32 returned [[ARG:%.*]]) {
31+
; IS__CGSCC_NPM-NEXT: entry:
32+
; IS__CGSCC_NPM-NEXT: [[STACK:%.*]] = alloca i32, align 4
33+
; IS__CGSCC_NPM-NEXT: store i32 [[ARG]], i32* [[STACK]], align 4
34+
; IS__CGSCC_NPM-NEXT: [[CALL:%.*]] = call i32 @foo(i32 [[ARG]])
35+
; IS__CGSCC_NPM-NEXT: ret i32 [[ARG]]
36+
;
37+
entry:
38+
%stack = alloca i32
39+
store i32 %arg, i32* %stack
40+
%call = call i32 @foo(i32* %stack)
41+
ret i32 %call
42+
}
43+
44+
define internal i32 @foo(i32* %arg) {
45+
; IS________OPM-LABEL: define {{[^@]+}}@foo
46+
; IS________OPM-SAME: (i32* noalias nocapture nofree noundef nonnull readonly align 4 dereferenceable(4) [[ARG:%.*]]) {
47+
; IS________OPM-NEXT: entry:
48+
; IS________OPM-NEXT: [[L:%.*]] = load i32, i32* [[ARG]], align 4
49+
; IS________OPM-NEXT: call void @use(i32 [[L]])
50+
; IS________OPM-NEXT: ret i32 [[L]]
51+
;
52+
; IS__TUNIT_NPM-LABEL: define {{[^@]+}}@foo
53+
; IS__TUNIT_NPM-SAME: (i32 [[TMP0:%.*]]) {
54+
; IS__TUNIT_NPM-NEXT: entry:
55+
; IS__TUNIT_NPM-NEXT: [[ARG_PRIV:%.*]] = alloca i32, align 4, addrspace(7)
56+
; IS__TUNIT_NPM-NEXT: store i32 [[TMP0]], i32 addrspace(7)* [[ARG_PRIV]], align 4
57+
; IS__TUNIT_NPM-NEXT: [[TMP1:%.*]] = addrspacecast i32 addrspace(7)* [[ARG_PRIV]] to i32*
58+
; IS__TUNIT_NPM-NEXT: [[L:%.*]] = load i32, i32* [[TMP1]], align 4
59+
; IS__TUNIT_NPM-NEXT: call void @use(i32 [[L]])
60+
; IS__TUNIT_NPM-NEXT: ret i32 [[L]]
61+
;
62+
; IS__CGSCC_NPM-LABEL: define {{[^@]+}}@foo
63+
; IS__CGSCC_NPM-SAME: (i32 returned [[TMP0:%.*]]) {
64+
; IS__CGSCC_NPM-NEXT: entry:
65+
; IS__CGSCC_NPM-NEXT: [[ARG_PRIV:%.*]] = alloca i32, align 4, addrspace(7)
66+
; IS__CGSCC_NPM-NEXT: store i32 [[TMP0]], i32 addrspace(7)* [[ARG_PRIV]], align 4
67+
; IS__CGSCC_NPM-NEXT: [[TMP1:%.*]] = addrspacecast i32 addrspace(7)* [[ARG_PRIV]] to i32*
68+
; IS__CGSCC_NPM-NEXT: [[L:%.*]] = load i32, i32* [[TMP1]], align 4
69+
; IS__CGSCC_NPM-NEXT: call void @use(i32 [[TMP0]])
70+
; IS__CGSCC_NPM-NEXT: ret i32 [[TMP0]]
71+
;
72+
entry:
73+
%l = load i32, i32* %arg
74+
call void @use(i32 %l)
75+
ret i32 %l
76+
}
77+
78+
declare void @use(i32)

0 commit comments

Comments
 (0)