llvm.org GIT mirror llvm / b87a1e6
Replace the use of MaxFunctionCount module flag Adds an interface to get ProfileSummary for a module and makes InlineCost use ProfileSummary to get max function count. Differential Revision: http://reviews.llvm.org/D18622 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266477 91177308-0d34-0410-b5e6-96231b3b80d8 Easwaran Raman 4 years ago
7 changed file(s) with 186 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
2121 #include
2222
2323 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/Mutex.h"
2426
2527 namespace llvm {
2628 class Function;
3537 class Metadata;
3638 class MDTuple;
3739 class MDNode;
40 class Module;
3841
3942 inline const char *getHotSectionPrefix() { return ".hot"; }
4043 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
6770 // appears in the profile. The map is kept sorted in the descending order of
6871 // counts.
6972 std::map> CountFrequencies;
73 // Compute profile summary for a module.
74 static ProfileSummary *computeProfileSummary(Module *M);
75 // Cache of last seen module and its profile summary.
76 static ManagedStatic>>
77 CachedSummary;
78 // Mutex to access summary cache
79 static ManagedStatic> CacheMutex;
80
7081 protected:
7182 SummaryEntryVector DetailedSummary;
7283 std::vector DetailedSummaryCutoffs;
8596 : PSK(K), DetailedSummary(DetailedSummary), TotalCount(TotalCount),
8697 MaxCount(MaxCount), MaxFunctionCount(MaxFunctionCount),
8798 NumCounts(NumCounts), NumFunctions(NumFunctions) {}
88 ~ProfileSummary() = default;
8999 inline void addCount(uint64_t Count);
90100 /// \brief Return metadata specific to the profile format.
91101 /// Derived classes implement this method to return a vector of Metadata.
92102 virtual std::vector getFormatSpecificMD(LLVMContext &Context) = 0;
93103 /// \brief Return detailed summary as metadata.
94104 Metadata *getDetailedSummaryMD(LLVMContext &Context);
95
96105 public:
97106 static const int Scale = 1000000;
98107 Kind getKind() const { return PSK; }
111120 static ProfileSummary *getFromMD(Metadata *MD);
112121 uint32_t getNumFunctions() { return NumFunctions; }
113122 uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
123 /// \brief Get profile summary associated with module \p M
124 static inline ProfileSummary *getProfileSummary(Module *M);
125 virtual ~ProfileSummary() = default;
126 virtual bool operator==(ProfileSummary &Other);
114127 };
115128
116129 class InstrProfSummary final : public ProfileSummary {
140153 uint64_t getTotalCount() { return TotalCount; }
141154 uint64_t getMaxBlockCount() { return MaxCount; }
142155 uint64_t getMaxInternalBlockCount() { return MaxInternalBlockCount; }
156 bool operator==(ProfileSummary &Other) override;
143157 };
144158
145159 class SampleProfileSummary final : public ProfileSummary {
180194 return DetailedSummary;
181195 }
182196
197 ProfileSummary *ProfileSummary::getProfileSummary(Module *M) {
198 if (!M)
199 return nullptr;
200 sys::SmartScopedLock Lock(*CacheMutex);
201 // Computing profile summary for a module involves parsing a fairly large
202 // metadata and could be expensive. We use a simple cache of the last seen
203 // module and its profile summary.
204 if (CachedSummary->first != M) {
205 auto *Summary = computeProfileSummary(M);
206 // Do not cache if the summary is empty. This is because a later pass
207 // (sample profile loader, for example) could attach the summary metadata on
208 // the module.
209 if (!Summary)
210 return nullptr;
211 CachedSummary->first = M;
212 CachedSummary->second.reset(Summary);
213 }
214 return CachedSummary->second.get();
215 }
183216 } // end namespace llvm
184217 #endif
2929 #include "llvm/IR/InstVisitor.h"
3030 #include "llvm/IR/IntrinsicInst.h"
3131 #include "llvm/IR/Operator.h"
32 #include "llvm/ProfileData/ProfileCommon.h"
3233 #include "llvm/Support/Debug.h"
3334 #include "llvm/Support/raw_ostream.h"
3435
625626 // a well-tuned heuristic based on *callsite* hotness and not callee hotness.
626627 uint64_t FunctionCount = 0, MaxFunctionCount = 0;
627628 bool HasPGOCounts = false;
628 if (Callee.getEntryCount() && Callee.getParent()->getMaximumFunctionCount()) {
629 ProfileSummary *PS = ProfileSummary::getProfileSummary(Callee.getParent());
630 if (Callee.getEntryCount() && PS) {
629631 HasPGOCounts = true;
630632 FunctionCount = Callee.getEntryCount().getValue();
631 MaxFunctionCount = Callee.getParent()->getMaximumFunctionCount().getValue();
633 MaxFunctionCount = PS->getMaxFunctionCount();
632634 }
633635
634636 // Listen to the inlinehint attribute or profile based hotness information
1414 #include "llvm/IR/Constants.h"
1515 #include "llvm/IR/Function.h"
1616 #include "llvm/IR/Metadata.h"
17 #include "llvm/IR/Module.h"
1718 #include "llvm/IR/Type.h"
1819 #include "llvm/ProfileData/InstrProf.h"
1920 #include "llvm/ProfileData/ProfileCommon.h"
3031 200000, 300000, 400000, 500000, 600000, 500000, 600000, 700000, 800000,
3132 900000, 950000, 990000, 999000, 999900, 999990, 999999});
3233 const char *ProfileSummary::KindStr[2] = {"InstrProf", "SampleProfile"};
34
35 ManagedStatic>>
36 ProfileSummary::CachedSummary;
37 ManagedStatic> ProfileSummary::CacheMutex;
3338
3439 void InstrProfSummary::addRecord(const InstrProfRecord &R) {
3540 addEntryCount(R.Counts[0]);
8186 }
8287 }
8388
89 bool ProfileSummary::operator==(ProfileSummary &Other) {
90 if (getKind() != Other.getKind())
91 return false;
92 if (TotalCount != Other.TotalCount)
93 return false;
94 if (MaxCount != Other.MaxCount)
95 return false;
96 if (MaxFunctionCount != Other.MaxFunctionCount)
97 return false;
98 if (NumFunctions != Other.NumFunctions)
99 return false;
100 if (NumCounts != Other.NumCounts)
101 return false;
102 std::vector DS1 = getDetailedSummary();
103 std::vector DS2 = Other.getDetailedSummary();
104 auto CompareSummaryEntry = [](ProfileSummaryEntry &E1,
105 ProfileSummaryEntry &E2) {
106 return E1.Cutoff == E2.Cutoff && E1.MinCount == E2.MinCount &&
107 E1.NumCounts == E2.NumCounts;
108 };
109 if (!std::equal(DS1.begin(), DS1.end(), DS2.begin(), CompareSummaryEntry))
110 return false;
111 return true;
112 }
113
114 bool InstrProfSummary::operator==(ProfileSummary &Other) {
115 InstrProfSummary *OtherIPS = dyn_cast(&Other);
116 if (!OtherIPS)
117 return false;
118 return MaxInternalBlockCount == OtherIPS->MaxInternalBlockCount &&
119 ProfileSummary::operator==(Other);
120 }
121
84122 // Returns true if the function is a hot function.
85123 bool ProfileSummary::isFunctionHot(const Function *F) {
86124 // FIXME: update when summary data is stored in module's metadata.
361399 else
362400 return nullptr;
363401 }
402
403 ProfileSummary *ProfileSummary::computeProfileSummary(Module *M) {
404 if (Metadata *MD = M->getProfileSummary())
405 return getFromMD(MD);
406 return nullptr;
407 }
44 ; A callee with identical body does gets inlined because cost fits within the
55 ; inline-threshold
66
7 define i32 @callee1(i32 %x) !prof !1 {
7 define i32 @callee1(i32 %x) !prof !21 {
88 %x1 = add i32 %x, 1
99 %x2 = add i32 %x1, 1
1010 %x3 = add i32 %x2, 1
1212 ret i32 %x3
1313 }
1414
15 define i32 @callee2(i32 %x) !prof !2 {
15 define i32 @callee2(i32 %x) !prof !22 {
1616 ; CHECK-LABEL: @callee2(
1717 %x1 = add i32 %x, 1
1818 %x2 = add i32 %x1, 1
2121 ret i32 %x3
2222 }
2323
24 define i32 @caller2(i32 %y1) !prof !2 {
24 define i32 @caller2(i32 %y1) !prof !22 {
2525 ; CHECK-LABEL: @caller2(
2626 ; CHECK: call i32 @callee2
2727 ; CHECK-NOT: call i32 @callee1
3131 ret i32 %y3
3232 }
3333
34 !llvm.module.flags = !{!0}
35 !0 = !{i32 1, !"MaxFunctionCount", i32 1000}
36 !1 = !{!"function_entry_count", i64 100}
37 !2 = !{!"function_entry_count", i64 1}
34 !llvm.module.flags = !{!1}
35 !21 = !{!"function_entry_count", i64 100}
36 !22 = !{!"function_entry_count", i64 1}
3837
38 !1 = !{i32 1, !"ProfileSummary", !2}
39 !2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
40 !3 = !{!"ProfileFormat", !"InstrProf"}
41 !4 = !{!"TotalCount", i64 10000}
42 !5 = !{!"MaxBlockCount", i64 1000}
43 !6 = !{!"MaxInternalBlockCount", i64 1}
44 !7 = !{!"MaxFunctionCount", i64 1000}
45 !8 = !{!"NumBlocks", i64 3}
46 !9 = !{!"NumFunctions", i64 3}
47 !10 = !{!"DetailedSummary", !11}
48 !11 = !{!12}
49 !12 = !{i32 10000, i64 0, i32 0}
44 ; A cold callee with identical body does not get inlined because cost exceeds the
55 ; inline-threshold
66
7 define i32 @callee1(i32 %x) !prof !1 {
7 define i32 @callee1(i32 %x) !prof !20 {
88 %x1 = add i32 %x, 1
99 %x2 = add i32 %x1, 1
1010 %x3 = add i32 %x2, 1
1212 ret i32 %x3
1313 }
1414
15 define i32 @callee2(i32 %x) !prof !2 {
15 define i32 @callee2(i32 %x) !prof !21 {
1616 ; CHECK-LABEL: @callee2(
1717 %x1 = add i32 %x, 1
1818 %x2 = add i32 %x1, 1
2121 ret i32 %x3
2222 }
2323
24 define i32 @caller2(i32 %y1) !prof !2 {
24 define i32 @caller2(i32 %y1) !prof !21 {
2525 ; CHECK-LABEL: @caller2(
2626 ; CHECK: call i32 @callee2
2727 ; CHECK-NOT: call i32 @callee1
3131 ret i32 %y3
3232 }
3333
34 !llvm.module.flags = !{!0}
35 !0 = !{i32 1, !"MaxFunctionCount", i32 10}
36 !1 = !{!"function_entry_count", i64 10}
37 !2 = !{!"function_entry_count", i64 1}
34 !llvm.module.flags = !{!1}
35 !20 = !{!"function_entry_count", i64 10}
36 !21 = !{!"function_entry_count", i64 1}
3837
38 !1 = !{i32 1, !"ProfileSummary", !2}
39 !2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
40 !3 = !{!"ProfileFormat", !"InstrProf"}
41 !4 = !{!"TotalCount", i64 10000}
42 !5 = !{!"MaxBlockCount", i64 10}
43 !6 = !{!"MaxInternalBlockCount", i64 1}
44 !7 = !{!"MaxFunctionCount", i64 10}
45 !8 = !{!"NumBlocks", i64 3}
46 !9 = !{!"NumFunctions", i64 3}
47 !10 = !{!"DetailedSummary", !11}
48 !11 = !{!12}
49 !12 = !{i32 10000, i64 0, i32 0}
66 add_llvm_unittest(ProfileDataTests
77 CoverageMappingTest.cpp
88 InstrProfTest.cpp
9 ProfileSummaryTest.cpp
910 SampleProfTest.cpp
1011 )
0 //===- unittest/ProfileData/ProfileSummaryTest.cpp --------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/IR/Module.h"
10 #include "llvm/ProfileData/InstrProf.h"
11 #include "llvm/ProfileData/ProfileCommon.h"
12 #include "llvm/ProfileData/SampleProf.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16 using namespace sampleprof;
17
18 struct ProfileSummaryTest : ::testing::Test {
19 InstrProfSummary IPS;
20 SampleProfileSummary SPS;
21
22 ProfileSummaryTest()
23 : IPS({100000, 900000, 999999}), SPS({100000, 900000, 999999}) {}
24 void SetUp() {
25 InstrProfRecord Record1("func1", 0x1234, {97531, 5, 99999});
26 InstrProfRecord Record2("func2", 0x1234, {57341, 10000, 10, 1});
27 IPS.addRecord(Record1);
28 IPS.addRecord(Record2);
29
30 IPS.computeDetailedSummary();
31
32 FunctionSamples FooSamples;
33 FooSamples.addTotalSamples(7711);
34 FooSamples.addHeadSamples(610);
35 FooSamples.addBodySamples(1, 0, 610);
36 FooSamples.addBodySamples(2, 0, 600);
37 FooSamples.addBodySamples(4, 0, 60000);
38 FooSamples.addBodySamples(8, 0, 60351);
39 FooSamples.addBodySamples(10, 0, 605);
40
41 FunctionSamples BarSamples;
42 BarSamples.addTotalSamples(20301);
43 BarSamples.addHeadSamples(1437);
44 BarSamples.addBodySamples(1, 0, 1437);
45
46 SPS.addRecord(FooSamples);
47 SPS.addRecord(BarSamples);
48
49 SPS.computeDetailedSummary();
50 }
51
52 };
53
54 TEST_F(ProfileSummaryTest, summary_from_module) {
55 LLVMContext Context;
56 Module M1("M1", Context);
57 EXPECT_FALSE(ProfileSummary::getProfileSummary(&M1));
58 M1.setProfileSummary(IPS.getMD(Context));
59 EXPECT_TRUE(IPS == *ProfileSummary::getProfileSummary(&M1));
60
61 Module M2("M2", Context);
62 EXPECT_FALSE(ProfileSummary::getProfileSummary(&M2));
63 M2.setProfileSummary(SPS.getMD(Context));
64 EXPECT_TRUE(SPS == *ProfileSummary::getProfileSummary(&M2));
65 }