https://github.com/AdityaSinha149 updated https://github.com/llvm/llvm-project/pull/231391
>From ebec042c12844bb47fd08531b341c2aac3401ecb Mon Sep 17 00:00:00 2001 From: AdityaSinha149 <[email protected]> Date: Tue, 13 Oct 2026 11:57:25 +0530 Subject: [PATCH] [clang-repl] Add OffloadType enum and HIP device parser skeleton --- clang/include/clang/Interpreter/Interpreter.h | 8 ++++++ clang/lib/Interpreter/DeviceOffload.cpp | 27 +++++++++++++++++++ clang/lib/Interpreter/DeviceOffload.h | 16 ++++++++++- clang/lib/Interpreter/Interpreter.cpp | 24 ++++++++++++++--- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h index a598574b417c08..64f02b797c204f 100644 --- a/clang/include/clang/Interpreter/Interpreter.h +++ b/clang/include/clang/Interpreter/Interpreter.h @@ -46,6 +46,10 @@ class Decl; class IncrementalParser; class IncrementalDeviceParser; +/// The device language to offload to. \c Detect selects the vendor of the +/// device available on the machine. +enum class OffloadType { Detect, CUDA, HIP }; + /// Create a pre-configured \c CompilerInstance for incremental processing. class IncrementalCompilerBuilder { using DriverCompilationFn = llvm::Error(const driver::Compilation &); @@ -147,6 +151,10 @@ class Interpreter { create(std::unique_ptr<CompilerInstance> CI, std::unique_ptr<IncrementalExecutorBuilder> IEB = nullptr); static llvm::Expected<std::unique_ptr<Interpreter>> + createWithDevice(std::unique_ptr<CompilerInstance> CI, + std::unique_ptr<CompilerInstance> DCI, + OffloadType Type = OffloadType::Detect); + static llvm::Expected<std::unique_ptr<Interpreter>> createWithCUDA(std::unique_ptr<CompilerInstance> CI, std::unique_ptr<CompilerInstance> DCI); diff --git a/clang/lib/Interpreter/DeviceOffload.cpp b/clang/lib/Interpreter/DeviceOffload.cpp index e5887bc9c88418..3f416a1918e245 100644 --- a/clang/lib/Interpreter/DeviceOffload.cpp +++ b/clang/lib/Interpreter/DeviceOffload.cpp @@ -36,6 +36,33 @@ IncrementalDeviceParser::IncrementalDeviceParser( IncrementalDeviceParser::~IncrementalDeviceParser() {} +IncrementalHIPDeviceParser::IncrementalHIPDeviceParser( + 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; + if (!Arch.starts_with("gfx")) { + Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>( + "Invalid HIP architecture", + llvm::inconvertibleErrorCode())); + return; + } +} + +llvm::Error IncrementalHIPDeviceParser::GenerateOffloadBinary() { + // FIXME: Generate the HSACO and bundle it for the host compilation. + return llvm::make_error<llvm::StringError>( + "HIP offload binary generation is not supported yet.", + llvm::inconvertibleErrorCode()); +} + +IncrementalHIPDeviceParser::~IncrementalHIPDeviceParser() {} + IncrementalCUDADeviceParser::IncrementalCUDADeviceParser( CompilerInstance &DeviceInstance, CompilerInstance &HostInstance, IncrementalAction *DeviceAct, diff --git a/clang/lib/Interpreter/DeviceOffload.h b/clang/lib/Interpreter/DeviceOffload.h index fc780ef64add89..0275c0e10cda80 100644 --- a/clang/lib/Interpreter/DeviceOffload.h +++ b/clang/lib/Interpreter/DeviceOffload.h @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines a base class required for offloading to devices. +// This file defines the classes required for offloading to devices. // //===----------------------------------------------------------------------===// @@ -44,6 +44,20 @@ class IncrementalDeviceParser : public IncrementalParser { const TargetOptions &TargetOpts; }; +class IncrementalHIPDeviceParser : public IncrementalDeviceParser { + +public: + IncrementalHIPDeviceParser( + CompilerInstance &DeviceInstance, CompilerInstance &HostInstance, + IncrementalAction *DeviceAct, + llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS, + llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs); + + llvm::Error GenerateOffloadBinary() override; + + ~IncrementalHIPDeviceParser() override; +}; + class IncrementalCUDADeviceParser : public IncrementalDeviceParser { public: diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp index a8e54d62864b5c..9bd711c24be26d 100644 --- a/clang/lib/Interpreter/Interpreter.cpp +++ b/clang/lib/Interpreter/Interpreter.cpp @@ -479,6 +479,18 @@ llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::create( llvm::Expected<std::unique_ptr<Interpreter>> Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI, std::unique_ptr<CompilerInstance> DCI) { + return createWithDevice(std::move(CI), std::move(DCI), OffloadType::CUDA); +} + +llvm::Expected<std::unique_ptr<Interpreter>> +Interpreter::createWithDevice(std::unique_ptr<CompilerInstance> CI, + std::unique_ptr<CompilerInstance> DCI, + OffloadType Type) { + if (Type == OffloadType::Detect) + return llvm::make_error<llvm::StringError>( + "Detecting the device vendor is not supported yet.", + llvm::inconvertibleErrorCode()); + // avoid writing fat binary to disk using an in-memory virtual file system llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> IMVFS = std::make_unique<llvm::vfs::InMemoryFileSystem>(); @@ -515,9 +527,15 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI, Interp->DeviceCI = std::move(DCI); - auto DeviceParser = std::make_unique<IncrementalCUDADeviceParser>( - *Interp->DeviceCI, *Interp->getCompilerInstance(), - Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs); + std::unique_ptr<IncrementalDeviceParser> DeviceParser; + if (Type == OffloadType::HIP) + DeviceParser = std::make_unique<IncrementalHIPDeviceParser>( + *Interp->DeviceCI, *Interp->getCompilerInstance(), + Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs); + else + DeviceParser = std::make_unique<IncrementalCUDADeviceParser>( + *Interp->DeviceCI, *Interp->getCompilerInstance(), + Interp->DeviceAct.get(), IMVFS, Err, Interp->PTUs); if (Err) return std::move(Err); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
