Switch CARBON_CHECK to a format string API (#4285)

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>
This commit is contained in:
Chandler Carruth
2024-09-12 16:42:08 +00:00
committed by GitHub
co-authored by Richard Smith josh11b
parent 35dfa5f03c
commit 4845f40dff
140 changed files with 1234 additions and 1161 deletions
+22 -24
View File
@@ -159,8 +159,8 @@ static auto GetPositionalElement(Nonnull<const TupleValue*> tuple,
const ElementPath::Component& path_comp,
SourceLocation source_loc)
-> ErrorOr<Nonnull<const Value*>> {
CARBON_CHECK(path_comp.element()->kind() == ElementKind::PositionalElement)
<< "Invalid non-tuple member";
CARBON_CHECK(path_comp.element()->kind() == ElementKind::PositionalElement,
"Invalid non-tuple member");
const auto* tuple_element = cast<PositionalElement>(path_comp.element());
const size_t index = tuple_element->index();
if (index < 0 || index >= tuple->elements().size()) {
@@ -175,8 +175,8 @@ static auto GetNamedElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
SourceLocation source_loc,
std::optional<Nonnull<const Value*>> me_value)
-> ErrorOr<Nonnull<const Value*>> {
CARBON_CHECK(field.element()->kind() == ElementKind::NamedElement)
<< "Invalid element, expecting NamedElement";
CARBON_CHECK(field.element()->kind() == ElementKind::NamedElement,
"Invalid element, expecting NamedElement");
const auto* member = cast<NamedElement>(field.element());
const auto f = member->name();
if (field.witness().has_value()) {
@@ -186,7 +186,7 @@ static auto GetNamedElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
if (const auto* assoc_const =
dyn_cast_or_null<AssociatedConstantDeclaration>(
member->declaration().value_or(nullptr))) {
CARBON_CHECK(field.interface()) << "have witness but no interface";
CARBON_CHECK(field.interface(), "have witness but no interface");
// TODO: Use witness to find the value of the constant.
return arena->New<AssociatedConstant>(v, *field.interface(), assoc_const,
witness);
@@ -259,8 +259,8 @@ static auto GetNamedElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
// Get class value matching the virtual method, and turn it into a
// bound method.
for (int i = 0; i < level_diff; ++i) {
CARBON_CHECK(m_class_value->base())
<< "Error trying to access function class value";
CARBON_CHECK(m_class_value->base(),
"Error trying to access function class value");
m_class_value = *m_class_value->base();
}
return arena->New<BoundMethodValue>(
@@ -299,7 +299,7 @@ static auto GetNamedElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
&class_type.bindings());
}
default:
CARBON_FATAL() << "named element access not supported for value " << *v;
CARBON_FATAL("named element access not supported for value {0}", *v);
}
}
@@ -315,7 +315,7 @@ static auto GetElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
if (const auto* tuple = dyn_cast<TupleValue>(v)) {
return GetPositionalElement(tuple, path_comp, source_loc);
} else {
CARBON_FATAL() << "Invalid value for positional element";
CARBON_FATAL("Invalid value for positional element");
}
}
case ElementKind::BaseElement:
@@ -328,7 +328,7 @@ static auto GetElement(Nonnull<Arena*> arena, Nonnull<const Value*> v,
ptr->address().ElementAddress(path_comp.element()));
}
default:
CARBON_FATAL() << "Invalid value for base element";
CARBON_FATAL("Invalid value for base element");
}
}
}
@@ -404,9 +404,9 @@ static auto SetFieldImpl(
}
case Value::Kind::TupleType:
case Value::Kind::TupleValue: {
CARBON_CHECK((*path_begin).element()->kind() ==
ElementKind::PositionalElement)
<< "Invalid non-positional member for tuple";
CARBON_CHECK(
(*path_begin).element()->kind() == ElementKind::PositionalElement,
"Invalid non-positional member for tuple");
std::vector<Nonnull<const Value*>> elements =
cast<TupleValueBase>(*value).elements();
const size_t index =
@@ -425,7 +425,7 @@ static auto SetFieldImpl(
}
}
default:
CARBON_FATAL() << "field access not allowed for value " << *value;
CARBON_FATAL("field access not allowed for value {0}", *value);
}
}
@@ -841,7 +841,7 @@ void IntrinsicConstraint::Print(llvm::raw_ostream& out) const {
static auto BindingMapEqual(
const BindingMap& map1, const BindingMap& map2,
std::optional<Nonnull<const EqualityContext*>> equality_ctx) -> bool {
CARBON_CHECK(map1.size() == map2.size()) << "maps should have same keys";
CARBON_CHECK(map1.size() == map2.size(), "maps should have same keys");
for (const auto& [key, value] : map1) {
if (!ValueEqual(value, map2.at(key), equality_ctx)) {
return false;
@@ -1025,17 +1025,16 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
case Value::Kind::MixinPseudoType:
case Value::Kind::TypeOfMixinPseudoType:
case Value::Kind::TypeOfNamespaceName:
CARBON_FATAL() << "TypeEqual used to compare non-type values\n"
<< *t1 << "\n"
<< *t2;
CARBON_FATAL("TypeEqual used to compare non-type values\n{0}\n{1}", *t1,
*t2);
case Value::Kind::ImplWitness:
case Value::Kind::BindingWitness:
case Value::Kind::ConstraintWitness:
case Value::Kind::ConstraintImplWitness:
CARBON_FATAL() << "TypeEqual: unexpected Witness";
CARBON_FATAL("TypeEqual: unexpected Witness");
break;
case Value::Kind::AutoType:
CARBON_FATAL() << "TypeEqual: unexpected AutoType";
CARBON_FATAL("TypeEqual: unexpected AutoType");
break;
}
}
@@ -1123,8 +1122,8 @@ static auto ValueStructurallyEqual(
GetName(cast<ParameterizedEntityName>(v1)->declaration());
std::optional<std::string_view> name2 =
GetName(cast<ParameterizedEntityName>(v2)->declaration());
CARBON_CHECK(name1.has_value() && name2.has_value())
<< "parameterized name refers to unnamed declaration";
CARBON_CHECK(name1.has_value() && name2.has_value(),
"parameterized name refers to unnamed declaration");
return *name1 == *name2;
}
case Value::Kind::AssociatedConstant: {
@@ -1171,8 +1170,7 @@ static auto ValueStructurallyEqual(
case Value::Kind::MemberName:
// TODO: support pointer comparisons once we have a clearer distinction
// between pointers and lvalues.
CARBON_FATAL() << "ValueEqual does not support this kind of value: "
<< *v1;
CARBON_FATAL("ValueEqual does not support this kind of value: {0}", *v1);
}
}