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
+24 -24
View File
@@ -23,9 +23,9 @@ auto DeclNameStack::NameContext::prev_inst_id() -> SemIR::InstId {
return SemIR::InstId::Invalid;
case NameContext::State::Empty:
CARBON_FATAL()
<< "Name is missing, not expected to call existing_inst_id (but "
"that may change based on error handling).";
CARBON_FATAL(
"Name is missing, not expected to call existing_inst_id (but that "
"may change based on error handling).");
case NameContext::State::Resolved:
return resolved_inst_id;
@@ -34,7 +34,7 @@ auto DeclNameStack::NameContext::prev_inst_id() -> SemIR::InstId {
return SemIR::InstId::Invalid;
case NameContext::State::Finished:
CARBON_FATAL() << "Finished state should only be used internally";
CARBON_FATAL("Finished state should only be used internally");
}
}
@@ -59,8 +59,8 @@ auto DeclNameStack::PushScopeAndStartName() -> void {
}
auto DeclNameStack::FinishName(const NameComponent& name) -> NameContext {
CARBON_CHECK(decl_name_stack_.back().state != NameContext::State::Finished)
<< "Finished name twice";
CARBON_CHECK(decl_name_stack_.back().state != NameContext::State::Finished,
"Finished name twice");
ApplyAndLookupName(decl_name_stack_.back(), name.name_loc_id, name.name_id);
@@ -70,8 +70,8 @@ auto DeclNameStack::FinishName(const NameComponent& name) -> NameContext {
}
auto DeclNameStack::FinishImplName() -> NameContext {
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Empty)
<< "Impl has a name";
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Empty,
"Impl has a name");
NameContext result = decl_name_stack_.back();
decl_name_stack_.back().state = NameContext::State::Finished;
@@ -79,15 +79,15 @@ auto DeclNameStack::FinishImplName() -> NameContext {
}
auto DeclNameStack::PopScope() -> void {
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished)
<< "Missing call to FinishName before PopScope";
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished,
"Missing call to FinishName before PopScope");
context_->scope_stack().PopTo(decl_name_stack_.back().initial_scope_index);
decl_name_stack_.pop_back();
}
auto DeclNameStack::Suspend() -> SuspendedName {
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished)
<< "Missing call to FinishName before Suspend";
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished,
"Missing call to FinishName before Suspend");
SuspendedName result = {.name_context = decl_name_stack_.pop_back_val(),
.scopes = {}};
auto scope_index = result.name_context.initial_scope_index;
@@ -95,17 +95,17 @@ auto DeclNameStack::Suspend() -> SuspendedName {
while (scope_stack.PeekIndex() > scope_index) {
result.scopes.push_back(scope_stack.Suspend());
}
CARBON_CHECK(scope_stack.PeekIndex() == scope_index)
<< "Scope index " << scope_index << " does not enclose the current scope "
<< scope_stack.PeekIndex();
CARBON_CHECK(scope_stack.PeekIndex() == scope_index,
"Scope index {0} does not enclose the current scope {1}",
scope_index, scope_stack.PeekIndex());
return result;
}
auto DeclNameStack::Restore(SuspendedName sus) -> void {
// The parent state must be the same when a name is restored.
CARBON_CHECK(context_->scope_stack().PeekIndex() ==
sus.name_context.initial_scope_index)
<< "Name restored at the wrong position in the name stack.";
sus.name_context.initial_scope_index,
"Name restored at the wrong position in the name stack.");
// clang-tidy warns that the `std::move` below has no effect. While that's
// true, this `move` defends against `NameContext` growing more state later.
@@ -164,15 +164,15 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id,
};
auto result = name_scope.name_map.Insert(
name_context.unresolved_name_id, add_scope);
CARBON_CHECK(result.is_inserted())
<< "Duplicate names should have been resolved previously: "
<< name_context.unresolved_name_id << " in "
<< name_context.parent_scope_id;
CARBON_CHECK(
result.is_inserted(),
"Duplicate names should have been resolved previously: {0} in {1}",
name_context.unresolved_name_id, name_context.parent_scope_id);
}
break;
default:
CARBON_FATAL() << "Should not be calling AddName";
CARBON_FATAL("Should not be calling AddName");
break;
}
}
@@ -284,7 +284,7 @@ static auto CheckQualifierIsResolved(
Context& context, const DeclNameStack::NameContext& name_context) -> bool {
switch (name_context.state) {
case DeclNameStack::NameContext::State::Empty:
CARBON_FATAL() << "No qualifier to resolve";
CARBON_FATAL("No qualifier to resolve");
case DeclNameStack::NameContext::State::Resolved:
return true;
@@ -297,7 +297,7 @@ static auto CheckQualifierIsResolved(
return false;
case DeclNameStack::NameContext::State::Finished:
CARBON_FATAL() << "Added a qualifier after calling FinishName";
CARBON_FATAL("Added a qualifier after calling FinishName");
case DeclNameStack::NameContext::State::Error:
// Already in an error state, so return without examining.