MLIR  20.0.0git
LLVMTranslationInterface.h
Go to the documentation of this file.
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 
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.
38  : public DialectInterface::Base<LLVMTranslationDialectInterface> {
39 public:
41 
42  /// Hook for derived dialect interface to provide translation of the
43  /// operations to LLVM IR.
44  virtual LogicalResult
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
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
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.
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
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
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
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
A collection of dialect interfaces within a context, for a given concrete interface type.
DialectInterfaceCollection< LLVMTranslationDialectInterface > Base
const LLVMTranslationDialectInterface * getInterfaceFor(Object *obj) const
Get the interface for a given object, or null if one is not registered.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
Base class for dialect interfaces providing translation to LLVM IR.
virtual LogicalResult convertParameterAttr(LLVM::LLVMFuncOp function, int argIdx, NamedAttribute attr, LLVM::ModuleTranslation &moduleTranslation) const
Hook for derived dialect interface to translate or act on a derived dialect attribute that appears on...
virtual LogicalResult amendOperation(Operation *op, ArrayRef< llvm::Instruction * > instructions, NamedAttribute attribute, LLVM::ModuleTranslation &moduleTranslation) const
Hook for derived dialect interface to act on an operation that has dialect attributes from the derive...
virtual LogicalResult convertOperation(Operation *op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) const
Hook for derived dialect interface to provide translation of the operations to LLVM IR.
Interface collection for translation to LLVM IR, dispatches to a concrete interface implementation ba...
virtual LogicalResult convertParameterAttr(LLVM::LLVMFuncOp function, int argIdx, NamedAttribute attribute, LLVM::ModuleTranslation &moduleTranslation) const
Acts on the given function operation using the interface implemented by the dialect of one of the fun...
virtual LogicalResult amendOperation(Operation *op, ArrayRef< llvm::Instruction * > instructions, NamedAttribute attribute, LLVM::ModuleTranslation &moduleTranslation) const
Acts on the given operation using the interface implemented by the dialect of one of the operation's ...
virtual LogicalResult convertOperation(Operation *op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) const
Translates the given operation to LLVM IR using the interface implemented by the op's dialect.
Implementation class for module translation.
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:207
StringAttr getName() const
Return the name of the attribute.
Definition: Attributes.cpp:49
Dialect * getNameDialect() const
Return the dialect of the name of this attribute, if the name is prefixed by a dialect namespace.
Definition: Attributes.cpp:53
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
The base class used for all derived interface types.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.