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/LLVMIR/Transforms/LegalizeForExport.h"
19 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
20 #include "mlir/IR/Attributes.h"
21 #include "mlir/IR/BuiltinOps.h"
22 #include "mlir/IR/BuiltinTypes.h"
23 #include "mlir/IR/RegionGraphTraits.h"
24 #include "mlir/Support/LLVM.h"
25 #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
26 #include "mlir/Target/LLVMIR/TypeToLLVM.h"
27 #include "llvm/ADT/TypeSwitch.h"
28 
29 #include "llvm/ADT/PostOrderIterator.h"
30 #include "llvm/ADT/SetVector.h"
31 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
32 #include "llvm/IR/BasicBlock.h"
33 #include "llvm/IR/CFG.h"
34 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/IRBuilder.h"
37 #include "llvm/IR/InlineAsm.h"
38 #include "llvm/IR/IntrinsicsNVPTX.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/IR/MDBuilder.h"
41 #include "llvm/IR/Module.h"
42 #include "llvm/IR/Verifier.h"
43 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
44 #include "llvm/Transforms/Utils/Cloning.h"
45 #include "llvm/Transforms/Utils/ModuleUtils.h"
46 
47 using namespace mlir;
48 using namespace mlir::LLVM;
49 using namespace mlir::LLVM::detail;
50 
51 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
52 
53 /// Builds a constant of a sequential LLVM type `type`, potentially containing
54 /// other sequential types recursively, from the individual constant values
55 /// provided in `constants`. `shape` contains the number of elements in nested
56 /// sequential types. Reports errors at `loc` and returns nullptr on error.
57 static llvm::Constant *
58 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
59                         ArrayRef<int64_t> shape, llvm::Type *type,
60                         Location loc) {
61   if (shape.empty()) {
62     llvm::Constant *result = constants.front();
63     constants = constants.drop_front();
64     return result;
65   }
66 
67   llvm::Type *elementType;
68   if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
69     elementType = arrayTy->getElementType();
70   } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
71     elementType = vectorTy->getElementType();
72   } else {
73     emitError(loc) << "expected sequential LLVM types wrapping a scalar";
74     return nullptr;
75   }
76 
77   SmallVector<llvm::Constant *, 8> nested;
78   nested.reserve(shape.front());
79   for (int64_t i = 0; i < shape.front(); ++i) {
80     nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
81                                              elementType, loc));
82     if (!nested.back())
83       return nullptr;
84   }
85 
86   if (shape.size() == 1 && type->isVectorTy())
87     return llvm::ConstantVector::get(nested);
88   return llvm::ConstantArray::get(
89       llvm::ArrayType::get(elementType, shape.front()), nested);
90 }
91 
92 /// Returns the first non-sequential type nested in sequential types.
93 static llvm::Type *getInnermostElementType(llvm::Type *type) {
94   do {
95     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
96       type = arrayTy->getElementType();
97     } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
98       type = vectorTy->getElementType();
99     } else {
100       return type;
101     }
102   } while (true);
103 }
104 
105 /// Convert a dense elements attribute to an LLVM IR constant using its raw data
106 /// storage if possible. This supports elements attributes of tensor or vector
107 /// type and avoids constructing separate objects for individual values of the
108 /// innermost dimension. Constants for other dimensions are still constructed
109 /// recursively. Returns null if constructing from raw data is not supported for
110 /// this type, e.g., element type is not a power-of-two-sized primitive. Reports
111 /// other errors at `loc`.
112 static llvm::Constant *
113 convertDenseElementsAttr(Location loc, DenseElementsAttr denseElementsAttr,
114                          llvm::Type *llvmType,
115                          const ModuleTranslation &moduleTranslation) {
116   if (!denseElementsAttr)
117     return nullptr;
118 
119   llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
120   if (!llvm::ConstantDataSequential::isElementTypeCompatible(innermostLLVMType))
121     return nullptr;
122 
123   ShapedType type = denseElementsAttr.getType();
124   if (type.getNumElements() == 0)
125     return nullptr;
126 
127   // Compute the shape of all dimensions but the innermost. Note that the
128   // innermost dimension may be that of the vector element type.
129   bool hasVectorElementType = type.getElementType().isa<VectorType>();
130   unsigned numAggregates =
131       denseElementsAttr.getNumElements() /
132       (hasVectorElementType ? 1
133                             : denseElementsAttr.getType().getShape().back());
134   ArrayRef<int64_t> outerShape = type.getShape();
135   if (!hasVectorElementType)
136     outerShape = outerShape.drop_back();
137 
138   // Handle the case of vector splat, LLVM has special support for it.
139   if (denseElementsAttr.isSplat() &&
140       (type.isa<VectorType>() || hasVectorElementType)) {
141     llvm::Constant *splatValue = LLVM::detail::getLLVMConstant(
142         innermostLLVMType, denseElementsAttr.getSplatValue<Attribute>(), loc,
143         moduleTranslation, /*isTopLevel=*/false);
144     llvm::Constant *splatVector =
145         llvm::ConstantDataVector::getSplat(0, splatValue);
146     SmallVector<llvm::Constant *> constants(numAggregates, splatVector);
147     ArrayRef<llvm::Constant *> constantsRef = constants;
148     return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
149   }
150   if (denseElementsAttr.isSplat())
151     return nullptr;
152 
153   // In case of non-splat, create a constructor for the innermost constant from
154   // a piece of raw data.
155   std::function<llvm::Constant *(StringRef)> buildCstData;
156   if (type.isa<TensorType>()) {
157     auto vectorElementType = type.getElementType().dyn_cast<VectorType>();
158     if (vectorElementType && vectorElementType.getRank() == 1) {
159       buildCstData = [&](StringRef data) {
160         return llvm::ConstantDataVector::getRaw(
161             data, vectorElementType.getShape().back(), innermostLLVMType);
162       };
163     } else if (!vectorElementType) {
164       buildCstData = [&](StringRef data) {
165         return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
166                                                innermostLLVMType);
167       };
168     }
169   } else if (type.isa<VectorType>()) {
170     buildCstData = [&](StringRef data) {
171       return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
172                                               innermostLLVMType);
173     };
174   }
175   if (!buildCstData)
176     return nullptr;
177 
178   // Create innermost constants and defer to the default constant creation
179   // mechanism for other dimensions.
180   SmallVector<llvm::Constant *> constants;
181   unsigned aggregateSize = denseElementsAttr.getType().getShape().back() *
182                            (innermostLLVMType->getScalarSizeInBits() / 8);
183   constants.reserve(numAggregates);
184   for (unsigned i = 0; i < numAggregates; ++i) {
185     StringRef data(denseElementsAttr.getRawData().data() + i * aggregateSize,
186                    aggregateSize);
187     constants.push_back(buildCstData(data));
188   }
189 
190   ArrayRef<llvm::Constant *> constantsRef = constants;
191   return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
192 }
193 
194 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
195 /// This currently supports integer, floating point, splat and dense element
196 /// attributes and combinations thereof. Also, an array attribute with two
197 /// elements is supported to represent a complex constant.  In case of error,
198 /// report it to `loc` and return nullptr.
199 llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
200     llvm::Type *llvmType, Attribute attr, Location loc,
201     const ModuleTranslation &moduleTranslation, bool isTopLevel) {
202   if (!attr)
203     return llvm::UndefValue::get(llvmType);
204   if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) {
205     if (!isTopLevel) {
206       emitError(loc, "nested struct types are not supported in constants");
207       return nullptr;
208     }
209     auto arrayAttr = attr.cast<ArrayAttr>();
210     llvm::Type *elementType = structType->getElementType(0);
211     llvm::Constant *real = getLLVMConstant(elementType, arrayAttr[0], loc,
212                                            moduleTranslation, false);
213     if (!real)
214       return nullptr;
215     llvm::Constant *imag = getLLVMConstant(elementType, arrayAttr[1], loc,
216                                            moduleTranslation, false);
217     if (!imag)
218       return nullptr;
219     return llvm::ConstantStruct::get(structType, {real, imag});
220   }
221   // For integer types, we allow a mismatch in sizes as the index type in
222   // MLIR might have a different size than the index type in the LLVM module.
223   if (auto intAttr = attr.dyn_cast<IntegerAttr>())
224     return llvm::ConstantInt::get(
225         llvmType,
226         intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
227   if (auto floatAttr = attr.dyn_cast<FloatAttr>()) {
228     if (llvmType !=
229         llvm::Type::getFloatingPointTy(llvmType->getContext(),
230                                        floatAttr.getValue().getSemantics())) {
231       emitError(loc, "FloatAttr does not match expected type of the constant");
232       return nullptr;
233     }
234     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
235   }
236   if (auto funcAttr = attr.dyn_cast<FlatSymbolRefAttr>())
237     return llvm::ConstantExpr::getBitCast(
238         moduleTranslation.lookupFunction(funcAttr.getValue()), llvmType);
239   if (auto splatAttr = attr.dyn_cast<SplatElementsAttr>()) {
240     llvm::Type *elementType;
241     uint64_t numElements;
242     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
243       elementType = arrayTy->getElementType();
244       numElements = arrayTy->getNumElements();
245     } else if (auto *fVectorTy = dyn_cast<llvm::FixedVectorType>(llvmType)) {
246       elementType = fVectorTy->getElementType();
247       numElements = fVectorTy->getNumElements();
248     } else if (auto *sVectorTy = dyn_cast<llvm::ScalableVectorType>(llvmType)) {
249       elementType = sVectorTy->getElementType();
250       numElements = sVectorTy->getMinNumElements();
251     } else {
252       llvm_unreachable("unrecognized constant vector type");
253     }
254     // Splat value is a scalar. Extract it only if the element type is not
255     // another sequence type. The recursion terminates because each step removes
256     // one outer sequential type.
257     bool elementTypeSequential =
258         isa<llvm::ArrayType, llvm::VectorType>(elementType);
259     llvm::Constant *child = getLLVMConstant(
260         elementType,
261         elementTypeSequential ? splatAttr
262                               : splatAttr.getSplatValue<Attribute>(),
263         loc, moduleTranslation, false);
264     if (!child)
265       return nullptr;
266     if (llvmType->isVectorTy())
267       return llvm::ConstantVector::getSplat(
268           llvm::ElementCount::get(numElements, /*Scalable=*/false), child);
269     if (llvmType->isArrayTy()) {
270       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
271       SmallVector<llvm::Constant *, 8> constants(numElements, child);
272       return llvm::ConstantArray::get(arrayType, constants);
273     }
274   }
275 
276   // Try using raw elements data if possible.
277   if (llvm::Constant *result =
278           convertDenseElementsAttr(loc, attr.dyn_cast<DenseElementsAttr>(),
279                                    llvmType, moduleTranslation)) {
280     return result;
281   }
282 
283   // Fall back to element-by-element construction otherwise.
284   if (auto elementsAttr = attr.dyn_cast<ElementsAttr>()) {
285     assert(elementsAttr.getType().hasStaticShape());
286     assert(!elementsAttr.getType().getShape().empty() &&
287            "unexpected empty elements attribute shape");
288 
289     SmallVector<llvm::Constant *, 8> constants;
290     constants.reserve(elementsAttr.getNumElements());
291     llvm::Type *innermostType = getInnermostElementType(llvmType);
292     for (auto n : elementsAttr.getValues<Attribute>()) {
293       constants.push_back(
294           getLLVMConstant(innermostType, n, loc, moduleTranslation, false));
295       if (!constants.back())
296         return nullptr;
297     }
298     ArrayRef<llvm::Constant *> constantsRef = constants;
299     llvm::Constant *result = buildSequentialConstant(
300         constantsRef, elementsAttr.getType().getShape(), llvmType, loc);
301     assert(constantsRef.empty() && "did not consume all elemental constants");
302     return result;
303   }
304 
305   if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
306     return llvm::ConstantDataArray::get(
307         moduleTranslation.getLLVMContext(),
308         ArrayRef<char>{stringAttr.getValue().data(),
309                        stringAttr.getValue().size()});
310   }
311   emitError(loc, "unsupported constant value");
312   return nullptr;
313 }
314 
315 ModuleTranslation::ModuleTranslation(Operation *module,
316                                      std::unique_ptr<llvm::Module> llvmModule)
317     : mlirModule(module), llvmModule(std::move(llvmModule)),
318       debugTranslation(
319           std::make_unique<DebugTranslation>(module, *this->llvmModule)),
320       typeTranslator(this->llvmModule->getContext()),
321       iface(module->getContext()) {
322   assert(satisfiesLLVMModule(mlirModule) &&
323          "mlirModule should honor LLVM's module semantics.");
324 }
325 ModuleTranslation::~ModuleTranslation() {
326   if (ompBuilder)
327     ompBuilder->finalize();
328 }
329 
330 void ModuleTranslation::forgetMapping(Region &region) {
331   SmallVector<Region *> toProcess;
332   toProcess.push_back(&region);
333   while (!toProcess.empty()) {
334     Region *current = toProcess.pop_back_val();
335     for (Block &block : *current) {
336       blockMapping.erase(&block);
337       for (Value arg : block.getArguments())
338         valueMapping.erase(arg);
339       for (Operation &op : block) {
340         for (Value value : op.getResults())
341           valueMapping.erase(value);
342         if (op.hasSuccessors())
343           branchMapping.erase(&op);
344         if (isa<LLVM::GlobalOp>(op))
345           globalsMapping.erase(&op);
346         accessGroupMetadataMapping.erase(&op);
347         llvm::append_range(
348             toProcess,
349             llvm::map_range(op.getRegions(), [](Region &r) { return &r; }));
350       }
351     }
352   }
353 }
354 
355 /// Get the SSA value passed to the current block from the terminator operation
356 /// of its predecessor.
357 static Value getPHISourceValue(Block *current, Block *pred,
358                                unsigned numArguments, unsigned index) {
359   Operation &terminator = *pred->getTerminator();
360   if (isa<LLVM::BrOp>(terminator))
361     return terminator.getOperand(index);
362 
363   SuccessorRange successors = terminator.getSuccessors();
364   assert(std::adjacent_find(successors.begin(), successors.end()) ==
365              successors.end() &&
366          "successors with arguments in LLVM branches must be different blocks");
367   (void)successors;
368 
369   // For instructions that branch based on a condition value, we need to take
370   // the operands for the branch that was taken.
371   if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) {
372     // For conditional branches, we take the operands from either the "true" or
373     // the "false" branch.
374     return condBranchOp.getSuccessor(0) == current
375                ? condBranchOp.getTrueDestOperands()[index]
376                : condBranchOp.getFalseDestOperands()[index];
377   }
378 
379   if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) {
380     // For switches, we take the operands from either the default case, or from
381     // the case branch that was taken.
382     if (switchOp.getDefaultDestination() == current)
383       return switchOp.getDefaultOperands()[index];
384     for (const auto &i : llvm::enumerate(switchOp.getCaseDestinations()))
385       if (i.value() == current)
386         return switchOp.getCaseOperands(i.index())[index];
387   }
388 
389   if (auto invokeOp = dyn_cast<LLVM::InvokeOp>(terminator)) {
390     return invokeOp.getNormalDest() == current
391                ? invokeOp.getNormalDestOperands()[index]
392                : invokeOp.getUnwindDestOperands()[index];
393   }
394 
395   llvm_unreachable(
396       "only branch, switch or invoke operations can be terminators "
397       "of a block that has successors");
398 }
399 
400 /// Connect the PHI nodes to the results of preceding blocks.
401 void mlir::LLVM::detail::connectPHINodes(Region &region,
402                                          const ModuleTranslation &state) {
403   // Skip the first block, it cannot be branched to and its arguments correspond
404   // to the arguments of the LLVM function.
405   for (auto it = std::next(region.begin()), eit = region.end(); it != eit;
406        ++it) {
407     Block *bb = &*it;
408     llvm::BasicBlock *llvmBB = state.lookupBlock(bb);
409     auto phis = llvmBB->phis();
410     auto numArguments = bb->getNumArguments();
411     assert(numArguments == std::distance(phis.begin(), phis.end()));
412     for (auto &numberedPhiNode : llvm::enumerate(phis)) {
413       auto &phiNode = numberedPhiNode.value();
414       unsigned index = numberedPhiNode.index();
415       for (auto *pred : bb->getPredecessors()) {
416         // Find the LLVM IR block that contains the converted terminator
417         // instruction and use it in the PHI node. Note that this block is not
418         // necessarily the same as state.lookupBlock(pred), some operations
419         // (in particular, OpenMP operations using OpenMPIRBuilder) may have
420         // split the blocks.
421         llvm::Instruction *terminator =
422             state.lookupBranch(pred->getTerminator());
423         assert(terminator && "missing the mapping for a terminator");
424         phiNode.addIncoming(
425             state.lookupValue(getPHISourceValue(bb, pred, numArguments, index)),
426             terminator->getParent());
427       }
428     }
429   }
430 }
431 
432 /// Sort function blocks topologically.
433 SetVector<Block *>
434 mlir::LLVM::detail::getTopologicallySortedBlocks(Region &region) {
435   // For each block that has not been visited yet (i.e. that has no
436   // predecessors), add it to the list as well as its successors.
437   SetVector<Block *> blocks;
438   for (Block &b : region) {
439     if (blocks.count(&b) == 0) {
440       llvm::ReversePostOrderTraversal<Block *> traversal(&b);
441       blocks.insert(traversal.begin(), traversal.end());
442     }
443   }
444   assert(blocks.size() == region.getBlocks().size() &&
445          "some blocks are not sorted");
446 
447   return blocks;
448 }
449 
450 llvm::Value *mlir::LLVM::detail::createIntrinsicCall(
451     llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
452     ArrayRef<llvm::Value *> args, ArrayRef<llvm::Type *> tys) {
453   llvm::Module *module = builder.GetInsertBlock()->getModule();
454   llvm::Function *fn = llvm::Intrinsic::getDeclaration(module, intrinsic, tys);
455   return builder.CreateCall(fn, args);
456 }
457 
458 /// Given a single MLIR operation, create the corresponding LLVM IR operation
459 /// using the `builder`.
460 LogicalResult
461 ModuleTranslation::convertOperation(Operation &op,
462                                     llvm::IRBuilderBase &builder) {
463   const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op);
464   if (!opIface)
465     return op.emitError("cannot be converted to LLVM IR: missing "
466                         "`LLVMTranslationDialectInterface` registration for "
467                         "dialect for op: ")
468            << op.getName();
469 
470   if (failed(opIface->convertOperation(&op, builder, *this)))
471     return op.emitError("LLVM Translation failed for operation: ")
472            << op.getName();
473 
474   return convertDialectAttributes(&op);
475 }
476 
477 /// Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
478 /// to define values corresponding to the MLIR block arguments.  These nodes
479 /// are not connected to the source basic blocks, which may not exist yet.  Uses
480 /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have
481 /// been created for `bb` and included in the block mapping.  Inserts new
482 /// instructions at the end of the block and leaves `builder` in a state
483 /// suitable for further insertion into the end of the block.
484 LogicalResult ModuleTranslation::convertBlock(Block &bb, bool ignoreArguments,
485                                               llvm::IRBuilderBase &builder) {
486   builder.SetInsertPoint(lookupBlock(&bb));
487   auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
488 
489   // Before traversing operations, make block arguments available through
490   // value remapping and PHI nodes, but do not add incoming edges for the PHI
491   // nodes just yet: those values may be defined by this or following blocks.
492   // This step is omitted if "ignoreArguments" is set.  The arguments of the
493   // first block have been already made available through the remapping of
494   // LLVM function arguments.
495   if (!ignoreArguments) {
496     auto predecessors = bb.getPredecessors();
497     unsigned numPredecessors =
498         std::distance(predecessors.begin(), predecessors.end());
499     for (auto arg : bb.getArguments()) {
500       auto wrappedType = arg.getType();
501       if (!isCompatibleType(wrappedType))
502         return emitError(bb.front().getLoc(),
503                          "block argument does not have an LLVM type");
504       llvm::Type *type = convertType(wrappedType);
505       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
506       mapValue(arg, phi);
507     }
508   }
509 
510   // Traverse operations.
511   for (auto &op : bb) {
512     // Set the current debug location within the builder.
513     builder.SetCurrentDebugLocation(
514         debugTranslation->translateLoc(op.getLoc(), subprogram));
515 
516     if (failed(convertOperation(op, builder)))
517       return failure();
518   }
519 
520   return success();
521 }
522 
523 /// A helper method to get the single Block in an operation honoring LLVM's
524 /// module requirements.
525 static Block &getModuleBody(Operation *module) {
526   return module->getRegion(0).front();
527 }
528 
529 /// A helper method to decide if a constant must not be set as a global variable
530 /// initializer. For an external linkage variable, the variable with an
531 /// initializer is considered externally visible and defined in this module, the
532 /// variable without an initializer is externally available and is defined
533 /// elsewhere.
534 static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage,
535                                         llvm::Constant *cst) {
536   return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) ||
537          linkage == llvm::GlobalVariable::ExternalWeakLinkage;
538 }
539 
540 /// Sets the runtime preemption specifier of `gv` to dso_local if
541 /// `dsoLocalRequested` is true, otherwise it is left unchanged.
542 static void addRuntimePreemptionSpecifier(bool dsoLocalRequested,
543                                           llvm::GlobalValue *gv) {
544   if (dsoLocalRequested)
545     gv->setDSOLocal(true);
546 }
547 
548 /// Create named global variables that correspond to llvm.mlir.global
549 /// definitions. Convert llvm.global_ctors and global_dtors ops.
550 LogicalResult ModuleTranslation::convertGlobals() {
551   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
552     llvm::Type *type = convertType(op.getType());
553     llvm::Constant *cst = nullptr;
554     if (op.getValueOrNull()) {
555       // String attributes are treated separately because they cannot appear as
556       // in-function constants and are thus not supported by getLLVMConstant.
557       if (auto strAttr = op.getValueOrNull().dyn_cast_or_null<StringAttr>()) {
558         cst = llvm::ConstantDataArray::getString(
559             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
560         type = cst->getType();
561       } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc(),
562                                          *this))) {
563         return failure();
564       }
565     }
566 
567     auto linkage = convertLinkageToLLVM(op.getLinkage());
568     auto addrSpace = op.getAddrSpace();
569 
570     // LLVM IR requires constant with linkage other than external or weak
571     // external to have initializers. If MLIR does not provide an initializer,
572     // default to undef.
573     bool dropInitializer = shouldDropGlobalInitializer(linkage, cst);
574     if (!dropInitializer && !cst)
575       cst = llvm::UndefValue::get(type);
576     else if (dropInitializer && cst)
577       cst = nullptr;
578 
579     auto *var = new llvm::GlobalVariable(
580         *llvmModule, type, op.getConstant(), linkage, cst, op.getSymName(),
581         /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal, addrSpace);
582 
583     if (op.getUnnamedAddr().hasValue())
584       var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
585 
586     if (op.getSection().hasValue())
587       var->setSection(*op.getSection());
588 
589     addRuntimePreemptionSpecifier(op.getDsoLocal(), var);
590 
591     Optional<uint64_t> alignment = op.getAlignment();
592     if (alignment.hasValue())
593       var->setAlignment(llvm::MaybeAlign(alignment.getValue()));
594 
595     globalsMapping.try_emplace(op, var);
596   }
597 
598   // Convert global variable bodies. This is done after all global variables
599   // have been created in LLVM IR because a global body may refer to another
600   // global or itself. So all global variables need to be mapped first.
601   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
602     if (Block *initializer = op.getInitializerBlock()) {
603       llvm::IRBuilder<> builder(llvmModule->getContext());
604       for (auto &op : initializer->without_terminator()) {
605         if (failed(convertOperation(op, builder)) ||
606             !isa<llvm::Constant>(lookupValue(op.getResult(0))))
607           return emitError(op.getLoc(), "unemittable constant value");
608       }
609       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
610       llvm::Constant *cst =
611           cast<llvm::Constant>(lookupValue(ret.getOperand(0)));
612       auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op));
613       if (!shouldDropGlobalInitializer(global->getLinkage(), cst))
614         global->setInitializer(cst);
615     }
616   }
617 
618   // Convert llvm.mlir.global_ctors and dtors.
619   for (Operation &op : getModuleBody(mlirModule)) {
620     auto ctorOp = dyn_cast<GlobalCtorsOp>(op);
621     auto dtorOp = dyn_cast<GlobalDtorsOp>(op);
622     if (!ctorOp && !dtorOp)
623       continue;
624     auto range = ctorOp ? llvm::zip(ctorOp.getCtors(), ctorOp.getPriorities())
625                         : llvm::zip(dtorOp.getDtors(), dtorOp.getPriorities());
626     auto appendGlobalFn =
627         ctorOp ? llvm::appendToGlobalCtors : llvm::appendToGlobalDtors;
628     for (auto symbolAndPriority : range) {
629       llvm::Function *f = lookupFunction(
630           std::get<0>(symbolAndPriority).cast<FlatSymbolRefAttr>().getValue());
631       appendGlobalFn(
632           *llvmModule, f,
633           std::get<1>(symbolAndPriority).cast<IntegerAttr>().getInt(),
634           /*Data=*/nullptr);
635     }
636   }
637 
638   return success();
639 }
640 
641 /// Attempts to add an attribute identified by `key`, optionally with the given
642 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the
643 /// attribute has a kind known to LLVM IR, create the attribute of this kind,
644 /// otherwise keep it as a string attribute. Performs additional checks for
645 /// attributes known to have or not have a value in order to avoid assertions
646 /// inside LLVM upon construction.
647 static LogicalResult checkedAddLLVMFnAttribute(Location loc,
648                                                llvm::Function *llvmFunc,
649                                                StringRef key,
650                                                StringRef value = StringRef()) {
651   auto kind = llvm::Attribute::getAttrKindFromName(key);
652   if (kind == llvm::Attribute::None) {
653     llvmFunc->addFnAttr(key, value);
654     return success();
655   }
656 
657   if (llvm::Attribute::isIntAttrKind(kind)) {
658     if (value.empty())
659       return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
660 
661     int result;
662     if (!value.getAsInteger(/*Radix=*/0, result))
663       llvmFunc->addFnAttr(
664           llvm::Attribute::get(llvmFunc->getContext(), kind, result));
665     else
666       llvmFunc->addFnAttr(key, value);
667     return success();
668   }
669 
670   if (!value.empty())
671     return emitError(loc) << "LLVM attribute '" << key
672                           << "' does not expect a value, found '" << value
673                           << "'";
674 
675   llvmFunc->addFnAttr(kind);
676   return success();
677 }
678 
679 /// Attaches the attributes listed in the given array attribute to `llvmFunc`.
680 /// Reports error to `loc` if any and returns immediately. Expects `attributes`
681 /// to be an array attribute containing either string attributes, treated as
682 /// value-less LLVM attributes, or array attributes containing two string
683 /// attributes, with the first string being the name of the corresponding LLVM
684 /// attribute and the second string beings its value. Note that even integer
685 /// attributes are expected to have their values expressed as strings.
686 static LogicalResult
687 forwardPassthroughAttributes(Location loc, Optional<ArrayAttr> attributes,
688                              llvm::Function *llvmFunc) {
689   if (!attributes)
690     return success();
691 
692   for (Attribute attr : *attributes) {
693     if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
694       if (failed(
695               checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue())))
696         return failure();
697       continue;
698     }
699 
700     auto arrayAttr = attr.dyn_cast<ArrayAttr>();
701     if (!arrayAttr || arrayAttr.size() != 2)
702       return emitError(loc)
703              << "expected 'passthrough' to contain string or array attributes";
704 
705     auto keyAttr = arrayAttr[0].dyn_cast<StringAttr>();
706     auto valueAttr = arrayAttr[1].dyn_cast<StringAttr>();
707     if (!keyAttr || !valueAttr)
708       return emitError(loc)
709              << "expected arrays within 'passthrough' to contain two strings";
710 
711     if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(),
712                                          valueAttr.getValue())))
713       return failure();
714   }
715   return success();
716 }
717 
718 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
719   // Clear the block, branch value mappings, they are only relevant within one
720   // function.
721   blockMapping.clear();
722   valueMapping.clear();
723   branchMapping.clear();
724   llvm::Function *llvmFunc = lookupFunction(func.getName());
725 
726   // Translate the debug information for this function.
727   debugTranslation->translate(func, *llvmFunc);
728 
729   // Add function arguments to the value remapping table.
730   // If there was noalias info then we decorate each argument accordingly.
731   unsigned int argIdx = 0;
732   for (auto kvp : llvm::zip(func.getArguments(), llvmFunc->args())) {
733     llvm::Argument &llvmArg = std::get<1>(kvp);
734     BlockArgument mlirArg = std::get<0>(kvp);
735 
736     if (auto attr = func.getArgAttrOfType<UnitAttr>(
737             argIdx, LLVMDialect::getNoAliasAttrName())) {
738       // NB: Attribute already verified to be boolean, so check if we can indeed
739       // attach the attribute to this argument, based on its type.
740       auto argTy = mlirArg.getType();
741       if (!argTy.isa<LLVM::LLVMPointerType>())
742         return func.emitError(
743             "llvm.noalias attribute attached to LLVM non-pointer argument");
744       llvmArg.addAttr(llvm::Attribute::AttrKind::NoAlias);
745     }
746 
747     if (auto attr = func.getArgAttrOfType<IntegerAttr>(
748             argIdx, LLVMDialect::getAlignAttrName())) {
749       // NB: Attribute already verified to be int, so check if we can indeed
750       // attach the attribute to this argument, based on its type.
751       auto argTy = mlirArg.getType();
752       if (!argTy.isa<LLVM::LLVMPointerType>())
753         return func.emitError(
754             "llvm.align attribute attached to LLVM non-pointer argument");
755       llvmArg.addAttrs(
756           llvm::AttrBuilder(llvmArg.getContext()).addAlignmentAttr(llvm::Align(attr.getInt())));
757     }
758 
759     if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.sret")) {
760       auto argTy = mlirArg.getType();
761       if (!argTy.isa<LLVM::LLVMPointerType>())
762         return func.emitError(
763             "llvm.sret attribute attached to LLVM non-pointer argument");
764       llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()).addStructRetAttr(
765           llvmArg.getType()->getPointerElementType()));
766     }
767 
768     if (auto attr = func.getArgAttrOfType<UnitAttr>(argIdx, "llvm.byval")) {
769       auto argTy = mlirArg.getType();
770       if (!argTy.isa<LLVM::LLVMPointerType>())
771         return func.emitError(
772             "llvm.byval attribute attached to LLVM non-pointer argument");
773       llvmArg.addAttrs(llvm::AttrBuilder(llvmArg.getContext()).addByValAttr(
774           llvmArg.getType()->getPointerElementType()));
775     }
776 
777     mapValue(mlirArg, &llvmArg);
778     argIdx++;
779   }
780 
781   // Check the personality and set it.
782   if (func.getPersonality().hasValue()) {
783     llvm::Type *ty = llvm::Type::getInt8PtrTy(llvmFunc->getContext());
784     if (llvm::Constant *pfunc = getLLVMConstant(ty, func.getPersonalityAttr(),
785                                                 func.getLoc(), *this))
786       llvmFunc->setPersonalityFn(pfunc);
787   }
788 
789   // First, create all blocks so we can jump to them.
790   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
791   for (auto &bb : func) {
792     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
793     llvmBB->insertInto(llvmFunc);
794     mapBlock(&bb, llvmBB);
795   }
796 
797   // Then, convert blocks one by one in topological order to ensure defs are
798   // converted before uses.
799   auto blocks = detail::getTopologicallySortedBlocks(func.getBody());
800   for (Block *bb : blocks) {
801     llvm::IRBuilder<> builder(llvmContext);
802     if (failed(convertBlock(*bb, bb->isEntryBlock(), builder)))
803       return failure();
804   }
805 
806   // After all blocks have been traversed and values mapped, connect the PHI
807   // nodes to the results of preceding blocks.
808   detail::connectPHINodes(func.getBody(), *this);
809 
810   // Finally, convert dialect attributes attached to the function.
811   return convertDialectAttributes(func);
812 }
813 
814 LogicalResult ModuleTranslation::convertDialectAttributes(Operation *op) {
815   for (NamedAttribute attribute : op->getDialectAttrs())
816     if (failed(iface.amendOperation(op, attribute, *this)))
817       return failure();
818   return success();
819 }
820 
821 LogicalResult ModuleTranslation::convertFunctionSignatures() {
822   // Declare all functions first because there may be function calls that form a
823   // call graph with cycles, or global initializers that reference functions.
824   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
825     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
826         function.getName(),
827         cast<llvm::FunctionType>(convertType(function.getType())));
828     llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
829     llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage()));
830     mapFunction(function.getName(), llvmFunc);
831     addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc);
832 
833     // Forward the pass-through attributes to LLVM.
834     if (failed(forwardPassthroughAttributes(
835             function.getLoc(), function.getPassthrough(), llvmFunc)))
836       return failure();
837   }
838 
839   return success();
840 }
841 
842 LogicalResult ModuleTranslation::convertFunctions() {
843   // Convert functions.
844   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
845     // Ignore external functions.
846     if (function.isExternal())
847       continue;
848 
849     if (failed(convertOneFunction(function)))
850       return failure();
851   }
852 
853   return success();
854 }
855 
856 llvm::MDNode *
857 ModuleTranslation::getAccessGroup(Operation &opInst,
858                                   SymbolRefAttr accessGroupRef) const {
859   auto metadataName = accessGroupRef.getRootReference();
860   auto accessGroupName = accessGroupRef.getLeafReference();
861   auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
862       opInst.getParentOp(), metadataName);
863   auto *accessGroupOp =
864       SymbolTable::lookupNearestSymbolFrom(metadataOp, accessGroupName);
865   return accessGroupMetadataMapping.lookup(accessGroupOp);
866 }
867 
868 LogicalResult ModuleTranslation::createAccessGroupMetadata() {
869   mlirModule->walk([&](LLVM::MetadataOp metadatas) {
870     metadatas.walk([&](LLVM::AccessGroupMetadataOp op) {
871       llvm::LLVMContext &ctx = llvmModule->getContext();
872       llvm::MDNode *accessGroup = llvm::MDNode::getDistinct(ctx, {});
873       accessGroupMetadataMapping.insert({op, accessGroup});
874     });
875   });
876   return success();
877 }
878 
879 void ModuleTranslation::setAccessGroupsMetadata(Operation *op,
880                                                 llvm::Instruction *inst) {
881   auto accessGroups =
882       op->getAttrOfType<ArrayAttr>(LLVMDialect::getAccessGroupsAttrName());
883   if (accessGroups && !accessGroups.empty()) {
884     llvm::Module *module = inst->getModule();
885     SmallVector<llvm::Metadata *> metadatas;
886     for (SymbolRefAttr accessGroupRef :
887          accessGroups.getAsRange<SymbolRefAttr>())
888       metadatas.push_back(getAccessGroup(*op, accessGroupRef));
889 
890     llvm::MDNode *unionMD = nullptr;
891     if (metadatas.size() == 1)
892       unionMD = llvm::cast<llvm::MDNode>(metadatas.front());
893     else if (metadatas.size() >= 2)
894       unionMD = llvm::MDNode::get(module->getContext(), metadatas);
895 
896     inst->setMetadata(module->getMDKindID("llvm.access.group"), unionMD);
897   }
898 }
899 
900 LogicalResult ModuleTranslation::createAliasScopeMetadata() {
901   mlirModule->walk([&](LLVM::MetadataOp metadatas) {
902     // Create the domains first, so they can be reference below in the scopes.
903     DenseMap<Operation *, llvm::MDNode *> aliasScopeDomainMetadataMapping;
904     metadatas.walk([&](LLVM::AliasScopeDomainMetadataOp op) {
905       llvm::LLVMContext &ctx = llvmModule->getContext();
906       llvm::SmallVector<llvm::Metadata *, 2> operands;
907       operands.push_back({}); // Placeholder for self-reference
908       if (Optional<StringRef> description = op.getDescription())
909         operands.push_back(llvm::MDString::get(ctx, description.getValue()));
910       llvm::MDNode *domain = llvm::MDNode::get(ctx, operands);
911       domain->replaceOperandWith(0, domain); // Self-reference for uniqueness
912       aliasScopeDomainMetadataMapping.insert({op, domain});
913     });
914 
915     // Now create the scopes, referencing the domains created above.
916     metadatas.walk([&](LLVM::AliasScopeMetadataOp op) {
917       llvm::LLVMContext &ctx = llvmModule->getContext();
918       assert(isa<LLVM::MetadataOp>(op->getParentOp()));
919       auto metadataOp = dyn_cast<LLVM::MetadataOp>(op->getParentOp());
920       Operation *domainOp =
921           SymbolTable::lookupNearestSymbolFrom(metadataOp, op.getDomainAttr());
922       llvm::MDNode *domain = aliasScopeDomainMetadataMapping.lookup(domainOp);
923       assert(domain && "Scope's domain should already be valid");
924       llvm::SmallVector<llvm::Metadata *, 3> operands;
925       operands.push_back({}); // Placeholder for self-reference
926       operands.push_back(domain);
927       if (Optional<StringRef> description = op.getDescription())
928         operands.push_back(llvm::MDString::get(ctx, description.getValue()));
929       llvm::MDNode *scope = llvm::MDNode::get(ctx, operands);
930       scope->replaceOperandWith(0, scope); // Self-reference for uniqueness
931       aliasScopeMetadataMapping.insert({op, scope});
932     });
933   });
934   return success();
935 }
936 
937 llvm::MDNode *
938 ModuleTranslation::getAliasScope(Operation &opInst,
939                                  SymbolRefAttr aliasScopeRef) const {
940   StringAttr metadataName = aliasScopeRef.getRootReference();
941   StringAttr scopeName = aliasScopeRef.getLeafReference();
942   auto metadataOp = SymbolTable::lookupNearestSymbolFrom<LLVM::MetadataOp>(
943       opInst.getParentOp(), metadataName);
944   Operation *aliasScopeOp =
945       SymbolTable::lookupNearestSymbolFrom(metadataOp, scopeName);
946   return aliasScopeMetadataMapping.lookup(aliasScopeOp);
947 }
948 
949 void ModuleTranslation::setAliasScopeMetadata(Operation *op,
950                                               llvm::Instruction *inst) {
951   auto populateScopeMetadata = [this, op, inst](StringRef attrName,
952                                                 StringRef llvmMetadataName) {
953     auto scopes = op->getAttrOfType<ArrayAttr>(attrName);
954     if (!scopes || scopes.empty())
955       return;
956     llvm::Module *module = inst->getModule();
957     SmallVector<llvm::Metadata *> scopeMDs;
958     for (SymbolRefAttr scopeRef : scopes.getAsRange<SymbolRefAttr>())
959       scopeMDs.push_back(getAliasScope(*op, scopeRef));
960     llvm::MDNode *unionMD = llvm::MDNode::get(module->getContext(), scopeMDs);
961     inst->setMetadata(module->getMDKindID(llvmMetadataName), unionMD);
962   };
963 
964   populateScopeMetadata(LLVMDialect::getAliasScopesAttrName(), "alias.scope");
965   populateScopeMetadata(LLVMDialect::getNoAliasScopesAttrName(), "noalias");
966 }
967 
968 llvm::Type *ModuleTranslation::convertType(Type type) {
969   return typeTranslator.translateType(type);
970 }
971 
972 /// A helper to look up remapped operands in the value remapping table.
973 SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) {
974   SmallVector<llvm::Value *> remapped;
975   remapped.reserve(values.size());
976   for (Value v : values)
977     remapped.push_back(lookupValue(v));
978   return remapped;
979 }
980 
981 const llvm::DILocation *
982 ModuleTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) {
983   return debugTranslation->translateLoc(loc, scope);
984 }
985 
986 llvm::NamedMDNode *
987 ModuleTranslation::getOrInsertNamedModuleMetadata(StringRef name) {
988   return llvmModule->getOrInsertNamedMetadata(name);
989 }
990 
991 void ModuleTranslation::StackFrame::anchor() {}
992 
993 static std::unique_ptr<llvm::Module>
994 prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
995                   StringRef name) {
996   m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
997   auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
998   if (auto dataLayoutAttr =
999           m->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()))
1000     llvmModule->setDataLayout(dataLayoutAttr.cast<StringAttr>().getValue());
1001   if (auto targetTripleAttr =
1002           m->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
1003     llvmModule->setTargetTriple(targetTripleAttr.cast<StringAttr>().getValue());
1004 
1005   // Inject declarations for `malloc` and `free` functions that can be used in
1006   // memref allocation/deallocation coming from standard ops lowering.
1007   llvm::IRBuilder<> builder(llvmContext);
1008   llvmModule->getOrInsertFunction("malloc", builder.getInt8PtrTy(),
1009                                   builder.getInt64Ty());
1010   llvmModule->getOrInsertFunction("free", builder.getVoidTy(),
1011                                   builder.getInt8PtrTy());
1012 
1013   return llvmModule;
1014 }
1015 
1016 std::unique_ptr<llvm::Module>
1017 mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
1018                               StringRef name) {
1019   if (!satisfiesLLVMModule(module))
1020     return nullptr;
1021   std::unique_ptr<llvm::Module> llvmModule =
1022       prepareLLVMModule(module, llvmContext, name);
1023 
1024   LLVM::ensureDistinctSuccessors(module);
1025 
1026   ModuleTranslation translator(module, std::move(llvmModule));
1027   if (failed(translator.convertFunctionSignatures()))
1028     return nullptr;
1029   if (failed(translator.convertGlobals()))
1030     return nullptr;
1031   if (failed(translator.createAccessGroupMetadata()))
1032     return nullptr;
1033   if (failed(translator.createAliasScopeMetadata()))
1034     return nullptr;
1035   if (failed(translator.convertFunctions()))
1036     return nullptr;
1037 
1038   // Convert other top-level operations if possible.
1039   llvm::IRBuilder<> llvmBuilder(llvmContext);
1040   for (Operation &o : getModuleBody(module).getOperations()) {
1041     if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp, LLVM::GlobalCtorsOp,
1042              LLVM::GlobalDtorsOp, LLVM::MetadataOp>(&o) &&
1043         !o.hasTrait<OpTrait::IsTerminator>() &&
1044         failed(translator.convertOperation(o, llvmBuilder))) {
1045       return nullptr;
1046     }
1047   }
1048 
1049   if (llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
1050     return nullptr;
1051 
1052   return std::move(translator.llvmModule);
1053 }
1054