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>
253 lines
9.2 KiB
C++
253 lines
9.2 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/lower/constant.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/Value.h"
|
|
#include "toolchain/base/kind_switch.h"
|
|
#include "toolchain/lower/file_context.h"
|
|
#include "toolchain/sem_ir/generic.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
// Context and shared functionality for lowering constant values.
|
|
class ConstantContext {
|
|
public:
|
|
explicit ConstantContext(FileContext& file_context,
|
|
llvm::MutableArrayRef<llvm::Constant*> constants)
|
|
: file_context_(&file_context), constants_(constants) {}
|
|
|
|
// Gets the lowered constant value for an instruction, which must have a
|
|
// constant value that has already been lowered.
|
|
auto GetConstant(SemIR::InstId inst_id) const -> llvm::Constant* {
|
|
return GetConstant(file_context_->sem_ir().constant_values().Get(inst_id));
|
|
}
|
|
|
|
// Gets the lowered constant value for a constant that has already been
|
|
// lowered.
|
|
auto GetConstant(SemIR::ConstantId const_id) const -> llvm::Constant* {
|
|
CARBON_CHECK(const_id.is_template(), "Unexpected constant ID {0}",
|
|
const_id);
|
|
auto inst_id =
|
|
file_context_->sem_ir().constant_values().GetInstId(const_id);
|
|
CARBON_CHECK(
|
|
inst_id.index >= 0 && inst_id.index <= last_lowered_constant_index_,
|
|
"Queried constant {0} with instruction {1} that has not been lowered "
|
|
"yet",
|
|
const_id, inst_id);
|
|
return constants_[inst_id.index];
|
|
}
|
|
|
|
// Returns a constant for the case of a value that should never be used.
|
|
auto GetUnusedConstant(SemIR::TypeId /*type_id*/) const -> llvm::Constant* {
|
|
// TODO: Consider using a poison value of the appropriate type.
|
|
return nullptr;
|
|
}
|
|
|
|
// Gets a callable's function. Returns nullptr for a builtin.
|
|
auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
|
|
return file_context_->GetFunction(function_id);
|
|
}
|
|
|
|
// Returns a lowered type for the given type_id.
|
|
auto GetType(SemIR::TypeId type_id) const -> llvm::Type* {
|
|
return file_context_->GetType(type_id);
|
|
}
|
|
|
|
// Returns a lowered value to use for a value of type `type`.
|
|
auto GetTypeAsValue() const -> llvm::Constant* {
|
|
return file_context_->GetTypeAsValue();
|
|
}
|
|
|
|
// Sets the index of the constant we most recently lowered. This is used to
|
|
// check we don't look at constants that we've not lowered yet.
|
|
auto SetLastLoweredConstantIndex(int32_t index) {
|
|
last_lowered_constant_index_ = index;
|
|
}
|
|
|
|
auto llvm_context() const -> llvm::LLVMContext& {
|
|
return file_context_->llvm_context();
|
|
}
|
|
auto llvm_module() const -> llvm::Module& {
|
|
return file_context_->llvm_module();
|
|
}
|
|
auto sem_ir() const -> const SemIR::File& { return file_context_->sem_ir(); }
|
|
|
|
private:
|
|
FileContext* file_context_;
|
|
llvm::MutableArrayRef<llvm::Constant*> constants_;
|
|
int32_t last_lowered_constant_index_ = -1;
|
|
};
|
|
|
|
// Emits an aggregate constant of LLVM type `Type` whose elements are the
|
|
// contents of `refs_id`.
|
|
template <typename ConstantType, typename Type>
|
|
static auto EmitAggregateConstant(ConstantContext& context,
|
|
SemIR::InstBlockId refs_id, Type* llvm_type)
|
|
-> llvm::Constant* {
|
|
auto refs = context.sem_ir().inst_blocks().Get(refs_id);
|
|
llvm::SmallVector<llvm::Constant*> elements;
|
|
elements.reserve(refs.size());
|
|
for (auto ref : refs) {
|
|
elements.push_back(context.GetConstant(ref));
|
|
}
|
|
|
|
return ConstantType::get(llvm_type, elements);
|
|
}
|
|
|
|
// For each instruction InstT, there is a function below to convert it to an
|
|
// `llvm::Constant*`:
|
|
//
|
|
// auto EmitAsConstant(ConstantContext& context, SemIR::InstT inst)
|
|
// -> llvm::Constant*;
|
|
|
|
template <typename InstT>
|
|
requires(InstT::Kind.constant_kind() == SemIR::InstConstantKind::Never ||
|
|
InstT::Kind.constant_kind() == SemIR::InstConstantKind::SymbolicOnly)
|
|
static auto EmitAsConstant(ConstantContext& /*context*/, InstT inst)
|
|
-> llvm::Constant* {
|
|
CARBON_FATAL("Unexpected constant instruction kind {0}", inst);
|
|
}
|
|
|
|
// For constants that are always of type `type`, produce the trivial runtime
|
|
// representation of type `type`.
|
|
template <typename InstT>
|
|
requires(InstT::Kind.is_type() == SemIR::InstIsType::Always)
|
|
static auto EmitAsConstant(ConstantContext& context, InstT /*inst*/)
|
|
-> llvm::Constant* {
|
|
return context.GetTypeAsValue();
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::StructValue inst)
|
|
-> llvm::Constant* {
|
|
return EmitAggregateConstant<llvm::ConstantStruct>(
|
|
context, inst.elements_id,
|
|
cast<llvm::StructType>(context.GetType(inst.type_id)));
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::TupleValue inst)
|
|
-> llvm::Constant* {
|
|
// TODO: Add an ArrayValue instruction and stop using TupleValues to represent
|
|
// array constants.
|
|
if (context.sem_ir().types().Is<SemIR::ArrayType>(inst.type_id)) {
|
|
return EmitAggregateConstant<llvm::ConstantArray>(
|
|
context, inst.elements_id,
|
|
cast<llvm::ArrayType>(context.GetType(inst.type_id)));
|
|
}
|
|
|
|
return EmitAggregateConstant<llvm::ConstantStruct>(
|
|
context, inst.elements_id,
|
|
cast<llvm::StructType>(context.GetType(inst.type_id)));
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& /*context*/, SemIR::AddrOf /*inst*/)
|
|
-> llvm::Constant* {
|
|
// TODO: Constant lvalue support. For now we have no constant lvalues, so we
|
|
// should never form a constant AddrOf.
|
|
CARBON_FATAL("AddrOf constants not supported yet");
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context,
|
|
SemIR::AssociatedEntity inst) -> llvm::Constant* {
|
|
return context.GetUnusedConstant(inst.type_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::BaseDecl inst)
|
|
-> llvm::Constant* {
|
|
return context.GetUnusedConstant(inst.type_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::BoolLiteral inst)
|
|
-> llvm::Constant* {
|
|
return llvm::ConstantInt::get(llvm::Type::getInt1Ty(context.llvm_context()),
|
|
inst.value.index);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::BoundMethod inst)
|
|
-> llvm::Constant* {
|
|
// Propagate just the function; the object is separately provided to the
|
|
// enclosing call as an implicit argument.
|
|
return context.GetConstant(inst.function_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::FieldDecl inst)
|
|
-> llvm::Constant* {
|
|
return context.GetUnusedConstant(inst.type_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::FloatLiteral inst)
|
|
-> llvm::Constant* {
|
|
const llvm::APFloat& value = context.sem_ir().floats().Get(inst.float_id);
|
|
return llvm::ConstantFP::get(context.GetType(inst.type_id), value);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context,
|
|
SemIR::InterfaceWitness inst) -> llvm::Constant* {
|
|
// TODO: For dynamic dispatch, we might want to lower witness tables as
|
|
// constants.
|
|
return context.GetUnusedConstant(inst.type_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::IntLiteral inst)
|
|
-> llvm::Constant* {
|
|
return llvm::ConstantInt::get(context.GetType(inst.type_id),
|
|
context.sem_ir().ints().Get(inst.int_id));
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& context, SemIR::Namespace inst)
|
|
-> llvm::Constant* {
|
|
return context.GetUnusedConstant(inst.type_id);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& /*context*/,
|
|
SemIR::StringLiteral inst) -> llvm::Constant* {
|
|
CARBON_FATAL("TODO: Add support: {0}", inst);
|
|
}
|
|
|
|
static auto EmitAsConstant(ConstantContext& /*context*/,
|
|
SemIR::StructTypeField /*inst*/) -> llvm::Constant* {
|
|
// A StructTypeField isn't a value, so this constant value won't ever be used.
|
|
// It also doesn't even have a type, so we can't use GetUnusedConstant.
|
|
return nullptr;
|
|
}
|
|
|
|
auto LowerConstants(FileContext& file_context,
|
|
llvm::MutableArrayRef<llvm::Constant*> constants) -> void {
|
|
ConstantContext context(file_context, constants);
|
|
// Lower each constant in InstId order. This guarantees we lower the
|
|
// dependencies of a constant before we lower the constant itself.
|
|
for (auto [inst_id_val, const_id] :
|
|
llvm::enumerate(file_context.sem_ir().constant_values().array_ref())) {
|
|
if (!const_id.is_valid() || !const_id.is_template()) {
|
|
// We are only interested in lowering template constants.
|
|
continue;
|
|
}
|
|
auto inst_id = file_context.sem_ir().constant_values().GetInstId(const_id);
|
|
|
|
if (inst_id.index != static_cast<int32_t>(inst_id_val)) {
|
|
// This isn't the instruction that defines the constant.
|
|
continue;
|
|
}
|
|
|
|
auto inst = file_context.sem_ir().insts().Get(inst_id);
|
|
llvm::Constant* value = nullptr;
|
|
CARBON_KIND_SWITCH(inst) {
|
|
#define CARBON_SEM_IR_INST_KIND(Name) \
|
|
case CARBON_KIND(SemIR::Name const_inst): { \
|
|
value = EmitAsConstant(context, const_inst); \
|
|
break; \
|
|
}
|
|
#include "toolchain/sem_ir/inst_kind.def"
|
|
}
|
|
|
|
constants[inst_id.index] = value;
|
|
context.SetLastLoweredConstantIndex(inst_id.index);
|
|
}
|
|
} // namespace Carbon::Lower
|
|
|
|
} // namespace Carbon::Lower
|