From 6375a8508e836a49ffcee306b57166a39a950afe Mon Sep 17 00:00:00 2001 From: Shay Kleiman <42376404+shay-kl@users.noreply.github.com> Date: Thu, 22 May 2025 17:08:05 +0300 Subject: [PATCH] [mlir][tosa] Fix indexing in TosaToTensor (#140906) Changed the indexing used in the extractOp from one that is intended for 0d tensors to one that is intended for 1d tensors. --------- Co-authored-by: Shay Kleiman --- .../Conversion/TosaToTensor/TosaToTensor.cpp | 3 ++- .../TosaToTensor/tosa-to-tensor.mlir | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp b/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp index 5f23a33049f8..615c7ca1cfd1 100644 --- a/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp +++ b/mlir/lib/Conversion/TosaToTensor/TosaToTensor.cpp @@ -362,7 +362,8 @@ public: // Setup the default constantAttr. Value padConstant = rewriter.createOrFold( - loc, padOp.getPadConst(), ValueRange({})); + loc, padOp.getPadConst(), + ValueRange({rewriter.create(loc, 0)})); if (!padConstant) { return rewriter.notifyMatchFailure( diff --git a/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir b/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir index b19cd2115891..0a276e2a5c3d 100644 --- a/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir +++ b/mlir/test/Conversion/TosaToTensor/tosa-to-tensor.mlir @@ -700,3 +700,25 @@ func.func @concat_non_axis_dyn_mixed(%arg0: tensor, %arg1: tensor : (tensor, tensor, tensor) -> tensor<5x3xf32> return } + +// ----- + +// CHECK-LABEL: func @pad_variable_pad_const +// CHECK-SAME: (%[[ARG0_SSA:.*]]: tensor<2x2xi32>, %[[PAD_INPUT_TENSOR_SSA:.*]]: tensor<1xi32>) +func.func @pad_variable_pad_const(%arg0: tensor<2x2xi32>, %pad_input_tensor: tensor<1xi32>) -> tensor<4x5xi32> { + // CHECK-DAG: %[[C0_INDEX:.*]] = arith.constant 0 : index + // CHECK-DAG: %[[EXTRACTED_PAD_VAL:.*]] = tensor.extract %[[PAD_INPUT_TENSOR_SSA]][%[[C0_INDEX]]] : tensor<1xi32> + + // CHECK: %[[C_PAD_LOW_0:.*]] = arith.constant 1 : index + // CHECK: %[[C_PAD_HIGH_0:.*]] = arith.constant 1 : index + // CHECK: %[[C_PAD_LOW_1:.*]] = arith.constant 0 : index + // CHECK: %[[C_PAD_HIGH_1:.*]] = arith.constant 3 : index + + // CHECK: %{{.*}} = tensor.pad %[[ARG0_SSA]] low[%[[C_PAD_LOW_0]], %[[C_PAD_LOW_1]]] high[%[[C_PAD_HIGH_0]], %[[C_PAD_HIGH_1]]] { + // CHECK: tensor.yield %[[EXTRACTED_PAD_VAL]] : i32 + // CHECK: } : tensor<2x2xi32> to tensor<4x5xi32> + + %padding_indices = tosa.const_shape {values = dense<[1, 1, 0, 3]> : tensor<4xindex>} : () -> !tosa.shape<4> + %result = "tosa.pad"(%arg0, %padding_indices, %pad_input_tensor) : (tensor<2x2xi32>, !tosa.shape<4>, tensor<1xi32>) -> tensor<4x5xi32> + return %result : tensor<4x5xi32> +}