Skip to content

Commit 353b658

Browse files
scottp101sys_zuul
authored andcommitted
adding GEPLowering Pass
Change-Id: I2a81877608c9fb2bd757bd24ededb8c88883c558
1 parent 187f187 commit 353b658

File tree

5 files changed

+140
-44
lines changed

5 files changed

+140
-44
lines changed

IGC/Compiler/CISACodeGen/Emu64OpsPass.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ namespace {
111111

112112
bool runOnFunction(Function& F) override;
113113

114+
StringRef getPassName() const override { return "Emu64Ops"; }
115+
114116
private:
115117
void getAnalysisUsage(AnalysisUsage& AU) const override {
116118
AU.addRequired<CodeGenContextWrapper>();
@@ -151,13 +153,19 @@ namespace {
151153
return Align;
152154
}
153155

156+
void copyKnownMetadata(Instruction* NewI, Instruction* OldI) const
157+
{
158+
// Nothing needed yet
159+
}
160+
154161
void dupMemoryAttribute(LoadInst* NewLD, LoadInst* RefLD, unsigned Off) const {
155162
unsigned Align = getAlignment(RefLD);
156163

157164
NewLD->setVolatile(RefLD->isVolatile());
158165
NewLD->setAlignment(unsigned(MinAlign(Align, Off)));
159166
NewLD->setOrdering(RefLD->getOrdering());
160167
IGCLLVM::CopySyncScopeID(NewLD, RefLD);
168+
copyKnownMetadata(NewLD, RefLD);
161169
}
162170

163171
void dupMemoryAttribute(StoreInst* NewST, StoreInst* RefST, unsigned Off) const {
@@ -167,6 +175,7 @@ namespace {
167175
NewST->setAlignment(unsigned(MinAlign(Align, Off)));
168176
NewST->setOrdering(RefST->getOrdering());
169177
IGCLLVM::CopySyncScopeID(NewST, RefST);
178+
copyKnownMetadata(NewST, RefST);
170179
}
171180

172181
bool expandArguments(Function& F);

IGC/Compiler/CISACodeGen/GenIRLowering.cpp

Lines changed: 113 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,21 @@ using namespace IGC;
5151
using namespace IGC::IGCMD;
5252

5353
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-
//
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

253278
bool 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)

IGC/Compiler/CISACodeGen/GenIRLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3838
namespace IGC {
3939

4040
llvm::FunctionPass* createGenIRLowerPass();
41+
llvm::FunctionPass* createGEPLoweringPass();
4142

4243
} // End namespace IGC
4344

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,19 @@ namespace IGC
550550
if (ctx.m_instrTypes.hasLocalLoadStore) {
551551
mpm.add(new BreakConstantExpr());
552552
}
553+
554+
bool KeepGEPs = false;
553555
mpm.add(createGenIRLowerPass());
554556

557+
if (KeepGEPs)
558+
{
559+
mpm.add(createSeparateConstOffsetFromGEPPass());
560+
}
561+
else
562+
{
563+
mpm.add(createGEPLoweringPass());
564+
}
565+
555566
mpm.add(new WorkaroundAnalysis());
556567
if (!isOptDisabled)
557568
{
@@ -640,6 +651,11 @@ namespace IGC
640651
// that i128 uses are expanded to i64. This is why we need to run PeepholeTypeLegalizer
641652
// beforehand.
642653
mpm.add(new Legalizer::PeepholeTypeLegalizer());
654+
// Lower all GEPs now as Emu64 doesn't know how to handle them.
655+
if (KeepGEPs)
656+
{
657+
mpm.add(createGEPLoweringPass());
658+
}
643659
mpm.add(createEmu64OpsPass());
644660
}
645661

IGC/Compiler/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void initializeFindInterestingConstantsPass(llvm::PassRegistry&);
5858
void initializeGenericAddressAnalysisPass(llvm::PassRegistry&);
5959
void initializeGenericAddressDynamicResolutionPass(llvm::PassRegistry&);
6060
void initializeGenIRLoweringPass(llvm::PassRegistry&);
61+
void initializeGEPLoweringPass(llvm::PassRegistry&);
6162
void initializeGenSpecificPatternPass(llvm::PassRegistry&);
6263
void initializeGreedyLiveRangeReductionPass(llvm::PassRegistry&);
6364
void initializeIGCIndirectICBPropagaionPass(llvm::PassRegistry&);

0 commit comments

Comments
 (0)