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