Builder API

class mlir.builder.builder.IRBuilder

MLIR AST builder. Provides convenience methods for adding core dialect operations to a Block.

block

The block that the builder is operating on.

position

An instance of int, indicating the position where the next operation is to be added in the block.

Note

  • The concepts here at not true to the implementation in llvm-project/mlir. It should be seen more of a convenience to emit MLIR modules.
  • This class shared design elements from llvmlite.ir.IRBuilder, querying mechanism from loopy.

Registering a custom dialect builder

register_dialect(name: str, dialect_builder: mlir.builder.builder.DialectBuilder, overwrite: bool = False) → None

Position/block manipulation

position_at_entry(block: mlir.astnodes.Block)

Starts building at block’s entry.

position_at_exit(block: mlir.astnodes.Block)

Starts building at block’s exit.

goto_block(block: mlir.astnodes.Block)

Context to start building at block’s exit.

Example usage:

with builder.goto_block(block):
    # starts building at *block*'s exit.
    z = builder.addf(x, y, F64)

# goes back to building at the builder's earlier position
goto_entry_block(block: mlir.astnodes.Block)

Context to start building at block’s entry.

Example usage:

with builder.goto_block(block):
    # starts building at *block*'s entry.
    z = builder.addf(x, y, F64)

# goes back to building at the builder's earlier position
goto_before(query: mlir.builder.match.MatchExpressionBase, block: Optional[mlir.astnodes.Block] = None)

Enters a context to build at the point just before query gets matched in block.

Parameters:block – Block to query the operations in. Defaults to the builder’s block.

Example usage:

with builder.goto_before(Reads("%c0") & Isa(AddfOperation)):
    # starts building before operation of form "... = addf %c0, ..."
    z = builder.mulf(x, y, F64)
# goes back to building at the builder's earlier position
goto_after(query: mlir.builder.match.MatchExpressionBase, block: Optional[mlir.astnodes.Block] = None)

Enters a context to build at the point just after query gets matched in block.

Parameters:block – Block to query the operations in. Defaults to the builder’s block.

Example usage:

with builder.goto_after(Writes("%c0") & Isa(ConstantOperation)):
    # starts building after operation of form "%c0 = constant ...: ..."
    z = builder.dim(x, c0, builder.INDEX)

# goes back to building at the builder's earlier position

Types

Attr F16:f16 type
Attr F32:f32 type
Attr F64:f64 type
Attr INT32:i32 type
Attr INT64:i64 type
Attr INDEX:index type
MemRefType(dtype: mlir.astnodes.Type, shape: Optional[Tuple[Optional[int], ...]], offset: Optional[int] = None, strides: Optional[Tuple[Optional[int], ...]] = None) → mlir.astnodes.MemRefType

Returns an instance of mlir.astnodes.UnrankedMemRefType if shape is None, else returns a mlir.astnodes.RankedMemRefType.

Standard dialect ops

dim(memref_or_tensor: mlir.astnodes.SsaId, index: mlir.astnodes.SsaId, memref_type: Union[mlir.astnodes.MemRefType, mlir.astnodes.TensorType], name: Optional[str] = None)
addf(op_a: mlir.astnodes.SsaId, op_b: mlir.astnodes.SsaId, type: mlir.astnodes.Type, name: Optional[str] = None)
mulf(op_a: mlir.astnodes.SsaId, op_b: mlir.astnodes.SsaId, type: mlir.astnodes.Type, name: Optional[str] = None)
index_constant(value: int, name: Optional[str] = None)
float_constant(value: float, type: mlir.astnodes.FloatType, name: Optional[str] = None)
class mlir.builder.builder.AffineBuilder(core_builder: mlir.builder.builder.IRBuilder)

Affine dialect ops builder.

for_(lower_bound: Union[int, mlir.astnodes.SsaId], upper_bound: Union[int, mlir.astnodes.SsaId], step: Optional[int] = None, indexname: Optional[str] = None)
load(memref: mlir.astnodes.SsaId, indices: Union[mlir.astnodes.AffineExpr, List[mlir.astnodes.AffineExpr]], memref_type: mlir.astnodes.MemRefType, name: Optional[str] = None)
store(address: mlir.astnodes.SsaId, memref: mlir.astnodes.SsaId, indices: Union[mlir.astnodes.AffineExpr, List[mlir.astnodes.AffineExpr]], memref_type: mlir.astnodes.MemRefType)

Querying expressions

class mlir.builder.match.All

Matches with all nodes.

class mlir.builder.match.And(children: List[mlir.builder.match.MatchExpressionBase])

Matches if all its children match.

class mlir.builder.match.Or(children: List[mlir.builder.match.MatchExpressionBase])

Matches if any of its children match.

class mlir.builder.match.Not(child: mlir.builder.match.MatchExpressionBase)

Matches if the child does not match.

class mlir.builder.match.Reads(name: Union[str, mlir.astnodes.SsaId])

Matches the variables read by the operation.

class mlir.builder.match.Writes(name: Union[str, mlir.astnodes.SsaId])

Matches the variable names written by the operation.

class mlir.builder.match.Isa(type: type)

Matches the operation’s type.