Skip to content

Commit 9ab133b

Browse files
[nfc][InstrProfTest]Parameterize the edge cases of value profile merge by value kind (llvm#73165)
There are three test cases to test the merge of value profiles. 'get_icall_data_merge1' tests the basic case; {get_icall_data_merge1_saturation, get_icall_data_merge_site_trunc} tests the edge case. This patch parameterizes the edge case test coverage by value kind and adds the coverage of 'IPVK_MemOPSize'. Keep the basic test structure as it is. The main reason is test data construction and test assertions is clearer for each kind in the basic test. - Using a loop for different value kinds in one test case doesn't work very well. The instr-prof-writer is stateful (e.g., keeps track of per-function profile data in a [container](https://github.com/llvm/llvm-project/blob/a9c149df7666bb2f8755794b97573134e5cfeb38/llvm/include/llvm/ProfileData/InstrProfWriter.h#L43))
1 parent dc683d2 commit 9ab133b

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) {
815815
ASSERT_EQ(1U, ValueData[3].Count);
816816
}
817817

818-
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
818+
TEST_P(MaybeSparseInstrProfTest, icall_data_merge) {
819819
static const char caller[] = "caller";
820820
NamedInstrProfRecord Record11(caller, 0x1234, {1, 2});
821821
NamedInstrProfRecord Record12(caller, 0x1234, {1, 2});
@@ -920,8 +920,18 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
920920
ASSERT_EQ(2U, VD_4[2].Count);
921921
}
922922

923-
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
923+
struct ValueProfileMergeEdgeCaseTest
924+
: public InstrProfTest,
925+
public ::testing::WithParamInterface<std::tuple<bool, uint32_t>> {
926+
void SetUp() override { Writer.setOutputSparse(std::get<0>(GetParam())); }
927+
928+
uint32_t getValueProfileKind() const { return std::get<1>(GetParam()); }
929+
};
930+
931+
TEST_P(ValueProfileMergeEdgeCaseTest, value_profile_data_merge_saturation) {
932+
const uint32_t ValueKind = getValueProfileKind();
924933
static const char bar[] = "bar";
934+
const uint64_t ProfiledValue = 0x5678;
925935

926936
const uint64_t MaxValCount = std::numeric_limits<uint64_t>::max();
927937
const uint64_t MaxEdgeCount = getInstrMaxCountValue();
@@ -944,18 +954,18 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
944954
ASSERT_EQ(Result, instrprof_error::success);
945955

946956
NamedInstrProfRecord Record4("baz", 0x5678, {3, 4});
947-
Record4.reserveSites(IPVK_IndirectCallTarget, 1);
948-
InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
949-
Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
957+
Record4.reserveSites(ValueKind, 1);
958+
InstrProfValueData VD4[] = {{ProfiledValue, 1}};
959+
Record4.addValueData(ValueKind, 0, VD4, 1, nullptr);
950960
Result = instrprof_error::success;
951961
Writer.addRecord(std::move(Record4), Err);
952962
ASSERT_EQ(Result, instrprof_error::success);
953963

954964
// Verify value data counter overflow.
955965
NamedInstrProfRecord Record5("baz", 0x5678, {5, 6});
956-
Record5.reserveSites(IPVK_IndirectCallTarget, 1);
957-
InstrProfValueData VD5[] = {{uint64_t(bar), MaxValCount}};
958-
Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
966+
Record5.reserveSites(ValueKind, 1);
967+
InstrProfValueData VD5[] = {{ProfiledValue, MaxValCount}};
968+
Record5.addValueData(ValueKind, 0, VD5, 1, nullptr);
959969
Result = instrprof_error::success;
960970
Writer.addRecord(std::move(Record5), Err);
961971
ASSERT_EQ(Result, instrprof_error::counter_overflow);
@@ -966,48 +976,48 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
966976
// Verify saturation of counts.
967977
Expected<InstrProfRecord> ReadRecord1 =
968978
Reader->getInstrProfRecord("foo", 0x1234);
969-
EXPECT_THAT_ERROR(ReadRecord1.takeError(), Succeeded());
970-
ASSERT_EQ(MaxEdgeCount, ReadRecord1->Counts[0]);
979+
ASSERT_THAT_ERROR(ReadRecord1.takeError(), Succeeded());
980+
EXPECT_EQ(MaxEdgeCount, ReadRecord1->Counts[0]);
971981

972982
Expected<InstrProfRecord> ReadRecord2 =
973983
Reader->getInstrProfRecord("baz", 0x5678);
974984
ASSERT_TRUE(bool(ReadRecord2));
975-
ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget));
985+
ASSERT_EQ(1U, ReadRecord2->getNumValueSites(ValueKind));
976986
std::unique_ptr<InstrProfValueData[]> VD =
977-
ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0);
978-
ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
979-
ASSERT_EQ(MaxValCount, VD[0].Count);
987+
ReadRecord2->getValueForSite(ValueKind, 0);
988+
EXPECT_EQ(ProfiledValue, VD[0].Value);
989+
EXPECT_EQ(MaxValCount, VD[0].Count);
980990
}
981991

