mirror of
https://github.com/intel/llvm.git
synced 2026-02-02 10:08:59 +08:00
Extract openInputFile() into Support/FileUtilities
Multiple binaries have the needs to open input files. Use this function to de-duplicate the code. Also changed openOutputFile() to return errors using std::string since it is a library call and accessing I/O in library call is not friendly. PiperOrigin-RevId: 228878221
This commit is contained in:
@@ -22,14 +22,28 @@
|
||||
#ifndef MLIR_SUPPORT_FILEUTILITIES_H_
|
||||
#define MLIR_SUPPORT_FILEUTILITIES_H_
|
||||
|
||||
#include "mlir/Support/LLVM.h"
|
||||
#include "llvm/Support/ToolOutputFile.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace llvm {
|
||||
class MemoryBuffer;
|
||||
class ToolOutputFile;
|
||||
class StringRef;
|
||||
} // namespace llvm
|
||||
|
||||
namespace mlir {
|
||||
|
||||
/// Open the file specified by its name for writing.
|
||||
std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef outputFilename);
|
||||
/// Open the file specified by its name for reading. Write the error message to
|
||||
/// `errorMessage` if errors occur and `errorMessage` is not nullptr.
|
||||
std::unique_ptr<llvm::MemoryBuffer>
|
||||
openInputFile(llvm::StringRef inputFilename,
|
||||
std::string *errorMessage = nullptr);
|
||||
|
||||
/// Open the file specified by its name for writing. Write the error message to
|
||||
/// `errorMessage` if errors occur and `errorMessage` is not nullptr.
|
||||
std::unique_ptr<llvm::ToolOutputFile>
|
||||
openOutputFile(llvm::StringRef outputFilename,
|
||||
std::string *errorMessage = nullptr);
|
||||
|
||||
} // namespace mlir
|
||||
|
||||
|
||||
@@ -20,18 +20,35 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "mlir/Support/FileUtilities.h"
|
||||
#include "mlir/Support/LLVM.h"
|
||||
#include "llvm/Support/FileUtilities.h"
|
||||
#include "llvm/Support/MemoryBuffer.h"
|
||||
#include "llvm/Support/ToolOutputFile.h"
|
||||
|
||||
using namespace mlir;
|
||||
|
||||
std::unique_ptr<llvm::MemoryBuffer>
|
||||
mlir::openInputFile(StringRef inputFilename, std::string *errorMessage) {
|
||||
auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
|
||||
if (std::error_code error = fileOrErr.getError()) {
|
||||
if (errorMessage)
|
||||
*errorMessage = "cannot open input file '" + inputFilename.str() +
|
||||
"': " + error.message();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::move(*fileOrErr);
|
||||
}
|
||||
|
||||
std::unique_ptr<llvm::ToolOutputFile>
|
||||
mlir::openOutputFile(StringRef outputFilename) {
|
||||
mlir::openOutputFile(StringRef outputFilename, std::string *errorMessage) {
|
||||
std::error_code error;
|
||||
auto result = llvm::make_unique<llvm::ToolOutputFile>(outputFilename, error,
|
||||
llvm::sys::fs::F_None);
|
||||
if (error) {
|
||||
llvm::errs() << error.message();
|
||||
if (errorMessage)
|
||||
*errorMessage = "cannot open output file '" + outputFilename.str() +
|
||||
"': " + error.message();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "llvm/IR/LLVMContext.h"
|
||||
#include "llvm/IR/Module.h"
|
||||
#include "llvm/IR/Type.h"
|
||||
#include "llvm/Support/ToolOutputFile.h"
|
||||
|
||||
using namespace mlir;
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "mlir/IR/Module.h"
|
||||
#include "mlir/Parser.h"
|
||||
#include "mlir/Pass.h"
|
||||
#include "mlir/Support/FileUtilities.h"
|
||||
#include "mlir/Support/PassNameParser.h"
|
||||
#include "mlir/TensorFlow/ControlFlowOps.h"
|
||||
#include "mlir/TensorFlow/Passes.h"
|
||||
@@ -71,20 +72,6 @@ static std::vector<const mlir::PassInfo *> *passList;
|
||||
|
||||
enum OptResult { OptSuccess, OptFailure };
|
||||
|
||||
/// Open the specified output file and return it, exiting if there is any I/O or
|
||||
/// other errors.
|
||||
static std::unique_ptr<ToolOutputFile> getOutputStream() {
|
||||
std::error_code error;
|
||||
auto result =
|
||||
llvm::make_unique<ToolOutputFile>(outputFilename, error, sys::fs::F_None);
|
||||
if (error) {
|
||||
llvm::errs() << error.message() << '\n';
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Given a MemoryBuffer along with a line and column within it, return the
|
||||
/// location being referenced.
|
||||
static SMLoc getLocFromLineAndCol(MemoryBuffer &membuf, unsigned lineNo,
|
||||
@@ -150,8 +137,14 @@ static OptResult performActions(SourceMgr &sourceMgr, MLIRContext *context) {
|
||||
return OptFailure;
|
||||
}
|
||||
|
||||
std::string errorMessage;
|
||||
auto output = openOutputFile(outputFilename, &errorMessage);
|
||||
if (!output) {
|
||||
llvm::errs() << errorMessage << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Print the output.
|
||||
auto output = getOutputStream();
|
||||
module->print(output->os());
|
||||
output->keep();
|
||||
return OptSuccess;
|
||||
@@ -381,17 +374,17 @@ int main(int argc, char **argv) {
|
||||
cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
|
||||
|
||||
// Set up the input file.
|
||||
auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
|
||||
if (std::error_code error = fileOrErr.getError()) {
|
||||
llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
|
||||
<< "': " << error.message() << "\n";
|
||||
return 1;
|
||||
std::string errorMessage;
|
||||
auto file = openInputFile(inputFilename, &errorMessage);
|
||||
if (!file) {
|
||||
llvm::errs() << errorMessage << "\n";
|
||||
return OptFailure;
|
||||
}
|
||||
|
||||
// The split-input-file mode is a very specific mode that slices the file
|
||||
// up into small pieces and checks each independently.
|
||||
if (splitInputFile)
|
||||
return splitAndProcessFile(std::move(*fileOrErr));
|
||||
return splitAndProcessFile(std::move(file));
|
||||
|
||||
return processFile(std::move(*fileOrErr));
|
||||
return processFile(std::move(file));
|
||||
}
|
||||
|
||||
@@ -45,15 +45,15 @@ static llvm::cl::opt<std::string>
|
||||
|
||||
static Module *parseMLIRInput(StringRef inputFilename, MLIRContext *context) {
|
||||
// Set up the input file.
|
||||
auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(inputFilename);
|
||||
if (std::error_code error = fileOrErr.getError()) {
|
||||
llvm::errs() << "Could not open input file '" << inputFilename
|
||||
<< "': " << error.message();
|
||||
std::string errorMessage;
|
||||
auto file = openInputFile(inputFilename, &errorMessage);
|
||||
if (!file) {
|
||||
llvm::errs() << errorMessage << "\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
llvm::SourceMgr sourceMgr;
|
||||
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), llvm::SMLoc());
|
||||
sourceMgr.AddNewSourceBuffer(std::move(file), llvm::SMLoc());
|
||||
return parseSourceFile(sourceMgr, context);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user