MLIR 24.0.0git
ModuleTranslation.cpp
Go to the documentation of this file.
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
15
16#include "AttrKindDetail.h"
17#include "DebugTranslation.h"
26#include "mlir/IR/Attributes.h"
27#include "mlir/IR/BuiltinOps.h"
30#include "mlir/Support/LLVM.h"
33
34#include "llvm/ADT/DenseSet.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/StringExtras.h"
37#include "llvm/ADT/TypeSwitch.h"
38#include "llvm/Analysis/TargetFolder.h"
39#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/CFG.h"
42#include "llvm/IR/Constants.h"
43#include "llvm/IR/DerivedTypes.h"
44#include "llvm/IR/IRBuilder.h"
45#include "llvm/IR/InlineAsm.h"
46#include "llvm/IR/LLVMContext.h"
47#include "llvm/IR/MDBuilder.h"
48#include "llvm/IR/Metadata.h"
49#include "llvm/IR/Module.h"
50#include "llvm/IR/Verifier.h"
51#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
53#include "llvm/Support/VirtualFileSystem.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 <numeric>
59#include <optional>
60
61#define DEBUG_TYPE "llvm-dialect-to-llvm-ir"
62
63using namespace mlir;
64using namespace mlir::LLVM;
65using namespace mlir::LLVM::detail;
66
67#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
68
69namespace {
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`.
90class InstructionCapturingInserter : public llvm::IRBuilderCallbackInserter {
91public:
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
136private:
137 /// List of captured instructions.
138 SmallVector<llvm::Instruction *> capturedInstructions;
139
140 /// Whether the collection is enabled.
141 bool enabled = false;
142};
143
144using CapturingIRBuilder =
145 llvm::IRBuilder<llvm::TargetFolder, InstructionCapturingInserter>;
146} // namespace
147
148InstructionCapturingInserter::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
162InstructionCapturingInserter::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.
178static FailureOr<llvm::DataLayout>
179translateDataLayout(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 continue;
198 }
199 if (key.getValue() == DLTIDialect::kDataLayoutManglingModeKey) {
200 auto value = cast<StringAttr>(entry.getValue());
201 layoutStream << "-m:" << value.getValue();
202 continue;
203 }
204 if (key.getValue() == DLTIDialect::kDataLayoutProgramMemorySpaceKey) {
205 auto value = cast<IntegerAttr>(entry.getValue());
206 uint64_t space = value.getValue().getZExtValue();
207 // Skip the default address space.
208 if (space == 0)
209 continue;
210 layoutStream << "-P" << space;
211 continue;
212 }
213 if (key.getValue() == DLTIDialect::kDataLayoutGlobalMemorySpaceKey) {
214 auto value = cast<IntegerAttr>(entry.getValue());
215 uint64_t space = value.getValue().getZExtValue();
216 // Skip the default address space.
217 if (space == 0)
218 continue;
219 layoutStream << "-G" << space;
220 continue;
221 }
222 if (key.getValue() == DLTIDialect::kDataLayoutAllocaMemorySpaceKey) {
223 auto value = cast<IntegerAttr>(entry.getValue());
224 uint64_t space = value.getValue().getZExtValue();
225 // Skip the default address space.
226 if (space == 0)
227 continue;
228 layoutStream << "-A" << space;
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 continue;
239 }
240 if (key.getValue() == DLTIDialect::kDataLayoutFunctionPointerAlignmentKey) {
241 auto value = cast<FunctionPointerAlignmentAttr>(entry.getValue());
242 uint64_t alignment = value.getAlignment();
243 // Skip the default function pointer alignment.
244 if (alignment == 0)
245 continue;
246 layoutStream << "-F" << (value.getFunctionDependent() ? "n" : "i")
247 << alignment;
248 continue;
249 }
250 if (key.getValue() == DLTIDialect::kDataLayoutLegalIntWidthsKey) {
251 layoutStream << "-n";
252 llvm::interleave(
253 cast<DenseI32ArrayAttr>(entry.getValue()).asArrayRef(), layoutStream,
254 [&](int32_t val) { layoutStream << val; }, ":");
255 continue;
256 }
257 emitError(*loc) << "unsupported data layout key " << key;
258 return failure();
259 }
260
261 // Go through the list of entries to check which types are explicitly
262 // specified in entries. Where possible, data layout queries are used instead
263 // of directly inspecting the entries.
264 for (DataLayoutEntryInterface entry : attribute.getEntries()) {
265 auto type = llvm::dyn_cast_if_present<Type>(entry.getKey());
266 if (!type)
267 continue;
268 // Data layout for the index type is irrelevant at this point.
269 if (isa<IndexType>(type))
270 continue;
271 layoutStream << "-";
272 LogicalResult result =
274 .Case<IntegerType, Float16Type, Float32Type, Float64Type,
275 Float80Type, Float128Type>([&](Type type) -> LogicalResult {
276 if (auto intType = dyn_cast<IntegerType>(type)) {
277 if (intType.getSignedness() != IntegerType::Signless)
278 return emitError(*loc)
279 << "unsupported data layout for non-signless integer "
280 << intType;
281 layoutStream << "i";
282 } else {
283 layoutStream << "f";
284 }
285 uint64_t size = dataLayout.getTypeSizeInBits(type);
286 uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
287 uint64_t preferred =
288 dataLayout.getTypePreferredAlignment(type) * 8u;
289 layoutStream << size << ":" << abi;
290 if (abi != preferred)
291 layoutStream << ":" << preferred;
292 return success();
293 })
294 .Case([&](LLVMPointerType type) {
295 layoutStream << "p" << type.getAddressSpace() << ":";
296 uint64_t size = dataLayout.getTypeSizeInBits(type);
297 uint64_t abi = dataLayout.getTypeABIAlignment(type) * 8u;
298 uint64_t preferred =
299 dataLayout.getTypePreferredAlignment(type) * 8u;
300 uint64_t index = *dataLayout.getTypeIndexBitwidth(type);
301 layoutStream << size << ":" << abi << ":" << preferred << ":"
302 << index;
303 return success();
304 })
305 .Default([loc](Type type) {
306 return emitError(*loc)
307 << "unsupported type in data layout: " << type;
308 });
309 if (failed(result))
310 return failure();
311 }
312 StringRef layoutSpec(llvmDataLayout);
313 layoutSpec.consume_front("-");
314
315 return llvm::DataLayout(layoutSpec);
316}
317
318/// Builds a constant of a sequential LLVM type `type`, potentially containing
319/// other sequential types recursively, from the individual constant values
320/// provided in `constants`. `shape` contains the number of elements in nested
321/// sequential types. Reports errors at `loc` and returns nullptr on error.
322static llvm::Constant *
324 ArrayRef<int64_t> shape, llvm::Type *type,
325 Location loc) {
326 if (shape.empty()) {
327 llvm::Constant *result = constants.front();
328 constants = constants.drop_front();
329 return result;
330 }
331
332 llvm::Type *elementType;
333 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
334 elementType = arrayTy->getElementType();
335 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
336 elementType = vectorTy->getElementType();
337 } else {
338 emitError(loc) << "expected sequential LLVM types wrapping a scalar";
339 return nullptr;
340 }
341
343 nested.reserve(shape.front());
344 for (int64_t i = 0; i < shape.front(); ++i) {
345 nested.push_back(buildSequentialConstant(constants, shape.drop_front(),
346 elementType, loc));
347 if (!nested.back())
348 return nullptr;
349 }
350
351 if (shape.size() == 1 && type->isVectorTy())
352 return llvm::ConstantVector::get(nested);
353 return llvm::ConstantArray::get(
354 llvm::ArrayType::get(elementType, shape.front()), nested);
355}
356
357/// Returns the first non-sequential type nested in sequential types.
358static llvm::Type *getInnermostElementType(llvm::Type *type) {
359 do {
360 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(type)) {
361 type = arrayTy->getElementType();
362 } else if (auto *vectorTy = dyn_cast<llvm::VectorType>(type)) {
363 type = vectorTy->getElementType();
364 } else {
365 return type;
366 }
367 } while (true);
368}
369
370/// Convert a dense elements attribute to an LLVM IR constant using its raw data
371/// storage if possible. This supports elements attributes of tensor or vector
372/// type and avoids constructing separate objects for individual values of the
373/// innermost dimension. Constants for other dimensions are still constructed
374/// recursively. Returns null if constructing from raw data is not supported for
375/// this type, e.g., element type is not a power-of-two-sized primitive. Reports
376/// other errors at `loc`.
377static llvm::Constant *
379 llvm::Type *llvmType,
380 const ModuleTranslation &moduleTranslation) {
381 if (!denseElementsAttr)
382 return nullptr;
383
384 llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
385 if (!llvm::ConstantDataSequential::isElementTypeCompatible(innermostLLVMType))
386 return nullptr;
387
388 ShapedType type = denseElementsAttr.getType();
389 if (type.getNumElements() == 0)
390 return nullptr;
391
392 // Check that the raw data size matches what is expected for the scalar size.
393 // TODO: in theory, we could repack the data here to keep constructing from
394 // raw data.
395 // TODO: we may also need to consider endianness when cross-compiling to an
396 // architecture where it is different.
397 int64_t elementByteSize = denseElementsAttr.getRawData().size() /
398 denseElementsAttr.getNumElements();
399 if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits())
400 return nullptr;
401
402 // Compute the shape of all dimensions but the innermost. Note that the
403 // innermost dimension may be that of the vector element type.
404 bool hasVectorElementType = isa<VectorType>(type.getElementType());
405 int64_t numAggregates =
406 denseElementsAttr.getNumElements() /
407 (hasVectorElementType ? 1
408 : denseElementsAttr.getType().getShape().back());
409 ArrayRef<int64_t> outerShape = type.getShape();
410 if (!hasVectorElementType)
411 outerShape = outerShape.drop_back();
412
413 // Handle the case of vector splat, LLVM has special support for it.
414 if (denseElementsAttr.isSplat() &&
415 (isa<VectorType>(type) || hasVectorElementType)) {
416 llvm::Constant *splatValue = LLVM::detail::getLLVMConstant(
417 innermostLLVMType, denseElementsAttr.getSplatValue<Attribute>(), loc,
418 moduleTranslation);
419 llvm::Constant *splatVector =
420 llvm::ConstantDataVector::getSplat(0, splatValue);
421 SmallVector<llvm::Constant *> constants(numAggregates, splatVector);
422 ArrayRef<llvm::Constant *> constantsRef = constants;
423 return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
424 }
425 if (denseElementsAttr.isSplat())
426 return nullptr;
427
428 // In case of non-splat, create a constructor for the innermost constant from
429 // a piece of raw data.
430 std::function<llvm::Constant *(StringRef)> buildCstData;
431 if (isa<TensorType>(type)) {
432 auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
433 if (vectorElementType && vectorElementType.getRank() == 1) {
434 buildCstData = [&](StringRef data) {
435 return llvm::ConstantDataVector::getRaw(
436 data, vectorElementType.getShape().back(), innermostLLVMType);
437 };
438 } else if (!vectorElementType) {
439 buildCstData = [&](StringRef data) {
440 return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
441 innermostLLVMType);
442 };
443 }
444 } else if (isa<VectorType>(type)) {
445 buildCstData = [&](StringRef data) {
446 return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
447 innermostLLVMType);
448 };
449 }
450 if (!buildCstData)
451 return nullptr;
452
453 // Create innermost constants and defer to the default constant creation
454 // mechanism for other dimensions.
456 int64_t aggregateSize = denseElementsAttr.getType().getShape().back() *
457 (innermostLLVMType->getScalarSizeInBits() / 8);
458 constants.reserve(numAggregates);
459 for (unsigned i = 0; i < numAggregates; ++i) {
460 StringRef data(denseElementsAttr.getRawData().data() + i * aggregateSize,
461 aggregateSize);
462 constants.push_back(buildCstData(data));
463 }
464
465 ArrayRef<llvm::Constant *> constantsRef = constants;
466 return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
467}
468
469/// Convert a dense resource elements attribute to an LLVM IR constant using its
470/// raw data storage if possible. This supports elements attributes of tensor or
471/// vector type and avoids constructing separate objects for individual values
472/// of the innermost dimension. Constants for other dimensions are still
473/// constructed recursively. Returns nullptr on failure and emits errors at
474/// `loc`.
475static llvm::Constant *convertDenseResourceElementsAttr(
476 Location loc, DenseResourceElementsAttr denseResourceAttr,
477 llvm::Type *llvmType, const ModuleTranslation &moduleTranslation) {
478 assert(denseResourceAttr && "expected non-null attribute");
479
480 llvm::Type *innermostLLVMType = getInnermostElementType(llvmType);
481 if (!llvm::ConstantDataSequential::isElementTypeCompatible(
482 innermostLLVMType)) {
483 emitError(loc, "no known conversion for innermost element type");
484 return nullptr;
485 }
486
487 ShapedType type = denseResourceAttr.getType();
488 assert(type.getNumElements() > 0 && "Expected non-empty elements attribute");
489
490 AsmResourceBlob *blob = denseResourceAttr.getRawHandle().getBlob();
491 if (!blob) {
492 emitError(loc, "resource does not exist");
493 return nullptr;
494 }
495
496 ArrayRef<char> rawData = blob->getData();
497
498 // Check that the raw data size matches what is expected for the scalar size.
499 // TODO: in theory, we could repack the data here to keep constructing from
500 // raw data.
501 // TODO: we may also need to consider endianness when cross-compiling to an
502 // architecture where it is different.
503 int64_t numElements = denseResourceAttr.getType().getNumElements();
504 int64_t elementByteSize = rawData.size() / numElements;
505 if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits()) {
506 emitError(loc, "raw data size does not match element type size");
507 return nullptr;
508 }
509
510 // Compute the shape of all dimensions but the innermost. Note that the
511 // innermost dimension may be that of the vector element type.
512 bool hasVectorElementType = isa<VectorType>(type.getElementType());
513 int64_t numAggregates =
514 numElements / (hasVectorElementType
515 ? 1
516 : denseResourceAttr.getType().getShape().back());
517 ArrayRef<int64_t> outerShape = type.getShape();
518 if (!hasVectorElementType)
519 outerShape = outerShape.drop_back();
520
521 // Create a constructor for the innermost constant from a piece of raw data.
522 std::function<llvm::Constant *(StringRef)> buildCstData;
523 if (isa<TensorType>(type)) {
524 auto vectorElementType = dyn_cast<VectorType>(type.getElementType());
525 if (vectorElementType && vectorElementType.getRank() == 1) {
526 buildCstData = [&](StringRef data) {
527 return llvm::ConstantDataVector::getRaw(
528 data, vectorElementType.getShape().back(), innermostLLVMType);
529 };
530 } else if (!vectorElementType) {
531 buildCstData = [&](StringRef data) {
532 return llvm::ConstantDataArray::getRaw(data, type.getShape().back(),
533 innermostLLVMType);
534 };
535 }
536 } else if (isa<VectorType>(type)) {
537 buildCstData = [&](StringRef data) {
538 return llvm::ConstantDataVector::getRaw(data, type.getShape().back(),
539 innermostLLVMType);
540 };
541 }
542 if (!buildCstData) {
543 emitError(loc, "unsupported dense_resource type");
544 return nullptr;
545 }
546
547 // Create innermost constants and defer to the default constant creation
548 // mechanism for other dimensions.
550 int64_t aggregateSize = denseResourceAttr.getType().getShape().back() *
551 (innermostLLVMType->getScalarSizeInBits() / 8);
552 constants.reserve(numAggregates);
553 for (unsigned i = 0; i < numAggregates; ++i) {
554 StringRef data(rawData.data() + i * aggregateSize, aggregateSize);
555 constants.push_back(buildCstData(data));
556 }
557
558 ArrayRef<llvm::Constant *> constantsRef = constants;
559 return buildSequentialConstant(constantsRef, outerShape, llvmType, loc);
560}
561
562/// Create an LLVM IR constant of `llvmType` from the MLIR attribute `attr`.
563/// This currently supports integer, floating point, splat and dense element
564/// attributes and combinations thereof. Also, an array attribute with two
565/// elements is supported to represent a complex constant. In case of error,
566/// report it to `loc` and return nullptr.
568 llvm::Type *llvmType, Attribute attr, Location loc,
569 const ModuleTranslation &moduleTranslation) {
570 if (!attr || isa<UndefAttr>(attr))
571 return llvm::UndefValue::get(llvmType);
572 if (isa<ZeroAttr>(attr))
573 return llvm::Constant::getNullValue(llvmType);
574 if (auto *structType = dyn_cast<::llvm::StructType>(llvmType)) {
575 auto arrayAttr = dyn_cast<ArrayAttr>(attr);
576 if (!arrayAttr) {
577 emitError(loc, "expected an array attribute for a struct constant");
578 return nullptr;
579 }
580 SmallVector<llvm::Constant *> structElements;
581 structElements.reserve(structType->getNumElements());
582 for (auto [elemType, elemAttr] :
583 zip_equal(structType->elements(), arrayAttr)) {
584 llvm::Constant *element =
585 getLLVMConstant(elemType, elemAttr, loc, moduleTranslation);
586 if (!element)
587 return nullptr;
588 structElements.push_back(element);
589 }
590 return llvm::ConstantStruct::get(structType, structElements);
591 }
592 // For integer types, we allow a mismatch in sizes as the index type in
593 // MLIR might have a different size than the index type in the LLVM module.
594 if (auto intAttr = dyn_cast<IntegerAttr>(attr)) {
595 // If the attribute is an unsigned integer or a 1-bit integer, zero-extend
596 // the value to the bit width of the LLVM type. Otherwise, sign-extend.
597 auto intTy = dyn_cast<IntegerType>(intAttr.getType());
598 APInt value;
599 if (intTy && (intTy.isUnsigned() || intTy.getWidth() == 1))
600 value = intAttr.getValue().zextOrTrunc(llvmType->getIntegerBitWidth());
601 else
602 value = intAttr.getValue().sextOrTrunc(llvmType->getIntegerBitWidth());
603 return llvm::ConstantInt::get(llvmType, value);
604 }
605 if (auto floatAttr = dyn_cast<FloatAttr>(attr)) {
606 const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
607 // Special case for 8-bit floats, which are represented by integers due to
608 // the lack of native fp8 types in LLVM at the moment. Additionally, handle
609 // targets (like AMDGPU) that don't implement bfloat and convert all bfloats
610 // to i16.
611 unsigned floatWidth = APFloat::getSizeInBits(sem);
612 if (llvmType->isIntegerTy(floatWidth))
613 return llvm::ConstantInt::get(llvmType,
614 floatAttr.getValue().bitcastToAPInt());
615 if (llvmType !=
616 llvm::Type::getFloatingPointTy(llvmType->getContext(),
617 floatAttr.getValue().getSemantics())) {
618 emitError(loc, "FloatAttr does not match expected type of the constant");
619 return nullptr;
620 }
621 return llvm::ConstantFP::get(llvmType, floatAttr.getValue());
622 }
623 if (auto symAttr = dyn_cast<FlatSymbolRefAttr>(attr)) {
624 StringRef name = symAttr.getValue();
625 if (llvm::Function *func = moduleTranslation.lookupFunction(name))
626 return llvm::ConstantExpr::getBitCast(func, llvmType);
627 if (llvm::GlobalValue *global = moduleTranslation.lookupGlobal(name))
628 return llvm::ConstantExpr::getBitCast(global, llvmType);
629 emitError(loc, "unknown symbol reference '") << name << "' in constant";
630 return nullptr;
631 }
632 if (auto splatAttr = dyn_cast<SplatElementsAttr>(attr)) {
633 llvm::Type *elementType;
634 uint64_t numElements;
635 bool isScalable = false;
636 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
637 elementType = arrayTy->getElementType();
638 numElements = arrayTy->getNumElements();
639 } else if (auto *fVectorTy = dyn_cast<llvm::FixedVectorType>(llvmType)) {
640 elementType = fVectorTy->getElementType();
641 numElements = fVectorTy->getNumElements();
642 } else if (auto *sVectorTy = dyn_cast<llvm::ScalableVectorType>(llvmType)) {
643 elementType = sVectorTy->getElementType();
644 numElements = sVectorTy->getMinNumElements();
645 isScalable = true;
646 } else {
647 llvm_unreachable("unrecognized constant vector type");
648 }
649 // Splat value is a scalar. Extract it only if the element type is not
650 // another sequence type. The recursion terminates because each step removes
651 // one outer sequential type.
652 bool elementTypeSequential =
653 isa<llvm::ArrayType, llvm::VectorType>(elementType);
654 llvm::Constant *child = getLLVMConstant(
655 elementType,
656 elementTypeSequential ? splatAttr
657 : splatAttr.getSplatValue<Attribute>(),
658 loc, moduleTranslation);
659 if (!child)
660 return nullptr;
661 if (llvmType->isVectorTy())
662 return llvm::ConstantVector::getSplat(
663 llvm::ElementCount::get(numElements, /*Scalable=*/isScalable), child);
664 if (llvmType->isArrayTy()) {
665 auto *arrayType = llvm::ArrayType::get(elementType, numElements);
666 if (child->isNullValue() && !elementType->isFPOrFPVectorTy()) {
667 return llvm::ConstantAggregateZero::get(arrayType);
668 }
669 if (llvm::ConstantDataSequential::isElementTypeCompatible(elementType)) {
670 if (isa<llvm::IntegerType>(elementType)) {
671 if (llvm::ConstantInt *ci = dyn_cast<llvm::ConstantInt>(child)) {
672 if (ci->getBitWidth() == 8) {
673 SmallVector<int8_t> constants(numElements, ci->getZExtValue());
674 return llvm::ConstantDataArray::get(elementType->getContext(),
675 constants);
676 }
677 if (ci->getBitWidth() == 16) {
678 SmallVector<int16_t> constants(numElements, ci->getZExtValue());
679 return llvm::ConstantDataArray::get(elementType->getContext(),
680 constants);
681 }
682 if (ci->getBitWidth() == 32) {
683 SmallVector<int32_t> constants(numElements, ci->getZExtValue());
684 return llvm::ConstantDataArray::get(elementType->getContext(),
685 constants);
686 }
687 if (ci->getBitWidth() == 64) {
688 SmallVector<int64_t> constants(numElements, ci->getZExtValue());
689 return llvm::ConstantDataArray::get(elementType->getContext(),
690 constants);
691 }
692 }
693 }
694 if (elementType->isFloatingPointTy()) {
695 if (llvm::ConstantFP *cfp = dyn_cast<llvm::ConstantFP>(child)) {
696 APInt bitPattern = cfp->getValueAPF().bitcastToAPInt();
697 uint64_t value = bitPattern.getZExtValue();
698 // TODO: This code only handles 16, 32, and 64 bit floats. Handle
699 // all compatible types, fp8, fp4, etc.
700 if (bitPattern.getBitWidth() == 16) {
701 SmallVector<uint16_t> constants(numElements, value);
702 return llvm::ConstantDataArray::getFP(elementType, constants);
703 }
704 if (bitPattern.getBitWidth() == 32) {
705 SmallVector<uint32_t> constants(numElements, value);
706 return llvm::ConstantDataArray::getFP(elementType, constants);
707 }
708 if (bitPattern.getBitWidth() == 64) {
709 SmallVector<uint64_t> constants(numElements, value);
710 return llvm::ConstantDataArray::getFP(elementType, constants);
711 }
712 }
713 }
714 }
715 // std::vector is used here to accomodate large number of elements that
716 // exceed SmallVector capacity.
717 std::vector<llvm::Constant *> constants(numElements, child);
718 return llvm::ConstantArray::get(arrayType, constants);
719 }
720 }
721
722 // Try using raw elements data if possible.
723 if (llvm::Constant *result =
724 convertDenseElementsAttr(loc, dyn_cast<DenseElementsAttr>(attr),
725 llvmType, moduleTranslation)) {
726 return result;
727 }
728
729 if (auto denseResourceAttr = dyn_cast<DenseResourceElementsAttr>(attr)) {
730 return convertDenseResourceElementsAttr(loc, denseResourceAttr, llvmType,
731 moduleTranslation);
732 }
733
734 // Fall back to element-by-element construction otherwise.
735 if (auto elementsAttr = dyn_cast<ElementsAttr>(attr)) {
736 assert(elementsAttr.getShapedType().hasStaticShape());
737 assert(!elementsAttr.getShapedType().getShape().empty() &&
738 "unexpected empty elements attribute shape");
739
740 SmallVector<llvm::Constant *, 8> constants;
741 constants.reserve(elementsAttr.getNumElements());
742 llvm::Type *innermostType = getInnermostElementType(llvmType);
743 for (auto n : elementsAttr.getValues<Attribute>()) {
744 constants.push_back(
745 getLLVMConstant(innermostType, n, loc, moduleTranslation));
746 if (!constants.back())
747 return nullptr;
748 }
749 ArrayRef<llvm::Constant *> constantsRef = constants;
750 llvm::Constant *result = buildSequentialConstant(
751 constantsRef, elementsAttr.getShapedType().getShape(), llvmType, loc);
752 assert(constantsRef.empty() && "did not consume all elemental constants");
753 return result;
754 }
755
756 if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
757 return llvm::ConstantDataArray::get(moduleTranslation.getLLVMContext(),
758 ArrayRef<char>{stringAttr.getValue()});
759 }
760
761 // Handle arrays of structs that cannot be represented as DenseElementsAttr
762 // in MLIR.
763 if (auto arrayAttr = dyn_cast<ArrayAttr>(attr)) {
764 if (auto *arrayTy = dyn_cast<llvm::ArrayType>(llvmType)) {
765 llvm::Type *elementType = arrayTy->getElementType();
766 Attribute previousElementAttr;
767 llvm::Constant *elementCst = nullptr;
768 SmallVector<llvm::Constant *> constants;
769 constants.reserve(arrayTy->getNumElements());
770 for (Attribute elementAttr : arrayAttr) {
771 // Arrays with a single value or with repeating values are quite common.
772 // Short-circuit the translation when the element value is the same as
773 // the previous one.
774 if (!previousElementAttr || previousElementAttr != elementAttr) {
775 previousElementAttr = elementAttr;
776 elementCst =
777 getLLVMConstant(elementType, elementAttr, loc, moduleTranslation);
778 if (!elementCst)
779 return nullptr;
780 }
781 constants.push_back(elementCst);
782 }
783 return llvm::ConstantArray::get(arrayTy, constants);
784 }
785 }
786
787 emitError(loc, "unsupported constant value");
788 return nullptr;
789}
790
791ModuleTranslation::ModuleTranslation(Operation *module,
792 std::unique_ptr<llvm::Module> llvmModule,
793 llvm::vfs::FileSystem *fs)
794 : mlirModule(module), llvmModule(std::move(llvmModule)),
795 debugTranslation(
796 std::make_unique<DebugTranslation>(module, *this->llvmModule)),
797 loopAnnotationTranslation(std::make_unique<LoopAnnotationTranslation>(
798 *this, *this->llvmModule)),
799 fileSystem(fs), typeTranslator(this->llvmModule->getContext()),
800 iface(module->getContext()) {
801 assert(satisfiesLLVMModule(mlirModule) &&
802 "mlirModule should honor LLVM's module semantics.");
803}
804
805ModuleTranslation::~ModuleTranslation() {
806 if (ompBuilder && !ompBuilder->isFinalized())
807 ompBuilder->finalize();
808}
809
811 SmallVector<Region *> toProcess;
812 toProcess.push_back(&region);
813 while (!toProcess.empty()) {
814 Region *current = toProcess.pop_back_val();
815 for (Block &block : *current) {
816 blockMapping.erase(&block);
817 for (Value arg : block.getArguments())
818 valueMapping.erase(arg);
819 for (Operation &op : block) {
820 for (Value value : op.getResults())
821 valueMapping.erase(value);
822 if (op.hasSuccessors())
823 branchMapping.erase(&op);
824 if (isa<LLVM::GlobalOp>(op))
825 globalsMapping.erase(&op);
826 if (isa<LLVM::AliasOp>(op))
827 aliasesMapping.erase(&op);
828 if (isa<LLVM::IFuncOp>(op))
829 ifuncMapping.erase(&op);
830 if (isa<LLVM::CallOp>(op))
831 callMapping.erase(&op);
832 llvm::append_range(
833 toProcess,
834 llvm::map_range(op.getRegions(), [](Region &r) { return &r; }));
835 }
836 }
837 }
838}
839
840/// Get the SSA value passed to the current block from the terminator operation
841/// of its predecessor.
842static Value getPHISourceValue(Block *current, Block *pred,
843 unsigned numArguments, unsigned index) {
844 Operation &terminator = *pred->getTerminator();
845 if (isa<LLVM::BrOp>(terminator))
846 return terminator.getOperand(index);
847
848#ifndef NDEBUG
849 llvm::SmallPtrSet<Block *, 4> seenSuccessors;
850 for (unsigned i = 0, e = terminator.getNumSuccessors(); i < e; ++i) {
851 Block *successor = terminator.getSuccessor(i);
852 auto branch = cast<BranchOpInterface>(terminator);
853 SuccessorOperands successorOperands = branch.getSuccessorOperands(i);
854 assert(
855 (!seenSuccessors.contains(successor) || successorOperands.empty()) &&
856 "successors with arguments in LLVM branches must be different blocks");
857 seenSuccessors.insert(successor);
858 }
859#endif
860
861 // For instructions that branch based on a condition value, we need to take
862 // the operands for the branch that was taken.
863 if (auto condBranchOp = dyn_cast<LLVM::CondBrOp>(terminator)) {
864 // For conditional branches, we take the operands from either the "true" or
865 // the "false" branch.
866 return condBranchOp.getSuccessor(0) == current
867 ? condBranchOp.getTrueDestOperands()[index]
868 : condBranchOp.getFalseDestOperands()[index];
869 }
870
871 if (auto switchOp = dyn_cast<LLVM::SwitchOp>(terminator)) {
872 // For switches, we take the operands from either the default case, or from
873 // the case branch that was taken.
874 if (switchOp.getDefaultDestination() == current)
875 return switchOp.getDefaultOperands()[index];
876 for (const auto &i : llvm::enumerate(switchOp.getCaseDestinations()))
877 if (i.value() == current)
878 return switchOp.getCaseOperands(i.index())[index];
879 }
880
881 if (auto indBrOp = dyn_cast<LLVM::IndirectBrOp>(terminator)) {
882 // For indirect branches we take operands for each successor.
883 for (const auto &i : llvm::enumerate(indBrOp->getSuccessors())) {
884 if (indBrOp->getSuccessor(i.index()) == current)
885 return indBrOp.getSuccessorOperands(i.index())[index];
886 }
887 }
888
889 if (auto invokeOp = dyn_cast<LLVM::InvokeOp>(terminator)) {
890 return invokeOp.getNormalDest() == current
891 ? invokeOp.getNormalDestOperands()[index]
892 : invokeOp.getUnwindDestOperands()[index];
893 }
894
895 llvm_unreachable(
896 "only branch, switch or invoke operations can be terminators "
897 "of a block that has successors");
898}
899
900/// Connect the PHI nodes to the results of preceding blocks.
902 const ModuleTranslation &state) {
903 // Skip the first block, it cannot be branched to and its arguments correspond
904 // to the arguments of the LLVM function.
905 for (Block &bb : llvm::drop_begin(region)) {
906 llvm::BasicBlock *llvmBB = state.lookupBlock(&bb);
907 auto phis = llvmBB->phis();
908 auto numArguments = bb.getNumArguments();
909 assert(numArguments == std::distance(phis.begin(), phis.end()));
910 for (auto [index, phiNode] : llvm::enumerate(phis)) {
911 for (auto *pred : bb.getPredecessors()) {
912 // Find the LLVM IR block that contains the converted terminator
913 // instruction and use it in the PHI node. Note that this block is not
914 // necessarily the same as state.lookupBlock(pred), some operations
915 // (in particular, OpenMP operations using OpenMPIRBuilder) may have
916 // split the blocks.
917 llvm::Instruction *terminator =
918 state.lookupBranch(pred->getTerminator());
919 assert(terminator && "missing the mapping for a terminator");
920 phiNode.addIncoming(state.lookupValue(getPHISourceValue(
921 &bb, pred, numArguments, index)),
922 terminator->getParent());
923 }
924 }
925 }
926}
927
929 llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
931 return builder.CreateIntrinsicWithoutFolding(intrinsic, tys, args);
932}
933
935 llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
936 llvm::Type *retTy, ArrayRef<llvm::Value *> args) {
937 return builder.CreateIntrinsicWithoutFolding(retTy, intrinsic, args);
938}
939
941 llvm::IRBuilderBase &builder, ModuleTranslation &moduleTranslation,
942 Operation *intrOp, llvm::Intrinsic::ID intrinsic, unsigned numResults,
943 ArrayRef<unsigned> overloadedResults, ArrayRef<unsigned> overloadedOperands,
944 ArrayRef<unsigned> immArgPositions,
945 ArrayRef<StringLiteral> immArgAttrNames) {
946 assert(immArgPositions.size() == immArgAttrNames.size() &&
947 "LLVM `immArgPositions` and MLIR `immArgAttrNames` should have equal "
948 "length");
949
951 size_t numOpBundleOperands = 0;
952 auto opBundleSizesAttr = cast_if_present<DenseI32ArrayAttr>(
953 intrOp->getInherentAttr(LLVMDialect::getOpBundleSizesAttrName())
954 .value_or(Attribute{}));
955 auto opBundleTagsAttr = cast_if_present<ArrayAttr>(
956 intrOp->getInherentAttr(LLVMDialect::getOpBundleTagsAttrName())
957 .value_or(Attribute{}));
958
959 if (opBundleSizesAttr && opBundleTagsAttr) {
960 ArrayRef<int> opBundleSizes = opBundleSizesAttr.asArrayRef();
961 assert(opBundleSizes.size() == opBundleTagsAttr.size() &&
962 "operand bundles and tags do not match");
963
964 numOpBundleOperands = llvm::sum_of(opBundleSizes);
965 assert(numOpBundleOperands <= intrOp->getNumOperands() &&
966 "operand bundle operands is more than the number of operands");
967
968 ValueRange operands = intrOp->getOperands().take_back(numOpBundleOperands);
969 size_t nextOperandIdx = 0;
970 opBundles.reserve(opBundleSizesAttr.size());
971
972 for (auto [opBundleTagAttr, bundleSize] :
973 llvm::zip(opBundleTagsAttr, opBundleSizes)) {
974 auto bundleTag = cast<StringAttr>(opBundleTagAttr).str();
975 auto bundleOperands = moduleTranslation.lookupValues(
976 operands.slice(nextOperandIdx, bundleSize));
977 opBundles.emplace_back(std::move(bundleTag), std::move(bundleOperands));
978 nextOperandIdx += bundleSize;
979 }
980 }
981
982 // Map operands and attributes to LLVM values.
983 auto opOperands = intrOp->getOperands().drop_back(numOpBundleOperands);
984 auto operands = moduleTranslation.lookupValues(opOperands);
985 SmallVector<llvm::Value *> args(immArgPositions.size() + operands.size());
986 for (auto [immArgPos, immArgName] :
987 llvm::zip(immArgPositions, immArgAttrNames)) {
988 Attribute attr = intrOp->getInherentAttr(immArgName).value_or(Attribute{});
989 if (auto intrinsicIntegerAttr =
990 dyn_cast<LLVM::IntrinsicIntegerAttrInterface>(attr))
991 attr = intrinsicIntegerAttr.getIntegerAttr();
992 auto typedAttr = llvm::cast<TypedAttr>(attr);
993 assert(typedAttr.getType().isIntOrFloat() &&
994 "expected int or float immarg");
995 auto *type = moduleTranslation.convertType(typedAttr.getType());
996 args[immArgPos] = LLVM::detail::getLLVMConstant(
997 type, typedAttr, intrOp->getLoc(), moduleTranslation);
998 }
999 unsigned opArg = 0;
1000 for (auto &arg : args) {
1001 if (!arg)
1002 arg = operands[opArg++];
1003 }
1004
1005 // Resolve overloaded intrinsic declaration.
1006 SmallVector<llvm::Type *> overloadedTypes;
1007 for (unsigned overloadedResultIdx : overloadedResults) {
1008 if (numResults > 1) {
1009 // More than one result is mapped to an LLVM struct.
1010 overloadedTypes.push_back(moduleTranslation.convertType(
1011 llvm::cast<LLVM::LLVMStructType>(intrOp->getResult(0).getType())
1012 .getBody()[overloadedResultIdx]));
1013 } else {
1014 overloadedTypes.push_back(
1015 moduleTranslation.convertType(intrOp->getResult(0).getType()));
1016 }
1017 }
1018 for (unsigned overloadedOperandIdx : overloadedOperands)
1019 overloadedTypes.push_back(args[overloadedOperandIdx]->getType());
1020 llvm::Module *module = builder.getModule();
1021 llvm::Function *llvmIntr = llvm::Intrinsic::getOrInsertDeclaration(
1022 module, intrinsic, overloadedTypes);
1023
1024 return builder.CreateCall(llvmIntr, args, opBundles);
1025}
1026
1027/// Given a single MLIR operation, create the corresponding LLVM IR operation
1028/// using the `builder`.
1029LogicalResult ModuleTranslation::convertOperationImpl(
1030 Operation &op, llvm::IRBuilderBase &builder, bool recordInsertions) {
1031 const LLVMTranslationDialectInterface *opIface = iface.getInterfaceFor(&op);
1032 if (!opIface)
1033 return op.emitError("cannot be converted to LLVM IR: missing "
1034 "`LLVMTranslationDialectInterface` registration for "
1035 "dialect for op: ")
1036 << op.getName();
1037
1038 InstructionCapturingInserter::CollectionScope scope(builder,
1039 recordInsertions);
1040 if (failed(opIface->convertOperation(&op, builder, *this)))
1041 return op.emitError("LLVM Translation failed for operation: ")
1042 << op.getName();
1043
1044 return convertDialectAttributes(&op, scope.getCapturedInstructions());
1045}
1046
1047/// Convert block to LLVM IR. Unless `ignoreArguments` is set, emit PHI nodes
1048/// to define values corresponding to the MLIR block arguments. These nodes
1049/// are not connected to the source basic blocks, which may not exist yet. Uses
1050/// `builder` to construct the LLVM IR. Expects the LLVM IR basic block to have
1051/// been created for `bb` and included in the block mapping. Inserts new
1052/// instructions at the end of the block and leaves `builder` in a state
1053/// suitable for further insertion into the end of the block.
1054LogicalResult ModuleTranslation::convertBlockImpl(Block &bb,
1055 bool ignoreArguments,
1056 llvm::IRBuilderBase &builder,
1057 bool recordInsertions) {
1058 builder.SetInsertPoint(lookupBlock(&bb));
1059 auto *subprogram = builder.GetInsertBlock()->getParent()->getSubprogram();
1060
1061 // Before traversing operations, make block arguments available through
1062 // value remapping and PHI nodes, but do not add incoming edges for the PHI
1063 // nodes just yet: those values may be defined by this or following blocks.
1064 // This step is omitted if "ignoreArguments" is set. The arguments of the
1065 // first block have been already made available through the remapping of
1066 // LLVM function arguments.
1067 if (!ignoreArguments) {
1068 auto predecessors = bb.getPredecessors();
1069 unsigned numPredecessors =
1070 std::distance(predecessors.begin(), predecessors.end());
1071 for (auto arg : bb.getArguments()) {
1072 auto wrappedType = arg.getType();
1073 if (!isCompatibleType(wrappedType))
1074 return emitError(bb.front().getLoc(),
1075 "block argument does not have an LLVM type");
1076 builder.SetCurrentDebugLocation(
1077 debugTranslation->translateLoc(arg.getLoc(), subprogram));
1078 llvm::Type *type = convertType(wrappedType);
1079 llvm::PHINode *phi = builder.CreatePHI(type, numPredecessors);
1080 mapValue(arg, phi);
1081 }
1082 }
1083
1084 // Traverse operations.
1085 for (auto &op : bb) {
1086 // Set the current debug location within the builder.
1087 builder.SetCurrentDebugLocation(
1088 debugTranslation->translateLoc(op.getLoc(), subprogram));
1089
1090 if (failed(convertOperationImpl(op, builder, recordInsertions)))
1091 return failure();
1092
1093 // Set the branch weight metadata on the translated instruction.
1094 if (auto iface = dyn_cast<WeightedBranchOpInterface>(op))
1096 }
1097
1098 return success();
1099}
1100
1101/// A helper method to get the single Block in an operation honoring LLVM's
1102/// module requirements.
1104 return module->getRegion(0).front();
1105}
1106
1107/// A helper method to decide if a constant must not be set as a global variable
1108/// initializer. For an external linkage variable, the variable with an
1109/// initializer is considered externally visible and defined in this module, the
1110/// variable without an initializer is externally available and is defined
1111/// elsewhere.
1112static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage,
1113 llvm::Constant *cst) {
1114 return (linkage == llvm::GlobalVariable::ExternalLinkage && !cst) ||
1115 linkage == llvm::GlobalVariable::ExternalWeakLinkage;
1116}
1117
1118/// Sets the runtime preemption specifier of `gv` to dso_local if
1119/// `dsoLocalRequested` is true, otherwise it is left unchanged.
1120static void addRuntimePreemptionSpecifier(bool dsoLocalRequested,
1121 llvm::GlobalValue *gv) {
1122 if (dsoLocalRequested)
1123 gv->setDSOLocal(true);
1124}
1125
1126/// Attempts to translate an MLIR attribute identified by `key`, optionally with
1127/// the given `value`, into an LLVM IR attribute. Reports errors at `loc` if
1128/// any. If the attribute name corresponds to a known LLVM IR attribute kind,
1129/// creates the LLVM attribute of that kind; otherwise, keeps it as a string
1130/// attribute. Performs additional checks for attributes known to have or not
1131/// have a value in order to avoid assertions inside LLVM upon construction.
1132static FailureOr<llvm::Attribute>
1133convertMLIRAttributeToLLVM(Location loc, llvm::LLVMContext &ctx, StringRef key,
1134 StringRef value = StringRef()) {
1135 auto kind = llvm::Attribute::getAttrKindFromName(key);
1136 if (kind == llvm::Attribute::None)
1137 return llvm::Attribute::get(ctx, key, value);
1138
1139 if (llvm::Attribute::isIntAttrKind(kind)) {
1140 if (value.empty())
1141 return emitError(loc) << "LLVM attribute '" << key << "' expects a value";
1142
1144 if (!value.getAsInteger(/*Radix=*/0, result))
1145 return llvm::Attribute::get(ctx, kind, result);
1146 return llvm::Attribute::get(ctx, key, value);
1147 }
1148
1149 if (!value.empty())
1150 return emitError(loc) << "LLVM attribute '" << key
1151 << "' does not expect a value, found '" << value
1152 << "'";
1153
1154 return llvm::Attribute::get(ctx, kind);
1155}
1156
1157/// Converts the MLIR attributes listed in the given array attribute into LLVM
1158/// attributes. Returns an `AttrBuilder` containing the converted attributes.
1159/// Reports error to `loc` if any and returns immediately. Expects `arrayAttr`
1160/// to contain either string attributes, treated as value-less LLVM attributes,
1161/// or array attributes containing two string attributes, with the first string
1162/// being the name of the corresponding LLVM attribute and the second string
1163/// beings its value. Note that even integer attributes are expected to have
1164/// their values expressed as strings.
1165static FailureOr<llvm::AttrBuilder>
1166convertMLIRAttributesToLLVM(Location loc, llvm::LLVMContext &ctx,
1167 ArrayAttr arrayAttr, StringRef arrayAttrName) {
1168 llvm::AttrBuilder attrBuilder(ctx);
1169 if (!arrayAttr)
1170 return attrBuilder;
1171
1172 for (Attribute attr : arrayAttr) {
1173 if (auto stringAttr = dyn_cast<StringAttr>(attr)) {
1174 FailureOr<llvm::Attribute> llvmAttr =
1175 convertMLIRAttributeToLLVM(loc, ctx, stringAttr.getValue());
1176 if (failed(llvmAttr))
1177 return failure();
1178 attrBuilder.addAttribute(*llvmAttr);
1179 continue;
1180 }
1181
1182 auto arrayAttr = dyn_cast<ArrayAttr>(attr);
1183 if (!arrayAttr || arrayAttr.size() != 2)
1184 return emitError(loc) << "expected '" << arrayAttrName
1185 << "' to contain string or array attributes";
1186
1187 auto keyAttr = dyn_cast<StringAttr>(arrayAttr[0]);
1188 auto valueAttr = dyn_cast<StringAttr>(arrayAttr[1]);
1189 if (!keyAttr || !valueAttr)
1190 return emitError(loc) << "expected arrays within '" << arrayAttrName
1191 << "' to contain two strings";
1192
1193 FailureOr<llvm::Attribute> llvmAttr = convertMLIRAttributeToLLVM(
1194 loc, ctx, keyAttr.getValue(), valueAttr.getValue());
1195 if (failed(llvmAttr))
1196 return failure();
1197 attrBuilder.addAttribute(*llvmAttr);
1198 }
1199
1200 return attrBuilder;
1201}
1202
1203LogicalResult ModuleTranslation::convertGlobalsAndAliases() {
1204 // Mapping from compile unit to its respective set of global variables.
1206 // Mapping from subprogram to its respective set of static local variables.
1208
1209 // First, create all global variables and global aliases in LLVM IR. A global
1210 // or alias body may refer to another global/alias or itself, so all the
1211 // mapping needs to happen prior to body conversion.
1212
1213 // Create all llvm::GlobalVariable
1214 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
1215 llvm::Type *type = convertType(op.getType());
1216 llvm::Constant *cst = nullptr;
1217 const bool deferValueAttrToPass2 = op.getValueOrNull() &&
1218 !op.getInitializerBlock() &&
1219 !isa<StringAttr>(op.getValueOrNull());
1220 if (op.getValueOrNull() && !deferValueAttrToPass2) {
1221 // String attributes are treated separately because they cannot appear as
1222 // in-function constants and are thus not supported by getLLVMConstant.
1223 if (auto strAttr = dyn_cast_or_null<StringAttr>(op.getValueOrNull())) {
1224 cst = llvm::ConstantDataArray::getString(
1225 llvmModule->getContext(), strAttr.getValue(), /*AddNull=*/false);
1226 type = cst->getType();
1227 }
1228 }
1229
1230 auto linkage = convertLinkageToLLVM(op.getLinkage());
1231
1232 // LLVM IR requires constant with linkage other than external or weak
1233 // external to have initializers. If MLIR does not provide an initializer,
1234 // default to undef.
1235 bool dropInitializer = shouldDropGlobalInitializer(linkage, cst);
1236 if (!deferValueAttrToPass2) {
1237 if (!dropInitializer && !cst)
1238 cst = llvm::UndefValue::get(type);
1239 else if (dropInitializer && cst)
1240 cst = nullptr;
1241 } else {
1242 cst = nullptr;
1243 }
1244
1245 auto *var = new llvm::GlobalVariable(
1246 *llvmModule, type, op.getConstant(), linkage, cst, op.getSymName(),
1247 /*InsertBefore=*/nullptr, convertThreadLocalModeToLLVM(op.getTlsMode()),
1248 op.getAddrSpace(), op.getExternallyInitialized());
1249
1250 if (std::optional<mlir::SymbolRefAttr> comdat = op.getComdat()) {
1251 auto selectorOp = cast<ComdatSelectorOp>(
1252 symbolTable().lookupNearestSymbolFrom(op, *comdat));
1253 var->setComdat(comdatMapping.lookup(selectorOp));
1254 }
1255
1256 if (op.getUnnamedAddr().has_value())
1257 var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
1258
1259 if (op.getSection().has_value())
1260 var->setSection(*op.getSection());
1261
1262 addRuntimePreemptionSpecifier(op.getDsoLocal(), var);
1263
1264 std::optional<uint64_t> alignment = op.getAlignment();
1265 if (alignment.has_value())
1266 var->setAlignment(llvm::MaybeAlign(alignment.value()));
1267
1268 var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
1269
1270 globalsMapping.try_emplace(op, var);
1271 globalsByNameMapping.try_emplace(op.getSymName(), var);
1272
1273 // Add debug information if present.
1274 if (op.getDbgExprs()) {
1275 for (auto exprAttr :
1276 op.getDbgExprs()->getAsRange<DIGlobalVariableExpressionAttr>()) {
1277 llvm::DIGlobalVariableExpression *diGlobalExpr =
1278 debugTranslation->translateGlobalVariableExpression(exprAttr);
1279 llvm::DIGlobalVariable *diGlobalVar = diGlobalExpr->getVariable();
1280 var->addDebugInfo(diGlobalExpr);
1281
1282 // There is no `globals` field in DICompileUnitAttr which can be
1283 // directly assigned to DICompileUnit. We have to build the list by
1284 // looking at the dbgExpr of all the GlobalOps. The scope of the
1285 // variable is used to get the DICompileUnit in which to add it. But
1286 // there are cases where the scope of a global does not directly point
1287 // to the DICompileUnit and we have to do a bit more work to get to
1288 // it. Some of those cases are:
1289 //
1290 // 1. For the languages that support modules, the scope hierarchy can
1291 // be variable -> DIModule -> DICompileUnit
1292 //
1293 // 2. For the Fortran common block variable, the scope hierarchy can
1294 // be variable -> DICommonBlock -> DISubprogram -> DICompileUnit
1295 //
1296 // 3. For entities like static local variables in C or variable with
1297 // SAVE attribute in Fortran, the scope hierarchy can be
1298 // variable (-> DILocalScope)* -> DISubprogram
1299 llvm::DIScope *scope = diGlobalVar->getScope();
1300 if (auto *mod = dyn_cast_if_present<llvm::DIModule>(scope))
1301 scope = mod->getScope();
1302 else if (auto *cb = dyn_cast_if_present<llvm::DICommonBlock>(scope)) {
1303 if (auto *sp =
1304 dyn_cast_if_present<llvm::DISubprogram>(cb->getScope()))
1305 scope = sp->getUnit();
1306 } else if (auto *lbb =
1307 dyn_cast_if_present<llvm::DILexicalBlockBase>(scope)) {
1308 scope = lbb->getSubprogram();
1309 }
1310
1311 // Get the compile unit (scope) of the the global variable, or the
1312 // subprogram of the static local variable.
1313 if (llvm::DICompileUnit *compileUnit =
1314 dyn_cast_if_present<llvm::DICompileUnit>(scope)) {
1315 // Update the compile unit with this incoming global variable
1316 // expression during the finalizing step later.
1317 globalGVars[compileUnit].push_back(diGlobalExpr);
1318 } else if (llvm::DISubprogram *sp =
1319 dyn_cast_if_present<llvm::DISubprogram>(scope)) {
1320 // Update the subprogram with this incoming static local variable
1321 // expression during the finalizing step later.
1322 staticLocals[sp].push_back(diGlobalExpr);
1323 }
1324 }
1325 }
1326
1327 // Forward the target-specific attributes to LLVM.
1328 FailureOr<llvm::AttrBuilder> convertedTargetSpecificAttrs =
1330 op.getTargetSpecificAttrsAttr(),
1331 op.getTargetSpecificAttrsAttrName());
1332 if (failed(convertedTargetSpecificAttrs))
1333 return failure();
1334 var->addAttributes(*convertedTargetSpecificAttrs);
1335 }
1336
1337 // Value-attribute initializers may reference other globals by symbol name.
1338 // Register every global above before materializing those constants.
1339 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
1340 if (!op.getValueOrNull() || op.getInitializerBlock() ||
1341 isa<StringAttr>(op.getValueOrNull()))
1342 continue;
1343
1344 llvm::Type *type = convertType(op.getType());
1345 llvm::Constant *cst =
1346 getLLVMConstant(type, op.getValueOrNull(), op.getLoc(), *this);
1347 if (!cst)
1348 return failure();
1349
1350 auto linkage = convertLinkageToLLVM(op.getLinkage());
1351 bool dropInitializer = shouldDropGlobalInitializer(linkage, cst);
1352 auto *var = cast<llvm::GlobalVariable>(lookupGlobal(op));
1353 if (dropInitializer)
1354 var->setInitializer(nullptr);
1355 else
1356 var->setInitializer(cst);
1357 }
1358
1359 // Create all llvm::GlobalAlias
1360 for (auto op : getModuleBody(mlirModule).getOps<LLVM::AliasOp>()) {
1361 llvm::Type *type = convertType(op.getType());
1362 llvm::Constant *cst = nullptr;
1363 llvm::GlobalValue::LinkageTypes linkage =
1364 convertLinkageToLLVM(op.getLinkage());
1365 llvm::Module &llvmMod = *llvmModule;
1366
1367 // Note address space and aliasee info isn't set just yet.
1368 llvm::GlobalAlias *var = llvm::GlobalAlias::create(
1369 type, op.getAddrSpace(), linkage, op.getSymName(), /*placeholder*/ cst,
1370 &llvmMod);
1371
1372 var->setThreadLocalMode(convertThreadLocalModeToLLVM(op.getTlsMode()));
1373
1374 // Note there is no need to setup the comdat because GlobalAlias calls into
1375 // the aliasee comdat information automatically.
1376
1377 if (op.getUnnamedAddr().has_value())
1378 var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.getUnnamedAddr()));
1379
1380 var->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
1381
1382 aliasesMapping.try_emplace(op, var);
1383 }
1384
1385 // Convert global variable bodies.
1386 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
1387 if (Block *initializer = op.getInitializerBlock()) {
1388 llvm::IRBuilder<llvm::TargetFolder> builder(
1389 *llvmModule, llvm::TargetFolder(llvmModule->getDataLayout()));
1390
1391 [[maybe_unused]] int numConstantsHit = 0;
1392 [[maybe_unused]] int numConstantsErased = 0;
1393 DenseMap<llvm::ConstantAggregate *, int> constantAggregateUseMap;
1394
1395 for (auto &op : initializer->without_terminator()) {
1396 if (failed(convertOperation(op, builder)))
1397 return emitError(op.getLoc(), "fail to convert global initializer");
1398 auto *cst = dyn_cast<llvm::Constant>(lookupValue(op.getResult(0)));
1399 if (!cst)
1400 return emitError(op.getLoc(), "unemittable constant value");
1401
1402 // When emitting an LLVM constant, a new constant is created and the old
1403 // constant may become dangling and take space. We should remove the
1404 // dangling constants to avoid memory explosion especially for constant
1405 // arrays whose number of elements is large.
1406 // Because multiple operations may refer to the same constant, we need
1407 // to count the number of uses of each constant array and remove it only
1408 // when the count becomes zero.
1409 if (auto *agg = dyn_cast<llvm::ConstantAggregate>(cst)) {
1410 numConstantsHit++;
1411 Value result = op.getResult(0);
1412 int numUsers = std::distance(result.use_begin(), result.use_end());
1413 auto [iterator, inserted] =
1414 constantAggregateUseMap.try_emplace(agg, numUsers);
1415 if (!inserted) {
1416 // Key already exists, update the value
1417 iterator->second += numUsers;
1418 }
1419 }
1420 // Scan the operands of the operation to decrement the use count of
1421 // constants. Erase the constant if the use count becomes zero.
1422 for (Value v : op.getOperands()) {
1423 auto *cst = dyn_cast<llvm::ConstantAggregate>(lookupValue(v));
1424 if (!cst)
1425 continue;
1426 auto iter = constantAggregateUseMap.find(cst);
1427 assert(iter != constantAggregateUseMap.end() && "constant not found");
1428 iter->second--;
1429 if (iter->second == 0) {
1430 // NOTE: cannot call removeDeadConstantUsers() here because it
1431 // may remove the constant which has uses not be converted yet.
1432 if (cst->user_empty()) {
1433 cst->destroyConstant();
1434 numConstantsErased++;
1435 }
1436 constantAggregateUseMap.erase(iter);
1437 }
1438 }
1439 }
1440
1441 ReturnOp ret = cast<ReturnOp>(initializer->getTerminator());
1442 llvm::Constant *cst =
1443 cast<llvm::Constant>(lookupValue(ret.getOperand(0)));
1444 auto *global = cast<llvm::GlobalVariable>(lookupGlobal(op));
1445 if (!shouldDropGlobalInitializer(global->getLinkage(), cst))
1446 global->setInitializer(cst);
1447
1448 // Try to remove the dangling constants again after all operations are
1449 // converted.
1450 for (auto it : constantAggregateUseMap) {
1451 auto *cst = it.first;
1452 cst->removeDeadConstantUsers();
1453 if (cst->user_empty()) {
1454 cst->destroyConstant();
1455 numConstantsErased++;
1456 }
1457 }
1458
1459 LLVM_DEBUG(llvm::dbgs()
1460 << "Convert initializer for " << op.getName() << "\n";
1461 llvm::dbgs() << numConstantsHit << " new constants hit\n";
1462 llvm::dbgs()
1463 << numConstantsErased << " dangling constants erased\n";);
1464 }
1465 }
1466
1467 // Convert llvm.mlir.global_ctors and dtors.
1468 for (Operation &op : getModuleBody(mlirModule)) {
1469 auto ctorOp = dyn_cast<GlobalCtorsOp>(op);
1470 auto dtorOp = dyn_cast<GlobalDtorsOp>(op);
1471 if (!ctorOp && !dtorOp)
1472 continue;
1473
1474 // The empty / zero initialized version of llvm.global_(c|d)tors cannot be
1475 // handled by appendGlobalFn logic below, which just ignores empty (c|d)tor
1476 // lists. Make sure it gets emitted.
1477 if ((ctorOp && ctorOp.getCtors().empty()) ||
1478 (dtorOp && dtorOp.getDtors().empty())) {
1479 llvm::IRBuilder<llvm::TargetFolder> builder(
1480 *llvmModule, llvm::TargetFolder(llvmModule->getDataLayout()));
1481 llvm::Type *eltTy = llvm::StructType::get(
1482 builder.getInt32Ty(), builder.getPtrTy(), builder.getPtrTy());
1483 llvm::ArrayType *at = llvm::ArrayType::get(eltTy, 0);
1484 llvm::Constant *zeroInit = llvm::Constant::getNullValue(at);
1485 (void)new llvm::GlobalVariable(
1486 *llvmModule, zeroInit->getType(), false,
1487 llvm::GlobalValue::AppendingLinkage, zeroInit,
1488 ctorOp ? "llvm.global_ctors" : "llvm.global_dtors");
1489 } else {
1490 auto range = ctorOp
1491 ? llvm::zip(ctorOp.getCtors(), ctorOp.getPriorities())
1492 : llvm::zip(dtorOp.getDtors(), dtorOp.getPriorities());
1493 auto appendGlobalFn =
1494 ctorOp ? llvm::appendToGlobalCtors : llvm::appendToGlobalDtors;
1495 for (const auto &[sym, prio] : range) {
1496 llvm::Function *f =
1497 lookupFunction(cast<FlatSymbolRefAttr>(sym).getValue());
1498 appendGlobalFn(*llvmModule, f, cast<IntegerAttr>(prio).getInt(),
1499 /*Data=*/nullptr);
1500 }
1501 }
1502 }
1503
1504 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>())
1505 if (failed(convertDialectAttributes(op, {})))
1506 return failure();
1507
1508 // Finally, update the compile units their respective sets of global variables
1509 // created earlier.
1510 for (const auto &[compileUnit, globals] : globalGVars)
1511 compileUnit->replaceGlobalVariables(
1512 llvm::MDTuple::get(getLLVMContext(), globals));
1513
1514 // And update the subprograms with their respective sets of static local
1515 // variables.
1516 for (const auto &[sp, globals] : staticLocals)
1517 sp->retainNodes(globals.begin(), globals.end());
1518
1519 // Convert global alias bodies.
1520 for (auto op : getModuleBody(mlirModule).getOps<LLVM::AliasOp>()) {
1521 Block &initializer = op.getInitializerBlock();
1522 llvm::IRBuilder<llvm::TargetFolder> builder(
1523 *llvmModule, llvm::TargetFolder(llvmModule->getDataLayout()));
1524
1525 for (mlir::Operation &op : initializer.without_terminator()) {
1526 if (failed(convertOperation(op, builder)))
1527 return emitError(op.getLoc(), "fail to convert alias initializer");
1528 if (!isa<llvm::Constant>(lookupValue(op.getResult(0))))
1529 return emitError(op.getLoc(), "unemittable constant value");
1530 }
1531
1532 auto ret = cast<ReturnOp>(initializer.getTerminator());
1533 auto *cst = cast<llvm::Constant>(lookupValue(ret.getOperand(0)));
1534 assert(aliasesMapping.count(op));
1535 auto *alias = cast<llvm::GlobalAlias>(aliasesMapping[op]);
1536 alias->setAliasee(cst);
1537 }
1538
1539 for (auto op : getModuleBody(mlirModule).getOps<LLVM::AliasOp>())
1540 if (failed(convertDialectAttributes(op, {})))
1541 return failure();
1542
1543 return success();
1544}
1545
1546/// Return a representation of `value` as metadata.
1547static llvm::Metadata *convertIntegerToMetadata(llvm::LLVMContext &context,
1548 const llvm::APInt &value) {
1549 llvm::Constant *constant = llvm::ConstantInt::get(context, value);
1550 return llvm::ConstantAsMetadata::get(constant);
1551}
1552
1553/// Return a representation of `value` as an MDNode.
1554static llvm::MDNode *convertIntegerToMDNode(llvm::LLVMContext &context,
1555 const llvm::APInt &value) {
1556 return llvm::MDNode::get(context, convertIntegerToMetadata(context, value));
1557}
1558
1559/// Return an MDNode encoding `vec_type_hint` metadata.
1560static llvm::MDNode *convertVecTypeHintToMDNode(llvm::LLVMContext &context,
1561 llvm::Type *type,
1562 bool isSigned) {
1563 llvm::Metadata *typeMD =
1564 llvm::ConstantAsMetadata::get(llvm::UndefValue::get(type));
1565 llvm::Metadata *isSignedMD =
1566 convertIntegerToMetadata(context, llvm::APInt(32, isSigned ? 1 : 0));
1567 return llvm::MDNode::get(context, {typeMD, isSignedMD});
1568}
1569
1570/// Return an MDNode with a tuple given by the values in `values`.
1571static llvm::MDNode *convertIntegerArrayToMDNode(llvm::LLVMContext &context,
1572 ArrayRef<int32_t> values) {
1574 llvm::transform(
1575 values, std::back_inserter(mdValues), [&context](int32_t value) {
1576 return convertIntegerToMetadata(context, llvm::APInt(32, value));
1577 });
1578 return llvm::MDNode::get(context, mdValues);
1579}
1580
1581FailureOr<llvm::Metadata *> ModuleTranslation::convertMetadataAttr(
1583 llvm::LLVMContext &llvmContext = getLLVMContext();
1584
1586 .Case([&](MDStringAttr a) -> FailureOr<llvm::Metadata *> {
1587 return llvm::MDString::get(llvmContext, a.getValue().getValue());
1588 })
1589 .Case([&](MDConstantAttr a) -> FailureOr<llvm::Metadata *> {
1590 IntegerAttr intAttr = llvm::dyn_cast<IntegerAttr>(a.getValue());
1591 if (!intAttr) {
1592 return emitError()
1593 << "expected integer attribute in metadata constant";
1594 }
1595 return llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1596 llvm::Type::getIntNTy(llvmContext,
1597 intAttr.getType().getIntOrFloatBitWidth()),
1598 intAttr.getValue()));
1599 })
1600 .Case([&](MDGlobalValueAttr a) -> FailureOr<llvm::Metadata *> {
1601 return convertSymbolRefToMetadata(a.getName(), emitError);
1602 })
1603 .Case([&](MDNullAttr a) -> FailureOr<llvm::Metadata *> {
1604 return llvm::ConstantAsMetadata::get(llvm::ConstantPointerNull::get(
1605 llvm::PointerType::get(llvmContext, a.getAddressSpace())));
1606 })
1607 .Case([&](MDAddrSpaceCastAttr a) -> FailureOr<llvm::Metadata *> {
1608 FailureOr<llvm::Metadata *> arg =
1609 convertMetadataAttr(a.getArg(), emitError);
1610 if (failed(arg))
1611 return failure();
1612 // The verifier restricts the operand to pointer-valued metadata
1613 // attributes, all of which translate to a ConstantAsMetadata.
1614 auto *argAsMD = cast<llvm::ConstantAsMetadata>(*arg);
1615 return llvm::ConstantAsMetadata::get(
1616 llvm::ConstantExpr::getAddrSpaceCast(
1617 argAsMD->getValue(),
1618 llvm::PointerType::get(llvmContext, a.getAddressSpace())));
1619 })
1620 .Case([&](MDNodeAttr a) -> FailureOr<llvm::Metadata *> {
1622 for (Attribute operand : a.getOperands()) {
1623 FailureOr<llvm::Metadata *> md =
1625 if (failed(md))
1626 return failure();
1627 operands.push_back(*md);
1628 }
1629 return llvm::MDNode::get(llvmContext, operands);
1630 })
1631 .Default([&](Attribute attr) -> FailureOr<llvm::Metadata *> {
1632 return emitError() << "unsupported LLVM metadata attribute " << attr;
1633 });
1634}
1635
1636FailureOr<llvm::Metadata *> ModuleTranslation::convertSymbolRefToMetadata(
1638 if (llvm::Function *fn = lookupFunction(name.getValue()))
1639 return llvm::ValueAsMetadata::get(fn);
1640 if (llvm::GlobalValue *global = lookupGlobal(name.getValue()))
1641 return llvm::ValueAsMetadata::get(global);
1642 Operation *symbol = symbolTable().lookupSymbolIn(mlirModule, name);
1643 if (auto alias = dyn_cast_if_present<LLVM::AliasOp>(symbol)) {
1644 if (llvm::GlobalValue *global = lookupAlias(alias))
1645 return llvm::ValueAsMetadata::get(global);
1646 }
1647 if (auto ifunc = dyn_cast_if_present<LLVM::IFuncOp>(symbol)) {
1648 if (llvm::GlobalValue *global = lookupIFunc(ifunc))
1649 return llvm::ValueAsMetadata::get(global);
1650 }
1651 return emitError() << "could not resolve metadata reference '" << name << "'";
1652}
1653
1654LogicalResult ModuleTranslation::convertFunctionMetadata() {
1655 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
1656 ArrayAttr metadata = function.getFunctionMetadataAttr();
1657 if (!metadata)
1658 continue;
1659
1660 llvm::Function *llvmFunc = lookupFunction(function.getName());
1661 for (auto entry : metadata.getAsRange<LLVM::FunctionMetadataAttr>()) {
1662 StringRef metadataName = entry.getMetadataName().getValue();
1663
1664 FailureOr<llvm::Metadata *> md =
1665 convertMetadataAttr(entry.getNode(), [&]() {
1666 return function.emitError()
1667 << "failed to convert function_metadata entry '"
1668 << metadataName << "': ";
1669 });
1670 if (failed(md))
1671 return failure();
1672 llvm::MDNode *node = llvm::dyn_cast_if_present<llvm::MDNode>(*md);
1673 if (!node) {
1674 return function.emitError()
1675 << "failed to convert function_metadata entry '" << metadataName
1676 << "'";
1677 }
1678 llvmFunc->addMetadata(metadataName, *node);
1679 }
1680 }
1681 return success();
1682}
1683
1684LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
1685 // Clear the block, branch value mappings, they are only relevant within one
1686 // function.
1687 blockMapping.clear();
1688 valueMapping.clear();
1689 branchMapping.clear();
1690 llvm::Function *llvmFunc = lookupFunction(func.getName());
1691 llvm::LLVMContext &llvmContext = llvmFunc->getContext();
1692
1693 // Add function arguments to the value remapping table.
1694 for (auto [mlirArg, llvmArg] :
1695 llvm::zip(func.getArguments(), llvmFunc->args()))
1696 mapValue(mlirArg, &llvmArg);
1697
1698 // Check the personality and set it.
1699 if (func.getPersonality()) {
1700 llvm::Type *ty = llvm::PointerType::get(
1701 llvmContext, llvmModule->getDataLayout().getProgramAddressSpace());
1702 if (llvm::Constant *pfunc = getLLVMConstant(ty, func.getPersonalityAttr(),
1703 func.getLoc(), *this))
1704 llvmFunc->setPersonalityFn(pfunc);
1705 }
1706
1707 if (std::optional<StringRef> section = func.getSection())
1708 llvmFunc->setSection(*section);
1709
1710 if (func.getArmStreaming())
1711 llvmFunc->addFnAttr("aarch64_pstate_sm_enabled");
1712 else if (func.getArmLocallyStreaming())
1713 llvmFunc->addFnAttr("aarch64_pstate_sm_body");
1714 else if (func.getArmStreamingCompatible())
1715 llvmFunc->addFnAttr("aarch64_pstate_sm_compatible");
1716
1717 if (func.getArmNewZa())
1718 llvmFunc->addFnAttr("aarch64_new_za");
1719 else if (func.getArmInZa())
1720 llvmFunc->addFnAttr("aarch64_in_za");
1721 else if (func.getArmOutZa())
1722 llvmFunc->addFnAttr("aarch64_out_za");
1723 else if (func.getArmInoutZa())
1724 llvmFunc->addFnAttr("aarch64_inout_za");
1725 else if (func.getArmPreservesZa())
1726 llvmFunc->addFnAttr("aarch64_preserves_za");
1727
1728 if (auto targetCpu = func.getTargetCpu())
1729 llvmFunc->addFnAttr("target-cpu", *targetCpu);
1730
1731 if (auto tuneCpu = func.getTuneCpu())
1732 llvmFunc->addFnAttr("tune-cpu", *tuneCpu);
1733
1734 if (auto reciprocalEstimates = func.getReciprocalEstimates())
1735 llvmFunc->addFnAttr("reciprocal-estimates", *reciprocalEstimates);
1736
1737 if (auto preferVectorWidth = func.getPreferVectorWidth())
1738 llvmFunc->addFnAttr("prefer-vector-width", *preferVectorWidth);
1739
1740 if (func.getUseSampleProfile())
1741 llvmFunc->addFnAttr("use-sample-profile");
1742
1743 if (auto disableTailCalls = func.getDisableTailCalls())
1744 llvmFunc->addFnAttr("disable-tail-calls",
1745 llvm::toStringRef(*disableTailCalls));
1746
1747 if (auto sampleProfileSuffixElisionPolicy =
1748 func.getSampleProfileSuffixElisionPolicy())
1749 llvmFunc->addFnAttr("sample-profile-suffix-elision-policy",
1750 *sampleProfileSuffixElisionPolicy);
1751
1752 if (auto attr = func.getVscaleRange())
1753 llvmFunc->addFnAttr(llvm::Attribute::getWithVScaleRangeArgs(
1754 getLLVMContext(), attr->getMinRange().getInt(),
1755 attr->getMaxRange().getInt()));
1756
1757 if (auto noSignedZerosFpMath = func.getNoSignedZerosFpMath())
1758 llvmFunc->addFnAttr("no-signed-zeros-fp-math",
1759 llvm::toStringRef(*noSignedZerosFpMath));
1760
1761 if (auto fpContract = func.getFpContract())
1762 llvmFunc->addFnAttr("fp-contract", *fpContract);
1763
1764 if (auto instrumentFunctionEntry = func.getInstrumentFunctionEntry())
1765 llvmFunc->addFnAttr("instrument-function-entry", *instrumentFunctionEntry);
1766
1767 if (auto instrumentFunctionExit = func.getInstrumentFunctionExit())
1768 llvmFunc->addFnAttr("instrument-function-exit", *instrumentFunctionExit);
1769
1770 // First, create all blocks so we can jump to them.
1771 for (auto &bb : func) {
1772 auto *llvmBB = llvm::BasicBlock::Create(llvmContext);
1773 llvmBB->insertInto(llvmFunc);
1774 mapBlock(&bb, llvmBB);
1775 }
1776
1777 // Then, convert blocks one by one in topological order to ensure defs are
1778 // converted before uses.
1779 auto blocks = getBlocksSortedByDominance(func.getBody());
1780 for (Block *bb : blocks) {
1781 CapturingIRBuilder builder(*llvmModule,
1782 llvm::TargetFolder(llvmModule->getDataLayout()));
1783 if (failed(convertBlockImpl(*bb, bb->isEntryBlock(), builder,
1784 /*recordInsertions=*/true)))
1785 return failure();
1786 }
1787
1788 // After all blocks have been traversed and values mapped, connect the PHI
1789 // nodes to the results of preceding blocks.
1790 detail::connectPHINodes(func.getBody(), *this);
1791
1792 // Finally, convert dialect attributes attached to the function.
1793 return convertDialectAttributes(func, {});
1794}
1795
1796LogicalResult ModuleTranslation::convertDialectAttributes(
1797 Operation *op, ArrayRef<llvm::Instruction *> instructions) {
1798 for (NamedAttribute attribute : op->getDialectAttrs())
1799 if (failed(iface.amendOperation(op, instructions, attribute, *this)))
1800 return failure();
1801 return success();
1802}
1803
1804/// Converts memory effect attributes from `func` and attaches them to
1805/// `llvmFunc`.
1807 llvm::Function *llvmFunc) {
1808 if (!func.getMemoryEffects())
1809 return;
1810
1811 MemoryEffectsAttr memEffects = func.getMemoryEffectsAttr();
1812
1813 // Add memory effects incrementally.
1814 llvm::MemoryEffects newMemEffects =
1815 llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
1816 convertModRefInfoToLLVM(memEffects.getArgMem()));
1817 newMemEffects |= llvm::MemoryEffects(
1818 llvm::MemoryEffects::Location::InaccessibleMem,
1819 convertModRefInfoToLLVM(memEffects.getInaccessibleMem()));
1820 newMemEffects |=
1821 llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
1822 convertModRefInfoToLLVM(memEffects.getOther()));
1823 newMemEffects |=
1824 llvm::MemoryEffects(llvm::MemoryEffects::Location::ErrnoMem,
1825 convertModRefInfoToLLVM(memEffects.getErrnoMem()));
1826 newMemEffects |=
1827 llvm::MemoryEffects(llvm::MemoryEffects::Location::TargetMem0,
1828 convertModRefInfoToLLVM(memEffects.getTargetMem0()));
1829 newMemEffects |=
1830 llvm::MemoryEffects(llvm::MemoryEffects::Location::TargetMem1,
1831 convertModRefInfoToLLVM(memEffects.getTargetMem1()));
1832 llvmFunc->setMemoryEffects(newMemEffects);
1833}
1834
1835llvm::Attribute
1837 if (!allocSizeAttr || allocSizeAttr.empty())
1838 return llvm::Attribute{};
1839
1840 unsigned elemSize = static_cast<unsigned>(allocSizeAttr[0]);
1841 std::optional<unsigned> numElems;
1842 if (allocSizeAttr.size() > 1)
1843 numElems = static_cast<unsigned>(allocSizeAttr[1]);
1844
1845 return llvm::Attribute::getWithAllocSizeArgs(getLLVMContext(), elemSize,
1846 numElems);
1847}
1849 llvm::AttrBuilder &Attrs) {
1850 std::optional<DenormalFPEnvAttr> denormalFpEnv = func.getDenormalFpenv();
1851 if (!denormalFpEnv)
1852 return;
1853
1854 llvm::DenormalMode DefaultMode(
1855 convertDenormalModeKindToLLVM(denormalFpEnv->getDefaultOutputMode()),
1856 convertDenormalModeKindToLLVM(denormalFpEnv->getDefaultInputMode()));
1857 llvm::DenormalMode FloatMode(
1858 convertDenormalModeKindToLLVM(denormalFpEnv->getFloatOutputMode()),
1859 convertDenormalModeKindToLLVM(denormalFpEnv->getFloatInputMode()));
1860
1861 llvm::DenormalFPEnv FPEnv(DefaultMode, FloatMode);
1862 Attrs.addDenormalFPEnvAttr(FPEnv);
1863}
1864
1865/// Converts function attributes from `func` and attaches them to `llvmFunc`.
1867 llvm::Function *llvmFunc) {
1868 // FIXME: Use AttrBuilder far all cases
1869 llvm::AttrBuilder AttrBuilder(llvmFunc->getContext());
1870
1871 if (func.getNoInlineAttr())
1872 llvmFunc->addFnAttr(llvm::Attribute::NoInline);
1873 if (func.getAlwaysInlineAttr())
1874 llvmFunc->addFnAttr(llvm::Attribute::AlwaysInline);
1875 if (func.getInlineHintAttr())
1876 llvmFunc->addFnAttr(llvm::Attribute::InlineHint);
1877 if (func.getOptimizeNoneAttr())
1878 llvmFunc->addFnAttr(llvm::Attribute::OptimizeNone);
1879 if (func.getReturnsTwiceAttr())
1880 llvmFunc->addFnAttr(llvm::Attribute::ReturnsTwice);
1881 if (func.getColdAttr())
1882 llvmFunc->addFnAttr(llvm::Attribute::Cold);
1883 if (func.getHotAttr())
1884 llvmFunc->addFnAttr(llvm::Attribute::Hot);
1885 if (func.getNoduplicateAttr())
1886 llvmFunc->addFnAttr(llvm::Attribute::NoDuplicate);
1887 if (func.getConvergentAttr())
1888 llvmFunc->addFnAttr(llvm::Attribute::Convergent);
1889 if (func.getNoUnwindAttr())
1890 llvmFunc->addFnAttr(llvm::Attribute::NoUnwind);
1891 if (func.getWillReturnAttr())
1892 llvmFunc->addFnAttr(llvm::Attribute::WillReturn);
1893 if (func.getNoreturnAttr())
1894 llvmFunc->addFnAttr(llvm::Attribute::NoReturn);
1895 if (func.getOptsizeAttr())
1896 llvmFunc->addFnAttr(llvm::Attribute::OptimizeForSize);
1897 if (func.getMinsizeAttr())
1898 llvmFunc->addFnAttr(llvm::Attribute::MinSize);
1899 if (func.getSaveRegParamsAttr())
1900 llvmFunc->addFnAttr("save-reg-params");
1901 if (func.getNoCallerSavedRegistersAttr())
1902 llvmFunc->addFnAttr("no_caller_saved_registers");
1903 if (func.getNocallbackAttr())
1904 llvmFunc->addFnAttr(llvm::Attribute::NoCallback);
1905 if (StringAttr modFormat = func.getModularFormatAttr())
1906 llvmFunc->addFnAttr("modular-format", modFormat.getValue());
1907 if (TargetFeaturesAttr targetFeatAttr = func.getTargetFeaturesAttr())
1908 llvmFunc->addFnAttr("target-features", targetFeatAttr.getFeaturesString());
1909 if (FramePointerKindAttr fpAttr = func.getFramePointerAttr())
1910 llvmFunc->addFnAttr("frame-pointer", stringifyFramePointerKind(
1911 fpAttr.getFramePointerKind()));
1912 if (UWTableKindAttr uwTableKindAttr = func.getUwtableKindAttr())
1913 llvmFunc->setUWTableKind(
1914 convertUWTableKindToLLVM(uwTableKindAttr.getUwtableKind()));
1915 if (StringAttr zcsr = func.getZeroCallUsedRegsAttr())
1916 llvmFunc->addFnAttr("zero-call-used-regs", zcsr.getValue());
1917 if (func.getUniformWorkGroupSizeAttr())
1918 llvmFunc->addFnAttr("uniform-work-group-size");
1919
1920 if (ArrayAttr noBuiltins = func.getNobuiltinsAttr()) {
1921 if (noBuiltins.empty())
1922 llvmFunc->addFnAttr("no-builtins");
1923
1924 mod.convertFunctionAttrCollection(noBuiltins, llvmFunc,
1926 }
1927
1928 mod.convertFunctionAttrCollection(func.getDefaultFuncAttrsAttr(), llvmFunc,
1930
1931 if (llvm::Attribute attr = mod.convertAllocsizeAttr(func.getAllocsizeAttr());
1932 attr.isValid())
1933 llvmFunc->addFnAttr(attr);
1934
1936
1937 convertDenormalFPEnvAttribute(func, AttrBuilder);
1938 llvmFunc->addFnAttrs(AttrBuilder);
1939}
1940
1941/// Converts function attributes from `func` and attaches them to `llvmFunc`.
1943 llvm::Function *llvmFunc,
1944 ModuleTranslation &translation) {
1945 llvm::LLVMContext &llvmContext = llvmFunc->getContext();
1946
1947 if (VecTypeHintAttr vecTypeHint = func.getVecTypeHintAttr()) {
1948 Type type = vecTypeHint.getHint().getValue();
1949 llvm::Type *llvmType = translation.convertType(type);
1950 bool isSigned = vecTypeHint.getIsSigned();
1951 llvmFunc->setMetadata(
1952 func.getVecTypeHintAttrName(),
1953 convertVecTypeHintToMDNode(llvmContext, llvmType, isSigned));
1954 }
1955
1956 if (std::optional<ArrayRef<int32_t>> workGroupSizeHint =
1957 func.getWorkGroupSizeHint()) {
1958 llvmFunc->setMetadata(
1959 func.getWorkGroupSizeHintAttrName(),
1960 convertIntegerArrayToMDNode(llvmContext, *workGroupSizeHint));
1961 }
1962
1963 if (std::optional<ArrayRef<int32_t>> reqdWorkGroupSize =
1964 func.getReqdWorkGroupSize()) {
1965 llvmFunc->setMetadata(
1966 func.getReqdWorkGroupSizeAttrName(),
1967 convertIntegerArrayToMDNode(llvmContext, *reqdWorkGroupSize));
1968 }
1969
1970 if (std::optional<uint32_t> intelReqdSubGroupSize =
1971 func.getIntelReqdSubGroupSize()) {
1972 llvmFunc->setMetadata(
1973 func.getIntelReqdSubGroupSizeAttrName(),
1974 convertIntegerToMDNode(llvmContext,
1975 llvm::APInt(32, *intelReqdSubGroupSize)));
1976 }
1977}
1978
1979static LogicalResult convertParameterAttr(llvm::AttrBuilder &attrBuilder,
1980 llvm::Attribute::AttrKind llvmKind,
1981 NamedAttribute namedAttr,
1982 ModuleTranslation &moduleTranslation,
1983 Location loc) {
1985 .Case([&](TypeAttr typeAttr) {
1986 attrBuilder.addTypeAttr(
1987 llvmKind, moduleTranslation.convertType(typeAttr.getValue()));
1988 return success();
1989 })
1990 .Case([&](IntegerAttr intAttr) {
1991 attrBuilder.addRawIntAttr(llvmKind, intAttr.getInt());
1992 return success();
1993 })
1994 .Case([&](UnitAttr) {
1995 attrBuilder.addAttribute(llvmKind);
1996 return success();
1997 })
1998 .Case([&](LLVM::ConstantRangeAttr rangeAttr) {
1999 attrBuilder.addConstantRangeAttr(
2000 llvmKind,
2001 llvm::ConstantRange(rangeAttr.getLower(), rangeAttr.getUpper()));
2002 return success();
2003 })
2004 .Default([loc](auto) {
2005 return emitError(loc, "unsupported parameter attribute type");
2006 });
2007}
2008
2009FailureOr<llvm::AttrBuilder>
2010ModuleTranslation::convertParameterAttrs(LLVMFuncOp func, int argIdx,
2011 DictionaryAttr paramAttrs) {
2012 llvm::AttrBuilder attrBuilder(llvmModule->getContext());
2013 auto attrNameToKindMapping = getAttrNameToKindMapping();
2014 Location loc = func.getLoc();
2015
2016 for (auto namedAttr : paramAttrs) {
2017 auto it = attrNameToKindMapping.find(namedAttr.getName());
2018 if (it != attrNameToKindMapping.end()) {
2019 llvm::Attribute::AttrKind llvmKind = it->second;
2020 if (failed(convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this,
2021 loc)))
2022 return failure();
2023 } else if (namedAttr.getNameDialect()) {
2024 if (failed(iface.convertParameterAttr(func, argIdx, namedAttr, *this)))
2025 return failure();
2026 }
2027 }
2028
2029 return attrBuilder;
2030}
2031
2033 ArgAndResultAttrsOpInterface attrsOp, llvm::CallBase *call,
2034 ArrayRef<unsigned> immArgPositions) {
2035 // Convert the argument attributes.
2036 if (ArrayAttr argAttrsArray = attrsOp.getArgAttrsAttr()) {
2037 unsigned argAttrIdx = 0;
2038 llvm::SmallDenseSet<unsigned> immArgPositionsSet(immArgPositions.begin(),
2039 immArgPositions.end());
2040 for (unsigned argIdx : llvm::seq<unsigned>(call->arg_size())) {
2041 if (argAttrIdx >= argAttrsArray.size())
2042 break;
2043 // Skip immediate arguments (they have no entries in argAttrsArray).
2044 if (immArgPositionsSet.contains(argIdx))
2045 continue;
2046 // Skip empty argument attributes.
2047 auto argAttrs = cast<DictionaryAttr>(argAttrsArray[argAttrIdx++]);
2048 if (argAttrs.empty())
2049 continue;
2050 // Convert and add attributes to the call instruction.
2051 FailureOr<llvm::AttrBuilder> attrBuilder =
2052 convertParameterAttrs(attrsOp->getLoc(), argAttrs);
2053 if (failed(attrBuilder))
2054 return failure();
2055 call->addParamAttrs(argIdx, *attrBuilder);
2056 }
2057 }
2058
2059 // Convert the result attributes.
2060 if (ArrayAttr resAttrsArray = attrsOp.getResAttrsAttr()) {
2061 if (!resAttrsArray.empty()) {
2062 auto resAttrs = cast<DictionaryAttr>(resAttrsArray[0]);
2063 FailureOr<llvm::AttrBuilder> attrBuilder =
2064 convertParameterAttrs(attrsOp->getLoc(), resAttrs);
2065 if (failed(attrBuilder))
2066 return failure();
2067 call->addRetAttrs(*attrBuilder);
2068 }
2069 }
2070
2071 return success();
2072}
2073
2074std::optional<llvm::Attribute>
2076 if (auto str = dyn_cast<StringAttr>(a))
2077 return llvm::Attribute::get(ctx, ("no-builtin-" + str.getValue()).str());
2078 return std::nullopt;
2079}
2080
2081std::optional<llvm::Attribute>
2083 mlir::NamedAttribute namedAttr) {
2084 StringAttr name = namedAttr.getName();
2085 Attribute value = namedAttr.getValue();
2086
2087 if (auto strVal = dyn_cast<StringAttr>(value))
2088 return llvm::Attribute::get(ctx, name.getValue(), strVal.getValue());
2089 if (mlir::isa<UnitAttr>(value))
2090 return llvm::Attribute::get(ctx, name.getValue());
2091 return std::nullopt;
2092}
2093
2094FailureOr<llvm::AttrBuilder>
2095ModuleTranslation::convertParameterAttrs(Location loc,
2096 DictionaryAttr paramAttrs) {
2097 llvm::AttrBuilder attrBuilder(llvmModule->getContext());
2098 auto attrNameToKindMapping = getAttrNameToKindMapping();
2099
2100 for (auto namedAttr : paramAttrs) {
2101 auto it = attrNameToKindMapping.find(namedAttr.getName());
2102 if (it != attrNameToKindMapping.end()) {
2103 llvm::Attribute::AttrKind llvmKind = it->second;
2104 if (failed(convertParameterAttr(attrBuilder, llvmKind, namedAttr, *this,
2105 loc)))
2106 return failure();
2107 }
2108 }
2109
2110 return attrBuilder;
2111}
2112
2113LogicalResult ModuleTranslation::convertFunctionSignatures() {
2114 // Declare all functions first because there may be function calls that form a
2115 // call graph with cycles, global initializers that reference functions, or
2116 // metadata that references functions declared later in the module.
2117 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
2118 llvm::FunctionCallee llvmFuncCst = llvmModule->getOrInsertFunction(
2119 function.getName(),
2120 cast<llvm::FunctionType>(convertType(function.getFunctionType())));
2121 llvm::Function *llvmFunc = cast<llvm::Function>(llvmFuncCst.getCallee());
2122 mapFunction(function.getName(), llvmFunc);
2123 }
2124
2125 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
2126 llvm::Function *llvmFunc = lookupFunction(function.getName());
2127 llvmFunc->setLinkage(convertLinkageToLLVM(function.getLinkage()));
2128 llvmFunc->setCallingConv(convertCConvToLLVM(function.getCConv()));
2129 addRuntimePreemptionSpecifier(function.getDsoLocal(), llvmFunc);
2130
2131 // Convert function attributes.
2132 convertFunctionAttributes(*this, function, llvmFunc);
2133
2134 // Convert function kernel attributes to metadata.
2135 convertFunctionKernelAttributes(function, llvmFunc, *this);
2136
2137 // Convert function_entry_count attribute to metadata.
2138 if (auto entryCount = function.getFunctionEntryCountAttr()) {
2139 ArrayRef<uint64_t> imports = entryCount.getImports();
2140 llvm::DenseSet<llvm::GlobalValue::GUID> importGUIDs;
2141 if (!imports.empty())
2142 importGUIDs.insert(imports.begin(), imports.end());
2143 llvmFunc->setEntryCount(entryCount.getEntryCount(),
2144 imports.empty() ? nullptr : &importGUIDs);
2145 }
2146
2147 // Convert result attributes.
2148 if (ArrayAttr allResultAttrs = function.getAllResultAttrs()) {
2149 DictionaryAttr resultAttrs = cast<DictionaryAttr>(allResultAttrs[0]);
2150 FailureOr<llvm::AttrBuilder> attrBuilder =
2151 convertParameterAttrs(function, -1, resultAttrs);
2152 if (failed(attrBuilder))
2153 return failure();
2154 llvmFunc->addRetAttrs(*attrBuilder);
2155 }
2156
2157 // Convert argument attributes.
2158 for (auto [argIdx, llvmArg] : llvm::enumerate(llvmFunc->args())) {
2159 if (DictionaryAttr argAttrs = function.getArgAttrDict(argIdx)) {
2160 FailureOr<llvm::AttrBuilder> attrBuilder =
2161 convertParameterAttrs(function, argIdx, argAttrs);
2162 if (failed(attrBuilder))
2163 return failure();
2164 llvmArg.addAttrs(*attrBuilder);
2165 }
2166 }
2167
2168 // Forward the pass-through attributes to LLVM.
2169 FailureOr<llvm::AttrBuilder> convertedPassthroughAttrs =
2170 convertMLIRAttributesToLLVM(function.getLoc(), llvmFunc->getContext(),
2171 function.getPassthroughAttr(),
2172 function.getPassthroughAttrName());
2173 if (failed(convertedPassthroughAttrs))
2174 return failure();
2175 llvmFunc->addFnAttrs(*convertedPassthroughAttrs);
2176
2177 // Convert visibility attribute.
2178 llvmFunc->setVisibility(convertVisibilityToLLVM(function.getVisibility_()));
2179
2180 // Convert the comdat attribute.
2181 if (std::optional<mlir::SymbolRefAttr> comdat = function.getComdat()) {
2182 auto selectorOp = cast<ComdatSelectorOp>(
2183 symbolTable().lookupNearestSymbolFrom(function, *comdat));
2184 llvmFunc->setComdat(comdatMapping.lookup(selectorOp));
2185 }
2186
2187 if (auto gc = function.getGarbageCollector())
2188 llvmFunc->setGC(gc->str());
2189
2190 if (auto unnamedAddr = function.getUnnamedAddr())
2191 llvmFunc->setUnnamedAddr(convertUnnamedAddrToLLVM(*unnamedAddr));
2192
2193 if (auto alignment = function.getAlignment())
2194 llvmFunc->setAlignment(llvm::MaybeAlign(*alignment));
2195
2196 // Translate the debug information for this function.
2197 debugTranslation->translate(function, *llvmFunc);
2198 }
2199
2200 return success();
2201}
2202
2203LogicalResult ModuleTranslation::convertFunctions() {
2204 // Convert functions.
2205 for (auto function : getModuleBody(mlirModule).getOps<LLVMFuncOp>()) {
2206 // Do not convert external functions, but do process dialect attributes
2207 // attached to them.
2208 if (function.isExternal()) {
2209 if (failed(convertDialectAttributes(function, {})))
2210 return failure();
2211 continue;
2212 }
2213
2214 if (failed(convertOneFunction(function)))
2215 return failure();
2216 }
2217
2218 return success();
2219}
2220
2221LogicalResult ModuleTranslation::convertIFuncs() {
2222 for (auto op : getModuleBody(mlirModule).getOps<IFuncOp>()) {
2223 llvm::Type *type = convertType(op.getIFuncType());
2224 llvm::GlobalValue::LinkageTypes linkage =
2225 convertLinkageToLLVM(op.getLinkage());
2226 llvm::Constant *resolver;
2227 if (auto *resolverFn = lookupFunction(op.getResolver())) {
2228 resolver = cast<llvm::Constant>(resolverFn);
2229 } else {
2230 Operation *aliasOp = symbolTable().lookupSymbolIn(parentLLVMModule(op),
2231 op.getResolverAttr());
2232 resolver = cast<llvm::Constant>(lookupAlias(aliasOp));
2233 }
2234
2235 auto *ifunc =
2236 llvm::GlobalIFunc::create(type, op.getAddressSpace(), linkage,
2237 op.getSymName(), resolver, llvmModule.get());
2238 addRuntimePreemptionSpecifier(op.getDsoLocal(), ifunc);
2239 ifunc->setUnnamedAddr(convertUnnamedAddrToLLVM(op.getUnnamedAddr()));
2240 ifunc->setVisibility(convertVisibilityToLLVM(op.getVisibility_()));
2241
2242 ifuncMapping.try_emplace(op, ifunc);
2243 }
2244
2245 return success();
2246}
2247
2248// Attach global metadata after all globals, aliases, ifuncs, and function
2249// signatures exist so symbol references can be resolved.
2250LogicalResult ModuleTranslation::convertGlobalMetadata() {
2251 for (auto op : getModuleBody(mlirModule).getOps<LLVM::GlobalOp>()) {
2252 auto *var = cast<llvm::GlobalVariable>(lookupGlobal(op));
2253 if (FlatSymbolRefAttr associated = op.getAssociatedAttr()) {
2254 FailureOr<llvm::Metadata *> md =
2255 convertSymbolRefToMetadata(associated, [&]() {
2256 return op.emitError("failed to convert associated metadata");
2257 });
2258 if (failed(md))
2259 return failure();
2260 var->setMetadata(llvm::LLVMContext::MD_associated,
2261 llvm::MDNode::get(var->getContext(), *md));
2262 }
2263
2264 if (ArrayAttr absSym = op.getAbsoluteSymbolAttr()) {
2265 SmallVector<llvm::Metadata *> mdOps;
2266 llvm::LLVMContext &ctx = var->getContext();
2267 mdOps.reserve(absSym.size());
2268 for (Attribute attr : absSym) {
2269 auto intAttr = cast<IntegerAttr>(attr);
2270 llvm::IntegerType *ty = llvm::IntegerType::get(
2271 ctx, intAttr.getType().getIntOrFloatBitWidth());
2272 mdOps.push_back(llvm::ConstantAsMetadata::get(
2273 llvm::ConstantInt::get(ty, intAttr.getValue())));
2274 }
2275 var->setMetadata(llvm::LLVMContext::MD_absolute_symbol,
2276 llvm::MDNode::get(ctx, mdOps));
2277 }
2278 }
2279
2280 return success();
2281}
2282
2283LogicalResult ModuleTranslation::convertComdats() {
2284 for (auto comdatOp : getModuleBody(mlirModule).getOps<ComdatOp>()) {
2285 for (auto selectorOp : comdatOp.getOps<ComdatSelectorOp>()) {
2286 llvm::Module *module = getLLVMModule();
2287 if (module->getComdatSymbolTable().contains(selectorOp.getSymName()))
2288 return emitError(selectorOp.getLoc())
2289 << "comdat selection symbols must be unique even in different "
2290 "comdat regions";
2291 llvm::Comdat *comdat = module->getOrInsertComdat(selectorOp.getSymName());
2292 comdat->setSelectionKind(convertComdatToLLVM(selectorOp.getComdat()));
2293 comdatMapping.try_emplace(selectorOp, comdat);
2294 }
2295 }
2296 return success();
2297}
2298
2299LogicalResult ModuleTranslation::convertUnresolvedBlockAddress() {
2300 for (auto &[blockAddressOp, llvmCst] : unresolvedBlockAddressMapping) {
2301 BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
2302 llvm::BasicBlock *llvmBlock = lookupBlockAddress(blockAddressAttr);
2303 assert(llvmBlock && "expected LLVM blocks to be already translated");
2304
2305 // Update mapping with new block address constant.
2306 auto *llvmBlockAddr = llvm::BlockAddress::get(
2307 lookupFunction(blockAddressAttr.getFunction().getValue()), llvmBlock);
2308 llvmCst->replaceAllUsesWith(llvmBlockAddr);
2309 assert(llvmCst->use_empty() && "expected all uses to be replaced");
2310 cast<llvm::GlobalVariable>(llvmCst)->eraseFromParent();
2311 }
2312 unresolvedBlockAddressMapping.clear();
2313 return success();
2314}
2315
2316void ModuleTranslation::setAccessGroupsMetadata(AccessGroupOpInterface op,
2317 llvm::Instruction *inst) {
2318 if (llvm::MDNode *node = loopAnnotationTranslation->getAccessGroups(op))
2319 inst->setMetadata(llvm::LLVMContext::MD_access_group, node);
2320}
2321
2322llvm::MDNode *
2323ModuleTranslation::getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr) {
2324 auto [scopeIt, scopeInserted] =
2325 aliasScopeMetadataMapping.try_emplace(aliasScopeAttr, nullptr);
2326 if (!scopeInserted)
2327 return scopeIt->second;
2328 llvm::LLVMContext &ctx = llvmModule->getContext();
2329 auto dummy = llvm::MDNode::getTemporary(ctx, {});
2330 // Convert the domain metadata node if necessary.
2331 auto [domainIt, insertedDomain] = aliasDomainMetadataMapping.try_emplace(
2332 aliasScopeAttr.getDomain(), nullptr);
2333 if (insertedDomain) {
2335 // Placeholder for potential self-reference.
2336 operands.push_back(dummy.get());
2337 operands.push_back(
2338 llvm::ConstantAsMetadata::get(llvm::ConstantInt::getFalse(ctx)));
2339 if (StringAttr description = aliasScopeAttr.getDomain().getDescription())
2340 operands.push_back(llvm::MDString::get(ctx, description));
2341 domainIt->second = llvm::MDNode::get(ctx, operands);
2342 // Self-reference for uniqueness.
2343 llvm::Metadata *replacement;
2344 if (auto stringAttr =
2345 dyn_cast<StringAttr>(aliasScopeAttr.getDomain().getId()))
2346 replacement = llvm::MDString::get(ctx, stringAttr.getValue());
2347 else
2348 replacement = domainIt->second;
2349 domainIt->second->replaceOperandWith(0, replacement);
2350 }
2351 // Convert the scope metadata node.
2352 assert(domainIt->second && "Scope's domain should already be valid");
2354 // Placeholder for potential self-reference.
2355 operands.push_back(dummy.get());
2356 operands.push_back(domainIt->second);
2357 if (StringAttr description = aliasScopeAttr.getDescription())
2358 operands.push_back(llvm::MDString::get(ctx, description));
2359 scopeIt->second = llvm::MDNode::get(ctx, operands);
2360 // Self-reference for uniqueness.
2361 llvm::Metadata *replacement;
2362 if (auto stringAttr = dyn_cast<StringAttr>(aliasScopeAttr.getId()))
2363 replacement = llvm::MDString::get(ctx, stringAttr.getValue());
2364 else
2365 replacement = scopeIt->second;
2366 scopeIt->second->replaceOperandWith(0, replacement);
2367 return scopeIt->second;
2368}
2369
2371 ArrayRef<AliasScopeAttr> aliasScopeAttrs) {
2373 nodes.reserve(aliasScopeAttrs.size());
2374 for (AliasScopeAttr aliasScopeAttr : aliasScopeAttrs)
2375 nodes.push_back(getOrCreateAliasScope(aliasScopeAttr));
2376 return llvm::MDNode::get(getLLVMContext(), nodes);
2377}
2378
2379void ModuleTranslation::setAliasScopeMetadata(AliasAnalysisOpInterface op,
2380 llvm::Instruction *inst) {
2381 auto populateScopeMetadata = [&](ArrayAttr aliasScopeAttrs, unsigned kind) {
2382 if (!aliasScopeAttrs || aliasScopeAttrs.empty())
2383 return;
2384 llvm::MDNode *node = getOrCreateAliasScopes(
2385 llvm::to_vector(aliasScopeAttrs.getAsRange<AliasScopeAttr>()));
2386 inst->setMetadata(kind, node);
2387 };
2388
2389 populateScopeMetadata(op.getAliasScopesOrNull(),
2390 llvm::LLVMContext::MD_alias_scope);
2391 populateScopeMetadata(op.getNoAliasScopesOrNull(),
2392 llvm::LLVMContext::MD_noalias);
2393}
2394
2395llvm::MDNode *ModuleTranslation::getTBAANode(TBAATagAttr tbaaAttr) const {
2396 return tbaaMetadataMapping.lookup(tbaaAttr);
2397}
2398
2399void ModuleTranslation::setTBAAMetadata(AliasAnalysisOpInterface op,
2400 llvm::Instruction *inst) {
2401 ArrayAttr tagRefs = op.getTBAATagsOrNull();
2402 if (!tagRefs || tagRefs.empty())
2403 return;
2404
2405 // LLVM IR currently does not support attaching more than one TBAA access tag
2406 // to a memory accessing instruction. It may be useful to support this in
2407 // future, but for the time being just ignore the metadata if MLIR operation
2408 // has multiple access tags.
2409 if (tagRefs.size() > 1) {
2410 op.emitWarning() << "TBAA access tags were not translated, because LLVM "
2411 "IR only supports a single tag per instruction";
2412 return;
2413 }
2414
2415 llvm::MDNode *node = getTBAANode(cast<TBAATagAttr>(tagRefs[0]));
2416 inst->setMetadata(llvm::LLVMContext::MD_tbaa, node);
2417}
2418
2420 DereferenceableOpInterface op, llvm::Instruction *inst) {
2421 DereferenceableAttr derefAttr = op.getDereferenceableOrNull();
2422 if (!derefAttr)
2423 return;
2424
2425 llvm::MDNode *derefSizeNode = llvm::MDNode::get(
2427 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
2428 llvm::IntegerType::get(getLLVMContext(), 64), derefAttr.getBytes())));
2429 unsigned kindId = derefAttr.getMayBeNull()
2430 ? llvm::LLVMContext::MD_dereferenceable_or_null
2431 : llvm::LLVMContext::MD_dereferenceable;
2432 inst->setMetadata(kindId, derefSizeNode);
2433}
2434
2435void ModuleTranslation::setBranchWeightsMetadata(WeightedBranchOpInterface op) {
2436 SmallVector<uint32_t> weights;
2437 llvm::transform(op.getWeights(), std::back_inserter(weights),
2438 [](int32_t value) { return static_cast<uint32_t>(value); });
2439 if (weights.empty())
2440 return;
2441
2442 llvm::Instruction *inst = isa<CallOp>(op) ? lookupCall(op) : lookupBranch(op);
2443 assert(inst && "expected the operation to have a mapping to an instruction");
2444 inst->setMetadata(
2445 llvm::LLVMContext::MD_prof,
2446 llvm::MDBuilder(getLLVMContext()).createBranchWeights(weights));
2447}
2448
2449LogicalResult ModuleTranslation::createTBAAMetadata() {
2450 llvm::LLVMContext &ctx = llvmModule->getContext();
2451 llvm::IntegerType *offsetTy = llvm::IntegerType::get(ctx, 64);
2452
2453 // Walk the entire module and create all metadata nodes for the TBAA
2454 // attributes. The code below relies on two invariants of the
2455 // `AttrTypeWalker`:
2456 // 1. Attributes are visited in post-order: Since the attributes create a DAG,
2457 // this ensures that any lookups into `tbaaMetadataMapping` for child
2458 // attributes succeed.
2459 // 2. Attributes are only ever visited once: This way we don't leak any
2460 // LLVM metadata instances.
2461 AttrTypeWalker walker;
2462 walker.addWalk([&](TBAARootAttr root) {
2463 llvm::MDNode *node;
2464 if (StringAttr id = root.getId()) {
2465 node = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, id));
2466 } else {
2467 // Anonymous root nodes are self-referencing.
2468 auto selfRef = llvm::MDNode::getTemporary(ctx, {});
2469 node = llvm::MDNode::get(ctx, {selfRef.get()});
2470 node->replaceOperandWith(0, node);
2471 }
2472 tbaaMetadataMapping.insert({root, node});
2473 });
2474
2475 walker.addWalk([&](TBAATypeDescriptorAttr descriptor) {
2476 SmallVector<llvm::Metadata *> operands;
2477 operands.push_back(llvm::MDString::get(ctx, descriptor.getId()));
2478 for (TBAAMemberAttr member : descriptor.getMembers()) {
2479 operands.push_back(tbaaMetadataMapping.lookup(member.getTypeDesc()));
2480 operands.push_back(llvm::ConstantAsMetadata::get(
2481 llvm::ConstantInt::get(offsetTy, member.getOffset())));
2482 }
2483
2484 tbaaMetadataMapping.insert({descriptor, llvm::MDNode::get(ctx, operands)});
2485 });
2486
2487 walker.addWalk([&](TBAATagAttr tag) {
2488 SmallVector<llvm::Metadata *> operands;
2489
2490 operands.push_back(tbaaMetadataMapping.lookup(tag.getBaseType()));
2491 operands.push_back(tbaaMetadataMapping.lookup(tag.getAccessType()));
2492
2493 operands.push_back(llvm::ConstantAsMetadata::get(
2494 llvm::ConstantInt::get(offsetTy, tag.getOffset())));
2495 if (tag.getConstant())
2496 operands.push_back(
2497 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(offsetTy, 1)));
2498
2499 tbaaMetadataMapping.insert({tag, llvm::MDNode::get(ctx, operands)});
2500 });
2501
2502 mlirModule->walk([&](AliasAnalysisOpInterface analysisOpInterface) {
2503 if (auto attr = analysisOpInterface.getTBAATagsOrNull())
2504 walker.walk(attr);
2505 });
2506
2507 return success();
2508}
2509
2510LogicalResult ModuleTranslation::createIdentMetadata() {
2511 if (auto attr = mlirModule->getDiscardableAttrOfType<StringAttr>(
2512 LLVMDialect::getIdentAttrName())) {
2513 StringRef ident = attr;
2514 llvm::LLVMContext &ctx = llvmModule->getContext();
2515 llvm::NamedMDNode *namedMd =
2516 llvmModule->getOrInsertNamedMetadata(LLVMDialect::getIdentAttrName());
2517 llvm::MDNode *md = llvm::MDNode::get(ctx, llvm::MDString::get(ctx, ident));
2518 namedMd->addOperand(md);
2519 }
2520
2521 return success();
2522}
2523
2524LogicalResult ModuleTranslation::createCommandlineMetadata() {
2525 if (auto attr = mlirModule->getDiscardableAttrOfType<StringAttr>(
2526 LLVMDialect::getCommandlineAttrName())) {
2527 StringRef cmdLine = attr;
2528 llvm::LLVMContext &ctx = llvmModule->getContext();
2529 llvm::NamedMDNode *nmd = llvmModule->getOrInsertNamedMetadata(
2530 LLVMDialect::getCommandlineAttrName());
2531 llvm::MDNode *md =
2532 llvm::MDNode::get(ctx, llvm::MDString::get(ctx, cmdLine));
2533 nmd->addOperand(md);
2534 }
2535
2536 return success();
2537}
2538
2539LogicalResult ModuleTranslation::createDependentLibrariesMetadata() {
2540 if (auto dependentLibrariesAttr = mlirModule->getDiscardableAttr(
2541 LLVM::LLVMDialect::getDependentLibrariesAttrName())) {
2542 auto *nmd =
2543 llvmModule->getOrInsertNamedMetadata("llvm.dependent-libraries");
2544 llvm::LLVMContext &ctx = llvmModule->getContext();
2545 for (auto libAttr :
2546 cast<ArrayAttr>(dependentLibrariesAttr).getAsRange<StringAttr>()) {
2547 auto *md =
2548 llvm::MDNode::get(ctx, llvm::MDString::get(ctx, libAttr.getValue()));
2549 nmd->addOperand(md);
2550 }
2551 }
2552 return success();
2553}
2554
2556 llvm::Instruction *inst) {
2557 LoopAnnotationAttr attr =
2559 .Case<LLVM::BrOp, LLVM::CondBrOp>(
2560 [](auto branchOp) { return branchOp.getLoopAnnotationAttr(); });
2561 if (!attr)
2562 return;
2563 llvm::MDNode *loopMD =
2564 loopAnnotationTranslation->translateLoopAnnotation(attr, op);
2565 inst->setMetadata(llvm::LLVMContext::MD_loop, loopMD);
2566}
2567
2568void ModuleTranslation::setDisjointFlag(Operation *op, llvm::Value *value) {
2569 auto iface = cast<DisjointFlagInterface>(op);
2570 // We do a dyn_cast here in case the value got folded into a constant.
2571 if (auto *disjointInst = dyn_cast<llvm::PossiblyDisjointInst>(value))
2572 disjointInst->setIsDisjoint(iface.getIsDisjoint());
2573}
2574
2576 return typeTranslator.translateType(type);
2577}
2578
2579/// A helper to look up remapped operands in the value remapping table.
2582 remapped.reserve(values.size());
2583 for (Value v : values)
2584 remapped.push_back(lookupValue(v));
2585 return remapped;
2586}
2587
2588void ModuleTranslation::remapAllValuesWith(llvm::Value *oldValue,
2589 llvm::Value *newValue) {
2590 if (oldValue == newValue)
2591 return;
2592
2593 for (auto &entry : valueMapping)
2594 if (entry.second == oldValue)
2595 entry.second = newValue;
2596}
2597
2598llvm::OpenMPIRBuilder *ModuleTranslation::getOpenMPBuilder() {
2599 if (!ompBuilder) {
2600 ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
2601
2602 // Flags represented as top-level OpenMP dialect attributes are set in
2603 // `OpenMPDialectLLVMIRTranslationInterface::amendOperation()`. Here we set
2604 // the default configuration.
2605 llvm::OpenMPIRBuilderConfig config(
2606 /* IsTargetDevice = */ false, /* IsGPU = */ false,
2607 /* OpenMPOffloadMandatory = */ false,
2608 /* HasRequiresReverseOffload = */ false,
2609 /* HasRequiresUnifiedAddress = */ false,
2610 /* HasRequiresUnifiedSharedMemory = */ false,
2611 /* HasRequiresDynamicAllocators = */ false);
2612 unsigned int defaultAS =
2613 llvmModule->getDataLayout().getProgramAddressSpace();
2614 config.setDefaultTargetAS(defaultAS);
2615 config.setRuntimeCC(llvmModule->getTargetTriple().isSPIRV()
2616 ? llvm::CallingConv::SPIR_FUNC
2617 : llvm::CallingConv::C);
2618 ompBuilder->setConfig(std::move(config));
2619 ompBuilder->initialize();
2620 }
2621 return ompBuilder.get();
2622}
2623
2624llvm::vfs::FileSystem &ModuleTranslation::getFileSystem() {
2625 if (fileSystem)
2626 return *fileSystem;
2627 return *llvm::vfs::getRealFileSystem();
2628}
2629
2631 llvm::DILocalScope *scope) {
2632 return debugTranslation->translateLoc(loc, scope);
2633}
2634
2635llvm::DIExpression *
2636ModuleTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
2637 return debugTranslation->translateExpression(attr);
2638}
2639
2640llvm::DIGlobalVariableExpression *
2642 LLVM::DIGlobalVariableExpressionAttr attr) {
2643 return debugTranslation->translateGlobalVariableExpression(attr);
2644}
2645
2647 return debugTranslation->translate(attr);
2648}
2649
2650llvm::RoundingMode
2651ModuleTranslation::translateRoundingMode(LLVM::RoundingMode rounding) {
2652 return convertRoundingModeToLLVM(rounding);
2653}
2654
2656 LLVM::FPExceptionBehavior exceptionBehavior) {
2657 return convertFPExceptionBehaviorToLLVM(exceptionBehavior);
2658}
2659
2660llvm::NamedMDNode *
2662 return llvmModule->getOrInsertNamedMetadata(name);
2663}
2664
2665static std::unique_ptr<llvm::Module>
2666prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
2667 StringRef name) {
2668 m->getContext()->getOrLoadDialect<LLVM::LLVMDialect>();
2669 auto llvmModule = std::make_unique<llvm::Module>(name, llvmContext);
2670 if (auto dataLayoutAttr =
2671 m->getDiscardableAttr(LLVM::LLVMDialect::getDataLayoutAttrName())) {
2672 llvmModule->setDataLayout(cast<StringAttr>(dataLayoutAttr).getValue());
2673 } else {
2674 FailureOr<llvm::DataLayout> llvmDataLayout(llvm::DataLayout(""));
2675 if (auto iface = dyn_cast<DataLayoutOpInterface>(m)) {
2676 if (DataLayoutSpecInterface spec = iface.getDataLayoutSpec()) {
2677 llvmDataLayout =
2678 translateDataLayout(spec, DataLayout(iface), m->getLoc());
2679 }
2680 } else if (auto mod = dyn_cast<ModuleOp>(m)) {
2681 if (DataLayoutSpecInterface spec = mod.getDataLayoutSpec()) {
2682 llvmDataLayout =
2683 translateDataLayout(spec, DataLayout(mod), m->getLoc());
2684 }
2685 }
2686 if (failed(llvmDataLayout))
2687 return nullptr;
2688 llvmModule->setDataLayout(*llvmDataLayout);
2689 }
2690 if (auto targetTripleAttr =
2691 m->getDiscardableAttr(LLVM::LLVMDialect::getTargetTripleAttrName()))
2692 llvmModule->setTargetTriple(
2693 llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue()));
2694
2695 if (auto asmAttr = m->getDiscardableAttr(
2696 LLVM::LLVMDialect::getModuleLevelAsmAttrName())) {
2697 auto asmArrayAttr = dyn_cast<ArrayAttr>(asmAttr);
2698 if (!asmArrayAttr) {
2699 m->emitError("expected an array attribute for a module level asm");
2700 return nullptr;
2701 }
2702
2703 for (Attribute elt : asmArrayAttr) {
2704 auto asmStrAttr = dyn_cast<StringAttr>(elt);
2705 if (!asmStrAttr) {
2706 m->emitError(
2707 "expected a string attribute for each entry of a module level asm");
2708 return nullptr;
2709 }
2710 llvmModule->appendModuleInlineAsm(asmStrAttr.getValue());
2711 }
2712 }
2713
2714 return llvmModule;
2715}
2716
2717std::unique_ptr<llvm::Module>
2718mlir::translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext,
2719 StringRef name, bool disableVerification,
2720 llvm::vfs::FileSystem *fs) {
2721 if (!satisfiesLLVMModule(module)) {
2722 module->emitOpError("can not be translated to an LLVMIR module");
2723 return nullptr;
2724 }
2725
2726 std::unique_ptr<llvm::Module> llvmModule =
2727 prepareLLVMModule(module, llvmContext, name);
2728 if (!llvmModule)
2729 return nullptr;
2730
2733
2734 ModuleTranslation translator(module, std::move(llvmModule), fs);
2735 llvm::IRBuilder<llvm::TargetFolder> llvmBuilder(
2736 *translator.getLLVMModule(),
2737 llvm::TargetFolder(translator.getLLVMModule()->getDataLayout()));
2738
2739 // Convert module before functions and operations inside, so dialect
2740 // attributes can be used to change dialect-specific global configurations via
2741 // `amendOperation()`. These configurations can then influence the translation
2742 // of operations afterwards.
2743 if (failed(translator.convertOperation(*module, llvmBuilder)))
2744 return nullptr;
2745
2746 if (failed(translator.convertComdats()))
2747 return nullptr;
2748 if (failed(translator.convertFunctionSignatures()))
2749 return nullptr;
2750 if (failed(translator.convertGlobalsAndAliases()))
2751 return nullptr;
2752 if (failed(translator.convertIFuncs()))
2753 return nullptr;
2754 if (failed(translator.convertGlobalMetadata()))
2755 return nullptr;
2756 if (failed(translator.convertFunctionMetadata()))
2757 return nullptr;
2758 if (failed(translator.createTBAAMetadata()))
2759 return nullptr;
2760 if (failed(translator.createIdentMetadata()))
2761 return nullptr;
2762 if (failed(translator.createCommandlineMetadata()))
2763 return nullptr;
2764 if (failed(translator.createDependentLibrariesMetadata()))
2765 return nullptr;
2766
2767 // Convert other top-level operations if possible.
2768 for (Operation &o : getModuleBody(module).getOperations()) {
2769 if (!isa<LLVM::LLVMFuncOp, LLVM::AliasOp, LLVM::GlobalOp,
2770 LLVM::GlobalCtorsOp, LLVM::GlobalDtorsOp, LLVM::ComdatOp,
2771 LLVM::IFuncOp>(&o) &&
2772 !o.hasTrait<OpTrait::IsTerminator>() &&
2773 failed(translator.convertOperation(o, llvmBuilder))) {
2774 return nullptr;
2775 }
2776 }
2777
2778 // Operations in function bodies with symbolic references must be converted
2779 // after the top-level operations they refer to are declared, so we do it
2780 // last.
2781 if (failed(translator.convertFunctions()))
2782 return nullptr;
2783
2784 // Now that all MLIR blocks are resolved into LLVM ones, patch block address
2785 // constants to point to the correct blocks.
2786 if (failed(translator.convertUnresolvedBlockAddress()))
2787 return nullptr;
2788
2789 // Add the necessary debug info module flags, if they were not encoded in MLIR
2790 // beforehand.
2791 translator.debugTranslation->addModuleFlagsIfNotPresent();
2792
2793 // Call the OpenMP IR Builder callbacks prior to verifying the module
2794 if (auto *ompBuilder = translator.getOpenMPBuilder())
2795 ompBuilder->finalize();
2796
2797 if (!disableVerification &&
2798 llvm::verifyModule(*translator.llvmModule, &llvm::errs()))
2799 return nullptr;
2800
2801 return std::move(translator.llvmModule);
2802}
return success()
getNumOperands() - 1))) return failure()
Given a list of lists of parsed operands, populates uniqueOperands with unique operands.
ArrayAttr()
b getContext())
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be the output argument nBegin is set to its * replacement(set to `begin` if no invalidation happens). Since outgoing *copies could have been inserted at `end`
static Value getPHISourceValue(Block *current, Block *pred, unsigned numArguments, unsigned index)
Get the SSA value passed to the current block from the terminator operation of its predecessor.
static llvm::Type * getInnermostElementType(llvm::Type *type)
Returns the first non-sequential type nested in sequential types.
static void addRuntimePreemptionSpecifier(bool dsoLocalRequested, llvm::GlobalValue *gv)
Sets the runtime preemption specifier of gv to dso_local if dsoLocalRequested is true,...
static Block & getModuleBody(Operation *module)
A helper method to get the single Block in an operation honoring LLVM's module requirements.
static llvm::MDNode * convertIntegerArrayToMDNode(llvm::LLVMContext &context, ArrayRef< int32_t > values)
Return an MDNode with a tuple given by the values in values.
static void convertDenormalFPEnvAttribute(LLVMFuncOp func, llvm::AttrBuilder &Attrs)
static bool shouldDropGlobalInitializer(llvm::GlobalValue::LinkageTypes linkage, llvm::Constant *cst)
A helper method to decide if a constant must not be set as a global variable initializer.
static llvm::MDNode * convertIntegerToMDNode(llvm::LLVMContext &context, const llvm::APInt &value)
Return a representation of value as an MDNode.
static llvm::Metadata * convertIntegerToMetadata(llvm::LLVMContext &context, const llvm::APInt &value)
Return a representation of value as metadata.
static FailureOr< llvm::Attribute > convertMLIRAttributeToLLVM(Location loc, llvm::LLVMContext &ctx, StringRef key, StringRef value=StringRef())
Attempts to translate an MLIR attribute identified by key, optionally with the given value,...
static void convertFunctionKernelAttributes(LLVMFuncOp func, llvm::Function *llvmFunc, ModuleTranslation &translation)
Converts function attributes from func and attaches them to llvmFunc.
static LogicalResult convertParameterAttr(llvm::AttrBuilder &attrBuilder, llvm::Attribute::AttrKind llvmKind, NamedAttribute namedAttr, ModuleTranslation &moduleTranslation, Location loc)
static llvm::Constant * buildSequentialConstant(ArrayRef< llvm::Constant * > &constants, ArrayRef< int64_t > shape, llvm::Type *type, Location loc)
Builds a constant of a sequential LLVM type type, potentially containing other sequential types recur...
static FailureOr< llvm::AttrBuilder > convertMLIRAttributesToLLVM(Location loc, llvm::LLVMContext &ctx, ArrayAttr arrayAttr, StringRef arrayAttrName)
Converts the MLIR attributes listed in the given array attribute into LLVM attributes.
static void convertFunctionMemoryAttributes(LLVMFuncOp func, llvm::Function *llvmFunc)
Converts memory effect attributes from func and attaches them to llvmFunc.
static void convertFunctionAttributes(ModuleTranslation &mod, LLVMFuncOp func, llvm::Function *llvmFunc)
Converts function attributes from func and attaches them to llvmFunc.
static llvm::Constant * convertDenseResourceElementsAttr(Location loc, DenseResourceElementsAttr denseResourceAttr, llvm::Type *llvmType, const ModuleTranslation &moduleTranslation)
Convert a dense resource elements attribute to an LLVM IR constant using its raw data storage if poss...
static llvm::MDNode * convertVecTypeHintToMDNode(llvm::LLVMContext &context, llvm::Type *type, bool isSigned)
Return an MDNode encoding vec_type_hint metadata.
static llvm::Constant * convertDenseElementsAttr(Location loc, DenseElementsAttr denseElementsAttr, llvm::Type *llvmType, const ModuleTranslation &moduleTranslation)
Convert a dense elements attribute to an LLVM IR constant using its raw data storage if possible.
static std::unique_ptr< llvm::Module > prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext, StringRef name)
static ArrayRef< int64_t > getShape(Type type)
Returns the shape of the given type.
Definition Traits.cpp:117
This class represents a processed binary blob of data.
Definition AsmState.h:91
ArrayRef< char > getData() const
Return the raw underlying data of this blob.
Definition AsmState.h:145
void addWalk(WalkFn< Attribute > &&fn)
Register a walk function for a given attribute or type.
WalkResult walk(T element)
Walk the given attribute/type, and recursively walk any sub elements.
Attributes are known-constant values of operations.
Definition Attributes.h:25
MLIRContext * getContext() const
Return the context this attribute belongs to.
Block represents an ordered list of Operations.
Definition Block.h:34
iterator_range< pred_iterator > getPredecessors()
Definition Block.h:250
Operation & front()
Definition Block.h:178
Operation * getTerminator()
Get the terminator operation of this block.
Definition Block.cpp:249
BlockArgListType getArguments()
Definition Block.h:112
iterator_range< iterator > without_terminator()
Return an iterator range over the operation within this block excluding the terminator operation at t...
Definition Block.h:222
The main mechanism for performing data layout queries.
std::optional< uint64_t > getTypeIndexBitwidth(Type t) const
Returns the bitwidth that should be used when performing index computations for the given pointer-lik...
uint64_t getTypePreferredAlignment(Type t) const
Returns the preferred of the given type in the current scope.
uint64_t getTypeABIAlignment(Type t) const
Returns the required alignment of the given type in the current scope.
llvm::TypeSize getTypeSizeInBits(Type t) const
Returns the size in bits of the given type in the current scope.
An attribute that represents a reference to a dense vector or tensor object.
int64_t getNumElements() const
Returns the number of elements held by this attribute.
std::enable_if_t<!std::is_base_of< Attribute, T >::value||std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for this attribute.
bool isSplat() const
Returns true if this attribute corresponds to a splat, i.e.
ArrayRef< char > getRawData() const
Return the raw storage data held by this attribute.
ShapedType getType() const
Return the type of this ElementsAttr, guaranteed to be a vector or tensor with static shape.
const InterfaceType * getInterfaceFor(Object *obj) const
Get the interface for a given object, or null if one is not registered.
A symbol reference with a reference path containing a single element.
StringRef getValue() const
Returns the name of the held symbol reference.
This class represents a diagnostic that is inflight and set to be reported.
This class represents the base attribute for all debug info attributes.
Definition LLVMAttrs.h:29
Implementation class for module translation.
llvm::fp::ExceptionBehavior translateFPExceptionBehavior(LLVM::FPExceptionBehavior exceptionBehavior)
Translates the given LLVM FP exception behavior metadata.
llvm::CallInst * lookupCall(Operation *op) const
Finds an LLVM call instruction that corresponds to the given MLIR call operation.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
llvm::DIGlobalVariableExpression * translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr)
Translates the given LLVM global variable expression metadata.
llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr)
llvm::NamedMDNode * getOrInsertNamedModuleMetadata(StringRef name)
Gets the named metadata in the LLVM IR module being constructed, creating it if it does not exist.
SmallVector< llvm::Value * > lookupValues(ValueRange values)
Looks up remapped a list of remapped values.
void mapFunction(StringRef name, llvm::Function *func)
Stores the mapping between a function name and its LLVM IR representation.
void convertFunctionAttrCollection(AttrsTy attrs, Operation *op, const Converter &conv)
A template that takes a collection-like attribute, and converts it via a user provided callback,...
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translates the given location.
void setDereferenceableMetadata(DereferenceableOpInterface op, llvm::Instruction *inst)
Sets LLVM dereferenceable metadata for operations that have dereferenceable attributes.
void setBranchWeightsMetadata(WeightedBranchOpInterface op)
Sets LLVM profiling metadata for operations that have branch weights.
llvm::Instruction * lookupBranch(Operation *op) const
Finds an LLVM IR instruction that corresponds to the given MLIR operation with successors.
llvm::Value * lookupValue(Value value) const
Finds an LLVM IR value corresponding to the given MLIR value.
LogicalResult convertArgAndResultAttrs(ArgAndResultAttrsOpInterface attrsOp, llvm::CallBase *call, ArrayRef< unsigned > immArgPositions={})
Converts argument and result attributes from attrsOp to LLVM IR attributes on the call instruction.
static std::optional< llvm::Attribute > convertNoBuiltin(llvm::LLVMContext &ctx, mlir::Attribute a)
SymbolTableCollection & symbolTable()
llvm::Type * convertType(Type type)
Converts the type from MLIR LLVM dialect to LLVM.
llvm::RoundingMode translateRoundingMode(LLVM::RoundingMode rounding)
Translates the given LLVM rounding mode metadata.
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::DIExpression * translateExpression(LLVM::DIExpressionAttr attr)
Translates the given LLVM DWARF expression metadata.
llvm::OpenMPIRBuilder * getOpenMPBuilder()
Returns the OpenMP IR builder associated with the LLVM IR module being constructed.
llvm::vfs::FileSystem & getFileSystem()
Returns the virtual filesystem to use for file operations.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
FailureOr< llvm::Metadata * > convertMetadataAttr(Attribute attr, function_ref< InFlightDiagnostic()> emitError)
Converts an LLVM dialect metadata attribute to LLVM IR metadata.
llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
void remapAllValuesWith(llvm::Value *oldValue, llvm::Value *newValue)
Remap old value with new value in the MLIR-to-LLVM value map so later translations use the replacemen...
llvm::GlobalValue * lookupIFunc(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining an IFunc.
llvm::Metadata * translateDebugInfo(LLVM::DINodeAttr attr)
Translates the given LLVM debug info metadata.
void setDisjointFlag(Operation *op, llvm::Value *value)
Sets the disjoint flag attribute for the exported instruction value given the original operation op.
llvm::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
LogicalResult convertOperation(Operation &op, llvm::IRBuilderBase &builder)
Converts the given MLIR operation into LLVM IR using this translator.
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
llvm::MDNode * getOrCreateAliasScopes(ArrayRef< AliasScopeAttr > aliasScopeAttrs)
Returns the LLVM metadata corresponding to an array of mlir LLVM dialect alias scope attributes.
void mapBlock(Block *mlir, llvm::BasicBlock *llvm)
Stores the mapping between an MLIR block and LLVM IR basic block.
llvm::MDNode * getOrCreateAliasScope(AliasScopeAttr aliasScopeAttr)
Returns the LLVM metadata corresponding to a mlir LLVM dialect alias scope attribute.
llvm::Module * getLLVMModule()
Returns the LLVM module in which the IR is being constructed.
static std::optional< llvm::Attribute > convertDefaultFuncAttr(llvm::LLVMContext &ctx, mlir::NamedAttribute namedAttr)
void forgetMapping(Region &region)
Removes the mapping for blocks contained in the region and values defined in these blocks.
void setAliasScopeMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
void setAccessGroupsMetadata(AccessGroupOpInterface op, llvm::Instruction *inst)
void mapValue(Value mlir, llvm::Value *llvm)
Stores the mapping between an MLIR value and its LLVM IR counterpart.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
void setLoopMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM loop metadata for branch operations that have a loop annotation attribute.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:179
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Attribute getDiscardableAttr(StringRef name)
Access a discardable attribute by name, returns a null Attribute if the discardable attribute does no...
Definition Operation.h:485
Value getOperand(unsigned idx)
Definition Operation.h:375
unsigned getNumSuccessors()
Definition Operation.h:758
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:432
std::optional< Attribute > getInherentAttr(StringRef name)
Access an inherent attribute by name: returns an empty optional if there is no inherent attribute wit...
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OperationName getName()
The name of an operation is the key identifier for it.
Definition Operation.h:115
dialect_attr_range getDialectAttrs()
Return a range corresponding to the dialect attributes for this operation.
Definition Operation.h:689
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:403
Block * getSuccessor(unsigned index)
Definition Operation.h:760
MLIRContext * getContext()
Return the context this operation is associated with.
Definition Operation.h:233
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
This class models how operands are forwarded to block arguments in control flow.
bool empty() const
Returns true if there are no successor operands.
virtual Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:389
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
void connectPHINodes(Region &region, const ModuleTranslation &state)
For all blocks in the region that were converted to LLVM IR using the given ModuleTranslation,...
llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
static llvm::DenseMap< llvm::StringRef, llvm::Attribute::AttrKind > getAttrNameToKindMapping()
Returns a dense map from LLVM attribute name to their kind in LLVM IR dialect.
llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)
Create an LLVM IR constant of llvmType from the MLIR attribute attr.
Operation * parentLLVMModule(Operation *op)
Lookup parent Module satisfying LLVM conditions on the Module Operation.
bool satisfiesLLVMModule(Operation *op)
LLVM requires some operations to be inside of a Module operation.
void legalizeDIExpressionsRecursively(Operation *op)
Register all known legalization patterns declared here and apply them to all ops in op.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
void ensureDistinctSuccessors(Operation *op)
Make argument-taking successors of each block distinct.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:734
Include the generated interface declarations.
SetVector< Block * > getBlocksSortedByDominance(Region &region)
Gets a list of blocks that is sorted according to dominance.
DataLayoutSpecInterface translateDataLayout(const llvm::DataLayout &dataLayout, MLIRContext *context)
Translate the given LLVM data layout into an MLIR equivalent using the DLTI dialect.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:147
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false, llvm::vfs::FileSystem *fs=nullptr)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155