1 //===- Translation.h - Translation registry ---------------------*- C++ -*-===//
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 // Registry for user-provided translations.
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef MLIR_TRANSLATION_H
13 #define MLIR_TRANSLATION_H
14 
15 #include "llvm/Support/CommandLine.h"
16 
17 namespace llvm {
18 class MemoryBuffer;
19 class SourceMgr;
20 class StringRef;
21 } // namespace llvm
22 
23 namespace mlir {
24 class DialectRegistry;
25 struct LogicalResult;
26 class MLIRContext;
27 class ModuleOp;
28 class OwningModuleRef;
29 
30 /// Interface of the function that translates the sources managed by `sourceMgr`
31 /// to MLIR. The source manager has at least one buffer. The implementation
32 /// should create a new MLIR ModuleOp in the given context and return a pointer
33 /// to it, or a nullptr in case of any error.
34 using TranslateSourceMgrToMLIRFunction =
35     std::function<OwningModuleRef(llvm::SourceMgr &sourceMgr, MLIRContext *)>;
36 
37 /// Interface of the function that translates the given string to MLIR. The
38 /// implementation should create a new MLIR ModuleOp in the given context. If
39 /// source-related error reporting is required from within the function, use
40 /// TranslateSourceMgrToMLIRFunction instead.
41 using TranslateStringRefToMLIRFunction =
42     std::function<OwningModuleRef(llvm::StringRef, MLIRContext *)>;
43 
44 /// Interface of the function that translates MLIR to a different format and
45 /// outputs the result to a stream. It is allowed to modify the module.
46 using TranslateFromMLIRFunction =
47     std::function<LogicalResult(ModuleOp, llvm::raw_ostream &output)>;
48 
49 /// Interface of the function that performs file-to-file translation involving
50 /// MLIR. The input file is held in the given MemoryBuffer; the output file
51 /// should be written to the given raw_ostream. The implementation should create
52 /// all MLIR constructs needed during the process inside the given context. This
53 /// can be used for round-tripping external formats through the MLIR system.
54 using TranslateFunction = std::function<LogicalResult(
55     llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>;
56 
57 /// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that
58 /// registers a function and associates it with name. This requires that a
59 /// translation has not been registered to a given name.
60 ///
61 /// Usage:
62 ///
63 ///   // At file scope.
64 ///   namespace mlir {
65 ///   void registerTRexToMLIRRegistration() {
66 ///     TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... });
67 ///   }
68 ///   } // namespace mlir
69 ///
70 /// \{
71 struct TranslateToMLIRRegistration {
72   TranslateToMLIRRegistration(llvm::StringRef name,
73                               const TranslateSourceMgrToMLIRFunction &function);
74   TranslateToMLIRRegistration(llvm::StringRef name,
75                               const TranslateStringRefToMLIRFunction &function);
76 };
77 
78 struct TranslateFromMLIRRegistration {
79   TranslateFromMLIRRegistration(
80       llvm::StringRef name, const TranslateFromMLIRFunction &function,
81       std::function<void(DialectRegistry &)> dialectRegistration =
82           [](DialectRegistry &) {});
83 };
84 struct TranslateRegistration {
85   TranslateRegistration(llvm::StringRef name,
86                         const TranslateFunction &function);
87 };
88 /// \}
89 
90 /// A command line parser for translation functions.
91 struct TranslationParser : public llvm::cl::parser<const TranslateFunction *> {
92   TranslationParser(llvm::cl::Option &opt);
93 
94   void printOptionInfo(const llvm::cl::Option &o,
95                        size_t globalWidth) const override;
96 };
97 
98 /// Translate to/from an MLIR module from/to an external representation (e.g.
99 /// LLVM IR, SPIRV binary, ...). This is the entry point for the implementation
100 /// of tools like `mlir-translate`. The translation to perform is parsed from
101 /// the command line. The `toolName` argument is used for the header displayed
102 /// by `--help`.
103 LogicalResult mlirTranslateMain(int argc, char **argv,
104                                 llvm::StringRef toolName);
105 
106 } // namespace mlir
107 
108 #endif // MLIR_TRANSLATION_H
109