From 5522e4f6138917c58406c9008949b9bca1eb3dd9 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Wed, 22 Nov 2023 13:43:17 -0800 Subject: [PATCH] Switch cross references to a ValueStore. (#3414) As I get ready to add imports, I need an API to add values and this offers a consistent way of doing so. --- toolchain/check/context.cpp | 6 +++--- toolchain/check/context.h | 4 ++++ toolchain/sem_ir/file.cpp | 15 ++++++++------- toolchain/sem_ir/file.h | 14 ++++++++------ toolchain/sem_ir/ids.h | 4 ++++ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/toolchain/check/context.cpp b/toolchain/check/context.cpp index be5331ca5cff..2a5ba63930b2 100644 --- a/toolchain/check/context.cpp +++ b/toolchain/check/context.cpp @@ -658,9 +658,9 @@ class TypeCompleter { auto BuildCrossReferenceValueRepresentation(SemIR::TypeId type_id, SemIR::CrossReference xref) const -> SemIR::ValueRepresentation { - auto xref_inst = context_.sem_ir() - .GetCrossReferenceIR(xref.ir_id) - .insts() + auto xref_inst = context_.cross_reference_irs() + .Get(xref.ir_id) + ->insts() .Get(xref.inst_id); // The canonical description of a type should only have cross-references diff --git a/toolchain/check/context.h b/toolchain/check/context.h index df54c76769bb..06ee593a03eb 100644 --- a/toolchain/check/context.h +++ b/toolchain/check/context.h @@ -300,6 +300,10 @@ class Context { auto classes() -> ValueStore& { return sem_ir().classes(); } + auto cross_reference_irs() + -> ValueStore& { + return sem_ir().cross_reference_irs(); + } auto names() -> SemIR::NameStoreWrapper { return sem_ir().names(); } auto name_scopes() -> SemIR::NameScopeStore& { return sem_ir().name_scopes(); diff --git a/toolchain/sem_ir/file.cpp b/toolchain/sem_ir/file.cpp index 067d9ae89d82..0ca0e6dce156 100644 --- a/toolchain/sem_ir/file.cpp +++ b/toolchain/sem_ir/file.cpp @@ -44,10 +44,12 @@ auto TypeInfo::Print(llvm::raw_ostream& out) const -> void { File::File(SharedValueStores& value_stores) : value_stores_(&value_stores), filename_(""), - // Builtins are always the first IR, even when self-referential. - cross_reference_irs_({this}), type_blocks_(allocator_), inst_blocks_(allocator_) { + auto builtins_id = cross_reference_irs_.Add(this); + CARBON_CHECK(builtins_id == CrossReferenceIRId::Builtins) + << "Builtins must be the first IR, even if self-referential"; + // Default entry for InstBlockId::Empty. inst_blocks_.AddDefaultValue(); @@ -72,13 +74,12 @@ File::File(SharedValueStores& value_stores, std::string filename, const File* builtins) : value_stores_(&value_stores), filename_(std::move(filename)), - // Builtins are always the first IR. - cross_reference_irs_({builtins}), type_blocks_(allocator_), inst_blocks_(allocator_) { CARBON_CHECK(builtins != nullptr); - CARBON_CHECK(builtins->cross_reference_irs_[0] == builtins) - << "Not called with builtins!"; + auto builtins_id = cross_reference_irs_.Add(builtins); + CARBON_CHECK(builtins_id == CrossReferenceIRId::Builtins) + << "Builtins must be the first IR"; // Default entry for InstBlockId::Empty. inst_blocks_.AddDefaultValue(); @@ -482,7 +483,7 @@ auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory { case CrossReference::Kind: { auto xref = inst.As(); - ir = &ir->GetCrossReferenceIR(xref.ir_id); + ir = ir->cross_reference_irs().Get(xref.ir_id); inst_id = xref.inst_id; continue; } diff --git a/toolchain/sem_ir/file.h b/toolchain/sem_ir/file.h index 1d22ee7cc8ab..0e79c54a6b40 100644 --- a/toolchain/sem_ir/file.h +++ b/toolchain/sem_ir/file.h @@ -204,11 +204,6 @@ class File : public Printable { .getZExtValue(); } - // Returns the requested IR. - auto GetCrossReferenceIR(CrossReferenceIRId xref_id) const -> const File& { - return *cross_reference_irs_[xref_id.index]; - } - // Marks a type as complete, and sets its value representation. auto CompleteType(TypeId object_type_id, ValueRepresentation value_representation) -> void { @@ -300,6 +295,13 @@ class File : public Printable { } auto classes() -> ValueStore& { return classes_; } auto classes() const -> const ValueStore& { return classes_; } + auto cross_reference_irs() -> ValueStore& { + return cross_reference_irs_; + } + auto cross_reference_irs() const + -> const ValueStore& { + return cross_reference_irs_; + } auto names() const -> NameStoreWrapper { return NameStoreWrapper(&identifiers()); } @@ -360,7 +362,7 @@ class File : public Printable { // Related IRs. There will always be at least 2 entries, the builtin IR (used // for references of builtins) followed by the current IR (used for references // crossing instruction blocks). - llvm::SmallVector cross_reference_irs_; + ValueStore cross_reference_irs_; // Storage for name scopes. NameScopeStore name_scopes_; diff --git a/toolchain/sem_ir/ids.h b/toolchain/sem_ir/ids.h index 2b10949e67d3..a8644622338a 100644 --- a/toolchain/sem_ir/ids.h +++ b/toolchain/sem_ir/ids.h @@ -83,6 +83,7 @@ constexpr ClassId ClassId::Invalid = ClassId(ClassId::InvalidIndex); // The ID of a cross-referenced IR. struct CrossReferenceIRId : public IndexBase, public Printable { + static const CrossReferenceIRId Builtins; using IndexBase::IndexBase; auto Print(llvm::raw_ostream& out) const -> void { out << "ir"; @@ -90,6 +91,9 @@ struct CrossReferenceIRId : public IndexBase, } }; +constexpr CrossReferenceIRId CrossReferenceIRId::Builtins = + CrossReferenceIRId(0); + // A boolean value. struct BoolValue : public IndexBase, public Printable { static const BoolValue False;