block_value_store.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Adds a block with the given content, returning an ID to reference it.
  29. auto Add(llvm::ArrayRef<ElementType> content) -> IdT {
  30. return values_.Add(AllocateCopy(content));
  31. }
  32. // Returns the requested block.
  33. auto Get(IdT id) const -> llvm::ArrayRef<ElementType> {
  34. return values_.Get(id);
  35. }
  36. // Returns the requested block.
  37. auto Get(IdT id) -> llvm::MutableArrayRef<ElementType> {
  38. return values_.Get(id);
  39. }
  40. // Adds a block or finds an existing canonical block with the given content,
  41. // and returns an ID to reference it.
  42. auto AddCanonical(llvm::ArrayRef<ElementType> content) -> IdT {
  43. auto result = canonical_blocks_.Insert(
  44. content, [&] { return Add(content); }, KeyContext(this));
  45. return result.key();
  46. }
  47. // Promotes an existing block ID to a canonical block ID, or returns an
  48. // existing canonical block ID if the block was already added. The specified
  49. // block must not be modified after this point.
  50. auto MakeCanonical(IdT id) -> IdT {
  51. // Get the content first so that we don't have unnecessary translation of
  52. // the `id` into the content during insertion.
  53. auto result = canonical_blocks_.Insert(
  54. Get(id), [id] { return id; }, KeyContext(this));
  55. return result.key();
  56. }
  57. auto OutputYaml() const -> Yaml::OutputMapping {
  58. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  59. for (auto block_index : llvm::seq(values_.size())) {
  60. auto block_id = IdT(block_index);
  61. map.Add(PrintToString(block_id),
  62. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  63. auto block = Get(block_id);
  64. for (auto i : llvm::seq(block.size())) {
  65. map.Add(llvm::itostr(i), Yaml::OutputScalar(block[i]));
  66. }
  67. }));
  68. }
  69. });
  70. }
  71. // Collects memory usage of members.
  72. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  73. -> void {
  74. mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
  75. mem_usage.Add(MemUsage::ConcatLabel(label, "canonical_blocks_"),
  76. canonical_blocks_, KeyContext(this));
  77. }
  78. auto size() const -> int { return values_.size(); }
  79. protected:
  80. // Reserves and returns a block ID. The contents of the block
  81. // should be specified by calling Set, or similar.
  82. auto AddDefaultValue() -> IdT { return values_.AddDefaultValue(); }
  83. // Adds an uninitialized block of the given size.
  84. auto AddUninitialized(size_t size) -> IdT {
  85. return values_.Add(AllocateUninitialized(size));
  86. }
  87. // Sets the contents of an empty block to the given content.
  88. auto SetContent(IdT block_id, llvm::ArrayRef<ElementType> content) -> void {
  89. CARBON_CHECK(Get(block_id).empty(),
  90. "inst block content set more than once");
  91. values_.Get(block_id) = AllocateCopy(content);
  92. }
  93. private:
  94. class KeyContext;
  95. // Allocates an uninitialized array using our slab allocator.
  96. auto AllocateUninitialized(std::size_t size)
  97. -> llvm::MutableArrayRef<ElementType> {
  98. // We're not going to run a destructor, so ensure that's OK.
  99. static_assert(std::is_trivially_destructible_v<ElementType>);
  100. auto storage = static_cast<ElementType*>(
  101. allocator_->Allocate(size * sizeof(ElementType), alignof(ElementType)));
  102. return llvm::MutableArrayRef<ElementType>(storage, size);
  103. }
  104. // Allocates a copy of the given data using our slab allocator.
  105. auto AllocateCopy(llvm::ArrayRef<ElementType> data)
  106. -> llvm::MutableArrayRef<ElementType> {
  107. auto result = AllocateUninitialized(data.size());
  108. std::uninitialized_copy(data.begin(), data.end(), result.begin());
  109. return result;
  110. }
  111. llvm::BumpPtrAllocator* allocator_;
  112. ValueStore<IdT> values_;
  113. Set<IdT, /*SmallSize=*/0, KeyContext> canonical_blocks_;
  114. };
  115. template <typename IdT>
  116. class BlockValueStore<IdT>::KeyContext
  117. : public TranslatingKeyContext<KeyContext> {
  118. public:
  119. explicit KeyContext(const BlockValueStore* store) : store_(store) {}
  120. auto TranslateKey(IdT id) const -> llvm::ArrayRef<ElementType> {
  121. return store_->Get(id);
  122. }
  123. private:
  124. const BlockValueStore* store_;
  125. };
  126. } // namespace Carbon::SemIR
  127. #endif // CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_