[Mlir-commits] [mlir] [mlir][Transforms] Add missing check in applyPermutation (PR #102099)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Aug 5 21:33:04 PDT 2024


https://github.com/DarshanRamakant created https://github.com/llvm/llvm-project/pull/102099

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.

>From b56c3834ad6a1a5c0f4f37edaadff07ea08a7d74 Mon Sep 17 00:00:00 2001
From: Darshan Bhat <darshanbhatsirsi at gmail.com>
Date: Mon, 5 Aug 2024 20:12:32 +0530
Subject: [PATCH] [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.
---
 mlir/include/mlir/Dialect/Utils/IndexingUtils.h        |  2 ++
 .../TosaToLinalg/tosa-to-linalg-invalidperm.mlir       | 10 ++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalidperm.mlir

diff --git a/mlir/include/mlir/Dialect/Utils/IndexingUtils.h b/mlir/include/mlir/Dialect/Utils/IndexingUtils.h
index 7849782e5442b..9f3f334d13c89 100644
--- a/mlir/include/mlir/Dialect/Utils/IndexingUtils.h
+++ b/mlir/include/mlir/Dialect/Utils/IndexingUtils.h
@@ -202,6 +202,8 @@ SmallVector<T> applyPermutation(ArrayRef<T> input,
                                 ArrayRef<int64_t> permutation) {
   assert(input.size() == permutation.size() &&
          "expected input rank to equal permutation rank");
+  assert(llvm::all_of(permutation, [&](size_t s) {return s < input.size();}) &&
+          "permutation must be within input bounds");
   auto permutationRange = llvm::map_range(
       llvm::seq<unsigned>(0, input.size()),
       [&](int64_t idx) -> T { return input[permutation[idx]]; });
diff --git a/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalidperm.mlir b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalidperm.mlir
new file mode 100644
index 0000000000000..85378e55cad77
--- /dev/null
+++ b/mlir/test/Conversion/TosaToLinalg/tosa-to-linalg-invalidperm.mlir
@@ -0,0 +1,10 @@
+// RUN: not --crash mlir-opt %s --pass-pipeline="builtin.module(func.func(tosa-to-linalg-named))" 2>&1 | FileCheck -check-prefix=CHECK %s
+
+func.func @func1() {
+	%arg0 = tensor.empty() : tensor<3x4x5xi32>
+	%1110 = arith.constant dense<[3, 0, 1]> : tensor<3xi32>
+	%143 = tosa.transpose %arg0, %1110: (tensor<3x4x5xi32>, tensor<3xi32>) -> tensor<3x4x5xi32>
+	return 
+}
+
+// CHECK: permutation must be within input bounds



More information about the Mlir-commits mailing list