Skip to content

Commit 3c759d0

Browse files
srividyakarumurisys_zuul
authored andcommitted
FindInterestingConstants pass weights calculation changes
Change-Id: If946742c1299e2aedf28ffc79dca5ae7684cfc56
1 parent 3dc0c88 commit 3c759d0

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

IGC/Compiler/CodeGenPublic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ namespace IGC
276276
uint32_t loopCount = 0;
277277
uint32_t samplerCount = 0;
278278
uint32_t extendedMath = 0;
279+
uint32_t selectCount = 0;
279280
uint32_t weight = 0;
280281
};
281282

IGC/Compiler/FindInterestingConstants.cpp

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ using namespace IGC;
3939
#define PASS_DESCRIPTION "Find interesting constants"
4040
#define PASS_CFG_ONLY false
4141
#define PASS_ANALYSIS false
42-
#define UpdateInstCount(INST) IsExtendedMathInstruction(INST)? m_extendedMath++ : m_instCount++
4342
IGC_INITIALIZE_PASS_BEGIN(FindInterestingConstants, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
4443
IGC_INITIALIZE_PASS_END(FindInterestingConstants, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
4544

@@ -52,6 +51,37 @@ FindInterestingConstants::FindInterestingConstants() : FunctionPass(ID)
5251
initializeFindInterestingConstantsPass(*PassRegistry::getPassRegistry());
5352
}
5453

54+
void FindInterestingConstants::UpdateInstCount(Instruction* inst)
55+
{
56+
if (IsExtendedMathInstruction(inst))
57+
{
58+
m_extendedMath++;
59+
}
60+
else if (SelectInst * selInst = dyn_cast<SelectInst>(inst))
61+
{
62+
if (dyn_cast<Constant>(selInst->getOperand(1)) && dyn_cast<Constant>(selInst->getOperand(2)))
63+
m_instCount += 5;
64+
else
65+
m_selectCount++;
66+
}
67+
else
68+
m_instCount++;
69+
}
70+
71+
72+
bool FindInterestingConstants::isReverseOpInstPair(llvm::Intrinsic::ID intr1, llvm::Intrinsic::ID intr2)
73+
{
74+
std::map<llvm::Intrinsic::ID, llvm::Intrinsic::ID> reverseOpInstPair;
75+
reverseOpInstPair[llvm::Intrinsic::exp] = llvm::Intrinsic::log;
76+
reverseOpInstPair[llvm::Intrinsic::log] = llvm::Intrinsic::exp;
77+
reverseOpInstPair[llvm::Intrinsic::exp2] = llvm::Intrinsic::log2;
78+
reverseOpInstPair[llvm::Intrinsic::log2] = llvm::Intrinsic::exp2;
79+
80+
if ((reverseOpInstPair.find(intr1) != reverseOpInstPair.end()) && (reverseOpInstPair[intr1] == intr2))
81+
return true;
82+
return false;
83+
}
84+
5585
bool FindInterestingConstants::FoldsToConst(Instruction* inst, Instruction* use, bool& propagate)
5686
{
5787
propagate = false;
@@ -178,7 +208,7 @@ bool FindInterestingConstants::allUsersVisitedForFolding(Instruction* inst, Inst
178208
// as such instructions are currently added to visitedForFolding to avoid double counting in stats
179209
llvm::GenIntrinsicInst* pIntr = llvm::dyn_cast<llvm::GenIntrinsicInst>(anotherUse);
180210
SelectInst* selInst = dyn_cast<SelectInst>(anotherUse);
181-
if (pIntr || selInst)
211+
if ((pIntr && !IsMathIntrinsic(GetOpCode(anotherUseInst))) || selInst)
182212
{
183213
return false;
184214
}
@@ -203,7 +233,7 @@ void FindInterestingConstants::CheckIfSampleBecomesDeadCode(Instruction* inst, I
203233
{
204234
if (allUsersVisitedForFolding(inst, use))
205235
{
206-
m_instCount++;
236+
m_instCount += IGC_GET_FLAG_VALUE(WeightSamplerScalarResult);
207237
if (allUsersVisitedForFolding(pIntr, inst))
208238
{
209239
m_samplerCount++;
@@ -282,19 +312,53 @@ void FindInterestingConstants::FoldsToZeroPropagate(llvm::Instruction* I)
282312

283313
bool FindInterestingConstants::FoldsToSource(llvm::Instruction* inst, llvm::Instruction* use)
284314
{
315+
bool foldsToSource = false;
316+
llvm::Value* binOperand = nullptr;
317+
285318
if (BinaryOperator * binInst = dyn_cast<BinaryOperator>(use))
286319
{
287320
if (binInst->getOpcode() == Instruction::FMul)
288321
{
289-
return true;
322+
if (binInst->getOperand(0) == inst)
323+
{
324+
binOperand = binInst->getOperand(1);
325+
}
326+
else
327+
{
328+
binOperand = binInst->getOperand(0);
329+
}
330+
foldsToSource = true;
290331
}
291332
else if (binInst->getOpcode() == Instruction::FDiv &&
292333
inst == binInst->getOperand(1))
293334
{
294-
return true;
335+
binOperand = binInst->getOperand(0);
336+
foldsToSource = true;
295337
}
296338
}
297-
return false;
339+
340+
// Check for cases where folds to source triggers optimizations like showing up instruction sequence with exp-log/log-exp
341+
if (foldsToSource)
342+
{
343+
// Figure out if binOperand is part of reverseOpInstPair (eg: log-exp/exp-log), update extended math count accordingly
344+
if (llvm::IntrinsicInst * intr = dyn_cast<IntrinsicInst>(binOperand))
345+
{
346+
for (auto UI = use->user_begin(), UE = use->user_end(); UI != UE; ++UI)
347+
{
348+
if (llvm::IntrinsicInst * useIntr = dyn_cast<IntrinsicInst>(*UI))
349+
{
350+
if (isReverseOpInstPair(intr->getIntrinsicID(), useIntr->getIntrinsicID()))
351+
{
352+
// Increment extended math count
353+
m_extendedMath += 2;
354+
visitedForFolding.insert(intr);
355+
visitedForFolding.insert(useIntr);
356+
}
357+
}
358+
}
359+
}
360+
}
361+
return foldsToSource;
298362
}
299363

300364
void FindInterestingConstants::FoldsToSourcePropagate(llvm::Instruction* I)
@@ -430,6 +494,7 @@ void FindInterestingConstants::addInterestingConstant(llvm::Type* loadTy, unsign
430494
interestingConst.branchCount = stats.branchCount;
431495
interestingConst.loopCount = stats.loopCount;
432496
interestingConst.samplerCount = stats.samplerCount;
497+
interestingConst.selectCount = stats.selectCount;
433498
interestingConst.extendedMath = stats.extendedMath;
434499
interestingConst.weight = stats.weight;
435500
// For constant buffer accesses of size <= 32bit.
@@ -502,6 +567,7 @@ void FindInterestingConstants::ResetStatCounters()
502567
m_constFoldBranch = 0;
503568
m_constFoldLoopBranch = 0;
504569
m_samplerCount = 0;
570+
m_selectCount = 0;
505571
m_extendedMath = 0;
506572
m_branchsize = 0;
507573
m_loopSize = 0;
@@ -552,7 +618,7 @@ void FindInterestingConstants::visitLoadInst(llvm::LoadInst& I)
552618
stats.extendedMath = m_extendedMath;
553619
stats.weight = (m_instCount * IGC_GET_FLAG_VALUE(WeightOtherInstruction)) + std::max(m_constFoldBranch * IGC_GET_FLAG_VALUE(BaseWeightBranch), m_branchsize * IGC_GET_FLAG_VALUE(WeightBranch)) +
554620
std::max(m_constFoldLoopBranch * IGC_GET_FLAG_VALUE(BaseWeightLoop), m_loopSize * IGC_GET_FLAG_VALUE(WeightLoop)) + (m_samplerCount * IGC_GET_FLAG_VALUE(WeightSampler)) +
555-
m_extendedMath *IGC_GET_FLAG_VALUE(WeightExtendedMath);
621+
(m_extendedMath * IGC_GET_FLAG_VALUE(WeightExtendedMath)) + (m_selectCount * IGC_GET_FLAG_VALUE(WeightSelect));
556622
constValue = 0;
557623
// Get the ConstantAddress from LoadInst and log it in interesting constants
558624
addInterestingConstant(I.getType(), bufIdOrGRFOffset, eltId, size_in_bytes, true, constValue, stats);
@@ -568,8 +634,10 @@ void FindInterestingConstants::visitLoadInst(llvm::LoadInst& I)
568634
stats.loopCount = m_constFoldLoopBranch;
569635
stats.samplerCount = m_samplerCount;
570636
stats.extendedMath = m_extendedMath;
637+
stats.selectCount = m_selectCount;
571638
stats.weight = (m_instCount * IGC_GET_FLAG_VALUE(WeightOtherInstruction)) + std::max(m_constFoldBranch * IGC_GET_FLAG_VALUE(BaseWeightBranch), m_branchsize * IGC_GET_FLAG_VALUE(WeightBranch)) +
572-
std::max(m_constFoldLoopBranch * IGC_GET_FLAG_VALUE(BaseWeightLoop), m_loopSize * IGC_GET_FLAG_VALUE(WeightLoop)) + (m_samplerCount * IGC_GET_FLAG_VALUE(WeightSampler)) + m_extendedMath * IGC_GET_FLAG_VALUE(WeightExtendedMath);
639+
std::max(m_constFoldLoopBranch * IGC_GET_FLAG_VALUE(BaseWeightLoop), m_loopSize * IGC_GET_FLAG_VALUE(WeightLoop)) + (m_samplerCount * IGC_GET_FLAG_VALUE(WeightSampler)) +
640+
(m_extendedMath * IGC_GET_FLAG_VALUE(WeightExtendedMath)) + (m_selectCount * IGC_GET_FLAG_VALUE(WeightSelect));
573641
constValue = 0;
574642
// Zero value for this constant is interesting
575643
// Get the ConstantAddress from LoadInst and log it in interesting constants
@@ -586,7 +654,8 @@ void FindInterestingConstants::visitLoadInst(llvm::LoadInst& I)
586654
{
587655
InstructionStats stats;
588656
stats.instCount = m_instCount;
589-
stats.weight = m_instCount * IGC_GET_FLAG_VALUE(WeightOtherInstruction);
657+
stats.extendedMath = m_extendedMath;
658+
stats.weight = (m_instCount * IGC_GET_FLAG_VALUE(WeightOtherInstruction)) + (m_extendedMath * IGC_GET_FLAG_VALUE(WeightExtendedMath));
590659
// One value for this constant is interesting
591660
// Get the ConstantAddress from LoadInst and log it in interesting constants
592661
if (I.getType()->isIntegerTy())

IGC/Compiler/FindInterestingConstants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace IGC
4646
uint32_t loopCount = 0;
4747
uint32_t samplerCount = 0;
4848
uint32_t extendedMath = 0;
49+
uint32_t selectCount = 0;
4950
uint32_t weight = 0;
5051
};
5152
class FindInterestingConstants : public llvm::FunctionPass, public llvm::InstVisitor<FindInterestingConstants>
@@ -77,12 +78,15 @@ namespace IGC
7778
unsigned int m_constFoldLoopBranch;
7879
unsigned int m_samplerCount;
7980
unsigned int m_extendedMath;
81+
unsigned int m_selectCount;
8082
unsigned int m_branchsize;
8183
unsigned int m_loopSize;
8284
std::unordered_map<unsigned int, std::vector<SConstantAddrValue>> m_InterestingConstants;
8385
const llvm::DataLayout* m_DL;
8486
std::unordered_set<llvm::Instruction*> visitedForFolding;
8587
// Helper functions
88+
bool isReverseOpInstPair(llvm::Intrinsic::ID intr1, llvm::Intrinsic::ID intr2);
89+
void UpdateInstCount(llvm::Instruction* inst);
8690
bool getConstantAddress(llvm::LoadInst& I, unsigned& bufIdOrGRFOffset, int& eltId, int& size_in_bytes);
8791
bool FoldsToConst(llvm::Instruction* inst, llvm::Instruction* use, bool& propagate);
8892
bool FoldsToZero(llvm::Instruction* inst, llvm::Instruction* use);

IGC/common/igc_flags.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ DECLARE_IGC_REGKEY(bool, EnableGenUpdateCBResInfo, false, "Enable derived c
161161
DECLARE_IGC_REGKEY(bool, EnableHighestSIMDForNoSpill, false, "When there is no spill choose highest SIMD (compute shader only).", false)
162162
DECLARE_IGC_REGKEY(bool, EnableFoldsToSourceCheck, true, "Enable the check for Folds To Source Propagate for finding interesting constant. This is for the number of instructions that translate to a mov. i.e., Multiplication or Division by 1", false)
163163
DECLARE_IGC_REGKEY(DWORD, WeightSampler, 10, "Set the weight for Sampler instruction for finding interesting constant", false)
164+
DECLARE_IGC_REGKEY(DWORD, WeightSamplerScalarResult, 5, "Set the weight for scalar of a Sampler instruction for finding interesting constant", false)
164165
DECLARE_IGC_REGKEY(DWORD, WeightOtherInstruction, 1, "Set the weight for instruction for finding interesting constant", false)
165166
DECLARE_IGC_REGKEY(DWORD, WeightExtendedMath, 5, "Set the weight for Extended math instruction for finding interesting constant ", false)
167+
DECLARE_IGC_REGKEY(DWORD, WeightSelect, 10, "Set the weight for Select instruction for finding interesting constant ", false)
166168
DECLARE_IGC_REGKEY(DWORD, BaseWeightBranch, 200, "Set the Base weight for Branch instruction found in finding interesting constant", false)
167169
DECLARE_IGC_REGKEY(DWORD, WeightBranch, 20, "Set the weight for Branch instruction found in finding interesting constant", false)
168170
DECLARE_IGC_REGKEY(DWORD, BaseWeightLoop, 300, "Set the Base weight for Loop instruction found in finding interesting constant", false)

0 commit comments

Comments
 (0)