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