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