diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h b/llvm/include/llvm/Transforms/Utils/Cloning.h --- a/llvm/include/llvm/Transforms/Utils/Cloning.h +++ b/llvm/include/llvm/Transforms/Utils/Cloning.h @@ -265,6 +265,14 @@ bool InsertLifetime = true, Function *ForwardVarArgsTo = nullptr); +/// In addition to what InlineFunction does, this function merges the callee's +/// attributes into the caller's upon successful inlining. You should call this +/// function rather than InlineFunctionImpl if you are inlining a normal user +/// function. +InlineResult inlineFunctionAndMergeAttributes( + CallBase &CB, InlineFunctionInfo &IFI, AAResults *CalleeAAR = nullptr, + bool InsertLifetime = true, Function *ForwardVarArgsTo = nullptr); + /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p /// Blocks. /// diff --git a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp --- a/llvm/lib/Transforms/IPO/AlwaysInliner.cpp +++ b/llvm/lib/Transforms/IPO/AlwaysInliner.cpp @@ -70,7 +70,7 @@ &FAM.getResult(*Caller), &FAM.getResult(F)); - InlineResult Res = InlineFunction( + InlineResult Res = inlineFunctionAndMergeAttributes( *CB, IFI, &FAM.getResult(F), InsertLifetime); if (!Res.isSuccess()) { ORE.emit([&]() { @@ -88,9 +88,6 @@ InlineCost::getAlways("always inline attribute"), /*ForProfileContext=*/false, DEBUG_TYPE); - // Merge the attributes based on the inlining. - AttributeFuncs::mergeAttributesForInlining(*Caller, F); - Changed = true; } diff --git a/llvm/lib/Transforms/IPO/Inliner.cpp b/llvm/lib/Transforms/IPO/Inliner.cpp --- a/llvm/lib/Transforms/IPO/Inliner.cpp +++ b/llvm/lib/Transforms/IPO/Inliner.cpp @@ -315,15 +315,14 @@ // Try to inline the function. Get the list of static allocas that were // inlined. - InlineResult IR = InlineFunction(CB, IFI, &AAR, InsertLifetime); + InlineResult IR = + inlineFunctionAndMergeAttributes(CB, IFI, &AAR, InsertLifetime); if (!IR.isSuccess()) return IR; if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No) ImportedFunctionsStats.recordInline(*Caller, *Callee); - AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee); - if (!DisableInlinedAllocaMerging) mergeInlinedArrayAllocas(Caller, IFI, InlinedArrayAllocas, InlineHistory); @@ -914,8 +913,8 @@ &FAM.getResult(*(CB->getCaller())), &FAM.getResult(Callee)); - InlineResult IR = - InlineFunction(*CB, IFI, &FAM.getResult(*CB->getCaller())); + InlineResult IR = inlineFunctionAndMergeAttributes( + *CB, IFI, &FAM.getResult(*CB->getCaller())); if (!IR.isSuccess()) { Advice->recordUnsuccessfulInlining(IR); continue; @@ -970,9 +969,6 @@ } } - // Merge the attributes based on the inlining. - AttributeFuncs::mergeAttributesForInlining(F, Callee); - // For local functions or discardable functions without comdats, check // whether this makes the callee trivially dead. In that case, we can drop // the body of the function eagerly which may reduce the number of callers diff --git a/llvm/lib/Transforms/IPO/ModuleInliner.cpp b/llvm/lib/Transforms/IPO/ModuleInliner.cpp --- a/llvm/lib/Transforms/IPO/ModuleInliner.cpp +++ b/llvm/lib/Transforms/IPO/ModuleInliner.cpp @@ -217,8 +217,8 @@ &FAM.getResult(*(CB->getCaller())), &FAM.getResult(Callee)); - InlineResult IR = - InlineFunction(*CB, IFI, &FAM.getResult(*CB->getCaller())); + InlineResult IR = inlineFunctionAndMergeAttributes( + *CB, IFI, &FAM.getResult(*CB->getCaller())); if (!IR.isSuccess()) { Advice->recordUnsuccessfulInlining(IR); continue; @@ -251,9 +251,6 @@ } } - // Merge the attributes based on the inlining. - AttributeFuncs::mergeAttributesForInlining(F, Callee); - // For local functions, check whether this makes the callee trivially // dead. In that case, we can drop the body of the function eagerly // which may reduce the number of callers of other functions to one, diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -1221,13 +1221,9 @@ InlineFunctionInfo IFI(nullptr, GetAC); IFI.UpdateProfile = false; - if (!InlineFunction(CB, IFI).isSuccess()) + if (!inlineFunctionAndMergeAttributes(CB, IFI).isSuccess()) return false; - // Merge the attributes based on the inlining. - AttributeFuncs::mergeAttributesForInlining(*BB->getParent(), - *CalledFunction); - // The call to InlineFunction erases I, so we can't pass it here. emitInlinedIntoBasedOnCost(*ORE, DLoc, BB, *CalledFunction, *BB->getParent(), Cost, true, getAnnotatedRemarkPassName()); diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -2674,3 +2674,17 @@ return InlineResult::success(); } + +llvm::InlineResult llvm::inlineFunctionAndMergeAttributes( + CallBase &CB, InlineFunctionInfo &IFI, AAResults *CalleeAAR, + bool InsertLifetime, Function *ForwardVarArgsTo) { + Function *Callee = CB.getCalledFunction(); + Function *Caller = CB.getCaller(); + InlineResult IR = + InlineFunction(CB, IFI, CalleeAAR, InsertLifetime, ForwardVarArgsTo); + + if (IR.isSuccess()) + AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee); + + return IR; +}