16#include "llvm/Support/SourceMgr.h"
17#include "llvm/Support/ToolOutputFile.h"
30 ErrorDiagnosticFilter(MLIRContext *ctx) : ScopedDiagnosticHandler(ctx) {
31 setHandler([](Diagnostic &
diag) {
32 if (
diag.getSeverity() != DiagnosticSeverity::Error)
45 std::unique_ptr<llvm::MemoryBuffer> input, llvm::raw_ostream &output,
48 if (translations.empty()) {
49 llvm::errs() <<
"no translation requested\n";
54 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
59 if (!ownedBuffer->getBuffer().ends_with(
'\0')) {
60 ownedBuffer = llvm::MemoryBuffer::getMemBufferCopy(
61 ownedBuffer->getBuffer(), ownedBuffer->getBufferIdentifier());
66 LogicalResult
result = LogicalResult::success();
68 for (
const auto [
index, translationRequested] :
69 llvm::enumerate(translations)) {
70 llvm::raw_ostream *stream;
71 llvm::raw_string_ostream dataStream(dataOut);
73 if (
index == translations.size() - 1) {
82 timing.
nest(translationRequested->getDescription());
90 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
91 sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
101 (
void)(*translationRequested)(sourceMgr, os, &context);
105 ErrorDiagnosticFilter diagnosticFilter(&context);
106 result = (*translationRequested)(sourceMgr, *stream, &context);
109 result = (*translationRequested)(sourceMgr, *stream, &context);
114 if (
index < translations.size() - 1) {
119 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(dataIn);
132 llvm::raw_ostream &output,
140 llvm::StringRef toolName) {
142 static llvm::cl::opt<std::string> inputFilename(
143 llvm::cl::Positional, llvm::cl::desc(
"<input file>"),
144 llvm::cl::init(
"-"));
146 static llvm::cl::opt<std::string> outputFilename(
147 "o", llvm::cl::desc(
"Output filename"), llvm::cl::value_desc(
"filename"),
148 llvm::cl::init(
"-"));
150 static llvm::cl::opt<bool> allowUnregisteredDialects(
151 "allow-unregistered-dialect",
152 llvm::cl::desc(
"Allow operation with no registered dialects "
153 "(discouraged: testing only!)"),
154 llvm::cl::init(
false));
156 static llvm::cl::opt<std::string> inputSplitMarker{
157 "split-input-file", llvm::cl::ValueOptional,
158 llvm::cl::callback([&](
const std::string &str) {
163 llvm::cl::desc(
"Split the input file into chunks using the given or "
164 "default marker and process each chunk independently"),
167 static llvm::cl::opt<SourceMgrDiagnosticVerifierHandler::Level>
169 "verify-diagnostics", llvm::cl::ValueOptional,
170 llvm::cl::desc(
"Check that emitted diagnostics match expected-* "
171 "lines on the corresponding line"),
175 "Check all diagnostics (expected, unexpected, near-misses)"),
180 "Check all diagnostics (expected, unexpected, near-misses)"),
183 "only-expected",
"Check only expected diagnostics"))};
185 static llvm::cl::opt<bool> errorDiagnosticsOnly(
186 "error-diagnostics-only",
187 llvm::cl::desc(
"Filter all non-error diagnostics "
188 "(discouraged: testing only!)"),
189 llvm::cl::init(
false));
191 static llvm::cl::opt<std::string> outputSplitMarker(
192 "output-split-marker",
193 llvm::cl::desc(
"Split marker to use for merging the ouput"),
197 llvm::cl::list<const Translation *, bool, TranslationParser>
198 translationsRequested(
"", llvm::cl::desc(
"Translations to perform"),
204 llvm::cl::ParseCommandLineOptions(argc, argv, toolName);
211 std::string errorMessage;
212 std::unique_ptr<llvm::MemoryBuffer> input;
213 if (
auto inputAlignment = translationsRequested[0]->getInputAlignment())
214 input =
openInputFile(inputFilename, *inputAlignment, &errorMessage);
218 llvm::errs() << errorMessage <<
"\n";
224 llvm::errs() << errorMessage <<
"\n";
232 if (inputSplitMarker.getNumOccurrences())
234 if (verifyDiagnostics.getNumOccurrences())
238 translationsRequested, config,
static LogicalResult processBuffer(raw_ostream &os, std::unique_ptr< MemoryBuffer > ownedBuffer, llvm::MemoryBufferRef sourceBuffer, const MlirOptMainConfig &config, DialectRegistry ®istry, SourceMgrDiagnosticVerifierHandler *verifyHandler, llvm::ThreadPoolInterface *threadPool)
Parses the memory buffer.
static LogicalResult mlirTranslateMainImpl(std::unique_ptr< llvm::MemoryBuffer > input, llvm::raw_ostream &output, ArrayRef< const Translation * > translations, const MlirTranslateMainConfig &config, TimingScope timing)
static std::string diag(const llvm::Value &value)
Facilities for time measurement and report printing to an output stream.
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.
Configuration options for the mlir-translate driver.
MlirTranslateMainConfig & outputSplitMarker(std::string splitMarker)
MlirTranslateMainConfig & splitInputFile(std::string splitMarker=kDefaultSplitMarker)
StringRef getOutputSplitMarker() const
StringRef getInputSplitMarker() const
bool shouldEmitErrorDiagnosticsOnly() const
MlirTranslateMainConfig & verifyDiagnostics(SourceMgrDiagnosticVerifierHandler::Level level)
SourceMgrDiagnosticVerifierHandler::Level getVerifyDiagnosticsLevel() const
MlirTranslateMainConfig & errorDiagnosticsOnly(bool errorOnly)
MlirTranslateMainConfig & allowUnregisteredDialects(bool allow)
bool shouldAllowUnregisteredDialects() const
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...
LogicalResult verify()
Returns the status of the handler and verifies that all expected diagnostics were emitted.
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.
Include the generated interface declarations.
LogicalResult mlirTranslateMain(std::unique_ptr< llvm::MemoryBuffer > input, llvm::raw_ostream &output, ArrayRef< const Translation * > translations, const MlirTranslateMainConfig &config={})
Apply the requested translations to an input buffer and write the result to the output stream.
const char *const kDefaultSplitMarker
void registerDefaultTimingManagerCLOptions()
Register a set of useful command-line options that can be used to configure a DefaultTimingManager.
std::unique_ptr< llvm::ToolOutputFile > openOutputFile(llvm::StringRef outputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for writing.
void registerTranslationCLOptions()
Register command-line options used by the translation registry.
void registerMLIRContextCLOptions()
Register a set of useful command-line options that can be used to configure various flags within the ...
std::unique_ptr< llvm::MemoryBuffer > openInputFile(llvm::StringRef inputFilename, std::string *errorMessage=nullptr)
Open the file specified by its name for reading.
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...