LLZK 3.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
LLZKInliningExtensions.cpp
Go to the documentation of this file.
1//===-- LLZKInliningExtensions.cpp ------------------------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
27
28#include <mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
29#include <mlir/Transforms/InliningUtils.h>
30
31using namespace mlir;
32using namespace llzk;
33
34namespace {
35
36template <typename InlinerImpl, typename DialectImpl, typename... RequiredDialects>
37// Suppress false positive from `clang-tidy`
38// NOLINTNEXTLINE(bugprone-crtp-constructor-accessibility)
39struct BaseInlinerInterface : public DialectInlinerInterface {
40protected:
41 using DialectInlinerInterface::DialectInlinerInterface;
42
43public:
44 static void registrationHook(MLIRContext *ctx, DialectImpl *dialect) {
45 dialect->template addInterfaces<InlinerImpl>();
46 if constexpr (sizeof...(RequiredDialects) != 0) {
47 ctx->loadDialect<RequiredDialects...>();
48 }
49 }
50};
51
52// Adapted from `mlir/lib/Dialect/Func/Extensions/InlinerExtension.cpp`
53struct FuncInlinerInterface
54 : public BaseInlinerInterface<
55 FuncInlinerInterface, function::FunctionDialect, cf::ControlFlowDialect> {
56 using BaseInlinerInterface::BaseInlinerInterface;
57
59 bool isLegalToInline(Operation *, Operation *, bool) const final { return true; }
60 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { return true; }
61 bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final { return true; }
62
63 void handleTerminator(Operation *op, Block *newDest) const final {
64 // Only return needs to be handled here. Replace the return with a branch to the dest.
65 // Note: This function is only called when there are multiple blocks in the region being
66 // inlined. In LLZK IR, that would only occur when the `cf` dialect is already used (since no
67 // LLZK dialect defines any kind of cross-block branching ops) so it's fine to add a
68 // `cf::BranchOp` here.
69 if (auto returnOp = llvm::dyn_cast<function::ReturnOp>(op)) {
70 OpBuilder builder(op);
71 cf::BranchOp::create(builder, op->getLoc(), newDest, returnOp.getOperands());
72 op->erase();
73 }
74 }
75
76 void handleTerminator(Operation *op, ValueRange valuesToRepl) const final {
77 // ASSERT: when region contains a single block, terminator must be ReturnOp
78 assert(llvm::isa<function::ReturnOp>(op));
79
80 // Replace the values directly with the return operands.
81 auto returnOp = llvm::cast<function::ReturnOp>(op);
82 assert(returnOp.getNumOperands() == valuesToRepl.size());
83 for (const auto &it : llvm::enumerate(returnOp.getOperands())) {
84 valuesToRepl[it.index()].replaceAllUsesWith(it.value());
85 }
86 }
87};
88
89template <typename DialectImpl>
90struct FullyLegalForInlining
91 : public BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl> {
92 using BaseInlinerInterface<FullyLegalForInlining<DialectImpl>, DialectImpl>::BaseInlinerInterface;
93
94 bool isLegalToInline(Operation *, Operation *, bool) const override { return true; }
95 bool isLegalToInline(Region *, Region *, bool, IRMapping &) const override { return true; }
96 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const override { return true; }
97};
98
99} // namespace
100
101namespace llzk {
102
103void registerInliningExtensions(DialectRegistry &registry) {
104 registry.addExtension(FullyLegalForInlining<array::ArrayDialect>::registrationHook);
105 registry.addExtension(FullyLegalForInlining<boolean::BoolDialect>::registrationHook);
106 registry.addExtension(FullyLegalForInlining<cast::CastDialect>::registrationHook);
107 registry.addExtension(FullyLegalForInlining<component::StructDialect>::registrationHook);
108 registry.addExtension(FullyLegalForInlining<constrain::ConstrainDialect>::registrationHook);
109 registry.addExtension(FullyLegalForInlining<felt::FeltDialect>::registrationHook);
110 registry.addExtension(FullyLegalForInlining<global::GlobalDialect>::registrationHook);
111 registry.addExtension(FullyLegalForInlining<include::IncludeDialect>::registrationHook);
112 registry.addExtension(FullyLegalForInlining<llzk::LLZKDialect>::registrationHook);
113 registry.addExtension(FullyLegalForInlining<pod::PODDialect>::registrationHook);
114 registry.addExtension(FullyLegalForInlining<polymorphic::PolymorphicDialect>::registrationHook);
115 registry.addExtension(FullyLegalForInlining<ram::RAMDialect>::registrationHook);
116 registry.addExtension(FullyLegalForInlining<smt_info::SMTInfoDialect>::registrationHook);
117 registry.addExtension(FullyLegalForInlining<string::StringDialect>::registrationHook);
118 registry.addExtension(FullyLegalForInlining<verif::VerifDialect>::registrationHook);
119 registry.addExtension(FuncInlinerInterface::registrationHook);
120}
121
122} // namespace llzk
void registerInliningExtensions(DialectRegistry &registry)