[Mlir-commits] [mlir] [mlir][tosa] Fix not to crash with large permutation indexes (PR #69857)
Kai Sasaki
llvmlistbot at llvm.org
Sat Oct 21 15:23:57 PDT 2023
https://github.com/Lewuathe created https://github.com/llvm/llvm-project/pull/69857
The tosa-infer-shape pass does not check the permutation validity if it’s a constant value. We need to compare the rank size with the input tensor not to crash the pass.
https://github.com/llvm/llvm-project/issues/67763
>From 3cca18b97b8e4ee0cfd9dff064571777aca61374 Mon Sep 17 00:00:00 2001
From: Kai Sasaki <lewuathe at gmail.com>
Date: Fri, 20 Oct 2023 15:30:16 +0900
Subject: [PATCH] [mlir][tosa] Fix not to crash with large permutation indexes
with tosa.transpose
---
mlir/lib/Dialect/Tosa/IR/TosaOps.cpp | 10 +++++++
mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir | 26 +++++++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index ff34183f9a030a8..537817db4abc588 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -966,6 +966,16 @@ LogicalResult tosa::TransposeOp::inferReturnTypeComponents(
if (matchPattern(adaptor.getPerms(), m_Constant(&attr)) &&
attr.getType().getRank() == 1) {
ShapeAdaptor permShape = attr;
+ // Constant permutation must be the same length as the input rank.
+ if (inputShape.getRank() != permShape.getRank())
+ return failure();
+
+ // Constant permutation values must be within the input rank.
+ for (int i = 0; i < inputShape.getRank(); i++) {
+ if (inputShape.getRank() <= permShape.getDimSize(i))
+ return failure();
+ }
+
outputShape.reserve(inputShape.getRank());
for (int i = 0, s = inputShape.getRank(); i < s; i++) {
outputShape[i] = inputShape.getDimSize(permShape.getDimSize(i));
diff --git a/mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir b/mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir
index 1ce4defcf4a6e65..2dc46a9a3b9cb73 100644
--- a/mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-infer-shapes.mlir
@@ -1272,3 +1272,29 @@ func.func @test_tosa_use_def_chain(%arg0: tensor<1x32x32x3xf32>, %arg1: tensor<1
%1 = tosa.max_pool2d %0 {kernel = array<i64: 2, 2>, pad = array<i64: 0, 0, 0, 0>, stride = array<i64: 2, 2>} : (tensor<?x32x32x16xf32>) -> tensor<?x16x16x16xf32>
return %1 : tensor<?x16x16x16xf32>
}
+
+// -----
+
+// CHECK-LABEL: test_rank_size_constant_permutation
+func.func @test_rank_size_constant_permutation() {
+ %c6 = arith.constant 6 : index
+ %cst_26 = arith.constant dense<[0, 2]> : tensor<2xi32>
+ %14 = tensor.empty(%c6) : tensor<?x27xi64>
+ // Fail to infer the shape but not crash.
+ // CHECK: (tensor<?x27xi64>, tensor<2xi32>) -> tensor<?x27xi64>
+ %72 = tosa.transpose %14, %cst_26 : (tensor<?x27xi64>, tensor<2xi32>) -> tensor<?x27xi64>
+ return
+}
+
+// -----
+
+// CHECK-LABEL: test_large_constant_permutation
+func.func @test_large_constant_permutation() {
+ %c6 = arith.constant 6 : index
+ %cst_26 = arith.constant dense<[1185677355, 332462212]> : tensor<2xi32>
+ %14 = tensor.empty(%c6) : tensor<?x27xi64>
+ // Fail to infer the shape but not crash.
+ // CHECK: (tensor<?x27xi64>, tensor<2xi32>) -> tensor<?x27xi64>
+ %72 = tosa.transpose %14, %cst_26 : (tensor<?x27xi64>, tensor<2xi32>) -> tensor<?x27xi64>
+ return
+}
\ No newline at end of file
More information about the Mlir-commits
mailing list