block_value_store.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "llvm/ADT/DenseMap.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. auto OutputYaml() const -> Yaml::OutputMapping {
  41. return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  42. for (auto block_index : llvm::seq(values_.size())) {
  43. auto block_id = IdT(block_index);
  44. map.Add(PrintToString(block_id),
  45. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  46. auto block = Get(block_id);
  47. for (auto i : llvm::seq(block.size())) {
  48. map.Add(llvm::itostr(i), Yaml::OutputScalar(block[i]));
  49. }
  50. }));
  51. }
  52. });
  53. }
  54. auto size() const -> int { return values_.size(); }
  55. protected:
  56. // Reserves and returns a block ID. The contents of the block
  57. // should be specified by calling Set, or similar.
  58. auto AddDefaultValue() -> IdT { return values_.AddDefaultValue(); }
  59. // Adds an uninitialized block of the given size.
  60. auto AddUninitialized(size_t size) -> IdT {
  61. return values_.Add(AllocateUninitialized(size));
  62. }
  63. // Sets the contents of an empty block to the given content.
  64. auto Set(IdT block_id, llvm::ArrayRef<ElementType> content) -> void {
  65. CARBON_CHECK(Get(block_id).empty())
  66. << "inst block content set more than once";
  67. values_.Get(block_id) = AllocateCopy(content);
  68. }
  69. private:
  70. // Allocates an uninitialized array using our slab allocator.
  71. auto AllocateUninitialized(std::size_t size)
  72. -> llvm::MutableArrayRef<ElementType> {
  73. // We're not going to run a destructor, so ensure that's OK.
  74. static_assert(std::is_trivially_destructible_v<ElementType>);
  75. auto storage = static_cast<ElementType*>(
  76. allocator_->Allocate(size * sizeof(ElementType), alignof(ElementType)));
  77. return llvm::MutableArrayRef<ElementType>(storage, size);
  78. }
  79. // Allocates a copy of the given data using our slab allocator.
  80. auto AllocateCopy(llvm::ArrayRef<ElementType> data)
  81. -> llvm::MutableArrayRef<ElementType> {
  82. auto result = AllocateUninitialized(data.size());
  83. std::uninitialized_copy(data.begin(), data.end(), result.begin());
  84. return result;
  85. }
  86. llvm::BumpPtrAllocator* allocator_;
  87. ValueStore<IdT> values_;
  88. };
  89. } // namespace Carbon::SemIR
  90. #endif // CARBON_TOOLCHAIN_SEM_IR_BLOCK_VALUE_STORE_H_