1 //
2 // Copyright 2018 Pierre Moreau
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 
23 #ifndef CLOVER_SPIRV_INVOCATION_HPP
24 #define CLOVER_SPIRV_INVOCATION_HPP
25 
26 #include <unordered_set>
27 
28 #include "core/context.hpp"
29 #include "core/binary.hpp"
30 #include "core/program.hpp"
31 
32 namespace clover {
33    namespace spirv {
34       // Returns whether the binary starts with the SPIR-V magic word.
35       //
36       // The first word is interpreted as little endian and big endian, but
37       // only one of them has to match.
38       bool is_binary_spirv(const std::string &binary);
39 
40       // Returns whether the given binary is considered valid for the given
41       // OpenCL version.
42       //
43       // It uses SPIRV-Tools validator to do the validation, and potential
44       // warnings and errors are appended to |r_log|.
45       bool is_valid_spirv(const std::string &binary,
46                           const cl_version opencl_version,
47                           std::string &r_log);
48 
49       // Converts an integer SPIR-V version into its textual representation.
50       std::string version_to_string(uint32_t version);
51 
52       // Creates a clover binary out of the given SPIR-V binary.
53       binary compile_program(const std::string &binary,
54                              const device &dev, std::string &r_log,
55                              bool validate = true);
56 
57       // Combines multiple clover objects into a single one, resolving
58       // link dependencies between them.
59       binary link_program(const std::vector<binary> &objects, const device &dev,
60                           const std::string &opts, std::string &r_log);
61 
62       // Returns a textual representation of the given binary.
63       std::string print_module(const std::string &binary,
64                                const cl_version opencl_version);
65 
66       // Returns a set of supported SPIR-V extensions.
67       std::unordered_set<std::string> supported_extensions();
68 
69       // Returns a vector (sorted in increasing order) of supported SPIR-V
70       // versions.
71       std::vector<cl_name_version> supported_versions();
72 
73       // Converts a version number from SPIR-V's encoding to OpenCL's one.
74       cl_version to_opencl_version_encoding(uint32_t version);
75 
76       // Converts a version number from OpenCL's encoding to SPIR-V's one.
77       uint32_t to_spirv_version_encoding(cl_version version);
78    }
79 }
80 
81 #endif
82