Skip to content

[SPIR-V] Ensure that internal intrinsic functions "ptrcast" for PHI's operand are inserted at the correct positions #92536

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

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,17 +489,15 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(Instruction *I) {
Type *Ty = GR->findDeducedElementType(Op);
if (Ty == KnownElemTy)
continue;
if (Instruction *User = dyn_cast<Instruction>(Op->use_begin()->get()))
setInsertPointSkippingPhis(B, User->getNextNode());
else
setInsertPointSkippingPhis(B, I);
Value *OpTyVal = Constant::getNullValue(KnownElemTy);
Type *OpTy = Op->getType();
if (!Ty) {
GR->addDeducedElementType(Op, KnownElemTy);
// check if there is existing Intrinsic::spv_assign_ptr_type instruction
auto It = AssignPtrTypeInstr.find(Op);
if (It == AssignPtrTypeInstr.end()) {
Instruction *User = dyn_cast<Instruction>(Op->use_begin()->get());
setInsertPointSkippingPhis(B, User ? User->getNextNode() : I);
CallInst *CI =
buildIntrWithMD(Intrinsic::spv_assign_ptr_type, {OpTy}, OpTyVal, Op,
{B.getInt32(getPointerAddressSpace(OpTy))}, B);
Expand All @@ -511,6 +509,17 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(Instruction *I) {
Ctx, MDNode::get(Ctx, ValueAsMetadata::getConstant(OpTyVal))));
}
} else {
if (auto *OpI = dyn_cast<Instruction>(Op)) {
// spv_ptrcast's argument Op denotes an instruction that generates
// a value, and we may use getInsertionPointAfterDef()
B.SetInsertPoint(*OpI->getInsertionPointAfterDef());
B.SetCurrentDebugLocation(OpI->getDebugLoc());
} else if (auto *OpA = dyn_cast<Argument>(Op)) {
B.SetInsertPointPastAllocas(OpA->getParent());
B.SetCurrentDebugLocation(DebugLoc());
} else {
B.SetInsertPoint(F->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
}
SmallVector<Type *, 2> Types = {OpTy, OpTy};
MetadataAsValue *VMD = MetadataAsValue::get(
Ctx, MDNode::get(Ctx, ValueAsMetadata::getConstant(OpTyVal)));
Expand Down
94 changes: 94 additions & 0 deletions llvm/test/CodeGen/SPIRV/phi-ptrcast-dominate.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
; The goal of the test is to check that newly inserted `ptrcast` internal
; intrinsic functions for PHI's operands are inserted at the correct
; positions, and don't break rules of instruction domination and PHI nodes
; grouping at top of basic block.

; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: OpName %[[#Case1:]] "case1"
; CHECK-DAG: OpName %[[#Case2:]] "case2"
; CHECK-DAG: OpName %[[#Case3:]] "case3"
; CHECK: %[[#Case1]] = OpFunction
; CHECK: OpBranchConditional
; CHECK: OpPhi
; CHECK: OpBranch
; CHECK-COUNT-2: OpBranchConditional
; CHECK: OpFunctionEnd
; CHECK: %[[#Case2]] = OpFunction
; CHECK: OpBranchConditional
; CHECK: OpPhi
; CHECK: OpBranch
; CHECK-COUNT-2: OpBranchConditional
; CHECK: OpFunctionEnd
; CHECK: %[[#Case3]] = OpFunction
; CHECK: OpBranchConditional
; CHECK: OpPhi
; CHECK: OpBranch
; CHECK: OpInBoundsPtrAccessChain
; CHECK: OpBranchConditional
; CHECK: OpInBoundsPtrAccessChain
; CHECK: OpBranchConditional
; CHECK: OpFunctionEnd

%struct1 = type { i64 }
%struct2 = type { i64, i64 }

@.str.1 = private unnamed_addr addrspace(1) constant [3 x i8] c"OK\00", align 1
@.str.2 = private unnamed_addr addrspace(1) constant [6 x i8] c"WRONG\00", align 1

define spir_func void @case1(i1 %b1, i1 %b2, i1 %b3) {
entry:
br i1 %b1, label %l1, label %l2

l1:
%str = phi ptr addrspace(1) [ @.str.1, %entry ], [ @.str.2, %l2 ], [ @.str.2, %l3 ]
br label %exit

l2:
br i1 %b2, label %l1, label %l3

l3:
br i1 %b3, label %l1, label %exit

exit:
ret void
}

define spir_func void @case2(i1 %b1, i1 %b2, i1 %b3, ptr addrspace(1) byval(%struct1) %str1, ptr addrspace(1) byval(%struct2) %str2) {
entry:
br i1 %b1, label %l1, label %l2

l1:
%str = phi ptr addrspace(1) [ %str1, %entry ], [ %str2, %l2 ], [ %str2, %l3 ]
br label %exit

l2:
br i1 %b2, label %l1, label %l3

l3:
br i1 %b3, label %l1, label %exit

exit:
ret void
}

define spir_func void @case3(i1 %b1, i1 %b2, i1 %b3, ptr addrspace(1) byval(%struct1) %_arg_str1, ptr addrspace(1) byval(%struct2) %_arg_str2) {
entry:
br i1 %b1, label %l1, label %l2

l1:
%str = phi ptr addrspace(1) [ %_arg_str1, %entry ], [ %str2, %l2 ], [ %str3, %l3 ]
br label %exit

l2:
%str2 = getelementptr inbounds %struct2, ptr addrspace(1) %_arg_str2, i32 1
br i1 %b2, label %l1, label %l3

l3:
%str3 = getelementptr inbounds %struct2, ptr addrspace(1) %_arg_str2, i32 2
br i1 %b3, label %l1, label %exit

exit:
ret void
}
Loading