4040import java .util .TreeSet ;
4141import java .util .function .Function ;
4242
43+ import jdk .graal .compiler .phases .PhaseSuite ;
4344import org .graalvm .collections .EconomicSet ;
4445import org .graalvm .word .LocationIdentity ;
4546
@@ -233,9 +234,28 @@ public SchedulePhase(SchedulingStrategy strategy, boolean immutableGraph, boolea
233234 /**
234235 * Last schedule to be run in any phase plan in the compiler. After this no further
235236 * optimizations or transformations must happen that would require a re-scheduling of the graph.
237+ *
238+ * Optionally, a post-processing phase can be provided via the constructor to perform low-tier
239+ * cleanups that rely on a stable schedule. Such a phase must be schedule-preserving and must
240+ * not introduce changes that require another scheduling pass. This is typically used to run
241+ * local transformations (e.g., {@link SchedulingStrategy#BASIC_BLOCK_LOCAL_SCHEDULING}) as part
242+ * of a {@link PhaseSuite} plan.
236243 */
237244 public static class FinalSchedulePhase extends BasePhase <LowTierContext > {
238245
246+ private final BasePhase <LowTierContext > postProcessingPhase ;
247+
248+ /**
249+ * Creates a FinalSchedulePhase without any post-processing.
250+ */
251+ public FinalSchedulePhase () {
252+ this (null );
253+ }
254+
255+ public FinalSchedulePhase (final BasePhase <LowTierContext > postProcessingPhase ) {
256+ this .postProcessingPhase = postProcessingPhase ;
257+ }
258+
239259 @ Override
240260 public Optional <NotApplicable > notApplicableTo (GraphState graphState ) {
241261 return NotApplicable .ifAny (
@@ -246,7 +266,11 @@ public Optional<NotApplicable> notApplicableTo(GraphState graphState) {
246266 @ Override
247267 protected void run (StructuredGraph graph , LowTierContext context ) {
248268 new SchedulePhase (SchedulePhase .SchedulingStrategy .LATEST_OUT_OF_LOOPS ).apply (graph , context );
249-
269+ // Apply the optional, schedule-preserving post-processing phase. This must not perform
270+ // transformations that would invalidate or require recomputing the final schedule.
271+ if (postProcessingPhase != null ) {
272+ postProcessingPhase .apply (graph , context );
273+ }
250274 }
251275
252276 @ Override
0 commit comments