From b988d69ea2864a1bdf9789f930ecf7f41d109653 Mon Sep 17 00:00:00 2001 From: Artem Belevich Date: Thu, 15 Jul 2021 13:39:47 -0700 Subject: [PATCH] [infer-address-spaces] Handle complex non-pointer constexpr arguments. Fixes https://bugs.llvm.org/show_bug.cgi?id=51099 Differential Revision: https://reviews.llvm.org/D106098 --- .../Transforms/Scalar/InferAddressSpaces.cpp | 6 ++- .../NVPTX/clone_constexpr.ll | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp index 63b8425f2d015c..f7d631f5e7851e 100644 --- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp +++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp @@ -635,8 +635,10 @@ static Value *cloneConstantExprWithNewAddressSpace( ConstantExpr *CE, unsigned NewAddrSpace, const ValueToValueMapTy &ValueWithNewAddrSpace, const DataLayout *DL, const TargetTransformInfo *TTI) { - Type *TargetType = PointerType::getWithSamePointeeType( - cast(CE->getType()), NewAddrSpace); + Type *TargetType = CE->getType()->isPointerTy() + ? PointerType::getWithSamePointeeType( + cast(CE->getType()), NewAddrSpace) + : CE->getType(); if (CE->getOpcode() == Instruction::AddrSpaceCast) { // Because CE is flat, the source address space must be specific. diff --git a/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll b/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll index 53dff196d32e9a..07593f19a4399e 100644 --- a/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll +++ b/llvm/test/Transforms/InferAddressSpaces/NVPTX/clone_constexpr.ll @@ -31,6 +31,51 @@ declare void @f1(i32*, i32) local_unnamed_addr #0 declare void @f2(i64*, i64) local_unnamed_addr #0 declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() #1 +; Make sure we can clone GEP which uses complex constant expressions as indices. +; https://bugs.llvm.org/show_bug.cgi?id=51099 +@g2 = internal addrspace(3) global [128 x i8] undef, align 1 + +; CHECK-LABEL: @complex_ce( +; CHECK: %0 = load float, float addrspace(3)* bitcast +; CHECK-SAME: i8 addrspace(3)* getelementptr (i8, +; CHECK-SAME: i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 0, i64 0), +; CHECK-SAME: i64 sub ( +; CHECK-SAME i64 ptrtoint ( +; CHECK-SAME i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 0, i64 123) to i64), +; CHECK-SAME: i64 ptrtoint ( +; CHECK-SAME: i8 addrspace(3)* getelementptr inbounds ([128 x i8], [128 x i8] addrspace(3)* @g2, i64 2, i64 0) to i64))) +; CHECK-SAME: to float addrspace(3)*) +; Function Attrs: norecurse nounwind +define float @complex_ce(i8* nocapture readnone %a, i8* nocapture readnone %b, i8* nocapture readnone %c) local_unnamed_addr #0 { +entry: + %0 = load float, float* bitcast ( + i8* getelementptr ( + i8, i8* getelementptr inbounds ( + [128 x i8], + [128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*), + i64 0, + i64 0), + i64 sub ( + i64 ptrtoint ( + i8* getelementptr inbounds ( + [128 x i8], + [128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*), + i64 0, + i64 123) + to i64), + i64 ptrtoint ( + i8* getelementptr inbounds ( + [128 x i8], + [128 x i8]* addrspacecast ([128 x i8] addrspace(3)* @g2 to [128 x i8]*), + i64 2, + i64 0) + to i64))) + to float*), align 4 + ret float %0 +} + + + attributes #0 = { convergent nounwind } attributes #1 = { nounwind readnone } attributes #2 = { nounwind }