Просмотр исходного кода

Import symbolic vtable entries as attached constants (#5871)

Similar to 26ec78ec00cc00f7d6c4c6edf94b8f558100d4b8 - vtable entries
can't be unattached symbolic constants, because if they are they can't
be `GetValueInSpecific`d because they lack the context for their
generic/specific.

Importing mostly wants to make/import things as unattached constants -
but `ImportRef` supports attached constants, so use those - but we don't
need the laziness, so use `LoadedImportRef`.
David Blaikie 8 месяцев назад
Родитель
Сommit
545354a3b4

+ 1 - 0
toolchain/check/class.cpp

@@ -187,6 +187,7 @@ static auto BuildVtable(Context& context, Parse::ClassDefinitionId node_id,
     // TODO: Avoid quadratic search. Perhaps build a map from `NameId` to the
     // elements of the top of `vtable_stack`.
     for (auto base_vtable_entry_id : base_vtable_inst_block) {
+      LoadImportRef(context, base_vtable_entry_id);
       auto [derived_vtable_entry_id, derived_vtable_entry_const_id, fn_id,
             specific_id] =
           DecomposeVirtualFunction(context.sem_ir(), base_vtable_entry_id,

+ 33 - 6
toolchain/check/import_ref.cpp

@@ -1977,20 +1977,46 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
                                        .Get(import_vtable.class_id)
                                        .first_owning_decl_id);
 
+  auto specific_data = GetLocalSpecificData(resolver, inst.specific_id);
+
+  auto class_const_inst = resolver.local_insts().Get(
+      resolver.local_constant_values().GetInstId(class_const_id));
+
   // TODO: Ensure the vtable is only imported once, in eg: if there's distinct
   // vtable constants (imported from multiple libraries using the vtable) that
   // refer to the same vtable, the vtable should still be singular.
   auto virtual_functions =
-      GetLocalInstBlockContents(resolver, import_vtable.virtual_functions_id);
+      resolver.import_inst_blocks().Get(import_vtable.virtual_functions_id);
 
-  auto specific_data = GetLocalSpecificData(resolver, inst.specific_id);
+  llvm::SmallVector<SemIR::InstId> lazy_virtual_functions;
+  lazy_virtual_functions.reserve(virtual_functions.size());
+  for (auto vtable_entry_id : virtual_functions) {
+    auto local_attached_constant_id = GetLocalConstantId(
+        resolver,
+        resolver.import_constant_values().GetAttached(vtable_entry_id));
+    lazy_virtual_functions.push_back(
+        resolver.local_constant_values().GetInstIdIfValid(
+            local_attached_constant_id));
+  }
 
   if (resolver.HasNewWork()) {
     return ResolveResult::Retry();
   }
 
-  auto class_const_inst = resolver.local_insts().Get(
-      resolver.local_constant_values().GetInstId(class_const_id));
+  for (auto& vtable_entry_id : lazy_virtual_functions) {
+    // Use LoadedImportRef for imported symbolic constant vtable entries so they
+    // can carry attached constants necessary for applying specifics to these
+    // constants when they are used.
+    auto local_attached_constant_id =
+        resolver.local_constant_values().Get(vtable_entry_id);
+    if (local_attached_constant_id.is_symbolic()) {
+      vtable_entry_id = AddLoadedImportRef(
+          resolver,
+          GetSingletonType(resolver.local_context(),
+                           SemIR::SpecificFunctionType::TypeInstId),
+          vtable_entry_id, local_attached_constant_id);
+    }
+  }
 
   auto class_id = SemIR::ClassId::None;
   if (class_const_inst.Is<SemIR::ClassType>()) {
@@ -2001,10 +2027,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
             class_const_inst.type_id());
     class_id = generic_class_type.class_id;
   }
+
   auto new_vtable_id = resolver.local_vtables().Add(
       {{.class_id = class_id,
-        .virtual_functions_id = GetLocalCanonicalInstBlockId(
-            resolver, import_vtable.virtual_functions_id, virtual_functions)}});
+        .virtual_functions_id =
+            resolver.local_inst_blocks().Add(lazy_virtual_functions)}});
 
   return ResolveAsDeduplicated<SemIR::VtablePtr>(
       resolver, {.type_id = GetPointerType(resolver.local_context(),

+ 16 - 13
toolchain/check/testdata/class/virtual_modifiers.carbon

@@ -6784,19 +6784,19 @@ class T2(G2:! type) {
 // CHECK:STDOUT:   %Base.F.e26: %Base.F.type.f17 = struct_value () [symbolic]
 // CHECK:STDOUT:   %pattern_type.9f7: type = pattern_type %Base.370 [symbolic]
 // CHECK:STDOUT:   %Base.F.specific_fn.892: <specific function> = specific_function %Base.F.e26, @Base.F(%T) [symbolic]
-// CHECK:STDOUT:   %Base.vtable_ptr.f4433b.1: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T) [symbolic]
+// CHECK:STDOUT:   %Base.vtable_ptr.f15b84.1: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T) [symbolic]
 // CHECK:STDOUT:   %require_complete: <witness> = require_complete_type %Base.370 [symbolic]
-// CHECK:STDOUT:   %Base.vtable_ptr.f4433b.2: ref %ptr.454 = vtable_ptr @Base.vtable.2, @Base(%T) [symbolic]
+// CHECK:STDOUT:   %Base.vtable_ptr.f15b84.2: ref %ptr.454 = vtable_ptr @Base.vtable.2, @Base(%T) [symbolic]
 // CHECK:STDOUT:   %Base.ea5: type = class_type @Base, @Base(%T1) [concrete]
 // CHECK:STDOUT:   %Base.F.type.d82: type = fn_type @Base.F, @Base(%T1) [concrete]
 // CHECK:STDOUT:   %Base.F.d25: %Base.F.type.d82 = struct_value () [concrete]
 // CHECK:STDOUT:   %pattern_type.3bf: type = pattern_type %Base.ea5 [concrete]
 // CHECK:STDOUT:   %Base.F.specific_fn.210: <specific function> = specific_function %Base.F.d25, @Base.F(%T1) [concrete]
-// CHECK:STDOUT:   %Base.vtable_ptr.5be: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T1) [concrete]
+// CHECK:STDOUT:   %Base.vtable_ptr.88e: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T1) [concrete]
 // CHECK:STDOUT:   %empty_struct_type: type = struct_type {} [concrete]
-// CHECK:STDOUT:   %Base.vtable_ptr.f4433b.3: ref %ptr.454 = vtable_ptr @Base.vtable.3, @Base(%T) [symbolic]
+// CHECK:STDOUT:   %Base.vtable_ptr.f15b84.3: ref %ptr.454 = vtable_ptr @Base.vtable.3, @Base(%T) [symbolic]
 // CHECK:STDOUT:   %.b74: ref %ptr.454 = class_element_access file.%v.var, element0 [concrete]
-// CHECK:STDOUT:   %Base.val: %Base.ea5 = struct_value (%Base.vtable_ptr.5be) [concrete]
+// CHECK:STDOUT:   %Base.val: %Base.ea5 = struct_value (%Base.vtable_ptr.88e) [concrete]
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: imports {
@@ -6805,12 +6805,15 @@ class T2(G2:! type) {
 // CHECK:STDOUT:     import Core//prelude
 // CHECK:STDOUT:     import Core//prelude/...
 // CHECK:STDOUT:   }
-// CHECK:STDOUT:   %Main.import_ref.03f: ref %ptr.454 = import_ref Main//generic_lib, loc6_1, loaded [symbolic = @Base.%vtable_ptr (constants.%Base.vtable_ptr.f4433b.3)]
+// CHECK:STDOUT:   %Main.import_ref.03f: ref %ptr.454 = import_ref Main//generic_lib, loc6_1, loaded [symbolic = @Base.%vtable_ptr (constants.%Base.vtable_ptr.f15b84.3)]
 // CHECK:STDOUT:   %Main.import_ref.5ab3ec.1: type = import_ref Main//generic_lib, loc4_17, loaded [symbolic = @Base.%T (constants.%T)]
 // CHECK:STDOUT:   %Main.import_ref.05e: <witness> = import_ref Main//generic_lib, loc6_1, loaded [concrete = constants.%complete_type]
 // CHECK:STDOUT:   %Main.import_ref.8e0 = import_ref Main//generic_lib, inst26 [no loc], unloaded
 // CHECK:STDOUT:   %Main.import_ref.e54 = import_ref Main//generic_lib, loc5_30, unloaded
 // CHECK:STDOUT:   %Main.import_ref.5ab3ec.2: type = import_ref Main//generic_lib, loc4_17, loaded [symbolic = @Base.%T (constants.%T)]
+// CHECK:STDOUT:   %Main.import_ref.4f4e13.1: <specific function> = import_ref Main//generic_lib, inst46 [no loc], loaded [symbolic = constants.%Base.F.specific_fn.892]
+// CHECK:STDOUT:   %Main.import_ref.4f4e13.2: <specific function> = import_ref Main//generic_lib, inst46 [no loc], loaded [symbolic = constants.%Base.F.specific_fn.892]
+// CHECK:STDOUT:   %Main.import_ref.4f4e13.3: <specific function> = import_ref Main//generic_lib, inst46 [no loc], loaded [symbolic = constants.%Base.F.specific_fn.892]
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: file {
@@ -6845,7 +6848,7 @@ class T2(G2:! type) {
 // CHECK:STDOUT:   %Base.F.type: type = fn_type @Base.F, @Base(%T) [symbolic = %Base.F.type (constants.%Base.F.type.f17)]
 // CHECK:STDOUT:   %Base.F: @Base.%Base.F.type (%Base.F.type.f17) = struct_value () [symbolic = %Base.F (constants.%Base.F.e26)]
 // CHECK:STDOUT:   %Base.F.specific_fn: <specific function> = specific_function %Base.F, @Base.F(%T) [symbolic = %Base.F.specific_fn (constants.%Base.F.specific_fn.892)]
-// CHECK:STDOUT:   %vtable_ptr: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T) [symbolic = %vtable_ptr (constants.%Base.vtable_ptr.f4433b.1)]
+// CHECK:STDOUT:   %vtable_ptr: ref %ptr.454 = vtable_ptr @Base.vtable.1, @Base(%T) [symbolic = %vtable_ptr (constants.%Base.vtable_ptr.f15b84.1)]
 // CHECK:STDOUT:
 // CHECK:STDOUT:   class {
 // CHECK:STDOUT:     complete_type_witness = imports.%Main.import_ref.05e
@@ -6858,15 +6861,15 @@ class T2(G2:! type) {
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: vtable @Base.vtable.1 {
-// CHECK:STDOUT:   constants.%Base.F.specific_fn.892
+// CHECK:STDOUT:   imports.%Main.import_ref.4f4e13.1
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: vtable @Base.vtable.2 {
-// CHECK:STDOUT:   constants.%Base.F.specific_fn.892
+// CHECK:STDOUT:   imports.%Main.import_ref.4f4e13.2
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: vtable @Base.vtable.3 {
-// CHECK:STDOUT:   constants.%Base.F.specific_fn.892
+// CHECK:STDOUT:   imports.%Main.import_ref.4f4e13.3
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: generic virtual fn @Base.F(imports.%Main.import_ref.5ab3ec.2: type) [from "generic_lib.carbon"] {
@@ -6884,7 +6887,7 @@ class T2(G2:! type) {
 // CHECK:STDOUT: !entry:
 // CHECK:STDOUT:   %.loc7_20.1: %empty_struct_type = struct_literal ()
 // CHECK:STDOUT:   %.loc7_20.2: ref %ptr.454 = class_element_access file.%v.var, element0 [concrete = constants.%.b74]
-// CHECK:STDOUT:   %.loc7_20.3: init %ptr.454 = initialize_from constants.%Base.vtable_ptr.5be to %.loc7_20.2 [concrete = constants.%Base.vtable_ptr.5be]
+// CHECK:STDOUT:   %.loc7_20.3: init %ptr.454 = initialize_from constants.%Base.vtable_ptr.88e to %.loc7_20.2 [concrete = constants.%Base.vtable_ptr.88e]
 // CHECK:STDOUT:   %.loc7_20.4: init %Base.ea5 = class_init (%.loc7_20.3), file.%v.var [concrete = constants.%Base.val]
 // CHECK:STDOUT:   %.loc7_1: init %Base.ea5 = converted %.loc7_20.1, %.loc7_20.4 [concrete = constants.%Base.val]
 // CHECK:STDOUT:   assign file.%v.var, %.loc7_1
@@ -6898,7 +6901,7 @@ class T2(G2:! type) {
 // CHECK:STDOUT:   %Base.F.type => constants.%Base.F.type.f17
 // CHECK:STDOUT:   %Base.F => constants.%Base.F.e26
 // CHECK:STDOUT:   %Base.F.specific_fn => constants.%Base.F.specific_fn.892
-// CHECK:STDOUT:   %vtable_ptr => constants.%Base.vtable_ptr.f4433b.2
+// CHECK:STDOUT:   %vtable_ptr => constants.%Base.vtable_ptr.f15b84.2
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: specific @Base.F(constants.%T) {
@@ -6917,7 +6920,7 @@ class T2(G2:! type) {
 // CHECK:STDOUT:   %Base.F.type => constants.%Base.F.type.d82
 // CHECK:STDOUT:   %Base.F => constants.%Base.F.d25
 // CHECK:STDOUT:   %Base.F.specific_fn => constants.%Base.F.specific_fn.210
-// CHECK:STDOUT:   %vtable_ptr => constants.%Base.vtable_ptr.5be
+// CHECK:STDOUT:   %vtable_ptr => constants.%Base.vtable_ptr.88e
 // CHECK:STDOUT: }
 // CHECK:STDOUT:
 // CHECK:STDOUT: specific @Base.F(constants.%T1) {