Skip to content

compiler: Infer the direction of an unsplittable loop from self-dependences - #3132

Open
acdanila wants to merge 1 commit into
devitocodes:mainfrom
acdanila:compiler-schedule-direction
Open

acdanila wants to merge 1 commit into
devitocodes:mainfrom
acdanila:compiler-schedule-direction

Conversation

@acdanila

@acdanila acdanila commented Dec 1, 2026

Copy link
Copy Markdown

Addresses the buffered case of #2780, following the discussion there.

Problem

Schedule.callback computes scope.d_flow and scope.d_anti while the Clusters still have direction Any, so the distances are those of a Forward loop. Consider an adjoint-like Operator:

Eq(u, u + src)                       # update u[t]
Eq(u.backward, 2*u - u.forward + …)  # u[t-1] = f(u[t], u[t+1])
Inc(g, u.backward*v)                 # read u[t-1]

Read as a forward loop, these Clusters carry both flow and anti dependences along time. Since #3008, a time loop with sub-iterators is no longer split in this situation, but the ambiguity is still resolved by

if maybe_break:
    idir.update({d: Forward for d in candidates if d.root in scope.d_flow.cause})

so the whole Operator runs forward, although it steps u.backward. On main, the #2780 example then gives sum(g) 5.137421e7 instead of 2.624460e7.

Change

When maybe_break holds and the loop cannot be split (sub-iterators), the direction is inferred from the dependences of each Cluster on itself (Schedule._recurrence_direction):

  • For each Cluster, build Scope(c.exprs) and keep the dependences carried by dim, excluding reductions (is_carried(dim) and not is_reduce(dim)).
  • If they are all flow dependences, the Cluster requires Forward, e.g. u[t+1] = f(u[t]). If they are all anti dependences (d_anti_logical), it requires Backward, e.g. u[t-1] = f(u[t]). A Cluster without carried self-dependences requires nothing.
  • If all Clusters that require a direction agree, that direction is used. Otherwise (a Cluster with both kinds, disagreeing Clusters, or no requirement) the existing rule applies.

For the example above, only the stencil Cluster requires a direction (Backward: 0 carried flow, 6 carried anti dependences). The injection and the Inc (a reduction) require none. In test_no_time_fission_reversing_war, both Clusters require Forward, so its behaviour is unchanged. Nothing in the analysis refers to time specifically; the fission decision is not changed.

Tests

  • New TestLoopScheduling::test_no_time_fission_backward_recurrence: checks for a single Backward time loop and compares against a NumPy reference computed one time step at a time. It fails on main and passes with this change.
  • All TestLoopScheduling tests pass, including those added in compiler: prevent schedule from breaking loop over subiterators #3008.
  • The Time loop splitting #2780 example (buffered): one backward loop, sum(g) 2.624460e7, equal to running the Operator one time step per call.
  • An adjoint with sparse injection, a ConditionalDimension-saved forward field and two Incs (5 variants): all equal to one-time-step-per-call execution; main gives NaN for the 2 variants that read u.backward after the stencil.
  • CPU test suite (-m "not parallel", without the GPU and MPI test files): 3881 passed. The two test_dse.py::TestAliases::test_blocking_options cases fail in our environment, with and without this change.

Open question: full storage

With u saved in full (save=nt) there are no sub-iterators, so Schedule still splits the loop and the Inc reads u[t-1] after the update at step t-1: sum(g) 2.624523e7. With buffering, it reads the value before that update: 2.624460e7, which is also what one-time-step-per-call execution gives with either storage. So the same equations currently give different results depending on storage. This PR does not change the fission decision. Which semantics is intended for a slot written by two equations at different steps? Depending on the answer, the full-storage case could be addressed in the same dependence analysis, either by not splitting or by raising an error where a split changes the result.

🤖 Generated with Claude Code

…dences

Schedule computes flow and anti dependences before any iteration direction is
known, that is as if the loop ran Forward. A backward recurrence, such as
`u[t-1] = f(u[t], u[t+1])`, then shows anti dependences, and a Cluster updating
`u[t]` before it adds flow dependences. When the iteration Dimension has
sub-iterators, the Clusters cannot be scheduled over different IterationSpaces
(devitocodes#3008), but the ambiguity is still resolved by forcing Forward, so a backward
Operator runs forward (devitocodes#2780).

In that case, infer the direction from the dependences of each Cluster on
itself instead: Forward if its carried self-dependences are all flow
dependences, Backward if they are all anti dependences. Reductions and Clusters
without carried self-dependences impose no direction. If a Cluster has both, or
the Clusters disagree, the previous rule applies.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants