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 if (auto *ue = dyn_cast<llvm::UndefValue>(c)) { 430 LLVMType type = processType(ue->getType()); 431 if (!type) 432 return nullptr; 433 return instMap[c] = bEntry.create<UndefOp>(UnknownLoc::get(context), type); 434 } 435 emitError(unknownLoc) << "unhandled constant: " << diag(*c); 436 return nullptr; 437 } 438 439 Value Importer::processValue(llvm::Value *value) { 440 auto it = instMap.find(value); 441 if (it != instMap.end()) 442 return it->second; 443 444 // We don't expect to see instructions in dominator order. If we haven't seen 445 // this instruction yet, create an unknown op and remap it later. 446 if (isa<llvm::Instruction>(value)) { 447 OperationState state(UnknownLoc::get(context), "unknown"); 448 LLVMType type = processType(value->getType()); 449 if (!type) 450 return nullptr; 451 state.addTypes(type); 452 unknownInstMap[value] = b.createOperation(state); 453 return unknownInstMap[value]->getResult(0); 454 } 455 456 if (auto *c = dyn_cast<llvm::Constant>(value)) 457 return processConstant(c); 458 459 emitError(unknownLoc) << "unhandled value: " << diag(*value); 460 return nullptr; 461 } 462 463 // Maps from LLVM opcode to MLIR OperationName. This is deliberately ordered 464 // as in llvm/IR/Instructions.def to aid comprehension and spot missing 465 // instructions. 466 #define INST(llvm_n, mlir_n) \ 467 { llvm::Instruction::llvm_n, LLVM::mlir_n##Op::getOperationName() } 468 static const DenseMap<unsigned, StringRef> opcMap = { 469 // Ret is handled specially. 470 // Br is handled specially. 471 // FIXME: switch 472 // FIXME: indirectbr 473 // FIXME: invoke 474 // FIXME: resume 475 // FIXME: unreachable 476 // FIXME: cleanupret 477 // FIXME: catchret 478 // FIXME: catchswitch 479 // FIXME: callbr 480 // FIXME: fneg 481 INST(Add, Add), INST(FAdd, FAdd), INST(Sub, Sub), INST(FSub, FSub), 482 INST(Mul, Mul), INST(FMul, FMul), INST(UDiv, UDiv), INST(SDiv, SDiv), 483 INST(FDiv, FDiv), INST(URem, URem), INST(SRem, SRem), INST(FRem, FRem), 484 INST(Shl, Shl), INST(LShr, LShr), INST(AShr, AShr), INST(And, And), 485 INST(Or, Or), INST(Xor, XOr), INST(Alloca, Alloca), INST(Load, Load), 486 INST(Store, Store), 487 // Getelementptr is handled specially. 488 INST(Ret, Return), INST(Fence, Fence), 489 // FIXME: atomiccmpxchg 490 // FIXME: atomicrmw 491 INST(Trunc, Trunc), INST(ZExt, ZExt), INST(SExt, SExt), 492 INST(FPToUI, FPToUI), INST(FPToSI, FPToSI), INST(UIToFP, UIToFP), 493 INST(SIToFP, SIToFP), INST(FPTrunc, FPTrunc), INST(FPExt, FPExt), 494 INST(PtrToInt, PtrToInt), INST(IntToPtr, IntToPtr), INST(BitCast, Bitcast), 495 INST(AddrSpaceCast, AddrSpaceCast), 496 // FIXME: cleanuppad 497 // FIXME: catchpad 498 // ICmp is handled specially. 499 // FIXME: fcmp 500 // PHI is handled specially. 501 INST(Freeze, Freeze), INST(Call, Call), 502 // FIXME: select 503 // FIXME: vaarg 504 // FIXME: extractelement 505 // FIXME: insertelement 506 // FIXME: shufflevector 507 // FIXME: extractvalue 508 // FIXME: insertvalue 509 // FIXME: landingpad 510 }; 511 #undef INST 512 513 static ICmpPredicate getICmpPredicate(llvm::CmpInst::Predicate p) { 514 switch (p) { 515 default: 516 llvm_unreachable("incorrect comparison predicate"); 517 case llvm::CmpInst::Predicate::ICMP_EQ: 518 return LLVM::ICmpPredicate::eq; 519 case llvm::CmpInst::Predicate::ICMP_NE: 520 return LLVM::ICmpPredicate::ne; 521 case llvm::CmpInst::Predicate::ICMP_SLT: 522 return LLVM::ICmpPredicate::slt; 523 case llvm::CmpInst::Predicate::ICMP_SLE: 524 return LLVM::ICmpPredicate::sle; 525 case llvm::CmpInst::Predicate::ICMP_SGT: 526 return LLVM::ICmpPredicate::sgt; 527 case llvm::CmpInst::Predicate::ICMP_SGE: 528 return LLVM::ICmpPredicate::sge; 529 case llvm::CmpInst::Predicate::ICMP_ULT: 530 return LLVM::ICmpPredicate::ult; 531 case llvm::CmpInst::Predicate::ICMP_ULE: 532 return LLVM::ICmpPredicate::ule; 533 case llvm::CmpInst::Predicate::ICMP_UGT: 534 return LLVM::ICmpPredicate::ugt; 535 case llvm::CmpInst::Predicate::ICMP_UGE: 536 return LLVM::ICmpPredicate::uge; 537 } 538 llvm_unreachable("incorrect comparison predicate"); 539 } 540 541 static AtomicOrdering getLLVMAtomicOrdering(llvm::AtomicOrdering ordering) { 542 switch (ordering) { 543 case llvm::AtomicOrdering::NotAtomic: 544 return LLVM::AtomicOrdering::not_atomic; 545 case llvm::AtomicOrdering::Unordered: 546 return LLVM::AtomicOrdering::unordered; 547 case llvm::AtomicOrdering::Monotonic: 548 return LLVM::AtomicOrdering::monotonic; 549 case llvm::AtomicOrdering::Acquire: 550 return LLVM::AtomicOrdering::acquire; 551 case llvm::AtomicOrdering::Release: 552 return LLVM::AtomicOrdering::release; 553 case llvm::AtomicOrdering::AcquireRelease: 554 return LLVM::AtomicOrdering::acq_rel; 555 case llvm::AtomicOrdering::SequentiallyConsistent: 556 return LLVM::AtomicOrdering::seq_cst; 557 } 558 llvm_unreachable("incorrect atomic ordering"); 559 } 560 561 // `br` branches to `target`. Return the branch arguments to `br`, in the 562 // same order of the PHIs in `target`. 563 LogicalResult 564 Importer::processBranchArgs(llvm::Instruction *br, llvm::BasicBlock *target, 565 SmallVectorImpl<Value> &blockArguments) { 566 for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) { 567 auto *PN = cast<llvm::PHINode>(&*inst); 568 Value value = processValue(PN->getIncomingValueForBlock(br->getParent())); 569 if (!value) 570 return failure(); 571 blockArguments.push_back(value); 572 } 573 return success(); 574 } 575 576 LogicalResult Importer::processInstruction(llvm::Instruction *inst) { 577 // FIXME: Support uses of SubtargetData. Currently inbounds GEPs, fast-math 578 // flags and call / operand attributes are not supported. 579 Location loc = processDebugLoc(inst->getDebugLoc(), inst); 580 Value &v = instMap[inst]; 581 assert(!v && "processInstruction must be called only once per instruction!"); 582 switch (inst->getOpcode()) { 583 default: 584 return emitError(loc) << "unknown instruction: " << diag(*inst); 585 case llvm::Instruction::Add: 586 case llvm::Instruction::FAdd: 587 case llvm::Instruction::Sub: 588 case llvm::Instruction::FSub: 589 case llvm::Instruction::Mul: 590 case llvm::Instruction::FMul: 591 case llvm::Instruction::UDiv: 592 case llvm::Instruction::SDiv: 593 case llvm::Instruction::FDiv: 594 case llvm::Instruction::URem: 595 case llvm::Instruction::SRem: 596 case llvm::Instruction::FRem: 597 case llvm::Instruction::Shl: 598 case llvm::Instruction::LShr: 599 case llvm::Instruction::AShr: 600 case llvm::Instruction::And: 601 case llvm::Instruction::Or: 602 case llvm::Instruction::Xor: 603 case llvm::Instruction::Alloca: 604 case llvm::Instruction::Load: 605 case llvm::Instruction::Store: 606 case llvm::Instruction::Ret: 607 case llvm::Instruction::Trunc: 608 case llvm::Instruction::ZExt: 609 case llvm::Instruction::SExt: 610 case llvm::Instruction::FPToUI: 611 case llvm::Instruction::FPToSI: 612 case llvm::Instruction::UIToFP: 613 case llvm::Instruction::SIToFP: 614 case llvm::Instruction::FPTrunc: 615 case llvm::Instruction::FPExt: 616 case llvm::Instruction::PtrToInt: 617 case llvm::Instruction::IntToPtr: 618 case llvm::Instruction::AddrSpaceCast: 619 case llvm::Instruction::Freeze: 620 case llvm::Instruction::BitCast: { 621 OperationState state(loc, opcMap.lookup(inst->getOpcode())); 622 SmallVector<Value, 4> ops; 623 ops.reserve(inst->getNumOperands()); 624 for (auto *op : inst->operand_values()) { 625 Value value = processValue(op); 626 if (!value) 627 return failure(); 628 ops.push_back(value); 629 } 630 state.addOperands(ops); 631 if (!inst->getType()->isVoidTy()) { 632 LLVMType type = processType(inst->getType()); 633 if (!type) 634 return failure(); 635 state.addTypes(type); 636 } 637 Operation *op = b.createOperation(state); 638 if (!inst->getType()->isVoidTy()) 639 v = op->getResult(0); 640 return success(); 641 } 642 case llvm::Instruction::ICmp: { 643 Value lhs = processValue(inst->getOperand(0)); 644 Value rhs = processValue(inst->getOperand(1)); 645 if (!lhs || !rhs) 646 return failure(); 647 v = b.create<ICmpOp>( 648 loc, getICmpPredicate(cast<llvm::ICmpInst>(inst)->getPredicate()), lhs, 649 rhs); 650 return success(); 651 } 652 case llvm::Instruction::Br: { 653 auto *brInst = cast<llvm::BranchInst>(inst); 654 OperationState state(loc, 655 brInst->isConditional() ? "llvm.cond_br" : "llvm.br"); 656 if (brInst->isConditional()) { 657 Value condition = processValue(brInst->getCondition()); 658 if (!condition) 659 return failure(); 660 state.addOperands(condition); 661 } 662 663 std::array<int32_t, 3> operandSegmentSizes = {1, 0, 0}; 664 for (int i : llvm::seq<int>(0, brInst->getNumSuccessors())) { 665 auto *succ = brInst->getSuccessor(i); 666 SmallVector<Value, 4> blockArguments; 667 if (failed(processBranchArgs(brInst, succ, blockArguments))) 668 return failure(); 669 state.addSuccessors(blocks[succ]); 670 state.addOperands(blockArguments); 671 operandSegmentSizes[i + 1] = blockArguments.size(); 672 } 673 674 if (brInst->isConditional()) { 675 state.addAttribute(LLVM::CondBrOp::getOperandSegmentSizeAttr(), 676 b.getI32VectorAttr(operandSegmentSizes)); 677 } 678 679 b.createOperation(state); 680 return success(); 681 } 682 case llvm::Instruction::PHI: { 683 LLVMType type = processType(inst->getType()); 684 if (!type) 685 return failure(); 686 v = b.getInsertionBlock()->addArgument(type); 687 return success(); 688 } 689 case llvm::Instruction::Call: { 690 llvm::CallInst *ci = cast<llvm::CallInst>(inst); 691 SmallVector<Value, 4> ops; 692 ops.reserve(inst->getNumOperands()); 693 for (auto &op : ci->arg_operands()) { 694 Value arg = processValue(op.get()); 695 if (!arg) 696 return failure(); 697 ops.push_back(arg); 698 } 699 700 SmallVector<Type, 2> tys; 701 if (!ci->getType()->isVoidTy()) { 702 LLVMType type = processType(inst->getType()); 703 if (!type) 704 return failure(); 705 tys.push_back(type); 706 } 707 Operation *op; 708 if (llvm::Function *callee = ci->getCalledFunction()) { 709 op = b.create<CallOp>(loc, tys, b.getSymbolRefAttr(callee->getName()), 710 ops); 711 } else { 712 Value calledValue = processValue(ci->getCalledValue()); 713 if (!calledValue) 714 return failure(); 715 ops.insert(ops.begin(), calledValue); 716 op = b.create<CallOp>(loc, tys, ops, ArrayRef<NamedAttribute>()); 717 } 718 if (!ci->getType()->isVoidTy()) 719 v = op->getResult(0); 720 return success(); 721 } 722 case llvm::Instruction::LandingPad: { 723 llvm::LandingPadInst *lpi = cast<llvm::LandingPadInst>(inst); 724 SmallVector<Value, 4> ops; 725 726 for (unsigned i = 0, ie = lpi->getNumClauses(); i < ie; i++) 727 ops.push_back(processConstant(lpi->getClause(i))); 728 729 b.create<LandingpadOp>(loc, processType(lpi->getType()), lpi->isCleanup(), 730 ops); 731 return success(); 732 } 733 case llvm::Instruction::Invoke: { 734 llvm::InvokeInst *ii = cast<llvm::InvokeInst>(inst); 735 736 SmallVector<Type, 2> tys; 737 if (!ii->getType()->isVoidTy()) 738 tys.push_back(processType(inst->getType())); 739 740 SmallVector<Value, 4> ops; 741 ops.reserve(inst->getNumOperands() + 1); 742 for (auto &op : ii->arg_operands()) 743 ops.push_back(processValue(op.get())); 744 745 SmallVector<Value, 4> normalArgs, unwindArgs; 746 processBranchArgs(ii, ii->getNormalDest(), normalArgs); 747 processBranchArgs(ii, ii->getUnwindDest(), unwindArgs); 748 749 Operation *op; 750 if (llvm::Function *callee = ii->getCalledFunction()) { 751 op = b.create<InvokeOp>(loc, tys, b.getSymbolRefAttr(callee->getName()), 752 ops, blocks[ii->getNormalDest()], normalArgs, 753 blocks[ii->getUnwindDest()], unwindArgs); 754 } else { 755 ops.insert(ops.begin(), processValue(ii->getCalledValue())); 756 op = b.create<InvokeOp>(loc, tys, ops, blocks[ii->getNormalDest()], 757 normalArgs, blocks[ii->getUnwindDest()], 758 unwindArgs); 759 } 760 761 if (!ii->getType()->isVoidTy()) 762 v = op->getResult(0); 763 return success(); 764 } 765 case llvm::Instruction::Fence: { 766 StringRef syncscope; 767 SmallVector<StringRef, 4> ssNs; 768 llvm::LLVMContext &llvmContext = dialect->getLLVMContext(); 769 llvm::FenceInst *fence = cast<llvm::FenceInst>(inst); 770 llvmContext.getSyncScopeNames(ssNs); 771 int fenceSyncScopeID = fence->getSyncScopeID(); 772 for (unsigned i = 0, e = ssNs.size(); i != e; i++) { 773 if (fenceSyncScopeID == llvmContext.getOrInsertSyncScopeID(ssNs[i])) { 774 syncscope = ssNs[i]; 775 break; 776 } 777 } 778 b.create<FenceOp>(loc, getLLVMAtomicOrdering(fence->getOrdering()), 779 syncscope); 780 return success(); 781 } 782 case llvm::Instruction::GetElementPtr: { 783 // FIXME: Support inbounds GEPs. 784 llvm::GetElementPtrInst *gep = cast<llvm::GetElementPtrInst>(inst); 785 SmallVector<Value, 4> ops; 786 for (auto *op : gep->operand_values()) { 787 Value value = processValue(op); 788 if (!value) 789 return failure(); 790 ops.push_back(value); 791 } 792 Type type = processType(inst->getType()); 793 if (!type) 794 return failure(); 795 v = b.create<GEPOp>(loc, type, ops, ArrayRef<NamedAttribute>()); 796 return success(); 797 } 798 } 799 } 800 801 LogicalResult Importer::processFunction(llvm::Function *f) { 802 blocks.clear(); 803 instMap.clear(); 804 unknownInstMap.clear(); 805 806 LLVMType functionType = processType(f->getFunctionType()); 807 if (!functionType) 808 return failure(); 809 810 b.setInsertionPoint(module.getBody(), getFuncInsertPt()); 811 LLVMFuncOp fop = b.create<LLVMFuncOp>(UnknownLoc::get(context), f->getName(), 812 functionType); 813 if (f->isDeclaration()) 814 return success(); 815 816 // Eagerly create all blocks. 817 SmallVector<Block *, 4> blockList; 818 for (llvm::BasicBlock &bb : *f) { 819 blockList.push_back(b.createBlock(&fop.body(), fop.body().end())); 820 blocks[&bb] = blockList.back(); 821 } 822 currentEntryBlock = blockList[0]; 823 824 // Add function arguments to the entry block. 825 for (auto kv : llvm::enumerate(f->args())) 826 instMap[&kv.value()] = blockList[0]->addArgument( 827 functionType.getFunctionParamType(kv.index())); 828 829 for (auto bbs : llvm::zip(*f, blockList)) { 830 if (failed(processBasicBlock(&std::get<0>(bbs), std::get<1>(bbs)))) 831 return failure(); 832 } 833 834 // Now that all instructions are guaranteed to have been visited, ensure 835 // any unknown uses we encountered are remapped. 836 for (auto &llvmAndUnknown : unknownInstMap) { 837 assert(instMap.count(llvmAndUnknown.first)); 838 Value newValue = instMap[llvmAndUnknown.first]; 839 Value oldValue = llvmAndUnknown.second->getResult(0); 840 oldValue.replaceAllUsesWith(newValue); 841 llvmAndUnknown.second->erase(); 842 } 843 return success(); 844 } 845 846 LogicalResult Importer::processBasicBlock(llvm::BasicBlock *bb, Block *block) { 847 b.setInsertionPointToStart(block); 848 for (llvm::Instruction &inst : *bb) { 849 if (failed(processInstruction(&inst))) 850 return failure(); 851 } 852 return success(); 853 } 854 855 OwningModuleRef 856 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule, 857 MLIRContext *context) { 858 OwningModuleRef module(ModuleOp::create( 859 FileLineColLoc::get("", /*line=*/0, /*column=*/0, context))); 860 861 Importer deserializer(context, module.get()); 862 for (llvm::GlobalVariable &gv : llvmModule->globals()) { 863 if (!deserializer.processGlobal(&gv)) 864 return {}; 865 } 866 for (llvm::Function &f : llvmModule->functions()) { 867 if (failed(deserializer.processFunction(&f))) 868 return {}; 869 } 870 871 return module; 872 } 873 874 // Deserializes the LLVM bitcode stored in `input` into an MLIR module in the 875 // LLVM dialect. 876 OwningModuleRef translateLLVMIRToModule(llvm::SourceMgr &sourceMgr, 877 MLIRContext *context) { 878 LLVMDialect *dialect = context->getRegisteredDialect<LLVMDialect>(); 879 assert(dialect && "Could not find LLVMDialect?"); 880 881 llvm::SMDiagnostic err; 882 std::unique_ptr<llvm::Module> llvmModule = 883 llvm::parseIR(*sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), err, 884 dialect->getLLVMContext(), 885 /*UpgradeDebugInfo=*/true, 886 /*DataLayoutString=*/""); 887 if (!llvmModule) { 888 std::string errStr; 889 llvm::raw_string_ostream errStream(errStr); 890 err.print(/*ProgName=*/"", errStream); 891 emitError(UnknownLoc::get(context)) << errStream.str(); 892 return {}; 893 } 894 return translateLLVMIRToModule(std::move(llvmModule), context); 895 } 896 897 static TranslateToMLIRRegistration 898 fromLLVM("import-llvm", 899 [](llvm::SourceMgr &sourceMgr, MLIRContext *context) { 900 return translateLLVMIRToModule(sourceMgr, context); 901 }); 902