MLIR  18.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 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.
36  : public DialectInterface::Base<LLVMTranslationDialectInterface> {
37 public:
39 
40  /// Hook for derived dialect interface to provide translation of the
41  /// operations to LLVM IR.
42  virtual LogicalResult
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
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.
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
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
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
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 amendOperation(Operation *op, 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 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.
virtual LogicalResult amendOperation(Operation *op, NamedAttribute attribute, LLVM::ModuleTranslation &moduleTranslation) const
Acts on the given operation using the interface implemented by the dialect of one of the operation's ...
Implementation class for module translation.
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:198
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