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