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