Skip to content

Commit 42b1783

Browse files
committed
fixup! fixup! [mlir][linalg] Refactor vectorization hooks to improve code reuse
* Restore the changes in transform-e2e.mlir + transform-vector.mlir * Updated in_bounds attribute calculation in `createWriteOrMaskedWrite` - otherwise transform-e2e.mlir goes into an infite loop. I will create a repro and open a GitHub issue before landing this. * The in_bounds attribute calculaiton is incorrect and I will create a GitHub ticket to fix it before merging this. See the comments in this patch.
1 parent cf4a2c5 commit 42b1783

File tree

4 files changed

+17
-37
lines changed

4 files changed

+17
-37
lines changed

mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,9 +1659,10 @@ createWriteOrMaskedWrite(OpBuilder &builder, Location loc, Value vectorToStore,
16591659
static_cast<size_t>(vecToStoreType.getRank()) &&
16601660
"Insufficient number of input vector sizes!");
16611661
// Update the inBounds attribute.
1662+
// FIXME: This computation is too weak - it ignores the write indices.
16621663
for (unsigned i = 0; i < vecToStoreRank; i++)
16631664
inBoundsVal[i] =
1664-
(destShape[i] == inputVecSizesForLeadingDims[i]) &&
1665+
(destShape[i] >= inputVecSizesForLeadingDims[i]) &&
16651666
!ShapedType::isDynamic(destShape[destRank - vecToStoreRank + i]);
16661667
}
16671668

