1 //===- ConvertFromLLVMIR.cpp - MLIR to LLVM IR 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 a translation between LLVM IR and the MLIR LLVM dialect. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 14 #include "mlir/IR/Builders.h" 15 #include "mlir/IR/MLIRContext.h" 16 #include "mlir/IR/Module.h" 17 #include "mlir/IR/StandardTypes.h" 18 #include "mlir/Target/LLVMIR.h" 19 #include "mlir/Translation.h" 20 21 #include "llvm/IR/Attributes.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/IR/Instructions.h" 25 #include "llvm/IR/Type.h" 26 #include "llvm/IRReader/IRReader.h" 27 #include "llvm/Support/Error.h" 28 #include "llvm/Support/SourceMgr.h" 29 30 using namespace mlir; 31 using namespace mlir::LLVM; 32 33 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc" 34 35 // Utility to print an LLVM value as a string for passing to emitError(). 36 // FIXME: Diagnostic should be able to natively handle types that have 37 // operator << (raw_ostream&) defined. 38 static std::string diag(llvm::Value &v) { 39 std::string s; 40 llvm::raw_string_ostream os(s); 41 os << v; 42 return os.str(); 43 } 44 45 // Handles importing globals and functions from an LLVM module. 46 namespace { 47 class Importer { 48 public: 49 Importer(MLIRContext *context, ModuleOp module) 50 : b(context), context(context), module(module), 51 unknownLoc(FileLineColLoc::get("imported-bitcode", 0, 0, context)) { 52 b.setInsertionPointToStart(module.getBody()); 53 dialect = context->getRegisteredDialect<LLVMDialect>(); 54 } 55 56 /// Imports `f` into the current module. 57 LogicalResult processFunction(llvm::Function *f); 58 59 /// Imports GV as a GlobalOp, creating it if it doesn't exist. 60 GlobalOp processGlobal(llvm::GlobalVariable *GV); 61 62 private: 63 /// Imports `bb` into `block`, which must be initially empty. 64 LogicalResult processBasicBlock(llvm::BasicBlock *bb, Block *block); 65 /// Imports `inst` and populates instMap[inst] with the imported Value. 66 LogicalResult processInstruction(llvm::Instruction *inst); 67 /// Creates an LLVMType for `type`. 68 LLVMType processType(llvm::Type *type); 69 /// `value` is an SSA-use. Return the remapped version of `value` or a 70 /// placeholder that will be remapped later if this is an instruction that 71 /// has not yet been visited. 72 Value processValue(llvm::Value *value); 73 /// Create the most accurate Location possible using a llvm::DebugLoc and 74 /// possibly an llvm::Instruction to narrow the Location if debug information 75 /// is unavailable. 76 Location processDebugLoc(const llvm::DebugLoc &loc, 77 llvm::Instruction *inst = nullptr); 78 /// `br` branches to `target`. Append the block arguments to attach to the 79 /// generated branch op to `blockArguments`. These should be in the same order 80 /// as the PHIs in `target`. 81 LogicalResult processBranchArgs(llvm::Instruction *br, 82 llvm::BasicBlock *target, 83 SmallVectorImpl<Value> &blockArguments); 84 /// Returns the standard type equivalent to be used in attributes for the 85 /// given LLVM IR dialect type. 86 Type getStdTypeForAttr(LLVMType type); 87 /// Return `value` as an attribute to attach to a GlobalOp. 88 Attribute getConstantAsAttr(llvm::Constant *value); 89 /// Return `c` as an MLIR Value. This could either be a ConstantOp, or 90 /// an expanded sequence of ops in the current function's entry block (for 91 /// ConstantExprs or ConstantGEPs). 92 Value processConstant(llvm::Constant *c); 93 94 /// The current builder, pointing at where the next Instruction should be 95 /// generated. 96 OpBuilder b; 97 /// The current context. 98 MLIRContext *context; 99 /// The current module being created. 100 ModuleOp module; 101 /// The entry block of the current function being processed. 102 Block *currentEntryBlock; 103 104 /// Globals are inserted before the first function, if any. 105 Block::iterator getGlobalInsertPt() { 106 auto i = module.getBody()->begin(); 107 while (!isa<LLVMFuncOp>(i) && !isa<ModuleTerminatorOp>(i)) 108 ++i; 109 return i; 110 } 111 112 /// Functions are always inserted before the module terminator. 113 Block::iterator getFuncInsertPt() { 114 return std::prev(module.getBody()->end()); 115 } 116 117 /// Remapped blocks, for the current function. 118 DenseMap<llvm::BasicBlock *, Block *> blocks; 119 /// Remapped values. These are function-local. 120 DenseMap<llvm::Value *, Value> instMap; 121 /// Instructions that had not been defined when first encountered as a use. 122 /// Maps to the dummy Operation that was created in processValue(). 123 DenseMap<llvm::Value *, Operation *> unknownInstMap; 124 /// Uniquing map of GlobalVariables. 125 DenseMap<llvm::GlobalVariable *, GlobalOp> globals; 126 /// Cached FileLineColLoc::get("imported-bitcode", 0, 0). 127 Location unknownLoc; 128 /// Cached dialect. 129 LLVMDialect *dialect; 130 }; 131 } // namespace 132 133 Location Importer::processDebugLoc(const llvm::DebugLoc &loc, 134 llvm::Instruction *inst) { 135 if (!loc && inst) { 136 std::string s; 137 llvm::raw_string_ostream os(s); 138 os << "llvm-imported-inst-%"; 139 inst->printAsOperand(os, /*PrintType=*/false); 140 return FileLineColLoc::get(os.str(), 0, 0, context); 141 } else if (!loc) { 142 return unknownLoc; 143 } 144 // FIXME: Obtain the filename from DILocationInfo. 145 return FileLineColLoc::get("imported-bitcode", loc.getLine(), loc.getCol(), 146 context); 147 } 148 149 LLVMType Importer::processType(llvm::Type *type) { 150 switch (type->getTypeID()) { 151 case llvm::Type::FloatTyID: 152 return LLVMType::getFloatTy(dialect); 153 case llvm::Type::DoubleTyID: 154 return LLVMType::getDoubleTy(dialect); 155 case llvm::Type::IntegerTyID: 156 return LLVMType::getIntNTy(dialect, type->getIntegerBitWidth()); 157 case llvm::Type::PointerTyID: { 158 LLVMType elementType = processType(type->getPointerElementType()); 159 if (!elementType) 160 return nullptr; 161 return elementType.getPointerTo(type->getPointerAddressSpace()); 162 } 163 case llvm::Type::ArrayTyID: { 164 LLVMType elementType = processType(type->getArrayElementType()); 165 if (!elementType) 166 return nullptr; 167 return LLVMType::getArrayTy(elementType, type->getArrayNumElements()); 168 } 169 case llvm::Type::VectorTyID: { 170 if (type->getVectorIsScalable()) { 171 emitError(unknownLoc) << "scalable vector types not supported"; 172 return nullptr; 173 } 174 LLVMType elementType = processType(type->getVectorElementType()); 175 if (!elementType) 176 return nullptr; 177 return LLVMType::getVectorTy(elementType, type->getVectorNumElements()); 178 } 179 case llvm::Type::VoidTyID: 180 return LLVMType::getVoidTy(dialect); 181 case llvm::Type::FP128TyID: 182 return LLVMType::getFP128Ty(dialect); 183 case llvm::Type::X86_FP80TyID: 184 return LLVMType::getX86_FP80Ty(dialect); 185 case llvm::Type::StructTyID: { 186 SmallVector<LLVMType, 4> elementTypes; 187 elementTypes.reserve(type->getStructNumElements()); 188 for (unsigned i = 0, e = type->getStructNumElements(); i != e; ++i) { 189 LLVMType ty = processType(type->getStructElementType(i)); 190 if (!ty) 191 return nullptr; 192 elementTypes.push_back(ty); 193 } 194 return LLVMType::getStructTy(dialect, elementTypes, 195 cast<llvm::StructType>(type)->isPacked()); 196 } 197 case llvm::Type::FunctionTyID: { 198 llvm::FunctionType *fty = cast<llvm::FunctionType>(type); 199 SmallVector<LLVMType, 4> paramTypes; 200 for (unsigned i = 0, e = fty->getNumParams(); i != e; ++i) { 201 LLVMType ty = processType(fty->getParamType(i)); 202 if (!ty) 203 return nullptr; 204 paramTypes.push_back(ty); 205 } 206 LLVMType result = processType(fty->getReturnType()); 207 if (!result) 208 return nullptr; 209 210 return LLVMType::getFunctionTy(result, paramTypes, fty->isVarArg()); 211 } 212 default: { 213 // FIXME: Diagnostic should be able to natively handle types that have 214 // operator<<(raw_ostream&) defined. 215 std::string s; 216 llvm::raw_string_ostream os(s); 217 os << *type; 218 emitError(unknownLoc) << "unhandled type: " << os.str(); 219 return nullptr; 220 } 221 } 222 } 223 224 // We only need integers, floats, doubles, and vectors and tensors thereof for 225 // attributes. Scalar and vector types are converted to the standard 226 // equivalents. Array types are converted to ranked tensors; nested array types 227 // are converted to multi-dimensional tensors or vectors, depending on the 228 // innermost type being a scalar or a vector. 229 Type Importer::getStdTypeForAttr(LLVMType type) { 230 if (!type) 231 return nullptr; 232 233 if (type.isIntegerTy()) 234 return b.getIntegerType(type.getUnderlyingType()->getIntegerBitWidth()); 235 236 if (type.getUnderlyingType()->isFloatTy()) 237 return b.getF32Type(); 238 239 if (type.getUnderlyingType()->isDoubleTy()) 240 return b.getF64Type(); 241 242 // LLVM vectors can only contain scalars. 243 if (type.isVectorTy()) { 244 auto numElements = type.getUnderlyingType()->getVectorElementCount(); 245 if (numElements.Scalable) { 246 emitError(unknownLoc) << "scalable vectors not supported"; 247 return nullptr; 248 } 249 Type elementType = getStdTypeForAttr(type.getVectorElementType()); 250 if (!elementType) 251 return nullptr; 252 return VectorType::get(numElements.Min, elementType); 253 } 254 255 // LLVM arrays can contain other arrays or vectors. 256 if (type.isArrayTy()) { 257 // Recover the nested array shape. 258 SmallVector<int64_t, 4> shape; 259 shape.push_back(type.getArrayNumElements()); 260 while (type.getArrayElementType().isArrayTy()) { 261 type = type.getArrayElementType(); 262 shape.push_back(type.getArrayNumElements()); 263 } 264 265 // If the innermost type is a vector, use the multi-dimensional vector as 266 // attribute type. 267 if (type.getArrayElementType().isVectorTy()) { 268 LLVMType vectorType = type.getArrayElementType(); 269 auto numElements = 270 vectorType.getUnderlyingType()->getVectorElementCount(); 271 if (numElements.Scalable) { 272 emitError(unknownLoc) << "scalable vectors not supported"; 273 return nullptr; 274 } 275 shape.push_back(numElements.Min); 276 277 Type elementType = getStdTypeForAttr(vectorType.getVectorElementType()); 278 if (!elementType) 279 return nullptr; 280 return VectorType::get(shape, elementType); 281 } 282 283 // Otherwise use a tensor. 284 Type elementType = getStdTypeForAttr(type.getArrayElementType()); 285 if (!elementType) 286 return nullptr; 287 return RankedTensorType::get(shape, elementType); 288 } 289 290 return nullptr; 291 } 292 293 // Get the given constant as an attribute. Not all constants can be represented 294 // as attributes. 295 Attribute Importer::getConstantAsAttr(llvm::Constant *value) { 296 if (auto *ci = dyn_cast<llvm::ConstantInt>(value)) 297 return b.getIntegerAttr( 298 IntegerType::get(ci->getType()->getBitWidth(), context), 299 ci->getValue()); 300 if (auto *c = dyn_cast<llvm::ConstantDataArray>(value)) 301 if (c->isString()) 302 return b.getStringAttr(c->getAsString()); 303 if (auto *c = dyn_cast<llvm::ConstantFP>(value)) { 304 if (c->getType()->isDoubleTy()) 305 return b.getFloatAttr(FloatType::getF64(context), c->getValueAPF()); 306 else if (c->getType()->isFloatingPointTy()) 307 return b.getFloatAttr(FloatType::getF32(context), c->getValueAPF()); 308 } 309 if (auto *f = dyn_cast<llvm::Function>(value)) 310 return b.getSymbolRefAttr(f->getName()); 311 312 // Convert constant data to a dense elements attribute. 313 if (auto *cd = dyn_cast<llvm::ConstantDataSequential>(value)) { 314 LLVMType type = processType(cd->getElementType()); 315 if (!type) 316 return nullptr; 317 318 auto attrType = getStdTypeForAttr(processType(cd->getType())) 319 .dyn_cast_or_null<ShapedType>(); 320 if (!attrType) 321 return nullptr; 322 323 if (type.isIntegerTy()) { 324 SmallVector<APInt, 8> values; 325 values.reserve(cd->getNumElements()); 326 for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i) 327 values.push_back(cd->getElementAsAPInt(i)); 328 return DenseElementsAttr::get(attrType, values); 329 } 330 331 if (type.isFloatTy() || type.isDoubleTy()) { 332 SmallVector<APFloat, 8> values; 333 values.reserve(cd->getNumElements()); 334 for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i) 335 values.push_back(cd->getElementAsAPFloat(i)); 336 return DenseElementsAttr::get(attrType, values); 337 } 338 339 return nullptr; 340 } 341 342 // Unpack constant aggregates to create dense elements attribute whenever 343 // possible. Return nullptr (failure) otherwise. 344 if (isa<llvm::ConstantAggregate>(value)) { 345 auto outerType = getStdTypeForAttr(processType(value->getType())) 346 .dyn_cast_or_null<ShapedType>(); 347 if (!outerType) 348 return nullptr; 349 350 SmallVector<Attribute, 8> values; 351 SmallVector<int64_t, 8> shape; 352 353 for (unsigned i = 0, e = value->getNumOperands(); i < e; ++i) { 354 auto nested = getConstantAsAttr(value->getAggregateElement(i)) 355 .dyn_cast_or_null<DenseElementsAttr>(); 356 if (!nested) 357 return nullptr; 358 359 values.append(nested.attr_value_begin(), nested.attr_value_end()); 360 } 361 362 return DenseElementsAttr::get(outerType, values); 363 } 364 365 return nullptr; 366 } 367 368 GlobalOp Importer::processGlobal(llvm::GlobalVariable *GV) { 369 auto it = globals.find(GV); 370 if (it != globals.end()) 371 return it->second; 372 373 OpBuilder b(module.getBody(), getGlobalInsertPt()); 374 Attribute valueAttr; 375 if (GV->hasInitializer()) 376 valueAttr = getConstantAsAttr(GV->getInitializer()); 377 LLVMType type = processType(GV->getValueType()); 378 if (!type) 379 return nullptr; 380 GlobalOp op = b.create<GlobalOp>( 381 UnknownLoc::get(context), type, GV->isConstant(), 382 convertLinkageFromLLVM(GV->getLinkage()), GV->getName(), valueAttr); 383 if (GV->hasInitializer() && !valueAttr) { 384 Region &r = op.getInitializerRegion(); 385 currentEntryBlock = b.createBlock(&r); 386 b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin()); 387 Value v = processConstant(GV->getInitializer()); 388 if (!v) 389 return nullptr; 390 b.create<ReturnOp>(op.getLoc(), ArrayRef<Value>({v})); 391 } 392 return globals[GV] = op; 393 } 394 395 Value Importer::processConstant(llvm::Constant *c) { 396 OpBuilder bEntry(currentEntryBlock, currentEntryBlock->begin()); 397 if (Attribute attr = getConstantAsAttr(c)) { 398 // These constants can be represented as attributes. 399 OpBuilder b(currentEntryBlock, currentEntryBlock->begin()); 400 LLVMType type = processType(c->getType()); 401 if (!type) 402 return nullptr; 403 return instMap[c] = bEntry.create<ConstantOp>(unknownLoc, type, attr); 404 } 405 if (auto *cn = dyn_cast<llvm::ConstantPointerNull>(c)) { 406 LLVMType type = processType(cn->getType()); 407 if (!type) 408 return nullptr; 409 return instMap[c] = bEntry.create<NullOp>(unknownLoc, type); 410 } 411 if (auto *GV = dyn_cast<llvm::GlobalVariable>(c)) 412 return bEntry.create<AddressOfOp>(UnknownLoc::get(context), 413 processGlobal(GV), 414 ArrayRef<NamedAttribute>()); 415 416 if (auto *ce = dyn_cast<llvm::ConstantExpr>(c)) { 417 llvm::Instruction *i = ce->getAsInstruction(); 418 OpBuilder::InsertionGuard guard(b); 419 b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin()); 420 if (failed(processInstruction(i))) 421 return nullptr; 422 assert(instMap.count(i)); 423 424 // Remove this zombie LLVM instruction now, leaving us only with the MLIR 425 // op. 426 i->deleteValue(); 427 return instMap[c] = instMap[i]; 428 } 429 emitError(unknownLoc) << "unhandled constant: " << diag(*c); 430 return nullptr; 431 } 432 433 Value Importer::processValue(llvm::Value *value) { 434 auto it = instMap.find(value); 435 if (it != instMap.end()) 436 return it->second; 437 438 // We don't expect to see instructions in dominator order. If we haven't seen 439 // this instruction yet, create an unknown op and remap it later. 440 if (isa<llvm::Instruction>(value)) { 441 OperationState state(UnknownLoc::get(context), "unknown"); 442 LLVMType type = processType(value->getType()); 443 if (!type) 444 return nullptr; 445 state.addTypes(type); 446 unknownInstMap[value] = b.createOperation(state); 447 return unknownInstMap[value]->getResult(0); 448 } 449 450 if (auto *c = dyn_cast<llvm::Constant>(value)) 451 return processConstant(c); 452 453 emitError(unknownLoc) << "unhandled value: " << diag(*value); 454 return nullptr; 455 } 456 457 // Maps from LLVM opcode to MLIR OperationName. This is deliberately ordered 458 // as in llvm/IR/Instructions.def to aid comprehension and spot missing 459 // instructions. 460 #define INST(llvm_n, mlir_n) \ 461 { llvm::Instruction::llvm_n, LLVM::mlir_n##Op::getOperationName() } 462 static const DenseMap<unsigned, StringRef> opcMap = { 463 // Ret is handled specially. 464 // Br is handled specially. 465 // FIXME: switch 466 // FIXME: indirectbr 467 // FIXME: invoke 468 // FIXME: resume 469 // FIXME: unreachable 470 // FIXME: cleanupret 471 // FIXME: catchret 472 // FIXME: catchswitch 473 // FIXME: callbr 474 // FIXME: fneg 475 INST(Add, Add), INST(FAdd, FAdd), INST(Sub, Sub), INST(FSub, FSub), 476 INST(Mul, Mul), INST(FMul, FMul), INST(UDiv, UDiv), INST(SDiv, SDiv), 477 INST(FDiv, FDiv), INST(URem, URem), INST(SRem, SRem), INST(FRem, FRem), 478 INST(Shl, Shl), INST(LShr, LShr), INST(AShr, AShr), INST(And, And), 479 INST(Or, Or), INST(Xor, XOr), INST(Alloca, Alloca), INST(Load, Load), 480 INST(Store, Store), 481 // Getelementptr is handled specially. 482 INST(Ret, Return), 483 // FIXME: fence 484 // FIXME: atomiccmpxchg 485 // FIXME: atomicrmw 486 INST(Trunc, Trunc), INST(ZExt, ZExt), INST(SExt, SExt), 487 INST(FPToUI, FPToUI), INST(FPToSI, FPToSI), INST(UIToFP, UIToFP), 488 INST(SIToFP, SIToFP), INST(FPTrunc, FPTrunc), INST(FPExt, FPExt), 489 INST(PtrToInt, PtrToInt), INST(IntToPtr, IntToPtr), INST(BitCast, Bitcast), 490 INST(AddrSpaceCast, AddrSpaceCast), 491 // FIXME: cleanuppad 492 // FIXME: catchpad 493 // ICmp is handled specially. 494 // FIXME: fcmp 495 // PHI is handled specially. 496 INST(Call, Call), 497 // FIXME: select 498 // FIXME: vaarg 499 // FIXME: extractelement 500 // FIXME: insertelement 501 // FIXME: shufflevector 502 // FIXME: extractvalue 503 // FIXME: insertvalue 504 // FIXME: landingpad 505 }; 506 #undef INST 507 508 static ICmpPredicate getICmpPredicate(llvm::CmpInst::Predicate p) { 509 switch (p) { 510 default: 511 llvm_unreachable("incorrect comparison predicate"); 512 case llvm::CmpInst::Predicate::ICMP_EQ: 513 return LLVM::ICmpPredicate::eq; 514 case llvm::CmpInst::Predicate::ICMP_NE: 515 return LLVM::ICmpPredicate::ne; 516 case llvm::CmpInst::Predicate::ICMP_SLT: 517 return LLVM::ICmpPredicate::slt; 518 case llvm::CmpInst::Predicate::ICMP_SLE: 519 return LLVM::ICmpPredicate::sle; 520 case llvm::CmpInst::Predicate::ICMP_SGT: 521 return LLVM::ICmpPredicate::sgt; 522 case llvm::CmpInst::Predicate::ICMP_SGE: 523 return LLVM::ICmpPredicate::sge; 524 case llvm::CmpInst::Predicate::ICMP_ULT: 525 return LLVM::ICmpPredicate::ult; 526 case llvm::CmpInst::Predicate::ICMP_ULE: 527 return LLVM::ICmpPredicate::ule; 528 case llvm::CmpInst::Predicate::ICMP_UGT: 529 return LLVM::ICmpPredicate::ugt; 530 case llvm::CmpInst::Predicate::ICMP_UGE: 531 return LLVM::ICmpPredicate::uge; 532 } 533 llvm_unreachable("incorrect comparison predicate"); 534 } 535 536 // `br` branches to `target`. Return the branch arguments to `br`, in the 537 // same order of the PHIs in `target`. 538 LogicalResult 539 Importer::processBranchArgs(llvm::Instruction *br, llvm::BasicBlock *target, 540 SmallVectorImpl<Value> &blockArguments) { 541 for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) { 542 auto *PN = cast<llvm::PHINode>(&*inst); 543 Value value = processValue(PN->getIncomingValueForBlock(br->getParent())); 544 if (!value) 545 return failure(); 546 blockArguments.push_back(value); 547 } 548 return success(); 549 } 550 551 LogicalResult Importer::processInstruction(llvm::Instruction *inst) { 552 // FIXME: Support uses of SubtargetData. Currently inbounds GEPs, fast-math 553 // flags and call / operand attributes are not supported. 554 Location loc = processDebugLoc(inst->getDebugLoc(), inst); 555 Value &v = instMap[inst]; 556 assert(!v && "processInstruction must be called only once per instruction!"); 557 switch (inst->getOpcode()) { 558 default: 559 return emitError(loc) << "unknown instruction: " << diag(*inst); 560 case llvm::Instruction::Add: 561 case llvm::Instruction::FAdd: 562 case llvm::Instruction::Sub: 563 case llvm::Instruction::FSub: 564 case llvm::Instruction::Mul: 565 case llvm::Instruction::FMul: 566 case llvm::Instruction::UDiv: 567 case llvm::Instruction::SDiv: 568 case llvm::Instruction::FDiv: 569 case llvm::Instruction::URem: 570 case llvm::Instruction::SRem: 571 case llvm::Instruction::FRem: 572 case llvm::Instruction::Shl: 573 case llvm::Instruction::LShr: 574 case llvm::Instruction::AShr: 575 case llvm::Instruction::And: 576 case llvm::Instruction::Or: 577 case llvm::Instruction::Xor: 578 case llvm::Instruction::Alloca: 579 case llvm::Instruction::Load: 580 case llvm::Instruction::Store: 581 case llvm::Instruction::Ret: 582 case llvm::Instruction::Trunc: 583 case llvm::Instruction::ZExt: 584 case llvm::Instruction::SExt: 585 case llvm::Instruction::FPToUI: 586 case llvm::Instruction::FPToSI: 587 case llvm::Instruction::UIToFP: 588 case llvm::Instruction::SIToFP: 589 case llvm::Instruction::FPTrunc: 590 case llvm::Instruction::FPExt: 591 case llvm::Instruction::PtrToInt: 592 case llvm::Instruction::IntToPtr: 593 case llvm::Instruction::AddrSpaceCast: 594 case llvm::Instruction::BitCast: { 595 OperationState state(loc, opcMap.lookup(inst->getOpcode())); 596 SmallVector<Value, 4> ops; 597 ops.reserve(inst->getNumOperands()); 598 for (auto *op : inst->operand_values()) { 599 Value value = processValue(op); 600 if (!value) 601 return failure(); 602 ops.push_back(value); 603 } 604 state.addOperands(ops); 605 if (!inst->getType()->isVoidTy()) { 606 LLVMType type = processType(inst->getType()); 607 if (!type) 608 return failure(); 609 state.addTypes(type); 610 } 611 Operation *op = b.createOperation(state); 612 if (!inst->getType()->isVoidTy()) 613 v = op->getResult(0); 614 return success(); 615 } 616 case llvm::Instruction::ICmp: { 617 Value lhs = processValue(inst->getOperand(0)); 618 Value rhs = processValue(inst->getOperand(1)); 619 if (!lhs || !rhs) 620 return failure(); 621 v = b.create<ICmpOp>( 622 loc, getICmpPredicate(cast<llvm::ICmpInst>(inst)->getPredicate()), lhs, 623 rhs); 624 return success(); 625 } 626 case llvm::Instruction::Br: { 627 auto *brInst = cast<llvm::BranchInst>(inst); 628 OperationState state(loc, 629 brInst->isConditional() ? "llvm.cond_br" : "llvm.br"); 630 SmallVector<Value, 4> ops; 631 if (brInst->isConditional()) { 632 Value condition = processValue(brInst->getCondition()); 633 if (!condition) 634 return failure(); 635 ops.push_back(condition); 636 } 637 state.addOperands(ops); 638 SmallVector<Block *, 4> succs; 639 for (auto *succ : llvm::reverse(brInst->successors())) { 640 SmallVector<Value, 4> blockArguments; 641 if (failed(processBranchArgs(brInst, succ, blockArguments))) 642 return failure(); 643 state.addSuccessor(blocks[succ], blockArguments); 644 } 645 b.createOperation(state); 646 return success(); 647 } 648 case llvm::Instruction::PHI: { 649 LLVMType type = processType(inst->getType()); 650 if (!type) 651 return failure(); 652 v = b.getInsertionBlock()->addArgument(type); 653 return success(); 654 } 655 case llvm::Instruction::Call: { 656 llvm::CallInst *ci = cast<llvm::CallInst>(inst); 657 SmallVector<Value, 4> ops; 658 ops.reserve(inst->getNumOperands()); 659 for (auto &op : ci->arg_operands()) { 660 Value arg = processValue(op.get()); 661 if (!arg) 662 return failure(); 663 ops.push_back(arg); 664 } 665 666 SmallVector<Type, 2> tys; 667 if (!ci->getType()->isVoidTy()) { 668 LLVMType type = processType(inst->getType()); 669 if (!type) 670 return failure(); 671 tys.push_back(type); 672 } 673 Operation *op; 674 if (llvm::Function *callee = ci->getCalledFunction()) { 675 op = b.create<CallOp>(loc, tys, b.getSymbolRefAttr(callee->getName()), 676 ops); 677 } else { 678 Value calledValue = processValue(ci->getCalledValue()); 679 if (!calledValue) 680 return failure(); 681 ops.insert(ops.begin(), calledValue); 682 op = b.create<CallOp>(loc, tys, ops, ArrayRef<NamedAttribute>()); 683 } 684 if (!ci->getType()->isVoidTy()) 685 v = op->getResult(0); 686 return success(); 687 } 688 case llvm::Instruction::LandingPad: { 689 llvm::LandingPadInst *lpi = cast<llvm::LandingPadInst>(inst); 690 SmallVector<Value, 4> ops; 691 692 for (unsigned i = 0, ie = lpi->getNumClauses(); i < ie; i++) 693 ops.push_back(processConstant(lpi->getClause(i))); 694 695 b.create<LandingpadOp>(loc, processType(lpi->getType()), lpi->isCleanup(), 696 ops); 697 return success(); 698 } 699 case llvm::Instruction::Invoke: { 700 llvm::InvokeInst *ii = cast<llvm::InvokeInst>(inst); 701 702 SmallVector<Type, 2> tys; 703 if (!ii->getType()->isVoidTy()) 704 tys.push_back(processType(inst->getType())); 705 706 SmallVector<Value, 4> ops; 707 ops.reserve(inst->getNumOperands() + 1); 708 for (auto &op : ii->arg_operands()) 709 ops.push_back(processValue(op.get())); 710 711 SmallVector<Value, 4> normalArgs, unwindArgs; 712 processBranchArgs(ii, ii->getNormalDest(), normalArgs); 713 processBranchArgs(ii, ii->getUnwindDest(), unwindArgs); 714 715 Operation *op; 716 if (llvm::Function *callee = ii->getCalledFunction()) { 717 op = b.create<InvokeOp>(loc, tys, b.getSymbolRefAttr(callee->getName()), 718 ops, blocks[ii->getNormalDest()], normalArgs, 719 blocks[ii->getUnwindDest()], unwindArgs); 720 } else { 721 ops.insert(ops.begin(), processValue(ii->getCalledValue())); 722 op = b.create<InvokeOp>(loc, tys, ops, blocks[ii->getNormalDest()], 723 normalArgs, blocks[ii->getUnwindDest()], 724 unwindArgs); 725 } 726 727 if (!ii->getType()->isVoidTy()) 728 v = op->getResult(0); 729 return success(); 730 } 731 case llvm::Instruction::GetElementPtr: { 732 // FIXME: Support inbounds GEPs. 733 llvm::GetElementPtrInst *gep = cast<llvm::GetElementPtrInst>(inst); 734 SmallVector<Value, 4> ops; 735 for (auto *op : gep->operand_values()) { 736 Value value = processValue(op); 737 if (!value) 738 return failure(); 739 ops.push_back(value); 740 } 741 Type type = processType(inst->getType()); 742 if (!type) 743 return failure(); 744 v = b.create<GEPOp>(loc, type, ops, ArrayRef<NamedAttribute>()); 745 return success(); 746 } 747 } 748 } 749 750 LogicalResult Importer::processFunction(llvm::Function *f) { 751 blocks.clear(); 752 instMap.clear(); 753 unknownInstMap.clear(); 754 755 LLVMType functionType = processType(f->getFunctionType()); 756 if (!functionType) 757 return failure(); 758 759 b.setInsertionPoint(module.getBody(), getFuncInsertPt()); 760 LLVMFuncOp fop = b.create<LLVMFuncOp>(UnknownLoc::get(context), f->getName(), 761 functionType); 762 if (f->isDeclaration()) 763 return success(); 764 765 // Eagerly create all blocks. 766 SmallVector<Block *, 4> blockList; 767 for (llvm::BasicBlock &bb : *f) { 768 blockList.push_back(b.createBlock(&fop.body(), fop.body().end())); 769 blocks[&bb] = blockList.back(); 770 } 771 currentEntryBlock = blockList[0]; 772 773 // Add function arguments to the entry block. 774 for (auto kv : llvm::enumerate(f->args())) 775 instMap[&kv.value()] = blockList[0]->addArgument( 776 functionType.getFunctionParamType(kv.index())); 777 778 for (auto bbs : llvm::zip(*f, blockList)) { 779 if (failed(processBasicBlock(&std::get<0>(bbs), std::get<1>(bbs)))) 780 return failure(); 781 } 782 783 // Now that all instructions are guaranteed to have been visited, ensure 784 // any unknown uses we encountered are remapped. 785 for (auto &llvmAndUnknown : unknownInstMap) { 786 assert(instMap.count(llvmAndUnknown.first)); 787 Value newValue = instMap[llvmAndUnknown.first]; 788 Value oldValue = llvmAndUnknown.second->getResult(0); 789 oldValue.replaceAllUsesWith(newValue); 790 llvmAndUnknown.second->erase(); 791 } 792 return success(); 793 } 794 795 LogicalResult Importer::processBasicBlock(llvm::BasicBlock *bb, Block *block) { 796 b.setInsertionPointToStart(block); 797 for (llvm::Instruction &inst : *bb) { 798 if (failed(processInstruction(&inst))) 799 return failure(); 800 } 801 return success(); 802 } 803 804 OwningModuleRef 805 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule, 806 MLIRContext *context) { 807 OwningModuleRef module(ModuleOp::create( 808 FileLineColLoc::get("", /*line=*/0, /*column=*/0, context))); 809 810 Importer deserializer(context, module.get()); 811 for (llvm::GlobalVariable &gv : llvmModule->globals()) { 812 if (!deserializer.processGlobal(&gv)) 813 return {}; 814 } 815 for (llvm::Function &f : llvmModule->functions()) { 816 if (failed(deserializer.processFunction(&f))) 817 return {}; 818 } 819 820 return module; 821 } 822 823 // Deserializes the LLVM bitcode stored in `input` into an MLIR module in the 824 // LLVM dialect. 825 OwningModuleRef translateLLVMIRToModule(llvm::SourceMgr &sourceMgr, 826 MLIRContext *context) { 827 LLVMDialect *dialect = context->getRegisteredDialect<LLVMDialect>(); 828 assert(dialect && "Could not find LLVMDialect?"); 829 830 llvm::SMDiagnostic err; 831 std::unique_ptr<llvm::Module> llvmModule = 832 llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), err, 833 dialect->getLLVMContext(), 834 /*UpgradeDebugInfo=*/true, 835 /*DataLayoutString=*/""); 836 if (!llvmModule) { 837 std::string errStr; 838 llvm::raw_string_ostream errStream(errStr); 839 err.print(/*ProgName=*/"", errStream); 840 emitError(UnknownLoc::get(context)) << errStream.str(); 841 return {}; 842 } 843 return translateLLVMIRToModule(std::move(llvmModule), context); 844 } 845 846 static TranslateToMLIRRegistration 847 fromLLVM("import-llvm", 848 [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { 849 return translateLLVMIRToModule(sourceMgr, context); 850 }); 851