1//===- MlirTranslateMain.cpp - MLIR Translation entry point ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "mlir/Tools/mlir-translate/MlirTranslateMain.h"
10#include "mlir/IR/AsmState.h"
11#include "mlir/Parser/Parser.h"
12#include "mlir/Support/FileUtilities.h"
13#include "mlir/Support/Timing.h"
14#include "mlir/Support/ToolUtilities.h"
15#include "mlir/Tools/mlir-translate/Translation.h"
16#include "llvm/Support/SourceMgr.h"
17#include "llvm/Support/ToolOutputFile.h"
18
19using namespace mlir;
20
21//===----------------------------------------------------------------------===//
22// Diagnostic Filter
23//===----------------------------------------------------------------------===//
24
25namespace {
26/// A scoped diagnostic handler that marks non-error diagnostics as handled. As
27/// a result, the main diagnostic handler does not print non-error diagnostics.
28class ErrorDiagnosticFilter : public ScopedDiagnosticHandler {
29public:
30 ErrorDiagnosticFilter(MLIRContext *ctx) : ScopedDiagnosticHandler(ctx) {
31 setHandler([](Diagnostic &diag) {
32 if (diag.getSeverity() != DiagnosticSeverity::Error)
33 return success();
34 return failure();
35 });
36 }
37};
38} // namespace
39
40//===----------------------------------------------------------------------===//
41// Translate Entry Point
42//===----------------------------------------------------------------------===//
43
44static LogicalResult mlirTranslateMainImpl(
45 std::unique_ptr<llvm::MemoryBuffer> input, llvm::raw_ostream &output,
46 ArrayRef<const Translation *> translations,
47 const MlirTranslateMainConfig &config, TimingScope timing) {
48 if (translations.empty()) {
49 llvm::errs() << "no translation requested\n";
50 return failure();
51 }
52
53 // Processes the memory buffer with a new MLIRContext.
54 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
55 raw_ostream &os) {
56 // Many of the translations expect a null-terminated buffer while splitting
57 // the buffer does not guarantee null-termination. Make a copy of the buffer
58 // to ensure null-termination.
59 if (!ownedBuffer->getBuffer().ends_with(Suffix: '\0')) {
60 ownedBuffer = llvm::MemoryBuffer::getMemBufferCopy(
61 InputData: ownedBuffer->getBuffer(), BufferName: ownedBuffer->getBufferIdentifier());
62 }
63 // Temporary buffers for chained translation processing.
64 std::string dataIn;
65 std::string dataOut;
66 LogicalResult result = LogicalResult::success();
67
68 for (const auto [index, translationRequested] :
69 llvm::enumerate(First&: translations)) {
70 llvm::raw_ostream *stream;
71 llvm::raw_string_ostream dataStream(dataOut);
72
73 if (index == translations.size() - 1) {
74 // Output last translation to output.
75 stream = &os;
76 } else {
77 // Output translation to temporary data buffer.
78 stream = &dataStream;
79 }
80
81 TimingScope translationTiming =
82 timing.nest(args: translationRequested->getDescription());
83
84 MLIRContext context;
85 context.allowUnregisteredDialects(
86 allow: config.shouldAllowUnregisteredDialects());
87 context.printOpOnDiagnostic(
88 enable: config.getVerifyDiagnosticsLevel() ==
89 SourceMgrDiagnosticVerifierHandler::Level::None);
90 auto sourceMgr = std::make_shared<llvm::SourceMgr>();
91 sourceMgr->AddNewSourceBuffer(F: std::move(ownedBuffer), IncludeLoc: SMLoc());
92
93 if (config.getVerifyDiagnosticsLevel() !=
94 SourceMgrDiagnosticVerifierHandler::Level::None) {
95 // In the diagnostic verification flow, we ignore whether the
96 // translation failed (in most cases, it is expected to fail) and we do
97 // not filter non-error diagnostics even if `errorDiagnosticsOnly` is
98 // set. Instead, we check if the diagnostics were produced as expected.
99 SourceMgrDiagnosticVerifierHandler sourceMgrHandler(
100 *sourceMgr, &context, config.getVerifyDiagnosticsLevel());
101 (void)(*translationRequested)(sourceMgr, os, &context);
102 result = sourceMgrHandler.verify();
103 } else if (config.shouldEmitErrorDiagnosticsOnly()) {
104 SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
105 ErrorDiagnosticFilter diagnosticFilter(&context);
106 result = (*translationRequested)(sourceMgr, *stream, &context);
107 } else {
108 SourceMgrDiagnosticHandler sourceMgrHandler(*sourceMgr, &context);
109 result = (*translationRequested)(sourceMgr, *stream, &context);
110 }
111 if (failed(Result: result))
112 return result;
113
114 if (index < translations.size() - 1) {
115 // If there are further translations, create a new buffer with the
116 // output data.
117 dataIn = dataOut;
118 dataOut.clear();
119 ownedBuffer = llvm::MemoryBuffer::getMemBuffer(InputData: dataIn);
120 }
121 }
122 return result;
123 };
124
125 return splitAndProcessBuffer(originalBuffer: std::move(input), processChunkBuffer: processBuffer, os&: output,
126 inputSplitMarker: config.getInputSplitMarker(),
127 outputSplitMarker: config.getOutputSplitMarker());
128}
129
130LogicalResult
131mlir::mlirTranslateMain(std::unique_ptr<llvm::MemoryBuffer> input,
132 llvm::raw_ostream &output,
133 ArrayRef<const Translation *> translations,
134 const MlirTranslateMainConfig &config) {
135 return mlirTranslateMainImpl(input: std::move(input), output, translations, config,
136 timing: TimingScope());
137}
138
139LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
140 llvm::StringRef toolName) {
141
142 static llvm::cl::opt<std::string> inputFilename(
143 llvm::cl::Positional, llvm::cl::desc("<input file>"),
144 llvm::cl::init(Val: "-"));
145
146 static llvm::cl::opt<std::string> outputFilename(
147 "o", llvm::cl::desc("Output filename"), llvm::cl::value_desc("filename"),
148 llvm::cl::init(Val: "-"));
149
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(Val: false));
155
156 static llvm::cl::opt<std::string> inputSplitMarker{
157 "split-input-file", llvm::cl::ValueOptional,
158 llvm::cl::callback(CB: [&](const std::string &str) {
159 // Implicit value: use default marker if flag was used without value.
160 if (str.empty())
161 inputSplitMarker.setValue(V: kDefaultSplitMarker);
162 }),
163 llvm::cl::desc("Split the input file into chunks using the given or "
164 "default marker and process each chunk independently"),
165 llvm::cl::init(Val: "")};
166
167 static llvm::cl::opt<SourceMgrDiagnosticVerifierHandler::Level>
168 verifyDiagnostics{
169 "verify-diagnostics", llvm::cl::ValueOptional,
170 llvm::cl::desc("Check that emitted diagnostics match expected-* "
171 "lines on the corresponding line"),
172 llvm::cl::values(
173 clEnumValN(
174 SourceMgrDiagnosticVerifierHandler::Level::All, "all",
175 "Check all diagnostics (expected, unexpected, near-misses)"),
176 // Implicit value: when passed with no arguments, e.g.
177 // `--verify-diagnostics` or `--verify-diagnostics=`.
178 clEnumValN(
179 SourceMgrDiagnosticVerifierHandler::Level::All, "",
180 "Check all diagnostics (expected, unexpected, near-misses)"),
181 clEnumValN(
182 SourceMgrDiagnosticVerifierHandler::Level::OnlyExpected,
183 "only-expected", "Check only expected diagnostics"))};
184
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(Val: false));
190
191 static llvm::cl::opt<std::string> outputSplitMarker(
192 "output-split-marker",
193 llvm::cl::desc("Split marker to use for merging the ouput"),
194 llvm::cl::init(Val: ""));
195
196 // Add flags for all the registered translations.
197 llvm::cl::list<const Translation *, bool, TranslationParser>
198 translationsRequested("", llvm::cl::desc("Translations to perform"),
199 llvm::cl::Required);
200 registerAsmPrinterCLOptions();
201 registerMLIRContextCLOptions();
202 registerTranslationCLOptions();
203 registerDefaultTimingManagerCLOptions();
204 llvm::cl::parseCommandLineOptions(Context&: getTranslationLibraryOptions(),
205 Args: ArrayRef(argv, argc), Overview: toolName);
206
207 // Initialize the timing manager.
208 DefaultTimingManager tm;
209 applyDefaultTimingManagerCLOptions(tm);
210 TimingScope timing = tm.getRootScope();
211
212 std::string errorMessage;
213 std::unique_ptr<llvm::MemoryBuffer> input;
214 if (auto inputAlignment = translationsRequested[0]->getInputAlignment())
215 input = openInputFile(inputFilename, alignment: *inputAlignment, errorMessage: &errorMessage);
216 else
217 input = openInputFile(inputFilename, errorMessage: &errorMessage);
218 if (!input) {
219 llvm::errs() << errorMessage << "\n";
220 return failure();
221 }
222
223 auto output = openOutputFile(outputFilename, errorMessage: &errorMessage);
224 if (!output) {
225 llvm::errs() << errorMessage << "\n";
226 return failure();
227 }
228
229 MlirTranslateMainConfig config;
230 config.allowUnregisteredDialects(allow: allowUnregisteredDialects)
231 .errorDiagnosticsOnly(errorOnly: errorDiagnosticsOnly)
232 .outputSplitMarker(splitMarker: outputSplitMarker);
233 if (inputSplitMarker.getNumOccurrences())
234 config.splitInputFile(splitMarker: inputSplitMarker);
235 if (verifyDiagnostics.getNumOccurrences())
236 config.verifyDiagnostics(level: verifyDiagnostics);
237
238 if (failed(Result: mlirTranslateMainImpl(input: std::move(input), output&: output->os(),
239 translations: translationsRequested, config,
240 timing: std::move(timing))))
241 return failure();
242
243 output->keep();
244 return success();
245}
246