Beaver.MLIR.IRMapping (beaver v0.4.8)

Copy Markdown

An owned mapping between values, blocks, and operations in two pieces of IR.

Mappings are mutable native resources. Destroy them explicitly with destroy/1, or use with_mapping/1 for scoped work. Mapped entities are borrowed: a mapping must not be queried after either side of a mapping has been destroyed.

clone/2 creates a detached, caller-owned operation. Insert that operation into a block or destroy it. clone/3 inserts through a rewriter and updates the mapping as part of the rewrite.

A mapping and a scoped insertion point can be combined in a rewrite callback without leaking native state or the rewriter's temporary position:

base = MLIR.PatternRewriter.as_base(rewriter)
old_result = MLIR.Operation.result(operation, 0)

MLIR.PatternRewriter.with_insertion_point(rewriter, {:before, operation}, fn ->
  MLIR.IRMapping.with_mapping(fn mapping ->
    replacement = build_replacement.(base)

    mapping
    |> MLIR.IRMapping.map(old_result, replacement)
    |> MLIR.IRMapping.lookup(old_result)
    |> then(&MLIR.RewriterBase.replace(base, old_result, &1))
  end)
end)

Summary

Functions

Clears all mappings and returns mapping.

Clones operation as a detached caller-owned operation and updates mapping.

Clones and inserts operation through rewriter, updating mapping.

Returns whether a mapping exists for a value, block, or operation.

Creates an empty owned IR mapping.

Destroys an owned IR mapping.

Erases a value, block, or operation mapping and returns mapping.

Looks up a mapped entity, returning nil when no mapping exists.

Looks up a mapped entity, returning the input entity when absent.

Adds or replaces a value, block, or operation mapping.

Runs fun with a new mapping and always destroys it.

Types

entity()

rewriter()

t()

@type t() :: %Beaver.MLIR.IRMapping{ref: term()}

Functions

clear(mapping)

@spec clear(t()) :: t()

Clears all mappings and returns mapping.

clone(mapping, operation)

Clones operation as a detached caller-owned operation and updates mapping.

clone(mapping, operation, rewriter)

Clones and inserts operation through rewriter, updating mapping.

contains?(mapping, entity)

@spec contains?(t(), entity()) :: boolean()

Returns whether a mapping exists for a value, block, or operation.

create()

@spec create() :: t()

Creates an empty owned IR mapping.

destroy(mapping)

@spec destroy(t()) :: :ok

Destroys an owned IR mapping.

erase(mapping, entity)

@spec erase(t(), entity()) :: t()

Erases a value, block, or operation mapping and returns mapping.

lookup(mapping, from)

@spec lookup(t(), entity()) :: entity() | nil

Looks up a mapped entity, returning nil when no mapping exists.

lookup_or_default(mapping, from)

@spec lookup_or_default(t(), entity()) :: entity()

Looks up a mapped entity, returning the input entity when absent.

make(value)

map(mapping, from, to)

@spec map(t(), entity(), entity()) :: t()

Adds or replaces a value, block, or operation mapping.

with_mapping(fun)

@spec with_mapping((t() -> result)) :: result when result: var

Runs fun with a new mapping and always destroys it.