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 "DebugTranslation.h" 17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 18 #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h" 19 #include "mlir/Dialect/OpenMP/OpenMPDialect.h" 20 #include "mlir/IR/Attributes.h" 21 #include "mlir/IR/BuiltinOps.h" 22 #include "mlir/IR/BuiltinTypes.h" 23 #include "mlir/IR/RegionGraphTraits.h" 24 #include "mlir/Support/LLVM.h" 25 #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" 26 #include "mlir/Target/LLVMIR/TypeToLLVM.h" 27 #include "llvm/ADT/TypeSwitch.h" 28 29 #include "llvm/ADT/PostOrderIterator.h" 30 #include "llvm/ADT/SetVector.h" 31 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 32 #include "llvm/IR/BasicBlock.h" 33 #include "llvm/IR/CFG.h" 34 #include "llvm/IR/Constants.h" 35 #include "llvm/IR/DerivedTypes.h" 36 #include "llvm/IR/IRBuilder.h" 37 #include "llvm/IR/InlineAsm.h" 38 #include "llvm/IR/IntrinsicsNVPTX.h" 39 #include "llvm/IR/LLVMContext.h" 40 #include "llvm/IR/MDBuilder.h" 41 #include "llvm/IR/Module.h" 42 #include "llvm/IR/Verifier.h" 43 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 44 #include "llvm/Transforms/Utils/Cloning.h" 45 46 using namespace mlir; 47 using namespace mlir::LLVM; 48 using namespace mlir::LLVM::detail; 49 50 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc" 51 52 /// Builds a constant of a sequential LLVM type `type`, potentially containing 53 /// other sequential types recursively, from the individual constant values 54 /// provided in `constants`. `shape` contains the number of elements in nested 55 /// sequential types. Reports errors at `loc` and returns nullptr on error. 56 static llvm::Constant * 57 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants, 58 ArrayRef<int64_t> shape, llvm::Type *type, 59 Location loc) { 60 if (shape.empty()) { 61 llvm::Constant *result = constants.front(); 62 constants = constants.drop_front(); 63 return result; 64 } 65 66 llvm::Type *elementType; 67 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 68 elementType = arrayTy->getElementType(); 69 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 70 elementType = vectorTy->getElementType(); 71 } else { 72 emitError(loc) << "expected sequential LLVM types wrapping a scalar"; 73 return nullptr; 74 } 75 76 SmallVector<llvm::Constant *, 8> nested; 77 nested.reserve(shape.front()); 78 for (int64_t i = 0; i < shape.front(); ++i) { 79 nested.push_back(buildSequentialConstant(constants, shape.drop_front(), 80 elementType, loc)); 81 if (!nested.back()) 82 return nullptr; 83 } 84 85 if (shape.size() == 1 && type->isVectorTy()) 86 return llvm::ConstantVector::get(nested); 87 return llvm::ConstantArray::get( 88 llvm::ArrayType::get(elementType, shape.front()), nested); 89 } 90 91 /// Returns the first non-sequential type nested in sequential types. 92 static llvm::Type *getInnermostElementType(llvm::Type *type) { 93 do { 94 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 95 type = arrayTy->getElementType(); 96 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 97 type = vectorTy->getElementType(); 98 } else { 99 return type; 100 } 101 } while (true); 102 } 103 104 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`. 105 /// This currently supports integer, floating point, splat and dense element 106 /// attributes and combinations thereof. Also, an array attribute with two 107 /// elements is supported to represent a complex constant. In case of error, 108 /// report it to `loc` and return nullptr. 109 llvm::Constant *mlir::LLVM::detail::getLLVMConstant( 110 llvm::Type *llvmType, Attribute attr, Location loc, 111 const ModuleTranslation &moduleTranslation, bool isTopLevel) { 112 if (!attr) 113 return llvm::UndefValue::get(llvmType); 114 if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) { 115 if (!isTopLevel) { 116 emitError(loc, "nested struct types are not supported in constants"); 117 return nullptr; 118 } 119 auto arrayAttr = attr.cast<ArrayAttr>(); 120 llvm::Type *elementType = structType->getElementType(0); 121 llvm::Constant *real = getLLVMConstant(elementType, arrayAttr[0], loc, 122 moduleTranslation, false); 123 if (!real) 124 return nullptr; 125 llvm::Constant *imag = getLLVMConstant(elementType, arrayAttr[1], loc, 126 moduleTranslation, false); 127 if (!imag) 128 return nullptr; 129 return llvm::ConstantStruct::get(structType, {real, imag}); 130 } 131 // For integer types, we allow a mismatch in sizes as the index type in 132 // MLIR might have a different size than the index type in the LLVM module. 133 if (auto intAttr = attr.dyn_cast<IntegerAttr>()) 134 return llvm::ConstantInt::get( 135 llvmType, 136 intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth())); 137 if (auto floatAttr = attr.dyn_cast<FloatAttr>()) { 138 if (llvmType != 139 llvm::Type::getFloatingPointTy(llvmType->getContext(), 140 floatAttr.getValue().getSemantics())) { 141 emitError(loc, "FloatAttr does not match expected type of the constant"); 142 return nullptr; 143 } 144 return llvm::ConstantFP::get(llvmType, floatAttr.getValue()); 145 } 146 if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>()) 147 return llvm::ConstantExpr::getBitCast( 148 moduleTranslation.lookupFunction(funcAttr.getValue()), llvmType); 149 if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) { 150 llvm::Type *elementType; 151 uint64_t numElements; 152 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) { 153 elementType = arrayTy->getElementType(); 154 numElements = arrayTy->getNumElements(); 155 } else { 156 auto *vectorTy = cast<llvm::FixedVectorType>(llvmType); 157 elementType = vectorTy->getElementType(); 158 numElements = vectorTy->getNumElements(); 159 } 160 // Splat value is a scalar. Extract it only if the element type is not 161 // another sequence type. The recursion terminates because each step removes 162 // one outer sequential type. 163 bool elementTypeSequential = 164 isa<llvm::ArrayType, llvm::VectorType>(elementType); 165 llvm::Constant *child = getLLVMConstant( 166 elementType, 167 elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc, 168 moduleTranslation, false); 169 if (!child) 170 return nullptr; 171 if (llvmType->isVectorTy()) 172 return llvm::ConstantVector::getSplat( 173 llvm::ElementCount::get(numElements, /*Scalable=*/false), child); 174 if (llvmType->isArrayTy()) { 175 auto *arrayType = llvm::ArrayType::get(elementType, numElements); 176 SmallVector<llvm::Constant *, 8> constants(numElements, child); 177 return llvm::ConstantArray::get(arrayType, constants); 178 } 179 } 180 181 if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) { 182 assert(elementsAttr.getType().hasStaticShape()); 183 assert(!elementsAttr.getType().getShape().empty() && 184 "unexpected empty elements attribute shape"); 185 186 SmallVector<llvm::Constant *, 8> constants; 187 constants.reserve(elementsAttr.getNumElements()); 188 llvm::Type *innermostType = getInnermostElementType(llvmType); 189 for (auto n : elementsAttr.getValues<Attribute>()) { 190 constants.push_back( 191 getLLVMConstant(innermostType, n, loc, moduleTranslation, false)); 192 if (!constants.back()) 193 return nullptr; 194 } 195 ArrayRef<llvm::Constant *> constantsRef = constants; 196 llvm::Constant *result = buildSequentialConstant( 197 constantsRef, elementsAttr.getType().getShape(), llvmType, loc); 198 assert(constantsRef.empty() && "did not consume all elemental constants"); 199 return result; 200 } 201 202 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 203 return llvm::ConstantDataArray::get( 204 moduleTranslation.getLLVMContext(), 205 ArrayRef<char>{stringAttr.getValue().data(), 206 stringAttr.getValue().size()}); 207 } 208 emitError(loc, "unsupported constant value"); 209 return nullptr; 210 } 211 212 ModuleTranslation::ModuleTranslation(Operation *module, 213 std::unique_ptr<llvm::Module> llvmModule) 214 : mlirModule(module), llvmModule(std::move(llvmModule)), 215 debugTranslation( 216 std::make_unique<DebugTranslation>(module, *this->llvmModule)), 217 typeTranslator(this->llvmModule->getContext()), 218 iface(module->getContext()) { 219 assert(satisfiesLLVMModule(mlirModule) && 220 "mlirModule should honor LLVM's module semantics."); 221 } 222 ModuleTranslation::~ModuleTranslation() { 223 if (ompBuilder) 224 ompBuilder->finalize(); 225 } 226 227 void ModuleTranslation::forgetMapping(Region ®ion) { 228 SmallVector<Region *> toProcess; 229 toProcess.push_back(®ion); 230 while (!toProcess.empty()) { 231 Region *current = toProcess.pop_back_val(); 232 for (Block &block : *current) { 233 blockMapping.erase(&block); 234 for (Value arg : block.getArguments()) 235 valueMapping.erase(arg); 236 for (Operation &op : block) { 237 for (Value value : op.getResults()) 238 valueMapping.erase(value); 239 if (op.hasSuccessors()) 240 branchMapping.erase(&op); 241 if (isa<LLVM::GlobalOp>(op)) 242 globalsMapping.erase(&op); 243 accessGroupMetadataMapping.erase(&op); 244 llvm::append_range( 245 toProcess, 246 llvm::map_range(op.getRegions(), [](Region &r) { return &r; })); 247 } 248 } 249 } 250 } 251 252 /// Get the SSA value passed to the current block from the terminator operation 253 /// of its predecessor. 254 static Value getPHISourceValue(Block *current, Block *pred, 255 unsigned numArguments, unsigned index) { 256 Operation &terminator = *pred->getTerminator(); 257 if (isa<LLVM::BrOp>(terminator)) 258 return terminator.getOperand(index); 259 260 SuccessorRange successors = terminator.getSuccessors(); 261 assert(std::adjacent_find(successors.begin(), successors.end()) == 262 successors.end() && 263 "successors with arguments in LLVM branches must be different blocks"); 264 (void)successors; 265 266 // For instructions that branch based on a condition value, we need to take 267 // the operands for the branch that was taken. 268 if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) { 269 // For conditional branches, we take the operands from either the "true" or 270 // the "false" branch. 271 return condBranchOp.getSuccessor(0) == current 272 ? condBranchOp.trueDestOperands()[index] 273 : condBranchOp.falseDestOperands()[index]; 274 } 275 276 if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) { 277 // For switches, we take the operands from either the default case, or from 278 // the case branch that was taken. 279 if (switchOp.defaultDestination() == current) 280 return switchOp.defaultOperands()[index]; 281 for (auto i : llvm::enumerate(switchOp.caseDestinations())) 282 if (i.value() == current) 283 return switchOp.getCaseOperands(i.index())[index]; 284 } 285 286 llvm_unreachable("only branch or switch operations can be terminators of a " 287 "block that has successors"); 288 } 289 290 /// Connect the PHI nodes to the results of preceding blocks. 291 void mlir::LLVM::detail::connectPHINodes(Region ®ion, 292 const ModuleTranslation &state) { 293 // Skip the first block, it cannot be branched to and its arguments correspond 294 // to the arguments of the LLVM function. 295 for (auto it = std::next(region.begin()), eit = region.end(); it != eit; 296 ++it) { 297 Block *bb = &*it; 298 llvm::BasicBlock *llvmBB = state.lookupBlock(bb); 299 auto phis = llvmBB->phis(); 300 auto numArguments = bb->getNumArguments(); 301 assert(numArguments == std::distance(phis.begin(), phis.end())); 302 for (auto &numberedPhiNode : llvm::enumerate(phis)) { 303 auto &phiNode = numberedPhiNode.value(); 304 unsigned index = numberedPhiNode.index(); 305 for (auto *pred : bb->getPredecessors()) { 306 // Find the LLVM IR block that contains the converted terminator 307 // instruction and use it in the PHI node. Note that this block is not 308 // necessarily the same as state.lookupBlock(pred), some operations 309 // (in particular, OpenMP operations using OpenMPIRBuilder) may have 310 // split the blocks. 311 llvm::Instruction *terminator = 312 state.lookupBranch(pred->getTerminator()); 313 assert(terminator && "missing the mapping for a terminator"); 314 phiNode.addIncoming( 315 state.lookupValue(getPHISourceValue(bb, pred, numArguments, index)), 316 terminator->getParent()); 317 } 318 } 319 } 320 } 321 322 /// Sort function blocks topologically. 323 SetVector<Block *> 324 mlir::LLVM::detail::getTopologicallySortedBlocks(Region ®ion) { 325 // For each block that has not been visited yet (i.e. that has no 326 // predecessors), add it to the list as well as its successors. 327 SetVector<Block *> blocks; 328 for (Block &b : region) { 329 if (blocks.count(&b) == 0) { 330 llvm::ReversePostOrderTraversal<Block *> traversal(&b); 331 blocks.insert(traversal.begin(), traversal.end()); 332 } 333 } 334 assert(blocks.size() == region.getBlocks().size() && 335 "some blocks are not sorted"); 336 337 return blocks; 338 } 339 340 llvm::Value *mlir::LLVM::detail::createIntrinsicCall( 341 llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, 342 ArrayRef<llvm::Value *> args, ArrayRef<llvm::Type *> tys) { 343 llvm::Module *module = builder.GetInsertBlock()->getModule(); 344 llvm::Function *fn = llvm::Intrinsic::getDeclaration(module, intrinsic, tys); 345 return builder.CreateCall(fn, args); 346 } 347 348 llvm::Value * 349 mlir::LLVM::detail::createNvvmIntrinsicCall(llvm::IRBuilderBase &builder, 350 llvm::Intrinsic::ID intrinsic, 351 ArrayRef<llvm::Value *> args) { 352 llvm::Module *module = builder.GetInsertBlock()->getModule(); 353 llvm::Function *fn; 354 if (llvm::Intrinsic::isOverloaded(intrinsic)) { 355 if (intrinsic != llvm::Intrinsic::nvvm_wmma_m16n16k16_mma_row_row_f16_f16 && 356 intrinsic != llvm::Intrinsic::nvvm_wmma_m16n16k16_mma_row_row_f32_f32) { 357 // NVVM load and store instrinsic names are overloaded on the 358 // source/destination pointer type. Pointer is the first argument in the 359 // corresponding NVVM Op. 360 fn = llvm::Intrinsic::getDeclaration(module, intrinsic, 361 {args[0]->getType()}); 362 } else { 363 fn = llvm::Intrinsic::getDeclaration(module, intrinsic, {}); 364 } 365 } else { 366 fn = llvm::Intrinsic::getDeclaration(module, intrinsic); 367 } 368 return builder.CreateCall(fn, args); 369 } 370 371 /// Given a single MLIR operation, create the corresponding LLVM IR operation 372 /// using the `builder`. 373 LogicalResult 374 ModuleTranslation::convertOperation(Operation &op, 375 llvm::IRBuilderBase &builder) { 376 const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op); 377 if (!opIface) 378 return op.emitError("cannot be converted to LLVM IR: missing " 379 "`LLVMTranslationDialectInterface` registration for " 380 "dialect for op: ") 381 << op.getName(); 382 383 if (failed(opIface->convertOperation(&op, builder, *this))) 384 return op.emitError("LLVM Translation failed for operation: ") 385 << op.getName(); 386 387 return convertDialectAttributes(&op); 388 } 389 390 /// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes 391 /// to define values corresponding to the MLIR block arguments. These nodes 392 /// are not connected to the source basic blocks, which may not exist yet. Uses 393 /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have 394 /// been created for `bb` and included in the block mapping. Inserts new 395 /// instructions at the end of the block and leaves `builder` in a state 396 /// suitable for further insertion into the end of the block. 397 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments, 398 llvm::IRBuilderBase &builder) { 399 builder.SetInsertPoint(lookupBlock(&bb)); 400 auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram(); 401 402 // Before traversing operations, make block arguments available through 403 // value remapping and PHI nodes, but do not add incoming edges for the PHI 404 // nodes just yet: those values may be defined by this or following blocks. 405 // This step is omitted if "ignoreArguments" is set. The arguments of the 406 // first block have been already made available through the remapping of 407 // LLVM function arguments. 408 if (!ignoreArguments) { 409 auto predecessors = bb.getPredecessors(); 410 unsigned numPredecessors = 411 std::distance(predecessors.begin(), predecessors.end()); 412 for (auto arg : bb.getArguments()) { 413 auto wrappedType = arg.getType(); 414 if (!isCompatibleType(wrappedType)) 415 return emitError(bb.front().getLoc(), 416 "block argument does not have an LLVM type"); 417 llvm::Type *type = convertType(wrappedType); 418 llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors); 419 mapValue(arg, phi); 420 } 421 } 422 423 // Traverse operations. 424 for (auto &op : bb) { 425 // Set the current debug location within the builder. 426 builder.SetCurrentDebugLocation( 427 debugTranslation->translateLoc(op.getLoc(), subprogram)); 428 429 if (failed(convertOperation(op, builder))) 430 return failure(); 431 } 432 433 return success(); 434 } 435 436 /// A helper method to get the single Block in an operation honoring LLVM's 437 /// module requirements. 438 static Block &getModuleBody(Operation *module) { 439 return module->getRegion(0).front(); 440 } 441 442 /// A helper method to decide if a constant must not be set as a global variable 443 /// initializer. For an external linkage variable, the variable with an 444 /// initializer is considered externally visible and defined in this module, the 445 /// variable without an initializer is externally available and is defined 446 /// elsewhere. 447 static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage, 448 llvm::Constant *cst) { 449 return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) || 450 linkage == llvm::GlobalVariable::ExternalWeakLinkage; 451 } 452 453 /// Sets the runtime preemption specifier of `gv` to dso_local if 454 /// `dsoLocalRequested` is true, otherwise it is left unchanged. 455 static void addRuntimePreemptionSpecifier(bool dsoLocalRequested, 456 llvm::GlobalValue *gv) { 457 if (dsoLocalRequested) 458 gv->setDSOLocal(true); 459 } 460 461 /// Create named global variables that correspond to llvm.mlir.global 462 /// definitions. 463 LogicalResult ModuleTranslation::convertGlobals() { 464 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 465 llvm::Type *type = convertType(op.getType()); 466 llvm::Constant *cst = nullptr; 467 if (op.getValueOrNull()) { 468 // String attributes are treated separately because they cannot appear as 469 // in-function constants and are thus not supported by getLLVMConstant. 470 if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) { 471 cst = llvm::ConstantDataArray::getString( 472 llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); 473 type = cst->getType(); 474 } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc(), 475 *this))) { 476 return failure(); 477 } 478 } 479 480 auto linkage = convertLinkageToLLVM(op.linkage()); 481 auto addrSpace = op.addr_space(); 482 483 // LLVM IR requires constant with linkage other than external or weak 484 // external to have initializers. If MLIR does not provide an initializer, 485 // default to undef. 486 bool dropInitializer = shouldDropGlobalInitializer(linkage, cst); 487 if (!dropInitializer && !cst) 488 cst = llvm::UndefValue::get(type); 489 else if (dropInitializer && cst) 490 cst = nullptr; 491 492 auto *var = new llvm::GlobalVariable( 493 *llvmModule, type, op.constant(), linkage, cst, op.sym_name(), 494 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace); 495 496 if (op.unnamed_addr().hasValue()) 497 var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.unnamed_addr())); 498 499 if (op.section().hasValue()) 500 var->setSection(*op.section()); 501 502 addRuntimePreemptionSpecifier(op.dso_local(), var); 503 504 Optional<uint64_t> alignment = op.alignment(); 505 if (alignment.hasValue()) 506 var->setAlignment(llvm::MaybeAlign(alignment.getValue())); 507 508 globalsMapping.try_emplace(op, var); 509 } 510 511 // Convert global variable bodies. This is done after all global variables 512 // have been created in LLVM IR because a global body may refer to another 513 // global or itself. So all global variables need to be mapped first. 514 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 515 if (Block *initializer = op.getInitializerBlock()) { 516 llvm::IRBuilder<> builder(llvmModule->getContext()); 517 for (auto &op : initializer->without_terminator()) { 518 if (failed(convertOperation(op, builder)) || 519 !isa<llvm::Constant>(lookupValue(op.getResult(0)))) 520 return emitError(op.getLoc(), "unemittable constant value"); 521 } 522 ReturnOp ret = cast<ReturnOp>(initializer->getTerminator()); 523 llvm::Constant *cst = 524 cast<llvm::Constant>(lookupValue(ret.getOperand(0))); 525 auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op)); 526 if (!shouldDropGlobalInitializer(global->getLinkage(), cst)) 527 global->setInitializer(cst); 528 } 529 } 530 531 return success(); 532 } 533 534 /// Attempts to add an attribute identified by `key`, optionally with the given 535 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the 536 /// attribute has a kind known to LLVM IR, create the attribute of this kind, 537 /// otherwise keep it as a string attribute. Performs additional checks for 538 /// attributes known to have or not have a value in order to avoid assertions 539 /// inside LLVM upon construction. 540 static LogicalResult checkedAddLLVMFnAttribute(Location loc, 541 llvm::Function *llvmFunc, 542 StringRef key, 543 StringRef value = StringRef()) { 544 auto kind = llvm::Attribute::getAttrKindFromName(key); 545 if (kind == llvm::Attribute::None) { 546 llvmFunc->addFnAttr(key, value); 547 return success(); 548 } 549 550 if (llvm::Attribute::isIntAttrKind(kind)) { 551 if (value.empty()) 552 return emitError(loc) << "LLVM attribute '" << key << "' expects a value"; 553 554 int result; 555 if (!value.getAsInteger(/*Radix=*/0, result)) 556 llvmFunc->addFnAttr( 557 llvm::Attribute::get(llvmFunc->getContext(), kind, result)); 558 else 559 llvmFunc->addFnAttr(key, value); 560 return success(); 561 } 562 563 if (!value.empty()) 564 return emitError(loc) << "LLVM attribute '" << key 565 << "' does not expect a value, found '" << value 566 << "'"; 567 568 llvmFunc->addFnAttr(kind); 569 return success(); 570 } 571 572 /// Attaches the attributes listed in the given array attribute to `llvmFunc`. 573 /// Reports error to `loc` if any and returns immediately. Expects `attributes` 574 /// to be an array attribute containing either string attributes, treated as 575 /// value-less LLVM attributes, or array attributes containing two string 576 /// attributes, with the first string being the name of the corresponding LLVM 577 /// attribute and the second string beings its value. Note that even integer 578 /// attributes are expected to have their values expressed as strings. 579 static LogicalResult 580 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes, 581 llvm::Function *llvmFunc) { 582 if (!attributes) 583 return success(); 584 585 for (Attribute attr : *attributes) { 586 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 587 if (failed( 588 checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue()))) 589 return failure(); 590 continue; 591 } 592 593 auto arrayAttr = attr.dyn_cast<ArrayAttr>(); 594 if (!arrayAttr || arrayAttr.size() != 2) 595 return emitError(loc) 596 << "expected 'passthrough' to contain string or array attributes"; 597 598 auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>(); 599 auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>(); 600 if (!keyAttr || !valueAttr) 601 return emitError(loc) 602 << "expected arrays within 'passthrough' to contain two strings"; 603 604 if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(), 605 valueAttr.getValue()))) 606 return failure(); 607 } 608 return success(); 609 } 610 611 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { 612 // Clear the block, branch value mappings, they are only relevant within one 613 // function. 614 blockMapping.clear(); 615 valueMapping.clear(); 616 branchMapping.clear(); 617 llvm::Function *llvmFunc = lookupFunction(func.getName()); 618 619 // Translate the debug information for this function. 620 debugTranslation->translate(func, *llvmFunc); 621 622 // Add function arguments to the value remapping table. 623 // If there was noalias info then we decorate each argument accordingly. 624 unsigned int argIdx = 0; 625 for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) { 626 llvm::Argument &llvmArg = std::get<1>(kvp); 627 BlockArgument mlirArg = std::get<0>(kvp); 628 629 if (auto attr = func.getArgAttrOfType<UnitAttr>( 630 argIdx, LLVMDialect::getNoAliasAttrName())) { 631 // NB: Attribute already verified to be boolean, so check if we can indeed 632 // attach the attribute to this argument, based on its type. 633 auto argTy = mlirArg.getType(); 634 if (!argTy.isa<LLVM::LLVMPointerType>()) 635 return func.emitError( 636 "llvm.noalias attribute attached to LLVM non-pointer argument"); 637 llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias); 638 } 639 640 if (auto attr = func.getArgAttrOfType<IntegerAttr>( 641 argIdx, LLVMDialect::getAlignAttrName())) { 642 // NB: Attribute already verified to be int, so check if we can indeed 643 // attach the attribute to this argument, based on its type. 644 auto argTy = mlirArg.getType(); 645 if (!argTy.isa<LLVM::LLVMPointerType>()) 646 return func.emitError( 647 "llvm.align attribute attached to LLVM non-pointer argument"); 648 llvmArg.addAttrs( 649 llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt()))); 650 } 651 652 if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.sret")) { 653 auto argTy = mlirArg.getType(); 654 if (!argTy.isa<LLVM::LLVMPointerType>()) 655 return func.emitError( 656 "llvm.sret attribute attached to LLVM non-pointer argument"); 657 llvmArg.addAttrs(llvm::AttrBuilder().addStructRetAttr( 658 llvmArg.getType()->getPointerElementType())); 659 } 660 661 if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.byval")) { 662 auto argTy = mlirArg.getType(); 663 if (!argTy.isa<LLVM::LLVMPointerType>()) 664 return func.emitError( 665 "llvm.byval attribute attached to LLVM non-pointer argument"); 666 llvmArg.addAttrs(llvm::AttrBuilder().addByValAttr( 667 llvmArg.getType()->getPointerElementType())); 668 } 669 670 mapValue(mlirArg, &llvmArg); 671 argIdx++; 672 } 673 674 // Check the personality and set it. 675 if (func.personality().hasValue()) { 676 llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext()); 677 if (llvm::Constant *pfunc = 678 getLLVMConstant(ty, func.personalityAttr(), func.getLoc(), *this)) 679 llvmFunc->setPersonalityFn(pfunc); 680 } 681 682 // First, create all blocks so we can jump to them. 683 llvm::LLVMContext &llvmContext = llvmFunc->getContext(); 684 for (auto &bb : func) { 685 auto *llvmBB = llvm::BasicBlock::Create(llvmContext); 686 llvmBB->insertInto(llvmFunc); 687 mapBlock(&bb, llvmBB); 688 } 689 690 // Then, convert blocks one by one in topological order to ensure defs are 691 // converted before uses. 692 auto blocks = detail::getTopologicallySortedBlocks(func.getBody()); 693 for (Block *bb : blocks) { 694 llvm::IRBuilder<> builder(llvmContext); 695 if (failed(convertBlock(*bb, bb->isEntryBlock(), builder))) 696 return failure(); 697 } 698 699 // After all blocks have been traversed and values mapped, connect the PHI 700 // nodes to the results of preceding blocks. 701 detail::connectPHINodes(func.getBody(), *this); 702 703 // Finally, convert dialect attributes attached to the function. 704 return convertDialectAttributes(func); 705 } 706 707 LogicalResult ModuleTranslation::convertDialectAttributes(Operation *op) { 708 for (NamedAttribute attribute : op->getDialectAttrs()) 709 if (failed(iface.amendOperation(op, attribute, *this))) 710 return failure(); 711 return success(); 712 } 713 714 LogicalResult ModuleTranslation::convertFunctionSignatures() { 715 // Declare all functions first because there may be function calls that form a 716 // call graph with cycles, or global initializers that reference functions. 717 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 718 llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( 719 function.getName(), 720 cast<llvm::FunctionType>(convertType(function.getType()))); 721 llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); 722 llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage())); 723 mapFunction(function.getName(), llvmFunc); 724 addRuntimePreemptionSpecifier(function.dso_local(), llvmFunc); 725 726 // Forward the pass-through attributes to LLVM. 727 if (failed(forwardPassthroughAttributes(function.getLoc(), 728 function.passthrough(), llvmFunc))) 729 return failure(); 730 } 731 732 return success(); 733 } 734 735 LogicalResult ModuleTranslation::convertFunctions() { 736 // Convert functions. 737 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 738 // Ignore external functions. 739 if (function.isExternal()) 740 continue; 741 742 if (failed(convertOneFunction(function))) 743 return failure(); 744 } 745 746 return success(); 747 } 748 749 llvm::MDNode * 750 ModuleTranslation::getAccessGroup(Operation &opInst, 751 SymbolRefAttr accessGroupRef) const { 752 auto metadataName = accessGroupRef.getRootReference(); 753 auto accessGroupName = accessGroupRef.getLeafReference(); 754 auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>( 755 opInst.getParentOp(), metadataName); 756 auto *accessGroupOp = 757 SymbolTable::lookupNearestSymbolFrom(metadataOp, accessGroupName); 758 return accessGroupMetadataMapping.lookup(accessGroupOp); 759 } 760 761 LogicalResult ModuleTranslation::createAccessGroupMetadata() { 762 mlirModule->walk([&](LLVM::MetadataOp metadatas) { 763 metadatas.walk([&](LLVM::AccessGroupMetadataOp op) { 764 llvm::LLVMContext &ctx = llvmModule->getContext(); 765 llvm::MDNode *accessGroup = llvm::MDNode::getDistinct(ctx, {}); 766 accessGroupMetadataMapping.insert({op, accessGroup}); 767 }); 768 }); 769 return success(); 770 } 771 772 void ModuleTranslation::setAccessGroupsMetadata(Operation *op, 773 llvm::Instruction *inst) { 774 auto accessGroups = 775 op->getAttrOfType<ArrayAttr>(LLVMDialect::getAccessGroupsAttrName()); 776 if (accessGroups && !accessGroups.empty()) { 777 llvm::Module *module = inst->getModule(); 778 SmallVector<llvm::Metadata *> metadatas; 779 for (SymbolRefAttr accessGroupRef : 780 accessGroups.getAsRange<SymbolRefAttr>()) 781 metadatas.push_back(getAccessGroup(*op, accessGroupRef)); 782 783 llvm::MDNode *unionMD = nullptr; 784 if (metadatas.size() == 1) 785 unionMD = llvm::cast<llvm::MDNode>(metadatas.front()); 786 else if (metadatas.size() >= 2) 787 unionMD = llvm::MDNode::get(module->getContext(), metadatas); 788 789 inst->setMetadata(module->getMDKindID("llvm.access.group"), unionMD); 790 } 791 } 792 793 LogicalResult ModuleTranslation::createAliasScopeMetadata() { 794 mlirModule->walk([&](LLVM::MetadataOp metadatas) { 795 // Create the domains first, so they can be reference below in the scopes. 796 DenseMap<Operation *, llvm::MDNode *> aliasScopeDomainMetadataMapping; 797 metadatas.walk([&](LLVM::AliasScopeDomainMetadataOp op) { 798 llvm::LLVMContext &ctx = llvmModule->getContext(); 799 llvm::SmallVector<llvm::Metadata *, 2> operands; 800 operands.push_back({}); // Placeholder for self-reference 801 if (Optional<StringRef> description = op.description()) 802 operands.push_back(llvm::MDString::get(ctx, description.getValue())); 803 llvm::MDNode *domain = llvm::MDNode::get(ctx, operands); 804 domain->replaceOperandWith(0, domain); // Self-reference for uniqueness 805 aliasScopeDomainMetadataMapping.insert({op, domain}); 806 }); 807 808 // Now create the scopes, referencing the domains created above. 809 metadatas.walk([&](LLVM::AliasScopeMetadataOp op) { 810 llvm::LLVMContext &ctx = llvmModule->getContext(); 811 assert(isa<LLVM::MetadataOp>(op->getParentOp())); 812 auto metadataOp = dyn_cast<LLVM::MetadataOp>(op->getParentOp()); 813 Operation *domainOp = 814 SymbolTable::lookupNearestSymbolFrom(metadataOp, op.domainAttr()); 815 llvm::MDNode *domain = aliasScopeDomainMetadataMapping.lookup(domainOp); 816 assert(domain && "Scope's domain should already be valid"); 817 llvm::SmallVector<llvm::Metadata *, 3> operands; 818 operands.push_back({}); // Placeholder for self-reference 819 operands.push_back(domain); 820 if (Optional<StringRef> description = op.description()) 821 operands.push_back(llvm::MDString::get(ctx, description.getValue())); 822 llvm::MDNode *scope = llvm::MDNode::get(ctx, operands); 823 scope->replaceOperandWith(0, scope); // Self-reference for uniqueness 824 aliasScopeMetadataMapping.insert({op, scope}); 825 }); 826 }); 827 return success(); 828 } 829 830 llvm::MDNode * 831 ModuleTranslation::getAliasScope(Operation &opInst, 832 SymbolRefAttr aliasScopeRef) const { 833 StringAttr metadataName = aliasScopeRef.getRootReference(); 834 StringAttr scopeName = aliasScopeRef.getLeafReference(); 835 auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>( 836 opInst.getParentOp(), metadataName); 837 Operation *aliasScopeOp = 838 SymbolTable::lookupNearestSymbolFrom(metadataOp, scopeName); 839 return aliasScopeMetadataMapping.lookup(aliasScopeOp); 840 } 841 842 void ModuleTranslation::setAliasScopeMetadata(Operation *op, 843 llvm::Instruction *inst) { 844 auto populateScopeMetadata = [this, op, inst](StringRef attrName, 845 StringRef llvmMetadataName) { 846 auto scopes = op->getAttrOfType<ArrayAttr>(attrName); 847 if (!scopes || scopes.empty()) 848 return; 849 llvm::Module *module = inst->getModule(); 850 SmallVector<llvm::Metadata *> scopeMDs; 851 for (SymbolRefAttr scopeRef : scopes.getAsRange<SymbolRefAttr>()) 852 scopeMDs.push_back(getAliasScope(*op, scopeRef)); 853 llvm::MDNode *unionMD = nullptr; 854 if (scopeMDs.size() == 1) 855 unionMD = llvm::cast<llvm::MDNode>(scopeMDs.front()); 856 else if (scopeMDs.size() >= 2) 857 unionMD = llvm::MDNode::get(module->getContext(), scopeMDs); 858 inst->setMetadata(module->getMDKindID(llvmMetadataName), unionMD); 859 }; 860 861 populateScopeMetadata(LLVMDialect::getAliasScopesAttrName(), "alias.scope"); 862 populateScopeMetadata(LLVMDialect::getNoAliasScopesAttrName(), "noalias"); 863 } 864 865 llvm::Type *ModuleTranslation::convertType(Type type) { 866 return typeTranslator.translateType(type); 867 } 868 869 /// A helper to look up remapped operands in the value remapping table. 870 SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) { 871 SmallVector<llvm::Value *> remapped; 872 remapped.reserve(values.size()); 873 for (Value v : values) 874 remapped.push_back(lookupValue(v)); 875 return remapped; 876 } 877 878 const llvm::DILocation * 879 ModuleTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) { 880 return debugTranslation->translateLoc(loc, scope); 881 } 882 883 llvm::NamedMDNode * 884 ModuleTranslation::getOrInsertNamedModuleMetadata(StringRef name) { 885 return llvmModule->getOrInsertNamedMetadata(name); 886 } 887 888 void ModuleTranslation::StackFrame::anchor() {} 889 890 static std::unique_ptr<llvm::Module> 891 prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext, 892 StringRef name) { 893 m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>(); 894 auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext); 895 if (auto dataLayoutAttr = 896 m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) 897 llvmModule->setDataLayout(dataLayoutAttr.cast<StringAttr>().getValue()); 898 if (auto targetTripleAttr = 899 m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) 900 llvmModule->setTargetTriple(targetTripleAttr.cast<StringAttr>().getValue()); 901 902 // Inject declarations for `malloc` and `free` functions that can be used in 903 // memref allocation/deallocation coming from standard ops lowering. 904 llvm::IRBuilder<> builder(llvmContext); 905 llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(), 906 builder.getInt64Ty()); 907 llvmModule->getOrInsertFunction("free", builder.getVoidTy(), 908 builder.getInt8PtrTy()); 909 910 return llvmModule; 911 } 912 913 std::unique_ptr<llvm::Module> 914 mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, 915 StringRef name) { 916 if (!satisfiesLLVMModule(module)) 917 return nullptr; 918 std::unique_ptr<llvm::Module> llvmModule = 919 prepareLLVMModule(module, llvmContext, name); 920 921 LLVM::ensureDistinctSuccessors(module); 922 923 ModuleTranslation translator(module, std::move(llvmModule)); 924 if (failed(translator.convertFunctionSignatures())) 925 return nullptr; 926 if (failed(translator.convertGlobals())) 927 return nullptr; 928 if (failed(translator.createAccessGroupMetadata())) 929 return nullptr; 930 if (failed(translator.createAliasScopeMetadata())) 931 return nullptr; 932 if (failed(translator.convertFunctions())) 933 return nullptr; 934 935 // Convert other top-level operations if possible. 936 llvm::IRBuilder<> llvmBuilder(llvmContext); 937 for (Operation &o : getModuleBody(module).getOperations()) { 938 if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp, LLVM::MetadataOp>(&o) && 939 !o.hasTrait<OpTrait::IsTerminator>() && 940 failed(translator.convertOperation(o, llvmBuilder))) { 941 return nullptr; 942 } 943 } 944 945 if (llvm::verifyModule(*translator.llvmModule, &llvm::errs())) 946 return nullptr; 947 948 return std::move(translator.llvmModule); 949 } 950