Skip to content

Commit 23bebca

Browse files
authored
Merge pull request #189 from Xilinx/christopher.fix_clamp_canonicalization_loc
fix(TosaCanonicalize): create FusedLoc when folding clamp
2 parents 3d27455 + 07c7f9c commit 23bebca

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

mlir/include/mlir/IR/PatternMatch.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,24 @@ class RewriterBase : public OpBuilder {
532532
virtual void replaceOp(Operation *op, Operation *newOp);
533533

534534
/// Replaces the result op with a new op that is created without verification.
535+
/// Use a given list of locations to generate a FusedLoc for the new op.
535536
/// The result values of the two ops must be the same types.
536537
template <typename OpTy, typename... Args>
537-
OpTy replaceOpWithNewOp(Operation *op, Args &&...args) {
538-
auto newOp = create<OpTy>(op->getLoc(), std::forward<Args>(args)...);
538+
OpTy replaceOpWithNewOp(Operation *op, ArrayRef<Location> locs,
539+
Args &&...args) {
540+
auto newOp = create<OpTy>(getFusedLoc(locs), std::forward<Args>(args)...);
539541
replaceOp(op, newOp.getOperation());
540542
return newOp;
541543
}
542544

545+
/// Replaces the result op with a new op that is created without verification.
546+
/// The result values of the two ops must be the same types.
547+
template <typename OpTy, typename... Args>
548+
OpTy replaceOpWithNewOp(Operation *op, Args &&...args) {
549+
return replaceOpWithNewOp<OpTy, Args...>(op, {op->getLoc()},
550+
std::forward<Args>(args)...);
551+
}
552+
543553
/// This method erases an operation that is known to have no uses.
544554
virtual void eraseOp(Operation *op);
545555

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ struct ClampClampOptimization : public OpRewritePattern<tosa::ClampOp> {
404404
auto minInt = std::max(op.getMinInt(), clampOp.getMinInt());
405405
auto maxInt = std::min(op.getMaxInt(), clampOp.getMaxInt());
406406

407-
rewriter.replaceOpWithNewOp<tosa::ClampOp>(
408-
op, op.getType(), clampOp.getInput(),
409-
rewriter.getI64IntegerAttr(minInt),
407+
rewriter.replaceOpWithNewOp<ClampOp>(
408+
op, {op->getLoc(), clampOp->getLoc()}, op.getType(),
409+
clampOp.getInput(), rewriter.getI64IntegerAttr(minInt),
410410
rewriter.getI64IntegerAttr(maxInt), rewriter.getF32FloatAttr(minFp),
411411
rewriter.getF32FloatAttr(maxFp));
412412
return success();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: mlir-opt -mlir-print-debuginfo -canonicalize="test-convergence" %s | FileCheck %s
2+
3+
// CHECK-LABEL: @clamp_twice_is_single_clamp
4+
func.func @clamp_twice_is_single_clamp(%arg0: tensor<4xi8>) -> tensor<4xi8> {
5+
// CHECK: tosa.clamp %arg0 {max_fp = 3.000000e+00 : f32, max_int = 2 : i64, min_fp = -3.000000e+00 : f32, min_int = -2 : i64} {{.*}} loc(#[[FUSED:.*]])
6+
// CHECK-DAG: #[[A:.*]] = loc("Clamp_A")
7+
// CHECK-DAG: #[[B:.*]] = loc("Clamp_B")
8+
// CHECK: #[[FUSED]] = loc(fused[#[[B]], #[[A]]])
9+
%0 = tosa.clamp %arg0 {max_fp = 3.0 : f32, max_int = 4 : i64, min_fp = -5.0 : f32, min_int = -2 : i64} : (tensor<4xi8>) -> tensor<4xi8> loc(#loc0)
10+
%1 = tosa.clamp %0 {max_fp = 5.0 : f32, max_int = 2 : i64, min_fp = -3.0 : f32, min_int = -4 : i64} : (tensor<4xi8>) -> tensor<4xi8> loc(#loc1)
11+
return %1 : tensor<4xi8>
12+
}
13+
#loc0 = loc("Clamp_A")
14+
#loc1 = loc("Clamp_B")

0 commit comments

Comments
 (0)