@@ -52,6 +52,15 @@ class LLVMImportDialectInterface
52
52
return failure ();
53
53
}
54
54
55
+ // / Hook for derived dialect interfaces to implement the import of
56
+ // / instructions into MLIR.
57
+ virtual LogicalResult
58
+ convertInstruction (OpBuilder &builder, llvm::Instruction *inst,
59
+ ArrayRef<llvm::Value *> llvmOperands,
60
+ LLVM::ModuleImport &moduleImport) const {
61
+ return failure ();
62
+ }
63
+
55
64
// / Hook for derived dialect interfaces to implement the import of metadata
56
65
// / into MLIR. Attaches the converted metadata kind and node to the provided
57
66
// / operation.
@@ -66,6 +75,11 @@ class LLVMImportDialectInterface
66
75
// / returns the list of supported intrinsic identifiers.
67
76
virtual ArrayRef<unsigned > getSupportedIntrinsics () const { return {}; }
68
77
78
+ // / Hook for derived dialect interfaces to publish the supported instructions.
79
+ // / As every LLVM IR instructions has a unique integer identifier, the
80
+ // / function returns the list of supported instructions identifiers.
81
+ virtual ArrayRef<unsigned > getSupportedInstructions () const { return {}; }
82
+
69
83
// / Hook for derived dialect interfaces to publish the supported metadata
70
84
// / kinds. As every metadata kind has a unique integer identifier, the
71
85
// / function returns the list of supported metadata identifiers.
@@ -100,9 +114,27 @@ class LLVMImportInterface
100
114
*it, iface.getDialect ()->getNamespace (),
101
115
intrinsicToDialect.lookup (*it)->getNamespace ()));
102
116
}
117
+ const auto *instIt =
118
+ llvm::find_if (iface.getSupportedInstructions (), [&](unsigned id) {
119
+ return instructionToDialect.count (id);
120
+ });
121
+ if (instIt != iface.getSupportedInstructions ().end ()) {
122
+ return emitError (
123
+ UnknownLoc::get (iface.getContext ()),
124
+ llvm::formatv (
125
+ " expected unique conversion for instruction ({0}), but "
126
+ " got conflicting {1} and {2} conversions" ,
127
+ *it, iface.getDialect ()->getNamespace (),
128
+ instructionToDialect.lookup (*it)
129
+ ->getDialect ()
130
+ ->getNamespace ()));
131
+ }
103
132
// Add a mapping for all supported intrinsic identifiers.
104
133
for (unsigned id : iface.getSupportedIntrinsics ())
105
134
intrinsicToDialect[id] = iface.getDialect ();
135
+ // Add a mapping for all supported instruction identifiers.
136
+ for (unsigned id : iface.getSupportedInstructions ())
137
+ instructionToDialect[id] = &iface;
106
138
// Add a mapping for all supported metadata kinds.
107
139
for (unsigned kind : iface.getSupportedMetadata ())
108
140
metadataToDialect[kind].push_back (iface.getDialect ());
@@ -132,6 +164,26 @@ class LLVMImportInterface
132
164
return intrinsicToDialect.count (id);
133
165
}
134
166
167
+ // / Converts the LLVM instruction to an MLIR operation if a conversion exists.
168
+ // / Returns failure otherwise.
169
+ LogicalResult convertInstruction (OpBuilder &builder, llvm::Instruction *inst,
170
+ ArrayRef<llvm::Value *> llvmOperands,
171
+ LLVM::ModuleImport &moduleImport) const {
172
+ // Lookup the dialect interface for the given instruction.
173
+ const LLVMImportDialectInterface *iface =
174
+ instructionToDialect.lookup (inst->getOpcode ());
175
+ if (!iface)
176
+ return failure ();
177
+
178
+ return iface->convertInstruction (builder, inst, llvmOperands, moduleImport);
179
+ }
180
+
181
+ // / Returns true if the given LLVM IR instruction is convertible to an MLIR
182
+ // / operation.
183
+ bool isConvertibleInstruction (unsigned id) {
184
+ return instructionToDialect.count (id);
185
+ }
186
+
135
187
// / Attaches the given LLVM metadata to the imported operation if a conversion
136
188
// / to one or more MLIR dialect attributes exists and succeeds. Returns
137
189
// / success if at least one of the conversions is successful and failure if
@@ -166,6 +218,7 @@ class LLVMImportInterface
166
218
167
219
private:
168
220
DenseMap<unsigned , Dialect *> intrinsicToDialect;
221
+ DenseMap<unsigned , const LLVMImportDialectInterface *> instructionToDialect;
169
222
DenseMap<unsigned , SmallVector<Dialect *, 1 >> metadataToDialect;
170
223
};
171
224
0 commit comments