Skip to content

Commit 20e54a1

Browse files
committed
Simplify handling of SYCL images in clang-linker-wrapper.
Now Offload Wrapper handles the binary blob which contains sycl images. Also, the issue with duplicated entry names strings is resolved. SYCLWrappingOptions are renamed to SYCLJITOptions.
1 parent ad141aa commit 20e54a1

File tree

7 files changed

+49
-73
lines changed

7 files changed

+49
-73
lines changed

clang/test/Driver/linker-wrapper-image.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,8 @@
276276
// SYCL: %__sycl.tgt_device_image = type { i16, i8, i8, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
277277
// SYCL-NEXT: %__sycl.tgt_bin_desc = type { i16, i16, ptr, ptr, ptr }
278278

279-
// SYCL: @.sycl_offloading.target.0 = internal unnamed_addr constant [1 x i8] zeroinitializer
280-
// SYCL-NEXT: @.sycl_offloading.opts.compile.0 = internal unnamed_addr constant [1 x i8] zeroinitializer
281-
// SYCL-NEXT: @.sycl_offloading.opts.link.0 = internal unnamed_addr constant [1 x i8] zeroinitializer
282-
// SYCL-NEXT: @.sycl_offloading.0.data = internal unnamed_addr constant [0 x i8] zeroinitializer
283-
// SYCL-NEXT: @__sycl_offload_entries_arr = internal constant [0 x %struct.__tgt_offload_entry] zeroinitializer
284-
// SYCL-NEXT: @.sycl_offloading.0.info = internal local_unnamed_addr constant [2 x i64] [i64 ptrtoint (ptr @.sycl_offloading.0.data to i64), i64 0], section ".tgtimg", align 16
285-
// SYCL-NEXT: @llvm.used = appending global [1 x ptr] [ptr @.sycl_offloading.0.info], section "llvm.metadata"
286-
// SYCL-NEXT: @.sycl_offloading.device_images = internal unnamed_addr constant [1 x %__sycl.tgt_device_image] [%__sycl.tgt_device_image { i16 3, i8 8, i8 0, ptr @.sycl_offloading.target.0, ptr @.sycl_offloading.opts.compile.0, ptr @.sycl_offloading.opts.link.0, ptr @.sycl_offloading.0.data, ptr @.sycl_offloading.0.data, ptr @__sycl_offload_entries_arr, ptr @__sycl_offload_entries_arr, ptr null, ptr null }]
287-
// SYCL-NEXT: @.sycl_offloading.descriptor = internal constant %__sycl.tgt_bin_desc { i16 1, i16 1, ptr @.sycl_offloading.device_images, ptr null, ptr null }
279+
// SYCL: @.sycl_offloading.device_images = internal unnamed_addr constant [0 x %__sycl.tgt_device_image] zeroinitializer
280+
// SYCL-NEXT: @.sycl_offloading.descriptor = internal constant %__sycl.tgt_bin_desc { i16 1, i16 0, ptr @.sycl_offloading.device_images, ptr null, ptr null }
288281
// SYCL-NEXT: @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @sycl.descriptor_reg, ptr null }]
289282
// SYCL-NEXT: @llvm.global_dtors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 1, ptr @sycl.descriptor_unreg, ptr null }]
290283

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ wrapDeviceImages(ArrayRef<std::unique_ptr<MemoryBuffer>> Buffers,
719719
break;
720720
case OFK_SYCL: {
721721
// TODO: fill these options once the Driver supports them.
722-
offloading::SYCLWrappingOptions WrappingOptions;
722+
offloading::SYCLJITOptions Options;
723723
if (Error Err =
724-
offloading::wrapSYCLBinaries(M, BuffersToWrap, WrappingOptions))
724+
offloading::wrapSYCLBinaries(M, BuffersToWrap.front(), Options))
725725
return std::move(Err);
726726
break;
727727
}
@@ -765,31 +765,14 @@ bundleOpenMP(ArrayRef<OffloadingImage> Images) {
765765
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>
766766
bundleSYCL(ArrayRef<OffloadingImage> Images) {
767767
SmallVector<std::unique_ptr<MemoryBuffer>> Buffers;
768-
if (DryRun) {
769-
// In dry-run mode there is an empty input which is insufficient for
770-
// the testing. Therefore, we insert a stub value.
771-
OffloadBinary::OffloadingImage Image;
772-
Image.TheOffloadKind = OffloadKind::OFK_SYCL;
773-
Image.Image = MemoryBuffer::getMemBufferCopy("");
774-
SmallString<0> SerializedImage = OffloadBinary::write(Image);
775-
Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(SerializedImage));
776-
return Buffers;
768+
for (const OffloadingImage &Image : Images) {
769+
// clang-sycl-linker packs outputs into one binary blob. Therefore, it is
770+
// passed to Offload Wrapper as is.
771+
StringRef S(Image.Image->getBufferStart(), Image.Image->getBufferSize());
772+
Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(S));
777773
}
778774

779-
for (const OffloadingImage &TheImage : Images) {
780-
SmallVector<OffloadFile> OffloadBinaries;
781-
if (Error E = extractOffloadBinaries(*TheImage.Image, OffloadBinaries))
782-
return E;
783-
784-
for (const OffloadFile &File : OffloadBinaries) {
785-
const OffloadBinary &Binary = *File.getBinary();
786-
SmallString<0> SerializedImage =
787-
OffloadBinary::write(Binary.getOffloadingImage());
788-
Buffers.emplace_back(MemoryBuffer::getMemBufferCopy(SerializedImage));
789-
}
790-
}
791-
792-
return Buffers;
775+
return std::move(Buffers);
793776
}
794777

