@@ -51,29 +51,21 @@ using namespace IGC;
5151using namespace IGC ::IGCMD;
5252
5353namespace {
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- //
5854 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 ;
6557 public:
6658 static char ID;
6759
68- GenIRLowering () : FunctionPass(ID), DL( nullptr ), Builder( nullptr ) {
60+ GenIRLowering () : FunctionPass(ID) {
6961 initializeGenIRLoweringPass (*PassRegistry::getPassRegistry ());
7062 }
7163
72- virtual llvm:: StringRef getPassName () const { return " GenIR Lowering" ; }
64+ StringRef getPassName () const override { return " GenIR Lowering" ; }
7365
74- virtual bool runOnFunction (Function& F);
66+ bool runOnFunction (Function& F) override ;
7567
76- virtual void getAnalysisUsage (AnalysisUsage& AU) const {
68+ void getAnalysisUsage (AnalysisUsage& AU) const override {
7769 AU.setPreservesCFG ();
7870 AU.addRequired <CodeGenContextWrapper>();
7971 AU.addRequired <MetaDataUtilsWrapper>();
@@ -82,12 +74,6 @@ namespace {
8274
8375 private:
8476 // 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-
9177 Value* rearrangeAdd (Value*, Loop*) const ;
9278
9379 bool combineFMaxFMin (CallInst* GII, BasicBlock::iterator& BBI) const ;
@@ -248,27 +234,61 @@ namespace {
248234 return ClampWithConstants_match<OpTy, ConstTy>(Op, Min, Max);
249235 }
250236
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+
251276} // End anonymous namespace
252277
253278bool GenIRLowering::runOnFunction (Function& F) {
254279 // Skip non-kernel function.
255280 MetaDataUtils* MDU = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils ();
256281 ModuleMetaData* modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData ();
257- m_LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo ();
258282 auto FII = MDU->findFunctionsInfoItem (&F);
259283 if (FII == MDU->end_FunctionsInfo ())
260284 return false ;
261285
262286 CodeGenContextWrapper* pCtxWrapper = &getAnalysis<CodeGenContextWrapper>();
263- m_ctx = pCtxWrapper->getCodeGenContext ();
287+ CodeGenContext* ctx = pCtxWrapper->getCodeGenContext ();
264288
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 ();
270290
271- BuilderTy TheBuilder (F.getContext (), TargetFolder (* DL));
291+ BuilderTy TheBuilder (F.getContext (), TargetFolder (DL));
272292 Builder = &TheBuilder;
273293
274294 bool Changed = false ;
@@ -352,7 +372,6 @@ bool GenIRLowering::runOnFunction(Function& F) {
352372 }
353373 }
354374
355- // Low GEP into gen.gep{32|64} in the 1st pass.
356375 for (auto & BB : F) {
357376 for (auto BI = BB.begin (), BE = BB.end (); BI != BE;) {
358377 Instruction* Inst = &(*BI++);
@@ -375,15 +394,12 @@ bool GenIRLowering::runOnFunction(Function& F) {
375394 break ;
376395 case Instruction::Select:
377396 // Enable the pattern match only when NaNs can be ignored.
378- if (m_ctx ->m_DriverInfo .IgnoreNan () ||
397+ if (ctx ->m_DriverInfo .IgnoreNan () ||
379398 modMD->compOpt .FiniteMathOnly )
380399 {
381400 Changed |= combineSelectInst (cast<SelectInst>(Inst), BI);
382401 }
383402 break ;
384- case Instruction::GetElementPtr:
385- Changed |= lowerGetElementPtrInst (cast<GetElementPtrInst>(Inst), BI);
386- break ;
387403 }
388404 }
389405 }
@@ -393,7 +409,47 @@ bool GenIRLowering::runOnFunction(Function& F) {
393409 return Changed;
394410}
395411
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 {
397453 Type* OldTy = Val->getType ();
398454 unsigned OldWidth = OldTy->getIntegerBitWidth ();
399455 unsigned NewWidth = NewTy->getIntegerBitWidth ();
@@ -409,7 +465,7 @@ Value* GenIRLowering::getSExtOrTrunc(Value* Val, Type* NewTy) const {
409465 return Val;
410466}
411467
412- Value* GenIRLowering ::truncExpr (Value* Val, Type* NewTy) const {
468+ Value* GEPLowering ::truncExpr (Value* Val, Type* NewTy) const {
413469 // Truncation on Gen could be as cheap as NOP by creating the proper region.
414470 // Instead of truncating the value itself, try to truncate how it's
415471 // calculated.
@@ -519,9 +575,8 @@ Value* GenIRLowering::rearrangeAdd(Value* val, Loop* loop) const
519575 }
520576}
521577
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+ {
525580 Value* PtrOp = GEP->getPointerOperand ();
526581 PointerType* PtrTy = dyn_cast<PointerType>(PtrOp->getType ());
527582
@@ -769,11 +824,6 @@ bool GenIRLowering::lowerGetElementPtrInst(GetElementPtrInst* GEP,
769824 GEP->replaceAllUsesWith (PointerValue);
770825 GEP->eraseFromParent ();
771826
772- if (Instruction * I = dyn_cast<Instruction>(PointerValue)) {
773- BBI = BasicBlock::iterator (I);
774- ++BBI;
775- }
776-
777827 return true ;
778828}
779829
@@ -903,7 +953,9 @@ bool GenIRLowering::combineSelectInst(SelectInst* Sel,
903953 return false ;
904954}
905955
906- FunctionPass* IGC::createGenIRLowerPass () { return new GenIRLowering (); }
956+ FunctionPass* IGC::createGenIRLowerPass () {
957+ return new GenIRLowering ();
958+ }
907959
908960// Register pass to igc-opt
909961#define PASS_FLAG " igc-gen-ir-lowering"
@@ -916,3 +968,20 @@ IGC_INITIALIZE_PASS_BEGIN(GenIRLowering, PASS_FLAG, PASS_DESCRIPTION,
916968 IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
917969 IGC_INITIALIZE_PASS_END(GenIRLowering, PASS_FLAG, PASS_DESCRIPTION,
918970 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