mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 11:10:10 +01:00
This switches `DCHECK` and `FATAL` as well. The goal is to reduce the code size impact of these assertions so that we can keep more of them enabled. Currently, the largest cost I see from `CHECK` is not the actual check or the cold code itself, but actually the failure to inline trivial functions due to the presence of the cold code. This means that our goal isn't to reduce apparent code size in the final binary but the LLVM IR cost assessed for these routines in the inliner, which closely correlates with code size but is a bit different. As discussed in #4283, experimentation shows that a single function call with a minimal number of arguments is the lowest cost model for these. This is easily achieved with a format-string API that internally uses `llvm::formatv`. This PR is essentially the `CHECK` version of #4283. However, the check macros are substantially harder to make work with both format strings and streaming because they also take a condition. Also, unexpectedly, I was very successful at devising a regular expression based automated rewrite from the streaming to the format string form with only low 10s of manual fixes. This includes compacting strings broken up across lines, etc. Given how well that went, I've prepared this PR which just directly switches to the format string API and migrate everything to use it. One nice side-effect is that the format string approach ends up greatly simplifying the implementation here as well. This is ... *shockingly* effective. Parsing speeds up by more than 3% with just this change. And checking speeds up by **8%** with this change alone: ``` BM_CompileAPIFileDenseDecls<Phase::Parse>/256 86.3µs ± 1% 82.9µs ± 1% -3.94% (p=0.000 n=17+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/1024 431µs ± 1% 415µs ± 1% -3.76% (p=0.000 n=18+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/4096 1.77ms ± 1% 1.71ms ± 1% -3.18% (p=0.000 n=18+19) BM_CompileAPIFileDenseDecls<Phase::Parse>/16384 7.44ms ± 1% 7.17ms ± 2% -3.56% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/65536 30.7ms ± 1% 29.7ms ± 1% -3.15% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Parse>/262144 131ms ± 1% 127ms ± 1% -2.81% (p=0.000 n=18+18) BM_CompileAPIFileDenseDecls<Phase::Check>/256 878µs ± 2% 800µs ± 1% -8.91% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/1024 1.88ms ± 2% 1.72ms ± 1% -8.56% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/4096 5.78ms ± 2% 5.28ms ± 1% -8.70% (p=0.000 n=20+18) BM_CompileAPIFileDenseDecls<Phase::Check>/16384 21.9ms ± 1% 20.1ms ± 1% -8.02% (p=0.000 n=18+20) BM_CompileAPIFileDenseDecls<Phase::Check>/65536 90.4ms ± 2% 83.1ms ± 1% -8.04% (p=0.000 n=19+20) BM_CompileAPIFileDenseDecls<Phase::Check>/262144 381ms ± 2% 352ms ± 1% -7.79% (p=0.000 n=19+19) ``` --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk> Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
229 lines
9.8 KiB
C++
229 lines
9.8 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 "toolchain/check/context.h"
|
|
#include "toolchain/check/convert.h"
|
|
#include "toolchain/check/handle.h"
|
|
#include "toolchain/check/return.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
|
|
bool is_generic) -> bool {
|
|
auto [type_node, parsed_type_id] = context.node_stack().PopExprWithNodeId();
|
|
auto cast_type_id = ExprAsType(context, type_node, parsed_type_id);
|
|
|
|
// TODO: Handle `_` bindings.
|
|
|
|
// Every other kind of pattern binding has a name.
|
|
auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
|
|
|
|
// Determine whether we're handling an associated constant. These share the
|
|
// syntax for a compile-time binding, but don't behave like other compile-time
|
|
// bindings.
|
|
// TODO: Consider using a different parse node kind to make this easier.
|
|
bool is_associated_constant = false;
|
|
if (is_generic) {
|
|
auto inst_id = context.scope_stack().PeekInstId();
|
|
is_associated_constant =
|
|
inst_id.is_valid() && context.insts().Is<SemIR::InterfaceDecl>(inst_id);
|
|
}
|
|
|
|
// Create the appropriate kind of binding for this pattern.
|
|
auto make_bind_name = [&](SemIR::TypeId type_id,
|
|
SemIR::InstId value_id) -> SemIR::LocIdAndInst {
|
|
// TODO: Eventually the name will need to support associations with other
|
|
// scopes, but right now we don't support qualified names here.
|
|
auto entity_name_id = context.entity_names().Add(
|
|
{.name_id = name_id,
|
|
.parent_scope_id = context.scope_stack().PeekNameScopeId(),
|
|
// TODO: Don't allocate a compile-time binding index for an associated
|
|
// constant declaration.
|
|
.bind_index = is_generic && !is_associated_constant
|
|
? context.scope_stack().AddCompileTimeBinding()
|
|
: SemIR::CompileTimeBindIndex::Invalid});
|
|
if (is_generic) {
|
|
// TODO: Create a `BindTemplateName` instead inside a `template` pattern.
|
|
return SemIR::LocIdAndInst(
|
|
name_node, SemIR::BindSymbolicName{.type_id = type_id,
|
|
.entity_name_id = entity_name_id,
|
|
.value_id = value_id});
|
|
} else {
|
|
return SemIR::LocIdAndInst(
|
|
name_node, SemIR::BindName{.type_id = type_id,
|
|
.entity_name_id = entity_name_id,
|
|
.value_id = value_id});
|
|
}
|
|
};
|
|
|
|
// Push the binding onto the node stack and, if necessary, onto the scope
|
|
// stack.
|
|
auto push_bind_name = [&](SemIR::InstId bind_id) {
|
|
context.node_stack().Push(node_id, bind_id);
|
|
if (is_generic && !is_associated_constant) {
|
|
context.scope_stack().PushCompileTimeBinding(bind_id);
|
|
}
|
|
};
|
|
|
|
// A `self` binding can only appear in an implicit parameter list.
|
|
if (name_id == SemIR::NameId::SelfValue &&
|
|
!context.node_stack().PeekIs<Parse::NodeKind::ImplicitParamListStart>()) {
|
|
CARBON_DIAGNOSTIC(
|
|
SelfOutsideImplicitParamList, Error,
|
|
"`self` can only be declared in an implicit parameter list.");
|
|
context.emitter().Emit(node_id, SelfOutsideImplicitParamList);
|
|
}
|
|
|
|
// Allocate an instruction of the appropriate kind, linked to the name for
|
|
// error locations.
|
|
// TODO: The node stack is a fragile way of getting context information.
|
|
// Get this information from somewhere else.
|
|
switch (auto context_node_kind = context.node_stack().PeekNodeKind()) {
|
|
case Parse::NodeKind::ReturnedModifier:
|
|
case Parse::NodeKind::VariableIntroducer: {
|
|
if (is_generic) {
|
|
CARBON_DIAGNOSTIC(
|
|
CompileTimeBindingInVarDecl, Error,
|
|
"`var` declaration cannot declare a compile-time binding.");
|
|
context.emitter().Emit(type_node, CompileTimeBindingInVarDecl);
|
|
}
|
|
auto binding_id =
|
|
is_generic
|
|
? Parse::NodeId::Invalid
|
|
: context.parse_tree().As<Parse::BindingPatternId>(node_id);
|
|
|
|
// A `var` declaration at class scope introduces a field.
|
|
auto parent_class_decl = context.GetCurrentScopeAs<SemIR::ClassDecl>();
|
|
cast_type_id = context.AsCompleteType(cast_type_id, [&] {
|
|
CARBON_DIAGNOSTIC(IncompleteTypeInVarDecl, Error,
|
|
"{0} has incomplete type `{1}`.", llvm::StringLiteral,
|
|
SemIR::TypeId);
|
|
return context.emitter().Build(type_node, IncompleteTypeInVarDecl,
|
|
parent_class_decl
|
|
? llvm::StringLiteral("Field")
|
|
: llvm::StringLiteral("Variable"),
|
|
cast_type_id);
|
|
});
|
|
if (parent_class_decl) {
|
|
CARBON_CHECK(context_node_kind == Parse::NodeKind::VariableIntroducer,
|
|
"`returned var` at class scope");
|
|
auto& class_info = context.classes().Get(parent_class_decl->class_id);
|
|
auto field_type_id = context.GetUnboundElementType(
|
|
class_info.self_type_id, cast_type_id);
|
|
auto field_id = context.AddInst<SemIR::FieldDecl>(
|
|
binding_id,
|
|
{.type_id = field_type_id,
|
|
.name_id = name_id,
|
|
.index = SemIR::ElementIndex(context.args_type_info_stack()
|
|
.PeekCurrentBlockContents()
|
|
.size())});
|
|
|
|
// Add a corresponding field to the object representation of the class.
|
|
context.args_type_info_stack().AddInstId(
|
|
context.AddInstInNoBlock<SemIR::StructTypeField>(
|
|
binding_id,
|
|
{.name_id = name_id, .field_type_id = cast_type_id}));
|
|
context.node_stack().Push(node_id, field_id);
|
|
break;
|
|
}
|
|
|
|
SemIR::InstId value_id = SemIR::InstId::Invalid;
|
|
if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
|
|
// TODO: Should we check this for the `var` as a whole, rather than for
|
|
// the name binding?
|
|
value_id =
|
|
CheckReturnedVar(context, context.node_stack().PeekNodeId(),
|
|
name_node, name_id, type_node, cast_type_id);
|
|
} else {
|
|
value_id = context.AddInst<SemIR::VarStorage>(
|
|
name_node, {.type_id = cast_type_id, .name_id = name_id});
|
|
}
|
|
auto bind_id = context.AddInst(make_bind_name(cast_type_id, value_id));
|
|
push_bind_name(bind_id);
|
|
|
|
if (context_node_kind == Parse::NodeKind::ReturnedModifier) {
|
|
RegisterReturnedVar(context, bind_id);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case Parse::NodeKind::ImplicitParamListStart:
|
|
case Parse::NodeKind::TuplePatternStart: {
|
|
// Parameters can have incomplete types in a function declaration, but not
|
|
// in a function definition. We don't know which kind we have here.
|
|
// TODO: A tuple pattern can appear in other places than function
|
|
// parameters.
|
|
auto param_id = context.AddInst<SemIR::Param>(
|
|
name_node, {.type_id = cast_type_id, .name_id = name_id});
|
|
auto bind_id = context.AddInst(make_bind_name(cast_type_id, param_id));
|
|
push_bind_name(bind_id);
|
|
// TODO: Bindings should come into scope immediately in other contexts
|
|
// too.
|
|
context.AddNameToLookup(name_id, bind_id);
|
|
break;
|
|
}
|
|
|
|
case Parse::NodeKind::LetIntroducer: {
|
|
cast_type_id = context.AsCompleteType(cast_type_id, [&] {
|
|
CARBON_DIAGNOSTIC(IncompleteTypeInLetDecl, Error,
|
|
"`let` binding has incomplete type `{0}`.",
|
|
SemIR::TypeId);
|
|
return context.emitter().Build(type_node, IncompleteTypeInLetDecl,
|
|
cast_type_id);
|
|
});
|
|
// Create the instruction, but don't add it to a block until after we've
|
|
// formed its initializer.
|
|
// TODO: For general pattern parsing, we'll need to create a block to hold
|
|
// the `let` pattern before we see the initializer.
|
|
auto bind_id = context.AddPlaceholderInstInNoBlock(
|
|
make_bind_name(cast_type_id, SemIR::InstId::Invalid));
|
|
push_bind_name(bind_id);
|
|
break;
|
|
}
|
|
|
|
default:
|
|
CARBON_FATAL("Found a pattern binding in unexpected context {0}",
|
|
context_node_kind);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::BindingPatternId node_id)
|
|
-> bool {
|
|
return HandleAnyBindingPattern(context, node_id, /*is_generic=*/false);
|
|
}
|
|
|
|
auto HandleParseNode(Context& context,
|
|
Parse::CompileTimeBindingPatternId node_id) -> bool {
|
|
return HandleAnyBindingPattern(context, node_id, /*is_generic=*/true);
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::AddrId node_id) -> bool {
|
|
auto self_param_id = context.node_stack().PopPattern();
|
|
if (auto self_param =
|
|
context.insts().TryGetAs<SemIR::AnyBindName>(self_param_id);
|
|
self_param &&
|
|
context.entity_names().Get(self_param->entity_name_id).name_id ==
|
|
SemIR::NameId::SelfValue) {
|
|
// TODO: The type of an `addr_pattern` should probably be the non-pointer
|
|
// type, because that's the type that the pattern matches.
|
|
context.AddInstAndPush<SemIR::AddrPattern>(
|
|
node_id, {.type_id = self_param->type_id, .inner_id = self_param_id});
|
|
} else {
|
|
CARBON_DIAGNOSTIC(AddrOnNonSelfParam, Error,
|
|
"`addr` can only be applied to a `self` parameter.");
|
|
context.emitter().Emit(TokenOnly(node_id), AddrOnNonSelfParam);
|
|
context.node_stack().Push(node_id, self_param_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::TemplateId node_id) -> bool {
|
|
return context.TODO(node_id, "HandleTemplate");
|
|
}
|
|
|
|
} // namespace Carbon::Check
|