Hash :
ed7c31c7
Author :
Date :
2023-11-29T18:31:43
OpenCL: Replace error-passing APIs to use angle::Result Part 2 of Issue 8435: This part replaces existing error handling for all CL APIs that return CL objects to now use "angle::Result". Bug: angleproject:8435 Change-Id: I75e41c7cbb06220cabec7cc9f9cb5107a3b6bd6d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5075773 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
//
// 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.
//
// CLKernelCL.cpp: Implements the class methods for CLKernelCL.
#include "libANGLE/renderer/cl/CLKernelCL.h"
#include "libANGLE/renderer/cl/CLCommandQueueCL.h"
#include "libANGLE/renderer/cl/CLContextCL.h"
#include "libANGLE/renderer/cl/CLDeviceCL.h"
#include "libANGLE/renderer/cl/CLMemoryCL.h"
#include "libANGLE/renderer/cl/CLSamplerCL.h"
#include "libANGLE/CLCommandQueue.h"
#include "libANGLE/CLContext.h"
#include "libANGLE/CLKernel.h"
#include "libANGLE/CLMemory.h"
#include "libANGLE/CLPlatform.h"
#include "libANGLE/CLProgram.h"
#include "libANGLE/CLSampler.h"
#include "libANGLE/cl_utils.h"
namespace rx
{
namespace
{
template <typename T>
bool GetWorkGroupInfo(cl_kernel kernel,
cl_device_id device,
cl::KernelWorkGroupInfo name,
T &value,
cl_int &errorCode)
{
errorCode = kernel->getDispatch().clGetKernelWorkGroupInfo(kernel, device, cl::ToCLenum(name),
sizeof(T), &value, nullptr);
return errorCode == CL_SUCCESS;
}
template <typename T>
bool GetArgInfo(cl_kernel kernel,
cl_uint index,
cl::KernelArgInfo name,
T &value,
cl_int &errorCode)
{
errorCode = kernel->getDispatch().clGetKernelArgInfo(kernel, index, cl::ToCLenum(name),
sizeof(T), &value, nullptr);
if (errorCode == CL_KERNEL_ARG_INFO_NOT_AVAILABLE)
{
errorCode = CL_SUCCESS;
}
return errorCode == CL_SUCCESS;
}
template <typename T>
bool GetKernelInfo(cl_kernel kernel, cl::KernelInfo name, T &value, cl_int &errorCode)
{
errorCode = kernel->getDispatch().clGetKernelInfo(kernel, cl::ToCLenum(name), sizeof(T), &value,
nullptr);
return errorCode == CL_SUCCESS;
}
bool GetArgString(cl_kernel kernel,
cl_uint index,
cl::KernelArgInfo name,
std::string &string,
cl_int &errorCode)
{
size_t size = 0u;
errorCode = kernel->getDispatch().clGetKernelArgInfo(kernel, index, cl::ToCLenum(name), 0u,
nullptr, &size);
if (errorCode == CL_KERNEL_ARG_INFO_NOT_AVAILABLE)
{
errorCode = CL_SUCCESS;
return true;
}
else if (errorCode != CL_SUCCESS)
{
return false;
}
std::vector<char> valString(size, '\0');
errorCode = kernel->getDispatch().clGetKernelArgInfo(kernel, index, cl::ToCLenum(name), size,
valString.data(), nullptr);
if (errorCode != CL_SUCCESS)
{
return false;
}
string.assign(valString.data(), valString.size() - 1u);
return true;
}
bool GetKernelString(cl_kernel kernel, cl::KernelInfo name, std::string &string, cl_int &errorCode)
{
size_t size = 0u;
errorCode =
kernel->getDispatch().clGetKernelInfo(kernel, cl::ToCLenum(name), 0u, nullptr, &size);
if (errorCode != CL_SUCCESS)
{
return false;
}
std::vector<char> valString(size, '\0');
errorCode = kernel->getDispatch().clGetKernelInfo(kernel, cl::ToCLenum(name), size,
valString.data(), nullptr);
if (errorCode != CL_SUCCESS)
{
return false;
}
string.assign(valString.data(), valString.size() - 1u);
return true;
}
} // namespace
CLKernelCL::CLKernelCL(const cl::Kernel &kernel, cl_kernel native)
: CLKernelImpl(kernel), mNative(native)
{}
CLKernelCL::~CLKernelCL()
{
if (mNative->getDispatch().clReleaseKernel(mNative) != CL_SUCCESS)
{
ERR() << "Error while releasing CL kernel";
}
}
angle::Result CLKernelCL::setArg(cl_uint argIndex, size_t argSize, const void *argValue)
{
void *value = nullptr;
if (argValue != nullptr)
{
// If argument is a CL object, fetch the mapped value
const CLContextCL &ctx = mKernel.getProgram().getContext().getImpl<CLContextCL>();
if (argSize == sizeof(cl_mem))
{
cl_mem memory = *static_cast<const cl_mem *>(argValue);
if (ctx.hasMemory(memory))
{
value = memory->cast<cl::Memory>().getImpl<CLMemoryCL>().getNative();
}
}
if (value == nullptr && argSize == sizeof(cl_sampler))
{
cl_sampler sampler = *static_cast<const cl_sampler *>(argValue);
if (ctx.hasSampler(sampler))
{
value = sampler->cast<cl::Sampler>().getImpl<CLSamplerCL>().getNative();
}
}
if (value == nullptr && argSize == sizeof(cl_command_queue))
{
cl_command_queue queue = *static_cast<const cl_command_queue *>(argValue);
if (ctx.hasDeviceQueue(queue))
{
value = queue->cast<cl::CommandQueue>().getImpl<CLCommandQueueCL>().getNative();
}
}
}
// If mapped value was found, use it instead of original value
if (value != nullptr)
{
argValue = &value;
}
ANGLE_CL_TRY(mNative->getDispatch().clSetKernelArg(mNative, argIndex, argSize, argValue));
return angle::Result::Continue;
}
angle::Result CLKernelCL::createInfo(CLKernelImpl::Info *infoOut) const
{
cl_int errorCode = CL_SUCCESS;
const cl::Context &ctx = mKernel.getProgram().getContext();
if (!GetKernelString(mNative, cl::KernelInfo::FunctionName, infoOut->functionName, errorCode) ||
!GetKernelInfo(mNative, cl::KernelInfo::NumArgs, infoOut->numArgs, errorCode) ||
(ctx.getPlatform().isVersionOrNewer(1u, 2u) &&
!GetKernelString(mNative, cl::KernelInfo::Attributes, infoOut->attributes, errorCode)))
{
ANGLE_CL_RETURN_ERROR(errorCode);
}
infoOut->workGroups.resize(ctx.getDevices().size());
for (size_t index = 0u; index < ctx.getDevices().size(); ++index)
{
const cl_device_id device = ctx.getDevices()[index]->getImpl<CLDeviceCL>().getNative();
WorkGroupInfo &workGroup = infoOut->workGroups[index];
if ((ctx.getPlatform().isVersionOrNewer(1u, 2u) &&
ctx.getDevices()[index]->supportsBuiltInKernel(infoOut->functionName) &&
!GetWorkGroupInfo(mNative, device, cl::KernelWorkGroupInfo::GlobalWorkSize,
workGroup.globalWorkSize, errorCode)) ||
!GetWorkGroupInfo(mNative, device, cl::KernelWorkGroupInfo::WorkGroupSize,
workGroup.workGroupSize, errorCode) ||
!GetWorkGroupInfo(mNative, device, cl::KernelWorkGroupInfo::CompileWorkGroupSize,
workGroup.compileWorkGroupSize, errorCode) ||
!GetWorkGroupInfo(mNative, device, cl::KernelWorkGroupInfo::LocalMemSize,
workGroup.localMemSize, errorCode) ||
!GetWorkGroupInfo(mNative, device,
cl::KernelWorkGroupInfo::PreferredWorkGroupSizeMultiple,
workGroup.prefWorkGroupSizeMultiple, errorCode) ||
!GetWorkGroupInfo(mNative, device, cl::KernelWorkGroupInfo::PrivateMemSize,
workGroup.privateMemSize, errorCode))
{
ANGLE_CL_RETURN_ERROR(errorCode);
}
}
infoOut->args.resize(infoOut->numArgs);
if (ctx.getPlatform().isVersionOrNewer(1u, 2u))
{
for (cl_uint index = 0u; index < infoOut->numArgs; ++index)
{
ArgInfo &arg = infoOut->args[index];
if (!GetArgInfo(mNative, index, cl::KernelArgInfo::AddressQualifier,
arg.addressQualifier, errorCode) ||
!GetArgInfo(mNative, index, cl::KernelArgInfo::AccessQualifier, arg.accessQualifier,
errorCode) ||
!GetArgString(mNative, index, cl::KernelArgInfo::TypeName, arg.typeName,
errorCode) ||
!GetArgInfo(mNative, index, cl::KernelArgInfo::TypeQualifier, arg.typeQualifier,
errorCode) ||
!GetArgString(mNative, index, cl::KernelArgInfo::Name, arg.name, errorCode))
{
ANGLE_CL_RETURN_ERROR(errorCode);
}
}
}
return angle::Result::Continue;
}
} // namespace rx