17ec681f3Smrg//
27ec681f3Smrg// Copyright 2018 Pierre Moreau
37ec681f3Smrg//
47ec681f3Smrg// Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg// copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg// to deal in the Software without restriction, including without limitation
77ec681f3Smrg// the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg// and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg// Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg//
117ec681f3Smrg// The above copyright notice and this permission notice shall be included in
127ec681f3Smrg// all copies or substantial portions of the Software.
137ec681f3Smrg//
147ec681f3Smrg// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
157ec681f3Smrg// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
167ec681f3Smrg// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
177ec681f3Smrg// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
187ec681f3Smrg// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
197ec681f3Smrg// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
207ec681f3Smrg// OTHER DEALINGS IN THE SOFTWARE.
217ec681f3Smrg//
227ec681f3Smrg
237ec681f3Smrg#ifndef CLOVER_SPIRV_INVOCATION_HPP
247ec681f3Smrg#define CLOVER_SPIRV_INVOCATION_HPP
257ec681f3Smrg
267ec681f3Smrg#include <unordered_set>
277ec681f3Smrg
287ec681f3Smrg#include "core/context.hpp"
297ec681f3Smrg#include "core/binary.hpp"
307ec681f3Smrg#include "core/program.hpp"
317ec681f3Smrg
327ec681f3Smrgnamespace clover {
337ec681f3Smrg   namespace spirv {
347ec681f3Smrg      // Returns whether the binary starts with the SPIR-V magic word.
357ec681f3Smrg      //
367ec681f3Smrg      // The first word is interpreted as little endian and big endian, but
377ec681f3Smrg      // only one of them has to match.
387ec681f3Smrg      bool is_binary_spirv(const std::string &binary);
397ec681f3Smrg
407ec681f3Smrg      // Returns whether the given binary is considered valid for the given
417ec681f3Smrg      // OpenCL version.
427ec681f3Smrg      //
437ec681f3Smrg      // It uses SPIRV-Tools validator to do the validation, and potential
447ec681f3Smrg      // warnings and errors are appended to |r_log|.
457ec681f3Smrg      bool is_valid_spirv(const std::string &binary,
467ec681f3Smrg                          const cl_version opencl_version,
477ec681f3Smrg                          std::string &r_log);
487ec681f3Smrg
497ec681f3Smrg      // Converts an integer SPIR-V version into its textual representation.
507ec681f3Smrg      std::string version_to_string(uint32_t version);
517ec681f3Smrg
527ec681f3Smrg      // Creates a clover binary out of the given SPIR-V binary.
537ec681f3Smrg      binary compile_program(const std::string &binary,
547ec681f3Smrg                             const device &dev, std::string &r_log,
557ec681f3Smrg                             bool validate = true);
567ec681f3Smrg
577ec681f3Smrg      // Combines multiple clover objects into a single one, resolving
587ec681f3Smrg      // link dependencies between them.
597ec681f3Smrg      binary link_program(const std::vector<binary> &objects, const device &dev,
607ec681f3Smrg                          const std::string &opts, std::string &r_log);
617ec681f3Smrg
627ec681f3Smrg      // Returns a textual representation of the given binary.
637ec681f3Smrg      std::string print_module(const std::string &binary,
647ec681f3Smrg                               const cl_version opencl_version);
657ec681f3Smrg
667ec681f3Smrg      // Returns a set of supported SPIR-V extensions.
677ec681f3Smrg      std::unordered_set<std::string> supported_extensions();
687ec681f3Smrg
697ec681f3Smrg      // Returns a vector (sorted in increasing order) of supported SPIR-V
707ec681f3Smrg      // versions.
717ec681f3Smrg      std::vector<cl_name_version> supported_versions();
727ec681f3Smrg
737ec681f3Smrg      // Converts a version number from SPIR-V's encoding to OpenCL's one.
747ec681f3Smrg      cl_version to_opencl_version_encoding(uint32_t version);
757ec681f3Smrg
767ec681f3Smrg      // Converts a version number from OpenCL's encoding to SPIR-V's one.
777ec681f3Smrg      uint32_t to_spirv_version_encoding(cl_version version);
787ec681f3Smrg   }
797ec681f3Smrg}
807ec681f3Smrg
817ec681f3Smrg#endif
82