MLIR  22.0.0git
Functions
ReshapeOpsUtils.cpp File Reference
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include <numeric>
#include <optional>

Go to the source code of this file.

Functions

static FailureOr< ReassociationIndexRange > findReassociationRangeForDynamicDim (ArrayRef< int64_t > sourceShape, int64_t sourceStartIdx, bool matchGreedily=false)
 Starting from sourceStartIdx, searches sourceShape for the first sequence that can be collapsed into a dynamic dimension (at least one must be present in the source). More...
 
static FailureOr< ReassociationIndexRange > findReassociationRangeForSize (ArrayRef< int64_t > sourceShape, int64_t sourceStartIdx, int64_t targetSize, bool matchGreedily=false)
 Starting from sourceStartIdx, searches sourceShape for the first sequence of static dimensions such that their product matches targetSize. More...
 
static FailureOr< SmallVector< ReassociationIndexRange > > findReassociationRangesForCollapse (ArrayRef< int64_t > sourceShape, ArrayRef< int64_t > targetShape)
 Attempts to find a valid collapsing reassociation of sourceShape into targetShape through a simple traversal. More...
 
static FailureOr< SmallVector< ReassociationIndexRange > > findReassociationRangesForCollapse (ArrayRef< int64_t > sourceShape, ArrayRef< int64_t > targetShape, bool iterateRightToLeft)
 A variant of findReassociationRangesForCollapse(...) that can also scan the shapes right-to-left. More...
 
template<typename AffineExprTy >
static unsigned getMaxPosOfType (ArrayRef< ReassociationExprs > exprArrays)
 
static std::optional< int64_t > getUniqueNonUnitDim (ArrayRef< int64_t > indices, ArrayRef< int64_t > shape)
 Returns the index of the only non-unit dimension among indices of shape, if such a dimension exists and indices has more than one element. More...
 
static SmallVector< std::optional< int64_t > > getCollapseShapeTrivialSegments (RankedTensorType sourceType, ArrayRef< ReassociationIndices > reassociationIndices)
 
static FailureOr< SmallVector< std::optional< int64_t > > > canCollapseShapeBeSimplifiedByRankReducingSlice (RankedTensorType sourceType, ArrayRef< ReassociationIndices > reassociationIndices)
 Returns true if any of the segments of the reassociation indices for a collapsing reshape can be simplified using a rank-reducing slice. More...
 

Function Documentation

◆ canCollapseShapeBeSimplifiedByRankReducingSlice()

static FailureOr<SmallVector<std::optional<int64_t> > > canCollapseShapeBeSimplifiedByRankReducingSlice ( RankedTensorType  sourceType,
ArrayRef< ReassociationIndices reassociationIndices 
)
static

Returns true if any of the segments of the reassociation indices for a collapsing reshape can be simplified using a rank-reducing slice.

Definition at line 648 of file ReshapeOpsUtils.cpp.

References getCollapseShapeTrivialSegments().

◆ findReassociationRangeForDynamicDim()

static FailureOr<ReassociationIndexRange> findReassociationRangeForDynamicDim ( ArrayRef< int64_t >  sourceShape,
int64_t  sourceStartIdx,
bool  matchGreedily = false 
)
static

Starting from sourceStartIdx, searches sourceShape for the first sequence that can be collapsed into a dynamic dimension (at least one must be present in the source).

By default, lazily returns once the first dynamic dimension has been found. Setting matchGreedily as true will also mark all subsequent source dimensions for collapsing into the target.

Definition at line 102 of file ReshapeOpsUtils.cpp.

Referenced by findReassociationRangesForCollapse().

◆ findReassociationRangeForSize()

static FailureOr<ReassociationIndexRange> findReassociationRangeForSize ( ArrayRef< int64_t >  sourceShape,
int64_t  sourceStartIdx,
int64_t  targetSize,
bool  matchGreedily = false 
)
static

Starting from sourceStartIdx, searches sourceShape for the first sequence of static dimensions such that their product matches targetSize.

By default, lazily returns once the product matches the target size. Setting matchGreedily as true will append all neighboring unit dimensions (dimensions of 1) to the match.

Definition at line 131 of file ReshapeOpsUtils.cpp.

Referenced by findReassociationRangesForCollapse().

◆ findReassociationRangesForCollapse() [1/2]

static FailureOr<SmallVector<ReassociationIndexRange> > findReassociationRangesForCollapse ( ArrayRef< int64_t >  sourceShape,
ArrayRef< int64_t >  targetShape 
)
static

Attempts to find a valid collapsing reassociation of sourceShape into targetShape through a simple traversal.

If successful, an array of source index ranges is returned, correspondingly to each dimension in the target shape. The resulting indices shall fully cover the sourceShape without overlaps.

The algorithm is essentially a lazy one, searching for non-greedy matches - it will only yield a greedy match for the last target dimension. FIXME: The algorithm can only backtrack when it needs to append an offset for a static target dimension to the preceding dynamic one (this retains the linear complexity). As feasible, consider adding further backtracking routines to enable more reassociations, e.g.:

  • ?x2x?x2 into ?x2

Definition at line 201 of file ReshapeOpsUtils.cpp.

References mlir::remark::failed(), findReassociationRangeForDynamicDim(), and findReassociationRangeForSize().

Referenced by findReassociationRangesForCollapse(), and mlir::getReassociationIndicesForCollapse().

◆ findReassociationRangesForCollapse() [2/2]

static FailureOr<SmallVector<ReassociationIndexRange> > findReassociationRangesForCollapse ( ArrayRef< int64_t >  sourceShape,
ArrayRef< int64_t >  targetShape,
bool  iterateRightToLeft 
)
static

A variant of findReassociationRangesForCollapse(...) that can also scan the shapes right-to-left.

Definition at line 257 of file ReshapeOpsUtils.cpp.

References mlir::remark::failed(), and findReassociationRangesForCollapse().

◆ getCollapseShapeTrivialSegments()

static SmallVector<std::optional<int64_t> > getCollapseShapeTrivialSegments ( RankedTensorType  sourceType,
ArrayRef< ReassociationIndices reassociationIndices 
)
static

◆ getMaxPosOfType()

template<typename AffineExprTy >
static unsigned getMaxPosOfType ( ArrayRef< ReassociationExprs exprArrays)
static

Definition at line 410 of file ReshapeOpsUtils.cpp.

References max().

◆ getUniqueNonUnitDim()

static std::optional<int64_t> getUniqueNonUnitDim ( ArrayRef< int64_t >  indices,
ArrayRef< int64_t >  shape 
)
static

Returns the index of the only non-unit dimension among indices of shape, if such a dimension exists and indices has more than one element.

Otherwise, return std::nullopt.

Definition at line 616 of file ReshapeOpsUtils.cpp.

Referenced by getCollapseShapeTrivialSegments().