summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Moreau <dev@pmoreau.org>2020-05-05 13:16:36 +0200
committerPierre Moreau <dev@pmoreau.org>2021-01-01 21:56:29 +0100
commita1db84be7a6960507f812bb70f1cbf5ceeaf1c8a (patch)
treedcb96ce5ce67f6cc7b0ef5c0b93200fd78391697
parent47feba98f3833801ec2e8fdd018081897af6c510 (diff)
clover/spirv: Add function checking whether a binary contains SPIR-V
v2: Change API to take a std::string Reviewed-by: Karol Herbst <kherbst@redhat.com> Reviewed-by: Francisco Jerez <currojerez@riseup.net> Signed-off-by: Pierre Moreau <dev@pmoreau.org> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2078>
-rw-r--r--src/gallium/frontends/clover/spirv/invocation.cpp20
-rw-r--r--src/gallium/frontends/clover/spirv/invocation.hpp6
2 files changed, 26 insertions, 0 deletions
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,
@@ -851,6 +865,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,
std::string &/*r_log*/) {
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.
//