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<std::string> inputSplitMarker{
66 "split-input-file", llvm::cl::ValueOptional,
67 llvm::cl::callback([&](
const std::string &str) {
72 llvm::cl::desc(
"Split the input file into chunks using the given or "
73 "default marker and process each chunk independently"),
76 static llvm::cl::opt<bool> verifyDiagnostics(
78 llvm::cl::desc(
"Check that emitted diagnostics match "
79 "expected-* lines on the corresponding line"),
80 llvm::cl::init(
false));
82 static llvm::cl::opt<bool> errorDiagnosticsOnly(
83 "error-diagnostics-only",
84 llvm::cl::desc(
"Filter all non-error diagnostics "
85 "(discouraged: testing only!)"),
86 llvm::cl::init(
false));
88 static llvm::cl::opt<std::string> outputSplitMarker(
89 "output-split-marker",
90 llvm::cl::desc(
"Split marker to use for merging the ouput"),
93 llvm::InitLLVM y(argc, argv);
96 llvm::cl::list<const Translation *, bool, TranslationParser>
97 translationsRequested(
"", llvm::cl::desc(
"Translations to perform"),
103 llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
110 std::string errorMessage;
111 std::unique_ptr<llvm::MemoryBuffer> input;
112 if (
auto inputAlignment = translationsRequested[0]->getInputAlignment())
113 input =
openInputFile(inputFilename, *inputAlignment, &errorMessage);
117 llvm::errs() << errorMessage <<
"\n";
123 llvm::errs() << errorMessage <<
"\n";
128 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
135 for (
size_t i = 0, e = translationsRequested.size(); i < e; ++i) {
136 llvm::raw_ostream *stream;
137 llvm::raw_string_ostream dataStream(dataOut);
144 stream = &dataStream;
147 const Translation *translationRequested = translationsRequested[i];
154 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
155 sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
157 if (verifyDiagnostics) {
164 (void)(*translationRequested)(sourceMgr, os, &context);
165 result = sourceMgrHandler.verify();
166 }
else if (errorDiagnosticsOnly) {
168 ErrorDiagnosticFilter diagnosticFilter(&context);
169 result = (*translationRequested)(sourceMgr, *stream, &context);
172 result = (*translationRequested)(sourceMgr, *stream, &context);
182 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(dataIn);
189 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.
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.
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 success(bool isSuccess=true)
Utility function to generate a LogicalResult.
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.
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.