gt4py.cartesian.gtc.passes.oir_optimizations package

Submodules

gt4py.cartesian.gtc.passes.oir_optimizations.caches module

class gt4py.cartesian.gtc.passes.oir_optimizations.caches.FillFlushToLocalKCaches[source]

Bases: NodeTranslator, VisitorWithSymbolTableTrait

Converts fill and flush k-caches to local k-caches.

For each cached field, the following actions are performed: 1. A new locally-k-cached temporary is introduced. 2. All accesses to the original field are replaced by accesses to this temporary. 3. Loop sections are split where necessary to allow single-level loads wherever possible. 3. Fill statements from the original field to the temporary are introduced. 4. Flush statements from the temporary to the original field are introduced.

static _fill_limits(loop_order: LoopOrder, section: VerticalLoopSection) → Dict[str, Tuple[int, int]][source]

Direction-normalized min and max read accesses for each accessed field.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

Returns:

A dict, mapping field names to min and max read offsets relative to loop order (i.e., positive means in the direction of the loop order).

classmethod _fill_stmts(loop_order: LoopOrder, section: VerticalLoopSection, filling_fields: Dict[str, str], first_unfilled: Dict[str, int], symtable: Dict[str, Any]) → Tuple[List[AssignStmt], Dict[str, int]][source]

Generate fill statements for the given loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • filling_fields – mapping from field names to cache names.

  • first_unfilled – direction-normalized offset of the first unfilled cache entry for each field.

Returns:

A list of fill statements and an updated first_unfilled map.

classmethod _flush_stmts(loop_order: LoopOrder, section: VerticalLoopSection, flushing_fields: Dict[str, str], symtable: Dict[str, Any]) → List[AssignStmt][source]

Generate flush statements for the given loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • flushing_fields – mapping from field names to cache names.

Returns:

A list of flush statements.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

static _requires_splitting(interval: Interval) → bool[source]

Check if an interval is larger than one level and thus may require splitting.

static _split_entry_level(loop_order: LoopOrder, section: VerticalLoopSection, new_symbol_name: Callable[[str], str]) → Tuple[VerticalLoopSection, VerticalLoopSection][source]

Split the entry level of a loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

Returns:

Two loop sections.

classmethod _split_section_with_multiple_fills(loop_order: LoopOrder, section: VerticalLoopSection, filling_fields: Iterable[str], first_unfilled: Dict[str, int], new_symbol_name: Callable[[str], str]) → Tuple[Tuple[VerticalLoopSection, ...], Dict[str, int]][source]

Split loop sections that require multiple fills.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • filling_fields – fields that are using fill caches.

  • first_unfilled – direction-normalized offset of the first unfilled cache entry for each field.

Returns:

A list of sections and an updated first_unfilled map.

