Commit d715a98a authored by Nico Hartmann's avatar Nico Hartmann Committed by V8 LUCI CQ

[Torque] Minimize #includes in torque-generated files (1)

This is the 1st CL in a series of CLs with the goal to significantly
reduce the number of includes in torque-generated files to reduce
the build time of Torque's output.

So far, all torque-generated builtins included all (197) other builtins
generated by Torque. This CL adds tracking of definition locations to
Torque's ImplementationVisitor that enables it to only include those
builtins that are actually needed for compilation. This change reduces
the number of includes for the majority of builtins by 150-180 files.

Bug: v8:11528

Change-Id: Id6f6dd60c9c0f197d14998e580f0fa5fbd60f194
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2876859Reviewed-by: 's avatarSeth Brenith <seth.brenith@microsoft.com>
Commit-Queue: Nico Hartmann <nicohartmann@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74443}
parent 22bacf9a
......@@ -79,6 +79,8 @@ class GlobalContext : public ContextualClass<GlobalContext> {
std::stringstream class_definition_inline_headerfile;
std::stringstream class_definition_ccfile;
std::set<SourceId> required_builtin_includes;
};
static PerFileStreams& GeneratedPerFile(SourceId file) {
PerFileStreams& result = Get().generated_per_file_[file];
......
......@@ -17,6 +17,7 @@
#include "src/torque/global-context.h"
#include "src/torque/parameter-difference.h"
#include "src/torque/server-data.h"
#include "src/torque/source-positions.h"
#include "src/torque/type-inference.h"
#include "src/torque/type-visitor.h"
#include "src/torque/types.h"
......@@ -26,6 +27,10 @@ namespace v8 {
namespace internal {
namespace torque {
namespace {
const char* BuiltinIncludesMarker = "// __BUILTIN_INCLUDES_MARKER__\n";
} // namespace
VisitResult ImplementationVisitor::Visit(Expression* expr) {
CurrentSourcePosition::Scope scope(expr->pos);
switch (expr->kind) {
......@@ -76,11 +81,17 @@ void ImplementationVisitor::BeginGeneratedFiles() {
out << "#include " << StringLiteralQuote(include_path) << "\n";
}
for (SourceId file : SourceFileMap::AllSources()) {
out << "#include \"torque-generated/" +
SourceFileMap::PathFromV8RootWithoutExtension(file) +
"-tq-csa.h\"\n";
}
out << "// Required Builtins:\n";
out << "#include \"torque-generated/" +
SourceFileMap::PathFromV8RootWithoutExtension(file) +
"-tq-csa.h\"\n";
// Now that required include files are collected while generting the file,
// we only know the full set at the end. Insert a marker here that is
// replaced with the list of includes at the very end.
// TODO(nicohartmann@): This is not the most beautiful way to do this,
// replace once the cpp file builder is available, where this can be
// handled easily.
out << BuiltinIncludesMarker;
out << "\n";
out << "namespace v8 {\n"
......@@ -1737,7 +1748,23 @@ void ImplementationVisitor::GenerateImplementation(const std::string& dir) {
GlobalContext::PerFileStreams& streams =
GlobalContext::GeneratedPerFile(file);
WriteFile(base_filename + "-tq-csa.cc", streams.csa_ccfile.str());
std::string csa_cc = streams.csa_ccfile.str();
// Insert missing builtin includes where the marker is.
{
auto pos = csa_cc.find(BuiltinIncludesMarker);
CHECK_NE(pos, std::string::npos);
std::string includes;
for (const SourceId& include : streams.required_builtin_includes) {
std::string include_file =
SourceFileMap::PathFromV8RootWithoutExtension(include);
includes += "#include \"torque-generated/";
includes += include_file;
includes += "-tq-csa.h\"\n";
}
csa_cc.replace(pos, strlen(BuiltinIncludesMarker), std::move(includes));
}
WriteFile(base_filename + "-tq-csa.cc", std::move(csa_cc));
WriteFile(base_filename + "-tq-csa.h", streams.csa_headerfile.str());
WriteFile(base_filename + "-tq.inc",
streams.class_definition_headerfile.str());
......@@ -2362,6 +2389,10 @@ LocationReference ImplementationVisitor::GetLocationReference(
}
}
Value* value = Declarations::LookupValue(name);
CHECK(value->Position().source.IsValid());
if (auto stream = CurrentFileStreams::Get()) {
stream->required_builtin_includes.insert(value->Position().source);
}
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(expr->name->pos, value->name()->pos);
}
......@@ -2617,6 +2648,11 @@ VisitResult ImplementationVisitor::GenerateCall(
Callable* callable, base::Optional<LocationReference> this_reference,
Arguments arguments, const TypeVector& specialization_types,
bool is_tailcall) {
CHECK(callable->Position().source.IsValid());
if (auto stream = CurrentFileStreams::Get()) {
stream->required_builtin_includes.insert(callable->Position().source);
}
const Type* return_type = callable->signature().return_type;
if (is_tailcall) {
......@@ -3122,8 +3158,7 @@ VisitResult ImplementationVisitor::Visit(CallMethodExpression* expr) {
TypeVector argument_types = arguments.parameters.ComputeTypeVector();
DCHECK_EQ(expr->method->namespace_qualification.size(), 0);
QualifiedName qualified_name = QualifiedName(method_name);
Callable* callable = nullptr;
callable = LookupMethod(method_name, target_type, arguments, {});
Callable* callable = LookupMethod(method_name, target_type, arguments, {});
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::AddDefinition(expr->method->name->pos,
callable->IdentifierPosition());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment