Skip to content

Revert "[NFC][lldb] Speed up lookup of shared modules (#152054)" #152582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025

Conversation

augusto2112
Copy link
Contributor

This reverts commit 229d860.

@llvmbot llvmbot added the lldb label Aug 7, 2025
@augusto2112 augusto2112 merged commit 75cc77e into llvm:main Aug 7, 2025
8 of 11 checks passed
@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2025

@llvm/pr-subscribers-lldb

Author: Augusto Noronha (augusto2112)

Changes

This reverts commit 229d860.


Full diff: https://github.com/llvm/llvm-project/pull/152582.diff

1 Files Affected:

  • (modified) lldb/source/Core/ModuleList.cpp (+7-235)
diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp
index 34caa474c1e32..d5ddc2b249e56 100644
--- a/lldb/source/Core/ModuleList.cpp
+++ b/lldb/source/Core/ModuleList.cpp
@@ -755,240 +755,11 @@ size_t ModuleList::GetIndexForModule(const Module *module) const {
 }
 
 namespace {
-/// A wrapper around ModuleList for shared modules. Provides fast lookups for
-/// file-based ModuleSpec queries.
-class SharedModuleList {
-public:
-  /// Finds all the modules matching the module_spec, and adds them to \p
-  /// matching_module_list.
-  void FindModules(const ModuleSpec &module_spec,
-                   ModuleList &matching_module_list) const {
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    // Try map first for performance - if found, skip expensive full list
-    // search
-    if (FindModulesInMap(module_spec, matching_module_list))
-      return;
-    m_list.FindModules(module_spec, matching_module_list);
-    // Assert that modules were found in the list but not the map, it's
-    // because the module_spec has no filename or the found module has a
-    // different filename. For example, when searching by UUID and finding a
-    // module with an alias.
-    assert((matching_module_list.IsEmpty() ||
-            module_spec.GetFileSpec().GetFilename().IsEmpty() ||
-            module_spec.GetFileSpec().GetFilename() !=
-                matching_module_list.GetModuleAtIndex(0)
-                    ->GetFileSpec()
-                    .GetFilename()) &&
-           "Search by name not found in SharedModuleList's map");
-  }
-
-  ModuleSP FindModule(const Module *module_ptr) {
-    if (!module_ptr)
-      return ModuleSP();
-
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    if (ModuleSP result = FindModuleInMap(module_ptr))
-      return result;
-    return m_list.FindModule(module_ptr);
-  }
-
-  // UUID searches bypass map since UUIDs aren't indexed by filename.
-  ModuleSP FindModule(const UUID &uuid) const {
-    return m_list.FindModule(uuid);
-  }
-
-  void Append(const ModuleSP &module_sp, bool use_notifier) {
-    if (!module_sp)
-      return;
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    m_list.Append(module_sp, use_notifier);
-    AddToMap(module_sp);
-  }
-
-  size_t RemoveOrphans(bool mandatory) {
-    std::unique_lock<std::recursive_mutex> lock(GetMutex(), std::defer_lock);
-    if (mandatory) {
-      lock.lock();
-    } else {
-      if (!lock.try_lock())
-        return 0;
-    }
-    size_t total_count = 0;
-    size_t run_count;
-    do {
-      // Remove indexed orphans first, then remove non-indexed orphans. This
-      // order is important because the shared count will be different if a
-      // module is indexed or not.
-      run_count = RemoveOrphansFromMapAndList();
-      run_count += m_list.RemoveOrphans(mandatory);
-      total_count += run_count;
-      // Because removing orphans might make new orphans, remove from both
-      // containers until a fixed-point is reached.
-    } while (run_count != 0);
-
-    return total_count;
-  }
-
-  bool Remove(const ModuleSP &module_sp, bool use_notifier = true) {
-    if (!module_sp)
-      return false;
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    RemoveFromMap(module_sp.get());
-    return m_list.Remove(module_sp, use_notifier);
-  }
-
-  void ReplaceEquivalent(const ModuleSP &module_sp,
-                         llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules) {
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    m_list.ReplaceEquivalent(module_sp, old_modules);
-    ReplaceEquivalentInMap(module_sp);
-  }
-
-  bool RemoveIfOrphaned(const Module *module_ptr) {
-    std::lock_guard<std::recursive_mutex> guard(GetMutex());
-    RemoveFromMap(module_ptr, /*if_orphaned =*/true);
-    return m_list.RemoveIfOrphaned(module_ptr);
-  }
-
-  std::recursive_mutex &GetMutex() const { return m_list.GetMutex(); }
-
-private:
-  ModuleSP FindModuleInMap(const Module *module_ptr) {
-    if (!module_ptr->GetFileSpec().GetFilename())
-      return ModuleSP();
-    ConstString name = module_ptr->GetFileSpec().GetFilename();
-    auto it = m_name_to_modules.find(name);
-    if (it == m_name_to_modules.end())
-      return ModuleSP();
-    const llvm::SmallVectorImpl<ModuleSP> &vector = it->second;
-    for (auto &module_sp : vector) {
-      if (module_sp.get() == module_ptr)
-        return module_sp;
-    }
-    return ModuleSP();
-  }
-
-  bool FindModulesInMap(const ModuleSpec &module_spec,
-                        ModuleList &matching_module_list) const {
-    auto it = m_name_to_modules.find(module_spec.GetFileSpec().GetFilename());
-    if (it == m_name_to_modules.end())
-      return false;
-    const llvm::SmallVectorImpl<ModuleSP> &vector = it->second;
-    bool found = false;
-    for (auto &module_sp : vector) {
-      if (module_sp->MatchesModuleSpec(module_spec)) {
-        matching_module_list.Append(module_sp);
-        found = true;
-      }
-    }
-    return found;
-  }
-
-  void AddToMap(const ModuleSP &module_sp) {
-    ConstString name = module_sp->GetFileSpec().GetFilename();
-    if (name.IsEmpty())
-      return;
-    llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
-    vec.push_back(module_sp);
-  }
-
-  void RemoveFromMap(const Module *module_ptr, bool if_orphaned = false) {
-    ConstString name = module_ptr->GetFileSpec().GetFilename();
-    if (m_name_to_modules.contains(name))
-      return;
-    llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
-    for (auto *it = vec.begin(); it != vec.end(); ++it) {
-      if (it->get() == module_ptr) {
-        // use_count == 2 means only held by map and list (orphaned)
-        if (!if_orphaned || it->use_count() == 2)
-          vec.erase(it);
-        break;
-      }
-    }
-  }
-
-  void ReplaceEquivalentInMap(const ModuleSP &module_sp) {
-    RemoveEquivalentModulesFromMap(module_sp);
-    AddToMap(module_sp);
-  }
-
-  void RemoveEquivalentModulesFromMap(const ModuleSP &module_sp) {
-    ConstString name = module_sp->GetFileSpec().GetFilename();
-    if (name.IsEmpty())
-      return;
-
-    auto it = m_name_to_modules.find(name);
-    if (it == m_name_to_modules.end())
-      return;
-
-    // First remove any equivalent modules. Equivalent modules are modules
-    // whose path, platform path and architecture match.
-    ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
-                                      module_sp->GetArchitecture());
-    equivalent_module_spec.GetPlatformFileSpec() =
-        module_sp->GetPlatformFileSpec();
-
-    llvm::SmallVectorImpl<ModuleSP> &vec = it->second;
-    llvm::erase_if(vec, [&equivalent_module_spec](ModuleSP &element) {
-      return element->MatchesModuleSpec(equivalent_module_spec);
-    });
-  }
-
-  /// Remove orphans from the vector.
-  ///
-  /// Returns the removed orphans.
-  ModuleList RemoveOrphansFromVector(llvm::SmallVectorImpl<ModuleSP> &vec) {
-    ModuleList to_remove;
-    for (int i = vec.size() - 1; i >= 0; --i) {
-      ModuleSP module = vec[i];
-      long kUseCountOrphaned = 2;
-      long kUseCountLocalVariable = 1;
-      // use_count == 3: map + list + local variable = orphaned.
-      if (module.use_count() == kUseCountOrphaned + kUseCountLocalVariable) {
-        to_remove.Append(module);
-        vec.erase(vec.begin() + i);
-      }
-    }
-    return to_remove;
-  }
-
-  /// Remove orphans that exist in both the map and list. This does not remove
-  /// any orphans that exist exclusively on the list.
-  ///
-  /// This assumes that the mutex is locked.
-  int RemoveOrphansFromMapAndList() {
-    // Modules might hold shared pointers to other modules, so removing one
-    // module might orphan other modules. Keep removing modules until
-    // there are no further modules that can be removed.
-    bool made_progress = true;
-    int remove_count = 0;
-    while (made_progress) {
-      made_progress = false;
-      for (auto &[name, vec] : m_name_to_modules) {
-        if (vec.empty())
-          continue;
-        ModuleList to_remove = RemoveOrphansFromVector(vec);
-        remove_count += to_remove.GetSize();
-        made_progress = !to_remove.IsEmpty();
-        m_list.Remove(to_remove);
-      }
-    }
-    return remove_count;
-  }
-
-  ModuleList m_list;
-
-  /// A hash map from a module's filename to all the modules that share that
-  /// filename, for fast module lookups by name.
-  llvm::DenseMap<ConstString, llvm::SmallVector<ModuleSP, 1>> m_name_to_modules;
-};
-
 struct SharedModuleListInfo {
-  SharedModuleList module_list;
+  ModuleList module_list;
   ModuleListProperties module_list_properties;
 };
