mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +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>
252 lines
10 KiB
C++
252 lines
10 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 "explorer/interpreter/pattern_match.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "explorer/ast/value.h"
|
|
#include "explorer/base/arena.h"
|
|
#include "explorer/base/trace_stream.h"
|
|
#include "explorer/interpreter/action.h"
|
|
#include "explorer/interpreter/type_utils.h"
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
using llvm::cast;
|
|
using llvm::dyn_cast;
|
|
|
|
namespace Carbon {
|
|
|
|
static auto InitializePlaceholderValue(const ValueNodeView& value_node,
|
|
ExpressionResult v,
|
|
Nonnull<RuntimeScope*> bindings) {
|
|
switch (value_node.expression_category()) {
|
|
case ExpressionCategory::Reference:
|
|
if (v.expression_category() == ExpressionCategory::Value ||
|
|
v.expression_category() == ExpressionCategory::Reference) {
|
|
// Build by copying from value or reference expression.
|
|
bindings->Initialize(value_node, v.value());
|
|
} else {
|
|
// Location initialized by initializing expression, bind node to
|
|
// address.
|
|
CARBON_CHECK(v.address(),
|
|
"Missing location from initializing expression");
|
|
bindings->Bind(value_node, *v.address());
|
|
}
|
|
break;
|
|
case ExpressionCategory::Value:
|
|
if (v.expression_category() == ExpressionCategory::Value) {
|
|
// We assume values are strictly nested for now.
|
|
bindings->BindValue(value_node, v.value());
|
|
} else if (v.expression_category() == ExpressionCategory::Reference) {
|
|
// Bind the reference expression value directly.
|
|
CARBON_CHECK(v.address(), "Missing location from reference expression");
|
|
bindings->BindAndPin(value_node, *v.address());
|
|
} else {
|
|
// Location initialized by initializing expression, bind node to
|
|
// address.
|
|
CARBON_CHECK(v.address(),
|
|
"Missing location from initializing expression");
|
|
bindings->Bind(value_node, *v.address());
|
|
}
|
|
break;
|
|
case ExpressionCategory::Initializing:
|
|
CARBON_FATAL("Cannot pattern match an initializing expression");
|
|
break;
|
|
}
|
|
}
|
|
|
|
auto PatternMatch(Nonnull<const Value*> p, ExpressionResult v,
|
|
SourceLocation source_loc,
|
|
std::optional<Nonnull<RuntimeScope*>> bindings,
|
|
BindingMap& generic_args, Nonnull<TraceStream*> trace_stream,
|
|
Nonnull<Arena*> arena) -> bool {
|
|
if (trace_stream->is_enabled()) {
|
|
trace_stream->Match() << "match pattern `" << *p << "`\n";
|
|
trace_stream->Indent() << "from "
|
|
<< ExpressionCategoryToString(
|
|
v.expression_category())
|
|
<< " expression with value `" << *v.value() << "`\n";
|
|
}
|
|
const auto make_expr_result =
|
|
[](Nonnull<const Value*> v) -> ExpressionResult {
|
|
if (const auto* expr_v = dyn_cast<ReferenceExpressionValue>(v)) {
|
|
return ExpressionResult::Reference(expr_v->value(), expr_v->address());
|
|
}
|
|
return ExpressionResult::Value(v);
|
|
};
|
|
if (v.value()->kind() == Value::Kind::ReferenceExpressionValue) {
|
|
return PatternMatch(p, make_expr_result(v.value()), source_loc, bindings,
|
|
generic_args, trace_stream, arena);
|
|
}
|
|
switch (p->kind()) {
|
|
case Value::Kind::BindingPlaceholderValue: {
|
|
CARBON_CHECK(bindings.has_value());
|
|
const auto& placeholder = cast<BindingPlaceholderValue>(*p);
|
|
if (placeholder.value_node().has_value()) {
|
|
InitializePlaceholderValue(*placeholder.value_node(), v, *bindings);
|
|
}
|
|
return true;
|
|
}
|
|
case Value::Kind::AddrValue: {
|
|
const auto& addr = cast<AddrValue>(*p);
|
|
CARBON_CHECK(v.value()->kind() == Value::Kind::LocationValue);
|
|
const auto& location = cast<LocationValue>(*v.value());
|
|
return PatternMatch(
|
|
&addr.pattern(),
|
|
ExpressionResult::Value(arena->New<PointerValue>(location.address())),
|
|
source_loc, bindings, generic_args, trace_stream, arena);
|
|
}
|
|
case Value::Kind::VariableType: {
|
|
const auto& var_type = cast<VariableType>(*p);
|
|
generic_args[&var_type.binding()] = v.value();
|
|
return true;
|
|
}
|
|
case Value::Kind::TupleType:
|
|
case Value::Kind::TupleValue:
|
|
switch (v.value()->kind()) {
|
|
case Value::Kind::TupleType:
|
|
case Value::Kind::TupleValue: {
|
|
const auto& p_tup = cast<TupleValueBase>(*p);
|
|
const auto& v_tup = cast<TupleValueBase>(*v.value());
|
|
CARBON_CHECK(p_tup.elements().size() == v_tup.elements().size());
|
|
for (size_t i = 0; i < p_tup.elements().size(); ++i) {
|
|
if (!PatternMatch(p_tup.elements()[i],
|
|
make_expr_result(v_tup.elements()[i]), source_loc,
|
|
bindings, generic_args, trace_stream, arena)) {
|
|
return false;
|
|
}
|
|
} // for
|
|
return true;
|
|
}
|
|
case Value::Kind::UninitializedValue: {
|
|
const auto& p_tup = cast<TupleValueBase>(*p);
|
|
for (const auto& ele : p_tup.elements()) {
|
|
if (!PatternMatch(ele,
|
|
ExpressionResult::Value(
|
|
arena->New<UninitializedValue>(ele)),
|
|
source_loc, bindings, generic_args, trace_stream,
|
|
arena)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
default:
|
|
CARBON_FATAL("expected a tuple value in pattern, not {0}",
|
|
*v.value());
|
|
}
|
|
case Value::Kind::StructValue: {
|
|
const auto& p_struct = cast<StructValue>(*p);
|
|
const auto& v_struct = cast<StructValue>(*v.value());
|
|
CARBON_CHECK(p_struct.elements().size() == v_struct.elements().size());
|
|
for (size_t i = 0; i < p_struct.elements().size(); ++i) {
|
|
CARBON_CHECK(p_struct.elements()[i].name ==
|
|
v_struct.elements()[i].name);
|
|
if (!PatternMatch(p_struct.elements()[i].value,
|
|
ExpressionResult::Value(v_struct.elements()[i].value),
|
|
source_loc, bindings, generic_args, trace_stream,
|
|
arena)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
case Value::Kind::AlternativeValue:
|
|
switch (v.value()->kind()) {
|
|
case Value::Kind::AlternativeValue: {
|
|
const auto& p_alt = cast<AlternativeValue>(*p);
|
|
const auto& v_alt = cast<AlternativeValue>(*v.value());
|
|
if (&p_alt.alternative() != &v_alt.alternative()) {
|
|
return false;
|
|
}
|
|
CARBON_CHECK(p_alt.argument().has_value() ==
|
|
v_alt.argument().has_value());
|
|
if (!p_alt.argument().has_value()) {
|
|
return true;
|
|
}
|
|
return PatternMatch(
|
|
*p_alt.argument(), ExpressionResult::Value(*v_alt.argument()),
|
|
source_loc, bindings, generic_args, trace_stream, arena);
|
|
}
|
|
default:
|
|
CARBON_FATAL("expected a choice alternative in pattern, not {0}",
|
|
*v.value());
|
|
}
|
|
case Value::Kind::UninitializedValue:
|
|
CARBON_FATAL("uninitialized value is not allowed in pattern {0}",
|
|
*v.value());
|
|
case Value::Kind::FunctionType:
|
|
switch (v.value()->kind()) {
|
|
case Value::Kind::FunctionType: {
|
|
const auto& p_fn = cast<FunctionType>(*p);
|
|
const auto& v_fn = cast<FunctionType>(*v.value());
|
|
if (!PatternMatch(&p_fn.parameters(),
|
|
ExpressionResult::Value(&v_fn.parameters()),
|
|
source_loc, bindings, generic_args, trace_stream,
|
|
arena)) {
|
|
return false;
|
|
}
|
|
if (!PatternMatch(&p_fn.return_type(),
|
|
ExpressionResult::Value(&v_fn.return_type()),
|
|
source_loc, bindings, generic_args, trace_stream,
|
|
arena)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
default:
|
|
return false;
|
|
}
|
|
case Value::Kind::AutoType:
|
|
// `auto` matches any type, without binding any new names. We rely
|
|
// on the typechecker to ensure that `v.value()` is a type.
|
|
return true;
|
|
case Value::Kind::StaticArrayType: {
|
|
const auto& p_arr = cast<StaticArrayType>(*p);
|
|
switch (v.value()->kind()) {
|
|
case Value::Kind::TupleType:
|
|
case Value::Kind::TupleValue: {
|
|
const auto& v_tup = cast<TupleValueBase>(*v.value());
|
|
if (v_tup.elements().empty()) {
|
|
return !TypeIsDeduceable(&p_arr.element_type());
|
|
}
|
|
|
|
std::vector<Nonnull<const Value*>> deduced_types;
|
|
deduced_types.reserve(v_tup.elements().size());
|
|
for (const auto& tup_elem : v_tup.elements()) {
|
|
if (!PatternMatch(&p_arr.element_type(), make_expr_result(tup_elem),
|
|
source_loc, bindings, generic_args, trace_stream,
|
|
arena)) {
|
|
return false;
|
|
}
|
|
|
|
deduced_types.emplace_back(
|
|
DeducePatternType(&p_arr.element_type(), tup_elem, arena));
|
|
} // for
|
|
|
|
return std::adjacent_find(deduced_types.begin(), deduced_types.end(),
|
|
[](const auto& lhs, const auto& rhs) {
|
|
return !TypeEqual(lhs, rhs, std::nullopt);
|
|
}) == deduced_types.end();
|
|
}
|
|
case Value::Kind::StaticArrayType: {
|
|
const auto& v_arr = cast<StaticArrayType>(*v.value());
|
|
if (!v_arr.has_size()) {
|
|
return false;
|
|
}
|
|
return PatternMatch(
|
|
&p_arr.element_type(), make_expr_result(&v_arr.element_type()),
|
|
source_loc, bindings, generic_args, trace_stream, arena);
|
|
}
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
default:
|
|
return ValueEqual(p, v.value(), std::nullopt);
|
|
}
|
|
}
|
|
} // namespace Carbon
|