visit_FieldAccess(node: FieldAccess, *, name_map: Dict[str, str], **kwargs: Any) → FieldAccess[source]
visit_HorizontalExecution(node: HorizontalExecution, *, name_map: Dict[str, str], fills: List[Stmt], flushes: List[Stmt], **kwargs: Any) → HorizontalExecution[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, *, new_tmps: List[Temporary], symtable: Dict[str, Any], new_symbol_name: Callable[[str], str], **kwargs: Any) → VerticalLoop[source]
visit_VerticalLoopSection(node: VerticalLoopSection, **kwargs: Any) → VerticalLoopSection[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.caches.IJCacheDetection[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, *, local_tmps: Set[str], **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.caches.KCacheDetection(max_cacheable_offset: int = 5)[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

max_cacheable_offset: int = 5
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.caches.PruneKCacheFills[source]

Bases: NodeTranslator

Prunes unneeded k-cache fills.

A fill is classified as required if at least one of the two following conditions holds in any of the loop sections: * There is a read with offset in the direction of looping. * The first centered access is a read access. If none of the conditions holds for any loop section, the fill is considered as unneeded.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_KCache(node: KCache, *, pruneable: Set[str], **kwargs: Any) → KCache[source]
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.caches.PruneKCacheFlushes[source]

Bases: NodeTranslator

Prunes unneeded k-cache flushes.

A flush is classified as unneeded under the following conditions: * All accesses to the field are read-only in the current vertical loop. * There are no read accesses to the field in a following loop.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_KCache(node: KCache, *, pruneable: Set[str], **kwargs: Any) → KCache[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]

gt4py.cartesian.gtc.passes.oir_optimizations.horizontal_execution_merging module

class gt4py.cartesian.gtc.passes.oir_optimizations.horizontal_execution_merging.HorizontalExecutionMerging[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_ScalarAccess(node: ScalarAccess, *, scalar_map: Dict[str, str], **kwargs: Any) → ScalarAccess[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoopSection(node: VerticalLoopSection, *, block_extents: Dict[int, Extent], new_symbol_name: Callable[[str], str], **kwargs: Any) → VerticalLoopSection[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.horizontal_execution_merging.OnTheFlyMerging(max_horizontal_execution_body_size: int = 100, allow_expensive_function_duplication: bool = False)[source]

Bases: NodeTranslator, VisitorWithSymbolTableTrait

Merges consecutive horizontal executions inside parallel vertical loops by introducing redundant computations.

Limitations: * Works on the level of whole horizontal executions, no full dependency analysis is performed (common subexpression and dead code elimination at a later stage can work around this limitation). * The chosen default merge limits are totally arbitrary.

_merge(horizontal_executions: List[HorizontalExecution], symtable: Dict[str, Any], new_symbol_name: Callable[[str], str], protected_fields: Set[str]) → List[HorizontalExecution][source]

Recursively merge horizontal executions.

Uses the following algorithm: 1. Get output fields of the first horizontal execution. 2. Check in which following h. execs. the outputs are read. 3. Duplicate the body of the first h. exec. for each read access (with corresponding offset) and prepend it to the depending h. execs. 4. Recurse on the resulting h. execs.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

allow_expensive_function_duplication: bool = False
max_horizontal_execution_body_size: int = 100
visit_CartesianOffset(node: CartesianOffset, *, shift: Tuple[int, int, int] | None = None, **kwargs: Any) → CartesianOffset[source]
visit_FieldAccess(node: FieldAccess, *, offset_symbol_map: Dict[Tuple[str, Tuple[int, int, int]], str] | None = None, **kwargs: Any) → FieldAccess | ScalarAccess[source]
visit_ScalarAccess(node: ScalarAccess, *, scalar_map: Dict[str, str], **kwargs: Any) → ScalarAccess[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
visit_VerticalLoopSection(node: VerticalLoopSection, **kwargs: Any) → VerticalLoopSection[source]

gt4py.cartesian.gtc.passes.oir_optimizations.inlining module

class gt4py.cartesian.gtc.passes.oir_optimizations.inlining.MaskCollector[source]

Bases: NodeVisitor

Collects the boolean expressions defining mask statements that are boolean fields.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_AssignStmt(node: AssignStmt, *, masks_to_inline: Dict[str, Expr]) → None[source]
visit_MaskStmt(node: MaskStmt, *, masks_to_inline: Dict[str, Expr], **kwargs: Any) → None[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Dict[str, Expr][source]
class gt4py.cartesian.gtc.passes.oir_optimizations.inlining.MaskInlining[source]

Bases: NodeTranslator

Inlines mask statements that are boolean mask fields with the expression that generates the field.

Preconditions: Mask statements exist as boolean temporary field accesses. Postcondition: Mask statements exist as boolean expressions.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_AssignStmt(node: AssignStmt, *, masks_to_inline: Dict[str, Expr], **kwargs: Any) → AssignStmt | NothingType[source]
visit_CacheDesc(node: CacheDesc, *, masks_to_inline, **kwargs)[source]
visit_FieldAccess(node: FieldAccess, *, masks_to_inline: Dict[str, Expr], **kwargs: Any) → Expr[source]
visit_Stencil(node: Stencil, **kwargs)[source]
visit_Temporary(node: Temporary, *, masks_to_inline, **kwargs)[source]

gt4py.cartesian.gtc.passes.oir_optimizations.mask_stmt_merging module

class gt4py.cartesian.gtc.passes.oir_optimizations.mask_stmt_merging.MaskStmtMerging[source]

Bases: NodeTranslator

_merge(stmts: List[Stmt]) → List[Stmt][source]
_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution) → HorizontalExecution[source]
visit_MaskStmt(node: MaskStmt) → MaskStmt[source]
visit_While(node: While) → While[source]

gt4py.cartesian.gtc.passes.oir_optimizations.pruning module

class gt4py.cartesian.gtc.passes.oir_optimizations.pruning.NoFieldAccessPruning[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution) → Any[source]
visit_Stencil(node: Stencil, **kwargs)[source]
visit_VerticalLoop(node: VerticalLoop) → Any[source]
visit_VerticalLoopSection(node: VerticalLoopSection) → Any[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.pruning.UnreachableStmtPruning[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution, *, block_extents: Dict[int, Extent]) → HorizontalExecution[source]
visit_HorizontalRestriction(node: HorizontalRestriction, *, block_extent: Extent, **kwargs: Any) → Any[source]
visit_Stencil(node: Stencil) → Stencil[source]

gt4py.cartesian.gtc.passes.oir_optimizations.temporaries module

class gt4py.cartesian.gtc.passes.oir_optimizations.temporaries.LocalTemporariesToScalars[source]

Bases: TemporariesToScalarsBase

Replaces temporary fields accessed only within a single horizontal execution by scalars.

  1. Finds temporaries that are only accessed within a single HorizontalExecution.

  2. Replaces corresponding FieldAccess nodes by ScalarAccess nodes.

  3. Removes matching temporaries from VerticalLoop declarations.

  4. Add matching temporaries to HorizontalExecution declarations.

Note that temporaries used in horizontal regions in a single horizontal execution may not be scalarized.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.temporaries.TemporariesToScalarsBase[source]

Bases: NodeTranslator, VisitorWithSymbolTableTrait

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_FieldAccess(node: FieldAccess, *, tmps_name_map: Dict[str, str], **kwargs: Any) → FieldAccess | ScalarAccess[source]
visit_HorizontalExecution(node: HorizontalExecution, tmps_to_replace: Set[str], symtable: Dict[str, Any], new_symbol_name: Callable[[str], str], **kwargs: Any) → HorizontalExecution[source]
visit_Interval(interval: Interval, **_: Any) → Interval[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, tmps_to_replace: Set[str], **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.temporaries.WriteBeforeReadTemporariesToScalars[source]

Bases: TemporariesToScalarsBase

Replaces temporary fields that are always written before read by scalars.

Note that temporaries used in horizontal regions in a single horizontal execution may not be scalarized.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]

gt4py.cartesian.gtc.passes.oir_optimizations.utils module

class gt4py.cartesian.gtc.passes.oir_optimizations.utils.AccessCollector[source]

Bases: NodeVisitor

Collects all field accesses and corresponding offsets.

class CartesianAccessCollection(_ordered_accesses: List[AccessT])[source]

Bases: GenericAccessCollection[CartesianAccess, Tuple[int, int, int]]

class GeneralAccessCollection(_ordered_accesses: List[AccessT])[source]

Bases: GenericAccessCollection[GeneralAccess, Tuple[int, int, int | None]]

cartesian_accesses() → CartesianAccessCollection[source]
has_variable_access() → bool[source]
class GenericAccessCollection(_ordered_accesses: 'List[AccessT]')[source]

Bases: Generic[AccessT, OffsetT]

static _offset_dict(accesses: XIterable) → Dict[str, Set[OffsetT]][source]
_ordered_accesses: List[AccessT]
fields() → Set[str][source]

Get a set of all accessed fields’ names.

offsets() → Dict[str, Set[OffsetT]][source]

Get a dictionary, mapping all accessed fields’ names to sets of offset tuples.

ordered_accesses() → List[AccessT][source]

Get a list of ordered accesses.

read_accesses() → List[AccessT][source]

Get the sub-list of read accesses.

read_fields() → Set[str][source]

Get a set of all read fields’ names.

read_offsets() → Dict[str, Set[OffsetT]][source]

Get a dictionary, mapping read fields’ names to sets of offset tuples.

write_accesses() → List[AccessT][source]

Get the sub-list of write accesses.

write_fields() → Set[str][source]

Get a set of all written fields’ names.

write_offsets() → Dict[str, Set[OffsetT]][source]

Get a dictionary, mapping written fields’ names to sets of offset tuples.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

classmethod apply(node: NodeT | list[NodeT | bool | bytes | int | float | str | IntEnum | StrEnum] | dict[Any, NodeT | bool | bytes | int | float | str | IntEnum | StrEnum] | set[NodeT | bool | bytes | int | float | str | IntEnum | StrEnum], **kwargs: Any) → GeneralAccessCollection[source]
visit_AssignStmt(node: AssignStmt, **kwargs: Any) → None[source]
visit_FieldAccess(node: FieldAccess, *, accesses: List[GeneralAccess], is_write: bool, horizontal_mask: HorizontalMask | None = None, conditional: bool | None = None, **kwargs: Any) → None[source]
visit_HorizontalRestriction(node: HorizontalRestriction, **kwargs: Any) → None[source]
visit_Interval(node: Interval, **kwargs: Any) → None[source]
visit_MaskStmt(node: MaskStmt, **kwargs: Any) → None[source]
visit_While(node: While, **kwargs: Any) → None[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.utils.CartesianAccess(field: str, offset: OffsetT, is_write: bool, data_index: List[Expr] = <factory>, horizontal_mask: HorizontalMask | None = None, conditional: bool | None = None)[source]

Bases: GenericAccess[Tuple[int, int, int]]

class gt4py.cartesian.gtc.passes.oir_optimizations.utils.GeneralAccess(field: str, offset: OffsetT, is_write: bool, data_index: List[Expr] = <factory>, horizontal_mask: HorizontalMask | None = None, conditional: bool | None = None)[source]

Bases: GenericAccess[Tuple[int, int, int | None]]

class gt4py.cartesian.gtc.passes.oir_optimizations.utils.GenericAccess(field: 'str', offset: 'OffsetT', is_write: 'bool', data_index: 'List[oir.Expr]' = <factory>, horizontal_mask: 'Optional[common.HorizontalMask]' = None, conditional: 'bool | None' = None)[source]

Bases: Generic[OffsetT]

conditional: bool | None = None

Set to True if this access is conditional, e.g. part of the body of a MaskStmt.

data_index: List[Expr]
field: str
horizontal_mask: HorizontalMask | None = None
property is_conditional: bool
property is_read: bool
is_write: bool
offset: OffsetT
to_extent(horizontal_extent: Extent, centered: bool = False, ignore_horizontal_mask: bool = False) → Extent | None[source]

Convert the access to an extent provided a horizontal extent for the access.

This returns None if no overlap exists between the horizontal mask and interval if ignore_horizontal_mask is not set.

class gt4py.cartesian.gtc.passes.oir_optimizations.utils.StencilExtentComputer(add_k: bool = False, centered_extent: bool = False, ignore_horizontal_mask: bool = False)[source]

Bases: NodeVisitor

Compute extent for fields and horizontal blocks.

Parameters:
  • add_k – Add an extent for the K axis. Defaults to False.

  • centered_extent – Center the extent on 0 (negative left, positive right). Defaults to False.

  • ignore_horizontal_mask – When computing extent, do not restrict it by reading the masks of horizontal regions. Defaults to False.

class Context(fields: 'Dict[str, Extent]'=<factory>, blocks: 'Dict[int, Extent]'=<factory>)[source]

Bases: object

blocks: Dict[int, Extent]
fields: Dict[str, Extent]
_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: oir.HorizontalExecution, *, ctx: Context) → None[source]
visit_Stencil(node: oir.Stencil) → Context[source]
visit_VerticalLoopSection(node: VerticalLoopSection, **kwargs: Any) → None[source]
gt4py.cartesian.gtc.passes.oir_optimizations.utils.collect_symbol_names(node: NodeT | list[NodeT | bool | bytes | int | float | str | IntEnum | StrEnum] | dict[Any, NodeT | bool | bytes | int | float | str | IntEnum | StrEnum] | set[NodeT | bool | bytes | int | float | str | IntEnum | StrEnum]) → Set[str][source]
gt4py.cartesian.gtc.passes.oir_optimizations.utils.compute_extents(node: Stencil, **kwargs: Any) → Tuple[Dict[str, Extent], Dict[int, Extent]][source]
gt4py.cartesian.gtc.passes.oir_optimizations.utils.compute_fields_extents(node: Stencil, **kwargs: Any) → Dict[str, Extent][source]
gt4py.cartesian.gtc.passes.oir_optimizations.utils.compute_horizontal_block_extents(node: Stencil, **kwargs: Any) → Dict[int, Extent][source]
gt4py.cartesian.gtc.passes.oir_optimizations.utils.symbol_name_creator(used_names: Set[str]) → Callable[[str], str][source]

Create a function that generates symbol names that are not already in use.

Parameters:

used_names – Symbol names that are already in use and thus should not be generated. NOTE: used_names will be modified to contain all generated symbols.

Returns:

A callable to generate new unique symbol names.

gt4py.cartesian.gtc.passes.oir_optimizations.vertical_loop_merging module

class gt4py.cartesian.gtc.passes.oir_optimizations.vertical_loop_merging.AdjacentLoopMerging[source]

Bases: NodeTranslator

static _merge(a: VerticalLoop, b: VerticalLoop) → VerticalLoop[source]
static _mergeable(a: VerticalLoop, b: VerticalLoop) → bool[source]
_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]

Module contents

class gt4py.cartesian.gtc.passes.oir_optimizations.AdjacentLoopMerging[source]

Bases: NodeTranslator

static _merge(a: VerticalLoop, b: VerticalLoop) → VerticalLoop[source]
static _mergeable(a: VerticalLoop, b: VerticalLoop) → bool[source]
_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.FillFlushToLocalKCaches[source]

Bases: NodeTranslator, VisitorWithSymbolTableTrait

Converts fill and flush k-caches to local k-caches.

For each cached field, the following actions are performed: 1. A new locally-k-cached temporary is introduced. 2. All accesses to the original field are replaced by accesses to this temporary. 3. Loop sections are split where necessary to allow single-level loads wherever possible. 3. Fill statements from the original field to the temporary are introduced. 4. Flush statements from the temporary to the original field are introduced.

static _fill_limits(loop_order: LoopOrder, section: VerticalLoopSection) → Dict[str, Tuple[int, int]][source]

Direction-normalized min and max read accesses for each accessed field.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

Returns:

A dict, mapping field names to min and max read offsets relative to loop order (i.e., positive means in the direction of the loop order).

classmethod _fill_stmts(loop_order: LoopOrder, section: VerticalLoopSection, filling_fields: Dict[str, str], first_unfilled: Dict[str, int], symtable: Dict[str, Any]) → Tuple[List[AssignStmt], Dict[str, int]][source]

Generate fill statements for the given loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • filling_fields – mapping from field names to cache names.

  • first_unfilled – direction-normalized offset of the first unfilled cache entry for each field.

Returns:

A list of fill statements and an updated first_unfilled map.

classmethod _flush_stmts(loop_order: LoopOrder, section: VerticalLoopSection, flushing_fields: Dict[str, str], symtable: Dict[str, Any]) → List[AssignStmt][source]

Generate flush statements for the given loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • flushing_fields – mapping from field names to cache names.

Returns:

A list of flush statements.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

static _requires_splitting(interval: Interval) → bool[source]

Check if an interval is larger than one level and thus may require splitting.

static _split_entry_level(loop_order: LoopOrder, section: VerticalLoopSection, new_symbol_name: Callable[[str], str]) → Tuple[VerticalLoopSection, VerticalLoopSection][source]

Split the entry level of a loop section.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

Returns:

Two loop sections.

classmethod _split_section_with_multiple_fills(loop_order: LoopOrder, section: VerticalLoopSection, filling_fields: Iterable[str], first_unfilled: Dict[str, int], new_symbol_name: Callable[[str], str]) → Tuple[Tuple[VerticalLoopSection, ...], Dict[str, int]][source]

Split loop sections that require multiple fills.

Parameters:
  • loop_order – forward or backward order.

  • section – loop section to split.

  • filling_fields – fields that are using fill caches.

  • first_unfilled – direction-normalized offset of the first unfilled cache entry for each field.

Returns:

A list of sections and an updated first_unfilled map.

visit_FieldAccess(node: FieldAccess, *, name_map: Dict[str, str], **kwargs: Any) → FieldAccess[source]
visit_HorizontalExecution(node: HorizontalExecution, *, name_map: Dict[str, str], fills: List[Stmt], flushes: List[Stmt], **kwargs: Any) → HorizontalExecution[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, *, new_tmps: List[Temporary], symtable: Dict[str, Any], new_symbol_name: Callable[[str], str], **kwargs: Any) → VerticalLoop[source]
visit_VerticalLoopSection(node: VerticalLoopSection, **kwargs: Any) → VerticalLoopSection[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.HorizontalExecutionMerging[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_ScalarAccess(node: ScalarAccess, *, scalar_map: Dict[str, str], **kwargs: Any) → ScalarAccess[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoopSection(node: VerticalLoopSection, *, block_extents: Dict[int, Extent], new_symbol_name: Callable[[str], str], **kwargs: Any) → VerticalLoopSection[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.IJCacheDetection[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, *, local_tmps: Set[str], **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.KCacheDetection(max_cacheable_offset: int = 5)[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

max_cacheable_offset: int = 5
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.LocalTemporariesToScalars[source]

Bases: TemporariesToScalarsBase

Replaces temporary fields accessed only within a single horizontal execution by scalars.

  1. Finds temporaries that are only accessed within a single HorizontalExecution.

  2. Replaces corresponding FieldAccess nodes by ScalarAccess nodes.

  3. Removes matching temporaries from VerticalLoop declarations.

  4. Add matching temporaries to HorizontalExecution declarations.

Note that temporaries used in horizontal regions in a single horizontal execution may not be scalarized.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.MaskInlining[source]

Bases: NodeTranslator

Inlines mask statements that are boolean mask fields with the expression that generates the field.

Preconditions: Mask statements exist as boolean temporary field accesses. Postcondition: Mask statements exist as boolean expressions.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_AssignStmt(node: AssignStmt, *, masks_to_inline: Dict[str, Expr], **kwargs: Any) → AssignStmt | NothingType[source]
visit_CacheDesc(node: CacheDesc, *, masks_to_inline, **kwargs)[source]
visit_FieldAccess(node: FieldAccess, *, masks_to_inline: Dict[str, Expr], **kwargs: Any) → Expr[source]
visit_Stencil(node: Stencil, **kwargs)[source]
visit_Temporary(node: Temporary, *, masks_to_inline, **kwargs)[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.MaskStmtMerging[source]

Bases: NodeTranslator

_merge(stmts: List[Stmt]) → List[Stmt][source]
_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution) → HorizontalExecution[source]
visit_MaskStmt(node: MaskStmt) → MaskStmt[source]
visit_While(node: While) → While[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.NoFieldAccessPruning[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution) → Any[source]
visit_Stencil(node: Stencil, **kwargs)[source]
visit_VerticalLoop(node: VerticalLoop) → Any[source]
visit_VerticalLoopSection(node: VerticalLoopSection) → Any[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.OnTheFlyMerging(max_horizontal_execution_body_size: int = 100, allow_expensive_function_duplication: bool = False)[source]

Bases: NodeTranslator, VisitorWithSymbolTableTrait

Merges consecutive horizontal executions inside parallel vertical loops by introducing redundant computations.

Limitations: * Works on the level of whole horizontal executions, no full dependency analysis is performed (common subexpression and dead code elimination at a later stage can work around this limitation). * The chosen default merge limits are totally arbitrary.

_merge(horizontal_executions: List[HorizontalExecution], symtable: Dict[str, Any], new_symbol_name: Callable[[str], str], protected_fields: Set[str]) → List[HorizontalExecution][source]

Recursively merge horizontal executions.

Uses the following algorithm: 1. Get output fields of the first horizontal execution. 2. Check in which following h. execs. the outputs are read. 3. Duplicate the body of the first h. exec. for each read access (with corresponding offset) and prepend it to the depending h. execs. 4. Recurse on the resulting h. execs.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

allow_expensive_function_duplication: bool = False
max_horizontal_execution_body_size: int = 100
visit_CartesianOffset(node: CartesianOffset, *, shift: Tuple[int, int, int] | None = None, **kwargs: Any) → CartesianOffset[source]
visit_FieldAccess(node: FieldAccess, *, offset_symbol_map: Dict[Tuple[str, Tuple[int, int, int]], str] | None = None, **kwargs: Any) → FieldAccess | ScalarAccess[source]
visit_ScalarAccess(node: ScalarAccess, *, scalar_map: Dict[str, str], **kwargs: Any) → ScalarAccess[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
visit_VerticalLoopSection(node: VerticalLoopSection, **kwargs: Any) → VerticalLoopSection[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.PruneKCacheFills[source]

Bases: NodeTranslator

Prunes unneeded k-cache fills.

A fill is classified as required if at least one of the two following conditions holds in any of the loop sections: * There is a read with offset in the direction of looping. * The first centered access is a read access. If none of the conditions holds for any loop section, the fill is considered as unneeded.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_KCache(node: KCache, *, pruneable: Set[str], **kwargs: Any) → KCache[source]
visit_VerticalLoop(node: VerticalLoop, **kwargs: Any) → VerticalLoop[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.PruneKCacheFlushes[source]

Bases: NodeTranslator

Prunes unneeded k-cache flushes.

A flush is classified as unneeded under the following conditions: * All accesses to the field are read-only in the current vertical loop. * There are no read accesses to the field in a following loop.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_KCache(node: KCache, *, pruneable: Set[str], **kwargs: Any) → KCache[source]
visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.UnreachableStmtPruning[source]

Bases: NodeTranslator

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_HorizontalExecution(node: HorizontalExecution, *, block_extents: Dict[int, Extent]) → HorizontalExecution[source]
visit_HorizontalRestriction(node: HorizontalRestriction, *, block_extent: Extent, **kwargs: Any) → Any[source]
visit_Stencil(node: Stencil) → Stencil[source]
class gt4py.cartesian.gtc.passes.oir_optimizations.WriteBeforeReadTemporariesToScalars[source]

Bases: TemporariesToScalarsBase

Replaces temporary fields that are always written before read by scalars.

Note that temporaries used in horizontal regions in a single horizontal execution may not be scalarized.

_node_visitor_cache_: ClassVar[dict[type, str] | None] = {}

Cache for visitor method names per node class to speed up dispatching. Note that we cache the visitor method name, not the bound method itself, to avoid implicitly caching the visitor state, which could lead to unexpected behavior.

visit_Stencil(node: Stencil, **kwargs: Any) → Stencil[source]