| 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 "DebugImporter.h" |
| 20 | #include "LoopAnnotationImporter.h" |
| 21 | |
| 22 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 23 | #include "mlir/Dialect/LLVMIR/LLVMAggregateOps.h" |
| 24 | #include "mlir/Dialect/LLVMIR/LLVMArithOps.h" |
| 25 | #include "mlir/Dialect/LLVMIR/LLVMCallOps.h" |
| 26 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 27 | #include "mlir/Dialect/LLVMIR/LLVMConversionOps.h" |
| 28 | #include "mlir/Dialect/LLVMIR/LLVMDebugIntrinsicOps.h" |
| 29 | #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h" |
| 30 | #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h" |
| 31 | #include "mlir/Dialect/LLVMIR/LLVMModuleOps.h" |
| 32 | #include "mlir/Dialect/LLVMIR/LLVMTerminatorOps.h" |
| 33 | #include "mlir/Dialect/LLVMIR/LLVMVarArgOps.h" |
| 34 | #include "mlir/Dialect/LLVMIR/LLVMVectorOps.h" |
| 35 | #include "mlir/IR/Builders.h" |
| 36 | #include "mlir/IR/Matchers.h" |
| 37 | #include "mlir/Interfaces/DataLayoutInterfaces.h" |
| 38 | #include "mlir/Target/LLVMIR/DataLayoutImporter.h" |
| 39 | #include "mlir/Tools/mlir-translate/Translation.h" |
| 40 | |
| 41 | #include "llvm/ADT/DepthFirstIterator.h" |
| 42 | #include "llvm/ADT/PostOrderIterator.h" |
| 43 | #include "llvm/ADT/ScopeExit.h" |
| 44 | #include "llvm/ADT/SmallPtrSet.h" |
| 45 | #include "llvm/ADT/StringExtras.h" |
| 46 | #include "llvm/ADT/TypeSwitch.h" |
| 47 | #include "llvm/IR/Comdat.h" |
| 48 | #include "llvm/IR/Constants.h" |
| 49 | #include "llvm/IR/DebugProgramInstruction.h" |
| 50 | #include "llvm/IR/InlineAsm.h" |
| 51 | #include "llvm/IR/InstIterator.h" |
| 52 | #include "llvm/IR/Instructions.h" |
| 53 | #include "llvm/IR/IntrinsicInst.h" |
| 54 | #include "llvm/IR/Metadata.h" |
| 55 | #include "llvm/IR/ModuleSlotTracker.h" |
| 56 | #include "llvm/IR/Operator.h" |
| 57 | #include "llvm/Support/LogicalResult.h" |
| 58 | #include "llvm/Support/ModRef.h" |
| 59 | #include <optional> |
| 60 | |
| 61 | using namespace mlir; |
| 62 | using namespace mlir::LLVM; |
| 63 | using namespace mlir::LLVM::detail; |
| 64 | |
| 65 | #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc" |
| 66 | |
| 67 | /// Upper bound on the characters a diagnostic renders for an LLVM entity. |
| 68 | /// One rendering is unbounded by nature: a GlobalVariable prints its entire |
| 69 | /// initializer, and a node with long MDStrings or large inline constants |
| 70 | /// prints them whole. On a merged library a single unbounded diagnostic |
| 71 | /// reaches tens of megabytes, so every rendering path below truncates its |
| 72 | /// output. Diagnostics only need to identify the entity; the full form is |
| 73 | /// in the input being imported. |
| 74 | static constexpr size_t kMaxDiagLength = 256; |
| 75 | |
| 76 | static std::string truncateDiag(std::string str) { |
| 77 | if (str.size() <= kMaxDiagLength) |
| 78 | return str; |
| 79 | str.resize(n: kMaxDiagLength); |
| 80 | str += "... <truncated>" ; |
| 81 | return str; |
| 82 | } |
| 83 | |
| 84 | /// Prints a metadata node through a caller-provided tracker so module-flag |
| 85 | /// conversion shares the importer's tracker instead of building one per call. |
| 86 | static std::string renderMetadataWithTracker(const llvm::Metadata *node, |
| 87 | llvm::ModuleSlotTracker &tracker, |
| 88 | const llvm::Module *module) { |
| 89 | std::string str; |
| 90 | llvm::raw_string_ostream os(str); |
| 91 | node->print(OS&: os, MST&: tracker, M: module, /*IsForDebug=*/true); |
| 92 | return truncateDiag(str: std::move(str)); |
| 93 | } |
| 94 | |
| 95 | llvm::ModuleSlotTracker &ModuleImport::getDiagSlotTracker() { |
| 96 | // Printing through a caller-provided tracker numbers exactly like the |
| 97 | // per-call tracker Metadata::print and Value::print would construct, but |
| 98 | // the tracker's lazy module pre-pass is paid once per import instead of |
| 99 | // once per rendering. |
| 100 | if (!diagSlotTracker) |
| 101 | diagSlotTracker = |
| 102 | std::make_unique<llvm::ModuleSlotTracker>(args: llvmModule.get()); |
| 103 | return *diagSlotTracker; |
| 104 | } |
| 105 | |
| 106 | std::string ModuleImport::renderValue(const llvm::Value &value) { |
| 107 | std::string str; |
| 108 | llvm::raw_string_ostream os(str); |
| 109 | // A GlobalValue renders its full definition. For a merged vtable or string |
| 110 | // table that is megabytes, so print globals by name only. |
| 111 | if (const auto *global = llvm::dyn_cast<llvm::GlobalValue>(Val: &value)) { |
| 112 | global->printAsOperand(O&: os, /*PrintType=*/false, MST&: getDiagSlotTracker()); |
| 113 | return truncateDiag(str: std::move(str)); |
| 114 | } |
| 115 | value.print(O&: os, MST&: getDiagSlotTracker()); |
| 116 | return truncateDiag(str: std::move(str)); |
| 117 | } |
| 118 | |
| 119 | std::string ModuleImport::renderMetadata(const llvm::Metadata *node) { |
| 120 | return renderMetadataWithTracker(node, tracker&: getDiagSlotTracker(), |
| 121 | module: llvmModule.get()); |
| 122 | } |
| 123 | |
| 124 | /// Returns the name of the global_ctors global variables. |
| 125 | static constexpr StringRef getGlobalCtorsVarName() { |
| 126 | return "llvm.global_ctors" ; |
| 127 | } |
| 128 | |
| 129 | /// Prefix used for symbols of nameless llvm globals. |
| 130 | static constexpr StringRef getNamelessGlobalPrefix() { |
| 131 | return "mlir.llvm.nameless_global" ; |
| 132 | } |
| 133 | |
| 134 | /// Returns the name of the global_dtors global variables. |
| 135 | static constexpr StringRef getGlobalDtorsVarName() { |
| 136 | return "llvm.global_dtors" ; |
| 137 | } |
| 138 | |
| 139 | /// Returns the symbol name for the module-level comdat operation. It must not |
| 140 | /// conflict with the user namespace. |
| 141 | static constexpr StringRef getGlobalComdatOpName() { |
| 142 | return "__llvm_global_comdat" ; |
| 143 | } |
| 144 | |
| 145 | /// Converts the sync scope identifier of `inst` to the string representation |
| 146 | /// necessary to build an atomic LLVM dialect operation. Returns the empty |
| 147 | /// string if the operation has either no sync scope or the default system-level |
| 148 | /// sync scope attached. The atomic operations only set their sync scope |
| 149 | /// attribute if they have a non-default sync scope attached. |
| 150 | static StringRef getLLVMSyncScope(llvm::Instruction *inst) { |
| 151 | std::optional<llvm::SyncScope::ID> syncScopeID = |
| 152 | llvm::getAtomicSyncScopeID(I: inst); |
| 153 | if (!syncScopeID) |
| 154 | return "" ; |
| 155 | |
| 156 | // Search the sync scope name for the given identifier. The default |
| 157 | // system-level sync scope thereby maps to the empty string. |
| 158 | SmallVector<StringRef> syncScopeName; |
| 159 | llvm::LLVMContext &llvmContext = inst->getContext(); |
| 160 | llvmContext.getSyncScopeNames(SSNs&: syncScopeName); |
| 161 | auto *it = llvm::find_if(Range&: syncScopeName, P: [&](StringRef name) { |
| 162 | return *syncScopeID == llvmContext.getOrInsertSyncScopeID(SSN: name); |
| 163 | }); |
| 164 | if (it != syncScopeName.end()) |
| 165 | return *it; |
| 166 | llvm_unreachable("incorrect sync scope identifier" ); |
| 167 | } |
| 168 | |
| 169 | /// Converts an array of unsigned indices to a signed integer position array. |
| 170 | static SmallVector<int64_t> getPositionFromIndices(ArrayRef<unsigned> indices) { |
| 171 | SmallVector<int64_t> position; |
| 172 | llvm::append_range(C&: position, R&: indices); |
| 173 | return position; |
| 174 | } |
| 175 | |
| 176 | /// Converts the LLVM instructions that have a generated MLIR builder. Using a |
| 177 | /// static implementation method called from the module import ensures the |
| 178 | /// builders have to use the `moduleImport` argument and cannot directly call |
| 179 | /// import methods. As a result, both the intrinsic and the instruction MLIR |
| 180 | /// builders have to use the `moduleImport` argument and none of them has direct |
| 181 | /// access to the private module import methods. |
| 182 | static LogicalResult convertInstructionImpl(OpBuilder &odsBuilder, |
| 183 | llvm::Instruction *inst, |
| 184 | ModuleImport &moduleImport, |
| 185 | LLVMImportInterface &iface) { |
| 186 | // Copy the operands to an LLVM operands array reference for conversion. |
| 187 | SmallVector<llvm::Value *> operands(inst->operands()); |
| 188 | ArrayRef<llvm::Value *> llvmOperands(operands); |
| 189 | |
| 190 | // Convert all instructions that provide an MLIR builder. |
| 191 | if (iface.isConvertibleInstruction(id: inst->getOpcode())) |
| 192 | return iface.convertInstruction(builder&: odsBuilder, inst, llvmOperands, |
| 193 | moduleImport); |
| 194 | // TODO: Implement the `convertInstruction` hooks in the |
| 195 | // `LLVMDialectLLVMIRImportInterface` and move the following include there. |
| 196 | #include "mlir/Dialect/LLVMIR/LLVMOpFromLLVMIRConversions.inc" |
| 197 | |
| 198 | return failure(); |
| 199 | } |
| 200 | |
| 201 | FlatSymbolRefAttr |
| 202 | ModuleImport::getMetadataGlobalValueSymbolRef(llvm::GlobalValue *global) { |
| 203 | if (auto *globalVar = dyn_cast<llvm::GlobalVariable>(Val: global)) { |
| 204 | StringRef name = globalVar->getName(); |
| 205 | if (name.empty()) |
| 206 | return getOrCreateNamelessSymbolName(globalVar); |
| 207 | if (name == getGlobalCtorsVarName() || name == getGlobalDtorsVarName()) |
| 208 | return {}; |
| 209 | } |
| 210 | |
| 211 | if (auto *func = dyn_cast<llvm::Function>(Val: global)) { |
| 212 | // Intrinsics with a dedicated import conversion do not have an imported |
| 213 | // function declaration that a metadata symbol reference could resolve to. |
| 214 | if (func->isIntrinsic() && |
| 215 | iface.isConvertibleIntrinsic(id: func->getIntrinsicID())) |
| 216 | return {}; |
| 217 | } |
| 218 | |
| 219 | if (global->getName().empty()) |
| 220 | return {}; |
| 221 | return FlatSymbolRefAttr::get(ctx: context, value: global->getName()); |
| 222 | } |
| 223 | |
| 224 | FlatSymbolRefAttr |
| 225 | ModuleImport::getMetadataOperandSymbolRef(const llvm::Metadata *md) { |
| 226 | auto *valueAsMD = dyn_cast_or_null<llvm::ValueAsMetadata>(Val: md); |
| 227 | if (!valueAsMD) |
| 228 | return {}; |
| 229 | llvm::Value *value = valueAsMD->getValue(); |
| 230 | llvm::GlobalValue *gv = dyn_cast<llvm::GlobalValue>(Val: value); |
| 231 | if (!gv) |
| 232 | gv = dyn_cast<llvm::GlobalValue>(Val: value->stripPointerCastsAndAliases()); |
| 233 | if (!gv) |
| 234 | return {}; |
| 235 | return getMetadataGlobalValueSymbolRef(global: gv); |
| 236 | } |
| 237 | |
| 238 | /// Depth-first conversion of the metadata node `md` to the matching LLVM |
| 239 | /// dialect metadata attribute. Returns a null attribute for shapes that the |
| 240 | /// dialect's metadata-attribute hierarchy does not currently model. `path` |
| 241 | /// holds the metadata nodes on the current depth-first search path. Cyclic |
| 242 | /// metadata graphs are valid in LLVM IR, but they cannot be expressed by the |
| 243 | /// immutable, structurally-uniqued metadata attributes built here. The `path` |
| 244 | /// set lets the traversal recognize such a back-edge and bail out. `attrMap` |
| 245 | /// caches the attributes of fully converted nodes so that shared subgraphs |
| 246 | /// are visited only once. |
| 247 | Attribute ModuleImport::convertMetadataToAttrImpl( |
| 248 | const llvm::Metadata *md, SmallPtrSetImpl<const llvm::Metadata *> &path, |
| 249 | DenseMap<const llvm::Metadata *, Attribute> &attrMap) { |
| 250 | if (!md) |
| 251 | return {}; |
| 252 | if (auto *mdStr = dyn_cast<llvm::MDString>(Val: md)) |
| 253 | return MDStringAttr::get(context, |
| 254 | value: StringAttr::get(context, bytes: mdStr->getString())); |
| 255 | if (auto *cam = dyn_cast<llvm::ConstantAsMetadata>(Val: md)) { |
| 256 | llvm::Constant *constant = cam->getValue(); |
| 257 | if (auto *global = dyn_cast<llvm::GlobalValue>(Val: constant)) { |
| 258 | if (FlatSymbolRefAttr symbolRef = getMetadataGlobalValueSymbolRef(global)) |
| 259 | return MDGlobalValueAttr::get(context, name: symbolRef); |
| 260 | } |
| 261 | if (auto *ci = dyn_cast<llvm::ConstantInt>(Val: constant)) { |
| 262 | auto intType = IntegerType::get(context, width: ci->getBitWidth()); |
| 263 | return MDConstantAttr::get(context, |
| 264 | value: IntegerAttr::get(type: intType, value: ci->getValue())); |
| 265 | } |
| 266 | if (auto *nullPtr = dyn_cast<llvm::ConstantPointerNull>(Val: constant)) |
| 267 | return MDNullAttr::get(context, |
| 268 | addressSpace: nullPtr->getType()->getPointerAddressSpace()); |
| 269 | if (auto *constExpr = dyn_cast<llvm::ConstantExpr>(Val: constant)) { |
| 270 | // Only `addrspacecast` is modelled; other constant expressions have no |
| 271 | // metadata-attribute counterpart. |
| 272 | if (constExpr->getOpcode() != llvm::Instruction::AddrSpaceCast) |
| 273 | return {}; |
| 274 | Attribute argAttr = convertMetadataToAttrImpl( |
| 275 | md: llvm::ConstantAsMetadata::get(C: constExpr->getOperand(i_nocapture: 0)), path, |
| 276 | attrMap); |
| 277 | if (!argAttr) |
| 278 | return {}; |
| 279 | return MDAddrSpaceCastAttr::get( |
| 280 | context, arg: argAttr, addressSpace: constExpr->getType()->getPointerAddressSpace()); |
| 281 | } |
| 282 | return {}; |
| 283 | } |
| 284 | if (auto *node = dyn_cast<llvm::MDNode>(Val: md)) { |
| 285 | // Metadata attributes cannot preserve distinctness, so bail out. |
| 286 | if (node->isDistinct()) |
| 287 | return {}; |
| 288 | if (Attribute cached = attrMap.lookup(Val: node)) |
| 289 | return cached; |
| 290 | // If `node` is already on the current search path, this is a back-edge into |
| 291 | // a cyclic graph. While that's valid it isn't implemented yet, so bail out. |
| 292 | if (!path.insert(Ptr: node).second) |
| 293 | return {}; |
| 294 | SmallVector<Attribute> operands; |
| 295 | operands.reserve(N: node->getNumOperands()); |
| 296 | for (const llvm::MDOperand &op : node->operands()) { |
| 297 | Attribute opAttr = convertMetadataToAttrImpl(md: op.get(), path, attrMap); |
| 298 | if (!opAttr) |
| 299 | return {}; |
| 300 | operands.push_back(Elt: opAttr); |
| 301 | } |
| 302 | path.erase(Ptr: node); |
| 303 | Attribute nodeAttr = MDNodeAttr::get(context, operands); |
| 304 | attrMap.try_emplace(Key: node, Args&: nodeAttr); |
| 305 | return nodeAttr; |
| 306 | } |
| 307 | return {}; |
| 308 | } |
| 309 | |
| 310 | /// Converts the metadata node `md` to the matching LLVM dialect metadata |
| 311 | /// attribute. Returns a null attribute for shapes that the dialect's |
| 312 | /// metadata-attribute hierarchy does not currently model, including distinct |
| 313 | /// nodes and cyclic metadata graphs that the immutable metadata attributes |
| 314 | /// cannot express. |
| 315 | Attribute ModuleImport::convertMetadataToAttr(const llvm::Metadata *md) { |
| 316 | SmallPtrSet<const llvm::Metadata *, 8> path; |
| 317 | DenseMap<const llvm::Metadata *, Attribute> attrMap; |
| 318 | return convertMetadataToAttrImpl(md, path, attrMap); |
| 319 | } |
| 320 | |
| 321 | /// Get a topologically sorted list of blocks for the given basic blocks. |
| 322 | static SetVector<llvm::BasicBlock *> |
| 323 | getTopologicallySortedBlocks(ArrayRef<llvm::BasicBlock *> basicBlocks) { |
| 324 | SetVector<llvm::BasicBlock *> blocks; |
| 325 | for (llvm::BasicBlock *basicBlock : basicBlocks) { |
| 326 | if (!blocks.contains(key: basicBlock)) { |
| 327 | llvm::ReversePostOrderTraversal<llvm::BasicBlock *> traversal(basicBlock); |
| 328 | blocks.insert_range(R&: traversal); |
| 329 | } |
| 330 | } |
| 331 | assert(blocks.size() == basicBlocks.size() && "some blocks are not sorted" ); |
| 332 | return blocks; |
| 333 | } |
| 334 | |
| 335 | ModuleImport::~ModuleImport() = default; |
| 336 | |
| 337 | ModuleImport::ModuleImport(ModuleOp mlirModule, |
| 338 | std::unique_ptr<llvm::Module> llvmModule, |
| 339 | bool emitExpensiveWarnings, |
| 340 | bool importEmptyDICompositeTypes, |
| 341 | bool preferUnregisteredIntrinsics, |
| 342 | bool importStructsAsLiterals) |
| 343 | : builder(mlirModule->getContext()), context(mlirModule->getContext()), |
| 344 | mlirModule(mlirModule), llvmModule(std::move(llvmModule)), |
| 345 | iface(mlirModule->getContext()), |
| 346 | typeTranslator(*mlirModule->getContext(), importStructsAsLiterals), |
| 347 | debugImporter(std::make_unique<DebugImporter>( |
| 348 | args&: mlirModule, args&: importEmptyDICompositeTypes)), |
| 349 | loopAnnotationImporter( |
| 350 | std::make_unique<LoopAnnotationImporter>(args&: *this, args&: builder)), |
| 351 | emitExpensiveWarnings(emitExpensiveWarnings), |
| 352 | preferUnregisteredIntrinsics(preferUnregisteredIntrinsics) { |
| 353 | builder.setInsertionPointToStart(mlirModule.getBody()); |
| 354 | } |
| 355 | |
| 356 | ComdatOp ModuleImport::getGlobalComdatOp() { |
| 357 | if (globalComdatOp) |
| 358 | return globalComdatOp; |
| 359 | |
| 360 | OpBuilder::InsertionGuard guard(builder); |
| 361 | builder.setInsertionPointToEnd(mlirModule.getBody()); |
| 362 | globalComdatOp = |
| 363 | ComdatOp::create(builder, location: mlirModule.getLoc(), symName: getGlobalComdatOpName()); |
| 364 | globalInsertionOp = globalComdatOp; |
| 365 | return globalComdatOp; |
| 366 | } |
| 367 | |
| 368 | LogicalResult ModuleImport::processTBAAMetadata(const llvm::MDNode *node) { |
| 369 | Location loc = mlirModule.getLoc(); |
| 370 | |
| 371 | // If `node` is a valid TBAA root node, then return its optional identity |
| 372 | // string, otherwise return failure. |
| 373 | auto getIdentityIfRootNode = |
| 374 | [&](const llvm::MDNode *node) -> FailureOr<std::optional<StringRef>> { |
| 375 | // Root node, e.g.: |
| 376 | // !0 = !{!"Simple C/C++ TBAA"} |
| 377 | // !1 = !{} |
| 378 | if (node->getNumOperands() > 1) |
| 379 | return failure(); |
| 380 | // If the operand is MDString, then assume that this is a root node. |
| 381 | if (node->getNumOperands() == 1) |
| 382 | if (const auto *op0 = dyn_cast<const llvm::MDString>(Val: node->getOperand(I: 0))) |
| 383 | return std::optional<StringRef>{op0->getString()}; |
| 384 | return std::optional<StringRef>{}; |
| 385 | }; |
| 386 | |
| 387 | // If `node` looks like a TBAA type descriptor metadata, |
| 388 | // then return true, if it is a valid node, and false otherwise. |
| 389 | // If it does not look like a TBAA type descriptor metadata, then |
| 390 | // return std::nullopt. |
| 391 | // If `identity` and `memberTypes/Offsets` are non-null, then they will |
| 392 | // contain the converted metadata operands for a valid TBAA node (i.e. when |
| 393 | // true is returned). |
| 394 | auto isTypeDescriptorNode = [&](const llvm::MDNode *node, |
| 395 | StringRef *identity = nullptr, |
| 396 | SmallVectorImpl<TBAAMemberAttr> *members = |
| 397 | nullptr) -> std::optional<bool> { |
| 398 | unsigned numOperands = node->getNumOperands(); |
| 399 | // Type descriptor, e.g.: |
| 400 | // !1 = !{!"int", !0, /*optional*/i64 0} /* scalar int type */ |
| 401 | // !2 = !{!"agg_t", !1, i64 0} /* struct agg_t { int x; } */ |
| 402 | if (numOperands < 2) |
| 403 | return std::nullopt; |
| 404 | |
| 405 | // TODO: support "new" format (D41501) for type descriptors, |
| 406 | // where the first operand is an MDNode. |
| 407 | const auto *identityNode = |
| 408 | dyn_cast<const llvm::MDString>(Val: node->getOperand(I: 0)); |
| 409 | if (!identityNode) |
| 410 | return std::nullopt; |
| 411 | |
| 412 | // This should be a type descriptor node. |
| 413 | if (identity) |
| 414 | *identity = identityNode->getString(); |
| 415 | |
| 416 | for (unsigned pairNum = 0, e = numOperands / 2; pairNum < e; ++pairNum) { |
| 417 | const auto *memberNode = |
| 418 | dyn_cast<const llvm::MDNode>(Val: node->getOperand(I: 2 * pairNum + 1)); |
| 419 | if (!memberNode) { |
| 420 | emitError(loc) << "operand '" << 2 * pairNum + 1 |
| 421 | << "' must be MDNode: " << renderMetadata(node); |
| 422 | return false; |
| 423 | } |
| 424 | int64_t offset = 0; |
| 425 | if (2 * pairNum + 2 >= numOperands) { |
| 426 | // Allow for optional 0 offset in 2-operand nodes. |
| 427 | if (numOperands != 2) { |
| 428 | emitError(loc) << "missing member offset: " << renderMetadata(node); |
| 429 | return false; |
| 430 | } |
| 431 | } else { |
| 432 | auto *offsetCI = llvm::mdconst::dyn_extract<llvm::ConstantInt>( |
| 433 | MD: node->getOperand(I: 2 * pairNum + 2)); |
| 434 | if (!offsetCI) { |
| 435 | emitError(loc) << "operand '" << 2 * pairNum + 2 |
| 436 | << "' must be ConstantInt: " << renderMetadata(node); |
| 437 | return false; |
| 438 | } |
| 439 | offset = offsetCI->getZExtValue(); |
| 440 | } |
| 441 | |
| 442 | if (members) |
| 443 | members->push_back(Elt: TBAAMemberAttr::get( |
| 444 | typeDesc: cast<TBAANodeAttr>(Val: tbaaMapping.lookup(Val: memberNode)), offset)); |
| 445 | } |
| 446 | |
| 447 | return true; |
| 448 | }; |
| 449 | |
| 450 | // If `node` looks like a TBAA access tag metadata, |
| 451 | // then return true, if it is a valid node, and false otherwise. |
| 452 | // If it does not look like a TBAA access tag metadata, then |
| 453 | // return std::nullopt. |
| 454 | // If the other arguments are non-null, then they will contain |
| 455 | // the converted metadata operands for a valid TBAA node (i.e. when true is |
| 456 | // returned). |
| 457 | auto isTagNode = [&](const llvm::MDNode *node, |
| 458 | TBAATypeDescriptorAttr *baseAttr = nullptr, |
| 459 | TBAATypeDescriptorAttr *accessAttr = nullptr, |
| 460 | int64_t *offset = nullptr, |
| 461 | bool *isConstant = nullptr) -> std::optional<bool> { |
| 462 | // Access tag, e.g.: |
| 463 | // !3 = !{!1, !1, i64 0} /* scalar int access */ |
| 464 | // !4 = !{!2, !1, i64 0} /* agg_t::x access */ |
| 465 | // |
| 466 | // Optional 4th argument is ConstantInt 0/1 identifying whether |
| 467 | // the location being accessed is "constant" (see for details: |
| 468 | // https://llvm.org/docs/LangRef.html#representation). |
| 469 | unsigned numOperands = node->getNumOperands(); |
| 470 | if (numOperands != 3 && numOperands != 4) |
| 471 | return std::nullopt; |
| 472 | const auto *baseMD = dyn_cast<const llvm::MDNode>(Val: node->getOperand(I: 0)); |
| 473 | const auto *accessMD = dyn_cast<const llvm::MDNode>(Val: node->getOperand(I: 1)); |
| 474 | auto *offsetCI = |
| 475 | llvm::mdconst::dyn_extract<llvm::ConstantInt>(MD: node->getOperand(I: 2)); |
| 476 | if (!baseMD || !accessMD || !offsetCI) |
| 477 | return std::nullopt; |
| 478 | // TODO: support "new" TBAA format, if needed (see D41501). |
| 479 | // In the "old" format the first operand of the access type |
| 480 | // metadata is MDString. We have to distinguish the formats, |
| 481 | // because access tags have the same structure, but different |
| 482 | // meaning for the operands. |
| 483 | if (accessMD->getNumOperands() < 1 || |
| 484 | !isa<llvm::MDString>(Val: accessMD->getOperand(I: 0))) |
| 485 | return std::nullopt; |
| 486 | bool isConst = false; |
| 487 | if (numOperands == 4) { |
| 488 | auto *isConstantCI = |
| 489 | llvm::mdconst::dyn_extract<llvm::ConstantInt>(MD: node->getOperand(I: 3)); |
| 490 | if (!isConstantCI) { |
| 491 | emitError(loc) << "operand '3' must be ConstantInt: " |
| 492 | << renderMetadata(node); |
| 493 | return false; |
| 494 | } |
| 495 | isConst = isConstantCI->getValue()[0]; |
| 496 | } |
| 497 | if (baseAttr) |
| 498 | *baseAttr = cast<TBAATypeDescriptorAttr>(Val: tbaaMapping.lookup(Val: baseMD)); |
| 499 | if (accessAttr) |
| 500 | *accessAttr = cast<TBAATypeDescriptorAttr>(Val: tbaaMapping.lookup(Val: accessMD)); |
| 501 | if (offset) |
| 502 | *offset = offsetCI->getZExtValue(); |
| 503 | if (isConstant) |
| 504 | *isConstant = isConst; |
| 505 | return true; |
| 506 | }; |
| 507 | |
| 508 | // Do a post-order walk over the TBAA Graph. Since a correct TBAA Graph is a |
| 509 | // DAG, a post-order walk guarantees that we convert any metadata node we |
| 510 | // depend on, prior to converting the current node. |
| 511 | DenseSet<const llvm::MDNode *> seen; |
| 512 | SmallVector<const llvm::MDNode *> workList; |
| 513 | workList.push_back(Elt: node); |
| 514 | while (!workList.empty()) { |
| 515 | const llvm::MDNode *current = workList.back(); |
| 516 | if (tbaaMapping.contains(Val: current)) { |
| 517 | // Already converted. Just pop from the worklist. |
| 518 | workList.pop_back(); |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | // If any child of this node is not yet converted, don't pop the current |
| 523 | // node from the worklist but push the not-yet-converted children in the |
| 524 | // front of the worklist. |
| 525 | bool anyChildNotConverted = false; |
| 526 | for (const llvm::MDOperand &operand : current->operands()) |
| 527 | if (auto *childNode = dyn_cast_or_null<const llvm::MDNode>(Val: operand.get())) |
| 528 | if (!tbaaMapping.contains(Val: childNode)) { |
| 529 | workList.push_back(Elt: childNode); |
| 530 | anyChildNotConverted = true; |
| 531 | } |
| 532 | |
| 533 | if (anyChildNotConverted) { |
| 534 | // If this is the second time we failed to convert an element in the |
| 535 | // worklist it must be because a child is dependent on it being converted |
| 536 | // and we have a cycle in the graph. Cycles are not allowed in TBAA |
| 537 | // graphs. |
| 538 | if (!seen.insert(V: current).second) |
| 539 | return emitError(loc) |
| 540 | << "has cycle in TBAA graph: " << renderMetadata(node: current); |
| 541 | |
| 542 | continue; |
| 543 | } |
| 544 | |
| 545 | // Otherwise simply import the current node. |
| 546 | workList.pop_back(); |
| 547 | |
| 548 | FailureOr<std::optional<StringRef>> rootNodeIdentity = |
| 549 | getIdentityIfRootNode(current); |
| 550 | if (succeeded(Result: rootNodeIdentity)) { |
| 551 | StringAttr stringAttr = *rootNodeIdentity |
| 552 | ? builder.getStringAttr(bytes: **rootNodeIdentity) |
| 553 | : nullptr; |
| 554 | // The root nodes do not have operands, so we can create |
| 555 | // the TBAARootAttr on the first walk. |
| 556 | tbaaMapping.insert(KV: {current, builder.getAttr<TBAARootAttr>(args&: stringAttr)}); |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | StringRef identity; |
| 561 | SmallVector<TBAAMemberAttr> members; |
| 562 | if (std::optional<bool> isValid = |
| 563 | isTypeDescriptorNode(current, &identity, &members)) { |
| 564 | assert(isValid.value() && "type descriptor node must be valid" ); |
| 565 | |
| 566 | tbaaMapping.insert(KV: {current, builder.getAttr<TBAATypeDescriptorAttr>( |
| 567 | args&: identity, args&: members)}); |
| 568 | continue; |
| 569 | } |
| 570 | |
| 571 | TBAATypeDescriptorAttr baseAttr, accessAttr; |
| 572 | int64_t offset; |
| 573 | bool isConstant; |
| 574 | if (std::optional<bool> isValid = |
| 575 | isTagNode(current, &baseAttr, &accessAttr, &offset, &isConstant)) { |
| 576 | assert(isValid.value() && "access tag node must be valid" ); |
| 577 | tbaaMapping.insert( |
| 578 | KV: {current, builder.getAttr<TBAATagAttr>(args&: baseAttr, args&: accessAttr, args&: offset, |
| 579 | args&: isConstant)}); |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | return emitError(loc) << "unsupported TBAA node format: " |
| 584 | << renderMetadata(node: current); |
| 585 | } |
| 586 | return success(); |
| 587 | } |
| 588 | |
| 589 | Attribute ModuleImport::convertTBAAStructMetadata(const llvm::MDNode *node) { |
| 590 | // Operands come in conceptual groups of three; anything else is malformed. |
| 591 | if (node->getNumOperands() == 0 || node->getNumOperands() % 3 != 0) |
| 592 | return {}; |
| 593 | |
| 594 | SmallVector<Attribute> fields; |
| 595 | fields.reserve(N: node->getNumOperands() / 3); |
| 596 | for (unsigned i = 0, e = node->getNumOperands(); i != e; i += 3) { |
| 597 | auto *offset = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>( |
| 598 | MD: node->getOperand(I: i)); |
| 599 | auto *size = llvm::mdconst::dyn_extract_or_null<llvm::ConstantInt>( |
| 600 | MD: node->getOperand(I: i + 1)); |
| 601 | if (!offset || !size) |
| 602 | return {}; |
| 603 | // Offsets and sizes are uint64_t in the attribute. Read them as APInts: |
| 604 | // LLVM compares offsets zero-extended, so a narrow constant with its |
| 605 | // high bit set (e.g. i32 0x80000000) must keep its unsigned value |
| 606 | // rather than sign-extending. Values using more than 64 bits cannot |
| 607 | // be represented and are declined. |
| 608 | const APInt &offsetAP = offset->getValue(); |
| 609 | const APInt &sizeAP = size->getValue(); |
| 610 | if (offsetAP.getActiveBits() > 64 || sizeAP.getActiveBits() > 64) |
| 611 | return {}; |
| 612 | auto *tagNode = dyn_cast_if_present<llvm::MDNode>(Val: node->getOperand(I: i + 2)); |
| 613 | if (!tagNode) |
| 614 | return {}; |
| 615 | auto tag = dyn_cast_if_present<TBAATagAttr>(Val: lookupTBAAAttr(node: tagNode)); |
| 616 | if (!tag) |
| 617 | return {}; |
| 618 | fields.push_back(Elt: builder.getAttr<TBAAStructFieldAttr>( |
| 619 | args: offsetAP.getZExtValue(), args: sizeAP.getZExtValue(), args&: tag)); |
| 620 | } |
| 621 | |
| 622 | return builder.getArrayAttr(value: fields); |
| 623 | } |
| 624 | |
| 625 | LogicalResult |
| 626 | ModuleImport::processAccessGroupMetadata(const llvm::MDNode *node) { |
| 627 | Location loc = mlirModule.getLoc(); |
| 628 | if (failed(Result: loopAnnotationImporter->translateAccessGroup(node, loc))) |
| 629 | return emitError(loc) << "unsupported access group node: " |
| 630 | << renderMetadata(node); |
| 631 | return success(); |
| 632 | } |
| 633 | |
| 634 | LogicalResult |
| 635 | ModuleImport::processAliasScopeMetadata(const llvm::MDNode *node) { |
| 636 | Location loc = mlirModule.getLoc(); |
| 637 | // Helper that verifies the node has a self reference operand. |
| 638 | auto verifySelfRef = [](const llvm::MDNode *node) { |
| 639 | return node->getNumOperands() != 0 && |
| 640 | node == dyn_cast<llvm::MDNode>(Val: node->getOperand(I: 0)); |
| 641 | }; |
| 642 | auto verifySelfRefOrString = [](const llvm::MDNode *node) { |
| 643 | return node->getNumOperands() != 0 && |
| 644 | (node == dyn_cast<llvm::MDNode>(Val: node->getOperand(I: 0)) || |
| 645 | isa<llvm::MDString>(Val: node->getOperand(I: 0))); |
| 646 | }; |
| 647 | // Helper that verifies the given operand is a string or does not exist. |
| 648 | auto verifyDescription = [](const llvm::MDNode *node, unsigned idx) { |
| 649 | return idx >= node->getNumOperands() || |
| 650 | isa<llvm::MDString>(Val: node->getOperand(I: idx)); |
| 651 | }; |
| 652 | |
| 653 | auto getIdAttr = [&](const llvm::MDNode *node) -> Attribute { |
| 654 | if (verifySelfRef(node)) |
| 655 | return DistinctAttr::create(referencedAttr: builder.getUnitAttr()); |
| 656 | |
| 657 | auto *name = cast<llvm::MDString>(Val: node->getOperand(I: 0)); |
| 658 | return builder.getStringAttr(bytes: name->getString()); |
| 659 | }; |
| 660 | |
| 661 | // Helper that creates an alias scope domain attribute. |
| 662 | auto createAliasScopeDomainOp = [&](const llvm::MDNode *aliasDomain) { |
| 663 | StringAttr description = nullptr; |
| 664 | StringRef descriptionStr = |
| 665 | llvm::AliasScopeDomainNode(aliasDomain).getDescription(); |
| 666 | if (!descriptionStr.empty()) |
| 667 | description = builder.getStringAttr(bytes: descriptionStr); |
| 668 | Attribute idAttr = getIdAttr(aliasDomain); |
| 669 | return builder.getAttr<AliasScopeDomainAttr>(args&: idAttr, args&: description); |
| 670 | }; |
| 671 | |
| 672 | // Collect the alias scopes and domains to translate them. |
| 673 | for (const llvm::MDOperand &operand : node->operands()) { |
| 674 | if (const auto *scope = dyn_cast<llvm::MDNode>(Val: operand)) { |
| 675 | llvm::AliasScopeNode aliasScope(scope); |
| 676 | const llvm::MDNode *domain = aliasScope.getDomain(); |
| 677 | |
| 678 | // Verify the scope node points to valid scope metadata which includes |
| 679 | // verifying its domain. Perform the verification before looking it up in |
| 680 | // the alias scope mapping since it could have been inserted as a domain |
| 681 | // node before. |
| 682 | if (!verifySelfRefOrString(scope) || !domain || |
| 683 | !verifyDescription(scope, 2)) |
| 684 | return emitError(loc) |
| 685 | << "unsupported alias scope node: " << renderMetadata(node: scope); |
| 686 | if (!verifySelfRefOrString(domain) || !verifyDescription(domain, 2)) |
| 687 | return emitError(loc) |
| 688 | << "unsupported alias domain node: " << renderMetadata(node: domain); |
| 689 | |
| 690 | if (aliasScopeMapping.contains(Val: scope)) |
| 691 | continue; |
| 692 | |
| 693 | // Convert the domain metadata node if it has not been translated before. |
| 694 | auto it = aliasScopeMapping.find(Val: aliasScope.getDomain()); |
| 695 | if (it == aliasScopeMapping.end()) { |
| 696 | auto aliasScopeDomainOp = createAliasScopeDomainOp(domain); |
| 697 | it = aliasScopeMapping.try_emplace(Key: domain, Args&: aliasScopeDomainOp).first; |
| 698 | } |
| 699 | |
| 700 | // Convert the scope metadata node if it has not been converted before. |
| 701 | StringAttr description = nullptr; |
| 702 | if (!aliasScope.getName().empty()) |
| 703 | description = builder.getStringAttr(bytes: aliasScope.getName()); |
| 704 | Attribute idAttr = getIdAttr(scope); |
| 705 | auto aliasScopeOp = builder.getAttr<AliasScopeAttr>( |
| 706 | args&: idAttr, args: cast<AliasScopeDomainAttr>(Val&: it->second), args&: description); |
| 707 | |
| 708 | aliasScopeMapping.try_emplace(Key: aliasScope.getNode(), Args&: aliasScopeOp); |
| 709 | } |
| 710 | } |
| 711 | return success(); |
| 712 | } |
| 713 | |
| 714 | FailureOr<SmallVector<AliasScopeAttr>> |
| 715 | ModuleImport::lookupAliasScopeAttrs(const llvm::MDNode *node) const { |
| 716 | SmallVector<AliasScopeAttr> aliasScopes; |
| 717 | aliasScopes.reserve(N: node->getNumOperands()); |
| 718 | for (const llvm::MDOperand &operand : node->operands()) { |
| 719 | auto *node = cast<llvm::MDNode>(Val: operand.get()); |
| 720 | aliasScopes.push_back( |
| 721 | Elt: dyn_cast_or_null<AliasScopeAttr>(Val: aliasScopeMapping.lookup(Val: node))); |
| 722 | } |
| 723 | // Return failure if one of the alias scope lookups failed. |
| 724 | if (llvm::is_contained(Range&: aliasScopes, Element: nullptr)) |
| 725 | return failure(); |
| 726 | return aliasScopes; |
| 727 | } |
| 728 | |
| 729 | void ModuleImport::addDebugIntrinsic(llvm::CallInst *intrinsic) { |
| 730 | debugIntrinsics.insert(X: intrinsic); |
| 731 | } |
| 732 | |
| 733 | void ModuleImport::addDebugRecord(llvm::DbgVariableRecord *dbgRecord) { |
| 734 | if (!dbgRecords.contains(key: dbgRecord)) |
| 735 | dbgRecords.insert(X: dbgRecord); |
| 736 | } |
| 737 | |
| 738 | static Attribute convertCGProfileModuleFlagValue(ModuleOp mlirModule, |
| 739 | llvm::MDTuple *mdTuple) { |
| 740 | auto getLLVMFunction = |
| 741 | [&](const llvm::MDOperand &funcMDO) -> llvm::Function * { |
| 742 | auto *f = cast_or_null<llvm::ValueAsMetadata>(Val: funcMDO); |
| 743 | // nullptr is a valid value for the function pointer. |
| 744 | if (!f) |
| 745 | return nullptr; |
| 746 | auto *llvmFn = cast<llvm::Function>(Val: f->getValue()->stripPointerCasts()); |
| 747 | return llvmFn; |
| 748 | }; |
| 749 | |
| 750 | // Each tuple element becomes one ModuleFlagCGProfileEntryAttr. |
| 751 | SmallVector<Attribute> cgProfile; |
| 752 | for (unsigned i = 0; i < mdTuple->getNumOperands(); i++) { |
| 753 | const llvm::MDOperand &mdo = mdTuple->getOperand(I: i); |
| 754 | auto *cgEntry = cast<llvm::MDNode>(Val: mdo); |
| 755 | llvm::Constant *llvmConstant = |
| 756 | cast<llvm::ConstantAsMetadata>(Val: cgEntry->getOperand(I: 2))->getValue(); |
| 757 | uint64_t count = cast<llvm::ConstantInt>(Val: llvmConstant)->getZExtValue(); |
| 758 | auto *fromFn = getLLVMFunction(cgEntry->getOperand(I: 0)); |
| 759 | auto *toFn = getLLVMFunction(cgEntry->getOperand(I: 1)); |
| 760 | // FlatSymbolRefAttr::get(mlirModule->getContext(), llvmFn->getName()); |
| 761 | cgProfile.push_back(Elt: ModuleFlagCGProfileEntryAttr::get( |
| 762 | context: mlirModule->getContext(), |
| 763 | from: fromFn ? FlatSymbolRefAttr::get(ctx: mlirModule->getContext(), |
| 764 | value: fromFn->getName()) |
| 765 | : nullptr, |
| 766 | to: toFn ? FlatSymbolRefAttr::get(ctx: mlirModule->getContext(), value: toFn->getName()) |
| 767 | : nullptr, |
| 768 | count)); |
| 769 | } |
| 770 | return ArrayAttr::get(context: mlirModule->getContext(), value: cgProfile); |
| 771 | } |
| 772 | |
| 773 | /// Extract a two element `MDTuple` from a `MDOperand`. Emit a warning in case |
| 774 | /// something else is found. |
| 775 | static llvm::MDTuple *getTwoElementMDTuple(ModuleOp mlirModule, |
| 776 | const llvm::Module *llvmModule, |
| 777 | const llvm::MDOperand &md, |
| 778 | llvm::ModuleSlotTracker &tracker) { |
| 779 | auto *tupleEntry = dyn_cast_or_null<llvm::MDTuple>(Val: md); |
| 780 | if (!tupleEntry || tupleEntry->getNumOperands() != 2) |
| 781 | emitWarning(loc: mlirModule.getLoc()) |
| 782 | << "expected 2-element tuple metadata: " |
| 783 | << renderMetadataWithTracker(node: md, tracker, module: llvmModule); |
| 784 | return tupleEntry; |
| 785 | } |
| 786 | |
| 787 | /// Extract a constant metadata value from a two element tuple (<key, value>). |
| 788 | /// Return nullptr if requirements are not met. A warning is emitted if the |
| 789 | /// `matchKey` is different from the tuple's key. |
| 790 | static llvm::ConstantAsMetadata *getConstantMDFromKeyValueTuple( |
| 791 | ModuleOp mlirModule, const llvm::Module *llvmModule, |
| 792 | const llvm::MDOperand &md, StringRef matchKey, |
| 793 | llvm::ModuleSlotTracker &tracker, bool optional = false) { |
| 794 | llvm::MDTuple *tupleEntry = |
| 795 | getTwoElementMDTuple(mlirModule, llvmModule, md, tracker); |
| 796 | if (!tupleEntry) |
| 797 | return nullptr; |
| 798 | auto *keyMD = dyn_cast<llvm::MDString>(Val: tupleEntry->getOperand(I: 0)); |
| 799 | if (!keyMD || keyMD->getString() != matchKey) { |
| 800 | if (!optional) |
| 801 | emitWarning(loc: mlirModule.getLoc()) |
| 802 | << "expected '" << matchKey << "' key, but found: " |
| 803 | << renderMetadataWithTracker(node: tupleEntry->getOperand(I: 0), tracker, |
| 804 | module: llvmModule); |
| 805 | return nullptr; |
| 806 | } |
| 807 | |
| 808 | return dyn_cast<llvm::ConstantAsMetadata>(Val: tupleEntry->getOperand(I: 1)); |
| 809 | } |
| 810 | |
| 811 | /// Extract an integer value from a two element tuple (<key, value>). |
| 812 | /// Fail if requirements are not met. A warning is emitted if the |
| 813 | /// found value isn't a LLVM constant integer. |
| 814 | static FailureOr<uint64_t> |
| 815 | convertInt64FromKeyValueTuple(ModuleOp mlirModule, |
| 816 | const llvm::Module *llvmModule, |
| 817 | const llvm::MDOperand &md, StringRef matchKey, |
| 818 | llvm::ModuleSlotTracker &tracker) { |
| 819 | llvm::ConstantAsMetadata *valMD = getConstantMDFromKeyValueTuple( |
| 820 | mlirModule, llvmModule, md, matchKey, tracker); |
| 821 | if (!valMD) |
| 822 | return failure(); |
| 823 | |
| 824 | if (auto *cstInt = dyn_cast<llvm::ConstantInt>(Val: valMD->getValue())) |
| 825 | return cstInt->getZExtValue(); |
| 826 | |
| 827 | emitWarning(loc: mlirModule.getLoc()) |
| 828 | << "expected integer metadata value for key '" << matchKey |
| 829 | << "': " << renderMetadataWithTracker(node: md, tracker, module: llvmModule); |
| 830 | return failure(); |
| 831 | } |
| 832 | |
| 833 | static std::optional<ProfileSummaryFormatKind> |
| 834 | convertProfileSummaryFormat(ModuleOp mlirModule, const llvm::Module *llvmModule, |
| 835 | const llvm::MDOperand &formatMD, |
| 836 | llvm::ModuleSlotTracker &tracker) { |
| 837 | auto *tupleEntry = |
| 838 | getTwoElementMDTuple(mlirModule, llvmModule, md: formatMD, tracker); |
| 839 | if (!tupleEntry) |
| 840 | return std::nullopt; |
| 841 | |
| 842 | llvm::MDString *keyMD = dyn_cast<llvm::MDString>(Val: tupleEntry->getOperand(I: 0)); |
| 843 | if (!keyMD || keyMD->getString() != "ProfileFormat" ) { |
| 844 | emitWarning(loc: mlirModule.getLoc()) |
| 845 | << "expected 'ProfileFormat' key: " |
| 846 | << renderMetadataWithTracker(node: tupleEntry->getOperand(I: 0), tracker, |
| 847 | module: llvmModule); |
| 848 | return std::nullopt; |
| 849 | } |
| 850 | |
| 851 | llvm::Metadata *valueMD = tupleEntry->getOperand(I: 1).get(); |
| 852 | if (!valueMD) { |
| 853 | emitWarning(loc: mlirModule.getLoc()) |
| 854 | << "expected string metadata value for key 'ProfileFormat': null" ; |
| 855 | return std::nullopt; |
| 856 | } |
| 857 | |
| 858 | llvm::MDString *valMD = dyn_cast<llvm::MDString>(Val: valueMD); |
| 859 | if (!valMD) { |
| 860 | emitWarning(loc: mlirModule.getLoc()) |
| 861 | << "expected string metadata value for key 'ProfileFormat': " |
| 862 | << renderMetadataWithTracker(node: valueMD, tracker, module: llvmModule); |
| 863 | return std::nullopt; |
| 864 | } |
| 865 | std::optional<ProfileSummaryFormatKind> fmtKind = |
| 866 | symbolizeProfileSummaryFormatKind(valMD->getString()); |
| 867 | if (!fmtKind) { |
| 868 | emitWarning(loc: mlirModule.getLoc()) |
| 869 | << "expected 'SampleProfile', 'InstrProf' or 'CSInstrProf' values, " |
| 870 | "but found: " |
| 871 | << renderMetadataWithTracker(node: valMD, tracker, module: llvmModule); |
| 872 | return std::nullopt; |
| 873 | } |
| 874 | |
| 875 | return fmtKind; |
| 876 | } |
| 877 | |
| 878 | static FailureOr<SmallVector<ModuleFlagProfileSummaryDetailedAttr>> |
| 879 | convertProfileSummaryDetailed(ModuleOp mlirModule, |
| 880 | const llvm::Module *llvmModule, |
| 881 | const llvm::MDOperand &summaryMD, |
| 882 | llvm::ModuleSlotTracker &tracker) { |
| 883 | auto *tupleEntry = |
| 884 | getTwoElementMDTuple(mlirModule, llvmModule, md: summaryMD, tracker); |
| 885 | if (!tupleEntry) |
| 886 | return failure(); |
| 887 | |
| 888 | llvm::MDString *keyMD = dyn_cast<llvm::MDString>(Val: tupleEntry->getOperand(I: 0)); |
| 889 | if (!keyMD || keyMD->getString() != "DetailedSummary" ) { |
| 890 | emitWarning(loc: mlirModule.getLoc()) |
| 891 | << "expected 'DetailedSummary' key: " |
| 892 | << renderMetadataWithTracker(node: tupleEntry->getOperand(I: 0), tracker, |
| 893 | module: llvmModule); |
| 894 | return failure(); |
| 895 | } |
| 896 | |
| 897 | llvm::MDTuple *entriesMD = dyn_cast<llvm::MDTuple>(Val: tupleEntry->getOperand(I: 1)); |
| 898 | if (!entriesMD) { |
| 899 | emitWarning(loc: mlirModule.getLoc()) |
| 900 | << "expected tuple value for 'DetailedSummary' key: " |
| 901 | << renderMetadataWithTracker(node: tupleEntry->getOperand(I: 1), tracker, |
| 902 | module: llvmModule); |
| 903 | return failure(); |
| 904 | } |
| 905 | |
| 906 | SmallVector<ModuleFlagProfileSummaryDetailedAttr> detailedSummary; |
| 907 | for (auto &&entry : entriesMD->operands()) { |
| 908 | llvm::MDTuple *entryMD = dyn_cast<llvm::MDTuple>(Val: entry); |
| 909 | if (!entryMD || entryMD->getNumOperands() != 3) { |
| 910 | emitWarning(loc: mlirModule.getLoc()) |
| 911 | << "'DetailedSummary' entry expects 3 operands: " |
| 912 | << renderMetadataWithTracker(node: entry, tracker, module: llvmModule); |
| 913 | return failure(); |
| 914 | } |
| 915 | |
| 916 | auto *op0 = dyn_cast<llvm::ConstantAsMetadata>(Val: entryMD->getOperand(I: 0)); |
| 917 | auto *op1 = dyn_cast<llvm::ConstantAsMetadata>(Val: entryMD->getOperand(I: 1)); |
| 918 | auto *op2 = dyn_cast<llvm::ConstantAsMetadata>(Val: entryMD->getOperand(I: 2)); |
| 919 | if (!op0 || !op1 || !op2) { |
| 920 | emitWarning(loc: mlirModule.getLoc()) |
| 921 | << "expected only integer entries in 'DetailedSummary': " |
| 922 | << renderMetadataWithTracker(node: entry, tracker, module: llvmModule); |
| 923 | return failure(); |
| 924 | } |
| 925 | |
| 926 | auto detaildSummaryEntry = ModuleFlagProfileSummaryDetailedAttr::get( |
| 927 | context: mlirModule->getContext(), |
| 928 | cut_off: cast<llvm::ConstantInt>(Val: op0->getValue())->getZExtValue(), |
| 929 | min_count: cast<llvm::ConstantInt>(Val: op1->getValue())->getZExtValue(), |
| 930 | num_counts: cast<llvm::ConstantInt>(Val: op2->getValue())->getZExtValue()); |
| 931 | detailedSummary.push_back(Elt: detaildSummaryEntry); |
| 932 | } |
| 933 | return detailedSummary; |
| 934 | } |
| 935 | |
| 936 | static Attribute convertProfileSummaryModuleFlagValue( |
| 937 | ModuleOp mlirModule, const llvm::Module *llvmModule, llvm::MDTuple *mdTuple, |
| 938 | llvm::ModuleSlotTracker &tracker) { |
| 939 | unsigned profileNumEntries = mdTuple->getNumOperands(); |
| 940 | if (profileNumEntries < 8) { |
| 941 | emitWarning(loc: mlirModule.getLoc()) |
| 942 | << "expected at 8 entries in 'ProfileSummary': " |
| 943 | << renderMetadataWithTracker(node: mdTuple, tracker, module: llvmModule); |
| 944 | return nullptr; |
| 945 | } |
| 946 | |
| 947 | unsigned summayIdx = 0; |
| 948 | auto checkOptionalPosition = [&](const llvm::MDOperand &md, |
| 949 | StringRef matchKey) -> LogicalResult { |
| 950 | // Make sure we won't step over the bound of the array of summary entries. |
| 951 | // Since (non-optional) DetailedSummary always comes last, the next entry in |
| 952 | // the tuple operand array must exist. |
| 953 | if (summayIdx + 1 >= profileNumEntries) { |
| 954 | emitWarning(loc: mlirModule.getLoc()) |
| 955 | << "the last summary entry is '" << matchKey |
| 956 | << "', expected 'DetailedSummary': " |
| 957 | << renderMetadataWithTracker(node: md, tracker, module: llvmModule); |
| 958 | return failure(); |
| 959 | } |
| 960 | |
| 961 | return success(); |
| 962 | }; |
| 963 | |
| 964 | auto getOptIntValue = |
| 965 | [&](const llvm::MDOperand &md, |
| 966 | StringRef matchKey) -> FailureOr<std::optional<uint64_t>> { |
| 967 | if (!getConstantMDFromKeyValueTuple(mlirModule, llvmModule, md, matchKey, |
| 968 | tracker, |
| 969 | /*optional=*/true)) |
| 970 | return FailureOr<std::optional<uint64_t>>(std::nullopt); |
| 971 | if (checkOptionalPosition(md, matchKey).failed()) |
| 972 | return failure(); |
| 973 | FailureOr<uint64_t> val = convertInt64FromKeyValueTuple( |
| 974 | mlirModule, llvmModule, md, matchKey, tracker); |
| 975 | if (failed(Result: val)) |
| 976 | return failure(); |
| 977 | return val; |
| 978 | }; |
| 979 | |
| 980 | auto getOptDoubleValue = [&](const llvm::MDOperand &md, |
| 981 | StringRef matchKey) -> FailureOr<FloatAttr> { |
| 982 | auto *valMD = getConstantMDFromKeyValueTuple(mlirModule, llvmModule, md, |
| 983 | matchKey, tracker, |
| 984 | /*optional=*/true); |
| 985 | if (!valMD) |
| 986 | return FloatAttr{}; |
| 987 | if (auto *cstFP = dyn_cast<llvm::ConstantFP>(Val: valMD->getValue())) { |
| 988 | if (checkOptionalPosition(md, matchKey).failed()) |
| 989 | return failure(); |
| 990 | return FloatAttr::get(type: Float64Type::get(context: mlirModule.getContext()), |
| 991 | value: cstFP->getValueAPF()); |
| 992 | } |
| 993 | emitWarning(loc: mlirModule.getLoc()) |
| 994 | << "expected double metadata value for key '" << matchKey |
| 995 | << "': " << renderMetadataWithTracker(node: md, tracker, module: llvmModule); |
| 996 | return failure(); |
| 997 | }; |
| 998 | |
| 999 | // Build ModuleFlagProfileSummaryAttr by sequentially fetching elements in |
| 1000 | // a fixed order: format, total count, etc. |
| 1001 | std::optional<ProfileSummaryFormatKind> format = convertProfileSummaryFormat( |
| 1002 | mlirModule, llvmModule, formatMD: mdTuple->getOperand(I: summayIdx++), tracker); |
| 1003 | if (!format.has_value()) |
| 1004 | return nullptr; |
| 1005 | |
| 1006 | FailureOr<uint64_t> totalCount = convertInt64FromKeyValueTuple( |
| 1007 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), matchKey: "TotalCount" , |
| 1008 | tracker); |
| 1009 | if (failed(Result: totalCount)) |
| 1010 | return nullptr; |
| 1011 | |
| 1012 | FailureOr<uint64_t> maxCount = convertInt64FromKeyValueTuple( |
| 1013 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), matchKey: "MaxCount" , |
| 1014 | tracker); |
| 1015 | if (failed(Result: maxCount)) |
| 1016 | return nullptr; |
| 1017 | |
| 1018 | FailureOr<uint64_t> maxInternalCount = convertInt64FromKeyValueTuple( |
| 1019 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), |
| 1020 | matchKey: "MaxInternalCount" , tracker); |
| 1021 | if (failed(Result: maxInternalCount)) |
| 1022 | return nullptr; |
| 1023 | |
| 1024 | FailureOr<uint64_t> maxFunctionCount = convertInt64FromKeyValueTuple( |
| 1025 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), |
| 1026 | matchKey: "MaxFunctionCount" , tracker); |
| 1027 | if (failed(Result: maxFunctionCount)) |
| 1028 | return nullptr; |
| 1029 | |
| 1030 | FailureOr<uint64_t> numCounts = convertInt64FromKeyValueTuple( |
| 1031 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), matchKey: "NumCounts" , |
| 1032 | tracker); |
| 1033 | if (failed(Result: numCounts)) |
| 1034 | return nullptr; |
| 1035 | |
| 1036 | FailureOr<uint64_t> numFunctions = convertInt64FromKeyValueTuple( |
| 1037 | mlirModule, llvmModule, md: mdTuple->getOperand(I: summayIdx++), matchKey: "NumFunctions" , |
| 1038 | tracker); |
| 1039 | if (failed(Result: numFunctions)) |
| 1040 | return nullptr; |
| 1041 | |
| 1042 | // Handle optional keys. |
| 1043 | FailureOr<std::optional<uint64_t>> isPartialProfile = |
| 1044 | getOptIntValue(mdTuple->getOperand(I: summayIdx), "IsPartialProfile" ); |
| 1045 | if (failed(Result: isPartialProfile)) |
| 1046 | return nullptr; |
| 1047 | if (isPartialProfile->has_value()) |
| 1048 | summayIdx++; |
| 1049 | |
| 1050 | FailureOr<FloatAttr> partialProfileRatio = |
| 1051 | getOptDoubleValue(mdTuple->getOperand(I: summayIdx), "PartialProfileRatio" ); |
| 1052 | if (failed(Result: partialProfileRatio)) |
| 1053 | return nullptr; |
| 1054 | if (*partialProfileRatio) |
| 1055 | summayIdx++; |
| 1056 | |
| 1057 | // Handle detailed summary. |
| 1058 | FailureOr<SmallVector<ModuleFlagProfileSummaryDetailedAttr>> detailed = |
| 1059 | convertProfileSummaryDetailed(mlirModule, llvmModule, |
| 1060 | summaryMD: mdTuple->getOperand(I: summayIdx), tracker); |
| 1061 | if (failed(Result: detailed)) |
| 1062 | return nullptr; |
| 1063 | |
| 1064 | // Build the final profile summary attribute. |
| 1065 | return ModuleFlagProfileSummaryAttr::get( |
| 1066 | context: mlirModule->getContext(), format: *format, total_count: *totalCount, max_count: *maxCount, |
| 1067 | max_internal_count: *maxInternalCount, max_function_count: *maxFunctionCount, num_counts: *numCounts, num_functions: *numFunctions, |
| 1068 | is_partial_profile: *isPartialProfile, partial_profile_ratio: *partialProfileRatio, detailed_summary: *detailed); |
| 1069 | } |
| 1070 | |
| 1071 | /// Invoke specific handlers for each known module flag value, returns nullptr |
| 1072 | /// if the key is unknown or unimplemented. |
| 1073 | static Attribute convertModuleFlagValueFromMDTuple( |
| 1074 | ModuleOp mlirModule, const llvm::Module *llvmModule, StringRef key, |
| 1075 | llvm::MDTuple *mdTuple, llvm::ModuleSlotTracker &tracker) { |
| 1076 | if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) |
| 1077 | return convertCGProfileModuleFlagValue(mlirModule, mdTuple); |
| 1078 | if (key == LLVMDialect::getModuleFlagKeyProfileSummaryName()) |
| 1079 | return convertProfileSummaryModuleFlagValue(mlirModule, llvmModule, mdTuple, |
| 1080 | tracker); |
| 1081 | // Handle MDTuples whose operands are all MDStrings (e.g. "riscv-isa"). |
| 1082 | // Convert them to ArrayAttr of StringAttrs for a lossless round-trip. |
| 1083 | Builder builder(mlirModule->getContext()); |
| 1084 | SmallVector<Attribute> strings; |
| 1085 | strings.reserve(N: mdTuple->getNumOperands()); |
| 1086 | for (const llvm::MDOperand &operand : mdTuple->operands()) { |
| 1087 | auto *mdString = dyn_cast_if_present<llvm::MDString>(Val: operand.get()); |
| 1088 | if (!mdString) |
| 1089 | return nullptr; |
| 1090 | strings.push_back(Elt: builder.getStringAttr(bytes: mdString->getString())); |
| 1091 | } |
| 1092 | return builder.getArrayAttr(value: strings); |
| 1093 | } |
| 1094 | |
| 1095 | LogicalResult ModuleImport::convertModuleFlagsMetadata() { |
| 1096 | SmallVector<llvm::Module::ModuleFlagEntry> llvmModuleFlags; |
| 1097 | llvmModule->getModuleFlagsMetadata(Flags&: llvmModuleFlags); |
| 1098 | |
| 1099 | SmallVector<Attribute> moduleFlags; |
| 1100 | for (const auto [behavior, key, val] : llvmModuleFlags) { |
| 1101 | Attribute valAttr = nullptr; |
| 1102 | if (auto *constInt = llvm::mdconst::dyn_extract<llvm::ConstantInt>(MD: val)) { |
| 1103 | valAttr = builder.getI32IntegerAttr(value: constInt->getZExtValue()); |
| 1104 | } else if (auto *mdString = dyn_cast<llvm::MDString>(Val: val)) { |
| 1105 | valAttr = builder.getStringAttr(bytes: mdString->getString()); |
| 1106 | } else if (auto *mdTuple = dyn_cast<llvm::MDTuple>(Val: val)) { |
| 1107 | valAttr = convertModuleFlagValueFromMDTuple(mlirModule, llvmModule: llvmModule.get(), |
| 1108 | key: key->getString(), mdTuple, |
| 1109 | tracker&: getDiagSlotTracker()); |
| 1110 | } |
| 1111 | |
| 1112 | if (!valAttr) { |
| 1113 | emitWarning(loc: mlirModule.getLoc()) |
| 1114 | << "unsupported module flag value for key '" << key->getString() |
| 1115 | << "' : " << renderMetadata(node: val); |
| 1116 | continue; |
| 1117 | } |
| 1118 | |
| 1119 | moduleFlags.push_back(Elt: builder.getAttr<ModuleFlagAttr>( |
| 1120 | args: convertModFlagBehaviorFromLLVM(value: behavior), |
| 1121 | args: builder.getStringAttr(bytes: key->getString()), args&: valAttr)); |
| 1122 | } |
| 1123 | |
| 1124 | if (!moduleFlags.empty()) |
| 1125 | LLVM::ModuleFlagsOp::create(builder, location: mlirModule.getLoc(), |
| 1126 | flags: builder.getArrayAttr(value: moduleFlags)); |
| 1127 | |
| 1128 | return success(); |
| 1129 | } |
| 1130 | |
| 1131 | LogicalResult ModuleImport::convertLinkerOptionsMetadata() { |
| 1132 | for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) { |
| 1133 | if (named.getName() != "llvm.linker.options" ) |
| 1134 | continue; |
| 1135 | // llvm.linker.options operands are lists of strings. |
| 1136 | for (const llvm::MDNode *node : named.operands()) { |
| 1137 | SmallVector<StringRef> options; |
| 1138 | options.reserve(N: node->getNumOperands()); |
| 1139 | for (const llvm::MDOperand &option : node->operands()) |
| 1140 | options.push_back(Elt: cast<llvm::MDString>(Val: option)->getString()); |
| 1141 | LLVM::LinkerOptionsOp::create(builder, location: mlirModule.getLoc(), |
| 1142 | options: builder.getStrArrayAttr(values: options)); |
| 1143 | } |
| 1144 | } |
| 1145 | return success(); |
| 1146 | } |
| 1147 | |
| 1148 | LogicalResult ModuleImport::convertDependentLibrariesMetadata() { |
| 1149 | for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) { |
| 1150 | if (named.getName() != "llvm.dependent-libraries" ) |
| 1151 | continue; |
| 1152 | SmallVector<StringRef> libraries; |
| 1153 | for (const llvm::MDNode *node : named.operands()) { |
| 1154 | if (node->getNumOperands() == 1) |
| 1155 | if (auto *mdString = dyn_cast<llvm::MDString>(Val: node->getOperand(I: 0))) |
| 1156 | libraries.push_back(Elt: mdString->getString()); |
| 1157 | } |
| 1158 | if (!libraries.empty()) |
| 1159 | mlirModule->setDiscardableAttr( |
| 1160 | name: LLVM::LLVMDialect::getDependentLibrariesAttrName(), |
| 1161 | value: builder.getStrArrayAttr(values: libraries)); |
| 1162 | } |
| 1163 | return success(); |
| 1164 | } |
| 1165 | |
| 1166 | LogicalResult ModuleImport::convertIdentMetadata() { |
| 1167 | for (const llvm::NamedMDNode &named : llvmModule->named_metadata()) { |
| 1168 | // llvm.ident should have a single operand. That operand is itself an |
| 1169 | // MDNode with a single string operand. |
| 1170 | if (named.getName() != LLVMDialect::getIdentAttrName()) |
| 1171 | continue; |
| 1172 | |
| 1173 | if (named.getNumOperands() == 1) |
| 1174 | if (auto *md = dyn_cast<llvm::MDNode>(Val: named.getOperand(i: 0))) |
| 1175 | if (md->getNumOperands() == 1) |
| 1176 | if (auto *mdStr = dyn_cast<llvm::MDString>(Val: md->getOperand(I: 0))) |
| 1177 | mlirModule->setDiscardableAttr( |
| 1178 | name: LLVMDialect::getIdentAttrName(), |
| 1179 | value: builder.getStringAttr(bytes: mdStr->getString())); |
| 1180 | } |
| 1181 | return success(); |
| 1182 | } |
| 1183 | |
| 1184 | LogicalResult ModuleImport::convertCommandlineMetadata() { |
| 1185 | for (const llvm::NamedMDNode &nmd : llvmModule->named_metadata()) { |
| 1186 | // llvm.commandline should have a single operand. That operand is itself an |
| 1187 | // MDNode with a single string operand. |
| 1188 | if (nmd.getName() != LLVMDialect::getCommandlineAttrName()) |
| 1189 | continue; |
| 1190 | |
| 1191 | if (nmd.getNumOperands() == 1) |
| 1192 | if (auto *md = dyn_cast<llvm::MDNode>(Val: nmd.getOperand(i: 0))) |
| 1193 | if (md->getNumOperands() == 1) |
| 1194 | if (auto *mdStr = dyn_cast<llvm::MDString>(Val: md->getOperand(I: 0))) |
| 1195 | mlirModule->setDiscardableAttr( |
| 1196 | name: LLVMDialect::getCommandlineAttrName(), |
| 1197 | value: builder.getStringAttr(bytes: mdStr->getString())); |
| 1198 | } |
| 1199 | return success(); |
| 1200 | } |
| 1201 | |
| 1202 | LogicalResult ModuleImport::convertMetadata() { |
| 1203 | OpBuilder::InsertionGuard guard(builder); |
| 1204 | builder.setInsertionPointToEnd(mlirModule.getBody()); |
| 1205 | for (const llvm::Function &func : llvmModule->functions()) { |
| 1206 | for (const llvm::Instruction &inst : llvm::instructions(F: func)) { |
| 1207 | // Convert access group metadata nodes. |
| 1208 | if (llvm::MDNode *node = |
| 1209 | inst.getMetadata(KindID: llvm::LLVMContext::MD_access_group)) |
| 1210 | if (failed(Result: processAccessGroupMetadata(node))) |
| 1211 | return failure(); |
| 1212 | |
| 1213 | // Convert alias analysis metadata nodes. |
| 1214 | llvm::AAMDNodes aliasAnalysisNodes = inst.getAAMetadata(); |
| 1215 | if (!aliasAnalysisNodes) |
| 1216 | continue; |
| 1217 | if (aliasAnalysisNodes.TBAA) |
| 1218 | if (failed(Result: processTBAAMetadata(node: aliasAnalysisNodes.TBAA))) |
| 1219 | return failure(); |
| 1220 | // A `!tbaa.struct` node reaches its own tags, and those tags may appear |
| 1221 | // nowhere else in the module. Without walking them here the struct |
| 1222 | // conversion below finds nothing and the node is declined. |
| 1223 | if (aliasAnalysisNodes.TBAAStruct) { |
| 1224 | const llvm::MDNode *structNode = aliasAnalysisNodes.TBAAStruct; |
| 1225 | // Operands come in groups of three: check the count before |
| 1226 | // walking the tags. The verifier rejects malformed nodes |
| 1227 | // before CLI import, so this guards direct API use. |
| 1228 | bool wellFormed = structNode->getNumOperands() % 3 == 0; |
| 1229 | if (wellFormed) { |
| 1230 | for (unsigned i = 2, e = structNode->getNumOperands(); i < e; i += 3) |
| 1231 | if (auto *tag = dyn_cast_if_present<llvm::MDNode>( |
| 1232 | Val: structNode->getOperand(I: i))) |
| 1233 | if (failed(Result: processTBAAMetadata(node: tag))) |
| 1234 | return failure(); |
| 1235 | } |
| 1236 | // Convert the struct node itself once its tags are in the map. A |
| 1237 | // node that cannot be modeled is recorded with a null value so it |
| 1238 | // is not converted again for every instruction carrying it, and is |
| 1239 | // declined at attach time. |
| 1240 | if (!tbaaStructMapping.contains(Val: structNode)) |
| 1241 | tbaaStructMapping.insert( |
| 1242 | KV: {structNode, convertTBAAStructMetadata(node: structNode)}); |
| 1243 | } |
| 1244 | if (aliasAnalysisNodes.Scope) |
| 1245 | if (failed(Result: processAliasScopeMetadata(node: aliasAnalysisNodes.Scope))) |
| 1246 | return failure(); |
| 1247 | if (aliasAnalysisNodes.NoAlias) |
| 1248 | if (failed(Result: processAliasScopeMetadata(node: aliasAnalysisNodes.NoAlias))) |
| 1249 | return failure(); |
| 1250 | } |
| 1251 | } |
| 1252 | if (failed(Result: convertLinkerOptionsMetadata())) |
| 1253 | return failure(); |
| 1254 | if (failed(Result: convertDependentLibrariesMetadata())) |
| 1255 | return failure(); |
| 1256 | if (failed(Result: convertModuleFlagsMetadata())) |
| 1257 | return failure(); |
| 1258 | if (failed(Result: convertIdentMetadata())) |
| 1259 | return failure(); |
| 1260 | if (failed(Result: convertCommandlineMetadata())) |
| 1261 | return failure(); |
| 1262 | return success(); |
| 1263 | } |
| 1264 | |
| 1265 | void ModuleImport::processComdat(const llvm::Comdat *comdat) { |
| 1266 | if (comdatMapping.contains(Val: comdat)) |
| 1267 | return; |
| 1268 | |
| 1269 | ComdatOp comdatOp = getGlobalComdatOp(); |
| 1270 | OpBuilder::InsertionGuard guard(builder); |
| 1271 | builder.setInsertionPointToEnd(&comdatOp.getBody().back()); |
| 1272 | auto selectorOp = ComdatSelectorOp::create( |
| 1273 | builder, location: mlirModule.getLoc(), sym_name: comdat->getName(), |
| 1274 | comdat: convertComdatFromLLVM(value: comdat->getSelectionKind()), |
| 1275 | /*sym_visibility=*/nullptr); |
| 1276 | auto symbolRef = |
| 1277 | SymbolRefAttr::get(ctx: builder.getContext(), value: getGlobalComdatOpName(), |
| 1278 | nestedRefs: FlatSymbolRefAttr::get(value: selectorOp.getSymNameAttr())); |
| 1279 | comdatMapping.try_emplace(Key: comdat, Args&: symbolRef); |
| 1280 | } |
| 1281 | |
| 1282 | LogicalResult ModuleImport::convertComdats() { |
| 1283 | for (llvm::GlobalVariable &globalVar : llvmModule->globals()) |
| 1284 | if (globalVar.hasComdat()) |
| 1285 | processComdat(comdat: globalVar.getComdat()); |
| 1286 | for (llvm::Function &func : llvmModule->functions()) |
| 1287 | if (func.hasComdat()) |
| 1288 | processComdat(comdat: func.getComdat()); |
| 1289 | return success(); |
| 1290 | } |
| 1291 | |
| 1292 | LogicalResult ModuleImport::convertGlobals() { |
| 1293 | for (llvm::GlobalVariable &globalVar : llvmModule->globals()) { |
| 1294 | if (globalVar.getName() == getGlobalCtorsVarName() || |
| 1295 | globalVar.getName() == getGlobalDtorsVarName()) { |
| 1296 | if (failed(Result: convertGlobalCtorsAndDtors(globalVar: &globalVar))) { |
| 1297 | return emitError(loc: UnknownLoc::get(context)) |
| 1298 | << "unhandled global variable: " << renderValue(value: globalVar); |
| 1299 | } |
| 1300 | continue; |
| 1301 | } |
| 1302 | if (failed(Result: convertGlobal(globalVar: &globalVar))) { |
| 1303 | return emitError(loc: UnknownLoc::get(context)) |
| 1304 | << "unhandled global variable: " << renderValue(value: globalVar); |
| 1305 | } |
| 1306 | } |
| 1307 | return success(); |
| 1308 | } |
| 1309 | |
| 1310 | LogicalResult ModuleImport::convertAliases() { |
| 1311 | for (llvm::GlobalAlias &alias : llvmModule->aliases()) { |
| 1312 | if (failed(Result: convertAlias(alias: &alias))) { |
| 1313 | return emitError(loc: UnknownLoc::get(context)) |
| 1314 | << "unhandled global alias: " << renderValue(value: alias); |
| 1315 | } |
| 1316 | } |
| 1317 | return success(); |
| 1318 | } |
| 1319 | |
| 1320 | LogicalResult ModuleImport::convertIFuncs() { |
| 1321 | for (llvm::GlobalIFunc &ifunc : llvmModule->ifuncs()) { |
| 1322 | if (failed(Result: convertIFunc(ifunc: &ifunc))) { |
| 1323 | return emitError(loc: UnknownLoc::get(context)) |
| 1324 | << "unhandled global ifunc: " << renderValue(value: ifunc); |
| 1325 | } |
| 1326 | } |
| 1327 | return success(); |
| 1328 | } |
| 1329 | |
| 1330 | LogicalResult ModuleImport::convertDataLayout() { |
| 1331 | Location loc = mlirModule.getLoc(); |
| 1332 | DataLayoutImporter dataLayoutImporter( |
| 1333 | context, llvmModule->getDataLayout().getStringRepresentation()); |
| 1334 | if (!dataLayoutImporter.getDataLayoutSpec()) |
| 1335 | return emitError(loc, message: "cannot translate data layout: " ) |
| 1336 | << dataLayoutImporter.getLastToken(); |
| 1337 | |
| 1338 | for (StringRef token : dataLayoutImporter.getUnhandledTokens()) |
| 1339 | emitWarning(loc, message: "unhandled data layout token: " ) << token; |
| 1340 | |
| 1341 | mlirModule->setDiscardableAttr(name: DLTIDialect::kDataLayoutAttrName, |
| 1342 | value: dataLayoutImporter.getDataLayoutSpec()); |
| 1343 | return success(); |
| 1344 | } |
| 1345 | |
| 1346 | void ModuleImport::convertTargetTriple() { |
| 1347 | mlirModule->setDiscardableAttr( |
| 1348 | name: LLVM::LLVMDialect::getTargetTripleAttrName(), |
| 1349 | value: builder.getStringAttr(bytes: llvmModule->getTargetTriple().str())); |
| 1350 | } |
| 1351 | |
| 1352 | void ModuleImport::convertModuleLevelAsm() { |
| 1353 | llvm::SmallVector<mlir::Attribute> asmArrayAttr; |
| 1354 | |
| 1355 | for (const llvm::Module::GlobalAsmFragment &Frag : |
| 1356 | llvmModule->getModuleInlineAsm()) { |
| 1357 | // TODO: Preserve module asm properties. |
| 1358 | for (llvm::StringRef line : llvm::split(Str: Frag.Asm, Separator: '\n')) |
| 1359 | if (!line.empty()) |
| 1360 | asmArrayAttr.push_back(Elt: builder.getStringAttr(bytes: line)); |
| 1361 | } |
| 1362 | |
| 1363 | mlirModule->setDiscardableAttr(name: LLVM::LLVMDialect::getModuleLevelAsmAttrName(), |
| 1364 | value: builder.getArrayAttr(value: asmArrayAttr)); |
| 1365 | } |
| 1366 | |
| 1367 | LogicalResult ModuleImport::convertFunctions() { |
| 1368 | for (llvm::Function &func : llvmModule->functions()) |
| 1369 | if (failed(Result: processFunction(func: &func))) |
| 1370 | return failure(); |
| 1371 | return success(); |
| 1372 | } |
| 1373 | |
| 1374 | void ModuleImport::setNonDebugMetadataAttrs(llvm::Instruction *inst, |
| 1375 | Operation *op) { |
| 1376 | SmallVector<std::pair<unsigned, llvm::MDNode *>> allMetadata; |
| 1377 | inst->getAllMetadataOtherThanDebugLoc(MDs&: allMetadata); |
| 1378 | for (auto &[kind, node] : allMetadata) { |
| 1379 | if (!iface.isConvertibleMetadata(kind)) |
| 1380 | continue; |
| 1381 | if (failed(Result: iface.setMetadataAttrs(builder, kind, node, op, moduleImport&: *this))) { |
| 1382 | if (emitExpensiveWarnings) { |
| 1383 | Location loc = debugImporter->translateLoc(loc: inst->getDebugLoc()); |
| 1384 | emitWarning(loc) << "unhandled metadata: " << renderMetadata(node) |
| 1385 | << " on " << renderValue(value: *inst); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | void ModuleImport::setIntegerOverflowFlags(llvm::Instruction *inst, |
| 1392 | Operation *op) const { |
| 1393 | auto iface = cast<IntegerOverflowFlagsInterface>(Val: op); |
| 1394 | |
| 1395 | IntegerOverflowFlags value = {}; |
| 1396 | value = bitEnumSet(bits: value, bit: IntegerOverflowFlags::nsw, value: inst->hasNoSignedWrap()); |
| 1397 | value = |
| 1398 | bitEnumSet(bits: value, bit: IntegerOverflowFlags::nuw, value: inst->hasNoUnsignedWrap()); |
| 1399 | |
| 1400 | iface.setOverflowFlags(value); |
| 1401 | } |
| 1402 | |
| 1403 | void ModuleImport::setExactFlag(llvm::Instruction *inst, Operation *op) const { |
| 1404 | auto iface = cast<ExactFlagInterface>(Val: op); |
| 1405 | |
| 1406 | iface.setIsExact(inst->isExact()); |
| 1407 | } |
| 1408 | |
| 1409 | void ModuleImport::setDisjointFlag(llvm::Instruction *inst, |
| 1410 | Operation *op) const { |
| 1411 | auto iface = cast<DisjointFlagInterface>(Val: op); |
| 1412 | auto *instDisjoint = cast<llvm::PossiblyDisjointInst>(Val: inst); |
| 1413 | |
| 1414 | iface.setIsDisjoint(instDisjoint->isDisjoint()); |
| 1415 | } |
| 1416 | |
| 1417 | void ModuleImport::setNonNegFlag(llvm::Instruction *inst, Operation *op) const { |
| 1418 | auto iface = cast<NonNegFlagInterface>(Val: op); |
| 1419 | |
| 1420 | iface.setNonNeg(inst->hasNonNeg()); |
| 1421 | } |
| 1422 | |
| 1423 | void ModuleImport::setFastmathFlagsAttr(llvm::Instruction *inst, |
| 1424 | Operation *op) const { |
| 1425 | auto iface = cast<FastmathFlagsInterface>(Val: op); |
| 1426 | |
| 1427 | // Even if the imported operation implements the fastmath interface, the |
| 1428 | // original instruction may not have fastmath flags set. Exit if an |
| 1429 | // instruction, such as a non floating-point function call, does not have |
| 1430 | // fastmath flags. |
| 1431 | if (!isa<llvm::FPMathOperator>(Val: inst)) |
| 1432 | return; |
| 1433 | llvm::FastMathFlags flags = inst->getFastMathFlags(); |
| 1434 | |
| 1435 | // Set the fastmath bits flag-by-flag. |
| 1436 | FastmathFlags value = {}; |
| 1437 | value = bitEnumSet(bits: value, bit: FastmathFlags::nnan, value: flags.noNaNs()); |
| 1438 | value = bitEnumSet(bits: value, bit: FastmathFlags::ninf, value: flags.noInfs()); |
| 1439 | value = bitEnumSet(bits: value, bit: FastmathFlags::nsz, value: flags.noSignedZeros()); |
| 1440 | value = bitEnumSet(bits: value, bit: FastmathFlags::arcp, value: flags.allowReciprocal()); |
| 1441 | value = bitEnumSet(bits: value, bit: FastmathFlags::contract, value: flags.allowContract()); |
| 1442 | value = bitEnumSet(bits: value, bit: FastmathFlags::afn, value: flags.approxFunc()); |
| 1443 | value = bitEnumSet(bits: value, bit: FastmathFlags::reassoc, value: flags.allowReassoc()); |
| 1444 | FastmathFlagsAttr attr = FastmathFlagsAttr::get(context: builder.getContext(), value); |
| 1445 | iface.setFastmathAttr(attr); |
| 1446 | } |
| 1447 | |
| 1448 | /// Returns `type` if it is a builtin integer or floating-point vector type that |
| 1449 | /// can be used to create an attribute or nullptr otherwise. If provided, |
| 1450 | /// `arrayShape` is added to the shape of the vector to create an attribute that |
| 1451 | /// matches an array of vectors. |
| 1452 | static Type getVectorTypeForAttr(Type type, ArrayRef<int64_t> arrayShape = {}) { |
| 1453 | if (!LLVM::isCompatibleVectorType(type)) |
| 1454 | return {}; |
| 1455 | |
| 1456 | llvm::ElementCount numElements = LLVM::getVectorNumElements(type); |
| 1457 | if (numElements.isScalable()) { |
| 1458 | emitError(loc: UnknownLoc::get(context: type.getContext())) |
| 1459 | << "scalable vectors not supported" ; |
| 1460 | return {}; |
| 1461 | } |
| 1462 | |
| 1463 | // An LLVM dialect vector can only contain scalars. |
| 1464 | Type elementType = cast<VectorType>(Val&: type).getElementType(); |
| 1465 | if (!elementType.isIntOrFloat()) |
| 1466 | return {}; |
| 1467 | |
| 1468 | SmallVector<int64_t> shape(arrayShape); |
| 1469 | shape.push_back(Elt: numElements.getKnownMinValue()); |
| 1470 | return VectorType::get(shape, elementType); |
| 1471 | } |
| 1472 | |
| 1473 | Type ModuleImport::getBuiltinTypeForAttr(Type type) { |
| 1474 | if (!type) |
| 1475 | return {}; |
| 1476 | |
| 1477 | // Return builtin integer and floating-point types as is. |
| 1478 | if (type.isIntOrFloat()) |
| 1479 | return type; |
| 1480 | |
| 1481 | // Return builtin vectors of integer and floating-point types as is. |
| 1482 | if (Type vectorType = getVectorTypeForAttr(type)) |
| 1483 | return vectorType; |
| 1484 | |
| 1485 | // Multi-dimensional array types are converted to tensors or vectors, |
| 1486 | // depending on the innermost type being a scalar or a vector. |
| 1487 | SmallVector<int64_t> arrayShape; |
| 1488 | while (auto arrayType = dyn_cast<LLVMArrayType>(Val&: type)) { |
| 1489 | arrayShape.push_back(Elt: arrayType.getNumElements()); |
| 1490 | type = arrayType.getElementType(); |
| 1491 | } |
| 1492 | if (type.isIntOrFloat()) |
| 1493 | return RankedTensorType::get(shape: arrayShape, elementType: type); |
| 1494 | return getVectorTypeForAttr(type, arrayShape); |
| 1495 | } |
| 1496 | |
| 1497 | /// Returns an integer or float attribute for the provided scalar constant |
| 1498 | /// `constScalar` or nullptr if the conversion fails. |
| 1499 | static TypedAttr getScalarConstantAsAttr(OpBuilder &builder, |
| 1500 | llvm::Constant *constScalar) { |
| 1501 | MLIRContext *context = builder.getContext(); |
| 1502 | |
| 1503 | if (constScalar->getType()->isVectorTy()) |
| 1504 | return {}; |
| 1505 | |
| 1506 | // Convert scalar integers. |
| 1507 | if (auto *constInt = dyn_cast<llvm::ConstantInt>(Val: constScalar)) { |
| 1508 | return builder.getIntegerAttr( |
| 1509 | type: IntegerType::get(context, width: constInt->getBitWidth()), |
| 1510 | value: constInt->getValue()); |
| 1511 | } |
| 1512 | |
| 1513 | // Convert scalar floats. |
| 1514 | if (auto *constFloat = dyn_cast<llvm::ConstantFP>(Val: constScalar)) { |
| 1515 | llvm::Type *type = constFloat->getType(); |
| 1516 | FloatType floatType = |
| 1517 | type->isBFloatTy() |
| 1518 | ? BFloat16Type::get(context) |
| 1519 | : LLVM::detail::getFloatType(context, width: type->getScalarSizeInBits()); |
| 1520 | if (!floatType) { |
| 1521 | emitError(loc: UnknownLoc::get(context: builder.getContext())) |
| 1522 | << "unexpected floating-point type" ; |
| 1523 | return {}; |
| 1524 | } |
| 1525 | return builder.getFloatAttr(type: floatType, value: constFloat->getValueAPF()); |
| 1526 | } |
| 1527 | return {}; |
| 1528 | } |
| 1529 | |
| 1530 | /// Returns an integer or float attribute array for the provided constant |
| 1531 | /// sequence `constSequence` or nullptr if the conversion fails. |
| 1532 | static SmallVector<Attribute> |
| 1533 | getSequenceConstantAsAttrs(OpBuilder &builder, |
| 1534 | llvm::ConstantDataSequential *constSequence) { |
| 1535 | SmallVector<Attribute> elementAttrs; |
| 1536 | elementAttrs.reserve(N: constSequence->getNumElements()); |
| 1537 | for (auto idx : llvm::seq<int64_t>(Begin: 0, End: constSequence->getNumElements())) { |
| 1538 | llvm::Constant *constElement = constSequence->getElementAsConstant(i: idx); |
| 1539 | elementAttrs.push_back(Elt: getScalarConstantAsAttr(builder, constScalar: constElement)); |
| 1540 | } |
| 1541 | return elementAttrs; |
| 1542 | } |
| 1543 | |
| 1544 | Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) { |
| 1545 | // Convert scalar constants. |
| 1546 | if (Attribute scalarAttr = getScalarConstantAsAttr(builder, constScalar: constant)) |
| 1547 | return scalarAttr; |
| 1548 | |
| 1549 | // Returns the static shape of the provided type if possible. |
| 1550 | auto getConstantShape = [&](llvm::Type *type) { |
| 1551 | return llvm::dyn_cast_if_present<ShapedType>( |
| 1552 | Val: getBuiltinTypeForAttr(type: convertType(type))); |
| 1553 | }; |
| 1554 | |
| 1555 | // Convert constant vector splat values. |
| 1556 | if (isa<llvm::ConstantInt, llvm::ConstantFP>(Val: constant)) { |
| 1557 | assert(constant->getType()->isVectorTy() && "expected a vector splat" ); |
| 1558 | auto shape = getConstantShape(constant->getType()); |
| 1559 | if (!shape) |
| 1560 | return {}; |
| 1561 | Attribute splatAttr = |
| 1562 | getScalarConstantAsAttr(builder, constScalar: constant->getSplatValue()); |
| 1563 | return SplatElementsAttr::get(type: shape, values: splatAttr); |
| 1564 | } |
| 1565 | |
| 1566 | // Convert one-dimensional constant arrays or vectors that store 1/2/4/8-byte |
| 1567 | // integer or half/bfloat/float/double values. |
| 1568 | if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(Val: constant)) { |
| 1569 | if (constArray->isString()) |
| 1570 | return builder.getStringAttr(bytes: constArray->getAsString()); |
| 1571 | auto shape = getConstantShape(constArray->getType()); |
| 1572 | if (!shape) |
| 1573 | return {}; |
| 1574 | // Convert splat constants to splat elements attributes. |
| 1575 | auto *constVector = dyn_cast<llvm::ConstantDataVector>(Val: constant); |
| 1576 | if (constVector && constVector->isSplat()) { |
| 1577 | // A vector is guaranteed to have at least size one. |
| 1578 | Attribute splatAttr = getScalarConstantAsAttr( |
| 1579 | builder, constScalar: constVector->getElementAsConstant(i: 0)); |
| 1580 | return SplatElementsAttr::get(type: shape, values: splatAttr); |
| 1581 | } |
| 1582 | // Convert non-splat constants to dense elements attributes. |
| 1583 | SmallVector<Attribute> elementAttrs = |
| 1584 | getSequenceConstantAsAttrs(builder, constSequence: constArray); |
| 1585 | return DenseElementsAttr::get(type: shape, values: elementAttrs); |
| 1586 | } |
| 1587 | |
| 1588 | // Convert multi-dimensional constant aggregates that store all kinds of |
| 1589 | // integer and floating-point types. |
| 1590 | if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(Val: constant)) { |
| 1591 | auto shape = getConstantShape(constAggregate->getType()); |
| 1592 | if (!shape) |
| 1593 | return {}; |
| 1594 | // Collect the aggregate elements in depths first order. |
| 1595 | SmallVector<Attribute> elementAttrs; |
| 1596 | SmallVector<llvm::Constant *> workList = {constAggregate}; |
| 1597 | while (!workList.empty()) { |
| 1598 | llvm::Constant *current = workList.pop_back_val(); |
| 1599 | // Append any nested aggregates in reverse order to ensure the head |
| 1600 | // element of the nested aggregates is at the back of the work list. |
| 1601 | if (auto *constAggregate = dyn_cast<llvm::ConstantAggregate>(Val: current)) { |
| 1602 | for (auto idx : |
| 1603 | reverse(C: llvm::seq<int64_t>(Begin: 0, End: constAggregate->getNumOperands()))) |
| 1604 | workList.push_back(Elt: constAggregate->getAggregateElement(Elt: idx)); |
| 1605 | continue; |
| 1606 | } |
| 1607 | // Append the elements of nested constant arrays or vectors that store |
| 1608 | // 1/2/4/8-byte integer or half/bfloat/float/double values. |
| 1609 | if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(Val: current)) { |
| 1610 | SmallVector<Attribute> attrs = |
| 1611 | getSequenceConstantAsAttrs(builder, constSequence: constArray); |
| 1612 | elementAttrs.append(in_start: attrs.begin(), in_end: attrs.end()); |
| 1613 | continue; |
| 1614 | } |
| 1615 | // Append nested scalar constants that store all kinds of integer and |
| 1616 | // floating-point types. |
| 1617 | if (Attribute scalarAttr = getScalarConstantAsAttr(builder, constScalar: current)) { |
| 1618 | elementAttrs.push_back(Elt: scalarAttr); |
| 1619 | continue; |
| 1620 | } |
| 1621 | // Bail if the aggregate contains a unsupported constant type such as a |
| 1622 | // constant expression. |
| 1623 | return {}; |
| 1624 | } |
| 1625 | return DenseElementsAttr::get(type: shape, values: elementAttrs); |
| 1626 | } |
| 1627 | |
| 1628 | // Convert zero aggregates. |
| 1629 | if (auto *constZero = dyn_cast<llvm::ConstantAggregateZero>(Val: constant)) { |
| 1630 | auto shape = llvm::dyn_cast_if_present<ShapedType>( |
| 1631 | Val: getBuiltinTypeForAttr(type: convertType(type: constZero->getType()))); |
| 1632 | if (!shape) |
| 1633 | return {}; |
| 1634 | // Convert zero aggregates with a static shape to splat elements attributes. |
| 1635 | Attribute splatAttr = builder.getZeroAttr(type: shape.getElementType()); |
| 1636 | assert(splatAttr && "expected non-null zero attribute for scalar types" ); |
| 1637 | return SplatElementsAttr::get(type: shape, values: splatAttr); |
| 1638 | } |
| 1639 | return {}; |
| 1640 | } |
| 1641 | |
| 1642 | FlatSymbolRefAttr |
| 1643 | ModuleImport::getOrCreateNamelessSymbolName(llvm::GlobalVariable *globalVar) { |
| 1644 | assert(globalVar->getName().empty() && |
| 1645 | "expected to work with a nameless global" ); |
| 1646 | auto [it, success] = namelessGlobals.try_emplace(Key: globalVar); |
| 1647 | if (!success) |
| 1648 | return it->second; |
| 1649 | |
| 1650 | // Make sure the symbol name does not clash with an existing symbol. |
| 1651 | SmallString<128> globalName = SymbolTable::generateSymbolName<128>( |
| 1652 | name: getNamelessGlobalPrefix(), |
| 1653 | uniqueChecker: [this](StringRef newName) { return llvmModule->getNamedValue(Name: newName); }, |
| 1654 | uniquingCounter&: namelessGlobalId); |
| 1655 | auto symbolRef = FlatSymbolRefAttr::get(ctx: context, value: globalName); |
| 1656 | it->getSecond() = symbolRef; |
| 1657 | return symbolRef; |
| 1658 | } |
| 1659 | |
| 1660 | OpBuilder::InsertionGuard ModuleImport::setGlobalInsertionPoint() { |
| 1661 | OpBuilder::InsertionGuard guard(builder); |
| 1662 | if (globalInsertionOp) |
| 1663 | builder.setInsertionPointAfter(globalInsertionOp); |
| 1664 | else |
| 1665 | builder.setInsertionPointToStart(mlirModule.getBody()); |
| 1666 | return guard; |
| 1667 | } |
| 1668 | |
| 1669 | LogicalResult ModuleImport::convertAlias(llvm::GlobalAlias *alias) { |
| 1670 | // Insert the alias after the last one or at the start of the module. |
| 1671 | OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(); |
| 1672 | |
| 1673 | Type type = convertType(type: alias->getValueType()); |
| 1674 | AliasOp aliasOp = AliasOp::create( |
| 1675 | builder, location: mlirModule.getLoc(), type, |
| 1676 | linkage: convertLinkageFromLLVM(value: alias->getLinkage()), name: alias->getName(), |
| 1677 | /*dsoLocal=*/alias->isDSOLocal(), |
| 1678 | tls_mode: convertThreadLocalModeFromLLVM(value: alias->getThreadLocalMode()), |
| 1679 | /*attrs=*/ArrayRef<NamedAttribute>()); |
| 1680 | globalInsertionOp = aliasOp; |
| 1681 | |
| 1682 | clearRegionState(); |
| 1683 | Block *block = builder.createBlock(parent: &aliasOp.getInitializerRegion()); |
| 1684 | setConstantInsertionPointToStart(block); |
| 1685 | FailureOr<Value> initializer = convertConstantExpr(constant: alias->getAliasee()); |
| 1686 | if (failed(Result: initializer)) |
| 1687 | return failure(); |
| 1688 | ReturnOp::create(builder, location: aliasOp.getLoc(), arg: *initializer); |
| 1689 | |
| 1690 | if (alias->hasAtLeastLocalUnnamedAddr()) |
| 1691 | aliasOp.setUnnamedAddr(convertUnnamedAddrFromLLVM(value: alias->getUnnamedAddr())); |
| 1692 | aliasOp.setVisibility_(convertVisibilityFromLLVM(value: alias->getVisibility())); |
| 1693 | |
| 1694 | return success(); |
| 1695 | } |
| 1696 | |
| 1697 | LogicalResult ModuleImport::convertIFunc(llvm::GlobalIFunc *ifunc) { |
| 1698 | OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(); |
| 1699 | |
| 1700 | Type type = convertType(type: ifunc->getValueType()); |
| 1701 | llvm::Constant *resolver = ifunc->getResolver(); |
| 1702 | Type resolverType = convertType(type: resolver->getType()); |
| 1703 | IFuncOp::create(builder, location: mlirModule.getLoc(), sym_name: ifunc->getName(), i_func_type: type, |
| 1704 | resolver: resolver->getName(), resolver_type: resolverType, |
| 1705 | linkage: convertLinkageFromLLVM(value: ifunc->getLinkage()), |
| 1706 | dso_local: ifunc->isDSOLocal(), address_space: ifunc->getAddressSpace(), |
| 1707 | unnamed_addr: convertUnnamedAddrFromLLVM(value: ifunc->getUnnamedAddr()), |
| 1708 | visibility_: convertVisibilityFromLLVM(value: ifunc->getVisibility()), |
| 1709 | /*sym_visibility=*/nullptr); |
| 1710 | return success(); |
| 1711 | } |
| 1712 | |
| 1713 | /// Converts LLVM string, integer, and enum attributes into MLIR attributes, |
| 1714 | /// skipping those in `attributesToSkip` and emitting a warning at `loc` for |
| 1715 | /// any other unsupported attributes. |
| 1716 | static ArrayAttr convertLLVMAttributesToMLIR( |
| 1717 | Location loc, MLIRContext *context, llvm::AttributeSet attributes, |
| 1718 | ArrayRef<StringLiteral> attributesToSkip = {}, |
| 1719 | ArrayRef<StringLiteral> attributePrefixesToSkip = {}) { |
| 1720 | SmallVector<Attribute> mlirAttributes; |
| 1721 | for (llvm::Attribute attr : attributes) { |
| 1722 | StringRef attrName; |
| 1723 | if (attr.isStringAttribute()) |
| 1724 | attrName = attr.getKindAsString(); |
| 1725 | else |
| 1726 | attrName = llvm::Attribute::getNameFromAttrKind(AttrKind: attr.getKindAsEnum()); |
| 1727 | if (llvm::is_contained(Range&: attributesToSkip, Element: attrName)) |
| 1728 | continue; |
| 1729 | |
| 1730 | auto attrNameStartsWith = [attrName](StringLiteral sl) { |
| 1731 | return attrName.starts_with(Prefix: sl); |
| 1732 | }; |
| 1733 | if (attributePrefixesToSkip.end() != |
| 1734 | llvm::find_if(Range&: attributePrefixesToSkip, P: attrNameStartsWith)) |
| 1735 | continue; |
| 1736 | |
| 1737 | auto keyAttr = StringAttr::get(context, bytes: attrName); |
| 1738 | if (attr.isStringAttribute()) { |
| 1739 | StringRef val = attr.getValueAsString(); |
| 1740 | if (val.empty()) { |
| 1741 | // For string attributes without values, add only the attribute name. |
| 1742 | mlirAttributes.push_back(Elt: keyAttr); |
| 1743 | continue; |
| 1744 | } |
| 1745 | // For string attributes with a value, create a [name, value] pair. |
| 1746 | mlirAttributes.push_back( |
| 1747 | Elt: ArrayAttr::get(context, value: {keyAttr, StringAttr::get(context, bytes: val)})); |
| 1748 | continue; |
| 1749 | } |
| 1750 | if (attr.isIntAttribute()) { |
| 1751 | // For integer attributes, convert the value to a string and create a |
| 1752 | // [name, value] pair. |
| 1753 | auto val = std::to_string(val: attr.getValueAsInt()); |
| 1754 | mlirAttributes.push_back( |
| 1755 | Elt: ArrayAttr::get(context, value: {keyAttr, StringAttr::get(context, bytes: val)})); |
| 1756 | continue; |
| 1757 | } |
| 1758 | if (attr.isEnumAttribute()) { |
| 1759 | // For enum attributes, add only the attribute name. |
| 1760 | mlirAttributes.push_back(Elt: keyAttr); |
| 1761 | continue; |
| 1762 | } |
| 1763 | |
| 1764 | emitWarning(loc) |
| 1765 | << "'" << attrName |
| 1766 | << "' attribute is invalid on current operation, skipping it" ; |
| 1767 | } |
| 1768 | return ArrayAttr::get(context, value: mlirAttributes); |
| 1769 | } |
| 1770 | |
| 1771 | /// Converts LLVM attributes from `globalVar` into MLIR attributes and adds them |
| 1772 | /// to `globalOp` as target-specific attributes. |
| 1773 | static void processTargetSpecificAttrs(llvm::GlobalVariable *globalVar, |
| 1774 | GlobalOp globalOp) { |
| 1775 | ArrayAttr targetSpecificAttrs = convertLLVMAttributesToMLIR( |
| 1776 | loc: globalOp.getLoc(), context: globalOp.getContext(), attributes: globalVar->getAttributes()); |
| 1777 | if (!targetSpecificAttrs.empty()) |
| 1778 | globalOp.setTargetSpecificAttrsAttr(targetSpecificAttrs); |
| 1779 | } |
| 1780 | |
| 1781 | LogicalResult ModuleImport::convertGlobal(llvm::GlobalVariable *globalVar) { |
| 1782 | // Insert the global after the last one or at the start of the module. |
| 1783 | OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(); |
| 1784 | |
| 1785 | Attribute valueAttr; |
| 1786 | if (globalVar->hasInitializer()) |
| 1787 | valueAttr = getConstantAsAttr(constant: globalVar->getInitializer()); |
| 1788 | Type type = convertType(type: globalVar->getValueType()); |
| 1789 | |
| 1790 | uint64_t alignment = 0; |
| 1791 | llvm::MaybeAlign maybeAlign = globalVar->getAlign(); |
| 1792 | if (maybeAlign.has_value()) { |
| 1793 | llvm::Align align = *maybeAlign; |
| 1794 | alignment = align.value(); |
| 1795 | } |
| 1796 | |
| 1797 | // Get the global expression associated with this global variable and convert |
| 1798 | // it. |
| 1799 | SmallVector<Attribute> globalExpressionAttrs; |
| 1800 | SmallVector<llvm::DIGlobalVariableExpression *> globalExpressions; |
| 1801 | globalVar->getDebugInfo(GVs&: globalExpressions); |
| 1802 | |
| 1803 | for (llvm::DIGlobalVariableExpression *expr : globalExpressions) { |
| 1804 | DIGlobalVariableExpressionAttr globalExpressionAttr = |
| 1805 | debugImporter->translateGlobalVariableExpression(node: expr); |
| 1806 | globalExpressionAttrs.push_back(Elt: globalExpressionAttr); |
| 1807 | } |
| 1808 | |
| 1809 | // Workaround to support LLVM's nameless globals. MLIR, in contrast to LLVM, |
| 1810 | // always requires a symbol name. |
| 1811 | StringRef globalName = globalVar->getName(); |
| 1812 | if (globalName.empty()) |
| 1813 | globalName = getOrCreateNamelessSymbolName(globalVar).getValue(); |
| 1814 | |
| 1815 | GlobalOp globalOp = GlobalOp::create( |
| 1816 | builder, location: mlirModule.getLoc(), type, isConstant: globalVar->isConstant(), |
| 1817 | linkage: convertLinkageFromLLVM(value: globalVar->getLinkage()), name: StringRef(globalName), |
| 1818 | value: valueAttr, alignment, /*addrSpace=*/globalVar->getAddressSpace(), |
| 1819 | /*dsoLocal=*/globalVar->isDSOLocal(), |
| 1820 | tls_mode: convertThreadLocalModeFromLLVM(value: globalVar->getThreadLocalMode()), |
| 1821 | /*comdat=*/SymbolRefAttr(), |
| 1822 | /*attrs=*/ArrayRef<NamedAttribute>(), /*dbgExprs=*/globalExpressionAttrs); |
| 1823 | globalInsertionOp = globalOp; |
| 1824 | |
| 1825 | if (globalVar->hasInitializer() && !valueAttr) { |
| 1826 | clearRegionState(); |
| 1827 | Block *block = builder.createBlock(parent: &globalOp.getInitializerRegion()); |
| 1828 | setConstantInsertionPointToStart(block); |
| 1829 | FailureOr<Value> initializer = |
| 1830 | convertConstantExpr(constant: globalVar->getInitializer()); |
| 1831 | if (failed(Result: initializer)) |
| 1832 | return failure(); |
| 1833 | ReturnOp::create(builder, location: globalOp.getLoc(), arg: *initializer); |
| 1834 | } |
| 1835 | if (globalVar->hasAtLeastLocalUnnamedAddr()) { |
| 1836 | globalOp.setUnnamedAddr( |
| 1837 | convertUnnamedAddrFromLLVM(value: globalVar->getUnnamedAddr())); |
| 1838 | } |
| 1839 | if (globalVar->hasSection()) |
| 1840 | globalOp.setSection(globalVar->getSection()); |
| 1841 | globalOp.setVisibility_( |
| 1842 | convertVisibilityFromLLVM(value: globalVar->getVisibility())); |
| 1843 | |
| 1844 | if (globalVar->hasComdat()) |
| 1845 | globalOp.setComdatAttr(comdatMapping.lookup(Val: globalVar->getComdat())); |
| 1846 | |
| 1847 | if (llvm::MDNode *associatedMD = |
| 1848 | globalVar->getMetadata(KindID: llvm::LLVMContext::MD_associated)) { |
| 1849 | FlatSymbolRefAttr symbolRef; |
| 1850 | if (associatedMD->getNumOperands() == 1) |
| 1851 | symbolRef = |
| 1852 | getMetadataOperandSymbolRef(md: associatedMD->getOperand(I: 0).get()); |
| 1853 | if (!symbolRef) { |
| 1854 | emitWarning(loc: globalOp.getLoc()) |
| 1855 | << "unhandled associated metadata: " << renderMetadata(node: associatedMD) |
| 1856 | << " on " << renderValue(value: *globalVar); |
| 1857 | } else { |
| 1858 | globalOp.setAssociatedAttr(symbolRef); |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | if (llvm::MDNode *absSymMD = |
| 1863 | globalVar->getMetadata(KindID: llvm::LLVMContext::MD_absolute_symbol)) { |
| 1864 | unsigned numOps = absSymMD->getNumOperands(); |
| 1865 | if (numOps >= 2 && numOps % 2 == 0) { |
| 1866 | SmallVector<Attribute> rangeAttrs; |
| 1867 | rangeAttrs.reserve(N: numOps); |
| 1868 | |
| 1869 | for (const llvm::MDOperand &op : absSymMD->operands()) { |
| 1870 | auto *constInt = llvm::mdconst::dyn_extract<llvm::ConstantInt>(MD: op); |
| 1871 | if (!constInt) |
| 1872 | break; |
| 1873 | |
| 1874 | auto intType = IntegerType::get(context, width: constInt->getBitWidth()); |
| 1875 | rangeAttrs.push_back(Elt: IntegerAttr::get(type: intType, value: constInt->getValue())); |
| 1876 | } |
| 1877 | |
| 1878 | if (rangeAttrs.size() == numOps) |
| 1879 | globalOp.setAbsoluteSymbolAttr(ArrayAttr::get(context, value: rangeAttrs)); |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | processTargetSpecificAttrs(globalVar, globalOp); |
| 1884 | |
| 1885 | return success(); |
| 1886 | } |
| 1887 | |
| 1888 | LogicalResult |
| 1889 | ModuleImport::convertGlobalCtorsAndDtors(llvm::GlobalVariable *globalVar) { |
| 1890 | if (!globalVar->hasInitializer() || !globalVar->hasAppendingLinkage()) |
| 1891 | return failure(); |
| 1892 | llvm::Constant *initializer = globalVar->getInitializer(); |
| 1893 | |
| 1894 | bool knownInit = isa<llvm::ConstantArray>(Val: initializer) || |
| 1895 | isa<llvm::ConstantAggregateZero>(Val: initializer); |
| 1896 | if (!knownInit) |
| 1897 | return failure(); |
| 1898 | |
| 1899 | // ConstantAggregateZero does not engage with the operand initialization |
| 1900 | // in the loop that follows - there should be no operands. This implies |
| 1901 | // empty ctor/dtor lists. |
| 1902 | if (auto *caz = dyn_cast<llvm::ConstantAggregateZero>(Val: initializer)) { |
| 1903 | if (caz->getElementCount().getFixedValue() != 0) |
| 1904 | return failure(); |
| 1905 | } |
| 1906 | |
| 1907 | SmallVector<Attribute> funcs; |
| 1908 | SmallVector<int32_t> priorities; |
| 1909 | SmallVector<Attribute> dataList; |
| 1910 | for (llvm::Value *operand : initializer->operands()) { |
| 1911 | auto *aggregate = dyn_cast<llvm::ConstantAggregate>(Val: operand); |
| 1912 | if (!aggregate || aggregate->getNumOperands() != 3) |
| 1913 | return failure(); |
| 1914 | |
| 1915 | auto *priority = dyn_cast<llvm::ConstantInt>(Val: aggregate->getOperand(i_nocapture: 0)); |
| 1916 | auto *func = dyn_cast<llvm::Function>(Val: aggregate->getOperand(i_nocapture: 1)); |
| 1917 | auto *data = dyn_cast<llvm::Constant>(Val: aggregate->getOperand(i_nocapture: 2)); |
| 1918 | if (!priority || !func || !data) |
| 1919 | return failure(); |
| 1920 | |
| 1921 | auto *gv = dyn_cast_or_null<llvm::GlobalValue>(Val: data); |
| 1922 | Attribute dataAttr; |
| 1923 | if (gv) |
| 1924 | dataAttr = FlatSymbolRefAttr::get(ctx: context, value: gv->getName()); |
| 1925 | else if (data->isNullValue()) |
| 1926 | dataAttr = ZeroAttr::get(ctx: context); |
| 1927 | else |
| 1928 | return failure(); |
| 1929 | |
| 1930 | funcs.push_back(Elt: FlatSymbolRefAttr::get(ctx: context, value: func->getName())); |
| 1931 | priorities.push_back(Elt: priority->getValue().getZExtValue()); |
| 1932 | dataList.push_back(Elt: dataAttr); |
| 1933 | } |
| 1934 | |
| 1935 | // Insert the global after the last one or at the start of the module. |
| 1936 | OpBuilder::InsertionGuard guard = setGlobalInsertionPoint(); |
| 1937 | |
| 1938 | if (globalVar->getName() == getGlobalCtorsVarName()) { |
| 1939 | globalInsertionOp = LLVM::GlobalCtorsOp::create( |
| 1940 | builder, location: mlirModule.getLoc(), ctors: builder.getArrayAttr(value: funcs), |
| 1941 | priorities: builder.getI32ArrayAttr(values: priorities), data: builder.getArrayAttr(value: dataList)); |
| 1942 | return success(); |
| 1943 | } |
| 1944 | globalInsertionOp = LLVM::GlobalDtorsOp::create( |
| 1945 | builder, location: mlirModule.getLoc(), dtors: builder.getArrayAttr(value: funcs), |
| 1946 | priorities: builder.getI32ArrayAttr(values: priorities), data: builder.getArrayAttr(value: dataList)); |
| 1947 | return success(); |
| 1948 | } |
| 1949 | |
| 1950 | SetVector<llvm::Constant *> |
| 1951 | ModuleImport::getConstantsToConvert(llvm::Constant *constant) { |
| 1952 | // Return the empty set if the constant has been translated before. |
| 1953 | if (valueMapping.contains(Val: constant)) |
| 1954 | return {}; |
| 1955 | |
| 1956 | // Traverse the constants in post-order and stop the traversal if a constant |
| 1957 | // already has a `valueMapping` from an earlier constant translation or if the |
| 1958 | // constant is traversed a second time. |
| 1959 | SetVector<llvm::Constant *> orderedSet; |
| 1960 | SetVector<llvm::Constant *> workList; |
| 1961 | DenseMap<llvm::Constant *, SmallVector<llvm::Constant *>> adjacencyLists; |
| 1962 | workList.insert(X: constant); |
| 1963 | while (!workList.empty()) { |
| 1964 | llvm::Constant *current = workList.back(); |
| 1965 | // References of global objects are just pointers to the object. Avoid |
| 1966 | // walking the elements of these here. |
| 1967 | if (isa<llvm::GlobalObject>(Val: current) || isa<llvm::GlobalAlias>(Val: current)) { |
| 1968 | orderedSet.insert(X: current); |
| 1969 | workList.pop_back(); |
| 1970 | continue; |
| 1971 | } |
| 1972 | |
| 1973 | // Collect all dependencies of the current constant and add them to the |
| 1974 | // adjacency list if none has been computed before. |
| 1975 | auto [adjacencyIt, inserted] = adjacencyLists.try_emplace(Key: current); |
| 1976 | if (inserted) { |
| 1977 | // Add all constant operands to the adjacency list and skip any other |
| 1978 | // values such as basic block addresses. |
| 1979 | for (llvm::Value *operand : current->operands()) |
| 1980 | if (auto *constDependency = dyn_cast<llvm::Constant>(Val: operand)) |
| 1981 | adjacencyIt->getSecond().push_back(Elt: constDependency); |
| 1982 | // Use the getElementValue method to add the dependencies of zero |
| 1983 | // initialized aggregate constants since they do not take any operands. |
| 1984 | if (auto *constAgg = dyn_cast<llvm::ConstantAggregateZero>(Val: current)) { |
| 1985 | unsigned numElements = constAgg->getElementCount().getFixedValue(); |
| 1986 | for (unsigned i = 0, e = numElements; i != e; ++i) |
| 1987 | adjacencyIt->getSecond().push_back(Elt: constAgg->getElementValue(Idx: i)); |
| 1988 | } |
| 1989 | } |
| 1990 | // Add the current constant to the `orderedSet` of the traversed nodes if |
| 1991 | // all its dependencies have been traversed before. Additionally, remove the |
| 1992 | // constant from the `workList` and continue the traversal. |
| 1993 | if (adjacencyIt->getSecond().empty()) { |
| 1994 | orderedSet.insert(X: current); |
| 1995 | workList.pop_back(); |
| 1996 | continue; |
| 1997 | } |
| 1998 | // Add the next dependency from the adjacency list to the `workList` and |
| 1999 | // continue the traversal. Remove the dependency from the adjacency list to |
| 2000 | // mark that it has been processed. Only enqueue the dependency if it has no |
| 2001 | // `valueMapping` from an earlier translation and if it has not been |
| 2002 | // enqueued before. |
| 2003 | llvm::Constant *dependency = adjacencyIt->getSecond().pop_back_val(); |
| 2004 | if (valueMapping.contains(Val: dependency) || workList.contains(key: dependency) || |
| 2005 | orderedSet.contains(key: dependency)) |
| 2006 | continue; |
| 2007 | workList.insert(X: dependency); |
| 2008 | } |
| 2009 | |
| 2010 | return orderedSet; |
| 2011 | } |
| 2012 | |
| 2013 | FailureOr<Value> ModuleImport::convertConstant(llvm::Constant *constant) { |
| 2014 | Location loc = UnknownLoc::get(context); |
| 2015 | |
| 2016 | // Convert constants that can be represented as attributes. |
| 2017 | if (Attribute attr = getConstantAsAttr(constant)) { |
| 2018 | Type type = convertType(type: constant->getType()); |
| 2019 | if (auto symbolRef = dyn_cast<FlatSymbolRefAttr>(Val&: attr)) { |
| 2020 | return AddressOfOp::create(builder, location: loc, res: type, global_name: symbolRef.getValue()) |
| 2021 | .getResult(); |
| 2022 | } |
| 2023 | return ConstantOp::create(builder, location: loc, res: type, value: attr).getResult(); |
| 2024 | } |
| 2025 | |
| 2026 | // Convert null pointer constants. |
| 2027 | if (auto *nullPtr = dyn_cast<llvm::ConstantPointerNull>(Val: constant)) { |
| 2028 | Type type = convertType(type: nullPtr->getType()); |
| 2029 | return ZeroOp::create(builder, location: loc, res: type).getResult(); |
| 2030 | } |
| 2031 | |
| 2032 | // Convert none token constants. |
| 2033 | if (isa<llvm::ConstantTokenNone>(Val: constant)) { |
| 2034 | return NoneTokenOp::create(builder, location: loc).getResult(); |
| 2035 | } |
| 2036 | |
| 2037 | // Convert poison. |
| 2038 | if (auto *poisonVal = dyn_cast<llvm::PoisonValue>(Val: constant)) { |
| 2039 | Type type = convertType(type: poisonVal->getType()); |
| 2040 | return PoisonOp::create(builder, location: loc, res: type).getResult(); |
| 2041 | } |
| 2042 | |
| 2043 | // Convert undef. |
| 2044 | if (auto *undefVal = dyn_cast<llvm::UndefValue>(Val: constant)) { |
| 2045 | Type type = convertType(type: undefVal->getType()); |
| 2046 | return UndefOp::create(builder, location: loc, res: type).getResult(); |
| 2047 | } |
| 2048 | |
| 2049 | // Convert dso_local_equivalent. |
| 2050 | if (auto *dsoLocalEquivalent = dyn_cast<llvm::DSOLocalEquivalent>(Val: constant)) { |
| 2051 | Type type = convertType(type: dsoLocalEquivalent->getType()); |
| 2052 | return DSOLocalEquivalentOp::create( |
| 2053 | builder, location: loc, res: type, |
| 2054 | function_name: FlatSymbolRefAttr::get( |
| 2055 | ctx: builder.getContext(), |
| 2056 | value: dsoLocalEquivalent->getGlobalValue()->getName())) |
| 2057 | .getResult(); |
| 2058 | } |
| 2059 | |
| 2060 | // Convert global variable accesses. |
| 2061 | if (auto *globalObj = dyn_cast<llvm::GlobalObject>(Val: constant)) { |
| 2062 | Type type = convertType(type: globalObj->getType()); |
| 2063 | StringRef globalName = globalObj->getName(); |
| 2064 | FlatSymbolRefAttr symbolRef; |
| 2065 | // Empty names are only allowed for global variables. |
| 2066 | if (globalName.empty()) |
| 2067 | symbolRef = |
| 2068 | getOrCreateNamelessSymbolName(globalVar: cast<llvm::GlobalVariable>(Val: globalObj)); |
| 2069 | else |
| 2070 | symbolRef = FlatSymbolRefAttr::get(ctx: context, value: globalName); |
| 2071 | return AddressOfOp::create(builder, location: loc, res: type, global_name: symbolRef).getResult(); |
| 2072 | } |
| 2073 | |
| 2074 | // Convert global alias accesses. |
| 2075 | if (auto *globalAliasObj = dyn_cast<llvm::GlobalAlias>(Val: constant)) { |
| 2076 | Type type = convertType(type: globalAliasObj->getType()); |
| 2077 | StringRef aliaseeName = globalAliasObj->getName(); |
| 2078 | FlatSymbolRefAttr symbolRef = FlatSymbolRefAttr::get(ctx: context, value: aliaseeName); |
| 2079 | return AddressOfOp::create(builder, location: loc, res: type, global_name: symbolRef).getResult(); |
| 2080 | } |
| 2081 | |
| 2082 | // Convert constant expressions. |
| 2083 | if (auto *constExpr = dyn_cast<llvm::ConstantExpr>(Val: constant)) { |
| 2084 | // Convert the constant expression to a temporary LLVM instruction and |
| 2085 | // translate it using the `processInstruction` method. Delete the |
| 2086 | // instruction after the translation and remove it from `valueMapping`, |
| 2087 | // since later calls to `getAsInstruction` may return the same address |
| 2088 | // resulting in a conflicting `valueMapping` entry. |
| 2089 | llvm::Instruction *inst = constExpr->getAsInstruction(); |
| 2090 | llvm::scope_exit guard([&]() { |
| 2091 | assert(!noResultOpMapping.contains(inst) && |
| 2092 | "expected constant expression to return a result" ); |
| 2093 | valueMapping.erase(Val: inst); |
| 2094 | inst->deleteValue(); |
| 2095 | }); |
| 2096 | // Note: `processInstruction` does not call `convertConstant` recursively |
| 2097 | // since all constant dependencies have been converted before. |
| 2098 | assert(llvm::all_of(inst->operands(), [&](llvm::Value *value) { |
| 2099 | return valueMapping.contains(value); |
| 2100 | })); |
| 2101 | if (failed(Result: processInstruction(inst))) |
| 2102 | return failure(); |
| 2103 | Value result = lookupValue(value: inst); |
| 2104 | // getAsInstruction() does not preserve GEP `inrange`, which exists only on |
| 2105 | // constant expressions. Reattach it to the imported GEPOp. |
| 2106 | if (constExpr->getOpcode() == llvm::Instruction::GetElementPtr) { |
| 2107 | auto *gepOperator = llvm::cast<llvm::GEPOperator>(Val: constExpr); |
| 2108 | if (std::optional<llvm::ConstantRange> inRange = |
| 2109 | gepOperator->getInRange()) { |
| 2110 | auto gepOp = result.getDefiningOp<GEPOp>(); |
| 2111 | assert(gepOp && "expected GEPOp for getelementptr constexpr" ); |
| 2112 | // The range is not always built at the index width of the base |
| 2113 | // pointer (clang uses 32 bits for vtable address points); bring it to |
| 2114 | // that width, as the textual IR parser does. |
| 2115 | unsigned indexWidth = |
| 2116 | llvmModule->getDataLayout().getIndexTypeSizeInBits( |
| 2117 | Ty: gepOperator->getPointerOperandType()); |
| 2118 | gepOp.setInrangeAttr(LLVM::ConstantRangeAttr::get( |
| 2119 | context, lower: inRange->getLower().sextOrTrunc(width: indexWidth), |
| 2120 | upper: inRange->getUpper().sextOrTrunc(width: indexWidth))); |
| 2121 | } |
| 2122 | } |
| 2123 | return result; |
| 2124 | } |
| 2125 | |
| 2126 | // Convert zero-initialized aggregates to ZeroOp. |
| 2127 | if (auto *aggregateZero = dyn_cast<llvm::ConstantAggregateZero>(Val: constant)) { |
| 2128 | Type type = convertType(type: aggregateZero->getType()); |
| 2129 | return ZeroOp::create(builder, location: loc, res: type).getResult(); |
| 2130 | } |
| 2131 | |
| 2132 | // Convert aggregate constants. |
| 2133 | if (auto *constAgg = dyn_cast<llvm::ConstantAggregate>(Val: constant)) { |
| 2134 | // Lookup the aggregate elements that have been converted before. |
| 2135 | SmallVector<Value> elementValues; |
| 2136 | |
| 2137 | elementValues.reserve(N: constAgg->getNumOperands()); |
| 2138 | for (llvm::Value *operand : constAgg->operands()) |
| 2139 | elementValues.push_back(Elt: lookupValue(value: operand)); |
| 2140 | |
| 2141 | assert(llvm::count(elementValues, nullptr) == 0 && |
| 2142 | "expected all elements have been converted before" ); |
| 2143 | |
| 2144 | // Generate an UndefOp as root value and insert the aggregate elements. |
| 2145 | Type rootType = convertType(type: constant->getType()); |
| 2146 | bool isArrayOrStruct = isa<LLVMArrayType, LLVMStructType>(Val: rootType); |
| 2147 | assert((isArrayOrStruct || LLVM::isCompatibleVectorType(rootType)) && |
| 2148 | "unrecognized aggregate type" ); |
| 2149 | Value root = UndefOp::create(builder, location: loc, res: rootType); |
| 2150 | for (const auto &it : llvm::enumerate(First&: elementValues)) { |
| 2151 | if (isArrayOrStruct) { |
| 2152 | root = |
| 2153 | InsertValueOp::create(builder, location: loc, container: root, value: it.value(), position: it.index()); |
| 2154 | } else { |
| 2155 | Attribute indexAttr = builder.getI32IntegerAttr(value: it.index()); |
| 2156 | Value indexValue = |
| 2157 | ConstantOp::create(builder, location: loc, res: builder.getI32Type(), value: indexAttr); |
| 2158 | root = InsertElementOp::create(builder, location: loc, res: rootType, vector: root, value: it.value(), |
| 2159 | position: indexValue); |
| 2160 | } |
| 2161 | } |
| 2162 | return root; |
| 2163 | } |
| 2164 | |
| 2165 | if (auto *constTargetNone = dyn_cast<llvm::ConstantTargetNone>(Val: constant)) { |
| 2166 | LLVMTargetExtType targetExtType = |
| 2167 | cast<LLVMTargetExtType>(Val: convertType(type: constTargetNone->getType())); |
| 2168 | assert(targetExtType.hasProperty(LLVMTargetExtType::HasZeroInit) && |
| 2169 | "target extension type does not support zero-initialization" ); |
| 2170 | // Create llvm.mlir.zero operation to represent zero-initialization of |
| 2171 | // target extension type. |
| 2172 | return LLVM::ZeroOp::create(builder, location: loc, res: targetExtType).getRes(); |
| 2173 | } |
| 2174 | |
| 2175 | if (auto *blockAddr = dyn_cast<llvm::BlockAddress>(Val: constant)) { |
| 2176 | auto fnSym = |
| 2177 | FlatSymbolRefAttr::get(ctx: context, value: blockAddr->getFunction()->getName()); |
| 2178 | auto blockTag = |
| 2179 | BlockTagAttr::get(context, id: blockAddr->getBasicBlock()->getNumber()); |
| 2180 | return BlockAddressOp::create( |
| 2181 | builder, location: loc, res: convertType(type: blockAddr->getType()), |
| 2182 | block_addr: BlockAddressAttr::get(context, function: fnSym, tag: blockTag)) |
| 2183 | .getRes(); |
| 2184 | } |
| 2185 | |
| 2186 | StringRef error = "" ; |
| 2187 | |
| 2188 | if (isa<llvm::ConstantPtrAuth>(Val: constant)) |
| 2189 | error = " since ptrauth(...) is unsupported" ; |
| 2190 | |
| 2191 | if (isa<llvm::NoCFIValue>(Val: constant)) |
| 2192 | error = " since no_cfi is unsupported" ; |
| 2193 | |
| 2194 | if (isa<llvm::GlobalValue>(Val: constant)) |
| 2195 | error = " since global value is unsupported" ; |
| 2196 | |
| 2197 | return emitError(loc) << "unhandled constant: " << renderValue(value: *constant) |
| 2198 | << error; |
| 2199 | } |
| 2200 | |
| 2201 | FailureOr<Value> ModuleImport::convertConstantExpr(llvm::Constant *constant) { |
| 2202 | // Only call the function for constants that have not been translated before |
| 2203 | // since it updates the constant insertion point assuming the converted |
| 2204 | // constant has been introduced at the end of the constant section. |
| 2205 | assert(!valueMapping.contains(constant) && |
| 2206 | "expected constant has not been converted before" ); |
| 2207 | assert(constantInsertionBlock && |
| 2208 | "expected the constant insertion block to be non-null" ); |
| 2209 | |
| 2210 | // Insert the constant after the last one or at the start of the entry block. |
| 2211 | OpBuilder::InsertionGuard guard(builder); |
| 2212 | if (!constantInsertionOp) |
| 2213 | builder.setInsertionPointToStart(constantInsertionBlock); |
| 2214 | else |
| 2215 | builder.setInsertionPointAfter(constantInsertionOp); |
| 2216 | |
| 2217 | // Convert all constants of the expression and add them to `valueMapping`. |
| 2218 | SetVector<llvm::Constant *> constantsToConvert = |
| 2219 | getConstantsToConvert(constant); |
| 2220 | for (llvm::Constant *constantToConvert : constantsToConvert) { |
| 2221 | FailureOr<Value> converted = convertConstant(constant: constantToConvert); |
| 2222 | if (failed(Result: converted)) |
| 2223 | return failure(); |
| 2224 | mapValue(llvm: constantToConvert, mlir: *converted); |
| 2225 | } |
| 2226 | |
| 2227 | // Update the constant insertion point and return the converted constant. |
| 2228 | Value result = lookupValue(value: constant); |
| 2229 | constantInsertionOp = result.getDefiningOp(); |
| 2230 | return result; |
| 2231 | } |
| 2232 | |
| 2233 | FailureOr<Value> ModuleImport::convertValue(llvm::Value *value) { |
| 2234 | // Return the mapped value if it has been converted before. |
| 2235 | auto it = valueMapping.find(Val: value); |
| 2236 | if (it != valueMapping.end()) |
| 2237 | return it->getSecond(); |
| 2238 | |
| 2239 | // `llvm::MetadataAsValue` operands (e.g. the rounding-mode / FP-exception |
| 2240 | // MDString arguments used by the constrained floating-point intrinsics, or |
| 2241 | // the named-register MDNode used by `llvm.read_register`) are lifted into a |
| 2242 | // `llvm.mlir.metadata_as_value` SSA op carrying the corresponding metadata |
| 2243 | // attribute. |
| 2244 | if (auto *mdAsVal = dyn_cast<llvm::MetadataAsValue>(Val: value)) { |
| 2245 | llvm::Metadata *md = mdAsVal->getMetadata(); |
| 2246 | Attribute mdAttr = convertMetadataToAttr(md); |
| 2247 | if (!mdAttr) |
| 2248 | return emitError(loc: mlirModule.getLoc()) |
| 2249 | << "unsupported metadata: " << renderMetadata(node: md); |
| 2250 | Value result = |
| 2251 | MetadataAsValueOp::create(builder, location: UnknownLoc::get(context), metadata: mdAttr) |
| 2252 | .getRes(); |
| 2253 | mapValue(llvm: value, mlir: result); |
| 2254 | return result; |
| 2255 | } |
| 2256 | |
| 2257 | // Convert constants such as immediate values that have no mapping yet. |
| 2258 | if (auto *constant = dyn_cast<llvm::Constant>(Val: value)) |
| 2259 | return convertConstantExpr(constant); |
| 2260 | |
| 2261 | Location loc = UnknownLoc::get(context); |
| 2262 | if (auto *inst = dyn_cast<llvm::Instruction>(Val: value)) |
| 2263 | loc = translateLoc(loc: inst->getDebugLoc()); |
| 2264 | return emitError(loc) << "unhandled value: " << renderValue(value: *value); |
| 2265 | } |
| 2266 | |
| 2267 | FailureOr<Value> ModuleImport::convertMetadataValue(llvm::Value *value) { |
| 2268 | // A value may be wrapped as metadata, for example, when passed to a debug |
| 2269 | // intrinsic. Unwrap these values before the conversion. |
| 2270 | auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(Val: value); |
| 2271 | if (!nodeAsVal) |
| 2272 | return failure(); |
| 2273 | auto *node = dyn_cast<llvm::ValueAsMetadata>(Val: nodeAsVal->getMetadata()); |
| 2274 | if (!node) |
| 2275 | return failure(); |
| 2276 | value = node->getValue(); |
| 2277 | |
| 2278 | // Return the mapped value if it has been converted before. |
| 2279 | auto it = valueMapping.find(Val: value); |
| 2280 | if (it != valueMapping.end()) |
| 2281 | return it->getSecond(); |
| 2282 | |
| 2283 | // Convert constants such as immediate values that have no mapping yet. |
| 2284 | if (auto *constant = dyn_cast<llvm::Constant>(Val: value)) |
| 2285 | return convertConstantExpr(constant); |
| 2286 | return failure(); |
| 2287 | } |
| 2288 | |
| 2289 | FailureOr<SmallVector<Value>> |
| 2290 | ModuleImport::convertValues(ArrayRef<llvm::Value *> values) { |
| 2291 | SmallVector<Value> remapped; |
| 2292 | remapped.reserve(N: values.size()); |
| 2293 | for (llvm::Value *value : values) { |
| 2294 | FailureOr<Value> converted = convertValue(value); |
| 2295 | if (failed(Result: converted)) |
| 2296 | return failure(); |
| 2297 | remapped.push_back(Elt: *converted); |
| 2298 | } |
| 2299 | return remapped; |
| 2300 | } |
| 2301 | |
| 2302 | LogicalResult ModuleImport::convertIntrinsicArguments( |
| 2303 | ArrayRef<llvm::Value *> values, ArrayRef<llvm::OperandBundleUse> opBundles, |
| 2304 | bool requiresOpBundles, ArrayRef<unsigned> immArgPositions, |
| 2305 | ArrayRef<StringLiteral> immArgAttrNames, SmallVectorImpl<Value> &valuesOut, |
| 2306 | SmallVectorImpl<NamedAttribute> &attrsOut) { |
| 2307 | assert(immArgPositions.size() == immArgAttrNames.size() && |
| 2308 | "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal " |
| 2309 | "length" ); |
| 2310 | |
| 2311 | SmallVector<llvm::Value *> operands(values); |
| 2312 | for (auto [immArgPos, immArgName] : |
| 2313 | llvm::zip(t&: immArgPositions, u&: immArgAttrNames)) { |
| 2314 | auto &value = operands[immArgPos]; |
| 2315 | auto *constant = llvm::cast<llvm::Constant>(Val: value); |
| 2316 | auto attr = getScalarConstantAsAttr(builder, constScalar: constant); |
| 2317 | assert(attr && attr.getType().isIntOrFloat() && |
| 2318 | "expected immarg to be float or integer constant" ); |
| 2319 | auto nameAttr = StringAttr::get(context: attr.getContext(), bytes: immArgName); |
| 2320 | attrsOut.push_back(Elt: {nameAttr, attr}); |
| 2321 | // Mark matched attribute values as null (so they can be removed below). |
| 2322 | value = nullptr; |
| 2323 | } |
| 2324 | |
| 2325 | for (llvm::Value *value : operands) { |
| 2326 | if (!value) |
| 2327 | continue; |
| 2328 | auto mlirValue = convertValue(value); |
| 2329 | if (failed(Result: mlirValue)) |
| 2330 | return failure(); |
| 2331 | valuesOut.push_back(Elt: *mlirValue); |
| 2332 | } |
| 2333 | |
| 2334 | SmallVector<int> opBundleSizes; |
| 2335 | SmallVector<Attribute> opBundleTagAttrs; |
| 2336 | if (requiresOpBundles) { |
| 2337 | opBundleSizes.reserve(N: opBundles.size()); |
| 2338 | opBundleTagAttrs.reserve(N: opBundles.size()); |
| 2339 | |
| 2340 | for (const llvm::OperandBundleUse &bundle : opBundles) { |
| 2341 | opBundleSizes.push_back(Elt: bundle.Inputs.size()); |
| 2342 | opBundleTagAttrs.push_back(Elt: StringAttr::get(context, bytes: bundle.getTagName())); |
| 2343 | |
| 2344 | for (const llvm::Use &opBundleOperand : bundle.Inputs) { |
| 2345 | auto operandMlirValue = convertValue(value: opBundleOperand.get()); |
| 2346 | if (failed(Result: operandMlirValue)) |
| 2347 | return failure(); |
| 2348 | valuesOut.push_back(Elt: *operandMlirValue); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | auto opBundleSizesAttr = DenseI32ArrayAttr::get(context, content: opBundleSizes); |
| 2353 | auto opBundleSizesAttrNameAttr = |
| 2354 | StringAttr::get(context, bytes: LLVMDialect::getOpBundleSizesAttrName()); |
| 2355 | attrsOut.push_back(Elt: {opBundleSizesAttrNameAttr, opBundleSizesAttr}); |
| 2356 | |
| 2357 | auto opBundleTagsAttr = ArrayAttr::get(context, value: opBundleTagAttrs); |
| 2358 | auto opBundleTagsAttrNameAttr = |
| 2359 | StringAttr::get(context, bytes: LLVMDialect::getOpBundleTagsAttrName()); |
| 2360 | attrsOut.push_back(Elt: {opBundleTagsAttrNameAttr, opBundleTagsAttr}); |
| 2361 | } |
| 2362 | |
| 2363 | return success(); |
| 2364 | } |
| 2365 | |
| 2366 | IntegerAttr ModuleImport::matchIntegerAttr(llvm::Value *value) { |
| 2367 | IntegerAttr integerAttr; |
| 2368 | FailureOr<Value> converted = convertValue(value); |
| 2369 | bool success = succeeded(Result: converted) && |
| 2370 | matchPattern(value: *converted, pattern: m_Constant(bind_value: &integerAttr)); |
| 2371 | assert(success && "expected a constant integer value" ); |
| 2372 | (void)success; |
| 2373 | return integerAttr; |
| 2374 | } |
| 2375 | |
| 2376 | FloatAttr ModuleImport::matchFloatAttr(llvm::Value *value) { |
| 2377 | FloatAttr floatAttr; |
| 2378 | FailureOr<Value> converted = convertValue(value); |
| 2379 | bool success = |
| 2380 | succeeded(Result: converted) && matchPattern(value: *converted, pattern: m_Constant(bind_value: &floatAttr)); |
| 2381 | assert(success && "expected a constant float value" ); |
| 2382 | (void)success; |
| 2383 | return floatAttr; |
| 2384 | } |
| 2385 | |
| 2386 | DILocalVariableAttr ModuleImport::matchLocalVariableAttr( |
| 2387 | llvm::PointerUnion<llvm::Value *, llvm::DILocalVariable *> valOrVariable) { |
| 2388 | llvm::DILocalVariable *node = nullptr; |
| 2389 | if (auto *value = dyn_cast<llvm::Value *>(Val&: valOrVariable)) { |
| 2390 | auto *nodeAsVal = cast<llvm::MetadataAsValue>(Val: value); |
| 2391 | node = cast<llvm::DILocalVariable>(Val: nodeAsVal->getMetadata()); |
| 2392 | } else { |
| 2393 | node = cast<llvm::DILocalVariable *>(Val&: valOrVariable); |
| 2394 | } |
| 2395 | return debugImporter->translate(node); |
| 2396 | } |
| 2397 | |
| 2398 | DILabelAttr ModuleImport::matchLabelAttr(llvm::Value *value) { |
| 2399 | auto *nodeAsVal = cast<llvm::MetadataAsValue>(Val: value); |
| 2400 | auto *node = cast<llvm::DILabel>(Val: nodeAsVal->getMetadata()); |
| 2401 | return debugImporter->translate(node); |
| 2402 | } |
| 2403 | |
| 2404 | FPExceptionBehaviorAttr |
| 2405 | ModuleImport::matchFPExceptionBehaviorAttr(llvm::Value *value) { |
| 2406 | auto *metadata = cast<llvm::MetadataAsValue>(Val: value); |
| 2407 | auto *mdstr = cast<llvm::MDString>(Val: metadata->getMetadata()); |
| 2408 | std::optional<llvm::fp::ExceptionBehavior> optLLVM = |
| 2409 | llvm::convertStrToExceptionBehavior(mdstr->getString()); |
| 2410 | assert(optLLVM && "Expecting FP exception behavior" ); |
| 2411 | return builder.getAttr<FPExceptionBehaviorAttr>( |
| 2412 | args: convertFPExceptionBehaviorFromLLVM(value: *optLLVM)); |
| 2413 | } |
| 2414 | |
| 2415 | RoundingModeAttr ModuleImport::matchRoundingModeAttr(llvm::Value *value) { |
| 2416 | auto *metadata = cast<llvm::MetadataAsValue>(Val: value); |
| 2417 | auto *mdstr = cast<llvm::MDString>(Val: metadata->getMetadata()); |
| 2418 | std::optional<llvm::RoundingMode> optLLVM = |
| 2419 | llvm::convertStrToRoundingMode(mdstr->getString()); |
| 2420 | assert(optLLVM && "Expecting rounding mode" ); |
| 2421 | return builder.getAttr<RoundingModeAttr>( |
| 2422 | args: convertRoundingModeFromLLVM(value: *optLLVM)); |
| 2423 | } |
| 2424 | |
| 2425 | FailureOr<SmallVector<AliasScopeAttr>> |
| 2426 | ModuleImport::matchAliasScopeAttrs(llvm::Value *value) { |
| 2427 | auto *nodeAsVal = cast<llvm::MetadataAsValue>(Val: value); |
| 2428 | auto *node = cast<llvm::MDNode>(Val: nodeAsVal->getMetadata()); |
| 2429 | return lookupAliasScopeAttrs(node); |
| 2430 | } |
| 2431 | |
| 2432 | Location ModuleImport::translateLoc(llvm::DILocation *loc) { |
| 2433 | return debugImporter->translateLoc(loc); |
| 2434 | } |
| 2435 | |
| 2436 | LogicalResult |
| 2437 | ModuleImport::convertBranchArgs(llvm::Instruction *branch, |
| 2438 | llvm::BasicBlock *target, |
| 2439 | SmallVectorImpl<Value> &blockArguments) { |
| 2440 | for (auto inst = target->begin(); isa<llvm::PHINode>(Val: inst); ++inst) { |
| 2441 | auto *phiInst = cast<llvm::PHINode>(Val: &*inst); |
| 2442 | llvm::Value *value = phiInst->getIncomingValueForBlock(BB: branch->getParent()); |
| 2443 | FailureOr<Value> converted = convertValue(value); |
| 2444 | if (failed(Result: converted)) |
| 2445 | return failure(); |
| 2446 | blockArguments.push_back(Elt: *converted); |
| 2447 | } |
| 2448 | return success(); |
| 2449 | } |
| 2450 | |
| 2451 | FailureOr<SmallVector<Value>> |
| 2452 | ModuleImport::convertCallOperands(llvm::CallBase *callInst, |
| 2453 | bool allowInlineAsm) { |
| 2454 | bool isInlineAsm = callInst->isInlineAsm(); |
| 2455 | if (isInlineAsm && !allowInlineAsm) |
| 2456 | return failure(); |
| 2457 | |
| 2458 | SmallVector<Value> operands; |
| 2459 | |
| 2460 | // Cannot use isIndirectCall() here because we need to handle Constant callees |
| 2461 | // that are not considered indirect calls by LLVM. However, in MLIR, they are |
| 2462 | // treated as indirect calls to constant operands that need to be converted. |
| 2463 | // Skip the callee operand if it's inline assembly, as it's handled separately |
| 2464 | // in InlineAsmOp. |
| 2465 | llvm::Value *calleeOperand = callInst->getCalledOperand(); |
| 2466 | if (!isa<llvm::Function, llvm::GlobalIFunc>(Val: calleeOperand) && !isInlineAsm) { |
| 2467 | FailureOr<Value> called = convertValue(value: calleeOperand); |
| 2468 | if (failed(Result: called)) |
| 2469 | return failure(); |
| 2470 | operands.push_back(Elt: *called); |
| 2471 | } |
| 2472 | |
| 2473 | SmallVector<llvm::Value *> args(callInst->args()); |
| 2474 | FailureOr<SmallVector<Value>> arguments = convertValues(values: args); |
| 2475 | if (failed(Result: arguments)) |
| 2476 | return failure(); |
| 2477 | |
| 2478 | llvm::append_range(C&: operands, R&: *arguments); |
| 2479 | return operands; |
| 2480 | } |
| 2481 | |
| 2482 | /// Checks if `callType` and `calleeType` are compatible and can be represented |
| 2483 | /// in MLIR. |
| 2484 | static LogicalResult |
| 2485 | checkFunctionTypeCompatibility(LLVMFunctionType callType, |
| 2486 | LLVMFunctionType calleeType) { |
| 2487 | if (callType.getReturnType() != calleeType.getReturnType()) |
| 2488 | return failure(); |
| 2489 | |
| 2490 | if (calleeType.isVarArg()) { |
| 2491 | // For variadic functions, the call can have more types than the callee |
| 2492 | // specifies. |
| 2493 | if (callType.getNumParams() < calleeType.getNumParams()) |
| 2494 | return failure(); |
| 2495 | } else { |
| 2496 | // For non-variadic functions, the number of parameters needs to be the |
| 2497 | // same. |
| 2498 | if (callType.getNumParams() != calleeType.getNumParams()) |
| 2499 | return failure(); |
| 2500 | } |
| 2501 | |
| 2502 | // Check that all operands match. |
| 2503 | for (auto [operandType, argumentType] : |
| 2504 | llvm::zip(t: callType.getParams(), u: calleeType.getParams())) |
| 2505 | if (operandType != argumentType) |
| 2506 | return failure(); |
| 2507 | |
| 2508 | return success(); |
| 2509 | } |
| 2510 | |
| 2511 | FailureOr<LLVMFunctionType> |
| 2512 | ModuleImport::convertFunctionType(llvm::CallBase *callInst, |
| 2513 | bool &isIncompatibleCall) { |
| 2514 | isIncompatibleCall = false; |
| 2515 | auto castOrFailure = [](Type convertedType) -> FailureOr<LLVMFunctionType> { |
| 2516 | auto funcTy = dyn_cast_or_null<LLVMFunctionType>(Val&: convertedType); |
| 2517 | if (!funcTy) |
| 2518 | return failure(); |
| 2519 | return funcTy; |
| 2520 | }; |
| 2521 | |
| 2522 | llvm::Value *calledOperand = callInst->getCalledOperand(); |
| 2523 | FailureOr<LLVMFunctionType> callType = |
| 2524 | castOrFailure(convertType(type: callInst->getFunctionType())); |
| 2525 | if (failed(Result: callType)) |
| 2526 | return failure(); |
| 2527 | auto *callee = dyn_cast<llvm::Function>(Val: calledOperand); |
| 2528 | |
| 2529 | llvm::FunctionType *origCalleeType = nullptr; |
| 2530 | if (callee) { |
| 2531 | origCalleeType = callee->getFunctionType(); |
| 2532 | } else if (auto *ifunc = dyn_cast<llvm::GlobalIFunc>(Val: calledOperand)) { |
| 2533 | origCalleeType = cast<llvm::FunctionType>(Val: ifunc->getValueType()); |
| 2534 | } |
| 2535 | |
| 2536 | // For indirect calls, return the type of the call itself. |
| 2537 | if (!origCalleeType) |
| 2538 | return callType; |
| 2539 | |
| 2540 | FailureOr<LLVMFunctionType> calleeType = |
| 2541 | castOrFailure(convertType(type: origCalleeType)); |
| 2542 | if (failed(Result: calleeType)) |
| 2543 | return failure(); |
| 2544 | |
| 2545 | // Compare the types and notify users via `isIncompatibleCall` if they are not |
| 2546 | // compatible. |
| 2547 | if (failed(Result: checkFunctionTypeCompatibility(callType: *callType, calleeType: *calleeType))) { |
| 2548 | isIncompatibleCall = true; |
| 2549 | Location loc = translateLoc(loc: callInst->getDebugLoc()); |
| 2550 | emitWarning(loc) << "incompatible call and callee types: " << *callType |
| 2551 | << " and " << *calleeType; |
| 2552 | return callType; |
| 2553 | } |
| 2554 | |
| 2555 | return calleeType; |
| 2556 | } |
| 2557 | |
| 2558 | FlatSymbolRefAttr ModuleImport::convertCalleeName(llvm::CallBase *callInst) { |
| 2559 | llvm::Value *calledOperand = callInst->getCalledOperand(); |
| 2560 | if (isa<llvm::Function, llvm::GlobalIFunc>(Val: calledOperand)) |
| 2561 | return SymbolRefAttr::get(ctx: context, value: calledOperand->getName()); |
| 2562 | return {}; |
| 2563 | } |
| 2564 | |
| 2565 | LogicalResult ModuleImport::convertIntrinsic(llvm::CallInst *inst) { |
| 2566 | if (succeeded(Result: iface.convertIntrinsic(builder, inst, moduleImport&: *this))) |
| 2567 | return success(); |
| 2568 | |
| 2569 | Location loc = translateLoc(loc: inst->getDebugLoc()); |
| 2570 | return emitError(loc) << "unhandled intrinsic: " << renderValue(value: *inst); |
| 2571 | } |
| 2572 | |
| 2573 | ArrayAttr |
| 2574 | ModuleImport::convertAsmInlineOperandAttrs(const llvm::CallBase &llvmCall) { |
| 2575 | const auto *ia = cast<llvm::InlineAsm>(Val: llvmCall.getCalledOperand()); |
| 2576 | unsigned argIdx = 0; |
| 2577 | SmallVector<mlir::Attribute> opAttrs; |
| 2578 | bool hasIndirect = false; |
| 2579 | |
| 2580 | for (const llvm::InlineAsm::ConstraintInfo &ci : ia->ParseConstraints()) { |
| 2581 | // Only deal with constraints that correspond to call arguments. |
| 2582 | if (ci.Type == llvm::InlineAsm::isLabel || !ci.hasArg()) |
| 2583 | continue; |
| 2584 | |
| 2585 | // Only increment `argIdx` in terms of constraints containing arguments, |
| 2586 | // which are guaranteed to happen in the same order of the call arguments. |
| 2587 | if (ci.isIndirect) { |
| 2588 | if (llvm::Type *paramEltType = llvmCall.getParamElementType(ArgNo: argIdx)) { |
| 2589 | SmallVector<mlir::NamedAttribute> attrs; |
| 2590 | attrs.push_back(Elt: builder.getNamedAttr( |
| 2591 | name: mlir::LLVM::InlineAsmOp::getElementTypeAttrName(), |
| 2592 | val: mlir::TypeAttr::get(type: convertType(type: paramEltType)))); |
| 2593 | opAttrs.push_back(Elt: builder.getDictionaryAttr(value: attrs)); |
| 2594 | hasIndirect = true; |
| 2595 | } |
| 2596 | } else { |
| 2597 | opAttrs.push_back(Elt: builder.getDictionaryAttr(value: {})); |
| 2598 | } |
| 2599 | argIdx++; |
| 2600 | } |
| 2601 | |
| 2602 | // Avoid emitting an array where all entries are empty dictionaries. |
| 2603 | return hasIndirect ? ArrayAttr::get(context: mlirModule->getContext(), value: opAttrs) |
| 2604 | : nullptr; |
| 2605 | } |
| 2606 | |
| 2607 | LogicalResult ModuleImport::convertInstruction(llvm::Instruction *inst) { |
| 2608 | // Convert all instructions that do not provide an MLIR builder. |
| 2609 | Location loc = translateLoc(loc: inst->getDebugLoc()); |
| 2610 | if (auto *brInst = dyn_cast<llvm::UncondBrInst>(Val: inst)) { |
| 2611 | llvm::BasicBlock *succ = brInst->getSuccessor(); |
| 2612 | SmallVector<Value> blockArgs; |
| 2613 | if (failed(Result: convertBranchArgs(branch: brInst, target: succ, blockArguments&: blockArgs))) |
| 2614 | return failure(); |
| 2615 | |
| 2616 | auto brOp = LLVM::BrOp::create(builder, location: loc, operands: blockArgs, dest: lookupBlock(block: succ)); |
| 2617 | mapNoResultOp(llvm: inst, mlir: brOp); |
| 2618 | return success(); |
| 2619 | } |
| 2620 | if (auto *brInst = dyn_cast<llvm::CondBrInst>(Val: inst)) { |
| 2621 | SmallVector<Block *> succBlocks; |
| 2622 | SmallVector<SmallVector<Value>> succBlockArgs; |
| 2623 | for (auto i : llvm::seq<unsigned>(Begin: 0, End: brInst->getNumSuccessors())) { |
| 2624 | llvm::BasicBlock *succ = brInst->getSuccessor(i); |
| 2625 | SmallVector<Value> blockArgs; |
| 2626 | if (failed(Result: convertBranchArgs(branch: brInst, target: succ, blockArguments&: blockArgs))) |
| 2627 | return failure(); |
| 2628 | succBlocks.push_back(Elt: lookupBlock(block: succ)); |
| 2629 | succBlockArgs.push_back(Elt: blockArgs); |
| 2630 | } |
| 2631 | |
| 2632 | FailureOr<Value> condition = convertValue(value: brInst->getCondition()); |
| 2633 | if (failed(Result: condition)) |
| 2634 | return failure(); |
| 2635 | auto condBrOp = LLVM::CondBrOp::create( |
| 2636 | builder, location: loc, condition: *condition, trueDest: succBlocks.front(), trueOperands: succBlockArgs.front(), |
| 2637 | falseDest: succBlocks.back(), falseOperands: succBlockArgs.back()); |
| 2638 | mapNoResultOp(llvm: inst, mlir: condBrOp); |
| 2639 | return success(); |
| 2640 | } |
| 2641 | if (inst->getOpcode() == llvm::Instruction::Switch) { |
| 2642 | auto *swInst = cast<llvm::SwitchInst>(Val: inst); |
| 2643 | // Process the condition value. |
| 2644 | FailureOr<Value> condition = convertValue(value: swInst->getCondition()); |
| 2645 | if (failed(Result: condition)) |
| 2646 | return failure(); |
| 2647 | SmallVector<Value> defaultBlockArgs; |
| 2648 | // Process the default case. |
| 2649 | llvm::BasicBlock *defaultBB = swInst->getDefaultDest(); |
| 2650 | if (failed(Result: convertBranchArgs(branch: swInst, target: defaultBB, blockArguments&: defaultBlockArgs))) |
| 2651 | return failure(); |
| 2652 | |
| 2653 | // Process the cases. |
| 2654 | unsigned numCases = swInst->getNumCases(); |
| 2655 | SmallVector<SmallVector<Value>> caseOperands(numCases); |
| 2656 | SmallVector<ValueRange> caseOperandRefs(numCases); |
| 2657 | SmallVector<APInt> caseValues(numCases); |
| 2658 | SmallVector<Block *> caseBlocks(numCases); |
| 2659 | for (const auto &it : llvm::enumerate(First: swInst->cases())) { |
| 2660 | const llvm::SwitchInst::CaseHandle &caseHandle = it.value(); |
| 2661 | llvm::BasicBlock *succBB = caseHandle.getCaseSuccessor(); |
| 2662 | if (failed(Result: convertBranchArgs(branch: swInst, target: succBB, blockArguments&: caseOperands[it.index()]))) |
| 2663 | return failure(); |
| 2664 | caseOperandRefs[it.index()] = caseOperands[it.index()]; |
| 2665 | caseValues[it.index()] = caseHandle.getCaseValue()->getValue(); |
| 2666 | caseBlocks[it.index()] = lookupBlock(block: succBB); |
| 2667 | } |
| 2668 | |
| 2669 | auto switchOp = SwitchOp::create(builder, location: loc, value: *condition, |
| 2670 | defaultDestination: lookupBlock(block: defaultBB), defaultOperands: defaultBlockArgs, |
| 2671 | caseValues, caseDestinations: caseBlocks, caseOperands: caseOperandRefs); |
| 2672 | mapNoResultOp(llvm: inst, mlir: switchOp); |
| 2673 | return success(); |
| 2674 | } |
| 2675 | if (inst->getOpcode() == llvm::Instruction::PHI) { |
| 2676 | Type type = convertType(type: inst->getType()); |
| 2677 | mapValue(llvm: inst, mlir: builder.getInsertionBlock()->addArgument( |
| 2678 | type, loc: translateLoc(loc: inst->getDebugLoc()))); |
| 2679 | return success(); |
| 2680 | } |
| 2681 | if (inst->getOpcode() == llvm::Instruction::Call) { |
| 2682 | auto *callInst = cast<llvm::CallInst>(Val: inst); |
| 2683 | llvm::Value *calledOperand = callInst->getCalledOperand(); |
| 2684 | |
| 2685 | FailureOr<SmallVector<Value>> operands = |
| 2686 | convertCallOperands(callInst, /*allowInlineAsm=*/true); |
| 2687 | if (failed(Result: operands)) |
| 2688 | return failure(); |
| 2689 | |
| 2690 | auto callOp = [&]() -> FailureOr<Operation *> { |
| 2691 | if (auto *asmI = dyn_cast<llvm::InlineAsm>(Val: calledOperand)) { |
| 2692 | Type resultTy = convertType(type: callInst->getType()); |
| 2693 | if (!resultTy) |
| 2694 | return failure(); |
| 2695 | ArrayAttr operandAttrs = convertAsmInlineOperandAttrs(llvmCall: *callInst); |
| 2696 | return InlineAsmOp::create( |
| 2697 | builder, location: loc, res: resultTy, operands: *operands, |
| 2698 | asm_string: builder.getStringAttr(bytes: asmI->getAsmString()), |
| 2699 | constraints: builder.getStringAttr(bytes: asmI->getConstraintString()), |
| 2700 | has_side_effects: asmI->hasSideEffects(), is_align_stack: asmI->isAlignStack(), |
| 2701 | tail_call_kind: convertTailCallKindFromLLVM(value: callInst->getTailCallKind()), |
| 2702 | convergent: callInst->hasFnAttr(Kind: llvm::Attribute::Convergent), |
| 2703 | asm_dialect: AsmDialectAttr::get( |
| 2704 | context: mlirModule.getContext(), |
| 2705 | val: convertAsmDialectFromLLVM(value: asmI->getDialect())), |
| 2706 | operand_attrs: operandAttrs) |
| 2707 | .getOperation(); |
| 2708 | } |
| 2709 | bool isIncompatibleCall; |
| 2710 | FailureOr<LLVMFunctionType> funcTy = |
| 2711 | convertFunctionType(callInst, isIncompatibleCall); |
| 2712 | if (failed(Result: funcTy)) |
| 2713 | return failure(); |
| 2714 | |
| 2715 | FlatSymbolRefAttr callee = nullptr; |
| 2716 | if (isIncompatibleCall) { |
| 2717 | // Use an indirect call (in order to represent valid and verifiable LLVM |
| 2718 | // IR). Build the indirect call by passing an empty `callee` operand and |
| 2719 | // insert into `operands` to include the indirect call target. |
| 2720 | FlatSymbolRefAttr calleeSym = convertCalleeName(callInst); |
| 2721 | Value indirectCallVal = LLVM::AddressOfOp::create( |
| 2722 | builder, location: loc, res: LLVM::LLVMPointerType::get(context), global_name: calleeSym); |
| 2723 | operands->insert(I: operands->begin(), Elt: indirectCallVal); |
| 2724 | } else { |
| 2725 | // Regular direct call using callee name. |
| 2726 | callee = convertCalleeName(callInst); |
| 2727 | } |
| 2728 | CallOp callOp = CallOp::create(builder, location: loc, calleeType: *funcTy, callee, args: *operands); |
| 2729 | |
| 2730 | if (failed(Result: convertCallAttributes(inst: callInst, op: callOp))) |
| 2731 | return failure(); |
| 2732 | |
| 2733 | // Handle parameter and result attributes unless it's an incompatible |
| 2734 | // call. |
| 2735 | if (!isIncompatibleCall) |
| 2736 | convertArgAndResultAttrs(call: callInst, attrsOp: callOp); |
| 2737 | return callOp.getOperation(); |
| 2738 | }(); |
| 2739 | |
| 2740 | if (failed(Result: callOp)) |
| 2741 | return failure(); |
| 2742 | |
| 2743 | if (!callInst->getType()->isVoidTy()) |
| 2744 | mapValue(llvm: inst, mlir: (*callOp)->getResult(idx: 0)); |
| 2745 | else |
| 2746 | mapNoResultOp(llvm: inst, mlir: *callOp); |
| 2747 | return success(); |
| 2748 | } |
| 2749 | if (inst->getOpcode() == llvm::Instruction::LandingPad) { |
| 2750 | auto *lpInst = cast<llvm::LandingPadInst>(Val: inst); |
| 2751 | |
| 2752 | SmallVector<Value> operands; |
| 2753 | operands.reserve(N: lpInst->getNumClauses()); |
| 2754 | for (auto i : llvm::seq<unsigned>(Begin: 0, End: lpInst->getNumClauses())) { |
| 2755 | FailureOr<Value> operand = convertValue(value: lpInst->getClause(Idx: i)); |
| 2756 | if (failed(Result: operand)) |
| 2757 | return failure(); |
| 2758 | operands.push_back(Elt: *operand); |
| 2759 | } |
| 2760 | |
| 2761 | Type type = convertType(type: lpInst->getType()); |
| 2762 | auto lpOp = |
| 2763 | LandingpadOp::create(builder, location: loc, res: type, cleanup: lpInst->isCleanup(), odsArg_0: operands); |
| 2764 | mapValue(llvm: inst, mlir: lpOp); |
| 2765 | return success(); |
| 2766 | } |
| 2767 | if (inst->getOpcode() == llvm::Instruction::Invoke) { |
| 2768 | auto *invokeInst = cast<llvm::InvokeInst>(Val: inst); |
| 2769 | |
| 2770 | if (invokeInst->isInlineAsm()) |
| 2771 | return emitError(loc) << "invoke of inline assembly is not supported" ; |
| 2772 | |
| 2773 | FailureOr<SmallVector<Value>> operands = convertCallOperands(callInst: invokeInst); |
| 2774 | if (failed(Result: operands)) |
| 2775 | return failure(); |
| 2776 | |
| 2777 | // Check whether the invoke result is an argument to the normal destination |
| 2778 | // block. |
| 2779 | bool invokeResultUsedInPhi = llvm::any_of( |
| 2780 | Range: invokeInst->getNormalDest()->phis(), P: [&](const llvm::PHINode &phi) { |
| 2781 | return phi.getIncomingValueForBlock(BB: invokeInst->getParent()) == |
| 2782 | invokeInst; |
| 2783 | }); |
| 2784 | |
| 2785 | Block *normalDest = lookupBlock(block: invokeInst->getNormalDest()); |
| 2786 | Block *directNormalDest = normalDest; |
| 2787 | if (invokeResultUsedInPhi) { |
| 2788 | // The invoke result cannot be an argument to the normal destination |
| 2789 | // block, as that would imply using the invoke operation result in its |
| 2790 | // definition, so we need to create a dummy block to serve as an |
| 2791 | // intermediate destination. |
| 2792 | OpBuilder::InsertionGuard g(builder); |
| 2793 | directNormalDest = builder.createBlock(insertBefore: normalDest); |
| 2794 | } |
| 2795 | |
| 2796 | SmallVector<Value> unwindArgs; |
| 2797 | if (failed(Result: convertBranchArgs(branch: invokeInst, target: invokeInst->getUnwindDest(), |
| 2798 | blockArguments&: unwindArgs))) |
| 2799 | return failure(); |
| 2800 | |
| 2801 | bool isIncompatibleInvoke; |
| 2802 | FailureOr<LLVMFunctionType> funcTy = |
| 2803 | convertFunctionType(callInst: invokeInst, isIncompatibleCall&: isIncompatibleInvoke); |
| 2804 | if (failed(Result: funcTy)) |
| 2805 | return failure(); |
| 2806 | |
| 2807 | FlatSymbolRefAttr calleeName = nullptr; |
| 2808 | if (isIncompatibleInvoke) { |
| 2809 | // Use an indirect invoke (in order to represent valid and verifiable LLVM |
| 2810 | // IR). Build the indirect invoke by passing an empty `callee` operand and |
| 2811 | // insert into `operands` to include the indirect invoke target. |
| 2812 | FlatSymbolRefAttr calleeSym = convertCalleeName(callInst: invokeInst); |
| 2813 | Value indirectInvokeVal = LLVM::AddressOfOp::create( |
| 2814 | builder, location: loc, res: LLVM::LLVMPointerType::get(context), global_name: calleeSym); |
| 2815 | operands->insert(I: operands->begin(), Elt: indirectInvokeVal); |
| 2816 | } else { |
| 2817 | // Regular direct invoke using callee name. |
| 2818 | calleeName = convertCalleeName(callInst: invokeInst); |
| 2819 | } |
| 2820 | // Create the invoke operation. Normal destination block arguments will be |
| 2821 | // added later on to handle the case in which the operation result is |
| 2822 | // included in this list. |
| 2823 | auto invokeOp = InvokeOp::create( |
| 2824 | builder, location: loc, calleeType: *funcTy, callee: calleeName, ops: *operands, normal: directNormalDest, |
| 2825 | normalOps: ValueRange(), unwind: lookupBlock(block: invokeInst->getUnwindDest()), unwindOps: unwindArgs); |
| 2826 | |
| 2827 | if (failed(Result: convertInvokeAttributes(inst: invokeInst, op: invokeOp))) |
| 2828 | return failure(); |
| 2829 | |
| 2830 | // Handle parameter and result attributes unless it's an incompatible |
| 2831 | // invoke. |
| 2832 | if (!isIncompatibleInvoke) |
| 2833 | convertArgAndResultAttrs(call: invokeInst, attrsOp: invokeOp); |
| 2834 | |
| 2835 | if (!invokeInst->getType()->isVoidTy()) |
| 2836 | mapValue(llvm: inst, mlir: invokeOp.getResults().front()); |
| 2837 | else |
| 2838 | mapNoResultOp(llvm: inst, mlir: invokeOp); |
| 2839 | |
| 2840 | SmallVector<Value> normalArgs; |
| 2841 | if (failed(Result: convertBranchArgs(branch: invokeInst, target: invokeInst->getNormalDest(), |
| 2842 | blockArguments&: normalArgs))) |
| 2843 | return failure(); |
| 2844 | |
| 2845 | if (invokeResultUsedInPhi) { |
| 2846 | // The dummy normal dest block will just host an unconditional branch |
| 2847 | // instruction to the normal destination block passing the required block |
| 2848 | // arguments (including the invoke operation's result). |
| 2849 | OpBuilder::InsertionGuard g(builder); |
| 2850 | builder.setInsertionPointToStart(directNormalDest); |
| 2851 | LLVM::BrOp::create(builder, location: loc, operands: normalArgs, dest: normalDest); |
| 2852 | } else { |
| 2853 | // If the invoke operation's result is not a block argument to the normal |
| 2854 | // destination block, just add the block arguments as usual. |
| 2855 | assert(llvm::none_of( |
| 2856 | normalArgs, |
| 2857 | [&](Value val) { return val.getDefiningOp() == invokeOp; }) && |
| 2858 | "An llvm.invoke operation cannot pass its result as a block " |
| 2859 | "argument." ); |
| 2860 | invokeOp.getNormalDestOperandsMutable().append(values: normalArgs); |
| 2861 | } |
| 2862 | |
| 2863 | return success(); |
| 2864 | } |
| 2865 | if (inst->getOpcode() == llvm::Instruction::GetElementPtr) { |
| 2866 | auto *gepInst = cast<llvm::GetElementPtrInst>(Val: inst); |
| 2867 | Type sourceElementType = convertType(type: gepInst->getSourceElementType()); |
| 2868 | FailureOr<Value> basePtr = convertValue(value: gepInst->getOperand(i_nocapture: 0)); |
| 2869 | if (failed(Result: basePtr)) |
| 2870 | return failure(); |
| 2871 | |
| 2872 | // Treat every indices as dynamic since GEPOp::build will refine those |
| 2873 | // indices into static attributes later. One small downside of this |
| 2874 | // approach is that many unused `llvm.mlir.constant` would be emitted |
| 2875 | // at first place. |
| 2876 | SmallVector<GEPArg> indices; |
| 2877 | for (llvm::Value *operand : llvm::drop_begin(RangeOrContainer: gepInst->operand_values())) { |
| 2878 | FailureOr<Value> index = convertValue(value: operand); |
| 2879 | if (failed(Result: index)) |
| 2880 | return failure(); |
| 2881 | indices.push_back(Elt: *index); |
| 2882 | } |
| 2883 | |
| 2884 | Type type = convertType(type: inst->getType()); |
| 2885 | auto gepOp = GEPOp::create( |
| 2886 | builder, location: loc, resultType: type, elementType: sourceElementType, basePtr: *basePtr, indices, |
| 2887 | noWrapFlags: static_cast<GEPNoWrapFlags>(gepInst->getNoWrapFlags().getRaw())); |
| 2888 | mapValue(llvm: inst, mlir: gepOp); |
| 2889 | return success(); |
| 2890 | } |
| 2891 | |
| 2892 | if (inst->getOpcode() == llvm::Instruction::IndirectBr) { |
| 2893 | auto *indBrInst = cast<llvm::IndirectBrInst>(Val: inst); |
| 2894 | |
| 2895 | FailureOr<Value> basePtr = convertValue(value: indBrInst->getAddress()); |
| 2896 | if (failed(Result: basePtr)) |
| 2897 | return failure(); |
| 2898 | |
| 2899 | SmallVector<Block *> succBlocks; |
| 2900 | SmallVector<SmallVector<Value>> succBlockArgs; |
| 2901 | for (auto i : llvm::seq<unsigned>(Begin: 0, End: indBrInst->getNumSuccessors())) { |
| 2902 | llvm::BasicBlock *succ = indBrInst->getSuccessor(i); |
| 2903 | SmallVector<Value> blockArgs; |
| 2904 | if (failed(Result: convertBranchArgs(branch: indBrInst, target: succ, blockArguments&: blockArgs))) |
| 2905 | return failure(); |
| 2906 | succBlocks.push_back(Elt: lookupBlock(block: succ)); |
| 2907 | succBlockArgs.push_back(Elt: blockArgs); |
| 2908 | } |
| 2909 | SmallVector<ValueRange> succBlockArgsRange = |
| 2910 | llvm::to_vector_of<ValueRange>(Range&: succBlockArgs); |
| 2911 | Location loc = translateLoc(loc: inst->getDebugLoc()); |
| 2912 | auto indBrOp = LLVM::IndirectBrOp::create(builder, location: loc, addr: *basePtr, |
| 2913 | succOperands: succBlockArgsRange, successors: succBlocks); |
| 2914 | |
| 2915 | mapNoResultOp(llvm: inst, mlir: indBrOp); |
| 2916 | return success(); |
| 2917 | } |
| 2918 | |
| 2919 | // Convert all instructions that have an mlirBuilder. |
| 2920 | if (succeeded(Result: convertInstructionImpl(odsBuilder&: builder, inst, moduleImport&: *this, iface))) |
| 2921 | return success(); |
| 2922 | |
| 2923 | return emitError(loc) << "unhandled instruction: " << renderValue(value: *inst); |
| 2924 | } |
| 2925 | |
| 2926 | LogicalResult ModuleImport::processInstruction(llvm::Instruction *inst) { |
| 2927 | // FIXME: Support uses of SubtargetData. |
| 2928 | // FIXME: Add support for call / operand attributes. |
| 2929 | // FIXME: Add support for the cleanupret, catchret, catchswitch, callbr, |
| 2930 | // vaarg, catchpad, cleanuppad instructions. |
| 2931 | |
| 2932 | // Convert LLVM intrinsics calls to MLIR intrinsics. |
| 2933 | if (auto *intrinsic = dyn_cast<llvm::IntrinsicInst>(Val: inst)) |
| 2934 | return convertIntrinsic(inst: intrinsic); |
| 2935 | |
| 2936 | // Process debug records attached to this instruction. Debug variable records |
| 2937 | // are stored for later processing after all SSA values are converted, while |
| 2938 | // debug label records can be converted immediately. |
| 2939 | if (inst->getDbgMarker()) { |
| 2940 | for (llvm::DbgRecord &dbgRecord : |
| 2941 | inst->getDbgMarker()->getDbgRecordRange()) { |
| 2942 | // Store debug variable records for later processing. |
| 2943 | if (auto *dbgVariableRecord = |
| 2944 | dyn_cast<llvm::DbgVariableRecord>(Val: &dbgRecord)) { |
| 2945 | addDebugRecord(dbgRecord: dbgVariableRecord); |
| 2946 | continue; |
| 2947 | } |
| 2948 | Location loc = translateLoc(loc: dbgRecord.getDebugLoc()); |
| 2949 | auto emitUnsupportedWarning = [&]() -> LogicalResult { |
| 2950 | if (!emitExpensiveWarnings) |
| 2951 | return success(); |
| 2952 | std::string options; |
| 2953 | llvm::raw_string_ostream optionsStream(options); |
| 2954 | dbgRecord.print(O&: optionsStream); |
| 2955 | emitWarning(loc) << "unhandled debug record " << optionsStream.str(); |
| 2956 | return success(); |
| 2957 | }; |
| 2958 | // Convert the debug label records in-place. |
| 2959 | if (auto *dbgLabelRecord = dyn_cast<llvm::DbgLabelRecord>(Val: &dbgRecord)) { |
| 2960 | DILabelAttr labelAttr = |
| 2961 | debugImporter->translate(node: dbgLabelRecord->getLabel()); |
| 2962 | if (!labelAttr) |
| 2963 | return emitUnsupportedWarning(); |
| 2964 | LLVM::DbgLabelOp::create(builder, location: loc, label: labelAttr); |
| 2965 | continue; |
| 2966 | } |
| 2967 | // Warn if an unsupported debug record is encountered. |
| 2968 | return emitUnsupportedWarning(); |
| 2969 | } |
| 2970 | } |
| 2971 | |
| 2972 | // Convert all remaining LLVM instructions to MLIR operations. |
| 2973 | return convertInstruction(inst); |
| 2974 | } |
| 2975 | |
| 2976 | FlatSymbolRefAttr ModuleImport::getPersonalityAsAttr(llvm::Function *f) { |
| 2977 | if (!f->hasPersonalityFn()) |
| 2978 | return nullptr; |
| 2979 | |
| 2980 | llvm::Constant *pf = f->getPersonalityFn(); |
| 2981 | |
| 2982 | // If it directly has a name, we can use it. |
| 2983 | if (pf->hasName()) |
| 2984 | return SymbolRefAttr::get(ctx: builder.getContext(), value: pf->getName()); |
| 2985 | |
| 2986 | // If it doesn't have a name, currently, only function pointers that are |
| 2987 | // bitcast to i8* are parsed. |
| 2988 | if (auto *ce = dyn_cast<llvm::ConstantExpr>(Val: pf)) { |
| 2989 | if (ce->getOpcode() == llvm::Instruction::BitCast && |
| 2990 | ce->getType() == llvm::PointerType::getUnqual(C&: f->getContext())) { |
| 2991 | if (auto *func = dyn_cast<llvm::Function>(Val: ce->getOperand(i_nocapture: 0))) |
| 2992 | return SymbolRefAttr::get(ctx: builder.getContext(), value: func->getName()); |
| 2993 | } |
| 2994 | } |
| 2995 | return FlatSymbolRefAttr(); |
| 2996 | } |
| 2997 | |
| 2998 | static void processMemoryEffects(llvm::Function *func, LLVMFuncOp funcOp) { |
| 2999 | llvm::MemoryEffects memEffects = func->getMemoryEffects(); |
| 3000 | |
| 3001 | auto othermem = convertModRefInfoFromLLVM( |
| 3002 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::Other)); |
| 3003 | auto argMem = convertModRefInfoFromLLVM( |
| 3004 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::ArgMem)); |
| 3005 | auto inaccessibleMem = convertModRefInfoFromLLVM( |
| 3006 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::InaccessibleMem)); |
| 3007 | auto errnoMem = convertModRefInfoFromLLVM( |
| 3008 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::ErrnoMem)); |
| 3009 | auto targetMem0 = convertModRefInfoFromLLVM( |
| 3010 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::TargetMem0)); |
| 3011 | auto targetMem1 = convertModRefInfoFromLLVM( |
| 3012 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::TargetMem1)); |
| 3013 | auto memAttr = |
| 3014 | MemoryEffectsAttr::get(context: funcOp.getContext(), other: othermem, argMem, |
| 3015 | inaccessibleMem, errnoMem, targetMem0, targetMem1); |
| 3016 | // Only set the attr when it does not match the default value. |
| 3017 | if (memAttr.isReadWrite()) |
| 3018 | return; |
| 3019 | funcOp.setMemoryEffectsAttr(memAttr); |
| 3020 | } |
| 3021 | |
| 3022 | static void processDenormalFPEnv(llvm::Function *func, LLVMFuncOp funcOp) { |
| 3023 | llvm::DenormalFPEnv denormalFpEnv = func->getDenormalFPEnv(); |
| 3024 | // Only set the attr when it does not match the default value. |
| 3025 | if (denormalFpEnv == llvm::DenormalFPEnv::getDefault()) |
| 3026 | return; |
| 3027 | |
| 3028 | llvm::DenormalMode defaultMode = denormalFpEnv.DefaultMode; |
| 3029 | llvm::DenormalMode floatMode = denormalFpEnv.F32Mode; |
| 3030 | |
| 3031 | auto denormalFpEnvAttr = DenormalFPEnvAttr::get( |
| 3032 | context: funcOp.getContext(), default_output_mode: convertDenormalModeKindFromLLVM(value: defaultMode.Output), |
| 3033 | default_input_mode: convertDenormalModeKindFromLLVM(value: defaultMode.Input), |
| 3034 | float_output_mode: convertDenormalModeKindFromLLVM(value: floatMode.Output), |
| 3035 | float_input_mode: convertDenormalModeKindFromLLVM(value: floatMode.Input)); |
| 3036 | funcOp.setDenormalFpenvAttr(denormalFpEnvAttr); |
| 3037 | } |
| 3038 | |
| 3039 | // List of LLVM IR attributes that map to an explicit attribute on the MLIR |
| 3040 | // LLVMFuncOp. |
| 3041 | static constexpr std::array kExplicitLLVMFuncOpAttributes{ |
| 3042 | StringLiteral("aarch64_in_za" ), |
| 3043 | StringLiteral("aarch64_inout_za" ), |
| 3044 | StringLiteral("aarch64_new_za" ), |
| 3045 | StringLiteral("aarch64_out_za" ), |
| 3046 | StringLiteral("aarch64_preserves_za" ), |
| 3047 | StringLiteral("aarch64_pstate_sm_body" ), |
| 3048 | StringLiteral("aarch64_pstate_sm_compatible" ), |
| 3049 | StringLiteral("aarch64_pstate_sm_enabled" ), |
| 3050 | StringLiteral("allocsize" ), |
| 3051 | StringLiteral("alwaysinline" ), |
| 3052 | StringLiteral("cold" ), |
| 3053 | StringLiteral("convergent" ), |
| 3054 | StringLiteral("disable-tail-calls" ), |
| 3055 | StringLiteral("fp-contract" ), |
| 3056 | StringLiteral("frame-pointer" ), |
| 3057 | StringLiteral("hot" ), |
| 3058 | StringLiteral("inlinehint" ), |
| 3059 | StringLiteral("instrument-function-entry" ), |
| 3060 | StringLiteral("instrument-function-exit" ), |
| 3061 | StringLiteral("modular-format" ), |
| 3062 | StringLiteral("memory" ), |
| 3063 | StringLiteral("minsize" ), |
| 3064 | StringLiteral("no_caller_saved_registers" ), |
| 3065 | StringLiteral("no-signed-zeros-fp-math" ), |
| 3066 | StringLiteral("no-builtins" ), |
| 3067 | StringLiteral("nocallback" ), |
| 3068 | StringLiteral("noduplicate" ), |
| 3069 | StringLiteral("noinline" ), |
| 3070 | StringLiteral("noreturn" ), |
| 3071 | StringLiteral("nounwind" ), |
| 3072 | StringLiteral("optnone" ), |
| 3073 | StringLiteral("optsize" ), |
| 3074 | StringLiteral("returns_twice" ), |
| 3075 | StringLiteral("save-reg-params" ), |
| 3076 | StringLiteral("target-features" ), |
| 3077 | StringLiteral("trap-func-name" ), |
| 3078 | StringLiteral("sample-profile-suffix-elision-policy" ), |
| 3079 | StringLiteral("tune-cpu" ), |
| 3080 | StringLiteral("uniform-work-group-size" ), |
| 3081 | StringLiteral("uwtable" ), |
| 3082 | StringLiteral("vscale_range" ), |
| 3083 | StringLiteral("willreturn" ), |
| 3084 | StringLiteral("zero-call-used-regs" ), |
| 3085 | StringLiteral("denormal_fpenv" ), |
| 3086 | }; |
| 3087 | |
| 3088 | // List of LLVM IR attributes that are handled by prefix to map onto an MLIR |
| 3089 | // LLVMFuncOp. |
| 3090 | static constexpr std::array kExplicitLLVMFuncOpAttributePrefixes{ |
| 3091 | StringLiteral("no-builtin-" ), |
| 3092 | }; |
| 3093 | |
| 3094 | template <typename OpTy> |
| 3095 | static void convertNoBuiltinAttrs(MLIRContext *ctx, |
| 3096 | const llvm::AttributeSet &attrs, |
| 3097 | OpTy target) { |
| 3098 | // 'no-builtins' is the complete collection, and overrides all the rest. |
| 3099 | if (attrs.hasAttribute(Kind: "no-builtins" )) { |
| 3100 | target.setNobuiltinsAttr(ArrayAttr::get(context: ctx, value: {})); |
| 3101 | return; |
| 3102 | } |
| 3103 | |
| 3104 | llvm::SetVector<Attribute> nbAttrs; |
| 3105 | for (llvm::Attribute attr : attrs) { |
| 3106 | // Attributes that are part of llvm directly (that is, have an AttributeKind |
| 3107 | // in the enum) shouldn't be checked. |
| 3108 | if (attr.hasKindAsEnum()) |
| 3109 | continue; |
| 3110 | |
| 3111 | StringRef val = attr.getKindAsString(); |
| 3112 | |
| 3113 | if (val.starts_with(Prefix: "no-builtin-" )) |
| 3114 | nbAttrs.insert( |
| 3115 | X: StringAttr::get(context: ctx, bytes: val.drop_front(N: sizeof("no-builtin-" ) - 1))); |
| 3116 | } |
| 3117 | |
| 3118 | if (!nbAttrs.empty()) |
| 3119 | target.setNobuiltinsAttr(ArrayAttr::get(context: ctx, value: nbAttrs.getArrayRef())); |
| 3120 | } |
| 3121 | |
| 3122 | template <typename OpTy> |
| 3123 | static void convertAllocsizeAttr(MLIRContext *ctx, |
| 3124 | const llvm::AttributeSet &attrs, OpTy target) { |
| 3125 | llvm::Attribute attr = attrs.getAttribute(Kind: llvm::Attribute::AllocSize); |
| 3126 | if (!attr.isValid()) |
| 3127 | return; |
| 3128 | |
| 3129 | auto [elemSize, numElems] = attr.getAllocSizeArgs(); |
| 3130 | if (numElems) { |
| 3131 | target.setAllocsizeAttr( |
| 3132 | DenseI32ArrayAttr::get(context: ctx, content: {static_cast<int32_t>(elemSize), |
| 3133 | static_cast<int32_t>(*numElems)})); |
| 3134 | } else { |
| 3135 | target.setAllocsizeAttr( |
| 3136 | DenseI32ArrayAttr::get(context: ctx, content: {static_cast<int32_t>(elemSize)})); |
| 3137 | } |
| 3138 | } |
| 3139 | |
| 3140 | /// Converts LLVM attributes from `func` into MLIR attributes and adds them |
| 3141 | /// to `funcOp` as passthrough attributes, skipping those listed in |
| 3142 | /// `kExplicitLLVMFuncAttributes`. |
| 3143 | static void processPassthroughAttrs(llvm::Function *func, LLVMFuncOp funcOp) { |
| 3144 | llvm::AttributeSet funcAttrs = func->getAttributes().getAttributes( |
| 3145 | Index: llvm::AttributeList::AttrIndex::FunctionIndex); |
| 3146 | ArrayAttr passthroughAttr = convertLLVMAttributesToMLIR( |
| 3147 | loc: funcOp.getLoc(), context: funcOp.getContext(), attributes: funcAttrs, |
| 3148 | attributesToSkip: kExplicitLLVMFuncOpAttributes, attributePrefixesToSkip: kExplicitLLVMFuncOpAttributePrefixes); |
| 3149 | if (!passthroughAttr.empty()) |
| 3150 | funcOp.setPassthroughAttr(passthroughAttr); |
| 3151 | } |
| 3152 | |
| 3153 | void ModuleImport::processFunctionAttributes(llvm::Function *func, |
| 3154 | LLVMFuncOp funcOp) { |
| 3155 | processMemoryEffects(func, funcOp); |
| 3156 | processDenormalFPEnv(func, funcOp); |
| 3157 | processPassthroughAttrs(func, funcOp); |
| 3158 | |
| 3159 | if (func->hasFnAttribute(Kind: llvm::Attribute::NoInline)) |
| 3160 | funcOp.setNoInline(true); |
| 3161 | if (func->hasFnAttribute(Kind: llvm::Attribute::AlwaysInline)) |
| 3162 | funcOp.setAlwaysInline(true); |
| 3163 | if (func->hasFnAttribute(Kind: llvm::Attribute::InlineHint)) |
| 3164 | funcOp.setInlineHint(true); |
| 3165 | if (func->hasFnAttribute(Kind: llvm::Attribute::OptimizeNone)) |
| 3166 | funcOp.setOptimizeNone(true); |
| 3167 | if (func->hasFnAttribute(Kind: llvm::Attribute::Convergent)) |
| 3168 | funcOp.setConvergent(true); |
| 3169 | if (func->hasFnAttribute(Kind: llvm::Attribute::NoUnwind)) |
| 3170 | funcOp.setNoUnwind(true); |
| 3171 | if (func->hasFnAttribute(Kind: llvm::Attribute::WillReturn)) |
| 3172 | funcOp.setWillReturn(true); |
| 3173 | if (func->hasFnAttribute(Kind: llvm::Attribute::NoReturn)) |
| 3174 | funcOp.setNoreturn(true); |
| 3175 | if (func->hasFnAttribute(Kind: llvm::Attribute::OptimizeForSize)) |
| 3176 | funcOp.setOptsize(true); |
| 3177 | if (func->hasFnAttribute(Kind: "save-reg-params" )) |
| 3178 | funcOp.setSaveRegParams(true); |
| 3179 | if (func->hasFnAttribute(Kind: "uniform-work-group-size" )) |
| 3180 | funcOp.setUniformWorkGroupSize(true); |
| 3181 | if (func->hasFnAttribute(Kind: llvm::Attribute::MinSize)) |
| 3182 | funcOp.setMinsize(true); |
| 3183 | if (func->hasFnAttribute(Kind: llvm::Attribute::ReturnsTwice)) |
| 3184 | funcOp.setReturnsTwice(true); |
| 3185 | if (func->hasFnAttribute(Kind: llvm::Attribute::Cold)) |
| 3186 | funcOp.setCold(true); |
| 3187 | if (func->hasFnAttribute(Kind: llvm::Attribute::Hot)) |
| 3188 | funcOp.setHot(true); |
| 3189 | if (func->hasFnAttribute(Kind: llvm::Attribute::NoDuplicate)) |
| 3190 | funcOp.setNoduplicate(true); |
| 3191 | if (func->hasFnAttribute(Kind: "no_caller_saved_registers" )) |
| 3192 | funcOp.setNoCallerSavedRegisters(true); |
| 3193 | if (func->hasFnAttribute(Kind: llvm::Attribute::NoCallback)) |
| 3194 | funcOp.setNocallback(true); |
| 3195 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "modular-format" ); |
| 3196 | attr.isStringAttribute()) |
| 3197 | funcOp.setModularFormat(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3198 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "zero-call-used-regs" ); |
| 3199 | attr.isStringAttribute()) |
| 3200 | funcOp.setZeroCallUsedRegsAttr( |
| 3201 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3202 | |
| 3203 | if (func->hasFnAttribute(Kind: "aarch64_pstate_sm_enabled" )) |
| 3204 | funcOp.setArmStreaming(true); |
| 3205 | else if (func->hasFnAttribute(Kind: "aarch64_pstate_sm_body" )) |
| 3206 | funcOp.setArmLocallyStreaming(true); |
| 3207 | else if (func->hasFnAttribute(Kind: "aarch64_pstate_sm_compatible" )) |
| 3208 | funcOp.setArmStreamingCompatible(true); |
| 3209 | |
| 3210 | if (func->hasFnAttribute(Kind: "aarch64_new_za" )) |
| 3211 | funcOp.setArmNewZa(true); |
| 3212 | else if (func->hasFnAttribute(Kind: "aarch64_in_za" )) |
| 3213 | funcOp.setArmInZa(true); |
| 3214 | else if (func->hasFnAttribute(Kind: "aarch64_out_za" )) |
| 3215 | funcOp.setArmOutZa(true); |
| 3216 | else if (func->hasFnAttribute(Kind: "aarch64_inout_za" )) |
| 3217 | funcOp.setArmInoutZa(true); |
| 3218 | else if (func->hasFnAttribute(Kind: "aarch64_preserves_za" )) |
| 3219 | funcOp.setArmPreservesZa(true); |
| 3220 | |
| 3221 | convertNoBuiltinAttrs(ctx: context, attrs: func->getAttributes().getFnAttrs(), target: funcOp); |
| 3222 | convertAllocsizeAttr(ctx: context, attrs: func->getAttributes().getFnAttrs(), target: funcOp); |
| 3223 | |
| 3224 | llvm::Attribute attr = func->getFnAttribute(Kind: llvm::Attribute::VScaleRange); |
| 3225 | if (attr.isValid()) { |
| 3226 | MLIRContext *context = funcOp.getContext(); |
| 3227 | auto intTy = IntegerType::get(context, width: 32); |
| 3228 | funcOp.setVscaleRangeAttr(LLVM::VScaleRangeAttr::get( |
| 3229 | context, minRange: IntegerAttr::get(type: intTy, value: attr.getVScaleRangeMin()), |
| 3230 | maxRange: IntegerAttr::get(type: intTy, value: attr.getVScaleRangeMax().value_or(u: 0)))); |
| 3231 | } |
| 3232 | |
| 3233 | // Process frame-pointer attribute. |
| 3234 | if (func->hasFnAttribute(Kind: "frame-pointer" )) { |
| 3235 | StringRef stringRefFramePointerKind = |
| 3236 | func->getFnAttribute(Kind: "frame-pointer" ).getValueAsString(); |
| 3237 | funcOp.setFramePointerAttr(LLVM::FramePointerKindAttr::get( |
| 3238 | context: funcOp.getContext(), framePointerKind: LLVM::framePointerKind::symbolizeFramePointerKind( |
| 3239 | stringRefFramePointerKind) |
| 3240 | .value())); |
| 3241 | } |
| 3242 | |
| 3243 | if (func->hasFnAttribute(Kind: "use-sample-profile" )) |
| 3244 | funcOp.setUseSampleProfile(true); |
| 3245 | |
| 3246 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "disable-tail-calls" ); |
| 3247 | attr.isStringAttribute()) { |
| 3248 | StringRef val = attr.getValueAsString(); |
| 3249 | if (val == "true" ) |
| 3250 | funcOp.setDisableTailCalls(true); |
| 3251 | else if (val != "false" ) |
| 3252 | emitError(loc: funcOp.getLoc()) |
| 3253 | << "unknown value '" << val << "' for 'disable-tail-calls' attribute" ; |
| 3254 | } |
| 3255 | |
| 3256 | if (llvm::Attribute attr = |
| 3257 | func->getFnAttribute(Kind: "sample-profile-suffix-elision-policy" ); |
| 3258 | attr.isStringAttribute()) |
| 3259 | funcOp.setSampleProfileSuffixElisionPolicy( |
| 3260 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3261 | |
| 3262 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "target-cpu" ); |
| 3263 | attr.isStringAttribute()) |
| 3264 | funcOp.setTargetCpuAttr(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3265 | |
| 3266 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "tune-cpu" ); |
| 3267 | attr.isStringAttribute()) |
| 3268 | funcOp.setTuneCpuAttr(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3269 | |
| 3270 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "target-features" ); |
| 3271 | attr.isStringAttribute()) |
| 3272 | funcOp.setTargetFeaturesAttr( |
| 3273 | LLVM::TargetFeaturesAttr::get(context, features: attr.getValueAsString())); |
| 3274 | |
| 3275 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "reciprocal-estimates" ); |
| 3276 | attr.isStringAttribute()) |
| 3277 | funcOp.setReciprocalEstimatesAttr( |
| 3278 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3279 | |
| 3280 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "prefer-vector-width" ); |
| 3281 | attr.isStringAttribute()) |
| 3282 | funcOp.setPreferVectorWidth(attr.getValueAsString()); |
| 3283 | |
| 3284 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "instrument-function-entry" ); |
| 3285 | attr.isStringAttribute()) |
| 3286 | funcOp.setInstrumentFunctionEntry( |
| 3287 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3288 | |
| 3289 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "instrument-function-exit" ); |
| 3290 | attr.isStringAttribute()) |
| 3291 | funcOp.setInstrumentFunctionExit( |
| 3292 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3293 | |
| 3294 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "no-signed-zeros-fp-math" ); |
| 3295 | attr.isStringAttribute()) |
| 3296 | funcOp.setNoSignedZerosFpMath(attr.getValueAsBool()); |
| 3297 | |
| 3298 | if (llvm::Attribute attr = func->getFnAttribute(Kind: "fp-contract" ); |
| 3299 | attr.isStringAttribute()) |
| 3300 | funcOp.setFpContractAttr(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3301 | |
| 3302 | if (func->hasUWTable()) { |
| 3303 | ::llvm::UWTableKind uwtableKind = func->getUWTableKind(); |
| 3304 | funcOp.setUwtableKindAttr(LLVM::UWTableKindAttr::get( |
| 3305 | context: funcOp.getContext(), uwtableKind: convertUWTableKindFromLLVM(value: uwtableKind))); |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | DictionaryAttr |
| 3310 | ModuleImport::convertArgOrResultAttrSet(llvm::AttributeSet llvmAttrSet) { |
| 3311 | SmallVector<NamedAttribute> paramAttrs; |
| 3312 | for (auto [llvmKind, mlirName] : getAttrKindToNameMapping()) { |
| 3313 | auto llvmAttr = llvmAttrSet.getAttribute(Kind: llvmKind); |
| 3314 | // Skip attributes that are not attached. |
| 3315 | if (!llvmAttr.isValid()) |
| 3316 | continue; |
| 3317 | |
| 3318 | // TODO: Import captures(none) as a nocapture unit attribute until the |
| 3319 | // LLVM dialect switches to the captures representation. |
| 3320 | if (llvmAttr.hasKindAsEnum() && |
| 3321 | llvmAttr.getKindAsEnum() == llvm::Attribute::Captures) { |
| 3322 | if (llvm::capturesNothing(CC: llvmAttr.getCaptureInfo())) |
| 3323 | paramAttrs.push_back( |
| 3324 | Elt: builder.getNamedAttr(name: mlirName, val: builder.getUnitAttr())); |
| 3325 | continue; |
| 3326 | } |
| 3327 | |
| 3328 | Attribute mlirAttr; |
| 3329 | if (llvmAttr.isTypeAttribute()) |
| 3330 | mlirAttr = TypeAttr::get(type: convertType(type: llvmAttr.getValueAsType())); |
| 3331 | else if (llvmAttr.isIntAttribute()) |
| 3332 | mlirAttr = builder.getI64IntegerAttr(value: llvmAttr.getValueAsInt()); |
| 3333 | else if (llvmAttr.isEnumAttribute()) |
| 3334 | mlirAttr = builder.getUnitAttr(); |
| 3335 | else if (llvmAttr.isConstantRangeAttribute()) { |
| 3336 | const llvm::ConstantRange &value = llvmAttr.getValueAsConstantRange(); |
| 3337 | mlirAttr = builder.getAttr<LLVM::ConstantRangeAttr>(args: value.getLower(), |
| 3338 | args: value.getUpper()); |
| 3339 | } else { |
| 3340 | llvm_unreachable("unexpected parameter attribute kind" ); |
| 3341 | } |
| 3342 | paramAttrs.push_back(Elt: builder.getNamedAttr(name: mlirName, val: mlirAttr)); |
| 3343 | } |
| 3344 | |
| 3345 | return builder.getDictionaryAttr(value: paramAttrs); |
| 3346 | } |
| 3347 | |
| 3348 | void ModuleImport::convertArgAndResultAttrs(llvm::Function *func, |
| 3349 | LLVMFuncOp funcOp) { |
| 3350 | auto llvmAttrs = func->getAttributes(); |
| 3351 | for (size_t i = 0, e = funcOp.getNumArguments(); i < e; ++i) { |
| 3352 | llvm::AttributeSet llvmArgAttrs = llvmAttrs.getParamAttrs(ArgNo: i); |
| 3353 | funcOp.setArgAttrs(index: i, attributes: convertArgOrResultAttrSet(llvmAttrSet: llvmArgAttrs)); |
| 3354 | } |
| 3355 | // Convert the result attributes and attach them wrapped in an ArrayAttribute |
| 3356 | // to the funcOp. |
| 3357 | llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs(); |
| 3358 | if (!llvmResAttr.hasAttributes()) |
| 3359 | return; |
| 3360 | funcOp.setResAttrsAttr( |
| 3361 | builder.getArrayAttr(value: {convertArgOrResultAttrSet(llvmAttrSet: llvmResAttr)})); |
| 3362 | } |
| 3363 | |
| 3364 | void ModuleImport::convertArgAndResultAttrs( |
| 3365 | llvm::CallBase *call, ArgAndResultAttrsOpInterface attrsOp, |
| 3366 | ArrayRef<unsigned> immArgPositions) { |
| 3367 | // Compute the set of immediate argument positions. |
| 3368 | llvm::SmallDenseSet<unsigned> immArgPositionsSet(immArgPositions.begin(), |
| 3369 | immArgPositions.end()); |
| 3370 | // Convert the argument attributes and filter out immediate arguments. |
| 3371 | llvm::AttributeList llvmAttrs = call->getAttributes(); |
| 3372 | SmallVector<llvm::AttributeSet> ; |
| 3373 | bool anyArgAttrs = false; |
| 3374 | for (size_t i = 0, e = call->arg_size(); i < e; ++i) { |
| 3375 | // Skip immediate arguments. |
| 3376 | if (immArgPositionsSet.contains(V: i)) |
| 3377 | continue; |
| 3378 | llvmArgAttrsSet.emplace_back(Args: llvmAttrs.getParamAttrs(ArgNo: i)); |
| 3379 | if (llvmArgAttrsSet.back().hasAttributes()) |
| 3380 | anyArgAttrs = true; |
| 3381 | } |
| 3382 | auto getArrayAttr = [&](ArrayRef<DictionaryAttr> dictAttrs) { |
| 3383 | SmallVector<Attribute> attrs; |
| 3384 | for (auto &dict : dictAttrs) |
| 3385 | attrs.push_back(Elt: dict ? dict : builder.getDictionaryAttr(value: {})); |
| 3386 | return builder.getArrayAttr(value: attrs); |
| 3387 | }; |
| 3388 | if (anyArgAttrs) { |
| 3389 | SmallVector<DictionaryAttr> argAttrs; |
| 3390 | for (auto &llvmArgAttrs : llvmArgAttrsSet) |
| 3391 | argAttrs.emplace_back(Args: convertArgOrResultAttrSet(llvmAttrSet: llvmArgAttrs)); |
| 3392 | attrsOp.setArgAttrsAttr(getArrayAttr(argAttrs)); |
| 3393 | } |
| 3394 | |
| 3395 | // Convert the result attributes. |
| 3396 | llvm::AttributeSet llvmResAttr = llvmAttrs.getRetAttrs(); |
| 3397 | if (!llvmResAttr.hasAttributes()) |
| 3398 | return; |
| 3399 | DictionaryAttr resAttrs = convertArgOrResultAttrSet(llvmAttrSet: llvmResAttr); |
| 3400 | attrsOp.setResAttrsAttr(getArrayAttr({resAttrs})); |
| 3401 | } |
| 3402 | |
| 3403 | template <typename Op> |
| 3404 | static LogicalResult convertCallBaseAttributes(llvm::CallBase *inst, Op op) { |
| 3405 | op.setCConv(convertCConvFromLLVM(value: inst->getCallingConv())); |
| 3406 | return success(); |
| 3407 | } |
| 3408 | |
| 3409 | LogicalResult ModuleImport::convertInvokeAttributes(llvm::InvokeInst *inst, |
| 3410 | InvokeOp op) { |
| 3411 | llvm::AttributeList invokeAttrs = inst->getAttributes(); |
| 3412 | op.setUniformWorkGroupSize( |
| 3413 | invokeAttrs.getFnAttr(Kind: "uniform-work-group-size" ).isValid()); |
| 3414 | return convertCallBaseAttributes(inst, op); |
| 3415 | } |
| 3416 | |
| 3417 | LogicalResult ModuleImport::convertCallAttributes(llvm::CallInst *inst, |
| 3418 | CallOp op) { |
| 3419 | setFastmathFlagsAttr(inst, op: op.getOperation()); |
| 3420 | // Query the attributes directly instead of using `inst->getFnAttr(Kind)`, the |
| 3421 | // latter does additional lookup to the parent and inherits, changing the |
| 3422 | // semantics too early. |
| 3423 | llvm::AttributeList callAttrs = inst->getAttributes(); |
| 3424 | |
| 3425 | op.setTailCallKind(convertTailCallKindFromLLVM(value: inst->getTailCallKind())); |
| 3426 | op.setConvergent(callAttrs.getFnAttr(Kind: llvm::Attribute::Convergent).isValid()); |
| 3427 | op.setNoUnwind(callAttrs.getFnAttr(Kind: llvm::Attribute::NoUnwind).isValid()); |
| 3428 | op.setWillReturn(callAttrs.getFnAttr(Kind: llvm::Attribute::WillReturn).isValid()); |
| 3429 | op.setNoreturn(callAttrs.getFnAttr(Kind: llvm::Attribute::NoReturn).isValid()); |
| 3430 | op.setOptsize( |
| 3431 | callAttrs.getFnAttr(Kind: llvm::Attribute::OptimizeForSize).isValid()); |
| 3432 | op.setSaveRegParams(callAttrs.getFnAttr(Kind: "save-reg-params" ).isValid()); |
| 3433 | op.setUniformWorkGroupSize( |
| 3434 | callAttrs.getFnAttr(Kind: "uniform-work-group-size" ).isValid()); |
| 3435 | op.setBuiltin(callAttrs.getFnAttr(Kind: llvm::Attribute::Builtin).isValid()); |
| 3436 | op.setNobuiltin(callAttrs.getFnAttr(Kind: llvm::Attribute::NoBuiltin).isValid()); |
| 3437 | op.setMinsize(callAttrs.getFnAttr(Kind: llvm::Attribute::MinSize).isValid()); |
| 3438 | |
| 3439 | op.setReturnsTwice( |
| 3440 | callAttrs.getFnAttr(Kind: llvm::Attribute::ReturnsTwice).isValid()); |
| 3441 | op.setHot(callAttrs.getFnAttr(Kind: llvm::Attribute::Hot).isValid()); |
| 3442 | op.setCold(callAttrs.getFnAttr(Kind: llvm::Attribute::Cold).isValid()); |
| 3443 | op.setNoduplicate( |
| 3444 | callAttrs.getFnAttr(Kind: llvm::Attribute::NoDuplicate).isValid()); |
| 3445 | op.setNoCallerSavedRegisters( |
| 3446 | callAttrs.getFnAttr(Kind: "no_caller_saved_registers" ).isValid()); |
| 3447 | op.setNocallback(callAttrs.getFnAttr(Kind: llvm::Attribute::NoCallback).isValid()); |
| 3448 | |
| 3449 | if (llvm::Attribute attr = callAttrs.getFnAttr(Kind: "modular-format" ); |
| 3450 | attr.isStringAttribute()) |
| 3451 | op.setModularFormat(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3452 | if (llvm::Attribute attr = callAttrs.getFnAttr(Kind: "zero-call-used-regs" ); |
| 3453 | attr.isStringAttribute()) |
| 3454 | op.setZeroCallUsedRegsAttr( |
| 3455 | StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3456 | if (llvm::Attribute attr = callAttrs.getFnAttr(Kind: "trap-func-name" ); |
| 3457 | attr.isStringAttribute()) |
| 3458 | op.setTrapFuncNameAttr(StringAttr::get(context, bytes: attr.getValueAsString())); |
| 3459 | op.setNoInline(callAttrs.getFnAttr(Kind: llvm::Attribute::NoInline).isValid()); |
| 3460 | op.setAlwaysInline( |
| 3461 | callAttrs.getFnAttr(Kind: llvm::Attribute::AlwaysInline).isValid()); |
| 3462 | op.setInlineHint(callAttrs.getFnAttr(Kind: llvm::Attribute::InlineHint).isValid()); |
| 3463 | |
| 3464 | llvm::MemoryEffects memEffects = inst->getMemoryEffects(); |
| 3465 | ModRefInfo othermem = convertModRefInfoFromLLVM( |
| 3466 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::Other)); |
| 3467 | ModRefInfo argMem = convertModRefInfoFromLLVM( |
| 3468 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::ArgMem)); |
| 3469 | ModRefInfo inaccessibleMem = convertModRefInfoFromLLVM( |
| 3470 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::InaccessibleMem)); |
| 3471 | ModRefInfo errnoMem = convertModRefInfoFromLLVM( |
| 3472 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::ErrnoMem)); |
| 3473 | ModRefInfo targetMem0 = convertModRefInfoFromLLVM( |
| 3474 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::TargetMem0)); |
| 3475 | ModRefInfo targetMem1 = convertModRefInfoFromLLVM( |
| 3476 | value: memEffects.getModRef(Loc: llvm::MemoryEffects::Location::TargetMem1)); |
| 3477 | auto memAttr = |
| 3478 | MemoryEffectsAttr::get(context: op.getContext(), other: othermem, argMem, inaccessibleMem, |
| 3479 | errnoMem, targetMem0, targetMem1); |
| 3480 | // Only set the attribute when it does not match the default value. |
| 3481 | if (!memAttr.isReadWrite()) |
| 3482 | op.setMemoryEffectsAttr(memAttr); |
| 3483 | |
| 3484 | convertNoBuiltinAttrs(ctx: op.getContext(), attrs: callAttrs.getFnAttrs(), target: op); |
| 3485 | convertAllocsizeAttr(ctx: op.getContext(), attrs: callAttrs.getFnAttrs(), target: op); |
| 3486 | |
| 3487 | return convertCallBaseAttributes(inst, op); |
| 3488 | } |
| 3489 | |
| 3490 | LogicalResult ModuleImport::processFunction(llvm::Function *func) { |
| 3491 | clearRegionState(); |
| 3492 | |
| 3493 | auto functionType = |
| 3494 | dyn_cast<LLVMFunctionType>(Val: convertType(type: func->getFunctionType())); |
| 3495 | if (func->isIntrinsic() && |
| 3496 | iface.isConvertibleIntrinsic(id: func->getIntrinsicID())) |
| 3497 | return success(); |
| 3498 | |
| 3499 | bool dsoLocal = func->isDSOLocal(); |
| 3500 | CConv cconv = convertCConvFromLLVM(value: func->getCallingConv()); |
| 3501 | |
| 3502 | // Insert the function at the end of the module. |
| 3503 | OpBuilder::InsertionGuard guard(builder); |
| 3504 | builder.setInsertionPointToEnd(mlirModule.getBody()); |
| 3505 | |
| 3506 | Location loc = debugImporter->translateFuncLocation(func); |
| 3507 | LLVMFuncOp funcOp = LLVMFuncOp::create( |
| 3508 | builder, location: loc, name: func->getName(), type: functionType, |
| 3509 | linkage: convertLinkageFromLLVM(value: func->getLinkage()), dsoLocal, cconv); |
| 3510 | |
| 3511 | convertArgAndResultAttrs(func, funcOp); |
| 3512 | |
| 3513 | if (FlatSymbolRefAttr personality = getPersonalityAsAttr(f: func)) |
| 3514 | funcOp.setPersonalityAttr(personality); |
| 3515 | else if (func->hasPersonalityFn()) |
| 3516 | emitWarning(loc: funcOp.getLoc(), message: "could not deduce personality, skipping it" ); |
| 3517 | |
| 3518 | if (func->hasGC()) |
| 3519 | funcOp.setGarbageCollector(StringRef(func->getGC())); |
| 3520 | |
| 3521 | if (func->hasAtLeastLocalUnnamedAddr()) |
| 3522 | funcOp.setUnnamedAddr(convertUnnamedAddrFromLLVM(value: func->getUnnamedAddr())); |
| 3523 | |
| 3524 | if (func->hasSection()) |
| 3525 | funcOp.setSection(StringRef(func->getSection())); |
| 3526 | |
| 3527 | funcOp.setVisibility_(convertVisibilityFromLLVM(value: func->getVisibility())); |
| 3528 | |
| 3529 | if (func->hasComdat()) |
| 3530 | funcOp.setComdatAttr(comdatMapping.lookup(Val: func->getComdat())); |
| 3531 | |
| 3532 | if (llvm::MaybeAlign maybeAlign = func->getAlign()) |
| 3533 | funcOp.setAlignment(maybeAlign->value()); |
| 3534 | |
| 3535 | // Handle Function attributes. |
| 3536 | processFunctionAttributes(func, funcOp); |
| 3537 | |
| 3538 | // Convert non-debug metadata by using the dialect interface. Metadata without |
| 3539 | // a kind-specific conversion is preserved in the generic function metadata |
| 3540 | // carrier. |
| 3541 | SmallVector<std::pair<unsigned, llvm::MDNode *>> allMetadata; |
| 3542 | func->getAllMetadata(MDs&: allMetadata); |
| 3543 | SmallVector<StringRef> metadataNames; |
| 3544 | llvmModule->getMDKindNames(Result&: metadataNames); |
| 3545 | SmallVector<Attribute> functionMetadata; |
| 3546 | for (auto &[kind, node] : allMetadata) { |
| 3547 | if (kind == llvm::LLVMContext::MD_dbg) |
| 3548 | continue; |
| 3549 | |
| 3550 | llvm::MDNode *metadataNode = node; |
| 3551 | auto emitUnhandledFunctionMetadataWarning = [&]() { |
| 3552 | if (!emitExpensiveWarnings) |
| 3553 | return; |
| 3554 | emitWarning(loc: funcOp.getLoc()) |
| 3555 | << "unhandled function metadata: " << renderMetadata(node: metadataNode) |
| 3556 | << " on " << renderValue(value: *func); |
| 3557 | }; |
| 3558 | |
| 3559 | if (iface.isConvertibleMetadata(kind)) { |
| 3560 | if (succeeded(Result: iface.setMetadataAttrs(builder, kind, node: metadataNode, op: funcOp, |
| 3561 | moduleImport&: *this))) |
| 3562 | continue; |
| 3563 | emitUnhandledFunctionMetadataWarning(); |
| 3564 | continue; |
| 3565 | } |
| 3566 | |
| 3567 | Attribute nodeAttr = convertMetadataToAttr(md: metadataNode); |
| 3568 | auto mdNodeAttr = dyn_cast_if_present<LLVM::MDNodeAttr>(Val&: nodeAttr); |
| 3569 | if (!mdNodeAttr || kind >= metadataNames.size()) { |
| 3570 | emitUnhandledFunctionMetadataWarning(); |
| 3571 | continue; |
| 3572 | } |
| 3573 | |
| 3574 | functionMetadata.push_back(Elt: LLVM::FunctionMetadataAttr::get( |
| 3575 | context, metadataName: builder.getStringAttr(bytes: metadataNames[kind]), node: mdNodeAttr)); |
| 3576 | } |
| 3577 | if (!functionMetadata.empty()) |
| 3578 | funcOp.setFunctionMetadataAttr(builder.getArrayAttr(value: functionMetadata)); |
| 3579 | |
| 3580 | if (func->isDeclaration()) |
| 3581 | return success(); |
| 3582 | |
| 3583 | // Collect the set of basic blocks reachable from the function's entry block. |
| 3584 | // This step is crucial as LLVM IR can contain unreachable blocks that |
| 3585 | // self-dominate. As a result, an operation might utilize a variable it |
| 3586 | // defines, which the import does not support. Given that MLIR lacks block |
| 3587 | // label support, we can safely remove unreachable blocks, as there are no |
| 3588 | // indirect branch instructions that could potentially target these blocks. |
| 3589 | llvm::df_iterator_default_set<llvm::BasicBlock *> reachable; |
| 3590 | for (llvm::BasicBlock *basicBlock : llvm::depth_first_ext(G: func, S&: reachable)) |
| 3591 | (void)basicBlock; |
| 3592 | |
| 3593 | // Eagerly create all reachable blocks. |
| 3594 | SmallVector<llvm::BasicBlock *> reachableBasicBlocks; |
| 3595 | for (llvm::BasicBlock &basicBlock : *func) { |
| 3596 | // Skip unreachable blocks. |
| 3597 | if (!reachable.contains(Ptr: &basicBlock)) { |
| 3598 | if (basicBlock.hasAddressTaken()) |
| 3599 | return emitError(loc: funcOp.getLoc()) |
| 3600 | << "unreachable block '" << basicBlock.getName() |
| 3601 | << "' with address taken" ; |
| 3602 | continue; |
| 3603 | } |
| 3604 | Region &body = funcOp.getBody(); |
| 3605 | Block *block = builder.createBlock(parent: &body, insertPt: body.end()); |
| 3606 | mapBlock(llvm: &basicBlock, mlir: block); |
| 3607 | reachableBasicBlocks.push_back(Elt: &basicBlock); |
| 3608 | } |
| 3609 | |
| 3610 | // Add function arguments to the entry block. |
| 3611 | for (const auto &it : llvm::enumerate(First: func->args())) { |
| 3612 | BlockArgument blockArg = funcOp.getFunctionBody().addArgument( |
| 3613 | type: functionType.getParamType(i: it.index()), loc: funcOp.getLoc()); |
| 3614 | mapValue(llvm: &it.value(), mlir: blockArg); |
| 3615 | } |
| 3616 | |
| 3617 | // Process the blocks in topological order. The ordered traversal ensures |
| 3618 | // operands defined in a dominating block have a valid mapping to an MLIR |
| 3619 | // value once a block is translated. |
| 3620 | SetVector<llvm::BasicBlock *> blocks = |
| 3621 | getTopologicallySortedBlocks(basicBlocks: reachableBasicBlocks); |
| 3622 | setConstantInsertionPointToStart(lookupBlock(block: blocks.front())); |
| 3623 | for (llvm::BasicBlock *basicBlock : blocks) |
| 3624 | if (failed(Result: processBasicBlock(bb: basicBlock, block: lookupBlock(block: basicBlock)))) |
| 3625 | return failure(); |
| 3626 | |
| 3627 | // Process the debug intrinsics that require a delayed conversion after |
| 3628 | // everything else was converted. |
| 3629 | if (failed(Result: processDebugIntrinsics())) |
| 3630 | return failure(); |
| 3631 | |
| 3632 | // Process the debug records that require a delayed conversion after |
| 3633 | // everything else was converted. |
| 3634 | if (failed(Result: processDebugRecords())) |
| 3635 | return failure(); |
| 3636 | |
| 3637 | return success(); |
| 3638 | } |
| 3639 | |
| 3640 | /// Checks if `dbgIntr` is a kill location that holds metadata instead of an SSA |
| 3641 | /// value. |
| 3642 | static bool isMetadataKillLocation(llvm::DbgVariableIntrinsic *dbgIntr) { |
| 3643 | if (!dbgIntr->isKillLocation()) |
| 3644 | return false; |
| 3645 | llvm::Value *value = dbgIntr->getArgOperand(i: 0); |
| 3646 | auto *nodeAsVal = dyn_cast<llvm::MetadataAsValue>(Val: value); |
| 3647 | if (!nodeAsVal) |
| 3648 | return false; |
| 3649 | return !isa<llvm::ValueAsMetadata>(Val: nodeAsVal->getMetadata()); |
| 3650 | } |
| 3651 | |
| 3652 | /// Ensure that the debug intrinsic is inserted right after the operand |
| 3653 | /// definition. Otherwise, the operand might not necessarily dominate the |
| 3654 | /// intrinsic. If the defining operation is a terminator, insert the intrinsic |
| 3655 | /// into a dominated block. |
| 3656 | static LogicalResult setDebugIntrinsicBuilderInsertionPoint( |
| 3657 | mlir::OpBuilder &builder, DominanceInfo &domInfo, Value argOperand) { |
| 3658 | if (Operation *op = argOperand.getDefiningOp(); |
| 3659 | op && op->hasTrait<OpTrait::IsTerminator>()) { |
| 3660 | // Find a dominated block that can hold the debug intrinsic. |
| 3661 | auto dominatedBlocks = domInfo.getNode(a: op->getBlock())->children(); |
| 3662 | // If no block is dominated by the terminator, this intrinisc cannot be |
| 3663 | // converted. |
| 3664 | if (dominatedBlocks.empty()) |
| 3665 | return failure(); |
| 3666 | // Set insertion point before the terminator, to avoid inserting something |
| 3667 | // before landingpads. |
| 3668 | Block *dominatedBlock = (*dominatedBlocks.begin())->getBlock(); |
| 3669 | builder.setInsertionPoint(dominatedBlock->getTerminator()); |
| 3670 | } else { |
| 3671 | Value insertPt = argOperand; |
| 3672 | if (auto blockArg = dyn_cast<BlockArgument>(Val&: argOperand)) { |
| 3673 | // The value might be coming from a phi node and is now a block argument, |
| 3674 | // which means the insertion point is set to the start of the block. If |
| 3675 | // this block is a target destination of an invoke, the insertion point |
| 3676 | // must happen after the landing pad operation. |
| 3677 | Block *insertionBlock = argOperand.getParentBlock(); |
| 3678 | if (!insertionBlock->empty() && |
| 3679 | isa<LandingpadOp>(Val: insertionBlock->front())) |
| 3680 | insertPt = cast<LandingpadOp>(Val&: insertionBlock->front()).getRes(); |
| 3681 | } |
| 3682 | |
| 3683 | builder.setInsertionPointAfterValue(insertPt); |
| 3684 | } |
| 3685 | return success(); |
| 3686 | } |
| 3687 | |
| 3688 | std::tuple<DILocalVariableAttr, DIExpressionAttr, Value> |
| 3689 | ModuleImport::processDebugOpArgumentsAndInsertionPt( |
| 3690 | Location loc, |
| 3691 | llvm::function_ref<FailureOr<Value>()> convertArgOperandToValue, |
| 3692 | llvm::Value *address, |
| 3693 | llvm::PointerUnion<llvm::Value *, llvm::DILocalVariable *> variable, |
| 3694 | llvm::DIExpression *expression, DominanceInfo &domInfo) { |
| 3695 | // Drop debug intrinsics if the associated debug information cannot be |
| 3696 | // translated due to an unsupported construct. |
| 3697 | DILocalVariableAttr localVarAttr = matchLocalVariableAttr(valOrVariable: variable); |
| 3698 | if (!localVarAttr) |
| 3699 | return {}; |
| 3700 | FailureOr<Value> argOperand = convertArgOperandToValue(); |
| 3701 | if (failed(Result: argOperand)) { |
| 3702 | emitError(loc) << "failed to convert a debug operand: " |
| 3703 | << renderValue(value: *address); |
| 3704 | return {}; |
| 3705 | } |
| 3706 | |
| 3707 | if (setDebugIntrinsicBuilderInsertionPoint(builder, domInfo, argOperand: *argOperand) |
| 3708 | .failed()) |
| 3709 | return {}; |
| 3710 | |
| 3711 | return {localVarAttr, debugImporter->translateExpression(node: expression), |
| 3712 | *argOperand}; |
| 3713 | } |
| 3714 | |
| 3715 | LogicalResult |
| 3716 | ModuleImport::processDebugIntrinsic(llvm::DbgVariableIntrinsic *dbgIntr, |
| 3717 | DominanceInfo &domInfo) { |
| 3718 | Location loc = translateLoc(loc: dbgIntr->getDebugLoc()); |
| 3719 | auto emitUnsupportedWarning = [&]() { |
| 3720 | if (emitExpensiveWarnings) |
| 3721 | emitWarning(loc) << "dropped intrinsic: " << renderValue(value: *dbgIntr); |
| 3722 | return success(); |
| 3723 | }; |
| 3724 | |
| 3725 | OpBuilder::InsertionGuard guard(builder); |
| 3726 | auto convertArgOperandToValue = [&]() { |
| 3727 | return convertMetadataValue(value: dbgIntr->getArgOperand(i: 0)); |
| 3728 | }; |
| 3729 | |
| 3730 | // Drop debug intrinsics with an argument list. |
| 3731 | // TODO: Support this case. |
| 3732 | if (dbgIntr->hasArgList()) |
| 3733 | return emitUnsupportedWarning(); |
| 3734 | |
| 3735 | // Drop debug intrinsics with kill locations that have metadata nodes as |
| 3736 | // location operand, which cannot be converted to poison as the type cannot be |
| 3737 | // reconstructed. |
| 3738 | // TODO: Support this case. |
| 3739 | if (isMetadataKillLocation(dbgIntr)) |
| 3740 | return emitUnsupportedWarning(); |
| 3741 | |
| 3742 | auto [localVariableAttr, locationExprAttr, locVal] = |
| 3743 | processDebugOpArgumentsAndInsertionPt( |
| 3744 | loc, convertArgOperandToValue, address: dbgIntr->getArgOperand(i: 0), |
| 3745 | variable: dbgIntr->getArgOperand(i: 1), expression: dbgIntr->getExpression(), domInfo); |
| 3746 | |
| 3747 | if (!localVariableAttr) |
| 3748 | return emitUnsupportedWarning(); |
| 3749 | |
| 3750 | if (!locVal) // Expected if localVariableAttr is present. |
| 3751 | return failure(); |
| 3752 | |
| 3753 | Operation *op = nullptr; |
| 3754 | if (isa<llvm::DbgDeclareInst>(Val: dbgIntr)) |
| 3755 | op = LLVM::DbgDeclareOp::create(builder, location: loc, addr: locVal, varInfo: localVariableAttr, |
| 3756 | locationExpr: locationExprAttr); |
| 3757 | else if (isa<llvm::DbgValueInst>(Val: dbgIntr)) |
| 3758 | op = LLVM::DbgValueOp::create(builder, location: loc, value: locVal, varInfo: localVariableAttr, |
| 3759 | locationExpr: locationExprAttr); |
| 3760 | else |
| 3761 | return emitUnsupportedWarning(); |
| 3762 | |
| 3763 | mapNoResultOp(llvm: dbgIntr, mlir: op); |
| 3764 | setNonDebugMetadataAttrs(inst: dbgIntr, op); |
| 3765 | return success(); |
| 3766 | } |
| 3767 | |
| 3768 | LogicalResult |
| 3769 | ModuleImport::processDebugRecord(llvm::DbgVariableRecord &dbgRecord, |
| 3770 | DominanceInfo &domInfo) { |
| 3771 | OpBuilder::InsertionGuard guard(builder); |
| 3772 | Location loc = translateLoc(loc: dbgRecord.getDebugLoc()); |
| 3773 | auto emitUnsupportedWarning = [&]() -> LogicalResult { |
| 3774 | if (!emitExpensiveWarnings) |
| 3775 | return success(); |
| 3776 | std::string options; |
| 3777 | llvm::raw_string_ostream optionsStream(options); |
| 3778 | dbgRecord.print(O&: optionsStream); |
| 3779 | emitWarning(loc) << "unhandled debug variable record " |
| 3780 | << optionsStream.str(); |
| 3781 | return success(); |
| 3782 | }; |
| 3783 | |
| 3784 | // Drop debug records with an argument list. |
| 3785 | // TODO: Support this case. |
| 3786 | if (dbgRecord.hasArgList()) |
| 3787 | return emitUnsupportedWarning(); |
| 3788 | |
| 3789 | // Drop all other debug records with a address operand that cannot be |
| 3790 | // converted to an SSA value such as an empty metadata node. |
| 3791 | // TODO: Support this case. |
| 3792 | if (!dbgRecord.getAddress()) |
| 3793 | return emitUnsupportedWarning(); |
| 3794 | |
| 3795 | auto convertArgOperandToValue = [&]() -> FailureOr<Value> { |
| 3796 | llvm::Value *value = dbgRecord.getAddress(); |
| 3797 | |
| 3798 | // Return the mapped value if it has been converted before. |
| 3799 | auto it = valueMapping.find(Val: value); |
| 3800 | if (it != valueMapping.end()) |
| 3801 | return it->getSecond(); |
| 3802 | |
| 3803 | // Convert constants such as immediate values that have no mapping yet. |
| 3804 | if (auto *constant = dyn_cast<llvm::Constant>(Val: value)) |
| 3805 | return convertConstantExpr(constant); |
| 3806 | return failure(); |
| 3807 | }; |
| 3808 | |
| 3809 | auto [localVariableAttr, locationExprAttr, locVal] = |
| 3810 | processDebugOpArgumentsAndInsertionPt( |
| 3811 | loc, convertArgOperandToValue, address: dbgRecord.getAddress(), |
| 3812 | variable: dbgRecord.getVariable(), expression: dbgRecord.getExpression(), domInfo); |
| 3813 | |
| 3814 | if (!localVariableAttr) |
| 3815 | return emitUnsupportedWarning(); |
| 3816 | |
| 3817 | if (!locVal) // Expected if localVariableAttr is present. |
| 3818 | return failure(); |
| 3819 | |
| 3820 | if (dbgRecord.isDbgDeclare()) |
| 3821 | LLVM::DbgDeclareOp::create(builder, location: loc, addr: locVal, varInfo: localVariableAttr, |
| 3822 | locationExpr: locationExprAttr); |
| 3823 | else if (dbgRecord.isDbgValue()) |
| 3824 | LLVM::DbgValueOp::create(builder, location: loc, value: locVal, varInfo: localVariableAttr, |
| 3825 | locationExpr: locationExprAttr); |
| 3826 | else // isDbgAssign |
| 3827 | return emitUnsupportedWarning(); |
| 3828 | |
| 3829 | return success(); |
| 3830 | } |
| 3831 | |
| 3832 | LogicalResult ModuleImport::processDebugIntrinsics() { |
| 3833 | DominanceInfo domInfo; |
| 3834 | for (llvm::Instruction *inst : debugIntrinsics) { |
| 3835 | auto *intrCall = cast<llvm::DbgVariableIntrinsic>(Val: inst); |
| 3836 | if (failed(Result: processDebugIntrinsic(dbgIntr: intrCall, domInfo))) |
| 3837 | return failure(); |
| 3838 | } |
| 3839 | return success(); |
| 3840 | } |
| 3841 | |
| 3842 | LogicalResult ModuleImport::processDebugRecords() { |
| 3843 | DominanceInfo domInfo; |
| 3844 | for (llvm::DbgVariableRecord *dbgRecord : dbgRecords) |
| 3845 | if (failed(Result: processDebugRecord(dbgRecord&: *dbgRecord, domInfo))) |
| 3846 | return failure(); |
| 3847 | dbgRecords.clear(); |
| 3848 | return success(); |
| 3849 | } |
| 3850 | |
| 3851 | LogicalResult ModuleImport::processBasicBlock(llvm::BasicBlock *bb, |
| 3852 | Block *block) { |
| 3853 | builder.setInsertionPointToStart(block); |
| 3854 | for (llvm::Instruction &inst : *bb) { |
| 3855 | if (failed(Result: processInstruction(inst: &inst))) |
| 3856 | return failure(); |
| 3857 | |
| 3858 | // Skip additional processing when the instructions is a debug intrinsics |
| 3859 | // that was not yet converted. |
| 3860 | if (debugIntrinsics.contains(key: &inst)) |
| 3861 | continue; |
| 3862 | |
| 3863 | // Set the non-debug metadata attributes on the imported operation and emit |
| 3864 | // a warning if an instruction other than a phi instruction is dropped |
| 3865 | // during the import. |
| 3866 | if (Operation *op = lookupOperation(inst: &inst)) { |
| 3867 | setNonDebugMetadataAttrs(inst: &inst, op); |
| 3868 | } else if (inst.getOpcode() != llvm::Instruction::PHI) { |
| 3869 | if (emitExpensiveWarnings) { |
| 3870 | Location loc = debugImporter->translateLoc(loc: inst.getDebugLoc()); |
| 3871 | emitWarning(loc) << "dropped instruction: " << renderValue(value: inst); |
| 3872 | } |
| 3873 | } |
| 3874 | } |
| 3875 | |
| 3876 | if (bb->hasAddressTaken()) { |
| 3877 | OpBuilder::InsertionGuard guard(builder); |
| 3878 | builder.setInsertionPointToStart(block); |
| 3879 | BlockTagOp::create(builder, location: block->getParentOp()->getLoc(), |
| 3880 | tag: BlockTagAttr::get(context, id: bb->getNumber())); |
| 3881 | } |
| 3882 | return success(); |
| 3883 | } |
| 3884 | |
| 3885 | FailureOr<SmallVector<AccessGroupAttr>> |
| 3886 | ModuleImport::lookupAccessGroupAttrs(const llvm::MDNode *node) const { |
| 3887 | return loopAnnotationImporter->lookupAccessGroupAttrs(node); |
| 3888 | } |
| 3889 | |
| 3890 | LoopAnnotationAttr |
| 3891 | ModuleImport::translateLoopAnnotationAttr(const llvm::MDNode *node, |
| 3892 | Location loc) const { |
| 3893 | return loopAnnotationImporter->translateLoopAnnotation(node, loc); |
| 3894 | } |
| 3895 | |
| 3896 | FailureOr<DereferenceableAttr> |
| 3897 | ModuleImport::translateDereferenceableAttr(const llvm::MDNode *node, |
| 3898 | unsigned kindID) { |
| 3899 | Location loc = mlirModule.getLoc(); |
| 3900 | |
| 3901 | // The only operand should be a constant integer representing the number of |
| 3902 | // dereferenceable bytes. |
| 3903 | if (node->getNumOperands() != 1) |
| 3904 | return emitError(loc) << "dereferenceable metadata must have one operand: " |
| 3905 | << renderMetadata(node); |
| 3906 | |
| 3907 | auto *numBytesMD = dyn_cast<llvm::ConstantAsMetadata>(Val: node->getOperand(I: 0)); |
| 3908 | auto *numBytesCst = dyn_cast<llvm::ConstantInt>(Val: numBytesMD->getValue()); |
| 3909 | if (!numBytesCst || !numBytesCst->getValue().isNonNegative()) |
| 3910 | return emitError(loc) << "dereferenceable metadata operand must be a " |
| 3911 | "non-negative constant integer: " |
| 3912 | << renderMetadata(node); |
| 3913 | |
| 3914 | bool mayBeNull = kindID == llvm::LLVMContext::MD_dereferenceable_or_null; |
| 3915 | auto derefAttr = builder.getAttr<DereferenceableAttr>( |
| 3916 | args: numBytesCst->getZExtValue(), args&: mayBeNull); |
| 3917 | |
| 3918 | return derefAttr; |
| 3919 | } |
| 3920 | |
| 3921 | OwningOpRef<ModuleOp> mlir::translateLLVMIRToModule( |
| 3922 | std::unique_ptr<llvm::Module> llvmModule, MLIRContext *context, |
| 3923 | bool emitExpensiveWarnings, bool dropDICompositeTypeElements, |
| 3924 | bool loadAllDialects, bool preferUnregisteredIntrinsics, |
| 3925 | bool importStructsAsLiterals) { |
| 3926 | // Preload all registered dialects to allow the import to iterate the |
| 3927 | // registered LLVMImportDialectInterface implementations and query the |
| 3928 | // supported LLVM IR constructs before starting the translation. Assumes the |
| 3929 | // LLVM and DLTI dialects that convert the core LLVM IR constructs have been |
| 3930 | // registered before. |
| 3931 | assert(llvm::is_contained(context->getAvailableDialects(), |
| 3932 | LLVMDialect::getDialectNamespace())); |
| 3933 | assert(llvm::is_contained(context->getAvailableDialects(), |
| 3934 | DLTIDialect::getDialectNamespace())); |
| 3935 | if (loadAllDialects) |
| 3936 | context->loadAllAvailableDialects(); |
| 3937 | OwningOpRef<ModuleOp> module(ModuleOp::create(loc: FileLineColLoc::get( |
| 3938 | filename: StringAttr::get(context, bytes: llvmModule->getSourceFileName()), /*line=*/0, |
| 3939 | /*column=*/0))); |
| 3940 | |
| 3941 | ModuleImport moduleImport(module.get(), std::move(llvmModule), |
| 3942 | emitExpensiveWarnings, dropDICompositeTypeElements, |
| 3943 | preferUnregisteredIntrinsics, |
| 3944 | importStructsAsLiterals); |
| 3945 | if (failed(Result: moduleImport.initializeImportInterface())) |
| 3946 | return {}; |
| 3947 | if (failed(Result: moduleImport.convertDataLayout())) |
| 3948 | return {}; |
| 3949 | if (failed(Result: moduleImport.convertComdats())) |
| 3950 | return {}; |
| 3951 | if (failed(Result: moduleImport.convertMetadata())) |
| 3952 | return {}; |
| 3953 | if (failed(Result: moduleImport.convertGlobals())) |
| 3954 | return {}; |
| 3955 | if (failed(Result: moduleImport.convertFunctions())) |
| 3956 | return {}; |
| 3957 | if (failed(Result: moduleImport.convertAliases())) |
| 3958 | return {}; |
| 3959 | if (failed(Result: moduleImport.convertIFuncs())) |
| 3960 | return {}; |
| 3961 | moduleImport.convertTargetTriple(); |
| 3962 | moduleImport.convertModuleLevelAsm(); |
| 3963 | return module; |
| 3964 | } |
| 3965 | |