Hash :
f9709279
Author :
Date :
2024-09-20T16:02:56
CL/Vulkan: Add support for printf builtin processing The support for printf builtin in clspv enabled through the SPIR-V non-semantic clspv reflection instructions - PrintfInfo and PrintfBufferStorageBuffer [1]. The printf buffer is setup with a separate descriptor layout and the pipeline layout is updated accordingly. Also, printf is enabled as default option now for clspv. [1]: https://github.com/KhronosGroup/SPIRV-Registry/blob/master/nonsemantic/NonSemantic.ClspvReflection.html Bug: angleproject:369724757 Change-Id: I20b245eb0fea69941bd1aeb42534f8b729ec17e8 Signed-off-by: Gowtham Tammana <g.tammana@samsung.com> Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5893958 Reviewed-by: Charlie Lao <cclao@google.com> 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
//
// Copyright 2024 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.
//
// Utilities to map clspv interface variables to OpenCL and Vulkan mappings.
//
#include "libANGLE/renderer/vulkan/clspv_utils.h"
#include "libANGLE/renderer/vulkan/CLDeviceVk.h"
#include <string>
#include "common/string_utils.h"
#include "CL/cl_half.h"
namespace rx
{
constexpr std::string_view kPrintfConversionSpecifiers = "diouxXfFeEgGaAcsp";
constexpr std::string_view kPrintfFlagsSpecifiers = "-+ #0";
constexpr std::string_view kPrintfPrecisionSpecifiers = "123456789.";
constexpr std::string_view kPrintfVectorSizeSpecifiers = "23468";
namespace
{
template <typename T>
T ReadPtrAs(const unsigned char *data)
{
return *(reinterpret_cast<const T *>(data));
}
template <typename T>
T ReadPtrAsAndIncrement(unsigned char *&data)
{
T out = *(reinterpret_cast<T *>(data));
data += sizeof(T);
return out;
}
char getPrintfConversionSpecifier(std::string_view formatString)
{
return formatString.at(formatString.find_first_of(kPrintfConversionSpecifiers));
}
bool IsVectorFormat(std::string_view formatString)
{
ASSERT(formatString.at(0) == '%');
// go past the flags, field width and precision
size_t pos = formatString.find_first_not_of(kPrintfFlagsSpecifiers, 1ul);
pos = formatString.find_first_not_of(kPrintfPrecisionSpecifiers, pos);
return (formatString.at(pos) == 'v');
}
// Printing an individual formatted string into a std::string
// snprintf is used for parsing as OpenCL C printf is similar to printf
std::string PrintFormattedString(const std::string &formatString,
const unsigned char *data,
size_t size)
{
ASSERT(std::count(formatString.begin(), formatString.end(), '%') == 1);
size_t outSize = 1024;
std::vector<char> out(outSize);
out[0] = '\0';
char conversion = std::tolower(getPrintfConversionSpecifier(formatString));
bool finished = false;
while (!finished)
{
int bytesWritten = 0;
switch (conversion)
{
case 's':
{
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(), data);
break;
}
case 'f':
case 'e':
case 'g':
case 'a':
{
// all floats with same convention as snprintf
if (size == 2)
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
cl_half_to_float(ReadPtrAs<cl_half>(data)));
else if (size == 4)
bytesWritten =
snprintf(out.data(), outSize, formatString.c_str(), ReadPtrAs<float>(data));
else
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
ReadPtrAs<double>(data));
break;
}
default:
{
if (size == 1)
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
ReadPtrAs<uint8_t>(data));
else if (size == 2)
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
ReadPtrAs<uint16_t>(data));
else if (size == 4)
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
ReadPtrAs<uint32_t>(data));
else
bytesWritten = snprintf(out.data(), outSize, formatString.c_str(),
ReadPtrAs<uint64_t>(data));
break;
}
}
if (bytesWritten < 0)
{
out[0] = '\0';
finished = true;
}
else if (bytesWritten < static_cast<long>(outSize))
{
finished = true;
}
else
{
// insufficient size redo above post increment of size
outSize *= 2;
out.resize(outSize);
}
}
return std::string(out.data());
}
// Spec mention vn modifier to be printed in the form v1,v2...vn
std::string PrintVectorFormatIntoString(std::string formatString,
const unsigned char *data,
const uint32_t size)
{
ASSERT(IsVectorFormat(formatString));
size_t conversionPos = formatString.find_first_of(kPrintfConversionSpecifiers);
// keep everything after conversion specifier in remainingFormat
std::string remainingFormat = formatString.substr(conversionPos + 1);
formatString = formatString.substr(0, conversionPos + 1);
size_t vectorPos = formatString.find_first_of('v');
size_t vectorLengthPos = ++vectorPos;
size_t vectorLengthPosEnd =
formatString.find_first_not_of(kPrintfVectorSizeSpecifiers, vectorLengthPos);
std::string preVectorString = formatString.substr(0, vectorPos - 1);
std::string postVectorString = formatString.substr(vectorLengthPosEnd, formatString.size());
std::string vectorLengthStr = formatString.substr(vectorLengthPos, vectorLengthPosEnd);
int vectorLength = std::atoi(vectorLengthStr.c_str());
// skip the vector specifier
formatString = preVectorString + postVectorString;
// Get the length modifier
int elementSize = 0;
if (postVectorString.find("hh") != std::string::npos)
{
elementSize = 1;
}
else if (postVectorString.find("hl") != std::string::npos)
{
elementSize = 4;
// snprintf doesn't recognize the hl modifier so strip it
size_t hl = formatString.find("hl");
formatString.erase(hl, 2);
}
else if (postVectorString.find("h") != std::string::npos)
{
elementSize = 2;
}
else if (postVectorString.find("l") != std::string::npos)
{
elementSize = 8;
}
else
{
WARN() << "Vector specifier is used without a length modifier. Guessing it from "
"vector length and argument sizes in PrintInfo. Kernel modification is "
"recommended.";
elementSize = size / vectorLength;
}
std::string out{""};
for (int i = 0; i < vectorLength - 1; i++)
{
out += PrintFormattedString(formatString, data, size / vectorLength) + ",";
data += elementSize;
}
out += PrintFormattedString(formatString, data, size / vectorLength) + remainingFormat;
return out;
}
// Process the printf stream by breaking them down into individual format specifier and processing
// them.
void ProcessPrintfStatement(unsigned char *&data,
const angle::HashMap<uint32_t, ClspvPrintfInfo> *descs,
const unsigned char *dataEnd)
{
// printf storage buffer contents - | id | formatString | argSizes... |
uint32_t printfID = ReadPtrAsAndIncrement<uint32_t>(data);
const std::string &formatString = descs->at(printfID).formatSpecifier;
std::string printfOutput = "";
// formatString could be "<string literal> <% format specifiers ...> <string literal>"
// print the literal part if any first
size_t nextFormatSpecPos = formatString.find_first_of('%');
printfOutput += formatString.substr(0, nextFormatSpecPos);
// print each <% format specifier> + any string literal separately using snprintf
size_t idx = 0;
while (nextFormatSpecPos < formatString.size() - 1)
{
// Get the part of the format string before the next format specifier
size_t partStart = nextFormatSpecPos;
size_t partEnd = formatString.find_first_of('%', partStart + 1);
std::string partFormatString = formatString.substr(partStart, partEnd - partStart);
// Handle special cases
if (partEnd == partStart + 1)
{
printfOutput += "%";
nextFormatSpecPos = partEnd + 1;
continue;
}
else if (partEnd == std::string::npos && idx >= descs->at(printfID).argSizes.size())
{
// If there are no remaining arguments, the rest of the format
// should be printed verbatim
printfOutput += partFormatString;
break;
}
// The size of the argument that this format part will consume
const uint32_t &size = descs->at(printfID).argSizes[idx];
if (data + size > dataEnd)
{
data += size;
return;
}
// vector format need special care for snprintf
if (!IsVectorFormat(partFormatString))
{
// not a vector format can be printed through snprintf
// except for %s
if (getPrintfConversionSpecifier(partFormatString) == 's')
{
uint32_t stringID = ReadPtrAs<uint32_t>(data);
printfOutput +=
PrintFormattedString(partFormatString,
reinterpret_cast<const unsigned char *>(
descs->at(stringID).formatSpecifier.c_str()),
size);
}
else
{
printfOutput += PrintFormattedString(partFormatString, data, size);
}
data += size;
}
else
{
printfOutput += PrintVectorFormatIntoString(partFormatString, data, size);
data += size;
}
// Move to the next format part and prepare to handle the next arg
nextFormatSpecPos = partEnd;
idx++;
}
std::printf("%s", printfOutput.c_str());
}
} // namespace
// Process the data recorded into printf storage buffer along with the info in printfino descriptor
// and write it to stdout.
angle::Result ClspvProcessPrintfBuffer(unsigned char *buffer,
const size_t bufferSize,
const angle::HashMap<uint32_t, ClspvPrintfInfo> *infoMap)
{
// printf storage buffer contains a series of uint32_t values
// the first integer is offset from second to next available free memory -- this is the amount
// of data written by kernel.
const size_t bytesWritten = ReadPtrAsAndIncrement<uint32_t>(buffer) * sizeof(uint32_t);
const size_t dataSize = bufferSize - sizeof(uint32_t);
const size_t limit = std::min(bytesWritten, dataSize);
const unsigned char *dataEnd = buffer + limit;
while (buffer < dataEnd)
{
ProcessPrintfStatement(buffer, infoMap, dataEnd);
}
if (bufferSize < bytesWritten)
{
WARN() << "Printf storage buffer was not sufficient for all printfs. Around "
<< 100.0 * (float)(bytesWritten - bufferSize) / bytesWritten
<< "% of them have been skipped.";
}
return angle::Result::Continue;
}
std::string ClspvGetCompilerOptions(const CLDeviceVk *device)
{
ASSERT(device && device->getRenderer());
const vk::Renderer *rendererVk = device->getRenderer();
std::string options{""};
cl_uint addressBits;
if (IsError(device->getInfoUInt(cl::DeviceInfo::AddressBits, &addressBits)))
{
// This should'nt fail here
ASSERT(false);
}
options += addressBits == 64 ? " -arch=spir64" : " -arch=spir";
// Other internal Clspv compiler flags that are needed/required
options += " --long-vector";
options += " --global-offset";
options += " --enable-printf";
// 8 bit storage buffer support
if (!rendererVk->getFeatures().supports8BitStorageBuffer.enabled)
{
options += " --no-8bit-storage=ssbo";
}
if (!rendererVk->getFeatures().supports8BitUniformAndStorageBuffer.enabled)
{
options += " --no-8bit-storage=ubo";
}
if (!rendererVk->getFeatures().supports8BitPushConstant.enabled)
{
options += " --no-8bit-storage=pushconstant";
}
// 16 bit storage options
if (!rendererVk->getFeatures().supports16BitStorageBuffer.enabled)
{
options += " --no-16bit-storage=ssbo";
}
if (!rendererVk->getFeatures().supports16BitUniformAndStorageBuffer.enabled)
{
options += " --no-16bit-storage=ubo";
}
if (!rendererVk->getFeatures().supports16BitPushConstant.enabled)
{
options += " --no-16bit-storage=pushconstant";
}
return options;
}
} // namespace rx