Skip to content

Commit 0a3347d

Browse files
authored
[mlir][linalg] Fix idx comparison in the vectorizer (#112900)
Fixes loop comparison condition in the vectorizer. As that logic is used specifically for vectorising `tensor.extract`, I also added a test that violates the assumptions made inside `getTrailingNonUnitLoopDimIdx`, namely that Linalg loops are non-empty. Vectorizer pre-conditions will capture that much earlier making sure that `getTrailingNonUnitLoopDimIdx` is only run when all the assumptions are actually met. Thank you for pointing this out, @pfusik !
1 parent 783901b commit 0a3347d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,10 @@ static uint64_t getTrailingNonUnitLoopDimIdx(LinalgOp linalgOp) {
863863
llvm::count_if(loopRanges, [](int64_t dim) { return dim != 1; }) == 1) &&
864864
"For statically shaped Linalg Ops, only one "
865865
"non-unit loop dim is expected");
866+
assert(loopRanges.size() != 0 && "Empty loops, nothing to analyse.");
866867

867868
size_t idx = loopRanges.size() - 1;
868-
for (; idx >= 0; idx--)
869+
for (; idx != 0; idx--)
869870
if (loopRanges[idx] != 1)
870871
break;
871872

mlir/test/Dialect/Linalg/vectorize-tensor-extract.mlir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ module attributes {transform.with_named_sequence} {
3636
}
3737
}
3838

39+
// -----
40+
41+
#map = affine_map<() -> ()>
42+
func.func @negative_no_loops(%arg0: tensor<f32>, %arg1: tensor<f32>) -> tensor<f32> {
43+
%1 = linalg.generic {
44+
indexing_maps = [#map],
45+
iterator_types = []
46+
} outs(%arg1 : tensor<f32>) {
47+
^bb0(%arg4: f32):
48+
%2 = tensor.extract %arg0[] : tensor<f32>
49+
linalg.yield %2 : f32
50+
} -> tensor<f32>
51+
return %1 : tensor<f32>
52+
}
53+
// CHECK-LABEL: func.func @negative_no_loops
54+
// CHECK: tensor.extract
55+
56+
module attributes {transform.with_named_sequence} {
57+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
58+
%0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
59+
%1 = transform.get_parent_op %0 {isolated_from_above} : (!transform.any_op) -> !transform.any_op
60+
%2 = transform.structured.vectorize_children_and_apply_patterns %1 : (!transform.any_op) -> !transform.any_op
61+
transform.yield
62+
}
63+
}
64+
65+
3966
// -----
4067

4168
#map = affine_map<(d0, d1, d2) -> (d0, d1, d2)>

0 commit comments

Comments
 (0)