[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / llvm / tools / llvm-profgen / CSPreInliner.h
blob4d848aafdab914da33ea9155e446c8ef257010bf
1 //===-- CSPreInliner.h - Profile guided preinliner ---------------- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_PROFGEN_PGOINLINEADVISOR_H
10 #define LLVM_TOOLS_LLVM_PROFGEN_PGOINLINEADVISOR_H
12 #include "ProfiledBinary.h"
13 #include "llvm/ADT/PriorityQueue.h"
14 #include "llvm/ProfileData/ProfileCommon.h"
15 #include "llvm/ProfileData/SampleProf.h"
16 #include "llvm/Transforms/IPO/ProfiledCallGraph.h"
17 #include "llvm/Transforms/IPO/SampleContextTracker.h"
19 using namespace llvm;
20 using namespace sampleprof;
22 namespace llvm {
23 namespace sampleprof {
25 // Inline candidate seen from profile
26 struct ProfiledInlineCandidate {
27 ProfiledInlineCandidate(const FunctionSamples *Samples, uint64_t Count,
28 uint32_t Size)
29 : CalleeSamples(Samples), CallsiteCount(Count), SizeCost(Size) {}
30 // Context-sensitive function profile for inline candidate
31 const FunctionSamples *CalleeSamples;
32 // Call site count for an inline candidate
33 // TODO: make sure entry count for context profile and call site
34 // target count for corresponding call are consistent.
35 uint64_t CallsiteCount;
36 // Size proxy for function under particular call context.
37 uint64_t SizeCost;
40 // Inline candidate comparer using call site weight
41 struct ProfiledCandidateComparer {
42 bool operator()(const ProfiledInlineCandidate &LHS,
43 const ProfiledInlineCandidate &RHS) {
44 // Always prioritize inlining zero-sized functions as they do not affect the
45 // size budget. This could happen when all of the callee's code is gone and
46 // only pseudo probes are left.
47 if ((LHS.SizeCost == 0 || RHS.SizeCost == 0) &&
48 (LHS.SizeCost != RHS.SizeCost))
49 return RHS.SizeCost == 0;
51 if (LHS.CallsiteCount != RHS.CallsiteCount)
52 return LHS.CallsiteCount < RHS.CallsiteCount;
54 if (LHS.SizeCost != RHS.SizeCost)
55 return LHS.SizeCost > RHS.SizeCost;
57 // Tie breaker using GUID so we have stable/deterministic inlining order
58 assert(LHS.CalleeSamples && RHS.CalleeSamples &&
59 "Expect non-null FunctionSamples");
60 return LHS.CalleeSamples->getGUID(LHS.CalleeSamples->getName()) <
61 RHS.CalleeSamples->getGUID(RHS.CalleeSamples->getName());
65 using ProfiledCandidateQueue =
66 PriorityQueue<ProfiledInlineCandidate, std::vector<ProfiledInlineCandidate>,
67 ProfiledCandidateComparer>;
69 // Pre-compilation inliner based on context-sensitive profile.
70 // The PreInliner estimates inline decision using hotness from profile
71 // and cost estimation from machine code size. It helps merges context
72 // profile globally and achieves better post-inine profile quality, which
73 // otherwise won't be possible for ThinLTO. It also reduce context profile
74 // size by only keep context that is estimated to be inlined.
75 class CSPreInliner {
76 public:
77 CSPreInliner(SampleContextTracker &Tracker, ProfiledBinary &Binary,
78 ProfileSummary *Summary);
79 void run();
81 private:
82 bool getInlineCandidates(ProfiledCandidateQueue &CQueue,
83 const FunctionSamples *FCallerContextSamples);
84 std::vector<StringRef> buildTopDownOrder();
85 void processFunction(StringRef Name);
86 bool shouldInline(ProfiledInlineCandidate &Candidate);
87 uint32_t getFuncSize(const ContextTrieNode *ContextNode);
88 bool UseContextCost;
89 SampleContextTracker &ContextTracker;
90 ProfiledBinary &Binary;
91 ProfileSummary *Summary;
94 } // end namespace sampleprof
95 } // end namespace llvm
97 #endif