MLIR  22.0.0git
Parser.cpp
Go to the documentation of this file.
1 //===- Parser.cpp - MLIR Unified Parser Interface -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the parser for the MLIR textual form.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Parser/Parser.h"
16 #include "llvm/Support/SourceMgr.h"
17 
18 using namespace mlir;
19 
20 static std::pair<int64_t, int64_t>
21 getLineAndColStart(const llvm::SourceMgr &sourceMgr) {
22  unsigned lastFileID = sourceMgr.getNumBuffers();
23  if (lastFileID == 1)
24  return {0, 0};
25 
26  auto bufferID = sourceMgr.getMainFileID();
27  const llvm::MemoryBuffer *main = sourceMgr.getMemoryBuffer(bufferID);
28  const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer(lastFileID);
29  // Exclude same start.
30  if (main->getBufferStart() < last->getBufferStart() &&
31  main->getBufferEnd() >= last->getBufferEnd()) {
32  return sourceMgr.getLineAndColumn(
33  llvm::SMLoc::getFromPointer(last->getBufferStart()), bufferID);
34  }
35  return {0, 0};
36 }
37 
38 LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
39  Block *block, const ParserConfig &config,
40  LocationAttr *sourceFileLoc) {
41  const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
42  if (sourceFileLoc) {
43  auto [line, column] = getLineAndColStart(sourceMgr);
44  *sourceFileLoc = FileLineColLoc::get(
45  config.getContext(), sourceBuf->getBufferIdentifier(), line, column);
46  }
47  if (isBytecode(*sourceBuf))
48  return readBytecodeFile(*sourceBuf, block, config);
49  return parseAsmSourceFile(sourceMgr, block, config);
50 }
51 LogicalResult
52 mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
53  Block *block, const ParserConfig &config,
54  LocationAttr *sourceFileLoc) {
55  const auto *sourceBuf =
56  sourceMgr->getMemoryBuffer(sourceMgr->getMainFileID());
57  if (sourceFileLoc) {
58  auto [line, column] = getLineAndColStart(*sourceMgr);
59  *sourceFileLoc = FileLineColLoc::get(
60  config.getContext(), sourceBuf->getBufferIdentifier(), line, column);
61  }
62  if (isBytecode(*sourceBuf))
63  return readBytecodeFile(sourceMgr, block, config);
64  return parseAsmSourceFile(*sourceMgr, block, config);
65 }
66 
67 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
68  const ParserConfig &config,
69  LocationAttr *sourceFileLoc) {
70  auto sourceMgr = std::make_shared<llvm::SourceMgr>();
71  return parseSourceFile(filename, sourceMgr, block, config, sourceFileLoc);
72 }
73 
74 static LogicalResult loadSourceFileBuffer(llvm::StringRef filename,
75  llvm::SourceMgr &sourceMgr,
76  MLIRContext *ctx) {
77  if (sourceMgr.getNumBuffers() != 0) {
78  // TODO: Extend to support multiple buffers.
79  return emitError(mlir::UnknownLoc::get(ctx),
80  "only main buffer parsed at the moment");
81  }
82  auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filename);
83  if (fileOrErr.getError())
84  return emitError(mlir::UnknownLoc::get(ctx),
85  "could not open input file " + filename);
86 
87  // Load the MLIR source file.
88  sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
89  return success();
90 }
91 
92 LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
93  llvm::SourceMgr &sourceMgr, Block *block,
94  const ParserConfig &config,
95  LocationAttr *sourceFileLoc) {
96  if (failed(loadSourceFileBuffer(filename, sourceMgr, config.getContext())))
97  return failure();
98  return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
99 }
100 LogicalResult mlir::parseSourceFile(
101  llvm::StringRef filename, const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
102  Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc) {
103  if (failed(loadSourceFileBuffer(filename, *sourceMgr, config.getContext())))
104  return failure();
105  return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
106 }
107 
108 LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
109  const ParserConfig &config,
110  StringRef sourceName,
111  LocationAttr *sourceFileLoc) {
112  auto memBuffer =
113  llvm::MemoryBuffer::getMemBuffer(sourceStr, sourceName,
114  /*RequiresNullTerminator=*/false);
115  if (!memBuffer)
116  return failure();
117 
118  llvm::SourceMgr sourceMgr;
119  sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
120  return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
121 }
static LogicalResult loadSourceFileBuffer(llvm::StringRef filename, llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
Definition: Parser.cpp:74
static std::pair< int64_t, int64_t > getLineAndColStart(const llvm::SourceMgr &sourceMgr)
Definition: Parser.cpp:21
Block represents an ordered list of Operations.
Definition: Block.h:33
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
Definition: Location.cpp:157
Location objects represent source locations information in MLIR.
Definition: Location.h:32
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:63
This class represents a configuration for the MLIR assembly parser.
Definition: AsmState.h:469
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
Include the generated interface declarations.
LogicalResult parseAsmSourceFile(const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config, AsmParserState *asmState=nullptr, AsmParserCodeCompleteContext *codeCompleteContext=nullptr)
This parses the file specified by the indicated SourceMgr and appends parsed operations to the given ...
Definition: Parser.cpp:2882
bool isBytecode(llvm::MemoryBufferRef buffer)
Returns true if the given buffer starts with the magic bytes that signal MLIR bytecode.
const FrozenRewritePatternSet GreedyRewriteConfig config
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult parseSourceString(llvm::StringRef sourceStr, Block *block, const ParserConfig &config, StringRef sourceName="", LocationAttr *sourceFileLoc=nullptr)
This parses the IR string and appends parsed operations to the given block.
Definition: Parser.cpp:108
LogicalResult parseSourceFile(const llvm::SourceMgr &sourceMgr, Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc=nullptr)
This parses the file specified by the indicated SourceMgr and appends parsed operations to the given ...
Definition: Parser.cpp:38
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult readBytecodeFile(llvm::MemoryBufferRef buffer, Block *block, const ParserConfig &config)
Read the operations defined within the given memory buffer, containing MLIR bytecode,...