GlobalScope

class hugr.passes.scope.GlobalScope(*values)[source]

Bases: PassScopeBase, Enum

Run the pass on the whole Hugr, regardless of the entrypoint.

For lowering passes, signature changes etc. should be applied across the Hugr.

For optimization passes, different variants specify which nodes in the Hugr must have their interface preserved. (Interface means signature/value ports, as well as static ports, and their types; also name (if public) for linking; and whether the node is a valid dataflow child or is a DataflowBlock, ExitBlock or Module).

  • root: The hugr module_root

  • recursive: true

Methods

root

Returns the root of the subtree to be optimized by the pass, or none if the pass should do nothing.

preserve_interface

Returns a list of nodes, in the subtree beneath root, for which the pass must preserve the observable semantics (ports, execution behaviour, linking).

recursive

Returns true if the pass should be applied recursively on the descendants of the root regions.

regions

Return every region (every [dataflow] or [CFG] container - but excluding Module) in the Hugr to be optimized by the pass.

in_scope

Returns whether the node may be modified by the pass.

Attributes

PRESERVE_ALL

Optimization passes must preserve interface and behaviour of every module child, as well as the entrypoint.

PRESERVE_PUBLIC

Optimization passes must preserve interface and behaviour of all public functions, as well as the entrypoint.

PRESERVE_ENTRYPOINT

Run the pass on the whole Hugr, but preserving behaviour only of the entrypoint.

PRESERVE_ALL = 'GlobalAll'

Optimization passes must preserve interface and behaviour of every module child, as well as the entrypoint.

  • preserve_interface: every public function defined in the module,

    and the entrypoint.

PRESERVE_ENTRYPOINT = 'GlobalEntrypoint'

Run the pass on the whole Hugr, but preserving behaviour only of the entrypoint.

Thus, appropriate for a Hugr that will be run as an executable, with the entrypoint indicating where execution will begin.

If the entrypoint is the module root, then the same as [Self::Public].

  • preserve_interface: if the entrypoint node is the module root, then all

    children of the module root; otherwise, just the entrypoint node.

PRESERVE_PUBLIC = 'GlobalPublic'

Optimization passes must preserve interface and behaviour of all public functions, as well as the entrypoint.

Private functions and constant definitions may be modified, including changing their behaviour or deleting them entirely, so long as this does not affect behaviour of the public functions (or entrypoint).

Thus, appropriate for a Hugr that will be linked as a library.

  • preserve_interface: Every public function defined in the module’, plus the entrypoint.

in_scope(hugr: Hugr, n: Node) InScope

Returns whether the node may be modified by the pass.

Nodes outside the root subtree are never in scope. Nodes inside the subtree may be InScope.YES or InScope.PRESERVE_INTERFACE.

preserve_interface(hugr: Hugr) Iterable[Node][source]

Returns a list of nodes, in the subtree beneath root, for which the pass must preserve the observable semantics (ports, execution behaviour, linking).

We include the Module in this list (if it is root) as these properties must be preserved (this rules out any other changes).

recursive() bool[source]

Returns true if the pass should be applied recursively on the descendants of the root regions.

regions(hugr: Hugr) Iterable[Node]

Return every region (every [dataflow] or [CFG] container - but excluding Module) in the Hugr to be optimized by the pass.

This computes all the regions to be optimized at once. In general, it is more efficient to traverse the Hugr incrementally starting from the PassScope.root instead.

root(hugr: Hugr) Node[source]

Returns the root of the subtree to be optimized by the pass, or none if the pass should do nothing.