-
Notifications
You must be signed in to change notification settings - Fork 15k
[InferAddressSpaces] Fix bad addrspacecast insertion for phinode
#163528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The IR verifier will carsh if there is any instructions localed before phi-node. The infer-address-spaces pass would like to insert addrspacecast before phi-node in some corner cases. Indeed, since the operand pointer(phi-node's incoming value) has been determined to NewAS by the pass, it is safe to addrspacecast it immediately after the position where defined it.
|
Fix issue: #162982 |
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-llvm-transforms @llvm/pr-subscribers-backend-nvptx Author: Kerang Mao (Kerang-BR) ChangesThe IR verifier will carsh if there is any instructions located before phi-node. The Full diff: https://github.com/llvm/llvm-project/pull/163528.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
index 3ad87545953ff..352a1b331001a 100644
--- a/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
+++ b/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp
@@ -617,6 +617,41 @@ InferAddressSpacesImpl::collectFlatAddressExpressions(Function &F) const {
return Postorder;
}
+// Inserts an addrspacecast for a phi node operand, handling the proper
+// insertion position based on the operand type.
+static Value *phiNodeOperandWithNewAddressSpace(AddrSpaceCastInst *NewI,
+ Value *Operand) {
+ auto InsertBefore = [NewI](auto It) {
+ NewI->insertBefore(It);
+ NewI->setDebugLoc(It->getDebugLoc());
+ return NewI;
+ };
+
+ if (auto *Arg = dyn_cast<Argument>(Operand)) {
+ // For arguments, insert the cast at the beginning of entry block.
+ // Consider inserting at the dominating block for better placement.
+ Function *F = Arg->getParent();
+ auto InsertI = F->getEntryBlock().getFirstNonPHIIt();
+ return InsertBefore(InsertI);
+ }
+
+ // No check for Constant here, as constants are already handled.
+ assert(isa<Instruction>(Operand));
+
+ Instruction *OpInst = cast<Instruction>(Operand);
+ if (LLVM_UNLIKELY(OpInst->getOpcode() == Instruction::PHI)) {
+ // If the operand is defined by another PHI node, insert after the first
+ // non-PHI instruction at the corresponding basic block.
+ auto InsertI = OpInst->getParent()->getFirstNonPHIIt();
+ return InsertBefore(InsertI);
+ }
+
+ // Otherwise, insert immediately after the operand definition.
+ NewI->insertAfter(OpInst->getIterator());
+ NewI->setDebugLoc(OpInst->getDebugLoc());
+ return NewI;
+}
+
// A helper function for cloneInstructionWithNewAddressSpace. Returns the clone
// of OperandUse.get() in the new address space. If the clone is not ready yet,
// returns poison in the new address space as a placeholder.
@@ -642,6 +677,10 @@ static Value *operandWithNewAddressSpaceOrCreatePoison(
unsigned NewAS = I->second;
Type *NewPtrTy = getPtrOrVecOfPtrsWithNewAS(Operand->getType(), NewAS);
auto *NewI = new AddrSpaceCastInst(Operand, NewPtrTy);
+
+ if (LLVM_UNLIKELY(Inst->getOpcode() == Instruction::PHI))
+ return phiNodeOperandWithNewAddressSpace(NewI, Operand);
+
NewI->insertBefore(Inst->getIterator());
NewI->setDebugLoc(Inst->getDebugLoc());
return NewI;
diff --git a/llvm/test/Transforms/InferAddressSpaces/NVPTX/phinode-address-infer.ll b/llvm/test/Transforms/InferAddressSpaces/NVPTX/phinode-address-infer.ll
new file mode 100644
index 0000000000000..e5c52cfc0d269
--- /dev/null
+++ b/llvm/test/Transforms/InferAddressSpaces/NVPTX/phinode-address-infer.ll
@@ -0,0 +1,57 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes='require<domtree>,infer-address-spaces' %s | FileCheck %s
+
+;;; Handle write corner case for infer-address-spaces with phi-nodes. The
+;;; verifier will crash if we insert `addrspacecast` before phi-node.
+
+target triple = "nvptx64-nvidia-cuda"
+
+declare void @llvm.assume(i1 noundef)
+declare i1 @llvm.nvvm.isspacep.shared(ptr) readnone noinline
+declare i1 @llvm.nvvm.isspacep.global(ptr) readnone noinline
+
+define ptr @phinode_instr() {
+; CHECK-LABEL: @phinode_instr(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[PTR_1:%.*]] = load ptr, ptr null, align 8
+; CHECK-NEXT: [[TMP0:%.*]] = addrspacecast ptr [[PTR_1]] to ptr addrspace(3)
+; CHECK-NEXT: [[BOOL_1:%.*]] = tail call i1 @llvm.nvvm.isspacep.shared(ptr [[PTR_1]])
+; CHECK-NEXT: tail call void @llvm.assume(i1 [[BOOL_1]])
+; CHECK-NEXT: br label [[IF_SINK_SPLIT:%.*]]
+; CHECK: if.sink.split:
+; CHECK-NEXT: [[PTR_SINK:%.*]] = phi ptr addrspace(3) [ [[TMP0]], [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr addrspace(3) [[PTR_SINK]] to ptr
+; CHECK-NEXT: ret ptr [[TMP1]]
+;
+entry:
+ %ptr.1 = load ptr, ptr null, align 8
+ %bool.1 = tail call i1 @llvm.nvvm.isspacep.shared(ptr %ptr.1)
+ tail call void @llvm.assume(i1 %bool.1)
+ br label %if.sink.split
+
+if.sink.split: ; preds = %entry
+ %ptr.sink = phi ptr [ %ptr.1, %entry ]
+ ret ptr %ptr.sink
+}
+
+define ptr @phinode_argument(ptr %lhs_ptr) {
+; CHECK-LABEL: @phinode_argument(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP0:%.*]] = addrspacecast ptr [[LHS_PTR:%.*]] to ptr addrspace(1)
+; CHECK-NEXT: [[BOOL_1:%.*]] = tail call i1 @llvm.nvvm.isspacep.global(ptr [[LHS_PTR]])
+; CHECK-NEXT: tail call void @llvm.assume(i1 [[BOOL_1]])
+; CHECK-NEXT: br label [[IF_SINK_SPLIT:%.*]]
+; CHECK: if.sink.split:
+; CHECK-NEXT: [[PTR_SINK:%.*]] = phi ptr addrspace(1) [ [[TMP0]], [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[TMP1:%.*]] = addrspacecast ptr addrspace(1) [[PTR_SINK]] to ptr
+; CHECK-NEXT: ret ptr [[TMP1]]
+;
+entry:
+ %bool.1 = tail call i1 @llvm.nvvm.isspacep.global(ptr %lhs_ptr)
+ tail call void @llvm.assume(i1 %bool.1)
+ br label %if.sink.split
+
+if.sink.split: ; preds = %entry
+ %ptr.sink = phi ptr [ %lhs_ptr, %entry ]
+ ret ptr %ptr.sink
+}
|
| return NewI; | ||
| }; | ||
|
|
||
| if (auto *Arg = dyn_cast<Argument>(Operand)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be simpler to just insert the new instruction before the terminator instruction in the incoming BB, so that we don't care the Operand is Argument, Constant, Phi or Instruction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is quite simple to insert new cast instructions in every incoming BB, but on the other hand, it seems a bit redundant.
The preferred insertion point for new cast instructions is the dominator BB. However, this approach would require significant refactoring of the codebase.
|
Ping |
The IR verifier will carsh if there is any instructions located before phi-node. The
infer-address-spacespass would like to insertaddrspacecastbefore phi-node in some corner cases. Indeed, since the operand pointer(phi-node's incoming value) has been determined toNewASby the pass, it is safe toaddrspacecastit immediately after the position where defined it.