25#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
26#include <mlir/Transforms/InliningUtils.h>
33template <
typename InlinerImpl,
typename DialectImpl,
typename... RequiredDialects>
34struct BaseInlinerInterface :
public DialectInlinerInterface {
35 using DialectInlinerInterface::DialectInlinerInterface;
37 static void registrationHook(MLIRContext *ctx, DialectImpl *dialect) {
38 dialect->template addInterfaces<InlinerImpl>();
39 if constexpr (
sizeof...(RequiredDialects) != 0) {
40 ctx->loadDialect<RequiredDialects...>();
46struct FuncInlinerInterface
47 :
public BaseInlinerInterface<
48 FuncInlinerInterface, function::FunctionDialect, cf::ControlFlowDialect> {
49 using BaseInlinerInterface::BaseInlinerInterface;
52 bool isLegalToInline(Operation *, Operation *,
bool)
const final {
return true; }
53 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
return true; }
54 bool isLegalToInline(Region *, Region *,
bool, IRMapping &)
const final {
return true; }
56 void handleTerminator(Operation *op, Block *newDest)
const final {
62 if (
auto returnOp = llvm::dyn_cast<function::ReturnOp>(op)) {
63 OpBuilder builder(op);
64 builder.create<cf::BranchOp>(op->getLoc(), newDest, returnOp.getOperands());
69 void handleTerminator(Operation *op, ValueRange valuesToRepl)
const final {
71 assert(llvm::isa<function::ReturnOp>(op));
74 auto returnOp = llvm::cast<function::ReturnOp>(op);
75 assert(returnOp.getNumOperands() == valuesToRepl.size());
76 for (
const auto &it : llvm::enumerate(returnOp.getOperands())) {
77 valuesToRepl[it.index()].replaceAllUsesWith(it.value());
82template <
typename DialectImpl>
83struct FullyLegalForInlining
84 :
public BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl> {
85 using BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl>::BaseInlinerInterface;
87 bool isLegalToInline(Operation *, Operation *,
bool)
const override {
return true; }
88 bool isLegalToInline(Region *, Region *,
bool, IRMapping &)
const override {
return true; }
89 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const override {
return true; }
97 registry.addExtension(FuncInlinerInterface::registrationHook);
98 registry.addExtension(FullyLegalForInlining<component::StructDialect>::registrationHook);
99 registry.addExtension(FullyLegalForInlining<constrain::ConstrainDialect>::registrationHook);
100 registry.addExtension(FullyLegalForInlining<string::StringDialect>::registrationHook);
101 registry.addExtension(FullyLegalForInlining<polymorphic::PolymorphicDialect>::registrationHook);
102 registry.addExtension(FullyLegalForInlining<felt::FeltDialect>::registrationHook);
103 registry.addExtension(FullyLegalForInlining<global::GlobalDialect>::registrationHook);
104 registry.addExtension(FullyLegalForInlining<boolean::BoolDialect>::registrationHook);
105 registry.addExtension(FullyLegalForInlining<array::ArrayDialect>::registrationHook);
106 registry.addExtension(FullyLegalForInlining<cast::CastDialect>::registrationHook);
107 registry.addExtension(FullyLegalForInlining<include::IncludeDialect>::registrationHook);
108 registry.addExtension(FullyLegalForInlining<llzk::LLZKDialect>::registrationHook);
109 registry.addExtension(FullyLegalForInlining<pod::PODDialect>::registrationHook);
void registerInliningExtensions(DialectRegistry ®istry)