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/Tools/mlir-translate/Translation.h"
24 
25 #include "llvm/ADT/StringSet.h"
26 #include "llvm/ADT/TypeSwitch.h"
27 #include "llvm/IR/Attributes.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/InlineAsm.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IRReader/IRReader.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/SourceMgr.h"
37 
38 using namespace mlir;
39 using namespace mlir::LLVM;
40 
41 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsFromLLVM.inc"
42 
43 // Utility to print an LLVM value as a string for passing to emitError().
44 // FIXME: Diagnostic should be able to natively handle types that have
45 // operator << (raw_ostream&) defined.
46 static std::string diag(llvm::Value &v) {
47   std::string s;
48   llvm::raw_string_ostream os(s);
49   os << v;
50   return os.str();
51 }
52 
53 /// Creates an attribute containing ABI and preferred alignment numbers parsed
54 /// a string. The string may be either "abi:preferred" or just "abi". In the
55 /// latter case, the prefrred alignment is considered equal to ABI alignment.
56 static DenseIntElementsAttr parseDataLayoutAlignment(MLIRContext &ctx,
57                                                      StringRef spec) {
58   auto i32 = IntegerType::get(&ctx, 32);
59 
60   StringRef abiString, preferredString;
61   std::tie(abiString, preferredString) = spec.split(':');
62   int abi, preferred;
63   if (abiString.getAsInteger(/*Radix=*/10, abi))
64     return nullptr;
65 
66   if (preferredString.empty())
67     preferred = abi;
68   else if (preferredString.getAsInteger(/*Radix=*/10, preferred))
69     return nullptr;
70 
71   return DenseIntElementsAttr::get(VectorType::get({2}, i32), {abi, preferred});
72 }
73 
74 /// Returns a supported MLIR floating point type of the given bit width or null
75 /// if the bit width is not supported.
76 static FloatType getDLFloatType(MLIRContext &ctx, int32_t bitwidth) {
77   switch (bitwidth) {
78   case 16:
79     return FloatType::getF16(&ctx);
80   case 32:
81     return FloatType::getF32(&ctx);
82   case 64:
83     return FloatType::getF64(&ctx);
84   case 80:
85     return FloatType::getF80(&ctx);
86   case 128:
87     return FloatType::getF128(&ctx);
88   default:
89     return nullptr;
90   }
91 }
92 
93 DataLayoutSpecInterface
94 mlir::translateDataLayout(const llvm::DataLayout &dataLayout,
95                           MLIRContext *context) {
96   assert(context && "expected MLIR context");
97   std::string layoutstr = dataLayout.getStringRepresentation();
98 
99   // Remaining unhandled default layout defaults
100   // e (little endian if not set)
101   // p[n]:64:64:64 (non zero address spaces have 64-bit properties)
102   std::string append =
103       "p:64:64:64-S0-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f16:16:16-f64:"
104       "64:64-f128:128:128-v64:64:64-v128:128:128-a:0:64";
105   if (layoutstr.empty())
106     layoutstr = append;
107   else
108     layoutstr = layoutstr + "-" + append;
109 
110   StringRef layout(layoutstr);
111 
112   SmallVector<DataLayoutEntryInterface> entries;
113   StringSet<> seen;
114   while (!layout.empty()) {
115     // Split at '-'.
116     std::pair<StringRef, StringRef> split = layout.split('-');
117     StringRef current;
118     std::tie(current, layout) = split;
119 
120     // Split at ':'.
121     StringRef kind, spec;
122     std::tie(kind, spec) = current.split(':');
123     if (seen.contains(kind))
124       continue;
125     seen.insert(kind);
126 
127     char symbol = kind.front();
128     StringRef parameter = kind.substr(1);
129 
130     if (symbol == 'i' || symbol == 'f') {
131       unsigned bitwidth;
132       if (parameter.getAsInteger(/*Radix=*/10, bitwidth))
133         return nullptr;
134       DenseIntElementsAttr params = parseDataLayoutAlignment(*context, spec);
135       if (!params)
136         return nullptr;
137       auto entry = DataLayoutEntryAttr::get(
138           symbol == 'i' ? static_cast<Type>(IntegerType::get(context, bitwidth))
139                         : getDLFloatType(*context, bitwidth),
140           params);
141       entries.emplace_back(entry);
142     } else if (symbol == 'e' || symbol == 'E') {
143       auto value = StringAttr::get(
144           context, symbol == 'e' ? DLTIDialect::kDataLayoutEndiannessLittle
145                                  : DLTIDialect::kDataLayoutEndiannessBig);
146       auto entry = DataLayoutEntryAttr::get(
147           StringAttr::get(context, DLTIDialect::kDataLayoutEndiannessKey),
148           value);
149       entries.emplace_back(entry);
150     }
151   }
152 
153   return DataLayoutSpecAttr::get(context, entries);
154 }
155 
156 // Handles importing globals and functions from an LLVM module.
157 namespace {
158 class Importer {
159 public:
160   Importer(MLIRContext *context, ModuleOp module)
161       : b(context), context(context), module(module),
162         unknownLoc(FileLineColLoc::get(context, "imported-bitcode", 0, 0)),
163         typeTranslator(*context) {
164     b.setInsertionPointToStart(module.getBody());
165   }
166 
167   /// Imports `f` into the current module.
168   LogicalResult processFunction(llvm::Function *f);
169 
170   /// Imports GV as a GlobalOp, creating it if it doesn't exist.
171   GlobalOp processGlobal(llvm::GlobalVariable *gv);
172 
173 private:
174   /// Returns personality of `f` as a FlatSymbolRefAttr.
175   FlatSymbolRefAttr getPersonalityAsAttr(llvm::Function *f);
176   /// Imports `bb` into `block`, which must be initially empty.
177   LogicalResult processBasicBlock(llvm::BasicBlock *bb, Block *block);
178   /// Imports `inst` and populates instMap[inst] with the imported Value.
179   LogicalResult processInstruction(llvm::Instruction *inst);
180   /// Creates an LLVM-compatible MLIR type for `type`.
181   Type processType(llvm::Type *type);
182   /// `value` is an SSA-use. Return the remapped version of `value` or a
183   /// placeholder that will be remapped later if this is an instruction that
184   /// has not yet been visited.
185   Value processValue(llvm::Value *value);
186   /// Create the most accurate Location possible using a llvm::DebugLoc and
187   /// possibly an llvm::Instruction to narrow the Location if debug information
188   /// is unavailable.
189   Location processDebugLoc(const llvm::DebugLoc &loc,
190                            llvm::Instruction *inst = nullptr);
191   /// `br` branches to `target`. Append the block arguments to attach to the
192   /// generated branch op to `blockArguments`. These should be in the same order
193   /// as the PHIs in `target`.
194   LogicalResult processBranchArgs(llvm::Instruction *br,
195                                   llvm::BasicBlock *target,
196                                   SmallVectorImpl<Value> &blockArguments);
197   /// Returns the builtin type equivalent to be used in attributes for the given
198   /// LLVM IR dialect type.
199   Type getStdTypeForAttr(Type type);
200   /// Return `value` as an attribute to attach to a GlobalOp.
201   Attribute getConstantAsAttr(llvm::Constant *value);
202   /// Return `c` as an MLIR Value. This could either be a ConstantOp, or
203   /// an expanded sequence of ops in the current function's entry block (for
204   /// ConstantExprs or ConstantGEPs).
205   Value processConstant(llvm::Constant *c);
206 
207   /// The current builder, pointing at where the next Instruction should be
208   /// generated.
209   OpBuilder b;
210   /// The current context.
211   MLIRContext *context;
212   /// The current module being created.
213   ModuleOp module;
214   /// The entry block of the current function being processed.
215   Block *currentEntryBlock = nullptr;
216 
217   /// Globals are inserted before the first function, if any.
218   Block::iterator getGlobalInsertPt() {
219     auto it = module.getBody()->begin();
220     auto endIt = module.getBody()->end();
221     while (it != endIt && !isa<LLVMFuncOp>(it))
222       ++it;
223     return it;
224   }
225 
226   /// Functions are always inserted before the module terminator.
227   Block::iterator getFuncInsertPt() {
228     return std::prev(module.getBody()->end());
229   }
230 
231   /// Remapped blocks, for the current function.
232   DenseMap<llvm::BasicBlock *, Block *> blocks;
233   /// Remapped values. These are function-local.
234   DenseMap<llvm::Value *, Value> instMap;
235   /// Instructions that had not been defined when first encountered as a use.
236   /// Maps to the dummy Operation that was created in processValue().
237   DenseMap<llvm::Value *, Operation *> unknownInstMap;
238   /// Uniquing map of GlobalVariables.
239   DenseMap<llvm::GlobalVariable *, GlobalOp> globals;
240   /// Cached FileLineColLoc::get("imported-bitcode", 0, 0).
241   Location unknownLoc;
242   /// The stateful type translator (contains named structs).
243   LLVM::TypeFromLLVMIRTranslator typeTranslator;
244 };
245 } // namespace
246 
247 Location Importer::processDebugLoc(const llvm::DebugLoc &loc,
248                                    llvm::Instruction *inst) {
249   if (!loc && inst) {
250     std::string s;
251     llvm::raw_string_ostream os(s);
252     os << "llvm-imported-inst-%";
253     inst->printAsOperand(os, /*PrintType=*/false);
254     return FileLineColLoc::get(context, os.str(), 0, 0);
255   }
256   if (!loc) {
257     return unknownLoc;
258   }
259   // FIXME: Obtain the filename from DILocationInfo.
260   return FileLineColLoc::get(context, "imported-bitcode", loc.getLine(),
261                              loc.getCol());
262 }
263 
264 Type Importer::processType(llvm::Type *type) {
265   if (Type result = typeTranslator.translateType(type))
266     return result;
267 
268   // FIXME: Diagnostic should be able to natively handle types that have
269   // operator<<(raw_ostream&) defined.
270   std::string s;
271   llvm::raw_string_ostream os(s);
272   os << *type;
273   emitError(unknownLoc) << "unhandled type: " << os.str();
274   return nullptr;
275 }
276 
277 // We only need integers, floats, doubles, and vectors and tensors thereof for
278 // attributes. Scalar and vector types are converted to the standard
279 // equivalents. Array types are converted to ranked tensors; nested array types
280 // are converted to multi-dimensional tensors or vectors, depending on the
281 // innermost type being a scalar or a vector.
282 Type Importer::getStdTypeForAttr(Type type) {
283   if (!type)
284     return nullptr;
285 
286   if (type.isa<IntegerType, FloatType>())
287     return type;
288 
289   // LLVM vectors can only contain scalars.
290   if (LLVM::isCompatibleVectorType(type)) {
291     auto numElements = LLVM::getVectorNumElements(type);
292     if (numElements.isScalable()) {
293       emitError(unknownLoc) << "scalable vectors not supported";
294       return nullptr;
295     }
296     Type elementType = getStdTypeForAttr(LLVM::getVectorElementType(type));
297     if (!elementType)
298       return nullptr;
299     return VectorType::get(numElements.getKnownMinValue(), elementType);
300   }
301 
302   // LLVM arrays can contain other arrays or vectors.
303   if (auto arrayType = type.dyn_cast<LLVMArrayType>()) {
304     // Recover the nested array shape.
305     SmallVector<int64_t, 4> shape;
306     shape.push_back(arrayType.getNumElements());
307     while (arrayType.getElementType().isa<LLVMArrayType>()) {
308       arrayType = arrayType.getElementType().cast<LLVMArrayType>();
309       shape.push_back(arrayType.getNumElements());
310     }
311 
312     // If the innermost type is a vector, use the multi-dimensional vector as
313     // attribute type.
314     if (LLVM::isCompatibleVectorType(arrayType.getElementType())) {
315       auto numElements = LLVM::getVectorNumElements(arrayType.getElementType());
316       if (numElements.isScalable()) {
317         emitError(unknownLoc) << "scalable vectors not supported";
318         return nullptr;
319       }
320       shape.push_back(numElements.getKnownMinValue());
321 
322       Type elementType = getStdTypeForAttr(
323           LLVM::getVectorElementType(arrayType.getElementType()));
324       if (!elementType)
325         return nullptr;
326       return VectorType::get(shape, elementType);
327     }
328 
329     // Otherwise use a tensor.
330     Type elementType = getStdTypeForAttr(arrayType.getElementType());
331     if (!elementType)
332       return nullptr;
333     return RankedTensorType::get(shape, elementType);
334   }
335 
336   return nullptr;
337 }
338 
339 // Get the given constant as an attribute. Not all constants can be represented
340 // as attributes.
341 Attribute Importer::getConstantAsAttr(llvm::Constant *value) {
342   if (auto *ci = dyn_cast<llvm::ConstantInt>(value))
343     return b.getIntegerAttr(
344         IntegerType::get(context, ci->getType()->getBitWidth()),
345         ci->getValue());
346   if (auto *c = dyn_cast<llvm::ConstantDataArray>(value))
347     if (c->isString())
348       return b.getStringAttr(c->getAsString());
349   if (auto *c = dyn_cast<llvm::ConstantFP>(value)) {
350     auto *type = c->getType();
351     FloatType floatTy;
352     if (type->isBFloatTy())
353       floatTy = FloatType::getBF16(context);
354     else
355       floatTy = getDLFloatType(*context, type->getScalarSizeInBits());
356     assert(floatTy && "unsupported floating point type");
357     return b.getFloatAttr(floatTy, c->getValueAPF());
358   }
359   if (auto *f = dyn_cast<llvm::Function>(value))
360     return SymbolRefAttr::get(b.getContext(), f->getName());
361 
362   // Convert constant data to a dense elements attribute.
363   if (auto *cd = dyn_cast<llvm::ConstantDataSequential>(value)) {
364     Type type = processType(cd->getElementType());
365     if (!type)
366       return nullptr;
367 
368     auto attrType = getStdTypeForAttr(processType(cd->getType()))
369                         .dyn_cast_or_null<ShapedType>();
370     if (!attrType)
371       return nullptr;
372 
373     if (type.isa<IntegerType>()) {
374       SmallVector<APInt, 8> values;
375       values.reserve(cd->getNumElements());
376       for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i)
377         values.push_back(cd->getElementAsAPInt(i));
378       return DenseElementsAttr::get(attrType, values);
379     }
380 
381     if (type.isa<Float32Type, Float64Type>()) {
382       SmallVector<APFloat, 8> values;
383       values.reserve(cd->getNumElements());
384       for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i)
385         values.push_back(cd->getElementAsAPFloat(i));
386       return DenseElementsAttr::get(attrType, values);
387     }
388 
389     return nullptr;
390   }
391 
392   // Unpack constant aggregates to create dense elements attribute whenever
393   // possible. Return nullptr (failure) otherwise.
394   if (isa<llvm::ConstantAggregate>(value)) {
395     auto outerType = getStdTypeForAttr(processType(value->getType()))
396                          .dyn_cast_or_null<ShapedType>();
397     if (!outerType)
398       return nullptr;
399 
400     SmallVector<Attribute, 8> values;
401     SmallVector<int64_t, 8> shape;
402 
403     for (unsigned i = 0, e = value->getNumOperands(); i < e; ++i) {
404       auto nested = getConstantAsAttr(value->getAggregateElement(i))
405                         .dyn_cast_or_null<DenseElementsAttr>();
406       if (!nested)
407         return nullptr;
408 
409       values.append(nested.value_begin<Attribute>(),
410                     nested.value_end<Attribute>());
411     }
412 
413     return DenseElementsAttr::get(outerType, values);
414   }
415 
416   return nullptr;
417 }
418 
419 GlobalOp Importer::processGlobal(llvm::GlobalVariable *gv) {
420   auto it = globals.find(gv);
421   if (it != globals.end())
422     return it->second;
423 
424   OpBuilder b(module.getBody(), getGlobalInsertPt());
425   Attribute valueAttr;
426   if (gv->hasInitializer())
427     valueAttr = getConstantAsAttr(gv->getInitializer());
428   Type type = processType(gv->getValueType());
429   if (!type)
430     return nullptr;
431 
432   uint64_t alignment = 0;
433   llvm::MaybeAlign maybeAlign = gv->getAlign();
434   if (maybeAlign.hasValue()) {
435     llvm::Align align = maybeAlign.getValue();
436     alignment = align.value();
437   }
438 
439   GlobalOp op = b.create<GlobalOp>(
440       UnknownLoc::get(context), type, gv->isConstant(),
441       convertLinkageFromLLVM(gv->getLinkage()), gv->getName(), valueAttr,
442       alignment, /*addr_space=*/gv->getAddressSpace(),
443       /*dso_local=*/gv->isDSOLocal(), /*thread_local=*/gv->isThreadLocal());
444 
445   if (gv->hasInitializer() && !valueAttr) {
446     Region &r = op.getInitializerRegion();
447     currentEntryBlock = b.createBlock(&r);
448     b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin());
449     Value v = processConstant(gv->getInitializer());
450     if (!v)
451       return nullptr;
452     b.create<ReturnOp>(op.getLoc(), ArrayRef<Value>({v}));
453   }
454   if (gv->hasAtLeastLocalUnnamedAddr())
455     op.setUnnamedAddrAttr(UnnamedAddrAttr::get(
456         context, convertUnnamedAddrFromLLVM(gv->getUnnamedAddr())));
457   if (gv->hasSection())
458     op.setSectionAttr(b.getStringAttr(gv->getSection()));
459 
460   return globals[gv] = op;
461 }
462 
463 Value Importer::processConstant(llvm::Constant *c) {
464   OpBuilder bEntry(currentEntryBlock, currentEntryBlock->begin());
465   if (Attribute attr = getConstantAsAttr(c)) {
466     // These constants can be represented as attributes.
467     OpBuilder b(currentEntryBlock, currentEntryBlock->begin());
468     Type type = processType(c->getType());
469     if (!type)
470       return nullptr;
471     if (auto symbolRef = attr.dyn_cast<FlatSymbolRefAttr>())
472       return bEntry.create<AddressOfOp>(unknownLoc, type, symbolRef.getValue());
473     return bEntry.create<ConstantOp>(unknownLoc, type, attr);
474   }
475   if (auto *cn = dyn_cast<llvm::ConstantPointerNull>(c)) {
476     Type type = processType(cn->getType());
477     if (!type)
478       return nullptr;
479     return bEntry.create<NullOp>(unknownLoc, type);
480   }
481   if (auto *gv = dyn_cast<llvm::GlobalVariable>(c))
482     return bEntry.create<AddressOfOp>(UnknownLoc::get(context),
483                                       processGlobal(gv));
484 
485   if (auto *ce = dyn_cast<llvm::ConstantExpr>(c)) {
486     llvm::Instruction *i = ce->getAsInstruction();
487     OpBuilder::InsertionGuard guard(b);
488     b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin());
489     if (failed(processInstruction(i)))
490       return nullptr;
491     assert(instMap.count(i));
492 
493     // If we don't remove entry of `i` here, it's totally possible that the
494     // next time llvm::ConstantExpr::getAsInstruction is called again, which
495     // always allocates a new Instruction, memory address of the newly
496     // created Instruction might be the same as `i`. Making processInstruction
497     // falsely believe that the new Instruction has been processed before
498     // and raised an assertion error.
499     Value value = instMap[i];
500     instMap.erase(i);
501     // Remove this zombie LLVM instruction now, leaving us only with the MLIR
502     // op.
503     i->deleteValue();
504     return value;
505   }
506   if (auto *ue = dyn_cast<llvm::UndefValue>(c)) {
507     Type type = processType(ue->getType());
508     if (!type)
509       return nullptr;
510     return bEntry.create<UndefOp>(UnknownLoc::get(context), type);
511   }
512 
513   if (isa<llvm::ConstantAggregate>(c) || isa<llvm::ConstantAggregateZero>(c)) {
514     unsigned numElements = c->getNumOperands();
515     std::function<llvm::Constant *(unsigned)> getElement =
516         [&](unsigned index) -> llvm::Constant * {
517       return c->getAggregateElement(index);
518     };
519     // llvm::ConstantAggregateZero doesn't take any operand
520     // so its getNumOperands is always zero.
521     if (auto *caz = dyn_cast<llvm::ConstantAggregateZero>(c)) {
522       numElements = caz->getElementCount().getFixedValue();
523       // We want to capture the pointer rather than reference
524       // to the pointer since the latter will become dangling upon
525       // exiting the scope.
526       getElement = [=](unsigned index) -> llvm::Constant * {
527         return caz->getElementValue(index);
528       };
529     }
530 
531     // Generate a llvm.undef as the root value first.
532     Type rootType = processType(c->getType());
533     if (!rootType)
534       return nullptr;
535     Value root = bEntry.create<UndefOp>(unknownLoc, rootType);
536     for (unsigned i = 0; i < numElements; ++i) {
537       llvm::Constant *element = getElement(i);
538       Value elementValue = processConstant(element);
539       if (!elementValue)
540         return nullptr;
541       ArrayAttr indexAttr = bEntry.getI32ArrayAttr({static_cast<int32_t>(i)});
542       root = bEntry.create<InsertValueOp>(UnknownLoc::get(context), rootType,
543                                           root, elementValue, indexAttr);
544     }
545     return root;
546   }
547 
548   emitError(unknownLoc) << "unhandled constant: " << diag(*c);
549   return nullptr;
550 }
551 
552 Value Importer::processValue(llvm::Value *value) {
553   auto it = instMap.find(value);
554   if (it != instMap.end())
555     return it->second;
556 
557   // We don't expect to see instructions in dominator order. If we haven't seen
558   // this instruction yet, create an unknown op and remap it later.
559   if (isa<llvm::Instruction>(value)) {
560     Type type = processType(value->getType());
561     if (!type)
562       return nullptr;
563     unknownInstMap[value] =
564         b.create(UnknownLoc::get(context), b.getStringAttr("llvm.unknown"),
565                  /*operands=*/{}, type);
566     return unknownInstMap[value]->getResult(0);
567   }
568 
569   if (auto *c = dyn_cast<llvm::Constant>(value))
570     return processConstant(c);
571 
572   emitError(unknownLoc) << "unhandled value: " << diag(*value);
573   return nullptr;
574 }
575 
576 /// Return the MLIR OperationName for the given LLVM opcode.
577 static StringRef lookupOperationNameFromOpcode(unsigned opcode) {
578 // Maps from LLVM opcode to MLIR OperationName. This is deliberately ordered
579 // as in llvm/IR/Instructions.def to aid comprehension and spot missing
580 // instructions.
581 #define INST(llvm_n, mlir_n)                                                   \
582   { llvm::Instruction::llvm_n, LLVM::mlir_n##Op::getOperationName() }
583   static const DenseMap<unsigned, StringRef> opcMap = {
584       // clang-format off
585       INST(Ret, Return),
586       // Br is handled specially.
587       // Switch is handled specially.
588       // FIXME: indirectbr
589       // Invoke is handled specially.
590       INST(Resume, Resume),
591       INST(Unreachable, Unreachable),
592       // FIXME: cleanupret
593       // FIXME: catchret
594       // FIXME: catchswitch
595       // FIXME: callbr
596       INST(FNeg, FNeg),
597       INST(Add, Add),
598       INST(FAdd, FAdd),
599       INST(Sub, Sub),
600       INST(FSub, FSub),
601       INST(Mul, Mul),
602       INST(FMul, FMul),
603       INST(UDiv, UDiv),
604       INST(SDiv, SDiv),
605       INST(FDiv, FDiv),
606       INST(URem, URem),
607       INST(SRem, SRem),
608       INST(FRem, FRem),
609       INST(Shl, Shl),
610       INST(LShr, LShr),
611       INST(AShr, AShr),
612       INST(And, And),
613       INST(Or, Or),
614       INST(Xor, XOr),
615       INST(ExtractElement, ExtractElement),
616       INST(InsertElement, InsertElement),
617       // ShuffleVector is handled specially.
618       // ExtractValue is handled specially.
619       // InsertValue is handled specially.
620       INST(Alloca, Alloca),
621       INST(Load, Load),
622       INST(Store, Store),
623       INST(Fence, Fence),
624       // FIXME: atomiccmpxchg
625       // FIXME: atomicrmw
626       // Getelementptr is handled specially.
627       INST(Trunc, Trunc),
628       INST(ZExt, ZExt),
629       INST(SExt, SExt),
630       INST(FPToUI, FPToUI),
631       INST(FPToSI, FPToSI),
632       INST(UIToFP, UIToFP),
633       INST(SIToFP, SIToFP),
634       INST(FPTrunc, FPTrunc),
635       INST(FPExt, FPExt),
636       INST(PtrToInt, PtrToInt),
637       INST(IntToPtr, IntToPtr),
638       INST(BitCast, Bitcast),
639       INST(AddrSpaceCast, AddrSpaceCast),
640       // ICmp is handled specially.
641       // FCmp is handled specially.
642       // PHI is handled specially.
643       INST(Select, Select),
644       INST(Freeze, Freeze),
645       INST(Call, Call),
646       // FIXME: vaarg
647       // FIXME: landingpad
648       // FIXME: catchpad
649       // FIXME: cleanuppad
650       // clang-format on
651   };
652 #undef INST
653 
654   return opcMap.lookup(opcode);
655 }
656 
657 static ICmpPredicate getICmpPredicate(llvm::CmpInst::Predicate p) {
658   switch (p) {
659   default:
660     llvm_unreachable("incorrect comparison predicate");
661   case llvm::CmpInst::Predicate::ICMP_EQ:
662     return LLVM::ICmpPredicate::eq;
663   case llvm::CmpInst::Predicate::ICMP_NE:
664     return LLVM::ICmpPredicate::ne;
665   case llvm::CmpInst::Predicate::ICMP_SLT:
666     return LLVM::ICmpPredicate::slt;
667   case llvm::CmpInst::Predicate::ICMP_SLE:
668     return LLVM::ICmpPredicate::sle;
669   case llvm::CmpInst::Predicate::ICMP_SGT:
670     return LLVM::ICmpPredicate::sgt;
671   case llvm::CmpInst::Predicate::ICMP_SGE:
672     return LLVM::ICmpPredicate::sge;
673   case llvm::CmpInst::Predicate::ICMP_ULT:
674     return LLVM::ICmpPredicate::ult;
675   case llvm::CmpInst::Predicate::ICMP_ULE:
676     return LLVM::ICmpPredicate::ule;
677   case llvm::CmpInst::Predicate::ICMP_UGT:
678     return LLVM::ICmpPredicate::ugt;
679   case llvm::CmpInst::Predicate::ICMP_UGE:
680     return LLVM::ICmpPredicate::uge;
681   }
682   llvm_unreachable("incorrect integer comparison predicate");
683 }
684 
685 static FCmpPredicate getFCmpPredicate(llvm::CmpInst::Predicate p) {
686   switch (p) {
687   default:
688     llvm_unreachable("incorrect comparison predicate");
689   case llvm::CmpInst::Predicate::FCMP_FALSE:
690     return LLVM::FCmpPredicate::_false;
691   case llvm::CmpInst::Predicate::FCMP_TRUE:
692     return LLVM::FCmpPredicate::_true;
693   case llvm::CmpInst::Predicate::FCMP_OEQ:
694     return LLVM::FCmpPredicate::oeq;
695   case llvm::CmpInst::Predicate::FCMP_ONE:
696     return LLVM::FCmpPredicate::one;
697   case llvm::CmpInst::Predicate::FCMP_OLT:
698     return LLVM::FCmpPredicate::olt;
699   case llvm::CmpInst::Predicate::FCMP_OLE:
700     return LLVM::FCmpPredicate::ole;
701   case llvm::CmpInst::Predicate::FCMP_OGT:
702     return LLVM::FCmpPredicate::ogt;
703   case llvm::CmpInst::Predicate::FCMP_OGE:
704     return LLVM::FCmpPredicate::oge;
705   case llvm::CmpInst::Predicate::FCMP_ORD:
706     return LLVM::FCmpPredicate::ord;
707   case llvm::CmpInst::Predicate::FCMP_ULT:
708     return LLVM::FCmpPredicate::ult;
709   case llvm::CmpInst::Predicate::FCMP_ULE:
710     return LLVM::FCmpPredicate::ule;
711   case llvm::CmpInst::Predicate::FCMP_UGT:
712     return LLVM::FCmpPredicate::ugt;
713   case llvm::CmpInst::Predicate::FCMP_UGE:
714     return LLVM::FCmpPredicate::uge;
715   case llvm::CmpInst::Predicate::FCMP_UNO:
716     return LLVM::FCmpPredicate::uno;
717   case llvm::CmpInst::Predicate::FCMP_UEQ:
718     return LLVM::FCmpPredicate::ueq;
719   case llvm::CmpInst::Predicate::FCMP_UNE:
720     return LLVM::FCmpPredicate::une;
721   }
722   llvm_unreachable("incorrect floating point comparison predicate");
723 }
724 
725 static AtomicOrdering getLLVMAtomicOrdering(llvm::AtomicOrdering ordering) {
726   switch (ordering) {
727   case llvm::AtomicOrdering::NotAtomic:
728     return LLVM::AtomicOrdering::not_atomic;
729   case llvm::AtomicOrdering::Unordered:
730     return LLVM::AtomicOrdering::unordered;
731   case llvm::AtomicOrdering::Monotonic:
732     return LLVM::AtomicOrdering::monotonic;
733   case llvm::AtomicOrdering::Acquire:
734     return LLVM::AtomicOrdering::acquire;
735   case llvm::AtomicOrdering::Release:
736     return LLVM::AtomicOrdering::release;
737   case llvm::AtomicOrdering::AcquireRelease:
738     return LLVM::AtomicOrdering::acq_rel;
739   case llvm::AtomicOrdering::SequentiallyConsistent:
740     return LLVM::AtomicOrdering::seq_cst;
741   }
742   llvm_unreachable("incorrect atomic ordering");
743 }
744 
745 // `br` branches to `target`. Return the branch arguments to `br`, in the
746 // same order of the PHIs in `target`.
747 LogicalResult
748 Importer::processBranchArgs(llvm::Instruction *br, llvm::BasicBlock *target,
749                             SmallVectorImpl<Value> &blockArguments) {
750   for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) {
751     auto *pn = cast<llvm::PHINode>(&*inst);
752     Value value = processValue(pn->getIncomingValueForBlock(br->getParent()));
753     if (!value)
754       return failure();
755     blockArguments.push_back(value);
756   }
757   return success();
758 }
759 
760 LogicalResult Importer::processInstruction(llvm::Instruction *inst) {
761   // FIXME: Support uses of SubtargetData. Currently inbounds GEPs, fast-math
762   // flags and call / operand attributes are not supported.
763   Location loc = processDebugLoc(inst->getDebugLoc(), inst);
764   assert(!instMap.count(inst) &&
765          "processInstruction must be called only once per instruction!");
766   switch (inst->getOpcode()) {
767   default:
768     return emitError(loc) << "unknown instruction: " << diag(*inst);
769   case llvm::Instruction::Add:
770   case llvm::Instruction::FAdd:
771   case llvm::Instruction::Sub:
772   case llvm::Instruction::FSub:
773   case llvm::Instruction::Mul:
774   case llvm::Instruction::FMul:
775   case llvm::Instruction::UDiv:
776   case llvm::Instruction::SDiv:
777   case llvm::Instruction::FDiv:
778   case llvm::Instruction::URem:
779   case llvm::Instruction::SRem:
780   case llvm::Instruction::FRem:
781   case llvm::Instruction::Shl:
782   case llvm::Instruction::LShr:
783   case llvm::Instruction::AShr:
784   case llvm::Instruction::And:
785   case llvm::Instruction::Or:
786   case llvm::Instruction::Xor:
787   case llvm::Instruction::Load:
788   case llvm::Instruction::Store:
789   case llvm::Instruction::Ret:
790   case llvm::Instruction::Resume:
791   case llvm::Instruction::Trunc:
792   case llvm::Instruction::ZExt:
793   case llvm::Instruction::SExt:
794   case llvm::Instruction::FPToUI:
795   case llvm::Instruction::FPToSI:
796   case llvm::Instruction::UIToFP:
797   case llvm::Instruction::SIToFP:
798   case llvm::Instruction::FPTrunc:
799   case llvm::Instruction::FPExt:
800   case llvm::Instruction::PtrToInt:
801   case llvm::Instruction::IntToPtr:
802   case llvm::Instruction::AddrSpaceCast:
803   case llvm::Instruction::Freeze:
804   case llvm::Instruction::BitCast:
805   case llvm::Instruction::ExtractElement:
806   case llvm::Instruction::InsertElement:
807   case llvm::Instruction::Select:
808   case llvm::Instruction::FNeg:
809   case llvm::Instruction::Unreachable: {
810     OperationState state(loc, lookupOperationNameFromOpcode(inst->getOpcode()));
811     SmallVector<Value, 4> ops;
812     ops.reserve(inst->getNumOperands());
813     for (auto *op : inst->operand_values()) {
814       Value value = processValue(op);
815       if (!value)
816         return failure();
817       ops.push_back(value);
818     }
819     state.addOperands(ops);
820     if (!inst->getType()->isVoidTy()) {
821       Type type = processType(inst->getType());
822       if (!type)
823         return failure();
824       state.addTypes(type);
825     }
826     Operation *op = b.create(state);
827     if (!inst->getType()->isVoidTy())
828       instMap[inst] = op->getResult(0);
829     return success();
830   }
831   case llvm::Instruction::Alloca: {
832     Value size = processValue(inst->getOperand(0));
833     if (!size)
834       return failure();
835 
836     auto *allocaInst = cast<llvm::AllocaInst>(inst);
837     instMap[inst] =
838         b.create<AllocaOp>(loc, processType(inst->getType()),
839                            processType(allocaInst->getAllocatedType()), size,
840                            allocaInst->getAlign().value());
841     return success();
842   }
843   case llvm::Instruction::ICmp: {
844     Value lhs = processValue(inst->getOperand(0));
845     Value rhs = processValue(inst->getOperand(1));
846     if (!lhs || !rhs)
847       return failure();
848     instMap[inst] = b.create<ICmpOp>(
849         loc, getICmpPredicate(cast<llvm::ICmpInst>(inst)->getPredicate()), lhs,
850         rhs);
851     return success();
852   }
853   case llvm::Instruction::FCmp: {
854     Value lhs = processValue(inst->getOperand(0));
855     Value rhs = processValue(inst->getOperand(1));
856     if (!lhs || !rhs)
857       return failure();
858     instMap[inst] = b.create<FCmpOp>(
859         loc, b.getI1Type(),
860         getFCmpPredicate(cast<llvm::FCmpInst>(inst)->getPredicate()), lhs, rhs);
861     return success();
862   }
863   case llvm::Instruction::Br: {
864     auto *brInst = cast<llvm::BranchInst>(inst);
865     OperationState state(loc,
866                          brInst->isConditional() ? "llvm.cond_br" : "llvm.br");
867     if (brInst->isConditional()) {
868       Value condition = processValue(brInst->getCondition());
869       if (!condition)
870         return failure();
871       state.addOperands(condition);
872     }
873 
874     std::array<int32_t, 3> operandSegmentSizes = {1, 0, 0};
875     for (int i : llvm::seq<int>(0, brInst->getNumSuccessors())) {
876       auto *succ = brInst->getSuccessor(i);
877       SmallVector<Value, 4> blockArguments;
878       if (failed(processBranchArgs(brInst, succ, blockArguments)))
879         return failure();
880       state.addSuccessors(blocks[succ]);
881       state.addOperands(blockArguments);
882       operandSegmentSizes[i + 1] = blockArguments.size();
883     }
884 
885     if (brInst->isConditional()) {
886       state.addAttribute(LLVM::CondBrOp::getOperandSegmentSizeAttr(),
887                          b.getI32VectorAttr(operandSegmentSizes));
888     }
889 
890     b.create(state);
891     return success();
892   }
893   case llvm::Instruction::Switch: {
894     auto *swInst = cast<llvm::SwitchInst>(inst);
895     // Process the condition value.
896     Value condition = processValue(swInst->getCondition());
897     if (!condition)
898       return failure();
899 
900     SmallVector<Value> defaultBlockArgs;
901     // Process the default case.
902     llvm::BasicBlock *defaultBB = swInst->getDefaultDest();
903     if (failed(processBranchArgs(swInst, defaultBB, defaultBlockArgs)))
904       return failure();
905 
906     // Process the cases.
907     unsigned numCases = swInst->getNumCases();
908     SmallVector<SmallVector<Value>> caseOperands(numCases);
909     SmallVector<ValueRange> caseOperandRefs(numCases);
910     SmallVector<int32_t> caseValues(numCases);
911     SmallVector<Block *> caseBlocks(numCases);
912     for (const auto &en : llvm::enumerate(swInst->cases())) {
913       const llvm::SwitchInst::CaseHandle &caseHandle = en.value();
914       unsigned i = en.index();
915       llvm::BasicBlock *succBB = caseHandle.getCaseSuccessor();
916       if (failed(processBranchArgs(swInst, succBB, caseOperands[i])))
917         return failure();
918       caseOperandRefs[i] = caseOperands[i];
919       caseValues[i] = caseHandle.getCaseValue()->getSExtValue();
920       caseBlocks[i] = blocks[succBB];
921     }
922 
923     b.create<SwitchOp>(loc, condition, blocks[defaultBB], defaultBlockArgs,
924                        caseValues, caseBlocks, caseOperandRefs);
925     return success();
926   }
927   case llvm::Instruction::PHI: {
928     Type type = processType(inst->getType());
929     if (!type)
930       return failure();
931     instMap[inst] = b.getInsertionBlock()->addArgument(
932         type, processDebugLoc(inst->getDebugLoc(), inst));
933     return success();
934   }
935   case llvm::Instruction::Call: {
936     llvm::CallInst *ci = cast<llvm::CallInst>(inst);
937     SmallVector<Value, 4> ops;
938     ops.reserve(inst->getNumOperands());
939     for (auto &op : ci->args()) {
940       Value arg = processValue(op.get());
941       if (!arg)
942         return failure();
943       ops.push_back(arg);
944     }
945 
946     SmallVector<Type, 2> tys;
947     if (!ci->getType()->isVoidTy()) {
948       Type type = processType(inst->getType());
949       if (!type)
950         return failure();
951       tys.push_back(type);
952     }
953     Operation *op;
954     if (llvm::Function *callee = ci->getCalledFunction()) {
955       op = b.create<CallOp>(
956           loc, tys, SymbolRefAttr::get(b.getContext(), callee->getName()), ops);
957     } else {
958       Value calledValue = processValue(ci->getCalledOperand());
959       if (!calledValue)
960         return failure();
961       ops.insert(ops.begin(), calledValue);
962       op = b.create<CallOp>(loc, tys, ops);
963     }
964     if (!ci->getType()->isVoidTy())
965       instMap[inst] = op->getResult(0);
966     return success();
967   }
968   case llvm::Instruction::LandingPad: {
969     llvm::LandingPadInst *lpi = cast<llvm::LandingPadInst>(inst);
970     SmallVector<Value, 4> ops;
971 
972     for (unsigned i = 0, ie = lpi->getNumClauses(); i < ie; i++)
973       ops.push_back(processConstant(lpi->getClause(i)));
974 
975     Type ty = processType(lpi->getType());
976     if (!ty)
977       return failure();
978 
979     instMap[inst] = b.create<LandingpadOp>(loc, ty, lpi->isCleanup(), ops);
980     return success();
981   }
982   case llvm::Instruction::Invoke: {
983     llvm::InvokeInst *ii = cast<llvm::InvokeInst>(inst);
984 
985     SmallVector<Type, 2> tys;
986     if (!ii->getType()->isVoidTy())
987       tys.push_back(processType(inst->getType()));
988 
989     SmallVector<Value, 4> ops;
990     ops.reserve(inst->getNumOperands() + 1);
991     for (auto &op : ii->args())
992       ops.push_back(processValue(op.get()));
993 
994     SmallVector<Value, 4> normalArgs, unwindArgs;
995     (void)processBranchArgs(ii, ii->getNormalDest(), normalArgs);
996     (void)processBranchArgs(ii, ii->getUnwindDest(), unwindArgs);
997 
998     Operation *op;
999     if (llvm::Function *callee = ii->getCalledFunction()) {
1000       op = b.create<InvokeOp>(
1001           loc, tys, SymbolRefAttr::get(b.getContext(), callee->getName()), ops,
1002           blocks[ii->getNormalDest()], normalArgs, blocks[ii->getUnwindDest()],
1003           unwindArgs);
1004     } else {
1005       ops.insert(ops.begin(), processValue(ii->getCalledOperand()));
1006       op = b.create<InvokeOp>(loc, tys, ops, blocks[ii->getNormalDest()],
1007                               normalArgs, blocks[ii->getUnwindDest()],
1008                               unwindArgs);
1009     }
1010 
1011     if (!ii->getType()->isVoidTy())
1012       instMap[inst] = op->getResult(0);
1013     return success();
1014   }
1015   case llvm::Instruction::Fence: {
1016     StringRef syncscope;
1017     SmallVector<StringRef, 4> ssNs;
1018     llvm::LLVMContext &llvmContext = inst->getContext();
1019     llvm::FenceInst *fence = cast<llvm::FenceInst>(inst);
1020     llvmContext.getSyncScopeNames(ssNs);
1021     int fenceSyncScopeID = fence->getSyncScopeID();
1022     for (unsigned i = 0, e = ssNs.size(); i != e; i++) {
1023       if (fenceSyncScopeID == llvmContext.getOrInsertSyncScopeID(ssNs[i])) {
1024         syncscope = ssNs[i];
1025         break;
1026       }
1027     }
1028     b.create<FenceOp>(loc, getLLVMAtomicOrdering(fence->getOrdering()),
1029                       syncscope);
1030     return success();
1031   }
1032   case llvm::Instruction::GetElementPtr: {
1033     // FIXME: Support inbounds GEPs.
1034     llvm::GetElementPtrInst *gep = cast<llvm::GetElementPtrInst>(inst);
1035     Value basePtr = processValue(gep->getOperand(0));
1036     Type sourceElementType = processType(gep->getSourceElementType());
1037 
1038     SmallVector<Value> indices;
1039     for (llvm::Value *operand : llvm::drop_begin(gep->operand_values())) {
1040       indices.push_back(processValue(operand));
1041       if (!indices.back())
1042         return failure();
1043     }
1044     // Treat every indices as dynamic since GEPOp::build will refine those
1045     // indices into static attributes later. One small downside of this
1046     // approach is that many unused `llvm.mlir.constant` would be emitted
1047     // at first place.
1048     SmallVector<int32_t> structIndices(indices.size(),
1049                                        LLVM::GEPOp::kDynamicIndex);
1050 
1051     Type type = processType(inst->getType());
1052     if (!type)
1053       return failure();
1054     instMap[inst] = b.create<GEPOp>(loc, type, sourceElementType, basePtr,
1055                                     indices, structIndices);
1056     return success();
1057   }
1058   case llvm::Instruction::InsertValue: {
1059     auto *ivInst = cast<llvm::InsertValueInst>(inst);
1060     Value inserted = processValue(ivInst->getInsertedValueOperand());
1061     if (!inserted)
1062       return failure();
1063     Value aggOperand = processValue(ivInst->getAggregateOperand());
1064     if (!aggOperand)
1065       return failure();
1066 
1067     SmallVector<int32_t> idxValues;
1068     for (unsigned idx : ivInst->getIndices())
1069       idxValues.push_back(static_cast<int32_t>(idx));
1070     ArrayAttr indices = b.getI32ArrayAttr(idxValues);
1071 
1072     instMap[inst] = b.create<InsertValueOp>(loc, aggOperand, inserted, indices);
1073     return success();
1074   }
1075   case llvm::Instruction::ExtractValue: {
1076     auto *evInst = cast<llvm::ExtractValueInst>(inst);
1077     Value aggOperand = processValue(evInst->getAggregateOperand());
1078     if (!aggOperand)
1079       return failure();
1080 
1081     Type type = processType(inst->getType());
1082     if (!type)
1083       return failure();
1084 
1085     SmallVector<int32_t> idxValues;
1086     for (unsigned idx : evInst->getIndices())
1087       idxValues.push_back(static_cast<int32_t>(idx));
1088     ArrayAttr indices = b.getI32ArrayAttr(idxValues);
1089 
1090     instMap[inst] = b.create<ExtractValueOp>(loc, type, aggOperand, indices);
1091     return success();
1092   }
1093   case llvm::Instruction::ShuffleVector: {
1094     auto *svInst = cast<llvm::ShuffleVectorInst>(inst);
1095     Value vec1 = processValue(svInst->getOperand(0));
1096     if (!vec1)
1097       return failure();
1098     Value vec2 = processValue(svInst->getOperand(1));
1099     if (!vec2)
1100       return failure();
1101 
1102     ArrayAttr mask = b.getI32ArrayAttr(svInst->getShuffleMask());
1103 
1104     instMap[inst] = b.create<ShuffleVectorOp>(loc, vec1, vec2, mask);
1105     return success();
1106   }
1107   }
1108 }
1109 
1110 FlatSymbolRefAttr Importer::getPersonalityAsAttr(llvm::Function *f) {
1111   if (!f->hasPersonalityFn())
1112     return nullptr;
1113 
1114   llvm::Constant *pf = f->getPersonalityFn();
1115 
1116   // If it directly has a name, we can use it.
1117   if (pf->hasName())
1118     return SymbolRefAttr::get(b.getContext(), pf->getName());
1119 
1120   // If it doesn't have a name, currently, only function pointers that are
1121   // bitcast to i8* are parsed.
1122   if (auto *ce = dyn_cast<llvm::ConstantExpr>(pf)) {
1123     if (ce->getOpcode() == llvm::Instruction::BitCast &&
1124         ce->getType() == llvm::Type::getInt8PtrTy(f->getContext())) {
1125       if (auto *func = dyn_cast<llvm::Function>(ce->getOperand(0)))
1126         return SymbolRefAttr::get(b.getContext(), func->getName());
1127     }
1128   }
1129   return FlatSymbolRefAttr();
1130 }
1131 
1132 LogicalResult Importer::processFunction(llvm::Function *f) {
1133   blocks.clear();
1134   instMap.clear();
1135   unknownInstMap.clear();
1136 
1137   auto functionType =
1138       processType(f->getFunctionType()).dyn_cast<LLVMFunctionType>();
1139   if (!functionType)
1140     return failure();
1141 
1142   b.setInsertionPoint(module.getBody(), getFuncInsertPt());
1143   LLVMFuncOp fop =
1144       b.create<LLVMFuncOp>(UnknownLoc::get(context), f->getName(), functionType,
1145                            convertLinkageFromLLVM(f->getLinkage()));
1146 
1147   if (FlatSymbolRefAttr personality = getPersonalityAsAttr(f))
1148     fop->setAttr(b.getStringAttr("personality"), personality);
1149   else if (f->hasPersonalityFn())
1150     emitWarning(UnknownLoc::get(context),
1151                 "could not deduce personality, skipping it");
1152 
1153   if (f->hasGC())
1154     fop.setGarbageCollectorAttr(b.getStringAttr(f->getGC()));
1155 
1156   if (f->isDeclaration())
1157     return success();
1158 
1159   // Eagerly create all blocks.
1160   SmallVector<Block *, 4> blockList;
1161   for (llvm::BasicBlock &bb : *f) {
1162     blockList.push_back(b.createBlock(&fop.getBody(), fop.getBody().end()));
1163     blocks[&bb] = blockList.back();
1164   }
1165   currentEntryBlock = blockList[0];
1166 
1167   // Add function arguments to the entry block.
1168   for (const auto &kv : llvm::enumerate(f->args())) {
1169     instMap[&kv.value()] = blockList[0]->addArgument(
1170         functionType.getParamType(kv.index()), fop.getLoc());
1171   }
1172 
1173   for (auto bbs : llvm::zip(*f, blockList)) {
1174     if (failed(processBasicBlock(&std::get<0>(bbs), std::get<1>(bbs))))
1175       return failure();
1176   }
1177 
1178   // Now that all instructions are guaranteed to have been visited, ensure
1179   // any unknown uses we encountered are remapped.
1180   for (auto &llvmAndUnknown : unknownInstMap) {
1181     assert(instMap.count(llvmAndUnknown.first));
1182     Value newValue = instMap[llvmAndUnknown.first];
1183     Value oldValue = llvmAndUnknown.second->getResult(0);
1184     oldValue.replaceAllUsesWith(newValue);
1185     llvmAndUnknown.second->erase();
1186   }
1187   return success();
1188 }
1189 
1190 LogicalResult Importer::processBasicBlock(llvm::BasicBlock *bb, Block *block) {
1191   b.setInsertionPointToStart(block);
1192   for (llvm::Instruction &inst : *bb) {
1193     if (failed(processInstruction(&inst)))
1194       return failure();
1195   }
1196   return success();
1197 }
1198 
1199 OwningOpRef<ModuleOp>
1200 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
1201                               MLIRContext *context) {
1202   context->loadDialect<LLVMDialect>();
1203   context->loadDialect<DLTIDialect>();
1204   OwningOpRef<ModuleOp> module(ModuleOp::create(
1205       FileLineColLoc::get(context, "", /*line=*/0, /*column=*/0)));
1206 
1207   DataLayoutSpecInterface dlSpec =
1208       translateDataLayout(llvmModule->getDataLayout(), context);
1209   if (!dlSpec) {
1210     emitError(UnknownLoc::get(context), "can't translate data layout");
1211     return {};
1212   }
1213 
1214   module.get()->setAttr(DLTIDialect::kDataLayoutAttrName, dlSpec);
1215 
1216   Importer deserializer(context, module.get());
1217   for (llvm::GlobalVariable &gv : llvmModule->globals()) {
1218     if (!deserializer.processGlobal(&gv))
1219       return {};
1220   }
1221   for (llvm::Function &f : llvmModule->functions()) {
1222     if (failed(deserializer.processFunction(&f)))
1223       return {};
1224   }
1225 
1226   return module;
1227 }
1228 
1229 // Deserializes the LLVM bitcode stored in `input` into an MLIR module in the
1230 // LLVM dialect.
1231 OwningOpRef<ModuleOp> translateLLVMIRToModule(llvm::SourceMgr &sourceMgr,
1232                                               MLIRContext *context) {
1233   llvm::SMDiagnostic err;
1234   llvm::LLVMContext llvmContext;
1235   std::unique_ptr<llvm::Module> llvmModule = llvm::parseIR(
1236       *sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), err, llvmContext);
1237   if (!llvmModule) {
1238     std::string errStr;
1239     llvm::raw_string_ostream errStream(errStr);
1240     err.print(/*ProgName=*/"", errStream);
1241     emitError(UnknownLoc::get(context)) << errStream.str();
1242     return {};
1243   }
1244   return translateLLVMIRToModule(std::move(llvmModule), context);
1245 }
1246 
1247 namespace mlir {
1248 void registerFromLLVMIRTranslation() {
1249   TranslateToMLIRRegistration fromLLVM(
1250       "import-llvm", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) {
1251         return ::translateLLVMIRToModule(sourceMgr, context);
1252       });
1253 }
1254 } // namespace mlir
1255