[clang] [clang-repl] Load the HIP runtime from the detected ROCm installation (PR #246462)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 23:11:33 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aditya Sinha (AdityaSinha149)

<details>
<summary>Changes</summary>

This PR fixes #<!-- -->231783. It changes clang-repl to load the HIP runtime(`libamdhip64.so`) from the ROCm installation that clang detects. If no HIP installation is found, it loads the library by name. 

The logic for loading CUDA runtime remains unchanged.

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


2 Files Affected:

- (modified) clang/tools/clang-repl/CMakeLists.txt (+1) 
- (modified) clang/tools/clang-repl/ClangRepl.cpp (+33-6) 


``````````diff
diff --git a/clang/tools/clang-repl/CMakeLists.txt b/clang/tools/clang-repl/CMakeLists.txt
index f83a7e607899ba..831a0354afc9e5 100644
--- a/clang/tools/clang-repl/CMakeLists.txt
+++ b/clang/tools/clang-repl/CMakeLists.txt
@@ -71,6 +71,7 @@ endif(MSVC)
 clang_target_link_libraries(clang-repl PRIVATE
   clangAST
   clangBasic
+  clangDriver
   clangFrontend
   clangInterpreter
   )
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index df0786796cb8ca..436c52a8822c33 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -14,6 +14,8 @@
 #include "clang/Basic/DiagnosticFrontend.h"
 #include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Driver/RocmInstallationDetector.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/CodeCompletion.h"
 #include "clang/Interpreter/Interpreter.h"
@@ -23,6 +25,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/LineEditor/LineEditor.h"
+#include "llvm/Option/ArgList.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/ManagedStatic.h" // llvm_shutdown
@@ -165,6 +168,28 @@ static llvm::Expected<unsigned> getSlabAllocSize(llvm::StringRef SizeString) {
   return SlabSize * Units;
 }
 
+static std::string getHIPRuntimePath(llvm::ArrayRef<const char *> UserArgs) {
+  const char *RuntimeLib = "libamdhip64.so";
+
+  clang::DiagnosticOptions DiagOpts;
+  clang::DiagnosticsEngine Diags(clang::DiagnosticIDs::create(), DiagOpts,
+                                 new clang::IgnoringDiagConsumer());
+  std::string TT = llvm::sys::getProcessTriple();
+  std::string Exe = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
+  clang::driver::Driver D(Exe, TT, Diags);
+  bool ContainsError = false;
+  llvm::opt::InputArgList Args =
+      D.ParseArgStrings(UserArgs, /*UseDriverMode=*/true, ContainsError);
+
+  clang::driver::RocmInstallationDetector Rocm(D, llvm::Triple(TT), Args);
+  if (!Rocm.hasHIPRuntime())
+    return RuntimeLib;
+
+  llvm::SmallString<256> Path(Rocm.getLibPath());
+  llvm::sys::path::append(Path, RuntimeLib);
+  return Path.str().str();
+}
+
 static void LLVMErrorHandler(void *UserData, const char *Message,
                              bool GenCrashDiag) {
   auto &Diags = *static_cast<clang::DiagnosticsEngine *>(UserData);
@@ -352,12 +377,14 @@ int main(int argc, const char **argv) {
         std::move(CI), std::move(DeviceCI), OffloadKind));
 
     const char *RuntimeLib = UseHip ? "libamdhip64.so" : "libcudart.so";
-    if (DevicePath.empty()) {
-      ExitOnErr(Interp->LoadDynamicLibrary(RuntimeLib));
-    } else {
-      std::string RuntimeLibPath = DevicePath + "/lib/" + RuntimeLib;
-      ExitOnErr(Interp->LoadDynamicLibrary(RuntimeLibPath.c_str()));
-    }
+    std::string RuntimeLibPath;
+    if (!DevicePath.empty())
+      RuntimeLibPath = DevicePath + "/lib/" + RuntimeLib;
+    else if (UseHip)
+      RuntimeLibPath = getHIPRuntimePath(ClangArgv);
+    else
+      RuntimeLibPath = RuntimeLib;
+    ExitOnErr(Interp->LoadDynamicLibrary(RuntimeLibPath.c_str()));
   } else {
     Interp =
         ExitOnErr(clang::Interpreter::create(std::move(CI), std::move(IEB)));

``````````

</details>


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


More information about the cfe-commits mailing list