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/Inliner.h"
22#include "mlir/Transforms/InliningUtils.h"
23#include "llvm/Support/LogicalResult.h"
27#define GEN_PASS_DEF_FUNCTIONELIMINATION
28#include "circt/Dialect/LLHD/Transforms/LLHDPasses.h.inc"
37struct FunctionInliner :
public InlinerInterface {
38 FunctionInliner(MLIRContext *context) : InlinerInterface(context) {}
40 bool isLegalToInline(Region *dest, Region *src,
bool wouldBeCloned,
41 IRMapping &valueMapping)
const override {
42 return dest->getParentOfType<llhd::ProcessOp>() &&
43 isa<func::FuncOp>(src->getParentOp());
45 bool isLegalToInline(Operation *op, Region *dest,
bool wouldBeCloned,
46 IRMapping &valueMapping)
const override {
51struct FunctionEliminationPass
52 :
public circt::llhd::impl::FunctionEliminationBase<
53 FunctionEliminationPass> {
54 void runOnOperation()
override;
58void FunctionEliminationPass::runOnOperation() {
59 for (
auto module : getOperation().getOps<
hw::HWModuleOp>())
60 if (failed(runOnModule(module)))
61 return signalPassFailure();
63 getOperation().walk([&](mlir::func::FuncOp op) {
64 if (op.symbolKnownUseEmpty(getOperation()))
69LogicalResult FunctionEliminationPass::runOnModule(
hw::HWModuleOp module) {
70 FunctionInliner inliner(&getContext());
71 SymbolTableCollection table;
72 mlir::InlinerConfig config;
74 SmallVector<CallOpInterface> calls;
75 module.walk([&](func::CallOp op) { calls.push_back(op); });
77 for (
auto call : calls) {
78 auto symbol = call.getCallableForCallee().dyn_cast<SymbolRefAttr>();
80 return call.emitError(
81 "functions not referred to by symbol are not supported");
83 auto func = cast<CallableOpInterface>(
84 table.lookupNearestSymbolFrom(module, symbol.getLeafReference()));
86 if (succeeded(mlir::inlineCall(inliner, config.getCloneCallback(), call,
87 func, func.getCallableRegion()))) {
92 return call.emitError(
93 "Not all functions are inlined, there is at least "
94 "one function call left within a llhd.process or hw.module.");
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.