Struct melior::dialect::ods::sparse_tensor::ForeachOperation
source · pub struct ForeachOperation<'c> { /* private fields */ }
Expand description
A foreach
operation. Iterates over elements in a tensor.
Iterates over stored elements in a tensor (which are typically, but not always, non-zero for sparse tensors) and executes the block.
tensor
: the input tensor to iterate over.
initArgs
: the initial loop argument to carry and update during each iteration.
order
: an optional permutation affine map that specifies the order in which
the dimensions are visited (e.g., row first or column first). This is only
applicable when the input tensor is a non-annotated dense tensor.
For an input tensor with dim-rank n
, the block must take n + 1
arguments (plus additional loop-carried variables as described below).
The first n
arguments provide the dimension-coordinates of the element
being visited, and must all have index
type. The (n+1)
-th argument
provides the element’s value, and must have the tensor’s element type.
sparse_tensor.foreach
can also operate on loop-carried variables and returns
the final values after loop termination. The initial values of the variables are
passed as additional SSA operands to the “sparse_tensor.foreach” following the n + 1
SSA values mentioned above (n coordinates and 1 value).
The region must terminate with a “sparse_tensor.yield” that passes the current values of all loop-carried variables to the next iteration, or to the result, if at the last iteration. The number and static types of loop-carried variables may not change with iterations.
For example:
%c0 = arith.constant 0 : i32
%ret = sparse_tensor.foreach in %0 init(%c0): tensor<?x?xi32, #DCSR>, i32 -> i32 do {
^bb0(%arg1: index, %arg2: index, %arg3: i32, %iter: i32):
%sum = arith.add %iter, %arg3
sparse_tensor.yield %sum
}
It is important to note that the generated loop iterates over elements in their storage order. However, regardless of the storage scheme used by the tensor, the block is always given the dimension-coordinates.
For example:
#COL_MAJOR = #sparse_tensor.encoding<{
dimLevelType = [ "compressed", "compressed" ],
dimOrdering = affine_map<(i,j) -> (j,i)>
}>
// foreach on a column-major sparse tensor
sparse_tensor.foreach in %0 : tensor<2x3xf64, #COL_MAJOR> do {
^bb0(%row: index, %col: index, %arg3: f64):
// [%row, %col] -> [0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1]
}
#ROW_MAJOR = #sparse_tensor.encoding<{
dimLevelType = [ "compressed", "compressed" ],
}>
// foreach on a row-major sparse tensor
sparse_tensor.foreach in %0 : tensor<2x3xf64, #ROW_MAJOR> do {
^bb0(%row: index, %col: index, %arg3: f64):
// [%row, %col] -> [0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]
}
// foreach on a row-major dense tensor but visit column first
sparse_tensor.foreach in %0 {order=affine_map<(i,j)->(j,i)>}: tensor<2x3xf64> do {
^bb0(%row: index, %col: index, %arg3: f64):
// [%row, %col] -> [0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1]
}
Implementations§
source§impl<'c> ForeachOperation<'c>
impl<'c> ForeachOperation<'c>
sourcepub fn as_operation(&self) -> &Operation<'c>
pub fn as_operation(&self) -> &Operation<'c>
Returns a generic operation.
sourcepub fn builder(
context: &'c Context,
location: Location<'c>
) -> ForeachOperationBuilder<'c, Unset, Unset, Unset, Unset>
pub fn builder( context: &'c Context, location: Location<'c> ) -> ForeachOperationBuilder<'c, Unset, Unset, Unset, Unset>
Creates a builder.