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>
196 lines
7.8 KiB
C++
196 lines
7.8 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 "toolchain/check/return.h"
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/convert.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Gets the function that lexically encloses the current location.
|
|
static auto GetCurrentFunction(Context& context) -> SemIR::Function& {
|
|
CARBON_CHECK(!context.return_scope_stack().empty(),
|
|
"Handling return but not in a function");
|
|
auto function_id = context.insts()
|
|
.GetAs<SemIR::FunctionDecl>(
|
|
context.return_scope_stack().back().decl_id)
|
|
.function_id;
|
|
return context.functions().Get(function_id);
|
|
}
|
|
|
|
// Gets the currently in scope `returned var`, if any, that would be returned
|
|
// by a `return var;`.
|
|
static auto GetCurrentReturnedVar(Context& context) -> SemIR::InstId {
|
|
CARBON_CHECK(!context.return_scope_stack().empty(),
|
|
"Handling return but not in a function");
|
|
return context.return_scope_stack().back().returned_var;
|
|
}
|
|
|
|
// Produces a note that the given function has no explicit return type.
|
|
static auto NoteNoReturnTypeProvided(Context::DiagnosticBuilder& diag,
|
|
const SemIR::Function& function) {
|
|
CARBON_DIAGNOSTIC(ReturnTypeOmittedNote, Note,
|
|
"There was no return type provided.");
|
|
diag.Note(function.latest_decl_id(), ReturnTypeOmittedNote);
|
|
}
|
|
|
|
// Produces a note describing the return type of the given function.
|
|
static auto NoteReturnType(Context::DiagnosticBuilder& diag,
|
|
const SemIR::Function& function,
|
|
SemIR::TypeId return_type_id) {
|
|
CARBON_DIAGNOSTIC(ReturnTypeHereNote, Note,
|
|
"Return type of function is `{0}`.", SemIR::TypeId);
|
|
diag.Note(function.return_storage_id, ReturnTypeHereNote, return_type_id);
|
|
}
|
|
|
|
// Produces a note pointing at the currently in scope `returned var`.
|
|
static auto NoteReturnedVar(Context::DiagnosticBuilder& diag,
|
|
SemIR::InstId returned_var_id) {
|
|
CARBON_DIAGNOSTIC(ReturnedVarHere, Note, "`returned var` was declared here.");
|
|
diag.Note(returned_var_id, ReturnedVarHere);
|
|
}
|
|
|
|
auto CheckReturnedVar(Context& context, Parse::NodeId returned_node,
|
|
Parse::NodeId name_node, SemIR::NameId name_id,
|
|
Parse::NodeId type_node, SemIR::TypeId type_id)
|
|
-> SemIR::InstId {
|
|
auto& function = GetCurrentFunction(context);
|
|
auto return_info =
|
|
SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
|
|
if (!return_info.is_valid()) {
|
|
// We already diagnosed this when we started defining the function. Create a
|
|
// placeholder for error recovery.
|
|
return context.AddInst<SemIR::VarStorage>(
|
|
name_node, {.type_id = type_id, .name_id = name_id});
|
|
}
|
|
|
|
// A `returned var` requires an explicit return type.
|
|
if (!return_info.type_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(ReturnedVarWithNoReturnType, Error,
|
|
"Cannot declare a `returned var` in this function.");
|
|
auto diag =
|
|
context.emitter().Build(returned_node, ReturnedVarWithNoReturnType);
|
|
NoteNoReturnTypeProvided(diag, function);
|
|
diag.Emit();
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// The declared type of the var must match the return type of the function.
|
|
if (return_info.type_id != type_id) {
|
|
CARBON_DIAGNOSTIC(ReturnedVarWrongType, Error,
|
|
"Type `{0}` of `returned var` does not match "
|
|
"return type of enclosing function.",
|
|
SemIR::TypeId);
|
|
auto diag =
|
|
context.emitter().Build(type_node, ReturnedVarWrongType, type_id);
|
|
NoteReturnType(diag, function, return_info.type_id);
|
|
diag.Emit();
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// The variable aliases the return slot if there is one. If not, it has its
|
|
// own storage.
|
|
if (return_info.has_return_slot()) {
|
|
return function.return_storage_id;
|
|
}
|
|
return context.AddInst<SemIR::VarStorage>(
|
|
name_node, {.type_id = type_id, .name_id = name_id});
|
|
}
|
|
|
|
auto RegisterReturnedVar(Context& context, SemIR::InstId bind_id) -> void {
|
|
auto existing_id = context.scope_stack().SetReturnedVarOrGetExisting(bind_id);
|
|
if (existing_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(ReturnedVarShadowed, Error,
|
|
"Cannot declare a `returned var` in the scope of "
|
|
"another `returned var`.");
|
|
auto diag = context.emitter().Build(bind_id, ReturnedVarShadowed);
|
|
NoteReturnedVar(diag, existing_id);
|
|
diag.Emit();
|
|
}
|
|
}
|
|
|
|
auto BuildReturnWithNoExpr(Context& context, Parse::ReturnStatementId node_id)
|
|
-> void {
|
|
const auto& function = GetCurrentFunction(context);
|
|
auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
|
|
|
|
if (return_type_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(ReturnStatementMissingExpr, Error,
|
|
"Missing return value.");
|
|
auto diag = context.emitter().Build(node_id, ReturnStatementMissingExpr);
|
|
NoteReturnType(diag, function, return_type_id);
|
|
diag.Emit();
|
|
}
|
|
|
|
context.AddInst<SemIR::Return>(node_id, {});
|
|
}
|
|
|
|
auto BuildReturnWithExpr(Context& context, Parse::ReturnStatementId node_id,
|
|
SemIR::InstId expr_id) -> void {
|
|
const auto& function = GetCurrentFunction(context);
|
|
auto returned_var_id = GetCurrentReturnedVar(context);
|
|
auto return_slot_id = SemIR::InstId::Invalid;
|
|
auto return_info =
|
|
SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
|
|
|
|
if (!return_info.type_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(
|
|
ReturnStatementDisallowExpr, Error,
|
|
"No return expression should be provided in this context.");
|
|
auto diag = context.emitter().Build(node_id, ReturnStatementDisallowExpr);
|
|
NoteNoReturnTypeProvided(diag, function);
|
|
diag.Emit();
|
|
expr_id = SemIR::InstId::BuiltinError;
|
|
} else if (returned_var_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(
|
|
ReturnExprWithReturnedVar, Error,
|
|
"Can only `return var;` in the scope of a `returned var`.");
|
|
auto diag = context.emitter().Build(node_id, ReturnExprWithReturnedVar);
|
|
NoteReturnedVar(diag, returned_var_id);
|
|
diag.Emit();
|
|
expr_id = SemIR::InstId::BuiltinError;
|
|
} else if (!return_info.is_valid()) {
|
|
// We already diagnosed that the return type is invalid. Don't try to
|
|
// convert to it.
|
|
expr_id = SemIR::InstId::BuiltinError;
|
|
} else if (return_info.has_return_slot()) {
|
|
expr_id = Initialize(context, node_id, function.return_storage_id, expr_id);
|
|
return_slot_id = function.return_storage_id;
|
|
} else {
|
|
expr_id =
|
|
ConvertToValueOfType(context, node_id, expr_id, return_info.type_id);
|
|
}
|
|
|
|
context.AddInst<SemIR::ReturnExpr>(
|
|
node_id, {.expr_id = expr_id, .dest_id = return_slot_id});
|
|
}
|
|
|
|
auto BuildReturnVar(Context& context, Parse::ReturnStatementId node_id)
|
|
-> void {
|
|
const auto& function = GetCurrentFunction(context);
|
|
auto returned_var_id = GetCurrentReturnedVar(context);
|
|
|
|
if (!returned_var_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(ReturnVarWithNoReturnedVar, Error,
|
|
"`return var;` with no `returned var` in scope.");
|
|
context.emitter().Emit(node_id, ReturnVarWithNoReturnedVar);
|
|
returned_var_id = SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
auto return_slot_id = function.return_storage_id;
|
|
if (!SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function)
|
|
.has_return_slot()) {
|
|
// If we don't have a return slot, we're returning by value. Convert to a
|
|
// value expression.
|
|
returned_var_id = ConvertToValueExpr(context, returned_var_id);
|
|
return_slot_id = SemIR::InstId::Invalid;
|
|
}
|
|
|
|
context.AddInst<SemIR::ReturnExpr>(
|
|
node_id, {.expr_id = returned_var_id, .dest_id = return_slot_id});
|
|
}
|
|
|
|
} // namespace Carbon::Check
|