alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1 | // Copyright 2019 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Builtins.h" |
| 16 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 17 | #include <cstdlib> |
| 18 | #include <unordered_map> |
| 19 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 20 | using namespace llvm; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 21 | using namespace clspv; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 22 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 23 | namespace { |
| 24 | //////////////////////////////////////////////////////////////////////////////// |
| 25 | //// Convert Builtin function name to a Type enum |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | Builtins::BuiltinType LookupBuiltinType(const std::string &name) { |
| 28 | |
| 29 | // Build static map of builtin function names |
| 30 | #include "BuiltinsMap.inc" |
| 31 | |
| 32 | auto ii = s_func_map.find(name.c_str()); |
| 33 | if (ii != s_func_map.end()) { |
| 34 | return (*ii).second; |
| 35 | } |
| 36 | return Builtins::kBuiltinNone; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 37 | } |
| 38 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 39 | //////////////////////////////////////////////////////////////////////////////// |
| 40 | // Mangled name parsing utilities |
| 41 | // - We only handle Itanium-style C++ mangling, plus modifications for OpenCL. |
| 42 | //////////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | // Given a mangled name starting at character position |pos| in |str|, extracts |
| 45 | // the original name (without mangling) and updates |pos| so it will index the |
| 46 | // character just past that original name (which might be just past the end of |
| 47 | // the string). If the mangling is invalid, then an empty string is returned, |
| 48 | // and |pos| is not updated. Example: if str = "_Z3fooi", and *pos = 2, then |
| 49 | // returns "foo" and adds 4 to *pos. |
| 50 | std::string GetUnmangledName(const std::string &str, size_t *pos) { |
| 51 | char *end = nullptr; |
| 52 | assert(*pos < str.size()); |
| 53 | auto name_len = strtol(&str[*pos], &end, 10); |
| 54 | if (!name_len) { |
| 55 | return ""; |
| 56 | } |
| 57 | ptrdiff_t name_pos = end - str.data(); |
| 58 | if (name_pos + name_len > str.size()) { |
| 59 | // Protect against maliciously large number. |
| 60 | return ""; |
| 61 | } |
| 62 | |
| 63 | *pos = name_pos + name_len; |
| 64 | return str.substr(size_t(name_pos), name_len); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 65 | } |
| 66 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 67 | // Capture parameter type and qualifiers starting at |pos| |
| 68 | // - return new parsing position pos, or zero for error |
| 69 | size_t GetParameterType(const std::string &mangled_name, |
| 70 | clspv::Builtins::ParamTypeInfo *type_info, size_t pos) { |
| 71 | // Parse a parameter type encoding |
| 72 | char type_code = mangled_name[pos++]; |
| 73 | |
| 74 | switch (type_code) { |
| 75 | // qualifiers |
| 76 | case 'P': // Pointer |
| 77 | case 'R': // Reference |
| 78 | return GetParameterType(mangled_name, type_info, pos); |
| 79 | case 'k': // ??? not part of cxxabi |
| 80 | case 'K': // const |
| 81 | case 'V': // volatile |
| 82 | return GetParameterType(mangled_name, type_info, pos); |
| 83 | case 'U': { // Address space |
| 84 | // address_space name not captured |
| 85 | (void)GetUnmangledName(mangled_name, &pos); |
| 86 | return GetParameterType(mangled_name, type_info, pos); |
| 87 | } |
| 88 | // OCL types |
| 89 | case 'D': |
| 90 | type_code = mangled_name[pos++]; |
| 91 | if (type_code == 'v') { // OCL vector |
| 92 | char *end = nullptr; |
| 93 | int numElems = strtol(&mangled_name[pos], &end, 10); |
| 94 | if (!numElems) { |
| 95 | return 0; |
| 96 | } |
| 97 | type_info->vector_size = numElems; |
| 98 | pos = end - mangled_name.data(); |
| 99 | if (pos > mangled_name.size()) { |
| 100 | // Protect against maliciously large number. |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | if (mangled_name[pos++] != '_') { |
| 105 | return 0; |
| 106 | } |
| 107 | return GetParameterType(mangled_name, type_info, pos); |
| 108 | } else if (type_code == 'h') { // OCL half |
| 109 | type_info->type_id = Type::FloatTyID; |
| 110 | type_info->is_signed = true; |
| 111 | type_info->byte_len = 2; |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 112 | return pos; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 113 | } else { |
| 114 | #ifdef DEBUG |
| 115 | llvm::outs() << "Func: " << mangled_name << "\n"; |
| 116 | llvm_unreachable("failed to demangle name"); |
| 117 | #endif |
| 118 | return 0; |
| 119 | } |
| 120 | break; |
| 121 | |
| 122 | // element types |
| 123 | case 'l': // long |
| 124 | case 'i': // int |
| 125 | case 's': // short |
| 126 | case 'c': // char |
| 127 | case 'a': // signed char |
| 128 | type_info->type_id = Type::IntegerTyID; |
| 129 | type_info->is_signed = true; |
| 130 | break; |
| 131 | case 'm': // unsigned long |
| 132 | case 'j': // unsigned int |
| 133 | case 't': // unsigned short |
| 134 | case 'h': // unsigned char |
| 135 | type_info->type_id = Type::IntegerTyID; |
| 136 | type_info->is_signed = false; |
| 137 | break; |
| 138 | case 'd': // double float |
| 139 | case 'f': // single float |
| 140 | type_info->type_id = Type::FloatTyID; |
| 141 | type_info->is_signed = true; |
| 142 | break; |
| 143 | case 'v': // void |
| 144 | break; |
| 145 | case '1': // struct name |
| 146 | case '2': // - a <positive length number> for size of the following name |
| 147 | case '3': // - e.g. struct Foobar {} - would be encoded as '6Foobar' |
| 148 | case '4': // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.unqualified-name |
| 149 | case '5': |
| 150 | case '6': |
| 151 | case '7': |
| 152 | case '8': |
| 153 | case '9': |
| 154 | type_info->type_id = Type::StructTyID; |
| 155 | pos--; |
| 156 | type_info->name = GetUnmangledName(mangled_name, &pos); |
| 157 | break; |
| 158 | case '.': |
| 159 | return 0; |
| 160 | default: |
| 161 | #ifdef DEBUG |
| 162 | llvm::outs() << "Func: " << mangled_name << "\n"; |
| 163 | llvm_unreachable("failed to demangle name"); |
| 164 | #endif |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | switch (type_code) { |
| 169 | // element types |
| 170 | case 'l': // long |
| 171 | case 'm': // unsigned long |
| 172 | case 'd': // double float |
| 173 | type_info->byte_len = 8; |
| 174 | break; |
| 175 | case 'i': // int |
| 176 | case 'j': // unsigned int |
| 177 | case 'f': // single float |
| 178 | type_info->byte_len = 4; |
| 179 | break; |
| 180 | case 's': // short |
| 181 | case 't': // unsigned short |
| 182 | type_info->byte_len = 2; |
| 183 | break; |
| 184 | case 'c': // char |
| 185 | case 'a': // signed char |
| 186 | case 'h': // unsigned char |
| 187 | type_info->byte_len = 1; |
| 188 | break; |
| 189 | default: |
| 190 | break; |
| 191 | } |
| 192 | return pos; |
| 193 | } |
| 194 | } // namespace |
| 195 | |
| 196 | //////////////////////////////////////////////////////////////////////////////// |
| 197 | // FunctionInfo::GetFromMangledNameCheck |
| 198 | // - parse mangled name as far as possible. Some names are an aggregate of |
| 199 | // fields separated by '.' |
| 200 | // - extract name and parameter types, and return type for 'convert' functions |
| 201 | // - return true if the mangled name can be fully parsed |
| 202 | bool Builtins::FunctionInfo::GetFromMangledNameCheck( |
| 203 | const std::string &mangled_name) { |
| 204 | size_t pos = 0; |
| 205 | if (!(mangled_name[pos++] == '_' && mangled_name[pos++] == 'Z')) { |
| 206 | name_ = mangled_name; |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | name_ = GetUnmangledName(mangled_name, &pos); |
| 211 | if (name_.empty()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (!name_.compare(0, 8, "convert_")) { |
| 216 | // deduce return type from name, only for convert |
| 217 | char tok = name_[8]; |
| 218 | return_type_.is_signed = tok != 'u'; // unsigned |
| 219 | return_type_.type_id = tok == 'f' ? Type::FloatTyID : Type::IntegerTyID; |
| 220 | } |
| 221 | |
| 222 | auto mangled_name_len = mangled_name.size(); |
| 223 | while (pos < mangled_name_len) { |
| 224 | ParamTypeInfo type_info; |
| 225 | if (mangled_name[pos] == 'S') { |
| 226 | // handle duplicate param_type |
| 227 | if (mangled_name[pos + 1] != '_') { |
| 228 | return false; |
| 229 | } |
| 230 | pos += 2; |
| 231 | if (params_.empty()) { |
| 232 | return false; |
| 233 | } |
| 234 | params_.push_back(params_.back()); |
alan-baker | adf4426 | 2020-03-16 11:17:21 -0400 | [diff] [blame] | 235 | } else if ((pos = GetParameterType(mangled_name, &type_info, pos))) { |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 236 | params_.push_back(type_info); |
| 237 | } else { |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return true; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 243 | } |
| 244 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 245 | //////////////////////////////////////////////////////////////////////////////// |
| 246 | // FunctionInfo ctor - parses mangled name |
| 247 | Builtins::FunctionInfo::FunctionInfo(const std::string &mangled_name) { |
| 248 | is_valid_ = GetFromMangledNameCheck(mangled_name); |
| 249 | type_ = LookupBuiltinType(name_); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 250 | } |
| 251 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 252 | // get const ParamTypeInfo for nth parameter |
| 253 | const Builtins::ParamTypeInfo & |
| 254 | Builtins::FunctionInfo::getParameter(size_t _arg) const { |
| 255 | assert(params_.size() > _arg); |
| 256 | return params_[_arg]; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 257 | } |
| 258 | |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 259 | // Test for OCL Sampler parameter type |
| 260 | bool Builtins::ParamTypeInfo::isSampler() const { |
| 261 | return type_id == Type::StructTyID && name == "ocl_sampler"; |
| 262 | } |
| 263 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 264 | //////////////////////////////////////////////////////////////////////////////// |
| 265 | //// Lookup interface |
| 266 | //// - only demangle once for any name encountered |
| 267 | //////////////////////////////////////////////////////////////////////////////// |
| 268 | const Builtins::FunctionInfo & |
| 269 | Builtins::Lookup(const std::string &mangled_name) { |
| 270 | static std::unordered_map<std::string, FunctionInfo> s_mangled_map; |
| 271 | auto fi = s_mangled_map.emplace(mangled_name, mangled_name); |
| 272 | return (*fi.first).second; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 273 | } |
| 274 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 275 | //////////////////////////////////////////////////////////////////////////////// |
| 276 | //// Legacy interface |
| 277 | //////////////////////////////////////////////////////////////////////////////// |
| 278 | bool Builtins::IsImageBuiltin(StringRef name) { |
| 279 | auto func_type = Lookup(name).getType(); |
| 280 | return func_type > kType_Image_Start && func_type < kType_Image_End; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 281 | } |
| 282 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 283 | bool Builtins::IsSampledImageRead(StringRef name) { |
| 284 | return IsFloatSampledImageRead(name) || IsUintSampledImageRead(name) || |
| 285 | IsIntSampledImageRead(name); |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 286 | } |
| 287 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 288 | bool Builtins::IsFloatSampledImageRead(StringRef _name) { |
| 289 | const auto &fi = Lookup(_name); |
| 290 | if (fi.getType() == kReadImagef) { |
| 291 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 292 | return pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 293 | } |
| 294 | return false; |
alan-baker | 75090e4 | 2020-02-20 11:21:04 -0500 | [diff] [blame] | 295 | } |
| 296 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 297 | bool Builtins::IsUintSampledImageRead(StringRef _name) { |
| 298 | const auto &fi = Lookup(_name); |
| 299 | if (fi.getType() == kReadImageui) { |
| 300 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 301 | return pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 302 | } |
| 303 | return false; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 304 | } |
| 305 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 306 | bool Builtins::IsIntSampledImageRead(StringRef _name) { |
| 307 | const auto &fi = Lookup(_name); |
| 308 | if (fi.getType() == kReadImagei) { |
| 309 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 310 | return pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 311 | } |
| 312 | return false; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 313 | } |
| 314 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 315 | bool Builtins::IsUnsampledImageRead(StringRef name) { |
| 316 | return IsFloatUnsampledImageRead(name) || IsUintUnsampledImageRead(name) || |
| 317 | IsIntUnsampledImageRead(name); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 318 | } |
| 319 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 320 | bool Builtins::IsFloatUnsampledImageRead(StringRef _name) { |
| 321 | const auto &fi = Lookup(_name); |
| 322 | if (fi.getType() == kReadImagef) { |
| 323 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 324 | return !pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 325 | } |
| 326 | return false; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 327 | } |
| 328 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 329 | bool Builtins::IsUintUnsampledImageRead(StringRef _name) { |
| 330 | const auto &fi = Lookup(_name); |
| 331 | if (fi.getType() == kReadImageui) { |
| 332 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 333 | return !pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 334 | } |
| 335 | return false; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 336 | } |
| 337 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 338 | bool Builtins::IsIntUnsampledImageRead(StringRef _name) { |
| 339 | const auto &fi = Lookup(_name); |
| 340 | if (fi.getType() == kReadImagei) { |
| 341 | const auto &pi = fi.getParameter(1); |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 342 | return !pi.isSampler(); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 343 | } |
| 344 | return false; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 345 | } |
| 346 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 347 | bool Builtins::IsImageWrite(StringRef name) { |
| 348 | auto func_code = Lookup(name).getType(); |
| 349 | return func_code == kWriteImagef || func_code == kWriteImageui || |
| 350 | func_code == kWriteImagei || func_code == kWriteImageh; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 351 | } |
| 352 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 353 | bool Builtins::IsFloatImageWrite(StringRef name) { |
| 354 | return Lookup(name) == kWriteImagef; |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 355 | } |
| 356 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 357 | bool Builtins::IsUintImageWrite(StringRef name) { |
| 358 | return Lookup(name) == kWriteImageui; |
| 359 | } |
| 360 | |
| 361 | bool Builtins::IsIntImageWrite(StringRef name) { |
| 362 | return Lookup(name) == kWriteImagei; |
| 363 | } |
| 364 | |
| 365 | bool Builtins::IsGetImageHeight(StringRef name) { |
| 366 | return Lookup(name) == kGetImageHeight; |
| 367 | } |
| 368 | |
| 369 | bool Builtins::IsGetImageWidth(StringRef name) { |
| 370 | return Lookup(name) == kGetImageWidth; |
| 371 | } |
| 372 | |
| 373 | bool Builtins::IsGetImageDepth(StringRef name) { |
| 374 | return Lookup(name) == kGetImageDepth; |
| 375 | } |
| 376 | |
| 377 | bool Builtins::IsGetImageDim(StringRef name) { |
| 378 | return Lookup(name) == kGetImageDim; |
| 379 | } |
| 380 | |
| 381 | bool Builtins::IsImageQuery(StringRef name) { |
| 382 | auto func_code = Lookup(name).getType(); |
| 383 | return func_code == kGetImageHeight || func_code == kGetImageWidth || |
| 384 | func_code == kGetImageDepth || func_code == kGetImageDim; |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 385 | } |