|
| 1 | +//==------- Kernel.h - Representation of a SYCL kernel for JIT compiler ----==// |
| 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 | +#ifndef SYCL_FUSION_COMMON_KERNEL_H |
| 10 | +#define SYCL_FUSION_COMMON_KERNEL_H |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <string> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +namespace jit_compiler { |
| 17 | + |
| 18 | +using BinaryAddress = const unsigned char *; |
| 19 | + |
| 20 | +/// |
| 21 | +/// Enumerate possible kinds of parameters. |
| 22 | +/// 1:1 correspondence with the definition in kernel_desc.hpp in the DPC++ SYCL |
| 23 | +/// runtime. |
| 24 | +enum class ParameterKind : unsigned { |
| 25 | + Accessor = 0, |
| 26 | + StdLayout = 1, |
| 27 | + Sampler = 2, |
| 28 | + Pointer = 3, |
| 29 | + SpecConstBuffer = 4, |
| 30 | + Stream = 5, |
| 31 | + Invalid = 0xF, |
| 32 | +}; |
| 33 | + |
| 34 | +/// Different binary formats supported as input to the JIT compiler. |
| 35 | +enum class BinaryFormat { INVALID, LLVM, SPIRV }; |
| 36 | + |
| 37 | +/// Information about a device intermediate representation module (e.g., SPIR-V, |
| 38 | +/// LLVM IR) from DPC++. |
| 39 | +struct SYCLKernelBinaryInfo { |
| 40 | + |
| 41 | + BinaryFormat Format = BinaryFormat::INVALID; |
| 42 | + |
| 43 | + size_t AddressBits = 0; |
| 44 | + |
| 45 | + BinaryAddress BinaryStart = nullptr; |
| 46 | + |
| 47 | + size_t BinarySize = 0; |
| 48 | +}; |
| 49 | + |
| 50 | +/// |
| 51 | +/// Describe a SYCL/OpenCL kernel attribute by its name and values. |
| 52 | +struct SYCLKernelAttribute { |
| 53 | + using AttributeValueList = std::vector<std::string>; |
| 54 | + |
| 55 | + // Explicit constructor for compatibility with LLVM YAML I/O. |
| 56 | + SYCLKernelAttribute() : Values{} {}; |
| 57 | + SYCLKernelAttribute(std::string Name) |
| 58 | + : AttributeName{std::move(Name)}, Values{} {} |
| 59 | + |
| 60 | + std::string AttributeName; |
| 61 | + AttributeValueList Values; |
| 62 | +}; |
| 63 | + |
| 64 | +enum ArgUsage : unsigned char { |
| 65 | + // Used to indicate that an argument is not used by the kernel |
| 66 | + Unused = 0, |
| 67 | + // Used to indicate that an argument is used by the kernel |
| 68 | + Used = 1u, |
| 69 | + // Used to indicate that the accessor/pointer argument has been promoted to |
| 70 | + // private memory |
| 71 | + PromotedPrivate = 1u << 4, |
| 72 | + // Used to indicate that the accessor/pointer argument has been promoted to |
| 73 | + // local memory |
| 74 | + PromotedLocal = 1u << 5, |
| 75 | +}; |
| 76 | + |
| 77 | +/// |
| 78 | +/// Encode usage of parameters for the actual kernel function. |
| 79 | +// This is a vector of unsigned char, because std::vector<bool> is a weird |
| 80 | +// construct and unlike all other std::vectors, and LLVM YAML I/O is having a |
| 81 | +// hard time coping with it. |
| 82 | +using ArgUsageMask = std::vector<std::underlying_type_t<ArgUsage>>; |
| 83 | + |
| 84 | +/// |
| 85 | +/// Describe the list of arguments by their kind. |
| 86 | +struct SYCLArgumentDescriptor { |
| 87 | + |
| 88 | + // Explicit constructor for compatibility with LLVM YAML I/O. |
| 89 | + SYCLArgumentDescriptor() : Kinds{}, UsageMask{} {} |
| 90 | + |
| 91 | + std::vector<ParameterKind> Kinds; |
| 92 | + |
| 93 | + ArgUsageMask UsageMask; |
| 94 | +}; |
| 95 | + |
| 96 | +/// |
| 97 | +/// List of SYCL/OpenCL kernel attributes. |
| 98 | +using AttributeList = std::vector<SYCLKernelAttribute>; |
| 99 | + |
| 100 | +/// Information about a kernel from DPC++. |
| 101 | +struct SYCLKernelInfo { |
| 102 | + |
| 103 | + std::string Name; |
| 104 | + |
| 105 | + SYCLArgumentDescriptor Args; |
| 106 | + |
| 107 | + AttributeList Attributes; |
| 108 | + |
| 109 | + SYCLKernelBinaryInfo BinaryInfo; |
| 110 | + |
| 111 | + //// Explicit constructor for compatibility with LLVM YAML I/O. |
| 112 | + SYCLKernelInfo() : Name{}, Args{}, Attributes{}, BinaryInfo{} {} |
| 113 | + |
| 114 | + SYCLKernelInfo(const std::string &KernelName, |
| 115 | + const SYCLArgumentDescriptor &ArgDesc, |
| 116 | + const SYCLKernelBinaryInfo &BinInfo) |
| 117 | + : Name{KernelName}, Args{ArgDesc}, Attributes{}, BinaryInfo{BinInfo} {} |
| 118 | + |
| 119 | + explicit SYCLKernelInfo(const std::string &KernelName) |
| 120 | + : Name{KernelName}, Args{}, Attributes{}, BinaryInfo{} {} |
| 121 | +}; |
| 122 | + |
| 123 | +/// |
| 124 | +/// Represents a SPIR-V translation unit containing SYCL kernels by the |
| 125 | +/// KernelInfo for each of the contained kernels. |
| 126 | +class SYCLModuleInfo { |
| 127 | +public: |
| 128 | + using KernelInfoList = std::vector<SYCLKernelInfo>; |
| 129 | + |
| 130 | + void addKernel(SYCLKernelInfo &Kernel) { Kernels.push_back(Kernel); } |
| 131 | + |
| 132 | + KernelInfoList &kernels() { return Kernels; } |
| 133 | + |
| 134 | + bool hasKernelFor(const std::string &KernelName) { |
| 135 | + return findKernelFor(KernelName) != nullptr; |
| 136 | + } |
| 137 | + |
| 138 | + SYCLKernelInfo *getKernelFor(const std::string &KernelName) { |
| 139 | + return findKernelFor(KernelName); |
| 140 | + } |
| 141 | + |
| 142 | +private: |
| 143 | + SYCLKernelInfo *findKernelFor(const std::string &KernelName) { |
| 144 | + auto It = |
| 145 | + std::find_if(Kernels.begin(), Kernels.end(), |
| 146 | + [&](SYCLKernelInfo &K) { return K.Name == KernelName; }); |
| 147 | + return (It != Kernels.end()) ? &*It : nullptr; |
| 148 | + } |
| 149 | + |
| 150 | + KernelInfoList Kernels; |
| 151 | +}; |
| 152 | + |
| 153 | +} // namespace jit_compiler |
| 154 | + |
| 155 | +#endif // SYCL_FUSION_COMMON_KERNEL_H |
0 commit comments