[clang] [llvm] [Clang][SYCL] Compile device code once per IGCA target (PR #239010)
Yury Plyakhin via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 18:10:31 PST 2026
================
@@ -909,15 +927,79 @@ bundleOpenMP(ArrayRef<OffloadingImage> Images) {
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
bundleSYCL(ArrayRef<OffloadingImage> Images) {
+ // clang-sycl-linker packs its outputs into one offload binary, which is
+ // passed to the offload wrapper as is.
SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
+ if (Images.size() <= 1) {
+ for (const OffloadingImage &Image : Images)
+ Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(
+ Image.Image->getBuffer(), Image.Image->getBufferIdentifier()));
+ return std::move(Buffers);
+ }
+
+ // Images with different "code" are linked separately, but the SYCL runtime
+ // registers a single offload binary, so merge their device images into one.
+ SmallVector<std::unique_ptr<OffloadBinary>> Binaries;
+ SmallVector<OffloadingImage> Merged;
for (const OffloadingImage &Image : Images) {
- // clang-sycl-linker packs outputs into one binary blob. Therefore, it is
- // passed to Offload Wrapper as is.
- StringRef S(Image.Image->getBufferStart(), Image.Image->getBufferSize());
- Buffers.emplace_back(
- MemoryBuffer::getMemBufferCopy(S, Image.Image->getBufferIdentifier()));
+ // A dry run has no linked output to read.
+ if (Image.Image->getBufferSize() == 0)
+ continue;
+
+ // With --embed-bitcode, the image is the linked bitcode itself rather than
+ // an offload binary, so it is one device image.
+ if (identify_magic(Image.Image->getBuffer()) !=
+ file_magic::offload_binary) {
+ OffloadingImage Copy;
+ Copy.TheImageKind = Image.TheImageKind;
+ Copy.TheOffloadKind = Image.TheOffloadKind;
+ Copy.Flags = Image.Flags;
+ Copy.StringData = Image.StringData;
+ Copy.Image = MemoryBuffer::getMemBuffer(Image.Image->getMemBufferRef(),
+ /*RequiresNullTerminator=*/false);
+ Merged.push_back(std::move(Copy));
+ continue;
+ }
+
+ auto BinariesOrErr = OffloadBinary::create(Image.Image->getMemBufferRef());
+ if (!BinariesOrErr)
+ return BinariesOrErr.takeError();
+ for (std::unique_ptr<OffloadBinary> &Binary : *BinariesOrErr) {
+ OffloadingImage Entry;
+ Entry.TheImageKind = Binary->getImageKind();
+ Entry.TheOffloadKind = Binary->getOffloadKind();
+ Entry.Flags = Binary->getFlags();
+ for (const auto &[Key, Value] : Binary->strings())
+ Entry.StringData[Key] = Value;
+ Entry.Image =
+ MemoryBuffer::getMemBuffer(Binary->getImage(), "",
+ /*RequiresNullTerminator=*/false);
+ Merged.push_back(std::move(Entry));
+ Binaries.push_back(std::move(Binary));
+ }
}
+ SmallString<0> Data;
+ if (!Merged.empty())
+ Data = OffloadBinary::write(Merged);
+
+ // With --save-temps, also write the merged offload binary to its own file,
+ // which --wrapper-verbose passes to llvm-offload-wrapper.
+ StringRef Identifier = Images.front().Image->getBufferIdentifier();
+ if (SaveTemps) {
+ auto FileOrErr = createOutputFile(ExecutableName + ".sycl.merged", "out");
+ if (!FileOrErr)
+ return FileOrErr.takeError();
+ if (!DryRun) {
+ int FD = -1;
+ if (std::error_code EC = sys::fs::openFileForWrite(*FileOrErr, FD))
+ return errorCodeToError(EC);
+ llvm::raw_fd_ostream OS(FD, true);
+ OS << Data;
+ }
+ Identifier = *FileOrErr;
+ }
+ Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(Data, Identifier));
----------------
YuriPlyakhin wrote:
Can we avoid Data copy?
```suggestion
Buffers.emplace_back(std::make_unique<SmallVectorMemoryBuffer>(std::move(Data), Identifier, /*RequiresNullTerminator=*/false));
```
https://github.com/llvm/llvm-project/pull/239010
More information about the cfe-commits
mailing list