1 //===- ModuleTranslation.cpp - MLIR to LLVM 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 the translation between an MLIR LLVM dialect module and
10 // the corresponding LLVMIR module. It only handles core LLVM IR operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
15 
16 #include "DebugTranslation.h"
17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
19 #include "mlir/IR/Attributes.h"
20 #include "mlir/IR/Module.h"
21 #include "mlir/IR/StandardTypes.h"
22 #include "mlir/Support/LLVM.h"
23 #include "llvm/ADT/TypeSwitch.h"
24 
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/IRBuilder.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Transforms/Utils/Cloning.h"
34 
35 using namespace mlir;
36 using namespace mlir::LLVM;
37 using namespace mlir::LLVM::detail;
38 
39 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
40 
41 /// Builds a constant of a sequential LLVM type `type`, potentially containing
42 /// other sequential types recursively, from the individual constant values
43 /// provided in `constants`. `shape` contains the number of elements in nested
44 /// sequential types. Reports errors at `loc` and returns nullptr on error.
45 static llvm::Constant *
46 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
47                         ArrayRef<int64_t> shape, llvm::Type *type,
48                         Location loc) {
49   if (shape.empty()) {
50     llvm::Constant *result = constants.front();
51     constants = constants.drop_front();
52     return result;
53   }
54 
55   llvm::Type *elementType;
56   if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
57     elementType = arrayTy->getElementType();
58   } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
59     elementType = vectorTy->getElementType();
60   } else {
61     emitError(loc) << "expected sequential LLVM types wrapping a scalar";
62     return nullptr;
63   }
64 
65   SmallVector<llvm::Constant *, 8> nested;
66   nested.reserve(shape.front());
67   for (int64_t i = 0; i < shape.front(); ++i) {
68     nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
69                                              elementType, loc));
70     if (!nested.back())
71       return nullptr;
72   }
73 
74   if (shape.size() == 1 && type->isVectorTy())
75     return llvm::ConstantVector::get(nested);
76   return llvm::ConstantArray::get(
77       llvm::ArrayType::get(elementType, shape.front()), nested);
78 }
79 
80 /// Returns the first non-sequential type nested in sequential types.
81 static llvm::Type *getInnermostElementType(llvm::Type *type) {
82   do {
83     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
84       type = arrayTy->getElementType();
85     } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
86       type = vectorTy->getElementType();
87     } else {
88       return type;
89     }
90   } while (1);
91 }
92 
93 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
94 /// This currently supports integer, floating point, splat and dense element
95 /// attributes and combinations thereof.  In case of error, report it to `loc`
96 /// and return nullptr.
97 llvm::Constant *ModuleTranslation::getLLVMConstant(llvm::Type *llvmType,
98                                                    Attribute attr,
99                                                    Location loc) {
100   if (!attr)
101     return llvm::UndefValue::get(llvmType);
102   if (llvmType->isStructTy()) {
103     emitError(loc, "struct types are not supported in constants");
104     return nullptr;
105   }
106   // For integer types, we allow a mismatch in sizes as the index type in
107   // MLIR might have a different size than the index type in the LLVM module.
108   if (auto intAttr = attr.dyn_cast<IntegerAttr>())
109     return llvm::ConstantInt::get(
110         llvmType,
111         intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
112   if (auto floatAttr = attr.dyn_cast<FloatAttr>())
113     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
114   if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
115     return llvm::ConstantExpr::getBitCast(
116         functionMapping.lookup(funcAttr.getValue()), llvmType);
117   if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
118     llvm::Type *elementType;
119     uint64_t numElements;
120     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
121       elementType = arrayTy->getElementType();
122       numElements = arrayTy->getNumElements();
123     } else {
124       auto *vectorTy = cast<llvm::FixedVectorType>(llvmType);
125       elementType = vectorTy->getElementType();
126       numElements = vectorTy->getNumElements();
127     }
128     // Splat value is a scalar. Extract it only if the element type is not
129     // another sequence type. The recursion terminates because each step removes
130     // one outer sequential type.
131     bool elementTypeSequential =
132         isa<llvm::ArrayType, llvm::VectorType>(elementType);
133     llvm::Constant *child = getLLVMConstant(
134         elementType,
135         elementTypeSequential ? splatAttr : splatAttr.getSplatValue(), loc);
136     if (!child)
137       return nullptr;
138     if (llvmType->isVectorTy())
139       return llvm::ConstantVector::getSplat(
140           llvm::ElementCount(numElements, /*Scalable=*/false), child);
141     if (llvmType->isArrayTy()) {
142       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
143       SmallVector<llvm::Constant *, 8> constants(numElements, child);
144       return llvm::ConstantArray::get(arrayType, constants);
145     }
146   }
147 
148   if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) {
149     assert(elementsAttr.getType().hasStaticShape());
150     assert(elementsAttr.getNumElements() != 0 &&
151            "unexpected empty elements attribute");
152     assert(!elementsAttr.getType().getShape().empty() &&
153            "unexpected empty elements attribute shape");
154 
155     SmallVector<llvm::Constant *, 8> constants;
156     constants.reserve(elementsAttr.getNumElements());
157     llvm::Type *innermostType = getInnermostElementType(llvmType);
158     for (auto n : elementsAttr.getValues<Attribute>()) {
159       constants.push_back(getLLVMConstant(innermostType, n, loc));
160       if (!constants.back())
161         return nullptr;
162     }
163     ArrayRef<llvm::Constant *> constantsRef = constants;
164     llvm::Constant *result = buildSequentialConstant(
165         constantsRef, elementsAttr.getType().getShape(), llvmType, loc);
166     assert(constantsRef.empty() && "did not consume all elemental constants");
167     return result;
168   }
169 
170   if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
171     return llvm::ConstantDataArray::get(
172         llvmModule->getContext(), ArrayRef<char>{stringAttr.getValue().data(),
173                                                  stringAttr.getValue().size()});
174   }
175   emitError(loc, "unsupported constant value");
176   return nullptr;
177 }
178 
179 /// Convert MLIR integer comparison predicate to LLVM IR comparison predicate.
180 static llvm::CmpInst::Predicate getLLVMCmpPredicate(ICmpPredicate p) {
181   switch (p) {
182   case LLVM::ICmpPredicate::eq:
183     return llvm::CmpInst::Predicate::ICMP_EQ;
184   case LLVM::ICmpPredicate::ne:
185     return llvm::CmpInst::Predicate::ICMP_NE;
186   case LLVM::ICmpPredicate::slt:
187     return llvm::CmpInst::Predicate::ICMP_SLT;
188   case LLVM::ICmpPredicate::sle:
189     return llvm::CmpInst::Predicate::ICMP_SLE;
190   case LLVM::ICmpPredicate::sgt:
191     return llvm::CmpInst::Predicate::ICMP_SGT;
192   case LLVM::ICmpPredicate::sge:
193     return llvm::CmpInst::Predicate::ICMP_SGE;
194   case LLVM::ICmpPredicate::ult:
195     return llvm::CmpInst::Predicate::ICMP_ULT;
196   case LLVM::ICmpPredicate::ule:
197     return llvm::CmpInst::Predicate::ICMP_ULE;
198   case LLVM::ICmpPredicate::ugt:
199     return llvm::CmpInst::Predicate::ICMP_UGT;
200   case LLVM::ICmpPredicate::uge:
201     return llvm::CmpInst::Predicate::ICMP_UGE;
202   }
203   llvm_unreachable("incorrect comparison predicate");
204 }
205 
206 static llvm::CmpInst::Predicate getLLVMCmpPredicate(FCmpPredicate p) {
207   switch (p) {
208   case LLVM::FCmpPredicate::_false:
209     return llvm::CmpInst::Predicate::FCMP_FALSE;
210   case LLVM::FCmpPredicate::oeq:
211     return llvm::CmpInst::Predicate::FCMP_OEQ;
212   case LLVM::FCmpPredicate::ogt:
213     return llvm::CmpInst::Predicate::FCMP_OGT;
214   case LLVM::FCmpPredicate::oge:
215     return llvm::CmpInst::Predicate::FCMP_OGE;
216   case LLVM::FCmpPredicate::olt:
217     return llvm::CmpInst::Predicate::FCMP_OLT;
218   case LLVM::FCmpPredicate::ole:
219     return llvm::CmpInst::Predicate::FCMP_OLE;
220   case LLVM::FCmpPredicate::one:
221     return llvm::CmpInst::Predicate::FCMP_ONE;
222   case LLVM::FCmpPredicate::ord:
223     return llvm::CmpInst::Predicate::FCMP_ORD;
224   case LLVM::FCmpPredicate::ueq:
225     return llvm::CmpInst::Predicate::FCMP_UEQ;
226   case LLVM::FCmpPredicate::ugt:
227     return llvm::CmpInst::Predicate::FCMP_UGT;
228   case LLVM::FCmpPredicate::uge:
229     return llvm::CmpInst::Predicate::FCMP_UGE;
230   case LLVM::FCmpPredicate::ult:
231     return llvm::CmpInst::Predicate::FCMP_ULT;
232   case LLVM::FCmpPredicate::ule:
233     return llvm::CmpInst::Predicate::FCMP_ULE;
234   case LLVM::FCmpPredicate::une:
235     return llvm::CmpInst::Predicate::FCMP_UNE;
236   case LLVM::FCmpPredicate::uno:
237     return llvm::CmpInst::Predicate::FCMP_UNO;
238   case LLVM::FCmpPredicate::_true:
239     return llvm::CmpInst::Predicate::FCMP_TRUE;
240   }
241   llvm_unreachable("incorrect comparison predicate");
242 }
243 
244 static llvm::AtomicRMWInst::BinOp getLLVMAtomicBinOp(AtomicBinOp op) {
245   switch (op) {
246   case LLVM::AtomicBinOp::xchg:
247     return llvm::AtomicRMWInst::BinOp::Xchg;
248   case LLVM::AtomicBinOp::add:
249     return llvm::AtomicRMWInst::BinOp::Add;
250   case LLVM::AtomicBinOp::sub:
251     return llvm::AtomicRMWInst::BinOp::Sub;
252   case LLVM::AtomicBinOp::_and:
253     return llvm::AtomicRMWInst::BinOp::And;
254   case LLVM::AtomicBinOp::nand:
255     return llvm::AtomicRMWInst::BinOp::Nand;
256   case LLVM::AtomicBinOp::_or:
257     return llvm::AtomicRMWInst::BinOp::Or;
258   case LLVM::AtomicBinOp::_xor:
259     return llvm::AtomicRMWInst::BinOp::Xor;
260   case LLVM::AtomicBinOp::max:
261     return llvm::AtomicRMWInst::BinOp::Max;
262   case LLVM::AtomicBinOp::min:
263     return llvm::AtomicRMWInst::BinOp::Min;
264   case LLVM::AtomicBinOp::umax:
265     return llvm::AtomicRMWInst::BinOp::UMax;
266   case LLVM::AtomicBinOp::umin:
267     return llvm::AtomicRMWInst::BinOp::UMin;
268   case LLVM::AtomicBinOp::fadd:
269     return llvm::AtomicRMWInst::BinOp::FAdd;
270   case LLVM::AtomicBinOp::fsub:
271     return llvm::AtomicRMWInst::BinOp::FSub;
272   }
273   llvm_unreachable("incorrect atomic binary operator");
274 }
275 
276 static llvm::AtomicOrdering getLLVMAtomicOrdering(AtomicOrdering ordering) {
277   switch (ordering) {
278   case LLVM::AtomicOrdering::not_atomic:
279     return llvm::AtomicOrdering::NotAtomic;
280   case LLVM::AtomicOrdering::unordered:
281     return llvm::AtomicOrdering::Unordered;
282   case LLVM::AtomicOrdering::monotonic:
283     return llvm::AtomicOrdering::Monotonic;
284   case LLVM::AtomicOrdering::acquire:
285     return llvm::AtomicOrdering::Acquire;
286   case LLVM::AtomicOrdering::release:
287     return llvm::AtomicOrdering::Release;
288   case LLVM::AtomicOrdering::acq_rel:
289     return llvm::AtomicOrdering::AcquireRelease;
290   case LLVM::AtomicOrdering::seq_cst:
291     return llvm::AtomicOrdering::SequentiallyConsistent;
292   }
293   llvm_unreachable("incorrect atomic ordering");
294 }
295 
296 ModuleTranslation::ModuleTranslation(Operation *module,
297                                      std::unique_ptr<llvm::Module> llvmModule)
298     : mlirModule(module), llvmModule(std::move(llvmModule)),
299       debugTranslation(
300           std::make_unique<DebugTranslation>(module, *this->llvmModule)),
301       ompDialect(
302           module->getContext()->getRegisteredDialect<omp::OpenMPDialect>()),
303       llvmDialect(module->getContext()->getRegisteredDialect<LLVMDialect>()) {
304   assert(satisfiesLLVMModule(mlirModule) &&
305          "mlirModule should honor LLVM's module semantics.");
306 }
307 ModuleTranslation::~ModuleTranslation() {}
308 
309 /// Given an OpenMP MLIR operation, create the corresponding LLVM IR
310 /// (including OpenMP runtime calls).
311 LogicalResult
312 ModuleTranslation::convertOmpOperation(Operation &opInst,
313                                        llvm::IRBuilder<> &builder) {
314   if (!ompBuilder) {
315     ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
316     ompBuilder->initialize();
317   }
318   return llvm::TypeSwitch<Operation *, LogicalResult>(&opInst)
319       .Case([&](omp::BarrierOp) {
320         ompBuilder->CreateBarrier(builder.saveIP(), llvm::omp::OMPD_barrier);
321         return success();
322       })
323       .Case([&](omp::TaskwaitOp) {
324         ompBuilder->CreateTaskwait(builder.saveIP());
325         return success();
326       })
327       .Case([&](omp::TaskyieldOp) {
328         ompBuilder->CreateTaskyield(builder.saveIP());
329         return success();
330       })
331       .Case([&](omp::FlushOp) {
332         // No support in Openmp runtime funciton (__kmpc_flush) to accept
333         // the argument list.
334         // OpenMP standard states the following:
335         //  "An implementation may implement a flush with a list by ignoring
336         //   the list, and treating it the same as a flush without a list."
337         //
338         // The argument list is discarded so that, flush with a list is treated
339         // same as a flush without a list.
340         ompBuilder->CreateFlush(builder.saveIP());
341         return success();
342       })
343       .Default([&](Operation *inst) {
344         return inst->emitError("unsupported OpenMP operation: ")
345                << inst->getName();
346       });
347 }
348 
349 /// Given a single MLIR operation, create the corresponding LLVM IR operation
350 /// using the `builder`.  LLVM IR Builder does not have a generic interface so
351 /// this has to be a long chain of `if`s calling different functions with a
352 /// different number of arguments.
353 LogicalResult ModuleTranslation::convertOperation(Operation &opInst,
354                                                   llvm::IRBuilder<> &builder) {
355   auto extractPosition = [](ArrayAttr attr) {
356     SmallVector<unsigned, 4> position;
357     position.reserve(attr.size());
358     for (Attribute v : attr)
359       position.push_back(v.cast<IntegerAttr>().getValue().getZExtValue());
360     return position;
361   };
362 
363 #include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
364 
365   // Emit function calls.  If the "callee" attribute is present, this is a
366   // direct function call and we also need to look up the remapped function
367   // itself.  Otherwise, this is an indirect call and the callee is the first
368   // operand, look it up as a normal value.  Return the llvm::Value representing
369   // the function result, which may be of llvm::VoidTy type.
370   auto convertCall = [this, &builder](Operation &op) -> llvm::Value * {
371     auto operands = lookupValues(op.getOperands());
372     ArrayRef<llvm::Value *> operandsRef(operands);
373     if (auto attr = op.getAttrOfType<FlatSymbolRefAttr>("callee")) {
374       return builder.CreateCall(functionMapping.lookup(attr.getValue()),
375                                 operandsRef);
376     } else {
377       auto *calleePtrType =
378           cast<llvm::PointerType>(operandsRef.front()->getType());
379       auto *calleeType =
380           cast<llvm::FunctionType>(calleePtrType->getElementType());
381       return builder.CreateCall(calleeType, operandsRef.front(),
382                                 operandsRef.drop_front());
383     }
384   };
385 
386   // Emit calls.  If the called function has a result, remap the corresponding
387   // value.  Note that LLVM IR dialect CallOp has either 0 or 1 result.
388   if (isa<LLVM::CallOp>(opInst)) {
389     llvm::Value *result = convertCall(opInst);
390     if (opInst.getNumResults() != 0) {
391       valueMapping[opInst.getResult(0)] = result;
392       return success();
393     }
394     // Check that LLVM call returns void for 0-result functions.
395     return success(result->getType()->isVoidTy());
396   }
397 
398   if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
399     auto operands = lookupValues(opInst.getOperands());
400     ArrayRef<llvm::Value *> operandsRef(operands);
401     if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
402       builder.CreateInvoke(functionMapping.lookup(attr.getValue()),
403                            blockMapping[invOp.getSuccessor(0)],
404                            blockMapping[invOp.getSuccessor(1)], operandsRef);
405     } else {
406       auto *calleePtrType =
407           cast<llvm::PointerType>(operandsRef.front()->getType());
408       auto *calleeType =
409           cast<llvm::FunctionType>(calleePtrType->getElementType());
410       builder.CreateInvoke(
411           calleeType, operandsRef.front(), blockMapping[invOp.getSuccessor(0)],
412           blockMapping[invOp.getSuccessor(1)], operandsRef.drop_front());
413     }
414     return success();
415   }
416 
417   if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
418     llvm::Type *ty = lpOp.getType().dyn_cast<LLVMType>().getUnderlyingType();
419     llvm::LandingPadInst *lpi =
420         builder.CreateLandingPad(ty, lpOp.getNumOperands());
421 
422     // Add clauses
423     for (auto operand : lookupValues(lpOp.getOperands())) {
424       // All operands should be constant - checked by verifier
425       if (auto constOperand = dyn_cast<llvm::Constant>(operand))
426         lpi->addClause(constOperand);
427     }
428     valueMapping[lpOp.getResult()] = lpi;
429     return success();
430   }
431 
432   // Emit branches.  We need to look up the remapped blocks and ignore the block
433   // arguments that were transformed into PHI nodes.
434   if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
435     builder.CreateBr(blockMapping[brOp.getSuccessor()]);
436     return success();
437   }
438   if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
439     builder.CreateCondBr(valueMapping.lookup(condbrOp.getOperand(0)),
440                          blockMapping[condbrOp.getSuccessor(0)],
441                          blockMapping[condbrOp.getSuccessor(1)]);
442     return success();
443   }
444 
445   // Emit addressof.  We need to look up the global value referenced by the
446   // operation and store it in the MLIR-to-LLVM value mapping.  This does not
447   // emit any LLVM instruction.
448   if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
449     LLVM::GlobalOp global = addressOfOp.getGlobal();
450     LLVM::LLVMFuncOp function = addressOfOp.getFunction();
451 
452     // The verifier should not have allowed this.
453     assert((global || function) &&
454            "referencing an undefined global or function");
455 
456     valueMapping[addressOfOp.getResult()] =
457         global ? globalsMapping.lookup(global)
458                : functionMapping.lookup(function.getName());
459     return success();
460   }
461 
462   if (opInst.getDialect() == ompDialect) {
463     return convertOmpOperation(opInst, builder);
464   }
465 
466   return opInst.emitError("unsupported or non-LLVM operation: ")
467          << opInst.getName();
468 }
469 
470 /// Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
471 /// to define values corresponding to the MLIR block arguments.  These nodes
472 /// are not connected to the source basic blocks, which may not exist yet.
473 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments) {
474   llvm::IRBuilder<> builder(blockMapping[&bb]);
475   auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
476 
477   // Before traversing operations, make block arguments available through
478   // value remapping and PHI nodes, but do not add incoming edges for the PHI
479   // nodes just yet: those values may be defined by this or following blocks.
480   // This step is omitted if "ignoreArguments" is set.  The arguments of the
481   // first block have been already made available through the remapping of
482   // LLVM function arguments.
483   if (!ignoreArguments) {
484     auto predecessors = bb.getPredecessors();
485     unsigned numPredecessors =
486         std::distance(predecessors.begin(), predecessors.end());
487     for (auto arg : bb.getArguments()) {
488       auto wrappedType = arg.getType().dyn_cast<LLVM::LLVMType>();
489       if (!wrappedType)
490         return emitError(bb.front().getLoc(),
491                          "block argument does not have an LLVM type");
492       llvm::Type *type = wrappedType.getUnderlyingType();
493       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
494       valueMapping[arg] = phi;
495     }
496   }
497 
498   // Traverse operations.
499   for (auto &op : bb) {
500     // Set the current debug location within the builder.
501     builder.SetCurrentDebugLocation(
502         debugTranslation->translateLoc(op.getLoc(), subprogram));
503 
504     if (failed(convertOperation(op, builder)))
505       return failure();
506   }
507 
508   return success();
509 }
510 
511 /// Create named global variables that correspond to llvm.mlir.global
512 /// definitions.
513 LogicalResult ModuleTranslation::convertGlobals() {
514   // Lock access to the llvm context.
515   llvm::sys::SmartScopedLock<true> scopedLock(
516       llvmDialect->getLLVMContextMutex());
517   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
518     llvm::Type *type = op.getType().getUnderlyingType();
519     llvm::Constant *cst = llvm::UndefValue::get(type);
520     if (op.getValueOrNull()) {
521       // String attributes are treated separately because they cannot appear as
522       // in-function constants and are thus not supported by getLLVMConstant.
523       if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
524         cst = llvm::ConstantDataArray::getString(
525             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
526         type = cst->getType();
527       } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(),
528                                          op.getLoc()))) {
529         return failure();
530       }
531     } else if (Block *initializer = op.getInitializerBlock()) {
532       llvm::IRBuilder<> builder(llvmModule->getContext());
533       for (auto &op : initializer->without_terminator()) {
534         if (failed(convertOperation(op, builder)) ||
535             !isa<llvm::Constant>(valueMapping.lookup(op.getResult(0))))
536           return emitError(op.getLoc(), "unemittable constant value");
537       }
538       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
539       cst = cast<llvm::Constant>(valueMapping.lookup(ret.getOperand(0)));
540     }
541 
542     auto linkage = convertLinkageToLLVM(op.linkage());
543     bool anyExternalLinkage =
544         ((linkage == llvm::GlobalVariable::ExternalLinkage &&
545           isa<llvm::UndefValue>(cst)) ||
546          linkage == llvm::GlobalVariable::ExternalWeakLinkage);
547     auto addrSpace = op.addr_space().getLimitedValue();
548     auto *var = new llvm::GlobalVariable(
549         *llvmModule, type, op.constant(), linkage,
550         anyExternalLinkage ? nullptr : cst, op.sym_name(),
551         /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace);
552 
553     globalsMapping.try_emplace(op, var);
554   }
555 
556   return success();
557 }
558 
559 /// Get the SSA value passed to the current block from the terminator operation
560 /// of its predecessor.
561 static Value getPHISourceValue(Block *current, Block *pred,
562                                unsigned numArguments, unsigned index) {
563   auto &terminator = *pred->getTerminator();
564   if (isa<LLVM::BrOp>(terminator)) {
565     return terminator.getOperand(index);
566   }
567 
568   // For conditional branches, we need to check if the current block is reached
569   // through the "true" or the "false" branch and take the relevant operands.
570   auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator);
571   assert(condBranchOp &&
572          "only branch operations can be terminators of a block that "
573          "has successors");
574   assert((condBranchOp.getSuccessor(0) != condBranchOp.getSuccessor(1)) &&
575          "successors with arguments in LLVM conditional branches must be "
576          "different blocks");
577 
578   return condBranchOp.getSuccessor(0) == current
579              ? condBranchOp.trueDestOperands()[index]
580              : condBranchOp.falseDestOperands()[index];
581 }
582 
583 void ModuleTranslation::connectPHINodes(LLVMFuncOp func) {
584   // Skip the first block, it cannot be branched to and its arguments correspond
585   // to the arguments of the LLVM function.
586   for (auto it = std::next(func.begin()), eit = func.end(); it != eit; ++it) {
587     Block *bb = &*it;
588     llvm::BasicBlock *llvmBB = blockMapping.lookup(bb);
589     auto phis = llvmBB->phis();
590     auto numArguments = bb->getNumArguments();
591     assert(numArguments == std::distance(phis.begin(), phis.end()));
592     for (auto &numberedPhiNode : llvm::enumerate(phis)) {
593       auto &phiNode = numberedPhiNode.value();
594       unsigned index = numberedPhiNode.index();
595       for (auto *pred : bb->getPredecessors()) {
596         phiNode.addIncoming(valueMapping.lookup(getPHISourceValue(
597                                 bb, pred, numArguments, index)),
598                             blockMapping.lookup(pred));
599       }
600     }
601   }
602 }
603 
604 // TODO(mlir-team): implement an iterative version
605 static void topologicalSortImpl(llvm::SetVector<Block *> &blocks, Block *b) {
606   blocks.insert(b);
607   for (Block *bb : b->getSuccessors()) {
608     if (blocks.count(bb) == 0)
609       topologicalSortImpl(blocks, bb);
610   }
611 }
612 
613 /// Sort function blocks topologically.
614 static llvm::SetVector<Block *> topologicalSort(LLVMFuncOp f) {
615   // For each blocks that has not been visited yet (i.e. that has no
616   // predecessors), add it to the list and traverse its successors in DFS
617   // preorder.
618   llvm::SetVector<Block *> blocks;
619   for (Block &b : f) {
620     if (blocks.count(&b) == 0)
621       topologicalSortImpl(blocks, &b);
622   }
623   assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
624 
625   return blocks;
626 }
627 
628 /// Attempts to add an attribute identified by `key`, optionally with the given
629 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the
630 /// attribute has a kind known to LLVM IR, create the attribute of this kind,
631 /// otherwise keep it as a string attribute. Performs additional checks for
632 /// attributes known to have or not have a value in order to avoid assertions
633 /// inside LLVM upon construction.
634 static LogicalResult checkedAddLLVMFnAttribute(Location loc,
635                                                llvm::Function *llvmFunc,
636                                                StringRef key,
637                                                StringRef value = StringRef()) {
638   auto kind = llvm::Attribute::getAttrKindFromName(key);
639   if (kind == llvm::Attribute::None) {
640     llvmFunc->addFnAttr(key, value);
641     return success();
642   }
643 
644   if (llvm::Attribute::doesAttrKindHaveArgument(kind)) {
645     if (value.empty())
646       return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
647 
648     int result;
649     if (!value.getAsInteger(/*Radix=*/0, result))
650       llvmFunc->addFnAttr(
651           llvm::Attribute::get(llvmFunc->getContext(), kind, result));
652     else
653       llvmFunc->addFnAttr(key, value);
654     return success();
655   }
656 
657   if (!value.empty())
658     return emitError(loc) << "LLVM attribute '" << key
659                           << "' does not expect a value, found '" << value
660                           << "'";
661 
662   llvmFunc->addFnAttr(kind);
663   return success();
664 }
665 
666 /// Attaches the attributes listed in the given array attribute to `llvmFunc`.
667 /// Reports error to `loc` if any and returns immediately. Expects `attributes`
668 /// to be an array attribute containing either string attributes, treated as
669 /// value-less LLVM attributes, or array attributes containing two string
670 /// attributes, with the first string being the name of the corresponding LLVM
671 /// attribute and the second string beings its value. Note that even integer
672 /// attributes are expected to have their values expressed as strings.
673 static LogicalResult
674 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes,
675                              llvm::Function *llvmFunc) {
676   if (!attributes)
677     return success();
678 
679   for (Attribute attr : *attributes) {
680     if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
681       if (failed(
682               checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue())))
683         return failure();
684       continue;
685     }
686 
687     auto arrayAttr = attr.dyn_cast<ArrayAttr>();
688     if (!arrayAttr || arrayAttr.size() != 2)
689       return emitError(loc)
690              << "expected 'passthrough' to contain string or array attributes";
691 
692     auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>();
693     auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>();
694     if (!keyAttr || !valueAttr)
695       return emitError(loc)
696              << "expected arrays within 'passthrough' to contain two strings";
697 
698     if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(),
699                                          valueAttr.getValue())))
700       return failure();
701   }
702   return success();
703 }
704 
705 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
706   // Clear the block and value mappings, they are only relevant within one
707   // function.
708   blockMapping.clear();
709   valueMapping.clear();
710   llvm::Function *llvmFunc = functionMapping.lookup(func.getName());
711 
712   // Translate the debug information for this function.
713   debugTranslation->translate(func, *llvmFunc);
714 
715   // Add function arguments to the value remapping table.
716   // If there was noalias info then we decorate each argument accordingly.
717   unsigned int argIdx = 0;
718   for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) {
719     llvm::Argument &llvmArg = std::get<1>(kvp);
720     BlockArgument mlirArg = std::get<0>(kvp);
721 
722     if (auto attr = func.getArgAttrOfType<BoolAttr>(argIdx, "llvm.noalias")) {
723       // NB: Attribute already verified to be boolean, so check if we can indeed
724       // attach the attribute to this argument, based on its type.
725       auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
726       if (!argTy.getUnderlyingType()->isPointerTy())
727         return func.emitError(
728             "llvm.noalias attribute attached to LLVM non-pointer argument");
729       if (attr.getValue())
730         llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
731     }
732 
733     if (auto attr = func.getArgAttrOfType<IntegerAttr>(argIdx, "llvm.align")) {
734       // NB: Attribute already verified to be int, so check if we can indeed
735       // attach the attribute to this argument, based on its type.
736       auto argTy = mlirArg.getType().dyn_cast<LLVM::LLVMType>();
737       if (!argTy.getUnderlyingType()->isPointerTy())
738         return func.emitError(
739             "llvm.align attribute attached to LLVM non-pointer argument");
740       llvmArg.addAttrs(
741           llvm::AttrBuilder().addAlignmentAttr(llvm::Align(attr.getInt())));
742     }
743 
744     valueMapping[mlirArg] = &llvmArg;
745     argIdx++;
746   }
747 
748   // Check the personality and set it.
749   if (func.personality().hasValue()) {
750     llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext());
751     if (llvm::Constant *pfunc =
752             getLLVMConstant(ty, func.personalityAttr(), func.getLoc()))
753       llvmFunc->setPersonalityFn(pfunc);
754   }
755 
756   // First, create all blocks so we can jump to them.
757   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
758   for (auto &bb : func) {
759     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
760     llvmBB->insertInto(llvmFunc);
761     blockMapping[&bb] = llvmBB;
762   }
763 
764   // Then, convert blocks one by one in topological order to ensure defs are
765   // converted before uses.
766   auto blocks = topologicalSort(func);
767   for (auto indexedBB : llvm::enumerate(blocks)) {
768     auto *bb = indexedBB.value();
769     if (failed(convertBlock(*bb, /*ignoreArguments=*/indexedBB.index() == 0)))
770       return failure();
771   }
772 
773   // Finally, after all blocks have been traversed and values mapped, connect
774   // the PHI nodes to the results of preceding blocks.
775   connectPHINodes(func);
776   return success();
777 }
778 
779 LogicalResult ModuleTranslation::checkSupportedModuleOps(Operation *m) {
780   for (Operation &o : getModuleBody(m).getOperations())
781     if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp>(&o) && !o.isKnownTerminator())
782       return o.emitOpError("unsupported module-level operation");
783   return success();
784 }
785 
786 LogicalResult ModuleTranslation::convertFunctions() {
787   // Lock access to the llvm context.
788   llvm::sys::SmartScopedLock<true> scopedLock(
789       llvmDialect->getLLVMContextMutex());
790   // Declare all functions first because there may be function calls that form a
791   // call graph with cycles.
792   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
793     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
794         function.getName(),
795         cast<llvm::FunctionType>(function.getType().getUnderlyingType()));
796     llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
797     functionMapping[function.getName()] = llvmFunc;
798 
799     // Forward the pass-through attributes to LLVM.
800     if (failed(forwardPassthroughAttributes(function.getLoc(),
801                                             function.passthrough(), llvmFunc)))
802       return failure();
803   }
804 
805   // Convert functions.
806   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
807     // Ignore external functions.
808     if (function.isExternal())
809       continue;
810 
811     if (failed(convertOneFunction(function)))
812       return failure();
813   }
814 
815   return success();
816 }
817 
818 /// A helper to look up remapped operands in the value remapping table.`
819 SmallVector<llvm::Value *, 8>
820 ModuleTranslation::lookupValues(ValueRange values) {
821   SmallVector<llvm::Value *, 8> remapped;
822   remapped.reserve(values.size());
823   for (Value v : values) {
824     assert(valueMapping.count(v) && "referencing undefined value");
825     remapped.push_back(valueMapping.lookup(v));
826   }
827   return remapped;
828 }
829 
830 std::unique_ptr<llvm::Module>
831 ModuleTranslation::prepareLLVMModule(Operation *m) {
832   auto *dialect = m->getContext()->getRegisteredDialect<LLVM::LLVMDialect>();
833   assert(dialect && "LLVM dialect must be registered");
834   // Lock the LLVM context as we might create new types here.
835   llvm::sys::SmartScopedLock<true> scopedLock(dialect->getLLVMContextMutex());
836 
837   auto llvmModule = llvm::CloneModule(dialect->getLLVMModule());
838   if (!llvmModule)
839     return nullptr;
840 
841   llvm::LLVMContext &llvmContext = llvmModule->getContext();
842   llvm::IRBuilder<> builder(llvmContext);
843 
844   // Inject declarations for `malloc` and `free` functions that can be used in
845   // memref allocation/deallocation coming from standard ops lowering.
846   llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
847                                   builder.getInt64Ty());
848   llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
849                                   builder.getInt8PtrTy());
850 
851   return llvmModule;
852 }
853