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