1 //===- mlir-translate.cpp - MLIR Translate Driver -------------------------===//
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 // 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 using namespace mlir;
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 
50 namespace mlir {
51 // Defined in the test directory, no public header.
52 void registerTestRoundtripSPIRV();
53 void registerTestRoundtripDebugSPIRV();
54 } // namespace mlir
55 
registerTestTranslations()56 static void registerTestTranslations() {
57   registerTestRoundtripSPIRV();
58   registerTestRoundtripDebugSPIRV();
59 }
60 
main(int argc,char ** argv)61 int main(int argc, char **argv) {
62   registerAllDialects();
63   registerAllTranslations();
64   registerTestTranslations();
65   llvm::InitLLVM y(argc, argv);
66 
67   // Add flags for all the registered translations.
68   llvm::cl::opt<const TranslateFunction *, false, TranslationParser>
69       translationRequested("", llvm::cl::desc("Translation to perform"),
70                            llvm::cl::Required);
71   registerAsmPrinterCLOptions();
72   registerMLIRContextCLOptions();
73   llvm::cl::ParseCommandLineOptions(argc, argv, "MLIR translation driver\n");
74 
75   std::string errorMessage;
76   auto input = openInputFile(inputFilename, &errorMessage);
77   if (!input) {
78     llvm::errs() << errorMessage << "\n";
79     return 1;
80   }
81 
82   auto output = openOutputFile(outputFilename, &errorMessage);
83   if (!output) {
84     llvm::errs() << errorMessage << "\n";
85     return 1;
86   }
87 
88   // Processes the memory buffer with a new MLIRContext.
89   auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
90                            raw_ostream &os) {
91     MLIRContext context;
92     context.allowUnregisteredDialects();
93     context.printOpOnDiagnostic(!verifyDiagnostics);
94     llvm::SourceMgr sourceMgr;
95     sourceMgr.AddNewSourceBuffer(std::move(ownedBuffer), llvm::SMLoc());
96 
97     if (!verifyDiagnostics) {
98       SourceMgrDiagnosticHandler sourceMgrHandler(sourceMgr, &context);
99       return (*translationRequested)(sourceMgr, os, &context);
100     }
101 
102     // In the diagnostic verification flow, we ignore whether the translation
103     // failed (in most cases, it is expected to fail). Instead, we check if the
104     // diagnostics were produced as expected.
105     SourceMgrDiagnosticVerifierHandler sourceMgrHandler(sourceMgr, &context);
106     (*translationRequested)(sourceMgr, os, &context);
107     return sourceMgrHandler.verify();
108   };
109 
110   if (splitInputFile) {
111     if (failed(splitAndProcessBuffer(std::move(input), processBuffer,
112                                      output->os())))
113       return 1;
114   } else {
115     if (failed(processBuffer(std::move(input), output->os())))
116       return 1;
117   }
118 
119   output->keep();
120   return 0;
121 }
122