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
18 using namespace mlir;
19
parseSourceFile(const llvm::SourceMgr & sourceMgr,Block * block,const ParserConfig & config,LocationAttr * sourceFileLoc)20 LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
21 Block *block, const ParserConfig &config,
22 LocationAttr *sourceFileLoc) {
23 const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
24 if (sourceFileLoc) {
25 *sourceFileLoc = FileLineColLoc::get(config.getContext(),
26 sourceBuf->getBufferIdentifier(),
27 /*line=*/0, /*column=*/0);
28 }
29 if (isBytecode(*sourceBuf))
30 return readBytecodeFile(*sourceBuf, block, config);
31 return parseAsmSourceFile(sourceMgr, block, config);
32 }
33 LogicalResult
parseSourceFile(const std::shared_ptr<llvm::SourceMgr> & sourceMgr,Block * block,const ParserConfig & config,LocationAttr * sourceFileLoc)34 mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
35 Block *block, const ParserConfig &config,
36 LocationAttr *sourceFileLoc) {
37 const auto *sourceBuf =
38 sourceMgr->getMemoryBuffer(sourceMgr->getMainFileID());
39 if (sourceFileLoc) {
40 *sourceFileLoc = FileLineColLoc::get(config.getContext(),
41 sourceBuf->getBufferIdentifier(),
42 /*line=*/0, /*column=*/0);
43 }
44 if (isBytecode(*sourceBuf))
45 return readBytecodeFile(sourceMgr, block, config);
46 return parseAsmSourceFile(*sourceMgr, block, config);
47 }
48
parseSourceFile(llvm::StringRef filename,Block * block,const ParserConfig & config,LocationAttr * sourceFileLoc)49 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
50 const ParserConfig &config,
51 LocationAttr *sourceFileLoc) {
52 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
53 return parseSourceFile(filename, sourceMgr, block, config, sourceFileLoc);
54 }
55
loadSourceFileBuffer(llvm::StringRef filename,llvm::SourceMgr & sourceMgr,MLIRContext * ctx)56 static LogicalResult loadSourceFileBuffer(llvm::StringRef filename,
57 llvm::SourceMgr &sourceMgr,
58 MLIRContext *ctx) {
59 if (sourceMgr.getNumBuffers() != 0) {
60 // TODO: Extend to support multiple buffers.
61 return emitError(mlir::UnknownLoc::get(ctx),
62 "only main buffer parsed at the moment");
63 }
64 auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filename);
65 if (fileOrErr.getError())
66 return emitError(mlir::UnknownLoc::get(ctx),
67 "could not open input file " + filename);
68
69 // Load the MLIR source file.
70 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
71 return success();
72 }
73
parseSourceFile(llvm::StringRef filename,llvm::SourceMgr & sourceMgr,Block * block,const ParserConfig & config,LocationAttr * sourceFileLoc)74 LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
75 llvm::SourceMgr &sourceMgr, Block *block,
76 const ParserConfig &config,
77 LocationAttr *sourceFileLoc) {
78 if (failed(loadSourceFileBuffer(filename, sourceMgr, config.getContext())))
79 return failure();
80 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
81 }
parseSourceFile(llvm::StringRef filename,const std::shared_ptr<llvm::SourceMgr> & sourceMgr,Block * block,const ParserConfig & config,LocationAttr * sourceFileLoc)82 LogicalResult mlir::parseSourceFile(
83 llvm::StringRef filename, const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
84 Block *block, const ParserConfig &config, LocationAttr *sourceFileLoc) {
85 if (failed(loadSourceFileBuffer(filename, *sourceMgr, config.getContext())))
86 return failure();
87 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
88 }
89
parseSourceString(llvm::StringRef sourceStr,Block * block,const ParserConfig & config,StringRef sourceName,LocationAttr * sourceFileLoc)90 LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
91 const ParserConfig &config,
92 StringRef sourceName,
93 LocationAttr *sourceFileLoc) {
94 auto memBuffer =
95 llvm::MemoryBuffer::getMemBuffer(sourceStr, sourceName,
96 /*RequiresNullTerminator=*/false);
97 if (!memBuffer)
98 return failure();
99
100 llvm::SourceMgr sourceMgr;
101 sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
102 return parseSourceFile(sourceMgr, block, config, sourceFileLoc);
103 }
104