@@ -167,8 +167,9 @@ static bool isMemInstrToReplace(Instruction *I) {
167
167
isa<ExtractValueInst>(I) || isa<AtomicCmpXchgInst>(I);
168
168
}
169
169
170
- static bool isAggrToReplace (const Value *V) {
171
- return isa<ConstantAggregate>(V) || isa<ConstantDataArray>(V) ||
170
+ static bool isAggrConstForceInt32 (const Value *V) {
171
+ return isa<ConstantArray>(V) || isa<ConstantStruct>(V) ||
172
+ isa<ConstantDataArray>(V) ||
172
173
(isa<ConstantAggregateZero>(V) && !V->getType ()->isVectorTy ());
173
174
}
174
175
@@ -576,36 +577,42 @@ void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
576
577
assert (I);
577
578
bool KeepInst = false ;
578
579
for (const auto &Op : I->operands ()) {
579
- auto BuildCompositeIntrinsic =
580
- [](Constant *AggrC, ArrayRef<Value *> Args, Value *Op, Instruction *I,
581
- IRBuilder<> &B, std::queue<Instruction *> &Worklist,
582
- bool &KeepInst, SPIRVEmitIntrinsics &SEI) {
583
- B.SetInsertPoint (I);
584
- auto *CCI =
585
- B.CreateIntrinsic (Intrinsic::spv_const_composite, {}, {Args});
586
- Worklist.push (CCI);
587
- I->replaceUsesOfWith (Op, CCI);
588
- KeepInst = true ;
589
- SEI.AggrConsts [CCI] = AggrC;
590
- SEI.AggrConstTypes [CCI] = SEI.deduceNestedTypeHelper (AggrC);
591
- };
592
-
593
- if (auto *AggrC = dyn_cast<ConstantAggregate>(Op)) {
594
- SmallVector<Value *> Args (AggrC->op_begin (), AggrC->op_end ());
595
- BuildCompositeIntrinsic (AggrC, Args, Op, I, B, Worklist, KeepInst,
596
- *this );
597
- } else if (auto *AggrC = dyn_cast<ConstantDataArray>(Op)) {
580
+ Constant *AggrConst = nullptr ;
581
+ Type *ResTy = nullptr ;
582
+ if (auto *COp = dyn_cast<ConstantVector>(Op)) {
583
+ AggrConst = cast<Constant>(COp);
584
+ ResTy = COp->getType ();
585
+ } else if (auto *COp = dyn_cast<ConstantArray>(Op)) {
586
+ AggrConst = cast<Constant>(COp);
587
+ ResTy = B.getInt32Ty ();
588
+ } else if (auto *COp = dyn_cast<ConstantStruct>(Op)) {
589
+ AggrConst = cast<Constant>(COp);
590
+ ResTy = B.getInt32Ty ();
591
+ } else if (auto *COp = dyn_cast<ConstantDataArray>(Op)) {
592
+ AggrConst = cast<Constant>(COp);
593
+ ResTy = B.getInt32Ty ();
594
+ } else if (auto *COp = dyn_cast<ConstantAggregateZero>(Op)) {
595
+ if (!Op->getType ()->isVectorTy ()) {
596
+ AggrConst = cast<Constant>(COp);
597
+ ResTy = B.getInt32Ty ();
598
+ }
599
+ }
600
+ if (AggrConst) {
598
601
SmallVector<Value *> Args;
599
- for (unsigned i = 0 ; i < AggrC->getNumElements (); ++i)
600
- Args.push_back (AggrC->getElementAsConstant (i));
601
- BuildCompositeIntrinsic (AggrC, Args, Op, I, B, Worklist, KeepInst,
602
- *this );
603
- } else if (isa<ConstantAggregateZero>(Op) &&
604
- !Op->getType ()->isVectorTy ()) {
605
- auto *AggrC = cast<ConstantAggregateZero>(Op);
606
- SmallVector<Value *> Args (AggrC->op_begin (), AggrC->op_end ());
607
- BuildCompositeIntrinsic (AggrC, Args, Op, I, B, Worklist, KeepInst,
608
- *this );
602
+ if (auto *COp = dyn_cast<ConstantDataSequential>(Op))
603
+ for (unsigned i = 0 ; i < COp->getNumElements (); ++i)
604
+ Args.push_back (COp->getElementAsConstant (i));
605
+ else
606
+ for (auto &COp : AggrConst->operands ())
607
+ Args.push_back (COp);
608
+ B.SetInsertPoint (I);
609
+ auto *CI =
610
+ B.CreateIntrinsic (Intrinsic::spv_const_composite, {ResTy}, {Args});
611
+ Worklist.push (CI);
612
+ I->replaceUsesOfWith (Op, CI);
613
+ KeepInst = true ;
614
+ AggrConsts[CI] = AggrConst;
615
+ AggrConstTypes[CI] = deduceNestedTypeHelper (AggrConst);
609
616
}
610
617
}
611
618
if (!KeepInst)
@@ -1054,8 +1061,8 @@ void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
1054
1061
// by llvm IR general logic.
1055
1062
deduceElementTypeHelper (&GV);
1056
1063
Constant *Init = GV.getInitializer ();
1057
- Type *Ty = isAggrToReplace (Init) ? B.getInt32Ty () : Init->getType ();
1058
- Constant *Const = isAggrToReplace (Init) ? B.getInt32 (1 ) : Init;
1064
+ Type *Ty = isAggrConstForceInt32 (Init) ? B.getInt32Ty () : Init->getType ();
1065
+ Constant *Const = isAggrConstForceInt32 (Init) ? B.getInt32 (1 ) : Init;
1059
1066
auto *InitInst = B.CreateIntrinsic (Intrinsic::spv_init_global,
1060
1067
{GV.getType (), Ty}, {&GV, Const});
1061
1068
InitInst->setArgOperand (1 , Init);
@@ -1132,11 +1139,11 @@ void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
1132
1139
if (II && II->getIntrinsicID () == Intrinsic::spv_const_composite &&
1133
1140
TrackConstants) {
1134
1141
B.SetInsertPoint (I->getNextNode ());
1135
- Type *Ty = B.getInt32Ty ();
1136
1142
auto t = AggrConsts.find (I);
1137
1143
assert (t != AggrConsts.end ());
1138
- auto *NewOp = buildIntrWithMD (Intrinsic::spv_track_constant, {Ty, Ty},
1139
- t->second , I, {}, B);
1144
+ auto *NewOp =
1145
+ buildIntrWithMD (Intrinsic::spv_track_constant,
1146
+ {II->getType (), II->getType ()}, t->second , I, {}, B);
1140
1147
I->replaceAllUsesWith (NewOp);
1141
1148
NewOp->setArgOperand (0 , I);
1142
1149
}
0 commit comments