---
# System prepended metadata

title: Support for horizontal constrained iteration
tags: [cycle 1]

---

# Support for horizontal constrained iteration

###### tags: `cycle 1`

## Problem

[GDP 2](https://github.com/GridTools/gt4py/pull/24) describes a syntax for restricting the limits of horizontal iteration on sets of statements. [#227](https://github.com/GridTools/gt4py/pull/227) implements this in the frontend, and follow-up PRs implement it in the v1 backends.

## Appetite

Half a cycle.

## Solution

### Frontend

Review and merge the feature in the existing PR.

### GTIR/OIR Representation

The existing IRs are ideally-suited to representing region code in the HorizontalExecutions. No new nodes are required.

To represent this, the parallel interval expressions in the definition IR need to be converted to a mask expression and put into a HorizontalExecution together with the stmts in the body.

### Extent Analysis

One unique aspect of regions is that the code may not execute depending on the mask expression. Similarly, if the mask only evaluates to true outside the compute domain, it still may evaluate if that stage extends computation.

The elegant extent analysis will no longer be quite as simple, since these are "provisional" statements that may not impart new extents on fields, even if they are accessed with an offset.

```python=3.8
extents: Dict[str, gtcpp.GTExtent] = (
    node.iter_tree()
    .if_isinstance(gtcpp.AccessorRef)
    .reduceby(
        (lambda extent, accessor_ref: extent + accessor_ref.offset),
        "name",
        init=gtcpp.GTExtent.zero(),
        as_dict=True,
    )
)
```

This extent analysis trick is implemented in [#228](https://github.com/GridTools/gt4py/pull/228).

1. Convert definition ir parallel intervals (list of AxisIntervals) to a mask expression and create HorizontalExecution nodes in gtir.
2. Add a pass on OIR that removes unused horizontal constrained statements.
3. Move extent analysis into the OIR.
4. Correctly compute extents from horizontally constrained executions.

### Backend

In terms of backend code generation, this reduces to an if statement, which is already implemented.

### No-goals

- Changing the frontend feature

### Time sinks

- Computing extents for all horizontally constrained executions regardless of the mask will be difficult. Could limit to convex constraints, etc.
- Extent analysis, which is critical for regions to work correctly, is currently delayed until the backend IRs. This means the code cannot remove unused regions until then. Could consider moving extents to OIR.