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
+3 -3
View File
@@ -25,15 +25,15 @@ void Bindings::Add(Nonnull<const GenericBinding*> binding,
Nonnull<const Value*> value,
std::optional<Nonnull<const Value*>> witness) {
bool added_value = args_.insert({binding, value}).second;
CARBON_CHECK(added_value) << "Add of already-existing binding";
CARBON_CHECK(added_value, "Add of already-existing binding");
if (witness) {
// TODO: Eventually we should check that we have a witness if and only if
// the binding has an impl binding.
auto impl_binding = binding->impl_binding();
CARBON_CHECK(impl_binding) << "Given witness but have no impl binding";
CARBON_CHECK(impl_binding, "Given witness but have no impl binding");
bool added_witness = witnesses_.insert({*impl_binding, *witness}).second;
CARBON_CHECK(added_witness) << "Add of already-existing binding";
CARBON_CHECK(added_witness, "Add of already-existing binding");
}
}
+5 -5
View File
@@ -13,10 +13,10 @@ namespace Carbon {
auto CloneContext::CloneBase(Nonnull<const AstNode*> node)
-> Nonnull<AstNode*> {
auto [it, added] = nodes_.insert({node, nullptr});
CARBON_CHECK(added) << (it->second
? "node was cloned multiple times: "
: "node was remapped before it was cloned: ")
<< *node;
CARBON_CHECK(
added, "node was {0}: {1}",
it->second ? "cloned multiple times" : "remapped before it was cloned",
*node);
// TODO: Generate a Visit member on AstNode and use it here to avoid these
// macros.
@@ -34,7 +34,7 @@ auto CloneContext::CloneBase(Nonnull<const AstNode*> node)
// Cloning may have invalidated our iterator; redo lookup.
auto* result = nodes_[node];
CARBON_CHECK(result) << "CloneImpl didn't set the result pointer";
CARBON_CHECK(result, "CloneImpl didn't set the result pointer");
return result;
}
+1 -1
View File
@@ -131,7 +131,7 @@ class CloneContext {
template <typename T>
auto GetExistingClone(Nonnull<const T*> node) -> Nonnull<T*> {
AstNode* cloned = nodes_.lookup(node);
CARBON_CHECK(cloned) << "expected node to be cloned";
CARBON_CHECK(cloned, "expected node to be cloned");
return llvm::cast<T>(cloned);
}
+1 -1
View File
@@ -413,7 +413,7 @@ auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
void CallableDeclaration::PrintIndent(int indent_num_spaces,
llvm::raw_ostream& out) const {
auto name = GetName(*this);
CARBON_CHECK(name) << "Unexpected missing name for `" << *this << "`.";
CARBON_CHECK(name, "Unexpected missing name for `{0}`.", *this);
out.indent(indent_num_spaces) << "fn " << *name << " ";
if (!deduced_parameters_.empty() || self_pattern_) {
out << "[";
+3 -3
View File
@@ -101,7 +101,7 @@ class Declaration : public AstNode {
// Set that this node is declared. Should only be called once, by the
// type-checker, once the node is ready to be named and used.
void set_is_declared() {
CARBON_CHECK(!is_declared_) << "should not be declared twice";
CARBON_CHECK(!is_declared_, "should not be declared twice");
is_declared_ = true;
}
@@ -111,7 +111,7 @@ class Declaration : public AstNode {
// Set that this node is type-checked. Should only be called once, by the
// type-checker, once full type-checking is complete.
void set_is_type_checked() {
CARBON_CHECK(!is_type_checked_) << "should not be type-checked twice";
CARBON_CHECK(!is_type_checked_, "should not be type-checked twice");
is_type_checked_ = true;
}
@@ -674,7 +674,7 @@ class VariableDeclaration : public Declaration {
// Can only be called by type-checking, if a conversion was required.
void set_initializer(Nonnull<Expression*> initializer) {
CARBON_CHECK(has_initializer()) << "should not add a new initializer";
CARBON_CHECK(has_initializer(), "should not add a new initializer");
initializer_ = initializer;
}
+2 -2
View File
@@ -113,8 +113,8 @@ class ElementPath : public Printable<ElementPath> {
// Removes all trailing `BaseElement`s, errors if there are no base elements.
auto RemoveTrailingBaseElements() -> void {
CARBON_CHECK(!components_.empty() && components_.back().element()->kind() ==
ElementKind::BaseElement)
<< "No base elements to remove.";
ElementKind::BaseElement,
"No base elements to remove.");
const auto r_it = std::find_if(
components_.rbegin(), components_.rend(), [](const Component& c) {
return c.element()->kind() != ElementKind::BaseElement;
+1 -2
View File
@@ -231,8 +231,7 @@ void Expression::Print(llvm::raw_ostream& out) const {
<< *op.arguments()[1];
break;
default:
CARBON_FATAL() << "Unexpected argument count: "
<< op.arguments().size();
CARBON_FATAL("Unexpected argument count: {0}", op.arguments().size());
}
out << ")";
break;
+4 -3
View File
@@ -113,7 +113,7 @@ class RewritableMixin : public Base {
// Set the rewritten form of this expression. Can only be called during type
// checking.
auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
CARBON_CHECK(!rewritten_form_.has_value(), "rewritten form set twice");
rewritten_form_ = rewritten_form;
this->set_static_type(&rewritten_form->static_type());
this->set_expression_category(rewritten_form->expression_category());
@@ -660,8 +660,9 @@ class StructTypeLiteral : public ConstantValueLiteral {
std::vector<FieldInitializer> fields)
: ConstantValueLiteral(AstNodeKind::StructTypeLiteral, loc),
fields_(std::move(fields)) {
CARBON_CHECK(!fields_.empty())
<< "`{}` is represented as a StructLiteral, not a StructTypeLiteral.";
CARBON_CHECK(
!fields_.empty(),
"`{}` is represented as a StructLiteral, not a StructTypeLiteral.");
}
explicit StructTypeLiteral(CloneContext& context,
+3 -3
View File
@@ -49,14 +49,14 @@ class ImplBinding : public AstNode {
// The constraint being implemented.
// TODO: Rename this to `constraint`.
auto interface() const -> Nonnull<const Value*> {
CARBON_CHECK(iface_) << "interface has not been set yet";
CARBON_CHECK(iface_, "interface has not been set yet");
return *iface_;
}
// Set the interface being implemented, if not set by the constructor. Should
// only be called by typechecking.
void set_interface(Nonnull<const Value*> iface) {
CARBON_CHECK(!iface_) << "interface set twice";
CARBON_CHECK(!iface_, "interface set twice");
iface_ = iface;
}
@@ -75,7 +75,7 @@ class ImplBinding : public AstNode {
// These functions exist only so that an `ImplBinding` can be used as a
// `ValueNodeView` as a key in a `StaticScope`.
auto static_type() const -> const Value& {
CARBON_FATAL() << "an ImplBinding has no type";
CARBON_FATAL("an ImplBinding has no type");
}
auto expression_category() const -> ExpressionCategory {
return ExpressionCategory::Value;
+2 -2
View File
@@ -79,7 +79,7 @@ class Pattern : public AstNode {
// Sets the value of this pattern. Can only be called once, during
// typechecking.
void set_value(Nonnull<const Value*> value) {
CARBON_CHECK(!value_) << "set_value called more than once";
CARBON_CHECK(!value_, "set_value called more than once");
value_ = value;
}
@@ -307,7 +307,7 @@ class GenericBinding : public Pattern {
// Set the index of this binding. Should be called only during type-checking.
void set_index(int index) {
CARBON_CHECK(!index_) << "should only set depth and index once";
CARBON_CHECK(!index_, "should only set depth and index once");
index_ = index;
}
+3 -3
View File
@@ -150,7 +150,7 @@ class Assign : public Statement {
// Set the rewritten form of this statement. Can only be called during type
// checking.
auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
CARBON_CHECK(!rewritten_form_.has_value(), "rewritten form set twice");
rewritten_form_ = rewritten_form;
}
@@ -196,7 +196,7 @@ class IncrementDecrement : public Statement {
// Set the rewritten form of this statement. Can only be called during type
// checking.
auto set_rewritten_form(Nonnull<const Expression*> rewritten_form) -> void {
CARBON_CHECK(!rewritten_form_.has_value()) << "rewritten form set twice";
CARBON_CHECK(!rewritten_form_.has_value(), "rewritten form set twice");
rewritten_form_ = rewritten_form;
}
@@ -256,7 +256,7 @@ class VariableDefinition : public Statement {
// Can only be called by type-checking, if a conversion was required.
void set_init(Nonnull<Expression*> init) {
CARBON_CHECK(has_init()) << "should not add a new initializer";
CARBON_CHECK(has_init(), "should not add a new initializer");
init_ = init;
}
+2 -2
View File
@@ -54,7 +54,7 @@ void StaticScope::PrintID(llvm::raw_ostream& out) const {
void StaticScope::MarkDeclared(std::string_view name) {
auto it = declared_names_.find(name);
CARBON_CHECK(it != declared_names_.end()) << name << " not found";
CARBON_CHECK(it != declared_names_.end(), "{0} not found", name);
if (it->second.status == NameStatus::KnownButNotDeclared) {
it->second.status = NameStatus::DeclaredButNotUsable;
if (trace_stream_->is_enabled()) {
@@ -67,7 +67,7 @@ void StaticScope::MarkDeclared(std::string_view name) {
void StaticScope::MarkUsable(std::string_view name) {
auto it = declared_names_.find(name);
CARBON_CHECK(it != declared_names_.end()) << name << " not found";
CARBON_CHECK(it != declared_names_.end(), "{0} not found", name);
it->second.status = NameStatus::Usable;
if (trace_stream_->is_enabled()) {
trace_stream_->Result()
+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);
}
}
+10 -10
View File
@@ -924,8 +924,8 @@ class MixinPseudoType : public Value {
public:
explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration)
: Value(Kind::MixinPseudoType), declaration_(declaration) {
CARBON_CHECK(!declaration->params().has_value())
<< "missing arguments for parameterized mixin type";
CARBON_CHECK(!declaration->params().has_value(),
"missing arguments for parameterized mixin type");
}
explicit MixinPseudoType(Nonnull<const MixinDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
@@ -982,8 +982,8 @@ class InterfaceType : public Value {
public:
explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
: Value(Kind::InterfaceType), declaration_(declaration) {
CARBON_CHECK(!declaration->params().has_value())
<< "missing arguments for parameterized interface type";
CARBON_CHECK(!declaration->params().has_value(),
"missing arguments for parameterized interface type");
}
explicit InterfaceType(Nonnull<const InterfaceDeclaration*> declaration,
Nonnull<const Bindings*> bindings)
@@ -1334,8 +1334,8 @@ class ConstraintImplWitness : public Witness {
// element.
static auto Make(Nonnull<Arena*> arena, Nonnull<const Witness*> witness,
int index) -> Nonnull<const Witness*> {
CARBON_CHECK(!llvm::isa<ImplWitness>(witness))
<< "impl witness has no components to access";
CARBON_CHECK(!llvm::isa<ImplWitness>(witness),
"impl witness has no components to access");
if (const auto* constraint_witness =
llvm::dyn_cast<ConstraintWitness>(witness)) {
return constraint_witness->witnesses()[index];
@@ -1348,8 +1348,8 @@ class ConstraintImplWitness : public Witness {
: Witness(Kind::ConstraintImplWitness),
constraint_witness_(constraint_witness),
index_(index) {
CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness))
<< "should have resolved element from constraint witness";
CARBON_CHECK(!llvm::isa<ConstraintWitness>(constraint_witness),
"should have resolved element from constraint witness");
}
static auto classof(const Value* value) -> bool {
@@ -1480,8 +1480,8 @@ class MemberName : public Value, public Printable<MemberName> {
base_type_(base_type),
interface_(interface),
member_(member) {
CARBON_CHECK(base_type || interface)
<< "member name must be in a type, an interface, or both";
CARBON_CHECK(base_type || interface,
"member name must be in a type, an interface, or both");
}
static auto classof(const Value* value) -> bool {