[llvm] 24ebdb6 - [CUDA] Add CUDA fatbinary magic

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 14 17:09:07 PDT 2022


Author: Joseph Huber
Date: 2022-03-14T20:08:31-04:00
New Revision: 24ebdb6c255e0168e4518cc7f0c465b53867abf8

URL: https://github.com/llvm/llvm-project/commit/24ebdb6c255e0168e4518cc7f0c465b53867abf8
DIFF: https://github.com/llvm/llvm-project/commit/24ebdb6c255e0168e4518cc7f0c465b53867abf8.diff

LOG: [CUDA] Add CUDA fatbinary magic

Nvidia uses fatbinaries to bundle all of their device code. This patch
adds the magic number "0x50ed55ba" used in their propeitary format to
the list of magic identifies. This is technically undocumented and could
unlikely be changed by Nvidia in the future.

Reviewed By: tra

Differential Revision: https://reviews.llvm.org/D120932

Added: 
    

Modified: 
    llvm/include/llvm/BinaryFormat/Magic.h
    llvm/lib/BinaryFormat/Magic.cpp
    llvm/lib/Object/Binary.cpp
    llvm/lib/Object/ObjectFile.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/BinaryFormat/Magic.h b/llvm/include/llvm/BinaryFormat/Magic.h
index 6988b2dde656a..e91f0fdcc8cd8 100644
--- a/llvm/include/llvm/BinaryFormat/Magic.h
+++ b/llvm/include/llvm/BinaryFormat/Magic.h
@@ -51,6 +51,7 @@ struct file_magic {
     wasm_object,         ///< WebAssembly Object file
     pdb,                 ///< Windows PDB debug info file
     tapi_file,           ///< Text-based Dynamic Library Stub file
+    cuda_fatbinary,      ///< CUDA Fatbinary object file
   };
 
   bool is_object() const { return V != unknown; }

diff  --git a/llvm/lib/BinaryFormat/Magic.cpp b/llvm/lib/BinaryFormat/Magic.cpp
index 044e4840cb3b0..5d999a907fd2b 100644
--- a/llvm/lib/BinaryFormat/Magic.cpp
+++ b/llvm/lib/BinaryFormat/Magic.cpp
@@ -185,6 +185,10 @@ file_magic llvm::identify_magic(StringRef Magic) {
   case 0x84: // Alpha 64-bit
   case 0x66: // MPS R4000 Windows
   case 0x50: // mc68K
+    if (startswith(Magic, "\x50\xed\x55\xba"))
+      return file_magic::cuda_fatbinary;
+    LLVM_FALLTHROUGH;
+
   case 0x4c: // 80386 Windows
   case 0xc4: // ARMNT Windows
     if (Magic[1] == 0x01)

diff  --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 6964cf2003668..67ed44a1cf143 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -82,6 +82,7 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
     // PDB does not support the Binary interface.
     return errorCodeToError(object_error::invalid_file_type);
   case file_magic::unknown:
+  case file_magic::cuda_fatbinary:
   case file_magic::coff_cl_gl_object:
     // Unrecognized object file format.
     return errorCodeToError(object_error::invalid_file_type);

diff  --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index fedf737251ace..2e3c2ed34df8f 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -146,6 +146,7 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
   case file_magic::pdb:
   case file_magic::minidump:
   case file_magic::goff_object:
+  case file_magic::cuda_fatbinary:
     return errorCodeToError(object_error::invalid_file_type);
   case file_magic::tapi_file:
     return errorCodeToError(object_error::invalid_file_type);


        


More information about the llvm-commits mailing list