MLIR

Multi-Level IR Compiler Framework

'bufferization' Dialect

Bufferization in MLIR is the process of converting the tensor type to the memref type. Simply put, bufferization is the process of converting computations on the mathematical tensor construct to computations on physical memory buffers. The bufferization dialect contains operations/interfaces specific to the bufferization passes.

An overview of the bufferization infrastructure and important conceptual details related to using the MLIR dialect conversion infrastructure can be found in bufferization and buffer deallocation.

Operation definition 

bufferization.alloc_tensor (::mlir::bufferization::AllocTensorOp) 

allocate buffer for a tensor

bufferization.alloc_tensor materializes an uninitialized tensor with a given shape (dynamic or static). It always bufferizes to a new buffer allocation of the given shape. The optional copy operand specifies the contents of the tensors. If no copy operand is specified, reading from the result of an alloc_tensor op yields an undefined value.

If copy is specified, no dynamic sizes should be passed, since they are the same as the dynamic sizes of the copy operand.

alloc_tensor is a helper op for bufferization. The operation is provided as an anchor that marks the beginning of a new tensor SSA use-def chain. It can be used to control in-place bufferization decisions during One-Shot Bufferize: The bufferized result of a bufferization.alloc_tensor does not alias with any other buffer, so it can be used to resolve read-after-write conflicts that would have been introduced by the in-place bufferization of another op.

The optional memory_space attribute specifies the memory space when bufferizing this op. The memory space is inferred from copy if specified. If neither copy nor memory_space is specified, the default memory space is used during bufferization.

The optional size_hint operand specifies the number of non-zero elements for sparse tensors. The value of size_hint should be not less than 1 and not larger than the linear size of the corresponding dense tensor type. If this requirement is not met, the behavior of the operator is undefined.

Both dense and sparse tensor types are supported. The result of a bufferization.alloc_tensor is a tensor value that can be used like any other tensor value. In practice, it is often used as the “out” operand of another op. Sparse tensor allocations should always be used in a local construction operation and never escape the function boundary directly.

Example:

