CostAllocator.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CostAllocator.h - PBQP Cost Allocator --------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Defines classes conforming to the PBQP cost value manager concept.
  15. //
  16. // Cost value managers are memory managers for PBQP cost values (vectors and
  17. // matrices). Since PBQP graphs can grow very large (E.g. hundreds of thousands
  18. // of edges on the largest function in SPEC2006).
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
  22. #define LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
  23. #include "llvm/ADT/DenseSet.h"
  24. #include <algorithm>
  25. #include <cstdint>
  26. #include <memory>
  27. namespace llvm {
  28. namespace PBQP {
  29. template <typename ValueT> class ValuePool {
  30. public:
  31. using PoolRef = std::shared_ptr<const ValueT>;
  32. private:
  33. class PoolEntry : public std::enable_shared_from_this<PoolEntry> {
  34. public:
  35. template <typename ValueKeyT>
  36. PoolEntry(ValuePool &Pool, ValueKeyT Value)
  37. : Pool(Pool), Value(std::move(Value)) {}
  38. ~PoolEntry() { Pool.removeEntry(this); }
  39. const ValueT &getValue() const { return Value; }
  40. private:
  41. ValuePool &Pool;
  42. ValueT Value;
  43. };
  44. class PoolEntryDSInfo {
  45. public:
  46. static inline PoolEntry *getEmptyKey() { return nullptr; }
  47. static inline PoolEntry *getTombstoneKey() {
  48. return reinterpret_cast<PoolEntry *>(static_cast<uintptr_t>(1));
  49. }
  50. template <typename ValueKeyT>
  51. static unsigned getHashValue(const ValueKeyT &C) {
  52. return hash_value(C);
  53. }
  54. static unsigned getHashValue(PoolEntry *P) {
  55. return getHashValue(P->getValue());
  56. }
  57. static unsigned getHashValue(const PoolEntry *P) {
  58. return getHashValue(P->getValue());
  59. }
  60. template <typename ValueKeyT1, typename ValueKeyT2>
  61. static bool isEqual(const ValueKeyT1 &C1, const ValueKeyT2 &C2) {
  62. return C1 == C2;
  63. }
  64. template <typename ValueKeyT>
  65. static bool isEqual(const ValueKeyT &C, PoolEntry *P) {
  66. if (P == getEmptyKey() || P == getTombstoneKey())
  67. return false;
  68. return isEqual(C, P->getValue());
  69. }
  70. static bool isEqual(PoolEntry *P1, PoolEntry *P2) {
  71. if (P1 == getEmptyKey() || P1 == getTombstoneKey())
  72. return P1 == P2;
  73. return isEqual(P1->getValue(), P2);
  74. }
  75. };
  76. using EntrySetT = DenseSet<PoolEntry *, PoolEntryDSInfo>;
  77. EntrySetT EntrySet;
  78. void removeEntry(PoolEntry *P) { EntrySet.erase(P); }
  79. public:
  80. template <typename ValueKeyT> PoolRef getValue(ValueKeyT ValueKey) {
  81. typename EntrySetT::iterator I = EntrySet.find_as(ValueKey);
  82. if (I != EntrySet.end())
  83. return PoolRef((*I)->shared_from_this(), &(*I)->getValue());
  84. auto P = std::make_shared<PoolEntry>(*this, std::move(ValueKey));
  85. EntrySet.insert(P.get());
  86. return PoolRef(std::move(P), &P->getValue());
  87. }
  88. };
  89. template <typename VectorT, typename MatrixT> class PoolCostAllocator {
  90. private:
  91. using VectorCostPool = ValuePool<VectorT>;
  92. using MatrixCostPool = ValuePool<MatrixT>;
  93. public:
  94. using Vector = VectorT;
  95. using Matrix = MatrixT;
  96. using VectorPtr = typename VectorCostPool::PoolRef;
  97. using MatrixPtr = typename MatrixCostPool::PoolRef;
  98. template <typename VectorKeyT> VectorPtr getVector(VectorKeyT v) {
  99. return VectorPool.getValue(std::move(v));
  100. }
  101. template <typename MatrixKeyT> MatrixPtr getMatrix(MatrixKeyT m) {
  102. return MatrixPool.getValue(std::move(m));
  103. }
  104. private:
  105. VectorCostPool VectorPool;
  106. MatrixCostPool MatrixPool;
  107. };
  108. } // end namespace PBQP
  109. } // end namespace llvm
  110. #endif // LLVM_CODEGEN_PBQP_COSTALLOCATOR_H
  111. #ifdef __GNUC__
  112. #pragma GCC diagnostic pop
  113. #endif