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

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 22:48:36 PST 2026


Author: Aditya Sinha
Date: 2026-12-05T08:48:29+02:00
New Revision: 7ff5c64452ca20b5855a63a82fa6e4f8e475c42c

URL: https://github.com/llvm/llvm-project/commit/7ff5c64452ca20b5855a63a82fa6e4f8e475c42c
DIFF: https://github.com/llvm/llvm-project/commit/7ff5c64452ca20b5855a63a82fa6e4f8e475c42c.diff

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

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.

Added: 
    clang/test/Interpreter/hip-runtime-path.cpp

Modified: 
    clang/include/clang/Interpreter/Interpreter.h
    clang/lib/Interpreter/Interpreter.cpp
    clang/tools/clang-repl/CMakeLists.txt
    clang/tools/clang-repl/ClangRepl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 5228b7fe80bd32..af5996e7709ed9 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -72,9 +72,10 @@ class IncrementalCompilerBuilder {
   // The SDK path (CUDA or ROCm) of the device language being offloaded to.
   void SetDeviceSDK(llvm::StringRef Path) { DeviceSDKPath = Path; }
 
-  // Hand over the compilation.
+  // Hand over the compilation. Callbacks are called in the order they were
+  // added.
   void SetDriverCompilationCallback(std::function<DriverCompilationFn> C) {
-    CompilationCB = C;
+    CompilationCBs.push_back(std::move(C));
   }
 
   llvm::Expected<std::unique_ptr<CompilerInstance>>
@@ -97,7 +98,7 @@ class IncrementalCompilerBuilder {
 
   std::string OffloadCUID;
 
-  std::optional<std::function<DriverCompilationFn>> CompilationCB;
+  std::vector<std::function<DriverCompilationFn>> CompilationCBs;
 };
 
 class IncrementalAction;

diff  --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index cc676396151471..11113df6e2308d 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -366,8 +366,8 @@ IncrementalCompilerBuilder::create(std::string TT,
   llvm::ArrayRef<const char *> RF = llvm::ArrayRef(ClangArgv);
   std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(RF));
 
-  if (CompilationCB)
-    if (auto Err = (*CompilationCB)(*Compilation.get()))
+  for (auto &CompilationCB : CompilationCBs)
+    if (auto Err = CompilationCB(*Compilation.get()))
       return std::move(Err);
 
   if (Compilation->getArgs().hasArg(options::OPT_v))

diff  --git a/clang/test/Interpreter/hip-runtime-path.cpp b/clang/test/Interpreter/hip-runtime-path.cpp
new file mode 100644
index 00000000000000..efb7a73933b748
--- /dev/null
+++ b/clang/test/Interpreter/hip-runtime-path.cpp
@@ -0,0 +1,41 @@
+// REQUIRES: amdgpu-registered-target, x86_64-linux
+//
+// Check how clang-repl picks the HIP runtime. Each stub libamdhip64.so prints
+// where it was loaded from when it is loaded.
+//
+// RUN: rm -rf %t && split-file %s %t
+// RUN: mkdir -p %t/sdk/lib %t/empty-sdk/lib %t/ldpath
+// RUN: %clang -fPIC -shared %t/stub.c -DLOCATION='"sdk"' \
+// RUN:     -o %t/sdk/lib/libamdhip64.so --target=x86_64-linux-gnu
+// RUN: %clang -fPIC -shared %t/stub.c -DLOCATION='"ldpath"' \
+// RUN:     -o %t/ldpath/libamdhip64.so --target=x86_64-linux-gnu
+//
+// Case 1: the ROCm installation selected by the config file has the runtime,
+// so it is loaded from there and LD_LIBRARY_PATH is not used.
+// RUN: env LD_LIBRARY_PATH=%t/ldpath:%ld_library_path \
+// RUN:     clang-repl -hip --offload-arch=gfx1100 -Xcc=--config=%t/sdk.cfg \
+// RUN:     < /dev/null 2>&1 | FileCheck %s --check-prefix=SDK
+// SDK: loaded from sdk
+//
+// Case 2: the selected installation does not have the runtime, so it is loaded
+// by name and found through LD_LIBRARY_PATH.
+// RUN: env LD_LIBRARY_PATH=%t/ldpath:%ld_library_path \
+// RUN:     clang-repl -hip --offload-arch=gfx1100 -Xcc=--config=%t/empty-sdk.cfg \
+// RUN:     < /dev/null 2>&1 | FileCheck %s --check-prefix=LDPATH
+// LDPATH: loaded from ldpath
+
+//--- sdk.cfg
+--rocm-path=<CFGDIR>/sdk
+-nogpulib
+-nogpuinc
+
+//--- empty-sdk.cfg
+--rocm-path=<CFGDIR>/empty-sdk
+-nogpulib
+-nogpuinc
+
+//--- stub.c
+#include <stdio.h>
+__attribute__((constructor)) static void report(void) {
+  fputs("loaded from " LOCATION "\n", stderr);
+}

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 99d408af4f5a8d..82a49799f448d2 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -14,6 +14,10 @@
 #include "clang/Basic/DiagnosticFrontend.h"
 #include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Driver/RocmInstallationDetector.h"
+#include "clang/Driver/ToolChain.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/CodeCompletion.h"
 #include "clang/Interpreter/Interpreter.h"
@@ -328,6 +332,25 @@ int main(int argc, const char **argv) {
 
   std::unique_ptr<clang::CompilerInstance> DeviceCI;
 
+  // Use the HIP runtime from the ROCm installation that the driver selected
+  // for the HIP compilation.
+  std::string HIPRuntimePath;
+  if (UseHip)
+    CB.SetDriverCompilationCallback(
+        [&HIPRuntimePath](const clang::driver::Compilation &C) -> llvm::Error {
+          if (!C.hasOffloadToolChain<clang::driver::Action::OFK_HIP>())
+            return llvm::Error::success();
+          clang::driver::RocmInstallationDetector Rocm(
+              C.getDriver(), C.getDefaultToolChain().getTriple(), C.getArgs());
+          if (!Rocm.hasHIPRuntime())
+            return llvm::Error::success();
+          llvm::SmallString<256> Path(Rocm.getLibPath());
+          llvm::sys::path::append(Path, "libamdhip64.so");
+          if (C.getDriver().getVFS().exists(Path))
+            HIPRuntimePath = std::string(Path);
+          return llvm::Error::success();
+        });
+
   if (DeviceEnabled) {
     CB.SetDeviceSDK(DevicePath);
     CB.SetOffloadArch(DeviceOffloadArch);
@@ -360,12 +383,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 && !HIPRuntimePath.empty())
+      RuntimeLibPath = HIPRuntimePath;
+    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