mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
co-authored by
Richard Smith
josh11b
parent
35dfa5f03c
commit
4845f40dff
+31
-30
@@ -142,9 +142,10 @@ auto Context::CheckCompatibleImportedNodeKind(
|
||||
auto& import_ir_inst = import_ir_insts().Get(imported_loc_id);
|
||||
const auto* import_ir = import_irs().Get(import_ir_inst.ir_id).sem_ir;
|
||||
auto imported_kind = import_ir->insts().Get(import_ir_inst.inst_id).kind();
|
||||
CARBON_CHECK(HasCompatibleImportedNodeKind(imported_kind, kind))
|
||||
<< "Node of kind " << kind
|
||||
<< " created with location of imported node of kind " << imported_kind;
|
||||
CARBON_CHECK(
|
||||
HasCompatibleImportedNodeKind(imported_kind, kind),
|
||||
"Node of kind {0} created with location of imported node of kind {1}",
|
||||
kind, imported_kind);
|
||||
}
|
||||
|
||||
auto Context::AddInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
|
||||
@@ -218,7 +219,7 @@ auto Context::DiagnoseNameNotFound(SemIRLoc loc, SemIR::NameId name_id)
|
||||
auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
|
||||
DiagnosticBuilder& builder) -> void {
|
||||
const auto& class_info = classes().Get(class_id);
|
||||
CARBON_CHECK(!class_info.is_defined()) << "Class is not incomplete";
|
||||
CARBON_CHECK(!class_info.is_defined(), "Class is not incomplete");
|
||||
if (class_info.definition_id.is_valid()) {
|
||||
CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
|
||||
"Class is incomplete within its definition.");
|
||||
@@ -233,7 +234,7 @@ auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
|
||||
auto Context::NoteUndefinedInterface(SemIR::InterfaceId interface_id,
|
||||
DiagnosticBuilder& builder) -> void {
|
||||
const auto& interface_info = interfaces().Get(interface_id);
|
||||
CARBON_CHECK(!interface_info.is_defined()) << "Interface is not incomplete";
|
||||
CARBON_CHECK(!interface_info.is_defined(), "Interface is not incomplete");
|
||||
if (interface_info.is_being_defined()) {
|
||||
CARBON_DIAGNOSTIC(InterfaceUndefinedWithinDefinition, Note,
|
||||
"Interface is currently being defined.");
|
||||
@@ -389,7 +390,7 @@ static auto DiagnoseInvalidQualifiedNameAccess(Context& context, SemIRLoc loc,
|
||||
class_info.adapt_id)) {
|
||||
parent_type_id = adapt_decl->adapted_type_id;
|
||||
} else {
|
||||
CARBON_FATAL() << "Expected parent for parent access";
|
||||
CARBON_FATAL("Expected parent for parent access");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -615,7 +616,7 @@ auto Context::AddDominatedBlockAndBranchIf(Parse::NodeId node_id,
|
||||
|
||||
auto Context::AddConvergenceBlockAndPush(Parse::NodeId node_id, int num_blocks)
|
||||
-> void {
|
||||
CARBON_CHECK(num_blocks >= 2) << "no convergence";
|
||||
CARBON_CHECK(num_blocks >= 2, "no convergence");
|
||||
|
||||
SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
|
||||
for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
|
||||
@@ -633,7 +634,7 @@ auto Context::AddConvergenceBlockAndPush(Parse::NodeId node_id, int num_blocks)
|
||||
auto Context::AddConvergenceBlockWithArgAndPush(
|
||||
Parse::NodeId node_id, std::initializer_list<SemIR::InstId> block_args)
|
||||
-> SemIR::InstId {
|
||||
CARBON_CHECK(block_args.size() >= 2) << "no convergence";
|
||||
CARBON_CHECK(block_args.size() >= 2, "no convergence");
|
||||
|
||||
SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
|
||||
for (auto arg_id : block_args) {
|
||||
@@ -671,8 +672,8 @@ auto Context::SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id,
|
||||
const_id = constant_values().Get(literal.value().value.ToBool() ? if_true
|
||||
: if_false);
|
||||
} else {
|
||||
CARBON_CHECK(cond_const_id == SemIR::ConstantId::Error)
|
||||
<< "Unexpected constant branch condition.";
|
||||
CARBON_CHECK(cond_const_id == SemIR::ConstantId::Error,
|
||||
"Unexpected constant branch condition.");
|
||||
const_id = SemIR::ConstantId::Error;
|
||||
}
|
||||
|
||||
@@ -684,11 +685,11 @@ auto Context::SetBlockArgResultBeforeConstantUse(SemIR::InstId select_id,
|
||||
}
|
||||
|
||||
auto Context::AddCurrentCodeBlockToFunction(Parse::NodeId node_id) -> void {
|
||||
CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
|
||||
CARBON_CHECK(!inst_block_stack().empty(), "no current code block");
|
||||
|
||||
if (return_scope_stack().empty()) {
|
||||
CARBON_CHECK(node_id.is_valid())
|
||||
<< "No current function, but node_id not provided";
|
||||
CARBON_CHECK(node_id.is_valid(),
|
||||
"No current function, but node_id not provided");
|
||||
TODO(node_id,
|
||||
"Control flow expressions are currently only supported inside "
|
||||
"functions.");
|
||||
@@ -798,16 +799,16 @@ class TypeCompleter {
|
||||
if (!AddNestedIncompleteTypes(inst)) {
|
||||
return false;
|
||||
}
|
||||
CARBON_CHECK(work_list_.size() >= old_work_list_size)
|
||||
<< "AddNestedIncompleteTypes should not remove work items";
|
||||
CARBON_CHECK(work_list_.size() >= old_work_list_size,
|
||||
"AddNestedIncompleteTypes should not remove work items");
|
||||
work_list_[old_work_list_size - 1].phase = Phase::BuildValueRepr;
|
||||
break;
|
||||
|
||||
case Phase::BuildValueRepr: {
|
||||
auto value_rep = BuildValueRepr(type_id, inst);
|
||||
context_.types().SetValueRepr(type_id, value_rep);
|
||||
CARBON_CHECK(old_work_list_size == work_list_.size())
|
||||
<< "BuildValueRepr should not change work items";
|
||||
CARBON_CHECK(old_work_list_size == work_list_.size(),
|
||||
"BuildValueRepr should not change work items");
|
||||
work_list_.pop_back();
|
||||
|
||||
// Also complete the value representation type, if necessary. This
|
||||
@@ -919,11 +920,11 @@ class TypeCompleter {
|
||||
// Gets the value representation of a nested type, which should already be
|
||||
// complete.
|
||||
auto GetNestedValueRepr(SemIR::TypeId nested_type_id) const {
|
||||
CARBON_CHECK(context_.types().IsComplete(nested_type_id))
|
||||
<< "Nested type should already be complete";
|
||||
CARBON_CHECK(context_.types().IsComplete(nested_type_id),
|
||||
"Nested type should already be complete");
|
||||
auto value_rep = context_.types().GetValueRepr(nested_type_id);
|
||||
CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown)
|
||||
<< "Complete type should have a value representation";
|
||||
CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown,
|
||||
"Complete type should have a value representation");
|
||||
return value_rep;
|
||||
}
|
||||
|
||||
@@ -1105,7 +1106,7 @@ class TypeCompleter {
|
||||
requires(InstT::Kind.is_type() == SemIR::InstIsType::Never)
|
||||
auto BuildValueReprForInst(SemIR::TypeId /*type_id*/, InstT inst) const
|
||||
-> SemIR::ValueRepr {
|
||||
CARBON_FATAL() << "Type refers to non-type inst " << inst;
|
||||
CARBON_FATAL("Type refers to non-type inst {0}", inst);
|
||||
}
|
||||
|
||||
// Builds and returns the value representation for the given type. All nested
|
||||
@@ -1173,16 +1174,16 @@ auto Context::TryToDefineType(SemIR::TypeId type_id,
|
||||
|
||||
auto Context::GetTypeIdForTypeConstant(SemIR::ConstantId constant_id)
|
||||
-> SemIR::TypeId {
|
||||
CARBON_CHECK(constant_id.is_constant())
|
||||
<< "Canonicalizing non-constant type: " << constant_id;
|
||||
CARBON_CHECK(constant_id.is_constant(),
|
||||
"Canonicalizing non-constant type: {0}", constant_id);
|
||||
auto type_id =
|
||||
insts().Get(constant_values().GetInstId(constant_id)).type_id();
|
||||
// TODO: For now, we allow values of facet type to be used as types.
|
||||
CARBON_CHECK(type_id == SemIR::TypeId::TypeType ||
|
||||
types().Is<SemIR::InterfaceType>(type_id) ||
|
||||
constant_id == SemIR::ConstantId::Error)
|
||||
<< "Forming type ID for non-type constant of type "
|
||||
<< types().GetAsInst(type_id);
|
||||
types().Is<SemIR::InterfaceType>(type_id) ||
|
||||
constant_id == SemIR::ConstantId::Error,
|
||||
"Forming type ID for non-type constant of type {0}",
|
||||
types().GetAsInst(type_id));
|
||||
|
||||
return SemIR::TypeId::ForTypeConstant(constant_id);
|
||||
}
|
||||
@@ -1205,7 +1206,7 @@ static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
|
||||
-> SemIR::TypeId {
|
||||
auto type_id = GetTypeImpl<InstT>(context, each_arg...);
|
||||
bool complete = context.TryToCompleteType(type_id);
|
||||
CARBON_CHECK(complete) << "Type completion should not fail";
|
||||
CARBON_CHECK(complete, "Type completion should not fail");
|
||||
return type_id;
|
||||
}
|
||||
|
||||
@@ -1231,7 +1232,7 @@ auto Context::GetBuiltinType(SemIR::BuiltinInstKind kind) -> SemIR::TypeId {
|
||||
auto type_id = GetTypeIdForTypeInst(SemIR::InstId::ForBuiltin(kind));
|
||||
// To keep client code simpler, complete builtin types before returning them.
|
||||
bool complete = TryToCompleteType(type_id);
|
||||
CARBON_CHECK(complete) << "Failed to complete builtin type";
|
||||
CARBON_CHECK(complete, "Failed to complete builtin type");
|
||||
return type_id;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user