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>
28#include <llvm/Support/Casting.h>
29#include <llvm/Support/MemoryBuffer.h>
30#include <llvm/Support/SourceMgr.h>
40 std::string resolvedPath;
41 std::unique_ptr<llvm::MemoryBuffer> buffer;
44inline FailureOr<OpenFile> openFile(
EmitErrorFn emitError,
const StringRef filename) {
49 return emitError() <<
"could not find file \"" << filename <<
"\"";
51 r.buffer = std::move(*buffer);
55FailureOr<OwningOpRef<ModuleOp>> parseFile(
const StringRef filename, Operation *origin) {
63 ParserConfig parseConfig(origin->getContext());
64 llvm::StringRef contents =
of->buffer->getBuffer();
65 auto res = parseSourceString<ModuleOp>(contents, parseConfig,
of->resolvedPath);
69 return origin->emitOpError() <<
"could not parse file \"" << filename <<
"\"";
73LogicalResult parseFile(
const StringRef filename, Operation *origin, Block *container) {
81 ParserConfig parseConfig(origin->getContext());
82 llvm::StringRef contents =
of->buffer->getBuffer();
83 auto res = parseSourceString(contents, container, parseConfig,
of->resolvedPath);
87 return origin->emitOpError() <<
"could not parse file \"" << filename <<
"\"";
91inline LogicalResult validateLoadedModuleOp(
EmitErrorFn emitError, ModuleOp importedMod) {
95 "expected '", ModuleOp::getOperationName(),
"' from included file to have \"",
98 .attachNote(importedMod.getLoc())
101 if (importedMod.getSymNameAttr()) {
103 .append(
"expected '", ModuleOp::getOperationName(),
"' from included file to be unnamed")
104 .attachNote(importedMod.getLoc())
105 .append(
"this should be unnamed");
113class InlineOperationsGuard {
115 InlineOperationsGuard(MLIRContext *ctx, IncludeOp &tIncOp)
116 : incOp(tIncOp), rewriter(ctx), dest(rewriter.createBlock(incOp->getBlock()->getParent())) {}
118 ~InlineOperationsGuard() {
121 rewriter.eraseOp(incOp);
129 void moduleWasLoaded() {
130 assert(!dest->empty());
135 FailureOr<ModuleOp> getModule() {
142 return incOp->emitOpError() <<
"failed to inline the module. No operation was written.";
145 auto &op = dest->front();
146 if (!isa<ModuleOp>(op)) {
147 return op.emitError()
149 "expected '", ModuleOp::getOperationName(),
150 "' as top level operation of included file. Got '", op.getName(),
"'."
152 .attachNote(incOp.getLoc())
153 .append(
"from file included here");
155 return llvm::cast<ModuleOp>(op);
158 Block *getDest() {
return dest; }
160 FailureOr<ModuleOp> commit() {
162 rewriter.setInsertionPointAfter(incOp);
163 auto insertionPoint = rewriter.getInsertionPoint();
166 auto modRes = getModule();
168 if (failed(modRes)) {
174 rewriter.inlineBlockBefore(dest, rewriter.getInsertionBlock(), insertionPoint);
177 rewriter.setInsertionPointAfter(incOp);
178 auto modOp = rewriter.getInsertionPoint();
179 ModuleOp
mod = llvm::dyn_cast<ModuleOp>(modOp);
182 mod.setSymNameAttr(incOp.getSymNameAttr());
190 bool commited =
false, blockWritten =
false;
198 InlineOperationsGuard guard(this->getContext(), *
this);
200 auto loadResult = parseFile(this->
getPath(), *
this, guard.getDest());
201 if (failed(loadResult)) {
204 guard.moduleWasLoaded();
206 auto importedMod = guard.getModule();
207 if (failed(importedMod)) {
212 auto validationResult = validateLoadedModuleOp(
getEmitOpErrFn(
this), *importedMod);
213 if (failed(validationResult)) {
217 return guard.commit();
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
static GlobalSourceMgr & get()
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > openIncludeFile(const mlir::StringRef filename, std::string &resolvedFile)
::llvm::StringRef getPath()
::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()
constexpr char LANG_ATTR_NAME[]
Name of the attribute on the top-level ModuleOp that specifies the IR language name.
llvm::function_ref< mlir::InFlightDiagnostic()> EmitErrorFn
OwningEmitErrorFn getEmitOpErrFn(mlir::Operation *op)
ExpressionValue mod(llvm::SMTSolverRef solver, const ExpressionValue &lhs, const ExpressionValue &rhs)