diff --git a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td --- a/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td +++ b/mlir/include/mlir/Dialect/StandardOps/IR/Ops.td @@ -467,6 +467,8 @@ // AssertOp is fully verified by its traits. let verifier = ?; + + let hasCanonicalizer = 1; } //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp --- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp +++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp @@ -439,6 +439,31 @@ [](APInt a, APInt b) { return a & b; }); } +//===----------------------------------------------------------------------===// +// AssertOp +//===----------------------------------------------------------------------===// + +namespace { +struct EraseRedundantAssertions : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(AssertOp op, + PatternRewriter &rewriter) const override { + // Erase assertion if argument is constant true. + if (matchPattern(op.arg(), m_One())) { + rewriter.eraseOp(op); + return success(); + } + return failure(); + } +}; +} // namespace + +void AssertOp::getCanonicalizationPatterns(OwningRewritePatternList &patterns, + MLIRContext *context) { + patterns.insert(context); +} + //===----------------------------------------------------------------------===// // AssumeAlignmentOp //===----------------------------------------------------------------------===// diff --git a/mlir/test/Dialect/Standard/canonicalize-cf.mlir b/mlir/test/Dialect/Standard/canonicalize-cf.mlir --- a/mlir/test/Dialect/Standard/canonicalize-cf.mlir +++ b/mlir/test/Dialect/Standard/canonicalize-cf.mlir @@ -138,3 +138,26 @@ ^bb2: return } + +// ----- + +// Erase assertion if condition is known to be true at compile time. +// CHECK-LABEL: @assert_true +func @assert_true() { + // CHECK-NOT: assert + %true = constant true + assert %true, "Computer says no" + return +} + +// ----- + +// Keep assertion if condition unknown at compile time. +// CHECK-LABEL: @assert +// CHECK-SAME: (%[[ARG:.*]]: i1) +func @assert(%arg : i1) { + // CHECK: assert %[[ARG]], "Computer says no" + assert %arg, "Computer says no" + return +} +