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

>From 92d4c5b309a8693c728bd69b9a4657e09c39892e Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <[email protected]>
Date: Mon, 12 Oct 2026 16:47:39 +0530
Subject: [PATCH] [NFC][clang-repl] Add IncrementalDeviceParser abstract base
 for device parsers

---
 clang/include/clang/Interpreter/Interpreter.h |  4 +-
 clang/lib/Interpreter/DeviceOffload.cpp       | 25 ++++++++++--
 clang/lib/Interpreter/DeviceOffload.h         | 40 +++++++++++++++----
 clang/lib/Interpreter/Interpreter.cpp         |  6 +--
 4 files changed, 57 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Interpreter/Interpreter.h 
b/clang/include/clang/Interpreter/Interpreter.h
index c2622b23d5d9c..a598574b417c0 100644
--- a/clang/include/clang/Interpreter/Interpreter.h
+++ b/clang/include/clang/Interpreter/Interpreter.h
@@ -44,7 +44,7 @@ class CompilerInstance;
 class CXXRecordDecl;
 class Decl;
 class IncrementalParser;
-class IncrementalCUDADeviceParser;
+class IncrementalDeviceParser;
 
 /// Create a pre-configured \c CompilerInstance for incremental processing.
 class IncrementalCompilerBuilder {
@@ -106,7 +106,7 @@ class Interpreter {
   std::unique_ptr<IncrementalExecutor> IncrExecutor;
 
   // An optional parser for CUDA offloading
-  std::unique_ptr<IncrementalCUDADeviceParser> DeviceParser;
+  std::unique_ptr<IncrementalDeviceParser> DeviceParser;
 
   // An optional action for CUDA offloading
   std::unique_ptr<IncrementalAction> DeviceAct;
diff --git a/clang/lib/Interpreter/DeviceOffload.cpp 
b/clang/lib/Interpreter/DeviceOffload.cpp
index 38cecd142a8e6..f8f2843f7b830 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -6,7 +6,8 @@
 //
 
//===----------------------------------------------------------------------===//
 //
-// This file implements offloading to CUDA devices.
+
+// This file implements offloading to devices.
 //
 
//===----------------------------------------------------------------------===//
 
@@ -24,14 +25,24 @@
 
 namespace clang {
 
-IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
+IncrementalDeviceParser::IncrementalDeviceParser(
     CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
     IncrementalAction *DeviceAct,
     llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
     llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs)
     : IncrementalParser(DeviceInstance, DeviceAct, Err, PTUs), VFS(FS),
       CodeGenOpts(HostInstance.getCodeGenOpts()),
-      TargetOpts(DeviceInstance.getTargetOpts()) {
+      TargetOpts(DeviceInstance.getTargetOpts()) {}
+
+IncrementalDeviceParser::~IncrementalDeviceParser() {}
+
+IncrementalCUDADeviceParser::IncrementalCUDADeviceParser(
+    CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
+    IncrementalAction *DeviceAct,
+    llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS,
+    llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs)
+    : IncrementalDeviceParser(DeviceInstance, HostInstance, DeviceAct, FS, Err,
+                              PTUs) {
   if (Err)
     return;
   StringRef Arch = TargetOpts.CPU;
@@ -159,6 +170,14 @@ llvm::Error 
IncrementalCUDADeviceParser::GenerateFatbinary() {
   return llvm::Error::success();
 }
 
+llvm::Error IncrementalCUDADeviceParser::GenerateOffloadBinary() {
+  llvm::Expected<llvm::StringRef> PTX = GeneratePTX();
+  if (!PTX)
+    return PTX.takeError();
+
+  return GenerateFatbinary();
+}
+
 IncrementalCUDADeviceParser::~IncrementalCUDADeviceParser() {}
 
 } // namespace clang
diff --git a/clang/lib/Interpreter/DeviceOffload.h 
b/clang/lib/Interpreter/DeviceOffload.h
index a31bd5a0499b8..1bdfb49a4d521 100644
--- a/clang/lib/Interpreter/DeviceOffload.h
+++ b/clang/lib/Interpreter/DeviceOffload.h
@@ -6,7 +6,7 @@
 //
 
//===----------------------------------------------------------------------===//
 //
-// This file implements classes required for offloading to CUDA devices.
+// This file defines the classes required for offloading to devices.
 //
 
//===----------------------------------------------------------------------===//
 
@@ -14,6 +14,7 @@
 #define LLVM_CLANG_LIB_INTERPRETER_DEVICE_OFFLOAD_H
 
 #include "IncrementalParser.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/VirtualFileSystem.h"
 
@@ -24,7 +25,31 @@ class CodeGenOptions;
 class TargetOptions;
 class IncrementalAction;
 
-class IncrementalCUDADeviceParser : public IncrementalParser {
+/// Abstract base class for the parsers of device code. It holds what every
+/// device parser needs, and leaves producing the device binary to the derived
+/// classes.
+class IncrementalDeviceParser : public IncrementalParser {
+
+public:
+  IncrementalDeviceParser(
+      CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
+      IncrementalAction *DeviceAct,
+      llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
+      llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
+
+  /// Generate the device binary for the last PTU and make it available to the
+  /// host compilation.
+  virtual llvm::Error GenerateOffloadBinary() = 0;
+
+  ~IncrementalDeviceParser() override;
+
+protected:
+  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
+  CodeGenOptions &CodeGenOpts; // Intentionally a reference.
+  const TargetOptions &TargetOpts;
+};
+
+class IncrementalCUDADeviceParser : public IncrementalDeviceParser {
 
 public:
   IncrementalCUDADeviceParser(
@@ -33,21 +58,20 @@ class IncrementalCUDADeviceParser : public 
IncrementalParser {
       llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
       llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
 
+  llvm::Error GenerateOffloadBinary() override;
+
+  ~IncrementalCUDADeviceParser() override;
+
+private:
   // Generate PTX for the last PTU.
   llvm::Expected<llvm::StringRef> GeneratePTX();
 
   // Generate fatbinary contents in memory
   llvm::Error GenerateFatbinary();
 
-  ~IncrementalCUDADeviceParser();
-
-protected:
   int SMVersion;
   llvm::SmallString<1024> PTXCode;
   llvm::SmallVector<char, 1024> FatbinContent;
-  llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
-  CodeGenOptions &CodeGenOpts; // Intentionally a reference.
-  const TargetOptions &TargetOpts;
 };
 
 } // namespace clang
diff --git a/clang/lib/Interpreter/Interpreter.cpp 
b/clang/lib/Interpreter/Interpreter.cpp
index 0536fdcd548a2..38bb914cc6303 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -548,11 +548,7 @@ Interpreter::Parse(llvm::StringRef Code) {
 
     DeviceParser->RegisterPTU(*DeviceTU);
 
-    llvm::Expected<llvm::StringRef> PTX = DeviceParser->GeneratePTX();
-    if (!PTX)
-      return PTX.takeError();
-
-    llvm::Error Err = DeviceParser->GenerateFatbinary();
+    llvm::Error Err = DeviceParser->GenerateOffloadBinary();
     if (Err)
       return std::move(Err);
   }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to