github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Translation/Translation.cpp (about)

     1  //===- Translation.cpp - Translation registry -----------------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  //
    18  // Definitions of the translation registry.
    19  //
    20  //===----------------------------------------------------------------------===//
    21  
    22  #include "mlir/Translation.h"
    23  #include "mlir/IR/Module.h"
    24  #include "mlir/Support/LLVM.h"
    25  #include "mlir/Support/LogicalResult.h"
    26  #include "llvm/Support/ManagedStatic.h"
    27  
    28  using namespace mlir;
    29  
    30  // Get the mutable static map between registered "to MLIR" translations and the
    31  // TranslateToMLIRFunctions that perform those translations.
    32  static llvm::StringMap<TranslateToMLIRFunction> &
    33  getMutableTranslationToMLIRRegistry() {
    34    static llvm::StringMap<TranslateToMLIRFunction> translationToMLIRRegistry;
    35    return translationToMLIRRegistry;
    36  }
    37  // Get the mutable static map between registered "from MLIR" translations and
    38  // the TranslateFromMLIRFunctions that perform those translations.
    39  static llvm::StringMap<TranslateFromMLIRFunction> &
    40  getMutableTranslationFromMLIRRegistry() {
    41    static llvm::StringMap<TranslateFromMLIRFunction> translationFromMLIRRegistry;
    42    return translationFromMLIRRegistry;
    43  }
    44  
    45  TranslateToMLIRRegistration::TranslateToMLIRRegistration(
    46      StringRef name, const TranslateToMLIRFunction &function) {
    47    auto &translationToMLIRRegistry = getMutableTranslationToMLIRRegistry();
    48    if (translationToMLIRRegistry.find(name) != translationToMLIRRegistry.end())
    49      llvm::report_fatal_error(
    50          "Attempting to overwrite an existing <to> function");
    51    assert(function && "Attempting to register an empty translate <to> function");
    52    translationToMLIRRegistry[name] = function;
    53  }
    54  
    55  TranslateFromMLIRRegistration::TranslateFromMLIRRegistration(
    56      StringRef name, const TranslateFromMLIRFunction &function) {
    57    auto &translationFromMLIRRegistry = getMutableTranslationFromMLIRRegistry();
    58    if (translationFromMLIRRegistry.find(name) !=
    59        translationFromMLIRRegistry.end())
    60      llvm::report_fatal_error(
    61          "Attempting to overwrite an existing <from> function");
    62    assert(function &&
    63           "Attempting to register an empty translate <from> function");
    64    translationFromMLIRRegistry[name] = function;
    65  }
    66  
    67  // Merely add the const qualifier to the mutable registry so that external users
    68  // cannot modify it.
    69  const llvm::StringMap<TranslateToMLIRFunction> &
    70  mlir::getTranslationToMLIRRegistry() {
    71    return getMutableTranslationToMLIRRegistry();
    72  }
    73  
    74  const llvm::StringMap<TranslateFromMLIRFunction> &
    75  mlir::getTranslationFromMLIRRegistry() {
    76    return getMutableTranslationFromMLIRRegistry();
    77  }