diff options
author | Joseph Huber <huberjn@outlook.com> | 2024-02-14 22:09:48 -0600 |
---|---|---|
committer | Joseph Huber <huberjn@outlook.com> | 2024-02-14 22:11:21 -0600 |
commit | fa9e297b8b63dacb962d99814e698658ad71f946 (patch) | |
tree | f71fa38a6e7598c94f2e07e158626ac9b0b538d1 | |
parent | d821650e13145a1acccd337c9853354ad6531507 (diff) | |
download | llvm-fa9e297b8b63dacb962d99814e698658ad71f946.zip llvm-fa9e297b8b63dacb962d99814e698658ad71f946.tar.gz llvm-fa9e297b8b63dacb962d99814e698658ad71f946.tar.bz2 |
[ClangPackager] Fix passing in multiple instances of `file`
Summary:
This is necessary because CMake build tools might need to generate
several files but are unable to put them in separate images. This patch
sipmly moves the file handling out into a separate split iterator.
-rw-r--r-- | clang/tools/clang-offload-packager/ClangOffloadPackager.cpp | 53 |
1 files changed, 28 insertions, 25 deletions
diff --git a/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp b/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp index 08de3f3..c36a5aa 100644 --- a/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp +++ b/clang/tools/clang-offload-packager/ClangOffloadPackager.cpp @@ -104,33 +104,36 @@ static Error bundleImages() { inconvertibleErrorCode(), "'file' and 'triple' are required image arguments"); - OffloadBinary::OffloadingImage ImageBinary{}; - std::unique_ptr<llvm::MemoryBuffer> DeviceImage; - for (const auto &[Key, Value] : Args) { - if (Key == "file") { - llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr = - llvm::MemoryBuffer::getFileOrSTDIN(Value); - if (std::error_code EC = ObjectOrErr.getError()) - return errorCodeToError(EC); - - // Clang uses the '.o' suffix for LTO bitcode. - if (identify_magic((*ObjectOrErr)->getBuffer()) == file_magic::bitcode) - ImageBinary.TheImageKind = object::IMG_Bitcode; - else - ImageBinary.TheImageKind = - getImageKind(sys::path::extension(Value).drop_front()); - ImageBinary.Image = std::move(*ObjectOrErr); - } else if (Key == "kind") { - ImageBinary.TheOffloadKind = getOffloadKind(Value); - } else { - ImageBinary.StringData[Key] = Value; + // Permit using multiple instances of `file` in a single string. + for (auto &File : llvm::split(Args["file"], ",")) { + OffloadBinary::OffloadingImage ImageBinary{}; + std::unique_ptr<llvm::MemoryBuffer> DeviceImage; + + llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ObjectOrErr = + llvm::MemoryBuffer::getFileOrSTDIN(File); + if (std::error_code EC = ObjectOrErr.getError()) + return errorCodeToError(EC); + + // Clang uses the '.o' suffix for LTO bitcode. + if (identify_magic((*ObjectOrErr)->getBuffer()) == file_magic::bitcode) + ImageBinary.TheImageKind = object::IMG_Bitcode; + else + ImageBinary.TheImageKind = + getImageKind(sys::path::extension(File).drop_front()); + ImageBinary.Image = std::move(*ObjectOrErr); + for (const auto &[Key, Value] : Args) { + if (Key == "kind") { + ImageBinary.TheOffloadKind = getOffloadKind(Value); + } else if (Key != "file") { + ImageBinary.StringData[Key] = Value; + } } + llvm::SmallString<0> Buffer = OffloadBinary::write(ImageBinary); + if (Buffer.size() % OffloadBinary::getAlignment() != 0) + return createStringError(inconvertibleErrorCode(), + "Offload binary has invalid size alignment"); + OS << Buffer; } - llvm::SmallString<0> Buffer = OffloadBinary::write(ImageBinary); - if (Buffer.size() % OffloadBinary::getAlignment() != 0) - return createStringError(inconvertibleErrorCode(), - "Offload binary has invalid size alignment"); - OS << Buffer; } if (Error E = writeFile(OutputFile, |