Skip to content

Commit

Permalink
JIT: remove phis right after optimizing (#110821)
Browse files Browse the repository at this point in the history
They serve no useful purpose once SSA is gone, and they potentially confuse
other parts of the JIT.

Was also going to make the PHIs zero cost, but this apparently was already
done (not sure when/how).

Closes #88841
  • Loading branch information
AndyAyersMS authored Dec 18, 2024
1 parent 4822cc0 commit 43f7d32
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5006,6 +5006,9 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
// Iterate if requested, resetting annotations first.
if (opts.optRepeatIteration == opts.optRepeatCount)
{
// If we're done optimizing, just remove the PHIs
//
fgResetForSsa(/* deepClean */ false);
break;
}

Expand Down Expand Up @@ -5849,7 +5852,7 @@ void Compiler::ResetOptAnnotations()
{
assert(opts.optRepeat);
assert(JitConfig.JitOptRepeatCount() > 0);
fgResetForSsa();
fgResetForSsa(/* deepClean */ true);
vnStore = nullptr;
m_blockToEHPreds = nullptr;
m_dominancePreds = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5725,7 +5725,7 @@ class Compiler
PhaseStatus fgSsaBuild();

// Reset any data structures to the state expected by "fgSsaBuild", so it can be run again.
void fgResetForSsa();
void fgResetForSsa(bool deepClean);

unsigned fgSsaPassesCompleted = 0; // Number of times fgSsaBuild has been run.
bool fgSsaValid = false; // True if SSA info is valid and can be cross-checked versus IR
Expand Down
52 changes: 35 additions & 17 deletions src/coreclr/jit/ssabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ PhaseStatus Compiler::fgSsaBuild()
// If this is not the first invocation, reset data structures for SSA.
if (fgSsaPassesCompleted > 0)
{
fgResetForSsa();
fgResetForSsa(/* deepClean */ true);
}

SsaBuilder builder(this);
Expand All @@ -29,21 +29,36 @@ PhaseStatus Compiler::fgSsaBuild()
return PhaseStatus::MODIFIED_EVERYTHING;
}

void Compiler::fgResetForSsa()
//------------------------------------------------------------------------
// fgResetForSsa: remove SSA artifacts
//
// Arguments:
// deepClean - if true, remove all SSA artifacts
// if false, just remove PHIs
//
// Notes:
// deepCleaning is needed in order to rebuild SSA.
//
void Compiler::fgResetForSsa(bool deepClean)
{
for (unsigned i = 0; i < lvaCount; ++i)
{
lvaTable[i].lvPerSsaData.Reset();
}
lvMemoryPerSsaData.Reset();
for (MemoryKind memoryKind : allMemoryKinds())
{
m_memorySsaMap[memoryKind] = nullptr;
}
JITDUMP("Removing %s\n", deepClean ? "all SSA artifacts" : "PHI functions");

if (m_outlinedCompositeSsaNums != nullptr)
if (deepClean)
{
m_outlinedCompositeSsaNums->Reset();
for (unsigned i = 0; i < lvaCount; ++i)
{
lvaTable[i].lvPerSsaData.Reset();
}
lvMemoryPerSsaData.Reset();
for (MemoryKind memoryKind : allMemoryKinds())
{
m_memorySsaMap[memoryKind] = nullptr;
}

if (m_outlinedCompositeSsaNums != nullptr)
{
m_outlinedCompositeSsaNums->Reset();
}
}

for (BasicBlock* const blk : Blocks())
Expand All @@ -63,13 +78,16 @@ void Compiler::fgResetForSsa()
}
}

for (Statement* const stmt : blk->Statements())
if (deepClean)
{
for (GenTree* const tree : stmt->TreeList())
for (Statement* const stmt : blk->Statements())
{
if (tree->IsAnyLocal())
for (GenTree* const tree : stmt->TreeList())
{
tree->AsLclVarCommon()->SetSsaNum(SsaConfig::RESERVED_SSA_NUM);
if (tree->IsAnyLocal())
{
tree->AsLclVarCommon()->SetSsaNum(SsaConfig::RESERVED_SSA_NUM);
}
}
}
}
Expand Down

0 comments on commit 43f7d32

Please sign in to comment.