Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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> 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> srcIdx(rank, 0);
SmallVector<int64_t> dstIdx(rank, 0);
SmallVector<int64_t> srcStrides = computeStrides(srcShape);
SmallVector<int64_t> dstStrides = computeStrides(dstShape);

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

// For each element in destination row-major order, pick the corresponding
// source element.
for (int64_t lin = 0; lin < dstNumElements; ++lin) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does lin represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So "lin" is short for "linear index" - it's a 1D index that represents the position of an element when the multi-dimensional vector is laid out in row-major order in memory. I felt it was a good iter name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find lin a bit too enigmatic. Why not linearIdx?

// 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.
elementsNew.push_back(elementsOld[srcLin]);
}

rewriter.replaceOpWithNewOp<FromElementsOp>(transposeOp, dstTy,
elementsNew);
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
42 changes: 42 additions & 0 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,48 @@ func.func @constant_mask_transpose_to_transposed_constant_mask() -> (vector<2x3x

// -----

// CHECK-LABEL: transpose_from_elements_1d
func.func @transpose_from_elements_1d(%el_0: i32, %el_1: i32) -> vector<2xi32> {
%v = vector.from_elements %el_0, %el_1 : vector<2xi32>
%t = vector.transpose %v, [0] : vector<2xi32> to vector<2xi32>
return %t : vector<2xi32>
// CHECK: %[[R:.*]] = vector.from_elements %[[EL_0:.*]], %[[EL_1:.*]] : vector<2xi32>
// CHECK-NOT: vector.transpose
}

// CHECK-LABEL: transpose_from_elements_2d
func.func @transpose_from_elements_2d(
%el_0_0: i32, %el_0_1: i32, %el_0_2: i32,
%el_1_0: i32, %el_1_1: i32, %el_1_2: i32
) -> vector<3x2xi32> {
%v = vector.from_elements %el_0_0, %el_0_1, %el_0_2, %el_1_0, %el_1_1, %el_1_2 : vector<2x3xi32>
%t = vector.transpose %v, [1, 0] : vector<2x3xi32> to vector<3x2xi32>
return %t : vector<3x2xi32>
// CHECK: %[[R:.*]] = vector.from_elements %[[EL_0_0:.*]], %[[EL_1_0:.*]], %[[EL_0_1:.*]], %[[EL_1_1:.*]], %[[EL_0_2:.*]], %[[EL_1_2:.*]] : vector<3x2xi32>
// CHECK-NOT: vector.transpose
}

// CHECK-LABEL: transpose_from_elements_3d
func.func @transpose_from_elements_3d(
%el_0_0_0: i32, %el_0_0_1: i32, %el_0_1_0: i32, %el_0_1_1: i32, %el_0_2_0: i32, %el_0_2_1: i32,
%el_1_0_0: i32, %el_1_0_1: i32, %el_1_1_0: i32, %el_1_1_1: i32, %el_1_2_0: i32, %el_1_2_1: i32
) -> vector<2x2x3xi32> {
%v = vector.from_elements
%el_0_0_0, %el_0_0_1,
%el_0_1_0, %el_0_1_1,
%el_0_2_0, %el_0_2_1,
%el_1_0_0, %el_1_0_1,
%el_1_1_0, %el_1_1_1,
%el_1_2_0, %el_1_2_1
: vector<2x3x2xi32>
%t = vector.transpose %v, [0, 2, 1] : vector<2x3x2xi32> to vector<2x2x3xi32>
return %t : vector<2x2x3xi32>
// CHECK: %[[R:.*]] = vector.from_elements %[[EL_0_0_0:.*]], %[[EL_0_1_0:.*]], %[[EL_0_2_0:.*]], %[[EL_0_0_1:.*]], %[[EL_0_1_1:.*]], %[[EL_0_2_1:.*]], %[[EL_1_0_0:.*]], %[[EL_1_1_0:.*]], %[[EL_1_2_0:.*]], %[[EL_1_0_1:.*]], %[[EL_1_1_1:.*]], %[[EL_1_2_1:.*]] : vector<2x2x3xi32>
// 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