From fa9e297b8b63dacb962d99814e698658ad71f946 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Wed, 14 Feb 2024 22:09:48 -0600 Subject: [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. --- .../ClangOffloadPackager.cpp | 53 ++++++++++++---------- 1 file 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 DeviceImage; - for (const auto &[Key, Value] : Args) { - if (Key == "file") { - llvm::ErrorOr> 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 DeviceImage; + + llvm::ErrorOr> 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, -- cgit v1.1