MLIR  19.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 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 }
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 
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 
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 
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 }
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 
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 }
static LogicalResult loadSourceFileBuffer(llvm::StringRef filename, llvm::SourceMgr &sourceMgr, MLIRContext *ctx)
Definition: Parser.cpp:56
Block represents an ordered list of Operations.
Definition: Block.h:30
Location objects represent source locations information in MLIR.
Definition: Location.h:31
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class represents a configuration for the MLIR assembly parser.
Definition: AsmState.h:460
MLIRContext * getContext() const
Return the MLIRContext to be used when parsing.
Definition: AsmState.h:474
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
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:2778
bool isBytecode(llvm::MemoryBufferRef buffer)
Returns true if the given buffer starts with the magic bytes that signal MLIR bytecode.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
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:90
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:20
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,...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26