|
| 1 | +//===------- online_compiler.hpp - Online source compilation service ------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <CL/sycl/context.hpp> |
| 12 | +#include <CL/sycl/detail/defines_elementary.hpp> // for __SYCL_INLINE_NAMESPACE |
| 13 | +#include <CL/sycl/device.hpp> |
| 14 | + |
| 15 | +#include <memory> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 19 | +namespace sycl { |
| 20 | +namespace INTEL { |
| 21 | + |
| 22 | +using byte = unsigned char; |
| 23 | + |
| 24 | +enum class compiled_code_format { |
| 25 | + spir_v // the only format supported for now |
| 26 | +}; |
| 27 | + |
| 28 | +class device_arch { |
| 29 | +public: |
| 30 | + static constexpr int any = 0; |
| 31 | + |
| 32 | + device_arch(int Val) : Val(Val) {} |
| 33 | + |
| 34 | + enum gpu { |
| 35 | + gpu_any = 1, |
| 36 | + gpu_gen9, |
| 37 | + gpu_skl = gpu_gen9, |
| 38 | + gpu_gen9_5, |
| 39 | + gpu_kbl = gpu_gen9_5, |
| 40 | + gpu_cfl = gpu_gen9_5, |
| 41 | + gpu_gen11, |
| 42 | + gpu_icl = gpu_gen11, |
| 43 | + gpu_gen12 |
| 44 | + }; |
| 45 | + |
| 46 | + enum cpu { |
| 47 | + cpu_any = 1, |
| 48 | + }; |
| 49 | + |
| 50 | + enum fpga { |
| 51 | + fpga_any = 1, |
| 52 | + }; |
| 53 | + |
| 54 | + operator int() { return Val; } |
| 55 | + |
| 56 | +private: |
| 57 | + int Val; |
| 58 | +}; |
| 59 | + |
| 60 | +/// Represents an error happend during online compilation. |
| 61 | +class online_compile_error : public sycl::exception { |
| 62 | + // TBD |
| 63 | +}; |
| 64 | + |
| 65 | +/// Designates a source language for the online compiler. |
| 66 | +enum class source_language { opencl_c, cm }; |
| 67 | + |
| 68 | +/// Represents an online compiler for the language given as template |
| 69 | +/// parameter. |
| 70 | +template <source_language Lang> class online_compiler { |
| 71 | +public: |
| 72 | + /// Constructs online compiler which can target any device and produces |
| 73 | + /// given compiled code format. Produces device code is 64-bit. |
| 74 | + /// The created compiler is "optimistic" - it assumes all applicable SYCL |
| 75 | + /// device capabilities are supported by the target device(s). |
| 76 | + online_compiler(compiled_code_format fmt = compiled_code_format::spir_v) |
| 77 | + : OutputFormat(fmt), OutputFormatVersion({0, 0}), |
| 78 | + DeviceArch(device_arch::any), Is64Bit(true), DeviceStepping("") {} |
| 79 | + |
| 80 | + /// Constructs online compiler which targets given architecture and produces |
| 81 | + /// given compiled code format. Produces device code is 64-bit. |
| 82 | + /// Throws online_compile_error if values of constructor arguments are |
| 83 | + /// contradictory or not supported - e.g. if the source language is not |
| 84 | + /// supported for given device type. |
| 85 | + online_compiler(sycl::info::device_type dev_type, device_arch arch, |
| 86 | + compiled_code_format fmt = compiled_code_format::spir_v) |
| 87 | + : OutputFormat(fmt), OutputFormatVersion({0, 0}), DeviceArch(arch), |
| 88 | + Is64Bit(true), DeviceStepping("") {} |
| 89 | + |
| 90 | + /// Constructs online compiler for the target specified by given SYCL device. |
| 91 | + online_compiler(const sycl::device &dev); |
| 92 | + |
| 93 | + /// Compiles given in-memory \c Lang source to a binary blob. Blob format, |
| 94 | + /// other parameters are set in the constructor by the compilation target |
| 95 | + /// specification parameters. |
| 96 | + /// Specialization for each language will provide exact signatures, which |
| 97 | + /// can be different for different languages. |
| 98 | + /// Throws online_compile_error if compilation is not successful. |
| 99 | + template <typename... Tys> |
| 100 | + std::vector<byte> compile(const std::string &src, const Tys &... args); |
| 101 | + |
| 102 | + /// Sets the compiled code format of the compilation target and returns *this. |
| 103 | + online_compiler<Lang> &setOutputFormat(compiled_code_format fmt); |
| 104 | + |
| 105 | + /// Sets the compiled code format version of the compilation target and |
| 106 | + /// returns *this. |
| 107 | + online_compiler<Lang> &setOutputFormatVersion(int major, int minor); |
| 108 | + |
| 109 | + /// Sets the device type of the compilation target and returns *this. |
| 110 | + online_compiler<Lang> &setTargetDeviceType(sycl::info::device_type type); |
| 111 | + |
| 112 | + /// Sets the device architecture of the compilation target and returns *this. |
| 113 | + online_compiler<Lang> &setTargetDeviceArch(device_arch arch); |
| 114 | + |
| 115 | + /// Makes the compilation target 32-bit and returns *this. |
| 116 | + online_compiler<Lang> &set32bitTarget(); |
| 117 | + |
| 118 | + /// Makes the compilation target 64-bit and returns *this. |
| 119 | + online_compiler<Lang> &set64bitTarget(); |
| 120 | + |
| 121 | + /// Sets implementation-defined target device stepping of the compilation |
| 122 | + /// target and returns *this. |
| 123 | + online_compiler<Lang> &setTargetDeviceStepping(const std::string &id); |
| 124 | + |
| 125 | +private: |
| 126 | + // Compilation target specification fields: { |
| 127 | + |
| 128 | + /// Compiled code format. |
| 129 | + compiled_code_format OutputFormat; |
| 130 | + |
| 131 | + /// Compiled code format version - a pair of "major" and "minor" components |
| 132 | + std::pair<int, int> OutputFormatVersion; |
| 133 | + |
| 134 | + /// Target device type |
| 135 | + sycl::info::device_type DeviceType; |
| 136 | + |
| 137 | + /// Target device architecture |
| 138 | + device_arch DeviceArch; |
| 139 | + |
| 140 | + /// Whether the target device architecture is 64-bit |
| 141 | + bool Is64Bit; |
| 142 | + |
| 143 | + /// Target device stepping (implementation defined) |
| 144 | + std::string DeviceStepping; |
| 145 | + // } |
| 146 | +}; |
| 147 | + |
| 148 | +// Specializations of the online_compiler class and 'compile' function for |
| 149 | +// particular languages and parameter types. |
| 150 | + |
| 151 | +/// Compiles given OpenCL source. May throw \c online_compile_error. |
| 152 | +/// @param src - contents of the source |
| 153 | +template <> |
| 154 | +template <> |
| 155 | +std::vector<byte> |
| 156 | +online_compiler<source_language::opencl_c>::compile(const std::string &src) { |
| 157 | + // real implementation will call some non-templated impl function here |
| 158 | + return std::vector<byte>{}; |
| 159 | +} |
| 160 | + |
| 161 | +/// Compiles given OpenCL source. May throw \c online_compile_error. |
| 162 | +/// @param src - contents of the source |
| 163 | +/// @param options - compilation options (implementation defined); standard |
| 164 | +/// OpenCL JIT compiler options must be supported |
| 165 | +template <> |
| 166 | +template <> |
| 167 | +std::vector<byte> online_compiler<source_language::opencl_c>::compile( |
| 168 | + const std::string &src, const std::vector<std::string> &options) { |
| 169 | + // real implementation will call some non-templated impl function here |
| 170 | + return std::vector<byte>{}; |
| 171 | +} |
| 172 | + |
| 173 | +/// Compiles given CM source. |
| 174 | +template <> |
| 175 | +template <> |
| 176 | +std::vector<byte> |
| 177 | +online_compiler<source_language::cm>::compile(const std::string &src) { |
| 178 | + // real implementation will call some non-templated impl function here |
| 179 | + return std::vector<byte>{}; |
| 180 | +} |
| 181 | + |
| 182 | +/// Compiles given CM source. |
| 183 | +/// @param options - compilation options (implementation defined) |
| 184 | +template <> |
| 185 | +template <> |
| 186 | +std::vector<byte> online_compiler<source_language::cm>::compile( |
| 187 | + const std::string &src, const std::vector<std::string> &options) { |
| 188 | + // real implementation will call some non-templated impl function here |
| 189 | + return std::vector<byte>{}; |
| 190 | +} |
| 191 | + |
| 192 | +} // namespace INTEL |
| 193 | +} // namespace sycl |
| 194 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments