1 //===- standalone-translate.cpp ---------------------------------*- C++ -*-===//
2 //
3 // This file is licensed 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 // This is a command line utility that translates a file from/to MLIR using one
10 // of the registered translations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "mlir/IR/AsmState.h"
15 #include "mlir/IR/Diagnostics.h"
16 #include "mlir/IR/MLIRContext.h"
17 #include "mlir/InitAllDialects.h"
18 #include "mlir/InitAllTranslations.h"
19 #include "mlir/Support/FileUtilities.h"
20 #include "mlir/Support/LogicalResult.h"
21 #include "mlir/Support/ToolUtilities.h"
22 #include "mlir/Translation.h"
23 #include "llvm/Support/InitLLVM.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/ToolOutputFile.h"
27
28 #include "Standalone/StandaloneDialect.h"
29
30 static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
31 llvm::cl::desc("<input file>"),
32 llvm::cl::init("-"));
33
34 static llvm::cl::opt<std::string>
35 outputFilename("o", llvm::cl::desc("Output filename"),
36 llvm::cl::value_desc("filename"), llvm::cl::init("-"));
37
38 static llvm::cl::opt<bool>
39 splitInputFile("split-input-file",
40 llvm::cl::desc("Split the input file into pieces and "
41 "process each chunk independently"),
42 llvm::cl::init(false));
43
44 static llvm::cl::opt<bool> verifyDiagnostics(
45 "verify-diagnostics",
46 llvm::cl::desc("Check that emitted diagnostics match "
47 "expected-* lines on the corresponding line"),
48 llvm::cl::init(false));
49
main(int argc,char ** argv)50 int main(int argc, char **argv) {
51 mlir::registerAllDialects();
52 mlir::registerAllTranslations();
53
54 mlir::registerDialect<mlir::standalone::StandaloneDialect>();
55 // TODO: Register standalone translations here.
56
57 llvm::InitLLVM y(argc, argv);
58
59 // Add flags for all the registered translations.
60 llvm::cl::opt<const mlir::TranslateFunction *, false, mlir::TranslationParser>
61 translationRequested("", llvm::cl::desc("Translation to perform"),
62 llvm::cl::Required);
63 mlir::registerAsmPrinterCLOptions();
64 mlir::registerMLIRContextCLOptions();
65 llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR translation driver\n");
66
67 std::string errorMessage;
68 auto input = mlir::openInputFile(inputFilename, &errorMessage);
69 if (!input) {
70 llvm::errs() << errorMessage << "\n";
71 return 1;
72 }
73
74 auto output = mlir::openOutputFile(outputFilename, &errorMessage);
75 if (!output) {
76 llvm::errs() << errorMessage << "\n";
77 return 1;
78 }
79
80 // Processes the memory buffer with a new MLIRContext.
81 auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
82 llvm::raw_ostream &os) {
83 mlir::MLIRContext context;
84 context.allowUnregisteredDialects();
85 context.printOpOnDiagnostic(!verifyDiagnostics);
86 llvm::SourceMgr sourceMgr;
87 sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), llvm::SMLoc());
88
89 if (!verifyDiagnostics) {
90 mlir::SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
91 return (*translationRequested)(sourceMgr, os, &context);
92 }
93
94 // In the diagnostic verification flow, we ignore whether the translation
95 // failed (in most cases, it is expected to fail). Instead, we check if the
96 // diagnostics were produced as expected.
97 mlir::SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr,
98 &context);
99 (*translationRequested)(sourceMgr, os, &context);
100 return sourceMgrHandler.verify();
101 };
102
103 if (splitInputFile) {
104 if (failed(mlir::splitAndProcessBuffer(std::move(input), processBuffer,
105 output->os())))
106 return 1;
107 } else {
108 if (failed(processBuffer(std::move(input), output->os())))
109 return 1;
110 }
111
112 output->keep();
113 return 0;
114 }
115