From 28aa7454d1d38b678bcabc21a17f2ac1d2f9ca7a Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Wed, 4 Dec 2024 15:17:48 +0100 Subject: [PATCH 1/2] JIT: Delay creation of scratch block needed by backend The frontend created a scratch BB required by the backend. Having a long term invariant like this seems unnecessary; instead create the scratch BB in lowering to shorten the time we have this invariant. --- src/coreclr/jit/flowgraph.cpp | 9 --------- src/coreclr/jit/lower.cpp | 11 +++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/coreclr/jit/flowgraph.cpp b/src/coreclr/jit/flowgraph.cpp index 52df8bab32b4f8..9d1c048a1253c8 100644 --- a/src/coreclr/jit/flowgraph.cpp +++ b/src/coreclr/jit/flowgraph.cpp @@ -2302,15 +2302,6 @@ PhaseStatus Compiler::fgAddInternal() // type with a runtime lookup madeChanges |= fgCreateFiltersForGenericExceptions(); - // The backend requires a scratch BB into which it can safely insert a P/Invoke method prolog if one is - // required. Similarly, we need a scratch BB for poisoning and when we have Swift parameters to reassemble. - // Create it here. - if (compMethodRequiresPInvokeFrame() || compShouldPoisonFrame() || lvaHasAnySwiftStackParamToReassemble()) - { - madeChanges |= fgEnsureFirstBBisScratch(); - fgFirstBB->SetFlags(BBF_DONT_REMOVE); - } - /* VSW441487 diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index ce829a0730db70..015920bfe6f2b1 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -7812,6 +7812,17 @@ void Lowering::WidenSIMD12IfNecessary(GenTreeLclVarCommon* node) PhaseStatus Lowering::DoPhase() { + // We need a scratch BB into which it can safely insert a P/Invoke method + // prolog if one is required. Similarly, we need a scratch BB for poisoning + // and when we have Swift parameters to reassemble. Create it here. + if (comp->compMethodRequiresPInvokeFrame() || comp->compShouldPoisonFrame() || comp->lvaHasAnySwiftStackParamToReassemble()) + { + if (comp->fgEnsureFirstBBisScratch()) + { + comp->fgInvalidateDfsTree(); + } + } + // If we have any PInvoke calls, insert the one-time prolog code. We'll insert the epilog code in the // appropriate spots later. NOTE: there is a minor optimization opportunity here, as we still create p/invoke // data structures and setup/teardown even if we've eliminated all p/invoke calls due to dead code elimination. From b56b75961d02e256813b70e5ea5c5defda41b2d6 Mon Sep 17 00:00:00 2001 From: Jakob Botsch Nielsen Date: Wed, 4 Dec 2024 16:13:55 +0100 Subject: [PATCH 2/2] Run jit-format --- src/coreclr/jit/lower.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index 015920bfe6f2b1..5120009c5564cb 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -7815,7 +7815,8 @@ PhaseStatus Lowering::DoPhase() // We need a scratch BB into which it can safely insert a P/Invoke method // prolog if one is required. Similarly, we need a scratch BB for poisoning // and when we have Swift parameters to reassemble. Create it here. - if (comp->compMethodRequiresPInvokeFrame() || comp->compShouldPoisonFrame() || comp->lvaHasAnySwiftStackParamToReassemble()) + if (comp->compMethodRequiresPInvokeFrame() || comp->compShouldPoisonFrame() || + comp->lvaHasAnySwiftStackParamToReassemble()) { if (comp->fgEnsureFirstBBisScratch()) {