Beaver.Walker (beaver v0.4.1)
Provides traversal capabilities for MLIR structures.
This module implements traversal functionality for MLIR structures including:
It implements both the Enumerable protocol and the Access behavior to provide
a familiar interface for working with MLIR structures.
Depth-first, pre-order and post-order walking
Allows traversing MLIR structures in depth-first order, visiting each node and its children before moving to siblings. Supports both pre-order (visit node before children) and post-order (visit children before node).
Mutation Support
It is possible to modifying the MLIR structure during traversal with CAPIs. It is recommended to use replace/2 to replace an operation to keep the traversal going.
Pattern-based Transformations
You can apply transformation patterns defined using Beaver.Pattern.defpat/2 to MLIR structures during traversal.
Access Syntax
- Access behavior to provide convenient attribute access:
op[:attr_name] op["attr_name"] - convenient access to get operands, results, regions
operands(op)[0]
Summary
Functions
Returns an enumerable of the arguments of an Block.t()
Returns an enumerable of the attributes of an operation().
Returns an enumerable of the blocks of an Region.t()
Returns an enumerable of the operands of an operation().
Returns an enumerable of the operations of an Block.t()
Performs a depth-first, post-order traversal of a MLIR structure.
Performs a depth-first, post-order traversal of a MLIR structure using an accumulator.
Performs a depth-first, pre-order traversal of a MLIR structure.
Performs a depth-first, pre-order traversal of a MLIR structure using an accumulator.
Returns an enumerable of the regions of an operation().
Replace a operation with a value
Returns an enumerable of the results of an operation().
Returns an enumerable of the successor blocks of an operation().
Traverse and transform a container in MLIR, it could be a operation, region, block.
You might expect this function works like Macro.traverse/4.
Returns an enumerable of the uses of an Value.t()
Types
@type container() :: operation() | Beaver.MLIR.Region.t() | Beaver.MLIR.Block.t() | Beaver.MLIR.NamedAttribute.t()
@type element() :: operation() | Beaver.MLIR.Region.t() | Beaver.MLIR.Block.t() | Beaver.MLIR.Value.t() | Beaver.MLIR.NamedAttribute.t()
@type element_module() ::
Beaver.MLIR.Operation
| Beaver.MLIR.Region
| Beaver.MLIR.Block
| Beaver.MLIR.Value
| Beaver.MLIR.Attribute
@type operation() :: Beaver.MLIR.Module.t() | Beaver.MLIR.Operation.t() | Beaver.Walker.OpReplacement.t()
@type t() :: %Beaver.Walker{ container: container(), element_equal: (element(), element() -> Beaver.Native.Bool.t() | bool()) | nil, element_module: element_module(), get_element: (container(), integer() -> element()) | nil, get_first: (container() -> element()) | nil, get_next: (element() -> element()) | nil, get_num: (container() -> Beaver.Native.I64.t() | integer()) | nil, get_parent: (element() -> container()) | nil, is_null: (element() -> Beaver.Native.Bool.t() | bool()) | nil, num: non_neg_integer() | nil, parent_equal: (element(), element() -> Beaver.Native.Bool.t() | bool()) | nil, this: element() | non_neg_integer() | nil }
Functions
@spec arguments(Beaver.MLIR.Block.t()) :: Enumerable.t()
Returns an enumerable of the arguments of an Block.t()
@spec attributes(operation()) :: Enumerable.t()
Returns an enumerable of the attributes of an operation().
@spec blocks(Beaver.MLIR.Region.t()) :: Enumerable.t()
Returns an enumerable of the blocks of an Region.t()
@spec operands(operation()) :: Enumerable.t()
Returns an enumerable of the operands of an operation().
@spec operations(Beaver.MLIR.Block.t()) :: Enumerable.t()
Returns an enumerable of the operations of an Block.t()
Performs a depth-first, post-order traversal of a MLIR structure.
Performs a depth-first, post-order traversal of a MLIR structure using an accumulator.
Performs a depth-first, pre-order traversal of a MLIR structure.
Performs a depth-first, pre-order traversal of a MLIR structure using an accumulator.
@spec regions(operation()) :: Enumerable.t()
Returns an enumerable of the regions of an operation().
Replace a operation with a value
@spec results(operation()) :: Enumerable.t()
Returns an enumerable of the results of an operation().
@spec successors(operation()) :: Enumerable.t()
Returns an enumerable of the successor blocks of an operation().
@spec traverse(mlir(), any(), (mlir(), any() -> {mlir(), any()}), (mlir(), any() -> {mlir(), any()})) :: {mlir(), any()}
Traverse and transform a container in MLIR, it could be a operation, region, block.
You might expect this function works like Macro.traverse/4.
More on manipulating the IR
During the traversal, there are generally two choices to manipulate the IR:
- Use a pattern defined by macro
Beaver.Pattern.defpat/2to have the PDL interpreter transform the IR for you. You can use both if it is proper to do so. - Use
Beaver.Walker.replace/2to replace the operation and return a walker as placeholder if is replaced by value. It could be mind-boggling to think the IR is mutable but not an issue if your approach is very functional. Inappropriate mutation might cause crash or bugs if somewhere else is keeping a reference of the replace op.
Some tips
- If your matching is very complicated, using
with/1in Elixir should cover it. - Use
defpatif you want MLIR's greedy pattern application based on benefits instead of implementing something alike yourself. - You can run traversals in a MLIR pass by calling them in
run/1so that it joins the general MLIR pass manager's orchestration and will be run in parallel when possible.
@spec uses(Beaver.MLIR.Value.t()) :: Enumerable.t()
Returns an enumerable of the uses of an Value.t()