From edc83886d479e110c87d104e7241ce67ee1b6316 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Wed, 13 Dec 2023 20:39:50 -0600 Subject: [LLVM] Add file magic detection for SPIR-V files. (#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. --- llvm/lib/Object/Binary.cpp | 1 + llvm/lib/Object/ObjectFile.cpp | 1 + 2 files changed, 2 insertions(+) (limited to 'llvm/lib/Object') diff --git a/llvm/lib/Object/Binary.cpp b/llvm/lib/Object/Binary.cpp index 0ee9f7f..0b9d954 100644 --- a/llvm/lib/Object/Binary.cpp +++ b/llvm/lib/Object/Binary.cpp @@ -89,6 +89,7 @@ Expected> 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 428166f..ca92183 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); -- cgit v1.1