49 changes: 18 additions & 31 deletions llvm/unittests/ProfileData/CoverageMappingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,10 @@ TEST_P(CoverageMappingTest, correct_deserialize_for_more_than_two_files) {
}
}

static const auto Err = [](Error E) { FAIL(); };

TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {0}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {0}}, Err);

const char *FileNames[] = {"bar", "baz", "foo"};
static const unsigned N = array_lengthof(FileNames);
Expand All @@ -326,17 +327,15 @@ TEST_P(CoverageMappingTest, load_coverage_for_more_than_two_files) {
}

TEST_P(CoverageMappingTest, load_coverage_with_bogus_function_name) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"", 0x1234, {10}}), Succeeded());
ProfileWriter.addRecord({"", 0x1234, {10}}, Err);
startFunction("", 0x1234);
addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
EXPECT_TRUE(ErrorEquals(coveragemap_error::malformed, loadCoverageMapping()));
}

TEST_P(CoverageMappingTest, load_coverage_for_several_functions) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func1", 0x1234, {10}}),
Succeeded());
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func2", 0x2345, {20}}),
Succeeded());
ProfileWriter.addRecord({"func1", 0x1234, {10}}, Err);
ProfileWriter.addRecord({"func2", 0x2345, {20}}, Err);

startFunction("func1", 0x1234);
addCMR(Counter::getCounter(0), "foo", 1, 1, 5, 5);
Expand Down Expand Up @@ -380,8 +379,7 @@ TEST_P(CoverageMappingTest, expansion_gets_first_counter) {
}

TEST_P(CoverageMappingTest, basic_coverage_iteration) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {30, 20, 10, 0}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand Down Expand Up @@ -429,8 +427,7 @@ TEST_P(CoverageMappingTest, uncovered_function_with_mapping) {
}

TEST_P(CoverageMappingTest, combine_regions) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {10, 20, 30}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand All @@ -448,8 +445,7 @@ TEST_P(CoverageMappingTest, combine_regions) {
}

TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {10, 20, 40}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand All @@ -469,10 +465,8 @@ TEST_P(CoverageMappingTest, restore_combined_counter_after_nested_region) {
// If CodeRegions and ExpansionRegions cover the same area,
// only counts of CodeRegions should be used.
TEST_P(CoverageMappingTest, dont_combine_expansions) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10, 20}}),
Succeeded());
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {0, 0}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {10, 20}}, Err);
ProfileWriter.addRecord({"func", 0x1234, {0, 0}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand All @@ -492,8 +486,7 @@ TEST_P(CoverageMappingTest, dont_combine_expansions) {

// If an area is covered only by ExpansionRegions, they should be combinated.
TEST_P(CoverageMappingTest, combine_expansions) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {2, 3, 7}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(1), "include1", 1, 1, 1, 10);
Expand All @@ -514,8 +507,7 @@ TEST_P(CoverageMappingTest, combine_expansions) {
}

TEST_P(CoverageMappingTest, strip_filename_prefix) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"file1:func", 0x1234, {0}}),
Succeeded());
ProfileWriter.addRecord({"file1:func", 0x1234, {0}}, Err);

startFunction("file1:func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand All @@ -529,8 +521,7 @@ TEST_P(CoverageMappingTest, strip_filename_prefix) {
}

TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}),
Succeeded());
ProfileWriter.addRecord({"<unknown>:func", 0x1234, {0}}, Err);

startFunction("<unknown>:func", 0x1234);
addCMR(Counter::getCounter(0), "", 1, 1, 9, 9);
Expand All @@ -544,10 +535,8 @@ TEST_P(CoverageMappingTest, strip_unknown_filename_prefix) {
}

TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"foo", 0x1234, {10}}),
Succeeded());
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"bar", 0x2345, {20}}),
Succeeded());
ProfileWriter.addRecord({"foo", 0x1234, {10}}, Err);
ProfileWriter.addRecord({"bar", 0x2345, {20}}, Err);

startFunction("foo", 0x1234);
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
Expand All @@ -565,8 +554,7 @@ TEST_P(CoverageMappingTest, dont_detect_false_instantiations) {
}

TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {10}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {10}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
Expand All @@ -582,8 +570,7 @@ TEST_P(CoverageMappingTest, load_coverage_for_expanded_file) {
}

