[clang] [offload-arch] Fix HIP DLL discovery and loading on Windows (PR #194063)

Matt Arsenault via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 27 07:55:37 PDT 2026


================
@@ -181,14 +209,39 @@ static std::pair<std::string, bool> findNewestHIPDLL() {
   if (DLLNames.empty())
     return {"amdhip64.dll", true};
 
-  llvm::sort(DLLNames, compareVersions);
+  // stable_sort preserves the insertion order from getSearchPaths() on
+  // version ties, so a colocated build DLL wins over a system copy.
+  llvm::stable_sort(DLLNames, compareVersions);
   return {DLLNames[0], false};
 #else
   // On Linux, fallback to default shared object
   return {"libamdhip64.so", true};
 #endif
 }
 
+#ifdef _WIN32
+// Pre-load DLL with LOAD_WITH_ALTERED_SEARCH_PATH so transitive deps
+// resolve from its directory. Pinned so getPermanentLibrary reuses it.
+static void primeLibraryLoad(StringRef Path) {
+  // One DLL primed per process; subsequent calls are no-ops.
+  // Not thread-safe, but offload-arch is single-threaded.
+  static HMODULE PinnedModule = nullptr;
+  if (PinnedModule)
+    return;
+  SmallVector<wchar_t, 256> WPath;
+  assert(sys::path::is_absolute(Path) && "priming requires absolute path");
+  if (!convertUTF8ToUTF16String(Path, WPath))
+    return;
+  WPath.push_back(L'\0'); // ensure null-termination for LoadLibraryExW
+  PinnedModule = LoadLibraryExW(WPath.data(), nullptr,
+                                LOAD_WITH_ALTERED_SEARCH_PATH);
+  DWORD Err = GetLastError();
+  if (!PinnedModule && Verbose)
+    errs() << "Note: priming LoadLibraryExW failed for " << Path
----------------
arsenm wrote:

Lowercase. Also WithColor? 

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


More information about the cfe-commits mailing list