-} // namespace
-
+}
 static SharedModuleListInfo &GetSharedModuleListInfo()
 {
   static SharedModuleListInfo *g_shared_module_list_info = nullptr;
@@ -1003,7 +774,7 @@ static SharedModuleListInfo &GetSharedModuleListInfo()
   return *g_shared_module_list_info;
 }
 
-static SharedModuleList &GetSharedModuleList() {
+static ModuleList &GetSharedModuleList() {
   return GetSharedModuleListInfo().module_list;
 }
 
@@ -1013,7 +784,7 @@ ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
 
 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
   if (module_ptr) {
-    SharedModuleList &shared_module_list = GetSharedModuleList();
+    ModuleList &shared_module_list = GetSharedModuleList();
     return shared_module_list.FindModule(module_ptr).get() != nullptr;
   }
   return false;
@@ -1037,8 +808,9 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
                             const FileSpecList *module_search_paths_ptr,
                             llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
                             bool *did_create_ptr, bool always_create) {
-  SharedModuleList &shared_module_list = GetSharedModuleList();
-  std::lock_guard<std::recursive_mutex> guard(shared_module_list.GetMutex());
+  ModuleList &shared_module_list = GetSharedModuleList();
+  std::lock_guard<std::recursive_mutex> guard(
+      shared_module_list.m_modules_mutex);
   char path[PATH_MAX];
 
   Status error;

Copy link

github-actions bot commented Aug 7, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- lldb/source/Core/ModuleList.cpp
View the diff from clang-format here.
diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp
index d5ddc2b24..abfc54c39 100644
--- a/lldb/source/Core/ModuleList.cpp
+++ b/lldb/source/Core/ModuleList.cpp
@@ -759,7 +759,7 @@ struct SharedModuleListInfo {
   ModuleList module_list;
   ModuleListProperties module_list_properties;
 };
-}
+} // namespace
 static SharedModuleListInfo &GetSharedModuleListInfo()
 {
   static SharedModuleListInfo *g_shared_module_list_info = nullptr;

@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 7, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building lldb at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/22275

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: commands/log/invalid-args/TestInvalidArgsLog.py (189 of 2304)
PASS: lldb-api :: commands/platform/basic/TestPlatformCommand.py (190 of 2304)
PASS: lldb-api :: commands/memory/write/TestMemoryWrite.py (191 of 2304)
PASS: lldb-api :: commands/platform/basic/TestPlatformPython.py (192 of 2304)
PASS: lldb-api :: commands/platform/file/close/TestPlatformFileClose.py (193 of 2304)
PASS: lldb-api :: commands/platform/file/read/TestPlatformFileRead.py (194 of 2304)
PASS: lldb-api :: commands/memory/read/TestMemoryRead.py (195 of 2304)
UNSUPPORTED: lldb-api :: commands/platform/sdk/TestPlatformSDK.py (196 of 2304)
PASS: lldb-api :: commands/platform/connect/TestPlatformConnect.py (197 of 2304)
UNRESOLVED: lldb-api :: commands/gui/spawn-threads/TestGuiSpawnThreads.py (198 of 2304)
******************** TEST 'lldb-api :: commands/gui/spawn-threads/TestGuiSpawnThreads.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --cmake-build-type Release /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/commands/gui/spawn-threads -p TestGuiSpawnThreads.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 22.0.0git (https://github.com/llvm/llvm-project.git revision 75cc77e55e12d39aed94702b0b1365e39713081e)
  clang revision 75cc77e55e12d39aed94702b0b1365e39713081e
  llvm revision 75cc77e55e12d39aed94702b0b1365e39713081e
Skipping the following test categories: ['libc++', 'msvcstl', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
FAIL: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_gui (TestGuiSpawnThreads.TestGuiSpawnThreadsTest)
======================================================================
ERROR: test_gui (TestGuiSpawnThreads.TestGuiSpawnThreadsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/packages/Python/lldbsuite/test/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/commands/gui/spawn-threads/TestGuiSpawnThreads.py", line 44, in test_gui
    self.child.expect_exact(f"thread #{i + 2}: tid =")
  File "/usr/local/lib/python3.10/dist-packages/pexpect/spawnbase.py", line 432, in expect_exact
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 179, in expect_loop
    return self.eof(e)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 122, in eof
    raise exc
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
<pexpect.pty_spawn.spawn object at 0xf58acbd16050>
command: /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/lldb
args: ['/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/lldb', '--no-lldbinit', '--no-use-colors', '-O', 'settings clear --all', '-O', 'settings set symbols.enable-external-lookup false', '-O', 'settings set target.inherit-tcc true', '-O', 'settings set target.disable-aslr false', '-O', 'settings set target.detach-on-error false', '-O', 'settings set target.auto-apply-fixits false', '-O', 'settings set plugin.process.gdb-remote.packet-timeout 60', '-O', 'settings set symbols.clang-modules-cache-path "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api"', '-O', 'settings set use-color false', '-O', 'settings set show-statusline false', '--file', '/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/commands/gui/spawn-threads/TestGuiSpawnThreads.test_gui/a.out']
buffer (last 100 chars): b''
before (last 100 chars): b'thread_create.c:442:8\n#20 0x0000fcbbe3c25edc ./misc/../sysdeps/unix/sysv/linux/aarch64/clone.S:82:0\n'
after: <class 'pexpect.exceptions.EOF'>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants