16 #include "llvm/Support/InitLLVM.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/ToolOutputFile.h"
46 llvm::StringRef toolName) {
48 static llvm::cl::opt<std::string> inputFilename(
49 llvm::cl::Positional, llvm::cl::desc(
"<input file>"),
52 static llvm::cl::opt<std::string> outputFilename(
53 "o", llvm::cl::desc(
"Output filename"), llvm::cl::value_desc(
"filename"),
56 static llvm::cl::opt<bool> allowUnregisteredDialects(
57 "allow-unregistered-dialect",
58 llvm::cl::desc(
"Allow operation with no registered dialects (discouraged: testing only!)"),
59 llvm::cl::init(
false));
61 static llvm::cl::opt<std::string> inputSplitMarker{
62 "split-input-file", llvm::cl::ValueOptional,
63 llvm::cl::callback([&](
const std::string &str) {
68 llvm::cl::desc(
"Split the input file into chunks using the given or "
69 "default marker and process each chunk independently"),
72 static llvm::cl::opt<SourceMgrDiagnosticVerifierHandler::Level>
74 "verify-diagnostics", llvm::cl::ValueOptional,
75 llvm::cl::desc(
"Check that emitted diagnostics match expected-* "
76 "lines on the corresponding line"),
80 "Check all diagnostics (expected, unexpected, near-misses)"),
85 "Check all diagnostics (expected, unexpected, near-misses)"),
88 "only-expected",
"Check only expected diagnostics"))};
90 static llvm::cl::opt<bool> errorDiagnosticsOnly(
91 "error-diagnostics-only",
92 llvm::cl::desc(
"Filter all non-error diagnostics "
93 "(discouraged: testing only!)"),
94 llvm::cl::init(
false));
96 static llvm::cl::opt<std::string> outputSplitMarker(
97 "output-split-marker",
98 llvm::cl::desc(
"Split marker to use for merging the ouput"),
101 llvm::InitLLVM y(argc, argv);
104 llvm::cl::list<const Translation *, bool, TranslationParser>
105 translationsRequested(
"", llvm::cl::desc(
"Translations to perform"),
111 llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
118 std::string errorMessage;
119 std::unique_ptr<llvm::MemoryBuffer> input;
120 if (
auto inputAlignment = translationsRequested[0]->getInputAlignment())
121 input =
openInputFile(inputFilename, *inputAlignment, &errorMessage);
125 llvm::errs() << errorMessage <<
"\n";
131 llvm::errs() << errorMessage <<
"\n";
136 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
141 LogicalResult result = LogicalResult::success();
143 for (
size_t i = 0, e = translationsRequested.size(); i < e; ++i) {
144 llvm::raw_ostream *stream;
145 llvm::raw_string_ostream dataStream(dataOut);
152 stream = &dataStream;
155 const Translation *translationRequested = translationsRequested[i];
162 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
163 sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
165 if (verifyDiagnostics.getNumOccurrences()) {
171 *sourceMgr, &context, verifyDiagnostics);
172 (void)(*translationRequested)(sourceMgr, os, &context);
173 result = sourceMgrHandler.verify();
174 }
else if (errorDiagnosticsOnly) {
176 ErrorDiagnosticFilter diagnosticFilter(&context);
177 result = (*translationRequested)(sourceMgr, *stream, &context);
180 result = (*translationRequested)(sourceMgr, *stream, &context);
190 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(dataIn);
197 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.