26#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
27#include <mlir/Transforms/InliningUtils.h>
34template <
typename InlinerImpl,
typename DialectImpl,
typename... RequiredDialects>
37struct BaseInlinerInterface :
public DialectInlinerInterface {
39 using DialectInlinerInterface::DialectInlinerInterface;
42 static void registrationHook(MLIRContext *ctx, DialectImpl *dialect) {
43 dialect->template addInterfaces<InlinerImpl>();
44 if constexpr (
sizeof...(RequiredDialects) != 0) {
45 ctx->loadDialect<RequiredDialects...>();
51struct FuncInlinerInterface
52 :
public BaseInlinerInterface<
53 FuncInlinerInterface, function::FunctionDialect, cf::ControlFlowDialect> {
54 using BaseInlinerInterface::BaseInlinerInterface;
57 bool isLegalToInline(Operation *, Operation *,
bool)
const final {
return true; }
58 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
return true; }
59 bool isLegalToInline(Region *, Region *,
bool, IRMapping &)
const final {
return true; }
61 void handleTerminator(Operation *op, Block *newDest)
const final {
67 if (
auto returnOp = llvm::dyn_cast<function::ReturnOp>(op)) {
68 OpBuilder builder(op);
69 builder.create<cf::BranchOp>(op->getLoc(), newDest, returnOp.getOperands());
74 void handleTerminator(Operation *op, ValueRange valuesToRepl)
const final {
76 assert(llvm::isa<function::ReturnOp>(op));
79 auto returnOp = llvm::cast<function::ReturnOp>(op);
80 assert(returnOp.getNumOperands() == valuesToRepl.size());
81 for (
const auto &it : llvm::enumerate(returnOp.getOperands())) {
82 valuesToRepl[it.index()].replaceAllUsesWith(it.value());
87template <
typename DialectImpl>
88struct FullyLegalForInlining
89 :
public BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl> {
90 using BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl>::BaseInlinerInterface;
92 bool isLegalToInline(Operation *, Operation *,
bool)
const override {
return true; }
93 bool isLegalToInline(Region *, Region *,
bool, IRMapping &)
const override {
return true; }
94 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const override {
return true; }
102 registry.addExtension(FuncInlinerInterface::registrationHook);
103 registry.addExtension(FullyLegalForInlining<component::StructDialect>::registrationHook);
104 registry.addExtension(FullyLegalForInlining<constrain::ConstrainDialect>::registrationHook);
105 registry.addExtension(FullyLegalForInlining<string::StringDialect>::registrationHook);
106 registry.addExtension(FullyLegalForInlining<polymorphic::PolymorphicDialect>::registrationHook);
107 registry.addExtension(FullyLegalForInlining<ram::RAMDialect>::registrationHook);
108 registry.addExtension(FullyLegalForInlining<felt::FeltDialect>::registrationHook);
109 registry.addExtension(FullyLegalForInlining<global::GlobalDialect>::registrationHook);
110 registry.addExtension(FullyLegalForInlining<boolean::BoolDialect>::registrationHook);
111 registry.addExtension(FullyLegalForInlining<array::ArrayDialect>::registrationHook);
112 registry.addExtension(FullyLegalForInlining<cast::CastDialect>::registrationHook);
113 registry.addExtension(FullyLegalForInlining<include::IncludeDialect>::registrationHook);
114 registry.addExtension(FullyLegalForInlining<llzk::LLZKDialect>::registrationHook);
115 registry.addExtension(FullyLegalForInlining<pod::PODDialect>::registrationHook);
void registerInliningExtensions(DialectRegistry ®istry)