@@ -260,9 +260,29 @@ FunctionPass* IGC::createPromoteConstantPass() { return new PromoteConstant(); }
260
260
//
261
261
// We only promote when there is load of GV inside loops.
262
262
//
263
- static bool checkUses (GlobalVariable* GV, const Function* F, LoopInfo& LI) {
263
+ static bool checkUses (GlobalVariable* GV, const Function* F, LoopInfo& LI, const ConstantArray* llvm_used ) {
264
264
bool LoadInLoop = false ;
265
265
for (auto U : GV->users ()) {
266
+ // Because of ProgramScopeConstantAnalysis there might be user in llvm.used
267
+ // let's ignore this one, as it doesn't prevent opt.
268
+ // TODO: is there any other case of Constant to worry about. (?)
269
+ if (auto Const = dyn_cast<Constant>(U))
270
+ {
271
+ bool foundInLLVMUsed = false ;
272
+ unsigned numOperands = llvm_used ? llvm_used->getNumOperands () : 0 ;
273
+ for (unsigned i = 0 ; i != numOperands; ++i)
274
+ {
275
+ Value* gvi = llvm_used->getOperand (i)->stripPointerCastsNoFollowAliases ();
276
+ if (gvi == Const->stripPointerCastsNoFollowAliases ())
277
+ {
278
+ foundInLLVMUsed = true ;
279
+ break ;
280
+ }
281
+ }
282
+ if (foundInLLVMUsed)
283
+ continue ;
284
+ }
285
+
266
286
auto Inst = dyn_cast<Instruction>(U);
267
287
if (!Inst)
268
288
// TODO: this may be a const expr.
@@ -284,6 +304,9 @@ static bool checkUses(GlobalVariable* GV, const Function* F, LoopInfo& LI) {
284
304
}
285
305
}
286
306
307
+ if (IGC_IS_FLAG_ENABLED (AllowNonLoopConstantPromotion))
308
+ return true ;
309
+
287
310
return LoadInLoop;
288
311
}
289
312
@@ -342,6 +365,11 @@ static bool checkSize(GlobalVariable* GV, VectorType*& DataType,
342
365
N *= VectorSize;
343
366
}
344
367
unsigned VS = getLegalVectorSize (N);
368
+
369
+ // Allow experimental overriding for bigger than 32 elem. tables
370
+ if (IGC_IS_FLAG_ENABLED (AllowNonLoopConstantPromotion))
371
+ VS = 1U << Log2_32_Ceil (N);
372
+
345
373
if (VS == 0 )
346
374
return false ;
347
375
@@ -409,7 +437,7 @@ static bool checkType(GlobalVariable* GV) {
409
437
if (auto CDA = dyn_cast<ConstantDataArray>(Init))
410
438
return !CDA->isString ();
411
439
412
- // Not simply data. Need to check if all emements have the same type in a
440
+ // Not simply data. Need to check if all elements have the same type in a
413
441
// constant array.
414
442
if (auto CA = dyn_cast<ConstantArray>(Init)) {
415
443
auto EltTy = CA->getType ()->getElementType ();
@@ -499,8 +527,8 @@ static void promote(GlobalVariable* GV, VectorType* AllocaType, bool IsSigned,
499
527
// Transform all uses
500
528
for (auto UI = GV->user_begin (); UI != GV->user_end (); /* empty*/ ) {
501
529
auto GEP = dyn_cast<GetElementPtrInst>(*UI++);
502
- assert (GEP && " nullptr " );
503
- if (GEP->getParent ()->getParent () != F)
530
+ // might be Constant user in llvm.used
531
+ if (!GEP || GEP->getParent ()->getParent () != F)
504
532
continue ;
505
533
assert (GEP->getNumIndices () == 2 );
506
534
// This is the index to address the array, and the first index is to address
@@ -569,8 +597,8 @@ static bool rewriteAsCmpSel(GlobalVariable* GV, Function& F) {
569
597
570
598
for (auto UI = GV->user_begin (); UI != GV->user_end (); /* empty*/ ) {
571
599
auto GEP = dyn_cast<GetElementPtrInst>(*UI++);
572
- assert (GEP && " nullptr " );
573
- if (GEP->getParent ()->getParent () != &F)
600
+ // might be Constant user in llvm.used
601
+ if (!GEP || GEP->getParent ()->getParent () != &F)
574
602
continue ;
575
603
576
604
// This is the index to address the array, and the first index is to
@@ -634,14 +662,17 @@ bool PromoteConstant::runOnFunction(Function& F) {
634
662
Module* M = F.getParent ();
635
663
bool Changed = false ;
636
664
665
+ auto gv = M->getGlobalVariable (" llvm.used" );
666
+ ConstantArray* llvm_used = gv ? dyn_cast_or_null<ConstantArray>(gv->getInitializer ()) : nullptr ;
667
+
637
668
for (auto I = M->global_begin (); I != M->global_end (); /* empty*/ ) {
638
669
GlobalVariable* GV = &(*I++);
639
670
if (GV->user_empty () || !GV->isConstant () || !GV->hasInitializer () ||
640
671
GV->getType ()->getAddressSpace () != ADDRESS_SPACE_CONSTANT)
641
672
continue ;
642
673
if (!checkType (GV))
643
674
continue ;
644
- if (!checkUses (GV, &F, LI))
675
+ if (!checkUses (GV, &F, LI, llvm_used ))
645
676
continue ;
646
677
647
678
// Rewrite as cmp+sel sequence if profitable.
0 commit comments