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

Kevin Frei via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 6 17:23:20 PDT 2024


================
@@ -495,21 +495,73 @@ 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. We can use a scoped writer lock since there are no
+  // other readers or writers at this point.
+  writer();
+  return false;
+}
 
-  bool HasCUDie = !DieArray.empty();
-  extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
+Error DWARFUnit::tryExtractDIEsIfNeeded(bool CUDieOnly) {
+  llvm::sys::ScopedReader FreeLock(CUDieFreeMutex);
+  bool HasCUDie = false;
+  Error Result = Error::success();
+
+  // Lambda to check if the CU DIE has been extracted already.
+  auto CheckIfCUDieExtracted = [this, CUDieOnly]() {
+    // True means already parsed.
+    return ((CUDieOnly && !DieArray.empty()) || DieArray.size() > 1);
+  };
+
+  // Lambda to extract the CU DIE.
----------------
kevinfrei wrote:

> I vote for readable. :D
> 
> I think I remember your internal one, and this one is more readable. at least to me.

Yeah, this is better than the previous iteration, but broken up in even smaller pieces looks downright well thought out. Update forthcoming...

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


More information about the llvm-commits mailing list