Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 1 | //===- SveEmitter.cpp - Generate arm_sve.h for use with clang -*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This tablegen backend is responsible for emitting arm_sve.h, which includes |
| 10 | // a declaration and definition of each function specified by the ARM C/C++ |
| 11 | // Language Extensions (ACLE). |
| 12 | // |
| 13 | // For details, visit: |
| 14 | // https://developer.arm.com/architectures/system-architectures/software-standards/acle |
| 15 | // |
| 16 | // Each SVE instruction is implemented in terms of 1 or more functions which |
| 17 | // are suffixed with the element type of the input vectors. Functions may be |
| 18 | // implemented in terms of generic vector operations such as +, *, -, etc. or |
| 19 | // by calling a __builtin_-prefixed function which will be handled by clang's |
| 20 | // CodeGen library. |
| 21 | // |
| 22 | // See also the documentation in include/clang/Basic/arm_sve.td. |
| 23 | // |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | #include "llvm/ADT/STLExtras.h" |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringMap.h" |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/ArrayRef.h" |
| 29 | #include "llvm/ADT/StringExtras.h" |
| 30 | #include "llvm/TableGen/Record.h" |
| 31 | #include "llvm/TableGen/Error.h" |
| 32 | #include <string> |
| 33 | #include <sstream> |
| 34 | #include <set> |
| 35 | #include <cctype> |
Eric Fiselier | af2968e | 2020-04-16 18:35:31 -0400 | [diff] [blame] | 36 | #include <tuple> |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 40 | enum ClassKind { |
| 41 | ClassNone, |
| 42 | ClassS, // signed/unsigned, e.g., "_s8", "_u8" suffix |
| 43 | ClassG, // Overloaded name without type suffix |
| 44 | }; |
| 45 | |
| 46 | using TypeSpec = std::string; |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 47 | |
| 48 | namespace { |
| 49 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 50 | class ImmCheck { |
| 51 | unsigned Arg; |
| 52 | unsigned Kind; |
| 53 | unsigned ElementSizeInBits; |
| 54 | |
| 55 | public: |
| 56 | ImmCheck(unsigned Arg, unsigned Kind, unsigned ElementSizeInBits = 0) |
| 57 | : Arg(Arg), Kind(Kind), ElementSizeInBits(ElementSizeInBits) {} |
| 58 | ImmCheck(const ImmCheck &Other) = default; |
| 59 | ~ImmCheck() = default; |
| 60 | |
| 61 | unsigned getArg() const { return Arg; } |
| 62 | unsigned getKind() const { return Kind; } |
| 63 | unsigned getElementSizeInBits() const { return ElementSizeInBits; } |
| 64 | }; |
| 65 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 66 | class SVEType { |
| 67 | TypeSpec TS; |
| 68 | bool Float, Signed, Immediate, Void, Constant, Pointer; |
| 69 | bool DefaultType, IsScalable, Predicate, PredicatePattern, PrefetchOp; |
| 70 | unsigned Bitwidth, ElementBitwidth, NumVectors; |
| 71 | |
Sander de Smalen | 8b409ea | 2020-03-16 10:14:05 +0000 | [diff] [blame] | 72 | public: |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 73 | SVEType() : SVEType(TypeSpec(), 'v') {} |
| 74 | |
| 75 | SVEType(TypeSpec TS, char CharMod) |
| 76 | : TS(TS), Float(false), Signed(true), Immediate(false), Void(false), |
| 77 | Constant(false), Pointer(false), DefaultType(false), IsScalable(true), |
| 78 | Predicate(false), PredicatePattern(false), PrefetchOp(false), |
| 79 | Bitwidth(128), ElementBitwidth(~0U), NumVectors(1) { |
| 80 | if (!TS.empty()) |
| 81 | applyTypespec(); |
| 82 | applyModifier(CharMod); |
| 83 | } |
| 84 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 85 | bool isPointer() const { return Pointer; } |
| 86 | bool isVoidPointer() const { return Pointer && Void; } |
| 87 | bool isSigned() const { return Signed; } |
| 88 | bool isImmediate() const { return Immediate; } |
| 89 | bool isScalar() const { return NumVectors == 0; } |
| 90 | bool isVector() const { return NumVectors > 0; } |
| 91 | bool isScalableVector() const { return isVector() && IsScalable; } |
| 92 | bool isChar() const { return ElementBitwidth == 8; } |
| 93 | bool isVoid() const { return Void & !Pointer; } |
| 94 | bool isDefault() const { return DefaultType; } |
| 95 | bool isFloat() const { return Float; } |
| 96 | bool isInteger() const { return !Float && !Predicate; } |
| 97 | bool isScalarPredicate() const { return !Float && ElementBitwidth == 1; } |
| 98 | bool isPredicateVector() const { return Predicate; } |
| 99 | bool isPredicatePattern() const { return PredicatePattern; } |
| 100 | bool isPrefetchOp() const { return PrefetchOp; } |
| 101 | bool isConstant() const { return Constant; } |
| 102 | unsigned getElementSizeInBits() const { return ElementBitwidth; } |
| 103 | unsigned getNumVectors() const { return NumVectors; } |
| 104 | |
| 105 | unsigned getNumElements() const { |
| 106 | assert(ElementBitwidth != ~0U); |
| 107 | return Bitwidth / ElementBitwidth; |
| 108 | } |
| 109 | unsigned getSizeInBits() const { |
| 110 | return Bitwidth; |
| 111 | } |
| 112 | |
| 113 | /// Return the string representation of a type, which is an encoded |
| 114 | /// string for passing to the BUILTIN() macro in Builtins.def. |
| 115 | std::string builtin_str() const; |
| 116 | |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame] | 117 | /// Return the C/C++ string representation of a type for use in the |
| 118 | /// arm_sve.h header file. |
| 119 | std::string str() const; |
| 120 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 121 | private: |
| 122 | /// Creates the type based on the typespec string in TS. |
| 123 | void applyTypespec(); |
| 124 | |
| 125 | /// Applies a prototype modifier to the type. |
| 126 | void applyModifier(char Mod); |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | class SVEEmitter; |
| 131 | |
| 132 | /// The main grunt class. This represents an instantiation of an intrinsic with |
| 133 | /// a particular typespec and prototype. |
| 134 | class Intrinsic { |
| 135 | /// The unmangled name. |
| 136 | std::string Name; |
| 137 | |
| 138 | /// The name of the corresponding LLVM IR intrinsic. |
| 139 | std::string LLVMName; |
| 140 | |
| 141 | /// Intrinsic prototype. |
| 142 | std::string Proto; |
| 143 | |
| 144 | /// The base type spec for this intrinsic. |
| 145 | TypeSpec BaseTypeSpec; |
| 146 | |
| 147 | /// The base class kind. Most intrinsics use ClassS, which has full type |
| 148 | /// info for integers (_s32/_u32), or ClassG which is used for overloaded |
| 149 | /// intrinsics. |
| 150 | ClassKind Class; |
| 151 | |
| 152 | /// The architectural #ifdef guard. |
| 153 | std::string Guard; |
| 154 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 155 | // The merge suffix such as _m, _x or _z. |
| 156 | std::string MergeSuffix; |
| 157 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 158 | /// The types of return value [0] and parameters [1..]. |
| 159 | std::vector<SVEType> Types; |
| 160 | |
| 161 | /// The "base type", which is VarType('d', BaseTypeSpec). |
| 162 | SVEType BaseType; |
| 163 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 164 | uint64_t Flags; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 165 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 166 | SmallVector<ImmCheck, 2> ImmChecks; |
| 167 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 168 | public: |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 169 | Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy, |
| 170 | StringRef MergeSuffix, uint64_t MemoryElementTy, StringRef LLVMName, |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 171 | uint64_t Flags, ArrayRef<ImmCheck> ImmChecks, TypeSpec BT, |
| 172 | ClassKind Class, SVEEmitter &Emitter, StringRef Guard); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 173 | |
| 174 | ~Intrinsic()=default; |
| 175 | |
| 176 | std::string getName() const { return Name; } |
| 177 | std::string getLLVMName() const { return LLVMName; } |
| 178 | std::string getProto() const { return Proto; } |
| 179 | TypeSpec getBaseTypeSpec() const { return BaseTypeSpec; } |
| 180 | SVEType getBaseType() const { return BaseType; } |
| 181 | |
| 182 | StringRef getGuard() const { return Guard; } |
| 183 | ClassKind getClassKind() const { return Class; } |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 184 | |
| 185 | SVEType getReturnType() const { return Types[0]; } |
| 186 | ArrayRef<SVEType> getTypes() const { return Types; } |
| 187 | SVEType getParamType(unsigned I) const { return Types[I + 1]; } |
| 188 | unsigned getNumParams() const { return Proto.size() - 1; } |
| 189 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 190 | uint64_t getFlags() const { return Flags; } |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 191 | bool isFlagSet(uint64_t Flag) const { return Flags & Flag;} |
| 192 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 193 | ArrayRef<ImmCheck> getImmChecks() const { return ImmChecks; } |
| 194 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 195 | /// Return the type string for a BUILTIN() macro in Builtins.def. |
| 196 | std::string getBuiltinTypeStr(); |
| 197 | |
| 198 | /// Return the name, mangled with type information. The name is mangled for |
| 199 | /// ClassS, so will add type suffixes such as _u32/_s32. |
| 200 | std::string getMangledName() const { return mangleName(ClassS); } |
| 201 | |
| 202 | /// Returns true if the intrinsic is overloaded, in that it should also generate |
| 203 | /// a short form without the type-specifiers, e.g. 'svld1(..)' instead of |
| 204 | /// 'svld1_u32(..)'. |
| 205 | static bool isOverloadedIntrinsic(StringRef Name) { |
| 206 | auto BrOpen = Name.find("["); |
| 207 | auto BrClose = Name.find(']'); |
| 208 | return BrOpen != std::string::npos && BrClose != std::string::npos; |
| 209 | } |
| 210 | |
| 211 | /// Emits the intrinsic declaration to the ostream. |
| 212 | void emitIntrinsic(raw_ostream &OS) const; |
| 213 | |
| 214 | private: |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 215 | std::string getMergeSuffix() const { return MergeSuffix; } |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 216 | std::string mangleName(ClassKind LocalCK) const; |
| 217 | std::string replaceTemplatedArgs(std::string Name, TypeSpec TS, |
| 218 | std::string Proto) const; |
| 219 | }; |
| 220 | |
| 221 | class SVEEmitter { |
| 222 | private: |
| 223 | RecordKeeper &Records; |
| 224 | llvm::StringMap<uint64_t> EltTypes; |
| 225 | llvm::StringMap<uint64_t> MemEltTypes; |
| 226 | llvm::StringMap<uint64_t> FlagTypes; |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 227 | llvm::StringMap<uint64_t> MergeTypes; |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 228 | llvm::StringMap<uint64_t> ImmCheckTypes; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 229 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 230 | public: |
| 231 | SVEEmitter(RecordKeeper &R) : Records(R) { |
| 232 | for (auto *RV : Records.getAllDerivedDefinitions("EltType")) |
| 233 | EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); |
| 234 | for (auto *RV : Records.getAllDerivedDefinitions("MemEltType")) |
| 235 | MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); |
| 236 | for (auto *RV : Records.getAllDerivedDefinitions("FlagType")) |
| 237 | FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 238 | for (auto *RV : Records.getAllDerivedDefinitions("MergeType")) |
| 239 | MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 240 | for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType")) |
| 241 | ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); |
| 242 | } |
| 243 | |
| 244 | /// Returns the enum value for the immcheck type |
| 245 | unsigned getEnumValueForImmCheck(StringRef C) const { |
| 246 | auto It = ImmCheckTypes.find(C); |
| 247 | if (It != ImmCheckTypes.end()) |
| 248 | return It->getValue(); |
| 249 | llvm_unreachable("Unsupported imm check"); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 252 | // Returns the SVETypeFlags for a given value and mask. |
| 253 | uint64_t encodeFlag(uint64_t V, StringRef MaskName) const { |
| 254 | auto It = FlagTypes.find(MaskName); |
| 255 | if (It != FlagTypes.end()) { |
| 256 | uint64_t Mask = It->getValue(); |
| 257 | unsigned Shift = llvm::countTrailingZeros(Mask); |
| 258 | return (V << Shift) & Mask; |
| 259 | } |
| 260 | llvm_unreachable("Unsupported flag"); |
| 261 | } |
| 262 | |
| 263 | // Returns the SVETypeFlags for the given element type. |
| 264 | uint64_t encodeEltType(StringRef EltName) { |
| 265 | auto It = EltTypes.find(EltName); |
| 266 | if (It != EltTypes.end()) |
| 267 | return encodeFlag(It->getValue(), "EltTypeMask"); |
| 268 | llvm_unreachable("Unsupported EltType"); |
| 269 | } |
| 270 | |
| 271 | // Returns the SVETypeFlags for the given memory element type. |
| 272 | uint64_t encodeMemoryElementType(uint64_t MT) { |
| 273 | return encodeFlag(MT, "MemEltTypeMask"); |
| 274 | } |
| 275 | |
| 276 | // Returns the SVETypeFlags for the given merge type. |
| 277 | uint64_t encodeMergeType(uint64_t MT) { |
| 278 | return encodeFlag(MT, "MergeTypeMask"); |
| 279 | } |
| 280 | |
| 281 | // Returns the SVETypeFlags value for the given SVEType. |
| 282 | uint64_t encodeTypeFlags(const SVEType &T); |
| 283 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 284 | /// Emit arm_sve.h. |
| 285 | void createHeader(raw_ostream &o); |
| 286 | |
| 287 | /// Emit all the __builtin prototypes and code needed by Sema. |
| 288 | void createBuiltins(raw_ostream &o); |
| 289 | |
| 290 | /// Emit all the information needed to map builtin -> LLVM IR intrinsic. |
| 291 | void createCodeGenMap(raw_ostream &o); |
| 292 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 293 | /// Emit all the range checks for the immediates. |
| 294 | void createRangeChecks(raw_ostream &o); |
| 295 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 296 | /// Create the SVETypeFlags used in CGBuiltins |
| 297 | void createTypeFlags(raw_ostream &o); |
| 298 | |
| 299 | /// Create intrinsic and add it to \p Out |
| 300 | void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out); |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | } // end anonymous namespace |
| 304 | |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 307 | // Type implementation |
Sander de Smalen | 8b409ea | 2020-03-16 10:14:05 +0000 | [diff] [blame] | 308 | //===----------------------------------------------------------------------===// |
Sander de Smalen | 8b409ea | 2020-03-16 10:14:05 +0000 | [diff] [blame] | 309 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 310 | std::string SVEType::builtin_str() const { |
| 311 | std::string S; |
| 312 | if (isVoid()) |
| 313 | return "v"; |
| 314 | |
| 315 | if (isVoidPointer()) |
| 316 | S += "v"; |
| 317 | else if (!Float) |
| 318 | switch (ElementBitwidth) { |
| 319 | case 1: S += "b"; break; |
| 320 | case 8: S += "c"; break; |
| 321 | case 16: S += "s"; break; |
| 322 | case 32: S += "i"; break; |
| 323 | case 64: S += "Wi"; break; |
| 324 | case 128: S += "LLLi"; break; |
| 325 | default: llvm_unreachable("Unhandled case!"); |
| 326 | } |
| 327 | else |
| 328 | switch (ElementBitwidth) { |
| 329 | case 16: S += "h"; break; |
| 330 | case 32: S += "f"; break; |
| 331 | case 64: S += "d"; break; |
| 332 | default: llvm_unreachable("Unhandled case!"); |
| 333 | } |
| 334 | |
| 335 | if (!isFloat()) { |
| 336 | if ((isChar() || isPointer()) && !isVoidPointer()) { |
| 337 | // Make chars and typed pointers explicitly signed. |
| 338 | if (Signed) |
| 339 | S = "S" + S; |
| 340 | else if (!Signed) |
| 341 | S = "U" + S; |
| 342 | } else if (!isVoidPointer() && !Signed) { |
| 343 | S = "U" + S; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Constant indices are "int", but have the "constant expression" modifier. |
| 348 | if (isImmediate()) { |
| 349 | assert(!isFloat() && "fp immediates are not supported"); |
| 350 | S = "I" + S; |
| 351 | } |
| 352 | |
| 353 | if (isScalar()) { |
| 354 | if (Constant) S += "C"; |
| 355 | if (Pointer) S += "*"; |
| 356 | return S; |
| 357 | } |
| 358 | |
| 359 | assert(isScalableVector() && "Unsupported type"); |
| 360 | return "q" + utostr(getNumElements() * NumVectors) + S; |
| 361 | } |
| 362 | |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame] | 363 | std::string SVEType::str() const { |
| 364 | if (isPredicatePattern()) |
| 365 | return "sv_pattern"; |
| 366 | |
| 367 | if (isPrefetchOp()) |
| 368 | return "sv_prfop"; |
| 369 | |
| 370 | std::string S; |
| 371 | if (Void) |
| 372 | S += "void"; |
| 373 | else { |
| 374 | if (isScalableVector()) |
| 375 | S += "sv"; |
| 376 | if (!Signed && !Float) |
| 377 | S += "u"; |
| 378 | |
| 379 | if (Float) |
| 380 | S += "float"; |
| 381 | else if (isScalarPredicate()) |
| 382 | S += "bool"; |
| 383 | else |
| 384 | S += "int"; |
| 385 | |
| 386 | if (!isScalarPredicate()) |
| 387 | S += utostr(ElementBitwidth); |
| 388 | if (!isScalableVector() && isVector()) |
| 389 | S += "x" + utostr(getNumElements()); |
| 390 | if (NumVectors > 1) |
| 391 | S += "x" + utostr(NumVectors); |
| 392 | S += "_t"; |
| 393 | } |
| 394 | |
| 395 | if (Constant) |
| 396 | S += " const"; |
| 397 | if (Pointer) |
| 398 | S += " *"; |
| 399 | |
| 400 | return S; |
| 401 | } |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 402 | void SVEType::applyTypespec() { |
| 403 | for (char I : TS) { |
| 404 | switch (I) { |
| 405 | case 'P': |
| 406 | Predicate = true; |
| 407 | ElementBitwidth = 1; |
| 408 | break; |
| 409 | case 'U': |
| 410 | Signed = false; |
| 411 | break; |
| 412 | case 'c': |
| 413 | ElementBitwidth = 8; |
| 414 | break; |
| 415 | case 's': |
| 416 | ElementBitwidth = 16; |
| 417 | break; |
| 418 | case 'i': |
| 419 | ElementBitwidth = 32; |
| 420 | break; |
| 421 | case 'l': |
| 422 | ElementBitwidth = 64; |
| 423 | break; |
| 424 | case 'h': |
| 425 | Float = true; |
| 426 | ElementBitwidth = 16; |
| 427 | break; |
| 428 | case 'f': |
| 429 | Float = true; |
| 430 | ElementBitwidth = 32; |
| 431 | break; |
| 432 | case 'd': |
| 433 | Float = true; |
| 434 | ElementBitwidth = 64; |
| 435 | break; |
| 436 | default: |
| 437 | llvm_unreachable("Unhandled type code!"); |
| 438 | } |
| 439 | } |
| 440 | assert(ElementBitwidth != ~0U && "Bad element bitwidth!"); |
| 441 | } |
| 442 | |
| 443 | void SVEType::applyModifier(char Mod) { |
| 444 | switch (Mod) { |
| 445 | case 'v': |
| 446 | Void = true; |
| 447 | break; |
| 448 | case 'd': |
| 449 | DefaultType = true; |
| 450 | break; |
| 451 | case 'c': |
| 452 | Constant = true; |
| 453 | LLVM_FALLTHROUGH; |
| 454 | case 'p': |
| 455 | Pointer = true; |
| 456 | Bitwidth = ElementBitwidth; |
| 457 | NumVectors = 0; |
| 458 | break; |
Sander de Smalen | fc64539 | 2020-04-20 14:57:13 +0100 | [diff] [blame] | 459 | case 'e': |
| 460 | Signed = false; |
| 461 | ElementBitwidth /= 2; |
| 462 | break; |
Sander de Smalen | 515020c | 2020-04-20 14:41:58 +0100 | [diff] [blame] | 463 | case 'h': |
| 464 | ElementBitwidth /= 2; |
| 465 | break; |
Sander de Smalen | fc64539 | 2020-04-20 14:57:13 +0100 | [diff] [blame] | 466 | case 'q': |
| 467 | ElementBitwidth /= 4; |
| 468 | break; |
| 469 | case 'o': |
| 470 | ElementBitwidth *= 4; |
| 471 | break; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 472 | case 'P': |
| 473 | Signed = true; |
| 474 | Float = false; |
| 475 | Predicate = true; |
| 476 | Bitwidth = 16; |
| 477 | ElementBitwidth = 1; |
| 478 | break; |
Sander de Smalen | 515020c | 2020-04-20 14:41:58 +0100 | [diff] [blame] | 479 | case 'u': |
| 480 | Predicate = false; |
| 481 | Signed = false; |
| 482 | Float = false; |
| 483 | break; |
Andrzej Warzynski | 72f5658 | 2020-04-07 11:09:01 +0100 | [diff] [blame^] | 484 | case 'x': |
| 485 | Predicate = false; |
| 486 | Signed = true; |
| 487 | Float = false; |
| 488 | break; |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 489 | case 'i': |
| 490 | Predicate = false; |
| 491 | Float = false; |
| 492 | ElementBitwidth = Bitwidth = 64; |
| 493 | NumVectors = 0; |
| 494 | Signed = false; |
| 495 | Immediate = true; |
| 496 | break; |
| 497 | case 'I': |
| 498 | Predicate = false; |
| 499 | Float = false; |
| 500 | ElementBitwidth = Bitwidth = 32; |
| 501 | NumVectors = 0; |
| 502 | Signed = true; |
| 503 | Immediate = true; |
| 504 | PredicatePattern = true; |
| 505 | break; |
Sander de Smalen | 17a68c6 | 2020-04-14 13:17:52 +0100 | [diff] [blame] | 506 | case 'l': |
| 507 | Predicate = false; |
| 508 | Signed = true; |
| 509 | Float = false; |
| 510 | ElementBitwidth = Bitwidth = 64; |
| 511 | NumVectors = 0; |
| 512 | break; |
| 513 | case 'S': |
| 514 | Constant = true; |
| 515 | Pointer = true; |
| 516 | ElementBitwidth = Bitwidth = 8; |
| 517 | NumVectors = 0; |
| 518 | Signed = true; |
| 519 | break; |
| 520 | case 'W': |
| 521 | Constant = true; |
| 522 | Pointer = true; |
| 523 | ElementBitwidth = Bitwidth = 8; |
| 524 | NumVectors = 0; |
| 525 | Signed = false; |
| 526 | break; |
| 527 | case 'T': |
| 528 | Constant = true; |
| 529 | Pointer = true; |
| 530 | ElementBitwidth = Bitwidth = 16; |
| 531 | NumVectors = 0; |
| 532 | Signed = true; |
| 533 | break; |
| 534 | case 'X': |
| 535 | Constant = true; |
| 536 | Pointer = true; |
| 537 | ElementBitwidth = Bitwidth = 16; |
| 538 | NumVectors = 0; |
| 539 | Signed = false; |
| 540 | break; |
| 541 | case 'Y': |
| 542 | Constant = true; |
| 543 | Pointer = true; |
| 544 | ElementBitwidth = Bitwidth = 32; |
| 545 | NumVectors = 0; |
| 546 | Signed = false; |
| 547 | break; |
| 548 | case 'U': |
| 549 | Constant = true; |
| 550 | Pointer = true; |
| 551 | ElementBitwidth = Bitwidth = 32; |
| 552 | NumVectors = 0; |
| 553 | Signed = true; |
| 554 | break; |
| 555 | case 'A': |
| 556 | Pointer = true; |
| 557 | ElementBitwidth = Bitwidth = 8; |
| 558 | NumVectors = 0; |
| 559 | Signed = true; |
| 560 | break; |
| 561 | case 'B': |
| 562 | Pointer = true; |
| 563 | ElementBitwidth = Bitwidth = 16; |
| 564 | NumVectors = 0; |
| 565 | Signed = true; |
| 566 | break; |
| 567 | case 'C': |
| 568 | Pointer = true; |
| 569 | ElementBitwidth = Bitwidth = 32; |
| 570 | NumVectors = 0; |
| 571 | Signed = true; |
| 572 | break; |
| 573 | case 'D': |
| 574 | Pointer = true; |
| 575 | ElementBitwidth = Bitwidth = 64; |
| 576 | NumVectors = 0; |
| 577 | Signed = true; |
| 578 | break; |
| 579 | case 'E': |
| 580 | Pointer = true; |
| 581 | ElementBitwidth = Bitwidth = 8; |
| 582 | NumVectors = 0; |
| 583 | Signed = false; |
| 584 | break; |
| 585 | case 'F': |
| 586 | Pointer = true; |
| 587 | ElementBitwidth = Bitwidth = 16; |
| 588 | NumVectors = 0; |
| 589 | Signed = false; |
| 590 | break; |
| 591 | case 'G': |
| 592 | Pointer = true; |
| 593 | ElementBitwidth = Bitwidth = 32; |
| 594 | NumVectors = 0; |
| 595 | Signed = false; |
| 596 | break; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 597 | default: |
| 598 | llvm_unreachable("Unhandled character!"); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | |
| 603 | //===----------------------------------------------------------------------===// |
| 604 | // Intrinsic implementation |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 607 | Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy, |
| 608 | StringRef MergeSuffix, uint64_t MemoryElementTy, |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 609 | StringRef LLVMName, uint64_t Flags, |
| 610 | ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class, |
| 611 | SVEEmitter &Emitter, StringRef Guard) |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 612 | : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()), |
| 613 | BaseTypeSpec(BT), Class(Class), Guard(Guard.str()), |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 614 | MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags), |
| 615 | ImmChecks(Checks.begin(), Checks.end()) { |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 616 | |
| 617 | // Types[0] is the return value. |
| 618 | for (unsigned I = 0; I < Proto.size(); ++I) { |
| 619 | SVEType T(BaseTypeSpec, Proto[I]); |
| 620 | Types.push_back(T); |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 621 | |
| 622 | // Add range checks for immediates |
| 623 | if (I > 0) { |
| 624 | if (T.isPredicatePattern()) |
| 625 | ImmChecks.emplace_back( |
| 626 | I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31")); |
| 627 | } |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Set flags based on properties |
| 631 | this->Flags |= Emitter.encodeTypeFlags(BaseType); |
| 632 | this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy); |
| 633 | this->Flags |= Emitter.encodeMergeType(MergeTy); |
| 634 | } |
| 635 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 636 | std::string Intrinsic::getBuiltinTypeStr() { |
| 637 | std::string S; |
| 638 | |
| 639 | SVEType RetT = getReturnType(); |
| 640 | // Since the return value must be one type, return a vector type of the |
| 641 | // appropriate width which we will bitcast. An exception is made for |
| 642 | // returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 643 | // fashion, storing them to a pointer arg. |
| 644 | if (RetT.getNumVectors() > 1) { |
| 645 | S += "vv*"; // void result with void* first argument |
| 646 | } else |
| 647 | S += RetT.builtin_str(); |
| 648 | |
| 649 | for (unsigned I = 0; I < getNumParams(); ++I) |
| 650 | S += getParamType(I).builtin_str(); |
| 651 | |
| 652 | return S; |
| 653 | } |
| 654 | |
| 655 | std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS, |
| 656 | std::string Proto) const { |
| 657 | std::string Ret = Name; |
| 658 | while (Ret.find('{') != std::string::npos) { |
| 659 | size_t Pos = Ret.find('{'); |
| 660 | size_t End = Ret.find('}'); |
| 661 | unsigned NumChars = End - Pos + 1; |
| 662 | assert(NumChars == 3 && "Unexpected template argument"); |
| 663 | |
| 664 | SVEType T; |
| 665 | char C = Ret[Pos+1]; |
| 666 | switch(C) { |
| 667 | default: |
| 668 | llvm_unreachable("Unknown predication specifier"); |
| 669 | case 'd': |
| 670 | T = SVEType(TS, 'd'); |
| 671 | break; |
| 672 | case '0': |
| 673 | case '1': |
| 674 | case '2': |
| 675 | case '3': |
| 676 | T = SVEType(TS, Proto[C - '0']); |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | // Replace templated arg with the right suffix (e.g. u32) |
| 681 | std::string TypeCode; |
| 682 | if (T.isInteger()) |
| 683 | TypeCode = T.isSigned() ? 's' : 'u'; |
| 684 | else if (T.isPredicateVector()) |
| 685 | TypeCode = 'b'; |
| 686 | else |
| 687 | TypeCode = 'f'; |
| 688 | Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits())); |
| 689 | } |
| 690 | |
| 691 | return Ret; |
| 692 | } |
| 693 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 694 | std::string Intrinsic::mangleName(ClassKind LocalCK) const { |
| 695 | std::string S = getName(); |
| 696 | |
| 697 | if (LocalCK == ClassG) { |
| 698 | // Remove the square brackets and everything in between. |
| 699 | while (S.find("[") != std::string::npos) { |
| 700 | auto Start = S.find("["); |
| 701 | auto End = S.find(']'); |
| 702 | S.erase(Start, (End-Start)+1); |
| 703 | } |
| 704 | } else { |
| 705 | // Remove the square brackets. |
| 706 | while (S.find("[") != std::string::npos) { |
| 707 | auto BrPos = S.find('['); |
| 708 | if (BrPos != std::string::npos) |
| 709 | S.erase(BrPos, 1); |
| 710 | BrPos = S.find(']'); |
| 711 | if (BrPos != std::string::npos) |
| 712 | S.erase(BrPos, 1); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // Replace all {d} like expressions with e.g. 'u32' |
| 717 | return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) + |
| 718 | getMergeSuffix(); |
| 719 | } |
| 720 | |
| 721 | void Intrinsic::emitIntrinsic(raw_ostream &OS) const { |
| 722 | // Use the preprocessor to |
| 723 | if (getClassKind() != ClassG || getProto().size() <= 1) { |
| 724 | OS << "#define " << mangleName(getClassKind()) |
| 725 | << "(...) __builtin_sve_" << mangleName(ClassS) |
| 726 | << "(__VA_ARGS__)\n"; |
| 727 | } else { |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame] | 728 | std::string FullName = mangleName(ClassS); |
| 729 | std::string ProtoName = mangleName(ClassG); |
| 730 | |
| 731 | OS << "__aio __attribute__((__clang_arm_builtin_alias(" |
| 732 | << "__builtin_sve_" << FullName << ")))\n"; |
| 733 | |
| 734 | OS << getTypes()[0].str() << " " << ProtoName << "("; |
| 735 | for (unsigned I = 0; I < getTypes().size() - 1; ++I) { |
| 736 | if (I != 0) |
| 737 | OS << ", "; |
| 738 | OS << getTypes()[I + 1].str(); |
| 739 | } |
| 740 | OS << ");\n"; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | //===----------------------------------------------------------------------===// |
| 745 | // SVEEmitter implementation |
| 746 | //===----------------------------------------------------------------------===// |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 747 | uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) { |
| 748 | if (T.isFloat()) { |
| 749 | switch (T.getElementSizeInBits()) { |
| 750 | case 16: |
| 751 | return encodeEltType("EltTyFloat16"); |
| 752 | case 32: |
| 753 | return encodeEltType("EltTyFloat32"); |
| 754 | case 64: |
| 755 | return encodeEltType("EltTyFloat64"); |
| 756 | default: |
| 757 | llvm_unreachable("Unhandled float element bitwidth!"); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | if (T.isPredicateVector()) { |
| 762 | switch (T.getElementSizeInBits()) { |
| 763 | case 8: |
| 764 | return encodeEltType("EltTyBool8"); |
| 765 | case 16: |
| 766 | return encodeEltType("EltTyBool16"); |
| 767 | case 32: |
| 768 | return encodeEltType("EltTyBool32"); |
| 769 | case 64: |
| 770 | return encodeEltType("EltTyBool64"); |
| 771 | default: |
| 772 | llvm_unreachable("Unhandled predicate element bitwidth!"); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | switch (T.getElementSizeInBits()) { |
| 777 | case 8: |
| 778 | return encodeEltType("EltTyInt8"); |
| 779 | case 16: |
| 780 | return encodeEltType("EltTyInt16"); |
| 781 | case 32: |
| 782 | return encodeEltType("EltTyInt32"); |
| 783 | case 64: |
| 784 | return encodeEltType("EltTyInt64"); |
| 785 | default: |
| 786 | llvm_unreachable("Unhandled integer element bitwidth!"); |
| 787 | } |
| 788 | } |
| 789 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 790 | void SVEEmitter::createIntrinsic( |
| 791 | Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) { |
| 792 | StringRef Name = R->getValueAsString("Name"); |
| 793 | StringRef Proto = R->getValueAsString("Prototype"); |
| 794 | StringRef Types = R->getValueAsString("Types"); |
| 795 | StringRef Guard = R->getValueAsString("ArchGuard"); |
| 796 | StringRef LLVMName = R->getValueAsString("LLVMIntrinsic"); |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 797 | uint64_t Merge = R->getValueAsInt("Merge"); |
| 798 | StringRef MergeSuffix = R->getValueAsString("MergeSuffix"); |
| 799 | uint64_t MemEltType = R->getValueAsInt("MemEltType"); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 800 | std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags"); |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 801 | std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks"); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 802 | |
| 803 | int64_t Flags = 0; |
| 804 | for (auto FlagRec : FlagsList) |
| 805 | Flags |= FlagRec->getValueAsInt("Value"); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 806 | |
| 807 | // Extract type specs from string |
| 808 | SmallVector<TypeSpec, 8> TypeSpecs; |
| 809 | TypeSpec Acc; |
| 810 | for (char I : Types) { |
| 811 | Acc.push_back(I); |
| 812 | if (islower(I)) { |
| 813 | TypeSpecs.push_back(TypeSpec(Acc)); |
| 814 | Acc.clear(); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | // Remove duplicate type specs. |
Benjamin Kramer | 4065e92 | 2020-03-28 19:19:55 +0100 | [diff] [blame] | 819 | llvm::sort(TypeSpecs); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 820 | TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()), |
| 821 | TypeSpecs.end()); |
| 822 | |
| 823 | // Create an Intrinsic for each type spec. |
| 824 | for (auto TS : TypeSpecs) { |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 825 | // Collate a list of range/option checks for the immediates. |
| 826 | SmallVector<ImmCheck, 2> ImmChecks; |
| 827 | for (auto *R : ImmCheckList) { |
Christopher Tetreault | 464a069 | 2020-04-15 15:16:17 -0700 | [diff] [blame] | 828 | int64_t Arg = R->getValueAsInt("Arg"); |
| 829 | int64_t EltSizeArg = R->getValueAsInt("EltSizeArg"); |
| 830 | int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value"); |
| 831 | assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative"); |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 832 | |
| 833 | unsigned ElementSizeInBits = 0; |
| 834 | if (EltSizeArg >= 0) |
| 835 | ElementSizeInBits = |
| 836 | SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1]) |
| 837 | .getElementSizeInBits(); |
| 838 | ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits)); |
| 839 | } |
| 840 | |
| 841 | Out.push_back(std::make_unique<Intrinsic>( |
| 842 | Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks, |
| 843 | TS, ClassS, *this, Guard)); |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame] | 844 | |
| 845 | // Also generate the short-form (e.g. svadd_m) for the given type-spec. |
| 846 | if (Intrinsic::isOverloadedIntrinsic(Name)) |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 847 | Out.push_back(std::make_unique<Intrinsic>( |
| 848 | Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, |
| 849 | ImmChecks, TS, ClassG, *this, Guard)); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | void SVEEmitter::createHeader(raw_ostream &OS) { |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 854 | OS << "/*===---- arm_sve.h - ARM SVE intrinsics " |
| 855 | "-----------------------------------===\n" |
| 856 | " *\n" |
| 857 | " *\n" |
| 858 | " * Part of the LLVM Project, under the Apache License v2.0 with LLVM " |
| 859 | "Exceptions.\n" |
| 860 | " * See https://llvm.org/LICENSE.txt for license information.\n" |
| 861 | " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" |
| 862 | " *\n" |
| 863 | " *===-----------------------------------------------------------------" |
| 864 | "------===\n" |
| 865 | " */\n\n"; |
| 866 | |
| 867 | OS << "#ifndef __ARM_SVE_H\n"; |
| 868 | OS << "#define __ARM_SVE_H\n\n"; |
| 869 | |
| 870 | OS << "#if !defined(__ARM_FEATURE_SVE)\n"; |
| 871 | OS << "#error \"SVE support not enabled\"\n"; |
| 872 | OS << "#else\n\n"; |
| 873 | |
| 874 | OS << "#include <stdint.h>\n\n"; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 875 | OS << "#ifdef __cplusplus\n"; |
| 876 | OS << "extern \"C\" {\n"; |
| 877 | OS << "#else\n"; |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 878 | OS << "#include <stdbool.h>\n"; |
| 879 | OS << "#endif\n\n"; |
| 880 | |
| 881 | OS << "typedef __fp16 float16_t;\n"; |
| 882 | OS << "typedef float float32_t;\n"; |
| 883 | OS << "typedef double float64_t;\n"; |
| 884 | OS << "typedef bool bool_t;\n\n"; |
| 885 | |
| 886 | OS << "typedef __SVInt8_t svint8_t;\n"; |
| 887 | OS << "typedef __SVInt16_t svint16_t;\n"; |
| 888 | OS << "typedef __SVInt32_t svint32_t;\n"; |
| 889 | OS << "typedef __SVInt64_t svint64_t;\n"; |
| 890 | OS << "typedef __SVUint8_t svuint8_t;\n"; |
| 891 | OS << "typedef __SVUint16_t svuint16_t;\n"; |
| 892 | OS << "typedef __SVUint32_t svuint32_t;\n"; |
| 893 | OS << "typedef __SVUint64_t svuint64_t;\n"; |
| 894 | OS << "typedef __SVFloat16_t svfloat16_t;\n"; |
| 895 | OS << "typedef __SVFloat32_t svfloat32_t;\n"; |
| 896 | OS << "typedef __SVFloat64_t svfloat64_t;\n"; |
| 897 | OS << "typedef __SVBool_t svbool_t;\n\n"; |
| 898 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 899 | OS << "typedef enum\n"; |
| 900 | OS << "{\n"; |
| 901 | OS << " SV_POW2 = 0,\n"; |
| 902 | OS << " SV_VL1 = 1,\n"; |
| 903 | OS << " SV_VL2 = 2,\n"; |
| 904 | OS << " SV_VL3 = 3,\n"; |
| 905 | OS << " SV_VL4 = 4,\n"; |
| 906 | OS << " SV_VL5 = 5,\n"; |
| 907 | OS << " SV_VL6 = 6,\n"; |
| 908 | OS << " SV_VL7 = 7,\n"; |
| 909 | OS << " SV_VL8 = 8,\n"; |
| 910 | OS << " SV_VL16 = 9,\n"; |
| 911 | OS << " SV_VL32 = 10,\n"; |
| 912 | OS << " SV_VL64 = 11,\n"; |
| 913 | OS << " SV_VL128 = 12,\n"; |
| 914 | OS << " SV_VL256 = 13,\n"; |
| 915 | OS << " SV_MUL4 = 29,\n"; |
| 916 | OS << " SV_MUL3 = 30,\n"; |
| 917 | OS << " SV_ALL = 31\n"; |
| 918 | OS << "} sv_pattern;\n\n"; |
| 919 | |
Sander de Smalen | 981f080 | 2020-03-18 15:05:08 +0000 | [diff] [blame] | 920 | OS << "/* Function attributes */\n"; |
| 921 | OS << "#define __aio static inline __attribute__((__always_inline__, " |
| 922 | "__nodebug__, __overloadable__))\n\n"; |
| 923 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 924 | SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; |
| 925 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 926 | for (auto *R : RV) |
| 927 | createIntrinsic(R, Defs); |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 928 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 929 | // Sort intrinsics in header file by following order/priority: |
| 930 | // - Architectural guard (i.e. does it require SVE2 or SVE2_AES) |
| 931 | // - Class (is intrinsic overloaded or not) |
| 932 | // - Intrinsic name |
| 933 | std::stable_sort( |
| 934 | Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A, |
| 935 | const std::unique_ptr<Intrinsic> &B) { |
Eric Fiselier | af2968e | 2020-04-16 18:35:31 -0400 | [diff] [blame] | 936 | auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) { |
| 937 | return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName()); |
| 938 | }; |
| 939 | return ToTuple(A) < ToTuple(B); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 940 | }); |
| 941 | |
| 942 | StringRef InGuard = ""; |
| 943 | for (auto &I : Defs) { |
| 944 | // Emit #endif/#if pair if needed. |
| 945 | if (I->getGuard() != InGuard) { |
| 946 | if (!InGuard.empty()) |
| 947 | OS << "#endif //" << InGuard << "\n"; |
| 948 | InGuard = I->getGuard(); |
| 949 | if (!InGuard.empty()) |
| 950 | OS << "\n#if " << InGuard << "\n"; |
| 951 | } |
| 952 | |
| 953 | // Actually emit the intrinsic declaration. |
| 954 | I->emitIntrinsic(OS); |
| 955 | } |
| 956 | |
| 957 | if (!InGuard.empty()) |
| 958 | OS << "#endif //" << InGuard << "\n"; |
| 959 | |
| 960 | OS << "#ifdef __cplusplus\n"; |
| 961 | OS << "} // extern \"C\"\n"; |
| 962 | OS << "#endif\n\n"; |
| 963 | OS << "#endif /*__ARM_FEATURE_SVE */\n\n"; |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 964 | OS << "#endif /* __ARM_SVE_H */\n"; |
| 965 | } |
| 966 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 967 | void SVEEmitter::createBuiltins(raw_ostream &OS) { |
| 968 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 969 | SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; |
| 970 | for (auto *R : RV) |
| 971 | createIntrinsic(R, Defs); |
| 972 | |
| 973 | // The mappings must be sorted based on BuiltinID. |
| 974 | llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, |
| 975 | const std::unique_ptr<Intrinsic> &B) { |
| 976 | return A->getMangledName() < B->getMangledName(); |
| 977 | }); |
| 978 | |
| 979 | OS << "#ifdef GET_SVE_BUILTINS\n"; |
| 980 | for (auto &Def : Defs) { |
| 981 | // Only create BUILTINs for non-overloaded intrinsics, as overloaded |
| 982 | // declarations only live in the header file. |
| 983 | if (Def->getClassKind() != ClassG) |
| 984 | OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \"" |
| 985 | << Def->getBuiltinTypeStr() << "\", \"n\")\n"; |
| 986 | } |
| 987 | OS << "#endif\n\n"; |
| 988 | } |
| 989 | |
| 990 | void SVEEmitter::createCodeGenMap(raw_ostream &OS) { |
| 991 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 992 | SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; |
| 993 | for (auto *R : RV) |
| 994 | createIntrinsic(R, Defs); |
| 995 | |
| 996 | // The mappings must be sorted based on BuiltinID. |
| 997 | llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, |
| 998 | const std::unique_ptr<Intrinsic> &B) { |
| 999 | return A->getMangledName() < B->getMangledName(); |
| 1000 | }); |
| 1001 | |
| 1002 | OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n"; |
| 1003 | for (auto &Def : Defs) { |
| 1004 | // Builtins only exist for non-overloaded intrinsics, overloaded |
| 1005 | // declarations only live in the header file. |
| 1006 | if (Def->getClassKind() == ClassG) |
| 1007 | continue; |
| 1008 | |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 1009 | uint64_t Flags = Def->getFlags(); |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 1010 | auto FlagString = std::to_string(Flags); |
| 1011 | |
| 1012 | std::string LLVMName = Def->getLLVMName(); |
| 1013 | std::string Builtin = Def->getMangledName(); |
| 1014 | if (!LLVMName.empty()) |
| 1015 | OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString |
| 1016 | << "),\n"; |
| 1017 | else |
| 1018 | OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n"; |
| 1019 | } |
| 1020 | OS << "#endif\n\n"; |
| 1021 | } |
| 1022 | |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 1023 | void SVEEmitter::createRangeChecks(raw_ostream &OS) { |
| 1024 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1025 | SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; |
| 1026 | for (auto *R : RV) |
| 1027 | createIntrinsic(R, Defs); |
| 1028 | |
| 1029 | // The mappings must be sorted based on BuiltinID. |
| 1030 | llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, |
| 1031 | const std::unique_ptr<Intrinsic> &B) { |
| 1032 | return A->getMangledName() < B->getMangledName(); |
| 1033 | }); |
| 1034 | |
| 1035 | |
| 1036 | OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n"; |
| 1037 | |
| 1038 | // Ensure these are only emitted once. |
| 1039 | std::set<std::string> Emitted; |
| 1040 | |
| 1041 | for (auto &Def : Defs) { |
| 1042 | if (Emitted.find(Def->getMangledName()) != Emitted.end() || |
| 1043 | Def->getImmChecks().empty()) |
| 1044 | continue; |
| 1045 | |
| 1046 | OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n"; |
| 1047 | for (auto &Check : Def->getImmChecks()) |
| 1048 | OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", " |
| 1049 | << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n"; |
| 1050 | OS << " break;\n"; |
| 1051 | |
| 1052 | Emitted.insert(Def->getMangledName()); |
| 1053 | } |
| 1054 | |
| 1055 | OS << "#endif\n\n"; |
| 1056 | } |
| 1057 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 1058 | /// Create the SVETypeFlags used in CGBuiltins |
| 1059 | void SVEEmitter::createTypeFlags(raw_ostream &OS) { |
| 1060 | OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n"; |
| 1061 | for (auto &KV : FlagTypes) |
| 1062 | OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n"; |
| 1063 | OS << "#endif\n\n"; |
| 1064 | |
| 1065 | OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n"; |
| 1066 | for (auto &KV : EltTypes) |
| 1067 | OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; |
| 1068 | OS << "#endif\n\n"; |
| 1069 | |
| 1070 | OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n"; |
| 1071 | for (auto &KV : MemEltTypes) |
| 1072 | OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; |
| 1073 | OS << "#endif\n\n"; |
Sander de Smalen | f6ea026 | 2020-04-14 15:31:20 +0100 | [diff] [blame] | 1074 | |
| 1075 | OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n"; |
| 1076 | for (auto &KV : MergeTypes) |
| 1077 | OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; |
| 1078 | OS << "#endif\n\n"; |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 1079 | |
| 1080 | OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n"; |
| 1081 | for (auto &KV : ImmCheckTypes) |
| 1082 | OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; |
| 1083 | OS << "#endif\n\n"; |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 1086 | namespace clang { |
| 1087 | void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) { |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 1088 | SVEEmitter(Records).createHeader(OS); |
| 1089 | } |
| 1090 | |
| 1091 | void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) { |
| 1092 | SVEEmitter(Records).createBuiltins(OS); |
| 1093 | } |
| 1094 | |
| 1095 | void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) { |
| 1096 | SVEEmitter(Records).createCodeGenMap(OS); |
| 1097 | } |
Sander de Smalen | c8a5b30 | 2020-04-14 15:56:36 +0100 | [diff] [blame] | 1098 | |
| 1099 | void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) { |
| 1100 | SVEEmitter(Records).createRangeChecks(OS); |
| 1101 | } |
| 1102 | |
Sander de Smalen | c5b8146 | 2020-03-18 11:07:20 +0000 | [diff] [blame] | 1103 | void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) { |
| 1104 | SVEEmitter(Records).createTypeFlags(OS); |
Sander de Smalen | 5087ace | 2020-03-15 14:29:45 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | } // End namespace clang |