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

Aditya Sinha via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 03:55:56 PST 2026


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

>From bdb0b8748654156447605d1c4c496c35fe759744 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Thu, 26 Nov 2026 23:04:21 +0530
Subject: [PATCH] [clang-repl] Load the HIP runtime from the detected ROCm
 installation

---
 clang/include/clang/Interpreter/Interpreter.h |  5 +++++
 clang/lib/Interpreter/Interpreter.cpp         | 12 ++++++++++++
 clang/tools/clang-repl/ClangRepl.cpp          | 12 ++++++------
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 64dee1cf80db8d..582c4bfda7bbfe 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -82,6 +82,10 @@ class IncrementalCompilerBuilder {
   llvm::Expected<std::unique_ptr<CompilerInstance>>
   CreateDevice(OffloadType Type);
 
+  /// The HIP runtime the driver detected for the HIP compilation, or empty
+  /// if no HIP installation was found.
+  llvm::StringRef getHIPRuntimePath() const { return HIPRuntimePath; }
+
 private:
   llvm::Expected<std::unique_ptr<CompilerInstance>>
   create(std::string TT, std::vector<const char *> &ClangArgv);
@@ -96,6 +100,7 @@ class IncrementalCompilerBuilder {
   llvm::StringRef DeviceSDKPath;
 
   std::string OffloadCUID;
+  std::string HIPRuntimePath;
 
   std::optional<std::function<DriverCompilationFn>> CompilationCB;
 };
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index a195835f4fe8e2..08d565f898f6d4 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -28,6 +28,7 @@
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
 #include "clang/Driver/Job.h"
+#include "clang/Driver/RocmInstallationDetector.h"
 #include "clang/Driver/Tool.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
@@ -287,6 +288,17 @@ IncrementalCompilerBuilder::create(std::string TT,
   llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv);
   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
 
+  if (Compilation->hasOffloadToolChain<driver::Action::OFK_HIP>()) {
+    driver::RocmInstallationDetector Rocm(Driver, llvm::Triple(TT),
+                                          Compilation->getArgs());
+    if (Rocm.hasHIPRuntime()) {
+      llvm::SmallString<256> Path(Rocm.getLibPath());
+      llvm::sys::path::append(Path, "libamdhip64.so");
+      if (Driver.getVFS().exists(Path))
+        HIPRuntimePath = std::string(Path);
+    }
+  }
+
   if (CompilationCB)
     if (auto Err = (*CompilationCB)(*Compilation.get()))
       return std::move(Err);
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index df0786796cb8ca..1010fff48527c0 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -352,12 +352,12 @@ 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 = RuntimeLib;
+    if (UseHip && !CB.getHIPRuntimePath().empty())
+      RuntimeLibPath = CB.getHIPRuntimePath().str();
+    else if (!DevicePath.empty())
+      RuntimeLibPath = DevicePath + "/lib/" + RuntimeLib;
+    ExitOnErr(Interp->LoadDynamicLibrary(RuntimeLibPath.c_str()));
   } else {
     Interp =
         ExitOnErr(clang::Interpreter::create(std::move(CI), std::move(IEB)));



More information about the cfe-commits mailing list