MLIR
20.0.0git
|
This class assists with generating IR required to materialize an arbitrary-sized slice from the result of a CollapseShapeOp. More...
#include "mlir/Dialect/Tensor/Transforms/TransformUtils.h"
Public Member Functions | |
ExtractSliceFromCollapseHelper (tensor::CollapseShapeOp collapseShapeOp, ArrayRef< OpFoldResult > collapseShapeInputShape, ArrayRef< OpFoldResult > collapseShapeOutputShape, ArrayRef< Range > extractSliceParams, const llvm::SmallBitVector &linearizedDimensions, const llvm::SmallBitVector &slicedDimensions, ArrayRef< Value > tiledSizes) | |
const SmallVector< Value > & | getIterationSpaceSizes () |
Return the upper bounds of the iteration space (with 0 offset and stride 1) required to create the desired slice. More... | |
std::pair< Value, SmallVector< Range > > | emitLoopNestBody (OpBuilder &builder, Location loc, ValueRange tileInductionVars) |
Generates the IR inside of the caller's loop nest for 1) inverting the index mappings of the ExtractSliceOp->CollapseShapeOp chain and 2) extracting the CollapseShapeOp source tensor tile for this specified iteration space point tileInductionVars and 3) calculating where to insert the extracted tile. More... | |
Static Public Member Functions | |
static FailureOr< ExtractSliceFromCollapseHelper > | create (OpBuilder &b, tensor::CollapseShapeOp op, ArrayRef< Range > sliceParams) |
Given a CollapseShapeOp and a set of ranges describing the desired slice of its result, emits IR to materialize the shapes of the input and output tensors, and returns an instance of the initialized class. More... | |
static FailureOr< ExtractSliceFromCollapseHelper > | create (OpBuilder &b, tensor::CollapseShapeOp collapseOp, tensor::ExtractSliceOp extractOp) |
Given a CollapseShapeOp and an ExtractSliceOp acting on its result, emits IR to materialize the shapes of the input and output tensors of the CollapseShapeOp, and returns an instance of the initialized class. More... | |
This class assists with generating IR required to materialize an arbitrary-sized slice from the result of a CollapseShapeOp.
In order to accomplish this, a loop nest or similar operation must be created by the caller. The purpose of the loop nest is to generate a "tiling by 1" of all sliced dimensions. The "tiling by 1" assembles all elements of the result tile over dimensions that would have been impossible to directly slice.
The class provides three methods:
ExtractSliceFromCollapseHelper::create
: emits IR that should appear before the loop nest and populates the internal state.ExtractSliceFromCollapseHelper::getIterationSpaceSizes
: returns parameters used by the caller to construct the loop nest.ExtractSliceFromCollapseHelper::emitLoopNestBody
: emits IR to construct a "size-1 tile" of the desired result and returns a set of ranges where the tile should be inserted into the destination tensor.The caller should first call ExtractSliceFromCollapseHelper::create
and then create a destination tensor that is the same size as the desired slice. The caller then creates a loop nest that iterates over the multi-dimensional iteration space defined by [0, ub[0]) x [0, ub[1]] x ... x [0, ub[N-1]]
where ub
is the upper bound given by ExtractSliceFromCollapseHelper::getIterationSpaceSizes
. Inside the body of the loop nest, the caller should call ExtractSliceFromCollapseHelper::emitLoopNestBody
and provide the induction variables. This returns a sub-tile and a set of ranges that describe where this tile should be inserted into the result by the caller. For a complete example of usage, see the examples in the TestTensorTransforms pass.
Consider the following IR:
We can construct %2 by generating the following, which only uses %0
:
Each step above is explained below.
This step is self-explanatory and performed by the caller. It can be done before or after calling ExtractSliceFromCollapseHelper::create
, which materializes the source shape (%0, %1, %2
).
The caller creates the loop nest (depicted here is scf.for
, but any other similar op can be used). The iteration should start at zero and proceed with step size 1 to the upper bounds given by ExtractSliceFromCollapseHelper::getIterationSpaceSizes
. This forms the basis for the "tiling by 1".
space of %0.
This step is performed by ExtractSliceFromCollapseHelper::emitLoopNestBody
.
The induction variables iv0
and iv1
live in the index space of %2 (for dimensions 0 and 1, respectively). lin0
and lin1
are the result of inverting or resolve the index space transformation represented by the slice operation, accounting for offset and stride. Subsequently, mi0
and mi1
are the result of applying the inverse index space transformation represented by tensor.collapse_shape
. This is accomplished using affine.delinearize_index
. Note that iv0 and iv1 now correspond to multi-indices mi0:3
and mi1:2
.
This step is also performed by ExtractSliceFromCollapseHelper::emitLoopNestBody
.
The indices mi0
and mi1
are used to extract a slice from %0. This slice is then collapsed down to match the result rank.
This step is performed by the caller using the results of ExtractSliceFromCollapseHelper::emitLoopNestBody
.
In the above example, the slice insertion parameters are straightforward, but in other possible situations, the slice parameters are more complicated, which is why this helper calculates them for the caller. These other situations correspond to:
Definition at line 149 of file TransformUtils.h.
|
inline |
Definition at line 166 of file TransformUtils.h.
|
static |
Given a CollapseShapeOp and an ExtractSliceOp acting on its result, emits IR to materialize the shapes of the input and output tensors of the CollapseShapeOp, and returns an instance of the initialized class.
Returns failure if the slice is rank-reducing.
Definition at line 63 of file ExtractSliceFromReshapeUtils.cpp.
|
static |
Given a CollapseShapeOp and a set of ranges describing the desired slice of its result, emits IR to materialize the shapes of the input and output tensors, and returns an instance of the initialized class.
Returns failure if the slice is rank-reducing.
Definition at line 80 of file ExtractSliceFromReshapeUtils.cpp.
References mlir::getLinearizedDimensions(), mlir::tensor::getMixedSizes(), mlir::getSlicedDimensions(), mlir::getValueOrCreateConstantIndexOp(), and mlir::reifyResultShapes().
std::pair< Value, SmallVector< Range > > tensor::ExtractSliceFromCollapseHelper::emitLoopNestBody | ( | OpBuilder & | builder, |
Location | loc, | ||
ValueRange | tileInductionVars | ||
) |
Generates the IR inside of the caller's loop nest for 1) inverting the index mappings of the ExtractSliceOp->CollapseShapeOp chain and 2) extracting the CollapseShapeOp source tensor tile for this specified iteration space point tileInductionVars
and 3) calculating where to insert the extracted tile.
The returned pair consists of the results of (2) and (3) and should be used by the caller to insert into the destination tensor.
Definition at line 121 of file ExtractSliceFromReshapeUtils.cpp.
References mlir::OpBuilder::create(), mlir::Builder::getContext(), invertCollapseShapeIndexing(), and invertSliceIndexing().
|
inline |
Return the upper bounds of the iteration space (with 0 offset and stride 1) required to create the desired slice.
Note that this is not the same as the sizes
parameters of the ExtractSliceOp because not all dimensions of the slice are required to be tiled to form the result.
Definition at line 184 of file TransformUtils.h.