|
| 1 | +From 5eeab81d22e07b6e12821067fced590f534c251a Mon Sep 17 00:00:00 2001 |
| 2 | +From: Keno Fischer < [email protected]> |
| 3 | +Date: Thu, 27 Apr 2017 14:33:33 -0400 |
| 4 | +Subject: [PATCH] [SROA] Fix crash due to bad bitcast |
| 5 | + |
| 6 | +Summary: |
| 7 | +As shown in the test case, SROA was crashing when trying to split |
| 8 | +stores (to the alloca) of loads (from anywhere), because it assumed |
| 9 | +the pointer operand to the loads and stores had to have the same |
| 10 | +address space. This isn't the case. Make sure to use the correct |
| 11 | +pointer type for both the load and the store. |
| 12 | + |
| 13 | +Reviewers: chandlerc, majnemer, sanjoy |
| 14 | + |
| 15 | +Subscribers: arsenm, llvm-commits |
| 16 | + |
| 17 | +Differential Revision: https://reviews.llvm.org/D32593 |
| 18 | +--- |
| 19 | + lib/Transforms/Scalar/SROA.cpp | 7 ++++--- |
| 20 | + test/Transforms/SROA/address-spaces.ll | 18 ++++++++++++++++++ |
| 21 | + 2 files changed, 22 insertions(+), 3 deletions(-) |
| 22 | + |
| 23 | +diff --git a/lib/Transforms/Scalar/SROA.cpp b/lib/Transforms/Scalar/SROA.cpp |
| 24 | +index d01e91a..610d5a8 100644 |
| 25 | +--- a/lib/Transforms/Scalar/SROA.cpp |
| 26 | ++++ b/lib/Transforms/Scalar/SROA.cpp |
| 27 | +@@ -3697,7 +3697,8 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { |
| 28 | + int Idx = 0, Size = Offsets.Splits.size(); |
| 29 | + for (;;) { |
| 30 | + auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8); |
| 31 | +- auto *PartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace()); |
| 32 | ++ auto *LoadPartPtrTy = PartTy->getPointerTo(LI->getPointerAddressSpace()); |
| 33 | ++ auto *StorePartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace()); |
| 34 | + |
| 35 | + // Either lookup a split load or create one. |
| 36 | + LoadInst *PLoad; |
| 37 | +@@ -3708,7 +3709,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { |
| 38 | + PLoad = IRB.CreateAlignedLoad( |
| 39 | + getAdjustedPtr(IRB, DL, LoadBasePtr, |
| 40 | + APInt(DL.getPointerSizeInBits(), PartOffset), |
| 41 | +- PartPtrTy, LoadBasePtr->getName() + "."), |
| 42 | ++ LoadPartPtrTy, LoadBasePtr->getName() + "."), |
| 43 | + getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false, |
| 44 | + LI->getName()); |
| 45 | + } |
| 46 | +@@ -3718,7 +3719,7 @@ bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { |
| 47 | + StoreInst *PStore = IRB.CreateAlignedStore( |
| 48 | + PLoad, getAdjustedPtr(IRB, DL, StoreBasePtr, |
| 49 | + APInt(DL.getPointerSizeInBits(), PartOffset), |
| 50 | +- PartPtrTy, StoreBasePtr->getName() + "."), |
| 51 | ++ StorePartPtrTy, StoreBasePtr->getName() + "."), |
| 52 | + getAdjustedAlignment(SI, PartOffset, DL), /*IsVolatile*/ false); |
| 53 | + |
| 54 | + // Now build a new slice for the alloca. |
| 55 | +diff --git a/test/Transforms/SROA/address-spaces.ll b/test/Transforms/SROA/address-spaces.ll |
| 56 | +index 119f225..8fba30c 100644 |
| 57 | +--- a/test/Transforms/SROA/address-spaces.ll |
| 58 | ++++ b/test/Transforms/SROA/address-spaces.ll |
| 59 | +@@ -83,3 +83,21 @@ define void @pr27557() { |
| 60 | + store i32 addrspace(3)* @l, i32 addrspace(3)** %3, align 8 |
| 61 | + ret void |
| 62 | + } |
| 63 | ++ |
| 64 | ++; Make sure pre-splitting doesn't try to introduce an illegal bitcast |
| 65 | ++define float @presplit(i64 addrspace(1)* %p) { |
| 66 | ++entry: |
| 67 | ++; CHECK-LABEL: @presplit( |
| 68 | ++; CHECK: %[[CAST:.*]] = bitcast i64 addrspace(1)* {{.*}} to i32 addrspace(1)* |
| 69 | ++; CHECK: load i32, i32 addrspace(1)* %[[CAST]] |
| 70 | ++ %b = alloca i64 |
| 71 | ++ %b.cast = bitcast i64* %b to [2 x float]* |
| 72 | ++ %b.gep1 = getelementptr [2 x float], [2 x float]* %b.cast, i32 0, i32 0 |
| 73 | ++ %b.gep2 = getelementptr [2 x float], [2 x float]* %b.cast, i32 0, i32 1 |
| 74 | ++ %l = load i64, i64 addrspace(1)* %p |
| 75 | ++ store i64 %l, i64* %b |
| 76 | ++ %f1 = load float, float* %b.gep1 |
| 77 | ++ %f2 = load float, float* %b.gep2 |
| 78 | ++ %ret = fadd float %f1, %f2 |
| 79 | ++ ret float %ret |
| 80 | ++} |
| 81 | +-- |
| 82 | +2.9.3 |
| 83 | + |
0 commit comments