Skip to content

JIT: Disallow flowgraph modifications during VN opts #115197

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

Merged
merged 3 commits into from
Apr 30, 2025
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
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5417,7 +5417,7 @@ class Compiler

void fgMergeBlockReturn(BasicBlock* block);

bool fgMorphBlockStmt(BasicBlock* block, Statement* stmt DEBUGARG(const char* msg), bool invalidateDFSTreeOnFGChange = true);
bool fgMorphBlockStmt(BasicBlock* block, Statement* stmt DEBUGARG(const char* msg), bool allowFGChange = true, bool invalidateDFSTreeOnFGChange = true);
void fgMorphStmtBlockOps(BasicBlock* block, Statement* stmt);

bool gtRemoveTreesAfterNoReturnCall(BasicBlock* block, Statement* stmt);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/earlyprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ bool Compiler::optFoldNullCheck(GenTree* tree, LocalNumberToNullCheckTreeMap* nu

// Re-morph the statement.
Statement* curStmt = compCurStmt;
fgMorphBlockStmt(compCurBB, nullCheckStmt DEBUGARG("optFoldNullCheck"));
fgMorphBlockStmt(compCurBB, nullCheckStmt DEBUGARG("optFoldNullCheck"), /* allowFGChange */ false);
optRecordSsaUses(nullCheckStmt->GetRootNode(), compCurBB);
compCurStmt = curStmt;

Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12265,6 +12265,7 @@ Compiler::FoldResult Compiler::fgFoldConditional(BasicBlock* block)
// block - block containing the statement
// stmt - statement to morph
// msg - string to identify caller in a dump
// allowFGChange - whether or not the flow graph can be changed
// invalidateDFSTreeOnFGChange - whether or not the DFS tree should be invalidated
// by this function if it makes a flow graph change
//
Expand All @@ -12277,6 +12278,7 @@ Compiler::FoldResult Compiler::fgFoldConditional(BasicBlock* block)
//
bool Compiler::fgMorphBlockStmt(BasicBlock* block,
Statement* stmt DEBUGARG(const char* msg),
bool allowFGChange,
bool invalidateDFSTreeOnFGChange)
{
assert(block != nullptr);
Expand Down Expand Up @@ -12326,7 +12328,7 @@ bool Compiler::fgMorphBlockStmt(BasicBlock* block,
bool removedStmt = fgCheckRemoveStmt(block, stmt);

// Or this is the last statement of a conditional branch that was just folded?
if (!removedStmt && (stmt->GetNextStmt() == nullptr) && !fgRemoveRestOfBlock)
if (allowFGChange && !removedStmt && (stmt->GetNextStmt() == nullptr) && !fgRemoveRestOfBlock)
{
FoldResult const fr = fgFoldConditional(block);
if (invalidateDFSTreeOnFGChange && (fr != FoldResult::FOLD_DID_NOTHING))
Expand Down Expand Up @@ -12369,7 +12371,7 @@ bool Compiler::fgMorphBlockStmt(BasicBlock* block,
//
// For compDbgCode, we prepend an empty BB as the firstBB, it is BBJ_ALWAYS.
// We should not convert it to a ThrowBB.
if ((block != fgFirstBB) || !fgFirstBB->HasFlag(BBF_INTERNAL))
if (allowFGChange && ((block != fgFirstBB) || !fgFirstBB->HasFlag(BBF_INTERNAL)))
{
// Convert block to a throw bb, or make it rarely run if already a throw.
//
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/jit/redundantbranchopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,8 @@ bool Compiler::optRedundantBranch(BasicBlock* const block)

JITDUMP("\nRedundant branch opt in " FMT_BB ":\n", block->bbNum);

fgMorphBlockStmt(block, stmt DEBUGARG(__FUNCTION__), /* invalidateDFSTreeOnFGChange */ false);
fgMorphBlockStmt(block, stmt DEBUGARG(__FUNCTION__), /* allowFGChange */ true,
/* invalidateDFSTreeOnFGChange */ false);
Metrics.RedundantBranchesEliminated++;
return true;
}
Expand Down
6 changes: 1 addition & 5 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10865,11 +10865,7 @@ PhaseStatus Compiler::fgValueNumber()
}

assert(m_dfsTree != nullptr);

if (m_loops == nullptr)
{
m_loops = FlowGraphNaturalLoops::Find(m_dfsTree);
}
assert(m_loops != nullptr);

m_blockToLoop = BlockToNaturalLoopMap::Build(m_loops);
// Compute the side effects of loops.
Expand Down
Loading