Skip to content

Commit

Permalink
Support PHI in LowerAddrSpaceCastPass (#1348)
Browse files Browse the repository at this point in the history
* Support PHI in LowerAddrSpaceCastPass

* Update test/AddressSpaceCast/issue-1341.ll

Co-authored-by: Romaric Jodin <[email protected]>

---------

Co-authored-by: Romaric Jodin <[email protected]>
  • Loading branch information
bmanga and rjodinchr authored May 14, 2024
1 parent ee4e850 commit cfa6067
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/LowerAddrSpaceCastPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,25 @@ Value *clspv::LowerAddrSpaceCastPass::visitPtrToIntInst(PtrToIntInst &I) {
return ptrToInt;
}

Value *clspv::LowerAddrSpaceCastPass::visitPHINode(llvm::PHINode &I) {
IRBuilder<> B(&I);
// NOTE: We assume that the first incoming value does not depend on I.
auto *V1 = visit(I.getIncomingValue(0));
auto *B1 = I.getIncomingBlock(0);

// Register the replacement early in order to avoid recursive calls
// when an incoming value depends on this PHI node.
auto Phi = B.CreatePHI(V1->getType(), I.getNumIncomingValues());
registerReplacement(&I, Phi);

Phi->addIncoming(V1, B1);
for (unsigned i = 1; i < I.getNumIncomingValues(); ++i) {
Phi->addIncoming(visit(I.getIncomingValue(i)), I.getIncomingBlock(i));
}

return Phi;
}

Value *clspv::LowerAddrSpaceCastPass::visitInstruction(Instruction &I) {
#ifndef NDEBUG
dbgs() << "Instruction not handled: " << I << '\n';
Expand Down Expand Up @@ -456,6 +475,11 @@ void clspv::LowerAddrSpaceCastPass::cleanDeadInstructions() {
}
},
[&NextBatch](Instruction *AliveInstruction) {
if (PHINode *Phi = dyn_cast<PHINode>(AliveInstruction)) {
if (RecursivelyDeleteDeadPHINode(Phi)) {
return;
}
}
NextBatch.push_back(AliveInstruction);
});

Expand Down
1 change: 1 addition & 0 deletions lib/LowerAddrSpaceCastPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct LowerAddrSpaceCastPass
llvm::Value *visitCallInst(llvm::CallInst &I);
llvm::Value *visitIntToPtrInst(llvm::IntToPtrInst &I);
llvm::Value *visitPtrToIntInst(llvm::PtrToIntInst &I);
llvm::Value *visitPHINode(llvm::PHINode &I);
llvm::Value *visitInstruction(llvm::Instruction &I);

void runOnFunction(llvm::Function &F);
Expand Down
24 changes: 24 additions & 0 deletions test/AddressSpaceCast/issue-1341.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; RUN: clspv-opt %s -o %t.ll --passes=lower-addrspacecast
; RUN: FileCheck %s < %t.ll

; CHECK: arrayinit.body:
; CHECK-NEXT: {{.*}} = phi ptr [ %{{.*}}, %entry ], [ %{{.*}}, %arrayinit.body ]
; CHECK-NOT: %arrayinit.cur = phi ptr addrspace(4) [ %arrayinit.begin, %entry ], [ %arrayinit.next, %arrayinit.body ]

define dso_local spir_kernel void @fun() {
entry:
%mem = alloca [4 x float]
%data = addrspacecast ptr %mem to ptr addrspace(4)
%arrayinit.begin = getelementptr inbounds [4 x float], ptr addrspace(4) %data, i32 0, i32 0
%arrayinit.end = getelementptr inbounds float, ptr addrspace(4) %arrayinit.begin, i32 4
br label %arrayinit.body

arrayinit.body:
%arrayinit.cur = phi ptr addrspace(4) [ %arrayinit.begin, %entry ], [ %arrayinit.next, %arrayinit.body ]
%arrayinit.next = getelementptr inbounds float, ptr addrspace(4) %arrayinit.cur, i32 1
%arrayinit.done = icmp eq ptr addrspace(4) %arrayinit.next, %arrayinit.end
br i1 %arrayinit.done, label %arrayinit.end2, label %arrayinit.body

arrayinit.end2:
ret void
}

0 comments on commit cfa6067

Please sign in to comment.