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