[clang] [clang-repl] Embed the HIP fat binary into the host compilation (PR #242209)

Aditya Sinha via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 00:20:11 PST 2026


https://github.com/AdityaSinha149 created https://github.com/llvm/llvm-project/pull/242209

This PR embeds the HIP fat binary into the host's compilation, so the HIP runtime can load it and kernels typed into `clang-repl` can run on an AMD GPU. The unit test now checks the embedded fat binary.

Assisted by Claude Opus 5.5

>From 5fd02a61297602f561c9fd9cc699228b82388782 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Wed, 4 Nov 2026 11:24:43 +0530
Subject: [PATCH 1/4] [clang][Driver] NFC: Share -B and --ld-path lookup
 helpers

---
 clang/include/clang/Driver/CommonArgs.h    | 14 ++++++
 clang/lib/Driver/Driver.cpp                | 39 ++-------------
 clang/lib/Driver/ToolChain.cpp             | 16 +++----
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 56 ++++++++++++++++++++++
 4 files changed, 82 insertions(+), 43 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h b/clang/include/clang/Driver/CommonArgs.h
index 138e18f8641b9..70fff756f801a 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -15,10 +15,12 @@
 #include "clang/Driver/Multilib.h"
 #include "clang/Driver/Tool.h"
 #include "clang/Driver/ToolChain.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Support/CodeGen.h"
+#include <optional>
 
 namespace clang {
 namespace driver {
@@ -37,6 +39,18 @@ struct OffloadJobsOpt {
 
 OffloadJobsOpt parseOffloadJobs(const llvm::opt::ArgList &Args);
 
+bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name);
+
+void addPrefixDirs(const llvm::opt::ArgList &Args,
+                   SmallVectorImpl<std::string> &PrefixDirs);
+
+std::optional<std::string>
+findProgramInPrefixDirs(StringRef Name, ArrayRef<std::string> PrefixDirs);
+
+std::optional<std::string>
+resolveLdPath(StringRef LdPath,
+              llvm::function_ref<std::string(StringRef)> GetProgramPath);
+
 void addPathIfExists(const Driver &D, const Twine &Path,
                      ToolChain::path_list &Paths);
 
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 37fb69baf6a3e..486b9841e0e84 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1697,20 +1697,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     TargetTriple = A->getValue();
   if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir))
     Dir = A->getValue();
-  for (const Arg *A : Args.filtered(options::OPT_B)) {
-    A->claim();
-    PrefixDirs.push_back(A->getValue(0));
-  }
-  if (std::optional<std::string> CompilerPathValue =
-          llvm::sys::Process::GetEnv("COMPILER_PATH")) {
-    StringRef CompilerPath = *CompilerPathValue;
-    while (!CompilerPath.empty()) {
-      std::pair<StringRef, StringRef> Split =
-          CompilerPath.split(llvm::sys::EnvPathSeparator);
-      PrefixDirs.push_back(std::string(Split.first));
-      CompilerPath = Split.second;
-    }
-  }
+  tools::addPrefixDirs(Args, PrefixDirs);
   if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ))
     SysRoot = A->getValue();
   if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ))
@@ -5971,31 +5958,15 @@ void Driver::generatePrefixedToolNames(
   Names.emplace_back(Tool);
 }
 
-static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
-  llvm::sys::path::append(Dir, Name);
-  if (llvm::sys::fs::can_execute(Twine(Dir)))
-    return true;
-  llvm::sys::path::remove_filename(Dir);
-  return false;
-}
-
 std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
   SmallVector<std::string, 2> TargetSpecificExecutables;
   generatePrefixedToolNames(Name, TC, TargetSpecificExecutables);
 
   // Respect a limited subset of the '-Bprefix' functionality in GCC by
   // attempting to use this prefix when looking for program paths.
