|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +// This file implements pass that inlines CIR operations regions into the parent |
| 10 | +// function region. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "PassDetail.h" |
| 15 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 16 | +#include "mlir/IR/PatternMatch.h" |
| 17 | +#include "mlir/Support/LogicalResult.h" |
| 18 | +#include "mlir/Transforms/DialectConversion.h" |
| 19 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 20 | +#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 21 | +#include "clang/CIR/Dialect/Passes.h" |
| 22 | +#include "clang/CIR/MissingFeatures.h" |
| 23 | + |
| 24 | +using namespace mlir; |
| 25 | +using namespace cir; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +struct CIRFlattenCFGPass : public CIRFlattenCFGBase<CIRFlattenCFGPass> { |
| 30 | + |
| 31 | + CIRFlattenCFGPass() = default; |
| 32 | + void runOnOperation() override; |
| 33 | +}; |
| 34 | + |
| 35 | +class CIRScopeOpFlattening : public mlir::OpRewritePattern<cir::ScopeOp> { |
| 36 | +public: |
| 37 | + using OpRewritePattern<cir::ScopeOp>::OpRewritePattern; |
| 38 | + |
| 39 | + mlir::LogicalResult |
| 40 | + matchAndRewrite(cir::ScopeOp scopeOp, |
| 41 | + mlir::PatternRewriter &rewriter) const override { |
| 42 | + mlir::OpBuilder::InsertionGuard guard(rewriter); |
| 43 | + mlir::Location loc = scopeOp.getLoc(); |
| 44 | + |
| 45 | + // Empty scope: just remove it. |
| 46 | + // TODO: Remove this logic once CIR uses MLIR infrastructure to remove |
| 47 | + // trivially dead operations. MLIR canonicalizer is too aggressive and we |
| 48 | + // need to either (a) make sure all our ops model all side-effects and/or |
| 49 | + // (b) have more options in the canonicalizer in MLIR to temper |
| 50 | + // aggressiveness level. |
| 51 | + if (scopeOp.isEmpty()) { |
| 52 | + rewriter.eraseOp(scopeOp); |
| 53 | + return mlir::success(); |
| 54 | + } |
| 55 | + |
| 56 | + // Split the current block before the ScopeOp to create the inlining |
| 57 | + // point. |
| 58 | + mlir::Block *currentBlock = rewriter.getInsertionBlock(); |
| 59 | + mlir::Block *continueBlock = |
| 60 | + rewriter.splitBlock(currentBlock, rewriter.getInsertionPoint()); |
| 61 | + if (scopeOp.getNumResults() > 0) |
| 62 | + continueBlock->addArguments(scopeOp.getResultTypes(), loc); |
| 63 | + |
| 64 | + // Inline body region. |
| 65 | + mlir::Block *beforeBody = &scopeOp.getScopeRegion().front(); |
| 66 | + mlir::Block *afterBody = &scopeOp.getScopeRegion().back(); |
| 67 | + rewriter.inlineRegionBefore(scopeOp.getScopeRegion(), continueBlock); |
| 68 | + |
| 69 | + // Save stack and then branch into the body of the region. |
| 70 | + rewriter.setInsertionPointToEnd(currentBlock); |
| 71 | + assert(!cir::MissingFeatures::stackSaveOp()); |
| 72 | + rewriter.create<cir::BrOp>(loc, mlir::ValueRange(), beforeBody); |
| 73 | + |
| 74 | + // Replace the scopeop return with a branch that jumps out of the body. |
| 75 | + // Stack restore before leaving the body region. |
| 76 | + rewriter.setInsertionPointToEnd(afterBody); |
| 77 | + if (auto yieldOp = dyn_cast<cir::YieldOp>(afterBody->getTerminator())) { |
| 78 | + rewriter.replaceOpWithNewOp<cir::BrOp>(yieldOp, yieldOp.getArgs(), |
| 79 | + continueBlock); |
| 80 | + } |
| 81 | + |
| 82 | + // Replace the op with values return from the body region. |
| 83 | + rewriter.replaceOp(scopeOp, continueBlock->getArguments()); |
| 84 | + |
| 85 | + return mlir::success(); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +void populateFlattenCFGPatterns(RewritePatternSet &patterns) { |
| 90 | + patterns.add<CIRScopeOpFlattening>(patterns.getContext()); |
| 91 | +} |
| 92 | + |
| 93 | +void CIRFlattenCFGPass::runOnOperation() { |
| 94 | + RewritePatternSet patterns(&getContext()); |
| 95 | + populateFlattenCFGPatterns(patterns); |
| 96 | + |
| 97 | + // Collect operations to apply patterns. |
| 98 | + llvm::SmallVector<Operation *, 16> ops; |
| 99 | + getOperation()->walk<mlir::WalkOrder::PostOrder>([&](Operation *op) { |
| 100 | + if (isa<ScopeOp>(op)) |
| 101 | + ops.push_back(op); |
| 102 | + }); |
| 103 | + |
| 104 | + // Apply patterns. |
| 105 | + if (applyOpPatternsGreedily(ops, std::move(patterns)).failed()) |
| 106 | + signalPassFailure(); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace |
| 110 | + |
| 111 | +namespace mlir { |
| 112 | + |
| 113 | +std::unique_ptr<Pass> createCIRFlattenCFGPass() { |
| 114 | + return std::make_unique<CIRFlattenCFGPass>(); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace mlir |
0 commit comments