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