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