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 | // |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 29 | // * const char *FunctionExtensionTable[] |
| 30 | // List of space-separated OpenCL extensions. A builtin references an |
| 31 | // entry in this table when the builtin requires a particular (set of) |
| 32 | // extension(s) to be enabled. |
| 33 | // |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 34 | // * OpenCLTypeStruct TypeTable[] |
| 35 | // Type information for return types and arguments. |
| 36 | // |
| 37 | // * unsigned SignatureTable[] |
| 38 | // A list of types representing function signatures. Each entry is an index |
| 39 | // into the above TypeTable. Multiple entries following each other form a |
| 40 | // signature, where the first entry is the return type and subsequent |
| 41 | // entries are the argument types. |
| 42 | // |
| 43 | // * OpenCLBuiltinStruct BuiltinTable[] |
| 44 | // Each entry represents one overload of an OpenCL builtin function and |
| 45 | // consists of an index into the SignatureTable and the number of arguments. |
| 46 | // |
| 47 | // * std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) |
| 48 | // Find out whether a string matches an existing OpenCL builtin function |
| 49 | // name and return an index into BuiltinTable and the number of overloads. |
| 50 | // |
| 51 | // * void OCL2Qual(ASTContext&, OpenCLTypeStruct, std::vector<QualType>&) |
| 52 | // Convert an OpenCLTypeStruct type to a list of QualType instances. |
| 53 | // One OpenCLTypeStruct can represent multiple types, primarily when using |
| 54 | // GenTypes. |
| 55 | // |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | |
John McCall | c45f8d4 | 2019-10-01 23:12:57 +0000 | [diff] [blame] | 58 | #include "TableGenBackends.h" |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/MapVector.h" |
| 60 | #include "llvm/ADT/STLExtras.h" |
| 61 | #include "llvm/ADT/SmallString.h" |
| 62 | #include "llvm/ADT/StringExtras.h" |
| 63 | #include "llvm/ADT/StringRef.h" |
| 64 | #include "llvm/ADT/StringSet.h" |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 65 | #include "llvm/ADT/StringSwitch.h" |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 66 | #include "llvm/Support/ErrorHandling.h" |
| 67 | #include "llvm/Support/raw_ostream.h" |
| 68 | #include "llvm/TableGen/Error.h" |
| 69 | #include "llvm/TableGen/Record.h" |
| 70 | #include "llvm/TableGen/StringMatcher.h" |
| 71 | #include "llvm/TableGen/TableGenBackend.h" |
| 72 | #include <set> |
| 73 | |
| 74 | using namespace llvm; |
| 75 | |
| 76 | namespace { |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 77 | |
| 78 | // A list of signatures that are shared by one or more builtin functions. |
| 79 | struct BuiltinTableEntries { |
| 80 | SmallVector<StringRef, 4> Names; |
| 81 | std::vector<std::pair<const Record *, unsigned>> Signatures; |
| 82 | }; |
| 83 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 84 | class BuiltinNameEmitter { |
| 85 | public: |
| 86 | BuiltinNameEmitter(RecordKeeper &Records, raw_ostream &OS) |
| 87 | : Records(Records), OS(OS) {} |
| 88 | |
| 89 | // Entrypoint to generate the functions and structures for checking |
| 90 | // whether a function is an OpenCL builtin function. |
| 91 | void Emit(); |
| 92 | |
| 93 | private: |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 94 | // A list of indices into the builtin function table. |
| 95 | using BuiltinIndexListTy = SmallVector<unsigned, 11>; |
| 96 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 97 | // Contains OpenCL builtin functions and related information, stored as |
| 98 | // Record instances. They are coming from the associated TableGen file. |
| 99 | RecordKeeper &Records; |
| 100 | |
| 101 | // The output file. |
| 102 | raw_ostream &OS; |
| 103 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 104 | // Helper function for BuiltinNameEmitter::EmitDeclarations. Generate enum |
| 105 | // definitions in the Output string parameter, and save their Record instances |
| 106 | // in the List parameter. |
| 107 | // \param Types (in) List containing the Types to extract. |
| 108 | // \param TypesSeen (inout) List containing the Types already extracted. |
| 109 | // \param Output (out) String containing the enums to emit in the output file. |
| 110 | // \param List (out) List containing the extracted Types, except the Types in |
| 111 | // TypesSeen. |
| 112 | void ExtractEnumTypes(std::vector<Record *> &Types, |
| 113 | StringMap<bool> &TypesSeen, std::string &Output, |
| 114 | std::vector<const Record *> &List); |
| 115 | |
| 116 | // Emit the enum or struct used in the generated file. |
| 117 | // Populate the TypeList at the same time. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 118 | void EmitDeclarations(); |
| 119 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 120 | // Parse the Records generated by TableGen to populate the SignaturesList, |
| 121 | // FctOverloadMap and TypeMap. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 122 | void GetOverloads(); |
| 123 | |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 124 | // Compare two lists of signatures and check that e.g. the OpenCL version, |
| 125 | // function attributes, and extension are equal for each signature. |
| 126 | // \param Candidate (in) Entry in the SignatureListMap to check. |
| 127 | // \param SignatureList (in) List of signatures of the considered function. |
| 128 | // \returns true if the two lists of signatures are identical. |
| 129 | bool CanReuseSignature( |
| 130 | BuiltinIndexListTy *Candidate, |
| 131 | std::vector<std::pair<const Record *, unsigned>> &SignatureList); |
| 132 | |
| 133 | // Group functions with the same list of signatures by populating the |
| 134 | // SignatureListMap. |
| 135 | // Some builtin functions have the same list of signatures, for example the |
| 136 | // "sin" and "cos" functions. To save space in the BuiltinTable, the |
| 137 | // "isOpenCLBuiltin" function will have the same output for these two |
| 138 | // function names. |
| 139 | void GroupBySignature(); |
| 140 | |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 141 | // Emit the FunctionExtensionTable that lists all function extensions. |
| 142 | void EmitExtensionTable(); |
| 143 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 144 | // Emit the TypeTable containing all types used by OpenCL builtins. |
| 145 | void EmitTypeTable(); |
| 146 | |
| 147 | // Emit the SignatureTable. This table contains all the possible signatures. |
| 148 | // A signature is stored as a list of indexes of the TypeTable. |
| 149 | // The first index references the return type (mandatory), and the followings |
| 150 | // reference its arguments. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 151 | // E.g.: |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 152 | // 15, 2, 15 can represent a function with the signature: |
| 153 | // int func(float, int) |
| 154 | // The "int" type being at the index 15 in the TypeTable. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 155 | void EmitSignatureTable(); |
| 156 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 157 | // Emit the BuiltinTable table. This table contains all the overloads of |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 158 | // each function, and is a struct OpenCLBuiltinDecl. |
| 159 | // E.g.: |
Sven van Haastregt | ed69faa | 2019-09-19 13:41:51 +0000 | [diff] [blame] | 160 | // // 891 convert_float2_rtn |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 161 | // { 58, 2, 3, 100, 0 }, |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 162 | // This means that the signature of this convert_float2_rtn overload has |
| 163 | // 1 argument (+1 for the return type), stored at index 58 in |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 164 | // the SignatureTable. This prototype requires extension "3" in the |
| 165 | // FunctionExtensionTable. The last two values represent the minimum (1.0) |
| 166 | // and maximum (0, meaning no max version) OpenCL version in which this |
| 167 | // overload is supported. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 168 | void EmitBuiltinTable(); |
| 169 | |
| 170 | // Emit a StringMatcher function to check whether a function name is an |
| 171 | // OpenCL builtin function name. |
| 172 | void EmitStringMatcher(); |
| 173 | |
| 174 | // Emit a function returning the clang QualType instance associated with |
| 175 | // the TableGen Record Type. |
| 176 | void EmitQualTypeFinder(); |
| 177 | |
| 178 | // Contains a list of the available signatures, without the name of the |
| 179 | // function. Each pair consists of a signature and a cumulative index. |
| 180 | // E.g.: <<float, float>, 0>, |
| 181 | // <<float, int, int, 2>>, |
| 182 | // <<float>, 5>, |
| 183 | // ... |
| 184 | // <<double, double>, 35>. |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 185 | std::vector<std::pair<std::vector<Record *>, unsigned>> SignaturesList; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 186 | |
| 187 | // Map the name of a builtin function to its prototypes (instances of the |
| 188 | // TableGen "Builtin" class). |
| 189 | // Each prototype is registered as a pair of: |
| 190 | // <pointer to the "Builtin" instance, |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 191 | // cumulative index of the associated signature in the SignaturesList> |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 192 | // E.g.: The function cos: (float cos(float), double cos(double), ...) |
| 193 | // <"cos", <<ptrToPrototype0, 5>, |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 194 | // <ptrToPrototype1, 35>, |
| 195 | // <ptrToPrototype2, 79>> |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 196 | // ptrToPrototype1 has the following signature: <double, double> |
| 197 | MapVector<StringRef, std::vector<std::pair<const Record *, unsigned>>> |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 198 | FctOverloadMap; |
| 199 | |
| 200 | // Contains the map of OpenCL types to their index in the TypeTable. |
| 201 | MapVector<const Record *, unsigned> TypeMap; |
| 202 | |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 203 | // List of OpenCL function extensions mapping extension strings to |
| 204 | // an index into the FunctionExtensionTable. |
| 205 | StringMap<unsigned> FunctionExtensionIndex; |
| 206 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 207 | // List of OpenCL type names in the same order as in enum OpenCLTypeID. |
| 208 | // This list does not contain generic types. |
| 209 | std::vector<const Record *> TypeList; |
| 210 | |
| 211 | // Same as TypeList, but for generic types only. |
| 212 | std::vector<const Record *> GenTypeList; |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 213 | |
| 214 | // Map an ordered vector of signatures to their original Record instances, |
| 215 | // and to a list of function names that share these signatures. |
| 216 | // |
| 217 | // For example, suppose the "cos" and "sin" functions have only three |
| 218 | // signatures, and these signatures are at index Ix in the SignatureTable: |
| 219 | // cos | sin | Signature | Index |
| 220 | // float cos(float) | float sin(float) | Signature1 | I1 |
| 221 | // double cos(double) | double sin(double) | Signature2 | I2 |
| 222 | // half cos(half) | half sin(half) | Signature3 | I3 |
| 223 | // |
| 224 | // Then we will create a mapping of the vector of signatures: |
| 225 | // SignatureListMap[<I1, I2, I3>] = < |
| 226 | // <"cos", "sin">, |
| 227 | // <Signature1, Signature2, Signature3>> |
| 228 | // The function "tan", having the same signatures, would be mapped to the |
| 229 | // same entry (<I1, I2, I3>). |
| 230 | MapVector<BuiltinIndexListTy *, BuiltinTableEntries> SignatureListMap; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 231 | }; |
| 232 | } // namespace |
| 233 | |
| 234 | void BuiltinNameEmitter::Emit() { |
| 235 | emitSourceFileHeader("OpenCL Builtin handling", OS); |
| 236 | |
| 237 | OS << "#include \"llvm/ADT/StringRef.h\"\n"; |
| 238 | OS << "using namespace clang;\n\n"; |
| 239 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 240 | // Emit enums and structs. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 241 | EmitDeclarations(); |
| 242 | |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 243 | // Parse the Records to populate the internal lists. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 244 | GetOverloads(); |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 245 | GroupBySignature(); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 246 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 247 | // Emit tables. |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 248 | EmitExtensionTable(); |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 249 | EmitTypeTable(); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 250 | EmitSignatureTable(); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 251 | EmitBuiltinTable(); |
| 252 | |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 253 | // Emit functions. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 254 | EmitStringMatcher(); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 255 | EmitQualTypeFinder(); |
| 256 | } |
| 257 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 258 | void BuiltinNameEmitter::ExtractEnumTypes(std::vector<Record *> &Types, |
| 259 | StringMap<bool> &TypesSeen, |
| 260 | std::string &Output, |
| 261 | std::vector<const Record *> &List) { |
| 262 | raw_string_ostream SS(Output); |
| 263 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 264 | for (const auto *T : Types) { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 265 | if (TypesSeen.find(T->getValueAsString("Name")) == TypesSeen.end()) { |
| 266 | SS << " OCLT_" + T->getValueAsString("Name") << ",\n"; |
| 267 | // Save the type names in the same order as their enum value. Note that |
| 268 | // the Record can be a VectorType or something else, only the name is |
| 269 | // important. |
| 270 | List.push_back(T); |
| 271 | TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); |
| 272 | } |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 273 | } |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 274 | SS.flush(); |
| 275 | } |
| 276 | |
| 277 | void BuiltinNameEmitter::EmitDeclarations() { |
| 278 | // Enum of scalar type names (float, int, ...) and generic type sets. |
| 279 | OS << "enum OpenCLTypeID {\n"; |
| 280 | |
| 281 | StringMap<bool> TypesSeen; |
| 282 | std::string GenTypeEnums; |
| 283 | std::string TypeEnums; |
| 284 | |
| 285 | // Extract generic types and non-generic types separately, to keep |
| 286 | // gentypes at the end of the enum which simplifies the special handling |
| 287 | // for gentypes in SemaLookup. |
| 288 | std::vector<Record *> GenTypes = |
| 289 | Records.getAllDerivedDefinitions("GenericType"); |
| 290 | ExtractEnumTypes(GenTypes, TypesSeen, GenTypeEnums, GenTypeList); |
| 291 | |
| 292 | std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); |
| 293 | ExtractEnumTypes(Types, TypesSeen, TypeEnums, TypeList); |
| 294 | |
| 295 | OS << TypeEnums; |
| 296 | OS << GenTypeEnums; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 297 | OS << "};\n"; |
| 298 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 299 | // Structure definitions. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 300 | OS << R"( |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 301 | // Image access qualifier. |
| 302 | enum OpenCLAccessQual : unsigned char { |
| 303 | OCLAQ_None, |
| 304 | OCLAQ_ReadOnly, |
| 305 | OCLAQ_WriteOnly, |
| 306 | OCLAQ_ReadWrite |
| 307 | }; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 308 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 309 | // Represents a return type or argument type. |
| 310 | struct OpenCLTypeStruct { |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 311 | // A type (e.g. float, int, ...). |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 312 | const OpenCLTypeID ID; |
| 313 | // Vector size (if applicable; 0 for scalars and generic types). |
| 314 | const unsigned VectorWidth; |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 315 | // 0 if the type is not a pointer. |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 316 | const bool IsPointer : 1; |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 317 | // 0 if the type is not const. |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 318 | const bool IsConst : 1; |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 319 | // 0 if the type is not volatile. |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 320 | const bool IsVolatile : 1; |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 321 | // Access qualifier. |
| 322 | const OpenCLAccessQual AccessQualifier; |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 323 | // Address space of the pointer (if applicable). |
| 324 | const LangAS AS; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | // One overload of an OpenCL builtin function. |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 328 | struct OpenCLBuiltinStruct { |
| 329 | // Index of the signature in the OpenCLTypeStruct table. |
| 330 | const unsigned SigTableIndex; |
| 331 | // Entries between index SigTableIndex and (SigTableIndex + NumTypes - 1) in |
| 332 | // the SignatureTable represent the complete signature. The first type at |
| 333 | // index SigTableIndex is the return type. |
| 334 | const unsigned NumTypes; |
Sven van Haastregt | 9a8d477 | 2019-11-05 10:07:43 +0000 | [diff] [blame] | 335 | // Function attribute __attribute__((pure)) |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 336 | const bool IsPure : 1; |
Sven van Haastregt | 9a8d477 | 2019-11-05 10:07:43 +0000 | [diff] [blame] | 337 | // Function attribute __attribute__((const)) |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 338 | const bool IsConst : 1; |
Sven van Haastregt | 9a8d477 | 2019-11-05 10:07:43 +0000 | [diff] [blame] | 339 | // Function attribute __attribute__((convergent)) |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 340 | const bool IsConv : 1; |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 341 | // OpenCL extension(s) required for this overload. |
| 342 | const unsigned short Extension; |
Sven van Haastregt | ed69faa | 2019-09-19 13:41:51 +0000 | [diff] [blame] | 343 | // First OpenCL version in which this overload was introduced (e.g. CL20). |
| 344 | const unsigned short MinVersion; |
| 345 | // First OpenCL version in which this overload was removed (e.g. CL20). |
| 346 | const unsigned short MaxVersion; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | )"; |
| 350 | } |
| 351 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 352 | // Verify that the combination of GenTypes in a signature is supported. |
| 353 | // To simplify the logic for creating overloads in SemaLookup, only allow |
| 354 | // a signature to contain different GenTypes if these GenTypes represent |
| 355 | // the same number of actual scalar or vector types. |
| 356 | // |
| 357 | // Exit with a fatal error if an unsupported construct is encountered. |
| 358 | static void VerifySignature(const std::vector<Record *> &Signature, |
| 359 | const Record *BuiltinRec) { |
| 360 | unsigned GenTypeVecSizes = 1; |
| 361 | unsigned GenTypeTypes = 1; |
| 362 | |
| 363 | for (const auto *T : Signature) { |
| 364 | // Check all GenericType arguments in this signature. |
| 365 | if (T->isSubClassOf("GenericType")) { |
| 366 | // Check number of vector sizes. |
| 367 | unsigned NVecSizes = |
| 368 | T->getValueAsDef("VectorList")->getValueAsListOfInts("List").size(); |
| 369 | if (NVecSizes != GenTypeVecSizes && NVecSizes != 1) { |
| 370 | if (GenTypeVecSizes > 1) { |
| 371 | // We already saw a gentype with a different number of vector sizes. |
| 372 | PrintFatalError(BuiltinRec->getLoc(), |
| 373 | "number of vector sizes should be equal or 1 for all gentypes " |
| 374 | "in a declaration"); |
| 375 | } |
| 376 | GenTypeVecSizes = NVecSizes; |
| 377 | } |
| 378 | |
| 379 | // Check number of data types. |
| 380 | unsigned NTypes = |
| 381 | T->getValueAsDef("TypeList")->getValueAsListOfDefs("List").size(); |
| 382 | if (NTypes != GenTypeTypes && NTypes != 1) { |
| 383 | if (GenTypeTypes > 1) { |
| 384 | // We already saw a gentype with a different number of types. |
| 385 | PrintFatalError(BuiltinRec->getLoc(), |
| 386 | "number of types should be equal or 1 for all gentypes " |
| 387 | "in a declaration"); |
| 388 | } |
| 389 | GenTypeTypes = NTypes; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 395 | void BuiltinNameEmitter::GetOverloads() { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 396 | // Populate the TypeMap. |
| 397 | std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); |
| 398 | unsigned I = 0; |
| 399 | for (const auto &T : Types) { |
| 400 | TypeMap.insert(std::make_pair(T, I++)); |
| 401 | } |
| 402 | |
| 403 | // Populate the SignaturesList and the FctOverloadMap. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 404 | unsigned CumulativeSignIndex = 0; |
| 405 | std::vector<Record *> Builtins = Records.getAllDerivedDefinitions("Builtin"); |
| 406 | for (const auto *B : Builtins) { |
| 407 | StringRef BName = B->getValueAsString("Name"); |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 408 | if (FctOverloadMap.find(BName) == FctOverloadMap.end()) { |
| 409 | FctOverloadMap.insert(std::make_pair( |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 410 | BName, std::vector<std::pair<const Record *, unsigned>>{})); |
| 411 | } |
| 412 | |
| 413 | auto Signature = B->getValueAsListOfDefs("Signature"); |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 414 | // Reuse signatures to avoid unnecessary duplicates. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 415 | auto it = |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 416 | std::find_if(SignaturesList.begin(), SignaturesList.end(), |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 417 | [&](const std::pair<std::vector<Record *>, unsigned> &a) { |
| 418 | return a.first == Signature; |
| 419 | }); |
| 420 | unsigned SignIndex; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 421 | if (it == SignaturesList.end()) { |
| 422 | VerifySignature(Signature, B); |
| 423 | SignaturesList.push_back(std::make_pair(Signature, CumulativeSignIndex)); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 424 | SignIndex = CumulativeSignIndex; |
| 425 | CumulativeSignIndex += Signature.size(); |
| 426 | } else { |
| 427 | SignIndex = it->second; |
| 428 | } |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 429 | FctOverloadMap[BName].push_back(std::make_pair(B, SignIndex)); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 433 | void BuiltinNameEmitter::EmitExtensionTable() { |
| 434 | OS << "static const char *FunctionExtensionTable[] = {\n"; |
| 435 | unsigned Index = 0; |
| 436 | std::vector<Record *> FuncExtensions = |
| 437 | Records.getAllDerivedDefinitions("FunctionExtension"); |
| 438 | |
| 439 | for (const auto &FE : FuncExtensions) { |
| 440 | // Emit OpenCL extension table entry. |
| 441 | OS << " // " << Index << ": " << FE->getName() << "\n" |
| 442 | << " \"" << FE->getValueAsString("ExtName") << "\",\n"; |
| 443 | |
| 444 | // Record index of this extension. |
| 445 | FunctionExtensionIndex[FE->getName()] = Index++; |
| 446 | } |
| 447 | OS << "};\n\n"; |
| 448 | } |
| 449 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 450 | void BuiltinNameEmitter::EmitTypeTable() { |
| 451 | OS << "static const OpenCLTypeStruct TypeTable[] = {\n"; |
| 452 | for (const auto &T : TypeMap) { |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 453 | const char *AccessQual = |
| 454 | StringSwitch<const char *>(T.first->getValueAsString("AccessQualifier")) |
| 455 | .Case("RO", "OCLAQ_ReadOnly") |
| 456 | .Case("WO", "OCLAQ_WriteOnly") |
| 457 | .Case("RW", "OCLAQ_ReadWrite") |
| 458 | .Default("OCLAQ_None"); |
| 459 | |
| 460 | OS << " // " << T.second << "\n" |
| 461 | << " {OCLT_" << T.first->getValueAsString("Name") << ", " |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 462 | << T.first->getValueAsInt("VecWidth") << ", " |
| 463 | << T.first->getValueAsBit("IsPointer") << ", " |
| 464 | << T.first->getValueAsBit("IsConst") << ", " |
| 465 | << T.first->getValueAsBit("IsVolatile") << ", " |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 466 | << AccessQual << ", " |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 467 | << T.first->getValueAsString("AddrSpace") << "},\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 468 | } |
| 469 | OS << "};\n\n"; |
| 470 | } |
| 471 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 472 | void BuiltinNameEmitter::EmitSignatureTable() { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 473 | // Store a type (e.g. int, float, int2, ...). The type is stored as an index |
| 474 | // of a struct OpenCLType table. Multiple entries following each other form a |
| 475 | // signature. |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 476 | OS << "static const unsigned short SignatureTable[] = {\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 477 | for (const auto &P : SignaturesList) { |
| 478 | OS << " // " << P.second << "\n "; |
| 479 | for (const Record *R : P.first) { |
Sven van Haastregt | 0fff659 | 2020-02-06 15:08:32 +0000 | [diff] [blame] | 480 | unsigned Entry = TypeMap.find(R)->second; |
| 481 | if (Entry > USHRT_MAX) { |
| 482 | // Report an error when seeing an entry that is too large for the |
| 483 | // current index type (unsigned short). When hitting this, the type |
| 484 | // of SignatureTable will need to be changed. |
| 485 | PrintFatalError("Entry in SignatureTable exceeds limit."); |
| 486 | } |
| 487 | OS << Entry << ", "; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 488 | } |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 489 | OS << "\n"; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 490 | } |
| 491 | OS << "};\n\n"; |
| 492 | } |
| 493 | |
| 494 | void BuiltinNameEmitter::EmitBuiltinTable() { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 495 | unsigned Index = 0; |
| 496 | |
| 497 | OS << "static const OpenCLBuiltinStruct BuiltinTable[] = {\n"; |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 498 | for (const auto &SLM : SignatureListMap) { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 499 | |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 500 | OS << " // " << (Index + 1) << ": "; |
| 501 | for (const auto &Name : SLM.second.Names) { |
| 502 | OS << Name << ", "; |
| 503 | } |
| 504 | OS << "\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 505 | |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 506 | for (const auto &Overload : SLM.second.Signatures) { |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 507 | StringRef ExtName = Overload.first->getValueAsDef("Extension")->getName(); |
Sven van Haastregt | ed69faa | 2019-09-19 13:41:51 +0000 | [diff] [blame] | 508 | OS << " { " << Overload.second << ", " |
| 509 | << Overload.first->getValueAsListOfDefs("Signature").size() << ", " |
Sven van Haastregt | 9a8d477 | 2019-11-05 10:07:43 +0000 | [diff] [blame] | 510 | << (Overload.first->getValueAsBit("IsPure")) << ", " |
| 511 | << (Overload.first->getValueAsBit("IsConst")) << ", " |
| 512 | << (Overload.first->getValueAsBit("IsConv")) << ", " |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 513 | << FunctionExtensionIndex[ExtName] << ", " |
Sven van Haastregt | ed69faa | 2019-09-19 13:41:51 +0000 | [diff] [blame] | 514 | << Overload.first->getValueAsDef("MinVersion")->getValueAsInt("ID") |
| 515 | << ", " |
| 516 | << Overload.first->getValueAsDef("MaxVersion")->getValueAsInt("ID") |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 517 | << " },\n"; |
Sven van Haastregt | ed69faa | 2019-09-19 13:41:51 +0000 | [diff] [blame] | 518 | Index++; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | OS << "};\n\n"; |
| 522 | } |
| 523 | |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 524 | bool BuiltinNameEmitter::CanReuseSignature( |
| 525 | BuiltinIndexListTy *Candidate, |
| 526 | std::vector<std::pair<const Record *, unsigned>> &SignatureList) { |
| 527 | assert(Candidate->size() == SignatureList.size() && |
| 528 | "signature lists should have the same size"); |
| 529 | |
| 530 | auto &CandidateSigs = |
| 531 | SignatureListMap.find(Candidate)->second.Signatures; |
| 532 | for (unsigned Index = 0; Index < Candidate->size(); Index++) { |
| 533 | const Record *Rec = SignatureList[Index].first; |
| 534 | const Record *Rec2 = CandidateSigs[Index].first; |
| 535 | if (Rec->getValueAsBit("IsPure") == Rec2->getValueAsBit("IsPure") && |
| 536 | Rec->getValueAsBit("IsConst") == Rec2->getValueAsBit("IsConst") && |
| 537 | Rec->getValueAsBit("IsConv") == Rec2->getValueAsBit("IsConv") && |
| 538 | Rec->getValueAsDef("MinVersion")->getValueAsInt("ID") == |
| 539 | Rec2->getValueAsDef("MinVersion")->getValueAsInt("ID") && |
| 540 | Rec->getValueAsDef("MaxVersion")->getValueAsInt("ID") == |
| 541 | Rec2->getValueAsDef("MaxVersion")->getValueAsInt("ID") && |
Sven van Haastregt | 308b8b7 | 2019-12-18 10:13:51 +0000 | [diff] [blame] | 542 | Rec->getValueAsDef("Extension")->getName() == |
| 543 | Rec2->getValueAsDef("Extension")->getName()) { |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 544 | return true; |
| 545 | } |
| 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | void BuiltinNameEmitter::GroupBySignature() { |
| 551 | // List of signatures known to be emitted. |
| 552 | std::vector<BuiltinIndexListTy *> KnownSignatures; |
| 553 | |
| 554 | for (auto &Fct : FctOverloadMap) { |
| 555 | bool FoundReusableSig = false; |
| 556 | |
| 557 | // Gather all signatures for the current function. |
| 558 | auto *CurSignatureList = new BuiltinIndexListTy(); |
| 559 | for (const auto &Signature : Fct.second) { |
| 560 | CurSignatureList->push_back(Signature.second); |
| 561 | } |
| 562 | // Sort the list to facilitate future comparisons. |
Benjamin Kramer | 4065e92 | 2020-03-28 19:19:55 +0100 | [diff] [blame] | 563 | llvm::sort(*CurSignatureList); |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 564 | |
| 565 | // Check if we have already seen another function with the same list of |
| 566 | // signatures. If so, just add the name of the function. |
| 567 | for (auto *Candidate : KnownSignatures) { |
| 568 | if (Candidate->size() == CurSignatureList->size() && |
| 569 | *Candidate == *CurSignatureList) { |
| 570 | if (CanReuseSignature(Candidate, Fct.second)) { |
| 571 | SignatureListMap.find(Candidate)->second.Names.push_back(Fct.first); |
| 572 | FoundReusableSig = true; |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (FoundReusableSig) { |
| 578 | delete CurSignatureList; |
| 579 | } else { |
| 580 | // Add a new entry. |
| 581 | SignatureListMap[CurSignatureList] = { |
| 582 | SmallVector<StringRef, 4>(1, Fct.first), Fct.second}; |
| 583 | KnownSignatures.push_back(CurSignatureList); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | for (auto *I : KnownSignatures) { |
| 588 | delete I; |
| 589 | } |
| 590 | } |
| 591 | |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 592 | void BuiltinNameEmitter::EmitStringMatcher() { |
| 593 | std::vector<StringMatcher::StringPair> ValidBuiltins; |
| 594 | unsigned CumulativeIndex = 1; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 595 | |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 596 | for (const auto &SLM : SignatureListMap) { |
| 597 | const auto &Ovl = SLM.second.Signatures; |
| 598 | |
| 599 | // A single signature list may be used by different builtins. Return the |
| 600 | // same <index, length> pair for each of those builtins. |
| 601 | for (const auto &FctName : SLM.second.Names) { |
| 602 | std::string RetStmt; |
| 603 | raw_string_ostream SS(RetStmt); |
| 604 | SS << "return std::make_pair(" << CumulativeIndex << ", " << Ovl.size() |
| 605 | << ");"; |
| 606 | SS.flush(); |
Benjamin Kramer | 735f90f | 2020-01-29 01:49:54 +0100 | [diff] [blame] | 607 | ValidBuiltins.push_back( |
| 608 | StringMatcher::StringPair(std::string(FctName), RetStmt)); |
Sven van Haastregt | 0e56b0f | 2019-11-05 10:16:45 +0000 | [diff] [blame] | 609 | } |
| 610 | CumulativeIndex += Ovl.size(); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | OS << R"( |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 614 | // Find out whether a string matches an existing OpenCL builtin function name. |
| 615 | // Returns: A pair <0, 0> if no name matches. |
| 616 | // A pair <Index, Len> indexing the BuiltinTable if the name is |
| 617 | // matching an OpenCL builtin function. |
| 618 | static std::pair<unsigned, unsigned> isOpenCLBuiltin(llvm::StringRef Name) { |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 619 | |
| 620 | )"; |
| 621 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 622 | StringMatcher("Name", ValidBuiltins, OS).Emit(0, true); |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 623 | |
| 624 | OS << " return std::make_pair(0, 0);\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 625 | OS << "} // isOpenCLBuiltin\n"; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void BuiltinNameEmitter::EmitQualTypeFinder() { |
| 629 | OS << R"( |
| 630 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 631 | // Convert an OpenCLTypeStruct type to a list of QualTypes. |
| 632 | // Generic types represent multiple types and vector sizes, thus a vector |
| 633 | // is returned. The conversion is done in two steps: |
| 634 | // Step 1: A switch statement fills a vector with scalar base types for the |
| 635 | // Cartesian product of (vector sizes) x (types) for generic types, |
| 636 | // or a single scalar type for non generic types. |
| 637 | // Step 2: Qualifiers and other type properties such as vector size are |
| 638 | // applied. |
| 639 | static void OCL2Qual(ASTContext &Context, const OpenCLTypeStruct &Ty, |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 640 | llvm::SmallVectorImpl<QualType> &QT) { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 641 | // Number of scalar types in the GenType. |
| 642 | unsigned GenTypeNumTypes; |
| 643 | // Pointer to the list of vector sizes for the GenType. |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 644 | llvm::ArrayRef<unsigned> GenVectorSizes; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 645 | )"; |
| 646 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 647 | // Generate list of vector sizes for each generic type. |
| 648 | for (const auto *VectList : Records.getAllDerivedDefinitions("IntList")) { |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 649 | OS << " constexpr unsigned List" |
| 650 | << VectList->getValueAsString("Name") << "[] = {"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 651 | for (const auto V : VectList->getValueAsListOfInts("List")) { |
| 652 | OS << V << ", "; |
| 653 | } |
| 654 | OS << "};\n"; |
| 655 | } |
| 656 | |
| 657 | // Step 1. |
| 658 | // Start of switch statement over all types. |
| 659 | OS << "\n switch (Ty.ID) {\n"; |
| 660 | |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 661 | // Switch cases for image types (Image2d, Image3d, ...) |
| 662 | std::vector<Record *> ImageTypes = |
| 663 | Records.getAllDerivedDefinitions("ImageType"); |
| 664 | |
| 665 | // Map an image type name to its 3 access-qualified types (RO, WO, RW). |
| 666 | std::map<StringRef, SmallVector<Record *, 3>> ImageTypesMap; |
| 667 | for (auto *IT : ImageTypes) { |
| 668 | auto Entry = ImageTypesMap.find(IT->getValueAsString("Name")); |
| 669 | if (Entry == ImageTypesMap.end()) { |
| 670 | SmallVector<Record *, 3> ImageList; |
| 671 | ImageList.push_back(IT); |
| 672 | ImageTypesMap.insert( |
| 673 | std::make_pair(IT->getValueAsString("Name"), ImageList)); |
| 674 | } else { |
| 675 | Entry->second.push_back(IT); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | // Emit the cases for the image types. For an image type name, there are 3 |
| 680 | // corresponding QualTypes ("RO", "WO", "RW"). The "AccessQualifier" field |
| 681 | // tells which one is needed. Emit a switch statement that puts the |
| 682 | // corresponding QualType into "QT". |
| 683 | for (const auto &ITE : ImageTypesMap) { |
| 684 | OS << " case OCLT_" << ITE.first.str() << ":\n" |
| 685 | << " switch (Ty.AccessQualifier) {\n" |
| 686 | << " case OCLAQ_None:\n" |
| 687 | << " llvm_unreachable(\"Image without access qualifier\");\n"; |
| 688 | for (const auto &Image : ITE.second) { |
| 689 | OS << StringSwitch<const char *>( |
| 690 | Image->getValueAsString("AccessQualifier")) |
| 691 | .Case("RO", " case OCLAQ_ReadOnly:\n") |
| 692 | .Case("WO", " case OCLAQ_WriteOnly:\n") |
| 693 | .Case("RW", " case OCLAQ_ReadWrite:\n") |
| 694 | << " QT.push_back(Context." |
| 695 | << Image->getValueAsDef("QTName")->getValueAsString("Name") << ");\n" |
| 696 | << " break;\n"; |
| 697 | } |
| 698 | OS << " }\n" |
| 699 | << " break;\n"; |
| 700 | } |
| 701 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 702 | // Switch cases for generic types. |
| 703 | for (const auto *GenType : Records.getAllDerivedDefinitions("GenericType")) { |
| 704 | OS << " case OCLT_" << GenType->getValueAsString("Name") << ":\n"; |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 705 | OS << " QT.append({"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 706 | |
| 707 | // Build the Cartesian product of (vector sizes) x (types). Only insert |
| 708 | // the plain scalar types for now; other type information such as vector |
| 709 | // size and type qualifiers will be added after the switch statement. |
| 710 | for (unsigned I = 0; I < GenType->getValueAsDef("VectorList") |
| 711 | ->getValueAsListOfInts("List") |
| 712 | .size(); |
| 713 | I++) { |
| 714 | for (const auto *T : |
| 715 | GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List")) { |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 716 | OS << "Context." |
| 717 | << T->getValueAsDef("QTName")->getValueAsString("Name") << ", "; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
Sven van Haastregt | 92b2be1 | 2019-09-03 11:23:24 +0000 | [diff] [blame] | 720 | OS << "});\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 721 | // GenTypeNumTypes is the number of types in the GenType |
| 722 | // (e.g. float/double/half). |
| 723 | OS << " GenTypeNumTypes = " |
| 724 | << GenType->getValueAsDef("TypeList")->getValueAsListOfDefs("List") |
| 725 | .size() |
| 726 | << ";\n"; |
| 727 | // GenVectorSizes is the list of vector sizes for this GenType. |
| 728 | // QT contains GenTypeNumTypes * #GenVectorSizes elements. |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 729 | OS << " GenVectorSizes = List" |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 730 | << GenType->getValueAsDef("VectorList")->getValueAsString("Name") |
| 731 | << ";\n"; |
| 732 | OS << " break;\n"; |
| 733 | } |
| 734 | |
| 735 | // Switch cases for non generic, non image types (int, int4, float, ...). |
| 736 | // Only insert the plain scalar type; vector information and type qualifiers |
| 737 | // are added in step 2. |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 738 | std::vector<Record *> Types = Records.getAllDerivedDefinitions("Type"); |
| 739 | StringMap<bool> TypesSeen; |
| 740 | |
| 741 | for (const auto *T : Types) { |
Sven van Haastregt | 988f1e3 | 2019-09-05 10:01:24 +0000 | [diff] [blame] | 742 | // Check this is not an image type |
| 743 | if (ImageTypesMap.find(T->getValueAsString("Name")) != ImageTypesMap.end()) |
| 744 | continue; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 745 | // Check we have not seen this Type |
| 746 | if (TypesSeen.find(T->getValueAsString("Name")) != TypesSeen.end()) |
| 747 | continue; |
| 748 | TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true)); |
| 749 | |
| 750 | // Check the Type does not have an "abstract" QualType |
| 751 | auto QT = T->getValueAsDef("QTName"); |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 752 | if (QT->getValueAsBit("IsAbstract") == 1) |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 753 | continue; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 754 | // Emit the cases for non generic, non image types. |
| 755 | OS << " case OCLT_" << T->getValueAsString("Name") << ":\n"; |
| 756 | OS << " QT.push_back(Context." << QT->getValueAsString("Name") |
| 757 | << ");\n"; |
| 758 | OS << " break;\n"; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 759 | } |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 760 | |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 761 | // End of switch statement. |
Jinsong Ji | e2b8e21 | 2020-01-14 16:21:13 +0000 | [diff] [blame] | 762 | OS << " } // end of switch (Ty.ID)\n\n"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 763 | |
| 764 | // Step 2. |
| 765 | // Add ExtVector types if this was a generic type, as the switch statement |
| 766 | // above only populated the list with scalar types. This completes the |
| 767 | // construction of the Cartesian product of (vector sizes) x (types). |
| 768 | OS << " // Construct the different vector types for each generic type.\n"; |
| 769 | OS << " if (Ty.ID >= " << TypeList.size() << ") {"; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 770 | OS << R"( |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 771 | for (unsigned I = 0; I < QT.size(); I++) { |
| 772 | // For scalars, size is 1. |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 773 | if (GenVectorSizes[I / GenTypeNumTypes] != 1) { |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 774 | QT[I] = Context.getExtVectorType(QT[I], |
Benjamin Kramer | 19651b6 | 2019-08-24 13:04:34 +0000 | [diff] [blame] | 775 | GenVectorSizes[I / GenTypeNumTypes]); |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 778 | } |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 779 | )"; |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 780 | |
| 781 | // Assign the right attributes to the types (e.g. vector size). |
| 782 | OS << R"( |
| 783 | // Set vector size for non-generic vector types. |
| 784 | if (Ty.VectorWidth > 1) { |
| 785 | for (unsigned Index = 0; Index < QT.size(); Index++) { |
| 786 | QT[Index] = Context.getExtVectorType(QT[Index], Ty.VectorWidth); |
| 787 | } |
| 788 | } |
Sven van Haastregt | cc0ba28 | 2019-08-20 12:21:03 +0000 | [diff] [blame] | 789 | |
| 790 | if (Ty.IsVolatile != 0) { |
| 791 | for (unsigned Index = 0; Index < QT.size(); Index++) { |
| 792 | QT[Index] = Context.getVolatileType(QT[Index]); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if (Ty.IsConst != 0) { |
| 797 | for (unsigned Index = 0; Index < QT.size(); Index++) { |
| 798 | QT[Index] = Context.getConstType(QT[Index]); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // Transform the type to a pointer as the last step, if necessary. |
| 803 | // Builtin functions only have pointers on [const|volatile], no |
| 804 | // [const|volatile] pointers, so this is ok to do it as a last step. |
| 805 | if (Ty.IsPointer != 0) { |
| 806 | for (unsigned Index = 0; Index < QT.size(); Index++) { |
| 807 | QT[Index] = Context.getAddrSpaceQualType(QT[Index], Ty.AS); |
| 808 | QT[Index] = Context.getPointerType(QT[Index]); |
| 809 | } |
| 810 | } |
Sven van Haastregt | b21a365 | 2019-08-19 11:56:03 +0000 | [diff] [blame] | 811 | )"; |
| 812 | |
| 813 | // End of the "OCL2Qual" function. |
| 814 | OS << "\n} // OCL2Qual\n"; |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 815 | } |
| 816 | |
John McCall | c45f8d4 | 2019-10-01 23:12:57 +0000 | [diff] [blame] | 817 | void clang::EmitClangOpenCLBuiltins(RecordKeeper &Records, raw_ostream &OS) { |
Sven van Haastregt | 79a222f | 2019-06-03 09:39:11 +0000 | [diff] [blame] | 818 | BuiltinNameEmitter NameChecker(Records, OS); |
| 819 | NameChecker.Emit(); |
| 820 | } |