Files
carbon-lang/toolchain/sem_ir/bind_name.h
T
Richard Smith 5c8fa6ad5c Replace FoldingSet with DenseMap for instruction canonicalization. (#3979)
Switch from recursing into non-canonical instruction fields to
separately canonicalizing those fields. This means we now form canonical
`InstBlockId`s, `TypeBlockId`s, `IntId`s, `FloatId`s, and `BindNameId`s
at least in the cases when they're referenced by a constant instruction.

This reduces the overall runtime for @chandlerc's 10MLoC example by
27.5% on my machine.
2024-05-23 00:48:49 +00:00

77 lines
2.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_SEM_IR_BIND_NAME_H_
#define CARBON_TOOLCHAIN_SEM_IR_BIND_NAME_H_
#include "common/hashing.h"
#include "toolchain/base/value_store.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
struct BindNameInfo : public Printable<BindNameInfo> {
auto Print(llvm::raw_ostream& out) const -> void {
out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id
<< ", index: " << bind_index << "}";
}
// The name.
NameId name_id;
// The enclosing scope.
NameScopeId enclosing_scope_id;
// The index for a compile-time binding. Invalid for a runtime binding.
CompileTimeBindIndex bind_index;
};
// Hashing for BindNameInfo.
inline auto CarbonHashValue(const BindNameInfo& value, uint64_t seed)
-> HashCode {
Hasher hasher(seed);
hasher.Hash(value);
return static_cast<HashCode>(hasher);
}
// DenseMapInfo for BindNameInfo.
struct BindNameInfoDenseMapInfo {
static auto getEmptyKey() -> BindNameInfo {
return BindNameInfo{.name_id = NameId::Invalid,
.enclosing_scope_id = NameScopeId::Invalid,
.bind_index = CompileTimeBindIndex(
CompileTimeBindIndex::InvalidIndex - 1)};
}
static auto getTombstoneKey() -> BindNameInfo {
return BindNameInfo{.name_id = NameId::Invalid,
.enclosing_scope_id = NameScopeId::Invalid,
.bind_index = CompileTimeBindIndex(
CompileTimeBindIndex::InvalidIndex - 2)};
}
static auto getHashValue(const BindNameInfo& val) -> unsigned {
return static_cast<uint64_t>(HashValue(val));
}
static auto isEqual(const BindNameInfo& lhs, const BindNameInfo& rhs)
-> bool {
return std::memcmp(&lhs, &rhs, sizeof(BindNameInfo)) == 0;
}
};
// Value store for BindNameInfo. In addition to the regular ValueStore
// functionality, this can provide optional canonical IDs for BindNameInfos.
struct BindNameStore : public ValueStore<BindNameId> {
public:
// Convert an ID to a canonical ID. All calls to this with equivalent
// `BindNameInfo`s will return the same `BindNameId`.
auto MakeCanonical(BindNameId id) -> BindNameId {
return canonical_ids_.insert({Get(id), id}).first->second;
}
private:
llvm::DenseMap<BindNameInfo, BindNameId, BindNameInfoDenseMapInfo>
canonical_ids_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_BIND_NAME_H_