Changes since: * 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.mor...@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 | 138 ++++++++++++++++++ .../clover/spirv/invocation.hpp | 47 ++++++ 5 files changed, 214 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 096c33f52e1..ffe858b1c2a 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..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 ¬ify) { + auto const validator_consumer = [¬ify](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 ¬ify); + } +} + +#endif -- 2.20.1 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev