Skip to content

Commit 543351b

Browse files
authored
[tosa] : Re-enable PR #135429 with ASAN fix (#135560)
Removed the calls to `sizeOp` after replacing `SliceOp`: ``` // Remove const_shape size op when it no longer has use point. Operation *sizeConstShape = sliceOp.getSize().getDefiningOp(); ``` Turns out as part of canonicalization, trivially dead ops are removed anyway, so the above piece of code isn't actually needed.
1 parent 91a2056 commit 543351b

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,56 @@ struct ConcatSliceOptimization : public OpRewritePattern<tosa::SliceOp> {
731731
}
732732
};
733733

734+
// Update size operand of tosa.slice if size has dynamic dims but corresponding
735+
// output dim is static
736+
struct SliceDynamicSizeCanonicalization
737+
: public OpRewritePattern<tosa::SliceOp> {
738+
using OpRewritePattern<tosa::SliceOp>::OpRewritePattern;
739+
740+
LogicalResult matchAndRewrite(tosa::SliceOp sliceOp,
741+
PatternRewriter &rewriter) const override {
742+
ShapedType resultType = cast<ShapedType>(sliceOp.getType());
743+
744+
ElementsAttr sizeElems;
745+
if (!matchPattern(sliceOp.getSize(), m_Constant(&sizeElems))) {
746+
return rewriter.notifyMatchFailure(
747+
sliceOp, "size of slice must be a static ranked shape");
748+
}
749+
750+
llvm::SmallVector<int64_t> sliceSizes =
751+
llvm::to_vector(sizeElems.getValues<int64_t>());
752+
753+
bool replaceSliceSize{false};
754+
// if size op has -1 indicating dynamic shape but corresponding dim on the
755+
// output is statically known, update size to match with known output dim
756+
// shape
757+
for (const auto &[index, size] : llvm::enumerate(sliceSizes)) {
758+
if (size == -1 && !resultType.isDynamicDim(index)) {
759+
sliceSizes[index] = resultType.getDimSize(index);
760+
replaceSliceSize = true;
761+
}
762+
}
763+
764+
if (!replaceSliceSize) {
765+
return rewriter.notifyMatchFailure(
766+
sliceOp, "no dimension of size of slice is dynamic that resolves "
767+
"to static output shape");
768+
}
769+
770+
auto size_op = getTosaConstShape(rewriter, sliceOp.getLoc(), sliceSizes);
771+
auto newSliceOp = rewriter.create<tosa::SliceOp>(
772+
sliceOp.getLoc(), sliceOp.getType(), sliceOp.getInput1(),
773+
sliceOp.getStart(), size_op);
774+
775+
rewriter.replaceOp(sliceOp, newSliceOp.getResult());
776+
return success();
777+
}
778+
};
779+
734780
void SliceOp::getCanonicalizationPatterns(RewritePatternSet &results,
735781
MLIRContext *context) {
736-
results.add<ConcatSliceOptimization>(context);
782+
results.add<ConcatSliceOptimization, SliceDynamicSizeCanonicalization>(
783+
context);
737784
}
738785

739786
//===----------------------------------------------------------------------===//

mlir/test/Dialect/Tosa/canonicalize.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,3 +1212,18 @@ func.func @do_not_fold_intdiv_division_by_0() -> tensor<1x24x2xi32> {
12121212
%16 = tosa.intdiv %4, %1 : (tensor<1x24x2xi32>, tensor<1x24x2xi32>) -> tensor<1x24x2xi32>
12131213
return %16 : tensor<1x24x2xi32>
12141214
}
1215+
1216+
1217+
// -----
1218+
// CHECK-LABEL: func.func @slice_dynamic_size_static_output_canonicalize(
1219+
// CHECK-SAME: %[[ARG0:.*]]: tensor<2x60x59x?xf32>) -> tensor<2x60x58x?xf32> {
1220+
// CHECK: %[[START:.*]] = tosa.const_shape {values = dense<0> : tensor<4xindex>} : () -> !tosa.shape<4>
1221+
// CHECK: %[[SIZE:.*]] = tosa.const_shape {values = dense<[2, 60, 58, -1]> : tensor<4xindex>} : () -> !tosa.shape<4>
1222+
// CHECK: %[[SLICE:.*]] = tosa.slice %[[ARG0]], %[[START]], %[[SIZE]] : (tensor<2x60x59x?xf32>, !tosa.shape<4>, !tosa.shape<4>) -> tensor<2x60x58x?xf32>
1223+
// CHECK: return %[[SLICE]]
1224+
func.func @slice_dynamic_size_static_output_canonicalize(%arg0: tensor<2x60x59x?xf32>) -> tensor<2x60x58x?xf32> {
1225+
%0 = tosa.const_shape {values = dense<0> : tensor<4xindex>} : () -> !tosa.shape<4>
1226+
%1 = tosa.const_shape {values = dense<[-1, 60, 58, -1]> : tensor<4xindex>} : () -> !tosa.shape<4>
1227+
%2 = tosa.slice %arg0, %0, %1 : (tensor<2x60x59x?xf32>, !tosa.shape<4>, !tosa.shape<4>) -> tensor<2x60x58x?xf32>
1228+
return %2 : tensor<2x60x58x?xf32>
1229+
}

0 commit comments

Comments
 (0)