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"
14#include "mlir/AsmParser/AsmParser.h"
15#include "mlir/Bytecode/BytecodeReader.h"
16#include "llvm/Support/SourceMgr.h"
17
18using namespace mlir;
19
20static std::pair<int64_t, int64_t>
21getLineAndColStart(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(i: bufferID);
28 const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer(i: lastFileID);
29 // Exclude same start.
30 if (main->getBufferStart() < last->getBufferStart() &&
31 main->getBufferEnd() >= last->getBufferEnd()) {
32 return sourceMgr.getLineAndColumn(
33 Loc: llvm::SMLoc::getFromPointer(Ptr: last->getBufferStart()), BufferID: bufferID);
34 }
35 return {0, 0};
36}
37
38LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
39 Block *block, const ParserConfig &config,
40 LocationAttr *sourceFileLoc) {
41 const auto *sourceBuf = sourceMgr.getMemoryBuffer(i: sourceMgr.getMainFileID());
42 if (sourceFileLoc) {
43 auto [line, column] = getLineAndColStart(sourceMgr);
44 *sourceFileLoc = FileLineColLoc::get(
45 context: config.getContext(), fileName: sourceBuf->getBufferIdentifier(), line, column);
46 }
47 if (isBytecode(buffer: *sourceBuf))
48 return readBytecodeFile(buffer: *sourceBuf, block, config);
49 return parseAsmSourceFile(sourceMgr, block, config);
50}
51LogicalResult
52mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
53 Block *block, const ParserConfig &config,
54 LocationAttr *sourceFileLoc) {
55 const auto *sourceBuf =
56 sourceMgr->getMemoryBuffer(i: sourceMgr->getMainFileID());
57 if (sourceFileLoc) {
58 auto [line, column] = getLineAndColStart(sourceMgr: *sourceMgr);
59 *sourceFileLoc = FileLineColLoc::get(
60 context: config.getContext(), fileName: sourceBuf->getBufferIdentifier(), line, column);
61 }
62 if (isBytecode(buffer: *sourceBuf))
63 return readBytecodeFile(sourceMgr, block, config);
64 return parseAsmSourceFile(sourceMgr: *sourceMgr, block, config);
65}
66
67LogicalResult 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
74static 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(loc: mlir::UnknownLoc::get(context: ctx),
80 message: "only main buffer parsed at the moment");
81 }
82 auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(Filename: filename);
83 if (fileOrErr.getError())
84 return emitError(loc: mlir::UnknownLoc::get(context: ctx),
85 message: "could not open input file " + filename);
86
87 // Load the MLIR source file.
88 sourceMgr.AddNewSourceBuffer(F: std::move(*fileOrErr), IncludeLoc: SMLoc());
89 return success();
90}
91
92LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
93 llvm::SourceMgr &sourceMgr, Block *block,
94 const ParserConfig &config,
95 LocationAttr *sourceFileLoc) {
96 if (failed(Result: loadSourceFileBuffer(filename, sourceMgr, ctx: config.getContext())))
97 return failure();
98 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
99}
100LogicalResult mlir::parseSourceFile(
101 llvm::StringRef filename, const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
102 Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc) {
103 if (failed(Result: loadSourceFileBuffer(filename, sourceMgr&: *sourceMgr, ctx: config.getContext())))
104 return failure();
105 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
106}
107
108LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
109 const ParserConfig &config,
110 StringRef sourceName,
111 LocationAttr *sourceFileLoc) {
112 auto memBuffer =
113 llvm::MemoryBuffer::getMemBuffer(InputData: sourceStr, BufferName: sourceName,
114 /*RequiresNullTerminator=*/false);
115 if (!memBuffer)
116 return failure();
117
118 llvm::SourceMgr sourceMgr;
119 sourceMgr.AddNewSourceBuffer(F: std::move(memBuffer), IncludeLoc: SMLoc());
120 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
121}
122