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

Kevin Frei via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 13:47:26 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) {
----------------
kevinfrei wrote:

Yup, you're correct: This is allocating (and extracting) all the DIE's. What's protected by the second lock appears to be extracting string (offset...), range, and location tables for the DIEs that were just extracted. Would renaming things to `extractDIEsIfNeeded`/`extractTablesForDIEsIfNeeded` seem reasonable?

(I'm also going to see if I can free those tables in a later diff, as I'd love to drive memory usage lower than it currently is even with this change)

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


More information about the llvm-commits mailing list