From de577af8c0617df6bb707ac0a7ebccbf77d7cb6e Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Mon, 14 May 2018 22:05:01 +0000 Subject: [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 https://github.com/JuliaLang/julia/issues/27055 Patch by Jeff Bezanson Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D46722 llvm-svn: 332302 --- .../Transforms/InstCombine/InstructionCombining.cpp | 5 +++-- llvm/test/Transforms/InstCombine/gep-addrspace.ll | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 2549257bc97..e0a9ec21621 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -1937,16 +1937,17 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // addrspacecast between types is canonicalized as a bitcast, then an // addrspacecast. To take advantage of the below bitcast + struct GEP, look // through the addrspacecast. + Value *ASCStrippedPtrOp = PtrOp; if (auto *ASC = dyn_cast(PtrOp)) { // X = bitcast A addrspace(1)* to B addrspace(1)* // Y = addrspacecast A addrspace(1)* to B addrspace(2)* // Z = gep Y, <...constant indices...> // Into an addrspacecasted GEP of the struct. if (auto *BC = dyn_cast(ASC->getOperand(0))) - PtrOp = BC; + ASCStrippedPtrOp = BC; } - if (auto *BCI = dyn_cast(PtrOp)) { + if (auto *BCI = dyn_cast(ASCStrippedPtrOp)) { Value *SrcOp = BCI->getOperand(0); PointerType *SrcType = cast(BCI->getSrcTy()); Type *SrcEltType = SrcType->getElementType(); diff --git a/llvm/test/Transforms/InstCombine/gep-addrspace.ll b/llvm/test/Transforms/InstCombine/gep-addrspace.ll index aa46ea67130..4a4951dee7f 100644 --- a/llvm/test/Transforms/InstCombine/gep-addrspace.ll +++ b/llvm/test/Transforms/InstCombine/gep-addrspace.ll @@ -32,3 +32,22 @@ entry: ret void } +declare void @escape_alloca(i16*) + +; check that addrspacecast is not ignored (leading to an assertion failure) +; when trying to mark a GEP as inbounds +define { i8, i8 } @inbounds_after_addrspacecast() { +top: +; CHECK-LABEL: @inbounds_after_addrspacecast + %0 = alloca i16, align 2 + call void @escape_alloca(i16* %0) + %tmpcast = bitcast i16* %0 to [2 x i8]* +; CHECK: addrspacecast [2 x i8]* %tmpcast to [2 x i8] addrspace(11)* + %1 = addrspacecast [2 x i8]* %tmpcast to [2 x i8] addrspace(11)* +; CHECK: getelementptr [2 x i8], [2 x i8] addrspace(11)* %1, i64 0, i64 1 + %2 = getelementptr [2 x i8], [2 x i8] addrspace(11)* %1, i64 0, i64 1 +; CHECK: addrspace(11) + %3 = load i8, i8 addrspace(11)* %2, align 1 + %.fca.1.insert = insertvalue { i8, i8 } zeroinitializer, i8 %3, 1 + ret { i8, i8 } %.fca.1.insert +} -- cgit v1.2.3