Repository navigation
Conversation
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.
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
commented
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 |
Contributor
Author
There was a problem hiding this comment.
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.
havogt
commented
Nov 13, 2026
| 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 |
Contributor
Author
There was a problem hiding this comment.
I think it's worse than the previous implementation. For humans: only look at the reproducers for now.
…in and the if's" This reverts commit c81a716.
…domain of the if" This reverts commit cd915cf.
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
commented
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)) |
Contributor
Author
There was a problem hiding this comment.
Maybe we need a domain argument at the if_ level similar to the as_fieldop domain?
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Temporary extraction (
create_global_tmps) produced accesses outside a field's domain when a field-levelifon 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 withflag=True.test_if_selecting_field_in_shifted_concat_where(read):t = a if flag else a + 1.0feeds aconcat_wherethat is read shifted.concat_where.transform_to_as_fieldopturns the restriction of theconcat_wherebranch into a condition inside a stencil, which domain inference can not see; the re-inference increate_global_tmpsthen gives the referenceathe extended domain of the loweredconcat_where, andais copied into a temporary overK: [-1, n)although it is only read on[0, n).test_if_selecting_temporary_on_larger_domain(write):u if flag else b, withuextracted 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 anas_fieldopholds it as an argument. The new passPinIfBranchDomains(pin_if_branch_domains.py) wraps such references, and tuples of them, intoas_fieldop(deref, <inferred domain>)right after domain inference, beforetransform_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_transformswithextract_temporaries);apply_fieldview_transformsextracts 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. Ongtfn_cpuboth 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 ongtfn_cpuandroundtrip(774 passed); the icon4py programcompute_tracer_advection_after_horizontal_limiter(branchtracer_refactoring_2) ongtfn_cpu, which crashed withcorrupted size vs. prev_sizebefore and passes now. Every iterator-level test xfails onroundtrip_with_temporaries((ALL, XFAIL)indefinitions.py), so that backend gives no further coverage.The history keeps an earlier fix in
_transform_ifand its revert.Requirements