Files
carbon-lang/toolchain/lower/function_context.h
T
4845f40dff 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>
2024-09-12 16:42:08 +00:00

181 lines
6.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_FUNCTION_CONTEXT_H_
#define CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_
#include "common/map.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "toolchain/lower/file_context.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::Lower {
// Context and shared functionality for lowering handlers that produce an
// `llvm::Function` definition.
class FunctionContext {
public:
explicit FunctionContext(FileContext& file_context, llvm::Function* function,
llvm::DISubprogram* di_subprogram,
llvm::raw_ostream* vlog_stream);
// Returns a basic block corresponding to the start of the given semantics
// block, and enqueues it for emission.
auto GetBlock(SemIR::InstBlockId block_id) -> llvm::BasicBlock*;
// If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
// `block`, and enqueue `block_id` for emission. Returns whether we set the
// block.
auto TryToReuseBlock(SemIR::InstBlockId block_id, llvm::BasicBlock* block)
-> bool;
// Builds LLVM IR for the sequence of instructions in `block_id`.
auto LowerBlock(SemIR::InstBlockId block_id) -> void;
// Builds LLVM IR for the specified instruction.
auto LowerInst(SemIR::InstId inst_id) -> void;
// Returns a phi node corresponding to the block argument of the given basic
// block.
auto GetBlockArg(SemIR::InstBlockId block_id, SemIR::TypeId type_id)
-> llvm::PHINode*;
// Returns a value for the given instruction.
auto GetValue(SemIR::InstId inst_id) -> llvm::Value* {
// All builtins are types, with the same empty lowered value.
if (inst_id.is_builtin()) {
return GetTypeAsValue();
}
if (auto result = locals_.Lookup(inst_id)) {
return result.value();
}
if (auto result = file_context_->global_variables().Lookup(inst_id)) {
return result.value();
}
return file_context_->GetGlobal(inst_id);
}
// Sets the value for the given instruction.
auto SetLocal(SemIR::InstId inst_id, llvm::Value* value) {
bool added = locals_.Insert(inst_id, value).is_inserted();
CARBON_CHECK(added, "Duplicate local insert: {0} {1}", inst_id,
sem_ir().insts().Get(inst_id));
}
// Gets a callable's function.
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) -> llvm::Type* {
return file_context_->GetType(type_id);
}
// Returns a lowered value to use for a value of type `type`.
auto GetTypeAsValue() -> llvm::Value* {
return file_context_->GetTypeAsValue();
}
// Create a synthetic block that corresponds to no SemIR::InstBlockId. Such
// a block should only ever have a single predecessor, and is used when we
// need multiple `llvm::BasicBlock`s to model the linear control flow in a
// single SemIR::File block.
auto MakeSyntheticBlock() -> llvm::BasicBlock*;
// Determine whether block is the most recently created synthetic block.
auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
return synthetic_block_ == block;
}
// After emitting an initializer `init_id`, finishes performing the
// initialization of `dest_id` from that initializer. This is a no-op if the
// initialization was performed in-place, and otherwise performs a store or a
// copy.
auto FinishInit(SemIR::TypeId type_id, SemIR::InstId dest_id,
SemIR::InstId source_id) -> void;
auto llvm_context() -> llvm::LLVMContext& {
return file_context_->llvm_context();
}
auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
auto builder() -> llvm::IRBuilderBase& { return builder_; }
auto sem_ir() -> const SemIR::File& { return file_context_->sem_ir(); }
private:
// Custom instruction inserter for our IR builder. Automatically names
// instructions.
class Inserter : public llvm::IRBuilderDefaultInserter {
public:
explicit Inserter(const SemIR::InstNamer* inst_namer)
: inst_namer_(inst_namer) {}
// Sets the instruction we are currently emitting.
void SetCurrentInstId(SemIR::InstId inst_id) { inst_id_ = inst_id; }
private:
auto InsertHelper(llvm::Instruction* inst, const llvm::Twine& name,
llvm::BasicBlock::iterator insert_pt) const
-> void override;
// The instruction namer.
const SemIR::InstNamer* inst_namer_;
// The current instruction ID.
SemIR::InstId inst_id_ = SemIR::InstId::Invalid;
};
// Emits a value copy for type `type_id` from `source_id` to `dest_id`.
// `source_id` must produce a value representation for `type_id`, and
// `dest_id` must be a pointer to a `type_id` object.
auto CopyValue(SemIR::TypeId type_id, SemIR::InstId source_id,
SemIR::InstId dest_id) -> void;
// Emits an object representation copy for type `type_id` from `source_id` to
// `dest_id`. `source_id` and `dest_id` must produce pointers to `type_id`
// objects.
auto CopyObject(SemIR::TypeId type_id, SemIR::InstId source_id,
SemIR::InstId dest_id) -> void;
// Context for the overall lowering process.
FileContext* file_context_;
// The IR function we're generating.
llvm::Function* function_;
llvm::IRBuilder<llvm::ConstantFolder, Inserter> builder_;
llvm::DISubprogram* di_subprogram_;
// The optional vlog stream.
llvm::raw_ostream* vlog_stream_;
// Maps a function's SemIR::File blocks to lowered blocks.
Map<SemIR::InstBlockId, llvm::BasicBlock*> blocks_;
// The synthetic block we most recently created. May be null if there is no
// such block.
llvm::BasicBlock* synthetic_block_ = nullptr;
// Maps a function's SemIR::File instructions to lowered values.
Map<SemIR::InstId, llvm::Value*> locals_;
};
// Provides handlers for instructions that occur in a FunctionContext. Although
// this is declared for all instructions, it should only be defined for
// instructions which are non-constant and not always typed. See
// `FunctionContext::LowerInst` for how this is used.
#define CARBON_SEM_IR_INST_KIND(Name) \
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id, \
SemIR::Name inst) -> void;
#include "toolchain/sem_ir/inst_kind.def"
} // namespace Carbon::Lower
#endif // CARBON_TOOLCHAIN_LOWER_FUNCTION_CONTEXT_H_