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/OpenMP/OpenMPDialect.h" 19 #include "mlir/IR/Attributes.h" 20 #include "mlir/IR/Module.h" 21 #include "mlir/IR/StandardTypes.h" 22 #include "mlir/Support/LLVM.h" 23 #include "mlir/Target/LLVMIR/TypeTranslation.h" 24 #include "llvm/ADT/TypeSwitch.h" 25 26 #include "llvm/ADT/SetVector.h" 27 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 28 #include "llvm/IR/BasicBlock.h" 29 #include "llvm/IR/CFG.h" 30 #include "llvm/IR/Constants.h" 31 #include "llvm/IR/DerivedTypes.h" 32 #include "llvm/IR/IRBuilder.h" 33 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/IR/MDBuilder.h" 35 #include "llvm/IR/Module.h" 36 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 37 #include "llvm/Transforms/Utils/Cloning.h" 38 39 using namespace mlir; 40 using namespace mlir::LLVM; 41 using namespace mlir::LLVM::detail; 42 43 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc" 44 45 /// Builds a constant of a sequential LLVM type `type`, potentially containing 46 /// other sequential types recursively, from the individual constant values 47 /// provided in `constants`. `shape` contains the number of elements in nested 48 /// sequential types. Reports errors at `loc` and returns nullptr on error. 49 static llvm::Constant * 50 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants, 51 ArrayRef<int64_t> shape, llvm::Type *type, 52 Location loc) { 53 if (shape.empty()) { 54 llvm::Constant *result = constants.front(); 55 constants = constants.drop_front(); 56 return result; 57 } 58 59 llvm::Type *elementType; 60 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 61 elementType = arrayTy->getElementType(); 62 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 63 elementType = vectorTy->getElementType(); 64 } else { 65 emitError(loc) << "expected sequential LLVM types wrapping a scalar"; 66 return nullptr; 67 } 68 69 SmallVector<llvm::Constant *, 8> nested; 70 nested.reserve(shape.front()); 71 for (int64_t i = 0; i < shape.front(); ++i) { 72 nested.push_back(buildSequentialConstant(constants, shape.drop_front(), 73 elementType, loc)); 74 if (!nested.back()) 75 return nullptr; 76 } 77 78 if (shape.size() == 1 && type->isVectorTy()) 79 return llvm::ConstantVector::get(nested); 80 return llvm::ConstantArray::get( 81 llvm::ArrayType::get(elementType, shape.front()), nested); 82 } 83 84 /// Returns the first non-sequential type nested in sequential types. 85 static llvm::Type *getInnermostElementType(llvm::Type *type) { 86 do { 87 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 88 type = arrayTy->getElementType(); 89 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 90 type = vectorTy->getElementType(); 91 } else { 92 return type; 93 } 94 } while (1); 95 } 96 97 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`. 98 /// This currently supports integer, floating point, splat and dense element 99 /// attributes and combinations thereof. In case of error, report it to `loc` 100 /// and return nullptr. 101 llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType, 102 Attribute attr, 103 Location loc) { 104 if (!attr) 105 return llvm::UndefValue::get(llvmType); 106 if (llvmType->isStructTy()) { 107 emitError(loc, "struct types are not supported in constants"); 108 return nullptr; 109 } 110 // For integer types, we allow a mismatch in sizes as the index type in 111 // MLIR might have a different size than the index type in the LLVM module. 112 if (auto intAttr = attr.dyn_cast<IntegerAttr>()) 113 return llvm::ConstantInt::get( 114 llvmType, 115 intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth())); 116 if (auto floatAttr = attr.dyn_cast<FloatAttr>()) 117 return llvm::ConstantFP::get(llvmType, floatAttr.getValue()); 118 if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>()) 119 return llvm::ConstantExpr::getBitCast( 120 functionMapping.lookup(funcAttr.getValue()), llvmType); 121 if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) { 122 llvm::Type *elementType; 123 uint64_t numElements; 124 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) { 125 elementType = arrayTy->getElementType(); 126 numElements = arrayTy->getNumElements(); 127 } else { 128 auto *vectorTy = cast<llvm::FixedVectorType>(llvmType); 129 elementType = vectorTy->getElementType(); 130 numElements = vectorTy->getNumElements(); 131 } 132 // Splat value is a scalar. Extract it only if the element type is not 133 // another sequence type. The recursion terminates because each step removes 134 // one outer sequential type. 135 bool elementTypeSequential = 136 isa<llvm::ArrayType, llvm::VectorType>(elementType); 137 llvm::Constant *child = getLLVMConstant( 138 elementType, 139 elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc); 140 if (!child) 141 return nullptr; 142 if (llvmType->isVectorTy()) 143 return llvm::ConstantVector::getSplat( 144 llvm::ElementCount(numElements, /*Scalable=*/false), child); 145 if (llvmType->isArrayTy()) { 146 auto *arrayType = llvm::ArrayType::get(elementType, numElements); 147 SmallVector<llvm::Constant *, 8> constants(numElements, child); 148 return llvm::ConstantArray::get(arrayType, constants); 149 } 150 } 151 152 if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) { 153 assert(elementsAttr.getType().hasStaticShape()); 154 assert(elementsAttr.getNumElements() != 0 && 155 "unexpected empty elements attribute"); 156 assert(!elementsAttr.getType().getShape().empty() && 157 "unexpected empty elements attribute shape"); 158 159 SmallVector<llvm::Constant *, 8> constants; 160 constants.reserve(elementsAttr.getNumElements()); 161 llvm::Type *innermostType = getInnermostElementType(llvmType); 162 for (auto n : elementsAttr.getValues<Attribute>()) { 163 constants.push_back(getLLVMConstant(innermostType, n, loc)); 164 if (!constants.back()) 165 return nullptr; 166 } 167 ArrayRef<llvm::Constant *> constantsRef = constants; 168 llvm::Constant *result = buildSequentialConstant( 169 constantsRef, elementsAttr.getType().getShape(), llvmType, loc); 170 assert(constantsRef.empty() && "did not consume all elemental constants"); 171 return result; 172 } 173 174 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 175 return llvm::ConstantDataArray::get( 176 llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(), 177 stringAttr.getValue().size()}); 178 } 179 emitError(loc, "unsupported constant value"); 180 return nullptr; 181 } 182 183 /// Convert MLIR integer comparison predicate to LLVM IR comparison predicate. 184 static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) { 185 switch (p) { 186 case LLVM::ICmpPredicate::eq: 187 return llvm::CmpInst::Predicate::ICMP_EQ; 188 case LLVM::ICmpPredicate::ne: 189 return llvm::CmpInst::Predicate::ICMP_NE; 190 case LLVM::ICmpPredicate::slt: 191 return llvm::CmpInst::Predicate::ICMP_SLT; 192 case LLVM::ICmpPredicate::sle: 193 return llvm::CmpInst::Predicate::ICMP_SLE; 194 case LLVM::ICmpPredicate::sgt: 195 return llvm::CmpInst::Predicate::ICMP_SGT; 196 case LLVM::ICmpPredicate::sge: 197 return llvm::CmpInst::Predicate::ICMP_SGE; 198 case LLVM::ICmpPredicate::ult: 199 return llvm::CmpInst::Predicate::ICMP_ULT; 200 case LLVM::ICmpPredicate::ule: 201 return llvm::CmpInst::Predicate::ICMP_ULE; 202 case LLVM::ICmpPredicate::ugt: 203 return llvm::CmpInst::Predicate::ICMP_UGT; 204 case LLVM::ICmpPredicate::uge: 205 return llvm::CmpInst::Predicate::ICMP_UGE; 206 } 207 llvm_unreachable("incorrect comparison predicate"); 208 } 209 210 static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) { 211 switch (p) { 212 case LLVM::FCmpPredicate::_false: 213 return llvm::CmpInst::Predicate::FCMP_FALSE; 214 case LLVM::FCmpPredicate::oeq: 215 return llvm::CmpInst::Predicate::FCMP_OEQ; 216 case LLVM::FCmpPredicate::ogt: 217 return llvm::CmpInst::Predicate::FCMP_OGT; 218 case LLVM::FCmpPredicate::oge: 219 return llvm::CmpInst::Predicate::FCMP_OGE; 220 case LLVM::FCmpPredicate::olt: 221 return llvm::CmpInst::Predicate::FCMP_OLT; 222 case LLVM::FCmpPredicate::ole: 223 return llvm::CmpInst::Predicate::FCMP_OLE; 224 case LLVM::FCmpPredicate::one: 225 return llvm::CmpInst::Predicate::FCMP_ONE; 226 case LLVM::FCmpPredicate::ord: 227 return llvm::CmpInst::Predicate::FCMP_ORD; 228 case LLVM::FCmpPredicate::ueq: 229 return llvm::CmpInst::Predicate::FCMP_UEQ; 230 case LLVM::FCmpPredicate::ugt: 231 return llvm::CmpInst::Predicate::FCMP_UGT; 232 case LLVM::FCmpPredicate::uge: 233 return llvm::CmpInst::Predicate::FCMP_UGE; 234 case LLVM::FCmpPredicate::ult: 235 return llvm::CmpInst::Predicate::FCMP_ULT; 236 case LLVM::FCmpPredicate::ule: 237 return llvm::CmpInst::Predicate::FCMP_ULE; 238 case LLVM::FCmpPredicate::une: 239 return llvm::CmpInst::Predicate::FCMP_UNE; 240 case LLVM::FCmpPredicate::uno: 241 return llvm::CmpInst::Predicate::FCMP_UNO; 242 case LLVM::FCmpPredicate::_true: 243 return llvm::CmpInst::Predicate::FCMP_TRUE; 244 } 245 llvm_unreachable("incorrect comparison predicate"); 246 } 247 248 static llvm::AtomicRMWInst::BinOp getLLVMAtomicBinOp(AtomicBinOp op) { 249 switch (op) { 250 case LLVM::AtomicBinOp::xchg: 251 return llvm::AtomicRMWInst::BinOp::Xchg; 252 case LLVM::AtomicBinOp::add: 253 return llvm::AtomicRMWInst::BinOp::Add; 254 case LLVM::AtomicBinOp::sub: 255 return llvm::AtomicRMWInst::BinOp::Sub; 256 case LLVM::AtomicBinOp::_and: 257 return llvm::AtomicRMWInst::BinOp::And; 258 case LLVM::AtomicBinOp::nand: 259 return llvm::AtomicRMWInst::BinOp::Nand; 260 case LLVM::AtomicBinOp::_or: 261 return llvm::AtomicRMWInst::BinOp::Or; 262 case LLVM::AtomicBinOp::_xor: 263 return llvm::AtomicRMWInst::BinOp::Xor; 264 case LLVM::AtomicBinOp::max: 265 return llvm::AtomicRMWInst::BinOp::Max; 266 case LLVM::AtomicBinOp::min: 267 return llvm::AtomicRMWInst::BinOp::Min; 268 case LLVM::AtomicBinOp::umax: 269 return llvm::AtomicRMWInst::BinOp::UMax; 270 case LLVM::AtomicBinOp::umin: 271 return llvm::AtomicRMWInst::BinOp::UMin; 272 case LLVM::AtomicBinOp::fadd: 273 return llvm::AtomicRMWInst::BinOp::FAdd; 274 case LLVM::AtomicBinOp::fsub: 275 return llvm::AtomicRMWInst::BinOp::FSub; 276 } 277 llvm_unreachable("incorrect atomic binary operator"); 278 } 279 280 static llvm::AtomicOrdering getLLVMAtomicOrdering(AtomicOrdering ordering) { 281 switch (ordering) { 282 case LLVM::AtomicOrdering::not_atomic: 283 return llvm::AtomicOrdering::NotAtomic; 284 case LLVM::AtomicOrdering::unordered: 285 return llvm::AtomicOrdering::Unordered; 286 case LLVM::AtomicOrdering::monotonic: 287 return llvm::AtomicOrdering::Monotonic; 288 case LLVM::AtomicOrdering::acquire: 289 return llvm::AtomicOrdering::Acquire; 290 case LLVM::AtomicOrdering::release: 291 return llvm::AtomicOrdering::Release; 292 case LLVM::AtomicOrdering::acq_rel: 293 return llvm::AtomicOrdering::AcquireRelease; 294 case LLVM::AtomicOrdering::seq_cst: 295 return llvm::AtomicOrdering::SequentiallyConsistent; 296 } 297 llvm_unreachable("incorrect atomic ordering"); 298 } 299 300 ModuleTranslation::ModuleTranslation(Operation *module, 301 std::unique_ptr<llvm::Module> llvmModule) 302 : mlirModule(module), llvmModule(std::move(llvmModule)), 303 debugTranslation( 304 std::make_unique<DebugTranslation>(module, *this->llvmModule)), 305 ompDialect( 306 module->getContext()->getRegisteredDialect<omp::OpenMPDialect>()), 307 llvmDialect(module->getContext()->getRegisteredDialect<LLVMDialect>()) { 308 assert(satisfiesLLVMModule(mlirModule) && 309 "mlirModule should honor LLVM's module semantics."); 310 } 311 ModuleTranslation::~ModuleTranslation() { 312 if (ompBuilder) 313 ompBuilder->finalize(); 314 } 315 316 /// Get the SSA value passed to the current block from the terminator operation 317 /// of its predecessor. 318 static Value getPHISourceValue(Block *current, Block *pred, 319 unsigned numArguments, unsigned index) { 320 Operation &terminator = *pred->getTerminator(); 321 if (isa<LLVM::BrOp>(terminator)) 322 return terminator.getOperand(index); 323 324 // For conditional branches, we need to check if the current block is reached 325 // through the "true" or the "false" branch and take the relevant operands. 326 auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator); 327 assert(condBranchOp && 328 "only branch operations can be terminators of a block that " 329 "has successors"); 330 assert((condBranchOp.getSuccessor(0) != condBranchOp.getSuccessor(1)) && 331 "successors with arguments in LLVM conditional branches must be " 332 "different blocks"); 333 334 return condBranchOp.getSuccessor(0) == current 335 ? condBranchOp.trueDestOperands()[index] 336 : condBranchOp.falseDestOperands()[index]; 337 } 338 339 /// Connect the PHI nodes to the results of preceding blocks. 340 template <typename T> 341 static void 342 connectPHINodes(T &func, const DenseMap<Value, llvm::Value *> &valueMapping, 343 const DenseMap<Block *, llvm::BasicBlock *> &blockMapping) { 344 // Skip the first block, it cannot be branched to and its arguments correspond 345 // to the arguments of the LLVM function. 346 for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) { 347 Block *bb = &*it; 348 llvm::BasicBlock *llvmBB = blockMapping.lookup(bb); 349 auto phis = llvmBB->phis(); 350 auto numArguments = bb->getNumArguments(); 351 assert(numArguments == std::distance(phis.begin(), phis.end())); 352 for (auto &numberedPhiNode : llvm::enumerate(phis)) { 353 auto &phiNode = numberedPhiNode.value(); 354 unsigned index = numberedPhiNode.index(); 355 for (auto *pred : bb->getPredecessors()) { 356 phiNode.addIncoming(valueMapping.lookup(getPHISourceValue( 357 bb, pred, numArguments, index)), 358 blockMapping.lookup(pred)); 359 } 360 } 361 } 362 } 363 364 // TODO: implement an iterative version 365 static void topologicalSortImpl(llvm::SetVector<Block *> &blocks, Block *b) { 366 blocks.insert(b); 367 for (Block *bb : b->getSuccessors()) { 368 if (blocks.count(bb) == 0) 369 topologicalSortImpl(blocks, bb); 370 } 371 } 372 373 /// Sort function blocks topologically. 374 template <typename T> 375 static llvm::SetVector<Block *> topologicalSort(T &f) { 376 // For each blocks that has not been visited yet (i.e. that has no 377 // predecessors), add it to the list and traverse its successors in DFS 378 // preorder. 379 llvm::SetVector<Block *> blocks; 380 for (Block &b : f) { 381 if (blocks.count(&b) == 0) 382 topologicalSortImpl(blocks, &b); 383 } 384 assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted"); 385 386 return blocks; 387 } 388 389 /// Convert the OpenMP parallel Operation to LLVM IR. 390 LogicalResult 391 ModuleTranslation::convertOmpParallel(Operation &opInst, 392 llvm::IRBuilder<> &builder) { 393 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 394 395 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 396 llvm::BasicBlock &continuationIP) { 397 llvm::LLVMContext &llvmContext = llvmModule->getContext(); 398 399 llvm::BasicBlock *codeGenIPBB = codeGenIP.getBlock(); 400 llvm::Instruction *codeGenIPBBTI = codeGenIPBB->getTerminator(); 401 402 builder.SetInsertPoint(codeGenIPBB); 403 // ParallelOp has only `1` region associated with it. 404 auto ®ion = cast<omp::ParallelOp>(opInst).getRegion(); 405 for (auto &bb : region) { 406 auto *llvmBB = llvm::BasicBlock::Create( 407 llvmContext, "omp.par.region", codeGenIP.getBlock()->getParent()); 408 blockMapping[&bb] = llvmBB; 409 } 410 411 // Then, convert blocks one by one in topological order to ensure 412 // defs are converted before uses. 413 llvm::SetVector<Block *> blocks = topologicalSort(region); 414 for (auto indexedBB : llvm::enumerate(blocks)) { 415 Block *bb = indexedBB.value(); 416 llvm::BasicBlock *curLLVMBB = blockMapping[bb]; 417 if (bb->isEntryBlock()) 418 codeGenIPBBTI->setSuccessor(0, curLLVMBB); 419 420 // TODO: Error not returned up the hierarchy 421 if (failed( 422 convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0))) 423 return; 424 425 // If this block has the terminator then add a jump to 426 // continuation bb 427 for (auto &op : *bb) { 428 if (isa<omp::TerminatorOp>(op)) { 429 builder.SetInsertPoint(curLLVMBB); 430 builder.CreateBr(&continuationIP); 431 } 432 } 433 } 434 // Finally, after all blocks have been traversed and values mapped, 435 // connect the PHI nodes to the results of preceding blocks. 436 connectPHINodes(region, valueMapping, blockMapping); 437 }; 438 439 // TODO: Perform appropriate actions according to the data-sharing 440 // attribute (shared, private, firstprivate, ...) of variables. 441 // Currently defaults to shared. 442 auto privCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 443 llvm::Value &vPtr, 444 llvm::Value *&replacementValue) -> InsertPointTy { 445 replacementValue = &vPtr; 446 447 return codeGenIP; 448 }; 449 450 // TODO: Perform finalization actions for variables. This has to be 451 // called for variables which have destructors/finalizers. 452 auto finiCB = [&](InsertPointTy codeGenIP) {}; 453 454 // TODO: The various operands of parallel operation are not handled. 455 // Parallel operation is created with some default options for now. 456 llvm::Value *ifCond = nullptr; 457 llvm::Value *numThreads = nullptr; 458 bool isCancellable = false; 459 // TODO: Determine the actual alloca insertion point, e.g., the function 460 // entry or the alloca insertion point as provided by the body callback 461 // above. 462 llvm::OpenMPIRBuilder::InsertPointTy allocaIP(builder.saveIP()); 463 builder.restoreIP(ompBuilder->CreateParallel( 464 builder, allocaIP, bodyGenCB, privCB, finiCB, ifCond, numThreads, 465 llvm::omp::OMP_PROC_BIND_default, isCancellable)); 466 return success(); 467 } 468 469 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR 470 /// (including OpenMP runtime calls). 471 LogicalResult 472 ModuleTranslation::convertOmpOperation(Operation &opInst, 473 llvm::IRBuilder<> &builder) { 474 if (!ompBuilder) { 475 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule); 476 ompBuilder->initialize(); 477 } 478 return llvm::TypeSwitch<Operation *, LogicalResult>(&opInst) 479 .Case([&](omp::BarrierOp) { 480 ompBuilder->CreateBarrier(builder.saveIP(), llvm::omp::OMPD_barrier); 481 return success(); 482 }) 483 .Case([&](omp::TaskwaitOp) { 484 ompBuilder->CreateTaskwait(builder.saveIP()); 485 return success(); 486 }) 487 .Case([&](omp::TaskyieldOp) { 488 ompBuilder->CreateTaskyield(builder.saveIP()); 489 return success(); 490 }) 491 .Case([&](omp::FlushOp) { 492 // No support in Openmp runtime funciton (__kmpc_flush) to accept 493 // the argument list. 494 // OpenMP standard states the following: 495 // "An implementation may implement a flush with a list by ignoring 496 // the list, and treating it the same as a flush without a list." 497 // 498 // The argument list is discarded so that, flush with a list is treated 499 // same as a flush without a list. 500 ompBuilder->CreateFlush(builder.saveIP()); 501 return success(); 502 }) 503 .Case([&](omp::TerminatorOp) { return success(); }) 504 .Case( 505 [&](omp::ParallelOp) { return convertOmpParallel(opInst, builder); }) 506 .Default([&](Operation *inst) { 507 return inst->emitError("unsupported OpenMP operation: ") 508 << inst->getName(); 509 }); 510 } 511 512 /// Given a single MLIR operation, create the corresponding LLVM IR operation 513 /// using the `builder`. LLVM IR Builder does not have a generic interface so 514 /// this has to be a long chain of `if`s calling different functions with a 515 /// different number of arguments. 516 LogicalResult ModuleTranslation::convertOperation(Operation &opInst, 517 llvm::IRBuilder<> &builder) { 518 auto extractPosition = [](ArrayAttr attr) { 519 SmallVector<unsigned, 4> position; 520 position.reserve(attr.size()); 521 for (Attribute v : attr) 522 position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue()); 523 return position; 524 }; 525 526 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc" 527 528 // Emit function calls. If the "callee" attribute is present, this is a 529 // direct function call and we also need to look up the remapped function 530 // itself. Otherwise, this is an indirect call and the callee is the first 531 // operand, look it up as a normal value. Return the llvm::Value representing 532 // the function result, which may be of llvm::VoidTy type. 533 auto convertCall = [this, &builder](Operation &op) -> llvm::Value * { 534 auto operands = lookupValues(op.getOperands()); 535 ArrayRef<llvm::Value *> operandsRef(operands); 536 if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) { 537 return builder.CreateCall(functionMapping.lookup(attr.getValue()), 538 operandsRef); 539 } else { 540 auto *calleePtrType = 541 cast<llvm::PointerType>(operandsRef.front()->getType()); 542 auto *calleeType = 543 cast<llvm::FunctionType>(calleePtrType->getElementType()); 544 return builder.CreateCall(calleeType, operandsRef.front(), 545 operandsRef.drop_front()); 546 } 547 }; 548 549 // Emit calls. If the called function has a result, remap the corresponding 550 // value. Note that LLVM IR dialect CallOp has either 0 or 1 result. 551 if (isa<LLVM::CallOp>(opInst)) { 552 llvm::Value *result = convertCall(opInst); 553 if (opInst.getNumResults() != 0) { 554 valueMapping[opInst.getResult(0)] = result; 555 return success(); 556 } 557 // Check that LLVM call returns void for 0-result functions. 558 return success(result->getType()->isVoidTy()); 559 } 560 561 if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) { 562 auto operands = lookupValues(opInst.getOperands()); 563 ArrayRef<llvm::Value *> operandsRef(operands); 564 if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) { 565 builder.CreateInvoke(functionMapping.lookup(attr.getValue()), 566 blockMapping[invOp.getSuccessor(0)], 567 blockMapping[invOp.getSuccessor(1)], operandsRef); 568 } else { 569 auto *calleePtrType = 570 cast<llvm::PointerType>(operandsRef.front()->getType()); 571 auto *calleeType = 572 cast<llvm::FunctionType>(calleePtrType->getElementType()); 573 builder.CreateInvoke( 574 calleeType, operandsRef.front(), blockMapping[invOp.getSuccessor(0)], 575 blockMapping[invOp.getSuccessor(1)], operandsRef.drop_front()); 576 } 577 return success(); 578 } 579 580 if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) { 581 llvm::Type *ty = convertType(lpOp.getType().cast<LLVMType>()); 582 llvm::LandingPadInst *lpi = 583 builder.CreateLandingPad(ty, lpOp.getNumOperands()); 584 585 // Add clauses 586 for (auto operand : lookupValues(lpOp.getOperands())) { 587 // All operands should be constant - checked by verifier 588 if (auto constOperand = dyn_cast<llvm::Constant>(operand)) 589 lpi->addClause(constOperand); 590 } 591 valueMapping[lpOp.getResult()] = lpi; 592 return success(); 593 } 594 595 // Emit branches. We need to look up the remapped blocks and ignore the block 596 // arguments that were transformed into PHI nodes. 597 if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) { 598 builder.CreateBr(blockMapping[brOp.getSuccessor()]); 599 return success(); 600 } 601 if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) { 602 auto weights = condbrOp.branch_weights(); 603 llvm::MDNode *branchWeights = nullptr; 604 if (weights) { 605 // Map weight attributes to LLVM metadata. 606 auto trueWeight = 607 weights.getValue().getValue(0).cast<IntegerAttr>().getInt(); 608 auto falseWeight = 609 weights.getValue().getValue(1).cast<IntegerAttr>().getInt(); 610 branchWeights = 611 llvm::MDBuilder(llvmModule->getContext()) 612 .createBranchWeights(static_cast<uint32_t>(trueWeight), 613 static_cast<uint32_t>(falseWeight)); 614 } 615 builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)), 616 blockMapping[condbrOp.getSuccessor(0)], 617 blockMapping[condbrOp.getSuccessor(1)], branchWeights); 618 return success(); 619 } 620 621 // Emit addressof. We need to look up the global value referenced by the 622 // operation and store it in the MLIR-to-LLVM value mapping. This does not 623 // emit any LLVM instruction. 624 if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) { 625 LLVM::GlobalOp global = addressOfOp.getGlobal(); 626 LLVM::LLVMFuncOp function = addressOfOp.getFunction(); 627 628 // The verifier should not have allowed this. 629 assert((global || function) && 630 "referencing an undefined global or function"); 631 632 valueMapping[addressOfOp.getResult()] = 633 global ? globalsMapping.lookup(global) 634 : functionMapping.lookup(function.getName()); 635 return success(); 636 } 637 638 if (opInst.getDialect() == ompDialect) { 639 return convertOmpOperation(opInst, builder); 640 } 641 642 return opInst.emitError("unsupported or non-LLVM operation: ") 643 << opInst.getName(); 644 } 645 646 /// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes 647 /// to define values corresponding to the MLIR block arguments. These nodes 648 /// are not connected to the source basic blocks, which may not exist yet. 649 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) { 650 llvm::IRBuilder<> builder(blockMapping[&bb]); 651 auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram(); 652 653 // Before traversing operations, make block arguments available through 654 // value remapping and PHI nodes, but do not add incoming edges for the PHI 655 // nodes just yet: those values may be defined by this or following blocks. 656 // This step is omitted if "ignoreArguments" is set. The arguments of the 657 // first block have been already made available through the remapping of 658 // LLVM function arguments. 659 if (!ignoreArguments) { 660 auto predecessors = bb.getPredecessors(); 661 unsigned numPredecessors = 662 std::distance(predecessors.begin(), predecessors.end()); 663 for (auto arg : bb.getArguments()) { 664 auto wrappedType = arg.getType().dyn_cast<LLVM::LLVMType>(); 665 if (!wrappedType) 666 return emitError(bb.front().getLoc(), 667 "block argument does not have an LLVM type"); 668 llvm::Type *type = convertType(wrappedType); 669 llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors); 670 valueMapping[arg] = phi; 671 } 672 } 673 674 // Traverse operations. 675 for (auto &op : bb) { 676 // Set the current debug location within the builder. 677 builder.SetCurrentDebugLocation( 678 debugTranslation->translateLoc(op.getLoc(), subprogram)); 679 680 if (failed(convertOperation(op, builder))) 681 return failure(); 682 } 683 684 return success(); 685 } 686 687 /// Create named global variables that correspond to llvm.mlir.global 688 /// definitions. 689 LogicalResult ModuleTranslation::convertGlobals() { 690 // Lock access to the llvm context. 691 llvm::sys::SmartScopedLock<true> scopedLock( 692 llvmDialect->getLLVMContextMutex()); 693 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 694 llvm::Type *type = convertType(op.getType()); 695 llvm::Constant *cst = llvm::UndefValue::get(type); 696 if (op.getValueOrNull()) { 697 // String attributes are treated separately because they cannot appear as 698 // in-function constants and are thus not supported by getLLVMConstant. 699 if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) { 700 cst = llvm::ConstantDataArray::getString( 701 llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); 702 type = cst->getType(); 703 } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), 704 op.getLoc()))) { 705 return failure(); 706 } 707 } else if (Block *initializer = op.getInitializerBlock()) { 708 llvm::IRBuilder<> builder(llvmModule->getContext()); 709 for (auto &op : initializer->without_terminator()) { 710 if (failed(convertOperation(op, builder)) || 711 !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0)))) 712 return emitError(op.getLoc(), "unemittable constant value"); 713 } 714 ReturnOp ret = cast<ReturnOp>(initializer->getTerminator()); 715 cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0))); 716 } 717 718 auto linkage = convertLinkageToLLVM(op.linkage()); 719 bool anyExternalLinkage = 720 ((linkage == llvm::GlobalVariable::ExternalLinkage && 721 isa<llvm::UndefValue>(cst)) || 722 linkage == llvm::GlobalVariable::ExternalWeakLinkage); 723 auto addrSpace = op.addr_space().getLimitedValue(); 724 auto *var = new llvm::GlobalVariable( 725 *llvmModule, type, op.constant(), linkage, 726 anyExternalLinkage ? nullptr : cst, op.sym_name(), 727 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace); 728 729 globalsMapping.try_emplace(op, var); 730 } 731 732 return success(); 733 } 734 735 /// Attempts to add an attribute identified by `key`, optionally with the given 736 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the 737 /// attribute has a kind known to LLVM IR, create the attribute of this kind, 738 /// otherwise keep it as a string attribute. Performs additional checks for 739 /// attributes known to have or not have a value in order to avoid assertions 740 /// inside LLVM upon construction. 741 static LogicalResult checkedAddLLVMFnAttribute(Location loc, 742 llvm::Function *llvmFunc, 743 StringRef key, 744 StringRef value = StringRef()) { 745 auto kind = llvm::Attribute::getAttrKindFromName(key); 746 if (kind == llvm::Attribute::None) { 747 llvmFunc->addFnAttr(key, value); 748 return success(); 749 } 750 751 if (llvm::Attribute::doesAttrKindHaveArgument(kind)) { 752 if (value.empty()) 753 return emitError(loc) << "LLVM attribute '" << key << "' expects a value"; 754 755 int result; 756 if (!value.getAsInteger(/*Radix=*/0, result)) 757 llvmFunc->addFnAttr( 758 llvm::Attribute::get(llvmFunc->getContext(), kind, result)); 759 else 760 llvmFunc->addFnAttr(key, value); 761 return success(); 762 } 763 764 if (!value.empty()) 765 return emitError(loc) << "LLVM attribute '" << key 766 << "' does not expect a value, found '" << value 767 << "'"; 768 769 llvmFunc->addFnAttr(kind); 770 return success(); 771 } 772 773 /// Attaches the attributes listed in the given array attribute to `llvmFunc`. 774 /// Reports error to `loc` if any and returns immediately. Expects `attributes` 775 /// to be an array attribute containing either string attributes, treated as 776 /// value-less LLVM attributes, or array attributes containing two string 777 /// attributes, with the first string being the name of the corresponding LLVM 778 /// attribute and the second string beings its value. Note that even integer 779 /// attributes are expected to have their values expressed as strings. 780 static LogicalResult 781 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes, 782 llvm::Function *llvmFunc) { 783 if (!attributes) 784 return success(); 785 786 for (Attribute attr : *attributes) { 787 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 788 if (failed( 789 checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue()))) 790 return failure(); 791 continue; 792 } 793 794 auto arrayAttr = attr.dyn_cast<ArrayAttr>(); 795 if (!arrayAttr || arrayAttr.size() != 2) 796 return emitError(loc) 797 << "expected 'passthrough' to contain string or array attributes"; 798 799 auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>(); 800 auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>(); 801 if (!keyAttr || !valueAttr) 802 return emitError(loc) 803 << "expected arrays within 'passthrough' to contain two strings"; 804 805 if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(), 806 valueAttr.getValue()))) 807 return failure(); 808 } 809 return success(); 810 } 811 812 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { 813 // Clear the block and value mappings, they are only relevant within one 814 // function. 815 blockMapping.clear(); 816 valueMapping.clear(); 817 llvm::Function *llvmFunc = functionMapping.lookup(func.getName()); 818 819 // Translate the debug information for this function. 820 debugTranslation->translate(func, *llvmFunc); 821 822 // Add function arguments to the value remapping table. 823 // If there was noalias info then we decorate each argument accordingly. 824 unsigned int argIdx = 0; 825 for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) { 826 llvm::Argument &llvmArg = std::get<1>(kvp); 827 BlockArgument mlirArg = std::get<0>(kvp); 828 829 if (auto attr = func.getArgAttrOfType<BoolAttr>(argIdx, "llvm.noalias")) { 830 // NB: Attribute already verified to be boolean, so check if we can indeed 831 // attach the attribute to this argument, based on its type. 832 auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>(); 833 if (!argTy.isPointerTy()) 834 return func.emitError( 835 "llvm.noalias attribute attached to LLVM non-pointer argument"); 836 if (attr.getValue()) 837 llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias); 838 } 839 840 if (auto attr = func.getArgAttrOfType<IntegerAttr>(argIdx, "llvm.align")) { 841 // NB: Attribute already verified to be int, so check if we can indeed 842 // attach the attribute to this argument, based on its type. 843 auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>(); 844 if (!argTy.isPointerTy()) 845 return func.emitError( 846 "llvm.align attribute attached to LLVM non-pointer argument"); 847 llvmArg.addAttrs( 848 llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt()))); 849 } 850 851 valueMapping[mlirArg] = &llvmArg; 852 argIdx++; 853 } 854 855 // Check the personality and set it. 856 if (func.personality().hasValue()) { 857 llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext()); 858 if (llvm::Constant *pfunc = 859 getLLVMConstant(ty, func.personalityAttr(), func.getLoc())) 860 llvmFunc->setPersonalityFn(pfunc); 861 } 862 863 // First, create all blocks so we can jump to them. 864 llvm::LLVMContext &llvmContext = llvmFunc->getContext(); 865 for (auto &bb : func) { 866 auto *llvmBB = llvm::BasicBlock::Create(llvmContext); 867 llvmBB->insertInto(llvmFunc); 868 blockMapping[&bb] = llvmBB; 869 } 870 871 // Then, convert blocks one by one in topological order to ensure defs are 872 // converted before uses. 873 auto blocks = topologicalSort(func); 874 for (auto indexedBB : llvm::enumerate(blocks)) { 875 auto *bb = indexedBB.value(); 876 if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0))) 877 return failure(); 878 } 879 880 // Finally, after all blocks have been traversed and values mapped, connect 881 // the PHI nodes to the results of preceding blocks. 882 connectPHINodes(func, valueMapping, blockMapping); 883 return success(); 884 } 885 886 LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) { 887 for (Operation &o : getModuleBody(m).getOperations()) 888 if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp>(&o) && !o.isKnownTerminator()) 889 return o.emitOpError("unsupported module-level operation"); 890 return success(); 891 } 892 893 LogicalResult ModuleTranslation::convertFunctionSignatures() { 894 // Lock access to the llvm context. 895 llvm::sys::SmartScopedLock<true> scopedLock( 896 llvmDialect->getLLVMContextMutex()); 897 898 // Declare all functions first because there may be function calls that form a 899 // call graph with cycles, or global initializers that reference functions. 900 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 901 llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( 902 function.getName(), 903 cast<llvm::FunctionType>(convertType(function.getType()))); 904 llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); 905 llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage())); 906 functionMapping[function.getName()] = llvmFunc; 907 908 // Forward the pass-through attributes to LLVM. 909 if (failed(forwardPassthroughAttributes(function.getLoc(), 910 function.passthrough(), llvmFunc))) 911 return failure(); 912 } 913 914 return success(); 915 } 916 917 LogicalResult ModuleTranslation::convertFunctions() { 918 // Lock access to the llvm context. 919 llvm::sys::SmartScopedLock<true> scopedLock( 920 llvmDialect->getLLVMContextMutex()); 921 922 // Convert functions. 923 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 924 // Ignore external functions. 925 if (function.isExternal()) 926 continue; 927 928 if (failed(convertOneFunction(function))) 929 return failure(); 930 } 931 932 return success(); 933 } 934 935 llvm::Type *ModuleTranslation::convertType(LLVMType type) { 936 // Lock the LLVM context as we create types in it. 937 llvm::sys::SmartScopedLock<true> lock(llvmDialect->getLLVMContextMutex()); 938 return LLVM::translateTypeToLLVMIR(type, llvmDialect->getLLVMContext()); 939 } 940 941 /// A helper to look up remapped operands in the value remapping table.` 942 SmallVector<llvm::Value *, 8> 943 ModuleTranslation::lookupValues(ValueRange values) { 944 SmallVector<llvm::Value *, 8> remapped; 945 remapped.reserve(values.size()); 946 for (Value v : values) { 947 assert(valueMapping.count(v) && "referencing undefined value"); 948 remapped.push_back(valueMapping.lookup(v)); 949 } 950 return remapped; 951 } 952 953 std::unique_ptr<llvm::Module> 954 ModuleTranslation::prepareLLVMModule(Operation *m) { 955 auto *dialect = m->getContext()->getRegisteredDialect<LLVM::LLVMDialect>(); 956 assert(dialect && "LLVM dialect must be registered"); 957 // Lock the LLVM context as we might create new types here. 958 llvm::sys::SmartScopedLock<true> scopedLock(dialect->getLLVMContextMutex()); 959 960 auto llvmModule = llvm::CloneModule(dialect->getLLVMModule()); 961 if (!llvmModule) 962 return nullptr; 963 964 llvm::LLVMContext &llvmContext = llvmModule->getContext(); 965 llvm::IRBuilder<> builder(llvmContext); 966 967 // Inject declarations for `malloc` and `free` functions that can be used in 968 // memref allocation/deallocation coming from standard ops lowering. 969 llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(), 970 builder.getInt64Ty()); 971 llvmModule->getOrInsertFunction("free", builder.getVoidTy(), 972 builder.getInt8PtrTy()); 973 974 return llvmModule; 975 } 976