Skip to content

Commit de577af

Browse files
committedMay 14, 2018
[InstCombine] fix crash due to ignored addrspacecast
Summary: Part of the InstCombine code for simplifying GEPs looks through addrspacecasts. However, this was done by updating a variable also used by the next transformation, for marking GEPs as inbounds. This led to replacing a GEP with a similar instruction in a different addrspace, which caused an assertion failure in RAUW. This caused julia issue JuliaLang/julia#27055 Patch by Jeff Bezanson <jeff@juliacomputing.com> Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D46722 llvm-svn: 332302
1 parent 165587b commit de577af

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1937,16 +1937,17 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
19371937
// addrspacecast between types is canonicalized as a bitcast, then an
19381938
// addrspacecast. To take advantage of the below bitcast + struct GEP, look
19391939
// through the addrspacecast.
1940+
Value *ASCStrippedPtrOp = PtrOp;
19401941
if (auto *ASC = dyn_cast<AddrSpaceCastInst>(PtrOp)) {
19411942
// X = bitcast A addrspace(1)* to B addrspace(1)*
19421943
// Y = addrspacecast A addrspace(1)* to B addrspace(2)*
19431944
// Z = gep Y, <...constant indices...>
19441945
// Into an addrspacecasted GEP of the struct.
19451946
if (auto *BC = dyn_cast<BitCastInst>(ASC->getOperand(0)))
1946-
PtrOp = BC;
1947+
ASCStrippedPtrOp = BC;
19471948
}
19481949

1949-
if (auto *BCI = dyn_cast<BitCastInst>(PtrOp)) {
1950+
if (auto *BCI = dyn_cast<BitCastInst>(ASCStrippedPtrOp)) {
19501951
Value *SrcOp = BCI->getOperand(0);
19511952
PointerType *SrcType = cast<PointerType>(BCI->getSrcTy());
19521953
Type *SrcEltType = SrcType->getElementType();

‎llvm/test/Transforms/InstCombine/gep-addrspace.ll

+19
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,22 @@ entry:
3232
ret void
3333
}
3434

35+
declare void @escape_alloca(i16*)
36+
37+
; check that addrspacecast is not ignored (leading to an assertion failure)
38+
; when trying to mark a GEP as inbounds
39+
define { i8, i8 } @inbounds_after_addrspacecast() {
40+
top:
41+
; CHECK-LABEL: @inbounds_after_addrspacecast
42+
%0 = alloca i16, align 2
43+
call void @escape_alloca(i16* %0)
44+
%tmpcast = bitcast i16* %0 to [2 x i8]*
45+
; CHECK: addrspacecast [2 x i8]* %tmpcast to [2 x i8] addrspace(11)*
46+
%1 = addrspacecast [2 x i8]* %tmpcast to [2 x i8] addrspace(11)*
47+
; CHECK: getelementptr [2 x i8], [2 x i8] addrspace(11)* %1, i64 0, i64 1
48+
%2 = getelementptr [2 x i8], [2 x i8] addrspace(11)* %1, i64 0, i64 1
49+
; CHECK: addrspace(11)
50+
%3 = load i8, i8 addrspace(11)* %2, align 1
51+
%.fca.1.insert = insertvalue { i8, i8 } zeroinitializer, i8 %3, 1
52+
ret { i8, i8 } %.fca.1.insert
53+
}

0 commit comments

Comments
 (0)
Please sign in to comment.