Эх сурвалжийг харах

Add unit tagging to FacetTypeId (#6256)

David Blaikie 6 сар өмнө
parent
commit
a340808062

+ 1 - 0
toolchain/base/BUILD

@@ -126,6 +126,7 @@ cc_library(
     name = "relational_value_store",
     hdrs = ["relational_value_store.h"],
     deps = [
+        ":value_store",
         ":value_store_types",
         "//common:check",
         "@llvm-project//llvm:Support",

+ 1 - 0
toolchain/base/canonical_value_store.h

@@ -25,6 +25,7 @@ namespace Carbon {
 template <typename IdT, typename KeyT, typename ValueT = KeyT>
 class CanonicalValueStore {
  public:
+  using IdType = IdT;
   using KeyType = std::remove_cvref_t<KeyT>;
   using ValueType = ValueStoreTypes<ValueT>::ValueType;
   using RefType = ValueStoreTypes<ValueT>::RefType;

+ 25 - 18
toolchain/base/relational_value_store.h

@@ -9,38 +9,44 @@
 
 #include "common/check.h"
 #include "llvm/ADT/SmallVector.h"
+#include "toolchain/base/value_store.h"
 #include "toolchain/base/value_store_types.h"
 
 namespace Carbon {
 
 // A ValueStore that builds a 1:1 relationship between two IDs.
-// * `RelatedIdT` represents a related ID that can be used to find values in the
-//   store.
+// * `RelatedStoreT` represents a related ValueStore with ids that can be used
+//   to find values in this store.
 // * `IdT` is the actual ID of values in this store, and `IdT::ValueType` is the
 //   value type being stored.
 //
 // The value store builds a mapping so that either ID can be used later to find
-// a value. And the user can query if a related `RelatedIdT` has been used to
-// add a value to the store or not.
+// a value. And the user can query if a related `RelatedStoreT::IdType` has been
+// used to add a value to the store or not.
 //
-// When adding to the store, the user provides the related `RelatedIdT` along
-// with the value being stored, and gets back the ID of the value in the store.
+// When adding to the store, the user provides the related
+// `RelatedStoreT::IdType` along with the value being stored, and gets back the
+// ID of the value in the store.
 //
 // This store requires more storage space than normal ValueStore does, as it
-// requires storing a bit for presence of each `RelatedIdT`. And it allocates
-// memory for values for all IDs up largest ID present in the store, even if
-// they are not yet used.
-template <typename RelatedIdT, typename IdT, typename ValueT>
+// requires storing a bit for presence of each `RelatedStore::IdType`. And it
+// allocates memory for values for all IDs up largest ID present in the store,
+// even if they are not yet used.
+template <typename RelatedStoreT, typename IdT, typename ValueT>
 class RelationalValueStore {
  public:
+  using RelatedIdType = RelatedStoreT::IdType;
   using ValueType = ValueStoreTypes<ValueT>::ValueType;
   using ConstRefType = ValueStoreTypes<ValueT>::ConstRefType;
 
+  explicit RelationalValueStore(const RelatedStoreT* related_store)
+      : related_store_(related_store) {}
+
   // Given the related ID and a value, stores the value and returns a mapped ID
   // to reference it in the store.
-  auto Add(RelatedIdT related_id, ValueType value) -> IdT {
-    CARBON_DCHECK(related_id.index >= 0, "{0}", related_id);
-    IdT id(related_id.index);
+  auto Add(RelatedIdType related_id, ValueType value) -> IdT {
+    auto related_index = related_store_->GetRawIndex(related_id);
+    IdT id(related_index);
     if (static_cast<size_t>(id.index) >= values_.size()) {
       values_.resize(id.index + 1);
     }
@@ -53,16 +59,16 @@ class RelationalValueStore {
 
   // Returns the ID of a value in the store if the `related_id` was previously
   // used to add a value to the store, or None.
-  auto TryGetId(RelatedIdT related_id) const -> IdT {
-    CARBON_DCHECK(related_id.index >= 0, "{0}", related_id);
-    if (static_cast<size_t>(related_id.index) >= values_.size()) {
+  auto TryGetId(RelatedIdType related_id) const -> IdT {
+    auto related_index = related_store_->GetRawIndex(related_id);
+    if (static_cast<size_t>(related_index) >= values_.size()) {
       return IdT::None;
     }
-    auto& opt = values_[related_id.index];
+    auto& opt = values_[related_index];
     if (!opt.has_value()) {
       return IdT::None;
     }
-    return IdT(related_id.index);
+    return IdT(related_index);
   }
 
   // Returns a value for an ID.
@@ -75,6 +81,7 @@ class RelationalValueStore {
   // Set inline size to 0 because these will typically be too large for the
   // stack, while this does make File smaller.
   llvm::SmallVector<std::optional<ValueType>, 0> values_;
+  const RelatedStoreT* related_store_;
 };
 
 }  // namespace Carbon

+ 2 - 2
toolchain/check/testdata/basics/raw_sem_ir/one_file.carbon

@@ -383,7 +383,7 @@ fn Foo[T:! type](p: T*) -> (T*, ()) {
 // CHECK:STDOUT:     inst0000000E:    {kind: Namespace, arg0: name_scope00000000, arg1: inst<none>, type: type(inst(NamespaceType))}
 // CHECK:STDOUT:     inst6000000F:    {kind: ImportDecl, arg0: name(Core)}
 // CHECK:STDOUT:     inst60000010:    {kind: Namespace, arg0: name_scope60000001, arg1: inst6000000F, type: type(inst(NamespaceType))}
-// CHECK:STDOUT:     inst60000011:    {kind: FacetType, arg0: facet_type00000000, type: type(TypeType)}
+// CHECK:STDOUT:     inst60000011:    {kind: FacetType, arg0: facet_type60000000, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000012:    {kind: BindSymbolicName, arg0: entity_name60000000, arg1: inst<none>, type: type(inst60000011)}
 // CHECK:STDOUT:     inst60000013:    {kind: BindSymbolicName, arg0: entity_name60000000, arg1: inst<none>, type: type(inst60000011)}
 // CHECK:STDOUT:     inst60000014:    {kind: BindSymbolicName, arg0: entity_name60000001, arg1: inst<none>, type: type(TypeType)}
@@ -437,7 +437,7 @@ fn Foo[T:! type](p: T*) -> (T*, ()) {
 // CHECK:STDOUT:     inst60000044:    {kind: RequireCompleteType, arg0: inst6000001B, type: type(inst(WitnessType))}
 // CHECK:STDOUT:     inst60000045:    {kind: ImportRefLoaded, arg0: import_ir_inst00000000, arg1: entity_name60000003, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000046:    {kind: InterfaceDecl, arg0: interface60000000, arg1: inst_block_empty, type: type(TypeType)}
-// CHECK:STDOUT:     inst60000047:    {kind: FacetType, arg0: facet_type00000001, type: type(TypeType)}
+// CHECK:STDOUT:     inst60000047:    {kind: FacetType, arg0: facet_type60000001, type: type(TypeType)}
 // CHECK:STDOUT:     inst60000048:    {kind: BindSymbolicName, arg0: entity_name60000004, arg1: inst<none>, type: type(inst60000047)}
 // CHECK:STDOUT:     inst60000049:    {kind: ImportRefUnloaded, arg0: import_ir_inst00000002, arg1: entity_name<none>}
 // CHECK:STDOUT:     inst6000004A:    {kind: ImportRefLoaded, arg0: import_ir_inst00000003, arg1: entity_name<none>, type: type(inst6000004C)}

+ 2 - 0
toolchain/sem_ir/file.cpp

@@ -41,6 +41,8 @@ File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
       classes_(check_ir_id),
       interfaces_(check_ir_id),
       associated_constants_(check_ir_id),
+      facet_types_(check_ir_id),
+      identified_facet_types_(&facet_types_),
       impls_(*this),
       specific_interfaces_(check_ir_id),
       generics_(check_ir_id),

+ 1 - 1
toolchain/sem_ir/file.h

@@ -69,7 +69,7 @@ using CustomLayoutStore = BlockValueStore<CustomLayoutId, uint64_t>;
 class File : public Printable<File> {
  public:
   using IdentifiedFacetTypeStore =
-      RelationalValueStore<FacetTypeId, IdentifiedFacetTypeId,
+      RelationalValueStore<FacetTypeInfoStore, IdentifiedFacetTypeId,
                            IdentifiedFacetType>;
 
   // Starts a new file for Check::CheckParseTree.