mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-27 00:01:18 +00:00
Add determinism checks for generated source size and fix a crash (#7917)
Benchmarks are only comparable if the source generator produces the same number of lines and bytes for a given configuration with any seed. Test this across many seeds and class shapes, and check it in the compile benchmark for the files it generates. Also check that generation emits every name in its name pools, which the byte count depends on. With field-heavy classes, generation could crash depending on the shuffle. A class becomes a valid type only after its field types are chosen, so references to the last class can only go on its own return and parameter types. When the shuffled pool left more references to it than that, `GetValidTypeName` found no valid type and failed its check. Cap the references to each class at its number of functions and methods, each of which has a return type that can use it, and replace the removed references with fixed types so the byte count stays seed-independent. Assisted-by: Claude Code
This commit is contained in:
1 parent
06b3c69b7f
commit
c578085840
3 files changed
+218
-11
No files matched your search
@@ -36,6 +36,9 @@ using ::testing::Le;
|
||||
using ::testing::MatchesRegex;
|
||||
using ::testing::SizeIs;
|
||||
using ::testing::StrEq;
|
||||
using ::testing::TestParamInfo;
|
||||
using ::testing::TestWithParam;
|
||||
using ::testing::Values;
|
||||
|
||||
// Tiny helper to sum the sizes of a range of ranges. Uses a template to avoid
|
||||
// hard coding any specific types for the two ranges.
|
||||
@@ -300,5 +303,164 @@ TEST(SourceGenTest, GenApiFileDenseDeclsCppTest) {
|
||||
EXPECT_TRUE(TestCompile(SourceGen::Language::Cpp, source));
|
||||
}
|
||||
|
||||
static auto CountLines(llvm::StringRef source) -> ssize_t {
|
||||
return llvm::count(source, '\n');
|
||||
}
|
||||
|
||||
// Matches a `SourceGen::Language` for which
|
||||
// `GenApiFileDenseDecls(target_lines, params)` generates files with the same
|
||||
// byte and line counts using each of `num_seeds` independently seeded
|
||||
// generators. The files must not all have the same content, which would make
|
||||
// that match vacuous.
|
||||
// NOLINTNEXTLINE(modernize-use-trailing-return-type): Macro based function.
|
||||
MATCHER_P3(GeneratesSeedIndependentSize, target_lines, params, num_seeds,
|
||||
"generates files of the same size with every seed") {
|
||||
SourceGen::Language language = arg;
|
||||
std::string first =
|
||||
SourceGen(language).GenApiFileDenseDecls(target_lines, params);
|
||||
bool any_different = false;
|
||||
for (int i : llvm::seq(1, num_seeds)) {
|
||||
std::string source =
|
||||
SourceGen(language).GenApiFileDenseDecls(target_lines, params);
|
||||
if (source.size() != first.size() ||
|
||||
CountLines(source) != CountLines(first)) {
|
||||
*result_listener << "where seed iteration " << i << " generated "
|
||||
<< source.size() << " bytes and " << CountLines(source)
|
||||
<< " lines, but seed iteration 0 generated "
|
||||
<< first.size() << " bytes and " << CountLines(first)
|
||||
<< " lines";
|
||||
return false;
|
||||
}
|
||||
any_different = any_different || source != first;
|
||||
}
|
||||
if (!any_different) {
|
||||
*result_listener << "where all files have the same content";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Each test generates files with one set of parameters, and checks that they
|
||||
// compile cleanly and have the same size for any seed, which benchmarks need to
|
||||
// be comparable. Each test runs for both languages.
|
||||
using GenApiFileDenseDeclsParamsTest = TestWithParam<SourceGen::Language>;
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
Languages, GenApiFileDenseDeclsParamsTest,
|
||||
Values(SourceGen::Language::Carbon, SourceGen::Language::Cpp),
|
||||
[](const TestParamInfo<SourceGen::Language>& info) -> std::string {
|
||||
return info.param == SourceGen::Language::Carbon ? "Carbon" : "Cpp";
|
||||
});
|
||||
|
||||
// Barely enough lines for one class.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, DefaultParamsAt200Lines) {
|
||||
SourceGen::DenseDeclParams params;
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(200, params, 16));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(200, params)));
|
||||
}
|
||||
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, DefaultParamsAt1000Lines) {
|
||||
SourceGen::DenseDeclParams params;
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(1000, params, 16));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(1000, params)));
|
||||
}
|
||||
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, DefaultParamsAt5000Lines) {
|
||||
SourceGen::DenseDeclParams params;
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(5000, params, 16));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(5000, params)));
|
||||
}
|
||||
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, DefaultParamsAt20000Lines) {
|
||||
SourceGen::DenseDeclParams params;
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(20000, params, 16));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(20000, params)));
|
||||
}
|
||||
|
||||
// Function declarations only: no methods and no fields.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, OnlyFunctions) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 20,
|
||||
.public_method_decls = 0,
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 0,
|
||||
.private_field_decls = 0}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(5000, params, 12));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(5000, params)));
|
||||
}
|
||||
|
||||
// Large parameter counts, which wrap onto several lines.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, WrappedParamLists) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 2,
|
||||
.public_function_decl_params = {.max_params = 16},
|
||||
.public_method_decls = 4,
|
||||
.public_method_decl_params = {.max_params = 16},
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 0,
|
||||
.private_field_decls = 0}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(5000, params, 12));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(5000, params)));
|
||||
}
|
||||
|
||||
// The default shape scaled up 2x.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, DoubledDefaultShape) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 8,
|
||||
.public_method_decls = 20,
|
||||
.private_function_decls = 4,
|
||||
.private_method_decls = 16,
|
||||
.private_field_decls = 12}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(5000, params, 12));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(5000, params)));
|
||||
}
|
||||
|
||||
// A class's fields can't reference it or any later class, so field-heavy
|
||||
// classes leave few type uses for references to a class. Whether the valid type
|
||||
// names run out depends on the shuffle, so these tests use many seeds.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, ManyFieldsAndTwoFunctions) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 1,
|
||||
.public_method_decls = 1,
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 0,
|
||||
.private_field_decls = 30}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(3000, params, 32));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(3000, params)));
|
||||
}
|
||||
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, ManyFieldsAndOneMethod) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 0,
|
||||
.public_method_decls = 1,
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 0,
|
||||
.private_field_decls = 50}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(3000, params, 32));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(3000, params)));
|
||||
}
|
||||
|
||||
// No functions or methods, so every type use is a fixed type.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, OnlyFields) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 0,
|
||||
.public_method_decls = 0,
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 0,
|
||||
.private_field_decls = 16}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(3000, params, 32));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(3000, params)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
Reference in new issue
Block a user