Import ImplWitnessTable into the imports block instead of the constants block. (#5374)

Don't import `ImplWitnessTable` into the `constants` block, because we
generally don't put `Unique` constants there. This matches the handling
of the other kinds of `Unique` constants. In order to keep the
instruction visible in formatted SemIR, add it to the `imports` block
instead.

Also fix a bug in the instruction formatter that resulted in
instructions in the `imports` block being omitted from the output if
they were only referenced by earlier instructions in the `imports` block
and by instructions in the `constants` block. This was already resulting
in some referenced instructions being omitted from the output, but also
occurred frequently for `impl_witness_table` instructions after this
change because it is common for the only reference to those instructions
to be from `impl_witness` instructions in the `constants` block.
This commit is contained in:
Richard Smith
2025-05-01 00:08:43 +00:00
committed by GitHub
parent 34887403ab
commit 797b14eb8e
375 changed files with 3922 additions and 620 deletions
+13 -4
View File
@@ -667,6 +667,9 @@ static auto AddPlaceholderImportedInst(ImportContext& context,
context.local_context(), AddImportIRInst(context, import_inst_id), inst));
CARBON_VLOG_TO(context.local_context().vlog_stream(),
"AddImportedInst: {0}\n", static_cast<SemIR::Inst>(inst));
// Track the instruction in the imports block so that it's included in
// formatted SemIR if it's referenced.
context.local_context().import_ref_ids().push_back(inst_id);
return inst_id;
}
@@ -2557,7 +2560,8 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
SemIR::ImplWitnessTable inst) -> ResolveResult {
SemIR::ImplWitnessTable inst,
SemIR::InstId import_inst_id) -> ResolveResult {
const auto& import_impl = resolver.import_impls().Get(inst.impl_id);
auto import_decl_inst_id = import_impl.first_decl_id();
auto local_decl_inst_id =
@@ -2570,8 +2574,13 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver.local_insts().GetAs<SemIR::ImplDecl>(local_decl_inst_id);
auto impl_id = impl_decl.impl_id;
auto elements_id = GetLocalImportRefInstBlock(resolver, inst.elements_id);
return ResolveAs<SemIR::ImplWitnessTable>(
resolver, {.elements_id = elements_id, .impl_id = impl_id});
// Create a corresponding instruction to represent the table.
auto inst_id = AddImportedInst<SemIR::ImplWitnessTable>(
resolver, import_inst_id,
{.elements_id = elements_id, .impl_id = impl_id});
return ResolveResult::Done(resolver.local_constant_values().Get(inst_id),
inst_id);
}
static auto TryResolveTypedInst(ImportRefResolver& resolver,
@@ -2940,7 +2949,7 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
return TryResolveTypedInst(resolver, inst);
}
case CARBON_KIND(SemIR::ImplWitnessTable inst): {
return TryResolveTypedInst(resolver, inst);
return TryResolveTypedInst(resolver, inst, inst_id);
}
case CARBON_KIND(SemIR::ImportRefLoaded inst): {
return TryResolveTypedInst(resolver, inst, inst_id);