982-
// This test tests that when there are too many values
983-
// for a given site, the merged results are properly
984-
// truncated.
985-
TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
992+
// This test tests that when there are too many values for a given site, the
993+
// merged results are properly truncated.
994+
TEST_P(ValueProfileMergeEdgeCaseTest, value_profile_data_merge_site_trunc) {
995+
const uint32_t ValueKind = getValueProfileKind();
986996
static const char caller[] = "caller";
987997

988998
NamedInstrProfRecord Record11(caller, 0x1234, {1, 2});
989999
NamedInstrProfRecord Record12(caller, 0x1234, {1, 2});
9901000

9911001
// 2 value sites.
992-
Record11.reserveSites(IPVK_IndirectCallTarget, 2);
1002+
Record11.reserveSites(ValueKind, 2);
9931003
InstrProfValueData VD0[255];
9941004
for (int I = 0; I < 255; I++) {
9951005
VD0[I].Value = 2 * I;
9961006
VD0[I].Count = 2 * I + 1000;
9971007
}
9981008

999-
Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr);
1000-
Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
1009+
Record11.addValueData(ValueKind, 0, VD0, 255, nullptr);
1010+
Record11.addValueData(ValueKind, 1, nullptr, 0, nullptr);
10011011

1002-
Record12.reserveSites(IPVK_IndirectCallTarget, 2);
1012+
Record12.reserveSites(ValueKind, 2);
10031013
InstrProfValueData VD1[255];
10041014
for (int I = 0; I < 255; I++) {
10051015
VD1[I].Value = 2 * I + 1;
10061016
VD1[I].Count = 2 * I + 1001;
10071017
}
10081018

1009-
Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
1010-
Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
1019+
Record12.addValueData(ValueKind, 0, VD1, 255, nullptr);
1020+
Record12.addValueData(ValueKind, 1, nullptr, 0, nullptr);
10111021

10121022
Writer.addRecord(std::move(Record11), Err);
10131023
// Merge profile data.
@@ -1017,17 +1027,23 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
10171027
readProfile(std::move(Profile));
10181028

10191029
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
1020-
EXPECT_THAT_ERROR(R.takeError(), Succeeded());
1021-
std::unique_ptr<InstrProfValueData[]> VD(
1022-
R->getValueForSite(IPVK_IndirectCallTarget, 0));
1023-
ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));
1024-
ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
1030+
ASSERT_THAT_ERROR(R.takeError(), Succeeded());
1031+
std::unique_ptr<InstrProfValueData[]> VD(R->getValueForSite(ValueKind, 0));
1032+
ASSERT_EQ(2U, R->getNumValueSites(ValueKind));
1033+
EXPECT_EQ(255U, R->getNumValueDataForSite(ValueKind, 0));
10251034
for (unsigned I = 0; I < 255; I++) {
1026-
ASSERT_EQ(VD[I].Value, 509 - I);
1027-
ASSERT_EQ(VD[I].Count, 1509 - I);
1035+
EXPECT_EQ(VD[I].Value, 509 - I);
1036+
EXPECT_EQ(VD[I].Count, 1509 - I);
10281037
}
10291038
}
10301039

1040+
INSTANTIATE_TEST_SUITE_P(
1041+
EdgeCaseTest, ValueProfileMergeEdgeCaseTest,
1042+
::testing::Combine(::testing::Bool(), /* Sparse */
1043+
::testing::Values(IPVK_IndirectCallTarget,
1044+
IPVK_MemOPSize) /* ValueKind */
1045+
));
1046+
10311047
static void addValueProfData(InstrProfRecord &Record) {
10321048
Record.reserveSites(IPVK_IndirectCallTarget, 5);
10331049
InstrProfValueData VD0[] = {{uint64_t(callee1), 400},

0 commit comments

Comments
 (0)