block_value_store.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_
  6. #include <type_traits>
  7. #include "common/hashing.h"
  8. #include "llvm/ADT/DenseMap.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/base/yaml.h"
  11. namespace Carbon::SemIR {
  12. // Provides a block-based ValueStore, which uses slab allocation of added
  13. // blocks. This allows references to values to outlast vector resizes that might
  14. // otherwise invalidate references.
  15. //
  16. // BlockValueStore is used as-is, but there are also children that expose the
  17. // protected members for type-specific functionality.
  18. //
  19. // On IdT, this requires:
  20. // - IdT::ElementType to represent the underlying type in the block.
  21. // - IdT::ValueType to be llvm::MutableArrayRef<IdT::ElementType> for
  22. // compatibility with ValueStore.
  23. template <typename IdT>
  24. class BlockValueStore : public Yaml::Printable<BlockValueStore<IdT>> {
  25. public:
  26. using ElementType = IdT::ElementType;
  27. explicit BlockValueStore(llvm::BumpPtrAllocator& allocator)
  28. : allocator_(&allocator) {}
  29. // Adds a block with the given content, returning an ID to reference it.
  30. auto Add(llvm::ArrayRef<ElementType> content) -> IdT {
  31. return values_.Add(AllocateCopy(content));
  32. }
  33. // Returns the requested block.
  34. auto Get(IdT id) const -> llvm::ArrayRef<ElementType> {
  35. return values_.Get(id);
  36. }
  37. // Returns the requested block.
  38. auto Get(IdT id) -> llvm::MutableArrayRef<ElementType> {
  39. return values_.Get(id);
  40. }
  41. // Adds a block or finds an existing canonical block with the given content,
  42. // and returns an ID to reference it.
  43. auto AddCanonical(llvm::ArrayRef<ElementType> content) -> IdT {
  44. auto [it, added] = canonical_blocks_.insert({{content}, IdT::Invalid});
  45. if (added) {
  46. auto id = Add(content);
  47. it->first.data = Get(id);
  48. it->second = id;
  49. }
  50. return it->second;
  51. }
  52. // Promotes an existing block ID to a canonical block ID, or returns an
  53. // existing canonical block ID if the block was already added. The specified
  54. // block must not be modified after this point.
  55. auto MakeCanonical(IdT id) -> IdT {
  56. return canonical_blocks_.insert({{Get(id)}, id}).first->second;
  57. }
  58. auto OutputYaml() const -> Yaml::OutputMapping {
  59. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  60. for (auto block_index : llvm::seq(values_.size())) {
  61. auto block_id = IdT(block_index);
  62. map.Add(PrintToString(block_id),
  63. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  64. auto block = Get(block_id);
  65. for (auto i : llvm::seq(block.size())) {
  66. map.Add(llvm::itostr(i), Yaml::OutputScalar(block[i]));
  67. }
  68. }));
  69. }
  70. });
  71. }
  72. auto size() const -> int { return values_.size(); }
  73. protected:
  74. // Reserves and returns a block ID. The contents of the block
  75. // should be specified by calling Set, or similar.
  76. auto AddDefaultValue() -> IdT { return values_.AddDefaultValue(); }
  77. // Adds an uninitialized block of the given size.
  78. auto AddUninitialized(size_t size) -> IdT {
  79. return values_.Add(AllocateUninitialized(size));
  80. }
  81. // Sets the contents of an empty block to the given content.
  82. auto Set(IdT block_id, llvm::ArrayRef<ElementType> content) -> void {
  83. CARBON_CHECK(Get(block_id).empty())
  84. << "inst block content set more than once";
  85. values_.Get(block_id) = AllocateCopy(content);
  86. }
  87. private:
  88. // A canonical block, for which we allocate a deduplicated ID.
  89. struct CanonicalBlock {
  90. // This is mutable so we can repoint it at the allocated data if insertion
  91. // succeeds.
  92. mutable llvm::ArrayRef<ElementType> data;
  93. // See common/hashing.h.
  94. friend auto CarbonHashValue(CanonicalBlock block, uint64_t seed)
  95. -> HashCode {
  96. Hasher hasher(seed);
  97. hasher.HashSizedBytes(block.data);
  98. return static_cast<HashCode>(hasher);
  99. }
  100. };
  101. struct CanonicalBlockDenseMapInfo {
  102. // Blocks whose data() points to the start of `SpecialData` are used to
  103. // represent the special "empty" and "tombstone" states.
  104. static constexpr ElementType SpecialData[1] = {ElementType::Invalid};
  105. static auto getEmptyKey() -> CanonicalBlock {
  106. return CanonicalBlock{
  107. llvm::ArrayRef(SpecialData, static_cast<size_t>(0))};
  108. }
  109. static auto getTombstoneKey() -> CanonicalBlock {
  110. return CanonicalBlock{llvm::ArrayRef(SpecialData, 1)};
  111. }
  112. static auto getHashValue(CanonicalBlock val) -> unsigned {
  113. return static_cast<uint64_t>(HashValue(val));
  114. }
  115. static auto isEqual(CanonicalBlock lhs, CanonicalBlock rhs) -> bool {
  116. return lhs.data == rhs.data && (lhs.data.data() == SpecialData) ==
  117. (rhs.data.data() == SpecialData);
  118. }
  119. };
  120. // Allocates an uninitialized array using our slab allocator.
  121. auto AllocateUninitialized(std::size_t size)
  122. -> llvm::MutableArrayRef<ElementType> {
  123. // We're not going to run a destructor, so ensure that's OK.
  124. static_assert(std::is_trivially_destructible_v<ElementType>);
  125. auto storage = static_cast<ElementType*>(
  126. allocator_->Allocate(size * sizeof(ElementType), alignof(ElementType)));
  127. return llvm::MutableArrayRef<ElementType>(storage, size);
  128. }
  129. // Allocates a copy of the given data using our slab allocator.
  130. auto AllocateCopy(llvm::ArrayRef<ElementType> data)
  131. -> llvm::MutableArrayRef<ElementType> {
  132. auto result = AllocateUninitialized(data.size());
  133. std::uninitialized_copy(data.begin(), data.end(), result.begin());
  134. return result;
  135. }
  136. llvm::BumpPtrAllocator* allocator_;
  137. ValueStore<IdT> values_;
  138. llvm::DenseMap<CanonicalBlock, IdT, CanonicalBlockDenseMapInfo>
  139. canonical_blocks_;
  140. };
  141. } // namespace Carbon::SemIR
  142. #endif // CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_