1 //===- ModuleTranslation.cpp - MLIR to LLVM conversion --------------------===// 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 translation between an MLIR LLVM dialect module and 10 // the corresponding LLVMIR module. It only handles core LLVM IR operations. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/Target/LLVMIR/ModuleTranslation.h" 15 16 #include "AttrKindDetail.h" 17 #include "DebugTranslation.h" 18 #include "LoopAnnotationTranslation.h" 19 #include "mlir/Dialect/DLTI/DLTI.h" 20 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 21 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h" 22 #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h" 23 #include "mlir/Dialect/OpenMP/OpenMPDialect.h" 24 #include "mlir/Dialect/OpenMP/OpenMPInterfaces.h" 25 #include "mlir/IR/AttrTypeSubElements.h" 26 #include "mlir/IR/Attributes.h" 27 #include "mlir/IR/BuiltinOps.h" 28 #include "mlir/IR/BuiltinTypes.h" 29 #include "mlir/IR/RegionGraphTraits.h" 30 #include "mlir/Support/LLVM.h" 31 #include "mlir/Support/LogicalResult.h" 32 #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" 33 #include "mlir/Target/LLVMIR/TypeToLLVM.h" 34 #include "mlir/Transforms/RegionUtils.h" 35 36 #include "llvm/ADT/PostOrderIterator.h" 37 #include "llvm/ADT/SetVector.h" 38 #include "llvm/ADT/TypeSwitch.h" 39 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 40 #include "llvm/IR/BasicBlock.h" 41 #include "llvm/IR/CFG.h" 42 #include "llvm/IR/Constants.h" 43 #include "llvm/IR/DerivedTypes.h" 44 #include "llvm/IR/IRBuilder.h" 45 #include "llvm/IR/InlineAsm.h" 46 #include "llvm/IR/IntrinsicsNVPTX.h" 47 #include "llvm/IR/LLVMContext.h" 48 #include "llvm/IR/MDBuilder.h" 49 #include "llvm/IR/Module.h" 50 #include "llvm/IR/Verifier.h" 51 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 52 #include "llvm/Transforms/Utils/Cloning.h" 53 #include "llvm/Transforms/Utils/ModuleUtils.h" 54 #include <optional> 55 56 using namespace mlir; 57 using namespace mlir::LLVM; 58 using namespace mlir::LLVM::detail; 59 60 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc" 61 62 /// Translates the given data layout spec attribute to the LLVM IR data layout. 63 /// Only integer, float, pointer and endianness entries are currently supported. 64 static FailureOr<llvm::DataLayout> 65 translateDataLayout(DataLayoutSpecInterface attribute, 66 const DataLayout &dataLayout, 67 std::optional<Location> loc = std::nullopt) { 68 if (!loc) 69 loc = UnknownLoc::get(attribute.getContext()); 70 71 // Translate the endianness attribute. 72 std::string llvmDataLayout; 73 llvm::raw_string_ostream layoutStream(llvmDataLayout); 74 for (DataLayoutEntryInterface entry : attribute.getEntries()) { 75 auto key = llvm::dyn_cast_if_present<StringAttr>(entry.getKey()); 76 if (!key) 77 continue; 78 if (key.getValue() == DLTIDialect::kDataLayoutEndiannessKey) { 79 auto value = cast<StringAttr>(entry.getValue()); 80 bool isLittleEndian = 81 value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle; 82 layoutStream << "-" << (isLittleEndian ? "e" : "E"); 83 layoutStream.flush(); 84 continue; 85 } 86 if (key.getValue() == DLTIDialect::kDataLayoutAllocaMemorySpaceKey) { 87 auto value = cast<IntegerAttr>(entry.getValue()); 88 uint64_t space = value.getValue().getZExtValue(); 89 // Skip the default address space. 90 if (space == 0) 91 continue; 92 layoutStream << "-A" << space; 93 layoutStream.flush(); 94 continue; 95 } 96 if (key.getValue() == DLTIDialect::kDataLayoutStackAlignmentKey) { 97 auto value = cast<IntegerAttr>(entry.getValue()); 98 uint64_t alignment = value.getValue().getZExtValue(); 99 // Skip the default stack alignment. 100 if (alignment == 0) 101 continue; 102 layoutStream << "-S" << alignment; 103 layoutStream.flush(); 104 continue; 105 } 106 emitError(*loc) << "unsupported data layout key " << key; 107 return failure(); 108 } 109 110 // Go through the list of entries to check which types are explicitly 111 // specified in entries. Where possible, data layout queries are used instead 112 // of directly inspecting the entries. 113 for (DataLayoutEntryInterface entry : attribute.getEntries()) { 114 auto type = llvm::dyn_cast_if_present<Type>(entry.getKey()); 115 if (!type) 116 continue; 117 // Data layout for the index type is irrelevant at this point. 118 if (isa<IndexType>(type)) 119 continue; 120 layoutStream << "-"; 121 LogicalResult result = 122 llvm::TypeSwitch<Type, LogicalResult>(type) 123 .Case<IntegerType, Float16Type, Float32Type, Float64Type, 124 Float80Type, Float128Type>([&](Type type) -> LogicalResult { 125 if (auto intType = dyn_cast<IntegerType>(type)) { 126 if (intType.getSignedness() != IntegerType::Signless) 127 return emitError(*loc) 128 << "unsupported data layout for non-signless integer " 129 << intType; 130 layoutStream << "i"; 131 } else { 132 layoutStream << "f"; 133 } 134 uint64_t size = dataLayout.getTypeSizeInBits(type); 135 uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u; 136 uint64_t preferred = 137 dataLayout.getTypePreferredAlignment(type) * 8u; 138 layoutStream << size << ":" << abi; 139 if (abi != preferred) 140 layoutStream << ":" << preferred; 141 return success(); 142 }) 143 .Case([&](LLVMPointerType ptrType) { 144 layoutStream << "p" << ptrType.getAddressSpace() << ":"; 145 uint64_t size = dataLayout.getTypeSizeInBits(type); 146 uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u; 147 uint64_t preferred = 148 dataLayout.getTypePreferredAlignment(type) * 8u; 149 layoutStream << size << ":" << abi << ":" << preferred; 150 if (std::optional<uint64_t> index = extractPointerSpecValue( 151 entry.getValue(), PtrDLEntryPos::Index)) 152 layoutStream << ":" << *index; 153 return success(); 154 }) 155 .Default([loc](Type type) { 156 return emitError(*loc) 157 << "unsupported type in data layout: " << type; 158 }); 159 if (failed(result)) 160 return failure(); 161 } 162 layoutStream.flush(); 163 StringRef layoutSpec(llvmDataLayout); 164 if (layoutSpec.startswith("-")) 165 layoutSpec = layoutSpec.drop_front(); 166 167 return llvm::DataLayout(layoutSpec); 168 } 169 170 /// Builds a constant of a sequential LLVM type `type`, potentially containing 171 /// other sequential types recursively, from the individual constant values 172 /// provided in `constants`. `shape` contains the number of elements in nested 173 /// sequential types. Reports errors at `loc` and returns nullptr on error. 174 static llvm::Constant * 175 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants, 176 ArrayRef<int64_t> shape, llvm::Type *type, 177 Location loc) { 178 if (shape.empty()) { 179 llvm::Constant *result = constants.front(); 180 constants = constants.drop_front(); 181 return result; 182 } 183 184 llvm::Type *elementType; 185 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 186 elementType = arrayTy->getElementType(); 187 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 188 elementType = vectorTy->getElementType(); 189 } else { 190 emitError(loc) << "expected sequential LLVM types wrapping a scalar"; 191 return nullptr; 192 } 193 194 SmallVector<llvm::Constant *, 8> nested; 195 nested.reserve(shape.front()); 196 for (int64_t i = 0; i < shape.front(); ++i) { 197 nested.push_back(buildSequentialConstant(constants, shape.drop_front(), 198 elementType, loc)); 199 if (!nested.back()) 200 return nullptr; 201 } 202 203 if (shape.size() == 1 && type->isVectorTy()) 204 return llvm::ConstantVector::get(nested); 205 return llvm::ConstantArray::get( 206 llvm::ArrayType::get(elementType, shape.front()), nested); 207 } 208 209 /// Returns the first non-sequential type nested in sequential types. 210 static llvm::Type *getInnermostElementType(llvm::Type *type) { 211 do { 212 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 213 type = arrayTy->getElementType(); 214 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 215 type = vectorTy->getElementType(); 216 } else { 217 return type; 218 } 219 } while (true); 220 } 221 222 /// Convert a dense elements attribute to an LLVM IR constant using its raw data 223 /// storage if possible. This supports elements attributes of tensor or vector 224 /// type and avoids constructing separate objects for individual values of the 225 /// innermost dimension. Constants for other dimensions are still constructed 226 /// recursively. Returns null if constructing from raw data is not supported for 227 /// this type, e.g., element type is not a power-of-two-sized primitive. Reports 228 /// other errors at `loc`. 229 static llvm::Constant * 230 convertDenseElementsAttr(Location loc, DenseElementsAttr denseElementsAttr, 231 llvm::Type *llvmType, 232 const ModuleTranslation &moduleTranslation) { 233 if (!denseElementsAttr) 234 return nullptr; 235 236 llvm::Type *innermostLLVMType = getInnermostElementType(llvmType); 237 if (!llvm::ConstantDataSequential::isElementTypeCompatible(innermostLLVMType)) 238 return nullptr; 239 240 ShapedType type = denseElementsAttr.getType(); 241 if (type.getNumElements() == 0) 242 return nullptr; 243 244 // Check that the raw data size matches what is expected for the scalar size. 245 // TODO: in theory, we could repack the data here to keep constructing from 246 // raw data. 247 // TODO: we may also need to consider endianness when cross-compiling to an 248 // architecture where it is different. 249 int64_t elementByteSize = denseElementsAttr.getRawData().size() / 250 denseElementsAttr.getNumElements(); 251 if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits()) 252 return nullptr; 253 254 // Compute the shape of all dimensions but the innermost. Note that the 255 // innermost dimension may be that of the vector element type. 256 bool hasVectorElementType = isa<VectorType>(type.getElementType()); 257 int64_t numAggregates = 258 denseElementsAttr.getNumElements() / 259 (hasVectorElementType ? 1 260 : denseElementsAttr.getType().getShape().back()); 261 ArrayRef<int64_t> outerShape = type.getShape(); 262 if (!hasVectorElementType) 263 outerShape = outerShape.drop_back(); 264 265 // Handle the case of vector splat, LLVM has special support for it. 266 if (denseElementsAttr.isSplat() && 267 (isa<VectorType>(type) || hasVectorElementType)) { 268 llvm::Constant *splatValue = LLVM::detail::getLLVMConstant( 269 innermostLLVMType, denseElementsAttr.getSplatValue<Attribute>(), loc, 270 moduleTranslation); 271 llvm::Constant *splatVector = 272 llvm::ConstantDataVector::getSplat(0, splatValue); 273 SmallVector<llvm::Constant *> constants(numAggregates, splatVector); 274 ArrayRef<llvm::Constant *> constantsRef = constants; 275 return buildSequentialConstant(constantsRef, outerShape, llvmType, loc); 276 } 277 if (denseElementsAttr.isSplat()) 278 return nullptr; 279 280 // In case of non-splat, create a constructor for the innermost constant from 281 // a piece of raw data. 282 std::function<llvm::Constant *(StringRef)> buildCstData; 283 if (isa<TensorType>(type)) { 284 auto vectorElementType = dyn_cast<VectorType>(type.getElementType()); 285 if (vectorElementType && vectorElementType.getRank() == 1) { 286 buildCstData = [&](StringRef data) { 287 return llvm::ConstantDataVector::getRaw( 288 data, vectorElementType.getShape().back(), innermostLLVMType); 289 }; 290 } else if (!vectorElementType) { 291 buildCstData = [&](StringRef data) { 292 return llvm::ConstantDataArray::getRaw(data, type.getShape().back(), 293 innermostLLVMType); 294 }; 295 } 296 } else if (isa<VectorType>(type)) { 297 buildCstData = [&](StringRef data) { 298 return llvm::ConstantDataVector::getRaw(data, type.getShape().back(), 299 innermostLLVMType); 300 }; 301 } 302 if (!buildCstData) 303 return nullptr; 304 305 // Create innermost constants and defer to the default constant creation 306 // mechanism for other dimensions. 307 SmallVector<llvm::Constant *> constants; 308 int64_t aggregateSize = denseElementsAttr.getType().getShape().back() * 309 (innermostLLVMType->getScalarSizeInBits() / 8); 310 constants.reserve(numAggregates); 311 for (unsigned i = 0; i < numAggregates; ++i) { 312 StringRef data(denseElementsAttr.getRawData().data() + i * aggregateSize, 313 aggregateSize); 314 constants.push_back(buildCstData(data)); 315 } 316 317 ArrayRef<llvm::Constant *> constantsRef = constants; 318 return buildSequentialConstant(constantsRef, outerShape, llvmType, loc); 319 } 320 321 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`. 322 /// This currently supports integer, floating point, splat and dense element 323 /// attributes and combinations thereof. Also, an array attribute with two 324 /// elements is supported to represent a complex constant. In case of error, 325 /// report it to `loc` and return nullptr. 326 llvm::Constant *mlir::LLVM::detail::getLLVMConstant( 327 llvm::Type *llvmType, Attribute attr, Location loc, 328 const ModuleTranslation &moduleTranslation) { 329 if (!attr) 330 return llvm::UndefValue::get(llvmType); 331 if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) { 332 auto arrayAttr = dyn_cast<ArrayAttr>(attr); 333 if (!arrayAttr || arrayAttr.size() != 2) { 334 emitError(loc, "expected struct type to be a complex number"); 335 return nullptr; 336 } 337 llvm::Type *elementType = structType->getElementType(0); 338 llvm::Constant *real = 339 getLLVMConstant(elementType, arrayAttr[0], loc, moduleTranslation); 340 if (!real) 341 return nullptr; 342 llvm::Constant *imag = 343 getLLVMConstant(elementType, arrayAttr[1], loc, moduleTranslation); 344 if (!imag) 345 return nullptr; 346 return llvm::ConstantStruct::get(structType, {real, imag}); 347 } 348 // For integer types, we allow a mismatch in sizes as the index type in 349 // MLIR might have a different size than the index type in the LLVM module. 350 if (auto intAttr = dyn_cast<IntegerAttr>(attr)) 351 return llvm::ConstantInt::get( 352 llvmType, 353 intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth())); 354 if (auto floatAttr = dyn_cast<FloatAttr>(attr)) { 355 const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics(); 356 // Special case for 8-bit floats, which are represented by integers due to 357 // the lack of native fp8 types in LLVM at the moment. Additionally, handle 358 // targets (like AMDGPU) that don't implement bfloat and convert all bfloats 359 // to i16. 360 unsigned floatWidth = APFloat::getSizeInBits(sem); 361 if (llvmType->isIntegerTy(floatWidth)) 362 return llvm::ConstantInt::get(llvmType, 363 floatAttr.getValue().bitcastToAPInt()); 364 if (llvmType != 365 llvm::Type::getFloatingPointTy(llvmType->getContext(), 366 floatAttr.getValue().getSemantics())) { 367 emitError(loc, "FloatAttr does not match expected type of the constant"); 368 return nullptr; 369 } 370 return llvm::ConstantFP::get(llvmType, floatAttr.getValue()); 371 } 372 if (auto funcAttr = dyn_cast<FlatSymbolRefAttr>(attr)) 373 return llvm::ConstantExpr::getBitCast( 374 moduleTranslation.lookupFunction(funcAttr.getValue()), llvmType); 375 if (auto splatAttr = dyn_cast<SplatElementsAttr>(attr)) { 376 llvm::Type *elementType; 377 uint64_t numElements; 378 bool isScalable = false; 379 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) { 380 elementType = arrayTy->getElementType(); 381 numElements = arrayTy->getNumElements(); 382 } else if (auto *fVectorTy = dyn_cast<llvm::FixedVectorType>(llvmType)) { 383 elementType = fVectorTy->getElementType(); 384 numElements = fVectorTy->getNumElements(); 385 } else if (auto *sVectorTy = dyn_cast<llvm::ScalableVectorType>(llvmType)) { 386 elementType = sVectorTy->getElementType(); 387 numElements = sVectorTy->getMinNumElements(); 388 isScalable = true; 389 } else { 390 llvm_unreachable("unrecognized constant vector type"); 391 } 392 // Splat value is a scalar. Extract it only if the element type is not 393 // another sequence type. The recursion terminates because each step removes 394 // one outer sequential type. 395 bool elementTypeSequential = 396 isa<llvm::ArrayType, llvm::VectorType>(elementType); 397 llvm::Constant *child = getLLVMConstant( 398 elementType, 399 elementTypeSequential ? splatAttr 400 : splatAttr.getSplatValue<Attribute>(), 401 loc, moduleTranslation); 402 if (!child) 403 return nullptr; 404 if (llvmType->isVectorTy()) 405 return llvm::ConstantVector::getSplat( 406 llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child); 407 if (llvmType->isArrayTy()) { 408 auto *arrayType = llvm::ArrayType::get(elementType, numElements); 409 SmallVector<llvm::Constant *, 8> constants(numElements, child); 410 return llvm::ConstantArray::get(arrayType, constants); 411 } 412 } 413 414 // Try using raw elements data if possible. 415 if (llvm::Constant *result = 416 convertDenseElementsAttr(loc, dyn_cast<DenseElementsAttr>(attr), 417 llvmType, moduleTranslation)) { 418 return result; 419 } 420 421 // Fall back to element-by-element construction otherwise. 422 if (auto elementsAttr = dyn_cast<ElementsAttr>(attr)) { 423 assert(elementsAttr.getShapedType().hasStaticShape()); 424 assert(!elementsAttr.getShapedType().getShape().empty() && 425 "unexpected empty elements attribute shape"); 426 427 SmallVector<llvm::Constant *, 8> constants; 428 constants.reserve(elementsAttr.getNumElements()); 429 llvm::Type *innermostType = getInnermostElementType(llvmType); 430 for (auto n : elementsAttr.getValues<Attribute>()) { 431 constants.push_back( 432 getLLVMConstant(innermostType, n, loc, moduleTranslation)); 433 if (!constants.back()) 434 return nullptr; 435 } 436 ArrayRef<llvm::Constant *> constantsRef = constants; 437 llvm::Constant *result = buildSequentialConstant( 438 constantsRef, elementsAttr.getShapedType().getShape(), llvmType, loc); 439 assert(constantsRef.empty() && "did not consume all elemental constants"); 440 return result; 441 } 442 443 if (auto stringAttr = dyn_cast<StringAttr>(attr)) { 444 return llvm::ConstantDataArray::get( 445 moduleTranslation.getLLVMContext(), 446 ArrayRef<char>{stringAttr.getValue().data(), 447 stringAttr.getValue().size()}); 448 } 449 emitError(loc, "unsupported constant value"); 450 return nullptr; 451 } 452 453 ModuleTranslation::ModuleTranslation(Operation *module, 454 std::unique_ptr<llvm::Module> llvmModule) 455 : mlirModule(module), llvmModule(std::move(llvmModule)), 456 debugTranslation( 457 std::make_unique<DebugTranslation>(module, *this->llvmModule)), 458 loopAnnotationTranslation(std::make_unique<LoopAnnotationTranslation>( 459 *this, *this->llvmModule)), 460 typeTranslator(this->llvmModule->getContext()), 461 iface(module->getContext()) { 462 assert(satisfiesLLVMModule(mlirModule) && 463 "mlirModule should honor LLVM's module semantics."); 464 } 465 466 ModuleTranslation::~ModuleTranslation() { 467 if (ompBuilder) 468 ompBuilder->finalize(); 469 } 470 471 void ModuleTranslation::forgetMapping(Region ®ion) { 472 SmallVector<Region *> toProcess; 473 toProcess.push_back(®ion); 474 while (!toProcess.empty()) { 475 Region *current = toProcess.pop_back_val(); 476 for (Block &block : *current) { 477 blockMapping.erase(&block); 478 for (Value arg : block.getArguments()) 479 valueMapping.erase(arg); 480 for (Operation &op : block) { 481 for (Value value : op.getResults()) 482 valueMapping.erase(value); 483 if (op.hasSuccessors()) 484 branchMapping.erase(&op); 485 if (isa<LLVM::GlobalOp>(op)) 486 globalsMapping.erase(&op); 487 llvm::append_range( 488 toProcess, 489 llvm::map_range(op.getRegions(), [](Region &r) { return &r; })); 490 } 491 } 492 } 493 } 494 495 /// Get the SSA value passed to the current block from the terminator operation 496 /// of its predecessor. 497 static Value getPHISourceValue(Block *current, Block *pred, 498 unsigned numArguments, unsigned index) { 499 Operation &terminator = *pred->getTerminator(); 500 if (isa<LLVM::BrOp>(terminator)) 501 return terminator.getOperand(index); 502 503 #ifndef NDEBUG 504 llvm::SmallPtrSet<Block *, 4> seenSuccessors; 505 for (unsigned i = 0, e = terminator.getNumSuccessors(); i < e; ++i) { 506 Block *successor = terminator.getSuccessor(i); 507 auto branch = cast<BranchOpInterface>(terminator); 508 SuccessorOperands successorOperands = branch.getSuccessorOperands(i); 509 assert( 510 (!seenSuccessors.contains(successor) || successorOperands.empty()) && 511 "successors with arguments in LLVM branches must be different blocks"); 512 seenSuccessors.insert(successor); 513 } 514 #endif 515 516 // For instructions that branch based on a condition value, we need to take 517 // the operands for the branch that was taken. 518 if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) { 519 // For conditional branches, we take the operands from either the "true" or 520 // the "false" branch. 521 return condBranchOp.getSuccessor(0) == current 522 ? condBranchOp.getTrueDestOperands()[index] 523 : condBranchOp.getFalseDestOperands()[index]; 524 } 525 526 if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) { 527 // For switches, we take the operands from either the default case, or from 528 // the case branch that was taken. 529 if (switchOp.getDefaultDestination() == current) 530 return switchOp.getDefaultOperands()[index]; 531 for (const auto &i : llvm::enumerate(switchOp.getCaseDestinations())) 532 if (i.value() == current) 533 return switchOp.getCaseOperands(i.index())[index]; 534 } 535 536 if (auto invokeOp = dyn_cast<LLVM::InvokeOp>(terminator)) { 537 return invokeOp.getNormalDest() == current 538 ? invokeOp.getNormalDestOperands()[index] 539 : invokeOp.getUnwindDestOperands()[index]; 540 } 541 542 llvm_unreachable( 543 "only branch, switch or invoke operations can be terminators " 544 "of a block that has successors"); 545 } 546 547 /// Connect the PHI nodes to the results of preceding blocks. 548 void mlir::LLVM::detail::connectPHINodes(Region ®ion, 549 const ModuleTranslation &state) { 550 // Skip the first block, it cannot be branched to and its arguments correspond 551 // to the arguments of the LLVM function. 552 for (Block &bb : llvm::drop_begin(region)) { 553 llvm::BasicBlock *llvmBB = state.lookupBlock(&bb); 554 auto phis = llvmBB->phis(); 555 auto numArguments = bb.getNumArguments(); 556 assert(numArguments == std::distance(phis.begin(), phis.end())); 557 for (auto [index, phiNode] : llvm::enumerate(phis)) { 558 for (auto *pred : bb.getPredecessors()) { 559 // Find the LLVM IR block that contains the converted terminator 560 // instruction and use it in the PHI node. Note that this block is not 561 // necessarily the same as state.lookupBlock(pred), some operations 562 // (in particular, OpenMP operations using OpenMPIRBuilder) may have 563 // split the blocks. 564 llvm::Instruction *terminator = 565 state.lookupBranch(pred->getTerminator()); 566 assert(terminator && "missing the mapping for a terminator"); 567 phiNode.addIncoming(state.lookupValue(getPHISourceValue( 568 &bb, pred, numArguments, index)), 569 terminator->getParent()); 570 } 571 } 572 } 573 } 574 575 llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall( 576 llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, 577 ArrayRef<llvm::Value *> args, ArrayRef<llvm::Type *> tys) { 578 llvm::Module *module = builder.GetInsertBlock()->getModule(); 579 llvm::Function *fn = llvm::Intrinsic::getDeclaration(module, intrinsic, tys); 580 return builder.CreateCall(fn, args); 581 } 582 583 llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall( 584 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation, 585 Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults, 586 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands, 587 ArrayRef<unsigned> immArgPositions, 588 ArrayRef<StringLiteral> immArgAttrNames) { 589 assert(immArgPositions.size() == immArgAttrNames.size() && 590 "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal " 591 "length"); 592 593 // Map operands and attributes to LLVM values. 594 auto operands = moduleTranslation.lookupValues(intrOp->getOperands()); 595 SmallVector<llvm::Value *> args(immArgPositions.size() + operands.size()); 596 for (auto [immArgPos, immArgName] : 597 llvm::zip(immArgPositions, immArgAttrNames)) { 598 auto attr = llvm::cast<TypedAttr>(intrOp->getAttr(immArgName)); 599 assert(attr.getType().isIntOrFloat() && "expected int or float immarg"); 600 auto *type = moduleTranslation.convertType(attr.getType()); 601 args[immArgPos] = LLVM::detail::getLLVMConstant( 602 type, attr, intrOp->getLoc(), moduleTranslation); 603 } 604 unsigned opArg = 0; 605 for (auto &arg : args) { 606 if (!arg) 607 arg = operands[opArg++]; 608 } 609 610 // Resolve overloaded intrinsic declaration. 611 SmallVector<llvm::Type *> overloadedTypes; 612 for (unsigned overloadedResultIdx : overloadedResults) { 613 if (numResults > 1) { 614 // More than one result is mapped to an LLVM struct. 615 overloadedTypes.push_back(moduleTranslation.convertType( 616 llvm::cast<LLVM::LLVMStructType>(intrOp->getResult(0).getType()) 617 .getBody()[overloadedResultIdx])); 618 } else { 619 overloadedTypes.push_back( 620 moduleTranslation.convertType(intrOp->getResult(0).getType())); 621 } 622 } 623 for (unsigned overloadedOperandIdx : overloadedOperands) 624 overloadedTypes.push_back(args[overloadedOperandIdx]->getType()); 625 llvm::Module *module = builder.GetInsertBlock()->getModule(); 626 llvm::Function *llvmIntr = 627 llvm::Intrinsic::getDeclaration(module, intrinsic, overloadedTypes); 628 629 return builder.CreateCall(llvmIntr, args); 630 } 631 632 /// Given a single MLIR operation, create the corresponding LLVM IR operation 633 /// using the `builder`. 634 LogicalResult 635 ModuleTranslation::convertOperation(Operation &op, 636 llvm::IRBuilderBase &builder) { 637 const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op); 638 if (!opIface) 639 return op.emitError("cannot be converted to LLVM IR: missing " 640 "`LLVMTranslationDialectInterface` registration for " 641 "dialect for op: ") 642 << op.getName(); 643 644 if (failed(opIface->convertOperation(&op, builder, *this))) 645 return op.emitError("LLVM Translation failed for operation: ") 646 << op.getName(); 647 648 return convertDialectAttributes(&op); 649 } 650 651 /// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes 652 /// to define values corresponding to the MLIR block arguments. These nodes 653 /// are not connected to the source basic blocks, which may not exist yet. Uses 654 /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have 655 /// been created for `bb` and included in the block mapping. Inserts new 656 /// instructions at the end of the block and leaves `builder` in a state 657 /// suitable for further insertion into the end of the block. 658 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments, 659 llvm::IRBuilderBase &builder) { 660 builder.SetInsertPoint(lookupBlock(&bb)); 661 auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram(); 662 663 // Before traversing operations, make block arguments available through 664 // value remapping and PHI nodes, but do not add incoming edges for the PHI 665 // nodes just yet: those values may be defined by this or following blocks. 666 // This step is omitted if "ignoreArguments" is set. The arguments of the 667 // first block have been already made available through the remapping of 668 // LLVM function arguments. 669 if (!ignoreArguments) { 670 auto predecessors = bb.getPredecessors(); 671 unsigned numPredecessors = 672 std::distance(predecessors.begin(), predecessors.end()); 673 for (auto arg : bb.getArguments()) { 674 auto wrappedType = arg.getType(); 675 if (!isCompatibleType(wrappedType)) 676 return emitError(bb.front().getLoc(), 677 "block argument does not have an LLVM type"); 678 llvm::Type *type = convertType(wrappedType); 679 llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors); 680 mapValue(arg, phi); 681 } 682 } 683 684 // Traverse operations. 685 for (auto &op : bb) { 686 // Set the current debug location within the builder. 687 builder.SetCurrentDebugLocation( 688 debugTranslation->translateLoc(op.getLoc(), subprogram)); 689 690 if (failed(convertOperation(op, builder))) 691 return failure(); 692 693 // Set the branch weight metadata on the translated instruction. 694 if (auto iface = dyn_cast<BranchWeightOpInterface>(op)) 695 setBranchWeightsMetadata(iface); 696 } 697 698 return success(); 699 } 700 701 /// A helper method to get the single Block in an operation honoring LLVM's 702 /// module requirements. 703 static Block &getModuleBody(Operation *module) { 704 return module->getRegion(0).front(); 705 } 706 707 /// A helper method to decide if a constant must not be set as a global variable 708 /// initializer. For an external linkage variable, the variable with an 709 /// initializer is considered externally visible and defined in this module, the 710 /// variable without an initializer is externally available and is defined 711 /// elsewhere. 712 static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage, 713 llvm::Constant *cst) { 714 return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) || 715 linkage == llvm::GlobalVariable::ExternalWeakLinkage; 716 } 717 718 /// Sets the runtime preemption specifier of `gv` to dso_local if 719 /// `dsoLocalRequested` is true, otherwise it is left unchanged. 720 static void addRuntimePreemptionSpecifier(bool dsoLocalRequested, 721 llvm::GlobalValue *gv) { 722 if (dsoLocalRequested) 723 gv->setDSOLocal(true); 724 } 725 726 /// Create named global variables that correspond to llvm.mlir.global 727 /// definitions. Convert llvm.global_ctors and global_dtors ops. 728 LogicalResult ModuleTranslation::convertGlobals() { 729 // Mapping from compile unit to its respective set of global variables. 730 DenseMap<llvm::DICompileUnit *, SmallVector<llvm::Metadata *>> allGVars; 731 732 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 733 llvm::Type *type = convertType(op.getType()); 734 llvm::Constant *cst = nullptr; 735 if (op.getValueOrNull()) { 736 // String attributes are treated separately because they cannot appear as 737 // in-function constants and are thus not supported by getLLVMConstant. 738 if (auto strAttr = dyn_cast_or_null<StringAttr>(op.getValueOrNull())) { 739 cst = llvm::ConstantDataArray::getString( 740 llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); 741 type = cst->getType(); 742 } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc(), 743 *this))) { 744 return failure(); 745 } 746 } 747 748 auto linkage = convertLinkageToLLVM(op.getLinkage()); 749 auto addrSpace = op.getAddrSpace(); 750 751 // LLVM IR requires constant with linkage other than external or weak 752 // external to have initializers. If MLIR does not provide an initializer, 753 // default to undef. 754 bool dropInitializer = shouldDropGlobalInitializer(linkage, cst); 755 if (!dropInitializer && !cst) 756 cst = llvm::UndefValue::get(type); 757 else if (dropInitializer && cst) 758 cst = nullptr; 759 760 auto *var = new llvm::GlobalVariable( 761 *llvmModule, type, op.getConstant(), linkage, cst, op.getSymName(), 762 /*InsertBefore=*/nullptr, 763 op.getThreadLocal_() ? llvm::GlobalValue::GeneralDynamicTLSModel 764 : llvm::GlobalValue::NotThreadLocal, 765 addrSpace); 766 767 if (std::optional<mlir::SymbolRefAttr> comdat = op.getComdat()) { 768 auto selectorOp = cast<ComdatSelectorOp>( 769 SymbolTable::lookupNearestSymbolFrom(op, *comdat)); 770 var->setComdat(comdatMapping.lookup(selectorOp)); 771 } 772 773 if (op.getUnnamedAddr().has_value()) 774 var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr())); 775 776 if (op.getSection().has_value()) 777 var->setSection(*op.getSection()); 778 779 addRuntimePreemptionSpecifier(op.getDsoLocal(), var); 780 781 std::optional<uint64_t> alignment = op.getAlignment(); 782 if (alignment.has_value()) 783 var->setAlignment(llvm::MaybeAlign(alignment.value())); 784 785 var->setVisibility(convertVisibilityToLLVM(op.getVisibility_())); 786 787 globalsMapping.try_emplace(op, var); 788 789 // Add debug information if present. 790 if (op.getDbgExpr()) { 791 llvm::DIGlobalVariableExpression *diGlobalExpr = 792 debugTranslation->translateGlobalVariableExpression(op.getDbgExpr()); 793 llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable(); 794 var->addDebugInfo(diGlobalExpr); 795 796 // Get the compile unit (scope) of the the global variable. 797 if (llvm::DICompileUnit *compileUnit = 798 dyn_cast<llvm::DICompileUnit>(diGlobalVar->getScope())) { 799 // Update the compile unit with this incoming global variable expression 800 // during the finalizing step later. 801 allGVars[compileUnit].push_back(diGlobalExpr); 802 } 803 } 804 } 805 806 // Convert global variable bodies. This is done after all global variables 807 // have been created in LLVM IR because a global body may refer to another 808 // global or itself. So all global variables need to be mapped first. 809 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 810 if (Block *initializer = op.getInitializerBlock()) { 811 llvm::IRBuilder<> builder(llvmModule->getContext()); 812 for (auto &op : initializer->without_terminator()) { 813 if (failed(convertOperation(op, builder)) || 814 !isa<llvm::Constant>(lookupValue(op.getResult(0)))) 815 return emitError(op.getLoc(), "unemittable constant value"); 816 } 817 ReturnOp ret = cast<ReturnOp>(initializer->getTerminator()); 818 llvm::Constant *cst = 819 cast<llvm::Constant>(lookupValue(ret.getOperand(0))); 820 auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op)); 821 if (!shouldDropGlobalInitializer(global->getLinkage(), cst)) 822 global->setInitializer(cst); 823 } 824 } 825 826 // Convert llvm.mlir.global_ctors and dtors. 827 for (Operation &op : getModuleBody(mlirModule)) { 828 auto ctorOp = dyn_cast<GlobalCtorsOp>(op); 829 auto dtorOp = dyn_cast<GlobalDtorsOp>(op); 830 if (!ctorOp && !dtorOp) 831 continue; 832 auto range = ctorOp ? llvm::zip(ctorOp.getCtors(), ctorOp.getPriorities()) 833 : llvm::zip(dtorOp.getDtors(), dtorOp.getPriorities()); 834 auto appendGlobalFn = 835 ctorOp ? llvm::appendToGlobalCtors : llvm::appendToGlobalDtors; 836 for (auto symbolAndPriority : range) { 837 llvm::Function *f = lookupFunction( 838 cast<FlatSymbolRefAttr>(std::get<0>(symbolAndPriority)).getValue()); 839 appendGlobalFn(*llvmModule, f, 840 cast<IntegerAttr>(std::get<1>(symbolAndPriority)).getInt(), 841 /*Data=*/nullptr); 842 } 843 } 844 845 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) 846 if (failed(convertDialectAttributes(op))) 847 return failure(); 848 849 // Finally, update the compile units their respective sets of global variables 850 // created earlier. 851 for (const auto &[compileUnit, globals] : allGVars) { 852 compileUnit->replaceGlobalVariables( 853 llvm::MDTuple::get(getLLVMContext(), globals)); 854 } 855 856 return success(); 857 } 858 859 /// Attempts to add an attribute identified by `key`, optionally with the given 860 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the 861 /// attribute has a kind known to LLVM IR, create the attribute of this kind, 862 /// otherwise keep it as a string attribute. Performs additional checks for 863 /// attributes known to have or not have a value in order to avoid assertions 864 /// inside LLVM upon construction. 865 static LogicalResult checkedAddLLVMFnAttribute(Location loc, 866 llvm::Function *llvmFunc, 867 StringRef key, 868 StringRef value = StringRef()) { 869 auto kind = llvm::Attribute::getAttrKindFromName(key); 870 if (kind == llvm::Attribute::None) { 871 llvmFunc->addFnAttr(key, value); 872 return success(); 873 } 874 875 if (llvm::Attribute::isIntAttrKind(kind)) { 876 if (value.empty()) 877 return emitError(loc) << "LLVM attribute '" << key << "' expects a value"; 878 879 int64_t result; 880 if (!value.getAsInteger(/*Radix=*/0, result)) 881 llvmFunc->addFnAttr( 882 llvm::Attribute::get(llvmFunc->getContext(), kind, result)); 883 else 884 llvmFunc->addFnAttr(key, value); 885 return success(); 886 } 887 888 if (!value.empty()) 889 return emitError(loc) << "LLVM attribute '" << key 890 << "' does not expect a value, found '" << value 891 << "'"; 892 893 llvmFunc->addFnAttr(kind); 894 return success(); 895 } 896 897 /// Attaches the attributes listed in the given array attribute to `llvmFunc`. 898 /// Reports error to `loc` if any and returns immediately. Expects `attributes` 899 /// to be an array attribute containing either string attributes, treated as 900 /// value-less LLVM attributes, or array attributes containing two string 901 /// attributes, with the first string being the name of the corresponding LLVM 902 /// attribute and the second string beings its value. Note that even integer 903 /// attributes are expected to have their values expressed as strings. 904 static LogicalResult 905 forwardPassthroughAttributes(Location loc, std::optional<ArrayAttr> attributes, 906 llvm::Function *llvmFunc) { 907 if (!attributes) 908 return success(); 909 910 for (Attribute attr : *attributes) { 911 if (auto stringAttr = dyn_cast<StringAttr>(attr)) { 912 if (failed( 913 checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue()))) 914 return failure(); 915 continue; 916 } 917 918 auto arrayAttr = dyn_cast<ArrayAttr>(attr); 919 if (!arrayAttr || arrayAttr.size() != 2) 920 return emitError(loc) 921 << "expected 'passthrough' to contain string or array attributes"; 922 923 auto keyAttr = dyn_cast<StringAttr>(arrayAttr[0]); 924 auto valueAttr = dyn_cast<StringAttr>(arrayAttr[1]); 925 if (!keyAttr || !valueAttr) 926 return emitError(loc) 927 << "expected arrays within 'passthrough' to contain two strings"; 928 929 if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(), 930 valueAttr.getValue()))) 931 return failure(); 932 } 933 return success(); 934 } 935 936 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { 937 // Clear the block, branch value mappings, they are only relevant within one 938 // function. 939 blockMapping.clear(); 940 valueMapping.clear(); 941 branchMapping.clear(); 942 llvm::Function *llvmFunc = lookupFunction(func.getName()); 943 944 // Translate the debug information for this function. 945 debugTranslation->translate(func, *llvmFunc); 946 947 // Add function arguments to the value remapping table. 948 for (auto [mlirArg, llvmArg] : 949 llvm::zip(func.getArguments(), llvmFunc->args())) 950 mapValue(mlirArg, &llvmArg); 951 952 // Check the personality and set it. 953 if (func.getPersonality()) { 954 llvm::Type *ty = llvm::PointerType::getUnqual(llvmFunc->getContext()); 955 if (llvm::Constant *pfunc = getLLVMConstant(ty, func.getPersonalityAttr(), 956 func.getLoc(), *this)) 957 llvmFunc->setPersonalityFn(pfunc); 958 } 959 960 if (std::optional<StringRef> section = func.getSection()) 961 llvmFunc->setSection(*section); 962 963 if (func.getArmStreaming()) 964 llvmFunc->addFnAttr("aarch64_pstate_sm_enabled"); 965 else if (func.getArmLocallyStreaming()) 966 llvmFunc->addFnAttr("aarch64_pstate_sm_body"); 967 968 if (func.getArmNewZa()) 969 llvmFunc->addFnAttr("aarch64_pstate_za_new"); 970 971 if (auto attr = func.getVscaleRange()) 972 llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs( 973 getLLVMContext(), attr->getMinRange().getInt(), 974 attr->getMaxRange().getInt())); 975 976 // First, create all blocks so we can jump to them. 977 llvm::LLVMContext &llvmContext = llvmFunc->getContext(); 978 for (auto &bb : func) { 979 auto *llvmBB = llvm::BasicBlock::Create(llvmContext); 980 llvmBB->insertInto(llvmFunc); 981 mapBlock(&bb, llvmBB); 982 } 983 984 // Then, convert blocks one by one in topological order to ensure defs are 985 // converted before uses. 986 auto blocks = getTopologicallySortedBlocks(func.getBody()); 987 for (Block *bb : blocks) { 988 llvm::IRBuilder<> builder(llvmContext); 989 if (failed(convertBlock(*bb, bb->isEntryBlock(), builder))) 990 return failure(); 991 } 992 993 // After all blocks have been traversed and values mapped, connect the PHI 994 // nodes to the results of preceding blocks. 995 detail::connectPHINodes(func.getBody(), *this); 996 997 // Finally, convert dialect attributes attached to the function. 998 return convertDialectAttributes(func); 999 } 1000 1001 LogicalResult ModuleTranslation::convertDialectAttributes(Operation *op) { 1002 for (NamedAttribute attribute : op->getDialectAttrs()) 1003 if (failed(iface.amendOperation(op, attribute, *this))) 1004 return failure(); 1005 return success(); 1006 } 1007 1008 /// Converts the function attributes from LLVMFuncOp and attaches them to the 1009 /// llvm::Function. 1010 static void convertFunctionAttributes(LLVMFuncOp func, 1011 llvm::Function *llvmFunc) { 1012 if (!func.getMemory()) 1013 return; 1014 1015 MemoryEffectsAttr memEffects = func.getMemoryAttr(); 1016 1017 // Add memory effects incrementally. 1018 llvm::MemoryEffects newMemEffects = 1019 llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem, 1020 convertModRefInfoToLLVM(memEffects.getArgMem())); 1021 newMemEffects |= llvm::MemoryEffects( 1022 llvm::MemoryEffects::Location::InaccessibleMem, 1023 convertModRefInfoToLLVM(memEffects.getInaccessibleMem())); 1024 newMemEffects |= 1025 llvm::MemoryEffects(llvm::MemoryEffects::Location::Other, 1026 convertModRefInfoToLLVM(memEffects.getOther())); 1027 llvmFunc->setMemoryEffects(newMemEffects); 1028 } 1029 1030 llvm::AttrBuilder 1031 ModuleTranslation::convertParameterAttrs(DictionaryAttr paramAttrs) { 1032 llvm::AttrBuilder attrBuilder(llvmModule->getContext()); 1033 1034 for (auto [llvmKind, mlirName] : getAttrKindToNameMapping()) { 1035 Attribute attr = paramAttrs.get(mlirName); 1036 // Skip attributes that are not present. 1037 if (!attr) 1038 continue; 1039 1040 // NOTE: C++17 does not support capturing structured bindings. 1041 llvm::Attribute::AttrKind llvmKindCap = llvmKind; 1042 1043 llvm::TypeSwitch<Attribute>(attr) 1044 .Case<TypeAttr>([&](auto typeAttr) { 1045 attrBuilder.addTypeAttr(llvmKindCap, 1046 convertType(typeAttr.getValue())); 1047 }) 1048 .Case<IntegerAttr>([&](auto intAttr) { 1049 attrBuilder.addRawIntAttr(llvmKindCap, intAttr.getInt()); 1050 }) 1051 .Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKindCap); }); 1052 } 1053 1054 return attrBuilder; 1055 } 1056 1057 LogicalResult ModuleTranslation::convertFunctionSignatures() { 1058 // Declare all functions first because there may be function calls that form a 1059 // call graph with cycles, or global initializers that reference functions. 1060 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 1061 llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( 1062 function.getName(), 1063 cast<llvm::FunctionType>(convertType(function.getFunctionType()))); 1064 llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); 1065 llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage())); 1066 llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv())); 1067 mapFunction(function.getName(), llvmFunc); 1068 addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc); 1069 1070 // Convert function attributes. 1071 convertFunctionAttributes(function, llvmFunc); 1072 1073 // Convert function_entry_count attribute to metadata. 1074 if (std::optional<uint64_t> entryCount = function.getFunctionEntryCount()) 1075 llvmFunc->setEntryCount(entryCount.value()); 1076 1077 // Convert result attributes. 1078 if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) { 1079 DictionaryAttr resultAttrs = cast<DictionaryAttr>(allResultAttrs[0]); 1080 llvmFunc->addRetAttrs(convertParameterAttrs(resultAttrs)); 1081 } 1082 1083 // Convert argument attributes. 1084 for (auto [argIdx, llvmArg] : llvm::enumerate(llvmFunc->args())) { 1085 if (DictionaryAttr argAttrs = function.getArgAttrDict(argIdx)) { 1086 llvm::AttrBuilder attrBuilder = convertParameterAttrs(argAttrs); 1087 llvmArg.addAttrs(attrBuilder); 1088 } 1089 } 1090 1091 // Forward the pass-through attributes to LLVM. 1092 if (failed(forwardPassthroughAttributes( 1093 function.getLoc(), function.getPassthrough(), llvmFunc))) 1094 return failure(); 1095 1096 // Convert visibility attribute. 1097 llvmFunc->setVisibility(convertVisibilityToLLVM(function.getVisibility_())); 1098 1099 // Convert the comdat attribute. 1100 if (std::optional<mlir::SymbolRefAttr> comdat = function.getComdat()) { 1101 auto selectorOp = cast<ComdatSelectorOp>( 1102 SymbolTable::lookupNearestSymbolFrom(function, *comdat)); 1103 llvmFunc->setComdat(comdatMapping.lookup(selectorOp)); 1104 } 1105 1106 if (auto gc = function.getGarbageCollector()) 1107 llvmFunc->setGC(gc->str()); 1108 1109 if (auto unnamedAddr = function.getUnnamedAddr()) 1110 llvmFunc->setUnnamedAddr(convertUnnamedAddrToLLVM(*unnamedAddr)); 1111 1112 if (auto alignment = function.getAlignment()) 1113 llvmFunc->setAlignment(llvm::MaybeAlign(*alignment)); 1114 } 1115 1116 return success(); 1117 } 1118 1119 LogicalResult ModuleTranslation::convertFunctions() { 1120 // Convert functions. 1121 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 1122 // Do not convert external functions, but do process dialect attributes 1123 // attached to them. 1124 if (function.isExternal()) { 1125 if (failed(convertDialectAttributes(function))) 1126 return failure(); 1127 continue; 1128 } 1129 1130 if (failed(convertOneFunction(function))) 1131 return failure(); 1132 } 1133 1134 return success(); 1135 } 1136 1137 LogicalResult ModuleTranslation::convertComdats() { 1138 for (auto comdatOp : getModuleBody(mlirModule).getOps<ComdatOp>()) { 1139 for (auto selectorOp : comdatOp.getOps<ComdatSelectorOp>()) { 1140 llvm::Module *module = getLLVMModule(); 1141 if (module->getComdatSymbolTable().contains(selectorOp.getSymName())) 1142 return emitError(selectorOp.getLoc()) 1143 << "comdat selection symbols must be unique even in different " 1144 "comdat regions"; 1145 llvm::Comdat *comdat = module->getOrInsertComdat(selectorOp.getSymName()); 1146 comdat->setSelectionKind(convertComdatToLLVM(selectorOp.getComdat())); 1147 comdatMapping.try_emplace(selectorOp, comdat); 1148 } 1149 } 1150 return success(); 1151 } 1152 1153 void ModuleTranslation::setAccessGroupsMetadata(AccessGroupOpInterface op, 1154 llvm::Instruction *inst) { 1155 if (llvm::MDNode *node = loopAnnotationTranslation->getAccessGroups(op)) 1156 inst->setMetadata(llvm::LLVMContext::MD_access_group, node); 1157 } 1158 1159 llvm::MDNode * 1160 ModuleTranslation::getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr) { 1161 auto [scopeIt, scopeInserted] = 1162 aliasScopeMetadataMapping.try_emplace(aliasScopeAttr, nullptr); 1163 if (!scopeInserted) 1164 return scopeIt->second; 1165 llvm::LLVMContext &ctx = llvmModule->getContext(); 1166 // Convert the domain metadata node if necessary. 1167 auto [domainIt, insertedDomain] = aliasDomainMetadataMapping.try_emplace( 1168 aliasScopeAttr.getDomain(), nullptr); 1169 if (insertedDomain) { 1170 llvm::SmallVector<llvm::Metadata *, 2> operands; 1171 // Placeholder for self-reference. 1172 operands.push_back({}); 1173 if (StringAttr description = aliasScopeAttr.getDomain().getDescription()) 1174 operands.push_back(llvm::MDString::get(ctx, description)); 1175 domainIt->second = llvm::MDNode::get(ctx, operands); 1176 // Self-reference for uniqueness. 1177 domainIt->second->replaceOperandWith(0, domainIt->second); 1178 } 1179 // Convert the scope metadata node. 1180 assert(domainIt->second && "Scope's domain should already be valid"); 1181 llvm::SmallVector<llvm::Metadata *, 3> operands; 1182 // Placeholder for self-reference. 1183 operands.push_back({}); 1184 operands.push_back(domainIt->second); 1185 if (StringAttr description = aliasScopeAttr.getDescription()) 1186 operands.push_back(llvm::MDString::get(ctx, description)); 1187 scopeIt->second = llvm::MDNode::get(ctx, operands); 1188 // Self-reference for uniqueness. 1189 scopeIt->second->replaceOperandWith(0, scopeIt->second); 1190 return scopeIt->second; 1191 } 1192 1193 llvm::MDNode *ModuleTranslation::getOrCreateAliasScopes( 1194 ArrayRef<AliasScopeAttr> aliasScopeAttrs) { 1195 SmallVector<llvm::Metadata *> nodes; 1196 nodes.reserve(aliasScopeAttrs.size()); 1197 for (AliasScopeAttr aliasScopeAttr : aliasScopeAttrs) 1198 nodes.push_back(getOrCreateAliasScope(aliasScopeAttr)); 1199 return llvm::MDNode::get(getLLVMContext(), nodes); 1200 } 1201 1202 void ModuleTranslation::setAliasScopeMetadata(AliasAnalysisOpInterface op, 1203 llvm::Instruction *inst) { 1204 auto populateScopeMetadata = [&](ArrayAttr aliasScopeAttrs, unsigned kind) { 1205 if (!aliasScopeAttrs || aliasScopeAttrs.empty()) 1206 return; 1207 llvm::MDNode *node = getOrCreateAliasScopes( 1208 llvm::to_vector(aliasScopeAttrs.getAsRange<AliasScopeAttr>())); 1209 inst->setMetadata(kind, node); 1210 }; 1211 1212 populateScopeMetadata(op.getAliasScopesOrNull(), 1213 llvm::LLVMContext::MD_alias_scope); 1214 populateScopeMetadata(op.getNoAliasScopesOrNull(), 1215 llvm::LLVMContext::MD_noalias); 1216 } 1217 1218 llvm::MDNode *ModuleTranslation::getTBAANode(TBAATagAttr tbaaAttr) const { 1219 return tbaaMetadataMapping.lookup(tbaaAttr); 1220 } 1221 1222 void ModuleTranslation::setTBAAMetadata(AliasAnalysisOpInterface op, 1223 llvm::Instruction *inst) { 1224 ArrayAttr tagRefs = op.getTBAATagsOrNull(); 1225 if (!tagRefs || tagRefs.empty()) 1226 return; 1227 1228 // LLVM IR currently does not support attaching more than one TBAA access tag 1229 // to a memory accessing instruction. It may be useful to support this in 1230 // future, but for the time being just ignore the metadata if MLIR operation 1231 // has multiple access tags. 1232 if (tagRefs.size() > 1) { 1233 op.emitWarning() << "TBAA access tags were not translated, because LLVM " 1234 "IR only supports a single tag per instruction"; 1235 return; 1236 } 1237 1238 llvm::MDNode *node = getTBAANode(cast<TBAATagAttr>(tagRefs[0])); 1239 inst->setMetadata(llvm::LLVMContext::MD_tbaa, node); 1240 } 1241 1242 void ModuleTranslation::setBranchWeightsMetadata(BranchWeightOpInterface op) { 1243 DenseI32ArrayAttr weightsAttr = op.getBranchWeightsOrNull(); 1244 if (!weightsAttr) 1245 return; 1246 1247 llvm::Instruction *inst = isa<CallOp>(op) ? lookupCall(op) : lookupBranch(op); 1248 assert(inst && "expected the operation to have a mapping to an instruction"); 1249 SmallVector<uint32_t> weights(weightsAttr.asArrayRef()); 1250 inst->setMetadata( 1251 llvm::LLVMContext::MD_prof, 1252 llvm::MDBuilder(getLLVMContext()).createBranchWeights(weights)); 1253 } 1254 1255 LogicalResult ModuleTranslation::createTBAAMetadata() { 1256 llvm::LLVMContext &ctx = llvmModule->getContext(); 1257 llvm::IntegerType *offsetTy = llvm::IntegerType::get(ctx, 64); 1258 1259 // Walk the entire module and create all metadata nodes for the TBAA 1260 // attributes. The code below relies on two invariants of the 1261 // `AttrTypeWalker`: 1262 // 1. Attributes are visited in post-order: Since the attributes create a DAG, 1263 // this ensures that any lookups into `tbaaMetadataMapping` for child 1264 // attributes succeed. 1265 // 2. Attributes are only ever visited once: This way we don't leak any 1266 // LLVM metadata instances. 1267 AttrTypeWalker walker; 1268 walker.addWalk([&](TBAARootAttr root) { 1269 tbaaMetadataMapping.insert( 1270 {root, llvm::MDNode::get(ctx, llvm::MDString::get(ctx, root.getId()))}); 1271 }); 1272 1273 walker.addWalk([&](TBAATypeDescriptorAttr descriptor) { 1274 SmallVector<llvm::Metadata *> operands; 1275 operands.push_back(llvm::MDString::get(ctx, descriptor.getId())); 1276 for (TBAAMemberAttr member : descriptor.getMembers()) { 1277 operands.push_back(tbaaMetadataMapping.lookup(member.getTypeDesc())); 1278 operands.push_back(llvm::ConstantAsMetadata::get( 1279 llvm::ConstantInt::get(offsetTy, member.getOffset()))); 1280 } 1281 1282 tbaaMetadataMapping.insert({descriptor, llvm::MDNode::get(ctx, operands)}); 1283 }); 1284 1285 walker.addWalk([&](TBAATagAttr tag) { 1286 SmallVector<llvm::Metadata *> operands; 1287 1288 operands.push_back(tbaaMetadataMapping.lookup(tag.getBaseType())); 1289 operands.push_back(tbaaMetadataMapping.lookup(tag.getAccessType())); 1290 1291 operands.push_back(llvm::ConstantAsMetadata::get( 1292 llvm::ConstantInt::get(offsetTy, tag.getOffset()))); 1293 if (tag.getConstant()) 1294 operands.push_back( 1295 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(offsetTy, 1))); 1296 1297 tbaaMetadataMapping.insert({tag, llvm::MDNode::get(ctx, operands)}); 1298 }); 1299 1300 mlirModule->walk([&](AliasAnalysisOpInterface analysisOpInterface) { 1301 if (auto attr = analysisOpInterface.getTBAATagsOrNull()) 1302 walker.walk(attr); 1303 }); 1304 1305 return success(); 1306 } 1307 1308 void ModuleTranslation::setLoopMetadata(Operation *op, 1309 llvm::Instruction *inst) { 1310 LoopAnnotationAttr attr = 1311 TypeSwitch<Operation *, LoopAnnotationAttr>(op) 1312 .Case<LLVM::BrOp, LLVM::CondBrOp>( 1313 [](auto branchOp) { return branchOp.getLoopAnnotationAttr(); }); 1314 if (!attr) 1315 return; 1316 llvm::MDNode *loopMD = 1317 loopAnnotationTranslation->translateLoopAnnotation(attr, op); 1318 inst->setMetadata(llvm::LLVMContext::MD_loop, loopMD); 1319 } 1320 1321 llvm::Type *ModuleTranslation::convertType(Type type) { 1322 return typeTranslator.translateType(type); 1323 } 1324 1325 /// A helper to look up remapped operands in the value remapping table. 1326 SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) { 1327 SmallVector<llvm::Value *> remapped; 1328 remapped.reserve(values.size()); 1329 for (Value v : values) 1330 remapped.push_back(lookupValue(v)); 1331 return remapped; 1332 } 1333 1334 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() { 1335 if (!ompBuilder) { 1336 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule); 1337 ompBuilder->initialize(); 1338 1339 // Flags represented as top-level OpenMP dialect attributes are set in 1340 // `OpenMPDialectLLVMIRTranslationInterface::amendOperation()`. Here we set 1341 // the default configuration. 1342 ompBuilder->setConfig(llvm::OpenMPIRBuilderConfig( 1343 /* IsTargetDevice = */ false, /* IsGPU = */ false, 1344 /* OpenMPOffloadMandatory = */ false, 1345 /* HasRequiresReverseOffload = */ false, 1346 /* HasRequiresUnifiedAddress = */ false, 1347 /* HasRequiresUnifiedSharedMemory = */ false, 1348 /* HasRequiresDynamicAllocators = */ false)); 1349 } 1350 return ompBuilder.get(); 1351 } 1352 1353 llvm::DILocation *ModuleTranslation::translateLoc(Location loc, 1354 llvm::DILocalScope *scope) { 1355 return debugTranslation->translateLoc(loc, scope); 1356 } 1357 1358 llvm::DIExpression * 1359 ModuleTranslation::translateExpression(LLVM::DIExpressionAttr attr) { 1360 return debugTranslation->translateExpression(attr); 1361 } 1362 1363 llvm::DIGlobalVariableExpression * 1364 ModuleTranslation::translateGlobalVariableExpression( 1365 LLVM::DIGlobalVariableExpressionAttr attr) { 1366 return debugTranslation->translateGlobalVariableExpression(attr); 1367 } 1368 1369 llvm::Metadata *ModuleTranslation::translateDebugInfo(LLVM::DINodeAttr attr) { 1370 return debugTranslation->translate(attr); 1371 } 1372 1373 llvm::NamedMDNode * 1374 ModuleTranslation::getOrInsertNamedModuleMetadata(StringRef name) { 1375 return llvmModule->getOrInsertNamedMetadata(name); 1376 } 1377 1378 void ModuleTranslation::StackFrame::anchor() {} 1379 1380 static std::unique_ptr<llvm::Module> 1381 prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext, 1382 StringRef name) { 1383 m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>(); 1384 auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext); 1385 if (auto dataLayoutAttr = 1386 m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) { 1387 llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue()); 1388 } else { 1389 FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout("")); 1390 if (auto iface = dyn_cast<DataLayoutOpInterface>(m)) { 1391 if (DataLayoutSpecInterface spec = iface.getDataLayoutSpec()) { 1392 llvmDataLayout = 1393 translateDataLayout(spec, DataLayout(iface), m->getLoc()); 1394 } 1395 } else if (auto mod = dyn_cast<ModuleOp>(m)) { 1396 if (DataLayoutSpecInterface spec = mod.getDataLayoutSpec()) { 1397 llvmDataLayout = 1398 translateDataLayout(spec, DataLayout(mod), m->getLoc()); 1399 } 1400 } 1401 if (failed(llvmDataLayout)) 1402 return nullptr; 1403 llvmModule->setDataLayout(*llvmDataLayout); 1404 } 1405 if (auto targetTripleAttr = 1406 m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) 1407 llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue()); 1408 1409 return llvmModule; 1410 } 1411 1412 std::unique_ptr<llvm::Module> 1413 mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, 1414 StringRef name) { 1415 if (!satisfiesLLVMModule(module)) { 1416 module->emitOpError("can not be translated to an LLVMIR module"); 1417 return nullptr; 1418 } 1419 1420 std::unique_ptr<llvm::Module> llvmModule = 1421 prepareLLVMModule(module, llvmContext, name); 1422 if (!llvmModule) 1423 return nullptr; 1424 1425 LLVM::ensureDistinctSuccessors(module); 1426 1427 ModuleTranslation translator(module, std::move(llvmModule)); 1428 llvm::IRBuilder<> llvmBuilder(llvmContext); 1429 1430 // Convert module before functions and operations inside, so dialect 1431 // attributes can be used to change dialect-specific global configurations via 1432 // `amendOperation()`. These configurations can then influence the translation 1433 // of operations afterwards. 1434 if (failed(translator.convertOperation(*module, llvmBuilder))) 1435 return nullptr; 1436 1437 if (failed(translator.convertComdats())) 1438 return nullptr; 1439 if (failed(translator.convertFunctionSignatures())) 1440 return nullptr; 1441 if (failed(translator.convertGlobals())) 1442 return nullptr; 1443 if (failed(translator.createTBAAMetadata())) 1444 return nullptr; 1445 1446 // Convert other top-level operations if possible. 1447 for (Operation &o : getModuleBody(module).getOperations()) { 1448 if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp, LLVM::GlobalCtorsOp, 1449 LLVM::GlobalDtorsOp, LLVM::ComdatOp>(&o) && 1450 !o.hasTrait<OpTrait::IsTerminator>() && 1451 failed(translator.convertOperation(o, llvmBuilder))) { 1452 return nullptr; 1453 } 1454 } 1455 1456 // Operations in function bodies with symbolic references must be converted 1457 // after the top-level operations they refer to are declared, so we do it 1458 // last. 1459 if (failed(translator.convertFunctions())) 1460 return nullptr; 1461 1462 if (llvm::verifyModule(*translator.llvmModule, &llvm::errs())) 1463 return nullptr; 1464 1465 return std::move(translator.llvmModule); 1466 } 1467