16 #include "mlir/Dialect/Func/IR/FuncOps.h"
17 #include "mlir/IR/SymbolTable.h"
18 #include "mlir/IR/Visitors.h"
19 #include "mlir/Interfaces/CallInterfaces.h"
20 #include "mlir/Pass/Pass.h"
21 #include "mlir/Transforms/InliningUtils.h"
22 #include "llvm/Support/LogicalResult.h"
26 #define GEN_PASS_DEF_FUNCTIONELIMINATION
27 #include "circt/Dialect/LLHD/Transforms/Passes.h.inc"
32 using namespace circt;
36 struct FunctionInliner :
public InlinerInterface {
37 FunctionInliner(MLIRContext *context) : InlinerInterface(context) {}
39 bool isLegalToInline(Region *dest, Region *src,
bool wouldBeCloned,
40 IRMapping &valueMapping)
const override {
41 return dest->getParentOfType<llhd::ProcessOp>() &&
42 isa<func::FuncOp>(src->getParentOp());
44 bool isLegalToInline(Operation *op, Region *dest,
bool wouldBeCloned,
45 IRMapping &valueMapping)
const override {
50 struct FunctionEliminationPass
51 :
public circt::llhd::impl::FunctionEliminationBase<
52 FunctionEliminationPass> {
53 void runOnOperation()
override;
57 void FunctionEliminationPass::runOnOperation() {
58 for (
auto module : getOperation().getOps<hw::HWModuleOp>())
59 if (failed(runOnModule(module)))
60 return signalPassFailure();
62 getOperation().walk([&](mlir::func::FuncOp op) {
63 if (op.symbolKnownUseEmpty(getOperation()))
68 LogicalResult FunctionEliminationPass::runOnModule(
hw::HWModuleOp module) {
69 FunctionInliner inliner(&getContext());
70 SymbolTableCollection table;
72 SmallVector<CallOpInterface> calls;
73 module.walk([&](func::CallOp op) { calls.push_back(op); });
75 for (
auto call : calls) {
76 auto symbol = call.getCallableForCallee().dyn_cast<SymbolRefAttr>();
78 return call.emitError(
79 "functions not referred to by symbol are not supported");
81 auto func = cast<CallableOpInterface>(
82 table.lookupNearestSymbolFrom(module, symbol.getLeafReference()));
85 mlir::inlineCall(inliner, call, func, func.getCallableRegion()))) {
90 return call.emitError(
91 "Not all functions are inlined, there is at least "
92 "one function call left within a llhd.process or hw.module.");
99 std::unique_ptr<OperationPass<ModuleOp>>
101 return std::make_unique<FunctionEliminationPass>();
std::unique_ptr< OperationPass< ModuleOp > > createFunctionEliminationPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.