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