xref: /llvm-project/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp (revision b3533a156da92262eb19429d8c12f53e87f5ccec)
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 "AttrKindDetail.h"
17 #include "DebugTranslation.h"
18 #include "LoopAnnotationTranslation.h"
19 #include "mlir/Analysis/TopologicalSortUtils.h"
20 #include "mlir/Dialect/DLTI/DLTI.h"
21 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
22 #include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
23 #include "mlir/Dialect/LLVMIR/Transforms/DIExpressionLegalization.h"
24 #include "mlir/Dialect/LLVMIR/Transforms/LegalizeForExport.h"
25 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
26 #include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
27 #include "mlir/IR/AttrTypeSubElements.h"
28 #include "mlir/IR/Attributes.h"
29 #include "mlir/IR/BuiltinOps.h"
30 #include "mlir/IR/BuiltinTypes.h"
31 #include "mlir/IR/DialectResourceBlobManager.h"
32 #include "mlir/IR/RegionGraphTraits.h"
33 #include "mlir/Support/LLVM.h"
34 #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
35 #include "mlir/Target/LLVMIR/TypeToLLVM.h"
36 
37 #include "llvm/ADT/PostOrderIterator.h"
38 #include "llvm/ADT/SetVector.h"
39 #include "llvm/ADT/StringExtras.h"
40 #include "llvm/ADT/TypeSwitch.h"
41 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/CFG.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DerivedTypes.h"
46 #include "llvm/IR/IRBuilder.h"
47 #include "llvm/IR/InlineAsm.h"
48 #include "llvm/IR/IntrinsicsNVPTX.h"
49 #include "llvm/IR/LLVMContext.h"
50 #include "llvm/IR/MDBuilder.h"
51 #include "llvm/IR/Module.h"
52 #include "llvm/IR/Verifier.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
56 #include "llvm/Transforms/Utils/Cloning.h"
57 #include "llvm/Transforms/Utils/ModuleUtils.h"
58 #include <optional>
59 
60 #define DEBUG_TYPE "llvm-dialect-to-llvm-ir"
61 
62 using namespace mlir;
63 using namespace mlir::LLVM;
64 using namespace mlir::LLVM::detail;
65 
66 extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
67 
68 #include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
69 
70 namespace {
71 /// A customized inserter for LLVM's IRBuilder that captures all LLVM IR
72 /// instructions that are created for future reference.
73 ///
74 /// This is intended to be used with the `CollectionScope` RAII object:
75 ///
76 ///     llvm::IRBuilder<..., InstructionCapturingInserter> builder;
77 ///     {
78 ///       InstructionCapturingInserter::CollectionScope scope(builder);
79 ///       // Call IRBuilder methods as usual.
80 ///
81 ///       // This will return a list of all instructions created by the builder,
82 ///       // in order of creation.
83 ///       builder.getInserter().getCapturedInstructions();
84 ///     }
85 ///     // This will return an empty list.
86 ///     builder.getInserter().getCapturedInstructions();
87 ///
88 /// The capturing functionality is _disabled_ by default for performance
89 /// consideration. It needs to be explicitly enabled, which is achieved by
90 /// creating a `CollectionScope`.
91 class InstructionCapturingInserter : public llvm::IRBuilderCallbackInserter {
92 public:
93   /// Constructs the inserter.
94   InstructionCapturingInserter()
95       : llvm::IRBuilderCallbackInserter([this](llvm::Instruction *instruction) {
96           if (LLVM_LIKELY(enabled))
97             capturedInstructions.push_back(instruction);
98         }) {}
99 
100   /// Returns the list of LLVM IR instructions captured since the last cleanup.
101   ArrayRef<llvm::Instruction *> getCapturedInstructions() const {
102     return capturedInstructions;
103   }
104 
105   /// Clears the list of captured LLVM IR instructions.
106   void clearCapturedInstructions() { capturedInstructions.clear(); }
107 
108   /// RAII object enabling the capture of created LLVM IR instructions.
109   class CollectionScope {
110   public:
111     /// Creates the scope for the given inserter.
112     CollectionScope(llvm::IRBuilderBase &irBuilder, bool isBuilderCapturing);
113 
114     /// Ends the scope.
115     ~CollectionScope();
116 
117     ArrayRef<llvm::Instruction *> getCapturedInstructions() {
118       if (!inserter)
119         return {};
120       return inserter->getCapturedInstructions();
121     }
122 
123   private:
124     /// Back reference to the inserter.
125     InstructionCapturingInserter *inserter = nullptr;
126 
127     /// List of instructions in the inserter prior to this scope.
128     SmallVector<llvm::Instruction *> previouslyCollectedInstructions;
129 
130     /// Whether the inserter was enabled prior to this scope.
131     bool wasEnabled;
132   };
133 
134   /// Enable or disable the capturing mechanism.
135   void setEnabled(bool enabled = true) { this->enabled = enabled; }
136 
137 private:
138   /// List of captured instructions.
139   SmallVector<llvm::Instruction *> capturedInstructions;
140 
141   /// Whether the collection is enabled.
142   bool enabled = false;
143 };
144 
145 using CapturingIRBuilder =
146     llvm::IRBuilder<llvm::ConstantFolder, InstructionCapturingInserter>;
147 } // namespace
148 
149 InstructionCapturingInserter::CollectionScope::CollectionScope(
150     llvm::IRBuilderBase &irBuilder, bool isBuilderCapturing) {
151 
152   if (!isBuilderCapturing)
153     return;
154 
155   auto &capturingIRBuilder = static_cast<CapturingIRBuilder &>(irBuilder);
156   inserter = &capturingIRBuilder.getInserter();
157   wasEnabled = inserter->enabled;
158   if (wasEnabled)
159     previouslyCollectedInstructions.swap(inserter->capturedInstructions);
160   inserter->setEnabled(true);
161 }
162 
163 InstructionCapturingInserter::CollectionScope::~CollectionScope() {
164   if (!inserter)
165     return;
166 
167   previouslyCollectedInstructions.swap(inserter->capturedInstructions);
168   // If collection was enabled (likely in another, surrounding scope), keep
169   // the instructions collected in this scope.
170   if (wasEnabled) {
171     llvm::append_range(inserter->capturedInstructions,
172                        previouslyCollectedInstructions);
173   }
174   inserter->setEnabled(wasEnabled);
175 }
176 
177 /// Translates the given data layout spec attribute to the LLVM IR data layout.
178 /// Only integer, float, pointer and endianness entries are currently supported.
179 static FailureOr<llvm::DataLayout>
180 translateDataLayout(DataLayoutSpecInterface attribute,
181                     const DataLayout &dataLayout,
182                     std::optional<Location> loc = std::nullopt) {
183   if (!loc)
184     loc = UnknownLoc::get(attribute.getContext());
185 
186   // Translate the endianness attribute.
187   std::string llvmDataLayout;
188   llvm::raw_string_ostream layoutStream(llvmDataLayout);
189   for (DataLayoutEntryInterface entry : attribute.getEntries()) {
190     auto key = llvm::dyn_cast_if_present<StringAttr>(entry.getKey());
191     if (!key)
192       continue;
193     if (key.getValue() == DLTIDialect::kDataLayoutEndiannessKey) {
194       auto value = cast<StringAttr>(entry.getValue());
195       bool isLittleEndian =
196           value.getValue() == DLTIDialect::kDataLayoutEndiannessLittle;
197       layoutStream << "-" << (isLittleEndian ? "e" : "E");
198       layoutStream.flush();
199       continue;
200     }
201     if (key.getValue() == DLTIDialect::kDataLayoutProgramMemorySpaceKey) {
202       auto value = cast<IntegerAttr>(entry.getValue());
203       uint64_t space = value.getValue().getZExtValue();
204       // Skip the default address space.
205       if (space == 0)
206         continue;
207       layoutStream << "-P" << space;
208       layoutStream.flush();
209       continue;
210     }
211     if (key.getValue() == DLTIDialect::kDataLayoutGlobalMemorySpaceKey) {
212       auto value = cast<IntegerAttr>(entry.getValue());
213       uint64_t space = value.getValue().getZExtValue();
214       // Skip the default address space.
215       if (space == 0)
216         continue;
217       layoutStream << "-G" << space;
218       layoutStream.flush();
219       continue;
220     }
221     if (key.getValue() == DLTIDialect::kDataLayoutAllocaMemorySpaceKey) {
222       auto value = cast<IntegerAttr>(entry.getValue());
223       uint64_t space = value.getValue().getZExtValue();
224       // Skip the default address space.
225       if (space == 0)
226         continue;
227       layoutStream << "-A" << space;
228       layoutStream.flush();
229       continue;
230     }
231     if (key.getValue() == DLTIDialect::kDataLayoutStackAlignmentKey) {
232       auto value = cast<IntegerAttr>(entry.getValue());
233       uint64_t alignment = value.getValue().getZExtValue();
234       // Skip the default stack alignment.
235       if (alignment == 0)
236         continue;
237       layoutStream << "-S" << alignment;
238       layoutStream.flush();
239       continue;
240     }
241     emitError(*loc) << "unsupported data layout key " << key;
242     return failure();
243   }
244 
245   // Go through the list of entries to check which types are explicitly
246   // specified in entries. Where possible, data layout queries are used instead
247   // of directly inspecting the entries.
248   for (DataLayoutEntryInterface entry : attribute.getEntries()) {
249     auto type = llvm::dyn_cast_if_present<Type>(entry.getKey());
250     if (!type)
251       continue;
252     // Data layout for the index type is irrelevant at this point.
253     if (isa<IndexType>(type))
254       continue;
255     layoutStream << "-";
256     LogicalResult result =
257         llvm::TypeSwitch<Type, LogicalResult>(type)
258             .Case<IntegerType, Float16Type, Float32Type, Float64Type,
259                   Float80Type, Float128Type>([&](Type type) -> LogicalResult {
260               if (auto intType = dyn_cast<IntegerType>(type)) {
261                 if (intType.getSignedness() != IntegerType::Signless)
262                   return emitError(*loc)
263                          << "unsupported data layout for non-signless integer "
264                          << intType;
265                 layoutStream << "i";
266               } else {
267                 layoutStream << "f";
268               }
269               uint64_t size = dataLayout.getTypeSizeInBits(type);
270               uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
271               uint64_t preferred =
272                   dataLayout.getTypePreferredAlignment(type) * 8u;
273               layoutStream << size << ":" << abi;
274               if (abi != preferred)
275                 layoutStream << ":" << preferred;
276               return success();
277             })
278             .Case([&](LLVMPointerType type) {
279               layoutStream << "p" << type.getAddressSpace() << ":";
280               uint64_t size = dataLayout.getTypeSizeInBits(type);
281               uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
282               uint64_t preferred =
283                   dataLayout.getTypePreferredAlignment(type) * 8u;
284               uint64_t index = *dataLayout.getTypeIndexBitwidth(type);
285               layoutStream << size << ":" << abi << ":" << preferred << ":"
286                            << index;
287               return success();
288             })
289             .Default([loc](Type type) {
290               return emitError(*loc)
291                      << "unsupported type in data layout: " << type;
292             });
293     if (failed(result))
294       return failure();
295   }
296   layoutStream.flush();
297   StringRef layoutSpec(llvmDataLayout);
298   if (layoutSpec.starts_with("-"))
299     layoutSpec = layoutSpec.drop_front();
300 
301   return llvm::DataLayout(layoutSpec);
302 }
303 
304 /// Builds a constant of a sequential LLVM type `type`, potentially containing
305 /// other sequential types recursively, from the individual constant values
306 /// provided in `constants`. `shape` contains the number of elements in nested
307 /// sequential types. Reports errors at `loc` and returns nullptr on error.
308 static llvm::Constant *
309 buildSequentialConstant(ArrayRef<llvm::Constant *> &constants,
310                         ArrayRef<int64_t> shape, llvm::Type *type,
311                         Location loc) {
312   if (shape.empty()) {
313     llvm::Constant *result = constants.front();
314     constants = constants.drop_front();
315     return result;
316   }
317 
318   llvm::Type *elementType;
319   if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
320     elementType = arrayTy->getElementType();
321   } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
322     elementType = vectorTy->getElementType();
323   } else {
324     emitError(loc) << "expected sequential LLVM types wrapping a scalar";
325     return nullptr;
326   }
327 
328   SmallVector<llvm::Constant *, 8> nested;
329   nested.reserve(shape.front());
330   for (int64_t i = 0; i < shape.front(); ++i) {
331     nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
332                                              elementType, loc));
333     if (!nested.back())
334       return nullptr;
335   }
336 
337   if (shape.size() == 1 && type->isVectorTy())
338     return llvm::ConstantVector::get(nested);
339   return llvm::ConstantArray::get(
340       llvm::ArrayType::get(elementType, shape.front()), nested);
341 }
342 
343 /// Returns the first non-sequential type nested in sequential types.
344 static llvm::Type *getInnermostElementType(llvm::Type *type) {
345   do {
346     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
347       type = arrayTy->getElementType();
348     } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
349       type = vectorTy->getElementType();
350     } else {
351       return type;
352     }
353   } while (true);
354 }
355 
356 /// Convert a dense elements attribute to an LLVM IR constant using its raw data
357 /// storage if possible. This supports elements attributes of tensor or vector
358 /// type and avoids constructing separate objects for individual values of the
359 /// innermost dimension. Constants for other dimensions are still constructed
360 /// recursively. Returns null if constructing from raw data is not supported for
361 /// this type, e.g., element type is not a power-of-two-sized primitive. Reports
362 /// other errors at `loc`.
363 static llvm::Constant *
364 convertDenseElementsAttr(Location loc, DenseElementsAttr denseElementsAttr,
365                          llvm::Type *llvmType,
366                          const ModuleTranslation &moduleTranslation) {
367   if (!denseElementsAttr)
368     return nullptr;
369 
370   llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
371   if (!llvm::ConstantDataSequential::isElementTypeCompatible(innermostLLVMType))
372     return nullptr;
373 
374   ShapedType type = denseElementsAttr.getType();
375   if (type.getNumElements() == 0)
376     return nullptr;
377 
378   // Check that the raw data size matches what is expected for the scalar size.
379   // TODO: in theory, we could repack the data here to keep constructing from
380   // raw data.
381   // TODO: we may also need to consider endianness when cross-compiling to an
382   // architecture where it is different.
383   int64_t elementByteSize = denseElementsAttr.getRawData().size() /
384                             denseElementsAttr.getNumElements();
385   if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits())
386     return nullptr;
387 
388   // Compute the shape of all dimensions but the innermost. Note that the
389   // innermost dimension may be that of the vector element type.
390   bool hasVectorElementType = isa<VectorType>(type.getElementType());
391   int64_t numAggregates =
392       denseElementsAttr.getNumElements() /
393       (hasVectorElementType ? 1
394                             : denseElementsAttr.getType().getShape().back());
395   ArrayRef<int64_t> outerShape = type.getShape();
396   if (!hasVectorElementType)
397     outerShape = outerShape.drop_back();
398 
399   // Handle the case of vector splat, LLVM has special support for it.
400   if (denseElementsAttr.isSplat() &&
401       (isa<VectorType>(type) || hasVectorElementType)) {
402     llvm::Constant *splatValue = LLVM::detail::getLLVMConstant(
403         innermostLLVMType, denseElementsAttr.getSplatValue<Attribute>(), loc,
404         moduleTranslation);
405     llvm::Constant *splatVector =
406         llvm::ConstantDataVector::getSplat(0, splatValue);
407     SmallVector<llvm::Constant *> constants(numAggregates, splatVector);
408     ArrayRef<llvm::Constant *> constantsRef = constants;
409     return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
410   }
411   if (denseElementsAttr.isSplat())
412     return nullptr;
413 
414   // In case of non-splat, create a constructor for the innermost constant from
415   // a piece of raw data.
416   std::function<llvm::Constant *(StringRef)> buildCstData;
417   if (isa<TensorType>(type)) {
418     auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
419     if (vectorElementType && vectorElementType.getRank() == 1) {
420       buildCstData = [&](StringRef data) {
421         return llvm::ConstantDataVector::getRaw(
422             data, vectorElementType.getShape().back(), innermostLLVMType);
423       };
424     } else if (!vectorElementType) {
425       buildCstData = [&](StringRef data) {
426         return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
427                                                innermostLLVMType);
428       };
429     }
430   } else if (isa<VectorType>(type)) {
431     buildCstData = [&](StringRef data) {
432       return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
433                                               innermostLLVMType);
434     };
435   }
436   if (!buildCstData)
437     return nullptr;
438 
439   // Create innermost constants and defer to the default constant creation
440   // mechanism for other dimensions.
441   SmallVector<llvm::Constant *> constants;
442   int64_t aggregateSize = denseElementsAttr.getType().getShape().back() *
443                           (innermostLLVMType->getScalarSizeInBits() / 8);
444   constants.reserve(numAggregates);
445   for (unsigned i = 0; i < numAggregates; ++i) {
446     StringRef data(denseElementsAttr.getRawData().data() + i * aggregateSize,
447                    aggregateSize);
448     constants.push_back(buildCstData(data));
449   }
450 
451   ArrayRef<llvm::Constant *> constantsRef = constants;
452   return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
453 }
454 
455 /// Convert a dense resource elements attribute to an LLVM IR constant using its
456 /// raw data storage if possible. This supports elements attributes of tensor or
457 /// vector type and avoids constructing separate objects for individual values
458 /// of the innermost dimension. Constants for other dimensions are still
459 /// constructed recursively. Returns nullptr on failure and emits errors at
460 /// `loc`.
461 static llvm::Constant *convertDenseResourceElementsAttr(
462     Location loc, DenseResourceElementsAttr denseResourceAttr,
463     llvm::Type *llvmType, const ModuleTranslation &moduleTranslation) {
464   assert(denseResourceAttr && "expected non-null attribute");
465 
466   llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
467   if (!llvm::ConstantDataSequential::isElementTypeCompatible(
468           innermostLLVMType)) {
469     emitError(loc, "no known conversion for innermost element type");
470     return nullptr;
471   }
472 
473   ShapedType type = denseResourceAttr.getType();
474   assert(type.getNumElements() > 0 && "Expected non-empty elements attribute");
475 
476   AsmResourceBlob *blob = denseResourceAttr.getRawHandle().getBlob();
477   if (!blob) {
478     emitError(loc, "resource does not exist");
479     return nullptr;
480   }
481 
482   ArrayRef<char> rawData = blob->getData();
483 
484   // Check that the raw data size matches what is expected for the scalar size.
485   // TODO: in theory, we could repack the data here to keep constructing from
486   // raw data.
487   // TODO: we may also need to consider endianness when cross-compiling to an
488   // architecture where it is different.
489   int64_t numElements = denseResourceAttr.getType().getNumElements();
490   int64_t elementByteSize = rawData.size() / numElements;
491   if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits()) {
492     emitError(loc, "raw data size does not match element type size");
493     return nullptr;
494   }
495 
496   // Compute the shape of all dimensions but the innermost. Note that the
497   // innermost dimension may be that of the vector element type.
498   bool hasVectorElementType = isa<VectorType>(type.getElementType());
499   int64_t numAggregates =
500       numElements / (hasVectorElementType
501                          ? 1
502                          : denseResourceAttr.getType().getShape().back());
503   ArrayRef<int64_t> outerShape = type.getShape();
504   if (!hasVectorElementType)
505     outerShape = outerShape.drop_back();
506 
507   // Create a constructor for the innermost constant from a piece of raw data.
508   std::function<llvm::Constant *(StringRef)> buildCstData;
509   if (isa<TensorType>(type)) {
510     auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
511     if (vectorElementType && vectorElementType.getRank() == 1) {
512       buildCstData = [&](StringRef data) {
513         return llvm::ConstantDataVector::getRaw(
514             data, vectorElementType.getShape().back(), innermostLLVMType);
515       };
516     } else if (!vectorElementType) {
517       buildCstData = [&](StringRef data) {
518         return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
519                                                innermostLLVMType);
520       };
521     }
522   } else if (isa<VectorType>(type)) {
523     buildCstData = [&](StringRef data) {
524       return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
525                                               innermostLLVMType);
526     };
527   }
528   if (!buildCstData) {
529     emitError(loc, "unsupported dense_resource type");
530     return nullptr;
531   }
532 
533   // Create innermost constants and defer to the default constant creation
534   // mechanism for other dimensions.
535   SmallVector<llvm::Constant *> constants;
536   int64_t aggregateSize = denseResourceAttr.getType().getShape().back() *
537                           (innermostLLVMType->getScalarSizeInBits() / 8);
538   constants.reserve(numAggregates);
539   for (unsigned i = 0; i < numAggregates; ++i) {
540     StringRef data(rawData.data() + i * aggregateSize, aggregateSize);
541     constants.push_back(buildCstData(data));
542   }
543 
544   ArrayRef<llvm::Constant *> constantsRef = constants;
545   return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
546 }
547 
548 /// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
549 /// This currently supports integer, floating point, splat and dense element
550 /// attributes and combinations thereof. Also, an array attribute with two
551 /// elements is supported to represent a complex constant.  In case of error,
552 /// report it to `loc` and return nullptr.
553 llvm::Constant *mlir::LLVM::detail::getLLVMConstant(
554     llvm::Type *llvmType, Attribute attr, Location loc,
555     const ModuleTranslation &moduleTranslation) {
556   if (!attr)
557     return llvm::UndefValue::get(llvmType);
558   if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) {
559     auto arrayAttr = dyn_cast<ArrayAttr>(attr);
560     if (!arrayAttr) {
561       emitError(loc, "expected an array attribute for a struct constant");
562       return nullptr;
563     }
564     SmallVector<llvm::Constant *> structElements;
565     structElements.reserve(structType->getNumElements());
566     for (auto [elemType, elemAttr] :
567          zip_equal(structType->elements(), arrayAttr)) {
568       llvm::Constant *element =
569           getLLVMConstant(elemType, elemAttr, loc, moduleTranslation);
570       if (!element)
571         return nullptr;
572       structElements.push_back(element);
573     }
574     return llvm::ConstantStruct::get(structType, structElements);
575   }
576   // For integer types, we allow a mismatch in sizes as the index type in
577   // MLIR might have a different size than the index type in the LLVM module.
578   if (auto intAttr = dyn_cast<IntegerAttr>(attr))
579     return llvm::ConstantInt::get(
580         llvmType,
581         intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth()));
582   if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
583     const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
584     // Special case for 8-bit floats, which are represented by integers due to
585     // the lack of native fp8 types in LLVM at the moment. Additionally, handle
586     // targets (like AMDGPU) that don't implement bfloat and convert all bfloats
587     // to i16.
588     unsigned floatWidth = APFloat::getSizeInBits(sem);
589     if (llvmType->isIntegerTy(floatWidth))
590       return llvm::ConstantInt::get(llvmType,
591                                     floatAttr.getValue().bitcastToAPInt());
592     if (llvmType !=
593         llvm::Type::getFloatingPointTy(llvmType->getContext(),
594                                        floatAttr.getValue().getSemantics())) {
595       emitError(loc, "FloatAttr does not match expected type of the constant");
596       return nullptr;
597     }
598     return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
599   }
600   if (auto funcAttr = dyn_cast<FlatSymbolRefAttr>(attr))
601     return llvm::ConstantExpr::getBitCast(
602         moduleTranslation.lookupFunction(funcAttr.getValue()), llvmType);
603   if (auto splatAttr = dyn_cast<SplatElementsAttr>(attr)) {
604     llvm::Type *elementType;
605     uint64_t numElements;
606     bool isScalable = false;
607     if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
608       elementType = arrayTy->getElementType();
609       numElements = arrayTy->getNumElements();
610     } else if (auto *fVectorTy = dyn_cast<llvm::FixedVectorType>(llvmType)) {
611       elementType = fVectorTy->getElementType();
612       numElements = fVectorTy->getNumElements();
613     } else if (auto *sVectorTy = dyn_cast<llvm::ScalableVectorType>(llvmType)) {
614       elementType = sVectorTy->getElementType();
615       numElements = sVectorTy->getMinNumElements();
616       isScalable = true;
617     } else {
618       llvm_unreachable("unrecognized constant vector type");
619     }
620     // Splat value is a scalar. Extract it only if the element type is not
621     // another sequence type. The recursion terminates because each step removes
622     // one outer sequential type.
623     bool elementTypeSequential =
624         isa<llvm::ArrayType, llvm::VectorType>(elementType);
625     llvm::Constant *child = getLLVMConstant(
626         elementType,
627         elementTypeSequential ? splatAttr
628                               : splatAttr.getSplatValue<Attribute>(),
629         loc, moduleTranslation);
630     if (!child)
631       return nullptr;
632     if (llvmType->isVectorTy())
633       return llvm::ConstantVector::getSplat(
634           llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child);
635     if (llvmType->isArrayTy()) {
636       auto *arrayType = llvm::ArrayType::get(elementType, numElements);
637       if (child->isZeroValue()) {
638         return llvm::ConstantAggregateZero::get(arrayType);
639       } else {
640         if (llvm::ConstantDataSequential::isElementTypeCompatible(
641                 elementType)) {
642           // TODO: Handle all compatible types. This code only handles integer.
643           if (isa<llvm::IntegerType>(elementType)) {
644             if (llvm::ConstantInt *ci = dyn_cast<llvm::ConstantInt>(child)) {
645               if (ci->getBitWidth() == 8) {
646                 SmallVector<int8_t> constants(numElements, ci->getZExtValue());
647                 return llvm::ConstantDataArray::get(elementType->getContext(),
648                                                     constants);
649               }
650               if (ci->getBitWidth() == 16) {
651                 SmallVector<int16_t> constants(numElements, ci->getZExtValue());
652                 return llvm::ConstantDataArray::get(elementType->getContext(),
653                                                     constants);
654               }
655               if (ci->getBitWidth() == 32) {
656                 SmallVector<int32_t> constants(numElements, ci->getZExtValue());
657                 return llvm::ConstantDataArray::get(elementType->getContext(),
658                                                     constants);
659               }
660               if (ci->getBitWidth() == 64) {
661                 SmallVector<int64_t> constants(numElements, ci->getZExtValue());
662                 return llvm::ConstantDataArray::get(elementType->getContext(),
663                                                     constants);
664               }
665             }
666           }
667         }
668         // std::vector is used here to accomodate large number of elements that
669         // exceed SmallVector capacity.
670         std::vector<llvm::Constant *> constants(numElements, child);
671         return llvm::ConstantArray::get(arrayType, constants);
672       }
673     }
674   }
675 
676   // Try using raw elements data if possible.
677   if (llvm::Constant *result =
678           convertDenseElementsAttr(loc, dyn_cast<DenseElementsAttr>(attr),
679                                    llvmType, moduleTranslation)) {
680     return result;
681   }
682 
683   if (auto denseResourceAttr = dyn_cast<DenseResourceElementsAttr>(attr)) {
684     return convertDenseResourceElementsAttr(loc, denseResourceAttr, llvmType,
685                                             moduleTranslation);
686   }
687 
688   // Fall back to element-by-element construction otherwise.
689   if (auto elementsAttr = dyn_cast<ElementsAttr>(attr)) {
690     assert(elementsAttr.getShapedType().hasStaticShape());
691     assert(!elementsAttr.getShapedType().getShape().empty() &&
692            "unexpected empty elements attribute shape");
693 
694     SmallVector<llvm::Constant *, 8> constants;
695     constants.reserve(elementsAttr.getNumElements());
696     llvm::Type *innermostType = getInnermostElementType(llvmType);
697     for (auto n : elementsAttr.getValues<Attribute>()) {
698       constants.push_back(
699           getLLVMConstant(innermostType, n, loc, moduleTranslation));
700       if (!constants.back())
701         return nullptr;
702     }
703     ArrayRef<llvm::Constant *> constantsRef = constants;
704     llvm::Constant *result = buildSequentialConstant(
705         constantsRef, elementsAttr.getShapedType().getShape(), llvmType, loc);
706     assert(constantsRef.empty() && "did not consume all elemental constants");
707     return result;
708   }
709 
710   if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
711     return llvm::ConstantDataArray::get(
712         moduleTranslation.getLLVMContext(),
713         ArrayRef<char>{stringAttr.getValue().data(),
714                        stringAttr.getValue().size()});
715   }
716   emitError(loc, "unsupported constant value");
717   return nullptr;
718 }
719 
720 ModuleTranslation::ModuleTranslation(Operation *module,
721                                      std::unique_ptr<llvm::Module> llvmModule)
722     : mlirModule(module), llvmModule(std::move(llvmModule)),
723       debugTranslation(
724           std::make_unique<DebugTranslation>(module, *this->llvmModule)),
725       loopAnnotationTranslation(std::make_unique<LoopAnnotationTranslation>(
726           *this, *this->llvmModule)),
727       typeTranslator(this->llvmModule->getContext()),
728       iface(module->getContext()) {
729   assert(satisfiesLLVMModule(mlirModule) &&
730          "mlirModule should honor LLVM's module semantics.");
731 }
732 
733 ModuleTranslation::~ModuleTranslation() {
734   if (ompBuilder)
735     ompBuilder->finalize();
736 }
737 
738 void ModuleTranslation::forgetMapping(Region &region) {
739   SmallVector<Region *> toProcess;
740   toProcess.push_back(&region);
741   while (!toProcess.empty()) {
742     Region *current = toProcess.pop_back_val();
743     for (Block &block : *current) {
744       blockMapping.erase(&block);
745       for (Value arg : block.getArguments())
746         valueMapping.erase(arg);
747       for (Operation &op : block) {
748         for (Value value : op.getResults())
749           valueMapping.erase(value);
750         if (op.hasSuccessors())
751           branchMapping.erase(&op);
752         if (isa<LLVM::GlobalOp>(op))
753           globalsMapping.erase(&op);
754         if (isa<LLVM::CallOp>(op))
755           callMapping.erase(&op);
756         llvm::append_range(
757             toProcess,
758             llvm::map_range(op.getRegions(), [](Region &r) { return &r; }));
759       }
760     }
761   }
762 }
763 
764 /// Get the SSA value passed to the current block from the terminator operation
765 /// of its predecessor.
766 static Value getPHISourceValue(Block *current, Block *pred,
767                                unsigned numArguments, unsigned index) {
768   Operation &terminator = *pred->getTerminator();
769   if (isa<LLVM::BrOp>(terminator))
770     return terminator.getOperand(index);
771 
772 #ifndef NDEBUG
773   llvm::SmallPtrSet<Block *, 4> seenSuccessors;
774   for (unsigned i = 0, e = terminator.getNumSuccessors(); i < e; ++i) {
775     Block *successor = terminator.getSuccessor(i);
776     auto branch = cast<BranchOpInterface>(terminator);
777     SuccessorOperands successorOperands = branch.getSuccessorOperands(i);
778     assert(
779         (!seenSuccessors.contains(successor) || successorOperands.empty()) &&
780         "successors with arguments in LLVM branches must be different blocks");
781     seenSuccessors.insert(successor);
782   }
783 #endif
784 
785   // For instructions that branch based on a condition value, we need to take
786   // the operands for the branch that was taken.
787   if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) {
788     // For conditional branches, we take the operands from either the "true" or
789     // the "false" branch.
790     return condBranchOp.getSuccessor(0) == current
791                ? condBranchOp.getTrueDestOperands()[index]
792                : condBranchOp.getFalseDestOperands()[index];
793   }
794 
795   if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) {
796     // For switches, we take the operands from either the default case, or from
797     // the case branch that was taken.
798     if (switchOp.getDefaultDestination() == current)
799       return switchOp.getDefaultOperands()[index];
800     for (const auto &i : llvm::enumerate(switchOp.getCaseDestinations()))
801       if (i.value() == current)
802         return switchOp.getCaseOperands(i.index())[index];
803   }
804 
805   if (auto invokeOp = dyn_cast<LLVM::InvokeOp>(terminator)) {
806     return invokeOp.getNormalDest() == current
807                ? invokeOp.getNormalDestOperands()[index]
808                : invokeOp.getUnwindDestOperands()[index];
809   }
810 
811   llvm_unreachable(
812       "only branch, switch or invoke operations can be terminators "
813       "of a block that has successors");
814 }
815 
816 /// Connect the PHI nodes to the results of preceding blocks.
817 void mlir::LLVM::detail::connectPHINodes(Region &region,
818                                          const ModuleTranslation &state) {
819   // Skip the first block, it cannot be branched to and its arguments correspond
820   // to the arguments of the LLVM function.
821   for (Block &bb : llvm::drop_begin(region)) {
822     llvm::BasicBlock *llvmBB = state.lookupBlock(&bb);
823     auto phis = llvmBB->phis();
824     auto numArguments = bb.getNumArguments();
825     assert(numArguments == std::distance(phis.begin(), phis.end()));
826     for (auto [index, phiNode] : llvm::enumerate(phis)) {
827       for (auto *pred : bb.getPredecessors()) {
828         // Find the LLVM IR block that contains the converted terminator
829         // instruction and use it in the PHI node. Note that this block is not
830         // necessarily the same as state.lookupBlock(pred), some operations
831         // (in particular, OpenMP operations using OpenMPIRBuilder) may have
832         // split the blocks.
833         llvm::Instruction *terminator =
834             state.lookupBranch(pred->getTerminator());
835         assert(terminator && "missing the mapping for a terminator");
836         phiNode.addIncoming(state.lookupValue(getPHISourceValue(
837                                 &bb, pred, numArguments, index)),
838                             terminator->getParent());
839       }
840     }
841   }
842 }
843 
844 llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
845     llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
846     ArrayRef<llvm::Value *> args, ArrayRef<llvm::Type *> tys) {
847   llvm::Module *module = builder.GetInsertBlock()->getModule();
848   llvm::Function *fn = llvm::Intrinsic::getDeclaration(module, intrinsic, tys);
849   return builder.CreateCall(fn, args);
850 }
851 
852 llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
853     llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
854     Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
855     ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
856     ArrayRef<unsigned> immArgPositions,
857     ArrayRef<StringLiteral> immArgAttrNames) {
858   assert(immArgPositions.size() == immArgAttrNames.size() &&
859          "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal "
860          "length");
861 
862   // Map operands and attributes to LLVM values.
863   auto operands = moduleTranslation.lookupValues(intrOp->getOperands());
864   SmallVector<llvm::Value *> args(immArgPositions.size() + operands.size());
865   for (auto [immArgPos, immArgName] :
866        llvm::zip(immArgPositions, immArgAttrNames)) {
867     auto attr = llvm::cast<TypedAttr>(intrOp->getAttr(immArgName));
868     assert(attr.getType().isIntOrFloat() && "expected int or float immarg");
869     auto *type = moduleTranslation.convertType(attr.getType());
870     args[immArgPos] = LLVM::detail::getLLVMConstant(
871         type, attr, intrOp->getLoc(), moduleTranslation);
872   }
873   unsigned opArg = 0;
874   for (auto &arg : args) {
875     if (!arg)
876       arg = operands[opArg++];
877   }
878 
879   // Resolve overloaded intrinsic declaration.
880   SmallVector<llvm::Type *> overloadedTypes;
881   for (unsigned overloadedResultIdx : overloadedResults) {
882     if (numResults > 1) {
883       // More than one result is mapped to an LLVM struct.
884       overloadedTypes.push_back(moduleTranslation.convertType(
885           llvm::cast<LLVM::LLVMStructType>(intrOp->getResult(0).getType())
886               .getBody()[overloadedResultIdx]));
887     } else {
888       overloadedTypes.push_back(
889           moduleTranslation.convertType(intrOp->getResult(0).getType()));
890     }
891   }
892   for (unsigned overloadedOperandIdx : overloadedOperands)
893     overloadedTypes.push_back(args[overloadedOperandIdx]->getType());
894   llvm::Module *module = builder.GetInsertBlock()->getModule();
895   llvm::Function *llvmIntr =
896       llvm::Intrinsic::getDeclaration(module, intrinsic, overloadedTypes);
897 
898   return builder.CreateCall(llvmIntr, args);
899 }
900 
901 /// Given a single MLIR operation, create the corresponding LLVM IR operation
902 /// using the `builder`.
903 LogicalResult ModuleTranslation::convertOperation(Operation &op,
904                                                   llvm::IRBuilderBase &builder,
905                                                   bool recordInsertions) {
906   const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op);
907   if (!opIface)
908     return op.emitError("cannot be converted to LLVM IR: missing "
909                         "`LLVMTranslationDialectInterface` registration for "
910                         "dialect for op: ")
911            << op.getName();
912 
913   InstructionCapturingInserter::CollectionScope scope(builder,
914                                                       recordInsertions);
915   if (failed(opIface->convertOperation(&op, builder, *this)))
916     return op.emitError("LLVM Translation failed for operation: ")
917            << op.getName();
918 
919   return convertDialectAttributes(&op, scope.getCapturedInstructions());
920 }
921 
922 /// Convert block to LLVM IR.  Unless `ignoreArguments` is set, emit PHI nodes
923 /// to define values corresponding to the MLIR block arguments.  These nodes
924 /// are not connected to the source basic blocks, which may not exist yet.  Uses
925 /// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have
926 /// been created for `bb` and included in the block mapping.  Inserts new
927 /// instructions at the end of the block and leaves `builder` in a state
928 /// suitable for further insertion into the end of the block.
929 LogicalResult ModuleTranslation::convertBlockImpl(Block &bb,
930                                                   bool ignoreArguments,
931                                                   llvm::IRBuilderBase &builder,
932                                                   bool recordInsertions) {
933   builder.SetInsertPoint(lookupBlock(&bb));
934   auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
935 
936   // Before traversing operations, make block arguments available through
937   // value remapping and PHI nodes, but do not add incoming edges for the PHI
938   // nodes just yet: those values may be defined by this or following blocks.
939   // This step is omitted if "ignoreArguments" is set.  The arguments of the
940   // first block have been already made available through the remapping of
941   // LLVM function arguments.
942   if (!ignoreArguments) {
943     auto predecessors = bb.getPredecessors();
944     unsigned numPredecessors =
945         std::distance(predecessors.begin(), predecessors.end());
946     for (auto arg : bb.getArguments()) {
947       auto wrappedType = arg.getType();
948       if (!isCompatibleType(wrappedType))
949         return emitError(bb.front().getLoc(),
950                          "block argument does not have an LLVM type");
951       builder.SetCurrentDebugLocation(
952           debugTranslation->translateLoc(arg.getLoc(), subprogram));
953       llvm::Type *type = convertType(wrappedType);
954       llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
955       mapValue(arg, phi);
956     }
957   }
958 
959   // Traverse operations.
960   for (auto &op : bb) {
961     // Set the current debug location within the builder.
962     builder.SetCurrentDebugLocation(
963         debugTranslation->translateLoc(op.getLoc(), subprogram));
964 
965     if (failed(convertOperation(op, builder, recordInsertions)))
966       return failure();
967 
968     // Set the branch weight metadata on the translated instruction.
969     if (auto iface = dyn_cast<BranchWeightOpInterface>(op))
970       setBranchWeightsMetadata(iface);
971   }
972 
973   return success();
974 }
975 
976 /// A helper method to get the single Block in an operation honoring LLVM's
977 /// module requirements.
978 static Block &getModuleBody(Operation *module) {
979   return module->getRegion(0).front();
980 }
981 
982 /// A helper method to decide if a constant must not be set as a global variable
983 /// initializer. For an external linkage variable, the variable with an
984 /// initializer is considered externally visible and defined in this module, the
985 /// variable without an initializer is externally available and is defined
986 /// elsewhere.
987 static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage,
988                                         llvm::Constant *cst) {
989   return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) ||
990          linkage == llvm::GlobalVariable::ExternalWeakLinkage;
991 }
992 
993 /// Sets the runtime preemption specifier of `gv` to dso_local if
994 /// `dsoLocalRequested` is true, otherwise it is left unchanged.
995 static void addRuntimePreemptionSpecifier(bool dsoLocalRequested,
996                                           llvm::GlobalValue *gv) {
997   if (dsoLocalRequested)
998     gv->setDSOLocal(true);
999 }
1000 
1001 /// Create named global variables that correspond to llvm.mlir.global
1002 /// definitions. Convert llvm.global_ctors and global_dtors ops.
1003 LogicalResult ModuleTranslation::convertGlobals() {
1004   // Mapping from compile unit to its respective set of global variables.
1005   DenseMap<llvm::DICompileUnit *, SmallVector<llvm::Metadata *>> allGVars;
1006 
1007   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
1008     llvm::Type *type = convertType(op.getType());
1009     llvm::Constant *cst = nullptr;
1010     if (op.getValueOrNull()) {
1011       // String attributes are treated separately because they cannot appear as
1012       // in-function constants and are thus not supported by getLLVMConstant.
1013       if (auto strAttr = dyn_cast_or_null<StringAttr>(op.getValueOrNull())) {
1014         cst = llvm::ConstantDataArray::getString(
1015             llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
1016         type = cst->getType();
1017       } else if (!(cst = getLLVMConstant(type, op.getValueOrNull(), op.getLoc(),
1018                                          *this))) {
1019         return failure();
1020       }
1021     }
1022 
1023     auto linkage = convertLinkageToLLVM(op.getLinkage());
1024 
1025     // LLVM IR requires constant with linkage other than external or weak
1026     // external to have initializers. If MLIR does not provide an initializer,
1027     // default to undef.
1028     bool dropInitializer = shouldDropGlobalInitializer(linkage, cst);
1029     if (!dropInitializer && !cst)
1030       cst = llvm::UndefValue::get(type);
1031     else if (dropInitializer && cst)
1032       cst = nullptr;
1033 
1034     auto *var = new llvm::GlobalVariable(
1035         *llvmModule, type, op.getConstant(), linkage, cst, op.getSymName(),
1036         /*InsertBefore=*/nullptr,
1037         op.getThreadLocal_() ? llvm::GlobalValue::GeneralDynamicTLSModel
1038                              : llvm::GlobalValue::NotThreadLocal,
1039         op.getAddrSpace(), op.getExternallyInitialized());
1040 
1041     if (std::optional<mlir::SymbolRefAttr> comdat = op.getComdat()) {
1042       auto selectorOp = cast<ComdatSelectorOp>(
1043           SymbolTable::lookupNearestSymbolFrom(op, *comdat));
1044       var->setComdat(comdatMapping.lookup(selectorOp));
1045     }
1046 
1047     if (op.getUnnamedAddr().has_value())
1048       var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
1049 
1050     if (op.getSection().has_value())
1051       var->setSection(*op.getSection());
1052 
1053     addRuntimePreemptionSpecifier(op.getDsoLocal(), var);
1054 
1055     std::optional<uint64_t> alignment = op.getAlignment();
1056     if (alignment.has_value())
1057       var->setAlignment(llvm::MaybeAlign(alignment.value()));
1058 
1059     var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
1060 
1061     globalsMapping.try_emplace(op, var);
1062 
1063     // Add debug information if present.
1064     if (op.getDbgExpr()) {
1065       llvm::DIGlobalVariableExpression *diGlobalExpr =
1066           debugTranslation->translateGlobalVariableExpression(op.getDbgExpr());
1067       llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable();
1068       var->addDebugInfo(diGlobalExpr);
1069 
1070       // There is no `globals` field in DICompileUnitAttr which can be directly
1071       // assigned to DICompileUnit. We have to build the list by looking at the
1072       // dbgExpr of all the GlobalOps. The scope of the variable is used to get
1073       // the DICompileUnit in which to add it. But for the languages that
1074       // support modules, the scope hierarchy can be
1075       // variable -> module -> compile unit
1076       // If a variable scope points to the module then we use the scope of the
1077       // module to get the compile unit.
1078       // Global variables are also used for things like static local variables
1079       // in C and local variables with the save attribute in Fortran. The scope
1080       // of the variable is the parent function. We use the compile unit of the
1081       // parent function in this case.
1082       llvm::DIScope *scope = diGlobalVar->getScope();
1083       if (auto *mod = dyn_cast_if_present<llvm::DIModule>(scope))
1084         scope = mod->getScope();
1085       else if (auto *sp = dyn_cast_if_present<llvm::DISubprogram>(scope))
1086         scope = sp->getUnit();
1087 
1088       // Get the compile unit (scope) of the the global variable.
1089       if (llvm::DICompileUnit *compileUnit =
1090               dyn_cast_if_present<llvm::DICompileUnit>(scope)) {
1091         // Update the compile unit with this incoming global variable expression
1092         // during the finalizing step later.
1093         allGVars[compileUnit].push_back(diGlobalExpr);
1094       }
1095     }
1096   }
1097 
1098   // Convert global variable bodies. This is done after all global variables
1099   // have been created in LLVM IR because a global body may refer to another
1100   // global or itself. So all global variables need to be mapped first.
1101   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
1102     if (Block *initializer = op.getInitializerBlock()) {
1103       llvm::IRBuilder<> builder(llvmModule->getContext());
1104 
1105       [[maybe_unused]] int numConstantsHit = 0;
1106       [[maybe_unused]] int numConstantsErased = 0;
1107       DenseMap<llvm::ConstantAggregate *, int> constantAggregateUseMap;
1108 
1109       for (auto &op : initializer->without_terminator()) {
1110         if (failed(convertOperation(op, builder)))
1111           return emitError(op.getLoc(), "fail to convert global initializer");
1112         auto *cst = dyn_cast<llvm::Constant>(lookupValue(op.getResult(0)));
1113         if (!cst)
1114           return emitError(op.getLoc(), "unemittable constant value");
1115 
1116         // When emitting an LLVM constant, a new constant is created and the old
1117         // constant may become dangling and take space. We should remove the
1118         // dangling constants to avoid memory explosion especially for constant
1119         // arrays whose number of elements is large.
1120         // Because multiple operations may refer to the same constant, we need
1121         // to count the number of uses of each constant array and remove it only
1122         // when the count becomes zero.
1123         if (auto *agg = dyn_cast<llvm::ConstantAggregate>(cst)) {
1124           numConstantsHit++;
1125           Value result = op.getResult(0);
1126           int numUsers = std::distance(result.use_begin(), result.use_end());
1127           auto [iterator, inserted] =
1128               constantAggregateUseMap.try_emplace(agg, numUsers);
1129           if (!inserted) {
1130             // Key already exists, update the value
1131             iterator->second += numUsers;
1132           }
1133         }
1134         // Scan the operands of the operation to decrement the use count of
1135         // constants. Erase the constant if the use count becomes zero.
1136         for (Value v : op.getOperands()) {
1137           auto cst = dyn_cast<llvm::ConstantAggregate>(lookupValue(v));
1138           if (!cst)
1139             continue;
1140           auto iter = constantAggregateUseMap.find(cst);
1141           assert(iter != constantAggregateUseMap.end() && "constant not found");
1142           iter->second--;
1143           if (iter->second == 0) {
1144             // NOTE: cannot call removeDeadConstantUsers() here because it
1145             // may remove the constant which has uses not be converted yet.
1146             if (cst->user_empty()) {
1147               cst->destroyConstant();
1148               numConstantsErased++;
1149             }
1150             constantAggregateUseMap.erase(iter);
1151           }
1152         }
1153       }
1154 
1155       ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
1156       llvm::Constant *cst =
1157           cast<llvm::Constant>(lookupValue(ret.getOperand(0)));
1158       auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op));
1159       if (!shouldDropGlobalInitializer(global->getLinkage(), cst))
1160         global->setInitializer(cst);
1161 
1162       // Try to remove the dangling constants again after all operations are
1163       // converted.
1164       for (auto it : constantAggregateUseMap) {
1165         auto cst = it.first;
1166         cst->removeDeadConstantUsers();
1167         if (cst->user_empty()) {
1168           cst->destroyConstant();
1169           numConstantsErased++;
1170         }
1171       }
1172 
1173       LLVM_DEBUG(llvm::dbgs()
1174                      << "Convert initializer for " << op.getName() << "\n";
1175                  llvm::dbgs() << numConstantsHit << " new constants hit\n";
1176                  llvm::dbgs()
1177                  << numConstantsErased << " dangling constants erased\n";);
1178     }
1179   }
1180 
1181   // Convert llvm.mlir.global_ctors and dtors.
1182   for (Operation &op : getModuleBody(mlirModule)) {
1183     auto ctorOp = dyn_cast<GlobalCtorsOp>(op);
1184     auto dtorOp = dyn_cast<GlobalDtorsOp>(op);
1185     if (!ctorOp && !dtorOp)
1186       continue;
1187     auto range = ctorOp ? llvm::zip(ctorOp.getCtors(), ctorOp.getPriorities())
1188                         : llvm::zip(dtorOp.getDtors(), dtorOp.getPriorities());
1189     auto appendGlobalFn =
1190         ctorOp ? llvm::appendToGlobalCtors : llvm::appendToGlobalDtors;
1191     for (auto symbolAndPriority : range) {
1192       llvm::Function *f = lookupFunction(
1193           cast<FlatSymbolRefAttr>(std::get<0>(symbolAndPriority)).getValue());
1194       appendGlobalFn(*llvmModule, f,
1195                      cast<IntegerAttr>(std::get<1>(symbolAndPriority)).getInt(),
1196                      /*Data=*/nullptr);
1197     }
1198   }
1199 
1200   for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>())
1201     if (failed(convertDialectAttributes(op, {})))
1202       return failure();
1203 
1204   // Finally, update the compile units their respective sets of global variables
1205   // created earlier.
1206   for (const auto &[compileUnit, globals] : allGVars) {
1207     compileUnit->replaceGlobalVariables(
1208         llvm::MDTuple::get(getLLVMContext(), globals));
1209   }
1210 
1211   return success();
1212 }
1213 
1214 /// Attempts to add an attribute identified by `key`, optionally with the given
1215 /// `value` to LLVM function `llvmFunc`. Reports errors at `loc` if any. If the
1216 /// attribute has a kind known to LLVM IR, create the attribute of this kind,
1217 /// otherwise keep it as a string attribute. Performs additional checks for
1218 /// attributes known to have or not have a value in order to avoid assertions
1219 /// inside LLVM upon construction.
1220 static LogicalResult checkedAddLLVMFnAttribute(Location loc,
1221                                                llvm::Function *llvmFunc,
1222                                                StringRef key,
1223                                                StringRef value = StringRef()) {
1224   auto kind = llvm::Attribute::getAttrKindFromName(key);
1225   if (kind == llvm::Attribute::None) {
1226     llvmFunc->addFnAttr(key, value);
1227     return success();
1228   }
1229 
1230   if (llvm::Attribute::isIntAttrKind(kind)) {
1231     if (value.empty())
1232       return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
1233 
1234     int64_t result;
1235     if (!value.getAsInteger(/*Radix=*/0, result))
1236       llvmFunc->addFnAttr(
1237           llvm::Attribute::get(llvmFunc->getContext(), kind, result));
1238     else
1239       llvmFunc->addFnAttr(key, value);
1240     return success();
1241   }
1242 
1243   if (!value.empty())
1244     return emitError(loc) << "LLVM attribute '" << key
1245                           << "' does not expect a value, found '" << value
1246                           << "'";
1247 
1248   llvmFunc->addFnAttr(kind);
1249   return success();
1250 }
1251 
1252 /// Return a representation of `value` as metadata.
1253 static llvm::Metadata *convertIntegerToMetadata(llvm::LLVMContext &context,
1254                                                 const llvm::APInt &value) {
1255   llvm::Constant *constant = llvm::ConstantInt::get(context, value);
1256   return llvm::ConstantAsMetadata::get(constant);
1257 }
1258 
1259 /// Return a representation of `value` as an MDNode.
1260 static llvm::MDNode *convertIntegerToMDNode(llvm::LLVMContext &context,
1261                                             const llvm::APInt &value) {
1262   return llvm::MDNode::get(context, convertIntegerToMetadata(context, value));
1263 }
1264 
1265 /// Return an MDNode encoding `vec_type_hint` metadata.
1266 static llvm::MDNode *convertVecTypeHintToMDNode(llvm::LLVMContext &context,
1267                                                 llvm::Type *type,
1268                                                 bool isSigned) {
1269   llvm::Metadata *typeMD =
1270       llvm::ConstantAsMetadata::get(llvm::UndefValue::get(type));
1271   llvm::Metadata *isSignedMD =
1272       convertIntegerToMetadata(context, llvm::APInt(32, isSigned ? 1 : 0));
1273   return llvm::MDNode::get(context, {typeMD, isSignedMD});
1274 }
1275 
1276 /// Return an MDNode with a tuple given by the values in `values`.
1277 static llvm::MDNode *convertIntegerArrayToMDNode(llvm::LLVMContext &context,
1278                                                  ArrayRef<int32_t> values) {
1279   SmallVector<llvm::Metadata *> mdValues;
1280   llvm::transform(
1281       values, std::back_inserter(mdValues), [&context](int32_t value) {
1282         return convertIntegerToMetadata(context, llvm::APInt(32, value));
1283       });
1284   return llvm::MDNode::get(context, mdValues);
1285 }
1286 
1287 /// Attaches the attributes listed in the given array attribute to `llvmFunc`.
1288 /// Reports error to `loc` if any and returns immediately. Expects `attributes`
1289 /// to be an array attribute containing either string attributes, treated as
1290 /// value-less LLVM attributes, or array attributes containing two string
1291 /// attributes, with the first string being the name of the corresponding LLVM
1292 /// attribute and the second string beings its value. Note that even integer
1293 /// attributes are expected to have their values expressed as strings.
1294 static LogicalResult
1295 forwardPassthroughAttributes(Location loc, std::optional<ArrayAttr> attributes,
1296                              llvm::Function *llvmFunc) {
1297   if (!attributes)
1298     return success();
1299 
1300   for (Attribute attr : *attributes) {
1301     if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
1302       if (failed(
1303               checkedAddLLVMFnAttribute(loc, llvmFunc, stringAttr.getValue())))
1304         return failure();
1305       continue;
1306     }
1307 
1308     auto arrayAttr = dyn_cast<ArrayAttr>(attr);
1309     if (!arrayAttr || arrayAttr.size() != 2)
1310       return emitError(loc)
1311              << "expected 'passthrough' to contain string or array attributes";
1312 
1313     auto keyAttr = dyn_cast<StringAttr>(arrayAttr[0]);
1314     auto valueAttr = dyn_cast<StringAttr>(arrayAttr[1]);
1315     if (!keyAttr || !valueAttr)
1316       return emitError(loc)
1317              << "expected arrays within 'passthrough' to contain two strings";
1318 
1319     if (failed(checkedAddLLVMFnAttribute(loc, llvmFunc, keyAttr.getValue(),
1320                                          valueAttr.getValue())))
1321       return failure();
1322   }
1323   return success();
1324 }
1325 
1326 LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
1327   // Clear the block, branch value mappings, they are only relevant within one
1328   // function.
1329   blockMapping.clear();
1330   valueMapping.clear();
1331   branchMapping.clear();
1332   llvm::Function *llvmFunc = lookupFunction(func.getName());
1333 
1334   // Add function arguments to the value remapping table.
1335   for (auto [mlirArg, llvmArg] :
1336        llvm::zip(func.getArguments(), llvmFunc->args()))
1337     mapValue(mlirArg, &llvmArg);
1338 
1339   // Check the personality and set it.
1340   if (func.getPersonality()) {
1341     llvm::Type *ty = llvm::PointerType::getUnqual(llvmFunc->getContext());
1342     if (llvm::Constant *pfunc = getLLVMConstant(ty, func.getPersonalityAttr(),
1343                                                 func.getLoc(), *this))
1344       llvmFunc->setPersonalityFn(pfunc);
1345   }
1346 
1347   if (std::optional<StringRef> section = func.getSection())
1348     llvmFunc->setSection(*section);
1349 
1350   if (func.getArmStreaming())
1351     llvmFunc->addFnAttr("aarch64_pstate_sm_enabled");
1352   else if (func.getArmLocallyStreaming())
1353     llvmFunc->addFnAttr("aarch64_pstate_sm_body");
1354   else if (func.getArmStreamingCompatible())
1355     llvmFunc->addFnAttr("aarch64_pstate_sm_compatible");
1356 
1357   if (func.getArmNewZa())
1358     llvmFunc->addFnAttr("aarch64_new_za");
1359   else if (func.getArmInZa())
1360     llvmFunc->addFnAttr("aarch64_in_za");
1361   else if (func.getArmOutZa())
1362     llvmFunc->addFnAttr("aarch64_out_za");
1363   else if (func.getArmInoutZa())
1364     llvmFunc->addFnAttr("aarch64_inout_za");
1365   else if (func.getArmPreservesZa())
1366     llvmFunc->addFnAttr("aarch64_preserves_za");
1367 
1368   if (auto targetCpu = func.getTargetCpu())
1369     llvmFunc->addFnAttr("target-cpu", *targetCpu);
1370 
1371   if (auto tuneCpu = func.getTuneCpu())
1372     llvmFunc->addFnAttr("tune-cpu", *tuneCpu);
1373 
1374   if (auto targetFeatures = func.getTargetFeatures())
1375     llvmFunc->addFnAttr("target-features", targetFeatures->getFeaturesString());
1376 
1377   if (auto attr = func.getVscaleRange())
1378     llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
1379         getLLVMContext(), attr->getMinRange().getInt(),
1380         attr->getMaxRange().getInt()));
1381 
1382   if (auto unsafeFpMath = func.getUnsafeFpMath())
1383     llvmFunc->addFnAttr("unsafe-fp-math", llvm::toStringRef(*unsafeFpMath));
1384 
1385   if (auto noInfsFpMath = func.getNoInfsFpMath())
1386     llvmFunc->addFnAttr("no-infs-fp-math", llvm::toStringRef(*noInfsFpMath));
1387 
1388   if (auto noNansFpMath = func.getNoNansFpMath())
1389     llvmFunc->addFnAttr("no-nans-fp-math", llvm::toStringRef(*noNansFpMath));
1390 
1391   if (auto approxFuncFpMath = func.getApproxFuncFpMath())
1392     llvmFunc->addFnAttr("approx-func-fp-math",
1393                         llvm::toStringRef(*approxFuncFpMath));
1394 
1395   if (auto noSignedZerosFpMath = func.getNoSignedZerosFpMath())
1396     llvmFunc->addFnAttr("no-signed-zeros-fp-math",
1397                         llvm::toStringRef(*noSignedZerosFpMath));
1398 
1399   if (auto denormalFpMath = func.getDenormalFpMath())
1400     llvmFunc->addFnAttr("denormal-fp-math", *denormalFpMath);
1401 
1402   if (auto denormalFpMathF32 = func.getDenormalFpMathF32())
1403     llvmFunc->addFnAttr("denormal-fp-math-f32", *denormalFpMathF32);
1404 
1405   if (auto fpContract = func.getFpContract())
1406     llvmFunc->addFnAttr("fp-contract", *fpContract);
1407 
1408   // Add function attribute frame-pointer, if found.
1409   if (FramePointerKindAttr attr = func.getFramePointerAttr())
1410     llvmFunc->addFnAttr("frame-pointer",
1411                         LLVM::framePointerKind::stringifyFramePointerKind(
1412                             (attr.getFramePointerKind())));
1413 
1414   // First, create all blocks so we can jump to them.
1415   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
1416   for (auto &bb : func) {
1417     auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
1418     llvmBB->insertInto(llvmFunc);
1419     mapBlock(&bb, llvmBB);
1420   }
1421 
1422   // Then, convert blocks one by one in topological order to ensure defs are
1423   // converted before uses.
1424   auto blocks = getBlocksSortedByDominance(func.getBody());
1425   for (Block *bb : blocks) {
1426     CapturingIRBuilder builder(llvmContext);
1427     if (failed(convertBlockImpl(*bb, bb->isEntryBlock(), builder,
1428                                 /*recordInsertions=*/true)))
1429       return failure();
1430   }
1431 
1432   // After all blocks have been traversed and values mapped, connect the PHI
1433   // nodes to the results of preceding blocks.
1434   detail::connectPHINodes(func.getBody(), *this);
1435 
1436   // Finally, convert dialect attributes attached to the function.
1437   return convertDialectAttributes(func, {});
1438 }
1439 
1440 LogicalResult ModuleTranslation::convertDialectAttributes(
1441     Operation *op, ArrayRef<llvm::Instruction *> instructions) {
1442   for (NamedAttribute attribute : op->getDialectAttrs())
1443     if (failed(iface.amendOperation(op, instructions, attribute, *this)))
1444       return failure();
1445   return success();
1446 }
1447 
1448 /// Converts memory effect attributes from `func` and attaches them to
1449 /// `llvmFunc`.
1450 static void convertFunctionMemoryAttributes(LLVMFuncOp func,
1451                                             llvm::Function *llvmFunc) {
1452   if (!func.getMemoryEffects())
1453     return;
1454 
1455   MemoryEffectsAttr memEffects = func.getMemoryEffectsAttr();
1456 
1457   // Add memory effects incrementally.
1458   llvm::MemoryEffects newMemEffects =
1459       llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
1460                           convertModRefInfoToLLVM(memEffects.getArgMem()));
1461   newMemEffects |= llvm::MemoryEffects(
1462       llvm::MemoryEffects::Location::InaccessibleMem,
1463       convertModRefInfoToLLVM(memEffects.getInaccessibleMem()));
1464   newMemEffects |=
1465       llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
1466                           convertModRefInfoToLLVM(memEffects.getOther()));
1467   llvmFunc->setMemoryEffects(newMemEffects);
1468 }
1469 
1470 /// Converts function attributes from `func` and attaches them to `llvmFunc`.
1471 static void convertFunctionAttributes(LLVMFuncOp func,
1472                                       llvm::Function *llvmFunc) {
1473   if (func.getNoInlineAttr())
1474     llvmFunc->addFnAttr(llvm::Attribute::NoInline);
1475   if (func.getAlwaysInlineAttr())
1476     llvmFunc->addFnAttr(llvm::Attribute::AlwaysInline);
1477   if (func.getOptimizeNoneAttr())
1478     llvmFunc->addFnAttr(llvm::Attribute::OptimizeNone);
1479   if (func.getConvergentAttr())
1480     llvmFunc->addFnAttr(llvm::Attribute::Convergent);
1481   if (func.getNoUnwindAttr())
1482     llvmFunc->addFnAttr(llvm::Attribute::NoUnwind);
1483   if (func.getWillReturnAttr())
1484     llvmFunc->addFnAttr(llvm::Attribute::WillReturn);
1485   convertFunctionMemoryAttributes(func, llvmFunc);
1486 }
1487 
1488 /// Converts function attributes from `func` and attaches them to `llvmFunc`.
1489 static void convertFunctionKernelAttributes(LLVMFuncOp func,
1490                                             llvm::Function *llvmFunc,
1491                                             ModuleTranslation &translation) {
1492   llvm::LLVMContext &llvmContext = llvmFunc->getContext();
1493 
1494   if (VecTypeHintAttr vecTypeHint = func.getVecTypeHintAttr()) {
1495     Type type = vecTypeHint.getHint().getValue();
1496     llvm::Type *llvmType = translation.convertType(type);
1497     bool isSigned = vecTypeHint.getIsSigned();
1498     llvmFunc->setMetadata(
1499         func.getVecTypeHintAttrName(),
1500         convertVecTypeHintToMDNode(llvmContext, llvmType, isSigned));
1501   }
1502 
1503   if (std::optional<ArrayRef<int32_t>> workGroupSizeHint =
1504           func.getWorkGroupSizeHint()) {
1505     llvmFunc->setMetadata(
1506         func.getWorkGroupSizeHintAttrName(),
1507         convertIntegerArrayToMDNode(llvmContext, *workGroupSizeHint));
1508   }
1509 
1510   if (std::optional<ArrayRef<int32_t>> reqdWorkGroupSize =
1511           func.getReqdWorkGroupSize()) {
1512     llvmFunc->setMetadata(
1513         func.getReqdWorkGroupSizeAttrName(),
1514         convertIntegerArrayToMDNode(llvmContext, *reqdWorkGroupSize));
1515   }
1516 
1517   if (std::optional<uint32_t> intelReqdSubGroupSize =
1518           func.getIntelReqdSubGroupSize()) {
1519     llvmFunc->setMetadata(
1520         func.getIntelReqdSubGroupSizeAttrName(),
1521         convertIntegerToMDNode(llvmContext,
1522                                llvm::APInt(32, *intelReqdSubGroupSize)));
1523   }
1524 }
1525 
1526 FailureOr<llvm::AttrBuilder>
1527 ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
1528                                          DictionaryAttr paramAttrs) {
1529   llvm::AttrBuilder attrBuilder(llvmModule->getContext());
1530   auto attrNameToKindMapping = getAttrNameToKindMapping();
1531 
1532   for (auto namedAttr : paramAttrs) {
1533     auto it = attrNameToKindMapping.find(namedAttr.getName());
1534     if (it != attrNameToKindMapping.end()) {
1535       llvm::Attribute::AttrKind llvmKind = it->second;
1536 
1537       llvm::TypeSwitch<Attribute>(namedAttr.getValue())
1538           .Case<TypeAttr>([&](auto typeAttr) {
1539             attrBuilder.addTypeAttr(llvmKind, convertType(typeAttr.getValue()));
1540           })
1541           .Case<IntegerAttr>([&](auto intAttr) {
1542             attrBuilder.addRawIntAttr(llvmKind, intAttr.getInt());
1543           })
1544           .Case<UnitAttr>([&](auto) { attrBuilder.addAttribute(llvmKind); });
1545     } else if (namedAttr.getNameDialect()) {
1546       if (failed(iface.convertParameterAttr(func, argIdx, namedAttr, *this)))
1547         return failure();
1548     }
1549   }
1550 
1551   return attrBuilder;
1552 }
1553 
1554 LogicalResult ModuleTranslation::convertFunctionSignatures() {
1555   // Declare all functions first because there may be function calls that form a
1556   // call graph with cycles, or global initializers that reference functions.
1557   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
1558     llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
1559         function.getName(),
1560         cast<llvm::FunctionType>(convertType(function.getFunctionType())));
1561     llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
1562     llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage()));
1563     llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv()));
1564     mapFunction(function.getName(), llvmFunc);
1565     addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc);
1566 
1567     // Convert function attributes.
1568     convertFunctionAttributes(function, llvmFunc);
1569 
1570     // Convert function kernel attributes to metadata.
1571     convertFunctionKernelAttributes(function, llvmFunc, *this);
1572 
1573     // Convert function_entry_count attribute to metadata.
1574     if (std::optional<uint64_t> entryCount = function.getFunctionEntryCount())
1575       llvmFunc->setEntryCount(entryCount.value());
1576 
1577     // Convert result attributes.
1578     if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) {
1579       DictionaryAttr resultAttrs = cast<DictionaryAttr>(allResultAttrs[0]);
1580       FailureOr<llvm::AttrBuilder> attrBuilder =
1581           convertParameterAttrs(function, -1, resultAttrs);
1582       if (failed(attrBuilder))
1583         return failure();
1584       llvmFunc->addRetAttrs(*attrBuilder);
1585     }
1586 
1587     // Convert argument attributes.
1588     for (auto [argIdx, llvmArg] : llvm::enumerate(llvmFunc->args())) {
1589       if (DictionaryAttr argAttrs = function.getArgAttrDict(argIdx)) {
1590         FailureOr<llvm::AttrBuilder> attrBuilder =
1591             convertParameterAttrs(function, argIdx, argAttrs);
1592         if (failed(attrBuilder))
1593           return failure();
1594         llvmArg.addAttrs(*attrBuilder);
1595       }
1596     }
1597 
1598     // Forward the pass-through attributes to LLVM.
1599     if (failed(forwardPassthroughAttributes(
1600             function.getLoc(), function.getPassthrough(), llvmFunc)))
1601       return failure();
1602 
1603     // Convert visibility attribute.
1604     llvmFunc->setVisibility(convertVisibilityToLLVM(function.getVisibility_()));
1605 
1606     // Convert the comdat attribute.
1607     if (std::optional<mlir::SymbolRefAttr> comdat = function.getComdat()) {
1608       auto selectorOp = cast<ComdatSelectorOp>(
1609           SymbolTable::lookupNearestSymbolFrom(function, *comdat));
1610       llvmFunc->setComdat(comdatMapping.lookup(selectorOp));
1611     }
1612 
1613     if (auto gc = function.getGarbageCollector())
1614       llvmFunc->setGC(gc->str());
1615 
1616     if (auto unnamedAddr = function.getUnnamedAddr())
1617       llvmFunc->setUnnamedAddr(convertUnnamedAddrToLLVM(*unnamedAddr));
1618 
1619     if (auto alignment = function.getAlignment())
1620       llvmFunc->setAlignment(llvm::MaybeAlign(*alignment));
1621 
1622     // Translate the debug information for this function.
1623     debugTranslation->translate(function, *llvmFunc);
1624   }
1625 
1626   return success();
1627 }
1628 
1629 LogicalResult ModuleTranslation::convertFunctions() {
1630   // Convert functions.
1631   for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
1632     // Do not convert external functions, but do process dialect attributes
1633     // attached to them.
1634     if (function.isExternal()) {
1635       if (failed(convertDialectAttributes(function, {})))
1636         return failure();
1637       continue;
1638     }
1639 
1640     if (failed(convertOneFunction(function)))
1641       return failure();
1642   }
1643 
1644   return success();
1645 }
1646 
1647 LogicalResult ModuleTranslation::convertComdats() {
1648   for (auto comdatOp : getModuleBody(mlirModule).getOps<ComdatOp>()) {
1649     for (auto selectorOp : comdatOp.getOps<ComdatSelectorOp>()) {
1650       llvm::Module *module = getLLVMModule();
1651       if (module->getComdatSymbolTable().contains(selectorOp.getSymName()))
1652         return emitError(selectorOp.getLoc())
1653                << "comdat selection symbols must be unique even in different "
1654                   "comdat regions";
1655       llvm::Comdat *comdat = module->getOrInsertComdat(selectorOp.getSymName());
1656       comdat->setSelectionKind(convertComdatToLLVM(selectorOp.getComdat()));
1657       comdatMapping.try_emplace(selectorOp, comdat);
1658     }
1659   }
1660   return success();
1661 }
1662 
1663 void ModuleTranslation::setAccessGroupsMetadata(AccessGroupOpInterface op,
1664                                                 llvm::Instruction *inst) {
1665   if (llvm::MDNode *node = loopAnnotationTranslation->getAccessGroups(op))
1666     inst->setMetadata(llvm::LLVMContext::MD_access_group, node);
1667 }
1668 
1669 llvm::MDNode *
1670 ModuleTranslation::getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr) {
1671   auto [scopeIt, scopeInserted] =
1672       aliasScopeMetadataMapping.try_emplace(aliasScopeAttr, nullptr);
1673   if (!scopeInserted)
1674     return scopeIt->second;
1675   llvm::LLVMContext &ctx = llvmModule->getContext();
1676   auto dummy = llvm::MDNode::getTemporary(ctx, std::nullopt);
1677   // Convert the domain metadata node if necessary.
1678   auto [domainIt, insertedDomain] = aliasDomainMetadataMapping.try_emplace(
1679       aliasScopeAttr.getDomain(), nullptr);
1680   if (insertedDomain) {
1681     llvm::SmallVector<llvm::Metadata *, 2> operands;
1682     // Placeholder for self-reference.
1683     operands.push_back(dummy.get());
1684     if (StringAttr description = aliasScopeAttr.getDomain().getDescription())
1685       operands.push_back(llvm::MDString::get(ctx, description));
1686     domainIt->second = llvm::MDNode::get(ctx, operands);
1687     // Self-reference for uniqueness.
1688     domainIt->second->replaceOperandWith(0, domainIt->second);
1689   }
1690   // Convert the scope metadata node.
1691   assert(domainIt->second && "Scope's domain should already be valid");
1692   llvm::SmallVector<llvm::Metadata *, 3> operands;
1693   // Placeholder for self-reference.
1694   operands.push_back(dummy.get());
1695   operands.push_back(domainIt->second);
1696   if (StringAttr description = aliasScopeAttr.getDescription())
1697     operands.push_back(llvm::MDString::get(ctx, description));
1698   scopeIt->second = llvm::MDNode::get(ctx, operands);
1699   // Self-reference for uniqueness.
1700   scopeIt->second->replaceOperandWith(0, scopeIt->second);
1701   return scopeIt->second;
1702 }
1703 
1704 llvm::MDNode *ModuleTranslation::getOrCreateAliasScopes(
1705     ArrayRef<AliasScopeAttr> aliasScopeAttrs) {
1706   SmallVector<llvm::Metadata *> nodes;
1707   nodes.reserve(aliasScopeAttrs.size());
1708   for (AliasScopeAttr aliasScopeAttr : aliasScopeAttrs)
1709     nodes.push_back(getOrCreateAliasScope(aliasScopeAttr));
1710   return llvm::MDNode::get(getLLVMContext(), nodes);
1711 }
1712 
1713 void ModuleTranslation::setAliasScopeMetadata(AliasAnalysisOpInterface op,
1714                                               llvm::Instruction *inst) {
1715   auto populateScopeMetadata = [&](ArrayAttr aliasScopeAttrs, unsigned kind) {
1716     if (!aliasScopeAttrs || aliasScopeAttrs.empty())
1717       return;
1718     llvm::MDNode *node = getOrCreateAliasScopes(
1719         llvm::to_vector(aliasScopeAttrs.getAsRange<AliasScopeAttr>()));
1720     inst->setMetadata(kind, node);
1721   };
1722 
1723   populateScopeMetadata(op.getAliasScopesOrNull(),
1724                         llvm::LLVMContext::MD_alias_scope);
1725   populateScopeMetadata(op.getNoAliasScopesOrNull(),
1726                         llvm::LLVMContext::MD_noalias);
1727 }
1728 
1729 llvm::MDNode *ModuleTranslation::getTBAANode(TBAATagAttr tbaaAttr) const {
1730   return tbaaMetadataMapping.lookup(tbaaAttr);
1731 }
1732 
1733 void ModuleTranslation::setTBAAMetadata(AliasAnalysisOpInterface op,
1734                                         llvm::Instruction *inst) {
1735   ArrayAttr tagRefs = op.getTBAATagsOrNull();
1736   if (!tagRefs || tagRefs.empty())
1737     return;
1738 
1739   // LLVM IR currently does not support attaching more than one TBAA access tag
1740   // to a memory accessing instruction. It may be useful to support this in
1741   // future, but for the time being just ignore the metadata if MLIR operation
1742   // has multiple access tags.
1743   if (tagRefs.size() > 1) {
1744     op.emitWarning() << "TBAA access tags were not translated, because LLVM "
1745                         "IR only supports a single tag per instruction";
1746     return;
1747   }
1748 
1749   llvm::MDNode *node = getTBAANode(cast<TBAATagAttr>(tagRefs[0]));
1750   inst->setMetadata(llvm::LLVMContext::MD_tbaa, node);
1751 }
1752 
1753 void ModuleTranslation::setBranchWeightsMetadata(BranchWeightOpInterface op) {
1754   DenseI32ArrayAttr weightsAttr = op.getBranchWeightsOrNull();
1755   if (!weightsAttr)
1756     return;
1757 
1758   llvm::Instruction *inst = isa<CallOp>(op) ? lookupCall(op) : lookupBranch(op);
1759   assert(inst && "expected the operation to have a mapping to an instruction");
1760   SmallVector<uint32_t> weights(weightsAttr.asArrayRef());
1761   inst->setMetadata(
1762       llvm::LLVMContext::MD_prof,
1763       llvm::MDBuilder(getLLVMContext()).createBranchWeights(weights));
1764 }
1765 
1766 LogicalResult ModuleTranslation::createTBAAMetadata() {
1767   llvm::LLVMContext &ctx = llvmModule->getContext();
1768   llvm::IntegerType *offsetTy = llvm::IntegerType::get(ctx, 64);
1769 
1770   // Walk the entire module and create all metadata nodes for the TBAA
1771   // attributes. The code below relies on two invariants of the
1772   // `AttrTypeWalker`:
1773   // 1. Attributes are visited in post-order: Since the attributes create a DAG,
1774   //    this ensures that any lookups into `tbaaMetadataMapping` for child
1775   //    attributes succeed.
1776   // 2. Attributes are only ever visited once: This way we don't leak any
1777   //    LLVM metadata instances.
1778   AttrTypeWalker walker;
1779   walker.addWalk([&](TBAARootAttr root) {
1780     tbaaMetadataMapping.insert(
1781         {root, llvm::MDNode::get(ctx, llvm::MDString::get(ctx, root.getId()))});
1782   });
1783 
1784   walker.addWalk([&](TBAATypeDescriptorAttr descriptor) {
1785     SmallVector<llvm::Metadata *> operands;
1786     operands.push_back(llvm::MDString::get(ctx, descriptor.getId()));
1787     for (TBAAMemberAttr member : descriptor.getMembers()) {
1788       operands.push_back(tbaaMetadataMapping.lookup(member.getTypeDesc()));
1789       operands.push_back(llvm::ConstantAsMetadata::get(
1790           llvm::ConstantInt::get(offsetTy, member.getOffset())));
1791     }
1792 
1793     tbaaMetadataMapping.insert({descriptor, llvm::MDNode::get(ctx, operands)});
1794   });
1795 
1796   walker.addWalk([&](TBAATagAttr tag) {
1797     SmallVector<llvm::Metadata *> operands;
1798 
1799     operands.push_back(tbaaMetadataMapping.lookup(tag.getBaseType()));
1800     operands.push_back(tbaaMetadataMapping.lookup(tag.getAccessType()));
1801 
1802     operands.push_back(llvm::ConstantAsMetadata::get(
1803         llvm::ConstantInt::get(offsetTy, tag.getOffset())));
1804     if (tag.getConstant())
1805       operands.push_back(
1806           llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(offsetTy, 1)));
1807 
1808     tbaaMetadataMapping.insert({tag, llvm::MDNode::get(ctx, operands)});
1809   });
1810 
1811   mlirModule->walk([&](AliasAnalysisOpInterface analysisOpInterface) {
1812     if (auto attr = analysisOpInterface.getTBAATagsOrNull())
1813       walker.walk(attr);
1814   });
1815 
1816   return success();
1817 }
1818 
1819 LogicalResult ModuleTranslation::createIdentMetadata() {
1820   if (auto attr = mlirModule->getAttrOfType<StringAttr>(
1821           LLVMDialect::getIdentAttrName())) {
1822     StringRef ident = attr;
1823     llvm::LLVMContext &ctx = llvmModule->getContext();
1824     llvm::NamedMDNode *namedMd =
1825         llvmModule->getOrInsertNamedMetadata(LLVMDialect::getIdentAttrName());
1826     llvm::MDNode *md = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, ident));
1827     namedMd->addOperand(md);
1828   }
1829 
1830   return success();
1831 }
1832 
1833 LogicalResult ModuleTranslation::createCommandlineMetadata() {
1834   if (auto attr = mlirModule->getAttrOfType<StringAttr>(
1835           LLVMDialect::getCommandlineAttrName())) {
1836     StringRef cmdLine = attr;
1837     llvm::LLVMContext &ctx = llvmModule->getContext();
1838     llvm::NamedMDNode *nmd = llvmModule->getOrInsertNamedMetadata(
1839         LLVMDialect::getCommandlineAttrName());
1840     llvm::MDNode *md =
1841         llvm::MDNode::get(ctx, llvm::MDString::get(ctx, cmdLine));
1842     nmd->addOperand(md);
1843   }
1844 
1845   return success();
1846 }
1847 
1848 void ModuleTranslation::setLoopMetadata(Operation *op,
1849                                         llvm::Instruction *inst) {
1850   LoopAnnotationAttr attr =
1851       TypeSwitch<Operation *, LoopAnnotationAttr>(op)
1852           .Case<LLVM::BrOp, LLVM::CondBrOp>(
1853               [](auto branchOp) { return branchOp.getLoopAnnotationAttr(); });
1854   if (!attr)
1855     return;
1856   llvm::MDNode *loopMD =
1857       loopAnnotationTranslation->translateLoopAnnotation(attr, op);
1858   inst->setMetadata(llvm::LLVMContext::MD_loop, loopMD);
1859 }
1860 
1861 llvm::Type *ModuleTranslation::convertType(Type type) {
1862   return typeTranslator.translateType(type);
1863 }
1864 
1865 /// A helper to look up remapped operands in the value remapping table.
1866 SmallVector<llvm::Value *> ModuleTranslation::lookupValues(ValueRange values) {
1867   SmallVector<llvm::Value *> remapped;
1868   remapped.reserve(values.size());
1869   for (Value v : values)
1870     remapped.push_back(lookupValue(v));
1871   return remapped;
1872 }
1873 
1874 llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
1875   if (!ompBuilder) {
1876     ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
1877     ompBuilder->initialize();
1878 
1879     // Flags represented as top-level OpenMP dialect attributes are set in
1880     // `OpenMPDialectLLVMIRTranslationInterface::amendOperation()`. Here we set
1881     // the default configuration.
1882     ompBuilder->setConfig(llvm::OpenMPIRBuilderConfig(
1883         /* IsTargetDevice = */ false, /* IsGPU = */ false,
1884         /* OpenMPOffloadMandatory = */ false,
1885         /* HasRequiresReverseOffload = */ false,
1886         /* HasRequiresUnifiedAddress = */ false,
1887         /* HasRequiresUnifiedSharedMemory = */ false,
1888         /* HasRequiresDynamicAllocators = */ false));
1889   }
1890   return ompBuilder.get();
1891 }
1892 
1893 llvm::DILocation *ModuleTranslation::translateLoc(Location loc,
1894                                                   llvm::DILocalScope *scope) {
1895   return debugTranslation->translateLoc(loc, scope);
1896 }
1897 
1898 llvm::DIExpression *
1899 ModuleTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
1900   return debugTranslation->translateExpression(attr);
1901 }
1902 
1903 llvm::DIGlobalVariableExpression *
1904 ModuleTranslation::translateGlobalVariableExpression(
1905     LLVM::DIGlobalVariableExpressionAttr attr) {
1906   return debugTranslation->translateGlobalVariableExpression(attr);
1907 }
1908 
1909 llvm::Metadata *ModuleTranslation::translateDebugInfo(LLVM::DINodeAttr attr) {
1910   return debugTranslation->translate(attr);
1911 }
1912 
1913 llvm::RoundingMode
1914 ModuleTranslation::translateRoundingMode(LLVM::RoundingMode rounding) {
1915   return convertRoundingModeToLLVM(rounding);
1916 }
1917 
1918 llvm::fp::ExceptionBehavior ModuleTranslation::translateFPExceptionBehavior(
1919     LLVM::FPExceptionBehavior exceptionBehavior) {
1920   return convertFPExceptionBehaviorToLLVM(exceptionBehavior);
1921 }
1922 
1923 llvm::NamedMDNode *
1924 ModuleTranslation::getOrInsertNamedModuleMetadata(StringRef name) {
1925   return llvmModule->getOrInsertNamedMetadata(name);
1926 }
1927 
1928 void ModuleTranslation::StackFrame::anchor() {}
1929 
1930 static std::unique_ptr<llvm::Module>
1931 prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
1932                   StringRef name) {
1933   m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
1934   auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
1935   // ModuleTranslation can currently only construct modules in the old debug
1936   // info format, so set the flag accordingly.
1937   llvmModule->setNewDbgInfoFormatFlag(false);
1938   if (auto dataLayoutAttr =
1939           m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
1940     llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
1941   } else {
1942     FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout(""));
1943     if (auto iface = dyn_cast<DataLayoutOpInterface>(m)) {
1944       if (DataLayoutSpecInterface spec = iface.getDataLayoutSpec()) {
1945         llvmDataLayout =
1946             translateDataLayout(spec, DataLayout(iface), m->getLoc());
1947       }
1948     } else if (auto mod = dyn_cast<ModuleOp>(m)) {
1949       if (DataLayoutSpecInterface spec = mod.getDataLayoutSpec()) {
1950         llvmDataLayout =
1951             translateDataLayout(spec, DataLayout(mod), m->getLoc());
1952       }
1953     }
1954     if (failed(llvmDataLayout))
1955       return nullptr;
1956     llvmModule->setDataLayout(*llvmDataLayout);
1957   }
1958   if (auto targetTripleAttr =
1959           m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
1960     llvmModule->setTargetTriple(cast<StringAttr>(targetTripleAttr).getValue());
1961 
1962   return llvmModule;
1963 }
1964 
1965 std::unique_ptr<llvm::Module>
1966 mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
1967                               StringRef name, bool disableVerification) {
1968   if (!satisfiesLLVMModule(module)) {
1969     module->emitOpError("can not be translated to an LLVMIR module");
1970     return nullptr;
1971   }
1972 
1973   std::unique_ptr<llvm::Module> llvmModule =
1974       prepareLLVMModule(module, llvmContext, name);
1975   if (!llvmModule)
1976     return nullptr;
1977 
1978   LLVM::ensureDistinctSuccessors(module);
1979   LLVM::legalizeDIExpressionsRecursively(module);
1980 
1981   ModuleTranslation translator(module, std::move(llvmModule));
1982   llvm::IRBuilder<> llvmBuilder(llvmContext);
1983 
1984   // Convert module before functions and operations inside, so dialect
1985   // attributes can be used to change dialect-specific global configurations via
1986   // `amendOperation()`. These configurations can then influence the translation
1987   // of operations afterwards.
1988   if (failed(translator.convertOperation(*module, llvmBuilder)))
1989     return nullptr;
1990 
1991   if (failed(translator.convertComdats()))
1992     return nullptr;
1993   if (failed(translator.convertFunctionSignatures()))
1994     return nullptr;
1995   if (failed(translator.convertGlobals()))
1996     return nullptr;
1997   if (failed(translator.createTBAAMetadata()))
1998     return nullptr;
1999   if (failed(translator.createIdentMetadata()))
2000     return nullptr;
2001   if (failed(translator.createCommandlineMetadata()))
2002     return nullptr;
2003 
2004   // Convert other top-level operations if possible.
2005   for (Operation &o : getModuleBody(module).getOperations()) {
2006     if (!isa<LLVM::LLVMFuncOp, LLVM::GlobalOp, LLVM::GlobalCtorsOp,
2007              LLVM::GlobalDtorsOp, LLVM::ComdatOp>(&o) &&
2008         !o.hasTrait<OpTrait::IsTerminator>() &&
2009         failed(translator.convertOperation(o, llvmBuilder))) {
2010       return nullptr;
2011     }
2012   }
2013 
2014   // Operations in function bodies with symbolic references must be converted
2015   // after the top-level operations they refer to are declared, so we do it
2016   // last.
2017   if (failed(translator.convertFunctions()))
2018     return nullptr;
2019 
2020   // Once we've finished constructing elements in the module, we should convert
2021   // it to use the debug info format desired by LLVM.
2022   // See https://llvm.org/docs/RemoveDIsDebugInfo.html
2023   translator.llvmModule->setIsNewDbgInfoFormat(UseNewDbgInfoFormat);
2024 
2025   if (!disableVerification &&
2026       llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
2027     return nullptr;
2028 
2029   return std::move(translator.llvmModule);
2030 }
2031