Skip to content

Commit a40f95b

Browse files
authored
JIT: refactor CSE heuristics (#95705)
Split the heuristics into a common base class and (currently) two derived classes, one for normal CSEs and another for random CSEs.
1 parent 738a247 commit a40f95b

File tree

5 files changed

+1504
-1403
lines changed

5 files changed

+1504
-1403
lines changed

src/coreclr/jit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ set( JIT_HEADERS
346346
namedintrinsiclist.h
347347
objectalloc.h
348348
opcode.h
349+
optcse.h
349350
phase.h
350351
promotion.h
351352
rangecheck.h

src/coreclr/jit/compiler.h

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ struct InitVarDscInfo; // defined in registerargconvention.h
7777
class FgStack; // defined in fgbasic.cpp
7878
class Instrumentor; // defined in fgprofile.cpp
7979
class SpanningTreeVisitor; // defined in fgprofile.cpp
80-
class CSE_DataFlow; // defined in OptCSE.cpp
80+
class CSE_DataFlow; // defined in optcse.cpp
81+
struct CSEdsc; // defined in optcse.h
8182
class OptBoolsDsc; // defined in optimizer.cpp
8283
struct RelopImplicationInfo; // defined in redundantbranchopts.cpp
8384
struct JumpThreadInfo; // defined in redundantbranchopts.cpp
@@ -2482,6 +2483,8 @@ class Compiler
24822483
friend class Phase;
24832484
friend class Lowering;
24842485
friend class CSE_DataFlow;
2486+
friend class CSE_HeuristicCommon;
2487+
friend class CSE_HeuristicRandom;
24852488
friend class CSE_Heuristic;
24862489
friend class CodeGenInterface;
24872490
friend class CodeGen;
@@ -7308,58 +7311,6 @@ class Compiler
73087311

73097312
EXPSET_TP cseCallKillsMask; // Computed once - A mask that is used to kill available CSEs at callsites
73107313

7311-
/* Generic list of nodes - used by the CSE logic */
7312-
7313-
struct treeLst
7314-
{
7315-
treeLst* tlNext;
7316-
GenTree* tlTree;
7317-
};
7318-
7319-
struct treeStmtLst
7320-
{
7321-
treeStmtLst* tslNext;
7322-
GenTree* tslTree; // tree node
7323-
Statement* tslStmt; // statement containing the tree
7324-
BasicBlock* tslBlock; // block containing the statement
7325-
};
7326-
7327-
// The following logic keeps track of expressions via a simple hash table.
7328-
7329-
struct CSEdsc
7330-
{
7331-
CSEdsc* csdNextInBucket; // used by the hash table
7332-
size_t csdHashKey; // the original hashkey
7333-
ssize_t csdConstDefValue; // When we CSE similar constants, this is the value that we use as the def
7334-
ValueNum csdConstDefVN; // When we CSE similar constants, this is the ValueNumber that we use for the LclVar
7335-
// assignment
7336-
unsigned csdIndex; // 1..optCSECandidateCount
7337-
bool csdIsSharedConst; // true if this CSE is a shared const
7338-
bool csdLiveAcrossCall;
7339-
7340-
unsigned short csdDefCount; // definition count
7341-
unsigned short csdUseCount; // use count (excluding the implicit uses at defs)
7342-
7343-
weight_t csdDefWtCnt; // weighted def count
7344-
weight_t csdUseWtCnt; // weighted use count (excluding the implicit uses at defs)
7345-
7346-
GenTree* csdTree; // treenode containing the 1st occurrence
7347-
Statement* csdStmt; // stmt containing the 1st occurrence
7348-
BasicBlock* csdBlock; // block containing the 1st occurrence
7349-
7350-
treeStmtLst* csdTreeList; // list of matching tree nodes: head
7351-
treeStmtLst* csdTreeLast; // list of matching tree nodes: tail
7352-
7353-
ValueNum defExcSetPromise; // The exception set that is now required for all defs of this CSE.
7354-
// This will be set to NoVN if we decide to abandon this CSE
7355-
7356-
ValueNum defExcSetCurrent; // The set of exceptions we currently can use for CSE uses.
7357-
7358-
ValueNum defConservNormVN; // if all def occurrences share the same conservative normal value
7359-
// number, this will reflect it; otherwise, NoVN.
7360-
// not used for shared const CSE's
7361-
};
7362-
73637314
static const size_t s_optCSEhashSizeInitial;
73647315
static const size_t s_optCSEhashGrowthFactor;
73657316
static const size_t s_optCSEhashBucketSize;

src/coreclr/jit/jitconfigvalues.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ CONFIG_INTEGER(JitStackChecks, W("JitStackChecks"), 0)
160160
CONFIG_INTEGER(JitStress, W("JitStress"), 0) // Internal Jit stress mode: 0 = no stress, 2 = all stress, other = vary
161161
// stress based on a hash of the method and this value
162162
CONFIG_INTEGER(JitStressBBProf, W("JitStressBBProf"), 0) // Internal Jit stress mode
163-
CONFIG_INTEGER(JitStressBiasedCSE, W("JitStressBiasedCSE"), 0x101) // Internal Jit stress mode: decimal bias value
164-
// between (0,100) to perform CSE on a candidate.
165-
// 100% = All CSEs. 0% = 0 CSE. (> 100) means no
166-
// stress.
167163
CONFIG_INTEGER(JitStressModeNamesOnly, W("JitStressModeNamesOnly"), 0) // Internal Jit stress: if nonzero, only enable
168164
// stress modes listed in JitStressModeNames
169165
CONFIG_INTEGER(JitStressProcedureSplitting, W("JitStressProcedureSplitting"), 0) // Always split after the first basic
@@ -402,6 +398,11 @@ CONFIG_INTEGER(JitCSEMask, W("JitCSEMask"), 0)
402398

403399
// Enable metric output in jit disasm & elsewhere
404400
CONFIG_INTEGER(JitMetrics, W("JitMetrics"), 0)
401+
402+
// When nonzero, choose CSE candidates randomly, with probability
403+
// specified by the (decimal) value of the config
404+
CONFIG_INTEGER(JitRandomCSE, W("JitRandomCSE"), 0)
405+
405406
#endif
406407

407408
///

0 commit comments

Comments
 (0)