MLIR 23.0.0git
LLVMToLLVMIRTranslation.cpp
Go to the documentation of this file.
1//===- LLVMToLLVMIRTranslation.cpp - Translate LLVM dialect to LLVM IR ----===//
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 a translation between the MLIR LLVM dialect and LLVM IR.
10//
11//===----------------------------------------------------------------------===//
12
16#include "mlir/IR/Operation.h"
18#include "mlir/Support/LLVM.h"
20
21#include "llvm/ADT/TypeSwitch.h"
22#include "llvm/IR/DIBuilder.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/InlineAsm.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/MDBuilder.h"
27#include "llvm/IR/MatrixBuilder.h"
28#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
29#include "llvm/Support/LogicalResult.h"
30
31using namespace mlir;
32using namespace mlir::LLVM;
34
35#include "mlir/Dialect/LLVMIR/LLVMConversionEnumsToLLVM.inc"
36
37static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op) {
38 using llvmFMF = llvm::FastMathFlags;
39 using FuncT = void (llvmFMF::*)(bool);
40 const std::pair<FastmathFlags, FuncT> handlers[] = {
41 // clang-format off
42 {FastmathFlags::nnan, &llvmFMF::setNoNaNs},
43 {FastmathFlags::ninf, &llvmFMF::setNoInfs},
44 {FastmathFlags::nsz, &llvmFMF::setNoSignedZeros},
45 {FastmathFlags::arcp, &llvmFMF::setAllowReciprocal},
46 {FastmathFlags::contract, &llvmFMF::setAllowContract},
47 {FastmathFlags::afn, &llvmFMF::setApproxFunc},
48 {FastmathFlags::reassoc, &llvmFMF::setAllowReassoc},
49 // clang-format on
50 };
51 llvm::FastMathFlags ret;
52 ::mlir::LLVM::FastmathFlags fmfMlir = op.getFastmathAttr().getValue();
53 for (auto it : handlers)
54 if (bitEnumContainsAll(fmfMlir, it.first))
55 (ret.*(it.second))(true);
56 return ret;
57}
58
59/// Convert the value of a DenseI64ArrayAttr to a vector of unsigned indices.
61 SmallVector<unsigned> position;
62 llvm::append_range(position, indices);
63 return position;
64}
65
66/// Convert an LLVM type to a string for printing in diagnostics.
67static std::string diagStr(const llvm::Type *type) {
68 std::string str;
69 llvm::raw_string_ostream os(str);
70 type->print(os);
71 return str;
72}
73
74/// Get the declaration of an overloaded llvm intrinsic. First we get the
75/// overloaded argument types and/or result type from the CallIntrinsicOp, and
76/// then use those to get the correct declaration of the overloaded intrinsic.
77static FailureOr<llvm::Function *>
78getOverloadedDeclaration(CallIntrinsicOp op, llvm::Intrinsic::ID id,
79 llvm::Module *module,
80 LLVM::ModuleTranslation &moduleTranslation) {
82 for (Type type : op->getOperandTypes())
83 allArgTys.push_back(moduleTranslation.convertType(type));
84
85 llvm::Type *resTy;
86 if (op.getNumResults() == 0)
87 resTy = llvm::Type::getVoidTy(module->getContext());
88 else
89 resTy = moduleTranslation.convertType(op.getResult(0).getType());
90
91 // ATM we do not support variadic intrinsics.
92 llvm::FunctionType *ft = llvm::FunctionType::get(resTy, allArgTys, false);
93
95 getIntrinsicInfoTableEntries(id, table);
97
98 SmallVector<llvm::Type *, 8> overloadedArgTys;
99 if (llvm::Intrinsic::matchIntrinsicSignature(ft, tableRef,
100 overloadedArgTys) !=
101 llvm::Intrinsic::MatchIntrinsicTypesResult::MatchIntrinsicTypes_Match) {
102 return mlir::emitError(op.getLoc(), "call intrinsic signature ")
103 << diagStr(ft) << " to overloaded intrinsic " << op.getIntrinAttr()
104 << " does not match any of the overloads";
105 }
106
107 ArrayRef<llvm::Type *> overloadedArgTysRef = overloadedArgTys;
108 return llvm::Intrinsic::getOrInsertDeclaration(module, id,
109 overloadedArgTysRef);
110}
111
112static llvm::OperandBundleDef
113convertOperandBundle(OperandRange bundleOperands, StringRef bundleTag,
114 LLVM::ModuleTranslation &moduleTranslation) {
115 std::vector<llvm::Value *> operands;
116 operands.reserve(bundleOperands.size());
117 for (Value bundleArg : bundleOperands)
118 operands.push_back(moduleTranslation.lookupValue(bundleArg));
119 return llvm::OperandBundleDef(bundleTag.str(), std::move(operands));
120}
121
124 LLVM::ModuleTranslation &moduleTranslation) {
126 bundles.reserve(bundleOperands.size());
127
128 for (auto [operands, tagAttr] : llvm::zip_equal(bundleOperands, bundleTags)) {
129 StringRef tag = cast<StringAttr>(tagAttr).getValue();
130 bundles.push_back(convertOperandBundle(operands, tag, moduleTranslation));
131 }
132 return bundles;
133}
134
137 std::optional<ArrayAttr> bundleTags,
138 LLVM::ModuleTranslation &moduleTranslation) {
139 if (!bundleTags)
140 return {};
141 return convertOperandBundles(bundleOperands, *bundleTags, moduleTranslation);
142}
143
144/// Builder for LLVM_CallIntrinsicOp
145static LogicalResult
146convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder,
147 LLVM::ModuleTranslation &moduleTranslation) {
148 llvm::Module *module = builder.GetInsertBlock()->getModule();
149 llvm::Intrinsic::ID id =
150 llvm::Intrinsic::lookupIntrinsicID(op.getIntrinAttr());
151 if (!id)
152 return mlir::emitError(op.getLoc(), "could not find LLVM intrinsic: ")
153 << op.getIntrinAttr();
154
155 llvm::Function *fn = nullptr;
156 if (llvm::Intrinsic::isOverloaded(id)) {
157 auto fnOrFailure =
158 getOverloadedDeclaration(op, id, module, moduleTranslation);
159 if (failed(fnOrFailure))
160 return failure();
161 fn = *fnOrFailure;
162 } else {
163 fn = llvm::Intrinsic::getOrInsertDeclaration(module, id, {});
164 }
165
166 // Check the result type of the call.
167 const llvm::Type *intrinType =
168 op.getNumResults() == 0
169 ? llvm::Type::getVoidTy(module->getContext())
170 : moduleTranslation.convertType(op.getResultTypes().front());
171 if (intrinType != fn->getReturnType()) {
172 return mlir::emitError(op.getLoc(), "intrinsic call returns ")
173 << diagStr(intrinType) << " but " << op.getIntrinAttr()
174 << " actually returns " << diagStr(fn->getReturnType());
175 }
176
177 // Check the argument types of the call. If the function is variadic, check
178 // the subrange of required arguments.
179 if (!fn->getFunctionType()->isVarArg() &&
180 op.getArgs().size() != fn->arg_size()) {
181 return mlir::emitError(op.getLoc(), "intrinsic call has ")
182 << op.getArgs().size() << " operands but " << op.getIntrinAttr()
183 << " expects " << fn->arg_size();
184 }
185 if (fn->getFunctionType()->isVarArg() &&
186 op.getArgs().size() < fn->arg_size()) {
187 return mlir::emitError(op.getLoc(), "intrinsic call has ")
188 << op.getArgs().size() << " operands but variadic "
189 << op.getIntrinAttr() << " expects at least " << fn->arg_size();
190 }
191 // Check the arguments up to the number the function requires.
192 for (unsigned i = 0, e = fn->arg_size(); i != e; ++i) {
193 const llvm::Type *expected = fn->getArg(i)->getType();
194 const llvm::Type *actual =
195 moduleTranslation.convertType(op.getOperandTypes()[i]);
196 if (actual != expected) {
197 return mlir::emitError(op.getLoc(), "intrinsic call operand #")
198 << i << " has type " << diagStr(actual) << " but "
199 << op.getIntrinAttr() << " expects " << diagStr(expected);
200 }
201 }
202
203 FastmathFlagsInterface itf = op;
204 builder.setFastMathFlags(getFastmathFlags(itf));
205
206 auto *inst = builder.CreateCall(
207 fn, moduleTranslation.lookupValues(op.getArgs()),
208 convertOperandBundles(op.getOpBundleOperands(), op.getOpBundleTags(),
209 moduleTranslation));
210
211 if (failed(moduleTranslation.convertArgAndResultAttrs(op, inst)))
212 return failure();
213
214 if (op.getNumResults() == 1)
215 moduleTranslation.mapValue(op->getResults().front()) = inst;
216 return success();
217}
218
219/// Recursively converts an MLIR metadata attribute to an LLVM metadata node.
220static llvm::Metadata *
221convertMetadataAttr(Attribute attr, llvm::IRBuilderBase &builder,
222 LLVM::ModuleTranslation &moduleTranslation) {
224 .Case<LLVM::MDStringAttr>([&](auto a) -> llvm::Metadata * {
225 return llvm::MDString::get(builder.getContext(),
226 a.getValue().getValue());
227 })
228 .Case<LLVM::MDConstantAttr>([&](auto a) -> llvm::Metadata * {
229 IntegerAttr intAttr = llvm::dyn_cast<IntegerAttr>(a.getValue());
230 if (!intAttr)
231 return nullptr;
232 return llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
233 llvm::Type::getIntNTy(builder.getContext(),
234 intAttr.getType().getIntOrFloatBitWidth()),
235 intAttr.getValue()));
236 })
237 .Case<LLVM::MDFuncAttr>([&](auto a) -> llvm::Metadata * {
238 if (llvm::Function *fn =
239 moduleTranslation.lookupFunction(a.getName().getValue()))
240 return llvm::ValueAsMetadata::get(fn);
241 return nullptr;
242 })
243 .Case<LLVM::MDNodeAttr>([&](auto a) -> llvm::Metadata * {
245 for (Attribute op : a.getOperands())
246 operands.push_back(
247 convertMetadataAttr(op, builder, moduleTranslation));
248 return llvm::MDNode::get(builder.getContext(), operands);
249 })
250 .Default([](auto) -> llvm::Metadata * { return nullptr; });
251}
252
253static void convertNamedMetadataOp(StringRef metadataName, ArrayAttr nodes,
254 llvm::IRBuilderBase &builder,
255 LLVM::ModuleTranslation &moduleTranslation) {
256 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
257 llvm::NamedMDNode *namedMD =
258 llvmModule->getOrInsertNamedMetadata(metadataName);
259 for (Attribute nodeAttr : nodes) {
260 llvm::Metadata *md =
261 convertMetadataAttr(nodeAttr, builder, moduleTranslation);
262 if (auto *mdNode = llvm::dyn_cast_or_null<llvm::MDNode>(md))
263 namedMD->addOperand(mdNode);
264 }
265}
266
268 llvm::IRBuilderBase &builder,
269 LLVM::ModuleTranslation &moduleTranslation) {
270 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
271 llvm::LLVMContext &context = llvmModule->getContext();
272 llvm::NamedMDNode *linkerMDNode =
273 llvmModule->getOrInsertNamedMetadata("llvm.linker.options");
275 mdNodes.reserve(options.size());
276 for (auto s : options.getAsRange<StringAttr>()) {
277 auto *mdNode = llvm::MDString::get(context, s.getValue());
278 mdNodes.push_back(mdNode);
279 }
280
281 auto *listMDNode = llvm::MDTuple::get(context, mdNodes);
282 linkerMDNode->addOperand(listMDNode);
283}
284
285static llvm::Metadata *
286convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr,
287 llvm::IRBuilderBase &builder,
288 LLVM::ModuleTranslation &moduleTranslation) {
289 llvm::LLVMContext &context = builder.getContext();
290 llvm::MDBuilder mdb(context);
292
293 if (key == LLVMDialect::getModuleFlagKeyCGProfileName()) {
294 for (auto entry : arrayAttr.getAsRange<ModuleFlagCGProfileEntryAttr>()) {
295 auto getFuncMetadata = [&](FlatSymbolRefAttr sym) -> llvm::Metadata * {
296 if (!sym)
297 return nullptr;
298 if (llvm::Function *fn =
299 moduleTranslation.lookupFunction(sym.getValue()))
300 return llvm::ValueAsMetadata::get(fn);
301 return nullptr;
302 };
303 llvm::Metadata *fromMetadata = getFuncMetadata(entry.getFrom());
304 llvm::Metadata *toMetadata = getFuncMetadata(entry.getTo());
305
306 llvm::Metadata *vals[] = {
307 fromMetadata, toMetadata,
308 mdb.createConstant(llvm::ConstantInt::get(
309 llvm::Type::getInt64Ty(context), entry.getCount()))};
310 nodes.push_back(llvm::MDNode::get(context, vals));
311 }
312 return llvm::MDTuple::getDistinct(context, nodes);
313 }
314 return nullptr;
315}
316
318 StringRef key, ModuleFlagProfileSummaryAttr summaryAttr,
319 llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation) {
320 llvm::LLVMContext &context = builder.getContext();
321 llvm::MDBuilder mdb(context);
322
323 auto getIntTuple = [&](StringRef key, uint64_t val) -> llvm::MDTuple * {
325 mdb.createString(key), mdb.createConstant(llvm::ConstantInt::get(
326 llvm::Type::getInt64Ty(context), val))};
327 return llvm::MDTuple::get(context, tupleNodes);
328 };
329
331 mdb.createString("ProfileFormat"),
332 mdb.createString(
333 stringifyProfileSummaryFormatKind(summaryAttr.getFormat()))};
334
336 llvm::MDTuple::get(context, fmtNode),
337 getIntTuple("TotalCount", summaryAttr.getTotalCount()),
338 getIntTuple("MaxCount", summaryAttr.getMaxCount()),
339 getIntTuple("MaxInternalCount", summaryAttr.getMaxInternalCount()),
340 getIntTuple("MaxFunctionCount", summaryAttr.getMaxFunctionCount()),
341 getIntTuple("NumCounts", summaryAttr.getNumCounts()),
342 getIntTuple("NumFunctions", summaryAttr.getNumFunctions()),
343 };
344
345 if (summaryAttr.getIsPartialProfile())
346 vals.push_back(
347 getIntTuple("IsPartialProfile", *summaryAttr.getIsPartialProfile()));
348
349 if (summaryAttr.getPartialProfileRatio()) {
351 mdb.createString("PartialProfileRatio"),
352 mdb.createConstant(llvm::ConstantFP::get(
353 llvm::Type::getDoubleTy(context),
354 summaryAttr.getPartialProfileRatio().getValue()))};
355 vals.push_back(llvm::MDTuple::get(context, tupleNodes));
356 }
357
358 SmallVector<llvm::Metadata *> detailedEntries;
359 llvm::Type *llvmInt64Type = llvm::Type::getInt64Ty(context);
360 for (ModuleFlagProfileSummaryDetailedAttr detailedEntry :
361 summaryAttr.getDetailedSummary()) {
363 mdb.createConstant(
364 llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getCutOff())),
365 mdb.createConstant(
366 llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getMinCount())),
367 mdb.createConstant(llvm::ConstantInt::get(
368 llvmInt64Type, detailedEntry.getNumCounts()))};
369 detailedEntries.push_back(llvm::MDTuple::get(context, tupleNodes));
370 }
371 SmallVector<llvm::Metadata *> detailedSummary{
372 mdb.createString("DetailedSummary"),
373 llvm::MDTuple::get(context, detailedEntries)};
374 vals.push_back(llvm::MDTuple::get(context, detailedSummary));
375
376 return llvm::MDNode::get(context, vals);
377}
378
379static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder,
380 LLVM::ModuleTranslation &moduleTranslation) {
381 llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
382 for (auto flagAttr : flags.getAsRange<ModuleFlagAttr>()) {
383 llvm::Metadata *valueMetadata =
385 .Case([&](StringAttr strAttr) {
386 return llvm::MDString::get(builder.getContext(),
387 strAttr.getValue());
388 })
389 .Case([&](IntegerAttr intAttr) {
390 return llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
391 llvm::Type::getInt32Ty(builder.getContext()),
392 intAttr.getInt()));
393 })
394 .Case([&](ArrayAttr arrayAttr) {
395 return convertModuleFlagValue(flagAttr.getKey().getValue(),
396 arrayAttr, builder,
397 moduleTranslation);
398 })
399 .Case([&](ModuleFlagProfileSummaryAttr summaryAttr) {
401 flagAttr.getKey().getValue(), summaryAttr, builder,
402 moduleTranslation);
403 })
404 .Default([](auto) { return nullptr; });
405
406 assert(valueMetadata && "expected valid metadata");
407 llvmModule->addModuleFlag(
408 convertModFlagBehaviorToLLVM(flagAttr.getBehavior()),
409 flagAttr.getKey().getValue(), valueMetadata);
410 }
411}
412
413/// Looks up the GlobalValue and FunctionType for a callee symbol that is not a
414/// regular LLVM function (i.e. an alias or ifunc). Returns the lowered
415/// GlobalValue and FunctionType derived from \p calleeFuncType.
416static std::pair<llvm::GlobalValue *, llvm::FunctionType *>
418 Operation &opInst,
419 LLVM::ModuleTranslation &moduleTranslation) {
420 Operation *moduleOp = parentLLVMModule(&opInst);
421 Operation *calleeOp =
422 moduleTranslation.symbolTable().lookupSymbolIn(moduleOp, attr);
423 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
424 moduleTranslation.convertType(calleeFuncType));
425 llvm::GlobalValue *calleeGV;
426 if (isa<LLVM::AliasOp>(calleeOp))
427 calleeGV = moduleTranslation.lookupAlias(calleeOp);
428 else
429 calleeGV = moduleTranslation.lookupIFunc(calleeOp);
430 return {calleeGV, calleeType};
431}
432
433static llvm::DILocalScope *
434getLocalScopeFromLoc(llvm::IRBuilderBase &builder, Location loc,
435 LLVM::ModuleTranslation &moduleTranslation) {
436 if (auto scopeLoc =
438 if (auto *localScope = llvm::dyn_cast<llvm::DILocalScope>(
439 moduleTranslation.translateDebugInfo(scopeLoc.getMetadata())))
440 return localScope;
441 return builder.GetInsertBlock()->getParent()->getSubprogram();
442}
443
444static LogicalResult
445convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder,
446 LLVM::ModuleTranslation &moduleTranslation) {
447
448 llvm::IRBuilder<>::FastMathFlagGuard fmfGuard(builder);
449 if (auto fmf = dyn_cast<FastmathFlagsInterface>(opInst))
450 builder.setFastMathFlags(getFastmathFlags(fmf));
451
452#include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
453#include "mlir/Dialect/LLVMIR/LLVMIntrinsicConversions.inc"
454
455 // Emit function calls. If the "callee" attribute is present, this is a
456 // direct function call and we also need to look up the remapped function
457 // itself. Otherwise, this is an indirect call and the callee is the first
458 // operand, look it up as a normal value.
459 if (auto callOp = dyn_cast<LLVM::CallOp>(opInst)) {
460 auto operands = moduleTranslation.lookupValues(callOp.getCalleeOperands());
462 convertOperandBundles(callOp.getOpBundleOperands(),
463 callOp.getOpBundleTags(), moduleTranslation);
464 ArrayRef<llvm::Value *> operandsRef(operands);
465 llvm::CallInst *call;
466 if (auto attr = callOp.getCalleeAttr()) {
467 if (llvm::Function *function =
468 moduleTranslation.lookupFunction(attr.getValue())) {
469 call = builder.CreateCall(function, operandsRef, opBundles);
470 } else {
471 auto [calleeGV, calleeType] = lookupNonFunctionSymbolCallee(
472 attr, callOp.getCalleeFunctionType(), opInst, moduleTranslation);
473 call = builder.CreateCall(calleeType, calleeGV, operandsRef, opBundles);
474 }
475 } else {
476 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
477 moduleTranslation.convertType(callOp.getCalleeFunctionType()));
478 call = builder.CreateCall(calleeType, operandsRef.front(),
479 operandsRef.drop_front(), opBundles);
480 }
481 call->setCallingConv(convertCConvToLLVM(callOp.getCConv()));
482 call->setTailCallKind(convertTailCallKindToLLVM(callOp.getTailCallKind()));
483 if (callOp.getConvergentAttr())
484 call->addFnAttr(llvm::Attribute::Convergent);
485 if (callOp.getNoUnwindAttr())
486 call->addFnAttr(llvm::Attribute::NoUnwind);
487 if (callOp.getWillReturnAttr())
488 call->addFnAttr(llvm::Attribute::WillReturn);
489 if (callOp.getNoreturnAttr())
490 call->addFnAttr(llvm::Attribute::NoReturn);
491 if (callOp.getOptsizeAttr())
492 call->addFnAttr(llvm::Attribute::OptimizeForSize);
493 if (callOp.getMinsizeAttr())
494 call->addFnAttr(llvm::Attribute::MinSize);
495 if (callOp.getSaveRegParamsAttr())
496 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
497 "save-reg-params"));
498 if (callOp.getBuiltinAttr())
499 call->addFnAttr(llvm::Attribute::Builtin);
500 if (callOp.getNobuiltinAttr())
501 call->addFnAttr(llvm::Attribute::NoBuiltin);
502 if (callOp.getReturnsTwiceAttr())
503 call->addFnAttr(llvm::Attribute::ReturnsTwice);
504 if (callOp.getColdAttr())
505 call->addFnAttr(llvm::Attribute::Cold);
506 if (callOp.getHotAttr())
507 call->addFnAttr(llvm::Attribute::Hot);
508 if (callOp.getNoduplicateAttr())
509 call->addFnAttr(llvm::Attribute::NoDuplicate);
510 if (callOp.getNoInlineAttr())
511 call->addFnAttr(llvm::Attribute::NoInline);
512 if (callOp.getAlwaysInlineAttr())
513 call->addFnAttr(llvm::Attribute::AlwaysInline);
514 if (callOp.getInlineHintAttr())
515 call->addFnAttr(llvm::Attribute::InlineHint);
516 if (callOp.getNoCallerSavedRegistersAttr())
517 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
518 "no_caller_saved_registers"));
519 if (callOp.getNocallbackAttr())
520 call->addFnAttr(llvm::Attribute::NoCallback);
521 if (StringAttr modFormat = callOp.getModularFormatAttr())
522 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
523 "modular-format",
524 modFormat.getValue()));
525 if (StringAttr zcsr = callOp.getZeroCallUsedRegsAttr())
526 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
527 "zero-call-used-regs",
528 zcsr.getValue()));
529 if (StringAttr trapFunc = callOp.getTrapFuncNameAttr())
530 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
531 "trap-func-name",
532 trapFunc.getValue()));
533
534 if (ArrayAttr noBuiltins = callOp.getNobuiltinsAttr()) {
535 if (noBuiltins.empty())
536 call->addFnAttr(llvm::Attribute::get(moduleTranslation.getLLVMContext(),
537 "no-builtins"));
538
539 moduleTranslation.convertFunctionAttrCollection(
540 noBuiltins, call, ModuleTranslation::convertNoBuiltin);
541 }
542
543 moduleTranslation.convertFunctionAttrCollection(
544 callOp.getDefaultFuncAttrsAttr(), call,
546
547 if (llvm::Attribute attr =
548 moduleTranslation.convertAllocsizeAttr(callOp.getAllocsizeAttr());
549 attr.isValid())
550 call->addFnAttr(attr);
551
552 if (failed(moduleTranslation.convertArgAndResultAttrs(callOp, call)))
553 return failure();
554
555 if (MemoryEffectsAttr memAttr = callOp.getMemoryEffectsAttr()) {
556 llvm::MemoryEffects memEffects =
557 llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
558 convertModRefInfoToLLVM(memAttr.getArgMem())) |
559 llvm::MemoryEffects(
560 llvm::MemoryEffects::Location::InaccessibleMem,
561 convertModRefInfoToLLVM(memAttr.getInaccessibleMem())) |
562 llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
563 convertModRefInfoToLLVM(memAttr.getOther())) |
564 llvm::MemoryEffects(llvm::MemoryEffects::Location::ErrnoMem,
565 convertModRefInfoToLLVM(memAttr.getErrnoMem())) |
566 llvm::MemoryEffects(
567 llvm::MemoryEffects::Location::TargetMem0,
568 convertModRefInfoToLLVM(memAttr.getTargetMem0())) |
569 llvm::MemoryEffects(llvm::MemoryEffects::Location::TargetMem1,
570 convertModRefInfoToLLVM(memAttr.getTargetMem1()));
571 call->setMemoryEffects(memEffects);
572 }
573
574 moduleTranslation.setAccessGroupsMetadata(callOp, call);
575 moduleTranslation.setAliasScopeMetadata(callOp, call);
576 moduleTranslation.setTBAAMetadata(callOp, call);
577 // If the called function has a result, remap the corresponding value. Note
578 // that LLVM IR dialect CallOp has either 0 or 1 result.
579 if (opInst.getNumResults() != 0)
580 moduleTranslation.mapValue(opInst.getResult(0), call);
581 // Check that LLVM call returns void for 0-result functions.
582 else if (!call->getType()->isVoidTy())
583 return failure();
584 moduleTranslation.mapCall(callOp, call);
585 return success();
586 }
587
588 if (auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
589 // TODO: refactor function type creation which usually occurs in std-LLVM
590 // conversion.
591 SmallVector<Type, 8> operandTypes;
592 llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
593
594 Type resultType;
595 if (inlineAsmOp.getNumResults() == 0) {
596 resultType = LLVM::LLVMVoidType::get(&moduleTranslation.getContext());
597 } else {
598 assert(inlineAsmOp.getNumResults() == 1);
599 resultType = inlineAsmOp.getResultTypes()[0];
600 }
601 auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes);
602 llvm::InlineAsm *inlineAsmInst =
603 inlineAsmOp.getAsmDialect()
604 ? llvm::InlineAsm::get(
605 static_cast<llvm::FunctionType *>(
606 moduleTranslation.convertType(ft)),
607 inlineAsmOp.getAsmString(), inlineAsmOp.getConstraints(),
608 inlineAsmOp.getHasSideEffects(),
609 inlineAsmOp.getIsAlignStack(),
610 convertAsmDialectToLLVM(*inlineAsmOp.getAsmDialect()))
611 : llvm::InlineAsm::get(static_cast<llvm::FunctionType *>(
612 moduleTranslation.convertType(ft)),
613 inlineAsmOp.getAsmString(),
614 inlineAsmOp.getConstraints(),
615 inlineAsmOp.getHasSideEffects(),
616 inlineAsmOp.getIsAlignStack());
617 llvm::CallInst *inst = builder.CreateCall(
618 inlineAsmInst,
619 moduleTranslation.lookupValues(inlineAsmOp.getOperands()));
620 inst->setTailCallKind(convertTailCallKindToLLVM(
621 inlineAsmOp.getTailCallKindAttr().getTailCallKind()));
622 if (auto maybeOperandAttrs = inlineAsmOp.getOperandAttrs()) {
623 llvm::AttributeList attrList;
624 for (const auto &it : llvm::enumerate(*maybeOperandAttrs)) {
625 Attribute attr = it.value();
626 if (!attr)
627 continue;
628 DictionaryAttr dAttr = cast<DictionaryAttr>(attr);
629 if (dAttr.empty())
630 continue;
631 TypeAttr tAttr =
632 cast<TypeAttr>(dAttr.get(InlineAsmOp::getElementTypeAttrName()));
633 llvm::AttrBuilder b(moduleTranslation.getLLVMContext());
634 llvm::Type *ty = moduleTranslation.convertType(tAttr.getValue());
635 b.addTypeAttr(llvm::Attribute::ElementType, ty);
636 // shift to account for the returned value (this is always 1 aggregate
637 // value in LLVM).
638 int shift = (opInst.getNumResults() > 0) ? 1 : 0;
639 attrList = attrList.addAttributesAtIndex(
640 moduleTranslation.getLLVMContext(), it.index() + shift, b);
641 }
642 inst->setAttributes(attrList);
643 }
644
645 if (opInst.getNumResults() != 0)
646 moduleTranslation.mapValue(opInst.getResult(0), inst);
647 return success();
648 }
649
650 if (auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
651 auto operands = moduleTranslation.lookupValues(invOp.getCalleeOperands());
653 convertOperandBundles(invOp.getOpBundleOperands(),
654 invOp.getOpBundleTags(), moduleTranslation);
655 ArrayRef<llvm::Value *> operandsRef(operands);
656 llvm::InvokeInst *result;
657 if (auto attr = opInst.getAttrOfType<FlatSymbolRefAttr>("callee")) {
658 if (llvm::Function *function =
659 moduleTranslation.lookupFunction(attr.getValue())) {
660 result = builder.CreateInvoke(
661 function, moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
662 moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef,
663 opBundles);
664 } else {
665 auto [calleeGV, calleeType] = lookupNonFunctionSymbolCallee(
666 attr, invOp.getCalleeFunctionType(), opInst, moduleTranslation);
667 result = builder.CreateInvoke(
668 calleeType, calleeGV,
669 moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
670 moduleTranslation.lookupBlock(invOp.getSuccessor(1)), operandsRef,
671 opBundles);
672 }
673 } else {
674 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
675 moduleTranslation.convertType(invOp.getCalleeFunctionType()));
676 result = builder.CreateInvoke(
677 calleeType, operandsRef.front(),
678 moduleTranslation.lookupBlock(invOp.getSuccessor(0)),
679 moduleTranslation.lookupBlock(invOp.getSuccessor(1)),
680 operandsRef.drop_front(), opBundles);
681 }
682 result->setCallingConv(convertCConvToLLVM(invOp.getCConv()));
683 if (failed(moduleTranslation.convertArgAndResultAttrs(invOp, result)))
684 return failure();
685 moduleTranslation.mapBranch(invOp, result);
686 // InvokeOp can only have 0 or 1 result
687 if (invOp->getNumResults() != 0) {
688 moduleTranslation.mapValue(opInst.getResult(0), result);
689 return success();
690 }
691 return success(result->getType()->isVoidTy());
692 }
693
694 if (auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
695 llvm::Type *ty = moduleTranslation.convertType(lpOp.getType());
696 llvm::LandingPadInst *lpi =
697 builder.CreateLandingPad(ty, lpOp.getNumOperands());
698 lpi->setCleanup(lpOp.getCleanup());
699
700 // Add clauses
701 for (llvm::Value *operand :
702 moduleTranslation.lookupValues(lpOp.getOperands())) {
703 // All operands should be constant - checked by verifier
704 if (auto *constOperand = dyn_cast<llvm::Constant>(operand))
705 lpi->addClause(constOperand);
706 }
707 moduleTranslation.mapValue(lpOp.getResult(), lpi);
708 return success();
709 }
710
711 // Emit branches. We need to look up the remapped blocks and ignore the
712 // block arguments that were transformed into PHI nodes.
713 if (auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
714 llvm::UncondBrInst *branch =
715 builder.CreateBr(moduleTranslation.lookupBlock(brOp.getSuccessor()));
716 moduleTranslation.mapBranch(&opInst, branch);
717 moduleTranslation.setLoopMetadata(&opInst, branch);
718 return success();
719 }
720 if (auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
721 llvm::CondBrInst *branch = builder.CreateCondBr(
722 moduleTranslation.lookupValue(condbrOp.getOperand(0)),
723 moduleTranslation.lookupBlock(condbrOp.getSuccessor(0)),
724 moduleTranslation.lookupBlock(condbrOp.getSuccessor(1)));
725 moduleTranslation.mapBranch(&opInst, branch);
726 moduleTranslation.setLoopMetadata(&opInst, branch);
727 return success();
728 }
729 if (auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) {
730 llvm::SwitchInst *switchInst = builder.CreateSwitch(
731 moduleTranslation.lookupValue(switchOp.getValue()),
732 moduleTranslation.lookupBlock(switchOp.getDefaultDestination()),
733 switchOp.getCaseDestinations().size());
734
735 // Handle switch with zero cases.
736 if (!switchOp.getCaseValues())
737 return success();
738
739 auto *ty = llvm::cast<llvm::IntegerType>(
740 moduleTranslation.convertType(switchOp.getValue().getType()));
741 for (auto i :
742 llvm::zip(llvm::cast<DenseIntElementsAttr>(*switchOp.getCaseValues()),
743 switchOp.getCaseDestinations()))
744 switchInst->addCase(
745 llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()),
746 moduleTranslation.lookupBlock(std::get<1>(i)));
747
748 moduleTranslation.mapBranch(&opInst, switchInst);
749 return success();
750 }
751 if (auto indBrOp = dyn_cast<LLVM::IndirectBrOp>(opInst)) {
752 llvm::IndirectBrInst *indBr = builder.CreateIndirectBr(
753 moduleTranslation.lookupValue(indBrOp.getAddr()),
754 indBrOp->getNumSuccessors());
755 for (auto *succ : indBrOp.getSuccessors())
756 indBr->addDestination(moduleTranslation.lookupBlock(succ));
757 moduleTranslation.mapBranch(&opInst, indBr);
758 return success();
759 }
760
761 // Emit addressof. We need to look up the global value referenced by the
762 // operation and store it in the MLIR-to-LLVM value mapping. This does not
763 // emit any LLVM instruction.
764 if (auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
765 LLVM::GlobalOp global =
766 addressOfOp.getGlobal(moduleTranslation.symbolTable());
767 LLVM::LLVMFuncOp function =
768 addressOfOp.getFunction(moduleTranslation.symbolTable());
769 LLVM::AliasOp alias = addressOfOp.getAlias(moduleTranslation.symbolTable());
770 LLVM::IFuncOp ifunc = addressOfOp.getIFunc(moduleTranslation.symbolTable());
771
772 // The verifier should not have allowed this.
773 assert((global || function || alias || ifunc) &&
774 "referencing an undefined global, function, alias, or ifunc");
775
776 llvm::Value *llvmValue = nullptr;
777 if (global)
778 llvmValue = moduleTranslation.lookupGlobal(global);
779 else if (alias)
780 llvmValue = moduleTranslation.lookupAlias(alias);
781 else if (function)
782 llvmValue = moduleTranslation.lookupFunction(function.getName());
783 else
784 llvmValue = moduleTranslation.lookupIFunc(ifunc);
785
786 moduleTranslation.mapValue(addressOfOp.getResult(), llvmValue);
787 return success();
788 }
789
790 // Emit dso_local_equivalent. We need to look up the global value referenced
791 // by the operation and store it in the MLIR-to-LLVM value mapping.
792 if (auto dsoLocalEquivalentOp =
793 dyn_cast<LLVM::DSOLocalEquivalentOp>(opInst)) {
794 LLVM::LLVMFuncOp function =
795 dsoLocalEquivalentOp.getFunction(moduleTranslation.symbolTable());
796 LLVM::AliasOp alias =
797 dsoLocalEquivalentOp.getAlias(moduleTranslation.symbolTable());
798
799 // The verifier should not have allowed this.
800 assert((function || alias) &&
801 "referencing an undefined function, or alias");
802
803 llvm::Value *llvmValue = nullptr;
804 if (alias)
805 llvmValue = moduleTranslation.lookupAlias(alias);
806 else
807 llvmValue = moduleTranslation.lookupFunction(function.getName());
808
809 moduleTranslation.mapValue(
810 dsoLocalEquivalentOp.getResult(),
811 llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(llvmValue)));
812 return success();
813 }
814
815 // Emit blockaddress. We first need to find the LLVM block referenced by this
816 // operation and then create a LLVM block address for it.
817 if (auto blockAddressOp = dyn_cast<LLVM::BlockAddressOp>(opInst)) {
818 BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
819 llvm::BasicBlock *llvmBlock =
820 moduleTranslation.lookupBlockAddress(blockAddressAttr);
821
822 llvm::Value *llvmValue = nullptr;
823 StringRef fnName = blockAddressAttr.getFunction().getValue();
824 if (llvmBlock) {
825 llvm::Function *llvmFn = moduleTranslation.lookupFunction(fnName);
826 llvmValue = llvm::BlockAddress::get(llvmFn, llvmBlock);
827 } else {
828 // The matching LLVM block is not yet emitted, a placeholder is created
829 // in its place. When the LLVM block is emitted later in translation,
830 // the llvmValue is replaced with the actual llvm::BlockAddress.
831 // A GlobalVariable is chosen as placeholder because in general LLVM
832 // constants are uniqued and are not proper for RAUW, since that could
833 // harm unrelated uses of the constant.
834 llvmValue = new llvm::GlobalVariable(
835 *moduleTranslation.getLLVMModule(),
836 llvm::PointerType::getUnqual(moduleTranslation.getLLVMContext()),
837 /*isConstant=*/true, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
838 /*Initializer=*/nullptr,
839 Twine("__mlir_block_address_")
840 .concat(Twine(fnName))
841 .concat(Twine((uint64_t)blockAddressOp.getOperation())));
842 moduleTranslation.mapUnresolvedBlockAddress(blockAddressOp, llvmValue);
843 }
844
845 moduleTranslation.mapValue(blockAddressOp.getResult(), llvmValue);
846 return success();
847 }
848
849 // Emit block label. If this label is seen before BlockAddressOp is
850 // translated, go ahead and already map it.
851 if (auto blockTagOp = dyn_cast<LLVM::BlockTagOp>(opInst)) {
852 auto funcOp = blockTagOp->getParentOfType<LLVMFuncOp>();
853 BlockAddressAttr blockAddressAttr = BlockAddressAttr::get(
854 &moduleTranslation.getContext(),
855 FlatSymbolRefAttr::get(&moduleTranslation.getContext(),
856 funcOp.getName()),
857 blockTagOp.getTag());
858 moduleTranslation.mapBlockAddress(blockAddressAttr,
859 builder.GetInsertBlock());
860 return success();
861 }
862
863 return failure();
864}
865
866static LogicalResult
868 NamedAttribute attribute,
869 LLVM::ModuleTranslation &moduleTranslation) {
870 StringRef name = attribute.getName();
871 if (name == LLVMDialect::getMmraAttrName()) {
873 if (auto oneTag = dyn_cast<LLVM::MMRATagAttr>(attribute.getValue())) {
874 tags.emplace_back(oneTag.getPrefix(), oneTag.getSuffix());
875 } else if (auto manyTags = dyn_cast<ArrayAttr>(attribute.getValue())) {
876 for (Attribute attr : manyTags) {
877 auto tag = dyn_cast<MMRATagAttr>(attr);
878 if (!tag)
879 return op.emitOpError(
880 "MMRA annotations array contains value that isn't an MMRA tag");
881 tags.emplace_back(tag.getPrefix(), tag.getSuffix());
882 }
883 } else {
884 return op.emitOpError(
885 "llvm.mmra is something other than an MMRA tag or an array of them");
886 }
887 llvm::MDTuple *mmraMd =
888 llvm::MMRAMetadata::getMD(moduleTranslation.getLLVMContext(), tags);
889 if (!mmraMd) {
890 // Empty list, canonicalizes to nothing
891 return success();
892 }
893 for (llvm::Instruction *inst : instructions)
894 inst->setMetadata(llvm::LLVMContext::MD_mmra, mmraMd);
895 return success();
896 }
897 return success();
898}
899
900namespace {
901/// Implementation of the dialect interface that converts operations belonging
902/// to the LLVM dialect to LLVM IR.
903class LLVMDialectLLVMIRTranslationInterface
904 : public LLVMTranslationDialectInterface {
905public:
906 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
907
908 /// Translates the given operation to LLVM IR using the provided IR builder
909 /// and saving the state in `moduleTranslation`.
910 LogicalResult
911 convertOperation(Operation *op, llvm::IRBuilderBase &builder,
912 LLVM::ModuleTranslation &moduleTranslation) const final {
913 return convertOperationImpl(*op, builder, moduleTranslation);
914 }
915
916 /// Handle some metadata that is represented as a discardable attribute.
917 LogicalResult
918 amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
919 NamedAttribute attribute,
920 LLVM::ModuleTranslation &moduleTranslation) const final {
921 return amendOperationImpl(*op, instructions, attribute, moduleTranslation);
922 }
923};
924} // namespace
925
927 registry.insert<LLVM::LLVMDialect>();
928 registry.addExtension(+[](MLIRContext *ctx, LLVM::LLVMDialect *dialect) {
929 dialect->addInterfaces<LLVMDialectLLVMIRTranslationInterface>();
930 });
931}
932
934 DialectRegistry registry;
936 context.appendDialectRegistry(registry);
937}
return success()
static std::string diagStr(const llvm::Type *type)
Convert an LLVM type to a string for printing in diagnostics.
static llvm::Metadata * convertModuleFlagValue(StringRef key, ArrayAttr arrayAttr, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static SmallVector< llvm::OperandBundleDef > convertOperandBundles(OperandRangeRange bundleOperands, ArrayAttr bundleTags, LLVM::ModuleTranslation &moduleTranslation)
static std::pair< llvm::GlobalValue *, llvm::FunctionType * > lookupNonFunctionSymbolCallee(FlatSymbolRefAttr attr, mlir::Type calleeFuncType, Operation &opInst, LLVM::ModuleTranslation &moduleTranslation)
Looks up the GlobalValue and FunctionType for a callee symbol that is not a regular LLVM function (i....
static FailureOr< llvm::Function * > getOverloadedDeclaration(CallIntrinsicOp op, llvm::Intrinsic::ID id, llvm::Module *module, LLVM::ModuleTranslation &moduleTranslation)
Get the declaration of an overloaded llvm intrinsic.
static void convertModuleFlagsOp(ArrayAttr flags, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static LogicalResult convertOperationImpl(Operation &opInst, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static llvm::Metadata * convertMetadataAttr(Attribute attr, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
Recursively converts an MLIR metadata attribute to an LLVM metadata node.
static void convertNamedMetadataOp(StringRef metadataName, ArrayAttr nodes, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static LogicalResult convertCallLLVMIntrinsicOp(CallIntrinsicOp op, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
Builder for LLVM_CallIntrinsicOp.
static LogicalResult amendOperationImpl(Operation &op, ArrayRef< llvm::Instruction * > instructions, NamedAttribute attribute, LLVM::ModuleTranslation &moduleTranslation)
static llvm::FastMathFlags getFastmathFlags(FastmathFlagsInterface &op)
static llvm::OperandBundleDef convertOperandBundle(OperandRange bundleOperands, StringRef bundleTag, LLVM::ModuleTranslation &moduleTranslation)
static SmallVector< unsigned > extractPosition(ArrayRef< int64_t > indices)
Convert the value of a DenseI64ArrayAttr to a vector of unsigned indices.
static llvm::DILocalScope * getLocalScopeFromLoc(llvm::IRBuilderBase &builder, Location loc, LLVM::ModuleTranslation &moduleTranslation)
static llvm::Metadata * convertModuleFlagProfileSummaryAttr(StringRef key, ModuleFlagProfileSummaryAttr summaryAttr, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
static void convertLinkerOptionsOp(ArrayAttr options, llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation)
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
ArrayAttr()
static llvm::ManagedStatic< PassManagerOptions > options
Attributes are known-constant values of operations.
Definition Attributes.h:25
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
A symbol reference with a reference path containing a single element.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
This class represents a fused location whose metadata is known to be an instance of the given type.
Definition Location.h:149
Implementation class for module translation.
void mapUnresolvedBlockAddress(BlockAddressOp op, llvm::Value *cst)
Maps a blockaddress operation to its corresponding placeholder LLVM value.
void mapCall(Operation *mlir, llvm::CallInst *llvm)
Stores a mapping between an MLIR call operation and a corresponding LLVM call instruction.
llvm::BasicBlock * lookupBlock(Block *block) const
Finds an LLVM IR basic block that corresponds to the given MLIR block.
llvm::Attribute convertAllocsizeAttr(DenseI32ArrayAttr allocsizeAttr)
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
void mapBranch(Operation *mlir, llvm::Instruction *llvm)
Stores the mapping between an MLIR operation with successors and a corresponding LLVM IR instruction.
SmallVector< llvm::Value * > lookupValues(ValueRange values)
Looks up remapped a list of remapped values.
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::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.
void setTBAAMetadata(AliasAnalysisOpInterface op, llvm::Instruction *inst)
Sets LLVM TBAA metadata for memory operations that have TBAA attributes.
llvm::GlobalValue * lookupGlobal(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global value.
llvm::BasicBlock * lookupBlockAddress(BlockAddressAttr attr) const
Finds the LLVM basic block that corresponds to the given BlockAddressAttr.
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.
llvm::GlobalValue * lookupAlias(Operation *op)
Finds an LLVM IR global value that corresponds to the given MLIR operation defining a global alias va...
llvm::Function * lookupFunction(StringRef name) const
Finds an LLVM IR function by its name.
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 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 mapBlockAddress(BlockAddressAttr attr, llvm::BasicBlock *block)
Maps a BlockAddressAttr to its corresponding LLVM basic block.
void setLoopMetadata(Operation *op, llvm::Instruction *inst)
Sets LLVM loop metadata for branch operations that have a loop annotation attribute.
T findInstanceOf()
Return an instance of the given location type if one is nested under the current location.
Definition Location.h:45
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
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
This class represents a contiguous range of operand ranges, e.g.
Definition ValueRange.h:85
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
AttrClass getAttrOfType(StringAttr name)
Definition Operation.h:576
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:433
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:430
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 represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
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.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
void registerLLVMDialectTranslation(DialectRegistry &registry)
Register the LLVM dialect and the translation from it to the LLVM IR in the given registry;.