Skip to content

[mlir][vector] Support scalable vectors when unrolling vector.bitcast #94197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2024
Merged
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
5 changes: 5 additions & 0 deletions mlir/include/mlir/Dialect/Utils/IndexingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class TileOffsetRangeImpl {
return getDynamicTileOffsets(linearIndex);
}

size_t getRank() const { return tileShape.size(); }

private:
/// The sub-shape that divides the larger outer shape (which is provided to
/// the constructor).
Expand Down Expand Up @@ -388,6 +390,9 @@ class StaticTileOffsetRange {
/// Returns the total number of tiles that fit in the larger shape.
size_t size() const { return params.getMaxLinearIndex(); }

/// Returns rank of the iterator's shape.
size_t getRank() const { return params.getRank(); }

private:
const ParamsTy params;
IteratorTy beginValue;
Expand Down
17 changes: 6 additions & 11 deletions mlir/lib/Dialect/Vector/Transforms/LowerVectorBitCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,12 @@ class UnrollBitCastOp final : public OpRewritePattern<vector::BitCastOp> {
if (!unrollIterator)
return failure();

// TODO: Support the scalable vector cases. It is not supported because
// the final rank could be values other than `targetRank`. It makes creating
// the result type of new vector.bitcast ops much harder.
if (resultType.isScalable()) {
return rewriter.notifyMatchFailure(op,
"unrolling vector.bitcast on scalable "
"vectors is not yet implemented");
}

ArrayRef<int64_t> shape = resultType.getShape().take_back(targetRank);
auto bitcastResType = VectorType::get(shape, resultType.getElementType());
auto unrollRank = unrollIterator->getRank();
ArrayRef<int64_t> shape = resultType.getShape().drop_front(unrollRank);
ArrayRef<bool> scalableDims =
resultType.getScalableDims().drop_front(unrollRank);
auto bitcastResType =
VectorType::get(shape, resultType.getElementType(), scalableDims);

Location loc = op.getLoc();
Value result = rewriter.create<arith::ConstantOp>(
Expand Down
34 changes: 33 additions & 1 deletion mlir/test/Dialect/Vector/vector-bitcast-lowering-transforms.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,39 @@ func.func @vector_bitcast_4d_with_scalable_dim(%arg0: vector<1x2x[3]x4xi64>) ->
return %0 : vector<1x2x[3]x8xi32>
}
// CHECK-LABEL: func.func @vector_bitcast_4d_with_scalable_dim
// CHECK: vector.bitcast {{.+}} : vector<1x2x[3]x4xi64> to vector<1x2x[3]x8xi32>
// CHECK-SAME: %[[IN:[a-zA-Z0-9]+]]
// CHECK: %[[INIT:.+]] = arith.constant dense<0> : vector<1x2x[3]x8xi32>
// CHECK: %[[V1:.+]] = vector.extract %[[IN]][0, 0] : vector<[3]x4xi64> from vector<1x2x[3]x4xi64>
// CHECK: %[[B1:.+]] = vector.bitcast %[[V1]] : vector<[3]x4xi64> to vector<[3]x8xi32>
// CHECK: %[[R1:.+]] = vector.insert %[[B1]], %[[INIT]] [0, 0] : vector<[3]x8xi32> into vector<1x2x[3]x8xi32>
// CHECK: %[[V2:.+]] = vector.extract %[[IN]][0, 1] : vector<[3]x4xi64> from vector<1x2x[3]x4xi64>
// CHECK: %[[B2:.+]] = vector.bitcast %[[V2]] : vector<[3]x4xi64> to vector<[3]x8xi32>
// CHECK: %[[R2:.+]] = vector.insert %[[B2]], %[[R1]] [0, 1] : vector<[3]x8xi32> into vector<1x2x[3]x8xi32>
// CHECK: return %[[R2]] : vector<1x2x[3]x8xi32>

func.func @vector_bitcast_2d_trailing_scalable_dim(%arg0: vector<2x[2]xi64>) -> vector<2x[4]xi32> {
%0 = vector.bitcast %arg0 : vector<2x[2]xi64> to vector<2x[4]xi32>
return %0 : vector<2x[4]xi32>
}
// CHECK-LABEL: func.func @vector_bitcast_2d_trailing_scalable_dim
// CHECK-SAME: %[[IN:[a-zA-Z0-9]+]]
// CHECK: %[[INIT:.+]] = arith.constant dense<0> : vector<2x[4]xi32>
// CHECK: %[[V1:.+]] = vector.extract %[[IN]][0] : vector<[2]xi64> from vector<2x[2]xi64>
// CHECK: %[[B1:.+]] = vector.bitcast %[[V1]] : vector<[2]xi64> to vector<[4]xi32>
// CHECK: %[[R1:.+]] = vector.insert %[[B1]], %[[INIT]] [0] : vector<[4]xi32> into vector<2x[4]xi32>
// CHECK: %[[V2:.+]] = vector.extract %[[IN]][1] : vector<[2]xi64> from vector<2x[2]xi64>
// CHECK: %[[B2:.+]] = vector.bitcast %[[V2]] : vector<[2]xi64> to vector<[4]xi32>
// CHECK: %[[R2:.+]] = vector.insert %[[B2]], %[[R1]] [1] : vector<[4]xi32> into vector<2x[4]xi32>
// CHECK: return %[[R2]] : vector<2x[4]xi32>

func.func @negative_vector_bitcast_2d_leading_scalable_dim(%arg0: vector<[2]x2xi64>) -> vector<[2]x4xi32>
{
%0 = vector.bitcast %arg0 : vector<[2]x2xi64> to vector<[2]x4xi32>
return %0 : vector<[2]x4xi32>
}
// CHECK-LABEL: func.func @negative_vector_bitcast_2d_leading_scalable_dim
// CHECK-NOT: vector.extract
// CHECK-NOT: vector.insert

module attributes {transform.with_named_sequence} {
transform.named_sequence @__transform_main(%module_op: !transform.any_op {transform.readonly}) {
Expand Down
Loading