LLZK 2.0.0
An open-source IR for Zero Knowledge (ZK) circuits
Loading...
Searching...
No Matches
IncludeHelper.cpp
Go to the documentation of this file.
1//===-- IncludeHelper.cpp - Helpers for LLZK file includes ------*- 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//===----------------------------------------------------------------------===//
13//===----------------------------------------------------------------------===//
14
16
20
21#include <mlir/IR/AsmState.h>
22#include <mlir/IR/Diagnostics.h>
23#include <mlir/IR/MLIRContext.h>
24#include <mlir/IR/OwningOpRef.h>
25#include <mlir/IR/PatternMatch.h>
26#include <mlir/Parser/Parser.h>
27#include <mlir/Support/LogicalResult.h>
28
29#include <llvm/Support/Casting.h>
30#include <llvm/Support/MemoryBuffer.h>
31#include <llvm/Support/SourceMgr.h>
32
33#include <functional>
34
35namespace llzk::include {
36
37namespace {
38using namespace mlir;
39
40struct OpenFile {
41 std::string resolvedPath;
42 std::unique_ptr<llvm::MemoryBuffer> buffer;
43};
44
45inline FailureOr<OpenFile> openFile(EmitErrorFn emitError, const StringRef filename) {
46 OpenFile r;
47
48 auto buffer = GlobalSourceMgr::get().openIncludeFile(filename, r.resolvedPath);
49 if (!buffer) {
50 return emitError() << "could not find file \"" << filename << '"';
51 }
52 r.buffer = std::move(*buffer);
53 return std::move(r);
54}
55
56FailureOr<OwningOpRef<ModuleOp>> parseFile(const StringRef filename, Operation *origin) {
57 // Load raw contents of the file
58 auto of = openFile(getEmitOpErrFn(origin), filename);
59 if (failed(of)) {
60 return failure();
61 }
62
63 // Parse the IR and write it in the destination block
64 ParserConfig parseConfig(origin->getContext());
65 llvm::StringRef contents = of->buffer->getBuffer();
66 auto res = parseSourceString<ModuleOp>(contents, parseConfig, /*sourceName=*/of->resolvedPath);
67 if (res) {
68 return res;
69 } else {
70 return origin->emitOpError() << "could not parse file \"" << filename << '"';
71 }
72}
73
74LogicalResult parseFile(const StringRef filename, Operation *origin, Block *container) {
75 // Load raw contents of the file
76 auto of = openFile(getEmitOpErrFn(origin), filename);
77 if (failed(of)) {
78 return failure();
79 }
80
81 // Parse the IR and write it in the destination block
82 ParserConfig parseConfig(origin->getContext());
83 llvm::StringRef contents = of->buffer->getBuffer();
84 auto res = parseSourceString(contents, container, parseConfig, /*sourceName=*/of->resolvedPath);
85 if (succeeded(res)) {
86 return res;
87 } else {
88 return origin->emitOpError() << "could not parse file \"" << filename << '"';
89 }
90}
91
92inline LogicalResult validateLoadedModuleOp(EmitErrorFn emitError, ModuleOp importedMod) {
93 if (!importedMod->hasAttr(LANG_ATTR_NAME)) {
94 return emitError()
95 .append(
96 "expected '", ModuleOp::getOperationName(), "' from included file to have \"",
97 LANG_ATTR_NAME, "\" attribute"
98 )
99 .attachNote(importedMod.getLoc())
100 .append("this should have \"", LANG_ATTR_NAME, "\" attribute");
101 }
102 if (importedMod.getSymNameAttr()) {
103 return emitError()
104 .append("expected '", ModuleOp::getOperationName(), "' from included file to be unnamed")
105 .attachNote(importedMod.getLoc())
106 .append("this should be unnamed");
107 }
108 return success();
109}
110
114class InlineOperationsGuard {
115public:
116 InlineOperationsGuard(MLIRContext *ctx, IncludeOp &tIncOp)
117 : incOp(tIncOp), rewriter(ctx), dest(rewriter.createBlock(incOp->getBlock()->getParent())) {}
118
119 ~InlineOperationsGuard() {
120 if (commited) {
121 // The container was inlined so get rid of the include op.
122 rewriter.eraseOp(incOp);
123 } else {
124 // The container was not inlined so delete the container.
125 dest->erase();
126 }
127 }
128
130 void moduleWasLoaded() {
131 assert(!dest->empty());
132 blockWritten = true;
133 }
134
135 // Attempts to get the module written into the block
136 FailureOr<ModuleOp> getModule() {
137 // If the block is not ready return failure but do not emit diagnostics.
138 if (!blockWritten) {
139 return failure();
140 }
141
142 if (dest->empty()) {
143 return incOp->emitOpError() << "failed to inline the module. No operation was written.";
144 }
145
146 auto &op = dest->front();
147 if (!isa<ModuleOp>(op)) {
148 return op.emitError()
149 .append(
150 "expected '", ModuleOp::getOperationName(),
151 "' as top level operation of included file. Got '", op.getName(), "'."
152 )
153 .attachNote(incOp.getLoc())
154 .append("from file included here");
155 }
156 return llvm::cast<ModuleOp>(op);
157 }
158
159 Block *getDest() { return dest; }
160
161 FailureOr<ModuleOp> commit() {
162 // Locate where to insert the inlined module
163 rewriter.setInsertionPointAfter(incOp);
164 auto insertionPoint = rewriter.getInsertionPoint();
165 {
166 // This op will be invalid after inlining the block
167 auto modRes = getModule();
168 // Won't commit on a failed result
169 if (failed(modRes)) {
170 return failure();
171 }
172
173 // Add the destination block after the insertion point.
174 // dest becomes the source from which to move operations.
175 rewriter.inlineBlockBefore(dest, rewriter.getInsertionBlock(), insertionPoint);
176 }
177
178 rewriter.setInsertionPointAfter(incOp);
179 auto modOp = rewriter.getInsertionPoint();
180 ModuleOp mod = llvm::dyn_cast<ModuleOp>(modOp);
181
182 // Apply the name from the IncludeOp to the new ModuleOp
183 mod.setSymNameAttr(incOp.getSymNameAttr());
184
185 // All good so we mark as commited and return a reference to the newly generated module.
186 commited = true;
187 return mod;
188 }
189
190private:
191 bool commited = false, blockWritten = false;
192 IncludeOp &incOp;
193 IRRewriter rewriter;
194 Block *dest;
195};
196} // namespace
197
198FailureOr<ModuleOp> IncludeOp::inlineAndErase() {
199 InlineOperationsGuard guard(this->getContext(), *this);
200
201 auto loadResult = parseFile(this->getPath(), *this, guard.getDest());
202 if (failed(loadResult)) {
203 return failure();
204 }
205 guard.moduleWasLoaded();
206
207 auto importedMod = guard.getModule();
208 if (failed(importedMod)) {
209 return failure(); // getModule() already generates an error message
210 }
211
212 // Check properties of the included file to ensure symbol resolution will still work.
213 auto validationResult = validateLoadedModuleOp(getEmitOpErrFn(this), *importedMod);
214 if (failed(validationResult)) {
215 return failure();
216 }
217
218 return guard.commit();
219}
220
221FailureOr<OwningOpRef<ModuleOp>> IncludeOp::openModule() {
222 return parseFile(this->getPathAttr(), *this);
223}
224
225} // namespace llzk::include
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf of
Definition LICENSE.txt:57
static GlobalSourceMgr & get()
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > openIncludeFile(const mlir::StringRef filename, std::string &resolvedFile)
::llvm::StringRef getPath()
Definition Ops.cpp.inc:208
::mlir::StringAttr getPathAttr()
Definition Ops.h.inc:205
::mlir::FailureOr< mlir::OwningOpRef< mlir::ModuleOp > > openModule()
Opens the module this include references but doesn't insert it into the parent module.
::mlir::FailureOr< mlir::ModuleOp > inlineAndErase()
Opens the module this include references and replace this include with that module.
ExpressionValue mod(const llvm::SMTSolverRef &solver, const ExpressionValue &lhs, const ExpressionValue &rhs)
constexpr char LANG_ATTR_NAME[]
Name of the attribute on the top-level ModuleOp that identifies the ModuleOp as the root module and s...
Definition Constants.h:23
llvm::function_ref< InFlightDiagnosticWrapper()> EmitErrorFn
Callback to produce an error diagnostic.
OwningEmitErrorFn getEmitOpErrFn(mlir::Operation *op)