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     if (c->getType()->isDoubleTy())
351       return b.getFloatAttr(FloatType::getF64(context), c->getValueAPF());
352     if (c->getType()->isFloatingPointTy())
353       return b.getFloatAttr(FloatType::getF32(context), c->getValueAPF());
354   }
355   if (auto *f = dyn_cast<llvm::Function>(value))
356     return SymbolRefAttr::get(b.getContext(), f->getName());
357 
358   // Convert constant data to a dense elements attribute.
359   if (auto *cd = dyn_cast<llvm::ConstantDataSequential>(value)) {
360     Type type = processType(cd->getElementType());
361     if (!type)
362       return nullptr;
363 
364     auto attrType = getStdTypeForAttr(processType(cd->getType()))
365                         .dyn_cast_or_null<ShapedType>();
366     if (!attrType)
367       return nullptr;
368 
369     if (type.isa<IntegerType>()) {
370       SmallVector<APInt, 8> values;
371       values.reserve(cd->getNumElements());
372       for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i)
373         values.push_back(cd->getElementAsAPInt(i));
374       return DenseElementsAttr::get(attrType, values);
375     }
376 
377     if (type.isa<Float32Type, Float64Type>()) {
378       SmallVector<APFloat, 8> values;
379       values.reserve(cd->getNumElements());
380       for (unsigned i = 0, e = cd->getNumElements(); i < e; ++i)
381         values.push_back(cd->getElementAsAPFloat(i));
382       return DenseElementsAttr::get(attrType, values);
383     }
384 
385     return nullptr;
386   }
387 
388   // Unpack constant aggregates to create dense elements attribute whenever
389   // possible. Return nullptr (failure) otherwise.
390   if (isa<llvm::ConstantAggregate>(value)) {
391     auto outerType = getStdTypeForAttr(processType(value->getType()))
392                          .dyn_cast_or_null<ShapedType>();
393     if (!outerType)
394       return nullptr;
395 
396     SmallVector<Attribute, 8> values;
397     SmallVector<int64_t, 8> shape;
398 
399     for (unsigned i = 0, e = value->getNumOperands(); i < e; ++i) {
400       auto nested = getConstantAsAttr(value->getAggregateElement(i))
401                         .dyn_cast_or_null<DenseElementsAttr>();
402       if (!nested)
403         return nullptr;
404 
405       values.append(nested.value_begin<Attribute>(),
406                     nested.value_end<Attribute>());
407     }
408 
409     return DenseElementsAttr::get(outerType, values);
410   }
411 
412   return nullptr;
413 }
414 
415 GlobalOp Importer::processGlobal(llvm::GlobalVariable *gv) {
416   auto it = globals.find(gv);
417   if (it != globals.end())
418     return it->second;
419 
420   OpBuilder b(module.getBody(), getGlobalInsertPt());
421   Attribute valueAttr;
422   if (gv->hasInitializer())
423     valueAttr = getConstantAsAttr(gv->getInitializer());
424   Type type = processType(gv->getValueType());
425   if (!type)
426     return nullptr;
427 
428   uint64_t alignment = 0;
429   llvm::MaybeAlign maybeAlign = gv->getAlign();
430   if (maybeAlign.hasValue()) {
431     llvm::Align align = maybeAlign.getValue();
432     alignment = align.value();
433   }
434 
435   GlobalOp op = b.create<GlobalOp>(
436       UnknownLoc::get(context), type, gv->isConstant(),
437       convertLinkageFromLLVM(gv->getLinkage()), gv->getName(), valueAttr,
438       alignment, /*addr_space=*/gv->getAddressSpace(),
439       /*dso_local=*/gv->isDSOLocal(), /*thread_local=*/gv->isThreadLocal());
440 
441   if (gv->hasInitializer() && !valueAttr) {
442     Region &r = op.getInitializerRegion();
443     currentEntryBlock = b.createBlock(&r);
444     b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin());
445     Value v = processConstant(gv->getInitializer());
446     if (!v)
447       return nullptr;
448     b.create<ReturnOp>(op.getLoc(), ArrayRef<Value>({v}));
449   }
450   if (gv->hasAtLeastLocalUnnamedAddr())
451     op.setUnnamedAddrAttr(UnnamedAddrAttr::get(
452         context, convertUnnamedAddrFromLLVM(gv->getUnnamedAddr())));
453   if (gv->hasSection())
454     op.setSectionAttr(b.getStringAttr(gv->getSection()));
455 
456   return globals[gv] = op;
457 }
458 
459 Value Importer::processConstant(llvm::Constant *c) {
460   OpBuilder bEntry(currentEntryBlock, currentEntryBlock->begin());
461   if (Attribute attr = getConstantAsAttr(c)) {
462     // These constants can be represented as attributes.
463     OpBuilder b(currentEntryBlock, currentEntryBlock->begin());
464     Type type = processType(c->getType());
465     if (!type)
466       return nullptr;
467     if (auto symbolRef = attr.dyn_cast<FlatSymbolRefAttr>())
468       return instMap[c] = bEntry.create<AddressOfOp>(unknownLoc, type,
469                                                      symbolRef.getValue());
470     return instMap[c] = bEntry.create<ConstantOp>(unknownLoc, type, attr);
471   }
472   if (auto *cn = dyn_cast<llvm::ConstantPointerNull>(c)) {
473     Type type = processType(cn->getType());
474     if (!type)
475       return nullptr;
476     return instMap[c] = bEntry.create<NullOp>(unknownLoc, type);
477   }
478   if (auto *gv = dyn_cast<llvm::GlobalVariable>(c))
479     return bEntry.create<AddressOfOp>(UnknownLoc::get(context),
480                                       processGlobal(gv));
481 
482   if (auto *ce = dyn_cast<llvm::ConstantExpr>(c)) {
483     llvm::Instruction *i = ce->getAsInstruction();
484     OpBuilder::InsertionGuard guard(b);
485     b.setInsertionPoint(currentEntryBlock, currentEntryBlock->begin());
486     if (failed(processInstruction(i)))
487       return nullptr;
488     assert(instMap.count(i));
489 
490     // If we don't remove entry of `i` here, it's totally possible that the
491     // next time llvm::ConstantExpr::getAsInstruction is called again, which
492     // always allocates a new Instruction, memory address of the newly
493     // created Instruction might be the same as `i`. Making processInstruction
494     // falsely believe that the new Instruction has been processed before
495     // and raised an assertion error.
496     Value value = instMap[i];
497     instMap.erase(i);
498     // Remove this zombie LLVM instruction now, leaving us only with the MLIR
499     // op.
500     i->deleteValue();
501     return instMap[c] = value;
502   }
503   if (auto *ue = dyn_cast<llvm::UndefValue>(c)) {
504     Type type = processType(ue->getType());
505     if (!type)
506       return nullptr;
507     return instMap[c] = bEntry.create<UndefOp>(UnknownLoc::get(context), type);
508   }
509 
510   if (isa<llvm::ConstantAggregate>(c) || isa<llvm::ConstantAggregateZero>(c)) {
511     unsigned numElements = c->getNumOperands();
512     std::function<llvm::Constant *(unsigned)> getElement =
513         [&](unsigned index) -> llvm::Constant * {
514       return c->getAggregateElement(index);
515     };
516     // llvm::ConstantAggregateZero doesn't take any operand
517     // so its getNumOperands is always zero.
518     if (auto *caz = dyn_cast<llvm::ConstantAggregateZero>(c)) {
519       numElements = caz->getElementCount().getFixedValue();
520       // We want to capture the pointer rather than reference
521       // to the pointer since the latter will become dangling upon
522       // exiting the scope.
523       getElement = [=](unsigned index) -> llvm::Constant * {
524         return caz->getElementValue(index);
525       };
526     }
527 
528     // Generate a llvm.undef as the root value first.
529     Type rootType = processType(c->getType());
530     if (!rootType)
531       return nullptr;
532     Value root = bEntry.create<UndefOp>(unknownLoc, rootType);
533     for (unsigned i = 0; i < numElements; ++i) {
534       llvm::Constant *element = getElement(i);
535       Value elementValue = processConstant(element);
536       if (!elementValue)
537         return nullptr;
538       ArrayAttr indexAttr = bEntry.getI32ArrayAttr({static_cast<int32_t>(i)});
539       root = bEntry.create<InsertValueOp>(UnknownLoc::get(context), rootType,
540                                           root, elementValue, indexAttr);
541     }
542     return root;
543   }
544 
545   emitError(unknownLoc) << "unhandled constant: " << diag(*c);
546   return nullptr;
547 }
548 
549 Value Importer::processValue(llvm::Value *value) {
550   auto it = instMap.find(value);
551   if (it != instMap.end())
552     return it->second;
553 
554   // We don't expect to see instructions in dominator order. If we haven't seen
555   // this instruction yet, create an unknown op and remap it later.
556   if (isa<llvm::Instruction>(value)) {
557     Type type = processType(value->getType());
558     if (!type)
559       return nullptr;
560     unknownInstMap[value] =
561         b.create(UnknownLoc::get(context), b.getStringAttr("llvm.unknown"),
562                  /*operands=*/{}, type);
563     return unknownInstMap[value]->getResult(0);
564   }
565 
566   if (auto *c = dyn_cast<llvm::Constant>(value))
567     return processConstant(c);
568 
569   emitError(unknownLoc) << "unhandled value: " << diag(*value);
570   return nullptr;
571 }
572 
573 /// Return the MLIR OperationName for the given LLVM opcode.
574 static StringRef lookupOperationNameFromOpcode(unsigned opcode) {
575 // Maps from LLVM opcode to MLIR OperationName. This is deliberately ordered
576 // as in llvm/IR/Instructions.def to aid comprehension and spot missing
577 // instructions.
578 #define INST(llvm_n, mlir_n)                                                   \
579   { llvm::Instruction::llvm_n, LLVM::mlir_n##Op::getOperationName() }
580   static const DenseMap<unsigned, StringRef> opcMap = {
581       // Ret is handled specially.
582       // Br is handled specially.
583       // FIXME: switch
584       // FIXME: indirectbr
585       // FIXME: invoke
586       INST(Resume, Resume),
587       // FIXME: unreachable
588       // FIXME: cleanupret
589       // FIXME: catchret
590       // FIXME: catchswitch
591       // FIXME: callbr
592       // FIXME: fneg
593       INST(Add, Add), INST(FAdd, FAdd), INST(Sub, Sub), INST(FSub, FSub),
594       INST(Mul, Mul), INST(FMul, FMul), INST(UDiv, UDiv), INST(SDiv, SDiv),
595       INST(FDiv, FDiv), INST(URem, URem), INST(SRem, SRem), INST(FRem, FRem),
596       INST(Shl, Shl), INST(LShr, LShr), INST(AShr, AShr), INST(And, And),
597       INST(Or, Or), INST(Xor, XOr), INST(Alloca, Alloca), INST(Load, Load),
598       INST(Store, Store),
599       // Getelementptr is handled specially.
600       INST(Ret, Return), INST(Fence, Fence),
601       // FIXME: atomiccmpxchg
602       // FIXME: atomicrmw
603       INST(Trunc, Trunc), INST(ZExt, ZExt), INST(SExt, SExt),
604       INST(FPToUI, FPToUI), INST(FPToSI, FPToSI), INST(UIToFP, UIToFP),
605       INST(SIToFP, SIToFP), INST(FPTrunc, FPTrunc), INST(FPExt, FPExt),
606       INST(PtrToInt, PtrToInt), INST(IntToPtr, IntToPtr),
607       INST(BitCast, Bitcast), INST(AddrSpaceCast, AddrSpaceCast),
608       // FIXME: cleanuppad
609       // FIXME: catchpad
610       // ICmp is handled specially.
611       // FIXME: fcmp
612       // PHI is handled specially.
613       INST(Freeze, Freeze), INST(Call, Call),
614       // FIXME: select
615       // FIXME: vaarg
616       // FIXME: extractelement
617       // FIXME: insertelement
618       // FIXME: shufflevector
619       // FIXME: extractvalue
620       // FIXME: insertvalue
621       // FIXME: landingpad
622   };
623 #undef INST
624 
625   return opcMap.lookup(opcode);
626 }
627 
628 static ICmpPredicate getICmpPredicate(llvm::CmpInst::Predicate p) {
629   switch (p) {
630   default:
631     llvm_unreachable("incorrect comparison predicate");
632   case llvm::CmpInst::Predicate::ICMP_EQ:
633     return LLVM::ICmpPredicate::eq;
634   case llvm::CmpInst::Predicate::ICMP_NE:
635     return LLVM::ICmpPredicate::ne;
636   case llvm::CmpInst::Predicate::ICMP_SLT:
637     return LLVM::ICmpPredicate::slt;
638   case llvm::CmpInst::Predicate::ICMP_SLE:
639     return LLVM::ICmpPredicate::sle;
640   case llvm::CmpInst::Predicate::ICMP_SGT:
641     return LLVM::ICmpPredicate::sgt;
642   case llvm::CmpInst::Predicate::ICMP_SGE:
643     return LLVM::ICmpPredicate::sge;
644   case llvm::CmpInst::Predicate::ICMP_ULT:
645     return LLVM::ICmpPredicate::ult;
646   case llvm::CmpInst::Predicate::ICMP_ULE:
647     return LLVM::ICmpPredicate::ule;
648   case llvm::CmpInst::Predicate::ICMP_UGT:
649     return LLVM::ICmpPredicate::ugt;
650   case llvm::CmpInst::Predicate::ICMP_UGE:
651     return LLVM::ICmpPredicate::uge;
652   }
653   llvm_unreachable("incorrect comparison predicate");
654 }
655 
656 static AtomicOrdering getLLVMAtomicOrdering(llvm::AtomicOrdering ordering) {
657   switch (ordering) {
658   case llvm::AtomicOrdering::NotAtomic:
659     return LLVM::AtomicOrdering::not_atomic;
660   case llvm::AtomicOrdering::Unordered:
661     return LLVM::AtomicOrdering::unordered;
662   case llvm::AtomicOrdering::Monotonic:
663     return LLVM::AtomicOrdering::monotonic;
664   case llvm::AtomicOrdering::Acquire:
665     return LLVM::AtomicOrdering::acquire;
666   case llvm::AtomicOrdering::Release:
667     return LLVM::AtomicOrdering::release;
668   case llvm::AtomicOrdering::AcquireRelease:
669     return LLVM::AtomicOrdering::acq_rel;
670   case llvm::AtomicOrdering::SequentiallyConsistent:
671     return LLVM::AtomicOrdering::seq_cst;
672   }
673   llvm_unreachable("incorrect atomic ordering");
674 }
675 
676 // `br` branches to `target`. Return the branch arguments to `br`, in the
677 // same order of the PHIs in `target`.
678 LogicalResult
679 Importer::processBranchArgs(llvm::Instruction *br, llvm::BasicBlock *target,
680                             SmallVectorImpl<Value> &blockArguments) {
681   for (auto inst = target->begin(); isa<llvm::PHINode>(inst); ++inst) {
682     auto *pn = cast<llvm::PHINode>(&*inst);
683     Value value = processValue(pn->getIncomingValueForBlock(br->getParent()));
684     if (!value)
685       return failure();
686     blockArguments.push_back(value);
687   }
688   return success();
689 }
690 
691 LogicalResult Importer::processInstruction(llvm::Instruction *inst) {
692   // FIXME: Support uses of SubtargetData. Currently inbounds GEPs, fast-math
693   // flags and call / operand attributes are not supported.
694   Location loc = processDebugLoc(inst->getDebugLoc(), inst);
695   Value &v = instMap[inst];
696   assert(!v && "processInstruction must be called only once per instruction!");
697   switch (inst->getOpcode()) {
698   default:
699     return emitError(loc) << "unknown instruction: " << diag(*inst);
700   case llvm::Instruction::Add:
701   case llvm::Instruction::FAdd:
702   case llvm::Instruction::Sub:
703   case llvm::Instruction::FSub:
704   case llvm::Instruction::Mul:
705   case llvm::Instruction::FMul:
706   case llvm::Instruction::UDiv:
707   case llvm::Instruction::SDiv:
708   case llvm::Instruction::FDiv:
709   case llvm::Instruction::URem:
710   case llvm::Instruction::SRem:
711   case llvm::Instruction::FRem:
712   case llvm::Instruction::Shl:
713   case llvm::Instruction::LShr:
714   case llvm::Instruction::AShr:
715   case llvm::Instruction::And:
716   case llvm::Instruction::Or:
717   case llvm::Instruction::Xor:
718   case llvm::Instruction::Load:
719   case llvm::Instruction::Store:
720   case llvm::Instruction::Ret:
721   case llvm::Instruction::Resume:
722   case llvm::Instruction::Trunc:
723   case llvm::Instruction::ZExt:
724   case llvm::Instruction::SExt:
725   case llvm::Instruction::FPToUI:
726   case llvm::Instruction::FPToSI:
727   case llvm::Instruction::UIToFP:
728   case llvm::Instruction::SIToFP:
729   case llvm::Instruction::FPTrunc:
730   case llvm::Instruction::FPExt:
731   case llvm::Instruction::PtrToInt:
732   case llvm::Instruction::IntToPtr:
733   case llvm::Instruction::AddrSpaceCast:
734   case llvm::Instruction::Freeze:
735   case llvm::Instruction::BitCast: {
736     OperationState state(loc, lookupOperationNameFromOpcode(inst->getOpcode()));
737     SmallVector<Value, 4> ops;
738     ops.reserve(inst->getNumOperands());
739     for (auto *op : inst->operand_values()) {
740       Value value = processValue(op);
741       if (!value)
742         return failure();
743       ops.push_back(value);
744     }
745     state.addOperands(ops);
746     if (!inst->getType()->isVoidTy()) {
747       Type type = processType(inst->getType());
748       if (!type)
749         return failure();
750       state.addTypes(type);
751     }
752     Operation *op = b.create(state);
753     if (!inst->getType()->isVoidTy())
754       v = op->getResult(0);
755     return success();
756   }
757   case llvm::Instruction::Alloca: {
758     Value size = processValue(inst->getOperand(0));
759     if (!size)
760       return failure();
761 
762     auto *allocaInst = cast<llvm::AllocaInst>(inst);
763     v = b.create<AllocaOp>(loc, processType(inst->getType()),
764                            processType(allocaInst->getAllocatedType()), size,
765                            allocaInst->getAlign().value());
766     return success();
767   }
768   case llvm::Instruction::ICmp: {
769     Value lhs = processValue(inst->getOperand(0));
770     Value rhs = processValue(inst->getOperand(1));
771     if (!lhs || !rhs)
772       return failure();
773     v = b.create<ICmpOp>(
774         loc, getICmpPredicate(cast<llvm::ICmpInst>(inst)->getPredicate()), lhs,
775         rhs);
776     return success();
777   }
778   case llvm::Instruction::Br: {
779     auto *brInst = cast<llvm::BranchInst>(inst);
780     OperationState state(loc,
781                          brInst->isConditional() ? "llvm.cond_br" : "llvm.br");
782     if (brInst->isConditional()) {
783       Value condition = processValue(brInst->getCondition());
784       if (!condition)
785         return failure();
786       state.addOperands(condition);
787     }
788 
789     std::array<int32_t, 3> operandSegmentSizes = {1, 0, 0};
790     for (int i : llvm::seq<int>(0, brInst->getNumSuccessors())) {
791       auto *succ = brInst->getSuccessor(i);
792       SmallVector<Value, 4> blockArguments;
793       if (failed(processBranchArgs(brInst, succ, blockArguments)))
794         return failure();
795       state.addSuccessors(blocks[succ]);
796       state.addOperands(blockArguments);
797       operandSegmentSizes[i + 1] = blockArguments.size();
798     }
799 
800     if (brInst->isConditional()) {
801       state.addAttribute(LLVM::CondBrOp::getOperandSegmentSizeAttr(),
802                          b.getI32VectorAttr(operandSegmentSizes));
803     }
804 
805     b.create(state);
806     return success();
807   }
808   case llvm::Instruction::PHI: {
809     Type type = processType(inst->getType());
810     if (!type)
811       return failure();
812     v = b.getInsertionBlock()->addArgument(
813         type, processDebugLoc(inst->getDebugLoc(), inst));
814     return success();
815   }
816   case llvm::Instruction::Call: {
817     llvm::CallInst *ci = cast<llvm::CallInst>(inst);
818     SmallVector<Value, 4> ops;
819     ops.reserve(inst->getNumOperands());
820     for (auto &op : ci->args()) {
821       Value arg = processValue(op.get());
822       if (!arg)
823         return failure();
824       ops.push_back(arg);
825     }
826 
827     SmallVector<Type, 2> tys;
828     if (!ci->getType()->isVoidTy()) {
829       Type type = processType(inst->getType());
830       if (!type)
831         return failure();
832       tys.push_back(type);
833     }
834     Operation *op;
835     if (llvm::Function *callee = ci->getCalledFunction()) {
836       op = b.create<CallOp>(
837           loc, tys, SymbolRefAttr::get(b.getContext(), callee->getName()), ops);
838     } else {
839       Value calledValue = processValue(ci->getCalledOperand());
840       if (!calledValue)
841         return failure();
842       ops.insert(ops.begin(), calledValue);
843       op = b.create<CallOp>(loc, tys, ops);
844     }
845     if (!ci->getType()->isVoidTy())
846       v = op->getResult(0);
847     return success();
848   }
849   case llvm::Instruction::LandingPad: {
850     llvm::LandingPadInst *lpi = cast<llvm::LandingPadInst>(inst);
851     SmallVector<Value, 4> ops;
852 
853     for (unsigned i = 0, ie = lpi->getNumClauses(); i < ie; i++)
854       ops.push_back(processConstant(lpi->getClause(i)));
855 
856     Type ty = processType(lpi->getType());
857     if (!ty)
858       return failure();
859 
860     v = b.create<LandingpadOp>(loc, ty, lpi->isCleanup(), ops);
861     return success();
862   }
863   case llvm::Instruction::Invoke: {
864     llvm::InvokeInst *ii = cast<llvm::InvokeInst>(inst);
865 
866     SmallVector<Type, 2> tys;
867     if (!ii->getType()->isVoidTy())
868       tys.push_back(processType(inst->getType()));
869 
870     SmallVector<Value, 4> ops;
871     ops.reserve(inst->getNumOperands() + 1);
872     for (auto &op : ii->args())
873       ops.push_back(processValue(op.get()));
874 
875     SmallVector<Value, 4> normalArgs, unwindArgs;
876     (void)processBranchArgs(ii, ii->getNormalDest(), normalArgs);
877     (void)processBranchArgs(ii, ii->getUnwindDest(), unwindArgs);
878 
879     Operation *op;
880     if (llvm::Function *callee = ii->getCalledFunction()) {
881       op = b.create<InvokeOp>(
882           loc, tys, SymbolRefAttr::get(b.getContext(), callee->getName()), ops,
883           blocks[ii->getNormalDest()], normalArgs, blocks[ii->getUnwindDest()],
884           unwindArgs);
885     } else {
886       ops.insert(ops.begin(), processValue(ii->getCalledOperand()));
887       op = b.create<InvokeOp>(loc, tys, ops, blocks[ii->getNormalDest()],
888                               normalArgs, blocks[ii->getUnwindDest()],
889                               unwindArgs);
890     }
891 
892     if (!ii->getType()->isVoidTy())
893       v = op->getResult(0);
894     return success();
895   }
896   case llvm::Instruction::Fence: {
897     StringRef syncscope;
898     SmallVector<StringRef, 4> ssNs;
899     llvm::LLVMContext &llvmContext = inst->getContext();
900     llvm::FenceInst *fence = cast<llvm::FenceInst>(inst);
901     llvmContext.getSyncScopeNames(ssNs);
902     int fenceSyncScopeID = fence->getSyncScopeID();
903     for (unsigned i = 0, e = ssNs.size(); i != e; i++) {
904       if (fenceSyncScopeID == llvmContext.getOrInsertSyncScopeID(ssNs[i])) {
905         syncscope = ssNs[i];
906         break;
907       }
908     }
909     b.create<FenceOp>(loc, getLLVMAtomicOrdering(fence->getOrdering()),
910                       syncscope);
911     return success();
912   }
913   case llvm::Instruction::GetElementPtr: {
914     // FIXME: Support inbounds GEPs.
915     llvm::GetElementPtrInst *gep = cast<llvm::GetElementPtrInst>(inst);
916     Value basePtr = processValue(gep->getOperand(0));
917     SmallVector<int32_t> staticIndices;
918     SmallVector<Value> dynamicIndices;
919     Type sourceElementType = processType(gep->getSourceElementType());
920     SmallVector<unsigned> staticIndexPositions;
921     GEPOp::findKnownStructIndices(sourceElementType, staticIndexPositions);
922 
923     for (const auto &en :
924          llvm::enumerate(llvm::drop_begin(gep->operand_values()))) {
925       llvm::Value *operand = en.value();
926       if (llvm::find(staticIndexPositions, en.index()) ==
927           staticIndexPositions.end()) {
928         staticIndices.push_back(GEPOp::kDynamicIndex);
929         dynamicIndices.push_back(processValue(operand));
930         if (!dynamicIndices.back())
931           return failure();
932       } else {
933         auto *constantInt = cast<llvm::ConstantInt>(operand);
934         staticIndices.push_back(
935             static_cast<int32_t>(constantInt->getValue().getZExtValue()));
936       }
937     }
938 
939     Type type = processType(inst->getType());
940     if (!type)
941       return failure();
942     v = b.create<GEPOp>(loc, type, sourceElementType, basePtr, dynamicIndices,
943                         staticIndices);
944     return success();
945   }
946   }
947 }
948 
949 FlatSymbolRefAttr Importer::getPersonalityAsAttr(llvm::Function *f) {
950   if (!f->hasPersonalityFn())
951     return nullptr;
952 
953   llvm::Constant *pf = f->getPersonalityFn();
954 
955   // If it directly has a name, we can use it.
956   if (pf->hasName())
957     return SymbolRefAttr::get(b.getContext(), pf->getName());
958 
959   // If it doesn't have a name, currently, only function pointers that are
960   // bitcast to i8* are parsed.
961   if (auto *ce = dyn_cast<llvm::ConstantExpr>(pf)) {
962     if (ce->getOpcode() == llvm::Instruction::BitCast &&
963         ce->getType() == llvm::Type::getInt8PtrTy(f->getContext())) {
964       if (auto *func = dyn_cast<llvm::Function>(ce->getOperand(0)))
965         return SymbolRefAttr::get(b.getContext(), func->getName());
966     }
967   }
968   return FlatSymbolRefAttr();
969 }
970 
971 LogicalResult Importer::processFunction(llvm::Function *f) {
972   blocks.clear();
973   instMap.clear();
974   unknownInstMap.clear();
975 
976   auto functionType =
977       processType(f->getFunctionType()).dyn_cast<LLVMFunctionType>();
978   if (!functionType)
979     return failure();
980 
981   b.setInsertionPoint(module.getBody(), getFuncInsertPt());
982   LLVMFuncOp fop =
983       b.create<LLVMFuncOp>(UnknownLoc::get(context), f->getName(), functionType,
984                            convertLinkageFromLLVM(f->getLinkage()));
985 
986   if (FlatSymbolRefAttr personality = getPersonalityAsAttr(f))
987     fop->setAttr(b.getStringAttr("personality"), personality);
988   else if (f->hasPersonalityFn())
989     emitWarning(UnknownLoc::get(context),
990                 "could not deduce personality, skipping it");
991 
992   if (f->hasGC())
993     fop.setGarbageCollectorAttr(b.getStringAttr(f->getGC()));
994 
995   if (f->isDeclaration())
996     return success();
997 
998   // Eagerly create all blocks.
999   SmallVector<Block *, 4> blockList;
1000   for (llvm::BasicBlock &bb : *f) {
1001     blockList.push_back(b.createBlock(&fop.getBody(), fop.getBody().end()));
1002     blocks[&bb] = blockList.back();
1003   }
1004   currentEntryBlock = blockList[0];
1005 
1006   // Add function arguments to the entry block.
1007   for (const auto &kv : llvm::enumerate(f->args())) {
1008     instMap[&kv.value()] = blockList[0]->addArgument(
1009         functionType.getParamType(kv.index()), fop.getLoc());
1010   }
1011 
1012   for (auto bbs : llvm::zip(*f, blockList)) {
1013     if (failed(processBasicBlock(&std::get<0>(bbs), std::get<1>(bbs))))
1014       return failure();
1015   }
1016 
1017   // Now that all instructions are guaranteed to have been visited, ensure
1018   // any unknown uses we encountered are remapped.
1019   for (auto &llvmAndUnknown : unknownInstMap) {
1020     assert(instMap.count(llvmAndUnknown.first));
1021     Value newValue = instMap[llvmAndUnknown.first];
1022     Value oldValue = llvmAndUnknown.second->getResult(0);
1023     oldValue.replaceAllUsesWith(newValue);
1024     llvmAndUnknown.second->erase();
1025   }
1026   return success();
1027 }
1028 
1029 LogicalResult Importer::processBasicBlock(llvm::BasicBlock *bb, Block *block) {
1030   b.setInsertionPointToStart(block);
1031   for (llvm::Instruction &inst : *bb) {
1032     if (failed(processInstruction(&inst)))
1033       return failure();
1034   }
1035   return success();
1036 }
1037 
1038 OwningOpRef<ModuleOp>
1039 mlir::translateLLVMIRToModule(std::unique_ptr<llvm::Module> llvmModule,
1040                               MLIRContext *context) {
1041   context->loadDialect<LLVMDialect>();
1042   context->loadDialect<DLTIDialect>();
1043   OwningOpRef<ModuleOp> module(ModuleOp::create(
1044       FileLineColLoc::get(context, "", /*line=*/0, /*column=*/0)));
1045 
1046   DataLayoutSpecInterface dlSpec =
1047       translateDataLayout(llvmModule->getDataLayout(), context);
1048   if (!dlSpec) {
1049     emitError(UnknownLoc::get(context), "can't translate data layout");
1050     return {};
1051   }
1052 
1053   module.get()->setAttr(DLTIDialect::kDataLayoutAttrName, dlSpec);
1054 
1055   Importer deserializer(context, module.get());
1056   for (llvm::GlobalVariable &gv : llvmModule->globals()) {
1057     if (!deserializer.processGlobal(&gv))
1058       return {};
1059   }
1060   for (llvm::Function &f : llvmModule->functions()) {
1061     if (failed(deserializer.processFunction(&f)))
1062       return {};
1063   }
1064 
1065   return module;
1066 }
1067 
1068 // Deserializes the LLVM bitcode stored in `input` into an MLIR module in the
1069 // LLVM dialect.
1070 OwningOpRef<ModuleOp> translateLLVMIRToModule(llvm::SourceMgr &sourceMgr,
1071                                               MLIRContext *context) {
1072   llvm::SMDiagnostic err;
1073   llvm::LLVMContext llvmContext;
1074   std::unique_ptr<llvm::Module> llvmModule = llvm::parseIR(
1075       *sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID()), err, llvmContext);
1076   if (!llvmModule) {
1077     std::string errStr;
1078     llvm::raw_string_ostream errStream(errStr);
1079     err.print(/*ProgName=*/"", errStream);
1080     emitError(UnknownLoc::get(context)) << errStream.str();
1081     return {};
1082   }
1083   return translateLLVMIRToModule(std::move(llvmModule), context);
1084 }
1085 
1086 namespace mlir {
1087 void registerFromLLVMIRTranslation() {
1088   TranslateToMLIRRegistration fromLLVM(
1089       "import-llvm", [](llvm::SourceMgr &sourceMgr, MLIRContext *context) {
1090         return ::translateLLVMIRToModule(sourceMgr, context);
1091       });
1092 }
1093 } // namespace mlir
1094