Skip to content

[LLVM] Add file magic detection for SPIR-V files. #75363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/Magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct file_magic {
dxcontainer_object, ///< DirectX container file
offload_bundle, ///< Clang offload bundle file
offload_bundle_compressed, ///< Compressed clang offload bundle file
spirv_object, ///< A binary SPIR-V file
};

bool is_object() const { return V != unknown; }
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/BinaryFormat/Magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ file_magic llvm::identify_magic(StringRef Magic) {
case 0x03:
if (startswith(Magic, "\x03\xF0\x00"))
return file_magic::goff_object;
// SPIR-V format in little-endian mode.
if (startswith(Magic, "\x03\x02\x23\x07"))
return file_magic::spirv_object;
break;

case 0x07: // SPIR-V format in big-endian mode.
if (startswith(Magic, "\x07\x23\x02\x03"))
return file_magic::spirv_object;
break;

case 0x10:
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Object/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
case file_magic::dxcontainer_object:
case file_magic::offload_bundle:
case file_magic::offload_bundle_compressed:
case file_magic::spirv_object:
// Unrecognized object file format.
return errorCodeToError(object_error::invalid_file_type);
case file_magic::offload_binary:
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Object/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
case file_magic::dxcontainer_object:
case file_magic::offload_bundle:
case file_magic::offload_bundle_compressed:
case file_magic::spirv_object:
return errorCodeToError(object_error::invalid_file_type);
case file_magic::tapi_file:
return errorCodeToError(object_error::invalid_file_type);
Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/BinaryFormat/TestFileMagic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ const char pdb[] = "Microsoft C/C++ MSF 7.00\r\n\x1a"
"DS\x00\x00\x00";
const char tapi_file[] = "--- !tapi-tbd-v1\n";
const char tapi_file_tbd_v1[] = "---\narchs: [";
const char spirv_object_le[] = "\x03\x02\x23\x07";
const char spirv_object_be[] = "\x07\x23\x02\x03";

TEST_F(MagicTest, Magic) {
struct type {
Expand Down Expand Up @@ -117,6 +119,10 @@ TEST_F(MagicTest, Magic) {
DEFINE(macho_dynamically_linked_shared_lib_stub),
DEFINE(macho_dsym_companion),
DEFINE(macho_kext_bundle),
{"spirv_object_le", spirv_object_le, sizeof(spirv_object_le),
file_magic ::spirv_object},
{"spirv_object_be", spirv_object_be, sizeof(spirv_object_be),
file_magic ::spirv_object},
DEFINE(windows_resource),
DEFINE(pdb),
{"ms_dos_stub_broken", ms_dos_stub_broken, sizeof(ms_dos_stub_broken),
Expand Down