xref: /llvm-project/mlir/include/mlir/Target/LLVMIR/LLVMTranslationInterface.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
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/Dialect/LLVMIR/LLVMDialect.h"
17 #include "mlir/IR/BuiltinAttributes.h"
18 #include "mlir/IR/DialectInterface.h"
19 
20 namespace llvm {
21 class Instruction;
22 class IRBuilderBase;
23 } // namespace llvm
24 
25 namespace mlir {
26 namespace LLVM {
27 class ModuleTranslation;
28 class LLVMFuncOp;
29 } // namespace LLVM
30 
31 /// Base class for dialect interfaces providing translation to LLVM IR.
32 /// Dialects that can be translated should provide an implementation of this
33 /// interface for the supported operations. The interface may be implemented in
34 /// a separate library to avoid the "main" dialect library depending on LLVM IR.
35 /// The interface can be attached using the delayed registration mechanism
36 /// available in DialectRegistry.
37 class LLVMTranslationDialectInterface
38     : public DialectInterface::Base<LLVMTranslationDialectInterface> {
39 public:
LLVMTranslationDialectInterface(Dialect * dialect)40   LLVMTranslationDialectInterface(Dialect *dialect) : Base(dialect) {}
41 
42   /// Hook for derived dialect interface to provide translation of the
43   /// operations to LLVM IR.
44   virtual LogicalResult
convertOperation(Operation * op,llvm::IRBuilderBase & builder,LLVM::ModuleTranslation & moduleTranslation)45   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
46                    LLVM::ModuleTranslation &moduleTranslation) const {
47     return failure();
48   }
49 
50   /// Hook for derived dialect interface to act on an operation that has dialect
51   /// attributes from the derived dialect (the operation itself may be from a
52   /// different dialect). This gets called after the operation has been
53   /// translated. The hook is expected to use moduleTranslation to look up the
54   /// translation results and amend the corresponding IR constructs. Does
55   /// nothing and succeeds by default.
56   virtual LogicalResult
amendOperation(Operation * op,ArrayRef<llvm::Instruction * > instructions,NamedAttribute attribute,LLVM::ModuleTranslation & moduleTranslation)57   amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
58                  NamedAttribute attribute,
59                  LLVM::ModuleTranslation &moduleTranslation) const {
60     return success();
61   }
62 
63   /// Hook for derived dialect interface to translate or act on a derived
64   /// dialect attribute that appears on a function parameter. This gets called
65   /// after the function operation has been translated.
66   virtual LogicalResult
convertParameterAttr(LLVM::LLVMFuncOp function,int argIdx,NamedAttribute attr,LLVM::ModuleTranslation & moduleTranslation)67   convertParameterAttr(LLVM::LLVMFuncOp function, int argIdx,
68                        NamedAttribute attr,
69                        LLVM::ModuleTranslation &moduleTranslation) const {
70     return success();
71   }
72 };
73 
74 /// Interface collection for translation to LLVM IR, dispatches to a concrete
75 /// interface implementation based on the dialect to which the given op belongs.
76 class LLVMTranslationInterface
77     : public DialectInterfaceCollection<LLVMTranslationDialectInterface> {
78 public:
79   using Base::Base;
80 
81   /// Translates the given operation to LLVM IR using the interface implemented
82   /// by the op's dialect.
83   virtual LogicalResult
convertOperation(Operation * op,llvm::IRBuilderBase & builder,LLVM::ModuleTranslation & moduleTranslation)84   convertOperation(Operation *op, llvm::IRBuilderBase &builder,
85                    LLVM::ModuleTranslation &moduleTranslation) const {
86     if (const LLVMTranslationDialectInterface *iface = getInterfaceFor(op))
87       return iface->convertOperation(op, builder, moduleTranslation);
88     return failure();
89   }
90 
91   /// Acts on the given operation using the interface implemented by the dialect
92   /// of one of the operation's dialect attributes.
93   virtual LogicalResult
amendOperation(Operation * op,ArrayRef<llvm::Instruction * > instructions,NamedAttribute attribute,LLVM::ModuleTranslation & moduleTranslation)94   amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
95                  NamedAttribute attribute,
96                  LLVM::ModuleTranslation &moduleTranslation) const {
97     if (const LLVMTranslationDialectInterface *iface =
98             getInterfaceFor(attribute.getNameDialect())) {
99       return iface->amendOperation(op, instructions, attribute,
100                                    moduleTranslation);
101     }
102     return success();
103   }
104 
105   /// Acts on the given function operation using the interface implemented by
106   /// the dialect of one of the function parameter attributes.
107   virtual LogicalResult
convertParameterAttr(LLVM::LLVMFuncOp function,int argIdx,NamedAttribute attribute,LLVM::ModuleTranslation & moduleTranslation)108   convertParameterAttr(LLVM::LLVMFuncOp function, int argIdx,
109                        NamedAttribute attribute,
110                        LLVM::ModuleTranslation &moduleTranslation) const {
111     if (const LLVMTranslationDialectInterface *iface =
112             getInterfaceFor(attribute.getNameDialect())) {
113       return iface->convertParameterAttr(function, argIdx, attribute,
114                                          moduleTranslation);
115     }
116     function.emitWarning("Unhandled parameter attribute '" +
117                          attribute.getName().str() + "'");
118     return success();
119   }
120 };
121 
122 } // namespace mlir
123 
124 #endif // MLIR_TARGET_LLVMIR_LLVMTRANSLATIONINTERFACE_H
125