1 //===- Parser.cpp - MLIR Unified Parser Interface -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the parser for the MLIR textual form.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/Parser/Parser.h"
14 #include "mlir/AsmParser/AsmParser.h"
15 #include "mlir/Bytecode/BytecodeReader.h"
16 #include "llvm/Support/SourceMgr.h"
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());
25 *sourceFileLoc
= FileLineColLoc::get(config
.getContext(),
26 sourceBuf
->getBufferIdentifier(),
27 /*line=*/0, /*column=*/0);
29 if (isBytecode(*sourceBuf
))
30 return readBytecodeFile(*sourceBuf
, block
, config
);
31 return parseAsmSourceFile(sourceMgr
, block
, config
);
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());
40 *sourceFileLoc
= FileLineColLoc::get(config
.getContext(),
41 sourceBuf
->getBufferIdentifier(),
42 /*line=*/0, /*column=*/0);
44 if (isBytecode(*sourceBuf
))
45 return readBytecodeFile(sourceMgr
, block
, config
);
46 return parseAsmSourceFile(*sourceMgr
, block
, config
);
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
);
56 static LogicalResult
loadSourceFileBuffer(llvm::StringRef filename
,
57 llvm::SourceMgr
&sourceMgr
,
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");
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
);
69 // Load the MLIR source file.
70 sourceMgr
.AddNewSourceBuffer(std::move(*fileOrErr
), SMLoc());
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())))
80 return parseSourceFile(sourceMgr
, block
, config
, 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())))
87 return parseSourceFile(sourceMgr
, block
, config
, sourceFileLoc
);
90 LogicalResult
mlir::parseSourceString(llvm::StringRef sourceStr
, Block
*block
,
91 const ParserConfig
&config
,
93 LocationAttr
*sourceFileLoc
) {
95 llvm::MemoryBuffer::getMemBuffer(sourceStr
, sourceName
,
96 /*RequiresNullTerminator=*/false);
100 llvm::SourceMgr sourceMgr
;
101 sourceMgr
.AddNewSourceBuffer(std::move(memBuffer
), SMLoc());
102 return parseSourceFile(sourceMgr
, block
, config
, sourceFileLoc
);