[llvm-objdump] - Remove one overload of reportError. NFCI.
[llvm-complete.git] / unittests / ProfileData / SampleProfTest.cpp
blobebf66b22c5babcb17ce68068dbdaadf4a5be437d
1 //===- unittest/ProfileData/SampleProfTest.cpp ------------------*- 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 #include "llvm/ProfileData/SampleProf.h"
10 #include "llvm/ADT/StringMap.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Metadata.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/ProfileData/SampleProfReader.h"
16 #include "llvm/ProfileData/SampleProfWriter.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "gtest/gtest.h"
22 #include <string>
23 #include <vector>
25 using namespace llvm;
26 using namespace sampleprof;
28 static ::testing::AssertionResult NoError(std::error_code EC) {
29 if (!EC)
30 return ::testing::AssertionSuccess();
31 return ::testing::AssertionFailure() << "error " << EC.value() << ": "
32 << EC.message();
35 namespace {
37 struct SampleProfTest : ::testing::Test {
38 LLVMContext Context;
39 std::unique_ptr<SampleProfileWriter> Writer;
40 std::unique_ptr<SampleProfileReader> Reader;
42 SampleProfTest() : Writer(), Reader() {}
44 void createWriter(SampleProfileFormat Format, StringRef Profile) {
45 std::error_code EC;
46 std::unique_ptr<raw_ostream> OS(
47 new raw_fd_ostream(Profile, EC, sys::fs::OF_None));
48 auto WriterOrErr = SampleProfileWriter::create(OS, Format);
49 ASSERT_TRUE(NoError(WriterOrErr.getError()));
50 Writer = std::move(WriterOrErr.get());
53 void readProfile(const Module &M, StringRef Profile) {
54 auto ReaderOrErr = SampleProfileReader::create(Profile, Context);
55 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
56 Reader = std::move(ReaderOrErr.get());
57 Reader->collectFuncsToUse(M);
60 void testRoundTrip(SampleProfileFormat Format, bool Remap) {
61 SmallVector<char, 128> ProfilePath;
62 ASSERT_TRUE(NoError(llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath)));
63 StringRef Profile(ProfilePath.data(), ProfilePath.size());
64 createWriter(Format, Profile);
66 StringRef FooName("_Z3fooi");
67 FunctionSamples FooSamples;
68 FooSamples.setName(FooName);
69 FooSamples.addTotalSamples(7711);
70 FooSamples.addHeadSamples(610);
71 FooSamples.addBodySamples(1, 0, 610);
72 FooSamples.addBodySamples(2, 0, 600);
73 FooSamples.addBodySamples(4, 0, 60000);
74 FooSamples.addBodySamples(8, 0, 60351);
75 FooSamples.addBodySamples(10, 0, 605);
77 StringRef BarName("_Z3bari");
78 FunctionSamples BarSamples;
79 BarSamples.setName(BarName);
80 BarSamples.addTotalSamples(20301);
81 BarSamples.addHeadSamples(1437);
82 BarSamples.addBodySamples(1, 0, 1437);
83 // Test how reader/writer handles unmangled names.
84 StringRef MconstructName("_M_construct<char *>");
85 StringRef StringviewName("string_view<std::allocator<char> >");
86 BarSamples.addCalledTargetSamples(1, 0, MconstructName, 1000);
87 BarSamples.addCalledTargetSamples(1, 0, StringviewName, 437);
89 Module M("my_module", Context);
90 FunctionType *fn_type =
91 FunctionType::get(Type::getVoidTy(Context), {}, false);
92 M.getOrInsertFunction(FooName, fn_type);
93 M.getOrInsertFunction(BarName, fn_type);
95 StringMap<FunctionSamples> Profiles;
96 Profiles[FooName] = std::move(FooSamples);
97 Profiles[BarName] = std::move(BarSamples);
99 std::error_code EC;
100 EC = Writer->write(Profiles);
101 ASSERT_TRUE(NoError(EC));
103 Writer->getOutputStream().flush();
105 readProfile(M, Profile);
107 EC = Reader->read();
108 ASSERT_TRUE(NoError(EC));
110 if (Remap) {
111 auto MemBuffer = llvm::MemoryBuffer::getMemBuffer(R"(
112 # Types 'int' and 'long' are equivalent
113 type i l
114 # Function names 'foo' and 'faux' are equivalent
115 name 3foo 4faux
116 )");
117 Reader.reset(new SampleProfileReaderItaniumRemapper(
118 std::move(MemBuffer), Context, std::move(Reader)));
119 FooName = "_Z4fauxi";
120 BarName = "_Z3barl";
122 EC = Reader->read();
123 ASSERT_TRUE(NoError(EC));
126 ASSERT_EQ(2u, Reader->getProfiles().size());
128 FunctionSamples *ReadFooSamples = Reader->getSamplesFor(FooName);
129 ASSERT_TRUE(ReadFooSamples != nullptr);
130 if (Format != SampleProfileFormat::SPF_Compact_Binary) {
131 ASSERT_EQ("_Z3fooi", ReadFooSamples->getName());
133 ASSERT_EQ(7711u, ReadFooSamples->getTotalSamples());
134 ASSERT_EQ(610u, ReadFooSamples->getHeadSamples());
136 FunctionSamples *ReadBarSamples = Reader->getSamplesFor(BarName);
137 ASSERT_TRUE(ReadBarSamples != nullptr);
138 if (Format != SampleProfileFormat::SPF_Compact_Binary) {
139 ASSERT_EQ("_Z3bari", ReadBarSamples->getName());
141 ASSERT_EQ(20301u, ReadBarSamples->getTotalSamples());
142 ASSERT_EQ(1437u, ReadBarSamples->getHeadSamples());
143 ErrorOr<SampleRecord::CallTargetMap> CTMap =
144 ReadBarSamples->findCallTargetMapAt(1, 0);
145 ASSERT_FALSE(CTMap.getError());
147 std::string MconstructGUID;
148 StringRef MconstructRep =
149 getRepInFormat(MconstructName, Format, MconstructGUID);
150 std::string StringviewGUID;
151 StringRef StringviewRep =
152 getRepInFormat(StringviewName, Format, StringviewGUID);
153 ASSERT_EQ(1000u, CTMap.get()[MconstructRep]);
154 ASSERT_EQ(437u, CTMap.get()[StringviewRep]);
156 auto VerifySummary = [](ProfileSummary &Summary) mutable {
157 ASSERT_EQ(ProfileSummary::PSK_Sample, Summary.getKind());
158 ASSERT_EQ(123603u, Summary.getTotalCount());
159 ASSERT_EQ(6u, Summary.getNumCounts());
160 ASSERT_EQ(2u, Summary.getNumFunctions());
161 ASSERT_EQ(1437u, Summary.getMaxFunctionCount());
162 ASSERT_EQ(60351u, Summary.getMaxCount());
164 uint32_t Cutoff = 800000;
165 auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
166 return PE.Cutoff == Cutoff;
168 std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();
169 auto EightyPerc = find_if(Details, Predicate);
170 Cutoff = 900000;
171 auto NinetyPerc = find_if(Details, Predicate);
172 Cutoff = 950000;
173 auto NinetyFivePerc = find_if(Details, Predicate);
174 Cutoff = 990000;
175 auto NinetyNinePerc = find_if(Details, Predicate);
176 ASSERT_EQ(60000u, EightyPerc->MinCount);
177 ASSERT_EQ(60000u, NinetyPerc->MinCount);
178 ASSERT_EQ(60000u, NinetyFivePerc->MinCount);
179 ASSERT_EQ(610u, NinetyNinePerc->MinCount);
182 ProfileSummary &Summary = Reader->getSummary();
183 VerifySummary(Summary);
185 // Test that conversion of summary to and from Metadata works.
186 Metadata *MD = Summary.getMD(Context);
187 ASSERT_TRUE(MD);
188 ProfileSummary *PS = ProfileSummary::getFromMD(MD);
189 ASSERT_TRUE(PS);
190 VerifySummary(*PS);
191 delete PS;
193 // Test that summary can be attached to and read back from module.
194 M.setProfileSummary(MD, ProfileSummary::PSK_Sample);
195 MD = M.getProfileSummary(/* IsCS */ false);
196 ASSERT_TRUE(MD);
197 PS = ProfileSummary::getFromMD(MD);
198 ASSERT_TRUE(PS);
199 VerifySummary(*PS);
200 delete PS;
203 void addFunctionSamples(StringMap<FunctionSamples> *Smap, const char *Fname,
204 uint64_t TotalSamples, uint64_t HeadSamples) {
205 StringRef Name(Fname);
206 FunctionSamples FcnSamples;
207 FcnSamples.setName(Name);
208 FcnSamples.addTotalSamples(TotalSamples);
209 FcnSamples.addHeadSamples(HeadSamples);
210 FcnSamples.addBodySamples(1, 0, HeadSamples);
211 (*Smap)[Name] = FcnSamples;
214 StringMap<FunctionSamples> setupFcnSamplesForElisionTest(StringRef Policy) {
215 StringMap<FunctionSamples> Smap;
216 addFunctionSamples(&Smap, "foo", uint64_t(20301), uint64_t(1437));
217 if (Policy == "" || Policy == "all")
218 return Smap;
219 addFunctionSamples(&Smap, "foo.bar", uint64_t(20303), uint64_t(1439));
220 if (Policy == "selected")
221 return Smap;
222 addFunctionSamples(&Smap, "foo.llvm.2465", uint64_t(20305), uint64_t(1441));
223 return Smap;
226 void createFunctionWithSampleProfileElisionPolicy(Module *M,
227 const char *Fname,
228 StringRef Policy) {
229 FunctionType *FnType =
230 FunctionType::get(Type::getVoidTy(Context), {}, false);
231 auto Inserted = M->getOrInsertFunction(Fname, FnType);
232 auto Fcn = cast<Function>(Inserted.getCallee());
233 if (Policy != "")
234 Fcn->addFnAttr("sample-profile-suffix-elision-policy", Policy);
237 void setupModuleForElisionTest(Module *M, StringRef Policy) {
238 createFunctionWithSampleProfileElisionPolicy(M, "foo", Policy);
239 createFunctionWithSampleProfileElisionPolicy(M, "foo.bar", Policy);
240 createFunctionWithSampleProfileElisionPolicy(M, "foo.llvm.2465", Policy);
243 void testSuffixElisionPolicy(SampleProfileFormat Format, StringRef Policy,
244 const StringMap<uint64_t> &Expected) {
245 SmallVector<char, 128> ProfilePath;
246 std::error_code EC;
247 EC = llvm::sys::fs::createTemporaryFile("profile", "", ProfilePath);
248 ASSERT_TRUE(NoError(EC));
249 StringRef ProfileFile(ProfilePath.data(), ProfilePath.size());
251 Module M("my_module", Context);
252 setupModuleForElisionTest(&M, Policy);
253 StringMap<FunctionSamples> ProfMap = setupFcnSamplesForElisionTest(Policy);
255 // write profile
256 createWriter(Format, ProfileFile);
257 EC = Writer->write(ProfMap);
258 ASSERT_TRUE(NoError(EC));
259 Writer->getOutputStream().flush();
261 // read profile
262 readProfile(M, ProfileFile);
263 EC = Reader->read();
264 ASSERT_TRUE(NoError(EC));
266 for (auto I = Expected.begin(); I != Expected.end(); ++I) {
267 uint64_t Esamples = uint64_t(-1);
268 FunctionSamples *Samples = Reader->getSamplesFor(I->getKey());
269 if (Samples != nullptr)
270 Esamples = Samples->getTotalSamples();
271 ASSERT_EQ(I->getValue(), Esamples);
276 TEST_F(SampleProfTest, roundtrip_text_profile) {
277 testRoundTrip(SampleProfileFormat::SPF_Text, false);
280 TEST_F(SampleProfTest, roundtrip_raw_binary_profile) {
281 testRoundTrip(SampleProfileFormat::SPF_Binary, false);
284 TEST_F(SampleProfTest, roundtrip_compact_binary_profile) {
285 testRoundTrip(SampleProfileFormat::SPF_Compact_Binary, false);
288 TEST_F(SampleProfTest, roundtrip_ext_binary_profile) {
289 testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, false);
292 TEST_F(SampleProfTest, remap_text_profile) {
293 testRoundTrip(SampleProfileFormat::SPF_Text, true);
296 TEST_F(SampleProfTest, remap_raw_binary_profile) {
297 testRoundTrip(SampleProfileFormat::SPF_Binary, true);
300 TEST_F(SampleProfTest, remap_ext_binary_profile) {
301 testRoundTrip(SampleProfileFormat::SPF_Ext_Binary, true);
304 TEST_F(SampleProfTest, sample_overflow_saturation) {
305 const uint64_t Max = std::numeric_limits<uint64_t>::max();
306 sampleprof_error Result;
308 FunctionSamples FooSamples;
309 Result = FooSamples.addTotalSamples(1);
310 ASSERT_EQ(Result, sampleprof_error::success);
312 Result = FooSamples.addHeadSamples(1);
313 ASSERT_EQ(Result, sampleprof_error::success);
315 Result = FooSamples.addBodySamples(10, 0, 1);
316 ASSERT_EQ(Result, sampleprof_error::success);
318 Result = FooSamples.addTotalSamples(Max);
319 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
320 ASSERT_EQ(FooSamples.getTotalSamples(), Max);
322 Result = FooSamples.addHeadSamples(Max);
323 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
324 ASSERT_EQ(FooSamples.getHeadSamples(), Max);
326 Result = FooSamples.addBodySamples(10, 0, Max);
327 ASSERT_EQ(Result, sampleprof_error::counter_overflow);
328 ErrorOr<uint64_t> BodySamples = FooSamples.findSamplesAt(10, 0);
329 ASSERT_FALSE(BodySamples.getError());
330 ASSERT_EQ(BodySamples.get(), Max);
333 TEST_F(SampleProfTest, default_suffix_elision_text) {
334 // Default suffix elision policy: strip everything after first dot.
335 // This implies that all suffix variants will map to "foo", so
336 // we don't expect to see any entries for them in the sample
337 // profile.
338 StringMap<uint64_t> Expected;
339 Expected["foo"] = uint64_t(20301);
340 Expected["foo.bar"] = uint64_t(-1);
341 Expected["foo.llvm.2465"] = uint64_t(-1);
342 testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "", Expected);
345 TEST_F(SampleProfTest, default_suffix_elision_compact_binary) {
346 // Default suffix elision policy: strip everything after first dot.
347 // This implies that all suffix variants will map to "foo", so
348 // we don't expect to see any entries for them in the sample
349 // profile.
350 StringMap<uint64_t> Expected;
351 Expected["foo"] = uint64_t(20301);
352 Expected["foo.bar"] = uint64_t(-1);
353 Expected["foo.llvm.2465"] = uint64_t(-1);
354 testSuffixElisionPolicy(SampleProfileFormat::SPF_Compact_Binary, "",
355 Expected);
358 TEST_F(SampleProfTest, selected_suffix_elision_text) {
359 // Profile is created and searched using the "selected"
360 // suffix elision policy: we only strip a .XXX suffix if
361 // it matches a pattern known to be generated by the compiler
362 // (e.g. ".llvm.<digits>").
363 StringMap<uint64_t> Expected;
364 Expected["foo"] = uint64_t(20301);
365 Expected["foo.bar"] = uint64_t(20303);
366 Expected["foo.llvm.2465"] = uint64_t(-1);
367 testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "selected", Expected);
370 TEST_F(SampleProfTest, selected_suffix_elision_compact_binary) {
371 // Profile is created and searched using the "selected"
372 // suffix elision policy: we only strip a .XXX suffix if
373 // it matches a pattern known to be generated by the compiler
374 // (e.g. ".llvm.<digits>").
375 StringMap<uint64_t> Expected;
376 Expected["foo"] = uint64_t(20301);
377 Expected["foo.bar"] = uint64_t(20303);
378 Expected["foo.llvm.2465"] = uint64_t(-1);
379 testSuffixElisionPolicy(SampleProfileFormat::SPF_Compact_Binary, "selected",
380 Expected);
383 TEST_F(SampleProfTest, none_suffix_elision_text) {
384 // Profile is created and searched using the "none"
385 // suffix elision policy: no stripping of suffixes at all.
386 // Here we expect to see all variants in the profile.
387 StringMap<uint64_t> Expected;
388 Expected["foo"] = uint64_t(20301);
389 Expected["foo.bar"] = uint64_t(20303);
390 Expected["foo.llvm.2465"] = uint64_t(20305);
391 testSuffixElisionPolicy(SampleProfileFormat::SPF_Text, "none", Expected);
394 TEST_F(SampleProfTest, none_suffix_elision_compact_binary) {
395 // Profile is created and searched using the "none"
396 // suffix elision policy: no stripping of suffixes at all.
397 // Here we expect to see all variants in the profile.
398 StringMap<uint64_t> Expected;
399 Expected["foo"] = uint64_t(20301);
400 Expected["foo.bar"] = uint64_t(20303);
401 Expected["foo.llvm.2465"] = uint64_t(20305);
402 testSuffixElisionPolicy(SampleProfileFormat::SPF_Compact_Binary, "none",
403 Expected);
406 } // end anonymous namespace