Mesa (master): clover/spirv: Add function checking whether a binary contains SPIR-V

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jan 1 22:28:33 UTC 2021


Module: Mesa
Branch: master
Commit: a1db84be7a6960507f812bb70f1cbf5ceeaf1c8a
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=a1db84be7a6960507f812bb70f1cbf5ceeaf1c8a

Author: Pierre Moreau <dev at pmoreau.org>
Date:   Tue May  5 13:16:36 2020 +0200

clover/spirv: Add function checking whether a binary contains SPIR-V

v2: Change API to take a std::string

Reviewed-by: Karol Herbst <kherbst at redhat.com>
Reviewed-by: Francisco Jerez <currojerez at riseup.net>
Signed-off-by: Pierre Moreau <dev at pmoreau.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2078>

---

 src/gallium/frontends/clover/spirv/invocation.cpp | 20 ++++++++++++++++++++
 src/gallium/frontends/clover/spirv/invocation.hpp |  6 ++++++
 2 files changed, 26 insertions(+)

diff --git a/src/gallium/frontends/clover/spirv/invocation.cpp b/src/gallium/frontends/clover/spirv/invocation.cpp
index 38b1c6398b0..dd698829a16 100644
--- a/src/gallium/frontends/clover/spirv/invocation.cpp
+++ b/src/gallium/frontends/clover/spirv/invocation.cpp
@@ -676,6 +676,20 @@ namespace {
 
 }
 
+bool
+clover::spirv::is_binary_spirv(const std::string &binary)
+{
+   // A SPIR-V binary is at the very least 5 32-bit words, which represent the
+   // SPIR-V header.
+   if (binary.size() < 20u)
+      return false;
+
+   const uint32_t first_word =
+      reinterpret_cast<const uint32_t *>(binary.data())[0u];
+   return (first_word == SpvMagicNumber) ||
+          (util_bswap32(first_word) == SpvMagicNumber);
+}
+
 module
 clover::spirv::compile_program(const std::vector<char> &binary,
                                const device &dev, std::string &r_log,
@@ -850,6 +864,12 @@ clover::spirv::to_spirv_version_encoding(cl_version version) {
 }
 
 #else
+bool
+clover::spirv::is_binary_spirv(const std::string &binary)
+{
+   return false;
+}
+
 bool
 clover::spirv::is_valid_spirv(const std::vector<char> &/*binary*/,
                               const cl_version opencl_version,
diff --git a/src/gallium/frontends/clover/spirv/invocation.hpp b/src/gallium/frontends/clover/spirv/invocation.hpp
index 559260627c1..8d81a08143b 100644
--- a/src/gallium/frontends/clover/spirv/invocation.hpp
+++ b/src/gallium/frontends/clover/spirv/invocation.hpp
@@ -31,6 +31,12 @@
 
 namespace clover {
    namespace spirv {
+      // Returns whether the binary starts with the SPIR-V magic word.
+      //
+      // The first word is interpreted as little endian and big endian, but
+      // only one of them has to match.
+      bool is_binary_spirv(const std::string &binary);
+
       // Returns whether the given binary is considered valid for the given
       // OpenCL version.
       //



More information about the mesa-commit mailing list