Skip to content

Commit 274fda2

Browse files
lhutton1Tai78641
authored andcommitted
[TOSA] Fold 'small' constant 1D slice operations
This commit extends the slice folder to fold constant slice operations consisting of all constant inputs where the number of output values does not exceed 6. Keeping the folder restricted to small inputs avoids a large folder runtime or increased memory requirements. This folder is useful in the context of legalizing dynamic models where the input shapes are resolved to static directly before legalization. In this context, constant shape operations are used over tensors of num elements <= 6 (tosa_level_8k MAX_RANK). TF_REFSPEC: refs/changes/44/946244/1 Change-Id: I1e59e5919f8c2936e98788c5a9b44a691940b28a Signed-off-by: Luke Hutton <[email protected]>
1 parent ad0c7da commit 274fda2

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,18 +1115,41 @@ OpFoldResult SliceOp::fold(FoldAdaptor adaptor) {
11151115
return SplatElementsAttr::get(outputTy, operand.getSplatValue<Attribute>());
11161116
}
11171117

1118-
if (inputTy.hasStaticShape() && outputTy.hasStaticShape() &&
1119-
outputTy.getNumElements() == 1) {
1120-
DenseElementsAttr startElems;
1121-
if (!matchPattern(getStart(), m_Constant(&startElems)))
1122-
return {};
1118+
if (!inputTy.hasStaticShape() || !outputTy.hasStaticShape())
1119+
return {};
1120+
1121+
DenseElementsAttr startElems;
1122+
if (!matchPattern(getStart(), m_Constant(&startElems)))
1123+
return {};
11231124

1124-
llvm::SmallVector<uint64_t> indices =
1125-
llvm::to_vector(startElems.getValues<uint64_t>());
1125+
llvm::SmallVector<uint64_t> indices =
1126+
llvm::to_vector(startElems.getValues<uint64_t>());
1127+
1128+
if (outputTy.getNumElements() == 1) {
11261129
auto value = operand.getValues<Attribute>()[indices];
11271130
return SplatElementsAttr::get(outputTy, value);
11281131
}
11291132

1133+
DenseElementsAttr size_elems;
1134+
if (!matchPattern(getSize(), m_Constant(&size_elems)))
1135+
return {};
1136+
const llvm::SmallVector<uint64_t> sizes =
1137+
llvm::to_vector(size_elems.getValues<uint64_t>());
1138+
1139+
// Fold slice when all operands are constant and the output is 'small'
1140+
// A 'small' output is currently defined as 1D and <= 6 elements
1141+
// (tosa_level_8k MAX_RANK)
1142+
if (inputTy.getRank() == 1 && outputTy.getRank() == 1 &&
1143+
outputTy.getNumElements() <= 6 && indices.size() == 1 &&
1144+
sizes.size() == 1) {
1145+
const auto begin = operand.value_begin<Attribute>();
1146+
const uint64_t offset = indices[0];
1147+
const uint64_t size = sizes[0];
1148+
const SmallVector<Attribute> slicedValues(begin + offset,
1149+
begin + offset + size);
1150+
return DenseElementsAttr::get(outputTy, slicedValues);
1151+
}
1152+
11301153
return {};
11311154
}
11321155

mlir/test/Dialect/Tosa/constant_folding.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ func.func @try_fold_equal_with_unranked_tensor(%arg0: tensor<4xi32>, %arg1: tens
2121
%0 = tosa.equal %arg0, %arg1 : (tensor<4xi32>, tensor<1xi32>) -> tensor<*xi1>
2222
return
2323
}
24+
25+
// -----
26+
27+
// CHECK-LABEL: test_1d_slice
28+
func.func @test_1d_slice() -> tensor<6xi32> {
29+
// CHECK: %[[VAL_0:.+]] = "tosa.const"() <{value = dense<[1, 2, 3, 4, 5, 6]> : tensor<6xi32>}> : () -> tensor<6xi32>
30+
// CHECK: return %[[VAL_0]] : tensor<6xi32>
31+
%0 = "tosa.const"() <{value = dense<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]> : tensor<10xi32>}> : () -> tensor<10xi32>
32+
%1 = tosa.const_shape {value = dense<1> : tensor<1xindex>} : () -> !tosa.shape<1>
33+
%2 = tosa.const_shape {value = dense<6> : tensor<1xindex>} : () -> !tosa.shape<1>
34+
%3 = tosa.slice %0, %1, %2 : (tensor<10xi32>, !tosa.shape<1>, !tosa.shape<1>) -> tensor<6xi32>
35+
return %3 : tensor<6xi32>
36+
}

0 commit comments

Comments
 (0)