|
| 1 | +//===- ExtractTopFunc.cc ----------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file implements a pass that extract the specified top function out of |
| 4 | +// the given module. |
| 5 | +// |
| 6 | +//===----------------------------------------------------------------------===// |
| 7 | + |
| 8 | +#include "phism/mlir/Transforms/PhismTransforms.h" |
| 9 | + |
| 10 | +#include "mlir/Analysis/AffineAnalysis.h" |
| 11 | +#include "mlir/Analysis/AffineStructures.h" |
| 12 | +#include "mlir/Analysis/SliceAnalysis.h" |
| 13 | +#include "mlir/Dialect/Affine/IR/AffineOps.h" |
| 14 | +#include "mlir/IR/BlockAndValueMapping.h" |
| 15 | +#include "mlir/IR/Builders.h" |
| 16 | +#include "mlir/IR/Dominance.h" |
| 17 | +#include "mlir/IR/OpImplementation.h" |
| 18 | +#include "mlir/IR/PatternMatch.h" |
| 19 | +#include "mlir/IR/Types.h" |
| 20 | +#include "mlir/IR/Value.h" |
| 21 | +#include "mlir/Pass/Pass.h" |
| 22 | +#include "mlir/Transforms/DialectConversion.h" |
| 23 | +#include "mlir/Transforms/Passes.h" |
| 24 | +#include "mlir/Transforms/RegionUtils.h" |
| 25 | +#include "mlir/Transforms/Utils.h" |
| 26 | + |
| 27 | +#include "llvm/ADT/SetVector.h" |
| 28 | + |
| 29 | +using namespace mlir; |
| 30 | +using namespace llvm; |
| 31 | +using namespace phism; |
| 32 | + |
| 33 | +/// Union the given function and all the others it calls into 'keep'. |
| 34 | +static void unionCallee(FuncOp f, ModuleOp m, |
| 35 | + SmallPtrSetImpl<Operation *> &keep) { |
| 36 | + std::deque<Operation *> worklist; |
| 37 | + worklist.push_back(f); |
| 38 | + keep.insert(f); |
| 39 | + |
| 40 | + // A breadth first search on the call graph, starting from f. |
| 41 | + while (!worklist.empty()) { |
| 42 | + Operation *g = worklist.front(); |
| 43 | + worklist.pop_front(); |
| 44 | + |
| 45 | + g->walk([&](Operation *op) { |
| 46 | + if (CallOp callOp = dyn_cast<CallOp>(op)) { |
| 47 | + FuncOp callee = |
| 48 | + dyn_cast_or_null<FuncOp>(m.lookupSymbol(callOp.getCallee())); |
| 49 | + assert(callee && "A callee cannot be resolved in the module."); |
| 50 | + |
| 51 | + if (!keep.contains(callee)) { |
| 52 | + worklist.push_back(callee); |
| 53 | + keep.insert(callee); |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +namespace { |
| 61 | + |
| 62 | +/// Extract the specified top function out of its module. |
| 63 | +struct ExtractTopFuncPass |
| 64 | + : public PassWrapper<ExtractTopFuncPass, OperationPass<ModuleOp>> { |
| 65 | + |
| 66 | + ExtractTopFuncPass() = default; |
| 67 | + ExtractTopFuncPass(const ExtractTopFuncPass &pass) {} |
| 68 | + |
| 69 | + Option<std::string> topFuncName{ |
| 70 | + *this, "name", |
| 71 | + llvm::cl::desc("Name of the top function to be extracted.")}; |
| 72 | + |
| 73 | + void runOnOperation() override { |
| 74 | + ModuleOp m = getOperation(); |
| 75 | + OpBuilder b(m.getContext()); |
| 76 | + |
| 77 | + assert(!topFuncName.empty() && "-name should be specified."); |
| 78 | + |
| 79 | + FuncOp f = dyn_cast_or_null<FuncOp>(m.lookupSymbol(topFuncName)); |
| 80 | + assert(f && "Given name cannot be found in the module as a FuncOp."); |
| 81 | + |
| 82 | + SmallPtrSet<Operation *, 4> keep; |
| 83 | + unionCallee(f, m, keep); |
| 84 | + |
| 85 | + m.walk([&](FuncOp g) { |
| 86 | + if (!keep.contains(g)) |
| 87 | + g.erase(); |
| 88 | + }); |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace |
| 93 | + |
| 94 | +void phism::registerExtractTopFuncPass() { |
| 95 | + PassRegistration<ExtractTopFuncPass>( |
| 96 | + "extract-top-func", "Extract the top function out of its module."); |
| 97 | +} |
0 commit comments