122 lines · cpp
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-exception6//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(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 38LogicalResult 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}51LogicalResult52mlir::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 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(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 92LogicalResult 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}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(loadSourceFileBuffer(filename, *sourceMgr, 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(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}122