Files
carbon-lang/toolchain/sem_ir/constant.cpp
T
Richard Smith 5c8fa6ad5c Replace FoldingSet with DenseMap for instruction canonicalization. (#3979)
Switch from recursing into non-canonical instruction fields to
separately canonicalizing those fields. This means we now form canonical
`InstBlockId`s, `TypeBlockId`s, `IntId`s, `FloatId`s, and `BindNameId`s
at least in the cases when they're referenced by a constant instruction.

This reduces the overall runtime for @chandlerc's 10MLoC example by
27.5% on my machine.
2024-05-23 00:48:49 +00:00

29 lines
1002 B
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/sem_ir/constant.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::SemIR {
auto ConstantStore::GetOrAdd(Inst inst, bool is_symbolic) -> ConstantId {
auto [it, added] = map_.insert({inst, ConstantId::Invalid});
if (added) {
auto inst_id = sem_ir_.insts().AddInNoBlock(LocIdAndInst::NoLoc(inst));
auto const_id = is_symbolic
? SemIR::ConstantId::ForSymbolicConstant(inst_id)
: SemIR::ConstantId::ForTemplateConstant(inst_id);
it->second = const_id;
sem_ir_.constant_values().Set(inst_id, const_id);
constants_.push_back(inst_id);
} else {
CARBON_CHECK(it->second != ConstantId::Invalid);
CARBON_CHECK(it->second.is_symbolic() == is_symbolic);
}
return it->second;
}
} // namespace Carbon::SemIR