File tree 3 files changed +50
-0
lines changed
include/mlir/Dialect/StandardOps/IR
lib/Dialect/StandardOps/IR
3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -467,6 +467,8 @@ def AssertOp : Std_Op<"assert"> {
467
467
468
468
// AssertOp is fully verified by its traits.
469
469
let verifier = ?;
470
+
471
+ let hasCanonicalizer = 1;
470
472
}
471
473
472
474
//===----------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -439,6 +439,31 @@ OpFoldResult AndOp::fold(ArrayRef<Attribute> operands) {
439
439
[](APInt a, APInt b) { return a & b; });
440
440
}
441
441
442
+ // ===----------------------------------------------------------------------===//
443
+ // AssertOp
444
+ // ===----------------------------------------------------------------------===//
445
+
446
+ namespace {
447
+ struct EraseRedundantAssertions : public OpRewritePattern <AssertOp> {
448
+ using OpRewritePattern<AssertOp>::OpRewritePattern;
449
+
450
+ LogicalResult matchAndRewrite (AssertOp op,
451
+ PatternRewriter &rewriter) const override {
452
+ // Erase assertion if argument is constant true.
453
+ if (matchPattern (op.arg (), m_One ())) {
454
+ rewriter.eraseOp (op);
455
+ return success ();
456
+ }
457
+ return failure ();
458
+ }
459
+ };
460
+ } // namespace
461
+
462
+ void AssertOp::getCanonicalizationPatterns (OwningRewritePatternList &patterns,
463
+ MLIRContext *context) {
464
+ patterns.insert <EraseRedundantAssertions>(context);
465
+ }
466
+
442
467
// ===----------------------------------------------------------------------===//
443
468
// AssumeAlignmentOp
444
469
// ===----------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -138,3 +138,26 @@ func @cond_br_pass_through_fail(%cond : i1) {
138
138
^bb2 :
139
139
return
140
140
}
141
+
142
+ // -----
143
+
144
+ // Erase assertion if condition is known to be true at compile time.
145
+ // CHECK-LABEL: @assert_true
146
+ func @assert_true () {
147
+ // CHECK-NOT: assert
148
+ %true = constant true
149
+ assert %true , " Computer says no"
150
+ return
151
+ }
152
+
153
+ // -----
154
+
155
+ // Keep assertion if condition unknown at compile time.
156
+ // CHECK-LABEL: @assert
157
+ // CHECK-SAME: (%[[ARG:.*]]: i1)
158
+ func @assert (%arg : i1 ) {
159
+ // CHECK: assert %[[ARG]], "Computer says no"
160
+ assert %arg , " Computer says no"
161
+ return
162
+ }
163
+
You can’t perform that action at this time.
0 commit comments