[clang] [clang-repl] Emit an AMDGPU object for the HIP device module (PR #234498)

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 21 23:05:45 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aditya Sinha (AdityaSinha149)

<details>
<summary>Changes</summary>

This PR adds code generation to the `IncrementalHIPDeviceParser`. It takes the last PTU's device module and converts it into an AMDGPU device object in the memory. `GenerateOffloadBinary()` is updated to call it and after that it returns the same error as before. 

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


2 Files Affected:

- (modified) clang/lib/Interpreter/DeviceOffload.cpp (+30-2) 
- (modified) clang/lib/Interpreter/DeviceOffload.h (+7) 


``````````diff
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp
index 31fa20fff450d..94fec23b1c0b2 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -14,6 +14,7 @@
 #include "DeviceOffload.h"
 
 #include "clang/Basic/TargetOptions.h"
+#include "clang/CodeGen/BackendUtil.h"
 #include "clang/CodeGen/ModuleBuilder.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/PartialTranslationUnit.h"
@@ -43,7 +44,8 @@ IncrementalHIPDeviceParser::IncrementalHIPDeviceParser(
     llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
     llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs)
     : IncrementalDeviceParser(DeviceInstance, HostInstance, DeviceAct, FS, Err,
-                              PTUs) {
+                              PTUs),
+      DeviceCI(DeviceInstance) {
   if (Err)
     return;
   if (!llvm::Triple(TargetOpts.Triple).isAMDGCN()) {
@@ -56,8 +58,34 @@ IncrementalHIPDeviceParser::IncrementalHIPDeviceParser(
   }
 }
 
+llvm::Expected<llvm::StringRef>
+IncrementalHIPDeviceParser::GenerateDeviceObject() {
+  auto &PTU = PTUs.back();
+
+  CodeGenOptions CodeGenOptsForObj = DeviceCI.getCodeGenOpts();
+  CodeGenOptsForObj.DisableLLVMPasses = true;
+
+  ObjectContent.clear();
+  auto ObjOS = std::make_unique<llvm::raw_svector_ostream>(ObjectContent);
+  emitBackendOutput(DeviceCI, CodeGenOptsForObj, PTU.TheModule.get(),
+                    Backend_EmitObj, DeviceCI.getVirtualFileSystemPtr(),
+                    std::move(ObjOS));
+
+  if (DeviceCI.getDiagnostics().hasErrorOccurred())
+    return llvm::make_error<llvm::StringError>(
+        "Backend code generation failed for HIP device code.",
+        llvm::inconvertibleErrorCode());
+
+  return llvm::StringRef(ObjectContent.data(), ObjectContent.size());
+}
+
 llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() {
-  // FIXME: Generate the HSACO and bundle it for the host compilation.
+  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.
   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 0275c0e10cda8..1b0f3e3d4bd59 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -56,6 +56,13 @@ class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
   llvm::Error GenerateOffloadBinary() override;
 
   ~IncrementalHIPDeviceParser() override;
+
+private:
+  // Generate the AMDGPU object file for the last PTU.
+  llvm::Expected<llvm::StringRef> GenerateDeviceObject();
+
+  CompilerInstance &DeviceCI;
+  llvm::SmallVector<char, 0> ObjectContent;
 };
 
 class IncrementalCUDADeviceParser : public IncrementalDeviceParser {

``````````

</details>


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


More information about the cfe-commits mailing list