Skip to content

Commit df55e95

Browse files
committed
Add option max-tile-fold-size to limit the output size of tosa.tiles to constant fold
Signed-off-by: Rickert, Jonas <[email protected]>
1 parent d2454a4 commit df55e95

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

mlir/include/mlir/Dialect/Tosa/Transforms/Passes.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7+
// Modifications (c) Copyright 2023-2025 Advanced Micro Devices, Inc. or its
8+
// affiliates
9+
710
//===----------------------------------------------------------------------===//
811
//
912
// This file declares the passes for the TOSA Dialect in MLIR.
@@ -35,6 +38,9 @@ def TosaLayerwiseConstantFoldPass : Pass<"tosa-layerwise-constant-fold", "func::
3538
Option<"enableTileFolding", "enable-tile-folding", "bool",
3639
/*default=*/"false",
3740
"Enables folding of tosa.tile operation. May generate bigger constant values.">,
41+
Option<"maxTileFoldSize", "max-tile-fold-size", "int",
42+
/*default=*/"0",
43+
"Maximum size in bytes of the constant to be generated when folding a tosa.tile operation. A value of 0 means no limit.">,
3844
];
3945
}
4046

mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,11 @@ DenseElementsAttr tile(DenseElementsAttr inputValues, ShapedType outputType) {
19391939
}
19401940

19411941
struct TosaFoldConstantTile : public TosaFoldConstantBase<tosa::TileOp> {
1942-
using TosaFoldConstantBase::TosaFoldConstantBase;
1942+
1943+
TosaFoldConstantTile(MLIRContext *ctxt, bool foldSplatOrSingleUseOnly,
1944+
int maxSizeToFold)
1945+
: TosaFoldConstantBase<tosa::TileOp>(ctxt, foldSplatOrSingleUseOnly),
1946+
maxSizeToFold(maxSizeToFold) {}
19431947

19441948
LogicalResult matchAndRewrite(tosa::TileOp op,
19451949
PatternRewriter &rewriter) const override {
@@ -1958,10 +1962,22 @@ struct TosaFoldConstantTile : public TosaFoldConstantBase<tosa::TileOp> {
19581962
foldSplatOrSingleUseOnly)
19591963
return failure();
19601964

1965+
assert(maxSizeToFold >= 0 && "maxSizeToFold should be non-negative");
1966+
if (maxSizeToFold > 0) {
1967+
if (!outputType.hasStaticShape())
1968+
return failure();
1969+
const int64_t numOfElements = outputType.getNumElements();
1970+
if (numOfElements * (outputType.getElementTypeBitWidth() / 8) >
1971+
static_cast<int64_t>(maxSizeToFold))
1972+
return failure();
1973+
}
1974+
19611975
rewriter.replaceOpWithNewOp<tosa::ConstOp>(op, outputType,
19621976
tile(inputValues, outputType));
19631977
return success();
19641978
}
1979+
1980+
const int maxSizeToFold;
19651981
};
19661982

19671983
/// Getting the axes position of the element which is located
@@ -2275,7 +2291,8 @@ void mlir::tosa::populateTosaFoldConstantPatterns(
22752291
patterns.add<TosaFoldConstantMatMul>(ctx, options.foldSplatOrSingleUseOnly);
22762292
patterns.add<TosaFoldConstantConcat>(ctx, options.foldSplatOrSingleUseOnly);
22772293
if (options.enableTileFolding)
2278-
patterns.add<TosaFoldConstantTile>(ctx, options.foldSplatOrSingleUseOnly);
2294+
patterns.add<TosaFoldConstantTile>(ctx, options.foldSplatOrSingleUseOnly,
2295+
options.maxTileFoldSize);
22792296
}
22802297

22812298
void mlir::tosa::populateTosaConstantReduction(MLIRContext *ctx,

mlir/lib/Dialect/Tosa/Transforms/TosaLayerwiseConstantFoldPass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
7+
// Modifications (c) Copyright 2023-2025 Advanced Micro Devices, Inc. or its
8+
// affiliates
9+
//
710
//===----------------------------------------------------------------------===//
811
//
912
// This file implements constant folding transformations on TOSA operations
@@ -57,6 +60,7 @@ struct TosaLayerwiseConstantFoldPass
5760
options.foldSplatOrSingleUseOnly = foldSplatOrSingleUseOnly;
5861
options.aggressiveReduceConstant = aggressiveReduceConstant;
5962
options.enableTileFolding = enableTileFolding;
63+
options.maxTileFoldSize = maxTileFoldSize;
6064

6165
mlir::tosa::populateTosaFoldConstantPatterns(ctx, patterns, options);
6266
mlir::tosa::populateTosaConstantReduction(ctx, patterns,

mlir/test/Dialect/Tosa/constant-tile.mlir

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: mlir-opt --split-input-file --tosa-layerwise-constant-fold=enable-tile-folding=true %s | FileCheck %s
1+
// Modifications (c) Copyright 2023-2025 Advanced Micro Devices, Inc. or its
2+
// affiliates
3+
// RUN: mlir-opt --split-input-file --tosa-layerwise-constant-fold="enable-tile-folding=true max-tile-fold-size=1024" %s | FileCheck %s
24
// RUN: mlir-opt --split-input-file --tosa-layerwise-constant-fold %s | FileCheck --check-prefix=NO-FOLDING-CHECK %s
35

46
// CHECK-LABEL: @tile_int_one_dim
@@ -12,6 +14,16 @@ func.func @tile_int_one_dim() -> (tensor<6xi32>) {
1214
// NO-FOLDING-CHECK: tosa.tile
1315
}
1416

17+
// CHECK-LABEL: @tile_int_one_dim_large
18+
func.func @tile_int_one_dim_large() -> (tensor<1500xi32>) {
19+
// CHECK: tosa.tile
20+
%0 = "tosa.const"() {value = dense<[60, 2, 3]> : tensor<3xi32>} : () -> tensor<3xi32>
21+
%cst = tosa.const_shape { value = dense<[500]> : tensor<1xindex> } : () -> !tosa.shape<1>
22+
%1 = tosa.tile %0, %cst : (tensor<3xi32>, !tosa.shape<1>) -> tensor<1500xi32>
23+
return %1 : tensor<1500xi32>
24+
// NO-FOLDING-CHECK: tosa.tile
25+
}
26+
1527
// CHECK-LABEL: @tile_bool
1628
func.func @tile_bool() -> (tensor<1x3x2x3xi1>) {
1729
// CHECK: "tosa.const"() <{value = dense

0 commit comments

Comments
 (0)