MLIR 24.0.0git
LLVMImportInterface.h
Go to the documentation of this file.
1//===- LLVMImportInterface.h - Import from LLVM interface -------*- 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 the LLVM IR import.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef MLIR_TARGET_LLVMIR_LLVMIMPORTINTERFACE_H
14#define MLIR_TARGET_LLVMIR_LLVMIMPORTINTERFACE_H
15
16#include "mlir/IR/Builders.h"
18#include "mlir/IR/Diagnostics.h"
20#include "mlir/IR/Location.h"
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/Support/FormatVariadic.h"
24
25namespace llvm {
26class IRBuilderBase;
27} // namespace llvm
28
29namespace mlir {
30namespace LLVM {
31class ModuleImport;
32} // namespace LLVM
33} // namespace mlir
34
35#include "mlir/Target/LLVMIR/LLVMImportDialectInterface.h.inc"
36
37namespace mlir {
38/// Interface collection for the import of LLVM IR that dispatches to a concrete
39/// dialect interface implementation. Queries the dialect interfaces to obtain a
40/// list of the supported LLVM IR constructs and then builds a mapping for the
41/// efficient dispatch.
43 : public DialectInterfaceCollection<LLVMImportDialectInterface> {
44public:
45 using Base::Base;
46
47 /// Queries all registered dialect interfaces for the supported LLVM IR
48 /// intrinsic and metadata kinds and builds the dispatch tables for the
49 /// conversion. Returns failure if multiple dialect interfaces translate the
50 /// same LLVM IR intrinsic.
51 LogicalResult initializeImport(llvm::LLVMContext &llvmContext) {
52 for (const LLVMImportDialectInterface &iface : *this) {
53 // Verify the supported intrinsics have not been mapped before.
54 const auto *intrinsicIt =
55 llvm::find_if(iface.getSupportedIntrinsics(), [&](unsigned id) {
56 return intrinsicToDialect.count(id);
57 });
58 if (intrinsicIt != iface.getSupportedIntrinsics().end()) {
59 return emitError(
60 UnknownLoc::get(iface.getContext()),
61 llvm::formatv(
62 "expected unique conversion for intrinsic ({0}), but "
63 "got conflicting {1} and {2} conversions",
64 *intrinsicIt, iface.getDialect()->getNamespace(),
65 intrinsicToDialect.lookup(*intrinsicIt)->getNamespace()));
66 }
67 const auto *instructionIt =
68 llvm::find_if(iface.getSupportedInstructions(), [&](unsigned id) {
69 return instructionToDialect.count(id);
70 });
71 if (instructionIt != iface.getSupportedInstructions().end()) {
72 return emitError(
73 UnknownLoc::get(iface.getContext()),
74 llvm::formatv(
75 "expected unique conversion for instruction ({0}), but "
76 "got conflicting {1} and {2} conversions",
77 *intrinsicIt, iface.getDialect()->getNamespace(),
78 instructionToDialect.lookup(*intrinsicIt)
79 ->getDialect()
80 ->getNamespace()));
81 }
82 // Add a mapping for all supported intrinsic identifiers.
83 for (unsigned id : iface.getSupportedIntrinsics())
84 intrinsicToDialect[id] = iface.getDialect();
85 // Add a mapping for all supported instruction identifiers.
86 for (unsigned id : iface.getSupportedInstructions())
87 instructionToDialect[id] = &iface;
88 // Add a mapping for all supported metadata kinds.
89 for (unsigned kind : iface.getSupportedMetadata(llvmContext))
90 metadataToDialect[kind].push_back(iface.getDialect());
91 }
92
93 return success();
94 }
95
96 /// Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
97 /// Returns failure otherwise.
98 LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
99 LLVM::ModuleImport &moduleImport) const;
100
101 /// Returns true if the given LLVM IR intrinsic is convertible to an MLIR
102 /// operation.
103 bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) {
104 return intrinsicToDialect.count(id);
105 }
106
107 /// Converts the LLVM instruction to an MLIR operation if a conversion exists.
108 /// Returns failure otherwise.
109 LogicalResult convertInstruction(OpBuilder &builder, llvm::Instruction *inst,
110 ArrayRef<llvm::Value *> llvmOperands,
111 LLVM::ModuleImport &moduleImport) const {
112 // Lookup the dialect interface for the given instruction.
113 const LLVMImportDialectInterface *iface =
114 instructionToDialect.lookup(inst->getOpcode());
115 if (!iface)
116 return failure();
117
118 return iface->convertInstruction(builder, inst, llvmOperands, moduleImport);
119 }
120
121 /// Returns true if the given LLVM IR instruction is convertible to an MLIR
122 /// operation.
123 bool isConvertibleInstruction(unsigned id) {
124 return instructionToDialect.count(id);
125 }
126
127 /// Attaches the given LLVM metadata to the imported operation if a conversion
128 /// to one or more MLIR dialect attributes exists and succeeds. Returns
129 /// success if at least one of the conversions is successful and failure if
130 /// all of them fail.
131 LogicalResult setMetadataAttrs(OpBuilder &builder, unsigned kind,
132 llvm::MDNode *node, Operation *op,
133 LLVM::ModuleImport &moduleImport) const {
134 // Lookup the dialect interfaces for the given metadata.
135 auto it = metadataToDialect.find(kind);
136 if (it == metadataToDialect.end())
137 return failure();
138
139 // Dispatch the conversion to the dialect interfaces.
140 bool isSuccess = false;
141 for (Dialect *dialect : it->getSecond()) {
142 const LLVMImportDialectInterface *iface = getInterfaceFor(dialect);
143 assert(iface && "expected to find a dialect interface");
144 if (succeeded(
145 iface->setMetadataAttrs(builder, kind, node, op, moduleImport)))
146 isSuccess = true;
147 }
148
149 // Returns failure if all conversions fail.
150 return success(isSuccess);
151 }
152
153 /// Returns true if the given LLVM IR metadata is convertible to an MLIR
154 /// attribute.
155 bool isConvertibleMetadata(unsigned kind) {
156 return metadataToDialect.count(kind);
157 }
158
159private:
160 /// Generate llvm.call_intrinsic when no supporting dialect available.
161 static LogicalResult
162 convertUnregisteredIntrinsic(OpBuilder &builder, llvm::CallInst *inst,
163 LLVM::ModuleImport &moduleImport);
164
165 DenseMap<unsigned, Dialect *> intrinsicToDialect;
168};
169
170} // namespace mlir
171
172#endif // MLIR_TARGET_LLVMIR_LLVMIMPORTINTERFACE_H
return success()
DialectInterfaceCollection< LLVMImportDialectInterface > Base
const LLVMImportDialectInterface * getInterfaceFor(Object *obj) const
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
Interface collection for the import of LLVM IR that dispatches to a concrete dialect interface implem...
LogicalResult convertInstruction(OpBuilder &builder, llvm::Instruction *inst, ArrayRef< llvm::Value * > llvmOperands, LLVM::ModuleImport &moduleImport) const
Converts the LLVM instruction to an MLIR operation if a conversion exists.
LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport) const
Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
LogicalResult setMetadataAttrs(OpBuilder &builder, unsigned kind, llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport) const
Attaches the given LLVM metadata to the imported operation if a conversion to one or more MLIR dialec...
bool isConvertibleMetadata(unsigned kind)
Returns true if the given LLVM IR metadata is convertible to an MLIR attribute.
LogicalResult initializeImport(llvm::LLVMContext &llvmContext)
Queries all registered dialect interfaces for the supported LLVM IR intrinsic and metadata kinds and ...
bool isConvertibleInstruction(unsigned id)
Returns true if the given LLVM IR instruction is convertible to an MLIR operation.
bool isConvertibleIntrinsic(llvm::Intrinsic::ID id)
Returns true if the given LLVM IR intrinsic is convertible to an MLIR operation.
Module import implementation class that provides methods to import globals and functions from an LLVM...
This class helps build Operations.
Definition Builders.h:210
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123