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