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