Skip to content

Commit

Permalink
JIT: move second pass of EH opts earlier (#111364)
Browse files Browse the repository at this point in the history
to avoid messing up loop exit canonicalization (which is sensitive
to loops with internal EH flow).

Fixed #110959.

Also, remove some of the ceremony around `fgModified` -- seems like this
is no longer useful/needed.

Defer deciding if we need a PSPSym until we create funclets. This removes
unused PSPSym allocations in some cases. Also, there is no benefit to
tracking the PSPSym, so stop doing that.
  • Loading branch information
AndyAyersMS authored Jan 14, 2025
1 parent 9fea441 commit a05d34b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 45 deletions.
24 changes: 12 additions & 12 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4825,6 +4825,18 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_COMPUTE_BLOCK_WEIGHTS, &Compiler::fgComputeBlockWeights);

// Try again to remove empty try finally/fault clauses
//
DoPhase(this, PHASE_EMPTY_FINALLY_2, &Compiler::fgRemoveEmptyFinally);

// Remove empty try regions (try/finally)
//
DoPhase(this, PHASE_EMPTY_TRY_2, &Compiler::fgRemoveEmptyTry);

// Remove empty try regions (try/catch/fault)
//
DoPhase(this, PHASE_EMPTY_TRY_CATCH_FAULT_2, &Compiler::fgRemoveEmptyTryCatchOrTryFault);

// Invert loops
//
DoPhase(this, PHASE_INVERT_LOOPS, &Compiler::optInvertLoops);
Expand Down Expand Up @@ -4860,18 +4872,6 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
//
DoPhase(this, PHASE_UNROLL_LOOPS, &Compiler::optUnrollLoops);

// Try again to remove empty try finally/fault clauses
//
DoPhase(this, PHASE_EMPTY_FINALLY_2, &Compiler::fgRemoveEmptyFinally);

// Remove empty try regions (try/finally)
//
DoPhase(this, PHASE_EMPTY_TRY_2, &Compiler::fgRemoveEmptyTry);

// Remove empty try regions (try/catch/fault)
//
DoPhase(this, PHASE_EMPTY_TRY_CATCH_FAULT_2, &Compiler::fgRemoveEmptyTryCatchOrTryFault);

// Compute dominators and exceptional entry blocks
//
DoPhase(this, PHASE_COMPUTE_DOMINATORS, &Compiler::fgComputeDominators);
Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/jit/fgehopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2458,12 +2458,6 @@ PhaseStatus Compiler::fgTailMergeThrows()
assert(numCandidates < optNoReturnCallCount);
optNoReturnCallCount -= numCandidates;

// If we altered flow, reset fgModified. Given where we sit in the
// phase list, flow-dependent side data hasn't been built yet, so
// nothing needs invalidation.
//
assert(fgModified);
fgModified = false;
return PhaseStatus::MODIFIED_EVERYTHING;
}

Expand Down
6 changes: 0 additions & 6 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6780,12 +6780,6 @@ PhaseStatus Compiler::fgHeadTailMerge(bool early)
madeChanges |= fgHeadMerge(block, early);
}

// If we altered flow, reset fgModified. Given where we sit in the
// phase list, flow-dependent side data hasn't been built yet, so
// nothing needs invalidation.
//
fgModified = false;

return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
}

Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2989,6 +2989,18 @@ PhaseStatus Compiler::fgCreateFunclets()
assert(UsesFunclets());
assert(!fgFuncletsCreated);

// Allocate the PSPSym, if needed. PSPSym is not used by the NativeAOT ABI
if (!IsTargetAbi(CORINFO_NATIVEAOT_ABI))
{
if (ehNeedsPSPSym())
{
lvaPSPSym = lvaGrabTempWithImplicitUse(false DEBUGARG("PSPSym"));
LclVarDsc* lclPSPSym = lvaGetDesc(lvaPSPSym);
lclPSPSym->lvType = TYP_I_IMPL;
lvaSetVarDoNotEnregister(lvaPSPSym DEBUGARG(DoNotEnregisterReason::VMNeedsStackAddr));
}
}

fgCreateFuncletPrologBlocks();

unsigned XTnum;
Expand Down
19 changes: 7 additions & 12 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4073,6 +4073,13 @@ void Compiler::lvaSortByRefCount()
}
#endif

// No benefit in tracking the PSPSym (if any)
//
if (lclNum == lvaPSPSym)
{
varDsc->lvTracked = 0;
}

// Are we not optimizing and we have exception handlers?
// if so mark all args and locals "do not enregister".
//
Expand Down Expand Up @@ -4747,18 +4754,6 @@ PhaseStatus Compiler::lvaMarkLocalVars()

#endif // FEATURE_EH_WINDOWS_X86

// PSPSym is not used by the NativeAOT ABI
if (!IsTargetAbi(CORINFO_NATIVEAOT_ABI))
{
if (UsesFunclets() && ehNeedsPSPSym())
{
lvaPSPSym = lvaGrabTempWithImplicitUse(false DEBUGARG("PSPSym"));
LclVarDsc* lclPSPSym = lvaGetDesc(lvaPSPSym);
lclPSPSym->lvType = TYP_I_IMPL;
lvaSetVarDoNotEnregister(lvaPSPSym DEBUGARG(DoNotEnregisterReason::VMNeedsStackAddr));
}
}

#ifdef JIT32_GCENCODER
// LocAllocSPvar is only required by the implicit frame layout expected by the VM on x86. Whether
// a function contains a Localloc is conveyed in the GC information, in the InfoHdrSmall.localloc
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,6 @@ bool Compiler::optInvertWhileLoop(BasicBlock* block)
PhaseStatus Compiler::optInvertLoops()
{
noway_assert(opts.OptimizationEnabled());
noway_assert(fgModified == false);

#if defined(OPT_CONFIG)
if (!JitConfig.JitDoLoopInversion())
Expand Down Expand Up @@ -2387,13 +2386,6 @@ PhaseStatus Compiler::optInvertLoops()
}
}

if (fgModified)
{
// Reset fgModified here as we've done a consistent set of edits.
//
fgModified = false;
}

return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
}

Expand All @@ -2410,7 +2402,6 @@ PhaseStatus Compiler::optInvertLoops()
PhaseStatus Compiler::optOptimizeFlow()
{
noway_assert(opts.OptimizationEnabled());
noway_assert(fgModified == false);

fgUpdateFlowGraph(/* doTailDuplication */ true);
fgReorderBlocks(/* useProfile */ false);
Expand Down

0 comments on commit a05d34b

Please sign in to comment.