Beaver.Walker (beaver v0.3.4)

Walker to traverse MLIR structures including operands, results, successors, attributes, regions. It implements the Enumerable protocol and the Access behavior.

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

Link to this type

element_module()

@type element_module() ::
  Beaver.MLIR.Operation
  | Beaver.MLIR.Region
  | Beaver.MLIR.Block
  | Beaver.MLIR.Value
  | Beaver.MLIR.Attribute
@type mlir() :: container() | element()
@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

Link to this function

arguments(block)

@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().

Link to this function

operations(block)

@spec operations(Beaver.MLIR.Block.t()) :: Enumerable.t()

Returns an enumerable of the operations of an Block.t()

Link to this function

postwalk(ast, fun)

@spec postwalk(mlir(), (mlir() -> mlir())) :: mlir()

Performs a depth-first, post-order traversal of a MLIR structure.

Link to this function

postwalk(ast, acc, fun)

@spec postwalk(mlir(), any(), (mlir(), any() -> {mlir(), any()})) :: {mlir(), any()}

Performs a depth-first, post-order traversal of a MLIR structure using an accumulator.

Link to this function

prewalk(ast, fun)

@spec prewalk(mlir(), (mlir() -> mlir())) :: mlir()

Performs a depth-first, pre-order traversal of a MLIR structure.

Link to this function

prewalk(ast, acc, fun)

@spec prewalk(mlir(), any(), (mlir(), any() -> {mlir(), any()})) :: {mlir(), any()}

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().

Link to this function

replace(op, value, opts \\ [destroy: true])

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().

Link to this function

traverse(mlir, acc, pre, post)

@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/2 to have the PDL interpreter transform the IR for you. You can use both if it is proper to do so.
  • Use Beaver.Walker.replace/2 to 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/1 in Elixir should cover it.
  • Use defpat if 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/1 so 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()