13#ifndef MLIR_TARGET_LLVMIR_LLVMIMPORTINTERFACE_H
14#define MLIR_TARGET_LLVMIR_LLVMIMPORTINTERFACE_H
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/Support/FormatVariadic.h"
35#include "mlir/Target/LLVMIR/LLVMImportDialectInterface.h.inc"
52 for (
const LLVMImportDialectInterface &iface : *
this) {
54 const auto *intrinsicIt =
55 llvm::find_if(iface.getSupportedIntrinsics(), [&](
unsigned id) {
56 return intrinsicToDialect.count(id);
58 if (intrinsicIt != iface.getSupportedIntrinsics().end()) {
60 UnknownLoc::get(iface.getContext()),
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()));
67 const auto *instructionIt =
68 llvm::find_if(iface.getSupportedInstructions(), [&](
unsigned id) {
69 return instructionToDialect.count(id);
71 if (instructionIt != iface.getSupportedInstructions().end()) {
73 UnknownLoc::get(iface.getContext()),
75 "expected unique conversion for instruction ({0}), but "
76 "got conflicting {1} and {2} conversions",
77 *intrinsicIt, iface.getDialect()->getNamespace(),
78 instructionToDialect.lookup(*intrinsicIt)
83 for (
unsigned id : iface.getSupportedIntrinsics())
84 intrinsicToDialect[id] = iface.getDialect();
86 for (
unsigned id : iface.getSupportedInstructions())
87 instructionToDialect[id] = &iface;
89 for (
unsigned kind : iface.getSupportedMetadata(llvmContext))
90 metadataToDialect[kind].push_back(iface.getDialect());
104 return intrinsicToDialect.count(
id);
113 const LLVMImportDialectInterface *iface =
114 instructionToDialect.lookup(inst->getOpcode());
118 return iface->convertInstruction(builder, inst, llvmOperands, moduleImport);
124 return instructionToDialect.count(
id);
135 auto it = metadataToDialect.find(kind);
136 if (it == metadataToDialect.end())
140 bool isSuccess =
false;
141 for (
Dialect *dialect : it->getSecond()) {
143 assert(iface &&
"expected to find a dialect interface");
145 iface->setMetadataAttrs(builder, kind, node, op, moduleImport)))
156 return metadataToDialect.count(kind);
162 convertUnregisteredIntrinsic(
OpBuilder &builder, llvm::CallInst *inst,
DialectInterfaceCollection< LLVMImportDialectInterface > Base
DialectInterfaceCollection(MLIRContext *ctx)
const LLVMImportDialectInterface * getInterfaceFor(Object *obj) const
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
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.
Operation is the basic unit of execution within MLIR.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
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