[OffloadPackager] Use appropriate kind for LTO bitcode
authorJoseph Huber <jhuber6@vols.utk.edu>
Mon, 4 Jul 2022 21:32:47 +0000 (17:32 -0400)
committerJoseph Huber <jhuber6@vols.utk.edu>
Mon, 4 Jul 2022 21:34:14 +0000 (17:34 -0400)
Summary:
Currently we just check the extension to set the image kind. This
incorrectly labels the `.o` files created during LTO as object files.
This patch simply adds a check for the bitcode magic bytes instead.

clang/tools/clang-offload-packager/CMakeLists.txt
clang/tools/clang-offload-packager/ClangOffloadPackager.cpp

index a781825..accc948 100644 (file)
@@ -1,5 +1,6 @@
 set(LLVM_LINK_COMPONENTS 
   ${LLVM_TARGETS_TO_BUILD}
+  BinaryFormat
   Object
   Support)
 
index 338b63a..8e98fab 100644 (file)
@@ -99,9 +99,14 @@ int main(int argc, const char **argv) {
             llvm::MemoryBuffer::getFileOrSTDIN(KeyAndValue.getValue());
         if (std::error_code EC = ObjectOrErr.getError())
           return reportError(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(KeyAndValue.getValue()).drop_front());
         ImageBinary.Image = std::move(*ObjectOrErr);
-        ImageBinary.TheImageKind = getImageKind(
-            sys::path::extension(KeyAndValue.getValue()).drop_front());
       } else if (Key == "kind") {
         ImageBinary.TheOffloadKind = getOffloadKind(KeyAndValue.getValue());
       } else {