[llvm] [LLVM] Add file magic detection for SPIR-V files. (PR #75363)
Joseph Huber via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 13 09:41:38 PST 2023
https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/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.
>From 2700151916b0fd91c793930127412af5690c9e41 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 13 Dec 2023 11:35:13 -0600
Subject: [PATCH] [LLVM] Add file magic detection for SPIR-V files.
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.
---
llvm/include/llvm/BinaryFormat/Magic.h | 1 +
llvm/lib/BinaryFormat/Magic.cpp | 9 +++++++++
llvm/lib/Object/Binary.cpp | 1 +
llvm/lib/Object/ObjectFile.cpp | 1 +
4 files changed, 12 insertions(+)
diff --git a/llvm/include/llvm/BinaryFormat/Magic.h b/llvm/include/llvm/BinaryFormat/Magic.h
index a28710dcdfaf2c..c635a269576587 100644
--- a/llvm/include/llvm/BinaryFormat/Magic.h
+++ b/llvm/include/llvm/BinaryFormat/Magic.h
@@ -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; }
diff --git a/llvm/lib/BinaryFormat/Magic.cpp b/llvm/lib/BinaryFormat/Magic.cpp
index 255937a5bdd04a..b0f0043f0e492a 100644
--- a/llvm/lib/BinaryFormat/Magic.cpp
+++ b/llvm/lib/BinaryFormat/Magic.cpp
@@ -72,6 +72,15 @@ 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:
diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp
index 0ee9f7fac448a2..0b9d95485287dc 100644
--- a/llvm/lib/Object/Binary.cpp
+++ b/llvm/lib/Object/Binary.cpp
@@ -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:
diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index 428166f58070d0..ca921836b7f65a 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -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);
More information about the llvm-commits
mailing list