Skip to content

Commit 485eda9

Browse files
authored
[mlir][tosa] Fix crash in slice op folder when input values are not iterable (#187339)
A crash was encountered in the slice op folder when the input was a constant with dense resource values. The folder was trying to iterate over the input values, which is not possible for resource values. This change fixes the crash and adds a test.
1 parent a261548 commit 485eda9

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,8 +1835,8 @@ OpFoldResult SliceOp::fold(FoldAdaptor adaptor) {
18351835
outputTy.getNumElements() == 1) {
18361836
llvm::SmallVector<uint64_t> indices =
18371837
llvm::to_vector(startElems.getValues<uint64_t>());
1838-
auto value = operand.getValues<Attribute>()[indices];
1839-
return SplatElementsAttr::get(outputTy, value);
1838+
if (auto values = operand.tryGetValues<Attribute>())
1839+
return SplatElementsAttr::get(outputTy, (*values)[indices]);
18401840
}
18411841

18421842
return {};

mlir/test/Dialect/Tosa/constant_folding.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,26 @@ func.func @slice_singleton() -> tensor<1x1xi32> {
820820

821821
// -----
822822

823+
// CHECK-LABEL: @test_slice_resource_no_fold
824+
func.func @test_slice_resource_no_fold() -> tensor<1x1xi32> {
825+
%input = "tosa.const"() {values = dense_resource<slice_resource> : tensor<3x4xi32>} : () -> tensor<3x4xi32>
826+
%start = tosa.const_shape {values = dense<[1, 2]> : tensor<2xindex>} : () -> !tosa.shape<2>
827+
%size = tosa.const_shape {values = dense<[1, 1]> : tensor<2xindex>} : () -> !tosa.shape<2>
828+
// CHECK: tosa.slice
829+
%slice= tosa.slice %input, %start, %size : (tensor<3x4xi32>, !tosa.shape<2>, !tosa.shape<2>) -> tensor<1x1xi32>
830+
return %slice : tensor<1x1xi32>
831+
}
832+
833+
{-#
834+
dialect_resources: {
835+
builtin: {
836+
slice_resource: "0x040000000700000000000000000000000000000000000000000000000900000000000000000000000000000000000000"
837+
}
838+
}
839+
#-}
840+
841+
// -----
842+
823843
// CHECK: func.func @cast_float_to_float
824844
func.func @cast_float_to_float() -> tensor<f16> {
825845
%splat = "tosa.const"() {values = dense<42.0> : tensor<f32>} : () -> tensor<f32>

0 commit comments

Comments
 (0)