[ORC] Further improvements to SymbolStringPtr & NonOwningSymbolStringPtr.
authorLang Hames <lhames@gmail.com>
Tue, 31 Jan 2023 22:43:11 +0000 (14:43 -0800)
committerLang Hames <lhames@gmail.com>
Wed, 1 Feb 2023 00:41:26 +0000 (16:41 -0800)
A follow-up to https://reviews.llvm.org/D142314:

* Make SymbolStringPtrs constructible from NonOwningSymbolStringPtrs.

* Move and rename getRefCount and isValid (now poolEntryIsAlive) to improve
readability. Also updates these routines to make them safe for use with
sentinel values (null, empty, tombstone).

* Move ref-counting operations into their own incRef and decRef methods.

llvm/include/llvm/ExecutionEngine/Orc/SymbolStringPool.h
llvm/unittests/ExecutionEngine/Orc/SymbolStringPoolTest.cpp

index 9372a3d..d488848 100644 (file)
@@ -26,6 +26,7 @@ namespace orc {
 
 class SymbolStringPtrBase;
 class SymbolStringPtr;
+class NonOwningSymbolStringPtr;
 
 /// String pool for symbol names used by the JIT.
 class SymbolStringPool {
@@ -48,12 +49,6 @@ public:
   /// Returns true if the pool is empty.
   bool empty() const;
 
-#ifndef NDEBUG
-  // Useful for debugging and testing: This method can be used to identify
-  // non-owning pointers pointing to unowned pool entries.
-  bool isValid(const SymbolStringPtrBase &S) const { return getRefCount(S); }
-#endif
-
 private:
   size_t getRefCount(const SymbolStringPtrBase &S) const;
 
@@ -94,6 +89,16 @@ public:
     return LHS.S < RHS.S;
   }
 
+#ifndef NDEBUG
+  // Returns true if the pool entry's ref count is above zero (or if the entry
+  // is an empty or tombstone value). Useful for debugging and testing -- this
+  // method can be used to identify SymbolStringPtrs and
+  // NonOwningSymbolStringPtrs that are pointing to abandoned pool entries.
+  bool poolEntryIsAlive() const {
+    return isRealPoolEntry(S) ? S->getValue() != 0 : true;
+  }
+#endif
+
 protected:
   using PoolEntry = SymbolStringPool::PoolMapEntry;
   using PoolEntryPtr = PoolEntry *;
@@ -112,6 +117,16 @@ protected:
       (std::numeric_limits<uintptr_t>::max() - 3)
       << PointerLikeTypeTraits<PoolEntryPtr>::NumLowBitsAvailable;
 
+  // Returns false for null, empty, and tombstone values, true otherwise.
+  static bool isRealPoolEntry(PoolEntryPtr P) {
+    return ((reinterpret_cast<uintptr_t>(P) - 1) & InvalidPtrMask) !=
+           InvalidPtrMask;
+  }
+
+  size_t getRefCount() const {
+    return isRealPoolEntry(S) ? size_t(S->getValue()) : size_t(0);
+  }
+
   PoolEntryPtr S = nullptr;
 };
 
@@ -125,50 +140,42 @@ public:
   SymbolStringPtr() = default;
   SymbolStringPtr(std::nullptr_t) {}
   SymbolStringPtr(const SymbolStringPtr &Other) : SymbolStringPtrBase(Other.S) {
-    if (isRealPoolEntry(S))
-      ++S->getValue();
+    incRef();
   }
 
+  explicit SymbolStringPtr(NonOwningSymbolStringPtr Other);
+
   SymbolStringPtr& operator=(const SymbolStringPtr &Other) {
-    if (isRealPoolEntry(S)) {
-      assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
-      --S->getValue();
-    }
+    decRef();
     S = Other.S;
-    if (isRealPoolEntry(S))
-      ++S->getValue();
+    incRef();
     return *this;
   }
 
   SymbolStringPtr(SymbolStringPtr &&Other) { std::swap(S, Other.S); }
 
   SymbolStringPtr& operator=(SymbolStringPtr &&Other) {
-    if (isRealPoolEntry(S)) {
-      assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
-      --S->getValue();
-    }
+    decRef();
     S = nullptr;
     std::swap(S, Other.S);
     return *this;
   }
 
-  ~SymbolStringPtr() {
-    if (isRealPoolEntry(S)) {
-      assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
-      --S->getValue();
-    }
-  }
+  ~SymbolStringPtr() { decRef(); }
 
 private:
-  SymbolStringPtr(PoolEntryPtr S) : SymbolStringPtrBase(S) {
+  SymbolStringPtr(PoolEntryPtr S) : SymbolStringPtrBase(S) { incRef(); }
+
+  void incRef() {
     if (isRealPoolEntry(S))
       ++S->getValue();
   }
 
-  // Returns false for null, empty, and tombstone values, true otherwise.
-  bool isRealPoolEntry(PoolEntryPtr P) {
-    return ((reinterpret_cast<uintptr_t>(P) - 1) & InvalidPtrMask) !=
-           InvalidPtrMask;
+  void decRef() {
+    if (isRealPoolEntry(S)) {
+      assert(S->getValue() && "Releasing SymbolStringPtr with zero ref count");
+      --S->getValue();
+    }
   }
 
   static SymbolStringPtr getEmptyVal() {
@@ -216,6 +223,15 @@ private:
   }
 };
 
+inline SymbolStringPtr::SymbolStringPtr(NonOwningSymbolStringPtr Other)
+    : SymbolStringPtrBase(Other) {
+  assert(poolEntryIsAlive() &&
+         "SymbolStringPtr constructed from invalid non-owning pointer.");
+
+  if (isRealPoolEntry(S))
+    ++S->getValue();
+}
+
 inline SymbolStringPool::~SymbolStringPool() {
 #ifndef NDEBUG
   clearDeadEntries();
@@ -247,7 +263,7 @@ inline bool SymbolStringPool::empty() const {
 
 inline size_t
 SymbolStringPool::getRefCount(const SymbolStringPtrBase &S) const {
-  return S.S->second;
+  return S.getRefCount();
 }
 
 } // end namespace orc
index cd11341..4aea0f4 100644 (file)
@@ -90,6 +90,9 @@ TEST_F(SymbolStringPoolTest, NonOwningPointerBasics) {
   // Assignment.
   ANP2 = ANP1;
   ANP2 = A;
+
+  SymbolStringPtr S(ANP1); // Construct SymbolStringPtr from non-owning.
+  EXPECT_EQ(S, A);
 }
 
 TEST_F(SymbolStringPoolTest, NonOwningPointerRefCounts) {