Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: initial support for reinforcement learning of CSE heuristic #96880

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#endif

#include "patchpointinfo.h"
#include "optcse.h" // for cse metrics

/*****************************************************************************/

Expand Down Expand Up @@ -2013,16 +2014,36 @@ void CodeGen::genEmitMachineCode()
}

#ifdef DEBUG
if (compiler->opts.disAsm || verbose)
const bool dspMetrics = compiler->opts.dspMetrics;
const bool dspSummary = compiler->opts.disAsm || verbose;
const bool dspMetricsOnly = dspMetrics && !dspSummary;

if (dspSummary || dspMetrics)
{
printf("\n; Total bytes of code %d, prolog size %d, PerfScore %.2f, instruction count %d, allocated bytes for "
if (!dspMetricsOnly)
{
printf("\n");
}

printf("Total bytes of code %d, prolog size %d, PerfScore %.2f, instruction count %d, allocated bytes for "
Copy link
Member

@EgorBo EgorBo Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: looks like "Total bytes of code" is no longer prefixed with ; (comments in asm)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in a subsequent change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added that back in #97677

"code %d",
codeSize, prologSize, compiler->info.compPerfScore, instrCount,
GetEmitter()->emitTotalHotCodeSize + GetEmitter()->emitTotalColdCodeSize);

if (JitConfig.JitMetrics() > 0)
if (dspMetrics)
{
printf(", num cse %d", compiler->optCSEcount);
printf(", num cse %d num cand %d", compiler->optCSEcount, compiler->optCSECandidateCount);

CSE_HeuristicCommon* const cseHeuristic = compiler->optGetCSEheuristic();
if (cseHeuristic != nullptr)
{
cseHeuristic->DumpMetrics();
}

if (compiler->info.compMethodSuperPMIIndex >= 0)
{
printf(" spmi index %d", compiler->info.compMethodSuperPMIIndex);
}
}

#if TRACK_LSRA_STATS
Expand All @@ -2035,7 +2056,10 @@ void CodeGen::genEmitMachineCode()
printf(" (MethodHash=%08x) for method %s (%s)\n", compiler->info.compMethodHash(), compiler->info.compFullName,
compiler->compGetTieringName(true));

printf("; ============================================================\n\n");
if (!dspMetricsOnly)
{
printf("; ============================================================\n\n");
}
printf(""); // in our logic this causes a flush
}

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,7 @@ void Compiler::compInitOptions(JitFlags* jitFlags)
opts.dspEHTable = false;
opts.dspDebugInfo = false;
opts.dspGCtbls = false;
opts.dspMetrics = false;
opts.disAsm2 = false;
opts.dspUnwind = false;
opts.compLongAddress = false;
Expand Down Expand Up @@ -2951,6 +2952,8 @@ void Compiler::compInitOptions(JitFlags* jitFlags)
{
opts.optRepeat = true;
}

opts.dspMetrics = (JitConfig.JitMetrics() != 0);
}

if (verboseDump)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2474,6 +2474,8 @@ class Compiler
friend class CSE_DataFlow;
friend class CSE_HeuristicCommon;
friend class CSE_HeuristicRandom;
friend class CSE_HeuristicReplay;
friend class CSE_HeuristicRL;
friend class CSE_Heuristic;
friend class CodeGenInterface;
friend class CodeGen;
Expand Down Expand Up @@ -9770,6 +9772,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
bool compLongAddress; // Force using large pseudo instructions for long address
// (IF_LARGEJMP/IF_LARGEADR/IF_LARGLDC)
bool dspGCtbls; // Display the GC tables
bool dspMetrics; // Display metrics
#endif

// Default numbers used to perform loop alignment. All the numbers are chosen
Expand Down
36 changes: 33 additions & 3 deletions src/coreclr/jit/jitconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ CONFIG_INTEGER(JitConstCSE, W("JitConstCSE"), 0)
// Allow fine-grained controls of CSEs done in a particular method
//
// Specify method that will respond to the CSEMask.
// 0 means feature disabled and all methods run CSE normally.
CONFIG_INTEGER(JitCSEHash, W("JitCSEHash"), 0)
// -1 means feature disabled and all methods run CSE normally.
CONFIG_INTEGER(JitCSEHash, W("JitCSEHash"), -1)

// Bitmask of allowed CSEs in methods specified by JitCSEHash.
// These bits control the "cse attempts" made by normal jitting,
Expand All @@ -404,10 +404,40 @@ CONFIG_INTEGER(JitCSEMask, W("JitCSEMask"), 0)
// Enable metric output in jit disasm & elsewhere
CONFIG_INTEGER(JitMetrics, W("JitMetrics"), 0)

// When nonzero, choose CSE candidates randomly, with probability
// When nonzero, choose CSE candidates randomly, with hash salt
// specified by the (decimal) value of the config
CONFIG_INTEGER(JitRandomCSE, W("JitRandomCSE"), 0)

// When set, specifies the exact CSEs to perform
// as a sequence of CSE candidate numbers
CONFIG_STRING(JitReplayCSE, W("JitReplayCSE"))

// When set, specify the sequence of rewards from the CSE replay.
// There should be one reward per step in the sequence.
CONFIG_STRING(JitReplayCSEReward, W("JitReplayCSEReward"))

// When set, specifies the initial parameter string for
// a reinforcement-learning based CSE heuristic.
//
// Note you can also set JitReplayCSE and JitReplayCSEPerfScore
// along with this, in which case we are asking for a policy
// evaluation/update based on the provided sequence.
CONFIG_STRING(JitRLCSE, W("JitRLCSE"))

// When set, specify the alpha value (step size) to
// use in learning.
CONFIG_STRING(JitRLCSEAlpha, W("JitRLCSEAlpha"))

// If nonzero, dump out details of policy evaluation and
// gradient updates
CONFIG_INTEGER(JitRLCSEVerbose, W("JitRLCSEVerbose"), 0)

// If nonzero, dump candidate feature values
CONFIG_INTEGER(JitRLCSECandidateFeatures, W("JitRLCSECandidateFeatures"), 0)

// If nonzero, use the greedy policy with current parameters.
CONFIG_INTEGER(JitRLCSEGreedy, W("JitRLCSEGreedy"), 0)

#endif

///
Expand Down
Loading