1//===- Translation.cpp - Translation registry -----------------------------===//
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// Definitions of the translation registry.
10//
11//===----------------------------------------------------------------------===//
12
13#include "mlir/Tools/mlir-translate/Translation.h"
14#include "mlir/IR/AsmState.h"
15#include "mlir/IR/Verifier.h"
16#include "mlir/Parser/Parser.h"
17#include "mlir/Tools/ParseUtilities.h"
18#include "llvm/Support/LibraryOptionsContext.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/SourceMgr.h"
21#include <optional>
22
23using namespace mlir;
24
25//===----------------------------------------------------------------------===//
26// Translation CommandLine Options
27//===----------------------------------------------------------------------===//
28
29struct TranslationOptions {
30 llvm::cl::opt<bool> noImplicitModule{
31 "no-implicit-module",
32 llvm::cl::desc("Disable the parsing of an implicit top-level module op"),
33 llvm::cl::init(Val: false)};
34};
35
36static llvm::ManagedStatic<TranslationOptions> clOptions;
37
38void mlir::registerTranslationCLOptions() { *clOptions; }
39
40llvm::cl::LibraryOptionsContext &mlir::getTranslationLibraryOptions() {
41 static llvm::cl::LibraryOptionsContext options;
42 return options;
43}
44
45//===----------------------------------------------------------------------===//
46// Translation Registry
47//===----------------------------------------------------------------------===//
48
49/// Get the mutable static map between registered file-to-file MLIR
50/// translations.
51static llvm::StringMap<Translation> &getTranslationRegistry() {
52 static llvm::StringMap<Translation> translationBundle;
53 return translationBundle;
54}
55
56/// Register the given translation.
57static void registerTranslation(StringRef name, StringRef description,
58 std::optional<llvm::Align> inputAlignment,
59 const TranslateFunction &function) {
60 auto &registry = getTranslationRegistry();
61 if (registry.count(Key: name))
62 llvm::report_fatal_error(
63 reason: "Attempting to overwrite an existing <file-to-file> function");
64 assert(function &&
65 "Attempting to register an empty translate <file-to-file> function");
66 registry[name] = Translation(function, description, inputAlignment);
67}
68
69TranslateRegistration::TranslateRegistration(
70 StringRef name, StringRef description, const TranslateFunction &function) {
71 registerTranslation(name, description, /*inputAlignment=*/std::nullopt,
72 function);
73}
74
75//===----------------------------------------------------------------------===//
76// Translation to MLIR
77//===----------------------------------------------------------------------===//
78
79// Puts `function` into the to-MLIR translation registry unless there is already
80// a function registered for the same name.
81static void registerTranslateToMLIRFunction(
82 StringRef name, StringRef description,
83 const DialectRegistrationFunction &dialectRegistration,
84 std::optional<llvm::Align> inputAlignment,
85 const TranslateSourceMgrToMLIRFunction &function) {
86 auto wrappedFn = [function, dialectRegistration](
87 const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
88 raw_ostream &output, MLIRContext *context) {
89 DialectRegistry registry;
90 dialectRegistration(registry);
91 context->appendDialectRegistry(registry);
92 OwningOpRef<Operation *> op = function(sourceMgr, context);
93 if (!op || failed(Result: verify(op: *op)))
94 return failure();
95 op.get()->print(os&: output);
96 return success();
97 };
98 registerTranslation(name, description, inputAlignment, function: wrappedFn);
99}
100
101TranslateToMLIRRegistration::TranslateToMLIRRegistration(
102 StringRef name, StringRef description,
103 const TranslateSourceMgrToMLIRFunction &function,
104 const DialectRegistrationFunction &dialectRegistration,
105 std::optional<llvm::Align> inputAlignment) {
106 registerTranslateToMLIRFunction(name, description, dialectRegistration,
107 inputAlignment, function);
108}
109TranslateToMLIRRegistration::TranslateToMLIRRegistration(
110 StringRef name, StringRef description,
111 const TranslateRawSourceMgrToMLIRFunction &function,
112 const DialectRegistrationFunction &dialectRegistration,
113 std::optional<llvm::Align> inputAlignment) {
114 registerTranslateToMLIRFunction(
115 name, description, dialectRegistration, inputAlignment,
116 function: [function](const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
117 MLIRContext *ctx) { return function(*sourceMgr, ctx); });
118}
119/// Wraps `function` with a lambda that extracts a StringRef from a source
120/// manager and registers the wrapper lambda as a to-MLIR conversion.
121TranslateToMLIRRegistration::TranslateToMLIRRegistration(
122 StringRef name, StringRef description,
123 const TranslateStringRefToMLIRFunction &function,
124 const DialectRegistrationFunction &dialectRegistration,
125 std::optional<llvm::Align> inputAlignment) {
126 registerTranslateToMLIRFunction(
127 name, description, dialectRegistration, inputAlignment,
128 function: [function](const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
129 MLIRContext *ctx) {
130 const llvm::MemoryBuffer *buffer =
131 sourceMgr->getMemoryBuffer(i: sourceMgr->getMainFileID());
132 return function(buffer->getBuffer(), ctx);
133 });
134}
135
136//===----------------------------------------------------------------------===//
137// Translation from MLIR
138//===----------------------------------------------------------------------===//
139
140TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
141 StringRef name, StringRef description,
142 const TranslateFromMLIRFunction &function,
143 const DialectRegistrationFunction &dialectRegistration) {
144 registerTranslation(
145 name, description, /*inputAlignment=*/std::nullopt,
146 function: [function,
147 dialectRegistration](const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
148 raw_ostream &output, MLIRContext *context) {
149 DialectRegistry registry;
150 dialectRegistration(registry);
151 context->appendDialectRegistry(registry);
152 bool implicitModule =
153 (!clOptions.isConstructed() || !clOptions->noImplicitModule);
154 OwningOpRef<Operation *> op =
155 parseSourceFileForTool(sourceMgr, config: context, insertImplicitModule: implicitModule);
156 if (!op || failed(Result: verify(op: *op)))
157 return failure();
158 return function(op.get(), output);
159 });
160}
161
162//===----------------------------------------------------------------------===//
163// Translation Parser
164//===----------------------------------------------------------------------===//
165
166TranslationParser::TranslationParser(llvm::cl::Option &opt)
167 : llvm::cl::parser<const Translation *>(opt) {
168 for (const auto &kv : getTranslationRegistry())
169 addLiteralOption(Name: kv.first(), V: &kv.second, HelpStr: kv.second.getDescription());
170}
171
172void TranslationParser::printOptionInfo(const llvm::cl::Option &o,
173 size_t globalWidth) const {
174 TranslationParser *tp = const_cast<TranslationParser *>(this);
175 llvm::array_pod_sort(Start: tp->Values.begin(), End: tp->Values.end(),
176 Compare: [](const TranslationParser::OptionInfo *lhs,
177 const TranslationParser::OptionInfo *rhs) {
178 return lhs->Name.compare(RHS: rhs->Name);
179 });
180 llvm::cl::parser<const Translation *>::printOptionInfo(O: o, GlobalWidth: globalWidth);
181}
182