[Mesa-dev] [PATCH v11 15/20] clover/spirv: Add functions for validating SPIR-V binaries

Pierre Moreau pierre.morrow at free.fr
Mon Jan 21 19:36:52 UTC 2019


Changes since:
* v10:
  - Reuse format_validation_msg in is_valid_spirv.
  - Remove LVL2STR macro in format_validation_msg.
* v9: Add `clover_cpp_std` to the overrides of the `libclspirv` target
      in Meson.
* v7: Add DEFINES to libclspirv and libclover, in autotools, as they
      would otherwise never know whether CLOVER_ALLOW_SPIRV has been
      defined (Dave Airlie)
* v6: Update the dependency name (meson) and the libs variable
      (Makefile) due to the replacement of llvm-spirv to the new
      official SPIRV-LLVM-Translator.
* v5: Changed to match the updated “clover/llvm: Allow translating from
      SPIR-V to LLVM IR” in the v6.

Signed-off-by: Pierre Moreau <pierre.morrow at free.fr>
---
 src/gallium/state_trackers/clover/Makefile.am |  17 ++-
 .../state_trackers/clover/Makefile.sources    |   4 +
 src/gallium/state_trackers/clover/meson.build |  11 +-
 .../clover/spirv/invocation.cpp               | 131 ++++++++++++++++++
 .../clover/spirv/invocation.hpp               |  47 +++++++
 5 files changed, 207 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/state_trackers/clover/spirv/invocation.cpp
 create mode 100644 src/gallium/state_trackers/clover/spirv/invocation.hpp

diff --git a/src/gallium/state_trackers/clover/Makefile.am b/src/gallium/state_trackers/clover/Makefile.am
index 2f42011104f..9bc078609fd 100644
--- a/src/gallium/state_trackers/clover/Makefile.am
+++ b/src/gallium/state_trackers/clover/Makefile.am
@@ -28,7 +28,7 @@ cl_HEADERS = \
 	$(top_srcdir)/include/CL/opencl.h
 endif
 
-noinst_LTLIBRARIES = libclover.la libclllvm.la
+noinst_LTLIBRARIES = libclover.la libclllvm.la libclspirv.la
 
 libclllvm_la_CXXFLAGS = \
 	$(CXX11_CXXFLAGS) \
@@ -47,13 +47,26 @@ libclllvm_la_SOURCES = $(LLVM_SOURCES)
 libclllvm_la_LDFLAGS = \
 	$(LLVMSPIRVLIB_LIBS)
 
+libclspirv_la_CXXFLAGS = \
+	$(CXX11_CXXFLAGS) \
+	$(CLOVER_STD_OVERRIDE) \
+	$(DEFINES) \
+	$(VISIBILITY_CXXFLAGS) \
+	$(SPIRV_TOOLS_CFLAGS)
+
+libclspirv_la_SOURCES = $(SPIRV_SOURCES)
+
+libclspirv_la_LDFLAGS = \
+	$(SPIRV_TOOLS_LIBS)
+
 libclover_la_CXXFLAGS = \
 	$(CXX11_CXXFLAGS) \
 	$(CLOVER_STD_OVERRIDE) \
+	$(DEFINES) \
 	$(VISIBILITY_CXXFLAGS)
 
 libclover_la_LIBADD = \
-	libclllvm.la
+	libclllvm.la libclspirv.la
 
 libclover_la_SOURCES = $(CPP_SOURCES)
 
diff --git a/src/gallium/state_trackers/clover/Makefile.sources b/src/gallium/state_trackers/clover/Makefile.sources
index 5167ca75af4..38f94981fb6 100644
--- a/src/gallium/state_trackers/clover/Makefile.sources
+++ b/src/gallium/state_trackers/clover/Makefile.sources
@@ -62,3 +62,7 @@ LLVM_SOURCES := \
 	llvm/invocation.hpp \
 	llvm/metadata.hpp \
 	llvm/util.hpp
+
+SPIRV_SOURCES := \
+	spirv/invocation.cpp \
+	spirv/invocation.hpp
diff --git a/src/gallium/state_trackers/clover/meson.build b/src/gallium/state_trackers/clover/meson.build
index c87fb61c1ab..6773efd39d4 100644
--- a/src/gallium/state_trackers/clover/meson.build
+++ b/src/gallium/state_trackers/clover/meson.build
@@ -52,6 +52,15 @@ libclllvm = static_library(
   override_options : clover_cpp_std,
 )
 
