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