diff options
| author | Alex Zinenko <zinenko@google.com> | 2018-11-21 06:22:17 -0800 |
|---|---|---|
| committer | jpienaar <jpienaar@google.com> | 2019-03-29 14:06:33 -0700 |
| commit | 6c5317eafa6e445ffb4b7553349d752dce60b9c2 (patch) | |
| tree | ddb7d9a230bf6b37ada7dfe38f287130c8f1d785 /mlir/lib/Translation | |
| parent | b5756fdaa148662b4684bb39e2a41f9b2dc95f19 (diff) | |
| download | bcm5719-llvm-6c5317eafa6e445ffb4b7553349d752dce60b9c2.tar.gz bcm5719-llvm-6c5317eafa6e445ffb4b7553349d752dce60b9c2.zip | |
Separate translators into "from MLIR" and "to MLIR".
Translations performed by mlir-translate only have MLIR on one end.
MLIR-to-MLIR conversions (including dialect changes) should be treated as
passes and run by mlir-opt. Individual translations should not care about
reading or writing MLIR and should work on in-memory representation of MLIR
modules instead. Split the TranslateFunction interface and the translate
registry into two parts: "from MLIR" and "to MLIR".
Update mlir-translate to handle both registries together by wrapping
translation functions into source-to-source convresions. Remove MLIR parsing
and writing from individual translations and make them operate on Modules
instead. This removes the need for individual translators to include
tools/mlir-translate/mlir-translate.h, which can now be safely removed.
Remove mlir-to-mlir translation that only existed as a registration example and
use mlir-opt instead for tests.
PiperOrigin-RevId: 222398707
Diffstat (limited to 'mlir/lib/Translation')
| -rw-r--r-- | mlir/lib/Translation/Translation.cpp | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/mlir/lib/Translation/Translation.cpp b/mlir/lib/Translation/Translation.cpp index 96765c71b81..c1a9f9d11fc 100644 --- a/mlir/lib/Translation/Translation.cpp +++ b/mlir/lib/Translation/Translation.cpp @@ -25,24 +25,50 @@ using namespace mlir; -// Get the mutable static map between translations registered and the -// TranslateFunctions that perform those translations. -static llvm::StringMap<TranslateFunction> &getMutableTranslationRegistry() { - static llvm::StringMap<TranslateFunction> translationRegistry; - return translationRegistry; +// Get the mutable static map between registered "to MLIR" translations and the +// TranslateToMLIRFunctions that perform those translations. +static llvm::StringMap<TranslateToMLIRFunction> & +getMutableTranslationToMLIRRegistry() { + static llvm::StringMap<TranslateToMLIRFunction> translationToMLIRRegistry; + return translationToMLIRRegistry; +} +// Get the mutable static map between registered "from MLIR" translations and +// the TranslateFromMLIRFunctions that perform those translations. +static llvm::StringMap<TranslateFromMLIRFunction> & +getMutableTranslationFromMLIRRegistry() { + static llvm::StringMap<TranslateFromMLIRFunction> translationFromMLIRRegistry; + return translationFromMLIRRegistry; +} + +TranslateToMLIRRegistration::TranslateToMLIRRegistration( + StringRef name, const TranslateToMLIRFunction &function) { + auto &translationToMLIRRegistry = getMutableTranslationToMLIRRegistry(); + if (translationToMLIRRegistry.find(name) != translationToMLIRRegistry.end()) + llvm::report_fatal_error( + "Attempting to overwrite an existing <to> function"); + assert(function && "Attempting to register an empty translate <to> function"); + translationToMLIRRegistry[name] = function; } -TranslateRegistration::TranslateRegistration( - StringRef name, const TranslateFunction &function) { - auto &translationRegistry = getMutableTranslationRegistry(); - if (translationRegistry.find(name) != translationRegistry.end()) - llvm::report_fatal_error("Attempting to overwrite an existing function"); - assert(function && "Attempting to register an empty translate function"); - translationRegistry[name] = function; +TranslateFromMLIRRegistration::TranslateFromMLIRRegistration( + StringRef name, const TranslateFromMLIRFunction &function) { + auto &translationFromMLIRRegistry = getMutableTranslationFromMLIRRegistry(); + if (translationFromMLIRRegistry.find(name) != + translationFromMLIRRegistry.end()) + llvm::report_fatal_error( + "Attempting to overwrite an existing <from> function"); + assert(function && "Attempting to register an empty translate <to> function"); + translationFromMLIRRegistry[name] = function; } // Merely add the const qualifier to the mutable registry so that external users // cannot modify it. -const llvm::StringMap<TranslateFunction> &mlir::getTranslationRegistry() { - return getMutableTranslationRegistry(); +const llvm::StringMap<TranslateToMLIRFunction> & +mlir::getTranslationToMLIRRegistry() { + return getMutableTranslationToMLIRRegistry(); +} + +const llvm::StringMap<TranslateFromMLIRFunction> & +mlir::getTranslationFromMLIRRegistry() { + return getMutableTranslationFromMLIRRegistry(); } |

