19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/ToolOutputFile.h"
49 llvm::StringRef toolName) {
51 static llvm::cl::opt<std::string> inputFilename(
52 llvm::cl::Positional, llvm::cl::desc(
"<input file>"),
55 static llvm::cl::opt<std::string> outputFilename(
56 "o", llvm::cl::desc(
"Output filename"), llvm::cl::value_desc(
"filename"),
59 static llvm::cl::opt<bool> allowUnregisteredDialects(
60 "allow-unregistered-dialect",
61 llvm::cl::desc(
"Allow operation with no registered dialects (discouraged: testing only!)"),
62 llvm::cl::init(
false));
64 static llvm::cl::opt<std::string> inputSplitMarker{
65 "split-input-file", llvm::cl::ValueOptional,
66 llvm::cl::callback([&](
const std::string &str) {
71 llvm::cl::desc(
"Split the input file into chunks using the given or "
72 "default marker and process each chunk independently"),
75 static llvm::cl::opt<bool> verifyDiagnostics(
77 llvm::cl::desc(
"Check that emitted diagnostics match "
78 "expected-* lines on the corresponding line"),
79 llvm::cl::init(
false));
81 static llvm::cl::opt<bool> errorDiagnosticsOnly(
82 "error-diagnostics-only",
83 llvm::cl::desc(
"Filter all non-error diagnostics "
84 "(discouraged: testing only!)"),
85 llvm::cl::init(
false));
87 static llvm::cl::opt<std::string> outputSplitMarker(
88 "output-split-marker",
89 llvm::cl::desc(
"Split marker to use for merging the ouput"),
92 llvm::InitLLVM y(argc, argv);
95 llvm::cl::list<const Translation *, bool, TranslationParser>
96 translationsRequested(
"", llvm::cl::desc(
"Translations to perform"),
102 llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
109 std::string errorMessage;
110 std::unique_ptr<llvm::MemoryBuffer> input;
111 if (
auto inputAlignment = translationsRequested[0]->getInputAlignment())
112 input =
openInputFile(inputFilename, *inputAlignment, &errorMessage);
116 llvm::errs() << errorMessage <<
"\n";
122 llvm::errs() << errorMessage <<
"\n";
127 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
132 LogicalResult result = LogicalResult::success();
134 for (
size_t i = 0, e = translationsRequested.size(); i < e; ++i) {
135 llvm::raw_ostream *stream;
136 llvm::raw_string_ostream dataStream(dataOut);
143 stream = &dataStream;
146 const Translation *translationRequested = translationsRequested[i];
153 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
154 sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
156 if (verifyDiagnostics) {
163 (void)(*translationRequested)(sourceMgr, os, &context);
164 result = sourceMgrHandler.verify();
165 }
else if (errorDiagnosticsOnly) {
167 ErrorDiagnosticFilter diagnosticFilter(&context);
168 result = (*translationRequested)(sourceMgr, *stream, &context);
171 result = (*translationRequested)(sourceMgr, *stream, &context);
181 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(dataIn);
188 output->os(), inputSplitMarker,
static LogicalResult processBuffer(raw_ostream &os, std::unique_ptr< MemoryBuffer > ownedBuffer, const MlirOptMainConfig &config, DialectRegistry ®istry, llvm::ThreadPoolInterface *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.
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
const char *const kDefaultSplitMarker
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 splitAndProcessBuffer(std::unique_ptr< llvm::MemoryBuffer > originalBuffer, ChunkBufferHandler processChunkBuffer, raw_ostream &os, llvm::StringRef inputSplitMarker=kDefaultSplitMarker, llvm::StringRef outputSplitMarker="")
Splits the specified buffer on a marker (// ----- by default), processes each chunk independently acc...
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.