diff --git a/toolchain/driver/compile_driver.h b/toolchain/driver/compile_driver.h index 725933737746..22ae8bd9725b 100644 --- a/toolchain/driver/compile_driver.h +++ b/toolchain/driver/compile_driver.h @@ -242,8 +242,8 @@ class MultiUnitCache { auto short_file_names() -> const SemIR::ShortFileNameStore& { if (!short_file_names_) { - SemIR::InstNamer::Namespace compilation_namespace; - llvm::SmallVector namespace_names; + SemIR::TextualNamespace compilation_namespace; + llvm::SmallVector namespace_names; namespace_names.reserve(units_.size()); for (auto [i, unit] : llvm::enumerate(units_)) { std::string short_name; diff --git a/toolchain/sem_ir/inst_namer.cpp b/toolchain/sem_ir/inst_namer.cpp index e934c34285cf..e50be9fffb83 100644 --- a/toolchain/sem_ir/inst_namer.cpp +++ b/toolchain/sem_ir/inst_namer.cpp @@ -32,6 +32,79 @@ namespace Carbon::SemIR { +auto TextualNamespace::Name::GetFullName() const -> llvm::StringRef { + if (!value_) { + return ""; + } + llvm::StringMapEntry* value = value_; + while (value->second.ambiguous && value->second.fallback) { + value = value->second.fallback.value_; + } + return value->first(); +} + +auto TextualNamespace::Name::GetBaseName() const -> llvm::StringRef { + if (!value_) { + return ""; + } + return value_->first().take_front(base_name_size_); +} + +auto TextualNamespace::AllocateNameWithSuffixes( + std::string full_name, llvm::ArrayRef name_sizes) -> Name { + // The best (shortest) name for this instruction so far, and the current + // name for it. + Name best; + Name current; + + const size_t base_name_size = name_sizes[0]; + + // Add `name` as a name for this entity. + auto add_name = [&](llvm::StringRef name, bool mark_ambiguous = true) { + auto [it, added] = allocated_.insert({name, NameResult()}); + Name new_name = Name(it, base_name_size); + + if (!added) { + if (mark_ambiguous) { + // This name was allocated for a different instruction. Mark it as + // ambiguous and keep looking for a name for this instruction. + new_name.SetAmbiguous(); + } + } else { + if (!best) { + best = new_name; + } else { + CARBON_CHECK(current); + current.SetFallback(new_name); + } + current = new_name; + } + return added; + }; + + int previous_size = -1; + for (auto name_size : name_sizes) { + CARBON_CHECK(name_size > previous_size, "`name_sizes` not sorted"); + previous_size = name_size; + if (name_size > 0) { + add_name(llvm::StringRef(full_name).take_front(name_size)); + } + } + CARBON_CHECK(static_cast(previous_size) == full_name.size(), + "`name_sizes` doesn't end with the size of `full_name`"); + + // Append numbers until we find an available name. + full_name.push_back('.'); + auto name_size_without_counter = full_name.size(); + for (int counter = 1;; ++counter) { + full_name.resize(name_size_without_counter); + llvm::raw_string_ostream(full_name) << counter; + if (add_name(full_name, /*mark_ambiguous=*/false)) { + return best; + } + } +} + class InstNamer::NamingContext { public: explicit NamingContext(InstNamer* inst_namer, InstNamer::ScopeId scope_id, @@ -107,7 +180,8 @@ InstNamer::InstNamer(const File* sem_ir, int total_ir_count) : sem_ir_(sem_ir), fingerprinter_(total_ir_count) { CARBON_CHECK(sem_ir_->is_complete(), "Can't create InstNamer for pending SemIR::File"); - insts_.resize(sem_ir->insts().size(), {ScopeId::None, Namespace::Name()}); + insts_.resize(sem_ir->insts().size(), + {ScopeId::None, TextualNamespace::Name()}); labels_.resize(sem_ir->inst_blocks().size()); scopes_.resize(GetScopeIdOffset(ScopeIdTypeEnum::None)); generic_scopes_.resize(sem_ir->generics().size(), ScopeId::None); @@ -326,79 +400,6 @@ auto InstNamer::GetLabelFor(ScopeId scope_id, InstBlockId block_id) const return (GetScopeName(label_scope) + ".!" + label_name.GetFullName()).str(); } -auto InstNamer::Namespace::Name::GetFullName() const -> llvm::StringRef { - if (!value_) { - return ""; - } - llvm::StringMapEntry* value = value_; - while (value->second.ambiguous && value->second.fallback) { - value = value->second.fallback.value_; - } - return value->first(); -} - -auto InstNamer::Namespace::Name::GetBaseName() const -> llvm::StringRef { - if (!value_) { - return ""; - } - return value_->first().take_front(base_name_size_); -} - -auto InstNamer::Namespace::AllocateNameWithSuffixes( - std::string full_name, llvm::ArrayRef name_sizes) -> Name { - // The best (shortest) name for this instruction so far, and the current - // name for it. - Name best; - Name current; - - const size_t base_name_size = name_sizes[0]; - - // Add `name` as a name for this entity. - auto add_name = [&](llvm::StringRef name, bool mark_ambiguous = true) { - auto [it, added] = allocated_.insert({name, NameResult()}); - Name new_name = Name(it, base_name_size); - - if (!added) { - if (mark_ambiguous) { - // This name was allocated for a different instruction. Mark it as - // ambiguous and keep looking for a name for this instruction. - new_name.SetAmbiguous(); - } - } else { - if (!best) { - best = new_name; - } else { - CARBON_CHECK(current); - current.SetFallback(new_name); - } - current = new_name; - } - return added; - }; - - int previous_size = -1; - for (auto name_size : name_sizes) { - CARBON_CHECK(name_size > previous_size, "`name_sizes` not sorted"); - previous_size = name_size; - if (name_size > 0) { - add_name(llvm::StringRef(full_name).take_front(name_size)); - } - } - CARBON_CHECK(static_cast(previous_size) == full_name.size(), - "`name_sizes` doesn't end with the size of `full_name`"); - - // Append numbers until we find an available name. - full_name.push_back('.'); - auto name_size_without_counter = full_name.size(); - for (int counter = 1;; ++counter) { - full_name.resize(name_size_without_counter); - llvm::raw_string_ostream(full_name) << counter; - if (add_name(full_name, /*mark_ambiguous=*/false)) { - return best; - } - } -} - auto InstNamer::FileNamespace::AllocateName( const InstNamer& inst_namer, std::variant loc_id_or_fingerprint, std::string name) diff --git a/toolchain/sem_ir/inst_namer.h b/toolchain/sem_ir/inst_namer.h index d3ce9419ff28..6e1c00c1f456 100644 --- a/toolchain/sem_ir/inst_namer.h +++ b/toolchain/sem_ir/inst_namer.h @@ -19,6 +19,67 @@ namespace Carbon::SemIR { +// A space in which unique names can be allocated, for use in textual SemIR +// formatting. +class TextualNamespace { + private: + // A result of a name lookup. + struct NameResult; + + public: + // A name in a namespace, which might be redirected to refer to another name + // for disambiguation purposes. + class Name { + public: + explicit Name() : value_(nullptr) {} + explicit Name(llvm::StringMap::iterator it, + size_t base_name_size) + : value_(&*it), base_name_size_(base_name_size) {} + + explicit operator bool() const { return value_; } + + // Returns the disambiguated name. Note that this is not stable until + // all names have been added to the `TextualNamespace`. + auto GetFullName() const -> llvm::StringRef; + + // Returns the base name, without any disambiguators. + auto GetBaseName() const -> llvm::StringRef; + + auto SetFallback(Name name) -> void { value_->second.fallback = name; } + + auto SetAmbiguous() -> void { value_->second.ambiguous = true; } + + private: + llvm::StringMapEntry* value_; + + // The base name length within `value_->first()`. + size_t base_name_size_; + }; + + // Allocates and returns a name, handling ambiguity. + auto AllocateName(std::string name) [[clang::lifetimebound]] -> Name { + auto size = std::ssize(name); + return AllocateNameWithSuffixes(std::move(name), {size}); + } + + // Allocates and returns a name, handling ambiguity. `full_name` is + // expected to be structured as a base name followed by a series of + // suffixes that add progressively more disambiguation. `name_sizes[N]` + // is the size of the base name plus the first N such suffixes + // (so `name_sizes.back()` should be `full_name.size()`). + auto AllocateNameWithSuffixes(std::string full_name, + llvm::ArrayRef name_sizes) + [[clang::lifetimebound]] -> Name; + + private: + struct NameResult { + bool ambiguous = false; + Name fallback = Name(); + }; + + llvm::StringMap allocated_; +}; + // Assigns names to instructions, blocks, and scopes in the Semantics IR. // // If `` occurs in output of valid SemIR, it often means the @@ -27,65 +88,6 @@ namespace Carbon::SemIR { // unable to correctly walk the SemIR. class InstNamer { public: - // A space in which unique names can be allocated. - class Namespace { - private: - // A result of a name lookup. - struct NameResult; - - public: - // A name in a namespace, which might be redirected to refer to another name - // for disambiguation purposes. - class Name { - public: - explicit Name() : value_(nullptr) {} - explicit Name(llvm::StringMap::iterator it, - size_t base_name_size) - : value_(&*it), base_name_size_(base_name_size) {} - - explicit operator bool() const { return value_; } - - // Returns the disambiguated name. Note that this is not stable until - // all names have been added to the `Namespace`. - auto GetFullName() const -> llvm::StringRef; - - // Returns the base name, without any disambiguators. - auto GetBaseName() const -> llvm::StringRef; - - auto SetFallback(Name name) -> void { value_->second.fallback = name; } - - auto SetAmbiguous() -> void { value_->second.ambiguous = true; } - - private: - llvm::StringMapEntry* value_; - - // The base name length within `value_->first()`. - size_t base_name_size_; - }; - - // Allocates and returns a name, handling ambiguity. - auto AllocateName(std::string name) -> Name { - auto size = std::ssize(name); - return AllocateNameWithSuffixes(std::move(name), {size}); - } - - // Allocates and returns a name, handling ambiguity. `full_name` is - // expected to be structured as a base name followed by a series of - // suffixes that add progressively more disambiguation. `name_sizes[N]` - // is the size of the base name plus the first N such suffixes - // (so `name_sizes.back()` should be `full_name.size()`). - auto AllocateNameWithSuffixes(std::string full_name, - llvm::ArrayRef name_sizes) -> Name; - - private: - struct NameResult { - bool ambiguous = false; - Name fallback = Name(); - }; - - llvm::StringMap allocated_; - }; - // int32_t matches the input value size. enum class ScopeId : int32_t { None = -1, @@ -205,7 +207,7 @@ class InstNamer { private: // A namespace within a SemIR::File. - class FileNamespace : public Namespace { + class FileNamespace : public TextualNamespace { public: // Allocates and returns a name, handling ambiguity. auto AllocateName(const InstNamer& inst_namer, @@ -215,7 +217,7 @@ class InstNamer { // A named scope that contains named entities. struct Scope { - Namespace::Name name; + TextualNamespace::Name name; FileNamespace insts; FileNamespace labels; }; @@ -299,10 +301,10 @@ class InstNamer { FileNamespace globals_; // The enclosing scope and name for each instruction, indexed by the InstId's // index. - std::vector> insts_; + std::vector> insts_; // The enclosing scope and name for each block that might be a branch target, // indexed by the InstBlockId's index. - std::vector> labels_; + std::vector> labels_; // The scopes corresponding to ScopeId values. std::vector scopes_; // The scope IDs corresponding to generics. The vector indexes are the