Skip to content

Commit 2c6c57f

Browse files
[mlir][Transforms] Add missing check in applyPermutation
The applyPermutation() utility should make sure that the permutation numbers are within the size of the input array. Otherwise it will cause a cryptic array out of bound assertion later.
1 parent b64ec3c commit 2c6c57f

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

mlir/include/mlir/Dialect/Utils/IndexingUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ SmallVector<T> applyPermutation(ArrayRef<T> input,
202202
ArrayRef<int64_t> permutation) {
203203
assert(input.size() == permutation.size() &&
204204
"expected input rank to equal permutation rank");
205+
assert(
206+
llvm::all_of(permutation, [&](size_t s) { return s < input.size(); }) &&
207+
"permutation must be within input bounds");
205208
auto permutationRange = llvm::map_range(
206209
llvm::seq<unsigned>(0, input.size()),
207210
[&](int64_t idx) -> T { return input[permutation[idx]]; });

mlir/lib/Dialect/Tosa/IR/TosaOps.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,12 @@ LogicalResult tosa::TransposeOp::verify() {
11161116
"Unexpectedly found permutation tensor without rank");
11171117
if (!isPermutationVector(constantPerms))
11181118
return emitOpError() << "expected valid permutation tensor";
1119+
1120+
if (inputType.hasRank() && (!inputType.getNumDynamicDims()) &&
1121+
!llvm::all_of(constantPerms,
1122+
[&](int64_t s) { return s < inputType.getRank(); })) {
1123+
return emitOpError() << "permutation must be within input bounds";
1124+
}
11191125
}
11201126
return success();
11211127
}

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalid.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ func.func @rfft2d_with_non_float_type(%arg0 : tensor<1x1x1xi32>) -> (tensor<1x1x
3636
%real, %imag = tosa.rfft2d %arg0 : (tensor<1x1x1xi32>) -> (tensor<1x1x1xi32>, tensor<1x1x1xi32>)
3737
return %real, %imag : tensor<1x1x1xi32>, tensor<1x1x1xi32>
3838
}
39+
40+
// -----
41+
42+
// CHECK-LABEL: @test_invalid_constant_permutation
43+
func.func @test_invalid_constant_permutation() {
44+
// expected-error@+3 {{permutation must be within input bounds}}
45+
%14 = tensor.empty() : tensor<3x4x5xi32>
46+
%c1 = arith.constant dense<[3, 0, 1]> : tensor<3xi32>
47+
%72 = tosa.transpose %14, %c1 : (tensor<3x4x5xi32>, tensor<3xi32>) -> tensor<3x4x5xi32>
48+
return
49+
}

0 commit comments

Comments
 (0)