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