TEST_P(CoverageMappingTest, skip_duplicate_function_record) {
EXPECT_THAT_ERROR(ProfileWriter.addRecord({"func", 0x1234, {1}}),
Succeeded());
ProfileWriter.addRecord({"func", 0x1234, {1}}, Err);

startFunction("func", 0x1234);
addCMR(Counter::getCounter(0), "file1", 1, 1, 9, 9);
Expand Down
136 changes: 70 additions & 66 deletions llvm/unittests/ProfileData/InstrProfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) {
ASSERT_TRUE(Reader->begin() == Reader->end());
}

static const auto Err = [](Error E) {
consumeError(std::move(E));
FAIL();
};

TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {1, 2, 3, 4}}),
Succeeded());
Writer.addRecord({"foo", 0x1234, {1, 2, 3, 4}}, Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand All @@ -83,8 +87,8 @@ TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) {
}

TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {1, 2}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1235, {3, 4}}), Succeeded());
Writer.addRecord({"foo", 0x1234, {1, 2}}, Err);
Writer.addRecord({"foo", 0x1235, {3, 4}}, Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand All @@ -108,8 +112,8 @@ TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
}

TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {1, 2}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1235, {3, 4}}), Succeeded());
Writer.addRecord({"foo", 0x1234, {1, 2}}, Err);
Writer.addRecord({"foo", 0x1235, {3, 4}}, Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand All @@ -135,15 +139,15 @@ TEST_P(MaybeSparseInstrProfTest, get_function_counts) {

// Profile data is copied from general.proftext
TEST_F(InstrProfTest, get_profile_summary) {
EXPECT_THAT_ERROR(Writer.addRecord({"func1", 0x1234, {97531}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"func2", 0x1234, {0, 0}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"func3",
0x1234,
{2305843009213693952, 1152921504606846976,
576460752303423488, 288230376151711744,
144115188075855872, 72057594037927936}}),
Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"func4", 0x1234, {0}}), Succeeded());
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);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand Down Expand Up @@ -194,13 +198,12 @@ TEST_F(InstrProfTest, get_profile_summary) {
}

TEST_F(InstrProfTest, test_writer_merge) {
EXPECT_THAT_ERROR(Writer.addRecord({"func1", 0x1234, {42}}), Succeeded());
Writer.addRecord({"func1", 0x1234, {42}}, Err);

InstrProfWriter Writer2;
EXPECT_THAT_ERROR(Writer2.addRecord({"func2", 0x1234, {0, 0}}), Succeeded());
Writer2.addRecord({"func2", 0x1234, {0, 0}}, Err);

EXPECT_THAT_ERROR(Writer.mergeRecordsFromWriter(std::move(Writer2)),
Succeeded());
Writer.mergeRecordsFromWriter(std::move(Writer2), Err);

auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));
Expand Down Expand Up @@ -239,10 +242,10 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);

EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record1)), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee1", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee2", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee3", 0x1235, {3, 4}}), Succeeded());
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);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand Down Expand Up @@ -274,7 +277,7 @@ TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) {
InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}, {5000, 5},
{4000, 4}, {6000, 6}};
Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 6, nullptr);
EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record)), Succeeded());
Writer.addRecord(std::move(Record), Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));
Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
Expand Down Expand Up @@ -379,10 +382,10 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) {
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);

EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record1), 10), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee1", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee2", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee3", 0x1235, {3, 4}}), Succeeded());
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);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand Down Expand Up @@ -422,10 +425,10 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) {
InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);

EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record1)), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee1", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee2", 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"callee3", 0x1235, {3, 4}}), Succeeded());
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);

// Set big endian output.
Writer.setValueProfDataEndianness(support::big);
Expand Down Expand Up @@ -501,15 +504,15 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
{uint64_t(callee3), 3}};
Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);

EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record11)), Succeeded());
Writer.addRecord(std::move(Record11), Err);
// Merge profile data.
EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record12)), Succeeded());
Writer.addRecord(std::move(Record12), Err);

