1 //===- Translation.h - Translation registry ---------------------*- C++ -*-===// 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 // Registry for user-provided translations. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H 14 #define MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H 15 16 #include "llvm/Support/CommandLine.h" 17 18 namespace llvm { 19 class MemoryBuffer; 20 class SourceMgr; 21 class StringRef; 22 } // namespace llvm 23 24 namespace mlir { 25 class DialectRegistry; 26 struct LogicalResult; 27 class MLIRContext; 28 class ModuleOp; 29 template <typename OpTy> 30 class OwningOpRef; 31 32 /// Interface of the function that translates the sources managed by `sourceMgr` 33 /// to MLIR. The source manager has at least one buffer. The implementation 34 /// should create a new MLIR ModuleOp in the given context and return a pointer 35 /// to it, or a nullptr in case of any error. 36 using TranslateSourceMgrToMLIRFunction = std::function<OwningOpRef<ModuleOp>( 37 llvm::SourceMgr &sourceMgr, MLIRContext *)>; 38 39 /// Interface of the function that translates the given string to MLIR. The 40 /// implementation should create a new MLIR ModuleOp in the given context. If 41 /// source-related error reporting is required from within the function, use 42 /// TranslateSourceMgrToMLIRFunction instead. 43 using TranslateStringRefToMLIRFunction = 44 std::function<OwningOpRef<ModuleOp>(llvm::StringRef, MLIRContext *)>; 45 46 /// Interface of the function that translates MLIR to a different format and 47 /// outputs the result to a stream. It is allowed to modify the module. 48 using TranslateFromMLIRFunction = 49 std::function<LogicalResult(ModuleOp, llvm::raw_ostream &output)>; 50 51 /// Interface of the function that performs file-to-file translation involving 52 /// MLIR. The input file is held in the given MemoryBuffer; the output file 53 /// should be written to the given raw_ostream. The implementation should create 54 /// all MLIR constructs needed during the process inside the given context. This 55 /// can be used for round-tripping external formats through the MLIR system. 56 using TranslateFunction = std::function<LogicalResult( 57 llvm::SourceMgr &sourceMgr, llvm::raw_ostream &output, MLIRContext *)>; 58 59 /// Use Translate[ToMLIR|FromMLIR]Registration as an initializer that 60 /// registers a function and associates it with name. This requires that a 61 /// translation has not been registered to a given name. 62 /// 63 /// Usage: 64 /// 65 /// // At file scope. 66 /// namespace mlir { 67 /// void registerTRexToMLIRRegistration() { 68 /// TranslateToMLIRRegistration Unused(&MySubCommand, [] { ... }); 69 /// } 70 /// } // namespace mlir 71 /// 72 /// \{ 73 struct TranslateToMLIRRegistration { 74 TranslateToMLIRRegistration(llvm::StringRef name, 75 const TranslateSourceMgrToMLIRFunction &function); 76 TranslateToMLIRRegistration(llvm::StringRef name, 77 const TranslateStringRefToMLIRFunction &function); 78 }; 79 80 struct TranslateFromMLIRRegistration { 81 TranslateFromMLIRRegistration( 82 llvm::StringRef name, const TranslateFromMLIRFunction &function, 83 const std::function<void(DialectRegistry &)> &dialectRegistration = 84 [](DialectRegistry &) {}); 85 }; 86 struct TranslateRegistration { 87 TranslateRegistration(llvm::StringRef name, 88 const TranslateFunction &function); 89 }; 90 /// \} 91 92 /// A command line parser for translation functions. 93 struct TranslationParser : public llvm::cl::parser<const TranslateFunction *> { 94 TranslationParser(llvm::cl::Option &opt); 95 96 void printOptionInfo(const llvm::cl::Option &o, 97 size_t globalWidth) const override; 98 }; 99 100 } // namespace mlir 101 102 #endif // MLIR_TOOLS_MLIRTRANSLATE_TRANSLATION_H 103