Skip to content

fix[next]: pin the domain of references selected by a field-level if - #2987

Draft
havogt wants to merge 6 commits into
GridTools:mainfrom
havogt:test-tmp-domain-if
Draft

havogt wants to merge 6 commits into
GridTools:mainfrom
havogt:test-tmp-domain-if

Conversation

@havogt

@havogt havogt commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Temporary extraction (create_global_tmps) produced accesses outside a field's domain when a field-level if on a runtime scalar selects a field reference. Both test programs are valid for either value of the flag (all fields are large enough for both paths); without the fix they fail with flag=True.

  • test_if_selecting_field_in_shifted_concat_where (read): t = a if flag else a + 1.0 feeds a concat_where that is read shifted. concat_where.transform_to_as_fieldop turns the restriction of the concat_where branch into a condition inside a stencil, which domain inference can not see; the re-inference in create_global_tmps then gives the reference a the extended domain of the lowered concat_where, and a is copied into a temporary over K: [-1, n) although it is only read on [0, n).
  • test_if_selecting_temporary_on_larger_domain (write): u if flag else b, with u extracted into a temporary on a larger domain. After extraction the branch is a reference to that temporary and carries its allocation domain, so the output temporary, declared on [0, n), is written on [0, n+1).

Both have one cause: a field reference that is a branch of a field-level if_ holds its domain only in its annex, while an as_fieldop holds it as an argument. The new pass PinIfBranchDomains (pin_if_branch_domains.py) wraps such references, and tuples of them, into as_fieldop(deref, <inferred domain>) right after domain inference, before transform_to_as_fieldop, so the domain is part of the IR and neither the re-inference nor temporary extraction can replace it. It is wired only when temporaries are extracted (apply_common_transforms with extract_temporaries); apply_fieldview_transforms extracts none. A wrapped reference was already copied into a temporary before (tmp ← a), so the pass adds no kernels or temporaries in these cases.

The tests are pinned to roundtrip_with_temporaries, which extracts the same temporaries and bounds-checks every access. On gtfn_cpu both overrun by one element without failing; with larger extents the same pattern corrupts the heap (found in icon4py's fused tracer advection, where the extent is 5 levels).

Tested: the new tests and test_pin_if_branch_domains.py, test_global_tmps.py, test_domain_inference.py, test_cse.py; all ffront feature and multi-feature tests on gtfn_cpu and roundtrip (774 passed); the icon4py program compute_tracer_advection_after_horizontal_limiter (branch tracer_refactoring_2) on gtfn_cpu, which crashed with corrupted size vs. prev_size before and passes now. Every iterator-level test xfails on roundtrip_with_temporaries ((ALL, XFAIL) in definitions.py), so that backend gives no further coverage.

The history keeps an earlier fix in _transform_if and its revert.

Requirements

  • All fixes and/or new features come with corresponding tests.
  • Important design decisions have been documented in the appropriate ADR inside the docs/development/ADRs/ folder.

Two cases where temporary extraction (create_global_tmps) produces a domain
larger than the one a field or temporary is defined on, when a field-level
if selects between branches:

- read: a program argument selected by the if is copied into a temporary
  over the extended domain of the concat_where it feeds;
- write: a temporary on the output domain is written over the larger
  allocation domain of the temporary it selects.

Pinned to roundtrip_with_temporaries, which bounds-checks every access;
gtfn_cpu overruns by one element without failing.
@havogt

havogt commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

🤖 Written by an agent on behalf of @havogt

…f the if

_transform_if turns `tmp ← if_(c, x, y)` into an IfStmt whose SetAt domains
_populate_and_homogenize_domains takes from each branch's annex. For a
reference to another temporary that annex is the referenced temporary's
allocation domain, which can be larger than the domain `tmp` is declared
on, so `tmp` was written out of bounds.
@havogt havogt changed the title test[next]: temporaries accessed outside their domain under a runtime if fix[next]: temporary written outside its domain when selected by a runtime if Nov 13, 2026
if isinstance(stmt, itir.SetAt) and cpm.is_call_to(stmt.expr, "if_"):
cond, true_val, false_val = stmt.expr.args

# A reference to a temporary carries the domain the temporary is allocated on, which

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think the conclusion is correct, I am not sure if the implementation is, especially the condition on SymRefs only. I would expect the fix should be earlier, i.e. the SymRef should already carry the correct domain?!

…he if's

Generalizes the previous commit from SymRef branches to every branch kind: a
branch's domain is the domain it is evaluated on, which can be larger than the
domain of the if_, but it must not be written beyond either. Giving every
branch the if_'s domain instead would make an as_fieldop branch execute on an
over-extended if_ domain and read its inputs out of bounds.
if isinstance(stmt, itir.SetAt) and cpm.is_call_to(stmt.expr, "if_"):
cond, true_val, false_val = stmt.expr.args

# The domain of a branch is the domain it is evaluated on, e.g. for a reference to a

@havogt havogt Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worse than the previous implementation. For humans: only look at the reproducers for now.

A field reference that is a branch of a field-level if_ holds its domain only
in its annex. Two later steps replace that annex: after
concat_where.transform_to_as_fieldop the re-inference in create_global_tmps
gives the reference the extended domain of the lowered concat_where (read
outside the field), and after temporary extraction the reference carries the
allocation domain of the referenced temporary (written outside the target).

PinIfBranchDomains wraps such references, and tuples of them, into
as_fieldop(deref, <inferred domain>) right after domain inference, so the
domain is part of the IR.
@havogt havogt changed the title fix[next]: temporary written outside its domain when selected by a runtime if fix[next]: pin the domain of references selected by a field-level if Nov 13, 2026
such references, is wrapped into an `as_fieldop(deref, ...)` on its domain from
`annex.domain`:

if_(c, a, b) -> if_(c, as_fieldop(deref, D_a)(a), as_fieldop(deref, D_b)(b))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need a domain argument at the if_ level similar to the as_fieldop domain?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant