Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 1 | //===- InlineAdvisor.cpp - analysis pass implementation -------------------===// |
| 2 | // |
Mircea Trofin | 296e477 | 2020-06-15 16:46:21 -0700 | [diff] [blame] | 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 |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 9 | // This file implements InlineAdvisorAnalysis and DefaultInlineAdvisor, and |
| 10 | // related types. |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/InlineAdvisor.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/Analysis/InlineCost.h" |
| 17 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 18 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 20 | #include "llvm/Analysis/TargetTransformInfo.h" |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 21 | #include "llvm/IR/DebugInfoMetadata.h" |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
Simon Pilgrim | cdceef4 | 2020-06-23 12:19:09 +0100 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | #include <sstream> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | #define DEBUG_TYPE "inline" |
| 30 | |
| 31 | // This weirdly named statistic tracks the number of times that, when attempting |
| 32 | // to inline a function A into B, we analyze the callers of B in order to see |
| 33 | // if those would be more profitable and blocked inline steps. |
| 34 | STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed"); |
| 35 | |
| 36 | /// Flag to add inline messages as callsite attributes 'inline-remark'. |
| 37 | static cl::opt<bool> |
| 38 | InlineRemarkAttribute("inline-remark-attribute", cl::init(false), |
| 39 | cl::Hidden, |
| 40 | cl::desc("Enable adding inline-remark attribute to" |
| 41 | " callsites processed by inliner but decided" |
| 42 | " to be not inlined")); |
| 43 | |
| 44 | // An integer used to limit the cost of inline deferral. The default negative |
| 45 | // number tells shouldBeDeferred to only take the secondary cost into account. |
| 46 | static cl::opt<int> |
| 47 | InlineDeferralScale("inline-deferral-scale", |
| 48 | cl::desc("Scale to limit the cost of inline deferral"), |
Kazu Hirata | cec20db | 2020-05-25 15:43:28 -0700 | [diff] [blame] | 49 | cl::init(2), cl::Hidden); |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 50 | |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 51 | namespace { |
| 52 | class DefaultInlineAdvice : public InlineAdvice { |
| 53 | public: |
| 54 | DefaultInlineAdvice(DefaultInlineAdvisor *Advisor, CallBase &CB, |
| 55 | Optional<InlineCost> OIC, OptimizationRemarkEmitter &ORE) |
Mircea Trofin | e82eff7 | 2020-06-09 14:33:46 -0700 | [diff] [blame] | 56 | : InlineAdvice(Advisor, CB, ORE, OIC.hasValue()), OriginalCB(&CB), |
| 57 | OIC(OIC) {} |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | void recordUnsuccessfulInliningImpl(const InlineResult &Result) override { |
| 61 | using namespace ore; |
| 62 | llvm::setInlineRemark(*OriginalCB, std::string(Result.getFailureReason()) + |
| 63 | "; " + inlineCostStr(*OIC)); |
| 64 | ORE.emit([&]() { |
| 65 | return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block) |
| 66 | << NV("Callee", Callee) << " will not be inlined into " |
| 67 | << NV("Caller", Caller) << ": " |
| 68 | << NV("Reason", Result.getFailureReason()); |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | void recordInliningWithCalleeDeletedImpl() override { |
| 73 | emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC); |
| 74 | } |
| 75 | |
| 76 | void recordInliningImpl() override { |
| 77 | emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | CallBase *const OriginalCB; |
| 82 | Optional<InlineCost> OIC; |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace |
| 86 | |
Benjamin Kramer | 9a0689e | 2020-07-17 13:49:11 +0200 | [diff] [blame] | 87 | llvm::Optional<llvm::InlineCost> static getDefaultInlineAdvice( |
| 88 | CallBase &CB, FunctionAnalysisManager &FAM, const InlineParams &Params) { |
Mircea Trofin | 8a2e2a6 | 2020-05-14 15:42:26 -0700 | [diff] [blame] | 89 | Function &Caller = *CB.getCaller(); |
| 90 | ProfileSummaryInfo *PSI = |
| 91 | FAM.getResult<ModuleAnalysisManagerFunctionProxy>(Caller) |
| 92 | .getCachedResult<ProfileSummaryAnalysis>( |
| 93 | *CB.getParent()->getParent()->getParent()); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 94 | |
Mircea Trofin | 8a2e2a6 | 2020-05-14 15:42:26 -0700 | [diff] [blame] | 95 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller); |
Mircea Trofin | 08e2386 | 2020-05-14 22:38:41 -0700 | [diff] [blame] | 96 | auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & { |
Mircea Trofin | 8a2e2a6 | 2020-05-14 15:42:26 -0700 | [diff] [blame] | 97 | return FAM.getResult<AssumptionAnalysis>(F); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 98 | }; |
| 99 | auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & { |
| 100 | return FAM.getResult<BlockFrequencyAnalysis>(F); |
| 101 | }; |
| 102 | auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & { |
| 103 | return FAM.getResult<TargetLibraryAnalysis>(F); |
| 104 | }; |
| 105 | |
| 106 | auto GetInlineCost = [&](CallBase &CB) { |
| 107 | Function &Callee = *CB.getCalledFunction(); |
| 108 | auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee); |
| 109 | bool RemarksEnabled = |
| 110 | Callee.getContext().getDiagHandlerPtr()->isMissedOptRemarkEnabled( |
| 111 | DEBUG_TYPE); |
Mircea Trofin | 08e2386 | 2020-05-14 22:38:41 -0700 | [diff] [blame] | 112 | return getInlineCost(CB, Params, CalleeTTI, GetAssumptionCache, GetTLI, |
| 113 | GetBFI, PSI, RemarksEnabled ? &ORE : nullptr); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 114 | }; |
Mircea Trofin | 11046ef | 2020-07-13 12:14:21 -0700 | [diff] [blame] | 115 | return llvm::shouldInline(CB, GetInlineCost, ORE, |
| 116 | Params.EnableDeferral.hasValue() && |
| 117 | Params.EnableDeferral.getValue()); |
| 118 | } |
| 119 | |
| 120 | std::unique_ptr<InlineAdvice> DefaultInlineAdvisor::getAdvice(CallBase &CB) { |
| 121 | auto OIC = getDefaultInlineAdvice(CB, FAM, Params); |
| 122 | return std::make_unique<DefaultInlineAdvice>( |
| 123 | this, CB, OIC, |
| 124 | FAM.getResult<OptimizationRemarkEmitterAnalysis>(*CB.getCaller())); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | InlineAdvice::InlineAdvice(InlineAdvisor *Advisor, CallBase &CB, |
Mircea Trofin | e82eff7 | 2020-06-09 14:33:46 -0700 | [diff] [blame] | 128 | OptimizationRemarkEmitter &ORE, |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 129 | bool IsInliningRecommended) |
| 130 | : Advisor(Advisor), Caller(CB.getCaller()), Callee(CB.getCalledFunction()), |
Mircea Trofin | e82eff7 | 2020-06-09 14:33:46 -0700 | [diff] [blame] | 131 | DLoc(CB.getDebugLoc()), Block(CB.getParent()), ORE(ORE), |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 132 | IsInliningRecommended(IsInliningRecommended) {} |
| 133 | |
| 134 | void InlineAdvisor::markFunctionAsDeleted(Function *F) { |
| 135 | assert((!DeletedFunctions.count(F)) && |
| 136 | "Cannot put cause a function to become dead twice!"); |
| 137 | DeletedFunctions.insert(F); |
| 138 | } |
| 139 | |
| 140 | void InlineAdvisor::freeDeletedFunctions() { |
| 141 | for (auto *F : DeletedFunctions) |
| 142 | delete F; |
| 143 | DeletedFunctions.clear(); |
| 144 | } |
| 145 | |
| 146 | void InlineAdvice::recordInliningWithCalleeDeleted() { |
| 147 | markRecorded(); |
| 148 | Advisor->markFunctionAsDeleted(Callee); |
| 149 | recordInliningWithCalleeDeletedImpl(); |
| 150 | } |
| 151 | |
| 152 | AnalysisKey InlineAdvisorAnalysis::Key; |
| 153 | |
| 154 | bool InlineAdvisorAnalysis::Result::tryCreate(InlineParams Params, |
| 155 | InliningAdvisorMode Mode) { |
Mircea Trofin | 999ea25 | 2020-05-21 08:40:49 -0700 | [diff] [blame] | 156 | auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 157 | switch (Mode) { |
| 158 | case InliningAdvisorMode::Default: |
Mircea Trofin | 999ea25 | 2020-05-21 08:40:49 -0700 | [diff] [blame] | 159 | Advisor.reset(new DefaultInlineAdvisor(FAM, Params)); |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 160 | break; |
Mircea Trofin | 5fe1026 | 2020-11-16 14:01:53 -0800 | [diff] [blame] | 161 | case InliningAdvisorMode::MandatoryOnly: |
| 162 | Advisor.reset(new MandatoryInlineAdvisor(FAM)); |
| 163 | break; |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 164 | case InliningAdvisorMode::Development: |
Mircea Trofin | 70f8d0a | 2020-07-08 18:55:36 -0700 | [diff] [blame] | 165 | #ifdef LLVM_HAVE_TF_API |
| 166 | Advisor = |
| 167 | llvm::getDevelopmentModeAdvisor(M, MAM, [&FAM, Params](CallBase &CB) { |
| 168 | auto OIC = getDefaultInlineAdvice(CB, FAM, Params); |
| 169 | return OIC.hasValue(); |
| 170 | }); |
| 171 | #endif |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 172 | break; |
| 173 | case InliningAdvisorMode::Release: |
Mircea Trofin | bdceefe | 2020-06-09 14:50:50 -0700 | [diff] [blame] | 174 | #ifdef LLVM_HAVE_TF_AOT |
| 175 | Advisor = llvm::getReleaseModeAdvisor(M, MAM); |
| 176 | #endif |
Mircea Trofin | d6695e1 | 2020-04-28 13:25:15 -0700 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | return !!Advisor; |
| 180 | } |
| 181 | |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 182 | /// Return true if inlining of CB can block the caller from being |
| 183 | /// inlined which is proved to be more beneficial. \p IC is the |
| 184 | /// estimated inline cost associated with callsite \p CB. |
| 185 | /// \p TotalSecondaryCost will be set to the estimated cost of inlining the |
| 186 | /// caller if \p CB is suppressed for inlining. |
Kazu Hirata | 0205fab | 2020-05-11 14:04:10 -0700 | [diff] [blame] | 187 | static bool |
| 188 | shouldBeDeferred(Function *Caller, InlineCost IC, int &TotalSecondaryCost, |
| 189 | function_ref<InlineCost(CallBase &CB)> GetInlineCost) { |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 190 | // For now we only handle local or inline functions. |
| 191 | if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage()) |
| 192 | return false; |
| 193 | // If the cost of inlining CB is non-positive, it is not going to prevent the |
| 194 | // caller from being inlined into its callers and hence we don't need to |
| 195 | // defer. |
| 196 | if (IC.getCost() <= 0) |
| 197 | return false; |
| 198 | // Try to detect the case where the current inlining candidate caller (call |
| 199 | // it B) is a static or linkonce-ODR function and is an inlining candidate |
| 200 | // elsewhere, and the current candidate callee (call it C) is large enough |
| 201 | // that inlining it into B would make B too big to inline later. In these |
| 202 | // circumstances it may be best not to inline C into B, but to inline B into |
| 203 | // its callers. |
| 204 | // |
| 205 | // This only applies to static and linkonce-ODR functions because those are |
| 206 | // expected to be available for inlining in the translation units where they |
| 207 | // are used. Thus we will always have the opportunity to make local inlining |
| 208 | // decisions. Importantly the linkonce-ODR linkage covers inline functions |
| 209 | // and templates in C++. |
| 210 | // |
| 211 | // FIXME: All of this logic should be sunk into getInlineCost. It relies on |
| 212 | // the internal implementation of the inline cost metrics rather than |
| 213 | // treating them as truly abstract units etc. |
| 214 | TotalSecondaryCost = 0; |
| 215 | // The candidate cost to be imposed upon the current function. |
| 216 | int CandidateCost = IC.getCost() - 1; |
| 217 | // If the caller has local linkage and can be inlined to all its callers, we |
| 218 | // can apply a huge negative bonus to TotalSecondaryCost. |
| 219 | bool ApplyLastCallBonus = Caller->hasLocalLinkage() && !Caller->hasOneUse(); |
| 220 | // This bool tracks what happens if we DO inline C into B. |
| 221 | bool InliningPreventsSomeOuterInline = false; |
| 222 | unsigned NumCallerUsers = 0; |
| 223 | for (User *U : Caller->users()) { |
| 224 | CallBase *CS2 = dyn_cast<CallBase>(U); |
| 225 | |
| 226 | // If this isn't a call to Caller (it could be some other sort |
| 227 | // of reference) skip it. Such references will prevent the caller |
| 228 | // from being removed. |
| 229 | if (!CS2 || CS2->getCalledFunction() != Caller) { |
| 230 | ApplyLastCallBonus = false; |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | InlineCost IC2 = GetInlineCost(*CS2); |
| 235 | ++NumCallerCallersAnalyzed; |
| 236 | if (!IC2) { |
| 237 | ApplyLastCallBonus = false; |
| 238 | continue; |
| 239 | } |
| 240 | if (IC2.isAlways()) |
| 241 | continue; |
| 242 | |
| 243 | // See if inlining of the original callsite would erase the cost delta of |
| 244 | // this callsite. We subtract off the penalty for the call instruction, |
| 245 | // which we would be deleting. |
| 246 | if (IC2.getCostDelta() <= CandidateCost) { |
| 247 | InliningPreventsSomeOuterInline = true; |
| 248 | TotalSecondaryCost += IC2.getCost(); |
| 249 | NumCallerUsers++; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (!InliningPreventsSomeOuterInline) |
| 254 | return false; |
| 255 | |
| 256 | // If all outer calls to Caller would get inlined, the cost for the last |
| 257 | // one is set very low by getInlineCost, in anticipation that Caller will |
| 258 | // be removed entirely. We did not account for this above unless there |
| 259 | // is only one caller of Caller. |
| 260 | if (ApplyLastCallBonus) |
| 261 | TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus; |
| 262 | |
| 263 | // If InlineDeferralScale is negative, then ignore the cost of primary |
| 264 | // inlining -- IC.getCost() multiplied by the number of callers to Caller. |
| 265 | if (InlineDeferralScale < 0) |
| 266 | return TotalSecondaryCost < IC.getCost(); |
| 267 | |
| 268 | int TotalCost = TotalSecondaryCost + IC.getCost() * NumCallerUsers; |
| 269 | int Allowance = IC.getCost() * InlineDeferralScale; |
| 270 | return TotalCost < Allowance; |
| 271 | } |
| 272 | |
| 273 | namespace llvm { |
| 274 | static std::basic_ostream<char> &operator<<(std::basic_ostream<char> &R, |
| 275 | const ore::NV &Arg) { |
| 276 | return R << Arg.Val; |
| 277 | } |
| 278 | |
| 279 | template <class RemarkT> |
| 280 | RemarkT &operator<<(RemarkT &&R, const InlineCost &IC) { |
| 281 | using namespace ore; |
| 282 | if (IC.isAlways()) { |
| 283 | R << "(cost=always)"; |
| 284 | } else if (IC.isNever()) { |
| 285 | R << "(cost=never)"; |
| 286 | } else { |
| 287 | R << "(cost=" << ore::NV("Cost", IC.getCost()) |
| 288 | << ", threshold=" << ore::NV("Threshold", IC.getThreshold()) << ")"; |
| 289 | } |
| 290 | if (const char *Reason = IC.getReason()) |
| 291 | R << ": " << ore::NV("Reason", Reason); |
| 292 | return R; |
| 293 | } |
| 294 | } // namespace llvm |
| 295 | |
| 296 | std::string llvm::inlineCostStr(const InlineCost &IC) { |
| 297 | std::stringstream Remark; |
| 298 | Remark << IC; |
| 299 | return Remark.str(); |
| 300 | } |
| 301 | |
| 302 | void llvm::setInlineRemark(CallBase &CB, StringRef Message) { |
| 303 | if (!InlineRemarkAttribute) |
| 304 | return; |
| 305 | |
| 306 | Attribute Attr = Attribute::get(CB.getContext(), "inline-remark", Message); |
| 307 | CB.addAttribute(AttributeList::FunctionIndex, Attr); |
| 308 | } |
| 309 | |
| 310 | /// Return the cost only if the inliner should attempt to inline at the given |
| 311 | /// CallSite. If we return the cost, we will emit an optimisation remark later |
| 312 | /// using that cost, so we won't do so from this function. Return None if |
| 313 | /// inlining should not be attempted. |
| 314 | Optional<InlineCost> |
| 315 | llvm::shouldInline(CallBase &CB, |
| 316 | function_ref<InlineCost(CallBase &CB)> GetInlineCost, |
Kazu Hirata | 347a599 | 2020-06-04 00:40:17 -0700 | [diff] [blame] | 317 | OptimizationRemarkEmitter &ORE, bool EnableDeferral) { |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 318 | using namespace ore; |
| 319 | |
| 320 | InlineCost IC = GetInlineCost(CB); |
| 321 | Instruction *Call = &CB; |
| 322 | Function *Callee = CB.getCalledFunction(); |
| 323 | Function *Caller = CB.getCaller(); |
| 324 | |
| 325 | if (IC.isAlways()) { |
| 326 | LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC) |
| 327 | << ", Call: " << CB << "\n"); |
| 328 | return IC; |
| 329 | } |
| 330 | |
| 331 | if (!IC) { |
| 332 | LLVM_DEBUG(dbgs() << " NOT Inlining " << inlineCostStr(IC) |
| 333 | << ", Call: " << CB << "\n"); |
| 334 | if (IC.isNever()) { |
| 335 | ORE.emit([&]() { |
| 336 | return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call) |
| 337 | << NV("Callee", Callee) << " not inlined into " |
| 338 | << NV("Caller", Caller) << " because it should never be inlined " |
| 339 | << IC; |
| 340 | }); |
| 341 | } else { |
| 342 | ORE.emit([&]() { |
| 343 | return OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call) |
| 344 | << NV("Callee", Callee) << " not inlined into " |
| 345 | << NV("Caller", Caller) << " because too costly to inline " |
| 346 | << IC; |
| 347 | }); |
| 348 | } |
| 349 | setInlineRemark(CB, inlineCostStr(IC)); |
| 350 | return None; |
| 351 | } |
| 352 | |
| 353 | int TotalSecondaryCost = 0; |
Kazu Hirata | 347a599 | 2020-06-04 00:40:17 -0700 | [diff] [blame] | 354 | if (EnableDeferral && |
| 355 | shouldBeDeferred(Caller, IC, TotalSecondaryCost, GetInlineCost)) { |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 356 | LLVM_DEBUG(dbgs() << " NOT Inlining: " << CB |
| 357 | << " Cost = " << IC.getCost() |
| 358 | << ", outer Cost = " << TotalSecondaryCost << '\n'); |
| 359 | ORE.emit([&]() { |
| 360 | return OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts", |
| 361 | Call) |
| 362 | << "Not inlining. Cost of inlining " << NV("Callee", Callee) |
| 363 | << " increases the cost of inlining " << NV("Caller", Caller) |
| 364 | << " in other contexts"; |
| 365 | }); |
| 366 | setInlineRemark(CB, "deferred"); |
| 367 | // IC does not bool() to false, so get an InlineCost that will. |
| 368 | // This will not be inspected to make an error message. |
| 369 | return None; |
| 370 | } |
| 371 | |
| 372 | LLVM_DEBUG(dbgs() << " Inlining " << inlineCostStr(IC) << ", Call: " << CB |
| 373 | << '\n'); |
| 374 | return IC; |
| 375 | } |
| 376 | |
Wenlei He | 577e58b | 2020-08-15 14:52:10 -0700 | [diff] [blame] | 377 | std::string llvm::getCallSiteLocation(DebugLoc DLoc) { |
| 378 | std::ostringstream CallSiteLoc; |
| 379 | bool First = true; |
| 380 | for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) { |
| 381 | if (!First) |
| 382 | CallSiteLoc << " @ "; |
| 383 | // Note that negative line offset is actually possible, but we use |
| 384 | // unsigned int to match line offset representation in remarks so |
| 385 | // it's directly consumable by relay advisor. |
| 386 | uint32_t Offset = |
| 387 | DIL->getLine() - DIL->getScope()->getSubprogram()->getLine(); |
| 388 | uint32_t Discriminator = DIL->getBaseDiscriminator(); |
| 389 | StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName(); |
| 390 | if (Name.empty()) |
| 391 | Name = DIL->getScope()->getSubprogram()->getName(); |
| 392 | CallSiteLoc << Name.str() << ":" << llvm::utostr(Offset); |
| 393 | if (Discriminator) { |
| 394 | CallSiteLoc << "." << llvm::utostr(Discriminator); |
| 395 | } |
| 396 | First = false; |
| 397 | } |
| 398 | |
| 399 | return CallSiteLoc.str(); |
| 400 | } |
| 401 | |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 402 | void llvm::addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc) { |
Wenlei He | 577e58b | 2020-08-15 14:52:10 -0700 | [diff] [blame] | 403 | if (!DLoc.get()) { |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 404 | return; |
Wenlei He | 577e58b | 2020-08-15 14:52:10 -0700 | [diff] [blame] | 405 | } |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 406 | |
| 407 | bool First = true; |
| 408 | Remark << " at callsite "; |
| 409 | for (DILocation *DIL = DLoc.get(); DIL; DIL = DIL->getInlinedAt()) { |
| 410 | if (!First) |
| 411 | Remark << " @ "; |
| 412 | unsigned int Offset = DIL->getLine(); |
| 413 | Offset -= DIL->getScope()->getSubprogram()->getLine(); |
| 414 | unsigned int Discriminator = DIL->getBaseDiscriminator(); |
| 415 | StringRef Name = DIL->getScope()->getSubprogram()->getLinkageName(); |
| 416 | if (Name.empty()) |
| 417 | Name = DIL->getScope()->getSubprogram()->getName(); |
| 418 | Remark << Name << ":" << ore::NV("Line", Offset); |
| 419 | if (Discriminator) |
| 420 | Remark << "." << ore::NV("Disc", Discriminator); |
| 421 | First = false; |
| 422 | } |
| 423 | } |
| 424 | |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 425 | void llvm::emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc, |
| 426 | const BasicBlock *Block, const Function &Callee, |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 427 | const Function &Caller, const InlineCost &IC, |
| 428 | bool ForProfileContext, const char *PassName) { |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 429 | ORE.emit([&]() { |
| 430 | bool AlwaysInline = IC.isAlways(); |
| 431 | StringRef RemarkName = AlwaysInline ? "AlwaysInline" : "Inlined"; |
Wenlei He | 7c8a693 | 2020-06-19 10:25:31 -0700 | [diff] [blame] | 432 | OptimizationRemark Remark(PassName ? PassName : DEBUG_TYPE, RemarkName, |
| 433 | DLoc, Block); |
| 434 | Remark << ore::NV("Callee", &Callee) << " inlined into "; |
| 435 | Remark << ore::NV("Caller", &Caller); |
| 436 | if (ForProfileContext) |
| 437 | Remark << " to match profiling context"; |
| 438 | Remark << " with " << IC; |
| 439 | addLocationToRemarks(Remark, DLoc); |
| 440 | return Remark; |
Mircea Trofin | 48fa355 | 2020-05-07 20:35:08 -0700 | [diff] [blame] | 441 | }); |
| 442 | } |
Mircea Trofin | 5fe1026 | 2020-11-16 14:01:53 -0800 | [diff] [blame] | 443 | |
| 444 | std::unique_ptr<InlineAdvice> MandatoryInlineAdvisor::getAdvice(CallBase &CB) { |
| 445 | auto &Caller = *CB.getCaller(); |
| 446 | auto &Callee = *CB.getCalledFunction(); |
| 447 | auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(Caller); |
| 448 | |
| 449 | bool Advice = MandatoryInliningKind::Always == |
| 450 | MandatoryInlineAdvisor::getMandatoryKind(CB, FAM, ORE) && |
| 451 | &Caller != &Callee; |
| 452 | return std::make_unique<InlineAdvice>(this, CB, ORE, Advice); |
| 453 | } |
| 454 | |
| 455 | MandatoryInlineAdvisor::MandatoryInliningKind |
| 456 | MandatoryInlineAdvisor::getMandatoryKind(CallBase &CB, |
| 457 | FunctionAnalysisManager &FAM, |
| 458 | OptimizationRemarkEmitter &ORE) { |
| 459 | auto &Callee = *CB.getCalledFunction(); |
| 460 | |
| 461 | auto GetTLI = [&](Function &F) -> const TargetLibraryInfo & { |
| 462 | return FAM.getResult<TargetLibraryAnalysis>(F); |
| 463 | }; |
| 464 | |
| 465 | auto &TIR = FAM.getResult<TargetIRAnalysis>(Callee); |
| 466 | |
| 467 | auto TrivialDecision = |
| 468 | llvm::getAttributeBasedInliningDecision(CB, &Callee, TIR, GetTLI); |
| 469 | |
| 470 | if (TrivialDecision.hasValue()) { |
| 471 | if (TrivialDecision->isSuccess()) |
| 472 | return MandatoryInliningKind::Always; |
| 473 | else |
| 474 | return MandatoryInliningKind::Never; |
| 475 | } |
| 476 | return MandatoryInliningKind::NotMandatory; |
| 477 | } |