[llvm] Reduce llvm-gsymutil memory usage (PR #91023)
Kevin Frei via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 6 15:14:28 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 showed the code to a coworker with lots more recent experience in LLVM, and he suggested inlining the lambdas (which was nearly unreadable originally), but breaking the code into pieces so you can see the locking at each point (which makes the code more legible). I'm gonna get that change up hopefully by end of day, then you can tell me which you prefer.
https://github.com/llvm/llvm-project/pull/91023
More information about the llvm-commits
mailing list