mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 14:22:37 +00: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>
154 lines
5.6 KiB
C++
154 lines
5.6 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|
|
#define CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/DIBuilder.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "toolchain/check/sem_ir_diagnostic_converter.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
#include "toolchain/sem_ir/inst_namer.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
// Context and shared functionality for lowering handlers.
|
|
class FileContext {
|
|
public:
|
|
// Location information for use with DebugInfo. The line_number and
|
|
// column_number are >= 0, with 0 as unknown, so that they can be passed
|
|
// directly to DebugInfo.
|
|
struct LocForDI {
|
|
llvm::StringRef filename;
|
|
int32_t line_number;
|
|
int32_t column_number;
|
|
};
|
|
|
|
explicit FileContext(llvm::LLVMContext& llvm_context, bool include_debug_info,
|
|
const Check::SemIRDiagnosticConverter& converter,
|
|
llvm::StringRef module_name, const SemIR::File& sem_ir,
|
|
const SemIR::InstNamer* inst_namer,
|
|
llvm::raw_ostream* vlog_stream);
|
|
|
|
// Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
|
|
// the main execution loop.
|
|
auto Run() -> std::unique_ptr<llvm::Module>;
|
|
|
|
// Create the DICompileUnit metadata for this compilation.
|
|
auto BuildDICompileUnit(llvm::StringRef module_name,
|
|
llvm::Module& llvm_module,
|
|
llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*;
|
|
|
|
// Gets a callable's function. Returns nullptr for a builtin.
|
|
auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
|
|
return functions_[function_id.index];
|
|
}
|
|
|
|
// Returns a lowered type for the given type_id.
|
|
auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
|
|
// InvalidType should not be passed in.
|
|
CARBON_CHECK(type_id.index >= 0, "{0}", type_id);
|
|
CARBON_CHECK(types_[type_id.index], "Missing type {0}", type_id);
|
|
return types_[type_id.index];
|
|
}
|
|
|
|
// Returns location information for use with DebugInfo.
|
|
auto GetLocForDI(SemIR::InstId inst_id) -> LocForDI;
|
|
|
|
// Returns a lowered value to use for a value of type `type`.
|
|
auto GetTypeAsValue() -> llvm::Constant* {
|
|
return llvm::ConstantStruct::get(GetTypeType());
|
|
}
|
|
|
|
// Returns a global value for the given instruction.
|
|
auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
|
|
|
|
// Returns the empty LLVM struct type used to represent the type `type`.
|
|
auto GetTypeType() -> llvm::StructType* {
|
|
if (!type_type_) {
|
|
// `type` is lowered to an empty LLVM StructType.
|
|
type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
|
|
}
|
|
return type_type_;
|
|
}
|
|
|
|
auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
|
|
auto llvm_module() -> llvm::Module& { return *llvm_module_; }
|
|
auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
|
|
auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
|
|
auto global_variables() -> const Map<SemIR::InstId, llvm::GlobalVariable*>& {
|
|
return global_variables_;
|
|
}
|
|
|
|
private:
|
|
// Builds the declaration for the given function, which should then be cached
|
|
// by the caller.
|
|
auto BuildFunctionDecl(SemIR::FunctionId function_id) -> llvm::Function*;
|
|
|
|
// Builds the definition for the given function. If the function is only a
|
|
// declaration with no definition, does nothing.
|
|
auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
|
|
|
|
// Build the DISubprogram metadata for the given function.
|
|
auto BuildDISubprogram(const SemIR::Function& function,
|
|
const llvm::Function* llvm_function)
|
|
-> llvm::DISubprogram*;
|
|
|
|
// Builds the type for the given instruction, which should then be cached by
|
|
// the caller.
|
|
auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
|
|
|
|
// Builds the global for the given instruction, which should then be cached by
|
|
// the caller.
|
|
auto BuildGlobalVariableDecl(SemIR::VarStorage var_storage)
|
|
-> llvm::GlobalVariable*;
|
|
|
|
// State for building the LLVM IR.
|
|
llvm::LLVMContext* llvm_context_;
|
|
std::unique_ptr<llvm::Module> llvm_module_;
|
|
|
|
// State for building the LLVM IR debug info metadata.
|
|
llvm::DIBuilder di_builder_;
|
|
|
|
// The DICompileUnit, if any - null implies debug info is not being emitted.
|
|
llvm::DICompileUnit* di_compile_unit_;
|
|
|
|
// The source location converter.
|
|
const Check::SemIRDiagnosticConverter& converter_;
|
|
|
|
// The input SemIR.
|
|
const SemIR::File* const sem_ir_;
|
|
|
|
// The instruction namer, if given.
|
|
const SemIR::InstNamer* const inst_namer_;
|
|
|
|
// The optional vlog stream.
|
|
llvm::raw_ostream* vlog_stream_;
|
|
|
|
// Maps callables to lowered functions. SemIR treats callables as the
|
|
// canonical form of a function, so lowering needs to do the same.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Function*, 0> functions_;
|
|
|
|
// Provides lowered versions of types.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Type*, 0> types_;
|
|
|
|
// Lowered version of the builtin type `type`.
|
|
llvm::StructType* type_type_ = nullptr;
|
|
|
|
// Maps constants to their lowered values.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Constant*, 0> constants_;
|
|
|
|
// Maps global variables to their lowered variant.
|
|
Map<SemIR::InstId, llvm::GlobalVariable*> global_variables_;
|
|
};
|
|
|
|
} // namespace Carbon::Lower
|
|
|
|
#endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|