LLZK 0.1.0
Veridise's ZK Language IR
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
19
20#include <mlir/IR/AsmState.h>
21#include <mlir/IR/Diagnostics.h>
22#include <mlir/IR/MLIRContext.h>
23#include <mlir/IR/OwningOpRef.h>
24#include <mlir/IR/PatternMatch.h>
25#include <mlir/Parser/Parser.h>
26#include <mlir/Support/LogicalResult.h>
27
28#include <llvm/Support/Casting.h>
29#include <llvm/Support/MemoryBuffer.h>
30#include <llvm/Support/SourceMgr.h>
31
32#include <functional>
33
34namespace llzk::include {
35
36namespace {
37using namespace mlir;
38
39struct OpenFile {
40 std::string resolvedPath;
41 std::unique_ptr<llvm::MemoryBuffer> buffer;
42};
43
44inline FailureOr<OpenFile> openFile(EmitErrorFn emitError, const StringRef filename) {
45 OpenFile r;
46
47 auto buffer = GlobalSourceMgr::get().openIncludeFile(filename, r.resolvedPath);
48 if (!buffer) {
49 return emitError() << "could not find file \"" << filename << "\"";
50 }
51 r.buffer = std::move(*buffer);
52 return std::move(r);
53}
54
55FailureOr<OwningOpRef<ModuleOp>> parseFile(const StringRef filename, Operation *origin) {
56 // Load raw contents of the file
57 auto of = openFile(getEmitOpErrFn(origin), filename);
58 if (failed(of)) {
59 return failure();
60 }
61
62 // Parse the IR and write it in the destination block
63 ParserConfig parseConfig(origin->getContext());
64 llvm::StringRef contents = of->buffer->getBuffer();
65 auto res = parseSourceString<ModuleOp>(contents, parseConfig, /*sourceName=*/of->resolvedPath);
66 if (res) {
67 return res;
68 } else {
69 return origin->emitOpError() << "could not parse file \"" << filename << "\"";
70 }
71}
72
73LogicalResult parseFile(const StringRef filename, Operation *origin, Block *container) {
74 // Load raw contents of the file
75 auto of = openFile(getEmitOpErrFn(origin), filename);
76 if (failed(of)) {
77 return failure();
78 }
79
80 // Parse the IR and write it in the destination block
81 ParserConfig parseConfig(origin->getContext());
82 llvm::StringRef contents = of->buffer->getBuffer();
83 auto res = parseSourceString(contents, container, parseConfig, /*sourceName=*/of->resolvedPath);
84 if (succeeded(res)) {
85 return res;
86 } else {
87 return origin->emitOpError() << "could not parse file \"" << filename << "\"";
88 }
89}
90
91inline LogicalResult validateLoadedModuleOp(EmitErrorFn emitError, ModuleOp importedMod) {
92 if (!importedMod->hasAttr(LANG_ATTR_NAME)) {
93 return emitError()
94 .append(
95 "expected '", ModuleOp::getOperationName(), "' from included file to have \"",
96 LANG_ATTR_NAME, "\" attribute"
97 )
98 .attachNote(importedMod.getLoc())
99 .append("this should have \"", LANG_ATTR_NAME, "\" attribute");
100 }
101 if (importedMod.getSymNameAttr()) {
102 return emitError()
103 .append("expected '", ModuleOp::getOperationName(), "' from included file to be unnamed")
104 .attachNote(importedMod.getLoc())
105 .append("this should be unnamed");
106 }
107 return success();
108}
109
113class InlineOperationsGuard {
114public:
115 InlineOperationsGuard(MLIRContext *ctx, IncludeOp &tIncOp)
116 : incOp(tIncOp), rewriter(ctx), dest(rewriter.createBlock(incOp->getBlock()->getParent())) {}
117
118 ~InlineOperationsGuard() {
119 if (commited) {
120 // The container was inlined so get rid of the include op.
121 rewriter.eraseOp(incOp);
122 } else {
123 // The container was not inlined so delete the container.
124 dest->erase();
125 }
126 }
127
129 void moduleWasLoaded() {
130 assert(!dest->empty());
131 blockWritten = true;
132 }
133
134 // Attempts to get the module written into the block
135 FailureOr<ModuleOp> getModule() {
136 // If the block is not ready return failure but do not emit diagnostics.
137 if (!blockWritten) {
138 return failure();
139 }
140
141 if (dest->empty()) {
142 return incOp->emitOpError() << "failed to inline the module. No operation was written.";
143 }
144
145 auto &op = dest->front();
146 if (!isa<ModuleOp>(op)) {
147 return op.emitError()
148 .append(
149 "expected '", ModuleOp::getOperationName(),
150 "' as top level operation of included file. Got '", op.getName(), "'."
151 )
152 .attachNote(incOp.getLoc())
153 .append("from file included here");
154 }
155 return llvm::cast<ModuleOp>(op);
156 }
157
158 Block *getDest() { return dest; }
159
160 FailureOr<ModuleOp> commit() {
161 // Locate where to insert the inlined module
162 rewriter.setInsertionPointAfter(incOp);
163 auto insertionPoint = rewriter.getInsertionPoint();
164 {
165 // This op will be invalid after inlining the block
166 auto modRes = getModule();
167 // Won't commit on a failed result
168 if (failed(modRes)) {
169 return failure();
170 }
171
172 // Add the destination block after the insertion point.
173 // dest becomes the source from which to move operations.
174 rewriter.inlineBlockBefore(dest, rewriter.getInsertionBlock(), insertionPoint);
175 }
176
177 rewriter.setInsertionPointAfter(incOp);
178 auto modOp = rewriter.getInsertionPoint();
179 ModuleOp mod = llvm::dyn_cast<ModuleOp>(modOp);
180
181 // Apply the name from the IncludeOp to the new ModuleOp
182 mod.setSymNameAttr(incOp.getSymNameAttr());
183
184 // All good so we mark as commited and return a reference to the newly generated module.
185 commited = true;
186 return mod;
187 }
188
189private:
190 bool commited = false, blockWritten = false;
191 IncludeOp &incOp;
192 IRRewriter rewriter;
193 Block *dest;
194};
195} // namespace
196
197FailureOr<ModuleOp> IncludeOp::inlineAndErase() {
198 InlineOperationsGuard guard(this->getContext(), *this);
199
200 auto loadResult = parseFile(this->getPath(), *this, guard.getDest());
201 if (failed(loadResult)) {
202 return failure();
203 }
204 guard.moduleWasLoaded();
205
206 auto importedMod = guard.getModule();
207 if (failed(importedMod)) {
208 return failure(); // getModule() already generates an error message
209 }
210
211 // Check properties of the included file to ensure symbol resolution will still work.
212 auto validationResult = validateLoadedModuleOp(getEmitOpErrFn(this), *importedMod);
213 if (failed(validationResult)) {
214 return failure();
215 }
216
217 return guard.commit();
218}
219
220FailureOr<OwningOpRef<ModuleOp>> IncludeOp::openModule() {
221 return parseFile(this->getPathAttr(), *this);
222}
223
224} // 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:268
::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.
::mlir::StringAttr getPathAttr()
Definition Ops.cpp.inc:264
constexpr char LANG_ATTR_NAME[]
Name of the attribute on the top-level ModuleOp that specifies the IR language name.
Definition Constants.h:31
llvm::function_ref< mlir::InFlightDiagnostic()> EmitErrorFn
Definition ErrorHelper.h:18
OwningEmitErrorFn getEmitOpErrFn(mlir::Operation *op)
Definition ErrorHelper.h:24
ExpressionValue mod(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)