MLIR  20.0.0git
ModuleImport.cpp
Go to the documentation of this file.
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 
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"
25 #include "mlir/IR/Builders.h"
26 #include "mlir/IR/Matchers.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.
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.
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,
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.
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;
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>(
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 
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()) {
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 
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 
539  OpBuilder::InsertionGuard guard(builder);
540  builder.setInsertionPointToEnd(mlirModule.getBody());
541  for (const llvm::Function &func : llvmModule->functions()) {
542  for (const llvm::Instruction &inst : llvm::instructions(func)) {
543  // Convert access group metadata nodes.
544  if (llvm::MDNode *node =
545  inst.getMetadata(llvm::LLVMContext::MD_access_group))
546  if (failed(processAccessGroupMetadata(node)))
547  return failure();
548 
549  // Convert alias analysis metadata nodes.
550  llvm::AAMDNodes aliasAnalysisNodes = inst.getAAMetadata();
551  if (!aliasAnalysisNodes)
552  continue;
553  if (aliasAnalysisNodes.TBAA)
554  if (failed(processTBAAMetadata(aliasAnalysisNodes.TBAA)))
555  return failure();
556  if (aliasAnalysisNodes.Scope)
557  if (failed(processAliasScopeMetadata(aliasAnalysisNodes.Scope)))
558  return failure();
559  if (aliasAnalysisNodes.NoAlias)
560  if (failed(processAliasScopeMetadata(aliasAnalysisNodes.NoAlias)))
561  return failure();
562  }
563  }
564  if (failed(convertLinkerOptionsMetadata()))
565  return failure();
566  if (failed(convertIdentMetadata()))
567  return failure();
568  return success();
569 }
570 
571 void ModuleImport::processComdat(const llvm::Comdat *comdat) {
572  if (comdatMapping.contains(comdat))
573  return;
574 
575  ComdatOp comdatOp = getGlobalComdatOp();
576  OpBuilder::InsertionGuard guard(builder);
577  builder.setInsertionPointToEnd(&comdatOp.getBody().back());
578  auto selectorOp = builder.create<ComdatSelectorOp>(
579  mlirModule.getLoc(), comdat->getName(),
580  convertComdatFromLLVM(comdat->getSelectionKind()));
581  auto symbolRef =
583  FlatSymbolRefAttr::get(selectorOp.getSymNameAttr()));
584  comdatMapping.try_emplace(comdat, symbolRef);
585 }
586 
588  for (llvm::GlobalVariable &globalVar : llvmModule->globals())
589  if (globalVar.hasComdat())
590  processComdat(globalVar.getComdat());
591  for (llvm::Function &func : llvmModule->functions())
592  if (func.hasComdat())
593  processComdat(func.getComdat());
594  return success();
595 }
596 
598  for (llvm::GlobalVariable &globalVar : llvmModule->globals()) {
599  if (globalVar.getName() == getGlobalCtorsVarName() ||
600  globalVar.getName() == getGlobalDtorsVarName()) {
601  if (failed(convertGlobalCtorsAndDtors(&globalVar))) {
602  return emitError(UnknownLoc::get(context))
603  << "unhandled global variable: " << diag(globalVar);
604  }
605  continue;
606  }
607  if (failed(convertGlobal(&globalVar))) {
608  return emitError(UnknownLoc::get(context))
609  << "unhandled global variable: " << diag(globalVar);
610  }
611  }
612  return success();
613 }
614 
616  Location loc = mlirModule.getLoc();
617  DataLayoutImporter dataLayoutImporter(context, llvmModule->getDataLayout());
618  if (!dataLayoutImporter.getDataLayout())
619  return emitError(loc, "cannot translate data layout: ")
620  << dataLayoutImporter.getLastToken();
621 
622  for (StringRef token : dataLayoutImporter.getUnhandledTokens())
623  emitWarning(loc, "unhandled data layout token: ") << token;
624 
625  mlirModule->setAttr(DLTIDialect::kDataLayoutAttrName,
626  dataLayoutImporter.getDataLayout());
627  return success();
628 }
629 
631  for (llvm::Function &func : llvmModule->functions())
632  if (failed(processFunction(&func)))
633  return failure();
634  return success();
635 }
636 
637 void ModuleImport::setNonDebugMetadataAttrs(llvm::Instruction *inst,
638  Operation *op) {
640  inst->getAllMetadataOtherThanDebugLoc(allMetadata);
641  for (auto &[kind, node] : allMetadata) {
642  if (!iface.isConvertibleMetadata(kind))
643  continue;
644  if (failed(iface.setMetadataAttrs(builder, kind, node, op, *this))) {
645  if (emitExpensiveWarnings) {
646  Location loc = debugImporter->translateLoc(inst->getDebugLoc());
647  emitWarning(loc) << "unhandled metadata: "
648  << diagMD(node, llvmModule.get()) << " on "
649  << diag(*inst);
650  }
651  }
652  }
653 }
654 
655 void ModuleImport::setIntegerOverflowFlags(llvm::Instruction *inst,
656  Operation *op) const {
657  auto iface = cast<IntegerOverflowFlagsInterface>(op);
658 
659  IntegerOverflowFlags value = {};
660  value = bitEnumSet(value, IntegerOverflowFlags::nsw, inst->hasNoSignedWrap());
661  value =
662  bitEnumSet(value, IntegerOverflowFlags::nuw, inst->hasNoUnsignedWrap());
663 
664  iface.setOverflowFlags(value);
665 }
666 
667 void ModuleImport::setFastmathFlagsAttr(llvm::Instruction *inst,
668  Operation *op) const {
669  auto iface = cast<FastmathFlagsInterface>(op);
670 
671  // Even if the imported operation implements the fastmath interface, the
672  // original instruction may not have fastmath flags set. Exit if an
673  // instruction, such as a non floating-point function call, does not have
674  // fastmath flags.
675  if (!isa<llvm::FPMathOperator>(inst))
676  return;
677  llvm::FastMathFlags flags = inst->getFastMathFlags();
678 
679  // Set the fastmath bits flag-by-flag.
680  FastmathFlags value = {};
681  value = bitEnumSet(value, FastmathFlags::nnan, flags.noNaNs());
682  value = bitEnumSet(value, FastmathFlags::ninf, flags.noInfs());
683  value = bitEnumSet(value, FastmathFlags::nsz, flags.noSignedZeros());
684  value = bitEnumSet(value, FastmathFlags::arcp, flags.allowReciprocal());
685  value = bitEnumSet(value, FastmathFlags::contract, flags.allowContract());
686  value = bitEnumSet(value, FastmathFlags::afn, flags.approxFunc());
687  value = bitEnumSet(value, FastmathFlags::reassoc, flags.allowReassoc());
688  FastmathFlagsAttr attr = FastmathFlagsAttr::get(builder.getContext(), value);
689  iface->setAttr(iface.getFastmathAttrName(), attr);
690 }
691 
692 /// Returns if `type` is a scalar integer or floating-point type.
693 static bool isScalarType(Type type) {
694  return isa<IntegerType, FloatType>(type);
695 }
696 
697 /// Returns `type` if it is a builtin integer or floating-point vector type that
698 /// can be used to create an attribute or nullptr otherwise. If provided,
699 /// `arrayShape` is added to the shape of the vector to create an attribute that
700 /// matches an array of vectors.
701 static Type getVectorTypeForAttr(Type type, ArrayRef<int64_t> arrayShape = {}) {
702  if (!LLVM::isCompatibleVectorType(type))
703  return {};
704 
705  llvm::ElementCount numElements = LLVM::getVectorNumElements(type);
706  if (numElements.isScalable()) {
708  << "scalable vectors not supported";
709  return {};
710  }
711 
712  // An LLVM dialect vector can only contain scalars.
713  Type elementType = LLVM::getVectorElementType(type);
714  if (!isScalarType(elementType))
715  return {};
716 
717  SmallVector<int64_t> shape(arrayShape);
718  shape.push_back(numElements.getKnownMinValue());
719  return VectorType::get(shape, elementType);
720 }
721 
722 Type ModuleImport::getBuiltinTypeForAttr(Type type) {
723  if (!type)
724  return {};
725 
726  // Return builtin integer and floating-point types as is.
727  if (isScalarType(type))
728  return type;
729 
730  // Return builtin vectors of integer and floating-point types as is.
731  if (Type vectorType = getVectorTypeForAttr(type))
732  return vectorType;
733 
734  // Multi-dimensional array types are converted to tensors or vectors,
735  // depending on the innermost type being a scalar or a vector.
736  SmallVector<int64_t> arrayShape;
737  while (auto arrayType = dyn_cast<LLVMArrayType>(type)) {
738  arrayShape.push_back(arrayType.getNumElements());
739  type = arrayType.getElementType();
740  }
741  if (isScalarType(type))
742  return RankedTensorType::get(arrayShape, type);
743  return getVectorTypeForAttr(type, arrayShape);
744 }
745 
746 /// Returns an integer or float attribute for the provided scalar constant
747 /// `constScalar` or nullptr if the conversion fails.
748 static TypedAttr getScalarConstantAsAttr(OpBuilder &builder,
749  llvm::Constant *constScalar) {
750  MLIRContext *context = builder.getContext();
751 
752  // Convert scalar intergers.
753  if (auto *constInt = dyn_cast<llvm::ConstantInt>(constScalar)) {
754  return builder.getIntegerAttr(
755  IntegerType::get(context, constInt->getBitWidth()),
756  constInt->getValue());
757  }
758 
759  // Convert scalar floats.
760  if (auto *constFloat = dyn_cast<llvm::ConstantFP>(constScalar)) {
761  llvm::Type *type = constFloat->getType();
762  FloatType floatType =
763  type->isBFloatTy()
764  ? FloatType::getBF16(context)
765  : LLVM::detail::getFloatType(context, type->getScalarSizeInBits());
766  if (!floatType) {
768  << "unexpected floating-point type";
769  return {};
770  }
771  return builder.getFloatAttr(floatType, constFloat->getValueAPF());
772  }
773  return {};
774 }
775 
776 /// Returns an integer or float attribute array for the provided constant
777 /// sequence `constSequence` or nullptr if the conversion fails.
780  llvm::ConstantDataSequential *constSequence) {
781  SmallVector<Attribute> elementAttrs;
782  elementAttrs.reserve(constSequence->getNumElements());
783  for (auto idx : llvm::seq<int64_t>(0, constSequence->getNumElements())) {
784  llvm::Constant *constElement = constSequence->getElementAsConstant(idx);
785  elementAttrs.push_back(getScalarConstantAsAttr(builder, constElement));
786  }
787  return elementAttrs;
788 }
789 
790 Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
791  // Convert scalar constants.
792  if (Attribute scalarAttr = getScalarConstantAsAttr(builder, constant))
793  return scalarAttr;
794 
795  // Returns the static shape of the provided type if possible.
796  auto getConstantShape = [&](llvm::Type *type) {
797  return llvm::dyn_cast_if_present<ShapedType>(
798  getBuiltinTypeForAttr(convertType(type)));
799  };
800 
801  // Convert one-dimensional constant arrays or vectors that store 1/2/4/8-byte
802  // integer or half/bfloat/float/double values.
803  if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(constant)) {
804  if (constArray->isString())
805  return builder.getStringAttr(constArray->getAsString());
806  auto shape = getConstantShape(constArray->getType());
807  if (!shape)
808  return {};
809  // Convert splat constants to splat elements attributes.
810  auto *constVector = dyn_cast<llvm::ConstantDataVector>(constant);
811  if (constVector && constVector->isSplat()) {
812  // A vector is guaranteed to have at least size one.
814  builder, constVector->getElementAsConstant(0));
815  return SplatElementsAttr::get(shape, splatAttr);
816  }
817  // Convert non-splat constants to dense elements attributes.
818  SmallVector<Attribute> elementAttrs =
819  getSequenceConstantAsAttrs(builder, constArray);
820  return DenseElementsAttr::get(shape, elementAttrs);
821  }
822 
823  // Convert multi-dimensional constant aggregates that store all kinds of
824  // integer and floating-point types.
825  if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(constant)) {
826  auto shape = getConstantShape(constAggregate->getType());
827  if (!shape)
828  return {};
829  // Collect the aggregate elements in depths first order.
830  SmallVector<Attribute> elementAttrs;
831  SmallVector<llvm::Constant *> workList = {constAggregate};
832  while (!workList.empty()) {
833  llvm::Constant *current = workList.pop_back_val();
834  // Append any nested aggregates in reverse order to ensure the head
835  // element of the nested aggregates is at the back of the work list.
836  if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(current)) {
837  for (auto idx :
838  reverse(llvm::seq<int64_t>(0, constAggregate->getNumOperands())))
839  workList.push_back(constAggregate->getAggregateElement(idx));
840  continue;
841  }
842  // Append the elements of nested constant arrays or vectors that store
843  // 1/2/4/8-byte integer or half/bfloat/float/double values.
844  if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(current)) {
845  SmallVector<Attribute> attrs =
846  getSequenceConstantAsAttrs(builder, constArray);
847  elementAttrs.append(attrs.begin(), attrs.end());
848  continue;
849  }
850  // Append nested scalar constants that store all kinds of integer and
851  // floating-point types.
852  if (Attribute scalarAttr = getScalarConstantAsAttr(builder, current)) {
853  elementAttrs.push_back(scalarAttr);
854  continue;
855  }
856  // Bail if the aggregate contains a unsupported constant type such as a
857  // constant expression.
858  return {};
859  }
860  return DenseElementsAttr::get(shape, elementAttrs);
861  }
862 
863  // Convert zero aggregates.
864  if (auto *constZero = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
865  auto shape = llvm::dyn_cast_if_present<ShapedType>(
866  getBuiltinTypeForAttr(convertType(constZero->getType())));
867  if (!shape)
868  return {};
869  // Convert zero aggregates with a static shape to splat elements attributes.
870  Attribute splatAttr = builder.getZeroAttr(shape.getElementType());
871  assert(splatAttr && "expected non-null zero attribute for scalar types");
872  return SplatElementsAttr::get(shape, splatAttr);
873  }
874  return {};
875 }
876 
878 ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) {
879  assert(globalVar->getName().empty() &&
880  "expected to work with a nameless global");
881  auto [it, success] = namelessGlobals.try_emplace(globalVar);
882  if (!success)
883  return it->second;
884 
885  // Make sure the symbol name does not clash with an existing symbol.
886  SmallString<128> globalName = SymbolTable::generateSymbolName<128>(
888  [this](StringRef newName) { return llvmModule->getNamedValue(newName); },
889  namelessGlobalId);
890  auto symbolRef = FlatSymbolRefAttr::get(context, globalName);
891  it->getSecond() = symbolRef;
892  return symbolRef;
893 }
894 
895 LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) {
896  // Insert the global after the last one or at the start of the module.
897  OpBuilder::InsertionGuard guard(builder);
898  if (!globalInsertionOp)
899  builder.setInsertionPointToStart(mlirModule.getBody());
900  else
901  builder.setInsertionPointAfter(globalInsertionOp);
902 
903  Attribute valueAttr;
904  if (globalVar->hasInitializer())
905  valueAttr = getConstantAsAttr(globalVar->getInitializer());
906  Type type = convertType(globalVar->getValueType());
907 
908  uint64_t alignment = 0;
909  llvm::MaybeAlign maybeAlign = globalVar->getAlign();
910  if (maybeAlign.has_value()) {
911  llvm::Align align = *maybeAlign;
912  alignment = align.value();
913  }
914 
915  // Get the global expression associated with this global variable and convert
916  // it.
917  DIGlobalVariableExpressionAttr globalExpressionAttr;
919  globalVar->getDebugInfo(globalExpressions);
920 
921  // There should only be a single global expression.
922  if (!globalExpressions.empty())
923  globalExpressionAttr =
924  debugImporter->translateGlobalVariableExpression(globalExpressions[0]);
925 
926  // Workaround to support LLVM's nameless globals. MLIR, in contrast to LLVM,
927  // always requires a symbol name.
928  StringRef globalName = globalVar->getName();
929  if (globalName.empty())
930  globalName = getOrCreateNamelessSymbolName(globalVar).getValue();
931 
932  GlobalOp globalOp = builder.create<GlobalOp>(
933  mlirModule.getLoc(), type, globalVar->isConstant(),
934  convertLinkageFromLLVM(globalVar->getLinkage()), StringRef(globalName),
935  valueAttr, alignment, /*addr_space=*/globalVar->getAddressSpace(),
936  /*dso_local=*/globalVar->isDSOLocal(),
937  /*thread_local=*/globalVar->isThreadLocal(), /*comdat=*/SymbolRefAttr(),
938  /*attrs=*/ArrayRef<NamedAttribute>(), /*dbgExpr=*/globalExpressionAttr);
939  globalInsertionOp = globalOp;
940 
941  if (globalVar->hasInitializer() && !valueAttr) {
942  clearRegionState();
943  Block *block = builder.createBlock(&globalOp.getInitializerRegion());
944  setConstantInsertionPointToStart(block);
945  FailureOr<Value> initializer =
946  convertConstantExpr(globalVar->getInitializer());
947  if (failed(initializer))
948  return failure();
949  builder.create<ReturnOp>(globalOp.getLoc(), *initializer);
950  }
951  if (globalVar->hasAtLeastLocalUnnamedAddr()) {
952  globalOp.setUnnamedAddr(
953  convertUnnamedAddrFromLLVM(globalVar->getUnnamedAddr()));
954  }
955  if (globalVar->hasSection())
956  globalOp.setSection(globalVar->getSection());
957  globalOp.setVisibility_(
958  convertVisibilityFromLLVM(globalVar->getVisibility()));
959 
960  if (globalVar->hasComdat())
961  globalOp.setComdatAttr(comdatMapping.lookup(globalVar->getComdat()));
962 
963  return success();
964 }
965 
966 LogicalResult
967 ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) {
968  if (!globalVar->hasInitializer() || !globalVar->hasAppendingLinkage())
969  return failure();
970  auto *initializer =
971  dyn_cast<llvm::ConstantArray>(globalVar->getInitializer());
972  if (!initializer)
973  return failure();
974 
976  SmallVector<int32_t> priorities;
977  for (llvm::Value *operand : initializer->operands()) {
978  auto *aggregate = dyn_cast<llvm::ConstantAggregate>(operand);
979  if (!aggregate || aggregate->getNumOperands() != 3)
980  return failure();
981 
982  auto *priority = dyn_cast<llvm::ConstantInt>(aggregate->getOperand(0));
983  auto *func = dyn_cast<llvm::Function>(aggregate->getOperand(1));
984  auto *data = dyn_cast<llvm::Constant>(aggregate->getOperand(2));
985  if (!priority || !func || !data)
986  return failure();
987 
988  // GlobalCtorsOps and GlobalDtorsOps do not support non-null data fields.
989  if (!data->isNullValue())
990  return failure();
991 
992  funcs.push_back(FlatSymbolRefAttr::get(context, func->getName()));
993  priorities.push_back(priority->getValue().getZExtValue());
994  }
995 
996  OpBuilder::InsertionGuard guard(builder);
997  if (!globalInsertionOp)
998  builder.setInsertionPointToStart(mlirModule.getBody());
999  else
1000  builder.setInsertionPointAfter(globalInsertionOp);
1001 
1002  if (globalVar->getName() == getGlobalCtorsVarName()) {
1003  globalInsertionOp = builder.create<LLVM::GlobalCtorsOp>(
1004  mlirModule.getLoc(), builder.getArrayAttr(funcs),
1005  builder.getI32ArrayAttr(priorities));
1006  return success();
1007  }
1008  globalInsertionOp = builder.create<LLVM::GlobalDtorsOp>(
1009  mlirModule.getLoc(), builder.getArrayAttr(funcs),
1010  builder.getI32ArrayAttr(priorities));
1011  return success();
1012 }
1013 
1015 ModuleImport::getConstantsToConvert(llvm::Constant *constant) {
1016  // Return the empty set if the constant has been translated before.
1017  if (valueMapping.contains(constant))
1018  return {};
1019 
1020  // Traverse the constants in post-order and stop the traversal if a constant
1021  // already has a `valueMapping` from an earlier constant translation or if the
1022  // constant is traversed a second time.
1023  SetVector<llvm::Constant *> orderedSet;
1024  SetVector<llvm::Constant *> workList;
1026  workList.insert(constant);
1027  while (!workList.empty()) {
1028  llvm::Constant *current = workList.back();
1029  // References of global objects are just pointers to the object. Avoid
1030  // walking the elements of these here.
1031  if (isa<llvm::GlobalObject>(current)) {
1032  orderedSet.insert(current);
1033  workList.pop_back();
1034  continue;
1035  }
1036 
1037  // Collect all dependencies of the current constant and add them to the
1038  // adjacency list if none has been computed before.
1039  auto [adjacencyIt, inserted] = adjacencyLists.try_emplace(current);
1040  if (inserted) {
1041  // Add all constant operands to the adjacency list and skip any other
1042  // values such as basic block addresses.
1043  for (llvm::Value *operand : current->operands())
1044  if (auto *constDependency = dyn_cast<llvm::Constant>(operand))
1045  adjacencyIt->getSecond().push_back(constDependency);
1046  // Use the getElementValue method to add the dependencies of zero
1047  // initialized aggregate constants since they do not take any operands.
1048  if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(current)) {
1049  unsigned numElements = constAgg->getElementCount().getFixedValue();
1050  for (unsigned i = 0, e = numElements; i != e; ++i)
1051  adjacencyIt->getSecond().push_back(constAgg->getElementValue(i));
1052  }
1053  }
1054  // Add the current constant to the `orderedSet` of the traversed nodes if
1055  // all its dependencies have been traversed before. Additionally, remove the
1056  // constant from the `workList` and continue the traversal.
1057  if (adjacencyIt->getSecond().empty()) {
1058  orderedSet.insert(current);
1059  workList.pop_back();
1060  continue;
1061  }
1062  // Add the next dependency from the adjacency list to the `workList` and
1063  // continue the traversal. Remove the dependency from the adjacency list to
1064  // mark that it has been processed. Only enqueue the dependency if it has no
1065  // `valueMapping` from an earlier translation and if it has not been
1066  // enqueued before.
1067  llvm::Constant *dependency = adjacencyIt->getSecond().pop_back_val();
1068  if (valueMapping.contains(dependency) || workList.contains(dependency) ||
1069  orderedSet.contains(dependency))
1070  continue;
1071  workList.insert(dependency);
1072  }
1073 
1074  return orderedSet;
1075 }
1076 
1077 FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) {
1078  Location loc = UnknownLoc::get(context);
1079 
1080  // Convert constants that can be represented as attributes.
1081  if (Attribute attr = getConstantAsAttr(constant)) {
1082  Type type = convertType(constant->getType());
1083  if (auto symbolRef = dyn_cast<FlatSymbolRefAttr>(attr)) {
1084  return builder.create<AddressOfOp>(loc, type, symbolRef.getValue())
1085  .getResult();
1086  }
1087  return builder.create<ConstantOp>(loc, type, attr).getResult();
1088  }
1089 
1090  // Convert null pointer constants.
1091  if (auto *nullPtr = dyn_cast<llvm::ConstantPointerNull>(constant)) {
1092  Type type = convertType(nullPtr->getType());
1093  return builder.create<ZeroOp>(loc, type).getResult();
1094  }
1095 
1096  // Convert none token constants.
1097  if (isa<llvm::ConstantTokenNone>(constant)) {
1098  return builder.create<NoneTokenOp>(loc).getResult();
1099  }
1100 
1101  // Convert poison.
1102  if (auto *poisonVal = dyn_cast<llvm::PoisonValue>(constant)) {
1103  Type type = convertType(poisonVal->getType());
1104  return builder.create<PoisonOp>(loc, type).getResult();
1105  }
1106 
1107  // Convert undef.
1108  if (auto *undefVal = dyn_cast<llvm::UndefValue>(constant)) {
1109  Type type = convertType(undefVal->getType());
1110  return builder.create<UndefOp>(loc, type).getResult();
1111  }
1112 
1113  // Convert global variable accesses.
1114  if (auto *globalObj = dyn_cast<llvm::GlobalObject>(constant)) {
1115  Type type = convertType(globalObj->getType());
1116  StringRef globalName = globalObj->getName();
1117  FlatSymbolRefAttr symbolRef;
1118  // Empty names are only allowed for global variables.
1119  if (globalName.empty())
1120  symbolRef =
1121  getOrCreateNamelessSymbolName(cast<llvm::GlobalVariable>(globalObj));
1122  else
1123  symbolRef = FlatSymbolRefAttr::get(context, globalName);
1124  return builder.create<AddressOfOp>(loc, type, symbolRef).getResult();
1125  }
1126 
1127  // Convert constant expressions.
1128  if (auto *constExpr = dyn_cast<llvm::ConstantExpr>(constant)) {
1129  // Convert the constant expression to a temporary LLVM instruction and
1130  // translate it using the `processInstruction` method. Delete the
1131  // instruction after the translation and remove it from `valueMapping`,
1132  // since later calls to `getAsInstruction` may return the same address
1133  // resulting in a conflicting `valueMapping` entry.
1134  llvm::Instruction *inst = constExpr->getAsInstruction();
1135  auto guard = llvm::make_scope_exit([&]() {
1136  assert(!noResultOpMapping.contains(inst) &&
1137  "expected constant expression to return a result");
1138  valueMapping.erase(inst);
1139  inst->deleteValue();
1140  });
1141  // Note: `processInstruction` does not call `convertConstant` recursively
1142  // since all constant dependencies have been converted before.
1143  assert(llvm::all_of(inst->operands(), [&](llvm::Value *value) {
1144  return valueMapping.contains(value);
1145  }));
1146  if (failed(processInstruction(inst)))
1147  return failure();
1148  return lookupValue(inst);
1149  }
1150 
1151  // Convert aggregate constants.
1152  if (isa<llvm::ConstantAggregate>(constant) ||
1153  isa<llvm::ConstantAggregateZero>(constant)) {
1154  // Lookup the aggregate elements that have been converted before.
1155  SmallVector<Value> elementValues;
1156  if (auto *constAgg = dyn_cast<llvm::ConstantAggregate>(constant)) {
1157  elementValues.reserve(constAgg->getNumOperands());
1158  for (llvm::Value *operand : constAgg->operands())
1159  elementValues.push_back(lookupValue(operand));
1160  }
1161  if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(constant)) {
1162  unsigned numElements = constAgg->getElementCount().getFixedValue();
1163  elementValues.reserve(numElements);
1164  for (unsigned i = 0, e = numElements; i != e; ++i)
1165  elementValues.push_back(lookupValue(constAgg->getElementValue(i)));
1166  }
1167  assert(llvm::count(elementValues, nullptr) == 0 &&
1168  "expected all elements have been converted before");
1169 
1170  // Generate an UndefOp as root value and insert the aggregate elements.
1171  Type rootType = convertType(constant->getType());
1172  bool isArrayOrStruct = isa<LLVMArrayType, LLVMStructType>(rootType);
1173  assert((isArrayOrStruct || LLVM::isCompatibleVectorType(rootType)) &&
1174  "unrecognized aggregate type");
1175  Value root = builder.create<UndefOp>(loc, rootType);
1176  for (const auto &it : llvm::enumerate(elementValues)) {
1177  if (isArrayOrStruct) {
1178  root = builder.create<InsertValueOp>(loc, root, it.value(), it.index());
1179  } else {
1180  Attribute indexAttr = builder.getI32IntegerAttr(it.index());
1181  Value indexValue =
1182  builder.create<ConstantOp>(loc, builder.getI32Type(), indexAttr);
1183  root = builder.create<InsertElementOp>(loc, rootType, root, it.value(),
1184  indexValue);
1185  }
1186  }
1187  return root;
1188  }
1189 
1190  if (auto *constTargetNone = dyn_cast<llvm::ConstantTargetNone>(constant)) {
1191  LLVMTargetExtType targetExtType =
1192  cast<LLVMTargetExtType>(convertType(constTargetNone->getType()));
1193  assert(targetExtType.hasProperty(LLVMTargetExtType::HasZeroInit) &&
1194  "target extension type does not support zero-initialization");
1195  // Create llvm.mlir.zero operation to represent zero-initialization of
1196  // target extension type.
1197  return builder.create<LLVM::ZeroOp>(loc, targetExtType).getRes();
1198  }
1199 
1200  StringRef error = "";
1201  if (isa<llvm::BlockAddress>(constant))
1202  error = " since blockaddress(...) is unsupported";
1203 
1204  return emitError(loc) << "unhandled constant: " << diag(*constant) << error;
1205 }
1206 
1207 FailureOr<Value> ModuleImport::convertConstantExpr(llvm::Constant *constant) {
1208  // Only call the function for constants that have not been translated before
1209  // since it updates the constant insertion point assuming the converted
1210  // constant has been introduced at the end of the constant section.
1211  assert(!valueMapping.contains(constant) &&
1212  "expected constant has not been converted before");
1213  assert(constantInsertionBlock &&
1214  "expected the constant insertion block to be non-null");
1215 
1216  // Insert the constant after the last one or at the start of the entry block.
1217  OpBuilder::InsertionGuard guard(builder);
1218  if (!constantInsertionOp)
1219  builder.setInsertionPointToStart(constantInsertionBlock);
1220  else
1221  builder.setInsertionPointAfter(constantInsertionOp);
1222 
1223  // Convert all constants of the expression and add them to `valueMapping`.
1224  SetVector<llvm::Constant *> constantsToConvert =
1225  getConstantsToConvert(constant);
1226  for (llvm::Constant *constantToConvert : constantsToConvert) {
1227  FailureOr<Value> converted = convertConstant(constantToConvert);
1228  if (failed(converted))
1229  return failure();
1230  mapValue(constantToConvert, *converted);
1231  }
1232 
1233  // Update the constant insertion point and return the converted constant.
1234  Value result = lookupValue(constant);
1235  constantInsertionOp = result.getDefiningOp();
1236  return result;
1237 }
1238 
1239 FailureOr<Value> ModuleImport::convertValue(llvm::Value *value) {
1240  assert(!isa<llvm::MetadataAsValue>(value) &&
1241  "expected value to not be metadata");
1242 
1243  // Return the mapped value if it has been converted before.
1244  auto it = valueMapping.find(value);
1245  if (it != valueMapping.end())
1246  return it->getSecond();
1247 
1248  // Convert constants such as immediate values that have no mapping yet.
1249  if (auto *constant = dyn_cast<llvm::Constant>(value))
1250  return convertConstantExpr(constant);
1251 
1252  Location loc = UnknownLoc::get(context);
1253  if (auto *inst = dyn_cast<llvm::Instruction>(value))
1254  loc = translateLoc(inst->getDebugLoc());
1255  return emitError(loc) << "unhandled value: " << diag(*value);
1256 }
1257 
1258 FailureOr<Value> ModuleImport::convertMetadataValue(llvm::Value *value) {
1259  // A value may be wrapped as metadata, for example, when passed to a debug
1260  // intrinsic. Unwrap these values before the conversion.
1261  auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(value);
1262  if (!nodeAsVal)
1263  return failure();
1264  auto *node = dyn_cast<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
1265  if (!node)
1266  return failure();
1267  value = node->getValue();
1268 
1269  // Return the mapped value if it has been converted before.
1270  auto it = valueMapping.find(value);
1271  if (it != valueMapping.end())
1272  return it->getSecond();
1273 
1274  // Convert constants such as immediate values that have no mapping yet.
1275  if (auto *constant = dyn_cast<llvm::Constant>(value))
1276  return convertConstantExpr(constant);
1277  return failure();
1278 }
1279 
1280 FailureOr<SmallVector<Value>>
1282  SmallVector<Value> remapped;
1283  remapped.reserve(values.size());
1284  for (llvm::Value *value : values) {
1285  FailureOr<Value> converted = convertValue(value);
1286  if (failed(converted))
1287  return failure();
1288  remapped.push_back(*converted);
1289  }
1290  return remapped;
1291 }
1292 
1294  ArrayRef<llvm::Value *> values, ArrayRef<unsigned> immArgPositions,
1295  ArrayRef<StringLiteral> immArgAttrNames, SmallVectorImpl<Value> &valuesOut,
1296  SmallVectorImpl<NamedAttribute> &attrsOut) {
1297  assert(immArgPositions.size() == immArgAttrNames.size() &&
1298  "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal "
1299  "length");
1300 
1301  SmallVector<llvm::Value *> operands(values);
1302  for (auto [immArgPos, immArgName] :
1303  llvm::zip(immArgPositions, immArgAttrNames)) {
1304  auto &value = operands[immArgPos];
1305  auto *constant = llvm::cast<llvm::Constant>(value);
1306  auto attr = getScalarConstantAsAttr(builder, constant);
1307  assert(attr && attr.getType().isIntOrFloat() &&
1308  "expected immarg to be float or integer constant");
1309  auto nameAttr = StringAttr::get(attr.getContext(), immArgName);
1310  attrsOut.push_back({nameAttr, attr});
1311  // Mark matched attribute values as null (so they can be removed below).
1312  value = nullptr;
1313  }
1314 
1315  for (llvm::Value *value : operands) {
1316  if (!value)
1317  continue;
1318  auto mlirValue = convertValue(value);
1319  if (failed(mlirValue))
1320  return failure();
1321  valuesOut.push_back(*mlirValue);
1322  }
1323 
1324  return success();
1325 }
1326 
1327 IntegerAttr ModuleImport::matchIntegerAttr(llvm::Value *value) {
1328  IntegerAttr integerAttr;
1329  FailureOr<Value> converted = convertValue(value);
1330  bool success = succeeded(converted) &&
1331  matchPattern(*converted, m_Constant(&integerAttr));
1332  assert(success && "expected a constant integer value");
1333  (void)success;
1334  return integerAttr;
1335 }
1336 
1337 FloatAttr ModuleImport::matchFloatAttr(llvm::Value *value) {
1338  FloatAttr floatAttr;
1339  FailureOr<Value> converted = convertValue(value);
1340  bool success =
1341  succeeded(converted) && matchPattern(*converted, m_Constant(&floatAttr));
1342  assert(success && "expected a constant float value");
1343  (void)success;
1344  return floatAttr;
1345 }
1346 
1347 DILocalVariableAttr ModuleImport::matchLocalVariableAttr(llvm::Value *value) {
1348  auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1349  auto *node = cast<llvm::DILocalVariable>(nodeAsVal->getMetadata());
1350  return debugImporter->translate(node);
1351 }
1352 
1353 DILabelAttr ModuleImport::matchLabelAttr(llvm::Value *value) {
1354  auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1355  auto *node = cast<llvm::DILabel>(nodeAsVal->getMetadata());
1356  return debugImporter->translate(node);
1357 }
1358 
1359 FPExceptionBehaviorAttr
1361  auto *metadata = cast<llvm::MetadataAsValue>(value);
1362  auto *mdstr = cast<llvm::MDString>(metadata->getMetadata());
1363  std::optional<llvm::fp::ExceptionBehavior> optLLVM =
1364  llvm::convertStrToExceptionBehavior(mdstr->getString());
1365  assert(optLLVM && "Expecting FP exception behavior");
1366  return builder.getAttr<FPExceptionBehaviorAttr>(
1367  convertFPExceptionBehaviorFromLLVM(*optLLVM));
1368 }
1369 
1370 RoundingModeAttr ModuleImport::matchRoundingModeAttr(llvm::Value *value) {
1371  auto *metadata = cast<llvm::MetadataAsValue>(value);
1372  auto *mdstr = cast<llvm::MDString>(metadata->getMetadata());
1373  std::optional<llvm::RoundingMode> optLLVM =
1374  llvm::convertStrToRoundingMode(mdstr->getString());
1375  assert(optLLVM && "Expecting rounding mode");
1376  return builder.getAttr<RoundingModeAttr>(
1377  convertRoundingModeFromLLVM(*optLLVM));
1378 }
1379 
1380 FailureOr<SmallVector<AliasScopeAttr>>
1382  auto *nodeAsVal = cast<llvm::MetadataAsValue>(value);
1383  auto *node = cast<llvm::MDNode>(nodeAsVal->getMetadata());
1384  return lookupAliasScopeAttrs(node);
1385 }
1386 
1387 Location ModuleImport::translateLoc(llvm::DILocation *loc) {
1388  return debugImporter->translateLoc(loc);
1389 }
1390 
1391 LogicalResult
1392 ModuleImport::convertBranchArgs(llvm::Instruction *branch,
1393  llvm::BasicBlock *target,
1394  SmallVectorImpl<Value> &blockArguments) {
1395  for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) {
1396  auto *phiInst = cast<llvm::PHINode>(&*inst);
1397  llvm::Value *value = phiInst->getIncomingValueForBlock(branch->getParent());
1398  FailureOr<Value> converted = convertValue(value);
1399  if (failed(converted))
1400  return failure();
1401  blockArguments.push_back(*converted);
1402  }
1403  return success();
1404 }
1405 
1406 LogicalResult
1407 ModuleImport::convertCallTypeAndOperands(llvm::CallBase *callInst,
1408  SmallVectorImpl<Type> &types,
1409  SmallVectorImpl<Value> &operands) {
1410  if (!callInst->getType()->isVoidTy())
1411  types.push_back(convertType(callInst->getType()));
1412 
1413  if (!callInst->getCalledFunction()) {
1414  FailureOr<Value> called = convertValue(callInst->getCalledOperand());
1415  if (failed(called))
1416  return failure();
1417  operands.push_back(*called);
1418  }
1419  SmallVector<llvm::Value *> args(callInst->args());
1420  FailureOr<SmallVector<Value>> arguments = convertValues(args);
1421  if (failed(arguments))
1422  return failure();
1423  llvm::append_range(operands, *arguments);
1424  return success();
1425 }
1426 
1427 LogicalResult ModuleImport::convertIntrinsic(llvm::CallInst *inst) {
1428  if (succeeded(iface.convertIntrinsic(builder, inst, *this)))
1429  return success();
1430 
1431  Location loc = translateLoc(inst->getDebugLoc());
1432  return emitError(loc) << "unhandled intrinsic: " << diag(*inst);
1433 }
1434 
1435 LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) {
1436  // Convert all instructions that do not provide an MLIR builder.
1437  Location loc = translateLoc(inst->getDebugLoc());
1438  if (inst->getOpcode() == llvm::Instruction::Br) {
1439  auto *brInst = cast<llvm::BranchInst>(inst);
1440 
1441  SmallVector<Block *> succBlocks;
1442  SmallVector<SmallVector<Value>> succBlockArgs;
1443  for (auto i : llvm::seq<unsigned>(0, brInst->getNumSuccessors())) {
1444  llvm::BasicBlock *succ = brInst->getSuccessor(i);
1445  SmallVector<Value> blockArgs;
1446  if (failed(convertBranchArgs(brInst, succ, blockArgs)))
1447  return failure();
1448  succBlocks.push_back(lookupBlock(succ));
1449  succBlockArgs.push_back(blockArgs);
1450  }
1451 
1452  if (!brInst->isConditional()) {
1453  auto brOp = builder.create<LLVM::BrOp>(loc, succBlockArgs.front(),
1454  succBlocks.front());
1455  mapNoResultOp(inst, brOp);
1456  return success();
1457  }
1458  FailureOr<Value> condition = convertValue(brInst->getCondition());
1459  if (failed(condition))
1460  return failure();
1461  auto condBrOp = builder.create<LLVM::CondBrOp>(
1462  loc, *condition, succBlocks.front(), succBlockArgs.front(),
1463  succBlocks.back(), succBlockArgs.back());
1464  mapNoResultOp(inst, condBrOp);
1465  return success();
1466  }
1467  if (inst->getOpcode() == llvm::Instruction::Switch) {
1468  auto *swInst = cast<llvm::SwitchInst>(inst);
1469  // Process the condition value.
1470  FailureOr<Value> condition = convertValue(swInst->getCondition());
1471  if (failed(condition))
1472  return failure();
1473  SmallVector<Value> defaultBlockArgs;
1474  // Process the default case.
1475  llvm::BasicBlock *defaultBB = swInst->getDefaultDest();
1476  if (failed(convertBranchArgs(swInst, defaultBB, defaultBlockArgs)))
1477  return failure();
1478 
1479  // Process the cases.
1480  unsigned numCases = swInst->getNumCases();
1481  SmallVector<SmallVector<Value>> caseOperands(numCases);
1482  SmallVector<ValueRange> caseOperandRefs(numCases);
1483  SmallVector<APInt> caseValues(numCases);
1484  SmallVector<Block *> caseBlocks(numCases);
1485  for (const auto &it : llvm::enumerate(swInst->cases())) {
1486  const llvm::SwitchInst::CaseHandle &caseHandle = it.value();
1487  llvm::BasicBlock *succBB = caseHandle.getCaseSuccessor();
1488  if (failed(convertBranchArgs(swInst, succBB, caseOperands[it.index()])))
1489  return failure();
1490  caseOperandRefs[it.index()] = caseOperands[it.index()];
1491  caseValues[it.index()] = caseHandle.getCaseValue()->getValue();
1492  caseBlocks[it.index()] = lookupBlock(succBB);
1493  }
1494 
1495  auto switchOp = builder.create<SwitchOp>(
1496  loc, *condition, lookupBlock(defaultBB), defaultBlockArgs, caseValues,
1497  caseBlocks, caseOperandRefs);
1498  mapNoResultOp(inst, switchOp);
1499  return success();
1500  }
1501  if (inst->getOpcode() == llvm::Instruction::PHI) {
1502  Type type = convertType(inst->getType());
1503  mapValue(inst, builder.getInsertionBlock()->addArgument(
1504  type, translateLoc(inst->getDebugLoc())));
1505  return success();
1506  }
1507  if (inst->getOpcode() == llvm::Instruction::Call) {
1508  auto *callInst = cast<llvm::CallInst>(inst);
1509 
1510  SmallVector<Type> types;
1511  SmallVector<Value> operands;
1512  if (failed(convertCallTypeAndOperands(callInst, types, operands)))
1513  return failure();
1514 
1515  auto funcTy =
1516  dyn_cast<LLVMFunctionType>(convertType(callInst->getFunctionType()));
1517  if (!funcTy)
1518  return failure();
1519 
1520  CallOp callOp;
1521 
1522  if (llvm::Function *callee = callInst->getCalledFunction()) {
1523  callOp = builder.create<CallOp>(
1524  loc, funcTy, SymbolRefAttr::get(context, callee->getName()),
1525  operands);
1526  } else {
1527  callOp = builder.create<CallOp>(loc, funcTy, operands);
1528  }
1529  callOp.setCConv(convertCConvFromLLVM(callInst->getCallingConv()));
1530  callOp.setTailCallKind(
1531  convertTailCallKindFromLLVM(callInst->getTailCallKind()));
1532  setFastmathFlagsAttr(inst, callOp);
1533 
1534  // Handle function attributes.
1535  if (callInst->hasFnAttr(llvm::Attribute::Convergent))
1536  callOp.setConvergent(true);
1537  if (callInst->hasFnAttr(llvm::Attribute::NoUnwind))
1538  callOp.setNoUnwind(true);
1539  if (callInst->hasFnAttr(llvm::Attribute::WillReturn))
1540  callOp.setWillReturn(true);
1541 
1542  llvm::MemoryEffects memEffects = callInst->getMemoryEffects();
1543  ModRefInfo othermem = convertModRefInfoFromLLVM(
1544  memEffects.getModRef(llvm::MemoryEffects::Location::Other));
1545  ModRefInfo argMem = convertModRefInfoFromLLVM(
1546  memEffects.getModRef(llvm::MemoryEffects::Location::ArgMem));
1547  ModRefInfo inaccessibleMem = convertModRefInfoFromLLVM(
1548  memEffects.getModRef(llvm::MemoryEffects::Location::InaccessibleMem));
1549  auto memAttr = MemoryEffectsAttr::get(callOp.getContext(), othermem, argMem,
1550  inaccessibleMem);
1551  // Only set the attribute when it does not match the default value.
1552  if (!memAttr.isReadWrite())
1553  callOp.setMemoryEffectsAttr(memAttr);
1554 
1555  if (!callInst->getType()->isVoidTy())
1556  mapValue(inst, callOp.getResult());
1557  else
1558  mapNoResultOp(inst, callOp);
1559  return success();
1560  }
1561  if (inst->getOpcode() == llvm::Instruction::LandingPad) {
1562  auto *lpInst = cast<llvm::LandingPadInst>(inst);
1563 
1564  SmallVector<Value> operands;
1565  operands.reserve(lpInst->getNumClauses());
1566  for (auto i : llvm::seq<unsigned>(0, lpInst->getNumClauses())) {
1567  FailureOr<Value> operand = convertValue(lpInst->getClause(i));
1568  if (failed(operand))
1569  return failure();
1570  operands.push_back(*operand);
1571  }
1572 
1573  Type type = convertType(lpInst->getType());
1574  auto lpOp =
1575  builder.create<LandingpadOp>(loc, type, lpInst->isCleanup(), operands);
1576  mapValue(inst, lpOp);
1577  return success();
1578  }
1579  if (inst->getOpcode() == llvm::Instruction::Invoke) {
1580  auto *invokeInst = cast<llvm::InvokeInst>(inst);
1581 
1582  SmallVector<Type> types;
1583  SmallVector<Value> operands;
1584  if (failed(convertCallTypeAndOperands(invokeInst, types, operands)))
1585  return failure();
1586 
1587  // Check whether the invoke result is an argument to the normal destination
1588  // block.
1589  bool invokeResultUsedInPhi = llvm::any_of(
1590  invokeInst->getNormalDest()->phis(), [&](const llvm::PHINode &phi) {
1591  return phi.getIncomingValueForBlock(invokeInst->getParent()) ==
1592  invokeInst;
1593  });
1594 
1595  Block *normalDest = lookupBlock(invokeInst->getNormalDest());
1596  Block *directNormalDest = normalDest;
1597  if (invokeResultUsedInPhi) {
1598  // The invoke result cannot be an argument to the normal destination
1599  // block, as that would imply using the invoke operation result in its
1600  // definition, so we need to create a dummy block to serve as an
1601  // intermediate destination.
1602  OpBuilder::InsertionGuard g(builder);
1603  directNormalDest = builder.createBlock(normalDest);
1604  }
1605 
1606  SmallVector<Value> unwindArgs;
1607  if (failed(convertBranchArgs(invokeInst, invokeInst->getUnwindDest(),
1608  unwindArgs)))
1609  return failure();
1610 
1611  auto funcTy =
1612  dyn_cast<LLVMFunctionType>(convertType(invokeInst->getFunctionType()));
1613  if (!funcTy)
1614  return failure();
1615 
1616  // Create the invoke operation. Normal destination block arguments will be
1617  // added later on to handle the case in which the operation result is
1618  // included in this list.
1619  InvokeOp invokeOp;
1620  if (llvm::Function *callee = invokeInst->getCalledFunction()) {
1621  invokeOp = builder.create<InvokeOp>(
1622  loc, funcTy,
1623  SymbolRefAttr::get(builder.getContext(), callee->getName()), operands,
1624  directNormalDest, ValueRange(),
1625  lookupBlock(invokeInst->getUnwindDest()), unwindArgs);
1626  } else {
1627  invokeOp = builder.create<InvokeOp>(
1628  loc, funcTy, /*callee=*/nullptr, operands, directNormalDest,
1629  ValueRange(), lookupBlock(invokeInst->getUnwindDest()), unwindArgs);
1630  }
1631  invokeOp.setCConv(convertCConvFromLLVM(invokeInst->getCallingConv()));
1632  if (!invokeInst->getType()->isVoidTy())
1633  mapValue(inst, invokeOp.getResults().front());
1634  else
1635  mapNoResultOp(inst, invokeOp);
1636 
1637  SmallVector<Value> normalArgs;
1638  if (failed(convertBranchArgs(invokeInst, invokeInst->getNormalDest(),
1639  normalArgs)))
1640  return failure();
1641 
1642  if (invokeResultUsedInPhi) {
1643  // The dummy normal dest block will just host an unconditional branch
1644  // instruction to the normal destination block passing the required block
1645  // arguments (including the invoke operation's result).
1646  OpBuilder::InsertionGuard g(builder);
1647  builder.setInsertionPointToStart(directNormalDest);
1648  builder.create<LLVM::BrOp>(loc, normalArgs, normalDest);
1649  } else {
1650  // If the invoke operation's result is not a block argument to the normal
1651  // destination block, just add the block arguments as usual.
1652  assert(llvm::none_of(
1653  normalArgs,
1654  [&](Value val) { return val.getDefiningOp() == invokeOp; }) &&
1655  "An llvm.invoke operation cannot pass its result as a block "
1656  "argument.");
1657  invokeOp.getNormalDestOperandsMutable().append(normalArgs);
1658  }
1659 
1660  return success();
1661  }
1662  if (inst->getOpcode() == llvm::Instruction::GetElementPtr) {
1663  auto *gepInst = cast<llvm::GetElementPtrInst>(inst);
1664  Type sourceElementType = convertType(gepInst->getSourceElementType());
1665  FailureOr<Value> basePtr = convertValue(gepInst->getOperand(0));
1666  if (failed(basePtr))
1667  return failure();
1668 
1669  // Treat every indices as dynamic since GEPOp::build will refine those
1670  // indices into static attributes later. One small downside of this
1671  // approach is that many unused `llvm.mlir.constant` would be emitted
1672  // at first place.
1673  SmallVector<GEPArg> indices;
1674  for (llvm::Value *operand : llvm::drop_begin(gepInst->operand_values())) {
1675  FailureOr<Value> index = convertValue(operand);
1676  if (failed(index))
1677  return failure();
1678  indices.push_back(*index);
1679  }
1680 
1681  Type type = convertType(inst->getType());
1682  auto gepOp = builder.create<GEPOp>(loc, type, sourceElementType, *basePtr,
1683  indices, gepInst->isInBounds());
1684  mapValue(inst, gepOp);
1685  return success();
1686  }
1687 
1688  // Convert all instructions that have an mlirBuilder.
1689  if (succeeded(convertInstructionImpl(builder, inst, *this, iface)))
1690  return success();
1691 
1692  return emitError(loc) << "unhandled instruction: " << diag(*inst);
1693 }
1694 
1695 LogicalResult ModuleImport::processInstruction(llvm::Instruction *inst) {
1696  // FIXME: Support uses of SubtargetData.
1697  // FIXME: Add support for call / operand attributes.
1698  // FIXME: Add support for the indirectbr, cleanupret, catchret, catchswitch,
1699  // callbr, vaarg, catchpad, cleanuppad instructions.
1700 
1701  // Convert LLVM intrinsics calls to MLIR intrinsics.
1702  if (auto *intrinsic = dyn_cast<llvm::IntrinsicInst>(inst))
1703  return convertIntrinsic(intrinsic);
1704 
1705  // Convert all remaining LLVM instructions to MLIR operations.
1706  return convertInstruction(inst);
1707 }
1708 
1709 FlatSymbolRefAttr ModuleImport::getPersonalityAsAttr(llvm::Function *f) {
1710  if (!f->hasPersonalityFn())
1711  return nullptr;
1712 
1713  llvm::Constant *pf = f->getPersonalityFn();
1714 
1715  // If it directly has a name, we can use it.
1716  if (pf->hasName())
1717  return SymbolRefAttr::get(builder.getContext(), pf->getName());
1718 
1719  // If it doesn't have a name, currently, only function pointers that are
1720  // bitcast to i8* are parsed.
1721  if (auto *ce = dyn_cast<llvm::ConstantExpr>(pf)) {
1722  if (ce->getOpcode() == llvm::Instruction::BitCast &&
1723  ce->getType() == llvm::PointerType::getUnqual(f->getContext())) {
1724  if (auto *func = dyn_cast<llvm::Function>(ce->getOperand(0)))
1725  return SymbolRefAttr::get(builder.getContext(), func->getName());
1726  }
1727  }
1728  return FlatSymbolRefAttr();
1729 }
1730 
1731 static void processMemoryEffects(llvm::Function *func, LLVMFuncOp funcOp) {
1732  llvm::MemoryEffects memEffects = func->getMemoryEffects();
1733 
1734  auto othermem = convertModRefInfoFromLLVM(
1735  memEffects.getModRef(llvm::MemoryEffects::Location::Other));
1736  auto argMem = convertModRefInfoFromLLVM(
1737  memEffects.getModRef(llvm::MemoryEffects::Location::ArgMem));
1738  auto inaccessibleMem = convertModRefInfoFromLLVM(
1739  memEffects.getModRef(llvm::MemoryEffects::Location::InaccessibleMem));
1740  auto memAttr = MemoryEffectsAttr::get(funcOp.getContext(), othermem, argMem,
1741  inaccessibleMem);
1742  // Only set the attr when it does not match the default value.
1743  if (memAttr.isReadWrite())
1744  return;
1745  funcOp.setMemoryEffectsAttr(memAttr);
1746 }
1747 
1748 // List of LLVM IR attributes that map to an explicit attribute on the MLIR
1749 // LLVMFuncOp.
1750 static constexpr std::array kExplicitAttributes{
1751  StringLiteral("aarch64_in_za"),
1752  StringLiteral("aarch64_inout_za"),
1753  StringLiteral("aarch64_new_za"),
1754  StringLiteral("aarch64_out_za"),
1755  StringLiteral("aarch64_preserves_za"),
1756  StringLiteral("aarch64_pstate_sm_body"),
1757  StringLiteral("aarch64_pstate_sm_compatible"),
1758  StringLiteral("aarch64_pstate_sm_enabled"),
1759  StringLiteral("alwaysinline"),
1760  StringLiteral("approx-func-fp-math"),
1761  StringLiteral("convergent"),
1762  StringLiteral("denormal-fp-math"),
1763  StringLiteral("denormal-fp-math-f32"),
1764  StringLiteral("fp-contract"),
1765  StringLiteral("frame-pointer"),
1766  StringLiteral("no-infs-fp-math"),
1767  StringLiteral("no-nans-fp-math"),
1768  StringLiteral("no-signed-zeros-fp-math"),
1769  StringLiteral("noinline"),
1770  StringLiteral("nounwind"),
1771  StringLiteral("optnone"),
1772  StringLiteral("target-features"),
1773  StringLiteral("tune-cpu"),
1774  StringLiteral("unsafe-fp-math"),
1775  StringLiteral("vscale_range"),
1776  StringLiteral("willreturn"),
1777 };
1778 
1779 static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp) {
1780  MLIRContext *context = funcOp.getContext();
1781  SmallVector<Attribute> passthroughs;
1782  llvm::AttributeSet funcAttrs = func->getAttributes().getAttributes(
1783  llvm::AttributeList::AttrIndex::FunctionIndex);
1784  for (llvm::Attribute attr : funcAttrs) {
1785  // Skip the memory attribute since the LLVMFuncOp has an explicit memory
1786  // attribute.
1787  if (attr.hasAttribute(llvm::Attribute::Memory))
1788  continue;
1789 
1790  // Skip invalid type attributes.
1791  if (attr.isTypeAttribute()) {
1792  emitWarning(funcOp.getLoc(),
1793  "type attributes on a function are invalid, skipping it");
1794  continue;
1795  }
1796 
1797  StringRef attrName;
1798  if (attr.isStringAttribute())
1799  attrName = attr.getKindAsString();
1800  else
1801  attrName = llvm::Attribute::getNameFromAttrKind(attr.getKindAsEnum());
1802  auto keyAttr = StringAttr::get(context, attrName);
1803 
1804  // Skip attributes that map to an explicit attribute on the LLVMFuncOp.
1805  if (llvm::is_contained(kExplicitAttributes, attrName))
1806  continue;
1807 
1808  if (attr.isStringAttribute()) {
1809  StringRef val = attr.getValueAsString();
1810  if (val.empty()) {
1811  passthroughs.push_back(keyAttr);
1812  continue;
1813  }
1814  passthroughs.push_back(
1815  ArrayAttr::get(context, {keyAttr, StringAttr::get(context, val)}));
1816  continue;
1817  }
1818  if (attr.isIntAttribute()) {
1819  auto val = std::to_string(attr.getValueAsInt());
1820  passthroughs.push_back(
1821  ArrayAttr::get(context, {keyAttr, StringAttr::get(context, val)}));
1822  continue;
1823  }
1824  if (attr.isEnumAttribute()) {
1825  passthroughs.push_back(keyAttr);
1826  continue;
1827  }
1828 
1829  llvm_unreachable("unexpected attribute kind");
1830  }
1831 
1832  if (!passthroughs.empty())
1833  funcOp.setPassthroughAttr(ArrayAttr::get(context, passthroughs));
1834 }
1835 
1837  LLVMFuncOp funcOp) {
1838  processMemoryEffects(func, funcOp);
1839  processPassthroughAttrs(func, funcOp);
1840 
1841  if (func->hasFnAttribute(llvm::Attribute::NoInline))
1842  funcOp.setNoInline(true);
1843  if (func->hasFnAttribute(llvm::Attribute::AlwaysInline))
1844  funcOp.setAlwaysInline(true);
1845  if (func->hasFnAttribute(llvm::Attribute::OptimizeNone))
1846  funcOp.setOptimizeNone(true);
1847  if (func->hasFnAttribute(llvm::Attribute::Convergent))
1848  funcOp.setConvergent(true);
1849  if (func->hasFnAttribute(llvm::Attribute::NoUnwind))
1850  funcOp.setNoUnwind(true);
1851  if (func->hasFnAttribute(llvm::Attribute::WillReturn))
1852  funcOp.setWillReturn(true);
1853 
1854  if (func->hasFnAttribute("aarch64_pstate_sm_enabled"))
1855  funcOp.setArmStreaming(true);
1856  else if (func->hasFnAttribute("aarch64_pstate_sm_body"))
1857  funcOp.setArmLocallyStreaming(true);
1858  else if (func->hasFnAttribute("aarch64_pstate_sm_compatible"))
1859  funcOp.setArmStreamingCompatible(true);
1860 
1861  if (func->hasFnAttribute("aarch64_new_za"))
1862  funcOp.setArmNewZa(true);
1863  else if (func->hasFnAttribute("aarch64_in_za"))
1864  funcOp.setArmInZa(true);
1865  else if (func->hasFnAttribute("aarch64_out_za"))
1866  funcOp.setArmOutZa(true);
1867  else if (func->hasFnAttribute("aarch64_inout_za"))
1868  funcOp.setArmInoutZa(true);
1869  else if (func->hasFnAttribute("aarch64_preserves_za"))
1870  funcOp.setArmPreservesZa(true);
1871 
1872  llvm::Attribute attr = func->getFnAttribute(llvm::Attribute::VScaleRange);
1873  if (attr.isValid()) {
1874  MLIRContext *context = funcOp.getContext();
1875  auto intTy = IntegerType::get(context, 32);
1876  funcOp.setVscaleRangeAttr(LLVM::VScaleRangeAttr::get(
1877  context, IntegerAttr::get(intTy, attr.getVScaleRangeMin()),
1878  IntegerAttr::get(intTy, attr.getVScaleRangeMax().value_or(0))));
1879  }
1880 
1881  // Process frame-pointer attribute.
1882  if (func->hasFnAttribute("frame-pointer")) {
1883  StringRef stringRefFramePointerKind =
1884  func->getFnAttribute("frame-pointer").getValueAsString();
1885  funcOp.setFramePointerAttr(LLVM::FramePointerKindAttr::get(
1886  funcOp.getContext(), LLVM::framePointerKind::symbolizeFramePointerKind(
1887  stringRefFramePointerKind)
1888  .value()));
1889  }
1890 
1891  if (llvm::Attribute attr = func->getFnAttribute("target-cpu");
1892  attr.isStringAttribute())
1893  funcOp.setTargetCpuAttr(StringAttr::get(context, attr.getValueAsString()));
1894 
1895  if (llvm::Attribute attr = func->getFnAttribute("tune-cpu");
1896  attr.isStringAttribute())
1897  funcOp.setTuneCpuAttr(StringAttr::get(context, attr.getValueAsString()));
1898 
1899  if (llvm::Attribute attr = func->getFnAttribute("target-features");
1900  attr.isStringAttribute())
1901  funcOp.setTargetFeaturesAttr(
1902  LLVM::TargetFeaturesAttr::get(context, attr.getValueAsString()));
1903 
1904  if (llvm::Attribute attr = func->getFnAttribute("unsafe-fp-math");
1905  attr.isStringAttribute())
1906  funcOp.setUnsafeFpMath(attr.getValueAsBool());
1907 
1908  if (llvm::Attribute attr = func->getFnAttribute("no-infs-fp-math");
1909  attr.isStringAttribute())
1910  funcOp.setNoInfsFpMath(attr.getValueAsBool());
1911 
1912  if (llvm::Attribute attr = func->getFnAttribute("no-nans-fp-math");
1913  attr.isStringAttribute())
1914  funcOp.setNoNansFpMath(attr.getValueAsBool());
1915 
1916  if (llvm::Attribute attr = func->getFnAttribute("approx-func-fp-math");
1917  attr.isStringAttribute())
1918  funcOp.setApproxFuncFpMath(attr.getValueAsBool());
1919 
1920  if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
1921  attr.isStringAttribute())
1922  funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());
1923 
1924  if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math");
1925  attr.isStringAttribute())
1926  funcOp.setDenormalFpMathAttr(
1927  StringAttr::get(context, attr.getValueAsString()));
1928 
1929  if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math-f32");
1930  attr.isStringAttribute())
1931  funcOp.setDenormalFpMathF32Attr(
1932  StringAttr::get(context, attr.getValueAsString()));
1933 
1934  if (llvm::Attribute attr = func->getFnAttribute("fp-contract");
1935  attr.isStringAttribute())
1936  funcOp.setFpContractAttr(StringAttr::get(context, attr.getValueAsString()));
1937 }
1938 
1939 DictionaryAttr
1940 ModuleImport::convertParameterAttribute(llvm::AttributeSet llvmParamAttrs,
1941  OpBuilder &builder) {
1942  SmallVector<NamedAttribute> paramAttrs;
1943  for (auto [llvmKind, mlirName] : getAttrKindToNameMapping()) {
1944  auto llvmAttr = llvmParamAttrs.getAttribute(llvmKind);
1945  // Skip attributes that are not attached.
1946  if (!llvmAttr.isValid())
1947  continue;
1948  Attribute mlirAttr;
1949  if (llvmAttr.isTypeAttribute())
1950  mlirAttr = TypeAttr::get(convertType(llvmAttr.getValueAsType()));
1951  else if (llvmAttr.isIntAttribute())
1952  mlirAttr = builder.getI64IntegerAttr(llvmAttr.getValueAsInt());
1953  else if (llvmAttr.isEnumAttribute())
1954  mlirAttr = builder.getUnitAttr();
1955  else
1956  llvm_unreachable("unexpected parameter attribute kind");
1957  paramAttrs.push_back(builder.getNamedAttr(mlirName, mlirAttr));
1958  }
1959 
1960  return builder.getDictionaryAttr(paramAttrs);
1961 }
1962 
1963 void ModuleImport::convertParameterAttributes(llvm::Function *func,
1964  LLVMFuncOp funcOp,
1965  OpBuilder &builder) {
1966  auto llvmAttrs = func->getAttributes();
1967  for (size_t i = 0, e = funcOp.getNumArguments(); i < e; ++i) {
1968  llvm::AttributeSet llvmArgAttrs = llvmAttrs.getParamAttrs(i);
1969  funcOp.setArgAttrs(i, convertParameterAttribute(llvmArgAttrs, builder));
1970  }
1971  // Convert the result attributes and attach them wrapped in an ArrayAttribute
1972  // to the funcOp.
1973  llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs();
1974  if (!llvmResAttr.hasAttributes())
1975  return;
1976  funcOp.setResAttrsAttr(
1977  builder.getArrayAttr(convertParameterAttribute(llvmResAttr, builder)));
1978 }
1979 
1980 LogicalResult ModuleImport::processFunction(llvm::Function *func) {
1981  clearRegionState();
1982 
1983  auto functionType =
1984  dyn_cast<LLVMFunctionType>(convertType(func->getFunctionType()));
1985  if (func->isIntrinsic() &&
1986  iface.isConvertibleIntrinsic(func->getIntrinsicID()))
1987  return success();
1988 
1989  bool dsoLocal = func->hasLocalLinkage();
1990  CConv cconv = convertCConvFromLLVM(func->getCallingConv());
1991 
1992  // Insert the function at the end of the module.
1993  OpBuilder::InsertionGuard guard(builder);
1994  builder.setInsertionPoint(mlirModule.getBody(), mlirModule.getBody()->end());
1995 
1996  Location loc = debugImporter->translateFuncLocation(func);
1997  LLVMFuncOp funcOp = builder.create<LLVMFuncOp>(
1998  loc, func->getName(), functionType,
1999  convertLinkageFromLLVM(func->getLinkage()), dsoLocal, cconv);
2000 
2001  convertParameterAttributes(func, funcOp, builder);
2002 
2003  if (FlatSymbolRefAttr personality = getPersonalityAsAttr(func))
2004  funcOp.setPersonalityAttr(personality);
2005  else if (func->hasPersonalityFn())
2006  emitWarning(funcOp.getLoc(), "could not deduce personality, skipping it");
2007 
2008  if (func->hasGC())
2009  funcOp.setGarbageCollector(StringRef(func->getGC()));
2010 
2011  if (func->hasAtLeastLocalUnnamedAddr())
2012  funcOp.setUnnamedAddr(convertUnnamedAddrFromLLVM(func->getUnnamedAddr()));
2013 
2014  if (func->hasSection())
2015  funcOp.setSection(StringRef(func->getSection()));
2016 
2017  funcOp.setVisibility_(convertVisibilityFromLLVM(func->getVisibility()));
2018 
2019  if (func->hasComdat())
2020  funcOp.setComdatAttr(comdatMapping.lookup(func->getComdat()));
2021 
2022  if (llvm::MaybeAlign maybeAlign = func->getAlign())
2023  funcOp.setAlignment(maybeAlign->value());
2024 
2025  // Handle Function attributes.
2026  processFunctionAttributes(func, funcOp);
2027 
2028  // Convert non-debug metadata by using the dialect interface.
2030  func->getAllMetadata(allMetadata);
2031  for (auto &[kind, node] : allMetadata) {
2032  if (!iface.isConvertibleMetadata(kind))
2033  continue;
2034  if (failed(iface.setMetadataAttrs(builder, kind, node, funcOp, *this))) {
2035  emitWarning(funcOp.getLoc())
2036  << "unhandled function metadata: " << diagMD(node, llvmModule.get())
2037  << " on " << diag(*func);
2038  }
2039  }
2040 
2041  if (func->isDeclaration())
2042  return success();
2043 
2044  // Collect the set of basic blocks reachable from the function's entry block.
2045  // This step is crucial as LLVM IR can contain unreachable blocks that
2046  // self-dominate. As a result, an operation might utilize a variable it
2047  // defines, which the import does not support. Given that MLIR lacks block
2048  // label support, we can safely remove unreachable blocks, as there are no
2049  // indirect branch instructions that could potentially target these blocks.
2050  llvm::df_iterator_default_set<llvm::BasicBlock *> reachable;
2051  for (llvm::BasicBlock *basicBlock : llvm::depth_first_ext(func, reachable))
2052  (void)basicBlock;
2053 
2054  // Eagerly create all reachable blocks.
2055  SmallVector<llvm::BasicBlock *> reachableBasicBlocks;
2056  for (llvm::BasicBlock &basicBlock : *func) {
2057  // Skip unreachable blocks.
2058  if (!reachable.contains(&basicBlock))
2059  continue;
2060  Region &body = funcOp.getBody();
2061  Block *block = builder.createBlock(&body, body.end());
2062  mapBlock(&basicBlock, block);
2063  reachableBasicBlocks.push_back(&basicBlock);
2064  }
2065 
2066  // Add function arguments to the entry block.
2067  for (const auto &it : llvm::enumerate(func->args())) {
2068  BlockArgument blockArg = funcOp.getFunctionBody().addArgument(
2069  functionType.getParamType(it.index()), funcOp.getLoc());
2070  mapValue(&it.value(), blockArg);
2071  }
2072 
2073  // Process the blocks in topological order. The ordered traversal ensures
2074  // operands defined in a dominating block have a valid mapping to an MLIR
2075  // value once a block is translated.
2077  getTopologicallySortedBlocks(reachableBasicBlocks);
2078  setConstantInsertionPointToStart(lookupBlock(blocks.front()));
2079  for (llvm::BasicBlock *basicBlock : blocks)
2080  if (failed(processBasicBlock(basicBlock, lookupBlock(basicBlock))))
2081  return failure();
2082 
2083  // Process the debug intrinsics that require a delayed conversion after
2084  // everything else was converted.
2085  if (failed(processDebugIntrinsics()))
2086  return failure();
2087 
2088  return success();
2089 }
2090 
2091 /// Checks if `dbgIntr` is a kill location that holds metadata instead of an SSA
2092 /// value.
2093 static bool isMetadataKillLocation(llvm::DbgVariableIntrinsic *dbgIntr) {
2094  if (!dbgIntr->isKillLocation())
2095  return false;
2096  llvm::Value *value = dbgIntr->getArgOperand(0);
2097  auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(value);
2098  if (!nodeAsVal)
2099  return false;
2100  return !isa<llvm::ValueAsMetadata>(nodeAsVal->getMetadata());
2101 }
2102 
2103 LogicalResult
2104 ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr,
2105  DominanceInfo &domInfo) {
2106  Location loc = translateLoc(dbgIntr->getDebugLoc());
2107  auto emitUnsupportedWarning = [&]() {
2108  if (emitExpensiveWarnings)
2109  emitWarning(loc) << "dropped intrinsic: " << diag(*dbgIntr);
2110  return success();
2111  };
2112  // Drop debug intrinsics with arg lists.
2113  // TODO: Support debug intrinsics that have arg lists.
2114  if (dbgIntr->hasArgList())
2115  return emitUnsupportedWarning();
2116  // Kill locations can have metadata nodes as location operand. This
2117  // cannot be converted to poison as the type cannot be reconstructed.
2118  // TODO: find a way to support this case.
2119  if (isMetadataKillLocation(dbgIntr))
2120  return emitUnsupportedWarning();
2121  // Drop debug intrinsics if the associated variable information cannot be
2122  // translated due to cyclic debug metadata.
2123  // TODO: Support cyclic debug metadata.
2124  DILocalVariableAttr localVariableAttr =
2125  matchLocalVariableAttr(dbgIntr->getArgOperand(1));
2126  if (!localVariableAttr)
2127  return emitUnsupportedWarning();
2128  FailureOr<Value> argOperand = convertMetadataValue(dbgIntr->getArgOperand(0));
2129  if (failed(argOperand))
2130  return emitError(loc) << "failed to convert a debug intrinsic operand: "
2131  << diag(*dbgIntr);
2132 
2133  // Ensure that the debug instrinsic is inserted right after its operand is
2134  // defined. Otherwise, the operand might not necessarily dominate the
2135  // intrinsic. If the defining operation is a terminator, insert the intrinsic
2136  // into a dominated block.
2137  OpBuilder::InsertionGuard guard(builder);
2138  if (Operation *op = argOperand->getDefiningOp();
2139  op && op->hasTrait<OpTrait::IsTerminator>()) {
2140  // Find a dominated block that can hold the debug intrinsic.
2141  auto dominatedBlocks = domInfo.getNode(op->getBlock())->children();
2142  // If no block is dominated by the terminator, this intrinisc cannot be
2143  // converted.
2144  if (dominatedBlocks.empty())
2145  return emitUnsupportedWarning();
2146  // Set insertion point before the terminator, to avoid inserting something
2147  // before landingpads.
2148  Block *dominatedBlock = (*dominatedBlocks.begin())->getBlock();
2149  builder.setInsertionPoint(dominatedBlock->getTerminator());
2150  } else {
2151  builder.setInsertionPointAfterValue(*argOperand);
2152  }
2153  auto locationExprAttr =
2154  debugImporter->translateExpression(dbgIntr->getExpression());
2155  Operation *op =
2157  .Case([&](llvm::DbgDeclareInst *) {
2158  return builder.create<LLVM::DbgDeclareOp>(
2159  loc, *argOperand, localVariableAttr, locationExprAttr);
2160  })
2161  .Case([&](llvm::DbgValueInst *) {
2162  return builder.create<LLVM::DbgValueOp>(
2163  loc, *argOperand, localVariableAttr, locationExprAttr);
2164  });
2165  mapNoResultOp(dbgIntr, op);
2166  setNonDebugMetadataAttrs(dbgIntr, op);
2167  return success();
2168 }
2169 
2170 LogicalResult ModuleImport::processDebugIntrinsics() {
2171  DominanceInfo domInfo;
2172  for (llvm::Instruction *inst : debugIntrinsics) {
2173  auto *intrCall = cast<llvm::DbgVariableIntrinsic>(inst);
2174  if (failed(processDebugIntrinsic(intrCall, domInfo)))
2175  return failure();
2176  }
2177  return success();
2178 }
2179 
2180 LogicalResult ModuleImport::processBasicBlock(llvm::BasicBlock *bb,
2181  Block *block) {
2182  builder.setInsertionPointToStart(block);
2183  for (llvm::Instruction &inst : *bb) {
2184  if (failed(processInstruction(&inst)))
2185  return failure();
2186 
2187  // Skip additional processing when the instructions is a debug intrinsics
2188  // that was not yet converted.
2189  if (debugIntrinsics.contains(&inst))
2190  continue;
2191 
2192  // Set the non-debug metadata attributes on the imported operation and emit
2193  // a warning if an instruction other than a phi instruction is dropped
2194  // during the import.
2195  if (Operation *op = lookupOperation(&inst)) {
2196  setNonDebugMetadataAttrs(&inst, op);
2197  } else if (inst.getOpcode() != llvm::Instruction::PHI) {
2198  if (emitExpensiveWarnings) {
2199  Location loc = debugImporter->translateLoc(inst.getDebugLoc());
2200  emitWarning(loc) << "dropped instruction: " << diag(inst);
2201  }
2202  }
2203  }
2204  return success();
2205 }
2206 
2207 FailureOr<SmallVector<AccessGroupAttr>>
2208 ModuleImport::lookupAccessGroupAttrs(const llvm::MDNode *node) const {
2209  return loopAnnotationImporter->lookupAccessGroupAttrs(node);
2210 }
2211 
2212 LoopAnnotationAttr
2214  Location loc) const {
2215  return loopAnnotationImporter->translateLoopAnnotation(node, loc);
2216 }
2217 
2219 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
2220  MLIRContext *context, bool emitExpensiveWarnings,
2221  bool dropDICompositeTypeElements) {
2222  // Preload all registered dialects to allow the import to iterate the
2223  // registered LLVMImportDialectInterface implementations and query the
2224  // supported LLVM IR constructs before starting the translation. Assumes the
2225  // LLVM and DLTI dialects that convert the core LLVM IR constructs have been
2226  // registered before.
2227  assert(llvm::is_contained(context->getAvailableDialects(),
2228  LLVMDialect::getDialectNamespace()));
2229  assert(llvm::is_contained(context->getAvailableDialects(),
2230  DLTIDialect::getDialectNamespace()));
2231  context->loadAllAvailableDialects();
2232  OwningOpRef<ModuleOp> module(ModuleOp::create(FileLineColLoc::get(
2233  StringAttr::get(context, llvmModule->getSourceFileName()), /*line=*/0,
2234  /*column=*/0)));
2235 
2236  ModuleImport moduleImport(module.get(), std::move(llvmModule),
2237  emitExpensiveWarnings, dropDICompositeTypeElements);
2238  if (failed(moduleImport.initializeImportInterface()))
2239  return {};
2240  if (failed(moduleImport.convertDataLayout()))
2241  return {};
2242  if (failed(moduleImport.convertComdats()))
2243  return {};
2244  if (failed(moduleImport.convertMetadata()))
2245  return {};
2246  if (failed(moduleImport.convertGlobals()))
2247  return {};
2248  if (failed(moduleImport.convertFunctions()))
2249  return {};
2250 
2251  return module;
2252 }
static MLIRContext * getContext(OpFoldResult val)
static SmallVector< int64_t > getPositionFromIndices(ArrayRef< unsigned > indices)
Converts an array of unsigned indices to a signed integer position array.
static constexpr std::array kExplicitAttributes
static StringRef getLLVMSyncScope(llvm::Instruction *inst)
Converts the sync scope identifier of inst to the string representation necessary to build an atomic ...
static std::string diag(const llvm::Value &value)
static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp)
static void processMemoryEffects(llvm::Function *func, LLVMFuncOp funcOp)
static constexpr StringRef getGlobalDtorsVarName()
Returns the name of the global_dtors global variables.
static Type getVectorTypeForAttr(Type type, ArrayRef< int64_t > arrayShape={})
Returns type if it is a builtin integer or floating-point vector type that can be used to create an a...
static LogicalResult convertInstructionImpl(OpBuilder &odsBuilder, llvm::Instruction *inst, ModuleImport &moduleImport, LLVMImportInterface &iface)
Converts the LLVM instructions that have a generated MLIR builder.
static constexpr StringRef getNamelessGlobalPrefix()
Prefix used for symbols of nameless llvm globals.
static constexpr StringRef getGlobalComdatOpName()
Returns the symbol name for the module-level comdat operation.
static constexpr StringRef getGlobalCtorsVarName()
Returns the name of the global_ctors global variables.
static bool isMetadataKillLocation(llvm::DbgVariableIntrinsic *dbgIntr)
Checks if dbgIntr is a kill location that holds metadata instead of an SSA value.
static TypedAttr getScalarConstantAsAttr(OpBuilder &builder, llvm::Constant *constScalar)
Returns an integer or float attribute for the provided scalar constant constScalar or nullptr if the ...
static SetVector< llvm::BasicBlock * > getTopologicallySortedBlocks(ArrayRef< llvm::BasicBlock * > basicBlocks)
Get a topologically sorted list of blocks for the given basic blocks.
static std::string diagMD(const llvm::Metadata *node, const llvm::Module *module)
static bool isScalarType(Type type)
Returns if type is a scalar integer or floating-point type.
static SmallVector< Attribute > getSequenceConstantAsAttrs(OpBuilder &builder, llvm::ConstantDataSequential *constSequence)
Returns an integer or float attribute array for the provided constant sequence constSequence or nullp...
static llvm::ManagedStatic< PassManagerOptions > options
static void contract(RootOrderingGraph &graph, ArrayRef< Value > cycle, const DenseMap< Value, unsigned > &parentDepths, DenseMap< Value, Value > &actualSource, DenseMap< Value, Value > &actualTarget)
Contracts the specified cycle in the given graph in-place.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class represents an argument of a Block.
Definition: Value.h:319
Block represents an ordered list of Operations.
Definition: Block.h:31
Operation * getTerminator()
Get the terminator operation of this block.
Definition: Block.cpp:243
BlockArgument addArgument(Type type, Location loc)
Add one value to the argument list.
Definition: Block.cpp:152
UnitAttr getUnitAttr()
Definition: Builders.cpp:138
IntegerAttr getI32IntegerAttr(int32_t value)
Definition: Builders.cpp:240
IntegerAttr getIntegerAttr(Type type, int64_t value)
Definition: Builders.cpp:262
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
Definition: Builders.cpp:307
FloatAttr getFloatAttr(Type type, double value)
Definition: Builders.cpp:285
IntegerType getI32Type()
Definition: Builders.cpp:107
IntegerAttr getI64IntegerAttr(int64_t value)
Definition: Builders.cpp:152
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:293
TypedAttr getZeroAttr(Type type)
Definition: Builders.cpp:355
MLIRContext * getContext() const
Definition: Builders.h:55
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
Definition: Builders.cpp:297
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition: Builders.cpp:144
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition: Builders.cpp:134
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition: Builders.cpp:337
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition: Builders.h:106
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
static DistinctAttr create(Attribute referencedAttr)
Creates a distinct attribute that associates a referenced attribute with a unique identifier.
A class for computing basic dominance information.
Definition: Dominance.h:136
A symbol reference with a reference path containing a single element.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
StringRef getValue() const
Returns the name of the held symbol reference.
static FloatType getBF16(MLIRContext *ctx)
Definition: BuiltinTypes.h:472
Interface collection for the import of LLVM IR that dispatches to a concrete dialect interface implem...
LogicalResult convertInstruction(OpBuilder &builder, llvm::Instruction *inst, ArrayRef< llvm::Value * > llvmOperands, LLVM::ModuleImport &moduleImport) const
Converts the LLVM instruction to an MLIR operation if a conversion exists.
LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, LLVM::ModuleImport &moduleImport) const
Converts the LLVM intrinsic to an MLIR operation if a conversion exists.
LogicalResult setMetadataAttrs(OpBuilder &builder, unsigned kind, llvm::MDNode *node, Operation *op, LLVM::ModuleImport &moduleImport) const
Attaches the given LLVM metadata to the imported operation if a conversion to one or more MLIR dialec...
bool isConvertibleMetadata(unsigned kind)
Returns true if the given LLVM IR metadata is convertible to an MLIR attribute.
bool isConvertibleInstruction(unsigned id)
Returns true if the given LLVM IR instruction is convertible to an MLIR operation.
bool isConvertibleIntrinsic(llvm::Intrinsic::ID id)
Returns true if the given LLVM IR intrinsic is convertible to an MLIR operation.
Module import implementation class that provides methods to import globals and functions from an LLVM...
Definition: ModuleImport.h:47
Location translateLoc(llvm::DILocation *loc)
Translates the debug location.
LogicalResult convertComdats()
Converts all comdat selectors of the LLVM module to MLIR comdat operations.
LogicalResult convertFunctions()
Converts all functions of the LLVM module to MLIR functions.
FailureOr< SmallVector< Value > > convertValues(ArrayRef< llvm::Value * > values)
Converts a range of LLVM values to a range of MLIR values using the convertValue method,...
DILocalVariableAttr matchLocalVariableAttr(llvm::Value *value)
Converts value to a local variable attribute.
LogicalResult convertLinkerOptionsMetadata()
Converts !llvm.linker.options metadata to the llvm.linker.options LLVM dialect operation.
void mapBlock(llvm::BasicBlock *llvm, Block *mlir)
Stores the mapping between an LLVM block and its MLIR counterpart.
Definition: ModuleImport.h:119
void processFunctionAttributes(llvm::Function *func, LLVMFuncOp funcOp)
Converts function attributes of LLVM Function func into LLVM dialect attributes of LLVMFuncOp funcOp.
LogicalResult convertMetadata()
Converts all LLVM metadata nodes that translate to attributes such as alias analysis or access group ...
LogicalResult convertIntrinsicArguments(ArrayRef< llvm::Value * > values, ArrayRef< unsigned > immArgPositions, ArrayRef< StringLiteral > immArgAttrNames, SmallVectorImpl< Value > &valuesOut, SmallVectorImpl< NamedAttribute > &attrsOut)
Converts the LLVM values for an intrinsic to mixed MLIR values and attributes for LLVM_IntrOpBase.
FailureOr< Value > convertValue(llvm::Value *value)
Converts an LLVM value to an MLIR value, or returns failure if the conversion fails.
LogicalResult initializeImportInterface()
Calls the LLVMImportInterface initialization that queries the registered dialect interfaces for the s...
Definition: ModuleImport.h:56
void addDebugIntrinsic(llvm::CallInst *intrinsic)
Adds a debug intrinsics to the list of intrinsics that should be converted after the function convers...
LogicalResult convertIdentMetadata()
Converts !llvm.ident metadata to the llvm.ident LLVM ModuleOp attribute.
Block * lookupBlock(llvm::BasicBlock *block) const
Returns the MLIR block mapped to the given LLVM block.
Definition: ModuleImport.h:126
FailureOr< Value > convertMetadataValue(llvm::Value *value)
Converts an LLVM metadata value to an MLIR value, or returns failure if the conversion fails.
FailureOr< SmallVector< AliasScopeAttr > > lookupAliasScopeAttrs(const llvm::MDNode *node) const
Returns the alias scope attributes that map to the alias scope nodes starting from the metadata node.
void mapNoResultOp(llvm::Instruction *llvm, Operation *mlir)
Stores a mapping between an LLVM instruction and the imported MLIR operation if the operation returns...
Definition: ModuleImport.h:92
Type convertType(llvm::Type *type)
Converts the type from LLVM to MLIR LLVM dialect.
Definition: ModuleImport.h:174
Operation * lookupOperation(llvm::Instruction *inst)
Returns the MLIR operation mapped to the given LLVM instruction.
Definition: ModuleImport.h:112
DILabelAttr matchLabelAttr(llvm::Value *value)
Converts value to a label attribute. Asserts if the matching fails.
FloatAttr matchFloatAttr(llvm::Value *value)
Converts value to a float attribute. Asserts if the matching fails.
LoopAnnotationAttr translateLoopAnnotationAttr(const llvm::MDNode *node, Location loc) const
Returns the loop annotation attribute that corresponds to the given LLVM loop metadata node.
void setFastmathFlagsAttr(llvm::Instruction *inst, Operation *op) const
Sets the fastmath flags attribute for the imported operation op given the original instruction inst.
FailureOr< SmallVector< AliasScopeAttr > > matchAliasScopeAttrs(llvm::Value *value)
Converts value to an array of alias scopes or returns failure if the conversion fails.
Value lookupValue(llvm::Value *value)
Returns the MLIR value mapped to the given LLVM value.
Definition: ModuleImport.h:87
LogicalResult processFunction(llvm::Function *func)
Imports func into the current module.
RoundingModeAttr matchRoundingModeAttr(llvm::Value *value)
Converts value to a rounding mode attribute.
void mapValue(llvm::Value *llvm, Value mlir)
Stores the mapping between an LLVM value and its MLIR counterpart.
Definition: ModuleImport.h:75
ModuleImport(ModuleOp mlirModule, std::unique_ptr< llvm::Module > llvmModule, bool emitExpensiveWarnings, bool importEmptyDICompositeTypes)
FailureOr< SmallVector< AccessGroupAttr > > lookupAccessGroupAttrs(const llvm::MDNode *node) const
Returns the access group attributes that map to the access group nodes starting from the access group...
LogicalResult convertGlobals()
Converts all global variables of the LLVM module to MLIR global variables.
void setIntegerOverflowFlags(llvm::Instruction *inst, Operation *op) const
Sets the integer overflow flags (nsw/nuw) attribute for the imported operation op given the original ...
FPExceptionBehaviorAttr matchFPExceptionBehaviorAttr(llvm::Value *value)
Converts value to a FP exception behavior attribute.
LogicalResult convertDataLayout()
Converts the data layout of the LLVM module to an MLIR data layout specification.
IntegerAttr matchIntegerAttr(llvm::Value *value)
Converts value to an integer attribute. Asserts if the matching fails.
Helper class that translates an LLVM data layout to an MLIR data layout specification.
StringRef getLastToken() const
Returns the last data layout token that has been processed before the data layout translation failed.
DataLayoutSpecInterface getDataLayout() const
Returns the MLIR data layout specification translated from the LLVM data layout.
ArrayRef< StringRef > getUnhandledTokens() const
Returns the data layout tokens that have not been handled during the data layout translation.
A helper class that converts llvm.loop metadata nodes into corresponding LoopAnnotationAttrs and llvm...
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
std::vector< StringRef > getAvailableDialects()
Return information about all available dialects in the registry in this context.
void loadAllAvailableDialects()
Load all dialects available in the registry in this context.
RAII guard to reset the insertion point of the builder when destroyed.
Definition: Builders.h:356
This class helps build Operations.
Definition: Builders.h:215
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Definition: Builders.h:439
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition: Builders.h:406
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
Definition: Builders.h:444
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes=std::nullopt, ArrayRef< Location > locs=std::nullopt)
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
Definition: Builders.cpp:461
void setInsertionPointAfterValue(Value val)
Sets the insertion point to the node after the specified value.
Definition: Builders.h:429
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:488
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
Definition: Builders.h:420
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Definition: Builders.h:450
This class provides the API for ops that are known to be terminators.
Definition: OpDefinition.h:764
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition: Operation.h:745
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:213
OpTy get() const
Allow accessing the internal op.
Definition: OwningOpRef.h:51
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
iterator end()
Definition: Region.h:56
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:35
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
Definition: Value.cpp:20
DominanceInfoNode * getNode(Block *a)
Return the dominance node from the Region containing block A.
Definition: Dominance.h:82
static llvm::ArrayRef< std::pair< llvm::Attribute::AttrKind, llvm::StringRef > > getAttrKindToNameMapping()
Returns a list of pairs that each hold a mapping from LLVM attribute kinds to their corresponding str...
FloatType getFloatType(MLIRContext *context, unsigned width)
Returns a supported MLIR floating point type of the given bit width or null if the bit width is not s...
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:874
llvm::ElementCount getVectorNumElements(Type type)
Returns the element count of any LLVM-compatible vector type.
Definition: LLVMTypes.cpp:899
Type getVectorElementType(Type type)
Returns the element type of any vector type compatible with the LLVM dialect.
Definition: LLVMTypes.cpp:890
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:344
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition: Matchers.h:485
InFlightDiagnostic emitWarning(Location loc)
Utility method to emit a warning message using this location.
OwningOpRef< ModuleOp > translateLLVMIRToModule(std::unique_ptr< llvm::Module > llvmModule, MLIRContext *context, bool emitExpensiveWarnings=true, bool dropDICompositeTypeElements=false)
Translates the LLVM module into an MLIR module living in the given context.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition: Matchers.h:369