1 //===- Translation.cpp - Translation registry -----------------------------===//
2 //
3 // Part of the MLIR 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/Translation.h"
14 #include "mlir/IR/Module.h"
15 #include "mlir/Support/LLVM.h"
16 #include "mlir/Support/LogicalResult.h"
17 #include "llvm/Support/SourceMgr.h"
18
19 using namespace mlir;
20
21 // Get the mutable static map between registered "to MLIR" translations and the
22 // TranslateToMLIRFunctions that perform those translations.
23 static llvm::StringMap<TranslateSourceMgrToMLIRFunction> &
getMutableTranslationToMLIRRegistry()24 getMutableTranslationToMLIRRegistry() {
25 static llvm::StringMap<TranslateSourceMgrToMLIRFunction>
26 translationToMLIRRegistry;
27 return translationToMLIRRegistry;
28 }
29 // Get the mutable static map between registered "from MLIR" translations and
30 // the TranslateFromMLIRFunctions that perform those translations.
31 static llvm::StringMap<TranslateFromMLIRFunction> &
getMutableTranslationFromMLIRRegistry()32 getMutableTranslationFromMLIRRegistry() {
33 static llvm::StringMap<TranslateFromMLIRFunction> translationFromMLIRRegistry;
34 return translationFromMLIRRegistry;
35 }
36
37 // Get the mutable static map between registered file-to-file MLIR translations
38 // and the TranslateFunctions that perform those translations.
getMutableTranslationRegistry()39 static llvm::StringMap<TranslateFunction> &getMutableTranslationRegistry() {
40 static llvm::StringMap<TranslateFunction> translationRegistry;
41 return translationRegistry;
42 }
43
44 // Puts `function` into the to-MLIR translation registry unless there is already
45 // a function registered for the same name.
registerTranslateToMLIRFunction(StringRef name,const TranslateSourceMgrToMLIRFunction & function)46 static void registerTranslateToMLIRFunction(
47 StringRef name, const TranslateSourceMgrToMLIRFunction &function) {
48 auto &translationToMLIRRegistry = getMutableTranslationToMLIRRegistry();
49 if (translationToMLIRRegistry.find(name) != translationToMLIRRegistry.end())
50 llvm::report_fatal_error(
51 "Attempting to overwrite an existing <to> function");
52 assert(function && "Attempting to register an empty translate <to> function");
53 translationToMLIRRegistry[name] = function;
54 }
55
TranslateToMLIRRegistration(StringRef name,const TranslateSourceMgrToMLIRFunction & function)56 TranslateToMLIRRegistration::TranslateToMLIRRegistration(
57 StringRef name, const TranslateSourceMgrToMLIRFunction &function) {
58 registerTranslateToMLIRFunction(name, function);
59 }
60
61 // Wraps `function` with a lambda that extracts a StringRef from a source
62 // manager and registers the wrapper lambda as a to-MLIR conversion.
TranslateToMLIRRegistration(StringRef name,const TranslateStringRefToMLIRFunction & function)63 TranslateToMLIRRegistration::TranslateToMLIRRegistration(
64 StringRef name, const TranslateStringRefToMLIRFunction &function) {
65 auto translationFunction = [function](llvm::SourceMgr &sourceMgr,
66 MLIRContext *ctx) {
67 const llvm::MemoryBuffer *buffer =
68 sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
69 return function(buffer->getBuffer(), ctx);
70 };
71 registerTranslateToMLIRFunction(name, translationFunction);
72 }
73
TranslateFromMLIRRegistration(StringRef name,const TranslateFromMLIRFunction & function)74 TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
75 StringRef name, const TranslateFromMLIRFunction &function) {
76 auto &translationFromMLIRRegistry = getMutableTranslationFromMLIRRegistry();
77 if (translationFromMLIRRegistry.find(name) !=
78 translationFromMLIRRegistry.end())
79 llvm::report_fatal_error(
80 "Attempting to overwrite an existing <from> function");
81 assert(function &&
82 "Attempting to register an empty translate <from> function");
83 translationFromMLIRRegistry[name] = function;
84 }
85
TranslateRegistration(StringRef name,const TranslateFunction & function)86 TranslateRegistration::TranslateRegistration(
87 StringRef name, const TranslateFunction &function) {
88 auto &translationRegistry = getMutableTranslationRegistry();
89 if (translationRegistry.find(name) != translationRegistry.end())
90 llvm::report_fatal_error(
91 "Attempting to overwrite an existing <file-to-file> function");
92 assert(function &&
93 "Attempting to register an empty translate <file-to-file> function");
94 translationRegistry[name] = function;
95 }
96
97 // Merely add the const qualifier to the mutable registry so that external users
98 // cannot modify it.
99 const llvm::StringMap<TranslateSourceMgrToMLIRFunction> &
getTranslationToMLIRRegistry()100 mlir::getTranslationToMLIRRegistry() {
101 return getMutableTranslationToMLIRRegistry();
102 }
103
104 const llvm::StringMap<TranslateFromMLIRFunction> &
getTranslationFromMLIRRegistry()105 mlir::getTranslationFromMLIRRegistry() {
106 return getMutableTranslationFromMLIRRegistry();
107 }
108
getTranslationRegistry()109 const llvm::StringMap<TranslateFunction> &mlir::getTranslationRegistry() {
110 return getMutableTranslationRegistry();
111 }
112