summaryrefslogtreecommitdiff
path: root/offload/plugins-nextgen
diff options
context:
space:
mode:
authorRoss Brunton <ross@codeplay.com>2025-07-14 16:05:41 +0100
committerRoss Brunton <ross@codeplay.com>2025-07-14 16:13:03 +0100
commit8589fcc6d053cb2937cf970d1ce354abfb84da31 (patch)
tree16d0fe40bf88867c2c2591c6e7eac86a9d43cda5 /offload/plugins-nextgen
parentfa31376cc84b9039fc91bdccb1362f98074da255 (diff)
[Offload] Add `olLinkProgram`users/RossBrunton/link2
A version of `olCreateProgram` that inputs many bitcode files and links them together before loading them.
Diffstat (limited to 'offload/plugins-nextgen')
-rw-r--r--offload/plugins-nextgen/common/include/JIT.h4
-rw-r--r--offload/plugins-nextgen/common/include/PluginInterface.h4
-rw-r--r--offload/plugins-nextgen/common/src/JIT.cpp41
-rw-r--r--offload/plugins-nextgen/common/src/PluginInterface.cpp7
4 files changed, 56 insertions, 0 deletions
diff --git a/offload/plugins-nextgen/common/include/JIT.h b/offload/plugins-nextgen/common/include/JIT.h
index 1d6280a0af14..08b82c4aefb8 100644
--- a/offload/plugins-nextgen/common/include/JIT.h
+++ b/offload/plugins-nextgen/common/include/JIT.h
@@ -55,6 +55,10 @@ struct JITEngine {
process(const __tgt_device_image &Image,
target::plugin::GenericDeviceTy &Device);
+ /// Link and compile multiple bitcode images into a single binary
+ Expected<__tgt_device_image> link(std::vector<__tgt_device_image> &Images,
+ target::plugin::GenericDeviceTy &Device);
+
private:
/// Compile the bitcode image \p Image and generate the binary image that can
/// be loaded to the target device of the triple \p Triple architecture \p
diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 7824257d28e1..79e021cc64f3 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -749,6 +749,10 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
/// Load the binary image into the device and return the target table.
Expected<DeviceImageTy *> loadBinary(GenericPluginTy &Plugin,
const __tgt_device_image *TgtImage);
+ /// Link and compile multiple bitcode images into a single image.
+ Expected<__tgt_device_image>
+ jitLinkBinary(GenericPluginTy &Plugin,
+ std::vector<__tgt_device_image> InputImages);
virtual Expected<DeviceImageTy *>
loadBinaryImpl(const __tgt_device_image *TgtImage, int32_t ImageId) = 0;
diff --git a/offload/plugins-nextgen/common/src/JIT.cpp b/offload/plugins-nextgen/common/src/JIT.cpp
index 835dcc0da2ec..2cf6ddbfdff0 100644
--- a/offload/plugins-nextgen/common/src/JIT.cpp
+++ b/offload/plugins-nextgen/common/src/JIT.cpp
@@ -327,3 +327,44 @@ JITEngine::process(const __tgt_device_image &Image,
return &Image;
}
+
+Expected<__tgt_device_image>
+JITEngine::link(std::vector<__tgt_device_image> &Images,
+ target::plugin::GenericDeviceTy &Device) {
+ const std::string &ComputeUnitKind = Device.getComputeUnitKind();
+ ComputeUnitInfo &CUI = ComputeUnitMap[ComputeUnitKind];
+
+ PostProcessingFn PostProcessing =
+ [&Device](llvm::SmallVector<std::unique_ptr<MemoryBuffer>> &&MB)
+ -> Expected<std::unique_ptr<MemoryBuffer>> {
+ return Device.doJITPostProcessing(std::move(MB));
+ };
+
+ std::lock_guard<std::mutex> Lock(ComputeUnitMapMutex);
+
+ llvm::SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
+ size_t Index = 0;
+ for (auto &I : Images) {
+ if (!isImageBitcode(I))
+ return error::createOffloadError(
+ error::ErrorCode::INVALID_BINARY,
+ "binary %i provided to link operation is not bitcode", Index);
+ Index++;
+
+ auto ObjMBOrErr = getOrCreateObjFile(I, CUI.Context, ComputeUnitKind);
+ if (!ObjMBOrErr)
+ return ObjMBOrErr.takeError();
+ Buffers.push_back(std::move(*ObjMBOrErr));
+ }
+
+ auto ImageMBOrErr = PostProcessing(std::move(Buffers));
+ if (!ImageMBOrErr)
+ return ImageMBOrErr.takeError();
+
+ auto &ImageMB = CUI.JITImages.emplace_back(std::move(*ImageMBOrErr));
+ __tgt_device_image JITedImage{};
+ JITedImage.ImageStart = const_cast<char *>(ImageMB->getBufferStart());
+ JITedImage.ImageEnd = const_cast<char *>(ImageMB->getBufferEnd());
+
+ return JITedImage;
+}
diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp
index 81b9d423e13d..9e2234dcc148 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -903,6 +903,13 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
return deinitImpl();
}
+
+Expected<__tgt_device_image>
+GenericDeviceTy::jitLinkBinary(GenericPluginTy &Plugin,
+ std::vector<__tgt_device_image> InputImages) {
+ return Plugin.getJIT().link(InputImages, *this);
+}
+
Expected<DeviceImageTy *>
GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
const __tgt_device_image *InputTgtImage) {