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