[clang] [clang-repl] Add OffloadType enum and HIP device parser skeleton (PR #231391)
Aditya Sinha via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 14 02:58:26 PDT 2026
https://github.com/AdityaSinha149 updated https://github.com/llvm/llvm-project/pull/231391
>From 94d9cdfce372ed8817590578fdb8d70cddebf369 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
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 | 29 +++++++++++++++++++
clang/lib/Interpreter/DeviceOffload.h | 16 +++++++++-
clang/lib/Interpreter/Interpreter.cpp | 24 +++++++++++++--
4 files changed, 73 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..31fa20fff450da 100644
--- a/clang/lib/Interpreter/DeviceOffload.cpp
+++ b/clang/lib/Interpreter/DeviceOffload.cpp
@@ -22,6 +22,7 @@
#include "llvm/IR/Module.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Target/TargetMachine.h"
+#include "llvm/TargetParser/Triple.h"
namespace clang {
@@ -36,6 +37,34 @@ 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;
+ if (!llvm::Triple(TargetOpts.Triple).isAMDGCN()) {
+ Err = llvm::joinErrors(
+ std::move(Err),
+ llvm::make_error<llvm::StringError>(
+ "Invalid HIP architecture for target '" + TargetOpts.Triple + "'",
+ 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);
More information about the cfe-commits
mailing list