mlir/lib/Dialect/Vector/Utils/VectorUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ Value vector::createReadOrMaskedRead(OpBuilder &builder, Location loc,
346346

347347
if (useInBoundsInsteadOfMasking) {
348348
// Update the inBounds attribute.
349+
// FIXME: This computation is too weak - it ignores the read indices.
349350
for (unsigned i = 0; i < readRank; i++)
350351
inBoundsVal[i] = (sourceShape[i] == inputVectorSizes[i]) &&
351352
!ShapedType::isDynamic(sourceShape[i]);

mlir/test/Dialect/LLVM/transform-e2e.mlir

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ module attributes {transform.with_named_sequence} {
1818
%1, %loops:3 = transform.structured.tile_using_for %0 tile_sizes [2, 2, 2] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
1919
%2 = transform.get_parent_op %1 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
2020
transform.structured.vectorize_children_and_apply_patterns %2 : (!transform.any_op) -> !transform.any_op
21+
%b = transform.bufferization.one_shot_bufferize layout{IdentityLayoutMap}
22+
%module_op {bufferize_function_boundaries = true}
23+
: (!transform.any_op) -> !transform.any_op
2124

22-
%f = transform.structured.match ops{["func.func"]} in %module_op
25+
%f = transform.structured.match ops{["func.func"]} in %b
2326
: (!transform.any_op) -> !transform.any_op
2427

2528
// TODO: group these lower-level controls into various properly named vector
2629
// lowering TD macros.
2730
transform.apply_patterns to %f {
28-
transform.apply_patterns.vector.lower_masked_transfers
2931
transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"
3032
transform.apply_patterns.vector.transfer_permutation_patterns
3133
transform.apply_patterns.vector.lower_multi_reduction lowering_strategy = "innerparallel"
@@ -35,10 +37,6 @@ module attributes {transform.with_named_sequence} {
3537
transform.apply_patterns.vector.lower_shape_cast
3638
transform.apply_patterns.vector.lower_transpose lowering_strategy = "shuffle_1d"
3739
} : !transform.any_op
38-
39-
%b = transform.bufferization.one_shot_bufferize layout{IdentityLayoutMap}
40-
%module_op {bufferize_function_boundaries = true}
41-
: (!transform.any_op) -> !transform.any_op
4240
transform.yield
4341
}
4442
}

mlir/test/Dialect/Vector/transform-vector.mlir

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ func.func @matmul_tensors(
66
-> tensor<8x32xf32> {
77
// CHECK-NOT: linalg
88
// CHECK: vector.extract {{.*}} : vector<4xf32> from vector<8x4xf32>
9-
// TODO: `vector.maskedstore` below could safely be replaced with
10-
// `vector.store`. It's present due to the vectorization logic for
11-
// `tensor.insert_slice` conservatively applying masks. However, it this case,
12-
// we should be able to remove it via value-bounds checks.
13-
// CHECK: vector.maskedstore {{.*}} : memref<8x32xf32>, vector<4xi1>, vector<4xf32>
9+
// CHECK: vector.store {{.*}} : memref<8x32xf32>, vector<4xf32>
1410
%0 = linalg.matmul ins(%arg0, %arg1: tensor<8x16xf32>, tensor<16x32xf32>)
1511
outs(%arg2: tensor<8x32xf32>)
1612
-> tensor<8x32xf32>
@@ -24,16 +20,16 @@ module attributes {transform.with_named_sequence} {
2420
: (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op, !transform.any_op)
2521
%2 = transform.get_parent_op %1 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
2622
transform.structured.vectorize_children_and_apply_patterns %2 : (!transform.any_op) -> !transform.any_op
23+
%b = transform.bufferization.one_shot_bufferize
24+
layout{IdentityLayoutMap} %module_op
25+
{bufferize_function_boundaries = true, allow_return_allocs = true}
26+
: (!transform.any_op) -> !transform.any_op
2727

28-
%f = transform.structured.match ops{["func.func"]} in %module_op
28+
%f = transform.structured.match ops{["func.func"]} in %b
2929
: (!transform.any_op) -> !transform.any_op
3030

3131
// TODO: group these lower-level controls into various properly named vector
3232
// lowering TD macros.
33-
transform.apply_patterns to %f {
34-
transform.apply_patterns.vector.lower_masked_transfers
35-
} : !transform.any_op
36-
3733
transform.apply_patterns to %f {
3834
transform.apply_patterns.vector.lower_contraction lowering_strategy = "outerproduct"
3935
} : !transform.any_op
@@ -50,37 +46,21 @@ module attributes {transform.with_named_sequence} {
5046
transform.apply_patterns.vector.split_transfer_full_partial split_transfer_strategy = "linalg-copy"
5147
} : !transform.any_op
5248

53-
// By default, UnrollTransferWriteConversion (applied below via
54-
// `transfer_to_scf`) will only work on MemRef(s). While there's an option
55-
// to relax that, it's currently not wired-up with the TD logic. Bufferize
56-
// here as otherwise unrolling will not work.
57-
// TODO: Extend `transform.apply_patterns.vector.transfer_to_scf` to allow
58-
// unrolling xfer Ops on tensors and move bufferization all the way down.
59-
%b = transform.bufferization.one_shot_bufferize
60-
layout{IdentityLayoutMap} %module_op
61-
{bufferize_function_boundaries = true, allow_return_allocs = true}
62-
: (!transform.any_op) -> !transform.any_op
63-
64-
%fb = transform.structured.match ops{["func.func"]} in %b
65-
: (!transform.any_op) -> !transform.any_op
66-
67-
transform.apply_patterns to %fb {
49+
transform.apply_patterns to %f {
6850
transform.apply_patterns.vector.transfer_to_scf max_transfer_rank = 1 full_unroll = true
6951
} : !transform.any_op
7052

71-
transform.apply_patterns to %fb {
53+
transform.apply_patterns to %f {
7254
transform.apply_patterns.vector.lower_transfer max_transfer_rank = 1
7355
} : !transform.any_op
7456

75-
transform.apply_patterns to %fb {
57+
transform.apply_patterns to %f {
7658
transform.apply_patterns.vector.lower_shape_cast
7759
} : !transform.any_op
7860

79-
transform.apply_patterns to %fb {
61+
transform.apply_patterns to %f {
8062
transform.apply_patterns.vector.lower_transpose lowering_strategy = "shuffle_1d"
8163
} : !transform.any_op
82-
83-
8464
transform.yield
8565
}
8666
}

0 commit comments

Comments
 (0)