EXPECT_THAT_ERROR(Writer.addRecord({callee1, 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({callee2, 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({callee3, 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({callee3, 0x1235, {3, 4}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({callee4, 0x1235, {3, 5}}), Succeeded());
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);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand Down Expand Up @@ -564,35 +567,37 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {

const uint64_t Max = std::numeric_limits<uint64_t>::max();

auto Result1 = Writer.addRecord({"foo", 0x1234, {1}});
ASSERT_EQ(InstrProfError::take(std::move(Result1)),
instrprof_error::success);
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);

// Verify counter overflow.
auto Result2 = Writer.addRecord({"foo", 0x1234, {Max}});
ASSERT_EQ(InstrProfError::take(std::move(Result2)),
instrprof_error::counter_overflow);
Result = instrprof_error::success;
Writer.addRecord({"foo", 0x1234, {Max}}, Err);
ASSERT_EQ(Result, instrprof_error::counter_overflow);

auto Result3 = Writer.addRecord({bar, 0x9012, {8}});
ASSERT_EQ(InstrProfError::take(std::move(Result3)),
instrprof_error::success);
Result = instrprof_error::success;
Writer.addRecord({bar, 0x9012, {8}}, Err);
ASSERT_EQ(Result, instrprof_error::success);

NamedInstrProfRecord Record4("baz", 0x5678, {3, 4});
Record4.reserveSites(IPVK_IndirectCallTarget, 1);
InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
auto Result4 = Writer.addRecord(std::move(Record4));
ASSERT_EQ(InstrProfError::take(std::move(Result4)),
instrprof_error::success);
Result = instrprof_error::success;
Writer.addRecord(std::move(Record4), Err);
ASSERT_EQ(Result, instrprof_error::success);

// Verify value data counter overflow.
NamedInstrProfRecord Record5("baz", 0x5678, {5, 6});
Record5.reserveSites(IPVK_IndirectCallTarget, 1);
InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
auto Result5 = Writer.addRecord(std::move(Record5));
ASSERT_EQ(InstrProfError::take(std::move(Result5)),
instrprof_error::counter_overflow);
Result = instrprof_error::success;
Writer.addRecord(std::move(Record5), Err);
ASSERT_EQ(Result, instrprof_error::counter_overflow);

auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));
Expand Down Expand Up @@ -643,9 +648,9 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);

EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record11)), Succeeded());
Writer.addRecord(std::move(Record11), Err);
// Merge profile data.
EXPECT_THAT_ERROR(Writer.addRecord(std::move(Record12)), Succeeded());
Writer.addRecord(std::move(Record12), Err);

auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));
Expand Down Expand Up @@ -790,20 +795,18 @@ TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write_mapping) {
}

TEST_P(MaybeSparseInstrProfTest, get_max_function_count) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {1ULL << 31, 2}}),
Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"bar", 0, {1ULL << 63}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"baz", 0x5678, {0, 0, 0, 0}}),
Succeeded());
Writer.addRecord({"foo", 0x1234, {1ULL << 31, 2}}, Err);
Writer.addRecord({"bar", 0, {1ULL << 63}}, Err);
Writer.addRecord({"baz", 0x5678, {0, 0, 0, 0}}, Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
}

TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {1, 2}}, 3), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1235, {3, 4}}, 5), Succeeded());
Writer.addRecord({"foo", 0x1234, {1, 2}}, 3, Err);
Writer.addRecord({"foo", 0x1235, {3, 4}}, 5, Err);
auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));

Expand Down Expand Up @@ -991,11 +994,12 @@ TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
}

TEST_F(SparseInstrProfTest, preserve_no_records) {
EXPECT_THAT_ERROR(Writer.addRecord({"foo", 0x1234, {0}}), Succeeded());
EXPECT_THAT_ERROR(Writer.addRecord({"bar", 0x4321, {0, 0}}), Succeeded());
Writer.addRecord({"foo", 0x1234, {0}}, Err);
Writer.addRecord({"bar", 0x4321, {0, 0}}, Err);
// FIXME: I'm guessing this data should be different, but the original author
// should check/update this test so it doesn't produce errors.
consumeError(Writer.addRecord({"bar", 0x4321, {0, 0, 0}}));
Writer.addRecord({"bar", 0x4321, {0, 0, 0}},
[](Error E) { consumeError(std::move(E)); });

auto Profile = Writer.writeBuffer();
readProfile(std::move(Profile));
Expand Down