Skip to content

Commit 4f7f70e

Browse files
committed
Recommit [SROA] Enhance SROA to handle addrspacecasted allocas
[SROA] Enhance SROA to handle `addrspacecast`ed allocas - Fix typo in original change - Add additional handling to ensure all return pointers are properly casted. Summary: - After `addrspacecast` is allowed to be eliminated in SROA, the adjusting of storage pointer (from `alloca) needs to handle the potential different address spaces between the storage pointer (from alloca) and the pointer being used. Reviewers: arsenm Subscribers: wdng, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63501 llvm-svn: 363743
1 parent 7bfb439 commit 4f7f70e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,12 @@ static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
15891589
PointerType *TargetPtrTy = cast<PointerType>(PointerTy);
15901590
Type *TargetTy = TargetPtrTy->getElementType();
15911591

1592+
// As `addrspacecast` is , `Ptr` (the storage pointer) may have different
1593+
// address space from the expected `PointerTy` (the pointer to be used).
1594+
// Adjust the pointer type based the original storage pointer.
1595+
auto AS = cast<PointerType>(Ptr->getType())->getAddressSpace();
1596+
PointerTy = TargetTy->getPointerTo(AS);
1597+
15921598
do {
15931599
// First fold any existing GEPs into the offset.
15941600
while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
@@ -1617,7 +1623,7 @@ static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
16171623
OffsetBasePtr = Ptr;
16181624
// If we also found a pointer of the right type, we're done.
16191625
if (P->getType() == PointerTy)
1620-
return P;
1626+
break;
16211627
}
16221628

16231629
// Stash this pointer if we've found an i8*.

llvm/test/Transforms/SROA/addrspacecast.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ define void @select_addrspacecast_gv(i1 %a, i1 %b) {
299299
ret void
300300
}
301301

302+
; CHECK-LABEL: @select_addrspacecast_i8(
303+
; CHECK: [[SEL:%.*]] = select i1 undef, i8 undef, i8 undef
304+
; CHECK-NEXT: ret i8 [[SEL]]
305+
define i8 @select_addrspacecast_i8() {
306+
%a = alloca i8
307+
%b = alloca i8
308+
309+
%a.ptr = addrspacecast i8* %a to i8 addrspace(1)*
310+
%b.ptr = addrspacecast i8* %b to i8 addrspace(1)*
311+
312+
%ptr = select i1 undef, i8 addrspace(1)* %a.ptr, i8 addrspace(1)* %b.ptr
313+
%ret = load i8, i8 addrspace(1)* %ptr
314+
ret i8 %ret
315+
}
316+
302317
!0 = !{!1, !1, i64 0, i64 1}
303318
!1 = !{!2, i64 1, !"type_0"}
304319
!2 = !{!"root"}

0 commit comments

Comments
 (0)