@@ -181,6 +181,14 @@ static void setInsertPointSkippingPhis(IRBuilder<> &B, Instruction *I) {
181
181
B.SetInsertPoint (I);
182
182
}
183
183
184
+ static void setInsertPointAfterDef (IRBuilder<> &B, Instruction *I) {
185
+ B.SetCurrentDebugLocation (I->getDebugLoc ());
186
+ if (I->getType ()->isVoidTy ())
187
+ B.SetInsertPoint (I->getNextNode ());
188
+ else
189
+ B.SetInsertPoint (*I->getInsertionPointAfterDef ());
190
+ }
191
+
184
192
static bool requireAssignType (Instruction *I) {
185
193
IntrinsicInst *Intr = dyn_cast<IntrinsicInst>(I);
186
194
if (Intr) {
@@ -560,14 +568,18 @@ void SPIRVEmitIntrinsics::preprocessUndefs(IRBuilder<> &B) {
560
568
561
569
while (!Worklist.empty ()) {
562
570
Instruction *I = Worklist.front ();
571
+ bool BPrepared = false ;
563
572
Worklist.pop ();
564
573
565
574
for (auto &Op : I->operands ()) {
566
575
auto *AggrUndef = dyn_cast<UndefValue>(Op);
567
576
if (!AggrUndef || !Op->getType ()->isAggregateType ())
568
577
continue ;
569
578
570
- B.SetInsertPoint (I);
579
+ if (!BPrepared) {
580
+ setInsertPointSkippingPhis (B, I);
581
+ BPrepared = true ;
582
+ }
571
583
auto *IntrUndef = B.CreateIntrinsic (Intrinsic::spv_undef, {}, {});
572
584
Worklist.push (IntrUndef);
573
585
I->replaceUsesOfWith (Op, IntrUndef);
@@ -584,6 +596,7 @@ void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
584
596
585
597
while (!Worklist.empty ()) {
586
598
auto *I = Worklist.front ();
599
+ bool IsPhi = isa<PHINode>(I), BPrepared = false ;
587
600
assert (I);
588
601
bool KeepInst = false ;
589
602
for (const auto &Op : I->operands ()) {
@@ -615,7 +628,11 @@ void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
615
628
else
616
629
for (auto &COp : AggrConst->operands ())
617
630
Args.push_back (COp);
618
- B.SetInsertPoint (I);
631
+ if (!BPrepared) {
632
+ IsPhi ? B.SetInsertPointPastAllocas (I->getParent ()->getParent ())
633
+ : B.SetInsertPoint (I);
634
+ BPrepared = true ;
635
+ }
619
636
auto *CI =
620
637
B.CreateIntrinsic (Intrinsic::spv_const_composite, {ResTy}, {Args});
621
638
Worklist.push (CI);
@@ -1111,8 +1128,7 @@ void SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I,
1111
1128
isa<BitCastInst>(I))
1112
1129
return ;
1113
1130
1114
- setInsertPointSkippingPhis (B, I->getNextNode ());
1115
-
1131
+ setInsertPointAfterDef (B, I);
1116
1132
Type *ElemTy = deduceElementType (I);
1117
1133
Constant *EltTyConst = UndefValue::get (ElemTy);
1118
1134
unsigned AddressSpace = getPointerAddressSpace (I->getType ());
@@ -1127,7 +1143,7 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
1127
1143
reportFatalOnTokenType (I);
1128
1144
Type *Ty = I->getType ();
1129
1145
if (!Ty->isVoidTy () && !isPointerTy (Ty) && requireAssignType (I)) {
1130
- setInsertPointSkippingPhis (B, I-> getNextNode () );
1146
+ setInsertPointAfterDef (B, I);
1131
1147
Type *TypeToAssign = Ty;
1132
1148
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1133
1149
if (II->getIntrinsicID () == Intrinsic::spv_const_composite ||
@@ -1149,7 +1165,7 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
1149
1165
if (isa<UndefValue>(Op) && Op->getType ()->isAggregateType ())
1150
1166
buildIntrWithMD (Intrinsic::spv_assign_type, {B.getInt32Ty ()}, Op,
1151
1167
UndefValue::get (B.getInt32Ty ()), {}, B);
1152
- else if (!isa<Instruction>(Op)) // TODO: This case could be removed
1168
+ else if (!isa<Instruction>(Op))
1153
1169
buildIntrWithMD (Intrinsic::spv_assign_type, {Op->getType ()}, Op, Op, {},
1154
1170
B);
1155
1171
}
@@ -1159,7 +1175,7 @@ void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
1159
1175
void SPIRVEmitIntrinsics::insertSpirvDecorations (Instruction *I,
1160
1176
IRBuilder<> &B) {
1161
1177
if (MDNode *MD = I->getMetadata (" spirv.Decorations" )) {
1162
- B. SetInsertPoint (I-> getNextNode () );
1178
+ setInsertPointAfterDef (B, I );
1163
1179
B.CreateIntrinsic (Intrinsic::spv_assign_decoration, {I->getType ()},
1164
1180
{I, MetadataAsValue::get (I->getContext (), MD)});
1165
1181
}
@@ -1170,7 +1186,7 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
1170
1186
auto *II = dyn_cast<IntrinsicInst>(I);
1171
1187
if (II && II->getIntrinsicID () == Intrinsic::spv_const_composite &&
1172
1188
TrackConstants) {
1173
- B. SetInsertPoint (I-> getNextNode () );
1189
+ setInsertPointAfterDef (B, I );
1174
1190
auto t = AggrConsts.find (I);
1175
1191
assert (t != AggrConsts.end ());
1176
1192
auto *NewOp =
@@ -1179,6 +1195,7 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
1179
1195
I->replaceAllUsesWith (NewOp);
1180
1196
NewOp->setArgOperand (0 , I);
1181
1197
}
1198
+ bool IsPhi = isa<PHINode>(I), BPrepared = false ;
1182
1199
for (const auto &Op : I->operands ()) {
1183
1200
if ((isa<ConstantAggregateZero>(Op) && Op->getType ()->isVectorTy ()) ||
1184
1201
isa<PHINode>(I) || isa<SwitchInst>(I))
@@ -1188,7 +1205,11 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
1188
1205
if (II && ((II->getIntrinsicID () == Intrinsic::spv_gep && OpNo == 0 ) ||
1189
1206
(II->paramHasAttr (OpNo, Attribute::ImmArg))))
1190
1207
continue ;
1191
- B.SetInsertPoint (I);
1208
+ if (!BPrepared) {
1209
+ IsPhi ? B.SetInsertPointPastAllocas (I->getParent ()->getParent ())
1210
+ : B.SetInsertPoint (I);
1211
+ BPrepared = true ;
1212
+ }
1192
1213
Value *OpTyVal = Op;
1193
1214
if (Op->getType ()->isTargetExtTy ())
1194
1215
OpTyVal = Constant::getNullValue (
@@ -1201,7 +1222,7 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
1201
1222
}
1202
1223
if (I->hasName ()) {
1203
1224
reportFatalOnTokenType (I);
1204
- setInsertPointSkippingPhis (B, I-> getNextNode () );
1225
+ setInsertPointAfterDef (B, I);
1205
1226
std::vector<Value *> Args = {I};
1206
1227
addStringImm (I->getName (), B, Args);
1207
1228
B.CreateIntrinsic (Intrinsic::spv_assign_name, {I->getType ()}, Args);
@@ -1345,7 +1366,7 @@ bool SPIRVEmitIntrinsics::runOnFunction(Function &Func) {
1345
1366
for (auto *I : Worklist) {
1346
1367
TrackConstants = true ;
1347
1368
if (!I->getType ()->isVoidTy () || isa<StoreInst>(I))
1348
- B. SetInsertPoint (I-> getNextNode () );
1369
+ setInsertPointAfterDef (B, I );
1349
1370
// Visitors return either the original/newly created instruction for further
1350
1371
// processing, nullptr otherwise.
1351
1372
I = visit (*I);
0 commit comments