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

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 6 12:02:13 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.
----------------
dwblaikie wrote:

oh, and usually for a lambda that's only used within its scope, I wouldn't bother with explicit capture lists, and jus tuse `[&]` by default - but perhaps due to these things happening under locks, etc, it might be worth the extra clarity - I'm not 100% sure, though.

Again, how's this code look if it's all in the one DoubleCheckedRWLocker call, with default capture?

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


More information about the llvm-commits mailing list