@@ -51,29 +51,21 @@ using namespace IGC;
51
51
using namespace IGC ::IGCMD;
52
52
53
53
namespace {
54
- // This pass lowers GEP into primitive ones (i.e. addition and/or
55
- // multiplication, converted to shift if applicable) to expose address
56
- // calculation to LLVM optimizations, such as CSE, LICM, and etc.
57
- //
58
54
class GenIRLowering : public FunctionPass {
59
- const DataLayout* DL;
60
- CodeGenContext* m_ctx;
61
- typedef IGCIRBuilder<TargetFolder> BuilderTy;
62
- BuilderTy* Builder;
63
- llvm::LoopInfo* m_LI;
64
-
55
+ using BuilderTy = IGCIRBuilder<TargetFolder>;
56
+ BuilderTy* Builder = nullptr ;
65
57
public:
66
58
static char ID;
67
59
68
- GenIRLowering () : FunctionPass(ID), DL( nullptr ), Builder( nullptr ) {
60
+ GenIRLowering () : FunctionPass(ID) {
69
61
initializeGenIRLoweringPass (*PassRegistry::getPassRegistry ());
70
62
}
71
63
72
- virtual llvm:: StringRef getPassName () const { return " GenIR Lowering" ; }
64
+ StringRef getPassName () const override { return " GenIR Lowering" ; }
73
65
74
- virtual bool runOnFunction (Function& F);
66
+ bool runOnFunction (Function& F) override ;
75
67
76
- virtual void getAnalysisUsage (AnalysisUsage& AU) const {
68
+ void getAnalysisUsage (AnalysisUsage& AU) const override {
77
69
AU.setPreservesCFG ();
78
70
AU.addRequired <CodeGenContextWrapper>();
79
71
AU.addRequired <MetaDataUtilsWrapper>();
@@ -82,12 +74,6 @@ namespace {
82
74
83
75
private:
84
76
// Helpers
85
- Value* getSExtOrTrunc (Value*, Type*) const ;
86
- Value* truncExpr (Value*, Type*) const ;
87
-
88
- bool lowerGetElementPtrInst (GetElementPtrInst* GEP,
89
- BasicBlock::iterator& BBI) const ;
90
-
91
77
Value* rearrangeAdd (Value*, Loop*) const ;
92
78
93
79
bool combineFMaxFMin (CallInst* GII, BasicBlock::iterator& BBI) const ;
@@ -248,27 +234,61 @@ namespace {
248
234
return ClampWithConstants_match<OpTy, ConstTy>(Op, Min, Max);
249
235
}
250
236
237
+ // This pass lowers GEP into primitive ones (i.e. addition and/or
238
+ // multiplication, converted to shift if applicable) to expose address
239
+ // calculation to LLVM optimizations, such as CSE, LICM, and etc.
240
+ //
241
+ class GEPLowering : public FunctionPass {
242
+ const DataLayout* DL = nullptr ;
243
+ CodeGenContext* m_ctx = nullptr ;
244
+ using BuilderTy = IGCIRBuilder<TargetFolder>;
245
+ BuilderTy* Builder = nullptr ;
246
+ llvm::LoopInfo* m_LI = nullptr ;
247
+ ModuleMetaData* modMD = nullptr ;
248
+ public:
249
+ static char ID;
250
+
251
+ GEPLowering () : FunctionPass(ID) {
252
+ initializeGEPLoweringPass (*PassRegistry::getPassRegistry ());
253
+ }
254
+
255
+ StringRef getPassName () const override { return " GEP Lowering" ; }
256
+
257
+ bool runOnFunction (Function& F) override ;
258
+
259
+ void getAnalysisUsage (AnalysisUsage& AU) const override {
260
+ AU.setPreservesCFG ();
261
+ AU.addRequired <CodeGenContextWrapper>();
262
+ AU.addRequired <MetaDataUtilsWrapper>();
263
+ AU.addRequired <LoopInfoWrapperPass>();
264
+ }
265
+
266
+ private:
267
+ // Helpers
268
+ Value* getSExtOrTrunc (Value*, Type*) const ;
269
+ Value* truncExpr (Value*, Type*) const ;
270
+
271
+ bool lowerGetElementPtrInst (GetElementPtrInst* GEP) const ;
272
+ };
273
+
274
+ char GEPLowering::ID = 0 ;
275
+
251
276
} // End anonymous namespace
252
277
253
278
bool GenIRLowering::runOnFunction (Function& F) {
254
279
// Skip non-kernel function.
255
280
MetaDataUtils* MDU = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
256
281
ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData ();
257
- m_LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
258
282
auto FII = MDU->findFunctionsInfoItem (&F);
259
283
if (FII == MDU->end_FunctionsInfo ())
260
284
return false ;
261
285
262
286
CodeGenContextWrapper* pCtxWrapper = &getAnalysis<CodeGenContextWrapper>();
263
- m_ctx = pCtxWrapper->getCodeGenContext ();
287
+ CodeGenContext* ctx = pCtxWrapper->getCodeGenContext ();
264
288
265
- DL = &F.getParent ()->getDataLayout ();
266
- // If we don't have DL, we don't know how to calculate addresses
267
- // efficiently.
268
- if (!DL)
269
- return false ;
289
+ auto &DL = F.getParent ()->getDataLayout ();
270
290
271
- BuilderTy TheBuilder (F.getContext (), TargetFolder (* DL));
291
+ BuilderTy TheBuilder (F.getContext (), TargetFolder (DL));
272
292
Builder = &TheBuilder;
273
293
274
294
bool Changed = false ;
@@ -352,7 +372,6 @@ bool GenIRLowering::runOnFunction(Function& F) {
352
372
}
353
373
}
354
374
355
- // Low GEP into gen.gep{32|64} in the 1st pass.
356
375
for (auto & BB : F) {
357
376
for (auto BI = BB.begin (), BE = BB.end (); BI != BE;) {
358
377
Instruction* Inst = &(*BI++);
@@ -375,15 +394,12 @@ bool GenIRLowering::runOnFunction(Function& F) {
375
394
break ;
376
395
case Instruction::Select:
377
396
// Enable the pattern match only when NaNs can be ignored.
378
- if (m_ctx ->m_DriverInfo .IgnoreNan () ||
397
+ if (ctx ->m_DriverInfo .IgnoreNan () ||
379
398
modMD->compOpt .FiniteMathOnly )
380
399
{
381
400
Changed |= combineSelectInst (cast<SelectInst>(Inst), BI);
382
401
}
383
402
break ;
384
- case Instruction::GetElementPtr:
385
- Changed |= lowerGetElementPtrInst (cast<GetElementPtrInst>(Inst), BI);
386
- break ;
387
403
}
388
404
}
389
405
}
@@ -393,7 +409,47 @@ bool GenIRLowering::runOnFunction(Function& F) {
393
409
return Changed;
394
410
}
395
411
396
- Value* GenIRLowering::getSExtOrTrunc (Value* Val, Type* NewTy) const {
412
+ bool GEPLowering::runOnFunction (Function& F) {
413
+ // Skip non-kernel function.
414
+ modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData ();
415
+ m_LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
416
+ MetaDataUtils* MDU = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
417
+ auto FII = MDU->findFunctionsInfoItem (&F);
418
+ if (FII == MDU->end_FunctionsInfo ())
419
+ return false ;
420
+
421
+ CodeGenContextWrapper* pCtxWrapper = &getAnalysis<CodeGenContextWrapper>();
422
+ m_ctx = pCtxWrapper->getCodeGenContext ();
423
+
424
+ DL = &F.getParent ()->getDataLayout ();
425
+
426
+ BuilderTy TheBuilder (F.getContext (), TargetFolder (*DL));
427
+ Builder = &TheBuilder;
428
+
429
+ bool Changed = false ;
430
+
431
+ for (auto & BB : F) {
432
+ for (auto BI = BB.begin (), BE = BB.end (); BI != BE;) {
433
+ Instruction* Inst = &(*BI++);
434
+ Builder->SetInsertPoint (Inst);
435
+
436
+ switch (Inst->getOpcode ()) {
437
+ default : // By default, DO NOTHING
438
+ break ;
439
+ // Lower GEPs to inttoptr/ptrtoint with offsets.
440
+ case Instruction::GetElementPtr:
441
+ Changed |=
442
+ lowerGetElementPtrInst (cast<GetElementPtrInst>(Inst));
443
+ break ;
444
+ }
445
+ }
446
+ }
447
+
448
+ return Changed;
449
+ }
450
+
451
+
452
+ Value* GEPLowering::getSExtOrTrunc (Value* Val, Type* NewTy) const {
397
453
Type* OldTy = Val->getType ();
398
454
unsigned OldWidth = OldTy->getIntegerBitWidth ();
399
455
unsigned NewWidth = NewTy->getIntegerBitWidth ();
@@ -409,7 +465,7 @@ Value* GenIRLowering::getSExtOrTrunc(Value* Val, Type* NewTy) const {
409
465
return Val;
410
466
}
411
467
412
- Value* GenIRLowering ::truncExpr (Value* Val, Type* NewTy) const {
468
+ Value* GEPLowering ::truncExpr (Value* Val, Type* NewTy) const {
413
469
// Truncation on Gen could be as cheap as NOP by creating the proper region.
414
470
// Instead of truncating the value itself, try to truncate how it's
415
471
// calculated.
@@ -519,9 +575,8 @@ Value* GenIRLowering::rearrangeAdd(Value* val, Loop* loop) const
519
575
}
520
576
}
521
577
522
- bool GenIRLowering::lowerGetElementPtrInst (GetElementPtrInst* GEP,
523
- BasicBlock::iterator& BBI) const {
524
- ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData ();
578
+ bool GEPLowering::lowerGetElementPtrInst (GetElementPtrInst* GEP) const
579
+ {
525
580
Value* PtrOp = GEP->getPointerOperand ();
526
581
PointerType* PtrTy = dyn_cast<PointerType>(PtrOp->getType ());
527
582
@@ -769,11 +824,6 @@ bool GenIRLowering::lowerGetElementPtrInst(GetElementPtrInst* GEP,
769
824
GEP->replaceAllUsesWith (PointerValue);
770
825
GEP->eraseFromParent ();
771
826
772
- if (Instruction * I = dyn_cast<Instruction>(PointerValue)) {
773
- BBI = BasicBlock::iterator (I);
774
- ++BBI;
775
- }
776
-
777
827
return true ;
778
828
}
779
829
@@ -903,7 +953,9 @@ bool GenIRLowering::combineSelectInst(SelectInst* Sel,
903
953
return false ;
904
954
}
905
955
906
- FunctionPass* IGC::createGenIRLowerPass () { return new GenIRLowering (); }
956
+ FunctionPass* IGC::createGenIRLowerPass () {
957
+ return new GenIRLowering ();
958
+ }
907
959
908
960
// Register pass to igc-opt
909
961
#define PASS_FLAG " igc-gen-ir-lowering"
@@ -916,3 +968,20 @@ IGC_INITIALIZE_PASS_BEGIN(GenIRLowering, PASS_FLAG, PASS_DESCRIPTION,
916
968
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
917
969
IGC_INITIALIZE_PASS_END(GenIRLowering, PASS_FLAG, PASS_DESCRIPTION,
918
970
PASS_CFG_ONLY, PASS_ANALYSIS)
971
+
972
+ FunctionPass* IGC::createGEPLoweringPass() {
973
+ return new GEPLowering ();
974
+ }
975
+
976
+ // Register pass to igc-opt
977
+ #define PASS_FLAG2 " igc-gep-lowering"
978
+ #define PASS_DESCRIPTION2 " Lowers GEP into primitive ones"
979
+ #define PASS_CFG_ONLY2 false
980
+ #define PASS_ANALYSIS2 false
981
+ IGC_INITIALIZE_PASS_BEGIN (GEPLowering, PASS_FLAG2, PASS_DESCRIPTION2,
982
+ PASS_CFG_ONLY2, PASS_ANALYSIS2)
983
+ IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
984
+ IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
985
+ IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
986
+ IGC_INITIALIZE_PASS_END(GEPLowering, PASS_FLAG2, PASS_DESCRIPTION2,
987
+ PASS_CFG_ONLY2, PASS_ANALYSIS2)
0 commit comments