[llvm] Reduce llvm-gsymutil memory usage (PR #91023)

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 13:01:40 PDT 2024


================
@@ -495,21 +495,78 @@ void DWARFUnit::extractDIEsIfNeeded(bool CUDieOnly) {
     Context.getRecoverableErrorHandler()(std::move(e));
 }
 
-Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) {
-  if ((CUDieOnly && !DieArray.empty()) ||
-      DieArray.size() > 1)
-    return Error::success(); // Already parsed.
+static bool DoubleCheckedRWLocker(llvm::sys::RWMutex &Mutex,
+                                  const std::function<bool()> &reader,
+                                  const std::function<void()> &writer) {
+  {
+    llvm::sys::ScopedReader Lock(Mutex);
+    if (reader())
+      return true;
+  }
+  llvm::sys::ScopedWriter Lock(Mutex);
+  if (reader())
+    return true;
+  // If we get here, then the reader function returned false. This means that
+  // no one else is currently writing to this data structure and it's safe for
+  // us to write to it now. The scoped writer lock guarantees there are no
+  // other readers or writers at this point.
+  writer();
+  return false;
+}
 
-  bool HasCUDie = !DieArray.empty();
-  extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
+// Helper to safely check if the Compile-Unit DIE has been extracted already.
+// If not, then extract it, and return false, indicating that it was *not*
+// already extracted.
+bool DWARFUnit::extractCUDieIfNeeded(bool CUDieOnly, bool &HasCUDie) {
----------------
dwblaikie wrote:

Is this function misnamed? It looks like it extracts more than only the CUDie (if CUDieOnly is false, it extracts all the DIEs, right?) So maybe this should be called "extractDiesIfNeeded"?

https://github.com/llvm/llvm-project/pull/91023


More information about the llvm-commits mailing list