From 3769d6fffbfaef97d8de87849e6bde6a46434a4a Mon Sep 17 00:00:00 2001 From: Romaric Jodin <89833130+rjodinchr@users.noreply.github.com> Date: Wed, 30 Aug 2023 16:54:08 +0200 Subject: [PATCH] add support for ptrtoint in LowerPrivatePointerPHI (#1210) also avoid accessing nullptr in BitcastUtils::IsImplicitCast Ref #1209 --- lib/BitcastUtils.cpp | 10 ++++++---- lib/LowerPrivatePointerPHIPass.cpp | 8 ++++++++ test/PrivatePointerPHI/ptrtoint.ll | 32 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 test/PrivatePointerPHI/ptrtoint.ll diff --git a/lib/BitcastUtils.cpp b/lib/BitcastUtils.cpp index fee7ce512..a2761e1cf 100644 --- a/lib/BitcastUtils.cpp +++ b/lib/BitcastUtils.cpp @@ -882,13 +882,15 @@ bool IsImplicitCasts(Module &M, DenseMap &type_cache, for (unsigned i = 1; i < phi->getNumIncomingValues(); i++) { auto Op = phi->getIncomingValue(i); auto OpTy = clspv::InferType(Op, M.getContext(), &type_cache); - if (SizeInBits(M.getDataLayout(), OpTy) > - SizeInBits(M.getDataLayout(), RefOpTy)) { + if (OpTy && RefOpTy && + SizeInBits(M.getDataLayout(), OpTy) > + SizeInBits(M.getDataLayout(), RefOpTy)) { source = Op; source_ty = OpTy; dest_ty = RefOpTy; - } else if (SizeInBits(M.getDataLayout(), OpTy) < - SizeInBits(M.getDataLayout(), RefOpTy)) { + } else if (OpTy && RefOpTy && + SizeInBits(M.getDataLayout(), OpTy) < + SizeInBits(M.getDataLayout(), RefOpTy)) { source = RefOp; source_ty = RefOpTy; dest_ty = OpTy; diff --git a/lib/LowerPrivatePointerPHIPass.cpp b/lib/LowerPrivatePointerPHIPass.cpp index 3d652b93c..821eaf774 100644 --- a/lib/LowerPrivatePointerPHIPass.cpp +++ b/lib/LowerPrivatePointerPHIPass.cpp @@ -191,6 +191,14 @@ void clspv::LowerPrivatePointerPHIPass::runOnFunction(Function &F) { store->getType(), CstVal, DynVal, SmallerBitWidths); B.CreateStore(store->getValueOperand(), gep); ToBeErased.push_back(store); + } else if (auto ptrtoint = dyn_cast(node)) { + IRBuilder<> B(ptrtoint); + auto gep = makeNewGEP(DL, B, alloca, alloca->getAllocatedType(), + B.getIntNTy(SmallerBitWidths), CstVal, DynVal, + SmallerBitWidths); + auto newPtrToInt = B.CreatePtrToInt(gep, ptrtoint->getDestTy()); + ptrtoint->replaceAllUsesWith(newPtrToInt); + ToBeErased.push_back(ptrtoint); } else { llvm_unreachable("Unexpected node when traversing alloca users"); } diff --git a/test/PrivatePointerPHI/ptrtoint.ll b/test/PrivatePointerPHI/ptrtoint.ll new file mode 100644 index 000000000..93b52af89 --- /dev/null +++ b/test/PrivatePointerPHI/ptrtoint.ll @@ -0,0 +1,32 @@ +; RUN: clspv-opt %s -o %t.ll --passes=lower-private-pointer-phi +; RUN: FileCheck %s < %t.ll + +; CHECK: loop: +; CHECK-NEXT: [[phi:%[^ ]+]] = phi i32 [ 0, %entry ], [ [[add:%[^ ]+]], %loop ] +; CHECK: [[gep:%[^ ]+]] = getelementptr inbounds [64 x i32], ptr %alloca, i32 0, i32 [[phi]] +; CHECK: load i32, ptr [[gep]], align 4 +; CHECK: [[add]] = add i32 1, [[phi]] +; CHECK: [[gep:%[^ ]+]] = getelementptr inbounds [64 x i32], ptr %alloca, i32 0, i32 [[phi]] +; CHECK: ptrtoint ptr [[gep]] to i32 + +target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" +target triple = "spir-unknown-unknown" + +define void @test() { +entry: + %alloca = alloca [64 x i32] + br label %loop + +loop: + %phi = phi ptr [ %alloca, %entry ], [ %gep, %loop ] + %count = phi i32 [ 0, %entry ], [ %next, %loop ] + %load = load i32, ptr %phi + %gep = getelementptr i32, ptr %phi, i32 1 + %next = add i32 %count, 1 + %cmp = icmp eq i32 %next, 64 + %ptrtoint = ptrtoint ptr %phi to i32 + br i1 %cmp, label %exit, label %loop + +exit: + ret void +}