MLIR 24.0.0git
SimplifyCFG.cpp
Go to the documentation of this file.
1//===- SimplifyCFG.cpp - Simplify LLVM control-flow graphs ----------------===//
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
10
14#include "mlir/Pass/Pass.h"
16
17namespace mlir {
18namespace LLVM {
19#define GEN_PASS_DEF_LLVMSIMPLIFYCFGPASS
20#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc"
21} // namespace LLVM
22} // namespace mlir
23
24using namespace mlir;
25
26namespace {
27// Running the branch patterns standalone is profitable where full
28// canonicalization is not wanted: the generic canonicalizer runs
29// every pattern and folds the whole module, which costs more than
30// merging blocks and changes operations beyond the branches. Today
31// the pass applies only the branch patterns, with folding off, so its
32// only effect is block merging, and pipelines that never run
33// canonicalization at all can still use the transform through the
34// same pattern set.
35// Merge forwarding blocks in `func` by applying the llvm.br
36// canonicalization patterns.
37static LogicalResult mergeBranchBlocks(LLVM::LLVMFuncOp func) {
38 RewritePatternSet patterns(func.getContext());
39 LLVM::BrOp::getCanonicalizationPatterns(patterns, func.getContext());
41 func.walk([&](LLVM::BrOp op) { ops.push_back(op); });
42 // Only the collected branches are rewritten, and folding is off:
43 // otherwise the driver would fold operations exposed by a merge and
44 // delete dead operand definitions, changing IR beyond block merging.
47 config.enableFolding(false);
48 return applyOpPatternsGreedily(ops, std::move(patterns), config);
49}
50
51// This pass is the home for control-flow simplifications in the LLVM
52// dialect. Simplifications should be added here.
53struct SimplifyCFGPass
54 : public LLVM::impl::LLVMSimplifyCFGPassBase<SimplifyCFGPass> {
55 void runOnOperation() override {
56 LLVM::LLVMFuncOp func = getOperation();
57 if (func.isOptimizeNone())
58 return;
59 if (failed(mergeBranchBlocks(func)))
60 signalPassFailure();
61 // Further transforms can be called here.
62 }
63};
64} // namespace
This class allows control over how the GreedyPatternRewriteDriver works.
GreedyRewriteConfig & enableFolding(bool enable=true)
GreedyRewriteConfig & setStrictness(GreedyRewriteStrictness mode)
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
Include the generated interface declarations.
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
@ ExistingOps
Only pre-existing ops are processed.