+libclspirv = static_library(
+  'clspirv',
+  files('spirv/invocation.cpp', 'spirv/invocation.hpp'),
+  include_directories : clover_incs,
+  cpp_args : [cpp_vis_args],
+  dependencies : [dep_spirv_tools],
+  override_options : clover_cpp_std,
+)
+
 clover_files = files(
   'api/context.cpp',
   'api/device.cpp',
@@ -112,6 +121,6 @@ libclover = static_library(
   [clover_files, sha1_h],
   include_directories : clover_incs,
   cpp_args : [clover_cpp_args, cpp_vis_args],
-  link_with : [libclllvm],
+  link_with : [libclllvm, libclspirv],
   override_options : clover_cpp_std,
 )
diff --git a/src/gallium/state_trackers/clover/spirv/invocation.cpp b/src/gallium/state_trackers/clover/spirv/invocation.cpp
new file mode 100644
index 00000000000..d248986f2f3
--- /dev/null
+++ b/src/gallium/state_trackers/clover/spirv/invocation.cpp
@@ -0,0 +1,131 @@
+//
+// Copyright 2018 Pierre Moreau
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#include "invocation.hpp"
+
+#ifdef CLOVER_ALLOW_SPIRV
+#include <spirv-tools/libspirv.hpp>
+#endif
+
+#include "util/u_math.h"
+
+#include "compiler/spirv/spirv.h"
+
+using namespace clover;
+
+namespace {
+
+#ifdef CLOVER_ALLOW_SPIRV
+   std::string
+   format_validator_msg(spv_message_level_t level, const char * /* source */,
+                        const spv_position_t &position, const char *message) {
+      auto const level_to_string = [](spv_message_level_t level){
+         switch (level) {
+            case SPV_MSG_FATAL:
+               return std::string("Fatal");
+            case SPV_MSG_INTERNAL_ERROR:
+               return std::string("Internal error");
+            case SPV_MSG_ERROR:
+               return std::string("Error");
+            case SPV_MSG_WARNING:
+               return std::string("Warning");
+            case SPV_MSG_INFO:
+               return std::string("Info");
+            case SPV_MSG_DEBUG:
+               return std::string("Debug");
+         }
+         return std::string();
+      };
+      return "[" + level_to_string(level) + "] At word No." +
+             std::to_string(position.index) + ": \"" + message + "\"\n";
+   }
+
+   spv_target_env
+   convert_opencl_str_to_target_env(const std::string &opencl_version) {
+      if (opencl_version == "2.2") {
+         return SPV_ENV_OPENCL_2_2;
+      } else if (opencl_version == "2.1") {
+         return SPV_ENV_OPENCL_2_1;
+      } else if (opencl_version == "2.0") {
+         return SPV_ENV_OPENCL_2_0;
+      } else if (opencl_version == "1.2" ||
+                 opencl_version == "1.1" ||
+                 opencl_version == "1.0") {
+         // SPIR-V is only defined for OpenCL >= 1.2, however some drivers
+         // might use it with OpenCL 1.0 and 1.1.
+         return SPV_ENV_OPENCL_1_2;
+      } else {
+         throw build_error("Invalid OpenCL version");
+      }
+   }
+#endif
+
+}
+
+bool
+clover::spirv::is_binary_spirv(const char *il, size_t length)
+{
+   const uint32_t *binary = reinterpret_cast<const uint32_t*>(il);
+
+   // A SPIR-V binary is at the very least 5 32-bit words, which represent the
+   // SPIR-V header.
+   if (length < 20u)
+      return false;
+
+   const uint32_t first_word = binary[0u];
+   return (first_word == SpvMagicNumber) ||
+          (util_bswap32(first_word) == SpvMagicNumber);
+}
+
+#ifdef CLOVER_ALLOW_SPIRV
+bool
+clover::spirv::is_valid_spirv(const uint32_t *binary, size_t length,
+                              const std::string &opencl_version,
+                              const context::notify_action &notify) {
+   auto const validator_consumer =
+      [&notify,format_validator_msg](spv_message_level_t level,
+                                     const char *source,
+                                     const spv_position_t &position,
+                                     const char *message) {
+      if (!notify)
+         return;
+
+      const std::string log = format_validator_msg(level, source, position,
+                                                   message);
+      notify(log.c_str());
+   };
+
+   const spv_target_env target_env =
+      convert_opencl_str_to_target_env(opencl_version);
+   spvtools::SpirvTools spvTool(target_env);
+   spvTool.SetMessageConsumer(validator_consumer);
+
+   return spvTool.Validate(binary, length);
+}
+#else
+bool
+clover::spirv::is_valid_spirv(const uint32_t * /*binary*/, size_t /*length*/,
+                              const std::string &/*opencl_version*/,
+                              const context::notify_action &/*notify*/) {
+   return false;
+}
+#endif
diff --git a/src/gallium/state_trackers/clover/spirv/invocation.hpp b/src/gallium/state_trackers/clover/spirv/invocation.hpp
new file mode 100644
index 00000000000..92255a68a8e
--- /dev/null
+++ b/src/gallium/state_trackers/clover/spirv/invocation.hpp
@@ -0,0 +1,47 @@
+//
+// Copyright 2018 Pierre Moreau
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+// OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#ifndef CLOVER_SPIRV_INVOCATION_HPP
+#define CLOVER_SPIRV_INVOCATION_HPP
+
+#include "core/context.hpp"
+
+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 char *binary, size_t length);
+
+      // Returns whether the given binary is considered valid for the given
+      // OpenCL version.
+      //
+      // It uses SPIRV-Tools validator to do the validation, and potential
+      // warnings and errors are forwarded to the application via |notify|.
+      bool is_valid_spirv(const uint32_t *binary, size_t length,
+                          const std::string &opencl_version,
+                          const context::notify_action &notify);
+   }
+}
+
+#endif
-- 
2.20.1



More information about the mesa-dev mailing list