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