2015-11-05 06:32:32 +08:00
|
|
|
//===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===//
|
2015-02-18 09:58:17 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-20 09:26:34 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-02-05 03:11:43 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2016-01-20 09:26:34 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2015-02-18 09:58:17 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
|
|
|
#include "llvm/ProfileData/InstrProfWriter.h"
|
2015-12-31 15:57:16 +08:00
|
|
|
#include "llvm/Support/Compression.h"
|
2017-07-08 05:02:59 +08:00
|
|
|
#include "llvm/Testing/Support/Error.h"
|
|
|
|
#include "llvm/Testing/Support/SupportHelpers.h"
|
2015-02-18 09:58:17 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <cstdarg>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-07-08 05:02:59 +08:00
|
|
|
LLVM_NODISCARD static ::testing::AssertionResult
|
|
|
|
ErrorEquals(instrprof_error Expected, Error E) {
|
2016-05-19 11:54:45 +08:00
|
|
|
instrprof_error Found;
|
|
|
|
std::string FoundMsg;
|
|
|
|
handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
|
|
|
|
Found = IPE.get();
|
|
|
|
FoundMsg = IPE.message();
|
|
|
|
});
|
2015-02-18 09:58:17 +08:00
|
|
|
if (Expected == Found)
|
|
|
|
return ::testing::AssertionSuccess();
|
2016-05-19 11:54:45 +08:00
|
|
|
return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
|
2015-02-18 09:58:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct InstrProfTest : ::testing::Test {
|
|
|
|
InstrProfWriter Writer;
|
|
|
|
std::unique_ptr<IndexedInstrProfReader> Reader;
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
void SetUp() { Writer.setOutputSparse(false); }
|
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
|
|
|
|
auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded());
|
2015-02-18 09:58:17 +08:00
|
|
|
Reader = std::move(ReaderOrErr.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
struct SparseInstrProfTest : public InstrProfTest {
|
|
|
|
void SetUp() { Writer.setOutputSparse(true); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MaybeSparseInstrProfTest : public InstrProfTest,
|
|
|
|
public ::testing::WithParamInterface<bool> {
|
2016-03-22 23:14:18 +08:00
|
|
|
void SetUp() { Writer.setOutputSparse(GetParam()); }
|
2016-01-30 06:54:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) {
|
2015-02-18 09:58:17 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
ASSERT_TRUE(Reader->begin() == Reader->end());
|
|
|
|
}
|
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
static const auto Err = [](Error E) {
|
|
|
|
consumeError(std::move(E));
|
|
|
|
FAIL();
|
|
|
|
};
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {1, 2, 3, 4}}, Err);
|
2015-02-18 09:58:17 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
auto I = Reader->begin(), E = Reader->end();
|
|
|
|
ASSERT_TRUE(I != E);
|
|
|
|
ASSERT_EQ(StringRef("foo"), I->Name);
|
|
|
|
ASSERT_EQ(0x1234U, I->Hash);
|
|
|
|
ASSERT_EQ(4U, I->Counts.size());
|
|
|
|
ASSERT_EQ(1U, I->Counts[0]);
|
|
|
|
ASSERT_EQ(2U, I->Counts[1]);
|
|
|
|
ASSERT_EQ(3U, I->Counts[2]);
|
|
|
|
ASSERT_EQ(4U, I->Counts[3]);
|
|
|
|
ASSERT_TRUE(++I == E);
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {1, 2}}, Err);
|
|
|
|
Writer.addRecord({"foo", 0x1235, {3, 4}}, Err);
|
2015-11-02 13:08:23 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(2U, R->Counts.size());
|
|
|
|
ASSERT_EQ(1U, R->Counts[0]);
|
|
|
|
ASSERT_EQ(2U, R->Counts[1]);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
R = Reader->getInstrProfRecord("foo", 0x1235);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(2U, R->Counts.size());
|
|
|
|
ASSERT_EQ(3U, R->Counts[0]);
|
|
|
|
ASSERT_EQ(4U, R->Counts[1]);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
R = Reader->getInstrProfRecord("foo", 0x5678);
|
2016-05-19 11:54:45 +08:00
|
|
|
ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError()));
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
R = Reader->getInstrProfRecord("bar", 0x1234);
|
2016-05-19 11:54:45 +08:00
|
|
|
ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError()));
|
2015-11-02 13:08:23 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {1, 2}}, Err);
|
|
|
|
Writer.addRecord({"foo", 0x1235, {3, 4}}, Err);
|
2015-02-18 09:58:17 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
std::vector<uint64_t> Counts;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1234, Counts),
|
|
|
|
Succeeded());
|
2015-02-18 09:58:17 +08:00
|
|
|
ASSERT_EQ(2U, Counts.size());
|
|
|
|
ASSERT_EQ(1U, Counts[0]);
|
|
|
|
ASSERT_EQ(2U, Counts[1]);
|
|
|
|
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1235, Counts),
|
|
|
|
Succeeded());
|
2015-06-23 07:56:53 +08:00
|
|
|
ASSERT_EQ(2U, Counts.size());
|
|
|
|
ASSERT_EQ(3U, Counts[0]);
|
|
|
|
ASSERT_EQ(4U, Counts[1]);
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts);
|
|
|
|
ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1)));
|
2015-02-18 09:58:17 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts);
|
|
|
|
ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2)));
|
2015-02-18 09:58:17 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 12:08:18 +08:00
|
|
|
// Profile data is copied from general.proftext
|
|
|
|
TEST_F(InstrProfTest, get_profile_summary) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"func1", 0x1234, {97531}}, Err);
|
|
|
|
Writer.addRecord({"func2", 0x1234, {0, 0}}, Err);
|
|
|
|
Writer.addRecord(
|
|
|
|
{"func3",
|
|
|
|
0x1234,
|
|
|
|
{2305843009213693952, 1152921504606846976, 576460752303423488,
|
|
|
|
288230376151711744, 144115188075855872, 72057594037927936}},
|
|
|
|
Err);
|
|
|
|
Writer.addRecord({"func4", 0x1234, {0}}, Err);
|
2016-02-03 12:08:18 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-20 05:53:28 +08:00
|
|
|
auto VerifySummary = [](ProfileSummary &IPS) mutable {
|
|
|
|
ASSERT_EQ(ProfileSummary::PSK_Instr, IPS.getKind());
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_EQ(2305843009213693952U, IPS.getMaxFunctionCount());
|
2016-05-20 05:53:28 +08:00
|
|
|
ASSERT_EQ(2305843009213693952U, IPS.getMaxCount());
|
|
|
|
ASSERT_EQ(10U, IPS.getNumCounts());
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_EQ(4539628424389557499U, IPS.getTotalCount());
|
|
|
|
std::vector<ProfileSummaryEntry> &Details = IPS.getDetailedSummary();
|
|
|
|
uint32_t Cutoff = 800000;
|
|
|
|
auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
|
|
|
|
return PE.Cutoff == Cutoff;
|
|
|
|
};
|
2016-08-12 08:18:03 +08:00
|
|
|
auto EightyPerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 900000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyPerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 950000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyFivePerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
Cutoff = 990000;
|
2016-08-12 08:18:03 +08:00
|
|
|
auto NinetyNinePerc = find_if(Details, Predicate);
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_EQ(576460752303423488U, EightyPerc->MinCount);
|
|
|
|
ASSERT_EQ(288230376151711744U, NinetyPerc->MinCount);
|
|
|
|
ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinCount);
|
|
|
|
ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinCount);
|
2016-02-03 12:08:18 +08:00
|
|
|
};
|
2016-05-20 05:53:28 +08:00
|
|
|
ProfileSummary &PS = Reader->getSummary();
|
2016-03-15 06:23:28 +08:00
|
|
|
VerifySummary(PS);
|
2016-03-19 05:29:30 +08:00
|
|
|
|
|
|
|
// Test that conversion of summary to and from Metadata works.
|
2016-04-15 05:59:01 +08:00
|
|
|
LLVMContext Context;
|
|
|
|
Metadata *MD = PS.getMD(Context);
|
2016-03-02 02:30:58 +08:00
|
|
|
ASSERT_TRUE(MD);
|
|
|
|
ProfileSummary *PSFromMD = ProfileSummary::getFromMD(MD);
|
|
|
|
ASSERT_TRUE(PSFromMD);
|
2016-05-20 05:53:28 +08:00
|
|
|
VerifySummary(*PSFromMD);
|
|
|
|
delete PSFromMD;
|
2016-03-19 05:29:30 +08:00
|
|
|
|
|
|
|
// Test that summary can be attached to and read back from module.
|
2016-04-15 05:59:01 +08:00
|
|
|
Module M("my_module", Context);
|
2016-03-19 05:29:30 +08:00
|
|
|
M.setProfileSummary(MD);
|
|
|
|
MD = M.getProfileSummary();
|
|
|
|
ASSERT_TRUE(MD);
|
|
|
|
PSFromMD = ProfileSummary::getFromMD(MD);
|
|
|
|
ASSERT_TRUE(PSFromMD);
|
2016-05-20 05:53:28 +08:00
|
|
|
VerifySummary(*PSFromMD);
|
|
|
|
delete PSFromMD;
|
2016-02-03 12:08:18 +08:00
|
|
|
}
|
|
|
|
|
2016-07-19 09:17:20 +08:00
|
|
|
TEST_F(InstrProfTest, test_writer_merge) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"func1", 0x1234, {42}}, Err);
|
2016-07-19 09:17:20 +08:00
|
|
|
|
|
|
|
InstrProfWriter Writer2;
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer2.addRecord({"func2", 0x1234, {0, 0}}, Err);
|
2016-07-19 09:17:20 +08:00
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.mergeRecordsFromWriter(std::move(Writer2), Err);
|
2016-07-19 09:17:20 +08:00
|
|
|
|
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("func1", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-07-19 09:17:20 +08:00
|
|
|
ASSERT_EQ(1U, R->Counts.size());
|
|
|
|
ASSERT_EQ(42U, R->Counts[0]);
|
|
|
|
|
|
|
|
R = Reader->getInstrProfRecord("func2", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-07-19 09:17:20 +08:00
|
|
|
ASSERT_EQ(2U, R->Counts.size());
|
|
|
|
ASSERT_EQ(0U, R->Counts[0]);
|
|
|
|
ASSERT_EQ(0U, R->Counts[1]);
|
|
|
|
}
|
|
|
|
|
2016-04-10 10:35:53 +08:00
|
|
|
static const char callee1[] = "callee1";
|
|
|
|
static const char callee2[] = "callee2";
|
|
|
|
static const char callee3[] = "callee3";
|
|
|
|
static const char callee4[] = "callee4";
|
|
|
|
static const char callee5[] = "callee5";
|
|
|
|
static const char callee6[] = "callee6";
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record1("caller", 0x1234, {1, 2});
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
// 4 value sites.
|
|
|
|
Record1.reserveSites(IPVK_IndirectCallTarget, 4);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD0[] = {
|
|
|
|
{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
|
2015-11-10 08:24:45 +08:00
|
|
|
// No value profile data at the second site.
|
2015-11-05 06:32:32 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record1), Err);
|
|
|
|
Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err);
|
2015-11-02 13:08:23 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
|
|
|
|
ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
|
|
|
|
ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-02-04 13:29:51 +08:00
|
|
|
uint64_t TotalC;
|
2015-11-02 13:08:23 +08:00
|
|
|
std::unique_ptr<InstrProfValueData[]> VD =
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
|
2016-01-08 11:49:59 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(3U, VD[0].Count);
|
|
|
|
ASSERT_EQ(2U, VD[1].Count);
|
|
|
|
ASSERT_EQ(1U, VD[2].Count);
|
2016-02-04 13:29:51 +08:00
|
|
|
ASSERT_EQ(6U, TotalC);
|
2016-01-08 11:49:59 +08:00
|
|
|
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
|
|
|
|
}
|
|
|
|
|
2016-02-05 03:11:43 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) {
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record("caller", 0x1234, {1, 2});
|
2016-02-05 03:11:43 +08:00
|
|
|
Record.reserveSites(IPVK_IndirectCallTarget, 1);
|
2016-02-11 06:19:43 +08:00
|
|
|
InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}, {5000, 5},
|
|
|
|
{4000, 4}, {6000, 6}};
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 6, nullptr);
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record), Err);
|
2016-02-05 03:11:43 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-05 03:11:43 +08:00
|
|
|
|
|
|
|
LLVMContext Ctx;
|
|
|
|
std::unique_ptr<Module> M(new Module("MyModule", Ctx));
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
Function *F =
|
|
|
|
Function::Create(FTy, Function::ExternalLinkage, "caller", M.get());
|
|
|
|
BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
|
|
|
|
IRBuilder<> Builder(BB);
|
|
|
|
BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
|
|
|
|
|
|
|
|
// Use branch instruction to annotate with value profile data for simplicity
|
|
|
|
Instruction *Inst = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
|
|
|
|
Instruction *Inst2 = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
|
2016-02-09 09:02:24 +08:00
|
|
|
annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0);
|
2016-02-05 03:11:43 +08:00
|
|
|
|
|
|
|
InstrProfValueData ValueData[5];
|
|
|
|
uint32_t N;
|
|
|
|
uint64_t T;
|
|
|
|
bool Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
|
|
|
|
ValueData, N, T);
|
|
|
|
ASSERT_TRUE(Res);
|
|
|
|
ASSERT_EQ(3U, N);
|
2016-02-11 06:19:43 +08:00
|
|
|
ASSERT_EQ(21U, T);
|
2016-02-05 03:11:43 +08:00
|
|
|
// The result should be sorted already:
|
2016-02-11 06:19:43 +08:00
|
|
|
ASSERT_EQ(6000U, ValueData[0].Value);
|
|
|
|
ASSERT_EQ(6U, ValueData[0].Count);
|
|
|
|
ASSERT_EQ(5000U, ValueData[1].Value);
|
|
|
|
ASSERT_EQ(5U, ValueData[1].Count);
|
|
|
|
ASSERT_EQ(4000U, ValueData[2].Value);
|
|
|
|
ASSERT_EQ(4U, ValueData[2].Count);
|
2016-02-05 03:11:43 +08:00
|
|
|
Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 1, ValueData,
|
|
|
|
N, T);
|
|
|
|
ASSERT_TRUE(Res);
|
|
|
|
ASSERT_EQ(1U, N);
|
2016-02-11 06:19:43 +08:00
|
|
|
ASSERT_EQ(21U, T);
|
2016-02-05 03:11:43 +08:00
|
|
|
|
|
|
|
Res = getValueProfDataFromInst(*Inst2, IPVK_IndirectCallTarget, 5, ValueData,
|
|
|
|
N, T);
|
|
|
|
ASSERT_FALSE(Res);
|
2016-02-11 06:19:43 +08:00
|
|
|
|
|
|
|
// Remove the MD_prof metadata
|
|
|
|
Inst->setMetadata(LLVMContext::MD_prof, 0);
|
|
|
|
// Annotate 5 records this time.
|
|
|
|
annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0, 5);
|
|
|
|
Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
|
|
|
|
ValueData, N, T);
|
|
|
|
ASSERT_TRUE(Res);
|
|
|
|
ASSERT_EQ(5U, N);
|
|
|
|
ASSERT_EQ(21U, T);
|
|
|
|
ASSERT_EQ(6000U, ValueData[0].Value);
|
|
|
|
ASSERT_EQ(6U, ValueData[0].Count);
|
|
|
|
ASSERT_EQ(5000U, ValueData[1].Value);
|
|
|
|
ASSERT_EQ(5U, ValueData[1].Count);
|
|
|
|
ASSERT_EQ(4000U, ValueData[2].Value);
|
|
|
|
ASSERT_EQ(4U, ValueData[2].Count);
|
|
|
|
ASSERT_EQ(3000U, ValueData[3].Value);
|
|
|
|
ASSERT_EQ(3U, ValueData[3].Count);
|
|
|
|
ASSERT_EQ(2000U, ValueData[4].Value);
|
|
|
|
ASSERT_EQ(2U, ValueData[4].Count);
|
2016-02-13 05:36:17 +08:00
|
|
|
|
|
|
|
// Remove the MD_prof metadata
|
|
|
|
Inst->setMetadata(LLVMContext::MD_prof, 0);
|
|
|
|
// Annotate with 4 records.
|
|
|
|
InstrProfValueData VD0Sorted[] = {{1000, 6}, {2000, 5}, {3000, 4}, {4000, 3},
|
|
|
|
{5000, 2}, {6000, 1}};
|
2016-03-31 00:56:31 +08:00
|
|
|
annotateValueSite(*M, *Inst, makeArrayRef(VD0Sorted).slice(2), 10,
|
|
|
|
IPVK_IndirectCallTarget, 5);
|
2016-02-13 05:36:17 +08:00
|
|
|
Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
|
|
|
|
ValueData, N, T);
|
|
|
|
ASSERT_TRUE(Res);
|
|
|
|
ASSERT_EQ(4U, N);
|
|
|
|
ASSERT_EQ(10U, T);
|
|
|
|
ASSERT_EQ(3000U, ValueData[0].Value);
|
|
|
|
ASSERT_EQ(4U, ValueData[0].Count);
|
|
|
|
ASSERT_EQ(4000U, ValueData[1].Value);
|
|
|
|
ASSERT_EQ(3U, ValueData[1].Count);
|
|
|
|
ASSERT_EQ(5000U, ValueData[2].Value);
|
|
|
|
ASSERT_EQ(2U, ValueData[2].Count);
|
|
|
|
ASSERT_EQ(6000U, ValueData[3].Value);
|
|
|
|
ASSERT_EQ(1U, ValueData[3].Count);
|
2016-02-05 03:11:43 +08:00
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) {
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record1("caller", 0x1234, {1, 2});
|
2016-01-08 11:49:59 +08:00
|
|
|
|
|
|
|
// 4 value sites.
|
|
|
|
Record1.reserveSites(IPVK_IndirectCallTarget, 4);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD0[] = {
|
|
|
|
{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
|
2016-01-08 11:49:59 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
|
|
|
|
// No value profile data at the second site.
|
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
|
2016-01-08 11:49:59 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
|
2016-01-08 11:49:59 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
|
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record1), 10, Err);
|
|
|
|
Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err);
|
2016-01-08 11:49:59 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
|
|
|
|
ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
|
|
|
|
ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
|
2016-01-08 11:49:59 +08:00
|
|
|
|
2016-02-04 13:29:51 +08:00
|
|
|
uint64_t TotalC;
|
2016-01-08 11:49:59 +08:00
|
|
|
std::unique_ptr<InstrProfValueData[]> VD =
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
|
2016-01-08 11:49:59 +08:00
|
|
|
ASSERT_EQ(30U, VD[0].Count);
|
|
|
|
ASSERT_EQ(20U, VD[1].Count);
|
|
|
|
ASSERT_EQ(10U, VD[2].Count);
|
2016-02-04 13:29:51 +08:00
|
|
|
ASSERT_EQ(60U, TotalC);
|
2016-01-08 11:49:59 +08:00
|
|
|
|
2015-11-02 13:08:23 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) {
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record1("caller", 0x1234, {1, 2});
|
2016-01-23 03:53:31 +08:00
|
|
|
|
|
|
|
// 4 value sites.
|
|
|
|
Record1.reserveSites(IPVK_IndirectCallTarget, 4);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD0[] = {
|
|
|
|
{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
|
2016-01-23 03:53:31 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
|
|
|
|
// No value profile data at the second site.
|
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
|
2016-01-23 03:53:31 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
|
2016-04-10 10:35:53 +08:00
|
|
|
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
|
2016-01-23 03:53:31 +08:00
|
|
|
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
|
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record1), Err);
|
|
|
|
Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err);
|
2016-01-23 03:53:31 +08:00
|
|
|
|
|
|
|
// Set big endian output.
|
|
|
|
Writer.setValueProfDataEndianness(support::big);
|
|
|
|
|
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
// Set big endian input.
|
|
|
|
Reader->setValueProfDataEndianness(support::big);
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
|
|
|
|
ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
|
|
|
|
ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
|
2016-01-23 03:53:31 +08:00
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD =
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 0);
|
2016-01-23 03:53:31 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
|
|
|
|
|
|
|
|
// Restore little endian default:
|
|
|
|
Writer.setValueProfDataEndianness(support::little);
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
|
2015-12-27 14:18:57 +08:00
|
|
|
static const char caller[] = "caller";
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record11(caller, 0x1234, {1, 2});
|
|
|
|
NamedInstrProfRecord Record12(caller, 0x1234, {1, 2});
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
// 5 value sites.
|
|
|
|
Record11.reserveSites(IPVK_IndirectCallTarget, 5);
|
2015-12-27 14:18:57 +08:00
|
|
|
InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
|
|
|
|
{uint64_t(callee2), 2},
|
|
|
|
{uint64_t(callee3), 3},
|
|
|
|
{uint64_t(callee4), 4}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-01-08 14:54:27 +08:00
|
|
|
// No value profile data at the second site.
|
2015-11-05 06:32:32 +08:00
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-01-08 14:54:27 +08:00
|
|
|
InstrProfValueData VD2[] = {
|
|
|
|
{uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2015-12-27 14:18:57 +08:00
|
|
|
InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2015-12-27 14:18:57 +08:00
|
|
|
InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
|
|
|
|
{uint64_t(callee2), 2},
|
|
|
|
{uint64_t(callee3), 3}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-04-22 14:37:48 +08:00
|
|
|
// A different record for the same caller.
|
2015-11-02 13:08:23 +08:00
|
|
|
Record12.reserveSites(IPVK_IndirectCallTarget, 5);
|
2016-01-08 14:54:27 +08:00
|
|
|
InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-01-08 14:54:27 +08:00
|
|
|
// No value profile data at the second site.
|
2015-11-05 06:32:32 +08:00
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-01-08 14:54:27 +08:00
|
|
|
InstrProfValueData VD22[] = {
|
|
|
|
{uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2015-11-05 06:32:32 +08:00
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2016-01-23 03:53:31 +08:00
|
|
|
InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
|
|
|
|
{uint64_t(callee2), 2},
|
|
|
|
{uint64_t(callee3), 3}};
|
2015-11-05 06:32:32 +08:00
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record11), Err);
|
2015-11-02 13:08:23 +08:00
|
|
|
// Merge profile data.
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record12), Err);
|
2015-11-02 13:08:23 +08:00
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({callee1, 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({callee2, 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({callee3, 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({callee3, 0x1235, {3, 4}}, Err);
|
|
|
|
Writer.addRecord({callee4, 0x1235, {3, 5}}, Err);
|
2015-11-02 13:08:23 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
|
|
|
|
ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
|
|
|
|
ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
|
|
|
|
ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD =
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 0);
|
2015-11-02 13:08:23 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(7U, VD[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(6U, VD[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
|
|
|
|
ASSERT_EQ(4U, VD[2].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(1U, VD[3].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_2(
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 2));
|
2015-11-02 13:08:23 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(6U, VD_2[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
|
|
|
|
ASSERT_EQ(4U, VD_2[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(3U, VD_2[2].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(1U, VD_2[3].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_3(
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 3));
|
2015-11-02 13:08:23 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(1U, VD_3[0].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_4(
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 4));
|
2015-11-02 13:08:23 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(6U, VD_4[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(4U, VD_4[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(2U, VD_4[2].Count);
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
|
2015-12-27 14:18:57 +08:00
|
|
|
static const char bar[] = "bar";
|
|
|
|
|
2015-11-20 21:13:53 +08:00
|
|
|
const uint64_t Max = std::numeric_limits<uint64_t>::max();
|
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
instrprof_error Result;
|
|
|
|
auto Err = [&](Error E) { Result = InstrProfError::take(std::move(E)); };
|
|
|
|
Result = instrprof_error::success;
|
|
|
|
Writer.addRecord({"foo", 0x1234, {1}}, Err);
|
|
|
|
ASSERT_EQ(Result, instrprof_error::success);
|
2015-12-17 05:45:43 +08:00
|
|
|
|
|
|
|
// Verify counter overflow.
|
2017-07-10 11:04:59 +08:00
|
|
|
Result = instrprof_error::success;
|
|
|
|
Writer.addRecord({"foo", 0x1234, {Max}}, Err);
|
|
|
|
ASSERT_EQ(Result, instrprof_error::counter_overflow);
|
2015-12-17 05:45:43 +08:00
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Result = instrprof_error::success;
|
|
|
|
Writer.addRecord({bar, 0x9012, {8}}, Err);
|
|
|
|
ASSERT_EQ(Result, instrprof_error::success);
|
2015-12-17 05:45:43 +08:00
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record4("baz", 0x5678, {3, 4});
|
2015-12-17 05:45:43 +08:00
|
|
|
Record4.reserveSites(IPVK_IndirectCallTarget, 1);
|
2015-12-27 14:18:57 +08:00
|
|
|
InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
|
2015-12-17 05:45:43 +08:00
|
|
|
Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
|
2017-07-10 11:04:59 +08:00
|
|
|
Result = instrprof_error::success;
|
|
|
|
Writer.addRecord(std::move(Record4), Err);
|
|
|
|
ASSERT_EQ(Result, instrprof_error::success);
|
2015-12-17 05:45:43 +08:00
|
|
|
|
|
|
|
// Verify value data counter overflow.
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record5("baz", 0x5678, {5, 6});
|
2015-12-17 05:45:43 +08:00
|
|
|
Record5.reserveSites(IPVK_IndirectCallTarget, 1);
|
2015-12-27 14:18:57 +08:00
|
|
|
InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
|
2015-12-17 05:45:43 +08:00
|
|
|
Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
|
2017-07-10 11:04:59 +08:00
|
|
|
Result = instrprof_error::success;
|
|
|
|
Writer.addRecord(std::move(Record5), Err);
|
|
|
|
ASSERT_EQ(Result, instrprof_error::counter_overflow);
|
2015-11-20 21:13:53 +08:00
|
|
|
|
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
// Verify saturation of counts.
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> ReadRecord1 =
|
2015-12-17 05:45:43 +08:00
|
|
|
Reader->getInstrProfRecord("foo", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(ReadRecord1.takeError(), Succeeded());
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(Max, ReadRecord1->Counts[0]);
|
2015-12-17 05:45:43 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> ReadRecord2 =
|
2015-12-17 05:45:43 +08:00
|
|
|
Reader->getInstrProfRecord("baz", 0x5678);
|
2016-05-19 11:54:45 +08:00
|
|
|
ASSERT_TRUE(bool(ReadRecord2));
|
2016-02-09 09:02:24 +08:00
|
|
|
ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget));
|
2015-11-20 21:13:53 +08:00
|
|
|
std::unique_ptr<InstrProfValueData[]> VD =
|
2016-02-09 09:02:24 +08:00
|
|
|
ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0);
|
2015-12-17 05:45:43 +08:00
|
|
|
ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
|
|
|
|
ASSERT_EQ(Max, VD[0].Count);
|
2015-11-20 21:13:53 +08:00
|
|
|
}
|
|
|
|
|
2016-01-08 14:54:27 +08:00
|
|
|
// This test tests that when there are too many values
|
|
|
|
// for a given site, the merged results are properly
|
|
|
|
// truncated.
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
|
2016-01-08 14:54:27 +08:00
|
|
|
static const char caller[] = "caller";
|
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record11(caller, 0x1234, {1, 2});
|
|
|
|
NamedInstrProfRecord Record12(caller, 0x1234, {1, 2});
|
2016-01-08 14:54:27 +08:00
|
|
|
|
|
|
|
// 2 value sites.
|
|
|
|
Record11.reserveSites(IPVK_IndirectCallTarget, 2);
|
|
|
|
InstrProfValueData VD0[255];
|
|
|
|
for (int I = 0; I < 255; I++) {
|
|
|
|
VD0[I].Value = 2 * I;
|
|
|
|
VD0[I].Count = 2 * I + 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr);
|
|
|
|
Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
|
|
|
|
|
|
|
Record12.reserveSites(IPVK_IndirectCallTarget, 2);
|
|
|
|
InstrProfValueData VD1[255];
|
|
|
|
for (int I = 0; I < 255; I++) {
|
|
|
|
VD1[I].Value = 2 * I + 1;
|
|
|
|
VD1[I].Count = 2 * I + 1001;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
|
|
|
|
Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
|
|
|
|
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record11), Err);
|
2016-01-08 14:54:27 +08:00
|
|
|
// Merge profile data.
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord(std::move(Record12), Err);
|
2016-01-08 14:54:27 +08:00
|
|
|
|
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
|
2016-01-08 14:54:27 +08:00
|
|
|
std::unique_ptr<InstrProfValueData[]> VD(
|
2016-02-09 09:02:24 +08:00
|
|
|
R->getValueForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
2016-01-08 15:58:20 +08:00
|
|
|
for (unsigned I = 0; I < 255; I++) {
|
2016-01-08 14:54:27 +08:00
|
|
|
ASSERT_EQ(VD[I].Value, 509 - I);
|
|
|
|
ASSERT_EQ(VD[I].Count, 1509 - I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 08:23:49 +08:00
|
|
|
static void addValueProfData(InstrProfRecord &Record) {
|
|
|
|
Record.reserveSites(IPVK_IndirectCallTarget, 5);
|
|
|
|
InstrProfValueData VD0[] = {{uint64_t(callee1), 400},
|
|
|
|
{uint64_t(callee2), 1000},
|
|
|
|
{uint64_t(callee3), 500},
|
|
|
|
{uint64_t(callee4), 300},
|
|
|
|
{uint64_t(callee5), 100}};
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 5, nullptr);
|
|
|
|
InstrProfValueData VD1[] = {{uint64_t(callee5), 800},
|
|
|
|
{uint64_t(callee3), 1000},
|
|
|
|
{uint64_t(callee2), 2500},
|
|
|
|
{uint64_t(callee1), 1300}};
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 1, VD1, 4, nullptr);
|
|
|
|
InstrProfValueData VD2[] = {{uint64_t(callee6), 800},
|
|
|
|
{uint64_t(callee3), 1000},
|
|
|
|
{uint64_t(callee4), 5500}};
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
|
|
|
|
InstrProfValueData VD3[] = {{uint64_t(callee2), 1800},
|
|
|
|
{uint64_t(callee3), 2000}};
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 3, VD3, 2, nullptr);
|
|
|
|
Record.addValueData(IPVK_IndirectCallTarget, 4, nullptr, 0, nullptr);
|
|
|
|
}
|
2015-11-26 07:31:18 +08:00
|
|
|
|
2016-05-13 08:23:49 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write) {
|
2017-07-07 03:00:12 +08:00
|
|
|
InstrProfRecord SrcRecord({1ULL << 31, 2});
|
2016-05-13 08:23:49 +08:00
|
|
|
addValueProfData(SrcRecord);
|
|
|
|
std::unique_ptr<ValueProfData> VPData =
|
|
|
|
ValueProfData::serializeFrom(SrcRecord);
|
2015-11-26 07:31:18 +08:00
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
InstrProfRecord Record({1ULL << 31, 2});
|
2016-01-27 02:48:36 +08:00
|
|
|
VPData->deserializeTo(Record, nullptr);
|
2015-11-26 07:31:18 +08:00
|
|
|
|
|
|
|
// Now read data from Record and sanity check the data
|
|
|
|
ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
|
|
|
|
ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
|
|
|
|
ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
|
|
|
|
ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
|
|
|
|
ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
|
|
|
|
|
|
|
|
auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
|
|
|
|
return VD1.Count > VD2.Count;
|
|
|
|
};
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_0(
|
|
|
|
Record.getValueForSite(IPVK_IndirectCallTarget, 0));
|
2018-04-07 09:29:45 +08:00
|
|
|
llvm::sort(&VD_0[0], &VD_0[5], Cmp);
|
2015-11-26 07:31:18 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(1000U, VD_0[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(500U, VD_0[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(400U, VD_0[2].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
|
|
|
|
ASSERT_EQ(300U, VD_0[3].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
|
|
|
|
ASSERT_EQ(100U, VD_0[4].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_1(
|
|
|
|
Record.getValueForSite(IPVK_IndirectCallTarget, 1));
|
2018-04-07 09:29:45 +08:00
|
|
|
llvm::sort(&VD_1[0], &VD_1[4], Cmp);
|
2015-11-26 07:31:18 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(2500U, VD_1[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
|
|
|
|
ASSERT_EQ(1300U, VD_1[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(1000U, VD_1[2].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
|
|
|
|
ASSERT_EQ(800U, VD_1[3].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_2(
|
|
|
|
Record.getValueForSite(IPVK_IndirectCallTarget, 2));
|
2018-04-07 09:29:45 +08:00
|
|
|
llvm::sort(&VD_2[0], &VD_2[3], Cmp);
|
2015-11-26 07:31:18 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
|
|
|
|
ASSERT_EQ(5500U, VD_2[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(1000U, VD_2[1].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
|
|
|
|
ASSERT_EQ(800U, VD_2[2].Count);
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_3(
|
|
|
|
Record.getValueForSite(IPVK_IndirectCallTarget, 3));
|
2018-04-07 09:29:45 +08:00
|
|
|
llvm::sort(&VD_3[0], &VD_3[2], Cmp);
|
2015-11-26 07:31:18 +08:00
|
|
|
ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
|
|
|
|
ASSERT_EQ(2000U, VD_3[0].Count);
|
|
|
|
ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
|
|
|
|
ASSERT_EQ(1800U, VD_3[1].Count);
|
|
|
|
}
|
|
|
|
|
2016-05-13 08:23:49 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write_mapping) {
|
2016-04-10 11:32:02 +08:00
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord SrcRecord("caller", 0x1234, {1ULL << 31, 2});
|
2016-05-13 08:23:49 +08:00
|
|
|
addValueProfData(SrcRecord);
|
|
|
|
std::unique_ptr<ValueProfData> VPData =
|
|
|
|
ValueProfData::serializeFrom(SrcRecord);
|
2016-04-10 11:32:02 +08:00
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
NamedInstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
|
2016-04-10 11:32:02 +08:00
|
|
|
InstrProfSymtab Symtab;
|
|
|
|
Symtab.mapAddress(uint64_t(callee1), 0x1000ULL);
|
|
|
|
Symtab.mapAddress(uint64_t(callee2), 0x2000ULL);
|
|
|
|
Symtab.mapAddress(uint64_t(callee3), 0x3000ULL);
|
|
|
|
Symtab.mapAddress(uint64_t(callee4), 0x4000ULL);
|
|
|
|
// Missing mapping for callee5
|
|
|
|
|
2018-03-22 06:27:31 +08:00
|
|
|
VPData->deserializeTo(Record, &Symtab);
|
2016-04-10 11:32:02 +08:00
|
|
|
|
|
|
|
// Now read data from Record and sanity check the data
|
2016-05-13 08:23:49 +08:00
|
|
|
ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
|
2016-04-10 11:32:02 +08:00
|
|
|
ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
|
|
|
|
|
|
|
|
auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
|
|
|
|
return VD1.Count > VD2.Count;
|
|
|
|
};
|
|
|
|
std::unique_ptr<InstrProfValueData[]> VD_0(
|
|
|
|
Record.getValueForSite(IPVK_IndirectCallTarget, 0));
|
2018-04-07 09:29:45 +08:00
|
|
|
llvm::sort(&VD_0[0], &VD_0[5], Cmp);
|
2016-04-10 11:32:02 +08:00
|
|
|
ASSERT_EQ(VD_0[0].Value, 0x2000ULL);
|
|
|
|
ASSERT_EQ(1000U, VD_0[0].Count);
|
|
|
|
ASSERT_EQ(VD_0[1].Value, 0x3000ULL);
|
|
|
|
ASSERT_EQ(500U, VD_0[1].Count);
|
|
|
|
ASSERT_EQ(VD_0[2].Value, 0x1000ULL);
|
|
|
|
ASSERT_EQ(400U, VD_0[2].Count);
|
|
|
|
|
|
|
|
// callee5 does not have a mapped value -- default to 0.
|
|
|
|
ASSERT_EQ(VD_0[4].Value, 0ULL);
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_max_function_count) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {1ULL << 31, 2}}, Err);
|
|
|
|
Writer.addRecord({"bar", 0, {1ULL << 63}}, Err);
|
|
|
|
Writer.addRecord({"baz", 0x5678, {0, 0, 0, 0}}, Err);
|
2015-02-18 09:58:17 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {1, 2}}, 3, Err);
|
|
|
|
Writer.addRecord({"foo", 0x1235, {3, 4}}, 5, Err);
|
2015-12-16 01:37:09 +08:00
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
std::vector<uint64_t> Counts;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1234, Counts),
|
|
|
|
Succeeded());
|
2015-12-16 01:37:09 +08:00
|
|
|
ASSERT_EQ(2U, Counts.size());
|
|
|
|
ASSERT_EQ(3U, Counts[0]);
|
|
|
|
ASSERT_EQ(6U, Counts[1]);
|
|
|
|
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1235, Counts),
|
|
|
|
Succeeded());
|
2015-12-16 01:37:09 +08:00
|
|
|
ASSERT_EQ(2U, Counts.size());
|
|
|
|
ASSERT_EQ(15U, Counts[0]);
|
|
|
|
ASSERT_EQ(20U, Counts[1]);
|
|
|
|
}
|
|
|
|
|
2016-02-09 13:47:08 +08:00
|
|
|
// Testing symtab creator interface used by indexed profile reader.
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) {
|
2015-12-19 15:44:57 +08:00
|
|
|
std::vector<StringRef> FuncNames;
|
|
|
|
FuncNames.push_back("func1");
|
|
|
|
FuncNames.push_back("func2");
|
|
|
|
FuncNames.push_back("func3");
|
|
|
|
FuncNames.push_back("bar1");
|
|
|
|
FuncNames.push_back("bar2");
|
|
|
|
FuncNames.push_back("bar3");
|
|
|
|
InstrProfSymtab Symtab;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Symtab.create(FuncNames), Succeeded());
|
2015-12-19 15:44:57 +08:00
|
|
|
StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
|
|
|
|
ASSERT_EQ(StringRef("func1"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
|
|
|
|
ASSERT_EQ(StringRef("func2"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
|
|
|
|
ASSERT_EQ(StringRef("func3"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
|
|
|
|
ASSERT_EQ(StringRef("bar1"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
|
|
|
|
ASSERT_EQ(StringRef("bar2"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
|
|
|
|
ASSERT_EQ(StringRef("bar3"), R);
|
2015-12-20 02:20:09 +08:00
|
|
|
|
2016-02-10 14:36:55 +08:00
|
|
|
// negative tests
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar4"));
|
|
|
|
ASSERT_EQ(StringRef(), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("foo4"));
|
|
|
|
ASSERT_EQ(StringRef(), R);
|
|
|
|
|
2015-12-20 02:20:09 +08:00
|
|
|
// Now incrementally update the symtab
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Symtab.addFuncName("blah_1"), Succeeded());
|
|
|
|
EXPECT_THAT_ERROR(Symtab.addFuncName("blah_2"), Succeeded());
|
|
|
|
EXPECT_THAT_ERROR(Symtab.addFuncName("blah_3"), Succeeded());
|
2015-12-20 02:20:09 +08:00
|
|
|
|
|
|
|
// Check again
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
|
|
|
|
ASSERT_EQ(StringRef("blah_1"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
|
|
|
|
ASSERT_EQ(StringRef("blah_2"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
|
|
|
|
ASSERT_EQ(StringRef("blah_3"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
|
|
|
|
ASSERT_EQ(StringRef("func1"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
|
|
|
|
ASSERT_EQ(StringRef("func2"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
|
|
|
|
ASSERT_EQ(StringRef("func3"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
|
|
|
|
ASSERT_EQ(StringRef("bar1"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
|
|
|
|
ASSERT_EQ(StringRef("bar2"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
|
|
|
|
ASSERT_EQ(StringRef("bar3"), R);
|
2015-12-19 15:44:57 +08:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:38:56 +08:00
|
|
|
// Test that we get an error when creating a bogus symtab.
|
|
|
|
TEST_P(MaybeSparseInstrProfTest, instr_prof_bogus_symtab_empty_func_name) {
|
|
|
|
InstrProfSymtab Symtab;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_TRUE(ErrorEquals(instrprof_error::malformed, Symtab.addFuncName("")));
|
2017-06-20 09:38:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-09 13:47:08 +08:00
|
|
|
// Testing symtab creator interface used by value profile transformer.
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) {
|
2016-01-20 09:26:34 +08:00
|
|
|
LLVMContext Ctx;
|
|
|
|
std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx);
|
|
|
|
FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get());
|
|
|
|
Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get());
|
|
|
|
Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get());
|
|
|
|
Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get());
|
|
|
|
Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get());
|
|
|
|
Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get());
|
|
|
|
Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get());
|
|
|
|
Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get());
|
|
|
|
Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get());
|
|
|
|
Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get());
|
|
|
|
Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get());
|
|
|
|
Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get());
|
|
|
|
|
|
|
|
InstrProfSymtab ProfSymtab;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(ProfSymtab.create(*M), Succeeded());
|
2016-01-20 09:26:34 +08:00
|
|
|
|
|
|
|
StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar",
|
|
|
|
"Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"};
|
|
|
|
|
|
|
|
for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) {
|
|
|
|
Function *F = M->getFunction(Funcs[I]);
|
2016-01-27 02:48:36 +08:00
|
|
|
ASSERT_TRUE(F != nullptr);
|
2016-01-20 10:49:53 +08:00
|
|
|
std::string PGOName = getPGOFuncName(*F);
|
2016-01-23 02:13:34 +08:00
|
|
|
uint64_t Key = IndexedInstrProf::ComputeHash(PGOName);
|
2016-01-20 10:49:53 +08:00
|
|
|
ASSERT_EQ(StringRef(PGOName),
|
2016-01-23 02:13:34 +08:00
|
|
|
ProfSymtab.getFuncName(Key));
|
|
|
|
ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key));
|
2016-01-20 09:26:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 13:47:08 +08:00
|
|
|
// Testing symtab serialization and creator/deserialization interface
|
|
|
|
// used by coverage map reader, and raw profile reader.
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
|
2015-12-31 15:57:16 +08:00
|
|
|
std::vector<std::string> FuncNames1;
|
|
|
|
std::vector<std::string> FuncNames2;
|
2016-02-09 13:36:57 +08:00
|
|
|
for (int I = 0; I < 3; I++) {
|
2015-12-31 15:57:16 +08:00
|
|
|
std::string str;
|
|
|
|
raw_string_ostream OS(str);
|
|
|
|
OS << "func_" << I;
|
|
|
|
FuncNames1.push_back(OS.str());
|
|
|
|
str.clear();
|
2016-03-29 05:06:42 +08:00
|
|
|
OS << "f oooooooooooooo_" << I;
|
2015-12-31 15:57:16 +08:00
|
|
|
FuncNames1.push_back(OS.str());
|
|
|
|
str.clear();
|
|
|
|
OS << "BAR_" << I;
|
|
|
|
FuncNames2.push_back(OS.str());
|
|
|
|
str.clear();
|
|
|
|
OS << "BlahblahBlahblahBar_" << I;
|
|
|
|
FuncNames2.push_back(OS.str());
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:29:15 +08:00
|
|
|
for (bool DoCompression : {false, true}) {
|
|
|
|
// Compressing:
|
|
|
|
std::string FuncNameStrings1;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(collectPGOFuncNameStrings(
|
|
|
|
FuncNames1, (DoCompression && zlib::isAvailable()),
|
|
|
|
FuncNameStrings1),
|
|
|
|
Succeeded());
|
2016-01-30 06:29:15 +08:00
|
|
|
|
|
|
|
// Compressing:
|
|
|
|
std::string FuncNameStrings2;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(collectPGOFuncNameStrings(
|
|
|
|
FuncNames2, (DoCompression && zlib::isAvailable()),
|
|
|
|
FuncNameStrings2),
|
|
|
|
Succeeded());
|
2016-01-30 06:29:15 +08:00
|
|
|
|
2016-02-09 13:36:57 +08:00
|
|
|
for (int Padding = 0; Padding < 2; Padding++) {
|
|
|
|
// Join with paddings :
|
2015-12-31 15:57:16 +08:00
|
|
|
std::string FuncNameStrings = FuncNameStrings1;
|
|
|
|
for (int P = 0; P < Padding; P++) {
|
|
|
|
FuncNameStrings.push_back('\0');
|
|
|
|
}
|
|
|
|
FuncNameStrings += FuncNameStrings2;
|
|
|
|
|
2016-01-05 07:59:14 +08:00
|
|
|
// Now decompress:
|
2015-12-31 15:57:16 +08:00
|
|
|
InstrProfSymtab Symtab;
|
2017-07-08 05:02:59 +08:00
|
|
|
EXPECT_THAT_ERROR(Symtab.create(StringRef(FuncNameStrings)), Succeeded());
|
2015-12-31 15:57:16 +08:00
|
|
|
|
2016-01-05 07:59:14 +08:00
|
|
|
// Now do the checks:
|
|
|
|
// First sampling some data points:
|
2016-01-30 05:26:31 +08:00
|
|
|
StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
|
2016-01-05 07:59:14 +08:00
|
|
|
ASSERT_EQ(StringRef("func_0"), R);
|
|
|
|
R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
|
2016-03-29 05:06:42 +08:00
|
|
|
ASSERT_EQ(StringRef("f oooooooooooooo_0"), R);
|
2016-02-09 13:36:57 +08:00
|
|
|
for (int I = 0; I < 3; I++) {
|
2015-12-31 15:57:16 +08:00
|
|
|
std::string N[4];
|
|
|
|
N[0] = FuncNames1[2 * I];
|
|
|
|
N[1] = FuncNames1[2 * I + 1];
|
|
|
|
N[2] = FuncNames2[2 * I];
|
|
|
|
N[3] = FuncNames2[2 * I + 1];
|
|
|
|
for (int J = 0; J < 4; J++) {
|
|
|
|
StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
|
|
|
|
ASSERT_EQ(StringRef(N[J]), R);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 06:54:45 +08:00
|
|
|
TEST_F(SparseInstrProfTest, preserve_no_records) {
|
2017-07-10 11:04:59 +08:00
|
|
|
Writer.addRecord({"foo", 0x1234, {0}}, Err);
|
|
|
|
Writer.addRecord({"bar", 0x4321, {0, 0}}, Err);
|
2017-07-11 05:44:43 +08:00
|
|
|
Writer.addRecord({"baz", 0x4321, {0, 0, 0}}, Err);
|
2016-01-30 06:54:45 +08:00
|
|
|
|
|
|
|
auto Profile = Writer.writeBuffer();
|
|
|
|
readProfile(std::move(Profile));
|
|
|
|
|
|
|
|
auto I = Reader->begin(), E = Reader->end();
|
|
|
|
ASSERT_TRUE(I == E);
|
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest,
|
2017-06-04 13:30:26 +08:00
|
|
|
::testing::Bool(),);
|
2016-01-30 06:54:45 +08:00
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
} // end anonymous namespace
|