xref: /llvm-project/mlir/lib/Target/LLVMIR/ModuleImport.cpp (revision 89aaf2cf68d00e86dfd102a449fc68ff7ea5c85c)
1 //===- ModuleImport.cpp - LLVM to MLIR conversion ---------------*- 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 file implements the import of an LLVM IR module into an LLVM dialect
10 // module.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Target/LLVMIR/ModuleImport.h"
15 #include "mlir/IR/BuiltinAttributes.h"
16 #include "mlir/Target/LLVMIR/Import.h"
17 
18 #include "AttrKindDetail.h"
19 #include "DataLayoutImporter.h"
20 #include "DebugImporter.h"
21 #include "LoopAnnotationImporter.h"
22 
23 #include "mlir/Dialect/DLTI/DLTI.h"
24 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
25 #include "mlir/IR/Builders.h"
26 #include "mlir/IR/Matchers.h"
27 #include "mlir/Interfaces/DataLayoutInterfaces.h"
28 #include "mlir/Tools/mlir-translate/Translation.h"
29 
30 #include "llvm/ADT/DepthFirstIterator.h"
31 #include "llvm/ADT/PostOrderIterator.h"
32 #include "llvm/ADT/ScopeExit.h"
33 #include "llvm/ADT/StringSet.h"
34 #include "llvm/ADT/TypeSwitch.h"
35 #include "llvm/IR/Comdat.h"
36 #include "llvm/IR/Constants.h"
37 #include "llvm/IR/InlineAsm.h"
38 #include "llvm/IR/InstIterator.h"
39 #include "llvm/IR/Instructions.h"
40 #include "llvm/IR/IntrinsicInst.h"
41 #include "llvm/IR/Metadata.h"
42 #include "llvm/IR/Operator.h"
43 #include "llvm/Support/ModRef.h"
44 
45 using namespace mlir;
46 using namespace mlir::LLVM;
47 using namespace mlir::LLVM::detail;
48 
49 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc"
50 
51 // Utility to print an LLVM value as a string for passing to emitError().
52 // FIXME: Diagnostic should be able to natively handle types that have
53 // operator << (raw_ostream&) defined.
54 static std::string diag(const llvm::Value &value) {
55   std::string str;
56   llvm::raw_string_ostream os(str);
57   os << value;
58   return str;
59 }
60 
61 // Utility to print an LLVM metadata node as a string for passing
62 // to emitError(). The module argument is needed to print the nodes
63 // canonically numbered.
64 static std::string diagMD(const llvm::Metadata *node,
65                           const llvm::Module *module) {
66   std::string str;
67   llvm::raw_string_ostream os(str);
68   node->print(os, module, /*IsForDebug=*/true);
69   return str;
70 }
71 
72 /// Returns the name of the global_ctors global variables.
73 static constexpr StringRef getGlobalCtorsVarName() {
74   return "llvm.global_ctors";
75 }
76 
77 /// Prefix used for symbols of nameless llvm globals.
78 static constexpr StringRef getNamelessGlobalPrefix() {
79   return "mlir.llvm.nameless_global";
80 }
81 
82 /// Returns the name of the global_dtors global variables.
83 static constexpr StringRef getGlobalDtorsVarName() {
84   return "llvm.global_dtors";
85 }
86 
87 /// Returns the symbol name for the module-level comdat operation. It must not
88 /// conflict with the user namespace.
89 static constexpr StringRef getGlobalComdatOpName() {
90   return "__llvm_global_comdat";
91 }
92 
93 /// Converts the sync scope identifier of `inst` to the string representation
94 /// necessary to build an atomic LLVM dialect operation. Returns the empty
95 /// string if the operation has either no sync scope or the default system-level
96 /// sync scope attached. The atomic operations only set their sync scope
97 /// attribute if they have a non-default sync scope attached.
98 static StringRef getLLVMSyncScope(llvm::Instruction *inst) {
99   std::optional<llvm::SyncScope::ID> syncScopeID =
100       llvm::getAtomicSyncScopeID(inst);
101   if (!syncScopeID)
102     return "";
103 
104   // Search the sync scope name for the given identifier. The default
105   // system-level sync scope thereby maps to the empty string.
106   SmallVector<StringRef> syncScopeName;
107   llvm::LLVMContext &llvmContext = inst->getContext();
108   llvmContext.getSyncScopeNames(syncScopeName);
109   auto *it = llvm::find_if(syncScopeName, [&](StringRef name) {
110     return *syncScopeID == llvmContext.getOrInsertSyncScopeID(name);
111   });
112   if (it != syncScopeName.end())
113     return *it;
114   llvm_unreachable("incorrect sync scope identifier");
115 }
116 
117 /// Converts an array of unsigned indices to a signed integer position array.
118 static SmallVector<int64_t> getPositionFromIndices(ArrayRef<unsigned> indices) {
119   SmallVector<int64_t> position;
120   llvm::append_range(position, indices);
121   return position;
122 }
123 
124 /// Converts the LLVM instructions that have a generated MLIR builder. Using a
125 /// static implementation method called from the module import ensures the
126 /// builders have to use the `moduleImport` argument and cannot directly call
127 /// import methods. As a result, both the intrinsic and the instruction MLIR
128 /// builders have to use the `moduleImport` argument and none of them has direct
129 /// access to the private module import methods.
130 static LogicalResult convertInstructionImpl(OpBuilder &odsBuilder,
131                                             llvm::Instruction *inst,
132                                             ModuleImport &moduleImport,
133                                             LLVMImportInterface &iface) {
134   // Copy the operands to an LLVM operands array reference for conversion.
135   SmallVector<llvm::Value *> operands(inst->operands());
136   ArrayRef<llvm::Value *> llvmOperands(operands);
137 
138   // Convert all instructions that provide an MLIR builder.
139   if (iface.isConvertibleInstruction(inst->getOpcode()))
140     return iface.convertInstruction(odsBuilder, inst, llvmOperands,
141                                     moduleImport);
142     // TODO: Implement the `convertInstruction` hooks in the
143     // `LLVMDialectLLVMIRImportInterface` and move the following include there.
144 #include "mlir/Dialect/LLVMIR/LLVMOpFromLLVMIRConversions.inc"
145   return failure();
146 }
147 
148 /// Get a topologically sorted list of blocks for the given basic blocks.
149 static SetVector<llvm::BasicBlock *>
150 getTopologicallySortedBlocks(ArrayRef<llvm::BasicBlock *> basicBlocks) {
151   SetVector<llvm::BasicBlock *> blocks;
152   for (llvm::BasicBlock *basicBlock : basicBlocks) {
153     if (!blocks.contains(basicBlock)) {
154       llvm::ReversePostOrderTraversal<llvm::BasicBlock *> traversal(basicBlock);
155       blocks.insert(traversal.begin(), traversal.end());
156     }
157   }
158   assert(blocks.size() == basicBlocks.size() && "some blocks are not sorted");
159   return blocks;
160 }
161 
162 ModuleImport::ModuleImport(ModuleOp mlirModule,
163                            std::unique_ptr<llvm::Module> llvmModule,
164                            bool emitExpensiveWarnings,
165                            bool importEmptyDICompositeTypes)
166     : builder(mlirModule->getContext()), context(mlirModule->getContext()),
167       mlirModule(mlirModule), llvmModule(std::move(llvmModule)),
168       iface(mlirModule->getContext()),
169       typeTranslator(*mlirModule->getContext()),
170       debugImporter(std::make_unique<DebugImporter>(
171           mlirModule, importEmptyDICompositeTypes)),
172       loopAnnotationImporter(
173           std::make_unique<LoopAnnotationImporter>(*this, builder)),
174       emitExpensiveWarnings(emitExpensiveWarnings) {
175   builder.setInsertionPointToStart(mlirModule.getBody());
176 }
177 
178 ComdatOp ModuleImport::getGlobalComdatOp() {
179   if (globalComdatOp)
180     return globalComdatOp;
181 
182   OpBuilder::InsertionGuard guard(builder);
183   builder.setInsertionPointToEnd(mlirModule.getBody());
184   globalComdatOp =
185       builder.create<ComdatOp>(mlirModule.getLoc(), getGlobalComdatOpName());
186   globalInsertionOp = globalComdatOp;
187   return globalComdatOp;
188 }
189 
190 LogicalResult ModuleImport::processTBAAMetadata(const llvm::MDNode *node) {
191   Location loc = mlirModule.getLoc();
192 
193   // If `node` is a valid TBAA root node, then return its optional identity
194   // string, otherwise return failure.
195   auto getIdentityIfRootNode =
196       [&](const llvm::MDNode *node) -> FailureOr<std::optional<StringRef>> {
197     // Root node, e.g.:
198     //   !0 = !{!"Simple C/C++ TBAA"}
199     //   !1 = !{}
200     if (node->getNumOperands() > 1)
201       return failure();
202     // If the operand is MDString, then assume that this is a root node.
203     if (node->getNumOperands() == 1)
204       if (const auto *op0 = dyn_cast<const llvm::MDString>(node->getOperand(0)))
205         return std::optional<StringRef>{op0->getString()};
206     return std::optional<StringRef>{};
207   };
208 
209   // If `node` looks like a TBAA type descriptor metadata,
210   // then return true, if it is a valid node, and false otherwise.
211   // If it does not look like a TBAA type descriptor metadata, then
212   // return std::nullopt.
213   // If `identity` and `memberTypes/Offsets` are non-null, then they will
214   // contain the converted metadata operands for a valid TBAA node (i.e. when
215   // true is returned).
216   auto isTypeDescriptorNode = [&](const llvm::MDNode *node,
217                                   StringRef *identity = nullptr,
218                                   SmallVectorImpl<TBAAMemberAttr> *members =
219                                       nullptr) -> std::optional<bool> {
220     unsigned numOperands = node->getNumOperands();
221     // Type descriptor, e.g.:
222     //   !1 = !{!"int", !0, /*optional*/i64 0} /* scalar int type */
223     //   !2 = !{!"agg_t", !1, i64 0} /* struct agg_t { int x; } */
224     if (numOperands < 2)
225       return std::nullopt;
226 
227     // TODO: support "new" format (D41501) for type descriptors,
228     //       where the first operand is an MDNode.
229     const auto *identityNode =
230         dyn_cast<const llvm::MDString>(node->getOperand(0));
231     if (!identityNode)
232       return std::nullopt;
233 
234     // This should be a type descriptor node.
235     if (identity)
236       *identity = identityNode->getString();
237 
238     for (unsigned pairNum = 0, e = numOperands / 2; pairNum < e; ++pairNum) {
239       const auto *memberNode =
240           dyn_cast<const llvm::MDNode>(node->getOperand(2 * pairNum + 1));
241       if (!memberNode) {
242         emitError(loc) << "operand '" << 2 * pairNum + 1 << "' must be MDNode: "
243                        << diagMD(node, llvmModule.get());
244         return false;
245       }
246       int64_t offset = 0;
247       if (2 * pairNum + 2 >= numOperands) {
248         // Allow for optional 0 offset in 2-operand nodes.
249         if (numOperands != 2) {
250           emitError(loc) << "missing member offset: "
251                          << diagMD(node, llvmModule.get());
252           return false;
253         }
254       } else {
255         auto *offsetCI = llvm::mdconst::dyn_extract<llvm::ConstantInt>(
256             node->getOperand(2 * pairNum + 2));
257         if (!offsetCI) {
258           emitError(loc) << "operand '" << 2 * pairNum + 2
259                          << "' must be ConstantInt: "
260                          << diagMD(node, llvmModule.get());
261           return false;
262         }
263         offset = offsetCI->getZExtValue();
264       }
265 
266       if (members)
267         members->push_back(TBAAMemberAttr::get(
268             cast<TBAANodeAttr>(tbaaMapping.lookup(memberNode)), offset));
269     }
270 
271     return true;
272   };
273 
274   // If `node` looks like a TBAA access tag metadata,
275   // then return true, if it is a valid node, and false otherwise.
276   // If it does not look like a TBAA access tag metadata, then
277   // return std::nullopt.
278   // If the other arguments are non-null, then they will contain
279   // the converted metadata operands for a valid TBAA node (i.e. when true is
280   // returned).
281   auto isTagNode = [&](const llvm::MDNode *node,
282                        TBAATypeDescriptorAttr *baseAttr = nullptr,
283                        TBAATypeDescriptorAttr *accessAttr = nullptr,
284                        int64_t *offset = nullptr,
285                        bool *isConstant = nullptr) -> std::optional<bool> {
286     // Access tag, e.g.:
287     //   !3 = !{!1, !1, i64 0} /* scalar int access */
288     //   !4 = !{!2, !1, i64 0} /* agg_t::x access */
289     //
290     // Optional 4th argument is ConstantInt 0/1 identifying whether
291     // the location being accessed is "constant" (see for details:
292     // https://llvm.org/docs/LangRef.html#representation).
293     unsigned numOperands = node->getNumOperands();
294     if (numOperands != 3 && numOperands != 4)
295       return std::nullopt;
296     const auto *baseMD = dyn_cast<const llvm::MDNode>(node->getOperand(0));
297     const auto *accessMD = dyn_cast<const llvm::MDNode>(node->getOperand(1));
298     auto *offsetCI =
299         llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(2));
300     if (!baseMD || !accessMD || !offsetCI)
301       return std::nullopt;
302     // TODO: support "new" TBAA format, if needed (see D41501).
303     // In the "old" format the first operand of the access type
304     // metadata is MDString. We have to distinguish the formats,
305     // because access tags have the same structure, but different
306     // meaning for the operands.
307     if (accessMD->getNumOperands() < 1 ||
308         !isa<llvm::MDString>(accessMD->getOperand(0)))
309       return std::nullopt;
310     bool isConst = false;
311     if (numOperands == 4) {
312       auto *isConstantCI =
313           llvm::mdconst::dyn_extract<llvm::ConstantInt>(node->getOperand(3));
314       if (!isConstantCI) {
315         emitError(loc) << "operand '3' must be ConstantInt: "
316                        << diagMD(node, llvmModule.get());
317         return false;
318       }
319       isConst = isConstantCI->getValue()[0];
320     }
321     if (baseAttr)
322       *baseAttr = cast<TBAATypeDescriptorAttr>(tbaaMapping.lookup(baseMD));
323     if (accessAttr)
324       *accessAttr = cast<TBAATypeDescriptorAttr>(tbaaMapping.lookup(accessMD));
325     if (offset)
326       *offset = offsetCI->getZExtValue();
327     if (isConstant)
328       *isConstant = isConst;
329     return true;
330   };
331 
332   // Do a post-order walk over the TBAA Graph. Since a correct TBAA Graph is a
333   // DAG, a post-order walk guarantees that we convert any metadata node we
334   // depend on, prior to converting the current node.
335   DenseSet<const llvm::MDNode *> seen;
336   SmallVector<const llvm::MDNode *> workList;
337   workList.push_back(node);
338   while (!workList.empty()) {
339     const llvm::MDNode *current = workList.back();
340     if (tbaaMapping.contains(current)) {
341       // Already converted. Just pop from the worklist.
342       workList.pop_back();
343       continue;
344     }
345 
346     // If any child of this node is not yet converted, don't pop the current
347     // node from the worklist but push the not-yet-converted children in the
348     // front of the worklist.
349     bool anyChildNotConverted = false;
350     for (const llvm::MDOperand &operand : current->operands())
351       if (auto *childNode = dyn_cast_or_null<const llvm::MDNode>(operand.get()))
352         if (!tbaaMapping.contains(childNode)) {
353           workList.push_back(childNode);
354           anyChildNotConverted = true;
355         }
356 
357     if (anyChildNotConverted) {
358       // If this is the second time we failed to convert an element in the
359       // worklist it must be because a child is dependent on it being converted
360       // and we have a cycle in the graph. Cycles are not allowed in TBAA
361       // graphs.
362       if (!seen.insert(current).second)
363         return emitError(loc) << "has cycle in TBAA graph: "
364                               << diagMD(current, llvmModule.get());
365 
366       continue;
367     }
368 
369     // Otherwise simply import the current node.
370     workList.pop_back();
371 
372     FailureOr<std::optional<StringRef>> rootNodeIdentity =
373         getIdentityIfRootNode(current);
374     if (succeeded(rootNodeIdentity)) {
375       StringAttr stringAttr = *rootNodeIdentity
376                                   ? builder.getStringAttr(**rootNodeIdentity)
377                                   : nullptr;
378       // The root nodes do not have operands, so we can create
379       // the TBAARootAttr on the first walk.
380       tbaaMapping.insert({current, builder.getAttr<TBAARootAttr>(stringAttr)});
381       continue;
382     }
383 
384     StringRef identity;
385     SmallVector<TBAAMemberAttr> members;
386     if (std::optional<bool> isValid =
387             isTypeDescriptorNode(current, &identity, &members)) {
388       assert(isValid.value() && "type descriptor node must be valid");
389 
390       tbaaMapping.insert({current, builder.getAttr<TBAATypeDescriptorAttr>(
391                                        identity, members)});
392       continue;
393     }
394 
395     TBAATypeDescriptorAttr baseAttr, accessAttr;
396     int64_t offset;
397     bool isConstant;
398     if (std::optional<bool> isValid =
399             isTagNode(current, &baseAttr, &accessAttr, &offset, &isConstant)) {
400       assert(isValid.value() && "access tag node must be valid");
401       tbaaMapping.insert(
402           {current, builder.getAttr<TBAATagAttr>(baseAttr, accessAttr, offset,
403                                                  isConstant)});
404       continue;
405     }
406 
407     return emitError(loc) << "unsupported TBAA node format: "
408                           << diagMD(current, llvmModule.get());
409   }
410   return success();
411 }
412 
413 LogicalResult
414 ModuleImport::processAccessGroupMetadata(const llvm::MDNode *node) {
415   Location loc = mlirModule.getLoc();
416   if (failed(loopAnnotationImporter->translateAccessGroup(node, loc)))
417     return emitError(loc) << "unsupported access group node: "
418                           << diagMD(node, llvmModule.get());
419   return success();
420 }
421 
422 LogicalResult
423 ModuleImport::processAliasScopeMetadata(const llvm::MDNode *node) {
424   Location loc = mlirModule.getLoc();
425   // Helper that verifies the node has a self reference operand.
426   auto verifySelfRef = [](const llvm::MDNode *node) {
427     return node->getNumOperands() != 0 &&
428            node == dyn_cast<llvm::MDNode>(node->getOperand(0));
429   };
430   // Helper that verifies the given operand is a string or does not exist.
431   auto verifyDescription = [](const llvm::MDNode *node, unsigned idx) {
432     return idx >= node->getNumOperands() ||
433            isa<llvm::MDString>(node->getOperand(idx));
434   };
435   // Helper that creates an alias scope domain attribute.
436   auto createAliasScopeDomainOp = [&](const llvm::MDNode *aliasDomain) {
437     StringAttr description = nullptr;
438     if (aliasDomain->getNumOperands() >= 2)
439       if (auto *operand = dyn_cast<llvm::MDString>(aliasDomain->getOperand(1)))
440         description = builder.getStringAttr(operand->getString());
441     return builder.getAttr<AliasScopeDomainAttr>(
442         DistinctAttr::create(builder.getUnitAttr()), description);
443   };
444 
445   // Collect the alias scopes and domains to translate them.
446   for (const llvm::MDOperand &operand : node->operands()) {
447     if (const auto *scope = dyn_cast<llvm::MDNode>(operand)) {
448       llvm::AliasScopeNode aliasScope(scope);
449       const llvm::MDNode *domain = aliasScope.getDomain();
450 
451       // Verify the scope node points to valid scope metadata which includes
452       // verifying its domain. Perform the verification before looking it up in
453       // the alias scope mapping since it could have been inserted as a domain
454       // node before.
455       if (!verifySelfRef(scope) || !domain || !verifyDescription(scope, 2))
456         return emitError(loc) << "unsupported alias scope node: "
457                               << diagMD(scope, llvmModule.get());
458       if (!verifySelfRef(domain) || !verifyDescription(domain, 1))
459         return emitError(loc) << "unsupported alias domain node: "
460                               << diagMD(domain, llvmModule.get());
461 
462       if (aliasScopeMapping.contains(scope))
463         continue;
464 
465       // Convert the domain metadata node if it has not been translated before.
466       auto it = aliasScopeMapping.find(aliasScope.getDomain());
467       if (it == aliasScopeMapping.end()) {
468         auto aliasScopeDomainOp = createAliasScopeDomainOp(domain);
469         it = aliasScopeMapping.try_emplace(domain, aliasScopeDomainOp).first;
470       }
471 
472       // Convert the scope metadata node if it has not been converted before.
473       StringAttr description = nullptr;
474       if (!aliasScope.getName().empty())
475         description = builder.getStringAttr(aliasScope.getName());
476       auto aliasScopeOp = builder.getAttr<AliasScopeAttr>(
477           DistinctAttr::create(builder.getUnitAttr()),
478           cast<AliasScopeDomainAttr>(it->second), description);
479       aliasScopeMapping.try_emplace(aliasScope.getNode(), aliasScopeOp);
480     }
481   }
482   return success();
483 }
484 
485 FailureOr<SmallVector<AliasScopeAttr>>
486 ModuleImport::lookupAliasScopeAttrs(const llvm::MDNode *node) const {
487   SmallVector<AliasScopeAttr> aliasScopes;
488   aliasScopes.reserve(node->getNumOperands());
489   for (const llvm::MDOperand &operand : node->operands()) {
490     auto *node = cast<llvm::MDNode>(operand.get());
491     aliasScopes.push_back(
492         dyn_cast_or_null<AliasScopeAttr>(aliasScopeMapping.lookup(node)));
493   }
494   // Return failure if one of the alias scope lookups failed.
495   if (llvm::is_contained(aliasScopes, nullptr))
496     return failure();
497   return aliasScopes;
498 }
499 
500 void ModuleImport::addDebugIntrinsic(llvm::CallInst *intrinsic) {
501   debugIntrinsics.insert(intrinsic);
502 }
503 
504 LogicalResult ModuleImport::convertLinkerOptionsMetadata() {
505   for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
506     if (named.getName() != "llvm.linker.options")
507       continue;
508     // llvm.linker.options operands are lists of strings.
509     for (const llvm::MDNode *node : named.operands()) {
510       SmallVector<StringRef> options;
511       options.reserve(node->getNumOperands());
512       for (const llvm::MDOperand &option : node->operands())
513         options.push_back(cast<llvm::MDString>(option)->getString());
514       builder.create<LLVM::LinkerOptionsOp>(mlirModule.getLoc(),
515                                             builder.getStrArrayAttr(options));
516     }
517   }
518   return success();
519 }
520 
521 LogicalResult ModuleImport::convertIdentMetadata() {
522   for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) {
523     // llvm.ident should have a single operand. That operand is itself an
524     // MDNode with a single string operand.
525     if (named.getName() != LLVMDialect::getIdentAttrName())
526       continue;
527 
528     if (named.getNumOperands() == 1)
529       if (auto *md = dyn_cast<llvm::MDNode>(named.getOperand(0)))
530         if (md->getNumOperands() == 1)
531           if (auto *mdStr = dyn_cast<llvm::MDString>(md->getOperand(0)))
532             mlirModule->setAttr(LLVMDialect::getIdentAttrName(),
533                                 builder.getStringAttr(mdStr->getString()));
534   }
535   return success();
536 }
537 
538 LogicalResult ModuleImport::convertCommandlineMetadata() {
539   for (const llvm::NamedMDNode &nmd : llvmModule->named_metadata()) {
540     // llvm.commandline should have a single operand. That operand is itself an
541     // MDNode with a single string operand.
542     if (nmd.getName() != LLVMDialect::getCommandlineAttrName())
543       continue;
544 
545     if (nmd.getNumOperands() == 1)
546       if (auto *md = dyn_cast<llvm::MDNode>(nmd.getOperand(0)))
547         if (md->getNumOperands() == 1)
548           if (auto *mdStr = dyn_cast<llvm::MDString>(md->getOperand(0)))
549             mlirModule->setAttr(LLVMDialect::getCommandlineAttrName(),
550                                 builder.getStringAttr(mdStr->getString()));
551   }
552   return success();
553 }
554 
555 LogicalResult ModuleImport::convertMetadata() {
556   OpBuilder::InsertionGuard guard(builder);
557   builder.setInsertionPointToEnd(mlirModule.getBody());
558   for (const llvm::Function &func : llvmModule->functions()) {
559     for (const llvm::Instruction &inst : llvm::instructions(func)) {
560       // Convert access group metadata nodes.
561       if (llvm::MDNode *node =
562               inst.getMetadata(llvm::LLVMContext::MD_access_group))
563         if (failed(processAccessGroupMetadata(node)))
564           return failure();
565 
566       // Convert alias analysis metadata nodes.
567       llvm::AAMDNodes aliasAnalysisNodes = inst.getAAMetadata();
568       if (!aliasAnalysisNodes)
569         continue;
570       if (aliasAnalysisNodes.TBAA)
571         if (failed(processTBAAMetadata(aliasAnalysisNodes.TBAA)))
572           return failure();
573       if (aliasAnalysisNodes.Scope)
574         if (failed(processAliasScopeMetadata(aliasAnalysisNodes.Scope)))
575           return failure();
576       if (aliasAnalysisNodes.NoAlias)
577         if (failed(processAliasScopeMetadata(aliasAnalysisNodes.NoAlias)))
578           return failure();
579     }
580   }
581   if (failed(convertLinkerOptionsMetadata()))
582     return failure();
583   if (failed(convertIdentMetadata()))
584     return failure();
585   if (failed(convertCommandlineMetadata()))
586     return failure();
587   return success();
588 }
589 
590 void ModuleImport::processComdat(const llvm::Comdat *comdat) {
591   if (comdatMapping.contains(comdat))
592     return;
593 
594   ComdatOp comdatOp = getGlobalComdatOp();
595   OpBuilder::InsertionGuard guard(builder);
596   builder.setInsertionPointToEnd(&comdatOp.getBody().back());
597   auto selectorOp = builder.create<ComdatSelectorOp>(
598       mlirModule.getLoc(), comdat->getName(),
599       convertComdatFromLLVM(comdat->getSelectionKind()));
600   auto symbolRef =
601       SymbolRefAttr::get(builder.getContext(), getGlobalComdatOpName(),
602                          FlatSymbolRefAttr::get(selectorOp.getSymNameAttr()));
603   comdatMapping.try_emplace(comdat, symbolRef);
604 }
605 
606 LogicalResult ModuleImport::convertComdats() {
607   for (llvm::GlobalVariable &globalVar : llvmModule->globals())
608     if (globalVar.hasComdat())
609       processComdat(globalVar.getComdat());
610   for (llvm::Function &func : llvmModule->functions())
611     if (func.hasComdat())
612       processComdat(func.getComdat());
613   return success();
614 }
615 
616 LogicalResult ModuleImport::convertGlobals() {
617   for (llvm::GlobalVariable &globalVar : llvmModule->globals()) {
618     if (globalVar.getName() == getGlobalCtorsVarName() ||
619         globalVar.getName() == getGlobalDtorsVarName()) {
620       if (failed(convertGlobalCtorsAndDtors(&globalVar))) {
621         return emitError(UnknownLoc::get(context))
622                << "unhandled global variable: " << diag(globalVar);
623       }
624       continue;
625     }
626     if (failed(convertGlobal(&globalVar))) {
627       return emitError(UnknownLoc::get(context))
628              << "unhandled global variable: " << diag(globalVar);
629     }
630   }
631   return success();
632 }
633 
634 LogicalResult ModuleImport::convertDataLayout() {
635   Location loc = mlirModule.getLoc();
636   DataLayoutImporter dataLayoutImporter(context, llvmModule->getDataLayout());
637   if (!dataLayoutImporter.getDataLayout())
638     return emitError(loc, "cannot translate data layout: ")
639            << dataLayoutImporter.getLastToken();
640 
641   for (StringRef token : dataLayoutImporter.getUnhandledTokens())
642     emitWarning(loc, "unhandled data layout token: ") << token;
643 
644   mlirModule->setAttr(DLTIDialect::kDataLayoutAttrName,
645                       dataLayoutImporter.getDataLayout());
646   return success();
647 }
648 
649 LogicalResult ModuleImport::convertFunctions() {
650   for (llvm::Function &func : llvmModule->functions())
651     if (failed(processFunction(&func)))
652       return failure();
653   return success();
654 }
655 
656 void ModuleImport::setNonDebugMetadataAttrs(llvm::Instruction *inst,
657                                             Operation *op) {
658   SmallVector<std::pair<unsigned, llvm::MDNode *>> allMetadata;
659   inst->getAllMetadataOtherThanDebugLoc(allMetadata);
660   for (auto &[kind, node] : allMetadata) {
661     if (!iface.isConvertibleMetadata(kind))
662       continue;
663     if (failed(iface.setMetadataAttrs(builder, kind, node, op, *this))) {
664       if (emitExpensiveWarnings) {
665         Location loc = debugImporter->translateLoc(inst->getDebugLoc());
666         emitWarning(loc) << "unhandled metadata: "
667                          << diagMD(node, llvmModule.get()) << " on "
668                          << diag(*inst);
669       }
670     }
671   }
672 }
673 
674 void ModuleImport::setIntegerOverflowFlags(llvm::Instruction *inst,
675                                            Operation *op) const {
676   auto iface = cast<IntegerOverflowFlagsInterface>(op);
677 
678   IntegerOverflowFlags value = {};
679   value = bitEnumSet(value, IntegerOverflowFlags::nsw, inst->hasNoSignedWrap());
680   value =
681       bitEnumSet(value, IntegerOverflowFlags::nuw, inst->hasNoUnsignedWrap());
682 
683   iface.setOverflowFlags(value);
684 }
685 
686 void ModuleImport::setExactFlag(llvm::Instruction *inst, Operation *op) const {
687   auto iface = cast<ExactFlagInterface>(op);
688 
689   iface.setIsExact(inst->isExact());
690 }
691 
692 void ModuleImport::setNonNegFlag(llvm::Instruction *inst, Operation *op) const {
693   auto iface = cast<NonNegFlagInterface>(op);
694 
695   iface.setNonNeg(inst->hasNonNeg());
696 }
697 
698 void ModuleImport::setFastmathFlagsAttr(llvm::Instruction *inst,
699                                         Operation *op) const {
700   auto iface = cast<FastmathFlagsInterface>(op);
701 
702   // Even if the imported operation implements the fastmath interface, the
703   // original instruction may not have fastmath flags set. Exit if an
704   // instruction, such as a non floating-point function call, does not have
705   // fastmath flags.
706   if (!isa<llvm::FPMathOperator>(inst))
707     return;
708   llvm::FastMathFlags flags = inst->getFastMathFlags();
709 
710   // Set the fastmath bits flag-by-flag.
711   FastmathFlags value = {};
712   value = bitEnumSet(value, FastmathFlags::nnan, flags.noNaNs());
713   value = bitEnumSet(value, FastmathFlags::ninf, flags.noInfs());
714   value = bitEnumSet(value, FastmathFlags::nsz, flags.noSignedZeros());
715   value = bitEnumSet(value, FastmathFlags::arcp, flags.allowReciprocal());
716   value = bitEnumSet(value, FastmathFlags::contract, flags.allowContract());
717   value = bitEnumSet(value, FastmathFlags::afn, flags.approxFunc());
718   value = bitEnumSet(value, FastmathFlags::reassoc, flags.allowReassoc());
719   FastmathFlagsAttr attr = FastmathFlagsAttr::get(builder.getContext(), value);
720   iface->setAttr(iface.getFastmathAttrName(), attr);
721 }
722 
723 /// Returns if `type` is a scalar integer or floating-point type.
724 static bool isScalarType(Type type) {
725   return isa<IntegerType, FloatType>(type);
726 }
727 
728 /// Returns `type` if it is a builtin integer or floating-point vector type that
729 /// can be used to create an attribute or nullptr otherwise. If provided,
730 /// `arrayShape` is added to the shape of the vector to create an attribute that
731 /// matches an array of vectors.
732 static Type getVectorTypeForAttr(Type type, ArrayRef<int64_t> arrayShape = {}) {
733   if (!LLVM::isCompatibleVectorType(type))
734     return {};
735 
736   llvm::ElementCount numElements = LLVM::getVectorNumElements(type);
737   if (numElements.isScalable()) {
738     emitError(UnknownLoc::get(type.getContext()))
739         << "scalable vectors not supported";
740     return {};
741   }
742 
743   // An LLVM dialect vector can only contain scalars.
744   Type elementType = LLVM::getVectorElementType(type);
745   if (!isScalarType(elementType))
746     return {};
747 
748   SmallVector<int64_t> shape(arrayShape);
749   shape.push_back(numElements.getKnownMinValue());
750   return VectorType::get(shape, elementType);
751 }
752 
753 Type ModuleImport::getBuiltinTypeForAttr(Type type) {
754   if (!type)
755     return {};
756 
757   // Return builtin integer and floating-point types as is.
758   if (isScalarType(type))
759     return type;
760 
761   // Return builtin vectors of integer and floating-point types as is.
762   if (Type vectorType = getVectorTypeForAttr(type))
763     return vectorType;
764 
765   // Multi-dimensional array types are converted to tensors or vectors,
766   // depending on the innermost type being a scalar or a vector.
767   SmallVector<int64_t> arrayShape;
768   while (auto arrayType = dyn_cast<LLVMArrayType>(type)) {
769     arrayShape.push_back(arrayType.getNumElements());
770     type = arrayType.getElementType();
771   }
772   if (isScalarType(type))
773     return RankedTensorType::get(arrayShape, type);
774   return getVectorTypeForAttr(type, arrayShape);
775 }
776 
777 /// Returns an integer or float attribute for the provided scalar constant
778 /// `constScalar` or nullptr if the conversion fails.
779 static TypedAttr getScalarConstantAsAttr(OpBuilder &builder,
780                                          llvm::Constant *constScalar) {
781   MLIRContext *context = builder.getContext();
782 
783   // Convert scalar intergers.
784   if (auto *constInt = dyn_cast<llvm::ConstantInt>(constScalar)) {
785     return builder.getIntegerAttr(
786         IntegerType::get(context, constInt->getBitWidth()),
787         constInt->getValue());
788   }
789 
790   // Convert scalar floats.
791   if (auto *constFloat = dyn_cast<llvm::ConstantFP>(constScalar)) {
792     llvm::Type *type = constFloat->getType();
793     FloatType floatType =
794         type->isBFloatTy()
795             ? FloatType::getBF16(context)
796             : LLVM::detail::getFloatType(context, type->getScalarSizeInBits());
797     if (!floatType) {
798       emitError(UnknownLoc::get(builder.getContext()))
799           << "unexpected floating-point type";
800       return {};
801     }
802     return builder.getFloatAttr(floatType, constFloat->getValueAPF());
803   }
804   return {};
805 }
806 
807 /// Returns an integer or float attribute array for the provided constant
808 /// sequence `constSequence` or nullptr if the conversion fails.
809 static SmallVector<Attribute>
810 getSequenceConstantAsAttrs(OpBuilder &builder,
811                            llvm::ConstantDataSequential *constSequence) {
812   SmallVector<Attribute> elementAttrs;
813   elementAttrs.reserve(constSequence->getNumElements());
814   for (auto idx : llvm::seq<int64_t>(0, constSequence->getNumElements())) {
815     llvm::Constant *constElement = constSequence->getElementAsConstant(idx);
816     elementAttrs.push_back(getScalarConstantAsAttr(builder, constElement));
817   }
818   return elementAttrs;
819 }
820 
821 Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
822   // Convert scalar constants.
823   if (Attribute scalarAttr = getScalarConstantAsAttr(builder, constant))
824     return scalarAttr;
825 
826   // Returns the static shape of the provided type if possible.
827   auto getConstantShape = [&](llvm::Type *type) {
828     return llvm::dyn_cast_if_present<ShapedType>(
829         getBuiltinTypeForAttr(convertType(type)));
830   };
831 
832   // Convert one-dimensional constant arrays or vectors that store 1/2/4/8-byte
833   // integer or half/bfloat/float/double values.
834   if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(constant)) {
835     if (constArray->isString())
836       return builder.getStringAttr(constArray->getAsString());
837     auto shape = getConstantShape(constArray->getType());
838     if (!shape)
839       return {};
840     // Convert splat constants to splat elements attributes.
841     auto *constVector = dyn_cast<llvm::ConstantDataVector>(constant);
842     if (constVector && constVector->isSplat()) {
843       // A vector is guaranteed to have at least size one.
844       Attribute splatAttr = getScalarConstantAsAttr(
845           builder, constVector->getElementAsConstant(0));
846       return SplatElementsAttr::get(shape, splatAttr);
847     }
848     // Convert non-splat constants to dense elements attributes.
849     SmallVector<Attribute> elementAttrs =
850         getSequenceConstantAsAttrs(builder, constArray);
851     return DenseElementsAttr::get(shape, elementAttrs);
852   }
853 
854   // Convert multi-dimensional constant aggregates that store all kinds of
855   // integer and floating-point types.
856   if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(constant)) {
857     auto shape = getConstantShape(constAggregate->getType());
858     if (!shape)
859       return {};
860     // Collect the aggregate elements in depths first order.
861     SmallVector<Attribute> elementAttrs;
862     SmallVector<llvm::Constant *> workList = {constAggregate};
863     while (!workList.empty()) {
864       llvm::Constant *current = workList.pop_back_val();
865       // Append any nested aggregates in reverse order to ensure the head
866       // element of the nested aggregates is at the back of the work list.
867       if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(current)) {
868         for (auto idx :
869              reverse(llvm::seq<int64_t>(0, constAggregate->getNumOperands())))
870           workList.push_back(constAggregate->getAggregateElement(idx));
871         continue;
872       }
873       // Append the elements of nested constant arrays or vectors that store
874       // 1/2/4/8-byte integer or half/bfloat/float/double values.
875       if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(current)) {
876         SmallVector<Attribute> attrs =
877             getSequenceConstantAsAttrs(builder, constArray);
878         elementAttrs.append(attrs.begin(), attrs.end());
879         continue;
880       }
881       // Append nested scalar constants that store all kinds of integer and
882       // floating-point types.
883       if (Attribute scalarAttr = getScalarConstantAsAttr(builder, current)) {
884         elementAttrs.push_back(scalarAttr);
885         continue;
886       }
887       // Bail if the aggregate contains a unsupported constant type such as a
888       // constant expression.
889       return {};
890     }
891     return DenseElementsAttr::get(shape, elementAttrs);
892   }
893 
894   // Convert zero aggregates.
895   if (auto *constZero = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
896     auto shape = llvm::dyn_cast_if_present<ShapedType>(
897         getBuiltinTypeForAttr(convertType(constZero->getType())));
898     if (!shape)
899       return {};
900     // Convert zero aggregates with a static shape to splat elements attributes.
901     Attribute splatAttr = builder.getZeroAttr(shape.getElementType());
902     assert(splatAttr && "expected non-null zero attribute for scalar types");
903     return SplatElementsAttr::get(shape, splatAttr);
904   }
905   return {};
906 }
907 
908 FlatSymbolRefAttr
909 ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) {
910   assert(globalVar->getName().empty() &&
911          "expected to work with a nameless global");
912   auto [it, success] = namelessGlobals.try_emplace(globalVar);
913   if (!success)
914     return it->second;
915 
916   // Make sure the symbol name does not clash with an existing symbol.
917   SmallString<128> globalName = SymbolTable::generateSymbolName<128>(
918       getNamelessGlobalPrefix(),
919       [this](StringRef newName) { return llvmModule->getNamedValue(newName); },
920       namelessGlobalId);
921   auto symbolRef = FlatSymbolRefAttr::get(context, globalName);
922   it->getSecond() = symbolRef;
923   return symbolRef;
924 }
925 
926 LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
927   // Insert the global after the last one or at the start of the module.
928   OpBuilder::InsertionGuard guard(builder);
929   if (!globalInsertionOp)
930     builder.setInsertionPointToStart(mlirModule.getBody());
931   else
932     builder.setInsertionPointAfter(globalInsertionOp);
933 
934   Attribute valueAttr;
935   if (globalVar->hasInitializer())
936     valueAttr = getConstantAsAttr(globalVar->getInitializer());
937   Type type = convertType(globalVar->getValueType());
938 
939   uint64_t alignment = 0;
940   llvm::MaybeAlign maybeAlign = globalVar->getAlign();
941   if (maybeAlign.has_value()) {
942     llvm::Align align = *maybeAlign;
943     alignment = align.value();
944   }
945 
946   // Get the global expression associated with this global variable and convert
947   // it.
948   SmallVector<Attribute> globalExpressionAttrs;
949   SmallVector<llvm::DIGlobalVariableExpression *> globalExpressions;
950   globalVar->getDebugInfo(globalExpressions);
951 
952   for (llvm::DIGlobalVariableExpression *expr : globalExpressions) {
953     DIGlobalVariableExpressionAttr globalExpressionAttr =
954         debugImporter->translateGlobalVariableExpression(expr);
955     globalExpressionAttrs.push_back(globalExpressionAttr);
956   }
957 
958   // Workaround to support LLVM's nameless globals. MLIR, in contrast to LLVM,
959   // always requires a symbol name.
960   StringRef globalName = globalVar->getName();
961   if (globalName.empty())
962     globalName = getOrCreateNamelessSymbolName(globalVar).getValue();
963 
964   GlobalOp globalOp = builder.create<GlobalOp>(
965       mlirModule.getLoc(), type, globalVar->isConstant(),
966       convertLinkageFromLLVM(globalVar->getLinkage()), StringRef(globalName),
967       valueAttr, alignment, /*addr_space=*/globalVar->getAddressSpace(),
968       /*dso_local=*/globalVar->isDSOLocal(),
969       /*thread_local=*/globalVar->isThreadLocal(), /*comdat=*/SymbolRefAttr(),
970       /*attrs=*/ArrayRef<NamedAttribute>(), /*dbgExprs=*/globalExpressionAttrs);
971   globalInsertionOp = globalOp;
972 
973   if (globalVar->hasInitializer() && !valueAttr) {
974     clearRegionState();
975     Block *block = builder.createBlock(&globalOp.getInitializerRegion());
976     setConstantInsertionPointToStart(block);
977     FailureOr<Value> initializer =
978         convertConstantExpr(globalVar->getInitializer());
979     if (failed(initializer))
980       return failure();
981     builder.create<ReturnOp>(globalOp.getLoc(), *initializer);
982   }
983   if (globalVar->hasAtLeastLocalUnnamedAddr()) {
984     globalOp.setUnnamedAddr(
985         convertUnnamedAddrFromLLVM(globalVar->getUnnamedAddr()));
986   }
987   if (globalVar->hasSection())
988     globalOp.setSection(globalVar->getSection());
989   globalOp.setVisibility_(
990       convertVisibilityFromLLVM(globalVar->getVisibility()));
991 
992   if (globalVar->hasComdat())
993     globalOp.setComdatAttr(comdatMapping.lookup(globalVar->getComdat()));
994 
995   return success();
996 }
997 
998 LogicalResult
999 ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) {
1000   if (!globalVar->hasInitializer() || !globalVar->hasAppendingLinkage())
1001     return failure();
1002   auto *initializer =
1003       dyn_cast<llvm::ConstantArray>(globalVar->getInitializer());
1004   if (!initializer)
1005     return failure();
1006 
1007   SmallVector<Attribute> funcs;
1008   SmallVector<int32_t> priorities;
1009   for (llvm::Value *operand : initializer->operands()) {
1010     auto *aggregate = dyn_cast<llvm::ConstantAggregate>(operand);
1011     if (!aggregate || aggregate->getNumOperands() != 3)
1012       return failure();
1013 
1014     auto *priority = dyn_cast<llvm::ConstantInt>(aggregate->getOperand(0));
1015     auto *func = dyn_cast<llvm::Function>(aggregate->getOperand(1));
1016     auto *data = dyn_cast<llvm::Constant>(aggregate->getOperand(2));
1017     if (!priority || !func || !data)
1018       return failure();
1019 
1020     // GlobalCtorsOps and GlobalDtorsOps do not support non-null data fields.
1021     if (!data->isNullValue())
1022       return failure();
1023 
1024     funcs.push_back(FlatSymbolRefAttr::get(context, func->getName()));
1025     priorities.push_back(priority->getValue().getZExtValue());
1026   }
1027 
1028   OpBuilder::InsertionGuard guard(builder);
1029   if (!globalInsertionOp)
1030     builder.setInsertionPointToStart(mlirModule.getBody());
1031   else
1032     builder.setInsertionPointAfter(globalInsertionOp);
1033 
1034   if (globalVar->getName() == getGlobalCtorsVarName()) {
1035     globalInsertionOp = builder.create<LLVM::GlobalCtorsOp>(
1036         mlirModule.getLoc(), builder.getArrayAttr(funcs),
1037         builder.getI32ArrayAttr(priorities));
1038     return success();
1039   }
1040   globalInsertionOp = builder.create<LLVM::GlobalDtorsOp>(
1041       mlirModule.getLoc(), builder.getArrayAttr(funcs),
1042       builder.getI32ArrayAttr(priorities));
1043   return success();
1044 }
1045 
1046 SetVector<llvm::Constant *>
1047 ModuleImport::getConstantsToConvert(llvm::Constant *constant) {
1048   // Return the empty set if the constant has been translated before.
1049   if (valueMapping.contains(constant))
1050     return {};
1051 
1052   // Traverse the constants in post-order and stop the traversal if a constant
1053   // already has a `valueMapping` from an earlier constant translation or if the
1054   // constant is traversed a second time.
1055   SetVector<llvm::Constant *> orderedSet;
1056   SetVector<llvm::Constant *> workList;
1057   DenseMap<llvm::Constant *, SmallVector<llvm::Constant *>> adjacencyLists;
1058   workList.insert(constant);
1059   while (!workList.empty()) {
1060     llvm::Constant *current = workList.back();
1061     // References of global objects are just pointers to the object. Avoid
1062     // walking the elements of these here.
1063     if (isa<llvm::GlobalObject>(current)) {
1064       orderedSet.insert(current);
1065       workList.pop_back();
1066       continue;
1067     }
1068 
1069     // Collect all dependencies of the current constant and add them to the
1070     // adjacency list if none has been computed before.
1071     auto [adjacencyIt, inserted] = adjacencyLists.try_emplace(current);
1072     if (inserted) {
1073       // Add all constant operands to the adjacency list and skip any other
1074       // values such as basic block addresses.
1075       for (llvm::Value *operand : current->operands())
1076         if (auto *constDependency = dyn_cast<llvm::Constant>(operand))
1077           adjacencyIt->getSecond().push_back(constDependency);
1078       // Use the getElementValue method to add the dependencies of zero
1079       // initialized aggregate constants since they do not take any operands.
1080       if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(current)) {
1081         unsigned numElements = constAgg->getElementCount().getFixedValue();
1082         for (unsigned i = 0, e = numElements; i != e; ++i)
1083           adjacencyIt->getSecond().push_back(constAgg->getElementValue(i));
1084       }
1085     }
1086     // Add the current constant to the `orderedSet` of the traversed nodes if
1087     // all its dependencies have been traversed before. Additionally, remove the
1088     // constant from the `workList` and continue the traversal.
1089     if (adjacencyIt->getSecond().empty()) {
1090       orderedSet.insert(current);
1091       workList.pop_back();
1092       continue;
1093     }
1094     // Add the next dependency from the adjacency list to the `workList` and
1095     // continue the traversal. Remove the dependency from the adjacency list to
1096     // mark that it has been processed. Only enqueue the dependency if it has no
1097     // `valueMapping` from an earlier translation and if it has not been
1098     // enqueued before.
1099     llvm::Constant *dependency = adjacencyIt->getSecond().pop_back_val();
1100     if (valueMapping.contains(dependency) || workList.contains(dependency) ||
1101         orderedSet.contains(dependency))
1102       continue;
1103     workList.insert(dependency);
1104   }
1105 
1106   return orderedSet;
1107 }
1108 
1109 FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
1110   Location loc = UnknownLoc::get(context);
1111 
1112   // Convert constants that can be represented as attributes.
1113   if (Attribute attr = getConstantAsAttr(constant)) {
1114     Type type = convertType(constant->getType());
1115     if (auto symbolRef = dyn_cast<FlatSymbolRefAttr>(attr)) {
1116       return builder.create<AddressOfOp>(loc, type, symbolRef.getValue())
1117           .getResult();
1118     }
1119     return builder.create<ConstantOp>(loc, type, attr).getResult();
1120   }
1121 
1122   // Convert null pointer constants.
1123   if (auto *nullPtr = dyn_cast<llvm::ConstantPointerNull>(constant)) {
1124     Type type = convertType(nullPtr->getType());
1125     return builder.create<ZeroOp>(loc, type).getResult();
1126   }
1127 
1128   // Convert none token constants.
1129   if (isa<llvm::ConstantTokenNone>(constant)) {
1130     return builder.create<NoneTokenOp>(loc).getResult();
1131   }
1132 
1133   // Convert poison.
1134   if (auto *poisonVal = dyn_cast<llvm::PoisonValue>(constant)) {
1135     Type type = convertType(poisonVal->getType());
1136     return builder.create<PoisonOp>(loc, type).getResult();
1137   }
1138 
1139   // Convert undef.
1140   if (auto *undefVal = dyn_cast<llvm::UndefValue>(constant)) {
1141     Type type = convertType(undefVal->getType());
1142     return builder.create<UndefOp>(loc, type).getResult();
1143   }
1144 
1145   // Convert global variable accesses.
1146   if (auto *globalObj = dyn_cast<llvm::GlobalObject>(constant)) {
1147     Type type = convertType(globalObj->getType());
1148     StringRef globalName = globalObj->getName();
1149     FlatSymbolRefAttr symbolRef;
1150     // Empty names are only allowed for global variables.
1151     if (globalName.empty())
1152       symbolRef =
1153           getOrCreateNamelessSymbolName(cast<llvm::GlobalVariable>(globalObj));
1154     else
1155       symbolRef = FlatSymbolRefAttr::get(context, globalName);
1156     return builder.create<AddressOfOp>(loc, type, symbolRef).getResult();
1157   }
1158 
1159   // Convert constant expressions.
1160   if (auto *constExpr = dyn_cast<llvm::ConstantExpr>(constant)) {
1161     // Convert the constant expression to a temporary LLVM instruction and
1162     // translate it using the `processInstruction` method. Delete the
1163     // instruction after the translation and remove it from `valueMapping`,
1164     // since later calls to `getAsInstruction` may return the same address
1165     // resulting in a conflicting `valueMapping` entry.
1166     llvm::Instruction *inst = constExpr->getAsInstruction();
1167     auto guard = llvm::make_scope_exit([&]() {
1168       assert(!noResultOpMapping.contains(inst) &&
1169              "expected constant expression to return a result");
1170       valueMapping.erase(inst);
1171       inst->deleteValue();
1172     });
1173     // Note: `processInstruction` does not call `convertConstant` recursively
1174     // since all constant dependencies have been converted before.
1175     assert(llvm::all_of(inst->operands(), [&](llvm::Value *value) {
1176       return valueMapping.contains(value);
1177     }));
1178     if (failed(processInstruction(inst)))
1179       return failure();
1180     return lookupValue(inst);
1181   }
1182 
1183   // Convert aggregate constants.
1184   if (isa<llvm::ConstantAggregate>(constant) ||
1185       isa<llvm::ConstantAggregateZero>(constant)) {
1186     // Lookup the aggregate elements that have been converted before.
1187     SmallVector<Value> elementValues;
1188     if (auto *constAgg = dyn_cast<llvm::ConstantAggregate>(constant)) {
1189       elementValues.reserve(constAgg->getNumOperands());
1190       for (llvm::Value *operand : constAgg->operands())
1191         elementValues.push_back(lookupValue(operand));
1192     }
1193     if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
1194       unsigned numElements = constAgg->getElementCount().getFixedValue();
1195       elementValues.reserve(numElements);
1196       for (unsigned i = 0, e = numElements; i != e; ++i)
1197         elementValues.push_back(lookupValue(constAgg->getElementValue(i)));
1198     }
1199     assert(llvm::count(elementValues, nullptr) == 0 &&
1200            "expected all elements have been converted before");
1201 
1202     // Generate an UndefOp as root value and insert the aggregate elements.
1203     Type rootType = convertType(constant->getType());
1204     bool isArrayOrStruct = isa<LLVMArrayType, LLVMStructType>(rootType);
1205     assert((isArrayOrStruct || LLVM::isCompatibleVectorType(rootType)) &&
1206            "unrecognized aggregate type");
1207     Value root = builder.create<UndefOp>(loc, rootType);
1208     for (const auto &it : llvm::enumerate(elementValues)) {
1209       if (isArrayOrStruct) {
1210         root = builder.create<InsertValueOp>(loc, root, it.value(), it.index());
1211       } else {
1212         Attribute indexAttr = builder.getI32IntegerAttr(it.index());
1213         Value indexValue =
1214             builder.create<ConstantOp>(loc, builder.getI32Type(), indexAttr);
1215         root = builder.create<InsertElementOp>(loc, rootType, root, it.value(),
1216                                                indexValue);
1217       }
1218     }
1219     return root;
1220   }
1221 
1222   if (auto *constTargetNone = dyn_cast<llvm::ConstantTargetNone>(constant)) {
1223     LLVMTargetExtType targetExtType =
1224         cast<LLVMTargetExtType>(convertType(constTargetNone->getType()));
1225     assert(targetExtType.hasProperty(LLVMTargetExtType::HasZeroInit) &&
1226            "target extension type does not support zero-initialization");
1227     // Create llvm.mlir.zero operation to represent zero-initialization of
1228     // target extension type.
1229     return builder.create<LLVM::ZeroOp>(loc, targetExtType).getRes();
1230   }
1231 
1232   StringRef error = "";
1233   if (isa<llvm::BlockAddress>(constant))
1234     error = " since blockaddress(...) is unsupported";
1235 
1236   return emitError(loc) << "unhandled constant: " << diag(*constant) << error;
1237 }
1238 
1239 FailureOr<Value> ModuleImport::convertConstantExpr(llvm::Constant *constant) {
1240   // Only call the function for constants that have not been translated before
1241   // since it updates the constant insertion point assuming the converted
1242   // constant has been introduced at the end of the constant section.
1243   assert(!valueMapping.contains(constant) &&
1244          "expected constant has not been converted before");
1245   assert(constantInsertionBlock &&
1246          "expected the constant insertion block to be non-null");
1247 
1248   // Insert the constant after the last one or at the start of the entry block.
1249   OpBuilder::InsertionGuard guard(builder);
1250   if (!constantInsertionOp)
1251     builder.setInsertionPointToStart(constantInsertionBlock);
1252   else
1253     builder.setInsertionPointAfter(constantInsertionOp);
1254 
1255   // Convert all constants of the expression and add them to `valueMapping`.
1256   SetVector<llvm::Constant *> constantsToConvert =
1257       getConstantsToConvert(constant);
1258   for (llvm::Constant *constantToConvert : constantsToConvert) {
1259     FailureOr<Value> converted = convertConstant(constantToConvert);
1260     if (failed(converted))
1261       return failure();
1262     mapValue(constantToConvert, *converted);
1263   }
1264 
1265   // Update the constant insertion point and return the converted constant.
1266   Value result = lookupValue(constant);
1267   constantInsertionOp = result.getDefiningOp();
1268   return result;
1269 }
1270 
1271 FailureOr<Value> ModuleImport::convertValue(llvm::Value *value) {
1272   assert(!isa<llvm::MetadataAsValue>(value) &&
1273          "expected value to not be metadata");
1274 
1275   // Return the mapped value if it has been converted before.
1276   auto it = valueMapping.find(value);
1277   if (it != valueMapping.end())
1278     return it->getSecond();
1279 
1280   // Convert constants such as immediate values that have no mapping yet.
1281   if (auto *constant = dyn_cast<llvm::Constant>(value))
1282     return convertConstantExpr(constant);
1283 
1284   Location loc = UnknownLoc::get(context);
1285   if (auto *inst = dyn_cast<llvm::Instruction>(value))
1286     loc = translateLoc(inst->getDebugLoc());
1287   return emitError(loc) << "unhandled value: " << diag(*value);
1288 }
1289 
1290 FailureOr<Value> ModuleImport::convertMetadataValue(llvm::Value *value) {
1291   // A value may be wrapped as metadata, for example, when passed to a debug
1292   // intrinsic. Unwrap these values before the conversion.
1293   auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(value);
1294   if (!nodeAsVal)
1295     return failure();
1296   auto *node = dyn_cast<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
1297   if (!node)
1298     return failure();
1299   value = node->getValue();
1300 
1301   // Return the mapped value if it has been converted before.
1302   auto it = valueMapping.find(value);
1303   if (it != valueMapping.end())
1304     return it->getSecond();
1305 
1306   // Convert constants such as immediate values that have no mapping yet.
1307   if (auto *constant = dyn_cast<llvm::Constant>(value))
1308     return convertConstantExpr(constant);
1309   return failure();
1310 }
1311 
1312 FailureOr<SmallVector<Value>>
1313 ModuleImport::convertValues(ArrayRef<llvm::Value *> values) {
1314   SmallVector<Value> remapped;
1315   remapped.reserve(values.size());
1316   for (llvm::Value *value : values) {
1317     FailureOr<Value> converted = convertValue(value);
1318     if (failed(converted))
1319       return failure();
1320     remapped.push_back(*converted);
1321   }
1322   return remapped;
1323 }
1324 
1325 LogicalResult ModuleImport::convertIntrinsicArguments(
1326     ArrayRef<llvm::Value *> values, ArrayRef<llvm::OperandBundleUse> opBundles,
1327     bool requiresOpBundles, ArrayRef<unsigned> immArgPositions,
1328     ArrayRef<StringLiteral> immArgAttrNames, SmallVectorImpl<Value> &valuesOut,
1329     SmallVectorImpl<NamedAttribute> &attrsOut) {
1330   assert(immArgPositions.size() == immArgAttrNames.size() &&
1331          "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal "
1332          "length");
1333 
1334   SmallVector<llvm::Value *> operands(values);
1335   for (auto [immArgPos, immArgName] :
1336        llvm::zip(immArgPositions, immArgAttrNames)) {
1337     auto &value = operands[immArgPos];
1338     auto *constant = llvm::cast<llvm::Constant>(value);
1339     auto attr = getScalarConstantAsAttr(builder, constant);
1340     assert(attr && attr.getType().isIntOrFloat() &&
1341            "expected immarg to be float or integer constant");
1342     auto nameAttr = StringAttr::get(attr.getContext(), immArgName);
1343     attrsOut.push_back({nameAttr, attr});
1344     // Mark matched attribute values as null (so they can be removed below).
1345     value = nullptr;
1346   }
1347 
1348   for (llvm::Value *value : operands) {
1349     if (!value)
1350       continue;
1351     auto mlirValue = convertValue(value);
1352     if (failed(mlirValue))
1353       return failure();
1354     valuesOut.push_back(*mlirValue);
1355   }
1356 
1357   SmallVector<int> opBundleSizes;
1358   SmallVector<Attribute> opBundleTagAttrs;
1359   if (requiresOpBundles) {
1360     opBundleSizes.reserve(opBundles.size());
1361     opBundleTagAttrs.reserve(opBundles.size());
1362 
1363     for (const llvm::OperandBundleUse &bundle : opBundles) {
1364       opBundleSizes.push_back(bundle.Inputs.size());
1365       opBundleTagAttrs.push_back(StringAttr::get(context, bundle.getTagName()));
1366 
1367       for (const llvm::Use &opBundleOperand : bundle.Inputs) {
1368         auto operandMlirValue = convertValue(opBundleOperand.get());
1369         if (failed(operandMlirValue))
1370           return failure();
1371         valuesOut.push_back(*operandMlirValue);
1372       }
1373     }
1374 
1375     auto opBundleSizesAttr = DenseI32ArrayAttr::get(context, opBundleSizes);
1376     auto opBundleSizesAttrNameAttr =
1377         StringAttr::get(context, LLVMDialect::getOpBundleSizesAttrName());
1378     attrsOut.push_back({opBundleSizesAttrNameAttr, opBundleSizesAttr});
1379 
1380     auto opBundleTagsAttr = ArrayAttr::get(context, opBundleTagAttrs);
1381     auto opBundleTagsAttrNameAttr =
1382         StringAttr::get(context, LLVMDialect::getOpBundleTagsAttrName());
1383     attrsOut.push_back({opBundleTagsAttrNameAttr, opBundleTagsAttr});
1384   }
1385 
1386   return success();
1387 }
1388 
1389 IntegerAttr ModuleImport::matchIntegerAttr(llvm::Value *value) {
1390   IntegerAttr integerAttr;
1391   FailureOr<Value> converted = convertValue(value);
1392   bool success = succeeded(converted) &&
1393                  matchPattern(*converted, m_Constant(&integerAttr));
1394   assert(success && "expected a constant integer value");
1395   (void)success;
1396   return integerAttr;
1397 }
1398 
1399 FloatAttr ModuleImport::matchFloatAttr(llvm::Value *value) {
1400   FloatAttr floatAttr;
1401   FailureOr<Value> converted = convertValue(value);
1402   bool success =
1403       succeeded(converted) && matchPattern(*converted, m_Constant(&floatAttr));
1404   assert(success && "expected a constant float value");
1405   (void)success;
1406   return floatAttr;
1407 }
1408 
1409 DILocalVariableAttr ModuleImport::matchLocalVariableAttr(llvm::Value *value) {
1410   auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1411   auto *node = cast<llvm::DILocalVariable>(nodeAsVal->getMetadata());
1412   return debugImporter->translate(node);
1413 }
1414 
1415 DILabelAttr ModuleImport::matchLabelAttr(llvm::Value *value) {
1416   auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1417   auto *node = cast<llvm::DILabel>(nodeAsVal->getMetadata());
1418   return debugImporter->translate(node);
1419 }
1420 
1421 FPExceptionBehaviorAttr
1422 ModuleImport::matchFPExceptionBehaviorAttr(llvm::Value *value) {
1423   auto *metadata = cast<llvm::MetadataAsValue>(value);
1424   auto *mdstr = cast<llvm::MDString>(metadata->getMetadata());
1425   std::optional<llvm::fp::ExceptionBehavior> optLLVM =
1426       llvm::convertStrToExceptionBehavior(mdstr->getString());
1427   assert(optLLVM && "Expecting FP exception behavior");
1428   return builder.getAttr<FPExceptionBehaviorAttr>(
1429       convertFPExceptionBehaviorFromLLVM(*optLLVM));
1430 }
1431 
1432 RoundingModeAttr ModuleImport::matchRoundingModeAttr(llvm::Value *value) {
1433   auto *metadata = cast<llvm::MetadataAsValue>(value);
1434   auto *mdstr = cast<llvm::MDString>(metadata->getMetadata());
1435   std::optional<llvm::RoundingMode> optLLVM =
1436       llvm::convertStrToRoundingMode(mdstr->getString());
1437   assert(optLLVM && "Expecting rounding mode");
1438   return builder.getAttr<RoundingModeAttr>(
1439       convertRoundingModeFromLLVM(*optLLVM));
1440 }
1441 
1442 FailureOr<SmallVector<AliasScopeAttr>>
1443 ModuleImport::matchAliasScopeAttrs(llvm::Value *value) {
1444   auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1445   auto *node = cast<llvm::MDNode>(nodeAsVal->getMetadata());
1446   return lookupAliasScopeAttrs(node);
1447 }
1448 
1449 Location ModuleImport::translateLoc(llvm::DILocation *loc) {
1450   return debugImporter->translateLoc(loc);
1451 }
1452 
1453 LogicalResult
1454 ModuleImport::convertBranchArgs(llvm::Instruction *branch,
1455                                 llvm::BasicBlock *target,
1456                                 SmallVectorImpl<Value> &blockArguments) {
1457   for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) {
1458     auto *phiInst = cast<llvm::PHINode>(&*inst);
1459     llvm::Value *value = phiInst->getIncomingValueForBlock(branch->getParent());
1460     FailureOr<Value> converted = convertValue(value);
1461     if (failed(converted))
1462       return failure();
1463     blockArguments.push_back(*converted);
1464   }
1465   return success();
1466 }
1467 
1468 LogicalResult
1469 ModuleImport::convertCallTypeAndOperands(llvm::CallBase *callInst,
1470                                          SmallVectorImpl<Type> &types,
1471                                          SmallVectorImpl<Value> &operands) {
1472   if (!callInst->getType()->isVoidTy())
1473     types.push_back(convertType(callInst->getType()));
1474 
1475   if (!callInst->getCalledFunction()) {
1476     FailureOr<Value> called = convertValue(callInst->getCalledOperand());
1477     if (failed(called))
1478       return failure();
1479     operands.push_back(*called);
1480   }
1481   SmallVector<llvm::Value *> args(callInst->args());
1482   FailureOr<SmallVector<Value>> arguments = convertValues(args);
1483   if (failed(arguments))
1484     return failure();
1485   llvm::append_range(operands, *arguments);
1486   return success();
1487 }
1488 
1489 LogicalResult ModuleImport::convertIntrinsic(llvm::CallInst *inst) {
1490   if (succeeded(iface.convertIntrinsic(builder, inst, *this)))
1491     return success();
1492 
1493   Location loc = translateLoc(inst->getDebugLoc());
1494   return emitError(loc) << "unhandled intrinsic: " << diag(*inst);
1495 }
1496 
1497 LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
1498   // Convert all instructions that do not provide an MLIR builder.
1499   Location loc = translateLoc(inst->getDebugLoc());
1500   if (inst->getOpcode() == llvm::Instruction::Br) {
1501     auto *brInst = cast<llvm::BranchInst>(inst);
1502 
1503     SmallVector<Block *> succBlocks;
1504     SmallVector<SmallVector<Value>> succBlockArgs;
1505     for (auto i : llvm::seq<unsigned>(0, brInst->getNumSuccessors())) {
1506       llvm::BasicBlock *succ = brInst->getSuccessor(i);
1507       SmallVector<Value> blockArgs;
1508       if (failed(convertBranchArgs(brInst, succ, blockArgs)))
1509         return failure();
1510       succBlocks.push_back(lookupBlock(succ));
1511       succBlockArgs.push_back(blockArgs);
1512     }
1513 
1514     if (!brInst->isConditional()) {
1515       auto brOp = builder.create<LLVM::BrOp>(loc, succBlockArgs.front(),
1516                                              succBlocks.front());
1517       mapNoResultOp(inst, brOp);
1518       return success();
1519     }
1520     FailureOr<Value> condition = convertValue(brInst->getCondition());
1521     if (failed(condition))
1522       return failure();
1523     auto condBrOp = builder.create<LLVM::CondBrOp>(
1524         loc, *condition, succBlocks.front(), succBlockArgs.front(),
1525         succBlocks.back(), succBlockArgs.back());
1526     mapNoResultOp(inst, condBrOp);
1527     return success();
1528   }
1529   if (inst->getOpcode() == llvm::Instruction::Switch) {
1530     auto *swInst = cast<llvm::SwitchInst>(inst);
1531     // Process the condition value.
1532     FailureOr<Value> condition = convertValue(swInst->getCondition());
1533     if (failed(condition))
1534       return failure();
1535     SmallVector<Value> defaultBlockArgs;
1536     // Process the default case.
1537     llvm::BasicBlock *defaultBB = swInst->getDefaultDest();
1538     if (failed(convertBranchArgs(swInst, defaultBB, defaultBlockArgs)))
1539       return failure();
1540 
1541     // Process the cases.
1542     unsigned numCases = swInst->getNumCases();
1543     SmallVector<SmallVector<Value>> caseOperands(numCases);
1544     SmallVector<ValueRange> caseOperandRefs(numCases);
1545     SmallVector<APInt> caseValues(numCases);
1546     SmallVector<Block *> caseBlocks(numCases);
1547     for (const auto &it : llvm::enumerate(swInst->cases())) {
1548       const llvm::SwitchInst::CaseHandle &caseHandle = it.value();
1549       llvm::BasicBlock *succBB = caseHandle.getCaseSuccessor();
1550       if (failed(convertBranchArgs(swInst, succBB, caseOperands[it.index()])))
1551         return failure();
1552       caseOperandRefs[it.index()] = caseOperands[it.index()];
1553       caseValues[it.index()] = caseHandle.getCaseValue()->getValue();
1554       caseBlocks[it.index()] = lookupBlock(succBB);
1555     }
1556 
1557     auto switchOp = builder.create<SwitchOp>(
1558         loc, *condition, lookupBlock(defaultBB), defaultBlockArgs, caseValues,
1559         caseBlocks, caseOperandRefs);
1560     mapNoResultOp(inst, switchOp);
1561     return success();
1562   }
1563   if (inst->getOpcode() == llvm::Instruction::PHI) {
1564     Type type = convertType(inst->getType());
1565     mapValue(inst, builder.getInsertionBlock()->addArgument(
1566                        type, translateLoc(inst->getDebugLoc())));
1567     return success();
1568   }
1569   if (inst->getOpcode() == llvm::Instruction::Call) {
1570     auto *callInst = cast<llvm::CallInst>(inst);
1571 
1572     SmallVector<Type> types;
1573     SmallVector<Value> operands;
1574     if (failed(convertCallTypeAndOperands(callInst, types, operands)))
1575       return failure();
1576 
1577     auto funcTy =
1578         dyn_cast<LLVMFunctionType>(convertType(callInst->getFunctionType()));
1579     if (!funcTy)
1580       return failure();
1581 
1582     CallOp callOp;
1583 
1584     if (llvm::Function *callee = callInst->getCalledFunction()) {
1585       callOp = builder.create<CallOp>(
1586           loc, funcTy, SymbolRefAttr::get(context, callee->getName()),
1587           operands);
1588     } else {
1589       callOp = builder.create<CallOp>(loc, funcTy, operands);
1590     }
1591     callOp.setCConv(convertCConvFromLLVM(callInst->getCallingConv()));
1592     callOp.setTailCallKind(
1593         convertTailCallKindFromLLVM(callInst->getTailCallKind()));
1594     setFastmathFlagsAttr(inst, callOp);
1595 
1596     // Handle function attributes.
1597     if (callInst->hasFnAttr(llvm::Attribute::Convergent))
1598       callOp.setConvergent(true);
1599     if (callInst->hasFnAttr(llvm::Attribute::NoUnwind))
1600       callOp.setNoUnwind(true);
1601     if (callInst->hasFnAttr(llvm::Attribute::WillReturn))
1602       callOp.setWillReturn(true);
1603 
1604     llvm::MemoryEffects memEffects = callInst->getMemoryEffects();
1605     ModRefInfo othermem = convertModRefInfoFromLLVM(
1606         memEffects.getModRef(llvm::MemoryEffects::Location::Other));
1607     ModRefInfo argMem = convertModRefInfoFromLLVM(
1608         memEffects.getModRef(llvm::MemoryEffects::Location::ArgMem));
1609     ModRefInfo inaccessibleMem = convertModRefInfoFromLLVM(
1610         memEffects.getModRef(llvm::MemoryEffects::Location::InaccessibleMem));
1611     auto memAttr = MemoryEffectsAttr::get(callOp.getContext(), othermem, argMem,
1612                                           inaccessibleMem);
1613     // Only set the attribute when it does not match the default value.
1614     if (!memAttr.isReadWrite())
1615       callOp.setMemoryEffectsAttr(memAttr);
1616 
1617     if (!callInst->getType()->isVoidTy())
1618       mapValue(inst, callOp.getResult());
1619     else
1620       mapNoResultOp(inst, callOp);
1621     return success();
1622   }
1623   if (inst->getOpcode() == llvm::Instruction::LandingPad) {
1624     auto *lpInst = cast<llvm::LandingPadInst>(inst);
1625 
1626     SmallVector<Value> operands;
1627     operands.reserve(lpInst->getNumClauses());
1628     for (auto i : llvm::seq<unsigned>(0, lpInst->getNumClauses())) {
1629       FailureOr<Value> operand = convertValue(lpInst->getClause(i));
1630       if (failed(operand))
1631         return failure();
1632       operands.push_back(*operand);
1633     }
1634 
1635     Type type = convertType(lpInst->getType());
1636     auto lpOp =
1637         builder.create<LandingpadOp>(loc, type, lpInst->isCleanup(), operands);
1638     mapValue(inst, lpOp);
1639     return success();
1640   }
1641   if (inst->getOpcode() == llvm::Instruction::Invoke) {
1642     auto *invokeInst = cast<llvm::InvokeInst>(inst);
1643 
1644     SmallVector<Type> types;
1645     SmallVector<Value> operands;
1646     if (failed(convertCallTypeAndOperands(invokeInst, types, operands)))
1647       return failure();
1648 
1649     // Check whether the invoke result is an argument to the normal destination
1650     // block.
1651     bool invokeResultUsedInPhi = llvm::any_of(
1652         invokeInst->getNormalDest()->phis(), [&](const llvm::PHINode &phi) {
1653           return phi.getIncomingValueForBlock(invokeInst->getParent()) ==
1654                  invokeInst;
1655         });
1656 
1657     Block *normalDest = lookupBlock(invokeInst->getNormalDest());
1658     Block *directNormalDest = normalDest;
1659     if (invokeResultUsedInPhi) {
1660       // The invoke result cannot be an argument to the normal destination
1661       // block, as that would imply using the invoke operation result in its
1662       // definition, so we need to create a dummy block to serve as an
1663       // intermediate destination.
1664       OpBuilder::InsertionGuard g(builder);
1665       directNormalDest = builder.createBlock(normalDest);
1666     }
1667 
1668     SmallVector<Value> unwindArgs;
1669     if (failed(convertBranchArgs(invokeInst, invokeInst->getUnwindDest(),
1670                                  unwindArgs)))
1671       return failure();
1672 
1673     auto funcTy =
1674         dyn_cast<LLVMFunctionType>(convertType(invokeInst->getFunctionType()));
1675     if (!funcTy)
1676       return failure();
1677 
1678     // Create the invoke operation. Normal destination block arguments will be
1679     // added later on to handle the case in which the operation result is
1680     // included in this list.
1681     InvokeOp invokeOp;
1682     if (llvm::Function *callee = invokeInst->getCalledFunction()) {
1683       invokeOp = builder.create<InvokeOp>(
1684           loc, funcTy,
1685           SymbolRefAttr::get(builder.getContext(), callee->getName()), operands,
1686           directNormalDest, ValueRange(),
1687           lookupBlock(invokeInst->getUnwindDest()), unwindArgs);
1688     } else {
1689       invokeOp = builder.create<InvokeOp>(
1690           loc, funcTy, /*callee=*/nullptr, operands, directNormalDest,
1691           ValueRange(), lookupBlock(invokeInst->getUnwindDest()), unwindArgs);
1692     }
1693     invokeOp.setCConv(convertCConvFromLLVM(invokeInst->getCallingConv()));
1694     if (!invokeInst->getType()->isVoidTy())
1695       mapValue(inst, invokeOp.getResults().front());
1696     else
1697       mapNoResultOp(inst, invokeOp);
1698 
1699     SmallVector<Value> normalArgs;
1700     if (failed(convertBranchArgs(invokeInst, invokeInst->getNormalDest(),
1701                                  normalArgs)))
1702       return failure();
1703 
1704     if (invokeResultUsedInPhi) {
1705       // The dummy normal dest block will just host an unconditional branch
1706       // instruction to the normal destination block passing the required block
1707       // arguments (including the invoke operation's result).
1708       OpBuilder::InsertionGuard g(builder);
1709       builder.setInsertionPointToStart(directNormalDest);
1710       builder.create<LLVM::BrOp>(loc, normalArgs, normalDest);
1711     } else {
1712       // If the invoke operation's result is not a block argument to the normal
1713       // destination block, just add the block arguments as usual.
1714       assert(llvm::none_of(
1715                  normalArgs,
1716                  [&](Value val) { return val.getDefiningOp() == invokeOp; }) &&
1717              "An llvm.invoke operation cannot pass its result as a block "
1718              "argument.");
1719       invokeOp.getNormalDestOperandsMutable().append(normalArgs);
1720     }
1721 
1722     return success();
1723   }
1724   if (inst->getOpcode() == llvm::Instruction::GetElementPtr) {
1725     auto *gepInst = cast<llvm::GetElementPtrInst>(inst);
1726     Type sourceElementType = convertType(gepInst->getSourceElementType());
1727     FailureOr<Value> basePtr = convertValue(gepInst->getOperand(0));
1728     if (failed(basePtr))
1729       return failure();
1730 
1731     // Treat every indices as dynamic since GEPOp::build will refine those
1732     // indices into static attributes later. One small downside of this
1733     // approach is that many unused `llvm.mlir.constant` would be emitted
1734     // at first place.
1735     SmallVector<GEPArg> indices;
1736     for (llvm::Value *operand : llvm::drop_begin(gepInst->operand_values())) {
1737       FailureOr<Value> index = convertValue(operand);
1738       if (failed(index))
1739         return failure();
1740       indices.push_back(*index);
1741     }
1742 
1743     Type type = convertType(inst->getType());
1744     auto gepOp = builder.create<GEPOp>(loc, type, sourceElementType, *basePtr,
1745                                        indices, gepInst->isInBounds());
1746     mapValue(inst, gepOp);
1747     return success();
1748   }
1749 
1750   // Convert all instructions that have an mlirBuilder.
1751   if (succeeded(convertInstructionImpl(builder, inst, *this, iface)))
1752     return success();
1753 
1754   return emitError(loc) << "unhandled instruction: " << diag(*inst);
1755 }
1756 
1757 LogicalResult ModuleImport::processInstruction(llvm::Instruction *inst) {
1758   // FIXME: Support uses of SubtargetData.
1759   // FIXME: Add support for call / operand attributes.
1760   // FIXME: Add support for the indirectbr, cleanupret, catchret, catchswitch,
1761   // callbr, vaarg, catchpad, cleanuppad instructions.
1762 
1763   // Convert LLVM intrinsics calls to MLIR intrinsics.
1764   if (auto *intrinsic = dyn_cast<llvm::IntrinsicInst>(inst))
1765     return convertIntrinsic(intrinsic);
1766 
1767   // Convert all remaining LLVM instructions to MLIR operations.
1768   return convertInstruction(inst);
1769 }
1770 
1771 FlatSymbolRefAttr ModuleImport::getPersonalityAsAttr(llvm::Function *f) {
1772   if (!f->hasPersonalityFn())
1773     return nullptr;
1774 
1775   llvm::Constant *pf = f->getPersonalityFn();
1776 
1777   // If it directly has a name, we can use it.
1778   if (pf->hasName())
1779     return SymbolRefAttr::get(builder.getContext(), pf->getName());
1780 
1781   // If it doesn't have a name, currently, only function pointers that are
1782   // bitcast to i8* are parsed.
1783   if (auto *ce = dyn_cast<llvm::ConstantExpr>(pf)) {
1784     if (ce->getOpcode() == llvm::Instruction::BitCast &&
1785         ce->getType() == llvm::PointerType::getUnqual(f->getContext())) {
1786       if (auto *func = dyn_cast<llvm::Function>(ce->getOperand(0)))
1787         return SymbolRefAttr::get(builder.getContext(), func->getName());
1788     }
1789   }
1790   return FlatSymbolRefAttr();
1791 }
1792 
1793 static void processMemoryEffects(llvm::Function *func, LLVMFuncOp funcOp) {
1794   llvm::MemoryEffects memEffects = func->getMemoryEffects();
1795 
1796   auto othermem = convertModRefInfoFromLLVM(
1797       memEffects.getModRef(llvm::MemoryEffects::Location::Other));
1798   auto argMem = convertModRefInfoFromLLVM(
1799       memEffects.getModRef(llvm::MemoryEffects::Location::ArgMem));
1800   auto inaccessibleMem = convertModRefInfoFromLLVM(
1801       memEffects.getModRef(llvm::MemoryEffects::Location::InaccessibleMem));
1802   auto memAttr = MemoryEffectsAttr::get(funcOp.getContext(), othermem, argMem,
1803                                         inaccessibleMem);
1804   // Only set the attr when it does not match the default value.
1805   if (memAttr.isReadWrite())
1806     return;
1807   funcOp.setMemoryEffectsAttr(memAttr);
1808 }
1809 
1810 // List of LLVM IR attributes that map to an explicit attribute on the MLIR
1811 // LLVMFuncOp.
1812 static constexpr std::array kExplicitAttributes{
1813     StringLiteral("aarch64_in_za"),
1814     StringLiteral("aarch64_inout_za"),
1815     StringLiteral("aarch64_new_za"),
1816     StringLiteral("aarch64_out_za"),
1817     StringLiteral("aarch64_preserves_za"),
1818     StringLiteral("aarch64_pstate_sm_body"),
1819     StringLiteral("aarch64_pstate_sm_compatible"),
1820     StringLiteral("aarch64_pstate_sm_enabled"),
1821     StringLiteral("alwaysinline"),
1822     StringLiteral("approx-func-fp-math"),
1823     StringLiteral("convergent"),
1824     StringLiteral("denormal-fp-math"),
1825     StringLiteral("denormal-fp-math-f32"),
1826     StringLiteral("fp-contract"),
1827     StringLiteral("frame-pointer"),
1828     StringLiteral("no-infs-fp-math"),
1829     StringLiteral("no-nans-fp-math"),
1830     StringLiteral("no-signed-zeros-fp-math"),
1831     StringLiteral("noinline"),
1832     StringLiteral("nounwind"),
1833     StringLiteral("optnone"),
1834     StringLiteral("target-features"),
1835     StringLiteral("tune-cpu"),
1836     StringLiteral("unsafe-fp-math"),
1837     StringLiteral("vscale_range"),
1838     StringLiteral("willreturn"),
1839 };
1840 
1841 static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp) {
1842   MLIRContext *context = funcOp.getContext();
1843   SmallVector<Attribute> passthroughs;
1844   llvm::AttributeSet funcAttrs = func->getAttributes().getAttributes(
1845       llvm::AttributeList::AttrIndex::FunctionIndex);
1846   for (llvm::Attribute attr : funcAttrs) {
1847     // Skip the memory attribute since the LLVMFuncOp has an explicit memory
1848     // attribute.
1849     if (attr.hasAttribute(llvm::Attribute::Memory))
1850       continue;
1851 
1852     // Skip invalid type attributes.
1853     if (attr.isTypeAttribute()) {
1854       emitWarning(funcOp.getLoc(),
1855                   "type attributes on a function are invalid, skipping it");
1856       continue;
1857     }
1858 
1859     StringRef attrName;
1860     if (attr.isStringAttribute())
1861       attrName = attr.getKindAsString();
1862     else
1863       attrName = llvm::Attribute::getNameFromAttrKind(attr.getKindAsEnum());
1864     auto keyAttr = StringAttr::get(context, attrName);
1865 
1866     // Skip attributes that map to an explicit attribute on the LLVMFuncOp.
1867     if (llvm::is_contained(kExplicitAttributes, attrName))
1868       continue;
1869 
1870     if (attr.isStringAttribute()) {
1871       StringRef val = attr.getValueAsString();
1872       if (val.empty()) {
1873         passthroughs.push_back(keyAttr);
1874         continue;
1875       }
1876       passthroughs.push_back(
1877           ArrayAttr::get(context, {keyAttr, StringAttr::get(context, val)}));
1878       continue;
1879     }
1880     if (attr.isIntAttribute()) {
1881       auto val = std::to_string(attr.getValueAsInt());
1882       passthroughs.push_back(
1883           ArrayAttr::get(context, {keyAttr, StringAttr::get(context, val)}));
1884       continue;
1885     }
1886     if (attr.isEnumAttribute()) {
1887       passthroughs.push_back(keyAttr);
1888       continue;
1889     }
1890 
1891     llvm_unreachable("unexpected attribute kind");
1892   }
1893 
1894   if (!passthroughs.empty())
1895     funcOp.setPassthroughAttr(ArrayAttr::get(context, passthroughs));
1896 }
1897 
1898 void ModuleImport::processFunctionAttributes(llvm::Function *func,
1899                                              LLVMFuncOp funcOp) {
1900   processMemoryEffects(func, funcOp);
1901   processPassthroughAttrs(func, funcOp);
1902 
1903   if (func->hasFnAttribute(llvm::Attribute::NoInline))
1904     funcOp.setNoInline(true);
1905   if (func->hasFnAttribute(llvm::Attribute::AlwaysInline))
1906     funcOp.setAlwaysInline(true);
1907   if (func->hasFnAttribute(llvm::Attribute::OptimizeNone))
1908     funcOp.setOptimizeNone(true);
1909   if (func->hasFnAttribute(llvm::Attribute::Convergent))
1910     funcOp.setConvergent(true);
1911   if (func->hasFnAttribute(llvm::Attribute::NoUnwind))
1912     funcOp.setNoUnwind(true);
1913   if (func->hasFnAttribute(llvm::Attribute::WillReturn))
1914     funcOp.setWillReturn(true);
1915 
1916   if (func->hasFnAttribute("aarch64_pstate_sm_enabled"))
1917     funcOp.setArmStreaming(true);
1918   else if (func->hasFnAttribute("aarch64_pstate_sm_body"))
1919     funcOp.setArmLocallyStreaming(true);
1920   else if (func->hasFnAttribute("aarch64_pstate_sm_compatible"))
1921     funcOp.setArmStreamingCompatible(true);
1922 
1923   if (func->hasFnAttribute("aarch64_new_za"))
1924     funcOp.setArmNewZa(true);
1925   else if (func->hasFnAttribute("aarch64_in_za"))
1926     funcOp.setArmInZa(true);
1927   else if (func->hasFnAttribute("aarch64_out_za"))
1928     funcOp.setArmOutZa(true);
1929   else if (func->hasFnAttribute("aarch64_inout_za"))
1930     funcOp.setArmInoutZa(true);
1931   else if (func->hasFnAttribute("aarch64_preserves_za"))
1932     funcOp.setArmPreservesZa(true);
1933 
1934   llvm::Attribute attr = func->getFnAttribute(llvm::Attribute::VScaleRange);
1935   if (attr.isValid()) {
1936     MLIRContext *context = funcOp.getContext();
1937     auto intTy = IntegerType::get(context, 32);
1938     funcOp.setVscaleRangeAttr(LLVM::VScaleRangeAttr::get(
1939         context, IntegerAttr::get(intTy, attr.getVScaleRangeMin()),
1940         IntegerAttr::get(intTy, attr.getVScaleRangeMax().value_or(0))));
1941   }
1942 
1943   // Process frame-pointer attribute.
1944   if (func->hasFnAttribute("frame-pointer")) {
1945     StringRef stringRefFramePointerKind =
1946         func->getFnAttribute("frame-pointer").getValueAsString();
1947     funcOp.setFramePointerAttr(LLVM::FramePointerKindAttr::get(
1948         funcOp.getContext(), LLVM::framePointerKind::symbolizeFramePointerKind(
1949                                  stringRefFramePointerKind)
1950                                  .value()));
1951   }
1952 
1953   if (llvm::Attribute attr = func->getFnAttribute("target-cpu");
1954       attr.isStringAttribute())
1955     funcOp.setTargetCpuAttr(StringAttr::get(context, attr.getValueAsString()));
1956 
1957   if (llvm::Attribute attr = func->getFnAttribute("tune-cpu");
1958       attr.isStringAttribute())
1959     funcOp.setTuneCpuAttr(StringAttr::get(context, attr.getValueAsString()));
1960 
1961   if (llvm::Attribute attr = func->getFnAttribute("target-features");
1962       attr.isStringAttribute())
1963     funcOp.setTargetFeaturesAttr(
1964         LLVM::TargetFeaturesAttr::get(context, attr.getValueAsString()));
1965 
1966   if (llvm::Attribute attr = func->getFnAttribute("unsafe-fp-math");
1967       attr.isStringAttribute())
1968     funcOp.setUnsafeFpMath(attr.getValueAsBool());
1969 
1970   if (llvm::Attribute attr = func->getFnAttribute("no-infs-fp-math");
1971       attr.isStringAttribute())
1972     funcOp.setNoInfsFpMath(attr.getValueAsBool());
1973 
1974   if (llvm::Attribute attr = func->getFnAttribute("no-nans-fp-math");
1975       attr.isStringAttribute())
1976     funcOp.setNoNansFpMath(attr.getValueAsBool());
1977 
1978   if (llvm::Attribute attr = func->getFnAttribute("approx-func-fp-math");
1979       attr.isStringAttribute())
1980     funcOp.setApproxFuncFpMath(attr.getValueAsBool());
1981 
1982   if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
1983       attr.isStringAttribute())
1984     funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());
1985 
1986   if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math");
1987       attr.isStringAttribute())
1988     funcOp.setDenormalFpMathAttr(
1989         StringAttr::get(context, attr.getValueAsString()));
1990 
1991   if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math-f32");
1992       attr.isStringAttribute())
1993     funcOp.setDenormalFpMathF32Attr(
1994         StringAttr::get(context, attr.getValueAsString()));
1995 
1996   if (llvm::Attribute attr = func->getFnAttribute("fp-contract");
1997       attr.isStringAttribute())
1998     funcOp.setFpContractAttr(StringAttr::get(context, attr.getValueAsString()));
1999 }
2000 
2001 DictionaryAttr
2002 ModuleImport::convertParameterAttribute(llvm::AttributeSet llvmParamAttrs,
2003                                         OpBuilder &builder) {
2004   SmallVector<NamedAttribute> paramAttrs;
2005   for (auto [llvmKind, mlirName] : getAttrKindToNameMapping()) {
2006     auto llvmAttr = llvmParamAttrs.getAttribute(llvmKind);
2007     // Skip attributes that are not attached.
2008     if (!llvmAttr.isValid())
2009       continue;
2010     Attribute mlirAttr;
2011     if (llvmAttr.isTypeAttribute())
2012       mlirAttr = TypeAttr::get(convertType(llvmAttr.getValueAsType()));
2013     else if (llvmAttr.isIntAttribute())
2014       mlirAttr = builder.getI64IntegerAttr(llvmAttr.getValueAsInt());
2015     else if (llvmAttr.isEnumAttribute())
2016       mlirAttr = builder.getUnitAttr();
2017     else
2018       llvm_unreachable("unexpected parameter attribute kind");
2019     paramAttrs.push_back(builder.getNamedAttr(mlirName, mlirAttr));
2020   }
2021 
2022   return builder.getDictionaryAttr(paramAttrs);
2023 }
2024 
2025 void ModuleImport::convertParameterAttributes(llvm::Function *func,
2026                                               LLVMFuncOp funcOp,
2027                                               OpBuilder &builder) {
2028   auto llvmAttrs = func->getAttributes();
2029   for (size_t i = 0, e = funcOp.getNumArguments(); i < e; ++i) {
2030     llvm::AttributeSet llvmArgAttrs = llvmAttrs.getParamAttrs(i);
2031     funcOp.setArgAttrs(i, convertParameterAttribute(llvmArgAttrs, builder));
2032   }
2033   // Convert the result attributes and attach them wrapped in an ArrayAttribute
2034   // to the funcOp.
2035   llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs();
2036   if (!llvmResAttr.hasAttributes())
2037     return;
2038   funcOp.setResAttrsAttr(
2039       builder.getArrayAttr(convertParameterAttribute(llvmResAttr, builder)));
2040 }
2041 
2042 LogicalResult ModuleImport::processFunction(llvm::Function *func) {
2043   clearRegionState();
2044 
2045   auto functionType =
2046       dyn_cast<LLVMFunctionType>(convertType(func->getFunctionType()));
2047   if (func->isIntrinsic() &&
2048       iface.isConvertibleIntrinsic(func->getIntrinsicID()))
2049     return success();
2050 
2051   bool dsoLocal = func->hasLocalLinkage();
2052   CConv cconv = convertCConvFromLLVM(func->getCallingConv());
2053 
2054   // Insert the function at the end of the module.
2055   OpBuilder::InsertionGuard guard(builder);
2056   builder.setInsertionPointToEnd(mlirModule.getBody());
2057 
2058   Location loc = debugImporter->translateFuncLocation(func);
2059   LLVMFuncOp funcOp = builder.create<LLVMFuncOp>(
2060       loc, func->getName(), functionType,
2061       convertLinkageFromLLVM(func->getLinkage()), dsoLocal, cconv);
2062 
2063   convertParameterAttributes(func, funcOp, builder);
2064 
2065   if (FlatSymbolRefAttr personality = getPersonalityAsAttr(func))
2066     funcOp.setPersonalityAttr(personality);
2067   else if (func->hasPersonalityFn())
2068     emitWarning(funcOp.getLoc(), "could not deduce personality, skipping it");
2069 
2070   if (func->hasGC())
2071     funcOp.setGarbageCollector(StringRef(func->getGC()));
2072 
2073   if (func->hasAtLeastLocalUnnamedAddr())
2074     funcOp.setUnnamedAddr(convertUnnamedAddrFromLLVM(func->getUnnamedAddr()));
2075 
2076   if (func->hasSection())
2077     funcOp.setSection(StringRef(func->getSection()));
2078 
2079   funcOp.setVisibility_(convertVisibilityFromLLVM(func->getVisibility()));
2080 
2081   if (func->hasComdat())
2082     funcOp.setComdatAttr(comdatMapping.lookup(func->getComdat()));
2083 
2084   if (llvm::MaybeAlign maybeAlign = func->getAlign())
2085     funcOp.setAlignment(maybeAlign->value());
2086 
2087   // Handle Function attributes.
2088   processFunctionAttributes(func, funcOp);
2089 
2090   // Convert non-debug metadata by using the dialect interface.
2091   SmallVector<std::pair<unsigned, llvm::MDNode *>> allMetadata;
2092   func->getAllMetadata(allMetadata);
2093   for (auto &[kind, node] : allMetadata) {
2094     if (!iface.isConvertibleMetadata(kind))
2095       continue;
2096     if (failed(iface.setMetadataAttrs(builder, kind, node, funcOp, *this))) {
2097       emitWarning(funcOp.getLoc())
2098           << "unhandled function metadata: " << diagMD(node, llvmModule.get())
2099           << " on " << diag(*func);
2100     }
2101   }
2102 
2103   if (func->isDeclaration())
2104     return success();
2105 
2106   // Collect the set of basic blocks reachable from the function's entry block.
2107   // This step is crucial as LLVM IR can contain unreachable blocks that
2108   // self-dominate. As a result, an operation might utilize a variable it
2109   // defines, which the import does not support. Given that MLIR lacks block
2110   // label support, we can safely remove unreachable blocks, as there are no
2111   // indirect branch instructions that could potentially target these blocks.
2112   llvm::df_iterator_default_set<llvm::BasicBlock *> reachable;
2113   for (llvm::BasicBlock *basicBlock : llvm::depth_first_ext(func, reachable))
2114     (void)basicBlock;
2115 
2116   // Eagerly create all reachable blocks.
2117   SmallVector<llvm::BasicBlock *> reachableBasicBlocks;
2118   for (llvm::BasicBlock &basicBlock : *func) {
2119     // Skip unreachable blocks.
2120     if (!reachable.contains(&basicBlock))
2121       continue;
2122     Region &body = funcOp.getBody();
2123     Block *block = builder.createBlock(&body, body.end());
2124     mapBlock(&basicBlock, block);
2125     reachableBasicBlocks.push_back(&basicBlock);
2126   }
2127 
2128   // Add function arguments to the entry block.
2129   for (const auto &it : llvm::enumerate(func->args())) {
2130     BlockArgument blockArg = funcOp.getFunctionBody().addArgument(
2131         functionType.getParamType(it.index()), funcOp.getLoc());
2132     mapValue(&it.value(), blockArg);
2133   }
2134 
2135   // Process the blocks in topological order. The ordered traversal ensures
2136   // operands defined in a dominating block have a valid mapping to an MLIR
2137   // value once a block is translated.
2138   SetVector<llvm::BasicBlock *> blocks =
2139       getTopologicallySortedBlocks(reachableBasicBlocks);
2140   setConstantInsertionPointToStart(lookupBlock(blocks.front()));
2141   for (llvm::BasicBlock *basicBlock : blocks)
2142     if (failed(processBasicBlock(basicBlock, lookupBlock(basicBlock))))
2143       return failure();
2144 
2145   // Process the debug intrinsics that require a delayed conversion after
2146   // everything else was converted.
2147   if (failed(processDebugIntrinsics()))
2148     return failure();
2149 
2150   return success();
2151 }
2152 
2153 /// Checks if `dbgIntr` is a kill location that holds metadata instead of an SSA
2154 /// value.
2155 static bool isMetadataKillLocation(llvm::DbgVariableIntrinsic *dbgIntr) {
2156   if (!dbgIntr->isKillLocation())
2157     return false;
2158   llvm::Value *value = dbgIntr->getArgOperand(0);
2159   auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(value);
2160   if (!nodeAsVal)
2161     return false;
2162   return !isa<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
2163 }
2164 
2165 LogicalResult
2166 ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
2167                                     DominanceInfo &domInfo) {
2168   Location loc = translateLoc(dbgIntr->getDebugLoc());
2169   auto emitUnsupportedWarning = [&]() {
2170     if (emitExpensiveWarnings)
2171       emitWarning(loc) << "dropped intrinsic: " << diag(*dbgIntr);
2172     return success();
2173   };
2174   // Drop debug intrinsics with arg lists.
2175   // TODO: Support debug intrinsics that have arg lists.
2176   if (dbgIntr->hasArgList())
2177     return emitUnsupportedWarning();
2178   // Kill locations can have metadata nodes as location operand. This
2179   // cannot be converted to poison as the type cannot be reconstructed.
2180   // TODO: find a way to support this case.
2181   if (isMetadataKillLocation(dbgIntr))
2182     return emitUnsupportedWarning();
2183   // Drop debug intrinsics if the associated variable information cannot be
2184   // translated due to cyclic debug metadata.
2185   // TODO: Support cyclic debug metadata.
2186   DILocalVariableAttr localVariableAttr =
2187       matchLocalVariableAttr(dbgIntr->getArgOperand(1));
2188   if (!localVariableAttr)
2189     return emitUnsupportedWarning();
2190   FailureOr<Value> argOperand = convertMetadataValue(dbgIntr->getArgOperand(0));
2191   if (failed(argOperand))
2192     return emitError(loc) << "failed to convert a debug intrinsic operand: "
2193                           << diag(*dbgIntr);
2194 
2195   // Ensure that the debug intrinsic is inserted right after its operand is
2196   // defined. Otherwise, the operand might not necessarily dominate the
2197   // intrinsic. If the defining operation is a terminator, insert the intrinsic
2198   // into a dominated block.
2199   OpBuilder::InsertionGuard guard(builder);
2200   if (Operation *op = argOperand->getDefiningOp();
2201       op && op->hasTrait<OpTrait::IsTerminator>()) {
2202     // Find a dominated block that can hold the debug intrinsic.
2203     auto dominatedBlocks = domInfo.getNode(op->getBlock())->children();
2204     // If no block is dominated by the terminator, this intrinisc cannot be
2205     // converted.
2206     if (dominatedBlocks.empty())
2207       return emitUnsupportedWarning();
2208     // Set insertion point before the terminator, to avoid inserting something
2209     // before landingpads.
2210     Block *dominatedBlock = (*dominatedBlocks.begin())->getBlock();
2211     builder.setInsertionPoint(dominatedBlock->getTerminator());
2212   } else {
2213     builder.setInsertionPointAfterValue(*argOperand);
2214   }
2215   auto locationExprAttr =
2216       debugImporter->translateExpression(dbgIntr->getExpression());
2217   Operation *op =
2218       llvm::TypeSwitch<llvm::DbgVariableIntrinsic *, Operation *>(dbgIntr)
2219           .Case([&](llvm::DbgDeclareInst *) {
2220             return builder.create<LLVM::DbgDeclareOp>(
2221                 loc, *argOperand, localVariableAttr, locationExprAttr);
2222           })
2223           .Case([&](llvm::DbgValueInst *) {
2224             return builder.create<LLVM::DbgValueOp>(
2225                 loc, *argOperand, localVariableAttr, locationExprAttr);
2226           });
2227   mapNoResultOp(dbgIntr, op);
2228   setNonDebugMetadataAttrs(dbgIntr, op);
2229   return success();
2230 }
2231 
2232 LogicalResult ModuleImport::processDebugIntrinsics() {
2233   DominanceInfo domInfo;
2234   for (llvm::Instruction *inst : debugIntrinsics) {
2235     auto *intrCall = cast<llvm::DbgVariableIntrinsic>(inst);
2236     if (failed(processDebugIntrinsic(intrCall, domInfo)))
2237       return failure();
2238   }
2239   return success();
2240 }
2241 
2242 LogicalResult ModuleImport::processBasicBlock(llvm::BasicBlock *bb,
2243                                               Block *block) {
2244   builder.setInsertionPointToStart(block);
2245   for (llvm::Instruction &inst : *bb) {
2246     if (failed(processInstruction(&inst)))
2247       return failure();
2248 
2249     // Skip additional processing when the instructions is a debug intrinsics
2250     // that was not yet converted.
2251     if (debugIntrinsics.contains(&inst))
2252       continue;
2253 
2254     // Set the non-debug metadata attributes on the imported operation and emit
2255     // a warning if an instruction other than a phi instruction is dropped
2256     // during the import.
2257     if (Operation *op = lookupOperation(&inst)) {
2258       setNonDebugMetadataAttrs(&inst, op);
2259     } else if (inst.getOpcode() != llvm::Instruction::PHI) {
2260       if (emitExpensiveWarnings) {
2261         Location loc = debugImporter->translateLoc(inst.getDebugLoc());
2262         emitWarning(loc) << "dropped instruction: " << diag(inst);
2263       }
2264     }
2265   }
2266   return success();
2267 }
2268 
2269 FailureOr<SmallVector<AccessGroupAttr>>
2270 ModuleImport::lookupAccessGroupAttrs(const llvm::MDNode *node) const {
2271   return loopAnnotationImporter->lookupAccessGroupAttrs(node);
2272 }
2273 
2274 LoopAnnotationAttr
2275 ModuleImport::translateLoopAnnotationAttr(const llvm::MDNode *node,
2276                                           Location loc) const {
2277   return loopAnnotationImporter->translateLoopAnnotation(node, loc);
2278 }
2279 
2280 OwningOpRef<ModuleOp>
2281 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
2282                               MLIRContext *context, bool emitExpensiveWarnings,
2283                               bool dropDICompositeTypeElements) {
2284   // Preload all registered dialects to allow the import to iterate the
2285   // registered LLVMImportDialectInterface implementations and query the
2286   // supported LLVM IR constructs before starting the translation. Assumes the
2287   // LLVM and DLTI dialects that convert the core LLVM IR constructs have been
2288   // registered before.
2289   assert(llvm::is_contained(context->getAvailableDialects(),
2290                             LLVMDialect::getDialectNamespace()));
2291   assert(llvm::is_contained(context->getAvailableDialects(),
2292                             DLTIDialect::getDialectNamespace()));
2293   context->loadAllAvailableDialects();
2294   OwningOpRef<ModuleOp> module(ModuleOp::create(FileLineColLoc::get(
2295       StringAttr::get(context, llvmModule->getSourceFileName()), /*line=*/0,
2296       /*column=*/0)));
2297 
2298   ModuleImport moduleImport(module.get(), std::move(llvmModule),
2299                             emitExpensiveWarnings, dropDICompositeTypeElements);
2300   if (failed(moduleImport.initializeImportInterface()))
2301     return {};
2302   if (failed(moduleImport.convertDataLayout()))
2303     return {};
2304   if (failed(moduleImport.convertComdats()))
2305     return {};
2306   if (failed(moduleImport.convertMetadata()))
2307     return {};
2308   if (failed(moduleImport.convertGlobals()))
2309     return {};
2310   if (failed(moduleImport.convertFunctions()))
2311     return {};
2312 
2313   return module;
2314 }
2315