@@ -186,10 +186,10 @@ class SPIRVEmitIntrinsics
186
186
CallInst *CI, SmallVector<std::pair<Value *, unsigned >> &Ops,
187
187
Type *&KnownElemTy, bool IsPostprocessing);
188
188
189
- CallInst *buildSpvPtrcast (Value *Op, Type *ElemTy);
189
+ CallInst *buildSpvPtrcast (Function *F, Value *Op, Type *ElemTy);
190
190
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 ,
193
193
std::unordered_set<Value *> &Visited);
194
194
195
195
void replaceAllUsesWith (Value *Src, Value *Dest, bool DeleteOld = true );
@@ -426,7 +426,8 @@ void SPIRVEmitIntrinsics::updateAssignType(CallInst *AssignCI, Value *Arg,
426
426
GR->addDeducedElementType (Arg, ElemTy);
427
427
}
428
428
429
- CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast (Value *Op, Type *ElemTy) {
429
+ CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast (Function *F, Value *Op,
430
+ Type *ElemTy) {
430
431
IRBuilder<> B (Op->getContext ());
431
432
if (auto *OpI = dyn_cast<Instruction>(Op)) {
432
433
// spv_ptrcast's argument Op denotes an instruction that generates
@@ -436,7 +437,7 @@ CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Value *Op, Type *ElemTy) {
436
437
B.SetInsertPointPastAllocas (OpA->getParent ());
437
438
B.SetCurrentDebugLocation (DebugLoc ());
438
439
} else {
439
- B.SetInsertPoint (CurrF ->getEntryBlock ().getFirstNonPHIOrDbgOrAlloca ());
440
+ B.SetInsertPoint (F ->getEntryBlock ().getFirstNonPHIOrDbgOrAlloca ());
440
441
}
441
442
Type *OpTy = Op->getType ();
442
443
SmallVector<Type *, 2 > Types = {OpTy, OpTy};
@@ -449,30 +450,36 @@ CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Value *Op, Type *ElemTy) {
449
450
}
450
451
451
452
void SPIRVEmitIntrinsics::propagateElemType (Value *Op, Type *ElemTy) {
452
- CallInst *PtrCasted = buildSpvPtrcast (Op, ElemTy);
453
+ // CallInst *PtrCasted = buildSpvPtrcast(Op, ElemTy);
453
454
SmallVector<User *> Users (Op->users ());
454
455
for (auto *U : Users) {
456
+ if (!isa<Instruction>(U))
457
+ continue ;
455
458
if (isa<BitCastInst>(U) || isa<GetElementPtrInst>(U) || isSpvIntrinsic (U))
456
459
continue ;
457
- U->replaceUsesOfWith (Op, PtrCasted);
460
+ U->replaceUsesOfWith (
461
+ Op, buildSpvPtrcast (dyn_cast<Instruction>(U)->getParent ()->getParent (),
462
+ Op, ElemTy));
458
463
}
459
464
}
460
465
461
466
void SPIRVEmitIntrinsics::propagateElemTypeRec (Value *Op, Type *PtrElemTy,
462
- CallInst *PtrCasted ) {
467
+ Type *CastElemTy ) {
463
468
if (!isNestedPointer (PtrElemTy))
464
469
return ;
465
470
std::unordered_set<Value *> Visited;
466
- propagateElemTypeRec (Op, PtrElemTy, PtrCasted , Visited);
471
+ propagateElemTypeRec (Op, PtrElemTy, CastElemTy , Visited);
467
472
}
468
473
469
474
void SPIRVEmitIntrinsics::propagateElemTypeRec (
470
- Value *Op, Type *PtrElemTy, CallInst *PtrCasted ,
475
+ Value *Op, Type *PtrElemTy, Type *CastElemTy ,
471
476
std::unordered_set<Value *> &Visited) {
472
477
if (!Visited.insert (Op).second )
473
478
return ;
474
479
SmallVector<User *> Users (Op->users ());
475
480
for (auto *U : Users) {
481
+ if (!isa<Instruction>(U))
482
+ continue ;
476
483
if (isa<BitCastInst>(U) || isSpvIntrinsic (U))
477
484
continue ;
478
485
if (auto *Ref = dyn_cast<GetElementPtrInst>(U)) {
@@ -490,12 +497,13 @@ void SPIRVEmitIntrinsics::propagateElemTypeRec(
490
497
updateAssignType (AssignCI, Ref, PoisonValue::get (NewElemTy));
491
498
// recursively propagate change
492
499
if (isNestedPointer (NewElemTy))
493
- propagateElemTypeRec (Ref, NewElemTy, buildSpvPtrcast (Ref, PrevElemTy),
494
- Visited);
500
+ propagateElemTypeRec (Ref, NewElemTy, PrevElemTy, Visited);
495
501
}
496
502
continue ;
497
503
}
498
- U->replaceUsesOfWith (Op, PtrCasted);
504
+ U->replaceUsesOfWith (
505
+ Op, buildSpvPtrcast (dyn_cast<Instruction>(U)->getParent ()->getParent (),
506
+ Op, CastElemTy));
499
507
}
500
508
}
501
509
@@ -1088,13 +1096,12 @@ void SPIRVEmitIntrinsics::deduceOperandElementType(
1088
1096
GR->addAssignPtrTypeInstr (Op, CI);
1089
1097
} else {
1090
1098
updateAssignType (AssignCI, Op, OpTyVal);
1091
- propagateElemTypeRec (
1092
- Op, KnownElemTy,
1093
- buildSpvPtrcast (Op, GR->findDeducedElementType (Op)));
1099
+ propagateElemTypeRec (Op, KnownElemTy, GR->findDeducedElementType (Op));
1094
1100
}
1095
1101
} else {
1096
1102
eraseTodoType (Op);
1097
- CallInst *PtrCastI = buildSpvPtrcast (Op, KnownElemTy);
1103
+ CallInst *PtrCastI =
1104
+ buildSpvPtrcast (I->getParent ()->getParent (), Op, KnownElemTy);
1098
1105
if (OpIt.second == std::numeric_limits<unsigned >::max ())
1099
1106
dyn_cast<CallInst>(I)->setCalledOperand (PtrCastI);
1100
1107
else
@@ -2280,7 +2287,7 @@ bool SPIRVEmitIntrinsics::postprocessTypes(Module &M) {
2280
2287
propagateElemType (CI, ElemTy);
2281
2288
} else {
2282
2289
updateAssignType (AssignCI, CI, PoisonValue::get (ElemTy));
2283
- propagateElemTypeRec (CI, ElemTy, buildSpvPtrcast (CI, KnownTy) );
2290
+ propagateElemTypeRec (CI, ElemTy, KnownTy);
2284
2291
}
2285
2292
eraseTodoType (Op);
2286
2293
continue ;
0 commit comments