Deduplicate and canonicalize all constants. (#3611)

Rather than producing multiple constants with the same value, fold all
instances of a given constant to the same constant instruction.

A future PR will use this to replace the current type canonicalization
system.
This commit is contained in:
Richard Smith
2024-01-18 21:42:19 +00:00
committed by GitHub
parent 62db3e9ad0
commit 29c294880d
142 changed files with 1603 additions and 1185 deletions
+5 -6
View File
@@ -100,14 +100,13 @@ auto Context::AddPlaceholderInst(SemIR::ParseNodeAndInst parse_node_and_inst)
auto Context::AddConstant(SemIR::Inst inst, bool is_symbolic)
-> SemIR::ConstantId {
// TODO: Deduplicate constants.
auto inst_id = sem_ir().insts().AddInNoBlock(
SemIR::ParseNodeAndInst::Untyped(Parse::NodeId::Invalid, inst));
constants().Add(inst_id);
auto [inst_id, added] = constants().GetOrAdd(inst);
auto const_id = is_symbolic ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
: SemIR::ConstantId::ForTemplateConstant(inst_id);
constant_values().Set(inst_id, const_id);
if (added) {
// TODO: Should `ConstantStore` do this for us?
constant_values().Set(inst_id, const_id);
}
CARBON_VLOG() << "AddConstantInst: " << inst << "\n";
return const_id;