|
12 | 12 | #include "mlir/Dialect/Arith/IR/Arith.h" |
13 | 13 | #include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h" |
14 | 14 | #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" |
| 15 | +#include "mlir/Dialect/UB/IR/UBOps.h" |
15 | 16 | #include "mlir/IR/AffineExpr.h" |
16 | 17 | #include "mlir/IR/AffineMap.h" |
17 | 18 | #include "mlir/IR/Builders.h" |
@@ -445,14 +446,43 @@ struct CondBranchTruthPropagation : public OpRewritePattern<CondBranchOp> { |
445 | 446 | return success(replaced); |
446 | 447 | } |
447 | 448 | }; |
| 449 | + |
| 450 | +struct DropUnreachableCondBranch : public OpRewritePattern<CondBranchOp> { |
| 451 | + using OpRewritePattern<CondBranchOp>::OpRewritePattern; |
| 452 | + |
| 453 | + LogicalResult matchAndRewrite(CondBranchOp condbr, |
| 454 | + PatternRewriter &rewriter) const override { |
| 455 | + // If the "true" destination is unreachable, branch to the "false" |
| 456 | + // destination. |
| 457 | + Block *trueDest = condbr.getTrueDest(); |
| 458 | + Block *falseDest = condbr.getFalseDest(); |
| 459 | + if (llvm::hasSingleElement(*trueDest) && |
| 460 | + isa<ub::UnreachableOp>(trueDest->getTerminator())) { |
| 461 | + rewriter.replaceOpWithNewOp<BranchOp>(condbr, falseDest, |
| 462 | + condbr.getFalseOperands()); |
| 463 | + return success(); |
| 464 | + } |
| 465 | + |
| 466 | + // If the "false" destination is unreachable, branch to the "true" |
| 467 | + // destination. |
| 468 | + if (llvm::hasSingleElement(*falseDest) && |
| 469 | + isa<ub::UnreachableOp>(falseDest->getTerminator())) { |
| 470 | + rewriter.replaceOpWithNewOp<BranchOp>(condbr, trueDest, |
| 471 | + condbr.getTrueOperands()); |
| 472 | + return success(); |
| 473 | + } |
| 474 | + |
| 475 | + return failure(); |
| 476 | + } |
| 477 | +}; |
448 | 478 | } // namespace |
449 | 479 |
|
450 | 480 | void CondBranchOp::getCanonicalizationPatterns(RewritePatternSet &results, |
451 | 481 | MLIRContext *context) { |
452 | 482 | results.add<SimplifyConstCondBranchPred, SimplifyPassThroughCondBranch, |
453 | 483 | SimplifyCondBranchIdenticalSuccessors, |
454 | 484 | SimplifyCondBranchFromCondBranchOnSameCondition, |
455 | | - CondBranchTruthPropagation>(context); |
| 485 | + CondBranchTruthPropagation, DropUnreachableCondBranch>(context); |
456 | 486 | } |
457 | 487 |
|
458 | 488 | SuccessorOperands CondBranchOp::getSuccessorOperands(unsigned index) { |
|
0 commit comments