Refactor AddInst-family functions to their own file (#4941)

This in particular uses free functions because it's likely to end up
more consistent with types (versus a wrapper object for InstStore).
Note, this is unlikely to have a performance impact, but if it does, we
can look into related approaches (and we've already discussed using
LTO).

Renames `PendingBlock::AddInst` to `PendingBlock::Add` because
`MakeElementAccessInst` expects the matching name to exist.
This commit is contained in:
Jon Ross-Perkins
2025-02-14 19:44:36 +00:00
committed by GitHub
parent dd7c64bad0
commit 311b4ff03d
44 changed files with 875 additions and 695 deletions
+25 -23
View File
@@ -146,10 +146,11 @@ static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
context.inst_block_stack().AddInstId(exit_block.front());
return region.result_id;
}
return context.AddInst<SemIR::SpliceBlock>(
loc_id, {.type_id = context.insts().Get(region.result_id).type_id(),
.block_id = region.block_ids.front(),
.result_id = region.result_id});
return AddInst<SemIR::SpliceBlock>(
context, loc_id,
{.type_id = context.insts().Get(region.result_id).type_id(),
.block_id = region.block_ids.front(),
.result_id = region.result_id});
}
if (context.region_stack().empty()) {
context.TODO(loc_id,
@@ -157,8 +158,8 @@ static auto InsertHere(Context& context, SemIR::ExprRegionId region_id)
"functions.");
return SemIR::ErrorInst::SingletonInstId;
}
context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
{.target_id = region.block_ids.front()}));
AddInst(context, SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
{.target_id = region.block_ids.front()}));
context.inst_block_stack().Pop();
// TODO: this will cumulatively cost O(MN) running time for M blocks
// at the Nth level of the stack. Figure out how to do better.
@@ -219,7 +220,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
auto bind_name = context.insts().GetAs<SemIR::AnyBindName>(bind_name_id);
CARBON_CHECK(!bind_name.value_id.has_value());
bind_name.value_id = value_id;
context.ReplaceInstBeforeConstantUse(bind_name_id, bind_name);
ReplaceInstBeforeConstantUse(context, bind_name_id, bind_name);
context.inst_block_stack().AddInstId(bind_name_id);
break;
}
@@ -251,8 +252,8 @@ auto MatchContext::EmitPatternMatch(Context& context,
return;
}
auto scrutinee_ref = context.insts().Get(scrutinee_ref_id);
auto new_scrutinee = context.AddInst<SemIR::AddrOf>(
context.insts().GetLocId(scrutinee_ref_id),
auto new_scrutinee = AddInst<SemIR::AddrOf>(
context, context.insts().GetLocId(scrutinee_ref_id),
{.type_id = GetPointerType(context, scrutinee_ref.type_id()),
.lvalue_id = scrutinee_ref_id});
AddWork(
@@ -285,13 +286,13 @@ auto MatchContext::EmitPatternMatch(Context& context,
if (param_pattern.runtime_index ==
SemIR::RuntimeParamIndex::Unknown) {
param_pattern.runtime_index = NextRuntimeIndex();
context.ReplaceInstBeforeConstantUse(entry.pattern_id,
param_pattern);
ReplaceInstBeforeConstantUse(context, entry.pattern_id,
param_pattern);
}
AddWork(
{.pattern_id = param_pattern.subpattern_id,
.scrutinee_id = context.AddInst<SemIR::ValueParam>(
pattern.loc_id,
.scrutinee_id = AddInst<SemIR::ValueParam>(
context, pattern.loc_id,
{.type_id = param_pattern.type_id,
.runtime_index = param_pattern.runtime_index,
.pretty_name_id = GetPrettyName(context, param_pattern)})});
@@ -322,13 +323,13 @@ auto MatchContext::EmitPatternMatch(Context& context,
if (param_pattern.runtime_index ==
SemIR::RuntimeParamIndex::Unknown) {
param_pattern.runtime_index = NextRuntimeIndex();
context.ReplaceInstBeforeConstantUse(entry.pattern_id,
param_pattern);
ReplaceInstBeforeConstantUse(context, entry.pattern_id,
param_pattern);
}
AddWork(
{.pattern_id = param_pattern.subpattern_id,
.scrutinee_id = context.AddInst<SemIR::OutParam>(
pattern.loc_id,
.scrutinee_id = AddInst<SemIR::OutParam>(
context, pattern.loc_id,
{.type_id = param_pattern.type_id,
.runtime_index = param_pattern.runtime_index,
.pretty_name_id = GetPrettyName(context, param_pattern)})});
@@ -342,10 +343,11 @@ auto MatchContext::EmitPatternMatch(Context& context,
}
case CARBON_KIND(SemIR::ReturnSlotPattern return_slot_pattern): {
CARBON_CHECK(kind_ == MatchKind::Callee);
auto return_slot_id = context.AddInst<SemIR::ReturnSlot>(
pattern.loc_id, {.type_id = return_slot_pattern.type_id,
.type_inst_id = return_slot_pattern.type_inst_id,
.storage_id = entry.scrutinee_id});
auto return_slot_id = AddInst<SemIR::ReturnSlot>(
context, pattern.loc_id,
{.type_id = return_slot_pattern.type_id,
.type_inst_id = return_slot_pattern.type_inst_id,
.storage_id = entry.scrutinee_id});
bool already_in_lookup =
context.scope_stack()
.LookupOrAddName(SemIR::NameId::ReturnSlot, return_slot_id)
@@ -367,8 +369,8 @@ auto MatchContext::EmitPatternMatch(Context& context,
Initialize(context, pattern.loc_id, var_id, entry.scrutinee_id);
// TODO: Consider using different instruction kinds for assignment
// versus initialization.
context.AddInst<SemIR::Assign>(pattern.loc_id,
{.lhs_id = var_id, .rhs_id = init_id});
AddInst<SemIR::Assign>(context, pattern.loc_id,
{.lhs_id = var_id, .rhs_id = init_id});
}
AddWork(
{.pattern_id = var_pattern.subpattern_id, .scrutinee_id = var_id});