Skip to content

Commit b92e239

Browse files
authored
Use a FixedSizeValueStore<CheckIRId> in Lower::Context (#6021)
Now that the total number of IRs is available from SemIR::File, we can use FixedSizeValueStore to store/look up values mapped from a CheckIRId instead of a Map, which is demonstrably faster (unsurprisingly, since it's just a vector index). See #6019. This replaces a Map with FixedSizeValueStore in Lower::Context for use in `GetFileContext()`. This function is used in some places that can become hot, such as `HandleInst()` and `GetType()`. In our current lowering tests, there's no measurable performance change from this PR, but based on #6019 we can expect to see one as the amount of instructions being lowered increases. Using a FixedSizeValueStore when possible is a better approach than a map, generally.
1 parent 64139e5 commit b92e239

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

toolchain/lower/context.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ Context::Context(
3030
: nullptr),
3131
tree_and_subtrees_getters_(tree_and_subtrees_getters),
3232
vlog_stream_(vlog_stream),
33-
total_ir_count_(total_ir_count) {}
33+
total_ir_count_(total_ir_count),
34+
file_contexts_(
35+
FileContextStore::MakeForOverwriteWithExplicitSize(total_ir_count_)) {
36+
}
3437

3538
auto Context::GetFileContext(const SemIR::File* file,
3639
const SemIR::InstNamer* inst_namer)
3740
-> FileContext& {
38-
auto insert_result = file_contexts_.Insert(file->check_ir_id(), [&] {
39-
auto file_context =
41+
auto& file_context = file_contexts_.Get(file->check_ir_id());
42+
if (!file_context) {
43+
file_context =
4044
std::make_unique<FileContext>(*this, *file, inst_namer, vlog_stream_);
4145
file_context->PrepareToLower();
42-
return file_context;
43-
});
44-
return *insert_result.value();
46+
}
47+
return *file_context;
4548
}
4649

4750
auto Context::LowerPendingDefinitions() -> void {
@@ -55,8 +58,11 @@ auto Context::LowerPendingDefinitions() -> void {
5558
auto Context::Finalize() && -> std::unique_ptr<llvm::Module> {
5659
LowerPendingDefinitions();
5760

58-
file_contexts_.ForEach(
59-
[](auto, auto& file_context) { file_context->Finalize(); });
61+
for (auto& file_context : file_contexts_.values()) {
62+
if (file_context) {
63+
file_context->Finalize();
64+
}
65+
}
6066

6167
return std::move(llvm_module_);
6268
}

toolchain/lower/context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/IR/DIBuilder.h"
1414
#include "llvm/IR/LLVMContext.h"
1515
#include "llvm/IR/Module.h"
16+
#include "toolchain/base/fixed_size_value_store.h"
1617
#include "toolchain/parse/tree_and_subtrees.h"
1718
#include "toolchain/sem_ir/absolute_node_id.h"
1819
#include "toolchain/sem_ir/ids.h"
@@ -145,7 +146,9 @@ class Context {
145146
int total_ir_count_;
146147

147148
// The `FileContext`s for each IR that is involved in this lowering action.
148-
Map<SemIR::CheckIRId, std::unique_ptr<FileContext>> file_contexts_;
149+
using FileContextStore =
150+
FixedSizeValueStore<SemIR::CheckIRId, std::unique_ptr<FileContext>>;
151+
FileContextStore file_contexts_;
149152

150153
// Lowered version of the builtin type `type`.
151154
llvm::StructType* type_type_ = nullptr;

0 commit comments

Comments
 (0)