10b57cec5SDimitry Andric //===- ClangOpenCLBuiltinEmitter.cpp - Generate Clang OpenCL Builtin handling 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // The LLVM Compiler Infrastructure 40b57cec5SDimitry Andric // 50b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 60b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 70b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // This tablegen backend emits code for checking whether a function is an 120b57cec5SDimitry Andric // OpenCL builtin function. If so, all overloads of this function are 130b57cec5SDimitry Andric // added to the LookupResult. The generated include file is used by 140b57cec5SDimitry Andric // SemaLookup.cpp 150b57cec5SDimitry Andric // 160b57cec5SDimitry Andric // For a successful lookup of e.g. the "cos" builtin, isOpenCLBuiltin("cos") 170b57cec5SDimitry Andric // returns a pair <Index, Len>. 18a7dea167SDimitry Andric // BuiltinTable[Index] to BuiltinTable[Index + Len] contains the pairs 190b57cec5SDimitry Andric // <SigIndex, SigLen> of the overloads of "cos". 20a7dea167SDimitry Andric // SignatureTable[SigIndex] to SignatureTable[SigIndex + SigLen] contains 21a7dea167SDimitry Andric // one of the signatures of "cos". The SignatureTable entry can be 22a7dea167SDimitry Andric // referenced by other functions, e.g. "sin", to exploit the fact that 23a7dea167SDimitry Andric // many OpenCL builtins share the same signature. 24a7dea167SDimitry Andric // 25a7dea167SDimitry Andric // The file generated by this TableGen emitter contains the following: 26a7dea167SDimitry Andric // 27a7dea167SDimitry Andric // * Structs and enums to represent types and function signatures. 28a7dea167SDimitry Andric // 29480093f4SDimitry Andric // * const char *FunctionExtensionTable[] 30480093f4SDimitry Andric // List of space-separated OpenCL extensions. A builtin references an 31480093f4SDimitry Andric // entry in this table when the builtin requires a particular (set of) 32480093f4SDimitry Andric // extension(s) to be enabled. 33480093f4SDimitry Andric // 34a7dea167SDimitry Andric // * OpenCLTypeStruct TypeTable[] 35a7dea167SDimitry Andric // Type information for return types and arguments. 36a7dea167SDimitry Andric // 37a7dea167SDimitry Andric // * unsigned SignatureTable[] 38a7dea167SDimitry Andric // A list of types representing function signatures. Each entry is an index 39a7dea167SDimitry Andric // into the above TypeTable. Multiple entries following each other form a 40a7dea167SDimitry Andric // signature, where the first entry is the return type and subsequent 41a7dea167SDimitry Andric // entries are the argument types. 42a7dea167SDimitry Andric // 43a7dea167SDimitry Andric // * OpenCLBuiltinStruct BuiltinTable[] 44a7dea167SDimitry Andric // Each entry represents one overload of an OpenCL builtin function and 45a7dea167SDimitry Andric // consists of an index into the SignatureTable and the number of arguments. 46a7dea167SDimitry Andric // 47a7dea167SDimitry Andric // * std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) 48a7dea167SDimitry Andric // Find out whether a string matches an existing OpenCL builtin function 49a7dea167SDimitry Andric // name and return an index into BuiltinTable and the number of overloads. 50a7dea167SDimitry Andric // 51a7dea167SDimitry Andric // * void OCL2Qual(ASTContext&, OpenCLTypeStruct, std::vector<QualType>&) 52a7dea167SDimitry Andric // Convert an OpenCLTypeStruct type to a list of QualType instances. 53a7dea167SDimitry Andric // One OpenCLTypeStruct can represent multiple types, primarily when using 54a7dea167SDimitry Andric // GenTypes. 55a7dea167SDimitry Andric // 560b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 570b57cec5SDimitry Andric 58a7dea167SDimitry Andric #include "TableGenBackends.h" 590b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 600b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 610b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 620b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 630b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 640b57cec5SDimitry Andric #include "llvm/ADT/StringSet.h" 65a7dea167SDimitry Andric #include "llvm/ADT/StringSwitch.h" 660b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 670b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 680b57cec5SDimitry Andric #include "llvm/TableGen/Error.h" 690b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 700b57cec5SDimitry Andric #include "llvm/TableGen/StringMatcher.h" 710b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 720b57cec5SDimitry Andric #include <set> 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric using namespace llvm; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric namespace { 77480093f4SDimitry Andric 78480093f4SDimitry Andric // A list of signatures that are shared by one or more builtin functions. 79480093f4SDimitry Andric struct BuiltinTableEntries { 80480093f4SDimitry Andric SmallVector<StringRef, 4> Names; 81480093f4SDimitry Andric std::vector<std::pair<const Record *, unsigned>> Signatures; 82480093f4SDimitry Andric }; 83480093f4SDimitry Andric 840b57cec5SDimitry Andric class BuiltinNameEmitter { 850b57cec5SDimitry Andric public: 860b57cec5SDimitry Andric BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS) 870b57cec5SDimitry Andric : Records(Records), OS(OS) {} 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // Entrypoint to generate the functions and structures for checking 900b57cec5SDimitry Andric // whether a function is an OpenCL builtin function. 910b57cec5SDimitry Andric void Emit(); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric private: 94480093f4SDimitry Andric // A list of indices into the builtin function table. 95480093f4SDimitry Andric using BuiltinIndexListTy = SmallVector<unsigned, 11>; 96480093f4SDimitry Andric 970b57cec5SDimitry Andric // Contains OpenCL builtin functions and related information, stored as 980b57cec5SDimitry Andric // Record instances. They are coming from the associated TableGen file. 990b57cec5SDimitry Andric RecordKeeper &Records; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // The output file. 1020b57cec5SDimitry Andric raw_ostream &OS; 1030b57cec5SDimitry Andric 104a7dea167SDimitry Andric // Helper function for BuiltinNameEmitter::EmitDeclarations. Generate enum 105a7dea167SDimitry Andric // definitions in the Output string parameter, and save their Record instances 106a7dea167SDimitry Andric // in the List parameter. 107a7dea167SDimitry Andric // \param Types (in) List containing the Types to extract. 108a7dea167SDimitry Andric // \param TypesSeen (inout) List containing the Types already extracted. 109a7dea167SDimitry Andric // \param Output (out) String containing the enums to emit in the output file. 110a7dea167SDimitry Andric // \param List (out) List containing the extracted Types, except the Types in 111a7dea167SDimitry Andric // TypesSeen. 112a7dea167SDimitry Andric void ExtractEnumTypes(std::vector<Record *> &Types, 113a7dea167SDimitry Andric StringMap<bool> &TypesSeen, std::string &Output, 114a7dea167SDimitry Andric std::vector<const Record *> &List); 115a7dea167SDimitry Andric 116a7dea167SDimitry Andric // Emit the enum or struct used in the generated file. 117a7dea167SDimitry Andric // Populate the TypeList at the same time. 1180b57cec5SDimitry Andric void EmitDeclarations(); 1190b57cec5SDimitry Andric 120a7dea167SDimitry Andric // Parse the Records generated by TableGen to populate the SignaturesList, 121a7dea167SDimitry Andric // FctOverloadMap and TypeMap. 1220b57cec5SDimitry Andric void GetOverloads(); 1230b57cec5SDimitry Andric 124480093f4SDimitry Andric // Compare two lists of signatures and check that e.g. the OpenCL version, 125480093f4SDimitry Andric // function attributes, and extension are equal for each signature. 126480093f4SDimitry Andric // \param Candidate (in) Entry in the SignatureListMap to check. 127480093f4SDimitry Andric // \param SignatureList (in) List of signatures of the considered function. 128480093f4SDimitry Andric // \returns true if the two lists of signatures are identical. 129480093f4SDimitry Andric bool CanReuseSignature( 130480093f4SDimitry Andric BuiltinIndexListTy *Candidate, 131480093f4SDimitry Andric std::vector<std::pair<const Record *, unsigned>> &SignatureList); 132480093f4SDimitry Andric 133480093f4SDimitry Andric // Group functions with the same list of signatures by populating the 134480093f4SDimitry Andric // SignatureListMap. 135480093f4SDimitry Andric // Some builtin functions have the same list of signatures, for example the 136480093f4SDimitry Andric // "sin" and "cos" functions. To save space in the BuiltinTable, the 137480093f4SDimitry Andric // "isOpenCLBuiltin" function will have the same output for these two 138480093f4SDimitry Andric // function names. 139480093f4SDimitry Andric void GroupBySignature(); 140480093f4SDimitry Andric 141480093f4SDimitry Andric // Emit the FunctionExtensionTable that lists all function extensions. 142480093f4SDimitry Andric void EmitExtensionTable(); 143480093f4SDimitry Andric 144a7dea167SDimitry Andric // Emit the TypeTable containing all types used by OpenCL builtins. 145a7dea167SDimitry Andric void EmitTypeTable(); 146a7dea167SDimitry Andric 147a7dea167SDimitry Andric // Emit the SignatureTable. This table contains all the possible signatures. 148a7dea167SDimitry Andric // A signature is stored as a list of indexes of the TypeTable. 149a7dea167SDimitry Andric // The first index references the return type (mandatory), and the followings 150a7dea167SDimitry Andric // reference its arguments. 1510b57cec5SDimitry Andric // E.g.: 152a7dea167SDimitry Andric // 15, 2, 15 can represent a function with the signature: 153a7dea167SDimitry Andric // int func(float, int) 154a7dea167SDimitry Andric // The "int" type being at the index 15 in the TypeTable. 1550b57cec5SDimitry Andric void EmitSignatureTable(); 1560b57cec5SDimitry Andric 157a7dea167SDimitry Andric // Emit the BuiltinTable table. This table contains all the overloads of 1580b57cec5SDimitry Andric // each function, and is a struct OpenCLBuiltinDecl. 1590b57cec5SDimitry Andric // E.g.: 160a7dea167SDimitry Andric // // 891 convert_float2_rtn 161480093f4SDimitry Andric // { 58, 2, 3, 100, 0 }, 162a7dea167SDimitry Andric // This means that the signature of this convert_float2_rtn overload has 163a7dea167SDimitry Andric // 1 argument (+1 for the return type), stored at index 58 in 164480093f4SDimitry Andric // the SignatureTable. This prototype requires extension "3" in the 165480093f4SDimitry Andric // FunctionExtensionTable. The last two values represent the minimum (1.0) 166480093f4SDimitry Andric // and maximum (0, meaning no max version) OpenCL version in which this 167480093f4SDimitry Andric // overload is supported. 1680b57cec5SDimitry Andric void EmitBuiltinTable(); 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // Emit a StringMatcher function to check whether a function name is an 1710b57cec5SDimitry Andric // OpenCL builtin function name. 1720b57cec5SDimitry Andric void EmitStringMatcher(); 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric // Emit a function returning the clang QualType instance associated with 1750b57cec5SDimitry Andric // the TableGen Record Type. 1760b57cec5SDimitry Andric void EmitQualTypeFinder(); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric // Contains a list of the available signatures, without the name of the 1790b57cec5SDimitry Andric // function. Each pair consists of a signature and a cumulative index. 1800b57cec5SDimitry Andric // E.g.: <<float, float>, 0>, 1810b57cec5SDimitry Andric // <<float, int, int, 2>>, 1820b57cec5SDimitry Andric // <<float>, 5>, 1830b57cec5SDimitry Andric // ... 1840b57cec5SDimitry Andric // <<double, double>, 35>. 185a7dea167SDimitry Andric std::vector<std::pair<std::vector<Record *>, unsigned>> SignaturesList; 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric // Map the name of a builtin function to its prototypes (instances of the 1880b57cec5SDimitry Andric // TableGen "Builtin" class). 1890b57cec5SDimitry Andric // Each prototype is registered as a pair of: 1900b57cec5SDimitry Andric // <pointer to the "Builtin" instance, 191a7dea167SDimitry Andric // cumulative index of the associated signature in the SignaturesList> 1920b57cec5SDimitry Andric // E.g.: The function cos: (float cos(float), double cos(double), ...) 1930b57cec5SDimitry Andric // <"cos", <<ptrToPrototype0, 5>, 194a7dea167SDimitry Andric // <ptrToPrototype1, 35>, 1950b57cec5SDimitry Andric // <ptrToPrototype2, 79>> 1960b57cec5SDimitry Andric // ptrToPrototype1 has the following signature: <double, double> 1970b57cec5SDimitry Andric MapVector<StringRef, std::vector<std::pair<const Record *, unsigned>>> 198a7dea167SDimitry Andric FctOverloadMap; 199a7dea167SDimitry Andric 200a7dea167SDimitry Andric // Contains the map of OpenCL types to their index in the TypeTable. 201a7dea167SDimitry Andric MapVector<const Record *, unsigned> TypeMap; 202a7dea167SDimitry Andric 203480093f4SDimitry Andric // List of OpenCL function extensions mapping extension strings to 204480093f4SDimitry Andric // an index into the FunctionExtensionTable. 205480093f4SDimitry Andric StringMap<unsigned> FunctionExtensionIndex; 206480093f4SDimitry Andric 207a7dea167SDimitry Andric // List of OpenCL type names in the same order as in enum OpenCLTypeID. 208a7dea167SDimitry Andric // This list does not contain generic types. 209a7dea167SDimitry Andric std::vector<const Record *> TypeList; 210a7dea167SDimitry Andric 211a7dea167SDimitry Andric // Same as TypeList, but for generic types only. 212a7dea167SDimitry Andric std::vector<const Record *> GenTypeList; 213480093f4SDimitry Andric 214480093f4SDimitry Andric // Map an ordered vector of signatures to their original Record instances, 215480093f4SDimitry Andric // and to a list of function names that share these signatures. 216480093f4SDimitry Andric // 217480093f4SDimitry Andric // For example, suppose the "cos" and "sin" functions have only three 218480093f4SDimitry Andric // signatures, and these signatures are at index Ix in the SignatureTable: 219480093f4SDimitry Andric // cos | sin | Signature | Index 220480093f4SDimitry Andric // float cos(float) | float sin(float) | Signature1 | I1 221480093f4SDimitry Andric // double cos(double) | double sin(double) | Signature2 | I2 222480093f4SDimitry Andric // half cos(half) | half sin(half) | Signature3 | I3 223480093f4SDimitry Andric // 224480093f4SDimitry Andric // Then we will create a mapping of the vector of signatures: 225480093f4SDimitry Andric // SignatureListMap[<I1, I2, I3>] = < 226480093f4SDimitry Andric // <"cos", "sin">, 227480093f4SDimitry Andric // <Signature1, Signature2, Signature3>> 228480093f4SDimitry Andric // The function "tan", having the same signatures, would be mapped to the 229480093f4SDimitry Andric // same entry (<I1, I2, I3>). 230480093f4SDimitry Andric MapVector<BuiltinIndexListTy *, BuiltinTableEntries> SignatureListMap; 2310b57cec5SDimitry Andric }; 2320b57cec5SDimitry Andric } // namespace 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric void BuiltinNameEmitter::Emit() { 2350b57cec5SDimitry Andric emitSourceFileHeader("OpenCL Builtin handling", OS); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric OS << "#include \"llvm/ADT/StringRef.h\"\n"; 2380b57cec5SDimitry Andric OS << "using namespace clang;\n\n"; 2390b57cec5SDimitry Andric 240a7dea167SDimitry Andric // Emit enums and structs. 2410b57cec5SDimitry Andric EmitDeclarations(); 2420b57cec5SDimitry Andric 243480093f4SDimitry Andric // Parse the Records to populate the internal lists. 2440b57cec5SDimitry Andric GetOverloads(); 245480093f4SDimitry Andric GroupBySignature(); 2460b57cec5SDimitry Andric 247a7dea167SDimitry Andric // Emit tables. 248480093f4SDimitry Andric EmitExtensionTable(); 249a7dea167SDimitry Andric EmitTypeTable(); 2500b57cec5SDimitry Andric EmitSignatureTable(); 2510b57cec5SDimitry Andric EmitBuiltinTable(); 2520b57cec5SDimitry Andric 253480093f4SDimitry Andric // Emit functions. 2540b57cec5SDimitry Andric EmitStringMatcher(); 2550b57cec5SDimitry Andric EmitQualTypeFinder(); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 258a7dea167SDimitry Andric void BuiltinNameEmitter::ExtractEnumTypes(std::vector<Record *> &Types, 259a7dea167SDimitry Andric StringMap<bool> &TypesSeen, 260a7dea167SDimitry Andric std::string &Output, 261a7dea167SDimitry Andric std::vector<const Record *> &List) { 262a7dea167SDimitry Andric raw_string_ostream SS(Output); 263a7dea167SDimitry Andric 2640b57cec5SDimitry Andric for (const auto *T : Types) { 265a7dea167SDimitry Andric if (TypesSeen.find(T->getValueAsString("Name")) == TypesSeen.end()) { 266a7dea167SDimitry Andric SS << " OCLT_" + T->getValueAsString("Name") << ",\n"; 267a7dea167SDimitry Andric // Save the type names in the same order as their enum value. Note that 268a7dea167SDimitry Andric // the Record can be a VectorType or something else, only the name is 269a7dea167SDimitry Andric // important. 270a7dea167SDimitry Andric List.push_back(T); 2710b57cec5SDimitry Andric TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); 2720b57cec5SDimitry Andric } 273a7dea167SDimitry Andric } 274a7dea167SDimitry Andric SS.flush(); 275a7dea167SDimitry Andric } 276a7dea167SDimitry Andric 277a7dea167SDimitry Andric void BuiltinNameEmitter::EmitDeclarations() { 278a7dea167SDimitry Andric // Enum of scalar type names (float, int, ...) and generic type sets. 279a7dea167SDimitry Andric OS << "enum OpenCLTypeID {\n"; 280a7dea167SDimitry Andric 281a7dea167SDimitry Andric StringMap<bool> TypesSeen; 282a7dea167SDimitry Andric std::string GenTypeEnums; 283a7dea167SDimitry Andric std::string TypeEnums; 284a7dea167SDimitry Andric 285a7dea167SDimitry Andric // Extract generic types and non-generic types separately, to keep 286a7dea167SDimitry Andric // gentypes at the end of the enum which simplifies the special handling 287a7dea167SDimitry Andric // for gentypes in SemaLookup. 288a7dea167SDimitry Andric std::vector<Record *> GenTypes = 289a7dea167SDimitry Andric Records.getAllDerivedDefinitions("GenericType"); 290a7dea167SDimitry Andric ExtractEnumTypes(GenTypes, TypesSeen, GenTypeEnums, GenTypeList); 291a7dea167SDimitry Andric 292a7dea167SDimitry Andric std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 293a7dea167SDimitry Andric ExtractEnumTypes(Types, TypesSeen, TypeEnums, TypeList); 294a7dea167SDimitry Andric 295a7dea167SDimitry Andric OS << TypeEnums; 296a7dea167SDimitry Andric OS << GenTypeEnums; 2970b57cec5SDimitry Andric OS << "};\n"; 2980b57cec5SDimitry Andric 299a7dea167SDimitry Andric // Structure definitions. 3000b57cec5SDimitry Andric OS << R"( 301a7dea167SDimitry Andric // Image access qualifier. 302a7dea167SDimitry Andric enum OpenCLAccessQual : unsigned char { 303a7dea167SDimitry Andric OCLAQ_None, 304a7dea167SDimitry Andric OCLAQ_ReadOnly, 305a7dea167SDimitry Andric OCLAQ_WriteOnly, 306a7dea167SDimitry Andric OCLAQ_ReadWrite 307a7dea167SDimitry Andric }; 3080b57cec5SDimitry Andric 309a7dea167SDimitry Andric // Represents a return type or argument type. 310a7dea167SDimitry Andric struct OpenCLTypeStruct { 311a7dea167SDimitry Andric // A type (e.g. float, int, ...). 312a7dea167SDimitry Andric const OpenCLTypeID ID; 313a7dea167SDimitry Andric // Vector size (if applicable; 0 for scalars and generic types). 314a7dea167SDimitry Andric const unsigned VectorWidth; 315a7dea167SDimitry Andric // 0 if the type is not a pointer. 316*5ffd83dbSDimitry Andric const bool IsPointer : 1; 317a7dea167SDimitry Andric // 0 if the type is not const. 318*5ffd83dbSDimitry Andric const bool IsConst : 1; 319a7dea167SDimitry Andric // 0 if the type is not volatile. 320*5ffd83dbSDimitry Andric const bool IsVolatile : 1; 321a7dea167SDimitry Andric // Access qualifier. 322a7dea167SDimitry Andric const OpenCLAccessQual AccessQualifier; 323a7dea167SDimitry Andric // Address space of the pointer (if applicable). 324a7dea167SDimitry Andric const LangAS AS; 3250b57cec5SDimitry Andric }; 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric // One overload of an OpenCL builtin function. 328a7dea167SDimitry Andric struct OpenCLBuiltinStruct { 329a7dea167SDimitry Andric // Index of the signature in the OpenCLTypeStruct table. 330a7dea167SDimitry Andric const unsigned SigTableIndex; 331a7dea167SDimitry Andric // Entries between index SigTableIndex and (SigTableIndex + NumTypes - 1) in 332a7dea167SDimitry Andric // the SignatureTable represent the complete signature. The first type at 333a7dea167SDimitry Andric // index SigTableIndex is the return type. 334a7dea167SDimitry Andric const unsigned NumTypes; 335480093f4SDimitry Andric // Function attribute __attribute__((pure)) 336*5ffd83dbSDimitry Andric const bool IsPure : 1; 337480093f4SDimitry Andric // Function attribute __attribute__((const)) 338*5ffd83dbSDimitry Andric const bool IsConst : 1; 339480093f4SDimitry Andric // Function attribute __attribute__((convergent)) 340*5ffd83dbSDimitry Andric const bool IsConv : 1; 341480093f4SDimitry Andric // OpenCL extension(s) required for this overload. 342480093f4SDimitry Andric const unsigned short Extension; 343a7dea167SDimitry Andric // First OpenCL version in which this overload was introduced (e.g. CL20). 344a7dea167SDimitry Andric const unsigned short MinVersion; 345a7dea167SDimitry Andric // First OpenCL version in which this overload was removed (e.g. CL20). 346a7dea167SDimitry Andric const unsigned short MaxVersion; 3470b57cec5SDimitry Andric }; 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric )"; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 352a7dea167SDimitry Andric // Verify that the combination of GenTypes in a signature is supported. 353a7dea167SDimitry Andric // To simplify the logic for creating overloads in SemaLookup, only allow 354a7dea167SDimitry Andric // a signature to contain different GenTypes if these GenTypes represent 355a7dea167SDimitry Andric // the same number of actual scalar or vector types. 356a7dea167SDimitry Andric // 357a7dea167SDimitry Andric // Exit with a fatal error if an unsupported construct is encountered. 358a7dea167SDimitry Andric static void VerifySignature(const std::vector<Record *> &Signature, 359a7dea167SDimitry Andric const Record *BuiltinRec) { 360a7dea167SDimitry Andric unsigned GenTypeVecSizes = 1; 361a7dea167SDimitry Andric unsigned GenTypeTypes = 1; 362a7dea167SDimitry Andric 363a7dea167SDimitry Andric for (const auto *T : Signature) { 364a7dea167SDimitry Andric // Check all GenericType arguments in this signature. 365a7dea167SDimitry Andric if (T->isSubClassOf("GenericType")) { 366a7dea167SDimitry Andric // Check number of vector sizes. 367a7dea167SDimitry Andric unsigned NVecSizes = 368a7dea167SDimitry Andric T->getValueAsDef("VectorList")->getValueAsListOfInts("List").size(); 369a7dea167SDimitry Andric if (NVecSizes != GenTypeVecSizes && NVecSizes != 1) { 370a7dea167SDimitry Andric if (GenTypeVecSizes > 1) { 371a7dea167SDimitry Andric // We already saw a gentype with a different number of vector sizes. 372a7dea167SDimitry Andric PrintFatalError(BuiltinRec->getLoc(), 373a7dea167SDimitry Andric "number of vector sizes should be equal or 1 for all gentypes " 374a7dea167SDimitry Andric "in a declaration"); 375a7dea167SDimitry Andric } 376a7dea167SDimitry Andric GenTypeVecSizes = NVecSizes; 377a7dea167SDimitry Andric } 378a7dea167SDimitry Andric 379a7dea167SDimitry Andric // Check number of data types. 380a7dea167SDimitry Andric unsigned NTypes = 381a7dea167SDimitry Andric T->getValueAsDef("TypeList")->getValueAsListOfDefs("List").size(); 382a7dea167SDimitry Andric if (NTypes != GenTypeTypes && NTypes != 1) { 383a7dea167SDimitry Andric if (GenTypeTypes > 1) { 384a7dea167SDimitry Andric // We already saw a gentype with a different number of types. 385a7dea167SDimitry Andric PrintFatalError(BuiltinRec->getLoc(), 386a7dea167SDimitry Andric "number of types should be equal or 1 for all gentypes " 387a7dea167SDimitry Andric "in a declaration"); 388a7dea167SDimitry Andric } 389a7dea167SDimitry Andric GenTypeTypes = NTypes; 390a7dea167SDimitry Andric } 391a7dea167SDimitry Andric } 392a7dea167SDimitry Andric } 393a7dea167SDimitry Andric } 394a7dea167SDimitry Andric 3950b57cec5SDimitry Andric void BuiltinNameEmitter::GetOverloads() { 396a7dea167SDimitry Andric // Populate the TypeMap. 397a7dea167SDimitry Andric std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 398a7dea167SDimitry Andric unsigned I = 0; 399a7dea167SDimitry Andric for (const auto &T : Types) { 400a7dea167SDimitry Andric TypeMap.insert(std::make_pair(T, I++)); 401a7dea167SDimitry Andric } 402a7dea167SDimitry Andric 403a7dea167SDimitry Andric // Populate the SignaturesList and the FctOverloadMap. 4040b57cec5SDimitry Andric unsigned CumulativeSignIndex = 0; 4050b57cec5SDimitry Andric std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin"); 4060b57cec5SDimitry Andric for (const auto *B : Builtins) { 4070b57cec5SDimitry Andric StringRef BName = B->getValueAsString("Name"); 408a7dea167SDimitry Andric if (FctOverloadMap.find(BName) == FctOverloadMap.end()) { 409a7dea167SDimitry Andric FctOverloadMap.insert(std::make_pair( 4100b57cec5SDimitry Andric BName, std::vector<std::pair<const Record *, unsigned>>{})); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric auto Signature = B->getValueAsListOfDefs("Signature"); 414a7dea167SDimitry Andric // Reuse signatures to avoid unnecessary duplicates. 4150b57cec5SDimitry Andric auto it = 416a7dea167SDimitry Andric std::find_if(SignaturesList.begin(), SignaturesList.end(), 4170b57cec5SDimitry Andric [&](const std::pair<std::vector<Record *>, unsigned> &a) { 4180b57cec5SDimitry Andric return a.first == Signature; 4190b57cec5SDimitry Andric }); 4200b57cec5SDimitry Andric unsigned SignIndex; 421a7dea167SDimitry Andric if (it == SignaturesList.end()) { 422a7dea167SDimitry Andric VerifySignature(Signature, B); 423a7dea167SDimitry Andric SignaturesList.push_back(std::make_pair(Signature, CumulativeSignIndex)); 4240b57cec5SDimitry Andric SignIndex = CumulativeSignIndex; 4250b57cec5SDimitry Andric CumulativeSignIndex += Signature.size(); 4260b57cec5SDimitry Andric } else { 4270b57cec5SDimitry Andric SignIndex = it->second; 4280b57cec5SDimitry Andric } 429a7dea167SDimitry Andric FctOverloadMap[BName].push_back(std::make_pair(B, SignIndex)); 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric 433480093f4SDimitry Andric void BuiltinNameEmitter::EmitExtensionTable() { 434480093f4SDimitry Andric OS << "static const char *FunctionExtensionTable[] = {\n"; 435480093f4SDimitry Andric unsigned Index = 0; 436480093f4SDimitry Andric std::vector<Record *> FuncExtensions = 437480093f4SDimitry Andric Records.getAllDerivedDefinitions("FunctionExtension"); 438480093f4SDimitry Andric 439480093f4SDimitry Andric for (const auto &FE : FuncExtensions) { 440480093f4SDimitry Andric // Emit OpenCL extension table entry. 441480093f4SDimitry Andric OS << " // " << Index << ": " << FE->getName() << "\n" 442480093f4SDimitry Andric << " \"" << FE->getValueAsString("ExtName") << "\",\n"; 443480093f4SDimitry Andric 444480093f4SDimitry Andric // Record index of this extension. 445480093f4SDimitry Andric FunctionExtensionIndex[FE->getName()] = Index++; 446480093f4SDimitry Andric } 447480093f4SDimitry Andric OS << "};\n\n"; 448480093f4SDimitry Andric } 449480093f4SDimitry Andric 450a7dea167SDimitry Andric void BuiltinNameEmitter::EmitTypeTable() { 451a7dea167SDimitry Andric OS << "static const OpenCLTypeStruct TypeTable[] = {\n"; 452a7dea167SDimitry Andric for (const auto &T : TypeMap) { 453a7dea167SDimitry Andric const char *AccessQual = 454a7dea167SDimitry Andric StringSwitch<const char *>(T.first->getValueAsString("AccessQualifier")) 455a7dea167SDimitry Andric .Case("RO", "OCLAQ_ReadOnly") 456a7dea167SDimitry Andric .Case("WO", "OCLAQ_WriteOnly") 457a7dea167SDimitry Andric .Case("RW", "OCLAQ_ReadWrite") 458a7dea167SDimitry Andric .Default("OCLAQ_None"); 459a7dea167SDimitry Andric 460a7dea167SDimitry Andric OS << " // " << T.second << "\n" 461a7dea167SDimitry Andric << " {OCLT_" << T.first->getValueAsString("Name") << ", " 462a7dea167SDimitry Andric << T.first->getValueAsInt("VecWidth") << ", " 463a7dea167SDimitry Andric << T.first->getValueAsBit("IsPointer") << ", " 464a7dea167SDimitry Andric << T.first->getValueAsBit("IsConst") << ", " 465a7dea167SDimitry Andric << T.first->getValueAsBit("IsVolatile") << ", " 466a7dea167SDimitry Andric << AccessQual << ", " 467a7dea167SDimitry Andric << T.first->getValueAsString("AddrSpace") << "},\n"; 4680b57cec5SDimitry Andric } 469a7dea167SDimitry Andric OS << "};\n\n"; 470a7dea167SDimitry Andric } 471a7dea167SDimitry Andric 472a7dea167SDimitry Andric void BuiltinNameEmitter::EmitSignatureTable() { 473a7dea167SDimitry Andric // Store a type (e.g. int, float, int2, ...). The type is stored as an index 474a7dea167SDimitry Andric // of a struct OpenCLType table. Multiple entries following each other form a 475a7dea167SDimitry Andric // signature. 476*5ffd83dbSDimitry Andric OS << "static const unsigned short SignatureTable[] = {\n"; 477a7dea167SDimitry Andric for (const auto &P : SignaturesList) { 478a7dea167SDimitry Andric OS << " // " << P.second << "\n "; 479a7dea167SDimitry Andric for (const Record *R : P.first) { 480*5ffd83dbSDimitry Andric unsigned Entry = TypeMap.find(R)->second; 481*5ffd83dbSDimitry Andric if (Entry > USHRT_MAX) { 482*5ffd83dbSDimitry Andric // Report an error when seeing an entry that is too large for the 483*5ffd83dbSDimitry Andric // current index type (unsigned short). When hitting this, the type 484*5ffd83dbSDimitry Andric // of SignatureTable will need to be changed. 485*5ffd83dbSDimitry Andric PrintFatalError("Entry in SignatureTable exceeds limit."); 486*5ffd83dbSDimitry Andric } 487*5ffd83dbSDimitry Andric OS << Entry << ", "; 488a7dea167SDimitry Andric } 489a7dea167SDimitry Andric OS << "\n"; 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric OS << "};\n\n"; 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric void BuiltinNameEmitter::EmitBuiltinTable() { 495a7dea167SDimitry Andric unsigned Index = 0; 496a7dea167SDimitry Andric 497a7dea167SDimitry Andric OS << "static const OpenCLBuiltinStruct BuiltinTable[] = {\n"; 498480093f4SDimitry Andric for (const auto &SLM : SignatureListMap) { 499a7dea167SDimitry Andric 500480093f4SDimitry Andric OS << " // " << (Index + 1) << ": "; 501480093f4SDimitry Andric for (const auto &Name : SLM.second.Names) { 502480093f4SDimitry Andric OS << Name << ", "; 503480093f4SDimitry Andric } 504480093f4SDimitry Andric OS << "\n"; 505a7dea167SDimitry Andric 506480093f4SDimitry Andric for (const auto &Overload : SLM.second.Signatures) { 507480093f4SDimitry Andric StringRef ExtName = Overload.first->getValueAsDef("Extension")->getName(); 508a7dea167SDimitry Andric OS << " { " << Overload.second << ", " 509a7dea167SDimitry Andric << Overload.first->getValueAsListOfDefs("Signature").size() << ", " 510480093f4SDimitry Andric << (Overload.first->getValueAsBit("IsPure")) << ", " 511480093f4SDimitry Andric << (Overload.first->getValueAsBit("IsConst")) << ", " 512480093f4SDimitry Andric << (Overload.first->getValueAsBit("IsConv")) << ", " 513480093f4SDimitry Andric << FunctionExtensionIndex[ExtName] << ", " 514a7dea167SDimitry Andric << Overload.first->getValueAsDef("MinVersion")->getValueAsInt("ID") 515a7dea167SDimitry Andric << ", " 516a7dea167SDimitry Andric << Overload.first->getValueAsDef("MaxVersion")->getValueAsInt("ID") 5170b57cec5SDimitry Andric << " },\n"; 518a7dea167SDimitry Andric Index++; 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric OS << "};\n\n"; 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric 524480093f4SDimitry Andric bool BuiltinNameEmitter::CanReuseSignature( 525480093f4SDimitry Andric BuiltinIndexListTy *Candidate, 526480093f4SDimitry Andric std::vector<std::pair<const Record *, unsigned>> &SignatureList) { 527480093f4SDimitry Andric assert(Candidate->size() == SignatureList.size() && 528480093f4SDimitry Andric "signature lists should have the same size"); 529480093f4SDimitry Andric 530480093f4SDimitry Andric auto &CandidateSigs = 531480093f4SDimitry Andric SignatureListMap.find(Candidate)->second.Signatures; 532480093f4SDimitry Andric for (unsigned Index = 0; Index < Candidate->size(); Index++) { 533480093f4SDimitry Andric const Record *Rec = SignatureList[Index].first; 534480093f4SDimitry Andric const Record *Rec2 = CandidateSigs[Index].first; 535480093f4SDimitry Andric if (Rec->getValueAsBit("IsPure") == Rec2->getValueAsBit("IsPure") && 536480093f4SDimitry Andric Rec->getValueAsBit("IsConst") == Rec2->getValueAsBit("IsConst") && 537480093f4SDimitry Andric Rec->getValueAsBit("IsConv") == Rec2->getValueAsBit("IsConv") && 538480093f4SDimitry Andric Rec->getValueAsDef("MinVersion")->getValueAsInt("ID") == 539480093f4SDimitry Andric Rec2->getValueAsDef("MinVersion")->getValueAsInt("ID") && 540480093f4SDimitry Andric Rec->getValueAsDef("MaxVersion")->getValueAsInt("ID") == 541480093f4SDimitry Andric Rec2->getValueAsDef("MaxVersion")->getValueAsInt("ID") && 542480093f4SDimitry Andric Rec->getValueAsDef("Extension")->getName() == 543480093f4SDimitry Andric Rec2->getValueAsDef("Extension")->getName()) { 544480093f4SDimitry Andric return true; 545480093f4SDimitry Andric } 546480093f4SDimitry Andric } 547480093f4SDimitry Andric return false; 548480093f4SDimitry Andric } 549480093f4SDimitry Andric 550480093f4SDimitry Andric void BuiltinNameEmitter::GroupBySignature() { 551480093f4SDimitry Andric // List of signatures known to be emitted. 552480093f4SDimitry Andric std::vector<BuiltinIndexListTy *> KnownSignatures; 553480093f4SDimitry Andric 554480093f4SDimitry Andric for (auto &Fct : FctOverloadMap) { 555480093f4SDimitry Andric bool FoundReusableSig = false; 556480093f4SDimitry Andric 557480093f4SDimitry Andric // Gather all signatures for the current function. 558480093f4SDimitry Andric auto *CurSignatureList = new BuiltinIndexListTy(); 559480093f4SDimitry Andric for (const auto &Signature : Fct.second) { 560480093f4SDimitry Andric CurSignatureList->push_back(Signature.second); 561480093f4SDimitry Andric } 562480093f4SDimitry Andric // Sort the list to facilitate future comparisons. 563*5ffd83dbSDimitry Andric llvm::sort(*CurSignatureList); 564480093f4SDimitry Andric 565480093f4SDimitry Andric // Check if we have already seen another function with the same list of 566480093f4SDimitry Andric // signatures. If so, just add the name of the function. 567480093f4SDimitry Andric for (auto *Candidate : KnownSignatures) { 568480093f4SDimitry Andric if (Candidate->size() == CurSignatureList->size() && 569480093f4SDimitry Andric *Candidate == *CurSignatureList) { 570480093f4SDimitry Andric if (CanReuseSignature(Candidate, Fct.second)) { 571480093f4SDimitry Andric SignatureListMap.find(Candidate)->second.Names.push_back(Fct.first); 572480093f4SDimitry Andric FoundReusableSig = true; 573480093f4SDimitry Andric } 574480093f4SDimitry Andric } 575480093f4SDimitry Andric } 576480093f4SDimitry Andric 577480093f4SDimitry Andric if (FoundReusableSig) { 578480093f4SDimitry Andric delete CurSignatureList; 579480093f4SDimitry Andric } else { 580480093f4SDimitry Andric // Add a new entry. 581480093f4SDimitry Andric SignatureListMap[CurSignatureList] = { 582480093f4SDimitry Andric SmallVector<StringRef, 4>(1, Fct.first), Fct.second}; 583480093f4SDimitry Andric KnownSignatures.push_back(CurSignatureList); 584480093f4SDimitry Andric } 585480093f4SDimitry Andric } 586480093f4SDimitry Andric 587480093f4SDimitry Andric for (auto *I : KnownSignatures) { 588480093f4SDimitry Andric delete I; 589480093f4SDimitry Andric } 590480093f4SDimitry Andric } 591480093f4SDimitry Andric 5920b57cec5SDimitry Andric void BuiltinNameEmitter::EmitStringMatcher() { 5930b57cec5SDimitry Andric std::vector<StringMatcher::StringPair> ValidBuiltins; 5940b57cec5SDimitry Andric unsigned CumulativeIndex = 1; 595480093f4SDimitry Andric 596480093f4SDimitry Andric for (const auto &SLM : SignatureListMap) { 597480093f4SDimitry Andric const auto &Ovl = SLM.second.Signatures; 598480093f4SDimitry Andric 599480093f4SDimitry Andric // A single signature list may be used by different builtins. Return the 600480093f4SDimitry Andric // same <index, length> pair for each of those builtins. 601480093f4SDimitry Andric for (const auto &FctName : SLM.second.Names) { 6020b57cec5SDimitry Andric std::string RetStmt; 6030b57cec5SDimitry Andric raw_string_ostream SS(RetStmt); 604480093f4SDimitry Andric SS << "return std::make_pair(" << CumulativeIndex << ", " << Ovl.size() 6050b57cec5SDimitry Andric << ");"; 6060b57cec5SDimitry Andric SS.flush(); 607*5ffd83dbSDimitry Andric ValidBuiltins.push_back( 608*5ffd83dbSDimitry Andric StringMatcher::StringPair(std::string(FctName), RetStmt)); 609480093f4SDimitry Andric } 610480093f4SDimitry Andric CumulativeIndex += Ovl.size(); 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric OS << R"( 614a7dea167SDimitry Andric // Find out whether a string matches an existing OpenCL builtin function name. 615a7dea167SDimitry Andric // Returns: A pair <0, 0> if no name matches. 616a7dea167SDimitry Andric // A pair <Index, Len> indexing the BuiltinTable if the name is 617a7dea167SDimitry Andric // matching an OpenCL builtin function. 618a7dea167SDimitry Andric static std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) { 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric )"; 6210b57cec5SDimitry Andric 622a7dea167SDimitry Andric StringMatcher("Name", ValidBuiltins, OS).Emit(0, true); 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric OS << " return std::make_pair(0, 0);\n"; 625a7dea167SDimitry Andric OS << "} // isOpenCLBuiltin\n"; 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric void BuiltinNameEmitter::EmitQualTypeFinder() { 6290b57cec5SDimitry Andric OS << R"( 6300b57cec5SDimitry Andric 631a7dea167SDimitry Andric // Convert an OpenCLTypeStruct type to a list of QualTypes. 632a7dea167SDimitry Andric // Generic types represent multiple types and vector sizes, thus a vector 633a7dea167SDimitry Andric // is returned. The conversion is done in two steps: 634a7dea167SDimitry Andric // Step 1: A switch statement fills a vector with scalar base types for the 635a7dea167SDimitry Andric // Cartesian product of (vector sizes) x (types) for generic types, 636a7dea167SDimitry Andric // or a single scalar type for non generic types. 637a7dea167SDimitry Andric // Step 2: Qualifiers and other type properties such as vector size are 638a7dea167SDimitry Andric // applied. 639a7dea167SDimitry Andric static void OCL2Qual(ASTContext &Context, const OpenCLTypeStruct &Ty, 640a7dea167SDimitry Andric llvm::SmallVectorImpl<QualType> &QT) { 641a7dea167SDimitry Andric // Number of scalar types in the GenType. 642a7dea167SDimitry Andric unsigned GenTypeNumTypes; 643a7dea167SDimitry Andric // Pointer to the list of vector sizes for the GenType. 644a7dea167SDimitry Andric llvm::ArrayRef<unsigned> GenVectorSizes; 6450b57cec5SDimitry Andric )"; 6460b57cec5SDimitry Andric 647a7dea167SDimitry Andric // Generate list of vector sizes for each generic type. 648a7dea167SDimitry Andric for (const auto *VectList : Records.getAllDerivedDefinitions("IntList")) { 649a7dea167SDimitry Andric OS << " constexpr unsigned List" 650a7dea167SDimitry Andric << VectList->getValueAsString("Name") << "[] = {"; 651a7dea167SDimitry Andric for (const auto V : VectList->getValueAsListOfInts("List")) { 652a7dea167SDimitry Andric OS << V << ", "; 653a7dea167SDimitry Andric } 654a7dea167SDimitry Andric OS << "};\n"; 655a7dea167SDimitry Andric } 656a7dea167SDimitry Andric 657a7dea167SDimitry Andric // Step 1. 658a7dea167SDimitry Andric // Start of switch statement over all types. 659a7dea167SDimitry Andric OS << "\n switch (Ty.ID) {\n"; 660a7dea167SDimitry Andric 661a7dea167SDimitry Andric // Switch cases for image types (Image2d, Image3d, ...) 662a7dea167SDimitry Andric std::vector<Record *> ImageTypes = 663a7dea167SDimitry Andric Records.getAllDerivedDefinitions("ImageType"); 664a7dea167SDimitry Andric 665a7dea167SDimitry Andric // Map an image type name to its 3 access-qualified types (RO, WO, RW). 666a7dea167SDimitry Andric std::map<StringRef, SmallVector<Record *, 3>> ImageTypesMap; 667a7dea167SDimitry Andric for (auto *IT : ImageTypes) { 668a7dea167SDimitry Andric auto Entry = ImageTypesMap.find(IT->getValueAsString("Name")); 669a7dea167SDimitry Andric if (Entry == ImageTypesMap.end()) { 670a7dea167SDimitry Andric SmallVector<Record *, 3> ImageList; 671a7dea167SDimitry Andric ImageList.push_back(IT); 672a7dea167SDimitry Andric ImageTypesMap.insert( 673a7dea167SDimitry Andric std::make_pair(IT->getValueAsString("Name"), ImageList)); 674a7dea167SDimitry Andric } else { 675a7dea167SDimitry Andric Entry->second.push_back(IT); 676a7dea167SDimitry Andric } 677a7dea167SDimitry Andric } 678a7dea167SDimitry Andric 679a7dea167SDimitry Andric // Emit the cases for the image types. For an image type name, there are 3 680a7dea167SDimitry Andric // corresponding QualTypes ("RO", "WO", "RW"). The "AccessQualifier" field 681a7dea167SDimitry Andric // tells which one is needed. Emit a switch statement that puts the 682a7dea167SDimitry Andric // corresponding QualType into "QT". 683a7dea167SDimitry Andric for (const auto &ITE : ImageTypesMap) { 684a7dea167SDimitry Andric OS << " case OCLT_" << ITE.first.str() << ":\n" 685a7dea167SDimitry Andric << " switch (Ty.AccessQualifier) {\n" 686a7dea167SDimitry Andric << " case OCLAQ_None:\n" 687a7dea167SDimitry Andric << " llvm_unreachable(\"Image without access qualifier\");\n"; 688a7dea167SDimitry Andric for (const auto &Image : ITE.second) { 689a7dea167SDimitry Andric OS << StringSwitch<const char *>( 690a7dea167SDimitry Andric Image->getValueAsString("AccessQualifier")) 691a7dea167SDimitry Andric .Case("RO", " case OCLAQ_ReadOnly:\n") 692a7dea167SDimitry Andric .Case("WO", " case OCLAQ_WriteOnly:\n") 693a7dea167SDimitry Andric .Case("RW", " case OCLAQ_ReadWrite:\n") 694a7dea167SDimitry Andric << " QT.push_back(Context." 695a7dea167SDimitry Andric << Image->getValueAsDef("QTName")->getValueAsString("Name") << ");\n" 696a7dea167SDimitry Andric << " break;\n"; 697a7dea167SDimitry Andric } 698a7dea167SDimitry Andric OS << " }\n" 699a7dea167SDimitry Andric << " break;\n"; 700a7dea167SDimitry Andric } 701a7dea167SDimitry Andric 702a7dea167SDimitry Andric // Switch cases for generic types. 703a7dea167SDimitry Andric for (const auto *GenType : Records.getAllDerivedDefinitions("GenericType")) { 704a7dea167SDimitry Andric OS << " case OCLT_" << GenType->getValueAsString("Name") << ":\n"; 705a7dea167SDimitry Andric OS << " QT.append({"; 706a7dea167SDimitry Andric 707a7dea167SDimitry Andric // Build the Cartesian product of (vector sizes) x (types). Only insert 708a7dea167SDimitry Andric // the plain scalar types for now; other type information such as vector 709a7dea167SDimitry Andric // size and type qualifiers will be added after the switch statement. 710a7dea167SDimitry Andric for (unsigned I = 0; I < GenType->getValueAsDef("VectorList") 711a7dea167SDimitry Andric ->getValueAsListOfInts("List") 712a7dea167SDimitry Andric .size(); 713a7dea167SDimitry Andric I++) { 714a7dea167SDimitry Andric for (const auto *T : 715a7dea167SDimitry Andric GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List")) { 716a7dea167SDimitry Andric OS << "Context." 717a7dea167SDimitry Andric << T->getValueAsDef("QTName")->getValueAsString("Name") << ", "; 718a7dea167SDimitry Andric } 719a7dea167SDimitry Andric } 720a7dea167SDimitry Andric OS << "});\n"; 721a7dea167SDimitry Andric // GenTypeNumTypes is the number of types in the GenType 722a7dea167SDimitry Andric // (e.g. float/double/half). 723a7dea167SDimitry Andric OS << " GenTypeNumTypes = " 724a7dea167SDimitry Andric << GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List") 725a7dea167SDimitry Andric .size() 726a7dea167SDimitry Andric << ";\n"; 727a7dea167SDimitry Andric // GenVectorSizes is the list of vector sizes for this GenType. 728a7dea167SDimitry Andric // QT contains GenTypeNumTypes * #GenVectorSizes elements. 729a7dea167SDimitry Andric OS << " GenVectorSizes = List" 730a7dea167SDimitry Andric << GenType->getValueAsDef("VectorList")->getValueAsString("Name") 731a7dea167SDimitry Andric << ";\n"; 732a7dea167SDimitry Andric OS << " break;\n"; 733a7dea167SDimitry Andric } 734a7dea167SDimitry Andric 735a7dea167SDimitry Andric // Switch cases for non generic, non image types (int, int4, float, ...). 736a7dea167SDimitry Andric // Only insert the plain scalar type; vector information and type qualifiers 737a7dea167SDimitry Andric // are added in step 2. 7380b57cec5SDimitry Andric std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); 7390b57cec5SDimitry Andric StringMap<bool> TypesSeen; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric for (const auto *T : Types) { 742a7dea167SDimitry Andric // Check this is not an image type 743a7dea167SDimitry Andric if (ImageTypesMap.find(T->getValueAsString("Name")) != ImageTypesMap.end()) 744a7dea167SDimitry Andric continue; 7450b57cec5SDimitry Andric // Check we have not seen this Type 7460b57cec5SDimitry Andric if (TypesSeen.find(T->getValueAsString("Name")) != TypesSeen.end()) 7470b57cec5SDimitry Andric continue; 7480b57cec5SDimitry Andric TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric // Check the Type does not have an "abstract" QualType 7510b57cec5SDimitry Andric auto QT = T->getValueAsDef("QTName"); 752a7dea167SDimitry Andric if (QT->getValueAsBit("IsAbstract") == 1) 7530b57cec5SDimitry Andric continue; 754a7dea167SDimitry Andric // Emit the cases for non generic, non image types. 7550b57cec5SDimitry Andric OS << " case OCLT_" << T->getValueAsString("Name") << ":\n"; 756a7dea167SDimitry Andric OS << " QT.push_back(Context." << QT->getValueAsString("Name") 757a7dea167SDimitry Andric << ");\n"; 7580b57cec5SDimitry Andric OS << " break;\n"; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 761a7dea167SDimitry Andric // End of switch statement. 762480093f4SDimitry Andric OS << " } // end of switch (Ty.ID)\n\n"; 763a7dea167SDimitry Andric 764a7dea167SDimitry Andric // Step 2. 765a7dea167SDimitry Andric // Add ExtVector types if this was a generic type, as the switch statement 766a7dea167SDimitry Andric // above only populated the list with scalar types. This completes the 767a7dea167SDimitry Andric // construction of the Cartesian product of (vector sizes) x (types). 768a7dea167SDimitry Andric OS << " // Construct the different vector types for each generic type.\n"; 769a7dea167SDimitry Andric OS << " if (Ty.ID >= " << TypeList.size() << ") {"; 7700b57cec5SDimitry Andric OS << R"( 771a7dea167SDimitry Andric for (unsigned I = 0; I < QT.size(); I++) { 772a7dea167SDimitry Andric // For scalars, size is 1. 773a7dea167SDimitry Andric if (GenVectorSizes[I / GenTypeNumTypes] != 1) { 774a7dea167SDimitry Andric QT[I] = Context.getExtVectorType(QT[I], 775a7dea167SDimitry Andric GenVectorSizes[I / GenTypeNumTypes]); 7760b57cec5SDimitry Andric } 777a7dea167SDimitry Andric } 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric )"; 780a7dea167SDimitry Andric 781a7dea167SDimitry Andric // Assign the right attributes to the types (e.g. vector size). 782a7dea167SDimitry Andric OS << R"( 783a7dea167SDimitry Andric // Set vector size for non-generic vector types. 784a7dea167SDimitry Andric if (Ty.VectorWidth > 1) { 785a7dea167SDimitry Andric for (unsigned Index = 0; Index < QT.size(); Index++) { 786a7dea167SDimitry Andric QT[Index] = Context.getExtVectorType(QT[Index], Ty.VectorWidth); 787a7dea167SDimitry Andric } 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 790a7dea167SDimitry Andric if (Ty.IsVolatile != 0) { 791a7dea167SDimitry Andric for (unsigned Index = 0; Index < QT.size(); Index++) { 792a7dea167SDimitry Andric QT[Index] = Context.getVolatileType(QT[Index]); 793a7dea167SDimitry Andric } 794a7dea167SDimitry Andric } 7950b57cec5SDimitry Andric 796a7dea167SDimitry Andric if (Ty.IsConst != 0) { 797a7dea167SDimitry Andric for (unsigned Index = 0; Index < QT.size(); Index++) { 798a7dea167SDimitry Andric QT[Index] = Context.getConstType(QT[Index]); 799a7dea167SDimitry Andric } 800a7dea167SDimitry Andric } 801a7dea167SDimitry Andric 802a7dea167SDimitry Andric // Transform the type to a pointer as the last step, if necessary. 803a7dea167SDimitry Andric // Builtin functions only have pointers on [const|volatile], no 804a7dea167SDimitry Andric // [const|volatile] pointers, so this is ok to do it as a last step. 805a7dea167SDimitry Andric if (Ty.IsPointer != 0) { 806a7dea167SDimitry Andric for (unsigned Index = 0; Index < QT.size(); Index++) { 807a7dea167SDimitry Andric QT[Index] = Context.getAddrSpaceQualType(QT[Index], Ty.AS); 808a7dea167SDimitry Andric QT[Index] = Context.getPointerType(QT[Index]); 809a7dea167SDimitry Andric } 810a7dea167SDimitry Andric } 811a7dea167SDimitry Andric )"; 812a7dea167SDimitry Andric 813a7dea167SDimitry Andric // End of the "OCL2Qual" function. 814a7dea167SDimitry Andric OS << "\n} // OCL2Qual\n"; 815a7dea167SDimitry Andric } 816a7dea167SDimitry Andric 817a7dea167SDimitry Andric void clang::EmitClangOpenCLBuiltins(RecordKeeper &Records, raw_ostream &OS) { 8180b57cec5SDimitry Andric BuiltinNameEmitter NameChecker(Records, OS); 8190b57cec5SDimitry Andric NameChecker.Emit(); 8200b57cec5SDimitry Andric } 821