-  for (const auto &PrefixDir : PrefixDirs) {
-    if (llvm::sys::fs::is_directory(PrefixDir)) {
-      SmallString<128> P(PrefixDir);
-      if (ScanDirForExecutable(P, Name))
-        return std::string(P);
-    } else {
-      SmallString<128> P((PrefixDir + Name).str());
-      if (llvm::sys::fs::can_execute(Twine(P)))
-        return std::string(P);
-    }
-  }
+  if (std::optional<std::string> P =
+          tools::findProgramInPrefixDirs(Name, PrefixDirs))
+    return *P;
 
   const ToolChain::path_list &List = TC.getProgramPaths();
   for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) {
@@ -6008,7 +5979,7 @@ std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const {
     // of gcc in the program path
     for (const auto &Path : List) {
       SmallString<128> P(Path);
-      if (ScanDirForExecutable(P, TargetSpecificExecutable))
+      if (tools::ScanDirForExecutable(P, TargetSpecificExecutable))
         return std::string(P);
     }
 
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index b3e7043a64fe5..6c1e5a98cf6ea 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -1321,15 +1321,13 @@ std::string ToolChain::GetLinkerPath(bool *LinkerIsLLD) const {
   // -fuse-ld=lld can be used with --ld-path= to inform clang that the binary
   // that --ld-path= points to is lld.
   if (const Arg *A = Args.getLastArg(options::OPT_ld_path_EQ)) {
-    std::string Path(A->getValue());
-    if (!Path.empty()) {
-      if (llvm::sys::path::parent_path(Path).empty())
-        Path = GetProgramPath(A->getValue());
-      if (llvm::sys::fs::can_execute(Path)) {
-        if (LinkerIsLLD)
-          *LinkerIsLLD = UseLinker == "lld";
-        return std::string(Path);
-      }
+    if (std::optional<std::string> Path =
+            tools::resolveLdPath(A->getValue(), [&](StringRef Name) {
+              return GetProgramPath(Name.str().c_str());
+            })) {
+      if (LinkerIsLLD)
+        *LinkerIsLLD = UseLinker == "lld";
+      return *Path;
     }
     getDriver().Diag(diag::err_drv_invalid_linker_name) << A->getAsString(Args);
     return GetProgramPath(getDefaultLinker());
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 0590c297edf39..67f13f3fdb147 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -82,6 +82,62 @@ OffloadJobsOpt tools::parseOffloadJobs(const ArgList &Args) {
   return {OffloadJobsOpt::Kind::Fixed, A, Val, unsigned(NumThreads)};
 }
 
+bool tools::ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) {
+  llvm::sys::path::append(Dir, Name);
+  if (llvm::sys::fs::can_execute(Twine(Dir)))
+    return true;
+  llvm::sys::path::remove_filename(Dir);
+  return false;
+}
+
+void tools::addPrefixDirs(const ArgList &Args,
+                          SmallVectorImpl<std::string> &PrefixDirs) {
+  for (const Arg *A : Args.filtered(options::OPT_B)) {
+    A->claim();
+    PrefixDirs.push_back(A->getValue(0));
+  }
+  if (std::optional<std::string> CompilerPathValue =
+          llvm::sys::Process::GetEnv("COMPILER_PATH")) {
+    StringRef CompilerPath = *CompilerPathValue;
+    while (!CompilerPath.empty()) {
+      std::pair<StringRef, StringRef> Split =
+          CompilerPath.split(llvm::sys::EnvPathSeparator);
+      PrefixDirs.push_back(std::string(Split.first));
+      CompilerPath = Split.second;
+    }
+  }
+}
+
+std::optional<std::string>
+tools::findProgramInPrefixDirs(StringRef Name,
+                               ArrayRef<std::string> PrefixDirs) {
+  for (const auto &PrefixDir : PrefixDirs) {
+    if (llvm::sys::fs::is_directory(PrefixDir)) {
+      SmallString<128> P(PrefixDir);
+      if (ScanDirForExecutable(P, Name))
+        return std::string(P);
+    } else {
+      SmallString<128> P((PrefixDir + Name).str());
+      if (llvm::sys::fs::can_execute(Twine(P)))
+        return std::string(P);
+    }
+  }
+  return std::nullopt;
+}
+
+std::optional<std::string> tools::resolveLdPath(
+    StringRef LdPath,
+    llvm::function_ref<std::string(StringRef)> GetProgramPath) {
+  std::string Path(LdPath);
+  if (Path.empty())
+    return std::nullopt;
+  if (llvm::sys::path::parent_path(Path).empty())
+    Path = GetProgramPath(Path);
+  if (!llvm::sys::fs::can_execute(Path))
+    return std::nullopt;
+  return Path;
+}
+
 static bool useFramePointerForTargetByDefault(const llvm::opt::ArgList &Args,
                                               const llvm::Triple &Triple) {
   if (Args.hasArg(options::OPT_pg) && !Args.hasArg(options::OPT_mfentry))

>From 23db71281a9f897e08b29b0a82eb50cbd1bbd273 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Wed, 4 Nov 2026 11:53:25 +0530
Subject: [PATCH 2/4] [clang-repl] Link HIP device object into an HSACO

---
 clang/include/clang/Interpreter/Interpreter.h |  3 +-
 clang/lib/Interpreter/DeviceOffload.cpp       | 95 ++++++++++++++++++-
 clang/lib/Interpreter/DeviceOffload.h         | 10 +-
 clang/lib/Interpreter/Interpreter.cpp         |  9 +-
 clang/tools/clang-repl/ClangRepl.cpp          |  2 +-
 .../Interpreter/DeviceOffloadTest.cpp         | 70 +++++++++++++-
 6 files changed, 176 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
index 5228b7fe80bd3..644f6f6247b79 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -158,7 +158,8 @@ class Interpreter {
   static llvm::Expected<std::unique_ptr<Interpreter>>
   createWithDevice(std::unique_ptr<CompilerInstance> CI,
                    std::unique_ptr<CompilerInstance> DCI,
-                   OffloadType Type = OffloadType::Detect);
+                   OffloadType Type = OffloadType::Detect,
+                   llvm::ArrayRef<const char *> DriverArgs = {});
 
   const ASTContext &getASTContext() const;
   ASTContext &getASTContext();
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index d7ae386466f40..0bba4fd1f1eb3 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -16,13 +16,22 @@
 #include "clang/Basic/TargetOptions.h"
 #include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ModuleBuilder.h"
+#include "clang/Driver/CommonArgs.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/PartialTranslationUnit.h"
+#include "clang/Options/Options.h"
 
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/MC/TargetRegistry.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/TargetParser/Triple.h"
 
@@ -43,7 +52,8 @@ IncrementalHIPDeviceParser::IncrementalHIPDeviceParser(
     CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
     IncrementalAction *DeviceAct,
     llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
-    llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs)
+    llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs,
+    llvm::ArrayRef<const char *> DriverArgs)
     : IncrementalDeviceParser(DeviceInstance, HostInstance, DeviceAct, FS, Err,
                               PTUs),
       DeviceCI(DeviceInstance) {
@@ -57,6 +67,27 @@ IncrementalHIPDeviceParser::IncrementalHIPDeviceParser(
             llvm::inconvertibleErrorCode()));
     return;
   }
+
+  unsigned MissingArgIndex, MissingArgCount;
+  llvm::opt::InputArgList Args = getDriverOptTable().ParseArgs(
+      DriverArgs, MissingArgIndex, MissingArgCount,
+      llvm::opt::Visibility(options::ClangOption));
+  driver::tools::addPrefixDirs(Args, PrefixDirs);
+  LdPath = Args.getLastArgValue(options::OPT_ld_path_EQ).str();
+}
+
+static std::string getProgramPath(llvm::StringRef Name,
+                                  llvm::ArrayRef<std::string> PrefixDirs) {
+  if (std::optional<std::string> P =
+          driver::tools::findProgramInPrefixDirs(Name, PrefixDirs))
+    return *P;
+  std::string Exe = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
+  if (llvm::ErrorOr<std::string> P = llvm::sys::findProgramByName(
+          Name, {llvm::sys::path::parent_path(Exe)}))
+    return *P;
+  if (llvm::ErrorOr<std::string> P = llvm::sys::findProgramByName(Name))
+    return *P;
+  return Name.str();
 }
 
 llvm::Expected<llvm::StringRef>
@@ -80,13 +111,71 @@ IncrementalHIPDeviceParser::GenerateDeviceObject() {
   return llvm::StringRef(ObjectContent.data(), ObjectContent.size());
 }
 
+llvm::Expected<llvm::StringRef>
+IncrementalHIPDeviceParser::GenerateHSACO(llvm::StringRef Object) {
+  // Without --ld-path, look for a program named "lld".
+  llvm::StringRef LinkerName = LdPath;
+  if (LinkerName.empty())
+    LinkerName = "lld";
+  std::optional<std::string> Linker =
+      driver::tools::resolveLdPath(LinkerName, [&](llvm::StringRef Name) {
+        return getProgramPath(Name, PrefixDirs);
+      });
+  if (!Linker)
+    return llvm::make_error<llvm::StringError>(
+        LdPath.empty()
+            ? "Could not find lld."
+            : "Invalid linker name in argument '--ld-path=" + LdPath + "'.",
+        llvm::inconvertibleErrorCode());
+
+  int ObjFD = -1;
+  llvm::SmallString<128> ObjFile;
+  if (llvm::sys::fs::createTemporaryFile("kernel", "o", ObjFD, ObjFile))
+    return llvm::make_error<llvm::StringError>(
+        "Failed to create a temporary object file.",
+        llvm::inconvertibleErrorCode());
+  llvm::FileRemover ObjRemover(ObjFile);
+  {
+    llvm::raw_fd_ostream OS(ObjFD, /*shouldClose=*/true);
+    OS << Object;
+  }
+
+  llvm::SmallString<128> HSACOFile;
+  if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco", HSACOFile))
+    return llvm::make_error<llvm::StringError>(
+        "Failed to create a temporary code object file.",
+        llvm::inconvertibleErrorCode());
+  llvm::FileRemover HSACORemover(HSACOFile);
+
+  // These match the driver's HIP device link (AMDGCN::Linker) for an object.
+  llvm::StringRef Args[] = {*Linker,   "-flavor",         "gnu",
+                            "-m",      "elf64_amdgpu",    "--no-undefined",
+                            "-shared", "--whole-archive", "-o",
+                            HSACOFile, ObjFile,           "--no-whole-archive"};
+  if (llvm::sys::ExecuteAndWait(*Linker, Args) != 0)
+    return llvm::make_error<llvm::StringError>("The linker invocation failed.",
+                                               llvm::inconvertibleErrorCode());
+
+  auto HSACOBuf = llvm::MemoryBuffer::getFile(HSACOFile, /*IsText=*/false);
+  if (!HSACOBuf)
+    return llvm::make_error<llvm::StringError>(
+        "Failed to read the code object.", llvm::inconvertibleErrorCode());
+
+  llvm::StringRef Buffer = (*HSACOBuf)->getBuffer();
+  HSACOContent.assign(Buffer.begin(), Buffer.end());
+  return llvm::StringRef(HSACOContent.data(), HSACOContent.size());
+}
+
 llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
   llvm::Expected<llvm::StringRef> Object = GenerateDeviceObject();
   if (!Object)
     return Object.takeError();
 
-  // FIXME: Link the object into an HSACO and bundle it for the host
-  // compilation.
+  llvm::Expected<llvm::StringRef> HSACO = GenerateHSACO(*Object);
+  if (!HSACO)
+    return HSACO.takeError();
+
+  // FIXME: Bundle the HSACO and embed it into the host compilation.
   return llvm::make_error<llvm::StringError>(
       "HIP offload binary generation is not supported yet.",
       llvm::inconvertibleErrorCode());
diff --git a/clang/lib/Interpreter/DeviceOffload.h b/clang/lib/Interpreter/DeviceOffload.h
index c01e373d5a2f9..0daec6c5f5750 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -47,16 +47,24 @@ class IncrementalDeviceParser : public IncrementalParser {
 class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
   CompilerInstance &DeviceCI;
   llvm::SmallVector<char, 0> ObjectContent;
+  llvm::SmallVector<char, 0> HSACOContent;
+  // The -B prefixes and --ld-path value from the driver arguments.
+  llvm::SmallVector<std::string, 4> PrefixDirs;
+  std::string LdPath;
 
   // Generate the AMDGPU object file for the last PTU.
   llvm::Expected<llvm::StringRef> GenerateDeviceObject();
 
+  // Link the object file into a loadable AMDGPU code object (HSACO).
+  llvm::Expected<llvm::StringRef> GenerateHSACO(llvm::StringRef Object);
+
 public:
   IncrementalHIPDeviceParser(
       CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
       IncrementalAction *DeviceAct,
       llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
-      llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
+      llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs,
+      llvm::ArrayRef<const char *> DriverArgs);
 
   llvm::Error GenerateOffloadBinary() override;
 
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index d6cce74b7275f..b0fc3e143c421 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -569,10 +569,9 @@ llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::create(
   return std::move(Interp);
 }
 
-llvm::Expected<std::unique_ptr<Interpreter>>
-Interpreter::createWithDevice(std::unique_ptr<CompilerInstance> CI,
-                              std::unique_ptr<CompilerInstance> DCI,
-                              OffloadType Type) {
+llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::createWithDevice(
+    std::unique_ptr<CompilerInstance> CI, std::unique_ptr<CompilerInstance> DCI,
+    OffloadType Type, llvm::ArrayRef<const char *> DriverArgs) {
   if (Type == OffloadType::Detect)
     return llvm::make_error<llvm::StringError>(
         "Detecting the device vendor is not supported yet.",
@@ -618,7 +617,7 @@ Interpreter::createWithDevice(std::unique_ptr<CompilerInstance> CI,
   if (Type == OffloadType::HIP)
     DeviceParser = std::make_unique<IncrementalHIPDeviceParser>(
         *Interp->DeviceCI, *Interp->getCompilerInstance(),
-        Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs);
+        Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs, DriverArgs);
   else
     DeviceParser = std::make_unique<IncrementalCUDADeviceParser>(
         *Interp->DeviceCI, *Interp->getCompilerInstance(),
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index df0786796cb8c..8fc30f874444e 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -349,7 +349,7 @@ int main(int argc, const char **argv) {
 
   if (DeviceEnabled) {
     Interp = ExitOnErr(clang::Interpreter::createWithDevice(
-        std::move(CI), std::move(DeviceCI), OffloadKind));
+        std::move(CI), std::move(DeviceCI), OffloadKind, ClangArgv));
 
     const char *RuntimeLib = UseHip ? "libamdhip64.so" : "libcudart.so";
     if (DevicePath.empty()) {
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index fa624f23f435b..88157ed9e8057 100644
--- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp
+++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
@@ -18,6 +18,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/Interpreter.h"
 
+#include "llvm/ADT/SmallString.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/LLVMContext.h"
@@ -27,6 +28,8 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/VirtualFileSystem.h"
 #include "llvm/Support/raw_ostream.h"
@@ -156,10 +159,73 @@ TEST_F(DeviceOffloadHIPTest, DeviceObjectIsGenerated) {
 
   // FIXME: Replace this with a check of the embedded bundle once the offload
   // binary is generated.
+  std::string Exe = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
+  bool HasLLD = llvm::sys::findProgramByName(
+                    "lld", {llvm::sys::path::parent_path(Exe)}) ||
+                llvm::sys::findProgramByName("lld");
   auto PTU = (*Interp)->Parse("__attribute__((device)) void f() {}");
   EXPECT_THAT_EXPECTED(
-      PTU, llvm::FailedWithMessage("HIP offload binary generation is not "
-                                   "supported yet."));
+      PTU, llvm::FailedWithMessage(
+               HasLLD ? "HIP offload binary generation is not supported yet."
+                      : "Could not find lld."));
+}
+
+TEST_F(DeviceOffloadHIPTest, LdPathSelectsTheLinker) {
+  std::vector<const char *> Args = {"--offload-arch=gfx1100", "-nogpulib",
+                                    "-nogpuinc", "--ld-path=/nonexistent/lld"};
+  IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(Args);
+  auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
+  ASSERT_THAT_EXPECTED(DeviceCI, llvm::Succeeded());
+  auto HostCI = CB.CreateHost(OffloadType::HIP);
+  ASSERT_THAT_EXPECTED(HostCI, llvm::Succeeded());
+  auto Interp = Interpreter::createWithDevice(
+      std::move(*HostCI), std::move(*DeviceCI), OffloadType::HIP, Args);
+  ASSERT_THAT_EXPECTED(Interp, llvm::Succeeded());
+
+  auto PTU = (*Interp)->Parse("__attribute__((device)) void f() {}");
+  EXPECT_THAT_EXPECTED(
+      PTU, llvm::FailedWithMessage("Invalid linker name in argument "
+                                   "'--ld-path=/nonexistent/lld'."));
+}
+
+TEST_F(DeviceOffloadHIPTest, PrefixDirSelectsTheLinker) {
+#ifdef _WIN32
+  GTEST_SKIP() << "Needs an executable shell script.";
+#else
+  // A fake lld in a -B directory must be found and run before any other lld.
+  llvm::SmallString<128> Dir;
+  ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory("fake-lld", Dir));
+  llvm::FileRemover DirRemover(Dir);
+  llvm::SmallString<128> Lld(Dir);
+  llvm::sys::path::append(Lld, "lld");
+  llvm::FileRemover LldRemover(Lld);
+  {
+    std::error_code EC;
+    llvm::raw_fd_ostream OS(Lld, EC);
+    ASSERT_FALSE(EC);
+    OS << "#!/bin/sh\nexit 1\n";
+  }
+  ASSERT_FALSE(llvm::sys::fs::setPermissions(Lld, llvm::sys::fs::all_all));
+
+  std::string Prefix = "-B" + Dir.str().str();
+  std::vector<const char *> Args = {"--offload-arch=gfx1100", "-nogpulib",
+                                    "-nogpuinc", Prefix.c_str()};
+  IncrementalCompilerBuilder CB;
+  CB.SetCompilerArgs(Args);
+  auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
+  ASSERT_THAT_EXPECTED(DeviceCI, llvm::Succeeded());
+  auto HostCI = CB.CreateHost(OffloadType::HIP);
+  ASSERT_THAT_EXPECTED(HostCI, llvm::Succeeded());
+  auto Interp = Interpreter::createWithDevice(
+      std::move(*HostCI), std::move(*DeviceCI), OffloadType::HIP, Args);
+  ASSERT_THAT_EXPECTED(Interp, llvm::Succeeded());
+
+  // The fake lld fails, which can only be reported if -B located and ran it.
+  auto PTU = (*Interp)->Parse("__attribute__((device)) void f() {}");
+  EXPECT_THAT_EXPECTED(
+      PTU, llvm::FailedWithMessage("The linker invocation failed."));
+#endif
 }
 
 // A bitcode library with one float(float) function that doubles its argument,

>From 8fbbdfe98ee4f10837de6cb535a97a20f93ed6a9 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Wed, 11 Nov 2026 16:07:58 +0530
Subject: [PATCH 3/4] [clang-repl] Bundle the HIP HSACO into an offload bundle

---
 clang/lib/Interpreter/DeviceOffload.cpp       | 74 ++++++++++++++++++-
 clang/lib/Interpreter/DeviceOffload.h         |  4 +
 .../Interpreter/DeviceOffloadTest.cpp         |  7 +-
 3 files changed, 81 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index 0bba4fd1f1eb3..c543885245b25 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -13,10 +13,12 @@
 
 #include "DeviceOffload.h"
 
+#include "clang/Basic/TargetID.h"
 #include "clang/Basic/TargetOptions.h"
 #include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ModuleBuilder.h"
 #include "clang/Driver/CommonArgs.h"
+#include "clang/Driver/OffloadBundler.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/PartialTranslationUnit.h"
 #include "clang/Options/Options.h"
@@ -33,6 +35,8 @@
 #include "llvm/Support/Program.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/TargetParser/AMDGPUTargetParser.h"
+#include "llvm/TargetParser/Host.h"
 #include "llvm/TargetParser/Triple.h"
 
 namespace clang {
@@ -166,6 +170,70 @@ IncrementalHIPDeviceParser::GenerateHSACO(llvm::StringRef Object) {
   return llvm::StringRef(HSACOContent.data(), HSACOContent.size());
 }
 
+llvm::Expected<llvm::StringRef>
+IncrementalHIPDeviceParser::GenerateFatbinary(llvm::StringRef HSACO) {
+  int HSACOFD = -1;
+  llvm::SmallString<128> HSACOFile;
+  if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco", HSACOFD, HSACOFile))
+    return llvm::make_error<llvm::StringError>(
+        "Failed to create a temporary code object file.",
+        llvm::inconvertibleErrorCode());
+  llvm::FileRemover HSACORemover(HSACOFile);
+  {
+    llvm::raw_fd_ostream OS(HSACOFD, /*shouldClose=*/true);
+    OS << HSACO;
+  }
+
+  llvm::StringRef OffloadKind =
+      (TargetOpts.CodeObjectVersion == llvm::CodeObjectVersionKind::COV_2 ||
+       TargetOpts.CodeObjectVersion == llvm::CodeObjectVersionKind::COV_3)
+          ? "hip"
+          : "hipv4";
+  llvm::SmallString<128> FatbinFile;
+  if (llvm::sys::fs::createTemporaryFile("kernel", "hipfb", FatbinFile))
+    return llvm::make_error<llvm::StringError>(
+        "Failed to create a temporary fat binary file.",
+        llvm::inconvertibleErrorCode());
+  llvm::FileRemover FatbinRemover(FatbinFile);
+
+  llvm::SmallString<128> HostFile;
+  if (llvm::sys::fs::createTemporaryFile("host", "o", HostFile))
+    return llvm::make_error<llvm::StringError>(
+        "Failed to create a temporary host file.",
+        llvm::inconvertibleErrorCode());
+  llvm::FileRemover HostRemover(HostFile);
+
+  llvm::Triple DeviceTriple(TargetOpts.Triple);
+  std::string TargetID =
+      llvm::AMDGPU::TargetID::createFromSubtargetFeatures(
+          DeviceTriple, TargetOpts.CPU, llvm::join(TargetOpts.Features, ","))
+          .getCanonicalFeatureString();
+
+  OffloadBundlerConfig BundlerConfig;
+  BundlerConfig.BundleAlignment = 4096;
+  BundlerConfig.HostInputIndex = 0;
+  BundlerConfig.FilesType = "o";
+  BundlerConfig.TargetNames = {"host-" + llvm::sys::getProcessTriple(),
+                               OffloadKind.str() + "-" +
+                                   normalizeForBundler(DeviceTriple, TargetID) +
+                                   "-" + TargetID};
+  BundlerConfig.InputFileNames = {std::string(HostFile),
+                                  std::string(HSACOFile)};
+  BundlerConfig.OutputFileNames = {std::string(FatbinFile)};
+
+  if (llvm::Error Err = OffloadBundler(BundlerConfig).BundleFiles())
+    return std::move(Err);
+
+  auto FatbinBuf = llvm::MemoryBuffer::getFile(FatbinFile, /*IsText=*/false);
+  if (!FatbinBuf)
+    return llvm::make_error<llvm::StringError>("Failed to read the fat binary.",
+                                               llvm::inconvertibleErrorCode());
+
+  llvm::StringRef Buffer = (*FatbinBuf)->getBuffer();
+  FatbinContent.assign(Buffer.begin(), Buffer.end());
+  return llvm::StringRef(FatbinContent.data(), FatbinContent.size());
+}
+
 llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
   llvm::Expected<llvm::StringRef> Object = GenerateDeviceObject();
   if (!Object)
@@ -175,7 +243,11 @@ llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
   if (!HSACO)
     return HSACO.takeError();
 
-  // FIXME: Bundle the HSACO and embed it into the host compilation.
+  llvm::Expected<llvm::StringRef> Fatbin = GenerateFatbinary(*HSACO);
+  if (!Fatbin)
+    return Fatbin.takeError();
+
+  // FIXME: Embed the fat binary into the host compilation.
   return llvm::make_error<llvm::StringError>(
       "HIP offload binary generation is not supported yet.",
       llvm::inconvertibleErrorCode());
diff --git a/clang/lib/Interpreter/DeviceOffload.h b/clang/lib/Interpreter/DeviceOffload.h
index 0daec6c5f5750..4b188b54e04c3 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -48,6 +48,7 @@ class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
   CompilerInstance &DeviceCI;
   llvm::SmallVector<char, 0> ObjectContent;
   llvm::SmallVector<char, 0> HSACOContent;
+  llvm::SmallVector<char, 0> FatbinContent;
   // The -B prefixes and --ld-path value from the driver arguments.
   llvm::SmallVector<std::string, 4> PrefixDirs;
   std::string LdPath;
@@ -58,6 +59,9 @@ class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
   // Link the object file into a loadable AMDGPU code object (HSACO).
   llvm::Expected<llvm::StringRef> GenerateHSACO(llvm::StringRef Object);
 
+  // Bundle the code object into a HIP fat binary.
+  llvm::Expected<llvm::StringRef> GenerateFatbinary(llvm::StringRef HSACO);
+
 public:
   IncrementalHIPDeviceParser(
       CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index 88157ed9e8057..39966640fba49 100644
--- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp
+++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
@@ -146,7 +146,7 @@ TEST(DeviceOffloadBuilderTest, MultipleOffloadArchsAreRejected) {
                               "supported. Pass a single --offload-arch"));
 }
 
-TEST_F(DeviceOffloadHIPTest, DeviceObjectIsGenerated) {
+TEST_F(DeviceOffloadHIPTest, DeviceCodeIsBundled) {
   IncrementalCompilerBuilder CB;
   CB.SetCompilerArgs({"--offload-arch=gfx1100", "-nogpulib", "-nogpuinc"});
   auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
@@ -157,8 +157,9 @@ TEST_F(DeviceOffloadHIPTest, DeviceObjectIsGenerated) {
       std::move(*HostCI), std::move(*DeviceCI), OffloadType::HIP);
   ASSERT_THAT_EXPECTED(Interp, llvm::Succeeded());
 
-  // FIXME: Replace this with a check of the embedded bundle once the offload
-  // binary is generated.
+  // With lld, the unsupported embedding step is only reached after the device
+  // object was linked into an HSACO and bundled.
+  // FIXME: Check the fat binary once it is embedded into the host compilation.
   std::string Exe = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
   bool HasLLD = llvm::sys::findProgramByName(
                     "lld", {llvm::sys::path::parent_path(Exe)}) ||

>From 6299c7cf23f0a4d411c1840729614dd757c44e38 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Fri, 13 Nov 2026 13:06:52 +0530
Subject: [PATCH 4/4] [clang-repl] Embed the HIP fat binary into the host
 compilation

---
 clang/lib/Interpreter/DeviceOffload.cpp       | 17 +++++++++----
 clang/lib/Interpreter/DeviceOffload.h         |  3 +++
 .../Interpreter/DeviceOffloadTest.cpp         | 24 +++++++++++++------
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index c543885245b25..4c39ff9d2c7d4 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -24,6 +24,7 @@
 #include "clang/Options/Options.h"
 
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/MC/TargetRegistry.h"
@@ -234,6 +235,16 @@ IncrementalHIPDeviceParser::GenerateFatbinary(llvm::StringRef HSACO) {
   return llvm::StringRef(FatbinContent.data(), FatbinContent.size());
 }
 
+void IncrementalHIPDeviceParser::EmbedOffloadBundle() {
+  const PartialTranslationUnit &PTU = PTUs.back();
+  std::string BundleFileName = "/" + PTU.TheModule->getName().str() + ".hipfb";
+  VFS->addFile(BundleFileName, 0,
+    llvm::MemoryBuffer::getMemBufferCopy(
+        llvm::StringRef(FatbinContent.data(), FatbinContent.size())));
+
+  CodeGenOpts.OffloadBinaryToEmbedFile = std::move(BundleFileName);
+}
+
 llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
   llvm::Expected<llvm::StringRef> Object = GenerateDeviceObject();
   if (!Object)
@@ -247,10 +258,8 @@ llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
   if (!Fatbin)
     return Fatbin.takeError();
 
-  // FIXME: Embed the fat binary into the host compilation.
-  return llvm::make_error<llvm::StringError>(
-      "HIP offload binary generation is not supported yet.",
-      llvm::inconvertibleErrorCode());
+  EmbedOffloadBundle();
+  return llvm::Error::success();
 }
 
 IncrementalHIPDeviceParser::~IncrementalHIPDeviceParser() {}
diff --git a/clang/lib/Interpreter/DeviceOffload.h b/clang/lib/Interpreter/DeviceOffload.h
index 4b188b54e04c3..33aeee0d46fd7 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -62,6 +62,9 @@ class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
   // Bundle the code object into a HIP fat binary.
   llvm::Expected<llvm::StringRef> GenerateFatbinary(llvm::StringRef HSACO);
 
+  //Embed the bundle in host codegen.
+  void EmbedOffloadBundle();
+
 public:
   IncrementalHIPDeviceParser(
       CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index 39966640fba49..d27eef930a699 100644
--- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp
+++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
@@ -157,18 +157,28 @@ TEST_F(DeviceOffloadHIPTest, DeviceCodeIsBundled) {
       std::move(*HostCI), std::move(*DeviceCI), OffloadType::HIP);
   ASSERT_THAT_EXPECTED(Interp, llvm::Succeeded());
 
-  // With lld, the unsupported embedding step is only reached after the device
-  // object was linked into an HSACO and bundled.
-  // FIXME: Check the fat binary once it is embedded into the host compilation.
   std::string Exe = llvm::sys::fs::getMainExecutable(nullptr, nullptr);
   bool HasLLD = llvm::sys::findProgramByName(
                     "lld", {llvm::sys::path::parent_path(Exe)}) ||
                 llvm::sys::findProgramByName("lld");
   auto PTU = (*Interp)->Parse("__attribute__((device)) void f() {}");
-  EXPECT_THAT_EXPECTED(
-      PTU, llvm::FailedWithMessage(
-               HasLLD ? "HIP offload binary generation is not supported yet."
-                      : "Could not find lld."));
+  if (!HasLLD) {
+    EXPECT_THAT_EXPECTED(PTU, llvm::FailedWithMessage("Could not find lld."));
+    return;
+  }
+  ASSERT_THAT_EXPECTED(PTU, llvm::Succeeded());
+
+  // The device object was linked into an HSACO, bundled into a fat binary and
+  // handed to the host compilation.
+  const CompilerInstance *CI = (*Interp)->getCompilerInstance();
+  llvm::StringRef Fatbin = CI->getCodeGenOpts().OffloadBinaryToEmbedFile;
+  ASSERT_FALSE(Fatbin.empty());
+  auto Buf = CI->getVirtualFileSystem().getBufferForFile(
+      Fatbin, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
+  ASSERT_TRUE(static_cast<bool>(Buf)) << Buf.getError().message();
+  llvm::StringRef Bundle = (*Buf)->getBuffer();
+  EXPECT_TRUE(Bundle.starts_with("__CLANG_OFFLOAD_BUNDLE__"));
+  EXPECT_TRUE(Bundle.contains("hipv4-amdgcn-amd-amdhsa--gfx1100"));
 }
 
 TEST_F(DeviceOffloadHIPTest, LdPathSelectsTheLinker) {



More information about the cfe-commits mailing list