MLIR  16.0.0git
Translation.h
Go to the documentation of this file.
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 
13 #ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
14 #define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
15 
16 #include "llvm/Support/CommandLine.h"
17 
18 namespace llvm {
19 class MemoryBuffer;
20 class SourceMgr;
21 class StringRef;
22 } // namespace llvm
23 
24 namespace mlir {
25 class DialectRegistry;
26 struct LogicalResult;
27 class MLIRContext;
28 class ModuleOp;
29 template <typename OpTy>
30 class OwningOpRef;
31 
32 /// Interface of the function that translates the sources managed by `sourceMgr`
33 /// to MLIR. The source manager has at least one buffer. The implementation
34 /// should create a new MLIR ModuleOp in the given context and return a pointer
35 /// to it, or a nullptr in case of any error.
36 using TranslateSourceMgrToMLIRFunction = std::function<OwningOpRef<ModuleOp>(
37  llvm::SourceMgr &sourceMgr, MLIRContext *)>;
38 
39 /// Interface of the function that translates the given string to MLIR. The
40 /// implementation should create a new MLIR ModuleOp in the given context. If
41 /// source-related error reporting is required from within the function, use
42 /// TranslateSourceMgrToMLIRFunction instead.
44  std::function<OwningOpRef<ModuleOp>(llvm::StringRef, MLIRContext *)>;
45 
46 /// Interface of the function that translates MLIR to a different format and
47 /// outputs the result to a stream. It is allowed to modify the module.
49  std::function<LogicalResult(ModuleOp, llvm::raw_ostream &output)>;
50 
51 /// Interface of the function that performs file-to-file translation involving
52 /// MLIR. The input file is held in the given MemoryBuffer; the output file
53 /// should be written to the given raw_ostream. The implementation should create
54 /// all MLIR constructs needed during the process inside the given context. This
55 /// can be used for round-tripping external formats through the MLIR system.
56 using TranslateFunction = std::function<LogicalResult(
57  llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>;
58 
59 /// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that
60 /// registers a function and associates it with name. This requires that a
61 /// translation has not been registered to a given name.
62 ///
63 /// Usage:
64 ///
65 /// // At file scope.
66 /// namespace mlir {
67 /// void registerTRexToMLIRRegistration() {
68 /// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... });
69 /// }
70 /// } // namespace mlir
71 ///
72 /// \{
74  TranslateToMLIRRegistration(llvm::StringRef name,
75  const TranslateSourceMgrToMLIRFunction &function);
76  TranslateToMLIRRegistration(llvm::StringRef name,
77  const TranslateStringRefToMLIRFunction &function);
78 };
79 
82  llvm::StringRef name, const TranslateFromMLIRFunction &function,
83  const std::function<void(DialectRegistry &)> &dialectRegistration =
84  [](DialectRegistry &) {});
85 };
87  TranslateRegistration(llvm::StringRef name,
88  const TranslateFunction &function);
89 };
90 /// \}
91 
92 /// A command line parser for translation functions.
93 struct TranslationParser : public llvm::cl::parser<const TranslateFunction *> {
94  TranslationParser(llvm::cl::Option &opt);
95 
96  void printOptionInfo(const llvm::cl::Option &o,
97  size_t globalWidth) const override;
98 };
99 
100 } // namespace mlir
101 
102 #endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
Use Translate[ToMLIR|FromMLIR]Registration as an initializer that registers a function and associates...
Definition: Translation.h:73
Include the generated interface declarations.
std::function< OwningOpRef< ModuleOp >(llvm::SourceMgr &sourceMgr, MLIRContext *)> TranslateSourceMgrToMLIRFunction
Interface of the function that translates the sources managed by sourceMgr to MLIR.
Definition: Translation.h:37
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:221
std::function< OwningOpRef< ModuleOp >(llvm::StringRef, MLIRContext *)> TranslateStringRefToMLIRFunction
Interface of the function that translates the given string to MLIR.
Definition: Translation.h:44
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
A command line parser for translation functions.
Definition: Translation.h:93
std::function< LogicalResult(ModuleOp, llvm::raw_ostream &output)> TranslateFromMLIRFunction
Interface of the function that translates MLIR to a different format and outputs the result to a stre...
Definition: Translation.h:49
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
std::function< LogicalResult(llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)> TranslateFunction
Interface of the function that performs file-to-file translation involving MLIR.
Definition: Translation.h:57