clover/spirv: Add functions for validating SPIR-V binaries

Changes since:
* v12:
  - remove autotools (Karol Herbst)
  - Remove the callback in format_validation_msg. (Francisco Jerez)
  - Removed is_binary_spirv. (Francisco Jerez)
  - Pass a string reference to is_valid_spirv instead of the
    notification callback. (Francisco Jerez)
* v11: Fix compilation error introduced in v11.
* 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.

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Francisco Jerez <currojerez@riseup.net>
This commit is contained in:
Pierre Moreau 2018-02-10 21:40:10 +01:00 committed by Karol Herbst
parent 866f6f11d9
commit 939a7e9a5c
4 changed files with 168 additions and 1 deletions

View File

@ -62,3 +62,7 @@ LLVM_SOURCES := \
llvm/invocation.hpp \
llvm/metadata.hpp \
llvm/util.hpp
SPIRV_SOURCES := \
spirv/invocation.cpp \
spirv/invocation.hpp

View File

@ -57,6 +57,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 : [clover_spirv_cpp_args, cpp_vis_args],
dependencies : [dep_spirv_tools],
override_options : clover_cpp_std,
)
clover_files = files(
'api/context.cpp',
'api/device.cpp',
@ -117,6 +126,6 @@ libclover = static_library(
[clover_files, sha1_h],
include_directories : clover_incs,
cpp_args : [clover_spirv_cpp_args, clover_cpp_args, cpp_vis_args],
link_with : [libclllvm],
link_with : [libclllvm, libclspirv],
override_options : clover_cpp_std,
)

View File

@ -0,0 +1,113 @@
//
// 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 HAVE_CLOVER_SPIRV
#include <spirv-tools/libspirv.hpp>
#endif
#include "util/u_math.h"
#include "compiler/spirv/spirv.h"
using namespace clover;
namespace {
#ifdef HAVE_CLOVER_SPIRV
std::string
format_validator_msg(spv_message_level_t level, const char * /* source */,
const spv_position_t &position, const char *message) {
std::string level_str;
switch (level) {
case SPV_MSG_FATAL:
level_str = "Fatal";
break;
case SPV_MSG_INTERNAL_ERROR:
level_str = "Internal error";
break;
case SPV_MSG_ERROR:
level_str = "Error";
break;
case SPV_MSG_WARNING:
level_str = "Warning";
break;
case SPV_MSG_INFO:
level_str = "Info";
break;
case SPV_MSG_DEBUG:
level_str = "Debug";
break;
}
return "[" + level_str + "] 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
}
#ifdef HAVE_CLOVER_SPIRV
bool
clover::spirv::is_valid_spirv(const uint32_t *binary, size_t length,
const std::string &opencl_version,
std::string &r_log) {
auto const validator_consumer =
[&r_log](spv_message_level_t level, const char *source,
const spv_position_t &position, const char *message) {
r_log += format_validator_msg(level, source, position, message);
};
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*/,
std::string &/*r_log*/) {
return false;
}
#endif

View File

@ -0,0 +1,41 @@
//
// 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 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 appended to |r_log|.
bool is_valid_spirv(const uint32_t *binary, size_t length,
const std::string &opencl_version,
std::string &r_log);
}
}
#endif