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