Canonicalize generated functions for Core witnesses (#7729)

Use a single `SemIR::Function` per `Core` interface method, whether it's
generated locally or imported. This prevents generating duplicate
functions, which lead to different types when the witness appears in a
`FacetValue` as part of a specific for a class.

We use a `CanonicalValueStore` of `GeneratedFunction` objects that allow
finding an existing FunctionId for a `Generated` special function before
(re-)generating it. Mangling for `Generated` functions is also moved to
use the values from the `GeneratedFunction`'s canonicalization key, so
that we have a consistent source of truth for the unique ID of a
`Generated` function across all files.

New tests are in
`toolchain/check/testdata/impl/custom_witness/destroy.carbon`.
This commit is contained in:
Dana Jansens
2026-09-15 16:02:13 +00:00
committed by GitHub
parent db2e26ba86
commit 4416f3525b
278 changed files with 5941 additions and 5506 deletions
+18
View File
@@ -7,6 +7,7 @@
#include <optional>
#include <variant>
#include "toolchain/base/canonical_value_store_impl.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/base/value_store_impl.h"
#include "toolchain/sem_ir/file.h"
@@ -153,6 +154,20 @@ auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
.specific_id = specific_id};
}
auto Function::GetBuiltinFunctionKind(const File& file) const
-> BuiltinFunctionKind {
switch (special_function_kind) {
case SpecialFunctionKind::Builtin:
return non_generated_builtin_function_kind();
case SpecialFunctionKind::Generated: {
auto generated_id = GeneratedFunctionId(special_function_kind_data.index);
return file.generated_functions().Get(generated_id).builtin_function_kind;
}
default:
return BuiltinFunctionKind::None;
}
}
auto Function::GetDeclaredReturnType(const File& file,
SpecificId specific_id) const -> TypeId {
if (!return_type_inst_id.has_value()) {
@@ -178,4 +193,7 @@ auto Function::GetDeclaredReturnForm(const File& file,
namespace Carbon {
template class ValueStore<SemIR::FunctionId, SemIR::Function,
Tag<SemIR::CheckIRId>>;
template class CanonicalValueStore<
SemIR::GeneratedFunctionId, SemIR::GeneratedFunction::CanonicalKey,
Tag<SemIR::CheckIRId>, SemIR::GeneratedFunction>;
} // namespace Carbon