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