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