1 //===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6 // See https://llvm.org/LICENSE.txt for license information. 7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8 // 9 //===----------------------------------------------------------------------===// 10 // 11 // This tablegen backend emits code for checking whether a function is an 12 // OpenCL builtin function. If so, all overloads of this function are 13 // added to the LookupResult. The generated include file is used by 14 // SemaLookup.cpp 15 // 16 // For a successful lookup of e.g. the "cos" builtin, isOpenCLBuiltin("cos") 17 // returns a pair <Index, Len>. 18 // BuiltinTable[Index] to BuiltinTable[Index + Len] contains the pairs 19 // <SigIndex, SigLen> of the overloads of "cos". 20 // SignatureTable[SigIndex] to SignatureTable[SigIndex + SigLen] contains 21 // one of the signatures of "cos". The SignatureTable entry can be 22 // referenced by other functions, e.g. "sin", to exploit the fact that 23 // many OpenCL builtins share the same signature. 24 // 25 // The file generated by this TableGen emitter contains the following: 26 // 27 // * Structs and enums to represent types and function signatures. 28 // 29 // * OpenCLTypeStruct TypeTable[] 30 // Type information for return types and arguments. 31 // 32 // * unsigned SignatureTable[] 33 // A list of types representing function signatures. Each entry is an index 34 // into the above TypeTable. Multiple entries following each other form a 35 // signature, where the first entry is the return type and subsequent 36 // entries are the argument types. 37 // 38 // * OpenCLBuiltinStruct BuiltinTable[] 39 // Each entry represents one overload of an OpenCL builtin function and 40 // consists of an index into the SignatureTable and the number of arguments. 41 // 42 // * std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) 43 // Find out whether a string matches an existing OpenCL builtin function 44 // name and return an index into BuiltinTable and the number of overloads. 45 // 46 // * void OCL2Qual(ASTContext&, OpenCLTypeStruct, std::vector<QualType>&) 47 // Convert an OpenCLTypeStruct type to a list of QualType instances. 48 // One OpenCLTypeStruct can represent multiple types, primarily when using 49 // GenTypes. 50 // 51 //===----------------------------------------------------------------------===// 52 53 #include "llvm/ADT/MapVector.h" 54 #include "llvm/ADT/STLExtras.h" 55 #include "llvm/ADT/SmallString.h" 56 #include "llvm/ADT/StringExtras.h" 57 #include "llvm/ADT/StringRef.h" 58 #include "llvm/ADT/StringSet.h" 59 #include "llvm/ADT/StringSwitch.h" 60 #include "llvm/Support/ErrorHandling.h" 61 #include "llvm/Support/raw_ostream.h" 62 #include "llvm/TableGen/Error.h" 63 #include "llvm/TableGen/Record.h" 64 #include "llvm/TableGen/StringMatcher.h" 65 #include "llvm/TableGen/TableGenBackend.h" 66 #include <set> 67 68 using namespace llvm; 69 70 namespace { 71 class BuiltinNameEmitter { 72 public: 73 BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS) 74 : Records(Records), OS(OS) {} 75 76 // Entrypoint to generate the functions and structures for checking 77 // whether a function is an OpenCL builtin function. 78 void Emit(); 79 80 private: 81 // Contains OpenCL builtin functions and related information, stored as 82 // Record instances. They are coming from the associated TableGen file. 83 RecordKeeper &Records; 84 85 // The output file. 86 raw_ostream &OS; 87 88 // Helper function for BuiltinNameEmitter::EmitDeclarations. Generate enum 89 // definitions in the Output string parameter, and save their Record instances 90 // in the List parameter. 91 // \param Types (in) List containing the Types to extract. 92 // \param TypesSeen (inout) List containing the Types already extracted. 93 // \param Output (out) String containing the enums to emit in the output file. 94 // \param List (out) List containing the extracted Types, except the Types in 95 // TypesSeen. 96 void ExtractEnumTypes(std::vector<Record *> &Types, 97 StringMap<bool> &TypesSeen, std::string &Output, 98 std::vector<const Record *> &List); 99 100 // Emit the enum or struct used in the generated file. 101 // Populate the TypeList at the same time. 102 void EmitDeclarations(); 103 104 // Parse the Records generated by TableGen to populate the SignaturesList, 105 // FctOverloadMap and TypeMap. 106 void GetOverloads(); 107 108 // Emit the TypeTable containing all types used by OpenCL builtins. 109 void EmitTypeTable(); 110 111 // Emit the SignatureTable. This table contains all the possible signatures. 112 // A signature is stored as a list of indexes of the TypeTable. 113 // The first index references the return type (mandatory), and the followings 114 // reference its arguments. 115 // E.g.: 116 // 15, 2, 15 can represent a function with the signature: 117 // int func(float, int) 118 // The "int" type being at the index 15 in the TypeTable. 119 void EmitSignatureTable(); 120 121 // Emit the BuiltinTable table. This table contains all the overloads of 122 // each function, and is a struct OpenCLBuiltinDecl. 123 // E.g.: 124 // // 891 convert_float2_rtn 125 // { 58, 2, 100, 0 }, 126 // This means that the signature of this convert_float2_rtn overload has 127 // 1 argument (+1 for the return type), stored at index 58 in 128 // the SignatureTable. The last two values represent the minimum (1.0) and 129 // maximum (0, meaning no max version) OpenCL version in which this overload 130 // is supported. 131 void EmitBuiltinTable(); 132 133 // Emit a StringMatcher function to check whether a function name is an 134 // OpenCL builtin function name. 135 void EmitStringMatcher(); 136 137 // Emit a function returning the clang QualType instance associated with 138 // the TableGen Record Type. 139 void EmitQualTypeFinder(); 140 141 // Contains a list of the available signatures, without the name of the 142 // function. Each pair consists of a signature and a cumulative index. 143 // E.g.: <<float, float>, 0>, 144 // <<float, int, int, 2>>, 145 // <<float>, 5>, 146 // ... 147 // <<double, double>, 35>. 148 std::vector<std::pair<std::vector<Record *>, unsigned>> SignaturesList; 149 150 // Map the name of a builtin function to its prototypes (instances of the 151 // TableGen "Builtin" class). 152 // Each prototype is registered as a pair of: 153 // <pointer to the "Builtin" instance, 154 // cumulative index of the associated signature in the SignaturesList> 155 // E.g.: The function cos: (float cos(float), double cos(double), ...) 156 // <"cos", <<ptrToPrototype0, 5>, 157 // <ptrToPrototype1, 35>, 158 // <ptrToPrototype2, 79>> 159 // ptrToPrototype1 has the following signature: <double, double> 160 MapVector<StringRef, std::vector<std::pair<const Record *, unsigned>>> 161 FctOverloadMap; 162 163 // Contains the map of OpenCL types to their index in the TypeTable. 164 MapVector<const Record *, unsigned> TypeMap; 165 166 // List of OpenCL type names in the same order as in enum OpenCLTypeID. 167 // This list does not contain generic types. 168 std::vector<const Record *> TypeList; 169 170 // Same as TypeList, but for generic types only. 171 std::vector<const Record *> GenTypeList; 172 }; 173 } // namespace 174 175 void BuiltinNameEmitter::Emit() { 176 emitSourceFileHeader("OpenCL Builtin handling", OS); 177 178 OS << "#include \"llvm/ADT/StringRef.h\"\n"; 179 OS << "using namespace clang;\n\n"; 180 181 // Emit enums and structs. 182 EmitDeclarations(); 183 184 GetOverloads(); 185 186 // Emit tables. 187 EmitTypeTable(); 188 EmitSignatureTable(); 189 EmitBuiltinTable(); 190 191 EmitStringMatcher(); 192 193 EmitQualTypeFinder(); 194 } 195 196 void BuiltinNameEmitter::ExtractEnumTypes(std::vector<Record *> &Types, 197 StringMap<bool> &TypesSeen, 198 std::string &Output, 199 std::vector<const Record *> &List) { 200 raw_string_ostream SS(Output); 201 202 for (const auto *T : Types) { 203 if (TypesSeen.find(T->getValueAsString("Name")) == TypesSeen.end()) { 204 SS << " OCLT_" + T->getValueAsString("Name") << ",\n"; 205 // Save the type names in the same order as their enum value. Note that 206 // the Record can be a VectorType or something else, only the name is 207 // important. 208 List.push_back(T); 209 TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); 210 } 211 } 212 SS.flush(); 213 } 214 215 void BuiltinNameEmitter::EmitDeclarations() { 216 // Enum of scalar type names (float, int, ...) and generic type sets. 217 OS << "enum OpenCLTypeID {\n"; 218 219 StringMap<bool> TypesSeen; 220 std::string GenTypeEnums; 221 std::string TypeEnums; 222 223 // Extract generic types and non-generic types separately, to keep 224 // gentypes at the end of the enum which simplifies the special handling 225 // for gentypes in SemaLookup. 226 std::vector<Record *> GenTypes = 227 Records.getAllDerivedDefinitions("GenericType"); 228 ExtractEnumTypes(GenTypes, TypesSeen, GenTypeEnums, GenTypeList); 229 230 std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 231 ExtractEnumTypes(Types, TypesSeen, TypeEnums, TypeList); 232 233 OS << TypeEnums; 234 OS << GenTypeEnums; 235 OS << "};\n"; 236 237 // Structure definitions. 238 OS << R"( 239 // Image access qualifier. 240 enum OpenCLAccessQual : unsigned char { 241 OCLAQ_None, 242 OCLAQ_ReadOnly, 243 OCLAQ_WriteOnly, 244 OCLAQ_ReadWrite 245 }; 246 247 // Represents a return type or argument type. 248 struct OpenCLTypeStruct { 249 // A type (e.g. float, int, ...). 250 const OpenCLTypeID ID; 251 // Vector size (if applicable; 0 for scalars and generic types). 252 const unsigned VectorWidth; 253 // 0 if the type is not a pointer. 254 const bool IsPointer; 255 // 0 if the type is not const. 256 const bool IsConst; 257 // 0 if the type is not volatile. 258 const bool IsVolatile; 259 // Access qualifier. 260 const OpenCLAccessQual AccessQualifier; 261 // Address space of the pointer (if applicable). 262 const LangAS AS; 263 }; 264 265 // One overload of an OpenCL builtin function. 266 struct OpenCLBuiltinStruct { 267 // Index of the signature in the OpenCLTypeStruct table. 268 const unsigned SigTableIndex; 269 // Entries between index SigTableIndex and (SigTableIndex + NumTypes - 1) in 270 // the SignatureTable represent the complete signature. The first type at 271 // index SigTableIndex is the return type. 272 const unsigned NumTypes; 273 // First OpenCL version in which this overload was introduced (e.g. CL20). 274 const unsigned short MinVersion; 275 // First OpenCL version in which this overload was removed (e.g. CL20). 276 const unsigned short MaxVersion; 277 }; 278 279 )"; 280 } 281 282 // Verify that the combination of GenTypes in a signature is supported. 283 // To simplify the logic for creating overloads in SemaLookup, only allow 284 // a signature to contain different GenTypes if these GenTypes represent 285 // the same number of actual scalar or vector types. 286 // 287 // Exit with a fatal error if an unsupported construct is encountered. 288 static void VerifySignature(const std::vector<Record *> &Signature, 289 const Record *BuiltinRec) { 290 unsigned GenTypeVecSizes = 1; 291 unsigned GenTypeTypes = 1; 292 293 for (const auto *T : Signature) { 294 // Check all GenericType arguments in this signature. 295 if (T->isSubClassOf("GenericType")) { 296 // Check number of vector sizes. 297 unsigned NVecSizes = 298 T->getValueAsDef("VectorList")->getValueAsListOfInts("List").size(); 299 if (NVecSizes != GenTypeVecSizes && NVecSizes != 1) { 300 if (GenTypeVecSizes > 1) { 301 // We already saw a gentype with a different number of vector sizes. 302 PrintFatalError(BuiltinRec->getLoc(), 303 "number of vector sizes should be equal or 1 for all gentypes " 304 "in a declaration"); 305 } 306 GenTypeVecSizes = NVecSizes; 307 } 308 309 // Check number of data types. 310 unsigned NTypes = 311 T->getValueAsDef("TypeList")->getValueAsListOfDefs("List").size(); 312 if (NTypes != GenTypeTypes && NTypes != 1) { 313 if (GenTypeTypes > 1) { 314 // We already saw a gentype with a different number of types. 315 PrintFatalError(BuiltinRec->getLoc(), 316 "number of types should be equal or 1 for all gentypes " 317 "in a declaration"); 318 } 319 GenTypeTypes = NTypes; 320 } 321 } 322 } 323 } 324 325 void BuiltinNameEmitter::GetOverloads() { 326 // Populate the TypeMap. 327 std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 328 unsigned I = 0; 329 for (const auto &T : Types) { 330 TypeMap.insert(std::make_pair(T, I++)); 331 } 332 333 // Populate the SignaturesList and the FctOverloadMap. 334 unsigned CumulativeSignIndex = 0; 335 std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin"); 336 for (const auto *B : Builtins) { 337 StringRef BName = B->getValueAsString("Name"); 338 if (FctOverloadMap.find(BName) == FctOverloadMap.end()) { 339 FctOverloadMap.insert(std::make_pair( 340 BName, std::vector<std::pair<const Record *, unsigned>>{})); 341 } 342 343 auto Signature = B->getValueAsListOfDefs("Signature"); 344 // Reuse signatures to avoid unnecessary duplicates. 345 auto it = 346 std::find_if(SignaturesList.begin(), SignaturesList.end(), 347 [&](const std::pair<std::vector<Record *>, unsigned> &a) { 348 return a.first == Signature; 349 }); 350 unsigned SignIndex; 351 if (it == SignaturesList.end()) { 352 VerifySignature(Signature, B); 353 SignaturesList.push_back(std::make_pair(Signature, CumulativeSignIndex)); 354 SignIndex = CumulativeSignIndex; 355 CumulativeSignIndex += Signature.size(); 356 } else { 357 SignIndex = it->second; 358 } 359 FctOverloadMap[BName].push_back(std::make_pair(B, SignIndex)); 360 } 361 } 362 363 void BuiltinNameEmitter::EmitTypeTable() { 364 OS << "static const OpenCLTypeStruct TypeTable[] = {\n"; 365 for (const auto &T : TypeMap) { 366 const char *AccessQual = 367 StringSwitch<const char *>(T.first->getValueAsString("AccessQualifier")) 368 .Case("RO", "OCLAQ_ReadOnly") 369 .Case("WO", "OCLAQ_WriteOnly") 370 .Case("RW", "OCLAQ_ReadWrite") 371 .Default("OCLAQ_None"); 372 373 OS << " // " << T.second << "\n" 374 << " {OCLT_" << T.first->getValueAsString("Name") << ", " 375 << T.first->getValueAsInt("VecWidth") << ", " 376 << T.first->getValueAsBit("IsPointer") << ", " 377 << T.first->getValueAsBit("IsConst") << ", " 378 << T.first->getValueAsBit("IsVolatile") << ", " 379 << AccessQual << ", " 380 << T.first->getValueAsString("AddrSpace") << "},\n"; 381 } 382 OS << "};\n\n"; 383 } 384 385 void BuiltinNameEmitter::EmitSignatureTable() { 386 // Store a type (e.g. int, float, int2, ...). The type is stored as an index 387 // of a struct OpenCLType table. Multiple entries following each other form a 388 // signature. 389 OS << "static const unsigned SignatureTable[] = {\n"; 390 for (const auto &P : SignaturesList) { 391 OS << " // " << P.second << "\n "; 392 for (const Record *R : P.first) { 393 OS << TypeMap.find(R)->second << ", "; 394 } 395 OS << "\n"; 396 } 397 OS << "};\n\n"; 398 } 399 400 void BuiltinNameEmitter::EmitBuiltinTable() { 401 unsigned Index = 0; 402 403 OS << "static const OpenCLBuiltinStruct BuiltinTable[] = {\n"; 404 for (const auto &FOM : FctOverloadMap) { 405 406 OS << " // " << (Index + 1) << ": " << FOM.first << "\n"; 407 408 for (const auto &Overload : FOM.second) { 409 OS << " { " << Overload.second << ", " 410 << Overload.first->getValueAsListOfDefs("Signature").size() << ", " 411 << Overload.first->getValueAsDef("MinVersion")->getValueAsInt("ID") 412 << ", " 413 << Overload.first->getValueAsDef("MaxVersion")->getValueAsInt("ID") 414 << " },\n"; 415 Index++; 416 } 417 } 418 OS << "};\n\n"; 419 } 420 421 void BuiltinNameEmitter::EmitStringMatcher() { 422 std::vector<StringMatcher::StringPair> ValidBuiltins; 423 unsigned CumulativeIndex = 1; 424 for (auto &i : FctOverloadMap) { 425 auto &Ov = i.second; 426 std::string RetStmt; 427 raw_string_ostream SS(RetStmt); 428 SS << "return std::make_pair(" << CumulativeIndex << ", " << Ov.size() 429 << ");"; 430 SS.flush(); 431 CumulativeIndex += Ov.size(); 432 433 ValidBuiltins.push_back(StringMatcher::StringPair(i.first, RetStmt)); 434 } 435 436 OS << R"( 437 // Find out whether a string matches an existing OpenCL builtin function name. 438 // Returns: A pair <0, 0> if no name matches. 439 // A pair <Index, Len> indexing the BuiltinTable if the name is 440 // matching an OpenCL builtin function. 441 static std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) { 442 443 )"; 444 445 StringMatcher("Name", ValidBuiltins, OS).Emit(0, true); 446 447 OS << " return std::make_pair(0, 0);\n"; 448 OS << "} // isOpenCLBuiltin\n"; 449 } 450 451 void BuiltinNameEmitter::EmitQualTypeFinder() { 452 OS << R"( 453 454 // Convert an OpenCLTypeStruct type to a list of QualTypes. 455 // Generic types represent multiple types and vector sizes, thus a vector 456 // is returned. The conversion is done in two steps: 457 // Step 1: A switch statement fills a vector with scalar base types for the 458 // Cartesian product of (vector sizes) x (types) for generic types, 459 // or a single scalar type for non generic types. 460 // Step 2: Qualifiers and other type properties such as vector size are 461 // applied. 462 static void OCL2Qual(ASTContext &Context, const OpenCLTypeStruct &Ty, 463 llvm::SmallVectorImpl<QualType> &QT) { 464 // Number of scalar types in the GenType. 465 unsigned GenTypeNumTypes; 466 // Pointer to the list of vector sizes for the GenType. 467 llvm::ArrayRef<unsigned> GenVectorSizes; 468 )"; 469 470 // Generate list of vector sizes for each generic type. 471 for (const auto *VectList : Records.getAllDerivedDefinitions("IntList")) { 472 OS << " constexpr unsigned List" 473 << VectList->getValueAsString("Name") << "[] = {"; 474 for (const auto V : VectList->getValueAsListOfInts("List")) { 475 OS << V << ", "; 476 } 477 OS << "};\n"; 478 } 479 480 // Step 1. 481 // Start of switch statement over all types. 482 OS << "\n switch (Ty.ID) {\n"; 483 484 // Switch cases for image types (Image2d, Image3d, ...) 485 std::vector<Record *> ImageTypes = 486 Records.getAllDerivedDefinitions("ImageType"); 487 488 // Map an image type name to its 3 access-qualified types (RO, WO, RW). 489 std::map<StringRef, SmallVector<Record *, 3>> ImageTypesMap; 490 for (auto *IT : ImageTypes) { 491 auto Entry = ImageTypesMap.find(IT->getValueAsString("Name")); 492 if (Entry == ImageTypesMap.end()) { 493 SmallVector<Record *, 3> ImageList; 494 ImageList.push_back(IT); 495 ImageTypesMap.insert( 496 std::make_pair(IT->getValueAsString("Name"), ImageList)); 497 } else { 498 Entry->second.push_back(IT); 499 } 500 } 501 502 // Emit the cases for the image types. For an image type name, there are 3 503 // corresponding QualTypes ("RO", "WO", "RW"). The "AccessQualifier" field 504 // tells which one is needed. Emit a switch statement that puts the 505 // corresponding QualType into "QT". 506 for (const auto &ITE : ImageTypesMap) { 507 OS << " case OCLT_" << ITE.first.str() << ":\n" 508 << " switch (Ty.AccessQualifier) {\n" 509 << " case OCLAQ_None:\n" 510 << " llvm_unreachable(\"Image without access qualifier\");\n"; 511 for (const auto &Image : ITE.second) { 512 OS << StringSwitch<const char *>( 513 Image->getValueAsString("AccessQualifier")) 514 .Case("RO", " case OCLAQ_ReadOnly:\n") 515 .Case("WO", " case OCLAQ_WriteOnly:\n") 516 .Case("RW", " case OCLAQ_ReadWrite:\n") 517 << " QT.push_back(Context." 518 << Image->getValueAsDef("QTName")->getValueAsString("Name") << ");\n" 519 << " break;\n"; 520 } 521 OS << " }\n" 522 << " break;\n"; 523 } 524 525 // Switch cases for generic types. 526 for (const auto *GenType : Records.getAllDerivedDefinitions("GenericType")) { 527 OS << " case OCLT_" << GenType->getValueAsString("Name") << ":\n"; 528 OS << " QT.append({"; 529 530 // Build the Cartesian product of (vector sizes) x (types). Only insert 531 // the plain scalar types for now; other type information such as vector 532 // size and type qualifiers will be added after the switch statement. 533 for (unsigned I = 0; I < GenType->getValueAsDef("VectorList") 534 ->getValueAsListOfInts("List") 535 .size(); 536 I++) { 537 for (const auto *T : 538 GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List")) { 539 OS << "Context." 540 << T->getValueAsDef("QTName")->getValueAsString("Name") << ", "; 541 } 542 } 543 OS << "});\n"; 544 // GenTypeNumTypes is the number of types in the GenType 545 // (e.g. float/double/half). 546 OS << " GenTypeNumTypes = " 547 << GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List") 548 .size() 549 << ";\n"; 550 // GenVectorSizes is the list of vector sizes for this GenType. 551 // QT contains GenTypeNumTypes * #GenVectorSizes elements. 552 OS << " GenVectorSizes = List" 553 << GenType->getValueAsDef("VectorList")->getValueAsString("Name") 554 << ";\n"; 555 OS << " break;\n"; 556 } 557 558 // Switch cases for non generic, non image types (int, int4, float, ...). 559 // Only insert the plain scalar type; vector information and type qualifiers 560 // are added in step 2. 561 std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 562 StringMap<bool> TypesSeen; 563 564 for (const auto *T : Types) { 565 // Check this is not an image type 566 if (ImageTypesMap.find(T->getValueAsString("Name")) != ImageTypesMap.end()) 567 continue; 568 // Check we have not seen this Type 569 if (TypesSeen.find(T->getValueAsString("Name")) != TypesSeen.end()) 570 continue; 571 TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); 572 573 // Check the Type does not have an "abstract" QualType 574 auto QT = T->getValueAsDef("QTName"); 575 if (QT->getValueAsBit("IsAbstract") == 1) 576 continue; 577 // Emit the cases for non generic, non image types. 578 OS << " case OCLT_" << T->getValueAsString("Name") << ":\n"; 579 OS << " QT.push_back(Context." << QT->getValueAsString("Name") 580 << ");\n"; 581 OS << " break;\n"; 582 } 583 584 // End of switch statement. 585 OS << " default:\n" 586 << " llvm_unreachable(\"OpenCL builtin type not handled yet\");\n" 587 << " } // end of switch (Ty.ID)\n\n"; 588 589 // Step 2. 590 // Add ExtVector types if this was a generic type, as the switch statement 591 // above only populated the list with scalar types. This completes the 592 // construction of the Cartesian product of (vector sizes) x (types). 593 OS << " // Construct the different vector types for each generic type.\n"; 594 OS << " if (Ty.ID >= " << TypeList.size() << ") {"; 595 OS << R"( 596 for (unsigned I = 0; I < QT.size(); I++) { 597 // For scalars, size is 1. 598 if (GenVectorSizes[I / GenTypeNumTypes] != 1) { 599 QT[I] = Context.getExtVectorType(QT[I], 600 GenVectorSizes[I / GenTypeNumTypes]); 601 } 602 } 603 } 604 )"; 605 606 // Assign the right attributes to the types (e.g. vector size). 607 OS << R"( 608 // Set vector size for non-generic vector types. 609 if (Ty.VectorWidth > 1) { 610 for (unsigned Index = 0; Index < QT.size(); Index++) { 611 QT[Index] = Context.getExtVectorType(QT[Index], Ty.VectorWidth); 612 } 613 } 614 615 if (Ty.IsVolatile != 0) { 616 for (unsigned Index = 0; Index < QT.size(); Index++) { 617 QT[Index] = Context.getVolatileType(QT[Index]); 618 } 619 } 620 621 if (Ty.IsConst != 0) { 622 for (unsigned Index = 0; Index < QT.size(); Index++) { 623 QT[Index] = Context.getConstType(QT[Index]); 624 } 625 } 626 627 // Transform the type to a pointer as the last step, if necessary. 628 // Builtin functions only have pointers on [const|volatile], no 629 // [const|volatile] pointers, so this is ok to do it as a last step. 630 if (Ty.IsPointer != 0) { 631 for (unsigned Index = 0; Index < QT.size(); Index++) { 632 QT[Index] = Context.getAddrSpaceQualType(QT[Index], Ty.AS); 633 QT[Index] = Context.getPointerType(QT[Index]); 634 } 635 } 636 )"; 637 638 // End of the "OCL2Qual" function. 639 OS << "\n} // OCL2Qual\n"; 640 } 641 642 namespace clang { 643 644 void EmitClangOpenCLBuiltins(RecordKeeper &Records, raw_ostream &OS) { 645 BuiltinNameEmitter NameChecker(Records, OS); 646 NameChecker.Emit(); 647 } 648 649 } // end namespace clang 650