795778
Expected<SmallVector<std::unique_ptr<MemoryBuffer>>>

llvm/include/llvm/Frontend/Offloading/OffloadWrapper.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ LLVM_ABI llvm::Error wrapHIPBinary(llvm::Module &M, llvm::ArrayRef<char> Images,
5555
llvm::StringRef Suffix = "",
5656
bool EmitSurfacesAndTextures = true);
5757

58-
struct SYCLWrappingOptions {
58+
struct SYCLJITOptions {
5959
// Target/compiler specific options that are suggested to use to "compile"
6060
// program at runtime.
6161
std::string CompileOptions;
@@ -66,10 +66,10 @@ struct SYCLWrappingOptions {
6666

6767
/// Wraps OffloadBinaries in the given \p Buffers into the module \p M
6868
/// as global symbols and registers the images with the SYCL Runtime.
69-
/// \param Options Settings that allows to turn on optional data and settings.
70-
llvm::Error LLVM_ABI
71-
wrapSYCLBinaries(llvm::Module &M, llvm::ArrayRef<llvm::ArrayRef<char>> Buffers,
72-
SYCLWrappingOptions Options = SYCLWrappingOptions());
69+
/// \param Options Data that needs to be encoded for the later use in a runtime.
70+
LLVM_ABI llvm::Error
71+
wrapSYCLBinaries(llvm::Module &M, llvm::ArrayRef<char> Buffer,
72+
SYCLJITOptions Options = SYCLJITOptions());
7373

7474
} // namespace offloading
7575
} // namespace llvm

llvm/include/llvm/Object/OffloadBinary.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class OffloadBinary : public Binary {
8585
/// Serialize the contents of \p File to a binary buffer to be read later.
8686
LLVM_ABI static SmallString<0> write(const OffloadingImage &);
8787

88-
OffloadingImage getOffloadingImage() const;
89-
9088
static uint64_t getAlignment() { return 8; }
9189

9290
ImageKind getImageKind() const { return TheEntry->TheImageKind; }

llvm/lib/Frontend/Offloading/OffloadWrapper.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -640,13 +640,13 @@ void createRegisterFatbinFunction(Module &M, GlobalVariable *FatbinDesc,
640640
struct SYCLWrapper {
641641
Module &M;
642642
LLVMContext &C;
643-
SYCLWrappingOptions Options;
643+
SYCLJITOptions Options;
644644

645645
StructType *EntryTy = nullptr;
646646
StructType *SyclDeviceImageTy = nullptr;
647647
StructType *SyclBinDescTy = nullptr;
648648

649-
SYCLWrapper(Module &M, const SYCLWrappingOptions &Options)
649+
SYCLWrapper(Module &M, const SYCLJITOptions &Options)
650650
: M(M), C(M.getContext()), Options(Options) {
651651
EntryTy = offloading::getEntryTy(M);
652652
SyclDeviceImageTy = getSyclDeviceImageTy();
@@ -812,11 +812,12 @@ struct SYCLWrapper {
812812
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(
813813
Entries, /*BufferName*/ "", /*RequiresNullTerminator*/ false);
814814
for (line_iterator LI(*MB); !LI.is_at_eof(); ++LI) {
815-
Constant *C = addStringToModule(*LI, "__sycl_offload_entry_name");
816-
GlobalVariable *GV =
817-
emitOffloadingEntry(M, /*Kind*/ OffloadKind::OFK_SYCL, C,
818-
/*Name*/ "__sycl_offload_entry_name", /*Size*/ 0,
819-
/*Flags*/ 0, /*Data*/ 0);
815+
GlobalVariable *GV = emitOffloadingEntry(
816+
M, /*Kind*/ OffloadKind::OFK_SYCL,
817+
Constant::getNullValue(PointerType::getUnqual(C)),
818+
/*Name*/ *LI, /*Size*/ 0,
819+
///*Name*/ "__sycl_offload_entry_name", /*Size*/ 0,
820+
/*Flags*/ 0, /*Data*/ 0);
820821
EntriesInits.push_back(GV->getInitializer());
821822
}
822823

@@ -1079,22 +1080,30 @@ Error offloading::wrapHIPBinary(Module &M, ArrayRef<char> Image,
10791080
return Error::success();
10801081
}
10811082

1082-
Error llvm::offloading::wrapSYCLBinaries(llvm::Module &M,
1083-
ArrayRef<ArrayRef<char>> Buffers,
1084-
SYCLWrappingOptions Options) {
1083+
Error llvm::offloading::wrapSYCLBinaries(llvm::Module &M, ArrayRef<char> Buffer,
1084+
SYCLJITOptions Options) {
10851085
SYCLWrapper W(M, Options);
1086-
SmallVector<std::unique_ptr<OffloadBinary>> OffloadBinaries;
1087-
OffloadBinaries.reserve(Buffers.size());
1086+
MemoryBufferRef MBR(StringRef(Buffer.begin(), Buffer.size()),
1087+
/*Identifier*/ "");
1088+
SmallVector<OffloadFile> OffloadFiles;
1089+
if (Error E = extractOffloadBinaries(MBR, OffloadFiles))
1090+
return E;
1091+
10881092
SmallVector<OffloadingImage> Images;
1089-
Images.reserve(Buffers.size());
1090-
for (auto Buf : Buffers) {
1091-
MemoryBufferRef MBR(StringRef(Buf.begin(), Buf.size()), /*Identifier*/ "");
1092-
auto OffloadBinaryOrErr = OffloadBinary::create(MBR);
1093-
if (!OffloadBinaryOrErr)
1094-
return OffloadBinaryOrErr.takeError();
1095-
1096-
OffloadBinaries.emplace_back(std::move(*OffloadBinaryOrErr));
1097-
Images.emplace_back(OffloadBinaries.back()->getOffloadingImage());
1093+
Images.reserve(OffloadFiles.size());
1094+
for (OffloadFile &File : OffloadFiles) {
1095+
OffloadBinary &Binary = *File.getBinary();
1096+
OffloadingImage OI;
1097+
OI.TheImageKind = Binary.getImageKind();
1098+
OI.TheOffloadKind = Binary.getOffloadKind();
1099+
OI.Flags = Binary.getFlags();
1100+
OI.StringData["arch"] = Binary.getString("arch");
1101+
OI.StringData["triple"] = Binary.getString("triple");
1102+
OI.StringData["symbols"] = Binary.getString("symbols");
1103+
OI.Image = MemoryBuffer::getMemBuffer(
1104+
MemoryBufferRef(Binary.getImage(), /*Identifier*/ ""),
1105+
/*RequiresNullTerminator*/ false);
1106+
Images.emplace_back(std::move(OI));
10981107
}
10991108

11001109
GlobalVariable *Desc = W.createFatbinDesc(Images);

llvm/lib/Object/OffloadBinary.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,6 @@ SmallString<0> OffloadBinary::write(const OffloadingImage &OffloadingData) {
266266
return Data;
267267
}
268268

269-
OffloadBinary::OffloadingImage OffloadBinary::getOffloadingImage() const {
270-
OffloadingImage OI;
271-
OI.TheImageKind = getImageKind();
272-
OI.TheOffloadKind = getOffloadKind();
273-
OI.Flags = getFlags();
274-
OI.StringData = StringData;
275-
OI.Image = MemoryBuffer::getMemBuffer(
276-
MemoryBufferRef(getImage(), /*Identifier*/ ""));
277-
return OI;
278-
}
279-
280269
Error object::extractOffloadBinaries(MemoryBufferRef Buffer,
281270
SmallVectorImpl<OffloadFile> &Binaries) {
282271
file_magic Type = identify_magic(Buffer.getBuffer());

llvm/tools/llvm-offload-wrapper/llvm-offload-wrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ static Error wrapImages(ArrayRef<ArrayRef<char>> BuffersToWrap) {
8484
M, BuffersToWrap.front(), offloading::getOffloadEntryArray(M)))
8585
return Err;
8686
break;
87+
case llvm::object::OFK_SYCL:
88+
if (Error Err = offloading::wrapSYCLBinaries(M, BuffersToWrap.front()))
89+
return Err;
90+
break;
8791
default:
8892
return createStringError(getOffloadKindName(Kind) +
8993
" wrapping is not supported");

0 commit comments

Comments
 (0)