Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6723,6 +6723,61 @@ class FoldTransposeShapeCast final : public OpRewritePattern<TransposeOp> {
}
};

/// Folds transpose(from_elements(...)) into a new from_elements with permuted
/// operands matching the transposed shape.
class FoldTransposeFromElements final : public OpRewritePattern<TransposeOp> {
public:
using Base::Base;
LogicalResult matchAndRewrite(vector::TransposeOp transposeOp,
PatternRewriter &rewriter) const override {
auto fromElementsOp =
transposeOp.getVector().getDefiningOp<vector::FromElementsOp>();
if (!fromElementsOp)
return failure();

VectorType srcTy = fromElementsOp.getDest().getType();
VectorType dstTy = transposeOp.getType();

ArrayRef<int64_t> permutation = transposeOp.getPermutation();
int64_t rank = srcTy.getRank();

// Build inverse permutation to map destination indices back to source.
SmallVector<int64_t, 4> inversePerm(rank, 0);
for (int64_t i = 0; i < rank; ++i)
inversePerm[permutation[i]] = i;

ArrayRef<int64_t> srcShape = srcTy.getShape();
ArrayRef<int64_t> dstShape = dstTy.getShape();
SmallVector<int64_t, 4> srcIdx(rank, 0);
SmallVector<int64_t, 4> dstIdx(rank, 0);
SmallVector<int64_t, 4> srcStrides = computeStrides(srcShape);
SmallVector<int64_t, 4> dstStrides = computeStrides(dstShape);

auto elements = fromElementsOp.getElements();
SmallVector<Value> newElements;
int64_t dstNumElements = dstTy.getNumElements();
newElements.reserve(dstNumElements);

// For each element in destination row-major order, pick the corresponding
// source element.
for (int64_t lin = 0; lin < dstNumElements; ++lin) {
// Pick the destination element index.
dstIdx = delinearize(lin, dstStrides);
// Map the destination element index to the source element index.
for (int64_t j = 0; j < rank; ++j)
srcIdx[j] = dstIdx[inversePerm[j]];
// Linearize the source element index.
int64_t srcLin = linearize(srcIdx, srcStrides);
// Add the source element to the new elements.
newElements.push_back(elements[srcLin]);
}

rewriter.replaceOpWithNewOp<FromElementsOp>(transposeOp, dstTy,
newElements);
return success();
}
};

/// Folds transpose(broadcast(x)) to broadcast(x) if the transpose is
/// 'order preserving', where 'order preserving' means the flattened
/// inputs and outputs of the transpose have identical (numerical) values.
Expand Down Expand Up @@ -6823,7 +6878,8 @@ class FoldTransposeBroadcast : public OpRewritePattern<vector::TransposeOp> {
void vector::TransposeOp::getCanonicalizationPatterns(
RewritePatternSet &results, MLIRContext *context) {
results.add<FoldTransposeCreateMask, FoldTransposeShapeCast, TransposeFolder,
FoldTransposeSplat, FoldTransposeBroadcast>(context);
FoldTransposeSplat, FoldTransposeFromElements,
FoldTransposeBroadcast>(context);
}

//===----------------------------------------------------------------------===//
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ func.func @constant_mask_transpose_to_transposed_constant_mask() -> (vector<2x3x

// -----

// CHECK-LABEL: transpose_from_elements_2d
func.func @transpose_from_elements_2d(%a0: i32, %a1: i32, %a2: i32,
%a3: i32, %a4: i32, %a5: i32) -> vector<3x2xi32> {
%v = vector.from_elements %a0, %a1, %a2, %a3, %a4, %a5 : vector<2x3xi32>
%t = vector.transpose %v, [1, 0] : vector<2x3xi32> to vector<3x2xi32>
return %t : vector<3x2xi32>
// CHECK: %[[R:.*]] = vector.from_elements %arg0, %arg3, %arg1, %arg4, %arg2, %arg5 : vector<3x2xi32>
// CHECK-NOT: vector.transpose
}

// -----

func.func @extract_strided_slice_of_constant_mask() -> (vector<2x2xi1>) {
%0 = vector.constant_mask [2, 2] : vector<4x3xi1>
%1 = vector.extract_strided_slice %0
Expand Down