Skip to content

Commit 5040afe

Browse files
authored
JIT: rework phase objects so we can use them more widely (#31808)
Next part of #2109. Add support for phases that are simply compiler methods or lambdas. These are not used yet -- the plan is to introduce new phases gradually, cleaning up redundant checking and dumping along the way. This will happen in subsequent changes. Remove a bit of now-redundant post-phase dumping and checking from lower. Add the active phase to the assertion text, so we have some context.
1 parent fb64ec5 commit 5040afe

File tree

15 files changed

+210
-87
lines changed

15 files changed

+210
-87
lines changed

src/coreclr/src/jit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ set( JIT_SOURCES
6767
objectalloc.cpp
6868
optcse.cpp
6969
optimizer.cpp
70+
phase.cpp
7071
rangecheck.cpp
7172
rationalize.cpp
7273
regalloc.cpp

src/coreclr/src/jit/compiler.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,12 @@ void Compiler::compInit(ArenaAllocator* pAlloc, InlineInfo* inlineInfo)
17511751
compInlineResult = nullptr;
17521752
}
17531753

1754+
// Initialize this to the first phase to run.
1755+
mostRecentlyActivePhase = PHASE_PRE_IMPORT;
1756+
1757+
// Initially, no phase checks are active.
1758+
activePhaseChecks = PhaseChecks::CHECK_NONE;
1759+
17541760
#ifdef FEATURE_TRACELOGGING
17551761
// Make sure JIT telemetry is initialized as soon as allocations can be made
17561762
// but no later than a point where noway_asserts can be thrown.
@@ -1759,9 +1765,8 @@ void Compiler::compInit(ArenaAllocator* pAlloc, InlineInfo* inlineInfo)
17591765
// Note: JIT telemetry could gather data when compiler is not fully initialized.
17601766
// So you have to initialize the compiler variables you use for telemetry.
17611767
assert((unsigned)PHASE_PRE_IMPORT == 0);
1762-
previousCompletedPhase = PHASE_PRE_IMPORT;
1763-
info.compILCodeSize = 0;
1764-
info.compMethodHnd = nullptr;
1768+
info.compILCodeSize = 0;
1769+
info.compMethodHnd = nullptr;
17651770
compJitTelemetry.Initialize(this);
17661771
#endif
17671772

@@ -4214,6 +4219,37 @@ void Compiler::compFunctionTraceEnd(void* methodCodePtr, ULONG methodCodeSize, b
42144219
#endif // DEBUG
42154220
}
42164221

4222+
//------------------------------------------------------------------------
4223+
// BeginPhase: begin execution of a phase
4224+
//
4225+
// Arguments:
4226+
// phase - the phase that is about to begin
4227+
//
4228+
void Compiler::BeginPhase(Phases phase)
4229+
{
4230+
mostRecentlyActivePhase = phase;
4231+
}
4232+
4233+
//------------------------------------------------------------------------
4234+
// EndPhase: finish execution of a phase
4235+
//
4236+
// Arguments:
4237+
// phase - the phase that has just finished
4238+
//
4239+
void Compiler::EndPhase(Phases phase)
4240+
{
4241+
#if defined(FEATURE_JIT_METHOD_PERF)
4242+
if (pCompJitTimer != nullptr)
4243+
{
4244+
pCompJitTimer->EndPhase(this, phase);
4245+
}
4246+
#endif
4247+
#if DUMP_FLOWGRAPHS
4248+
fgDumpFlowGraph(phase);
4249+
#endif // DUMP_FLOWGRAPHS
4250+
mostRecentlyActivePhase = phase;
4251+
}
4252+
42174253
//------------------------------------------------------------------------
42184254
// compCompile: run phases needed for compilation
42194255
//
@@ -4564,7 +4600,10 @@ void Compiler::compCompile(void** methodCodePtr, ULONG* methodCodeSize, JitFlags
45644600
#endif // DEBUG
45654601

45664602
// End of the morphing phases
4603+
//
4604+
// We can now enable all phase checking
45674605
EndPhase(PHASE_MORPH_END);
4606+
activePhaseChecks = PhaseChecks::CHECK_ALL;
45684607

45694608
// GS security checks for unsafe buffers
45704609
if (getNeedsGSSecurityCookie())

src/coreclr/src/jit/compiler.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,14 @@ extern const char* PhaseNames[];
10941094
extern const char* PhaseEnums[];
10951095
extern const LPCWSTR PhaseShortNames[];
10961096

1097+
// Specify which checks should be run after each phase
1098+
//
1099+
enum class PhaseChecks
1100+
{
1101+
CHECK_NONE,
1102+
CHECK_ALL
1103+
};
1104+
10971105
// The following enum provides a simple 1:1 mapping to CLR API's
10981106
enum API_ICorJitInfo_Names
10991107
{
@@ -8979,7 +8987,8 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
89798987

89808988
#endif // !TARGET_X86
89818989

8982-
Phases previousCompletedPhase; // the most recently completed phase
8990+
Phases mostRecentlyActivePhase; // the most recently active phase
8991+
PhaseChecks activePhaseChecks; // the currently active phase checks
89838992

89848993
//-------------------------------------------------------------------------
89858994
// The following keeps track of how many bytes of local frame space we've
@@ -9492,7 +9501,8 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
94929501
static LPCWSTR JitTimeLogCsv(); // Retrieve the file name for CSV from ConfigDWORD.
94939502
static LPCWSTR compJitTimeLogFilename; // If a log file for JIT time is desired, filename to write it to.
94949503
#endif
9495-
inline void EndPhase(Phases phase); // Indicate the end of the given phase.
9504+
void BeginPhase(Phases phase); // Indicate the start of the given phase.
9505+
void EndPhase(Phases phase); // Indicate the end of the given phase.
94969506

94979507
#if MEASURE_CLRAPI_CALLS
94989508
// Thin wrappers that call into JitTimer (if present).

src/coreclr/src/jit/compiler.hpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4075,20 +4075,6 @@ inline bool Compiler::lvaIsGCTracked(const LclVarDsc* varDsc)
40754075
}
40764076
}
40774077

4078-
inline void Compiler::EndPhase(Phases phase)
4079-
{
4080-
#if defined(FEATURE_JIT_METHOD_PERF)
4081-
if (pCompJitTimer != nullptr)
4082-
{
4083-
pCompJitTimer->EndPhase(this, phase);
4084-
}
4085-
#endif
4086-
#if DUMP_FLOWGRAPHS
4087-
fgDumpFlowGraph(phase);
4088-
#endif // DUMP_FLOWGRAPHS
4089-
previousCompletedPhase = phase;
4090-
}
4091-
40924078
/*****************************************************************************/
40934079
#if MEASURE_CLRAPI_CALLS
40944080

src/coreclr/src/jit/error.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,21 @@ extern "C" void __cdecl assertAbort(const char* why, const char* file, unsigned
272272
LogEnv* env = JitTls::GetLogEnv();
273273
const int BUFF_SIZE = 8192;
274274
char* buff = (char*)alloca(BUFF_SIZE);
275+
const char* phaseName = "unknown phase";
275276
if (env->compiler)
276277
{
277-
_snprintf_s(buff, BUFF_SIZE, _TRUNCATE, "Assertion failed '%s' in '%s' (IL size %d)\n", why,
278-
env->compiler->info.compFullName, env->compiler->info.compILCodeSize);
278+
phaseName = PhaseNames[env->compiler->mostRecentlyActivePhase];
279+
_snprintf_s(buff, BUFF_SIZE, _TRUNCATE, "Assertion failed '%s' in '%s' during '%s' (IL size %d)\n", why,
280+
env->compiler->info.compFullName, phaseName, env->compiler->info.compILCodeSize);
279281
msg = buff;
280282
}
281283
printf(""); // null string means flush
282284

283285
#if FUNC_INFO_LOGGING
284286
if (Compiler::compJitFuncInfoFile != nullptr)
285287
{
286-
fprintf(Compiler::compJitFuncInfoFile, "%s - Assertion failed (%s:%d - %s)\n",
287-
(env == nullptr) ? "UNKNOWN" : env->compiler->info.compFullName, file, line, why);
288+
fprintf(Compiler::compJitFuncInfoFile, "%s - Assertion failed (%s:%d - %s) during %s\n",
289+
(env == nullptr) ? "UNKNOWN" : env->compiler->info.compFullName, file, line, why, phaseName);
288290
}
289291
#endif // FUNC_INFO_LOGGING
290292

src/coreclr/src/jit/jit.settings.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<CppCompile Include="..\LSRA.cpp" />
9494
<CppCompile Include="..\lsrabuild.cpp" />
9595
<CppCompile Include="..\codegenlinear.cpp" />
96+
<CppCompile Include="..\phase.cpp" />
9697
</ItemGroup>
9798
<ItemGroup Condition="'$(TargetArch)'=='i386'">
9899
<CppCompile Include="..\emitXArch.cpp" />

src/coreclr/src/jit/jittelemetry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ void JitTelemetry::NotifyNowayAssert(const char* filename, unsigned line)
273273
{
274274
codeSize = comp->info.compILCodeSize;
275275
minOpts = comp->opts.IsMinOptsSet() ? comp->opts.MinOpts() : -1;
276-
lastPhase = PhaseNames[comp->previousCompletedPhase];
276+
lastPhase = PhaseNames[comp->mostRecentlyActivePhase];
277277
}
278278

279279
CacheCurrentMethodInfo();

src/coreclr/src/jit/lower.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,19 +5326,6 @@ void Lowering::DoPhase()
53265326
// impact of any dead code removal. Note this may leave us with
53275327
// tracked vars that have zero refs.
53285328
comp->lvaComputeRefCounts(isRecompute, setSlotNumbers);
5329-
5330-
#ifdef DEBUG
5331-
JITDUMP("Liveness pass finished after lowering, IR:\n");
5332-
if (VERBOSE)
5333-
{
5334-
comp->fgDispBasicBlocks(true);
5335-
}
5336-
5337-
for (BasicBlock* block = comp->fgFirstBB; block; block = block->bbNext)
5338-
{
5339-
assert(LIR::AsRange(block).CheckLIR(comp, true));
5340-
}
5341-
#endif
53425329
}
53435330

53445331
#ifdef DEBUG

src/coreclr/src/jit/lower.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1919
#include "lsra.h"
2020
#include "sideeffects.h"
2121

22-
class Lowering : public Phase
22+
class Lowering final : public Phase
2323
{
2424
public:
2525
inline Lowering(Compiler* compiler, LinearScanInterface* lsra)
26-
: Phase(compiler, "Lowering", PHASE_LOWERING), vtableCallTemp(BAD_VAR_NUM)
26+
: Phase(compiler, PHASE_LOWERING), vtableCallTemp(BAD_VAR_NUM)
2727
{
2828
m_lsra = (LinearScan*)lsra;
2929
assert(m_lsra);

src/coreclr/src/jit/objectalloc.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,12 @@ class ObjectAllocator final : public Phase
7676
//===============================================================================
7777

7878
inline ObjectAllocator::ObjectAllocator(Compiler* comp)
79-
: Phase(comp, "Allocate Objects", PHASE_ALLOCATE_OBJECTS)
79+
: Phase(comp, PHASE_ALLOCATE_OBJECTS)
8080
, m_IsObjectStackAllocationEnabled(false)
8181
, m_AnalysisDone(false)
8282
, m_bitVecTraits(comp->lvaCount, comp)
8383
, m_HeapLocalToStackLocalMap(comp->getAllocator())
8484
{
85-
// Disable checks since this phase runs before fgComputePreds phase.
86-
// Checks are not expected to pass before fgComputePreds.
87-
doChecks = false;
8885
m_EscapingPointers = BitVecOps::UninitVal();
8986
m_PossiblyStackPointingPointers = BitVecOps::UninitVal();
9087
m_DefinitelyStackPointingPointers = BitVecOps::UninitVal();

0 commit comments

Comments
 (0)