mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
The big addition here is a very, very rough and very early skeleton of a source code generator framework. This builds upon the lexers identifier synthesis logic, improving on its framework and wiring it up with the most rudimentary of source file generation. This is just enough to roughly replicate my "big API file" source code benchmarks. The source generation works *very* hard to both vary the structure and content of the source as much as possible while ensuring the same *total* amount of each construct is in use, from bytes in identifiers to line breaks, parameters, etc. This lets us generate randomly structure inputs that should consistently take the exact same amount of total work to compile. The complex identifier synthesis logic from the lexer's benchmark is moved over here and the lexer uses APIs in the source generator for identifiers. The other source synthesis in the lexer's benchmark isn't yet moved over, but should likely be slowly absorbed here as it can be refactored into a more principled and re-usable form. Some bits may stay of course if they're just too lexer-specific. Next, this adds a simple end-to-end compile benchmark for the driver that directly and much more clearly reproduces all the measurements I've done manually up until now. It should also be easy to extend to more patterns over time as we add support to the source generator to produce those patterns. Last but not least, I've added a tiny CLI to the source generator so that you can generate source code manually. This is especially nice for generating demo source code to actually run through the driver or look at in an editor. The CLI can also generate C++ source code which lets us do some minimal comparative benchmarking between Carbon and C++/Clang. There are huge number of TODOs in the source generation framework. This is going to be a large ongoing effort I suspect. There are also a bunch of rough edges I've left to try and get this out for review sooner. I've left TODOs for refactorings that really need to be done here, but hoping these can maybe be follow-ups. If not, please flag and I'll try to layer them on here. Sample compile benchmark output, nicely showing where we are w.r.t. our goal speeds (2x behind on lex and check, 5x on parse) at least on a recent AMD server CPU: ``` ------------------------------------------------------------------------------------------------------ Benchmark Time CPU Iterations Lines ------------------------------------------------------------------------------------------------------ BM_CompileAPIFileDenseDecls<Phase::Lex>/256 29420 ns 29419 ns 22860 6.62847M/s BM_CompileAPIFileDenseDecls<Phase::Lex>/1024 146130 ns 146128 ns 4840 6.69959M/s BM_CompileAPIFileDenseDecls<Phase::Lex>/4096 601584 ns 601577 ns 1020 6.69573M/s BM_CompileAPIFileDenseDecls<Phase::Lex>/16384 2547578 ns 2547313 ns 280 6.404M/s BM_CompileAPIFileDenseDecls<Phase::Lex>/65536 10816591 ns 10816389 ns 80 6.05193M/s BM_CompileAPIFileDenseDecls<Phase::Lex>/262144 52191320 ns 52189828 ns 20 5.02261M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/256 101706 ns 101698 ns 6900 1.91745M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/1024 512161 ns 512162 ns 1380 1.9115M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/4096 2078426 ns 2078430 ns 340 1.938M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/16384 8795786 ns 8795583 ns 100 1.85468M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/65536 35073596 ns 35072973 ns 20 1.86639M/s BM_CompileAPIFileDenseDecls<Phase::Parse>/262144 151100688 ns 151097370 ns 20 1.73483M/s BM_CompileAPIFileDenseDecls<Phase::Check>/256 957059 ns 957049 ns 740 203.751k/s BM_CompileAPIFileDenseDecls<Phase::Check>/1024 1956134 ns 1955985 ns 360 500.515k/s BM_CompileAPIFileDenseDecls<Phase::Check>/4096 5797864 ns 5797417 ns 120 694.792k/s BM_CompileAPIFileDenseDecls<Phase::Check>/16384 21219608 ns 21217584 ns 40 768.843k/s BM_CompileAPIFileDenseDecls<Phase::Check>/65536 96311116 ns 96302334 ns 20 679.734k/s BM_CompileAPIFileDenseDecls<Phase::Check>/262144 371637963 ns 371609964 ns 20 705.387k/s ``` Lest someone think this is *bad*, the fact that we're already within 2x of our rather audacious goals makes me quite happy. =D --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com> Co-authored-by: Richard Smith <richard@metafoo.co.uk>
197 lines
7.2 KiB
C++
197 lines
7.2 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "testing/base/source_gen.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "common/set.h"
|
|
#include "testing/base/gtest_main.h"
|
|
#include "toolchain/driver/driver.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
using ::testing::AllOf;
|
|
using ::testing::ContainerEq;
|
|
using ::testing::Contains;
|
|
using ::testing::Each;
|
|
using ::testing::Eq;
|
|
using ::testing::Ge;
|
|
using ::testing::Gt;
|
|
using ::testing::Le;
|
|
using ::testing::MatchesRegex;
|
|
using ::testing::SizeIs;
|
|
|
|
// 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.
|
|
template <typename T>
|
|
static auto SumSizes(const T& range) -> ssize_t {
|
|
ssize_t sum = 0;
|
|
for (const auto& inner_range : range) {
|
|
sum += inner_range.size();
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
TEST(SourceGenTest, Identifiers) {
|
|
SourceGen gen;
|
|
|
|
auto idents = gen.GetShuffledIdentifiers(1000);
|
|
EXPECT_THAT(idents.size(), Eq(1000));
|
|
for (llvm::StringRef ident : idents) {
|
|
EXPECT_THAT(ident, MatchesRegex("[A-Za-z][A-Za-z0-9_]*"));
|
|
}
|
|
|
|
// We should have at least one identifier of each length [1, 64]. The exact
|
|
// distribution is an implementation detail designed to vaguely match the
|
|
// expected distribution in source code.
|
|
for (int size : llvm::seq_inclusive(1, 64)) {
|
|
EXPECT_THAT(idents, Contains(SizeIs(size)));
|
|
}
|
|
|
|
// Check that identifiers 4 characters or shorter are more common than longer
|
|
// lengths. This is a very rough way of double checking that we got the
|
|
// intended distribution.
|
|
for (int short_size : llvm::seq_inclusive(1, 4)) {
|
|
int short_count = llvm::count_if(idents, [&](auto ident) {
|
|
return static_cast<int>(ident.size()) == short_size;
|
|
});
|
|
for (int long_size : llvm::seq_inclusive(5, 64)) {
|
|
EXPECT_THAT(short_count, Gt(llvm::count_if(idents, [&](auto ident) {
|
|
return static_cast<int>(ident.size()) == long_size;
|
|
})));
|
|
}
|
|
}
|
|
|
|
// Check that repeated calls are different in interesting ways, but have the
|
|
// exact same total bytes.
|
|
ssize_t idents_size_sum = SumSizes(idents);
|
|
for ([[maybe_unused]] int _ : llvm::seq(10)) {
|
|
auto idents2 = gen.GetShuffledIdentifiers(1000);
|
|
EXPECT_THAT(idents2, SizeIs(1000));
|
|
// Should be (at least) a different shuffle of identifiers.
|
|
EXPECT_THAT(idents2, Not(ContainerEq(idents)));
|
|
// But the sum of lengths should be identical.
|
|
EXPECT_THAT(SumSizes(idents2), Eq(idents_size_sum));
|
|
}
|
|
|
|
// Check length constraints have the desired effect.
|
|
idents =
|
|
gen.GetShuffledIdentifiers(1000, /*min_length=*/10, /*max_length=*/20);
|
|
EXPECT_THAT(idents, Each(SizeIs(AllOf(Ge(10), Le(20)))));
|
|
}
|
|
|
|
TEST(SourceGenTest, UniformIdentifiers) {
|
|
SourceGen gen;
|
|
// Check that uniform identifier length results in exact coverage of each
|
|
// possible length for an easy case, both without and with a remainder.
|
|
auto idents =
|
|
gen.GetShuffledIdentifiers(100, /*min_length=*/10, /*max_length=*/19,
|
|
/*uniform=*/true);
|
|
EXPECT_THAT(idents, Contains(SizeIs(10)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(11)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(12)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(13)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(14)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(15)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(16)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(17)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(18)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(19)).Times(10));
|
|
|
|
idents = gen.GetShuffledIdentifiers(97, /*min_length=*/10, /*max_length=*/19,
|
|
/*uniform=*/true);
|
|
EXPECT_THAT(idents, Contains(SizeIs(10)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(11)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(12)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(13)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(14)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(15)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(16)).Times(10));
|
|
EXPECT_THAT(idents, Contains(SizeIs(17)).Times(9));
|
|
EXPECT_THAT(idents, Contains(SizeIs(18)).Times(9));
|
|
EXPECT_THAT(idents, Contains(SizeIs(19)).Times(9));
|
|
}
|
|
|
|
// Largely covered by `Identifiers` and `UniformIdentifiers`, but need to check
|
|
// for uniqueness specifically.
|
|
TEST(SourceGenTest, UniqueIdentifiers) {
|
|
SourceGen gen;
|
|
|
|
auto unique = gen.GetShuffledUniqueIdentifiers(1000);
|
|
EXPECT_THAT(unique.size(), Eq(1000));
|
|
Set<llvm::StringRef> set;
|
|
for (llvm::StringRef ident : unique) {
|
|
EXPECT_THAT(ident, MatchesRegex("[A-Za-z][A-Za-z0-9_]*"));
|
|
EXPECT_TRUE(set.Insert(ident).is_inserted())
|
|
<< "Colliding identifier: " << ident;
|
|
}
|
|
|
|
// Check single length specifically where uniqueness is the most challenging.
|
|
set.Clear();
|
|
unique = gen.GetShuffledUniqueIdentifiers(1000, /*min_length=*/4,
|
|
/*max_length=*/4);
|
|
for (llvm::StringRef ident : unique) {
|
|
EXPECT_TRUE(set.Insert(ident).is_inserted())
|
|
<< "Colliding identifier: " << ident;
|
|
}
|
|
}
|
|
|
|
// Check that the source code doesn't have compiler errors.
|
|
auto TestCompile(llvm::StringRef source) -> bool {
|
|
llvm::vfs::InMemoryFileSystem fs;
|
|
InstallPaths installation(
|
|
InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath()));
|
|
Driver driver(fs, &installation, llvm::outs(), llvm::errs());
|
|
|
|
// Load the prelude into our VFS.
|
|
//
|
|
// TODO: Factor this and analogous code in file_test into a Driver helper.
|
|
auto prelude =
|
|
Driver::FindPreludeFiles(installation.core_package(), llvm::errs());
|
|
CARBON_CHECK(!prelude.empty());
|
|
for (const auto& path : prelude) {
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file =
|
|
llvm::MemoryBuffer::getFile(path);
|
|
CARBON_CHECK(file) << file.getError().message();
|
|
CARBON_CHECK(fs.addFile(path, /*ModificationTime=*/0, std::move(*file)))
|
|
<< "Duplicate file: " << path;
|
|
}
|
|
|
|
fs.addFile("test.carbon", /*ModificationTime=*/0,
|
|
llvm::MemoryBuffer::getMemBuffer(source));
|
|
return driver.RunCommand({"compile", "--phase=check", "test.carbon"}).success;
|
|
}
|
|
|
|
TEST(SourceGenTest, GenAPIFileDenseDeclsTest) {
|
|
SourceGen gen;
|
|
|
|
std::string source =
|
|
gen.GenAPIFileDenseDecls(1000, SourceGen::DenseDeclParams{});
|
|
// Should be within 1% of the requested line count.
|
|
EXPECT_THAT(source, Contains('\n').Times(AllOf(Ge(950), Le(1050))));
|
|
|
|
// Make sure we generated valid Carbon code.
|
|
EXPECT_TRUE(TestCompile(source));
|
|
}
|
|
|
|
TEST(SourceGenTest, GenAPIFileDenseDeclsCppTest) {
|
|
SourceGen gen(SourceGen::Language::Cpp);
|
|
|
|
// Generate a 1000-line file which is enough to have a reasonably accurate
|
|
// line count estimate and have a few classes.
|
|
std::string source =
|
|
gen.GenAPIFileDenseDecls(1000, SourceGen::DenseDeclParams{});
|
|
// Should be within 10% of the requested line count.
|
|
EXPECT_THAT(source, Contains('\n').Times(AllOf(Ge(900), Le(1100))));
|
|
|
|
// TODO: When the driver supports compiling C++ code as easily as Carbon, we
|
|
// should test that the generated C++ code is valid.
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|