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/BuiltinOps.h" 21 #include "mlir/IR/BuiltinTypes.h" 22 #include "mlir/IR/RegionGraphTraits.h" 23 #include "mlir/Support/LLVM.h" 24 #include "mlir/Target/LLVMIR/TypeTranslation.h" 25 #include "llvm/ADT/TypeSwitch.h" 26 27 #include "llvm/ADT/PostOrderIterator.h" 28 #include "llvm/ADT/SetVector.h" 29 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 30 #include "llvm/IR/BasicBlock.h" 31 #include "llvm/IR/CFG.h" 32 #include "llvm/IR/Constants.h" 33 #include "llvm/IR/DerivedTypes.h" 34 #include "llvm/IR/IRBuilder.h" 35 #include "llvm/IR/InlineAsm.h" 36 #include "llvm/IR/LLVMContext.h" 37 #include "llvm/IR/MDBuilder.h" 38 #include "llvm/IR/Module.h" 39 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 40 #include "llvm/Transforms/Utils/Cloning.h" 41 42 using namespace mlir; 43 using namespace mlir::LLVM; 44 using namespace mlir::LLVM::detail; 45 46 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc" 47 48 /// Builds a constant of a sequential LLVM type `type`, potentially containing 49 /// other sequential types recursively, from the individual constant values 50 /// provided in `constants`. `shape` contains the number of elements in nested 51 /// sequential types. Reports errors at `loc` and returns nullptr on error. 52 static llvm::Constant * 53 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants, 54 ArrayRef<int64_t> shape, llvm::Type *type, 55 Location loc) { 56 if (shape.empty()) { 57 llvm::Constant *result = constants.front(); 58 constants = constants.drop_front(); 59 return result; 60 } 61 62 llvm::Type *elementType; 63 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 64 elementType = arrayTy->getElementType(); 65 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 66 elementType = vectorTy->getElementType(); 67 } else { 68 emitError(loc) << "expected sequential LLVM types wrapping a scalar"; 69 return nullptr; 70 } 71 72 SmallVector<llvm::Constant *, 8> nested; 73 nested.reserve(shape.front()); 74 for (int64_t i = 0; i < shape.front(); ++i) { 75 nested.push_back(buildSequentialConstant(constants, shape.drop_front(), 76 elementType, loc)); 77 if (!nested.back()) 78 return nullptr; 79 } 80 81 if (shape.size() == 1 && type->isVectorTy()) 82 return llvm::ConstantVector::get(nested); 83 return llvm::ConstantArray::get( 84 llvm::ArrayType::get(elementType, shape.front()), nested); 85 } 86 87 /// Returns the first non-sequential type nested in sequential types. 88 static llvm::Type *getInnermostElementType(llvm::Type *type) { 89 do { 90 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) { 91 type = arrayTy->getElementType(); 92 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) { 93 type = vectorTy->getElementType(); 94 } else { 95 return type; 96 } 97 } while (1); 98 } 99 100 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`. 101 /// This currently supports integer, floating point, splat and dense element 102 /// attributes and combinations thereof. In case of error, report it to `loc` 103 /// and return nullptr. 104 llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType, 105 Attribute attr, 106 Location loc) { 107 if (!attr) 108 return llvm::UndefValue::get(llvmType); 109 if (llvmType->isStructTy()) { 110 emitError(loc, "struct types are not supported in constants"); 111 return nullptr; 112 } 113 // For integer types, we allow a mismatch in sizes as the index type in 114 // MLIR might have a different size than the index type in the LLVM module. 115 if (auto intAttr = attr.dyn_cast<IntegerAttr>()) 116 return llvm::ConstantInt::get( 117 llvmType, 118 intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth())); 119 if (auto floatAttr = attr.dyn_cast<FloatAttr>()) 120 return llvm::ConstantFP::get(llvmType, floatAttr.getValue()); 121 if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>()) 122 return llvm::ConstantExpr::getBitCast( 123 functionMapping.lookup(funcAttr.getValue()), llvmType); 124 if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) { 125 llvm::Type *elementType; 126 uint64_t numElements; 127 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) { 128 elementType = arrayTy->getElementType(); 129 numElements = arrayTy->getNumElements(); 130 } else { 131 auto *vectorTy = cast<llvm::FixedVectorType>(llvmType); 132 elementType = vectorTy->getElementType(); 133 numElements = vectorTy->getNumElements(); 134 } 135 // Splat value is a scalar. Extract it only if the element type is not 136 // another sequence type. The recursion terminates because each step removes 137 // one outer sequential type. 138 bool elementTypeSequential = 139 isa<llvm::ArrayType, llvm::VectorType>(elementType); 140 llvm::Constant *child = getLLVMConstant( 141 elementType, 142 elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc); 143 if (!child) 144 return nullptr; 145 if (llvmType->isVectorTy()) 146 return llvm::ConstantVector::getSplat( 147 llvm::ElementCount::get(numElements, /*Scalable=*/false), child); 148 if (llvmType->isArrayTy()) { 149 auto *arrayType = llvm::ArrayType::get(elementType, numElements); 150 SmallVector<llvm::Constant *, 8> constants(numElements, child); 151 return llvm::ConstantArray::get(arrayType, constants); 152 } 153 } 154 155 if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) { 156 assert(elementsAttr.getType().hasStaticShape()); 157 assert(elementsAttr.getNumElements() != 0 && 158 "unexpected empty elements attribute"); 159 assert(!elementsAttr.getType().getShape().empty() && 160 "unexpected empty elements attribute shape"); 161 162 SmallVector<llvm::Constant *, 8> constants; 163 constants.reserve(elementsAttr.getNumElements()); 164 llvm::Type *innermostType = getInnermostElementType(llvmType); 165 for (auto n : elementsAttr.getValues<Attribute>()) { 166 constants.push_back(getLLVMConstant(innermostType, n, loc)); 167 if (!constants.back()) 168 return nullptr; 169 } 170 ArrayRef<llvm::Constant *> constantsRef = constants; 171 llvm::Constant *result = buildSequentialConstant( 172 constantsRef, elementsAttr.getType().getShape(), llvmType, loc); 173 assert(constantsRef.empty() && "did not consume all elemental constants"); 174 return result; 175 } 176 177 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 178 return llvm::ConstantDataArray::get( 179 llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(), 180 stringAttr.getValue().size()}); 181 } 182 emitError(loc, "unsupported constant value"); 183 return nullptr; 184 } 185 186 /// Convert MLIR integer comparison predicate to LLVM IR comparison predicate. 187 static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) { 188 switch (p) { 189 case LLVM::ICmpPredicate::eq: 190 return llvm::CmpInst::Predicate::ICMP_EQ; 191 case LLVM::ICmpPredicate::ne: 192 return llvm::CmpInst::Predicate::ICMP_NE; 193 case LLVM::ICmpPredicate::slt: 194 return llvm::CmpInst::Predicate::ICMP_SLT; 195 case LLVM::ICmpPredicate::sle: 196 return llvm::CmpInst::Predicate::ICMP_SLE; 197 case LLVM::ICmpPredicate::sgt: 198 return llvm::CmpInst::Predicate::ICMP_SGT; 199 case LLVM::ICmpPredicate::sge: 200 return llvm::CmpInst::Predicate::ICMP_SGE; 201 case LLVM::ICmpPredicate::ult: 202 return llvm::CmpInst::Predicate::ICMP_ULT; 203 case LLVM::ICmpPredicate::ule: 204 return llvm::CmpInst::Predicate::ICMP_ULE; 205 case LLVM::ICmpPredicate::ugt: 206 return llvm::CmpInst::Predicate::ICMP_UGT; 207 case LLVM::ICmpPredicate::uge: 208 return llvm::CmpInst::Predicate::ICMP_UGE; 209 } 210 llvm_unreachable("incorrect comparison predicate"); 211 } 212 213 static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) { 214 switch (p) { 215 case LLVM::FCmpPredicate::_false: 216 return llvm::CmpInst::Predicate::FCMP_FALSE; 217 case LLVM::FCmpPredicate::oeq: 218 return llvm::CmpInst::Predicate::FCMP_OEQ; 219 case LLVM::FCmpPredicate::ogt: 220 return llvm::CmpInst::Predicate::FCMP_OGT; 221 case LLVM::FCmpPredicate::oge: 222 return llvm::CmpInst::Predicate::FCMP_OGE; 223 case LLVM::FCmpPredicate::olt: 224 return llvm::CmpInst::Predicate::FCMP_OLT; 225 case LLVM::FCmpPredicate::ole: 226 return llvm::CmpInst::Predicate::FCMP_OLE; 227 case LLVM::FCmpPredicate::one: 228 return llvm::CmpInst::Predicate::FCMP_ONE; 229 case LLVM::FCmpPredicate::ord: 230 return llvm::CmpInst::Predicate::FCMP_ORD; 231 case LLVM::FCmpPredicate::ueq: 232 return llvm::CmpInst::Predicate::FCMP_UEQ; 233 case LLVM::FCmpPredicate::ugt: 234 return llvm::CmpInst::Predicate::FCMP_UGT; 235 case LLVM::FCmpPredicate::uge: 236 return llvm::CmpInst::Predicate::FCMP_UGE; 237 case LLVM::FCmpPredicate::ult: 238 return llvm::CmpInst::Predicate::FCMP_ULT; 239 case LLVM::FCmpPredicate::ule: 240 return llvm::CmpInst::Predicate::FCMP_ULE; 241 case LLVM::FCmpPredicate::une: 242 return llvm::CmpInst::Predicate::FCMP_UNE; 243 case LLVM::FCmpPredicate::uno: 244 return llvm::CmpInst::Predicate::FCMP_UNO; 245 case LLVM::FCmpPredicate::_true: 246 return llvm::CmpInst::Predicate::FCMP_TRUE; 247 } 248 llvm_unreachable("incorrect comparison predicate"); 249 } 250 251 static llvm::AtomicRMWInst::BinOp getLLVMAtomicBinOp(AtomicBinOp op) { 252 switch (op) { 253 case LLVM::AtomicBinOp::xchg: 254 return llvm::AtomicRMWInst::BinOp::Xchg; 255 case LLVM::AtomicBinOp::add: 256 return llvm::AtomicRMWInst::BinOp::Add; 257 case LLVM::AtomicBinOp::sub: 258 return llvm::AtomicRMWInst::BinOp::Sub; 259 case LLVM::AtomicBinOp::_and: 260 return llvm::AtomicRMWInst::BinOp::And; 261 case LLVM::AtomicBinOp::nand: 262 return llvm::AtomicRMWInst::BinOp::Nand; 263 case LLVM::AtomicBinOp::_or: 264 return llvm::AtomicRMWInst::BinOp::Or; 265 case LLVM::AtomicBinOp::_xor: 266 return llvm::AtomicRMWInst::BinOp::Xor; 267 case LLVM::AtomicBinOp::max: 268 return llvm::AtomicRMWInst::BinOp::Max; 269 case LLVM::AtomicBinOp::min: 270 return llvm::AtomicRMWInst::BinOp::Min; 271 case LLVM::AtomicBinOp::umax: 272 return llvm::AtomicRMWInst::BinOp::UMax; 273 case LLVM::AtomicBinOp::umin: 274 return llvm::AtomicRMWInst::BinOp::UMin; 275 case LLVM::AtomicBinOp::fadd: 276 return llvm::AtomicRMWInst::BinOp::FAdd; 277 case LLVM::AtomicBinOp::fsub: 278 return llvm::AtomicRMWInst::BinOp::FSub; 279 } 280 llvm_unreachable("incorrect atomic binary operator"); 281 } 282 283 static llvm::AtomicOrdering getLLVMAtomicOrdering(AtomicOrdering ordering) { 284 switch (ordering) { 285 case LLVM::AtomicOrdering::not_atomic: 286 return llvm::AtomicOrdering::NotAtomic; 287 case LLVM::AtomicOrdering::unordered: 288 return llvm::AtomicOrdering::Unordered; 289 case LLVM::AtomicOrdering::monotonic: 290 return llvm::AtomicOrdering::Monotonic; 291 case LLVM::AtomicOrdering::acquire: 292 return llvm::AtomicOrdering::Acquire; 293 case LLVM::AtomicOrdering::release: 294 return llvm::AtomicOrdering::Release; 295 case LLVM::AtomicOrdering::acq_rel: 296 return llvm::AtomicOrdering::AcquireRelease; 297 case LLVM::AtomicOrdering::seq_cst: 298 return llvm::AtomicOrdering::SequentiallyConsistent; 299 } 300 llvm_unreachable("incorrect atomic ordering"); 301 } 302 303 ModuleTranslation::ModuleTranslation(Operation *module, 304 std::unique_ptr<llvm::Module> llvmModule) 305 : mlirModule(module), llvmModule(std::move(llvmModule)), 306 debugTranslation( 307 std::make_unique<DebugTranslation>(module, *this->llvmModule)), 308 ompDialect(module->getContext()->getLoadedDialect("omp")), 309 typeTranslator(this->llvmModule->getContext()) { 310 assert(satisfiesLLVMModule(mlirModule) && 311 "mlirModule should honor LLVM's module semantics."); 312 } 313 ModuleTranslation::~ModuleTranslation() { 314 if (ompBuilder) 315 ompBuilder->finalize(); 316 } 317 318 /// Get the SSA value passed to the current block from the terminator operation 319 /// of its predecessor. 320 static Value getPHISourceValue(Block *current, Block *pred, 321 unsigned numArguments, unsigned index) { 322 Operation &terminator = *pred->getTerminator(); 323 if (isa<LLVM::BrOp>(terminator)) 324 return terminator.getOperand(index); 325 326 SuccessorRange successors = terminator.getSuccessors(); 327 assert(std::adjacent_find(successors.begin(), successors.end()) == 328 successors.end() && 329 "successors with arguments in LLVM branches must be different blocks"); 330 (void)successors; 331 332 // For instructions that branch based on a condition value, we need to take 333 // the operands for the branch that was taken. 334 if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) { 335 // For conditional branches, we take the operands from either the "true" or 336 // the "false" branch. 337 return condBranchOp.getSuccessor(0) == current 338 ? condBranchOp.trueDestOperands()[index] 339 : condBranchOp.falseDestOperands()[index]; 340 } else if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) { 341 // For switches, we take the operands from either the default case, or from 342 // the case branch that was taken. 343 if (switchOp.defaultDestination() == current) 344 return switchOp.defaultOperands()[index]; 345 for (auto i : llvm::enumerate(switchOp.caseDestinations())) 346 if (i.value() == current) 347 return switchOp.getCaseOperands(i.index())[index]; 348 } 349 350 llvm_unreachable("only branch or switch operations can be terminators of a " 351 "block that has successors"); 352 } 353 354 /// Connect the PHI nodes to the results of preceding blocks. 355 template <typename T> 356 static void connectPHINodes( 357 T &func, const DenseMap<Value, llvm::Value *> &valueMapping, 358 const DenseMap<Block *, llvm::BasicBlock *> &blockMapping, 359 const DenseMap<Operation *, llvm::Instruction *> &branchMapping) { 360 // Skip the first block, it cannot be branched to and its arguments correspond 361 // to the arguments of the LLVM function. 362 for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) { 363 Block *bb = &*it; 364 llvm::BasicBlock *llvmBB = blockMapping.lookup(bb); 365 auto phis = llvmBB->phis(); 366 auto numArguments = bb->getNumArguments(); 367 assert(numArguments == std::distance(phis.begin(), phis.end())); 368 for (auto &numberedPhiNode : llvm::enumerate(phis)) { 369 auto &phiNode = numberedPhiNode.value(); 370 unsigned index = numberedPhiNode.index(); 371 for (auto *pred : bb->getPredecessors()) { 372 // Find the LLVM IR block that contains the converted terminator 373 // instruction and use it in the PHI node. Note that this block is not 374 // necessarily the same as blockMapping.lookup(pred), some operations 375 // (in particular, OpenMP operations using OpenMPIRBuilder) may have 376 // split the blocks. 377 llvm::Instruction *terminator = 378 branchMapping.lookup(pred->getTerminator()); 379 assert(terminator && "missing the mapping for a terminator"); 380 phiNode.addIncoming(valueMapping.lookup(getPHISourceValue( 381 bb, pred, numArguments, index)), 382 terminator->getParent()); 383 } 384 } 385 } 386 } 387 388 /// Sort function blocks topologically. 389 template <typename T> 390 static llvm::SetVector<Block *> topologicalSort(T &f) { 391 // For each block that has not been visited yet (i.e. that has no 392 // predecessors), add it to the list as well as its successors. 393 llvm::SetVector<Block *> blocks; 394 for (Block &b : f) { 395 if (blocks.count(&b) == 0) { 396 llvm::ReversePostOrderTraversal<Block *> traversal(&b); 397 blocks.insert(traversal.begin(), traversal.end()); 398 } 399 } 400 assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted"); 401 402 return blocks; 403 } 404 405 /// Convert the OpenMP parallel Operation to LLVM IR. 406 LogicalResult 407 ModuleTranslation::convertOmpParallel(Operation &opInst, 408 llvm::IRBuilder<> &builder) { 409 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 410 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 411 // relying on captured variables. 412 LogicalResult bodyGenStatus = success(); 413 414 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 415 llvm::BasicBlock &continuationIP) { 416 // ParallelOp has only one region associated with it. 417 auto ®ion = cast<omp::ParallelOp>(opInst).getRegion(); 418 convertOmpOpRegions(region, "omp.par.region", valueMapping, blockMapping, 419 *codeGenIP.getBlock(), continuationIP, builder, 420 bodyGenStatus); 421 }; 422 423 // TODO: Perform appropriate actions according to the data-sharing 424 // attribute (shared, private, firstprivate, ...) of variables. 425 // Currently defaults to shared. 426 auto privCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 427 llvm::Value &, llvm::Value &vPtr, 428 llvm::Value *&replacementValue) -> InsertPointTy { 429 replacementValue = &vPtr; 430 431 return codeGenIP; 432 }; 433 434 // TODO: Perform finalization actions for variables. This has to be 435 // called for variables which have destructors/finalizers. 436 auto finiCB = [&](InsertPointTy codeGenIP) {}; 437 438 llvm::Value *ifCond = nullptr; 439 if (auto ifExprVar = cast<omp::ParallelOp>(opInst).if_expr_var()) 440 ifCond = valueMapping.lookup(ifExprVar); 441 llvm::Value *numThreads = nullptr; 442 if (auto numThreadsVar = cast<omp::ParallelOp>(opInst).num_threads_var()) 443 numThreads = valueMapping.lookup(numThreadsVar); 444 llvm::omp::ProcBindKind pbKind = llvm::omp::OMP_PROC_BIND_default; 445 if (auto bind = cast<omp::ParallelOp>(opInst).proc_bind_val()) 446 pbKind = llvm::omp::getProcBindKind(bind.getValue()); 447 // TODO: Is the Parallel construct cancellable? 448 bool isCancellable = false; 449 // TODO: Determine the actual alloca insertion point, e.g., the function 450 // entry or the alloca insertion point as provided by the body callback 451 // above. 452 llvm::OpenMPIRBuilder::InsertPointTy allocaIP(builder.saveIP()); 453 if (failed(bodyGenStatus)) 454 return failure(); 455 builder.restoreIP( 456 ompBuilder->createParallel(builder, allocaIP, bodyGenCB, privCB, finiCB, 457 ifCond, numThreads, pbKind, isCancellable)); 458 return success(); 459 } 460 461 void ModuleTranslation::convertOmpOpRegions( 462 Region ®ion, StringRef blockName, 463 DenseMap<Value, llvm::Value *> &valueMapping, 464 DenseMap<Block *, llvm::BasicBlock *> &blockMapping, 465 llvm::BasicBlock &sourceBlock, llvm::BasicBlock &continuationBlock, 466 llvm::IRBuilder<> &builder, LogicalResult &bodyGenStatus) { 467 llvm::LLVMContext &llvmContext = builder.getContext(); 468 for (Block &bb : region) { 469 llvm::BasicBlock *llvmBB = llvm::BasicBlock::Create( 470 llvmContext, blockName, builder.GetInsertBlock()->getParent()); 471 blockMapping[&bb] = llvmBB; 472 } 473 474 llvm::Instruction *sourceTerminator = sourceBlock.getTerminator(); 475 476 // Convert blocks one by one in topological order to ensure 477 // defs are converted before uses. 478 llvm::SetVector<Block *> blocks = topologicalSort(region); 479 for (Block *bb : blocks) { 480 llvm::BasicBlock *llvmBB = blockMapping[bb]; 481 // Retarget the branch of the entry block to the entry block of the 482 // converted region (regions are single-entry). 483 if (bb->isEntryBlock()) { 484 assert(sourceTerminator->getNumSuccessors() == 1 && 485 "provided entry block has multiple successors"); 486 assert(sourceTerminator->getSuccessor(0) == &continuationBlock && 487 "ContinuationBlock is not the successor of the entry block"); 488 sourceTerminator->setSuccessor(0, llvmBB); 489 } 490 491 llvm::IRBuilder<>::InsertPointGuard guard(builder); 492 if (failed(convertBlock(*bb, bb->isEntryBlock(), builder))) { 493 bodyGenStatus = failure(); 494 return; 495 } 496 497 // Special handling for `omp.yield` and `omp.terminator` (we may have more 498 // than one): they return the control to the parent OpenMP dialect operation 499 // so replace them with the branch to the continuation block. We handle this 500 // here to avoid relying inter-function communication through the 501 // ModuleTranslation class to set up the correct insertion point. This is 502 // also consistent with MLIR's idiom of handling special region terminators 503 // in the same code that handles the region-owning operation. 504 if (isa<omp::TerminatorOp, omp::YieldOp>(bb->getTerminator())) 505 builder.CreateBr(&continuationBlock); 506 } 507 // Finally, after all blocks have been traversed and values mapped, 508 // connect the PHI nodes to the results of preceding blocks. 509 connectPHINodes(region, valueMapping, blockMapping, branchMapping); 510 } 511 512 LogicalResult ModuleTranslation::convertOmpMaster(Operation &opInst, 513 llvm::IRBuilder<> &builder) { 514 using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; 515 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 516 // relying on captured variables. 517 LogicalResult bodyGenStatus = success(); 518 519 auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP, 520 llvm::BasicBlock &continuationIP) { 521 // MasterOp has only one region associated with it. 522 auto ®ion = cast<omp::MasterOp>(opInst).getRegion(); 523 convertOmpOpRegions(region, "omp.master.region", valueMapping, blockMapping, 524 *codeGenIP.getBlock(), continuationIP, builder, 525 bodyGenStatus); 526 }; 527 528 // TODO: Perform finalization actions for variables. This has to be 529 // called for variables which have destructors/finalizers. 530 auto finiCB = [&](InsertPointTy codeGenIP) {}; 531 532 builder.restoreIP(ompBuilder->createMaster(builder, bodyGenCB, finiCB)); 533 return success(); 534 } 535 536 /// Converts an OpenMP workshare loop into LLVM IR using OpenMPIRBuilder. 537 LogicalResult ModuleTranslation::convertOmpWsLoop(Operation &opInst, 538 llvm::IRBuilder<> &builder) { 539 auto loop = cast<omp::WsLoopOp>(opInst); 540 // TODO: this should be in the op verifier instead. 541 if (loop.lowerBound().empty()) 542 return failure(); 543 544 if (loop.getNumLoops() != 1) 545 return opInst.emitOpError("collapsed loops not yet supported"); 546 547 if (loop.schedule_val().hasValue() && 548 omp::symbolizeClauseScheduleKind(loop.schedule_val().getValue()) != 549 omp::ClauseScheduleKind::Static) 550 return opInst.emitOpError( 551 "only static (default) loop schedule is currently supported"); 552 553 // Find the loop configuration. 554 llvm::Value *lowerBound = valueMapping.lookup(loop.lowerBound()[0]); 555 llvm::Value *upperBound = valueMapping.lookup(loop.upperBound()[0]); 556 llvm::Value *step = valueMapping.lookup(loop.step()[0]); 557 llvm::Type *ivType = step->getType(); 558 llvm::Value *chunk = loop.schedule_chunk_var() 559 ? valueMapping[loop.schedule_chunk_var()] 560 : llvm::ConstantInt::get(ivType, 1); 561 562 // Set up the source location value for OpenMP runtime. 563 llvm::DISubprogram *subprogram = 564 builder.GetInsertBlock()->getParent()->getSubprogram(); 565 const llvm::DILocation *diLoc = 566 debugTranslation->translateLoc(opInst.getLoc(), subprogram); 567 llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder.saveIP(), 568 llvm::DebugLoc(diLoc)); 569 570 // Generator of the canonical loop body. Produces an SESE region of basic 571 // blocks. 572 // TODO: support error propagation in OpenMPIRBuilder and use it instead of 573 // relying on captured variables. 574 LogicalResult bodyGenStatus = success(); 575 auto bodyGen = [&](llvm::OpenMPIRBuilder::InsertPointTy ip, llvm::Value *iv) { 576 llvm::IRBuilder<>::InsertPointGuard guard(builder); 577 578 // Make sure further conversions know about the induction variable. 579 valueMapping[loop.getRegion().front().getArgument(0)] = iv; 580 581 llvm::BasicBlock *entryBlock = ip.getBlock(); 582 llvm::BasicBlock *exitBlock = 583 entryBlock->splitBasicBlock(ip.getPoint(), "omp.wsloop.exit"); 584 585 // Convert the body of the loop. 586 convertOmpOpRegions(loop.region(), "omp.wsloop.region", valueMapping, 587 blockMapping, *entryBlock, *exitBlock, builder, 588 bodyGenStatus); 589 }; 590 591 // Delegate actual loop construction to the OpenMP IRBuilder. 592 // TODO: this currently assumes WsLoop is semantically similar to SCF loop, 593 // i.e. it has a positive step, uses signed integer semantics, and its upper 594 // bound is not included. Reconsider this code when WsLoop clearly supports 595 // more cases. 596 llvm::BasicBlock *insertBlock = builder.GetInsertBlock(); 597 llvm::CanonicalLoopInfo *loopInfo = ompBuilder->createCanonicalLoop( 598 ompLoc, bodyGen, lowerBound, upperBound, step, /*IsSigned=*/true, 599 /*InclusiveStop=*/false); 600 if (failed(bodyGenStatus)) 601 return failure(); 602 603 // TODO: get the alloca insertion point from the parallel operation builder. 604 // If we insert the at the top of the current function, they will be passed as 605 // extra arguments into the function the parallel operation builder outlines. 606 // Put them at the start of the current block for now. 607 llvm::OpenMPIRBuilder::InsertPointTy allocaIP( 608 insertBlock, insertBlock->getFirstInsertionPt()); 609 loopInfo = ompBuilder->createStaticWorkshareLoop( 610 ompLoc, loopInfo, allocaIP, 611 !loop.nowait().hasValue() || loop.nowait().getValue(), chunk); 612 613 // Continue building IR after the loop. 614 builder.restoreIP(loopInfo->getAfterIP()); 615 return success(); 616 } 617 618 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR 619 /// (including OpenMP runtime calls). 620 LogicalResult 621 ModuleTranslation::convertOmpOperation(Operation &opInst, 622 llvm::IRBuilder<> &builder) { 623 if (!ompBuilder) { 624 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule); 625 ompBuilder->initialize(); 626 } 627 return llvm::TypeSwitch<Operation *, LogicalResult>(&opInst) 628 .Case([&](omp::BarrierOp) { 629 ompBuilder->createBarrier(builder.saveIP(), llvm::omp::OMPD_barrier); 630 return success(); 631 }) 632 .Case([&](omp::TaskwaitOp) { 633 ompBuilder->createTaskwait(builder.saveIP()); 634 return success(); 635 }) 636 .Case([&](omp::TaskyieldOp) { 637 ompBuilder->createTaskyield(builder.saveIP()); 638 return success(); 639 }) 640 .Case([&](omp::FlushOp) { 641 // No support in Openmp runtime function (__kmpc_flush) to accept 642 // the argument list. 643 // OpenMP standard states the following: 644 // "An implementation may implement a flush with a list by ignoring 645 // the list, and treating it the same as a flush without a list." 646 // 647 // The argument list is discarded so that, flush with a list is treated 648 // same as a flush without a list. 649 ompBuilder->createFlush(builder.saveIP()); 650 return success(); 651 }) 652 .Case( 653 [&](omp::ParallelOp) { return convertOmpParallel(opInst, builder); }) 654 .Case([&](omp::MasterOp) { return convertOmpMaster(opInst, builder); }) 655 .Case([&](omp::WsLoopOp) { return convertOmpWsLoop(opInst, builder); }) 656 .Case<omp::YieldOp, omp::TerminatorOp>([](auto op) { 657 // `yield` and `terminator` can be just omitted. The block structure was 658 // created in the function that handles their parent operation. 659 assert(op->getNumOperands() == 0 && 660 "unexpected OpenMP terminator with operands"); 661 return success(); 662 }) 663 .Default([&](Operation *inst) { 664 return inst->emitError("unsupported OpenMP operation: ") 665 << inst->getName(); 666 }); 667 } 668 669 static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op) { 670 using llvmFMF = llvm::FastMathFlags; 671 using FuncT = void (llvmFMF::*)(bool); 672 const std::pair<FastmathFlags, FuncT> handlers[] = { 673 // clang-format off 674 {FastmathFlags::nnan, &llvmFMF::setNoNaNs}, 675 {FastmathFlags::ninf, &llvmFMF::setNoInfs}, 676 {FastmathFlags::nsz, &llvmFMF::setNoSignedZeros}, 677 {FastmathFlags::arcp, &llvmFMF::setAllowReciprocal}, 678 {FastmathFlags::contract, &llvmFMF::setAllowContract}, 679 {FastmathFlags::afn, &llvmFMF::setApproxFunc}, 680 {FastmathFlags::reassoc, &llvmFMF::setAllowReassoc}, 681 {FastmathFlags::fast, &llvmFMF::setFast}, 682 // clang-format on 683 }; 684 llvm::FastMathFlags ret; 685 auto fmf = op.fastmathFlags(); 686 for (auto it : handlers) 687 if (bitEnumContains(fmf, it.first)) 688 (ret.*(it.second))(true); 689 return ret; 690 } 691 692 /// Given a single MLIR operation, create the corresponding LLVM IR operation 693 /// using the `builder`. LLVM IR Builder does not have a generic interface so 694 /// this has to be a long chain of `if`s calling different functions with a 695 /// different number of arguments. 696 LogicalResult ModuleTranslation::convertOperation(Operation &opInst, 697 llvm::IRBuilder<> &builder) { 698 auto extractPosition = [](ArrayAttr attr) { 699 SmallVector<unsigned, 4> position; 700 position.reserve(attr.size()); 701 for (Attribute v : attr) 702 position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue()); 703 return position; 704 }; 705 706 llvm::IRBuilder<>::FastMathFlagGuard fmfGuard(builder); 707 if (auto fmf = dyn_cast<FastmathFlagsInterface>(opInst)) 708 builder.setFastMathFlags(getFastmathFlags(fmf)); 709 710 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc" 711 712 // Emit function calls. If the "callee" attribute is present, this is a 713 // direct function call and we also need to look up the remapped function 714 // itself. Otherwise, this is an indirect call and the callee is the first 715 // operand, look it up as a normal value. Return the llvm::Value representing 716 // the function result, which may be of llvm::VoidTy type. 717 auto convertCall = [this, &builder](Operation &op) -> llvm::Value * { 718 auto operands = lookupValues(op.getOperands()); 719 ArrayRef<llvm::Value *> operandsRef(operands); 720 if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) { 721 return builder.CreateCall(functionMapping.lookup(attr.getValue()), 722 operandsRef); 723 } else { 724 auto *calleePtrType = 725 cast<llvm::PointerType>(operandsRef.front()->getType()); 726 auto *calleeType = 727 cast<llvm::FunctionType>(calleePtrType->getElementType()); 728 return builder.CreateCall(calleeType, operandsRef.front(), 729 operandsRef.drop_front()); 730 } 731 }; 732 733 // Emit calls. If the called function has a result, remap the corresponding 734 // value. Note that LLVM IR dialect CallOp has either 0 or 1 result. 735 if (isa<LLVM::CallOp>(opInst)) { 736 llvm::Value *result = convertCall(opInst); 737 if (opInst.getNumResults() != 0) { 738 valueMapping[opInst.getResult(0)] = result; 739 return success(); 740 } 741 // Check that LLVM call returns void for 0-result functions. 742 return success(result->getType()->isVoidTy()); 743 } 744 745 if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) { 746 // TODO: refactor function type creation which usually occurs in std-LLVM 747 // conversion. 748 SmallVector<Type, 8> operandTypes; 749 operandTypes.reserve(inlineAsmOp.operands().size()); 750 for (auto t : inlineAsmOp.operands().getTypes()) 751 operandTypes.push_back(t); 752 753 Type resultType; 754 if (inlineAsmOp.getNumResults() == 0) { 755 resultType = LLVM::LLVMVoidType::get(mlirModule->getContext()); 756 } else { 757 assert(inlineAsmOp.getNumResults() == 1); 758 resultType = inlineAsmOp.getResultTypes()[0]; 759 } 760 auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes); 761 llvm::InlineAsm *inlineAsmInst = 762 inlineAsmOp.asm_dialect().hasValue() 763 ? llvm::InlineAsm::get( 764 static_cast<llvm::FunctionType *>(convertType(ft)), 765 inlineAsmOp.asm_string(), inlineAsmOp.constraints(), 766 inlineAsmOp.has_side_effects(), inlineAsmOp.is_align_stack(), 767 convertAsmDialectToLLVM(*inlineAsmOp.asm_dialect())) 768 : llvm::InlineAsm::get( 769 static_cast<llvm::FunctionType *>(convertType(ft)), 770 inlineAsmOp.asm_string(), inlineAsmOp.constraints(), 771 inlineAsmOp.has_side_effects(), inlineAsmOp.is_align_stack()); 772 llvm::Value *result = 773 builder.CreateCall(inlineAsmInst, lookupValues(inlineAsmOp.operands())); 774 if (opInst.getNumResults() != 0) 775 valueMapping[opInst.getResult(0)] = result; 776 return success(); 777 } 778 779 if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) { 780 auto operands = lookupValues(opInst.getOperands()); 781 ArrayRef<llvm::Value *> operandsRef(operands); 782 if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) { 783 builder.CreateInvoke(functionMapping.lookup(attr.getValue()), 784 blockMapping[invOp.getSuccessor(0)], 785 blockMapping[invOp.getSuccessor(1)], operandsRef); 786 } else { 787 auto *calleePtrType = 788 cast<llvm::PointerType>(operandsRef.front()->getType()); 789 auto *calleeType = 790 cast<llvm::FunctionType>(calleePtrType->getElementType()); 791 builder.CreateInvoke( 792 calleeType, operandsRef.front(), blockMapping[invOp.getSuccessor(0)], 793 blockMapping[invOp.getSuccessor(1)], operandsRef.drop_front()); 794 } 795 return success(); 796 } 797 798 if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) { 799 llvm::Type *ty = convertType(lpOp.getType()); 800 llvm::LandingPadInst *lpi = 801 builder.CreateLandingPad(ty, lpOp.getNumOperands()); 802 803 // Add clauses 804 for (auto operand : lookupValues(lpOp.getOperands())) { 805 // All operands should be constant - checked by verifier 806 if (auto constOperand = dyn_cast<llvm::Constant>(operand)) 807 lpi->addClause(constOperand); 808 } 809 valueMapping[lpOp.getResult()] = lpi; 810 return success(); 811 } 812 813 // Emit branches. We need to look up the remapped blocks and ignore the block 814 // arguments that were transformed into PHI nodes. 815 if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) { 816 llvm::BranchInst *branch = 817 builder.CreateBr(blockMapping[brOp.getSuccessor()]); 818 branchMapping.try_emplace(&opInst, branch); 819 return success(); 820 } 821 if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) { 822 auto weights = condbrOp.branch_weights(); 823 llvm::MDNode *branchWeights = nullptr; 824 if (weights) { 825 // Map weight attributes to LLVM metadata. 826 auto trueWeight = 827 weights.getValue().getValue(0).cast<IntegerAttr>().getInt(); 828 auto falseWeight = 829 weights.getValue().getValue(1).cast<IntegerAttr>().getInt(); 830 branchWeights = 831 llvm::MDBuilder(llvmModule->getContext()) 832 .createBranchWeights(static_cast<uint32_t>(trueWeight), 833 static_cast<uint32_t>(falseWeight)); 834 } 835 llvm::BranchInst *branch = builder.CreateCondBr( 836 valueMapping.lookup(condbrOp.getOperand(0)), 837 blockMapping[condbrOp.getSuccessor(0)], 838 blockMapping[condbrOp.getSuccessor(1)], branchWeights); 839 branchMapping.try_emplace(&opInst, branch); 840 return success(); 841 } 842 if (auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) { 843 llvm::MDNode *branchWeights = nullptr; 844 if (auto weights = switchOp.branch_weights()) { 845 llvm::SmallVector<uint32_t> weightValues; 846 weightValues.reserve(weights->size()); 847 for (llvm::APInt weight : weights->cast<DenseIntElementsAttr>()) 848 weightValues.push_back(weight.getLimitedValue()); 849 branchWeights = llvm::MDBuilder(llvmModule->getContext()) 850 .createBranchWeights(weightValues); 851 } 852 853 llvm::SwitchInst *switchInst = 854 builder.CreateSwitch(valueMapping[switchOp.value()], 855 blockMapping[switchOp.defaultDestination()], 856 switchOp.caseDestinations().size(), branchWeights); 857 858 auto *ty = 859 llvm::cast<llvm::IntegerType>(convertType(switchOp.value().getType())); 860 for (auto i : 861 llvm::zip(switchOp.case_values()->cast<DenseIntElementsAttr>(), 862 switchOp.caseDestinations())) 863 switchInst->addCase( 864 llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()), 865 blockMapping[std::get<1>(i)]); 866 867 branchMapping.try_emplace(&opInst, switchInst); 868 return success(); 869 } 870 871 // Emit addressof. We need to look up the global value referenced by the 872 // operation and store it in the MLIR-to-LLVM value mapping. This does not 873 // emit any LLVM instruction. 874 if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) { 875 LLVM::GlobalOp global = addressOfOp.getGlobal(); 876 LLVM::LLVMFuncOp function = addressOfOp.getFunction(); 877 878 // The verifier should not have allowed this. 879 assert((global || function) && 880 "referencing an undefined global or function"); 881 882 valueMapping[addressOfOp.getResult()] = 883 global ? globalsMapping.lookup(global) 884 : functionMapping.lookup(function.getName()); 885 return success(); 886 } 887 888 if (ompDialect && opInst.getDialect() == ompDialect) 889 return convertOmpOperation(opInst, builder); 890 891 return opInst.emitError("unsupported or non-LLVM operation: ") 892 << opInst.getName(); 893 } 894 895 /// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes 896 /// to define values corresponding to the MLIR block arguments. These nodes 897 /// are not connected to the source basic blocks, which may not exist yet. Uses 898 /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have 899 /// been created for `bb` and included in the block mapping. Inserts new 900 /// instructions at the end of the block and leaves `builder` in a state 901 /// suitable for further insertion into the end of the block. 902 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments, 903 llvm::IRBuilder<> &builder) { 904 builder.SetInsertPoint(blockMapping[&bb]); 905 auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram(); 906 907 // Before traversing operations, make block arguments available through 908 // value remapping and PHI nodes, but do not add incoming edges for the PHI 909 // nodes just yet: those values may be defined by this or following blocks. 910 // This step is omitted if "ignoreArguments" is set. The arguments of the 911 // first block have been already made available through the remapping of 912 // LLVM function arguments. 913 if (!ignoreArguments) { 914 auto predecessors = bb.getPredecessors(); 915 unsigned numPredecessors = 916 std::distance(predecessors.begin(), predecessors.end()); 917 for (auto arg : bb.getArguments()) { 918 auto wrappedType = arg.getType(); 919 if (!isCompatibleType(wrappedType)) 920 return emitError(bb.front().getLoc(), 921 "block argument does not have an LLVM type"); 922 llvm::Type *type = convertType(wrappedType); 923 llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors); 924 valueMapping[arg] = phi; 925 } 926 } 927 928 // Traverse operations. 929 for (auto &op : bb) { 930 // Set the current debug location within the builder. 931 builder.SetCurrentDebugLocation( 932 debugTranslation->translateLoc(op.getLoc(), subprogram)); 933 934 if (failed(convertOperation(op, builder))) 935 return failure(); 936 } 937 938 return success(); 939 } 940 941 /// Create named global variables that correspond to llvm.mlir.global 942 /// definitions. 943 LogicalResult ModuleTranslation::convertGlobals() { 944 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) { 945 llvm::Type *type = convertType(op.getType()); 946 llvm::Constant *cst = llvm::UndefValue::get(type); 947 if (op.getValueOrNull()) { 948 // String attributes are treated separately because they cannot appear as 949 // in-function constants and are thus not supported by getLLVMConstant. 950 if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) { 951 cst = llvm::ConstantDataArray::getString( 952 llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false); 953 type = cst->getType(); 954 } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), 955 op.getLoc()))) { 956 return failure(); 957 } 958 } else if (Block *initializer = op.getInitializerBlock()) { 959 llvm::IRBuilder<> builder(llvmModule->getContext()); 960 for (auto &op : initializer->without_terminator()) { 961 if (failed(convertOperation(op, builder)) || 962 !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0)))) 963 return emitError(op.getLoc(), "unemittable constant value"); 964 } 965 ReturnOp ret = cast<ReturnOp>(initializer->getTerminator()); 966 cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0))); 967 } 968 969 auto linkage = convertLinkageToLLVM(op.linkage()); 970 bool anyExternalLinkage = 971 ((linkage == llvm::GlobalVariable::ExternalLinkage && 972 isa<llvm::UndefValue>(cst)) || 973 linkage == llvm::GlobalVariable::ExternalWeakLinkage); 974 auto addrSpace = op.addr_space(); 975 auto *var = new llvm::GlobalVariable( 976 *llvmModule, type, op.constant(), linkage, 977 anyExternalLinkage ? nullptr : cst, op.sym_name(), 978 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace); 979 980 globalsMapping.try_emplace(op, var); 981 } 982 983 return success(); 984 } 985 986 /// Attempts to add an attribute identified by `key`, optionally with the given 987 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the 988 /// attribute has a kind known to LLVM IR, create the attribute of this kind, 989 /// otherwise keep it as a string attribute. Performs additional checks for 990 /// attributes known to have or not have a value in order to avoid assertions 991 /// inside LLVM upon construction. 992 static LogicalResult checkedAddLLVMFnAttribute(Location loc, 993 llvm::Function *llvmFunc, 994 StringRef key, 995 StringRef value = StringRef()) { 996 auto kind = llvm::Attribute::getAttrKindFromName(key); 997 if (kind == llvm::Attribute::None) { 998 llvmFunc->addFnAttr(key, value); 999 return success(); 1000 } 1001 1002 if (llvm::Attribute::doesAttrKindHaveArgument(kind)) { 1003 if (value.empty()) 1004 return emitError(loc) << "LLVM attribute '" << key << "' expects a value"; 1005 1006 int result; 1007 if (!value.getAsInteger(/*Radix=*/0, result)) 1008 llvmFunc->addFnAttr( 1009 llvm::Attribute::get(llvmFunc->getContext(), kind, result)); 1010 else 1011 llvmFunc->addFnAttr(key, value); 1012 return success(); 1013 } 1014 1015 if (!value.empty()) 1016 return emitError(loc) << "LLVM attribute '" << key 1017 << "' does not expect a value, found '" << value 1018 << "'"; 1019 1020 llvmFunc->addFnAttr(kind); 1021 return success(); 1022 } 1023 1024 /// Attaches the attributes listed in the given array attribute to `llvmFunc`. 1025 /// Reports error to `loc` if any and returns immediately. Expects `attributes` 1026 /// to be an array attribute containing either string attributes, treated as 1027 /// value-less LLVM attributes, or array attributes containing two string 1028 /// attributes, with the first string being the name of the corresponding LLVM 1029 /// attribute and the second string beings its value. Note that even integer 1030 /// attributes are expected to have their values expressed as strings. 1031 static LogicalResult 1032 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes, 1033 llvm::Function *llvmFunc) { 1034 if (!attributes) 1035 return success(); 1036 1037 for (Attribute attr : *attributes) { 1038 if (auto stringAttr = attr.dyn_cast<StringAttr>()) { 1039 if (failed( 1040 checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue()))) 1041 return failure(); 1042 continue; 1043 } 1044 1045 auto arrayAttr = attr.dyn_cast<ArrayAttr>(); 1046 if (!arrayAttr || arrayAttr.size() != 2) 1047 return emitError(loc) 1048 << "expected 'passthrough' to contain string or array attributes"; 1049 1050 auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>(); 1051 auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>(); 1052 if (!keyAttr || !valueAttr) 1053 return emitError(loc) 1054 << "expected arrays within 'passthrough' to contain two strings"; 1055 1056 if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(), 1057 valueAttr.getValue()))) 1058 return failure(); 1059 } 1060 return success(); 1061 } 1062 1063 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) { 1064 // Clear the block, branch value mappings, they are only relevant within one 1065 // function. 1066 blockMapping.clear(); 1067 valueMapping.clear(); 1068 branchMapping.clear(); 1069 llvm::Function *llvmFunc = functionMapping.lookup(func.getName()); 1070 1071 // Translate the debug information for this function. 1072 debugTranslation->translate(func, *llvmFunc); 1073 1074 // Add function arguments to the value remapping table. 1075 // If there was noalias info then we decorate each argument accordingly. 1076 unsigned int argIdx = 0; 1077 for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) { 1078 llvm::Argument &llvmArg = std::get<1>(kvp); 1079 BlockArgument mlirArg = std::get<0>(kvp); 1080 1081 if (auto attr = func.getArgAttrOfType<BoolAttr>( 1082 argIdx, LLVMDialect::getNoAliasAttrName())) { 1083 // NB: Attribute already verified to be boolean, so check if we can indeed 1084 // attach the attribute to this argument, based on its type. 1085 auto argTy = mlirArg.getType(); 1086 if (!argTy.isa<LLVM::LLVMPointerType>()) 1087 return func.emitError( 1088 "llvm.noalias attribute attached to LLVM non-pointer argument"); 1089 if (attr.getValue()) 1090 llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias); 1091 } 1092 1093 if (auto attr = func.getArgAttrOfType<IntegerAttr>( 1094 argIdx, LLVMDialect::getAlignAttrName())) { 1095 // NB: Attribute already verified to be int, so check if we can indeed 1096 // attach the attribute to this argument, based on its type. 1097 auto argTy = mlirArg.getType(); 1098 if (!argTy.isa<LLVM::LLVMPointerType>()) 1099 return func.emitError( 1100 "llvm.align attribute attached to LLVM non-pointer argument"); 1101 llvmArg.addAttrs( 1102 llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt()))); 1103 } 1104 1105 if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.sret")) { 1106 auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>(); 1107 if (!argTy.isa<LLVM::LLVMPointerType>()) 1108 return func.emitError( 1109 "llvm.sret attribute attached to LLVM non-pointer argument"); 1110 llvmArg.addAttr(llvm::Attribute::AttrKind::StructRet); 1111 } 1112 1113 if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.byval")) { 1114 auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>(); 1115 if (!argTy.isa<LLVM::LLVMPointerType>()) 1116 return func.emitError( 1117 "llvm.byval attribute attached to LLVM non-pointer argument"); 1118 llvmArg.addAttr(llvm::Attribute::AttrKind::ByVal); 1119 } 1120 1121 valueMapping[mlirArg] = &llvmArg; 1122 argIdx++; 1123 } 1124 1125 // Check the personality and set it. 1126 if (func.personality().hasValue()) { 1127 llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext()); 1128 if (llvm::Constant *pfunc = 1129 getLLVMConstant(ty, func.personalityAttr(), func.getLoc())) 1130 llvmFunc->setPersonalityFn(pfunc); 1131 } 1132 1133 // First, create all blocks so we can jump to them. 1134 llvm::LLVMContext &llvmContext = llvmFunc->getContext(); 1135 for (auto &bb : func) { 1136 auto *llvmBB = llvm::BasicBlock::Create(llvmContext); 1137 llvmBB->insertInto(llvmFunc); 1138 blockMapping[&bb] = llvmBB; 1139 } 1140 1141 // Then, convert blocks one by one in topological order to ensure defs are 1142 // converted before uses. 1143 auto blocks = topologicalSort(func); 1144 for (Block *bb : blocks) { 1145 llvm::IRBuilder<> builder(llvmContext); 1146 if (failed(convertBlock(*bb, bb->isEntryBlock(), builder))) 1147 return failure(); 1148 } 1149 1150 // Finally, after all blocks have been traversed and values mapped, connect 1151 // the PHI nodes to the results of preceding blocks. 1152 connectPHINodes(func, valueMapping, blockMapping, branchMapping); 1153 return success(); 1154 } 1155 1156 LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) { 1157 for (Operation &o : getModuleBody(m).getOperations()) 1158 if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp>(&o) && !o.isKnownTerminator()) 1159 return o.emitOpError("unsupported module-level operation"); 1160 return success(); 1161 } 1162 1163 LogicalResult ModuleTranslation::convertFunctionSignatures() { 1164 // Declare all functions first because there may be function calls that form a 1165 // call graph with cycles, or global initializers that reference functions. 1166 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 1167 llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction( 1168 function.getName(), 1169 cast<llvm::FunctionType>(convertType(function.getType()))); 1170 llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee()); 1171 llvmFunc->setLinkage(convertLinkageToLLVM(function.linkage())); 1172 functionMapping[function.getName()] = llvmFunc; 1173 1174 // Forward the pass-through attributes to LLVM. 1175 if (failed(forwardPassthroughAttributes(function.getLoc(), 1176 function.passthrough(), llvmFunc))) 1177 return failure(); 1178 } 1179 1180 return success(); 1181 } 1182 1183 LogicalResult ModuleTranslation::convertFunctions() { 1184 // Convert functions. 1185 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) { 1186 // Ignore external functions. 1187 if (function.isExternal()) 1188 continue; 1189 1190 if (failed(convertOneFunction(function))) 1191 return failure(); 1192 } 1193 1194 return success(); 1195 } 1196 1197 llvm::Type *ModuleTranslation::convertType(Type type) { 1198 return typeTranslator.translateType(type); 1199 } 1200 1201 /// A helper to look up remapped operands in the value remapping table.` 1202 SmallVector<llvm::Value *, 8> 1203 ModuleTranslation::lookupValues(ValueRange values) { 1204 SmallVector<llvm::Value *, 8> remapped; 1205 remapped.reserve(values.size()); 1206 for (Value v : values) { 1207 assert(valueMapping.count(v) && "referencing undefined value"); 1208 remapped.push_back(valueMapping.lookup(v)); 1209 } 1210 return remapped; 1211 } 1212 1213 std::unique_ptr<llvm::Module> ModuleTranslation::prepareLLVMModule( 1214 Operation *m, llvm::LLVMContext &llvmContext, StringRef name) { 1215 m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>(); 1216 auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext); 1217 if (auto dataLayoutAttr = 1218 m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) 1219 llvmModule->setDataLayout(dataLayoutAttr.cast<StringAttr>().getValue()); 1220 if (auto targetTripleAttr = 1221 m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) 1222 llvmModule->setTargetTriple(targetTripleAttr.cast<StringAttr>().getValue()); 1223 1224 // Inject declarations for `malloc` and `free` functions that can be used in 1225 // memref allocation/deallocation coming from standard ops lowering. 1226 llvm::IRBuilder<> builder(llvmContext); 1227 llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(), 1228 builder.getInt64Ty()); 1229 llvmModule->getOrInsertFunction("free", builder.getVoidTy(), 1230 builder.getInt8PtrTy()); 1231 1232 return llvmModule; 1233 } 1234