block_value_store.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/set.h"
  8. #include "toolchain/base/value_store.h"
  9. #include "toolchain/base/yaml.h"
  10. namespace Carbon::SemIR {
  11. // Provides a block-based ValueStore, which uses slab allocation of added
  12. // blocks. This allows references to values to outlast vector resizes that might
  13. // otherwise invalidate references.
  14. //
  15. // BlockValueStore is used as-is, but there are also children that expose the
  16. // protected members for type-specific functionality.
  17. //
  18. // On IdT, this requires:
  19. // - IdT::ElementType to represent the underlying type in the block.
  20. // - IdT::ValueType to be llvm::MutableArrayRef<IdT::ElementType> for
  21. // compatibility with ValueStore.
  22. template <typename IdT>
  23. class BlockValueStore : public Yaml::Printable<BlockValueStore<IdT>> {
  24. public:
  25. using ElementType = IdT::ElementType;
  26. explicit BlockValueStore(llvm::BumpPtrAllocator& allocator)
  27. : allocator_(&allocator) {
  28. auto empty = llvm::MutableArrayRef<ElementType>();
  29. auto empty_val = canonical_blocks_.Insert(
  30. empty, [&] { return values_.Add(empty); }, KeyContext(this));
  31. CARBON_CHECK(empty_val.key() == IdT::Empty);
  32. }
  33. // Adds a block with the given content, returning an ID to reference it.
  34. auto Add(llvm::ArrayRef<ElementType> content) -> IdT {
  35. if (content.empty()) {
  36. return IdT::Empty;
  37. }
  38. return values_.Add(AllocateCopy(content));
  39. }
  40. // Returns the requested block.
  41. auto Get(IdT id) const -> llvm::ArrayRef<ElementType> {
  42. return values_.Get(id);
  43. }
  44. // Returns a mutable view of the requested block. This operation should be
  45. // avoided where possible; we generally want blocks to be immutable once
  46. // created.
  47. auto GetMutable(IdT id) -> llvm::MutableArrayRef<ElementType> {
  48. return values_.Get(id);
  49. }
  50. // Returns a new block formed by applying `transform(elem_id)` to each element
  51. // in the specified block.
  52. template <typename TransformFnT>
  53. auto Transform(IdT id, TransformFnT transform) -> IdT {
  54. llvm::SmallVector<ElementType> block(llvm::map_range(Get(id), transform));
  55. return Add(block);
  56. }
  57. // Adds a block or finds an existing canonical block with the given content,
  58. // and returns an ID to reference it.
  59. auto AddCanonical(llvm::ArrayRef<ElementType> content) -> IdT {
  60. if (content.empty()) {
  61. return IdT::Empty;
  62. }
  63. auto result = canonical_blocks_.Insert(
  64. content, [&] { return Add(content); }, KeyContext(this));
  65. return result.key();
  66. }
  67. // Promotes an existing block ID to a canonical block ID, or returns an
  68. // existing canonical block ID if the block was already added. The specified
  69. // block must not be modified after this point.
  70. auto MakeCanonical(IdT id) -> IdT {
  71. // Get the content first so that we don't have unnecessary translation of
  72. // the `id` into the content during insertion.
  73. auto result = canonical_blocks_.Insert(
  74. Get(id), [id] { return id; }, KeyContext(this));
  75. return result.key();
  76. }
  77. auto OutputYaml() const -> Yaml::OutputMapping {
  78. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  79. for (auto [block_id, block] : values_.enumerate()) {
  80. map.Add(PrintToString(block_id),
  81. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  82. for (auto [i, elem_id] : llvm::enumerate(block)) {
  83. map.Add(llvm::itostr(i), Yaml::OutputScalar(elem_id));
  84. }
  85. }));
  86. }
  87. });
  88. }
  89. // Collects memory usage of members.
  90. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  91. -> void {
  92. mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
  93. mem_usage.Collect(MemUsage::ConcatLabel(label, "canonical_blocks_"),
  94. canonical_blocks_, KeyContext(this));
  95. }
  96. auto size() const -> int { return values_.size(); }
  97. protected:
  98. // Allocates a copy of the given data using our slab allocator.
  99. auto AllocateCopy(llvm::ArrayRef<ElementType> data)
  100. -> llvm::MutableArrayRef<ElementType> {
  101. auto result = AllocateUninitialized(data.size());
  102. std::uninitialized_copy(data.begin(), data.end(), result.begin());
  103. return result;
  104. }
  105. // Allocates an uninitialized array using our slab allocator.
  106. auto AllocateUninitialized(size_t size)
  107. -> llvm::MutableArrayRef<ElementType> {
  108. // We're not going to run a destructor, so ensure that's OK.
  109. static_assert(std::is_trivially_destructible_v<ElementType>);
  110. auto storage = static_cast<ElementType*>(
  111. allocator_->Allocate(size * sizeof(ElementType), alignof(ElementType)));
  112. return llvm::MutableArrayRef<ElementType>(storage, size);
  113. }
  114. // Allow children to have more complex value handling.
  115. auto values() -> ValueStore<IdT>& { return values_; }
  116. private:
  117. class KeyContext;
  118. llvm::BumpPtrAllocator* allocator_;
  119. ValueStore<IdT> values_;
  120. Set<IdT, /*SmallSize=*/0, KeyContext> canonical_blocks_;
  121. };
  122. template <typename IdT>
  123. class BlockValueStore<IdT>::KeyContext
  124. : public TranslatingKeyContext<KeyContext> {
  125. public:
  126. explicit KeyContext(const BlockValueStore* store) : store_(store) {}
  127. auto TranslateKey(IdT id) const -> llvm::ArrayRef<ElementType> {
  128. return store_->Get(id);
  129. }
  130. private:
  131. const BlockValueStore* store_;
  132. };
  133. } // namespace Carbon::SemIR
  134. #endif // CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_