20 #include "llvm/Support/InitLLVM.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/ToolOutputFile.h"
50 llvm::StringRef toolName) {
52 static llvm::cl::opt<std::string> inputFilename(
53 llvm::cl::Positional, llvm::cl::desc(
"<input file>"),
56 static llvm::cl::opt<std::string> outputFilename(
57 "o", llvm::cl::desc(
"Output filename"), llvm::cl::value_desc(
"filename"),
60 static llvm::cl::opt<bool> allowUnregisteredDialects(
61 "allow-unregistered-dialect",
62 llvm::cl::desc(
"Allow operation with no registered dialects (discouraged: testing only!)"),
63 llvm::cl::init(
false));
65 static llvm::cl::opt<bool> splitInputFile(
67 llvm::cl::desc(
"Split the input file into pieces and "
68 "process each chunk independently"),
69 llvm::cl::init(
false));
71 static llvm::cl::opt<bool> verifyDiagnostics(
73 llvm::cl::desc(
"Check that emitted diagnostics match "
74 "expected-* lines on the corresponding line"),
75 llvm::cl::init(
false));
77 static llvm::cl::opt<bool> errorDiagnosticsOnly(
78 "error-diagnostics-only",
79 llvm::cl::desc(
"Filter all non-error diagnostics "
80 "(discouraged: testing only!)"),
81 llvm::cl::init(
false));
83 llvm::InitLLVM y(argc, argv);
86 llvm::cl::list<const Translation *, bool, TranslationParser>
87 translationsRequested(
"", llvm::cl::desc(
"Translations to perform"),
93 llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
100 std::string errorMessage;
101 std::unique_ptr<llvm::MemoryBuffer> input;
102 if (
auto inputAlignment = translationsRequested[0]->getInputAlignment())
103 input =
openInputFile(inputFilename, *inputAlignment, &errorMessage);
107 llvm::errs() << errorMessage <<
"\n";
113 llvm::errs() << errorMessage <<
"\n";
118 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
125 for (
size_t i = 0, e = translationsRequested.size(); i < e; ++i) {
126 llvm::raw_ostream *stream;
127 llvm::raw_string_ostream dataStream(dataOut);
134 stream = &dataStream;
137 const Translation *translationRequested = translationsRequested[i];
144 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
145 sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
147 if (verifyDiagnostics) {
154 (void)(*translationRequested)(sourceMgr, os, &context);
155 result = sourceMgrHandler.verify();
156 }
else if (errorDiagnosticsOnly) {
158 ErrorDiagnosticFilter diagnosticFilter(&context);
159 result = (*translationRequested)(sourceMgr, *stream, &context);
162 result = (*translationRequested)(sourceMgr, *stream, &context);
172 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(dataIn);
179 output->os(), splitInputFile)))
static LogicalResult processBuffer(raw_ostream &os, std::unique_ptr< MemoryBuffer > ownedBuffer, const MlirOptMainConfig &config, DialectRegistry ®istry, llvm::ThreadPool *threadPool)
Parses the memory buffer.
static std::string diag(const llvm::Value &value)
Facilities for time measurement and report printing to an output stream.
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
MLIRContext is the top-level object for a collection of MLIR operations.
void printOpOnDiagnostic(bool enable)
Set the flag specifying if we should attach the operation to diagnostics emitted via Operation::emit.
void allowUnregisteredDialects(bool allow=true)
Enables creating operations in unregistered dialects.
This diagnostic handler is a simple RAII class that registers and erases a diagnostic handler on a gi...
This class is a utility diagnostic handler for use with llvm::SourceMgr.
This class is a utility diagnostic handler for use with llvm::SourceMgr that verifies that emitted di...
TimingScope getRootScope()
Get the root timer of this timing manager wrapped in a TimingScope for convenience.
An RAII-style wrapper around a timer that ensures the timer is properly started and stopped.
TimingScope nest(Args... args)
Create a nested timing scope.
This class contains all of the components necessary for performing a translation.
StringRef getDescription() const
Return the description of this translation.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
void registerDefaultTimingManagerCLOptions()
Register a set of useful command-line options that can be used to configure a DefaultTimingManager.
void registerTranslationCLOptions()
Register command-line options used by the translation registry.
std::unique_ptr< llvm::ToolOutputFile > openOutputFile(llvm::StringRef outputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for writing.
void registerMLIRContextCLOptions()
Register a set of useful command-line options that can be used to configure various flags within the ...
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
void registerAsmPrinterCLOptions()
Register a set of useful command-line options that can be used to configure various flags within the ...
void applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm)
Apply any values that were registered with 'registerDefaultTimingManagerOptions' to a DefaultTimingMa...
LogicalResult mlirTranslateMain(int argc, char **argv, StringRef toolName)
Translate to/from an MLIR module from/to an external representation (e.g.
LogicalResult splitAndProcessBuffer(std::unique_ptr< llvm::MemoryBuffer > originalBuffer, ChunkBufferHandler processChunkBuffer, raw_ostream &os, bool enableSplitting=true, bool insertMarkerInOutput=false)
Splits the specified buffer on a marker (// -----), processes each chunk independently according to t...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.
static LogicalResult success(bool isSuccess=true)
If isSuccess is true a success result is generated, otherwise a 'failure' result is generated.