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>
29#include <llvm/Support/Casting.h>
30#include <llvm/Support/MemoryBuffer.h>
31#include <llvm/Support/SourceMgr.h>
41 std::string resolvedPath;
42 std::unique_ptr<llvm::MemoryBuffer> buffer;
45inline FailureOr<OpenFile> openFile(
EmitErrorFn emitError,
const StringRef filename) {
50 return emitError() <<
"could not find file \"" << filename <<
'"';
52 r.buffer = std::move(*buffer);
56FailureOr<OwningOpRef<ModuleOp>> parseFile(
const StringRef filename, Operation *origin) {
64 ParserConfig parseConfig(origin->getContext());
65 llvm::StringRef contents =
of->buffer->getBuffer();
66 auto res = parseSourceString<ModuleOp>(contents, parseConfig,
of->resolvedPath);
70 return origin->emitOpError() <<
"could not parse file \"" << filename <<
'"';
74LogicalResult parseFile(
const StringRef filename, Operation *origin, Block *container) {
82 ParserConfig parseConfig(origin->getContext());
83 llvm::StringRef contents =
of->buffer->getBuffer();
84 auto res = parseSourceString(contents, container, parseConfig,
of->resolvedPath);
88 return origin->emitOpError() <<
"could not parse file \"" << filename <<
'"';
92inline LogicalResult validateLoadedModuleOp(
EmitErrorFn emitError, ModuleOp importedMod) {
96 "expected '", ModuleOp::getOperationName(),
"' from included file to have \"",
99 .attachNote(importedMod.getLoc())
102 if (importedMod.getSymNameAttr()) {
104 .append(
"expected '", ModuleOp::getOperationName(),
"' from included file to be unnamed")
105 .attachNote(importedMod.getLoc())
106 .append(
"this should be unnamed");
114class InlineOperationsGuard {
116 InlineOperationsGuard(MLIRContext *ctx, IncludeOp &tIncOp)
117 : incOp(tIncOp), rewriter(ctx), dest(rewriter.createBlock(incOp->getBlock()->getParent())) {}
119 ~InlineOperationsGuard() {
122 rewriter.eraseOp(incOp);
130 void moduleWasLoaded() {
131 assert(!dest->empty());
136 FailureOr<ModuleOp> getModule() {
143 return incOp->emitOpError() <<
"failed to inline the module. No operation was written.";
146 auto &op = dest->front();
147 if (!isa<ModuleOp>(op)) {
148 return op.emitError()
150 "expected '", ModuleOp::getOperationName(),
151 "' as top level operation of included file. Got '", op.getName(),
"'."
153 .attachNote(incOp.getLoc())
154 .append(
"from file included here");
156 return llvm::cast<ModuleOp>(op);
159 Block *getDest() {
return dest; }
161 FailureOr<ModuleOp> commit() {
163 rewriter.setInsertionPointAfter(incOp);
164 auto insertionPoint = rewriter.getInsertionPoint();
167 auto modRes = getModule();
169 if (failed(modRes)) {
175 rewriter.inlineBlockBefore(dest, rewriter.getInsertionBlock(), insertionPoint);
178 rewriter.setInsertionPointAfter(incOp);
179 auto modOp = rewriter.getInsertionPoint();
180 ModuleOp
mod = llvm::dyn_cast<ModuleOp>(modOp);
183 mod.setSymNameAttr(incOp.getSymNameAttr());
191 bool commited =
false, blockWritten =
false;
199 InlineOperationsGuard guard(this->getContext(), *
this);
201 auto loadResult = parseFile(this->
getPath(), *
this, guard.getDest());
202 if (failed(loadResult)) {
205 guard.moduleWasLoaded();
207 auto importedMod = guard.getModule();
208 if (failed(importedMod)) {
213 auto validationResult = validateLoadedModuleOp(
getEmitOpErrFn(
this), *importedMod);
214 if (failed(validationResult)) {
218 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::StringAttr getPathAttr()
::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...
llvm::function_ref< InFlightDiagnosticWrapper()> EmitErrorFn
Callback to produce an error diagnostic.
OwningEmitErrorFn getEmitOpErrFn(mlir::Operation *op)