llvm.org GIT mirror llvm / 79f2742
Reapply r266477 and r266488 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269131 91177308-0d34-0410-b5e6-96231b3b80d8 Easwaran Raman 4 years ago
8 changed file(s) with 187 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2020 #include
2121
2222 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Mutex.h"
2325
2426 namespace llvm {
2527 class Function;
3436 class Metadata;
3537 class MDTuple;
3638 class MDNode;
39 class Module;
3740
3841 inline const char *getHotSectionPrefix() { return ".hot"; }
3942 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
6669 // appears in the profile. The map is kept sorted in the descending order of
6770 // counts.
6871 std::map> CountFrequencies;
72 // Compute profile summary for a module.
73 static ProfileSummary *computeProfileSummary(Module *M);
74 // Cache of last seen module and its profile summary.
75 static ManagedStatic>>
76 CachedSummary;
77 // Mutex to access summary cache
78 static ManagedStatic> CacheMutex;
79
6980 protected:
7081 SummaryEntryVector DetailedSummary;
7182 std::vector DetailedSummaryCutoffs;
8495 : PSK(K), DetailedSummary(DetailedSummary), TotalCount(TotalCount),
8596 MaxCount(MaxCount), MaxFunctionCount(MaxFunctionCount),
8697 NumCounts(NumCounts), NumFunctions(NumFunctions) {}
87 ~ProfileSummary() = default;
8898 inline void addCount(uint64_t Count);
8999 /// \brief Return metadata specific to the profile format.
90100 /// Derived classes implement this method to return a vector of Metadata.
91101 virtual std::vector getFormatSpecificMD(LLVMContext &Context) = 0;
92102 /// \brief Return detailed summary as metadata.
93103 Metadata *getDetailedSummaryMD(LLVMContext &Context);
94
95104 public:
96105 static const int Scale = 1000000;
97106 Kind getKind() const { return PSK; }
110119 static ProfileSummary *getFromMD(Metadata *MD);
111120 uint32_t getNumFunctions() { return NumFunctions; }
112121 uint64_t getMaxFunctionCount() { return MaxFunctionCount; }
122 /// \brief Get profile summary associated with module \p M
123 static inline ProfileSummary *getProfileSummary(Module *M);
124 virtual ~ProfileSummary() = default;
125 virtual bool operator==(ProfileSummary &Other);
113126 };
114127
115128 class InstrProfSummary final : public ProfileSummary {
139152 uint64_t getTotalCount() { return TotalCount; }
140153 uint64_t getMaxBlockCount() { return MaxCount; }
141154 uint64_t getMaxInternalBlockCount() { return MaxInternalBlockCount; }
155 bool operator==(ProfileSummary &Other) override;
142156 };
143157
144158 class SampleProfileSummary final : public ProfileSummary {
179193 return DetailedSummary;
180194 }
181195
196 ProfileSummary *ProfileSummary::getProfileSummary(Module *M) {
197 if (!M)
198 return nullptr;
199 sys::SmartScopedLock Lock(*CacheMutex);
200 // Computing profile summary for a module involves parsing a fairly large
201 // metadata and could be expensive. We use a simple cache of the last seen
202 // module and its profile summary.
203 if (CachedSummary->first != M) {
204 auto *Summary = computeProfileSummary(M);
205 // Do not cache if the summary is empty. This is because a later pass
206 // (sample profile loader, for example) could attach the summary metadata on
207 // the module.
208 if (!Summary)
209 return nullptr;
210 CachedSummary->first = M;
211 CachedSummary->second.reset(Summary);
212 }
213 return CachedSummary->second.get();
214 }
182215 } // end namespace llvm
183216 #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
629630 // a well-tuned heuristic based on *callsite* hotness and not callee hotness.
630631 uint64_t FunctionCount = 0, MaxFunctionCount = 0;
631632 bool HasPGOCounts = false;
632 if (Callee.getEntryCount() && Callee.getParent()->getMaximumFunctionCount()) {
633 ProfileSummary *PS = ProfileSummary::getProfileSummary(Callee.getParent());
634 if (Callee.getEntryCount() && PS) {
633635 HasPGOCounts = true;
634636 FunctionCount = Callee.getEntryCount().getValue();
635 MaxFunctionCount = Callee.getParent()->getMaximumFunctionCount().getValue();
637 MaxFunctionCount = PS->getMaxFunctionCount();
636638 }
637639
638640 // Listen to the inlinehint attribute or profile based hotness information
1818 type = Library
1919 name = Analysis
2020 parent = Libraries
21 required_libraries = Core Support
21 required_libraries = Core ProfileData Support
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 // The first counter is not necessarily an entry count for IR
8590 }
8691 }
8792
93 bool ProfileSummary::operator==(ProfileSummary &Other) {
94 if (getKind() != Other.getKind())
95 return false;
96 if (TotalCount != Other.TotalCount)
97 return false;
98 if (MaxCount != Other.MaxCount)
99 return false;
100 if (MaxFunctionCount != Other.MaxFunctionCount)
101 return false;
102 if (NumFunctions != Other.NumFunctions)
103 return false;
104 if (NumCounts != Other.NumCounts)
105 return false;
106 std::vector DS1 = getDetailedSummary();
107 std::vector DS2 = Other.getDetailedSummary();
108 auto CompareSummaryEntry = [](ProfileSummaryEntry &E1,
109 ProfileSummaryEntry &E2) {
110 return E1.Cutoff == E2.Cutoff && E1.MinCount == E2.MinCount &&
111 E1.NumCounts == E2.NumCounts;
112 };
113 if (!std::equal(DS1.begin(), DS1.end(), DS2.begin(), CompareSummaryEntry))
114 return false;
115 return true;
116 }
117
118 bool InstrProfSummary::operator==(ProfileSummary &Other) {
119 InstrProfSummary *OtherIPS = dyn_cast(&Other);
120 if (!OtherIPS)
121 return false;
122 return MaxInternalBlockCount == OtherIPS->MaxInternalBlockCount &&
123 ProfileSummary::operator==(Other);
124 }
125
88126 // Returns true if the function is a hot function.
89127 bool ProfileSummary::isFunctionHot(const Function *F) {
90128 // FIXME: update when summary data is stored in module's metadata.
365403 else
366404 return nullptr;
367405 }
406
407 ProfileSummary *ProfileSummary::computeProfileSummary(Module *M) {
408 if (Metadata *MD = M->getProfileSummary())
409 return getFromMD(MD);
410 return nullptr;
411 }
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}
77 add_llvm_unittest(ProfileDataTests
88 CoverageMappingTest.cpp
99 InstrProfTest.cpp
10 ProfileSummaryTest.cpp
1011 SampleProfTest.cpp
1112 )
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 }