Skip to content

Commit 92ac82f

Browse files
fix referring to an instruction in another function
1 parent 57dcd73 commit 92ac82f

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ class SPIRVEmitIntrinsics
186186
CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
187187
Type *&KnownElemTy, bool IsPostprocessing);
188188

189-
CallInst *buildSpvPtrcast(Value *Op, Type *ElemTy);
189+
CallInst *buildSpvPtrcast(Function *F, Value *Op, Type *ElemTy);
190190
void propagateElemType(Value *Op, Type *ElemTy);
191-
void propagateElemTypeRec(Value *Op, Type *PtrElemTy, CallInst *PtrCasted);
192-
void propagateElemTypeRec(Value *Op, Type *PtrElemTy, CallInst *PtrCasted,
191+
void propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy);
192+
void propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
193193
std::unordered_set<Value *> &Visited);
194194

195195
void replaceAllUsesWith(Value *Src, Value *Dest, bool DeleteOld = true);
@@ -426,7 +426,8 @@ void SPIRVEmitIntrinsics::updateAssignType(CallInst *AssignCI, Value *Arg,
426426
GR->addDeducedElementType(Arg, ElemTy);
427427
}
428428

429-
CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Value *Op, Type *ElemTy) {
429+
CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Function *F, Value *Op,
430+
Type *ElemTy) {
430431
IRBuilder<> B(Op->getContext());
431432
if (auto *OpI = dyn_cast<Instruction>(Op)) {
432433
// spv_ptrcast's argument Op denotes an instruction that generates
@@ -436,7 +437,7 @@ CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Value *Op, Type *ElemTy) {
436437
B.SetInsertPointPastAllocas(OpA->getParent());
437438
B.SetCurrentDebugLocation(DebugLoc());
438439
} else {
439-
B.SetInsertPoint(CurrF->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
440+
B.SetInsertPoint(F->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
440441
}
441442
Type *OpTy = Op->getType();
442443
SmallVector<Type *, 2> Types = {OpTy, OpTy};
@@ -449,30 +450,36 @@ CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Value *Op, Type *ElemTy) {
449450
}
450451

451452
void SPIRVEmitIntrinsics::propagateElemType(Value *Op, Type *ElemTy) {
452-
CallInst *PtrCasted = buildSpvPtrcast(Op, ElemTy);
453+
// CallInst *PtrCasted = buildSpvPtrcast(Op, ElemTy);
453454
SmallVector<User *> Users(Op->users());
454455
for (auto *U : Users) {
456+
if (!isa<Instruction>(U))
457+
continue;
455458
if (isa<BitCastInst>(U) || isa<GetElementPtrInst>(U) || isSpvIntrinsic(U))
456459
continue;
457-
U->replaceUsesOfWith(Op, PtrCasted);
460+
U->replaceUsesOfWith(
461+
Op, buildSpvPtrcast(dyn_cast<Instruction>(U)->getParent()->getParent(),
462+
Op, ElemTy));
458463
}
459464
}
460465

461466
void SPIRVEmitIntrinsics::propagateElemTypeRec(Value *Op, Type *PtrElemTy,
462-
CallInst *PtrCasted) {
467+
Type *CastElemTy) {
463468
if (!isNestedPointer(PtrElemTy))
464469
return;
465470
std::unordered_set<Value *> Visited;
466-
propagateElemTypeRec(Op, PtrElemTy, PtrCasted, Visited);
471+
propagateElemTypeRec(Op, PtrElemTy, CastElemTy, Visited);
467472
}
468473

469474
void SPIRVEmitIntrinsics::propagateElemTypeRec(
470-
Value *Op, Type *PtrElemTy, CallInst *PtrCasted,
475+
Value *Op, Type *PtrElemTy, Type *CastElemTy,
471476
std::unordered_set<Value *> &Visited) {
472477
if (!Visited.insert(Op).second)
473478
return;
474479
SmallVector<User *> Users(Op->users());
475480
for (auto *U : Users) {
481+
if (!isa<Instruction>(U))
482+
continue;
476483
if (isa<BitCastInst>(U) || isSpvIntrinsic(U))
477484
continue;
478485
if (auto *Ref = dyn_cast<GetElementPtrInst>(U)) {
@@ -490,12 +497,13 @@ void SPIRVEmitIntrinsics::propagateElemTypeRec(
490497
updateAssignType(AssignCI, Ref, PoisonValue::get(NewElemTy));
491498
// recursively propagate change
492499
if (isNestedPointer(NewElemTy))
493-
propagateElemTypeRec(Ref, NewElemTy, buildSpvPtrcast(Ref, PrevElemTy),
494-
Visited);
500+
propagateElemTypeRec(Ref, NewElemTy, PrevElemTy, Visited);
495501
}
496502
continue;
497503
}
498-
U->replaceUsesOfWith(Op, PtrCasted);
504+
U->replaceUsesOfWith(
505+
Op, buildSpvPtrcast(dyn_cast<Instruction>(U)->getParent()->getParent(),
506+
Op, CastElemTy));
499507
}
500508
}
501509

@@ -1088,13 +1096,12 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(
10881096
GR->addAssignPtrTypeInstr(Op, CI);
10891097
} else {
10901098
updateAssignType(AssignCI, Op, OpTyVal);
1091-
propagateElemTypeRec(
1092-
Op, KnownElemTy,
1093-
buildSpvPtrcast(Op, GR->findDeducedElementType(Op)));
1099+
propagateElemTypeRec(Op, KnownElemTy, GR->findDeducedElementType(Op));
10941100
}
10951101
} else {
10961102
eraseTodoType(Op);
1097-
CallInst *PtrCastI = buildSpvPtrcast(Op, KnownElemTy);
1103+
CallInst *PtrCastI =
1104+
buildSpvPtrcast(I->getParent()->getParent(), Op, KnownElemTy);
10981105
if (OpIt.second == std::numeric_limits<unsigned>::max())
10991106
dyn_cast<CallInst>(I)->setCalledOperand(PtrCastI);
11001107
else
@@ -2280,7 +2287,7 @@ bool SPIRVEmitIntrinsics::postprocessTypes(Module &M) {
22802287
propagateElemType(CI, ElemTy);
22812288
} else {
22822289
updateAssignType(AssignCI, CI, PoisonValue::get(ElemTy));
2283-
propagateElemTypeRec(CI, ElemTy, buildSpvPtrcast(CI, KnownTy));
2290+
propagateElemTypeRec(CI, ElemTy, KnownTy);
22842291
}
22852292
eraseTodoType(Op);
22862293
continue;

llvm/test/CodeGen/SPIRV/pointers/phi-valid-operand-types.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
; CHECK-DAG: %[[#PtrInt:]] = OpTypePointer Function %[[#Int]]
88
; CHECK: %[[#R1:]] = OpFunctionCall %[[#PtrChar]] %[[#]]
99
; CHECK: %[[#R2:]] = OpFunctionCall %[[#PtrInt]] %[[#]]
10-
; CHECK: %[[#Casted:]] = OpBitcast %[[#PtrChar]] %[[#R2]]
11-
; CHECK: OpPhi %[[#PtrChar]] %[[#R1]] %[[#]] %[[#Casted]] %[[#]]
12-
; CHECK: OpPhi %[[#PtrChar]] %[[#R1]] %[[#]] %[[#Casted]] %[[#]]
10+
; CHECK-2: %[[#]] = OpBitcast %[[#PtrChar]] %[[#R2]]
11+
; CHECK-2: OpPhi %[[#PtrChar]] %[[#R1]] %[[#]] %[[#]] %[[#]]
1312

1413
define ptr @foo(i1 %arg) {
1514
entry:

0 commit comments

Comments
 (0)