%c = bufferization.alloc_tensor(%d1, %d2) : tensor<?x?xf32, #SparseMatrix>
%0 = linalg.matmul
  ins(%a, %b: tensor<?x?xf32, #SparseMatrix>, tensor<?x?xf32, #SparseMatrix>)
  outs(%c: tensor<?x?xf32, #SparseMatrix>) -> tensor<?x?xf32, #SparseMatrix>
return %0 : tensor<?x?xf32, #SparseMatrix>
%c = bufferization.alloc_tensor(%d1, %d2) size_hint = %noe
  : tensor<?x?xf32, #SparseMatrix>

Traits: AttrSizedOperandSegments

Interfaces: BufferizableOpInterface, ReifyRankedShapedTypeOpInterface

Attributes: 

AttributeMLIR TypeDescription
memory_space::mlir::Attributeany attribute

Operands: 

OperandDescription
dynamic_sizesindex
copytensor of any type values
size_hintindex

Results: 

ResultDescription
resulttensor of any type values

bufferization.clone (::mlir::bufferization::CloneOp) 

clone a memref

Syntax:

operation ::= `bufferization.clone` $input attr-dict `:` type($input) `to` type($output)

Clones the data in the input view into an implicitly defined output view.

Usage:

%arg1 = bufferization.clone %arg0 : memref<?xf32> to memref<?xf32>

Valid implementations of this operation may alias the input and output views or create an actual copy. Mutating the source or result of the clone operation after the clone operation thus leads to undefined behavior.

Interfaces: AllocationOpInterface, CopyOpInterface, MemoryEffectOpInterface

Operands: 

OperandDescription
inputranked or unranked memref of any type values

Results: 

ResultDescription
outputranked or unranked memref of any type values

bufferization.dealloc_tensor (::mlir::bufferization::DeallocTensorOp) 

release underlying storage format of given tensor

Syntax:

operation ::= `bufferization.dealloc_tensor` $tensor attr-dict `:` type($tensor)

bufferization.dealloc_tensor is a buffer deallocation in tensor land. This op can be used for manual buffer deallocation. Some bufferizations (such as One-Shot Bufferize) take care of buffer deallocation, in which case this op is usually not needed. Details can be found in the documentation of the respective bufferization passes.

In case of a dense tensor, this op lowers to a memref.dealloc op during bufferization.

In case of a sparse tensor, this op releases the underlying sparse storage format for a tensor that materialized earlier through a new operation, a convert operation with annotated destination tensor type (unless the convert is folded away), or a bufferization.alloc_tensor operation. The release operation should only be called once for any materialized tensor. After this operation, any subsequent memref querying operation on the tensor returns undefined results.

Example:

bufferization.dealloc_tensor %tensor : tensor<1024x1024xf64, #CSR>

Interfaces: BufferizableOpInterface

Operands: 

OperandDescription
tensortensor of any type values

bufferization.to_memref (::mlir::bufferization::ToMemrefOp) 

cast a tensor to memref

Syntax:

operation ::= `bufferization.to_memref` $tensor attr-dict `:` type($memref)

An operation that returns the future buffer of a tensor.

// Result type is memref<4x?xf32, #layout, 0>
%m = bufferization.to_memref %t : memref<4x?xf32, #layout, 0>

This operation is a specialized variant of the built-in unrealized_conversion_cast and is used to make sure that the IR stays valid at any point during the bufferization.

IR that contains to_memref ops cannot be bufferized with One-Shot Bufferize.

Traits: AlwaysSpeculatableImplTrait, SameOperandsAndResultElementType, SameOperandsAndResultShape

Interfaces: BufferizableOpInterface, ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{}

Operands: 

OperandDescription
tensortensor of any type values

Results: 

ResultDescription
memrefranked or unranked memref of any type values

bufferization.to_tensor (::mlir::bufferization::ToTensorOp) 

create a tensor from a memref

Syntax:

operation ::= `bufferization.to_tensor` $memref (`restrict` $restrict^)? (`writable` $writable^)? attr-dict
              `:` type($memref)

An operation that creates a tensor from a memref. The result value is a tensor whose shape and element type match the memref operand.

The opposite of this op is to_memref. Together, these two ops are useful for source/target materializations when doing type conversions involving tensors and memrefs.

Example:

// Produces a value of tensor<4x?xf32> type.
%t = bufferization.to_tensor %m : memref<4x?xf32, #layout, 0>

If the writable unit attribute is set, the produced tensor is considered “writable” during bufferization. Otherwise, every OpOperand that bufferizes to a write to the future buffer of the resulting tensor (or an alias thereof) will bufferize out-of-place to prevent emitting any writes to memref during bufferization.

If the given memref does not alias with any other memref passed to another to_tensor op, the restrict unit attribute can be set. Only such operations are supported by One-Shot Bufferize. (Otherwise, potential memref aliasing relationships would have to be captured in One-Shot Bufferize.)

Example:

%t = bufferization.to_tensor %m restrict writable : memref<4xf32>

// %t is writable, so the tensor.insert may bufferize in-place in the
// absence of other conflicts.
%r = tensor.insert %f into %t[%idx] : tensor<4xf32>

to_tensor ops are not bufferized. They are expected to fold away after bufferization. If there are non-bufferizable ops in the IR and allowUnknownOps is set, they may be part of the resulting IR and not fold away. However, such IR is no longer bufferizable with One-Shot Bufferize.

Traits: SameOperandsAndResultElementType, SameOperandsAndResultShape

Interfaces: BufferizableOpInterface, InferTypeOpInterface

Attributes: 

AttributeMLIR TypeDescription
restrict::mlir::UnitAttrunit attribute
writable::mlir::UnitAttrunit attribute

Operands: 

OperandDescription
memrefranked or unranked memref of any type values

Results: 

ResultDescription
resulttensor of any type values