Skip to content

Commit

Permalink
[SYCL][NFC] Move SYCL Module Splitting to library. Part 1 (#13054)
Browse files Browse the repository at this point in the history
This patch is a part 1 of moving sycl-post-link functionality to library
for New Offloading initiative.
This patch replaces `std::vector<char>` with `MemoryBuffer` as a
container for accessing device image in the `SYCLImage` class.
`MemoryBuffer` uses `mmap` that allows not to hold the whole device
image in the RAM.
  • Loading branch information
maksimsab authored Mar 27, 2024
1 parent 25b75ea commit bb48303
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
17 changes: 4 additions & 13 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,16 +714,6 @@ static Expected<StringRef> runLLVMToSPIRVTranslation(StringRef InputTable,
return *Output;
}

Expected<std::vector<char>> readBinaryFile(StringRef File) {
auto MBOrErr = MemoryBuffer::getFile(File, /*IsText*/ false,
/*RequiresNullTerminator */ false);
if (!MBOrErr)
return createFileError(File, MBOrErr.getError());

auto &MB = *MBOrErr;
return std::vector<char>(MB->getBufferStart(), MB->getBufferEnd());
}

Expected<std::string> readTextFile(StringRef File) {
auto MBOrErr = MemoryBuffer::getFile(File, /*IsText*/ true,
/*RequiresNullTerminator */ true);
Expand Down Expand Up @@ -768,9 +758,10 @@ readSYCLImagesFromTable(StringRef TableFile, const ArgList &Args) {

SmallVector<offloading::SYCLImage> Images;
for (const util::SimpleTable::Row &row : Table->rows()) {
auto ImageOrErr = readBinaryFile(row.getCell("Code"));
auto ImagePath = row.getCell("Code");
auto ImageOrErr = MemoryBuffer::getFile(ImagePath);
if (!ImageOrErr)
return ImageOrErr.takeError();
return createFileError(ImagePath, ImageOrErr.getError());

auto PropertiesOrErr =
readPropertyRegistryFromFile(row.getCell("Properties"));
Expand All @@ -788,7 +779,7 @@ readSYCLImagesFromTable(StringRef TableFile, const ArgList &Args) {
Images.push_back(std::move(Image));
}

return Images;
return std::move(Images);
}

/// Reads device images from the given \p InputFile and wraps them
Expand Down
18 changes: 16 additions & 2 deletions llvm/include/llvm/Frontend/Offloading/SYCLOffloadWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PropertySetIO.h"

#include <string>
Expand All @@ -30,7 +31,20 @@ enum class SYCLBinaryImageFormat {
};

struct SYCLImage {
std::vector<char> Image;
SYCLImage() = default;
SYCLImage(const SYCLImage &) = delete;
SYCLImage &operator=(const SYCLImage &) = delete;
SYCLImage(SYCLImage &&) = default;
SYCLImage &operator=(SYCLImage &&) = default;

SYCLImage(std::unique_ptr<llvm::MemoryBuffer> Image,
const llvm::util::PropertySetRegistry &Registry,
llvm::StringRef Entries, llvm::StringRef Target = "")
: Image(std::move(Image)), PropertyRegistry(std::move(Registry)),
Entries(Entries.begin(), Entries.size()),
Target(Target.begin(), Target.size()) {}

std::unique_ptr<llvm::MemoryBuffer> Image;
llvm::util::PropertySetRegistry PropertyRegistry;

std::string Entries;
Expand All @@ -57,7 +71,7 @@ struct SYCLWrappingOptions {
/// as global symbols and registers the images with the SYCL Runtime.
/// \param Options Settings that allows to turn on optional data and settings.
llvm::Error
wrapSYCLBinaries(llvm::Module &M, llvm::SmallVector<SYCLImage> &Images,
wrapSYCLBinaries(llvm::Module &M, const llvm::SmallVector<SYCLImage> &Images,
SYCLWrappingOptions Options = SYCLWrappingOptions());

} // namespace offloading
Expand Down
16 changes: 8 additions & 8 deletions llvm/lib/Frontend/Offloading/SYCLOffloadWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,10 @@ struct Wrapper {
if (Image.Target == "native_cpu")
Binary = addDeclarationsForNativeCPU(Image.Entries);
else {
auto &MB = *Image.Image;
Binary = addDeviceImageToModule(
Image.Image, Twine(OffloadKindTag) + ImageID + ".data", Image.Target);
ArrayRef<char>(MB.getBufferStart(), MB.getBufferEnd()),
Twine(OffloadKindTag) + ImageID + ".data", Image.Target);
}

// TODO: Manifests are going to be removed.
Expand All @@ -576,8 +578,8 @@ struct Wrapper {
PropSets.first, PropSets.second);

if (Options.EmitRegistrationFunctions)
emitRegistrationFunctions(Binary.first, Image.Image.size(), ImageID,
OffloadKindTag);
emitRegistrationFunctions(Binary.first, Image.Image->getBufferSize(),
ImageID, OffloadKindTag);

return WrappedImage;
}
Expand Down Expand Up @@ -669,14 +671,12 @@ struct Wrapper {
/// \endcode
///
/// \returns Global variable that represents FatbinDesc.
GlobalVariable *createFatbinDesc(SmallVector<SYCLImage> &Images) {
GlobalVariable *createFatbinDesc(const SmallVector<SYCLImage> &Images) {
const char *OffloadKindTag = ".sycl_offloading.";
SmallVector<Constant *> WrappedImages;
WrappedImages.reserve(Images.size());
for (size_t i = 0; i != Images.size(); ++i) {
for (size_t i = 0; i != Images.size(); ++i)
WrappedImages.push_back(wrapImage(Images[i], Twine(i), OffloadKindTag));
Images[i].Image.clear(); // This is just for economy of RAM.
}

return combineWrappedImages(WrappedImages, OffloadKindTag);
}
Expand Down Expand Up @@ -729,7 +729,7 @@ struct Wrapper {
} // anonymous namespace

Error llvm::offloading::wrapSYCLBinaries(llvm::Module &M,
SmallVector<SYCLImage> &Images,
const SmallVector<SYCLImage> &Images,
SYCLWrappingOptions Options) {
Wrapper W(M, Options);
GlobalVariable *Desc = W.createFatbinDesc(Images);
Expand Down

0 comments on commit bb48303

Please sign in to comment.