[Mesa-dev] [PATCH v7 16/21] clover/spirv: Add functions for validating SPIR-V binaries

Pierre Moreau pierre.morrow at free.fr
Wed May 23 21:44:35 UTC 2018


Signed-off-by: Pierre Moreau <pierre.morrow at free.fr>
---
Changes in:
- v7: Update the dependency name (meson) and the libs variable (Makefile) due
      to the replacement of llvm-spirv to the new official SPIRV-LLVM-Translator

 src/gallium/state_trackers/clover/Makefile.am |  15 +-
 .../state_trackers/clover/Makefile.sources    |   4 +
 src/gallium/state_trackers/clover/meson.build |  10 +-
 .../clover/spirv/invocation.cpp               | 138 ++++++++++++++++++
 .../clover/spirv/invocation.hpp               |  47 ++++++
 5 files changed, 211 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..c78d83b57ec 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,24 @@ libclllvm_la_SOURCES = $(LLVM_SOURCES)
 libclllvm_la_LDFLAGS = \
 	$(LLVMSPIRVLIB_LIBS)
 
+libclspirv_la_CXXFLAGS = \
+	$(CXX11_CXXFLAGS) \
+	$(CLOVER_STD_OVERRIDE) \
+	$(VISIBILITY_CXXFLAGS) \
+	$(SPIRV_TOOLS_CFLAGS)
+
+libclspirv_la_SOURCES = $(SPIRV_SOURCES)
+
+libclspirv_la_LDFLAGS = \
+	$(SPIRV_TOOLS_LIBS)
+
 libclover_la_CXXFLAGS = \
 	$(CXX11_CXXFLAGS) \
 	$(CLOVER_STD_OVERRIDE) \
 	$(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 ff9ed044f3c..230553e8001 100644
--- a/src/gallium/state_trackers/clover/meson.build
+++ b/src/gallium/state_trackers/clover/meson.build
@@ -51,6 +51,14 @@ libclllvm = static_library(
   dependencies : [dep_llvm, dep_elf, dep_llvmspirvlib],
 )
 
+libclspirv = static_library(
+  'clspirv',
+  files('spirv/invocation.cpp', 'spirv/invocation.hpp'),
+  include_directories : clover_incs,
+  cpp_args : [cpp_vis_args],
+  dependencies : [dep_spirv_tools],
+)
+
 clover_files = files(
   'api/context.cpp',
   'api/device.cpp',
@@ -111,5 +119,5 @@ libclover = static_library(
   clover_files,
   include_directories : clover_incs,
   cpp_args : [clover_cpp_args, cpp_vis_args],
-  link_with : [libclllvm],
+  link_with : [libclllvm, libclspirv],
 )
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..93cc49931d5
--- /dev/null
+++ b/src/gallium/state_trackers/clover/spirv/invocation.cpp
@@ -0,0 +1,138 @@
+//
+// 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 spv_position_t &position, const char *message) {
+      auto const level_to_string = [](spv_message_level_t level){
+#define LVL2STR(lvl) case SPV_MSG_##lvl: return std::string(#lvl)
+         switch (level) {
+            LVL2STR(FATAL);
+            LVL2STR(INTERNAL_ERROR);
+            LVL2STR(ERROR);
+            LVL2STR(WARNING);
+            LVL2STR(INFO);
+            LVL2STR(DEBUG);
+         }
+#undef LVL2STR
+         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](spv_message_level_t level,
+                                             const char * /* source */,
+                                             const spv_position_t &position,
+                                             const char *message) {
+      if (!notify)
+         return;
+
+      std::string str_level;
+      switch (level) {
+#define LVL2STR(lvl) case SPV_MSG_##lvl: str_level = std::string(#lvl)
+         LVL2STR(FATAL);
+         LVL2STR(INTERNAL_ERROR);
+         LVL2STR(ERROR);
+         LVL2STR(WARNING);
+         LVL2STR(INFO);
+         LVL2STR(DEBUG);
+#undef LVL2STR
+      }
+      const std::string log = "[" + str_level + "] At word No." +
+                              std::to_string(position.index) + ": \"" +
+                              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.17.0



More information about the mesa-dev mailing list