Hash :
0ed0de4f
Author :
Date :
2024-01-18T10:47:50
OpenCL/Vulkan: Add initial program build support Introducing both clspv compiler lib integration, and clBuildProgram support. Internally we also add buildInternal() routine to abstract compile, link, and build phases. Output SPIR-V is also parsed internally via parseReflection() so that we can extract needed reflection information for CL runtime. Bug: angleproject:8549 Change-Id: If0563f4bea7ed0e04a13ea7a46c125c811d9c2a2 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5303564 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// CLProgramVk.h: Defines the class interface for CLProgramVk, implementing CLProgramImpl.
#ifndef LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_
#define LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_
#include "libANGLE/renderer/vulkan/CLKernelVk.h"
#include "libANGLE/renderer/vulkan/cl_types.h"
#include "libANGLE/renderer/vulkan/vk_cache_utils.h"
#include "libANGLE/renderer/vulkan/vk_helpers.h"
#include "libANGLE/renderer/CLProgramImpl.h"
#include "libANGLE/CLProgram.h"
#include "clspv/Compiler.h"
#include "vulkan/vulkan_core.h"
#include "spirv-tools/libspirv.h"
namespace rx
{
class CLProgramVk : public CLProgramImpl
{
public:
struct SpvReflectionData
{
angle::HashMap<uint32_t, uint32_t> spvIntLookup;
angle::HashMap<uint32_t, std::string> spvStrLookup;
angle::HashMap<uint32_t, CLKernelVk::ArgInfo> kernelArgInfos;
angle::HashMap<std::string, uint32_t> kernelFlags;
angle::HashMap<std::string, std::string> kernelAttributes;
angle::HashMap<std::string, std::array<uint32_t, 3>> kernelCompileWGS;
angle::HashMap<uint32_t, VkPushConstantRange> pushConstants;
std::array<uint32_t, 3> specConstantWGS{0, 0, 0};
CLKernelArgsMap kernelArgsMap;
};
// Output binary structure (for CL_PROGRAM_BINARIES query)
struct ProgramBinaryOutputHeader
{
uint32_t headerVersion{1};
cl_program_binary_type binaryType{CL_PROGRAM_BINARY_TYPE_NONE};
};
static constexpr uint32_t LatestSupportedBinaryVersion = 1;
struct ScopedClspvContext : angle::NonCopyable
{
ScopedClspvContext() = default;
~ScopedClspvContext() { clspvFreeOutputBuildObjs(mOutputBin, mOutputBuildLog); }
size_t mOutputBinSize{0};
char *mOutputBin{nullptr};
char *mOutputBuildLog{nullptr};
};
struct ScopedProgramCallback : angle::NonCopyable
{
ScopedProgramCallback() = delete;
ScopedProgramCallback(cl::Program *notify) : mNotify(notify) {}
~ScopedProgramCallback()
{
if (mNotify)
{
mNotify->callback();
}
}
cl::Program *mNotify{nullptr};
};
enum class BuildType
{
BUILD = 0,
COMPILE,
LINK,
BINARY
};
struct DeviceProgramData
{
std::vector<char> IR;
std::string buildLog;
angle::spirv::Blob binary;
SpvReflectionData reflectionData;
VkPushConstantRange pushConstRange{};
cl_build_status buildStatus{CL_BUILD_NONE};
cl_program_binary_type binaryType{CL_PROGRAM_BINARY_TYPE_NONE};
size_t numKernels() const { return reflectionData.kernelArgsMap.size(); }
size_t numKernelArgs(const std::string &kernelName) const
{
return containsKernel(kernelName) ? getKernelArgsMap().at(kernelName).size() : 0;
}
const CLKernelArgsMap &getKernelArgsMap() const { return reflectionData.kernelArgsMap; }
bool containsKernel(const std::string &name) const
{
return reflectionData.kernelArgsMap.contains(name);
}
std::string getKernelNames() const
{
std::string names;
for (auto name = getKernelArgsMap().begin(); name != getKernelArgsMap().end(); ++name)
{
names += name->first + (std::next(name) != getKernelArgsMap().end() ? ";" : "\0");
}
return names;
}
CLKernelArguments getKernelArguments(const std::string &kernelName) const
{
CLKernelArguments kargsCopy;
if (containsKernel(kernelName))
{
const CLKernelArguments &kargs = getKernelArgsMap().at(kernelName);
for (const CLKernelArgument &karg : kargs)
{
kargsCopy.push_back(karg);
}
}
return kargsCopy;
}
cl::CompiledWorkgroupSize getCompiledWGS(const std::string &kernelName) const
{
cl::CompiledWorkgroupSize compiledWGS{0, 0, 0};
if (reflectionData.kernelCompileWGS.contains(kernelName))
{
compiledWGS = reflectionData.kernelCompileWGS.at(kernelName);
}
return compiledWGS;
}
std::string getKernelAttributes(const std::string &kernelName) const
{
if (containsKernel(kernelName))
{
return reflectionData.kernelAttributes.at(kernelName.c_str());
}
return std::string{};
}
};
using DevicePrograms = angle::HashMap<const _cl_device_id *, DeviceProgramData>;
using DeviceProgramDatas = std::vector<const DeviceProgramData *>;
CLProgramVk(const cl::Program &program);
~CLProgramVk() override;
angle::Result init();
angle::Result init(const size_t *lengths, const unsigned char **binaries, cl_int *binaryStatus);
angle::Result build(const cl::DevicePtrs &devices,
const char *options,
cl::Program *notify) override;
angle::Result compile(const cl::DevicePtrs &devices,
const char *options,
const cl::ProgramPtrs &inputHeaders,
const char **headerIncludeNames,
cl::Program *notify) override;
angle::Result getInfo(cl::ProgramInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const override;
angle::Result getBuildInfo(const cl::Device &device,
cl::ProgramBuildInfo name,
size_t valueSize,
void *value,
size_t *valueSizeRet) const override;
angle::Result createKernel(const cl::Kernel &kernel,
const char *name,
CLKernelImpl::Ptr *kernelOut) override;
angle::Result createKernels(cl_uint numKernels,
CLKernelImpl::CreateFuncs &createFuncs,
cl_uint *numKernelsRet) override;
const DeviceProgramData *getDeviceProgramData(const char *kernelName) const;
const DeviceProgramData *getDeviceProgramData(const _cl_device_id *device) const;
bool buildInternal(const cl::DevicePtrs &devices,
std::string options,
std::string internalOptions,
BuildType buildType,
const DeviceProgramDatas &inputProgramDatas);
angle::spirv::Blob stripReflection(const DeviceProgramData *deviceProgramData);
private:
CLContextVk *mContext;
std::string mProgramOpts;
DevicePrograms mAssociatedDevicePrograms;
PipelineLayoutCache mPipelineLayoutCache;
vk::MetaDescriptorPool mMetaDescriptorPool;
DescriptorSetLayoutCache mDescSetLayoutCache;
vk::DescriptorSetLayoutPointerArray mDescriptorSetLayouts;
vk::DescriptorSetArray<vk::DescriptorPoolPointer> mDescriptorPools;
std::mutex mProgramMutex;
};
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_CLPROGRAMVK_H_