|
| 1 | +//===- CIRSimplify.cpp - performs CIR canonicalization --------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "PassDetail.h" |
| 10 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 11 | +#include "mlir/IR/Block.h" |
| 12 | +#include "mlir/IR/Operation.h" |
| 13 | +#include "mlir/IR/PatternMatch.h" |
| 14 | +#include "mlir/IR/Region.h" |
| 15 | +#include "mlir/Support/LogicalResult.h" |
| 16 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 17 | +#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 18 | +#include "clang/CIR/Dialect/Passes.h" |
| 19 | + |
| 20 | +using namespace mlir; |
| 21 | +using namespace cir; |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +/// Removes branches between two blocks if it is the only branch. |
| 26 | +/// |
| 27 | +/// From: |
| 28 | +/// ^bb0: |
| 29 | +/// cir.br ^bb1 |
| 30 | +/// ^bb1: // pred: ^bb0 |
| 31 | +/// cir.return |
| 32 | +/// |
| 33 | +/// To: |
| 34 | +/// ^bb0: |
| 35 | +/// cir.return |
| 36 | +struct RemoveRedundantBranches : public OpRewritePattern<BrOp> { |
| 37 | + using OpRewritePattern<BrOp>::OpRewritePattern; |
| 38 | + |
| 39 | + LogicalResult matchAndRewrite(BrOp op, |
| 40 | + PatternRewriter &rewriter) const final { |
| 41 | + Block *block = op.getOperation()->getBlock(); |
| 42 | + Block *dest = op.getDest(); |
| 43 | + |
| 44 | + if (isa<mlir::cir::LabelOp>(dest->front())) |
| 45 | + return failure(); |
| 46 | + |
| 47 | + // Single edge between blocks: merge it. |
| 48 | + if (block->getNumSuccessors() == 1 && |
| 49 | + dest->getSinglePredecessor() == block) { |
| 50 | + rewriter.eraseOp(op); |
| 51 | + rewriter.mergeBlocks(dest, block); |
| 52 | + return success(); |
| 53 | + } |
| 54 | + |
| 55 | + return failure(); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +struct RemoveEmptyScope : public OpRewritePattern<ScopeOp> { |
| 60 | + using OpRewritePattern<ScopeOp>::OpRewritePattern; |
| 61 | + |
| 62 | + LogicalResult match(ScopeOp op) const final { |
| 63 | + return success(op.getRegion().empty() || |
| 64 | + (op.getRegion().getBlocks().size() == 1 && |
| 65 | + op.getRegion().front().empty())); |
| 66 | + } |
| 67 | + |
| 68 | + void rewrite(ScopeOp op, PatternRewriter &rewriter) const final { |
| 69 | + rewriter.eraseOp(op); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +struct RemoveEmptySwitch : public OpRewritePattern<SwitchOp> { |
| 74 | + using OpRewritePattern<SwitchOp>::OpRewritePattern; |
| 75 | + |
| 76 | + LogicalResult match(SwitchOp op) const final { |
| 77 | + return success(op.getRegions().empty()); |
| 78 | + } |
| 79 | + |
| 80 | + void rewrite(SwitchOp op, PatternRewriter &rewriter) const final { |
| 81 | + rewriter.eraseOp(op); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +struct RemoveTrivialTry : public OpRewritePattern<TryOp> { |
| 86 | + using OpRewritePattern<TryOp>::OpRewritePattern; |
| 87 | + |
| 88 | + LogicalResult match(TryOp op) const final { |
| 89 | + // FIXME: also check all catch regions are empty |
| 90 | + // return success(op.getTryRegion().hasOneBlock()); |
| 91 | + return mlir::failure(); |
| 92 | + } |
| 93 | + |
| 94 | + void rewrite(TryOp op, PatternRewriter &rewriter) const final { |
| 95 | + // Move try body to the parent. |
| 96 | + assert(op.getTryRegion().hasOneBlock()); |
| 97 | + |
| 98 | + Block *parentBlock = op.getOperation()->getBlock(); |
| 99 | + mlir::Block *tryBody = &op.getTryRegion().getBlocks().front(); |
| 100 | + YieldOp y = dyn_cast<YieldOp>(tryBody->getTerminator()); |
| 101 | + assert(y && "expected well wrapped up try block"); |
| 102 | + y->erase(); |
| 103 | + |
| 104 | + rewriter.inlineBlockBefore(tryBody, parentBlock, Block::iterator(op)); |
| 105 | + rewriter.eraseOp(op); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +// Remove call exception with empty cleanups |
| 110 | +struct SimplifyCallOp : public OpRewritePattern<CallOp> { |
| 111 | + using OpRewritePattern<CallOp>::OpRewritePattern; |
| 112 | + |
| 113 | + LogicalResult match(CallOp op) const final { |
| 114 | + // Applicable to cir.call exception ... clean { cir.yield } |
| 115 | + mlir::Region *r = &op.getCleanup(); |
| 116 | + if (r->empty() || !r->hasOneBlock()) |
| 117 | + return failure(); |
| 118 | + |
| 119 | + mlir::Block *b = &r->getBlocks().back(); |
| 120 | + if (&b->back() != &b->front()) |
| 121 | + return failure(); |
| 122 | + |
| 123 | + return success(isa<YieldOp>(&b->getOperations().back())); |
| 124 | + } |
| 125 | + |
| 126 | + void rewrite(CallOp op, PatternRewriter &rewriter) const final { |
| 127 | + mlir::Block *b = &op.getCleanup().back(); |
| 128 | + rewriter.eraseOp(&b->back()); |
| 129 | + rewriter.eraseBlock(b); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +//===----------------------------------------------------------------------===// |
| 134 | +// CIRCanonicalizePass |
| 135 | +//===----------------------------------------------------------------------===// |
| 136 | + |
| 137 | +struct CIRCanonicalizePass : public CIRCanonicalizeBase<CIRCanonicalizePass> { |
| 138 | + using CIRCanonicalizeBase::CIRCanonicalizeBase; |
| 139 | + |
| 140 | + // The same operation rewriting done here could have been performed |
| 141 | + // by CanonicalizerPass (adding hasCanonicalizer for target Ops and |
| 142 | + // implementing the same from above in CIRDialects.cpp). However, it's |
| 143 | + // currently too aggressive for static analysis purposes, since it might |
| 144 | + // remove things where a diagnostic can be generated. |
| 145 | + // |
| 146 | + // FIXME: perhaps we can add one more mode to GreedyRewriteConfig to |
| 147 | + // disable this behavior. |
| 148 | + void runOnOperation() override; |
| 149 | +}; |
| 150 | + |
| 151 | +void populateCIRCanonicalizePatterns(RewritePatternSet &patterns) { |
| 152 | + // clang-format off |
| 153 | + patterns.add< |
| 154 | + RemoveRedundantBranches, |
| 155 | + RemoveEmptyScope, |
| 156 | + RemoveEmptySwitch, |
| 157 | + RemoveTrivialTry, |
| 158 | + SimplifyCallOp |
| 159 | + >(patterns.getContext()); |
| 160 | + // clang-format on |
| 161 | +} |
| 162 | + |
| 163 | +void CIRCanonicalizePass::runOnOperation() { |
| 164 | + // Collect rewrite patterns. |
| 165 | + RewritePatternSet patterns(&getContext()); |
| 166 | + populateCIRCanonicalizePatterns(patterns); |
| 167 | + |
| 168 | + // Collect operations to apply patterns. |
| 169 | + SmallVector<Operation *, 16> ops; |
| 170 | + getOperation()->walk([&](Operation *op) { |
| 171 | + // CastOp here is to perform a manual `fold` in |
| 172 | + // applyOpPatternsAndFold |
| 173 | + if (isa<BrOp, BrCondOp, ScopeOp, SwitchOp, CastOp, TryOp, UnaryOp, SelectOp, |
| 174 | + ComplexCreateOp, ComplexRealOp, ComplexImagOp, CallOp>(op)) |
| 175 | + ops.push_back(op); |
| 176 | + }); |
| 177 | + |
| 178 | + // Apply patterns. |
| 179 | + if (applyOpPatternsAndFold(ops, std::move(patterns)).failed()) |
| 180 | + signalPassFailure(); |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace |
| 184 | + |
| 185 | +std::unique_ptr<Pass> mlir::createCIRCanonicalizePass() { |
| 186 | + return std::make_unique<CIRCanonicalizePass>(); |
| 187 | +} |
0 commit comments