269 StringRef key, ModuleFlagProfileSummaryAttr summaryAttr,
271 llvm::LLVMContext &context = builder.getContext();
272 llvm::MDBuilder mdb(context);
274 auto getIntTuple = [&](StringRef key, uint64_t val) -> llvm::MDTuple * {
276 mdb.createString(key), mdb.createConstant(llvm::ConstantInt::get(
277 llvm::Type::getInt64Ty(context), val))};
278 return llvm::MDTuple::get(context, tupleNodes);
282 mdb.createString(
"ProfileFormat"),
284 stringifyProfileSummaryFormatKind(summaryAttr.getFormat()))};
287 llvm::MDTuple::get(context, fmtNode),
288 getIntTuple(
"TotalCount", summaryAttr.getTotalCount()),
289 getIntTuple(
"MaxCount", summaryAttr.getMaxCount()),
290 getIntTuple(
"MaxInternalCount", summaryAttr.getMaxInternalCount()),
291 getIntTuple(
"MaxFunctionCount", summaryAttr.getMaxFunctionCount()),
292 getIntTuple(
"NumCounts", summaryAttr.getNumCounts()),
293 getIntTuple(
"NumFunctions", summaryAttr.getNumFunctions()),
296 if (summaryAttr.getIsPartialProfile())
298 getIntTuple(
"IsPartialProfile", *summaryAttr.getIsPartialProfile()));
300 if (summaryAttr.getPartialProfileRatio()) {
302 mdb.createString(
"PartialProfileRatio"),
303 mdb.createConstant(llvm::ConstantFP::get(
304 llvm::Type::getDoubleTy(context),
305 summaryAttr.getPartialProfileRatio().getValue()))};
306 vals.push_back(llvm::MDTuple::get(context, tupleNodes));
310 llvm::Type *llvmInt64Type = llvm::Type::getInt64Ty(context);
311 for (ModuleFlagProfileSummaryDetailedAttr detailedEntry :
312 summaryAttr.getDetailedSummary()) {
315 llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getCutOff())),
317 llvm::ConstantInt::get(llvmInt64Type, detailedEntry.getMinCount())),
318 mdb.createConstant(llvm::ConstantInt::get(
319 llvmInt64Type, detailedEntry.getNumCounts()))};
320 detailedEntries.push_back(llvm::MDTuple::get(context, tupleNodes));
323 mdb.createString(
"DetailedSummary"),
324 llvm::MDTuple::get(context, detailedEntries)};
325 vals.push_back(llvm::MDTuple::get(context, detailedSummary));
327 return llvm::MDNode::get(context, vals);
379 llvm::IRBuilder<>::FastMathFlagGuard fmfGuard(builder);
380 if (
auto fmf = dyn_cast<FastmathFlagsInterface>(opInst))
383#include "mlir/Dialect/LLVMIR/LLVMConversions.inc"
384#include "mlir/Dialect/LLVMIR/LLVMIntrinsicConversions.inc"
390 if (
auto callOp = dyn_cast<LLVM::CallOp>(opInst)) {
391 auto operands = moduleTranslation.
lookupValues(callOp.getCalleeOperands());
394 callOp.getOpBundleTags(), moduleTranslation);
396 llvm::CallInst *call;
397 if (
auto attr = callOp.getCalleeAttr()) {
398 if (llvm::Function *function =
400 call = builder.CreateCall(function, operandsRef, opBundles);
405 llvm::GlobalValue *ifunc = moduleTranslation.
lookupIFunc(ifuncOp);
406 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
407 moduleTranslation.
convertType(callOp.getCalleeFunctionType()));
408 call = builder.CreateCall(calleeType, ifunc, operandsRef, opBundles);
411 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
412 moduleTranslation.
convertType(callOp.getCalleeFunctionType()));
413 call = builder.CreateCall(calleeType, operandsRef.front(),
414 operandsRef.drop_front(), opBundles);
416 call->setCallingConv(convertCConvToLLVM(callOp.getCConv()));
417 call->setTailCallKind(convertTailCallKindToLLVM(callOp.getTailCallKind()));
418 if (callOp.getConvergentAttr())
419 call->addFnAttr(llvm::Attribute::Convergent);
420 if (callOp.getNoUnwindAttr())
421 call->addFnAttr(llvm::Attribute::NoUnwind);
422 if (callOp.getWillReturnAttr())
423 call->addFnAttr(llvm::Attribute::WillReturn);
424 if (callOp.getNoInlineAttr())
425 call->addFnAttr(llvm::Attribute::NoInline);
426 if (callOp.getAlwaysInlineAttr())
427 call->addFnAttr(llvm::Attribute::AlwaysInline);
428 if (callOp.getInlineHintAttr())
429 call->addFnAttr(llvm::Attribute::InlineHint);
434 if (MemoryEffectsAttr memAttr = callOp.getMemoryEffectsAttr()) {
435 llvm::MemoryEffects memEffects =
436 llvm::MemoryEffects(llvm::MemoryEffects::Location::ArgMem,
437 convertModRefInfoToLLVM(memAttr.getArgMem())) |
439 llvm::MemoryEffects::Location::InaccessibleMem,
440 convertModRefInfoToLLVM(memAttr.getInaccessibleMem())) |
441 llvm::MemoryEffects(llvm::MemoryEffects::Location::Other,
442 convertModRefInfoToLLVM(memAttr.getOther())) |
443 llvm::MemoryEffects(llvm::MemoryEffects::Location::ErrnoMem,
444 convertModRefInfoToLLVM(memAttr.getErrnoMem())) |
446 llvm::MemoryEffects::Location::TargetMem0,
447 convertModRefInfoToLLVM(memAttr.getTargetMem0())) |
448 llvm::MemoryEffects(llvm::MemoryEffects::Location::TargetMem1,
449 convertModRefInfoToLLVM(memAttr.getTargetMem1()));
450 call->setMemoryEffects(memEffects);
461 else if (!call->getType()->isVoidTy())
463 moduleTranslation.
mapCall(callOp, call);
467 if (
auto inlineAsmOp = dyn_cast<LLVM::InlineAsmOp>(opInst)) {
471 llvm::append_range(operandTypes, inlineAsmOp.getOperands().getTypes());
474 if (inlineAsmOp.getNumResults() == 0) {
475 resultType = LLVM::LLVMVoidType::get(&moduleTranslation.
getContext());
477 assert(inlineAsmOp.getNumResults() == 1);
478 resultType = inlineAsmOp.getResultTypes()[0];
480 auto ft = LLVM::LLVMFunctionType::get(resultType, operandTypes);
481 llvm::InlineAsm *inlineAsmInst =
482 inlineAsmOp.getAsmDialect()
483 ? llvm::InlineAsm::get(
484 static_cast<llvm::FunctionType *
>(
486 inlineAsmOp.getAsmString(), inlineAsmOp.getConstraints(),
487 inlineAsmOp.getHasSideEffects(),
488 inlineAsmOp.getIsAlignStack(),
489 convertAsmDialectToLLVM(*inlineAsmOp.getAsmDialect()))
490 : llvm::InlineAsm::get(
static_cast<llvm::FunctionType *
>(
492 inlineAsmOp.getAsmString(),
493 inlineAsmOp.getConstraints(),
494 inlineAsmOp.getHasSideEffects(),
495 inlineAsmOp.getIsAlignStack());
496 llvm::CallInst *inst = builder.CreateCall(
498 moduleTranslation.
lookupValues(inlineAsmOp.getOperands()));
499 inst->setTailCallKind(convertTailCallKindToLLVM(
500 inlineAsmOp.getTailCallKindAttr().getTailCallKind()));
501 if (
auto maybeOperandAttrs = inlineAsmOp.getOperandAttrs()) {
502 llvm::AttributeList attrList;
503 for (
const auto &it : llvm::enumerate(*maybeOperandAttrs)) {
507 DictionaryAttr dAttr = cast<DictionaryAttr>(attr);
511 cast<TypeAttr>(dAttr.get(InlineAsmOp::getElementTypeAttrName()));
513 llvm::Type *ty = moduleTranslation.
convertType(tAttr.getValue());
514 b.addTypeAttr(llvm::Attribute::ElementType, ty);
518 attrList = attrList.addAttributesAtIndex(
521 inst->setAttributes(attrList);
529 if (
auto invOp = dyn_cast<LLVM::InvokeOp>(opInst)) {
530 auto operands = moduleTranslation.
lookupValues(invOp.getCalleeOperands());
533 invOp.getOpBundleTags(), moduleTranslation);
537 result = builder.CreateInvoke(
539 moduleTranslation.
lookupBlock(invOp.getSuccessor(0)),
540 moduleTranslation.
lookupBlock(invOp.getSuccessor(1)), operandsRef,
543 llvm::FunctionType *calleeType = llvm::cast<llvm::FunctionType>(
544 moduleTranslation.
convertType(invOp.getCalleeFunctionType()));
545 result = builder.CreateInvoke(
546 calleeType, operandsRef.front(),
547 moduleTranslation.
lookupBlock(invOp.getSuccessor(0)),
548 moduleTranslation.
lookupBlock(invOp.getSuccessor(1)),
549 operandsRef.drop_front(), opBundles);
551 result->setCallingConv(convertCConvToLLVM(invOp.getCConv()));
556 if (invOp->getNumResults() != 0) {
563 if (
auto lpOp = dyn_cast<LLVM::LandingpadOp>(opInst)) {
564 llvm::Type *ty = moduleTranslation.
convertType(lpOp.getType());
565 llvm::LandingPadInst *lpi =
566 builder.CreateLandingPad(ty, lpOp.getNumOperands());
567 lpi->setCleanup(lpOp.getCleanup());
570 for (llvm::Value *operand :
573 if (
auto *constOperand = dyn_cast<llvm::Constant>(operand))
574 lpi->addClause(constOperand);
576 moduleTranslation.
mapValue(lpOp.getResult(), lpi);
582 if (
auto brOp = dyn_cast<LLVM::BrOp>(opInst)) {
583 llvm::BranchInst *branch =
584 builder.CreateBr(moduleTranslation.
lookupBlock(brOp.getSuccessor()));
585 moduleTranslation.
mapBranch(&opInst, branch);
589 if (
auto condbrOp = dyn_cast<LLVM::CondBrOp>(opInst)) {
590 llvm::BranchInst *branch = builder.CreateCondBr(
591 moduleTranslation.
lookupValue(condbrOp.getOperand(0)),
592 moduleTranslation.
lookupBlock(condbrOp.getSuccessor(0)),
593 moduleTranslation.
lookupBlock(condbrOp.getSuccessor(1)));
594 moduleTranslation.
mapBranch(&opInst, branch);
598 if (
auto switchOp = dyn_cast<LLVM::SwitchOp>(opInst)) {
599 llvm::SwitchInst *switchInst = builder.CreateSwitch(
600 moduleTranslation.
lookupValue(switchOp.getValue()),
601 moduleTranslation.
lookupBlock(switchOp.getDefaultDestination()),
602 switchOp.getCaseDestinations().size());
605 if (!switchOp.getCaseValues())
608 auto *ty = llvm::cast<llvm::IntegerType>(
609 moduleTranslation.
convertType(switchOp.getValue().getType()));
611 llvm::zip(llvm::cast<DenseIntElementsAttr>(*switchOp.getCaseValues()),
612 switchOp.getCaseDestinations()))
614 llvm::ConstantInt::get(ty, std::get<0>(i).getLimitedValue()),
617 moduleTranslation.
mapBranch(&opInst, switchInst);
620 if (
auto indBrOp = dyn_cast<LLVM::IndirectBrOp>(opInst)) {
621 llvm::IndirectBrInst *indBr = builder.CreateIndirectBr(
623 indBrOp->getNumSuccessors());
624 for (
auto *succ : indBrOp.getSuccessors())
625 indBr->addDestination(moduleTranslation.
lookupBlock(succ));
626 moduleTranslation.
mapBranch(&opInst, indBr);
633 if (
auto addressOfOp = dyn_cast<LLVM::AddressOfOp>(opInst)) {
634 LLVM::GlobalOp global =
635 addressOfOp.getGlobal(moduleTranslation.
symbolTable());
636 LLVM::LLVMFuncOp function =
637 addressOfOp.getFunction(moduleTranslation.
symbolTable());
638 LLVM::AliasOp alias = addressOfOp.getAlias(moduleTranslation.
symbolTable());
639 LLVM::IFuncOp ifunc = addressOfOp.getIFunc(moduleTranslation.
symbolTable());
642 assert((global || function || alias || ifunc) &&
643 "referencing an undefined global, function, alias, or ifunc");
645 llvm::Value *llvmValue =
nullptr;
655 moduleTranslation.
mapValue(addressOfOp.getResult(), llvmValue);
661 if (
auto dsoLocalEquivalentOp =
662 dyn_cast<LLVM::DSOLocalEquivalentOp>(opInst)) {
663 LLVM::LLVMFuncOp function =
664 dsoLocalEquivalentOp.getFunction(moduleTranslation.
symbolTable());
665 LLVM::AliasOp alias =
666 dsoLocalEquivalentOp.getAlias(moduleTranslation.
symbolTable());
669 assert((function || alias) &&
670 "referencing an undefined function, or alias");
672 llvm::Value *llvmValue =
nullptr;
679 dsoLocalEquivalentOp.getResult(),
680 llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(llvmValue)));
686 if (
auto blockAddressOp = dyn_cast<LLVM::BlockAddressOp>(opInst)) {
687 BlockAddressAttr blockAddressAttr = blockAddressOp.getBlockAddr();
688 llvm::BasicBlock *llvmBlock =
691 llvm::Value *llvmValue =
nullptr;
692 StringRef fnName = blockAddressAttr.getFunction().getValue();
694 llvm::Function *llvmFn = moduleTranslation.
lookupFunction(fnName);
695 llvmValue = llvm::BlockAddress::get(llvmFn, llvmBlock);
703 llvmValue =
new llvm::GlobalVariable(
706 true, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
708 Twine(
"__mlir_block_address_")
709 .concat(Twine(fnName))
710 .concat(Twine((uint64_t)blockAddressOp.getOperation())));
714 moduleTranslation.
mapValue(blockAddressOp.getResult(), llvmValue);
720 if (
auto blockTagOp = dyn_cast<LLVM::BlockTagOp>(opInst)) {
721 auto funcOp = blockTagOp->getParentOfType<LLVMFuncOp>();
722 BlockAddressAttr blockAddressAttr = BlockAddressAttr::get(
726 blockTagOp.getTag());
728 builder.GetInsertBlock());