-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
Translation.h
153 lines (131 loc) · 6.11 KB
/
Translation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//===- Translation.h - Translation registry ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Registry for user-provided translations.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
#define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H
#include "mlir/IR/Operation.h"
#include "llvm/Support/CommandLine.h"
namespace mlir {
template <typename OpTy>
class OwningOpRef;
/// Interface of the function that translates the sources managed by `sourceMgr`
/// to MLIR. The source manager has at least one buffer. The implementation
/// should create a new MLIR Operation in the given context and return a
/// pointer to it, or a nullptr in case of any error.
using TranslateSourceMgrToMLIRFunction = std::function<OwningOpRef<Operation *>(
llvm::SourceMgr &sourceMgr, MLIRContext *)>;
/// Interface of the function that translates the given string to MLIR. The
/// implementation should create a new MLIR Operation in the given context. If
/// source-related error reporting is required from within the function, use
/// TranslateSourceMgrToMLIRFunction instead.
using TranslateStringRefToMLIRFunction =
std::function<OwningOpRef<Operation *>(llvm::StringRef, MLIRContext *)>;
/// Interface of the function that translates MLIR to a different format and
/// outputs the result to a stream. It is allowed to modify the operation.
using TranslateFromMLIRFunction =
std::function<LogicalResult(Operation *, llvm::raw_ostream &output)>;
/// Interface of the function that performs file-to-file translation involving
/// MLIR. The input file is held in the given MemoryBuffer; the output file
/// should be written to the given raw_ostream. The implementation should create
/// all MLIR constructs needed during the process inside the given context. This
/// can be used for round-tripping external formats through the MLIR system.
using TranslateFunction = std::function<LogicalResult(
llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>;
/// This class contains all of the components necessary for performing a
/// translation.
class Translation {
public:
Translation() = default;
Translation(TranslateFunction function, StringRef description,
Optional<llvm::Align> inputAlignment)
: function(std::move(function)), description(description),
inputAlignment(inputAlignment) {}
/// Return the description of this translation.
StringRef getDescription() const { return description; }
/// Return the optional alignment desired for the input of the translation.
Optional<llvm::Align> getInputAlignment() const { return inputAlignment; }
/// Invoke the translation function with the given input and output streams.
LogicalResult operator()(llvm::SourceMgr &sourceMgr,
llvm::raw_ostream &output,
MLIRContext *context) const {
return function(sourceMgr, output, context);
}
private:
/// The underlying translation function.
TranslateFunction function;
/// The description of the translation.
StringRef description;
/// An optional alignment desired for the input of the translation.
Optional<llvm::Align> inputAlignment;
};
/// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that
/// registers a function and associates it with name. This requires that a
/// translation has not been registered to a given name. `inputAlign` is an
/// optional expected alignment for the input data.
///
/// Usage:
///
/// // At file scope.
/// namespace mlir {
/// void registerTRexToMLIRRegistration() {
/// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... });
/// }
/// } // namespace mlir
///
/// \{
struct TranslateToMLIRRegistration {
TranslateToMLIRRegistration(
llvm::StringRef name, llvm::StringRef description,
const TranslateSourceMgrToMLIRFunction &function,
Optional<llvm::Align> inputAlignment = llvm::None);
TranslateToMLIRRegistration(
llvm::StringRef name, llvm::StringRef description,
const TranslateStringRefToMLIRFunction &function,
Optional<llvm::Align> inputAlignment = llvm::None);
};
struct TranslateFromMLIRRegistration {
TranslateFromMLIRRegistration(
llvm::StringRef name, llvm::StringRef description,
const TranslateFromMLIRFunction &function,
const std::function<void(DialectRegistry &)> &dialectRegistration =
[](DialectRegistry &) {});
template <typename FuncTy, typename OpTy = detail::first_argument<FuncTy>,
typename = std::enable_if_t<!std::is_same_v<OpTy, Operation *>>>
TranslateFromMLIRRegistration(
llvm::StringRef name, llvm::StringRef description, FuncTy function,
const std::function<void(DialectRegistry &)> &dialectRegistration =
[](DialectRegistry &) {})
: TranslateFromMLIRRegistration(
name, description,
[function](Operation *op, raw_ostream &os) -> LogicalResult {
if (auto casted = dyn_cast<OpTy>(op))
return function(casted, os);
return emitError(op->getLoc())
<< "expected a '" << OpTy::getOperationName()
<< "' op, got '" << op->getName().getStringRef() << "'";
},
dialectRegistration){}
};
struct TranslateRegistration {
TranslateRegistration(llvm::StringRef name, llvm::StringRef description,
const TranslateFunction &function);
};
/// \}
/// A command line parser for translation functions.
struct TranslationParser : public llvm::cl::parser<const Translation *> {
TranslationParser(llvm::cl::Option &opt);
void printOptionInfo(const llvm::cl::Option &o,
size_t globalWidth) const override;
};
/// Register command-line options used by the translation registry.
void registerTranslationCLOptions();
} // namespace mlir
#endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H