Skip to content

Commit 854165c

Browse files
iwwusys_zuul
authored andcommitted
Force retry if LICM pass may cause high spill in CS
Change-Id: I09a1bb109ebceb8cab15ed9570d9e4de6f1322ef
1 parent 0d06030 commit 854165c

File tree

7 files changed

+85
-22
lines changed

7 files changed

+85
-22
lines changed

IGC/Compiler/CISACodeGen/CheckInstrTypes.cpp

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
/*===================== begin_copyright_notice ==================================
23
34
Copyright (c) 2017 Intel Corporation
@@ -373,19 +374,20 @@ void CheckInstrTypes::visitGetElementPtrInst(llvm::GetElementPtrInst& I)
373374
#undef PASS_CFG_ONLY
374375
#undef PASS_ANALYSIS
375376

376-
#define PASS_FLAG "InstrStatitic"
377+
#define PASS_FLAG "InstrStatistic"
377378
#define PASS_DESCRIPTION "Check individual type of instructions"
378379
#define PASS_CFG_ONLY false
379380
#define PASS_ANALYSIS false
380-
IGC_INITIALIZE_PASS_BEGIN(InstrStatitic, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
381-
IGC_INITIALIZE_PASS_END(InstrStatitic, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
381+
IGC_INITIALIZE_PASS_BEGIN(InstrStatistic, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
382+
IGC_INITIALIZE_PASS_END(InstrStatistic, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
382383

383-
char InstrStatitic::ID = 0;
384+
char InstrStatistic::ID = 0;
384385

385-
InstrStatitic::InstrStatitic(CodeGenContext* ctx, InstrStatTypes type, InstrStatStage stage, int threshold) :
386+
InstrStatistic::InstrStatistic(CodeGenContext* ctx, InstrStatTypes type, InstrStatStage stage, int threshold) :
386387
FunctionPass(ID), m_ctx(ctx), m_type(type), m_stage(stage), m_threshold(threshold)
387388
{
388-
initializeInstrStatiticPass(*PassRegistry::getPassRegistry());
389+
initializeInstrStatisticPass(*PassRegistry::getPassRegistry());
390+
initializeLoopInfoWrapperPassPass(*PassRegistry::getPassRegistry());
389391

390392
if (stage == InstrStatStage::BEGIN)
391393
{
@@ -395,34 +397,74 @@ InstrStatitic::InstrStatitic(CodeGenContext* ctx, InstrStatTypes type, InstrStat
395397
}
396398
}
397399

398-
bool InstrStatitic::runOnFunction(Function& F)
400+
bool InstrStatistic::runOnFunction(Function& F)
399401
{
400-
// run the pass
401-
visit(F);
402+
bool changed = false;
403+
404+
if (m_type == LICM_STAT) {
405+
m_LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
406+
changed = parseLoops();
407+
}
408+
else {
409+
// run the pass
410+
visit(F);
411+
}
402412

403413
// if this is a call for ending statistic, find out if the difference exceeds the threshold.
404414
if (m_stage == InstrStatStage::END)
405415
{
406416
if (m_ctx->instrStat[m_type][InstrStatStage::BEGIN] - m_ctx->instrStat[m_type][InstrStatStage::END] > m_threshold)
407417
{
408418
m_ctx->instrStat[m_type][InstrStatStage::EXCEED_THRESHOLD] = 1;
419+
}
420+
421+
if (m_type == SROA_PROMOTED)
422+
{
409423
m_ctx->m_retryManager.Disable();
410424
}
411425
}
412-
return false;
426+
427+
return changed;
413428
}
414429

415-
void InstrStatitic::visitInstruction(llvm::Instruction& I)
430+
void InstrStatistic::visitInstruction(llvm::Instruction& I)
416431
{
417432
}
418433

419-
void InstrStatitic::visitLoadInst(LoadInst& I)
434+
void InstrStatistic::visitLoadInst(LoadInst& I)
420435
{
421-
m_ctx->instrStat[m_type][m_stage]++;
436+
if (m_type == SROA_PROMOTED)
437+
m_ctx->instrStat[m_type][m_stage]++;
422438
}
423439

424-
void InstrStatitic::visitStoreInst(StoreInst& I)
440+
void InstrStatistic::visitStoreInst(StoreInst& I)
425441
{
426-
m_ctx->instrStat[m_type][m_stage]++;
442+
if (m_type == SROA_PROMOTED)
443+
m_ctx->instrStat[m_type][m_stage]++;
427444
}
428445

446+
bool InstrStatistic::parseLoops()
447+
{
448+
bool changed = false;
449+
450+
for (auto& LI : *m_LI)
451+
{
452+
Loop* L1 = &(*LI);
453+
changed |= parseLoop(L1);
454+
455+
for (auto& L2 : L1->getSubLoops())
456+
{
457+
changed |= parseLoop(L2);
458+
}
459+
}
460+
461+
return changed;
462+
}
463+
464+
bool InstrStatistic::parseLoop(Loop* loop)
465+
{
466+
auto* header = loop->getHeader();
467+
m_ctx->instrStat[m_type][m_stage] += header->size();
468+
469+
return false;
470+
}

IGC/Compiler/CISACodeGen/CheckInstrTypes.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929
#include <llvm/Pass.h>
3030
#include <llvm/IR/InstVisitor.h>
3131
#include <llvm/Analysis/LoopInfo.h>
32+
#include <llvm/Analysis/LoopPass.h>
3233
#include "common/LLVMWarningsPop.hpp"
3334
#include "Compiler/IGCPassSupport.h"
3435
#include "Compiler/CodeGenPublic.h"
@@ -83,31 +84,41 @@ namespace IGC
8384

8485
};
8586

86-
class InstrStatitic : public llvm::FunctionPass, public llvm::InstVisitor<InstrStatitic>
87+
class InstrStatistic : public llvm::FunctionPass, public llvm::InstVisitor<InstrStatistic>
8788
{
8889
public:
8990
static char ID;
90-
InstrStatitic() : FunctionPass(ID), m_ctx(nullptr), m_type(InstrStatTypes(0)), m_stage(InstrStatStage::BEGIN), m_threshold(0)
91+
InstrStatistic() : FunctionPass(ID), m_ctx(nullptr), m_type(InstrStatTypes(0)), m_stage(InstrStatStage::BEGIN), m_threshold(0)
9192
{
9293
};
93-
InstrStatitic(CodeGenContext* ctx, InstrStatTypes type, InstrStatStage stage, int threshold);
94+
InstrStatistic(CodeGenContext* ctx, InstrStatTypes type, InstrStatStage stage, int threshold);
9495

9596
virtual bool runOnFunction(llvm::Function& F) override;
9697

9798
virtual llvm::StringRef getPassName() const override
9899
{
99-
return "InstrStatitic";
100+
return "InstrStatistic";
100101
}
101102

102103
void visitInstruction(llvm::Instruction& I);
103104
void visitLoadInst(llvm::LoadInst& I);
104105
void visitStoreInst(llvm::StoreInst& I);
105106

107+
virtual void getAnalysisUsage(llvm::AnalysisUsage& AU) const override
108+
{
109+
AU.addRequired<llvm::LoopInfoWrapperPass>();
110+
AU.setPreservesAll();
111+
}
112+
106113
private:
107114
CodeGenContext* m_ctx;
108115
IGC::InstrStatTypes m_type;
109116
InstrStatStage m_stage;
110117
int m_threshold;
118+
llvm::LoopInfo* m_LI;
119+
120+
bool parseLoops();
121+
bool parseLoop(llvm::Loop* loop);
111122
};
112123

113124
} // namespace IGC

IGC/Compiler/CISACodeGen/ComputeShaderCodeGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,11 @@ namespace IGC
452452
{
453453
return false;
454454
}
455+
else if (!ctx->m_retryManager.IsLastTry() && ctx->instrStat[LICM_STAT][EXCEED_THRESHOLD])
456+
{
457+
// skip SIMD8 if LICM threshold is met, unless it's lastTry
458+
return false;
459+
}
455460
else
456461
{
457462
return true;

IGC/Compiler/CISACodeGen/ShaderCodeGen.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,8 +1456,11 @@ void OptimizeIR(CodeGenContext* const pContext)
14561456

14571457
if (pContext->m_retryManager.AllowLICM() && IGC_IS_FLAG_ENABLED(allowLICM))
14581458
{
1459+
int licmTh = IGC_GET_FLAG_VALUE(LICMStatThreshold);
1460+
mpm.add(new InstrStatistic(pContext, LICM_STAT, InstrStatStage::BEGIN, licmTh));
14591461
mpm.add(llvm::createLICMPass());
14601462
mpm.add(llvm::createLICMPass());
1463+
mpm.add(new InstrStatistic(pContext, LICM_STAT, InstrStatStage::END, licmTh));
14611464
}
14621465

14631466
mpm.add(CreateHoistFMulInLoopPass());
@@ -1514,9 +1517,9 @@ void OptimizeIR(CodeGenContext* const pContext)
15141517
{
15151518
if (pContext->m_DriverInfo.NeedCountSROA())
15161519
{
1517-
mpm.add(new InstrStatitic(pContext, SROA_PROMOTED, InstrStatStage::BEGIN, 300));
1520+
mpm.add(new InstrStatistic(pContext, SROA_PROMOTED, InstrStatStage::BEGIN, 300));
15181521
mpm.add(createSROAPass());
1519-
mpm.add(new InstrStatitic(pContext, SROA_PROMOTED, InstrStatStage::END, 300));
1522+
mpm.add(new InstrStatistic(pContext, SROA_PROMOTED, InstrStatStage::END, 300));
15201523
}
15211524
else
15221525
{

IGC/Compiler/CodeGenPublic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ namespace IGC
213213
enum InstrStatTypes
214214
{
215215
SROA_PROMOTED,
216+
LICM_STAT,
216217
TOTAL_TYPES
217218
};
218219
enum InstrStatStage

IGC/Compiler/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void initializeTrivialLocalMemoryOpsEliminationPass(llvm::PassRegistry&);
144144
void initializeSLMConstPropPass(llvm::PassRegistry&);
145145
void initializeBlendToDiscardPass(llvm::PassRegistry&);
146146
void initializeCheckInstrTypesPass(llvm::PassRegistry&);
147-
void initializeInstrStatiticPass(llvm::PassRegistry&);
147+
void initializeInstrStatisticPass(llvm::PassRegistry&);
148148
void initializeHalfPromotionPass(llvm::PassRegistry&);
149149
void initializeFixFastMathFlagsPass(llvm::PassRegistry&);
150150
void initializeFCmpPaternMatchPass(llvm::PassRegistry&);

IGC/common/igc_flags.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ DECLARE_IGC_REGKEY(bool, allowLICM, true, "Enable LICM in I
271271
DECLARE_IGC_REGKEY(DWORD, CSSpillThresholdSLM, 12, "Spill Threshold for CS SIMD16 with SLM", false)
272272
DECLARE_IGC_REGKEY(DWORD, CSSpillThresholdNoSLM, 5, "Spill Threshold for CS SIMD16 without SLM", false)
273273
DECLARE_IGC_REGKEY(DWORD, AllowedSpillRegCount, 0, "Max allowed spill size without recompile", false)
274+
DECLARE_IGC_REGKEY(DWORD, LICMStatThreshold, 70, "LICM stat threshold to avoid retry SIMD16 for CS", false)
274275
DECLARE_IGC_REGKEY(bool, EnableTypeDemotion, true, "Enable Type Demotion", false)
275276
DECLARE_IGC_REGKEY(bool, EnablePreRARematFlag, true, "Enable PreRA Rematerialization of Flag", false)
276277
DECLARE_IGC_REGKEY(bool, EnableGASResolver, true, "Enable GAS Resolver", false)

0 commit comments

Comments
 (0)