1 //===- LLVMTranslationInterface.h - Translation to LLVM iface ---*- 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 // This header file defines dialect interfaces for translation to LLVM IR. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_TARGET_LLVMIR_LLVMTRANSLATIONINTERFACE_H 14 #define MLIR_TARGET_LLVMIR_LLVMTRANSLATIONINTERFACE_H 15 16 #include "mlir/IR/BuiltinAttributes.h" 17 #include "mlir/IR/DialectInterface.h" 18 #include "mlir/Support/LogicalResult.h" 19 20 namespace llvm { 21 class IRBuilderBase; 22 } // namespace llvm 23 24 namespace mlir { 25 namespace LLVM { 26 class ModuleTranslation; 27 } // namespace LLVM 28 29 /// Base class for dialect interfaces providing translation to LLVM IR. 30 /// Dialects that can be translated should provide an implementation of this 31 /// interface for the supported operations. The interface may be implemented in 32 /// a separate library to avoid the "main" dialect library depending on LLVM IR. 33 /// The interface can be attached using the delayed registration mechanism 34 /// available in DialectRegistry. 35 class LLVMTranslationDialectInterface 36 : public DialectInterface::Base<LLVMTranslationDialectInterface> { 37 public: LLVMTranslationDialectInterface(Dialect * dialect)38 LLVMTranslationDialectInterface(Dialect *dialect) : Base(dialect) {} 39 40 /// Hook for derived dialect interface to provide translation of the 41 /// operations to LLVM IR. 42 virtual LogicalResult convertOperation(Operation * op,llvm::IRBuilderBase & builder,LLVM::ModuleTranslation & moduleTranslation)43 convertOperation(Operation *op, llvm::IRBuilderBase &builder, 44 LLVM::ModuleTranslation &moduleTranslation) const { 45 return failure(); 46 } 47 48 /// Hook for derived dialect interface to act on an operation that has dialect 49 /// attributes from the derived dialect (the operation itself may be from a 50 /// different dialect). This gets called after the operation has been 51 /// translated. The hook is expected to use moduleTranslation to look up the 52 /// translation results and amend the corresponding IR constructs. Does 53 /// nothing and succeeds by default. 54 virtual LogicalResult amendOperation(Operation * op,NamedAttribute attribute,LLVM::ModuleTranslation & moduleTranslation)55 amendOperation(Operation *op, NamedAttribute attribute, 56 LLVM::ModuleTranslation &moduleTranslation) const { 57 return success(); 58 } 59 }; 60 61 /// Interface collection for translation to LLVM IR, dispatches to a concrete 62 /// interface implementation based on the dialect to which the given op belongs. 63 class LLVMTranslationInterface 64 : public DialectInterfaceCollection<LLVMTranslationDialectInterface> { 65 public: 66 using Base::Base; 67 68 /// Translates the given operation to LLVM IR using the interface implemented 69 /// by the op's dialect. 70 virtual LogicalResult convertOperation(Operation * op,llvm::IRBuilderBase & builder,LLVM::ModuleTranslation & moduleTranslation)71 convertOperation(Operation *op, llvm::IRBuilderBase &builder, 72 LLVM::ModuleTranslation &moduleTranslation) const { 73 if (const LLVMTranslationDialectInterface *iface = getInterfaceFor(op)) 74 return iface->convertOperation(op, builder, moduleTranslation); 75 return failure(); 76 } 77 78 /// Acts on the given operation using the interface implemented by the dialect 79 /// of one of the operation's dialect attributes. 80 virtual LogicalResult amendOperation(Operation * op,NamedAttribute attribute,LLVM::ModuleTranslation & moduleTranslation)81 amendOperation(Operation *op, NamedAttribute attribute, 82 LLVM::ModuleTranslation &moduleTranslation) const { 83 if (const LLVMTranslationDialectInterface *iface = 84 getInterfaceFor(attribute.getNameDialect())) { 85 return iface->amendOperation(op, attribute, moduleTranslation); 86 } 87 return success(); 88 } 89 }; 90 91 } // namespace mlir 92 93 #endif // MLIR_TARGET_LLVMIR_LLVMTRANSLATIONINTERFACE_H 94