Skip to content

Commit edc8388

Browse files
authored
[LLVM] Add file magic detection for SPIR-V files. (llvm#75363)
Summary: More SPIR-V related patches are being upstreamed. We should add support to detect when a binary file is SPIR-V. This will be used in the future when support for SPIR-V is added to the offloading runtime or more support for bundling. The magic number is described in the official documentation: https://registry.khronos.org/SPIR-V/specs/1.0/SPIRV.html#Magic. Notably, SPIR-V files are streams of 32-bit words. This means that the magic numbers differ depending on the endianness. Here we simply check the strandard and byte-reversed versions.
1 parent 1f3d13c commit edc8388

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

llvm/include/llvm/BinaryFormat/Magic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct file_magic {
5757
dxcontainer_object, ///< DirectX container file
5858
offload_bundle, ///< Clang offload bundle file
5959
offload_bundle_compressed, ///< Compressed clang offload bundle file
60+
spirv_object, ///< A binary SPIR-V file
6061
};
6162

6263
bool is_object() const { return V != unknown; }

llvm/lib/BinaryFormat/Magic.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ file_magic llvm::identify_magic(StringRef Magic) {
7272
case 0x03:
7373
if (startswith(Magic, "\x03\xF0\x00"))
7474
return file_magic::goff_object;
75+
// SPIR-V format in little-endian mode.
76+
if (startswith(Magic, "\x03\x02\x23\x07"))
77+
return file_magic::spirv_object;
78+
break;
79+
80+
case 0x07: // SPIR-V format in big-endian mode.
81+
if (startswith(Magic, "\x07\x23\x02\x03"))
82+
return file_magic::spirv_object;
7583
break;
7684

7785
case 0x10:

llvm/lib/Object/Binary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
8989
case file_magic::dxcontainer_object:
9090
case file_magic::offload_bundle:
9191
case file_magic::offload_bundle_compressed:
92+
case file_magic::spirv_object:
9293
// Unrecognized object file format.
9394
return errorCodeToError(object_error::invalid_file_type);
9495
case file_magic::offload_binary:

llvm/lib/Object/ObjectFile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
160160
case file_magic::dxcontainer_object:
161161
case file_magic::offload_bundle:
162162
case file_magic::offload_bundle_compressed:
163+
case file_magic::spirv_object:
163164
return errorCodeToError(object_error::invalid_file_type);
164165
case file_magic::tapi_file:
165166
return errorCodeToError(object_error::invalid_file_type);

llvm/unittests/BinaryFormat/TestFileMagic.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ const char pdb[] = "Microsoft C/C++ MSF 7.00\r\n\x1a"
8787
"DS\x00\x00\x00";
8888
const char tapi_file[] = "--- !tapi-tbd-v1\n";
8989
const char tapi_file_tbd_v1[] = "---\narchs: [";
90+
const char spirv_object_le[] = "\x03\x02\x23\x07";
91+
const char spirv_object_be[] = "\x07\x23\x02\x03";
9092

9193
TEST_F(MagicTest, Magic) {
9294
struct type {
@@ -117,6 +119,10 @@ TEST_F(MagicTest, Magic) {
117119
DEFINE(macho_dynamically_linked_shared_lib_stub),
118120
DEFINE(macho_dsym_companion),
119121
DEFINE(macho_kext_bundle),
122+
{"spirv_object_le", spirv_object_le, sizeof(spirv_object_le),
123+
file_magic ::spirv_object},
124+
{"spirv_object_be", spirv_object_be, sizeof(spirv_object_be),
125+
file_magic ::spirv_object},
120126
DEFINE(windows_resource),
121127
DEFINE(pdb),
122128
{"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken),

0 commit comments

Comments
 (0)