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" 27 #include "llvm/ADT/StringMap.h" 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> 36 #include <tuple> 37 38 using namespace llvm; 39 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; 47 48 namespace { 49 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 66 class SVEType { 67 TypeSpec TS; 68 bool Float, Signed, Immediate, Void, Constant, Pointer, BFloat; 69 bool DefaultType, IsScalable, Predicate, PredicatePattern, PrefetchOp; 70 unsigned Bitwidth, ElementBitwidth, NumVectors; 71 72 public: 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), BFloat(false), DefaultType(false), 78 IsScalable(true), Predicate(false), PredicatePattern(false), 79 PrefetchOp(false), Bitwidth(128), ElementBitwidth(~0U), NumVectors(1) { 80 if (!TS.empty()) 81 applyTypespec(); 82 applyModifier(CharMod); 83 } 84 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 isBFloat() const { return BFloat; } 97 bool isFloatingPoint() const { return Float || BFloat; } 98 bool isInteger() const { return !isFloatingPoint() && !Predicate; } 99 bool isScalarPredicate() const { 100 return !isFloatingPoint() && Predicate && NumVectors == 0; 101 } 102 bool isPredicateVector() const { return Predicate; } 103 bool isPredicatePattern() const { return PredicatePattern; } 104 bool isPrefetchOp() const { return PrefetchOp; } 105 bool isConstant() const { return Constant; } 106 unsigned getElementSizeInBits() const { return ElementBitwidth; } 107 unsigned getNumVectors() const { return NumVectors; } 108 109 unsigned getNumElements() const { 110 assert(ElementBitwidth != ~0U); 111 return Bitwidth / ElementBitwidth; 112 } 113 unsigned getSizeInBits() const { 114 return Bitwidth; 115 } 116 117 /// Return the string representation of a type, which is an encoded 118 /// string for passing to the BUILTIN() macro in Builtins.def. 119 std::string builtin_str() const; 120 121 /// Return the C/C++ string representation of a type for use in the 122 /// arm_sve.h header file. 123 std::string str() const; 124 125 private: 126 /// Creates the type based on the typespec string in TS. 127 void applyTypespec(); 128 129 /// Applies a prototype modifier to the type. 130 void applyModifier(char Mod); 131 }; 132 133 134 class SVEEmitter; 135 136 /// The main grunt class. This represents an instantiation of an intrinsic with 137 /// a particular typespec and prototype. 138 class Intrinsic { 139 /// The unmangled name. 140 std::string Name; 141 142 /// The name of the corresponding LLVM IR intrinsic. 143 std::string LLVMName; 144 145 /// Intrinsic prototype. 146 std::string Proto; 147 148 /// The base type spec for this intrinsic. 149 TypeSpec BaseTypeSpec; 150 151 /// The base class kind. Most intrinsics use ClassS, which has full type 152 /// info for integers (_s32/_u32), or ClassG which is used for overloaded 153 /// intrinsics. 154 ClassKind Class; 155 156 /// The architectural #ifdef guard. 157 std::string Guard; 158 159 // The merge suffix such as _m, _x or _z. 160 std::string MergeSuffix; 161 162 /// The types of return value [0] and parameters [1..]. 163 std::vector<SVEType> Types; 164 165 /// The "base type", which is VarType('d', BaseTypeSpec). 166 SVEType BaseType; 167 168 uint64_t Flags; 169 170 SmallVector<ImmCheck, 2> ImmChecks; 171 172 public: 173 Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy, 174 StringRef MergeSuffix, uint64_t MemoryElementTy, StringRef LLVMName, 175 uint64_t Flags, ArrayRef<ImmCheck> ImmChecks, TypeSpec BT, 176 ClassKind Class, SVEEmitter &Emitter, StringRef Guard); 177 178 ~Intrinsic()=default; 179 180 std::string getName() const { return Name; } 181 std::string getLLVMName() const { return LLVMName; } 182 std::string getProto() const { return Proto; } 183 TypeSpec getBaseTypeSpec() const { return BaseTypeSpec; } 184 SVEType getBaseType() const { return BaseType; } 185 186 StringRef getGuard() const { return Guard; } 187 ClassKind getClassKind() const { return Class; } 188 189 SVEType getReturnType() const { return Types[0]; } 190 ArrayRef<SVEType> getTypes() const { return Types; } 191 SVEType getParamType(unsigned I) const { return Types[I + 1]; } 192 unsigned getNumParams() const { return Proto.size() - 1; } 193 194 uint64_t getFlags() const { return Flags; } 195 bool isFlagSet(uint64_t Flag) const { return Flags & Flag;} 196 197 ArrayRef<ImmCheck> getImmChecks() const { return ImmChecks; } 198 199 /// Return the type string for a BUILTIN() macro in Builtins.def. 200 std::string getBuiltinTypeStr(); 201 202 /// Return the name, mangled with type information. The name is mangled for 203 /// ClassS, so will add type suffixes such as _u32/_s32. 204 std::string getMangledName() const { return mangleName(ClassS); } 205 206 /// Returns true if the intrinsic is overloaded, in that it should also generate 207 /// a short form without the type-specifiers, e.g. 'svld1(..)' instead of 208 /// 'svld1_u32(..)'. 209 static bool isOverloadedIntrinsic(StringRef Name) { 210 auto BrOpen = Name.find("["); 211 auto BrClose = Name.find(']'); 212 return BrOpen != std::string::npos && BrClose != std::string::npos; 213 } 214 215 /// Return true if the intrinsic takes a splat operand. 216 bool hasSplat() const { 217 // These prototype modifiers are described in arm_sve.td. 218 return Proto.find_first_of("ajfrKLR@") != std::string::npos; 219 } 220 221 /// Return the parameter index of the splat operand. 222 unsigned getSplatIdx() const { 223 // These prototype modifiers are described in arm_sve.td. 224 auto Idx = Proto.find_first_of("ajfrKLR@"); 225 assert(Idx != std::string::npos && Idx > 0 && 226 "Prototype has no splat operand"); 227 return Idx - 1; 228 } 229 230 /// Emits the intrinsic declaration to the ostream. 231 void emitIntrinsic(raw_ostream &OS) const; 232 233 private: 234 std::string getMergeSuffix() const { return MergeSuffix; } 235 std::string mangleName(ClassKind LocalCK) const; 236 std::string replaceTemplatedArgs(std::string Name, TypeSpec TS, 237 std::string Proto) const; 238 }; 239 240 class SVEEmitter { 241 private: 242 // The reinterpret builtins are generated separately because they 243 // need the cross product of all types (121 functions in total), 244 // which is inconvenient to specify in the arm_sve.td file or 245 // generate in CGBuiltin.cpp. 246 struct ReinterpretTypeInfo { 247 const char *Suffix; 248 const char *Type; 249 const char *BuiltinType; 250 }; 251 SmallVector<ReinterpretTypeInfo, 11> Reinterprets = { 252 {"s8", "svint8_t", "q16Sc"}, {"s16", "svint16_t", "q8Ss"}, 253 {"s32", "svint32_t", "q4Si"}, {"s64", "svint64_t", "q2SWi"}, 254 {"u8", "svuint8_t", "q16Uc"}, {"u16", "svuint16_t", "q8Us"}, 255 {"u32", "svuint32_t", "q4Ui"}, {"u64", "svuint64_t", "q2UWi"}, 256 {"f16", "svfloat16_t", "q8h"}, {"f32", "svfloat32_t", "q4f"}, 257 {"f64", "svfloat64_t", "q2d"}}; 258 259 RecordKeeper &Records; 260 llvm::StringMap<uint64_t> EltTypes; 261 llvm::StringMap<uint64_t> MemEltTypes; 262 llvm::StringMap<uint64_t> FlagTypes; 263 llvm::StringMap<uint64_t> MergeTypes; 264 llvm::StringMap<uint64_t> ImmCheckTypes; 265 266 public: 267 SVEEmitter(RecordKeeper &R) : Records(R) { 268 for (auto *RV : Records.getAllDerivedDefinitions("EltType")) 269 EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); 270 for (auto *RV : Records.getAllDerivedDefinitions("MemEltType")) 271 MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); 272 for (auto *RV : Records.getAllDerivedDefinitions("FlagType")) 273 FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); 274 for (auto *RV : Records.getAllDerivedDefinitions("MergeType")) 275 MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); 276 for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType")) 277 ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value"); 278 } 279 280 /// Returns the enum value for the immcheck type 281 unsigned getEnumValueForImmCheck(StringRef C) const { 282 auto It = ImmCheckTypes.find(C); 283 if (It != ImmCheckTypes.end()) 284 return It->getValue(); 285 llvm_unreachable("Unsupported imm check"); 286 } 287 288 /// Returns the enum value for the flag type 289 uint64_t getEnumValueForFlag(StringRef C) const { 290 auto Res = FlagTypes.find(C); 291 if (Res != FlagTypes.end()) 292 return Res->getValue(); 293 llvm_unreachable("Unsupported flag"); 294 } 295 296 // Returns the SVETypeFlags for a given value and mask. 297 uint64_t encodeFlag(uint64_t V, StringRef MaskName) const { 298 auto It = FlagTypes.find(MaskName); 299 if (It != FlagTypes.end()) { 300 uint64_t Mask = It->getValue(); 301 unsigned Shift = llvm::countTrailingZeros(Mask); 302 return (V << Shift) & Mask; 303 } 304 llvm_unreachable("Unsupported flag"); 305 } 306 307 // Returns the SVETypeFlags for the given element type. 308 uint64_t encodeEltType(StringRef EltName) { 309 auto It = EltTypes.find(EltName); 310 if (It != EltTypes.end()) 311 return encodeFlag(It->getValue(), "EltTypeMask"); 312 llvm_unreachable("Unsupported EltType"); 313 } 314 315 // Returns the SVETypeFlags for the given memory element type. 316 uint64_t encodeMemoryElementType(uint64_t MT) { 317 return encodeFlag(MT, "MemEltTypeMask"); 318 } 319 320 // Returns the SVETypeFlags for the given merge type. 321 uint64_t encodeMergeType(uint64_t MT) { 322 return encodeFlag(MT, "MergeTypeMask"); 323 } 324 325 // Returns the SVETypeFlags for the given splat operand. 326 unsigned encodeSplatOperand(unsigned SplatIdx) { 327 assert(SplatIdx < 7 && "SplatIdx out of encodable range"); 328 return encodeFlag(SplatIdx + 1, "SplatOperandMask"); 329 } 330 331 // Returns the SVETypeFlags value for the given SVEType. 332 uint64_t encodeTypeFlags(const SVEType &T); 333 334 /// Emit arm_sve.h. 335 void createHeader(raw_ostream &o); 336 337 /// Emit all the __builtin prototypes and code needed by Sema. 338 void createBuiltins(raw_ostream &o); 339 340 /// Emit all the information needed to map builtin -> LLVM IR intrinsic. 341 void createCodeGenMap(raw_ostream &o); 342 343 /// Emit all the range checks for the immediates. 344 void createRangeChecks(raw_ostream &o); 345 346 /// Create the SVETypeFlags used in CGBuiltins 347 void createTypeFlags(raw_ostream &o); 348 349 /// Create intrinsic and add it to \p Out 350 void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out); 351 }; 352 353 } // end anonymous namespace 354 355 356 //===----------------------------------------------------------------------===// 357 // Type implementation 358 //===----------------------------------------------------------------------===// 359 360 std::string SVEType::builtin_str() const { 361 std::string S; 362 if (isVoid()) 363 return "v"; 364 365 if (isVoidPointer()) 366 S += "v"; 367 else if (!isFloatingPoint()) 368 switch (ElementBitwidth) { 369 case 1: S += "b"; break; 370 case 8: S += "c"; break; 371 case 16: S += "s"; break; 372 case 32: S += "i"; break; 373 case 64: S += "Wi"; break; 374 case 128: S += "LLLi"; break; 375 default: llvm_unreachable("Unhandled case!"); 376 } 377 else if (isFloat()) 378 switch (ElementBitwidth) { 379 case 16: S += "h"; break; 380 case 32: S += "f"; break; 381 case 64: S += "d"; break; 382 default: llvm_unreachable("Unhandled case!"); 383 } 384 else if (isBFloat()) { 385 assert(ElementBitwidth == 16 && "Not a valid BFloat."); 386 S += "y"; 387 } 388 389 if (!isFloatingPoint()) { 390 if ((isChar() || isPointer()) && !isVoidPointer()) { 391 // Make chars and typed pointers explicitly signed. 392 if (Signed) 393 S = "S" + S; 394 else if (!Signed) 395 S = "U" + S; 396 } else if (!isVoidPointer() && !Signed) { 397 S = "U" + S; 398 } 399 } 400 401 // Constant indices are "int", but have the "constant expression" modifier. 402 if (isImmediate()) { 403 assert(!isFloat() && "fp immediates are not supported"); 404 S = "I" + S; 405 } 406 407 if (isScalar()) { 408 if (Constant) S += "C"; 409 if (Pointer) S += "*"; 410 return S; 411 } 412 413 assert(isScalableVector() && "Unsupported type"); 414 return "q" + utostr(getNumElements() * NumVectors) + S; 415 } 416 417 std::string SVEType::str() const { 418 if (isPredicatePattern()) 419 return "sv_pattern"; 420 421 if (isPrefetchOp()) 422 return "sv_prfop"; 423 424 std::string S; 425 if (Void) 426 S += "void"; 427 else { 428 if (isScalableVector()) 429 S += "sv"; 430 if (!Signed && !isFloatingPoint()) 431 S += "u"; 432 433 if (Float) 434 S += "float"; 435 else if (isScalarPredicate() || isPredicateVector()) 436 S += "bool"; 437 else if (isBFloat()) 438 S += "bfloat"; 439 else 440 S += "int"; 441 442 if (!isScalarPredicate() && !isPredicateVector()) 443 S += utostr(ElementBitwidth); 444 if (!isScalableVector() && isVector()) 445 S += "x" + utostr(getNumElements()); 446 if (NumVectors > 1) 447 S += "x" + utostr(NumVectors); 448 if (!isScalarPredicate()) 449 S += "_t"; 450 } 451 452 if (Constant) 453 S += " const"; 454 if (Pointer) 455 S += " *"; 456 457 return S; 458 } 459 void SVEType::applyTypespec() { 460 for (char I : TS) { 461 switch (I) { 462 case 'P': 463 Predicate = true; 464 break; 465 case 'U': 466 Signed = false; 467 break; 468 case 'c': 469 ElementBitwidth = 8; 470 break; 471 case 's': 472 ElementBitwidth = 16; 473 break; 474 case 'i': 475 ElementBitwidth = 32; 476 break; 477 case 'l': 478 ElementBitwidth = 64; 479 break; 480 case 'h': 481 Float = true; 482 ElementBitwidth = 16; 483 break; 484 case 'f': 485 Float = true; 486 ElementBitwidth = 32; 487 break; 488 case 'd': 489 Float = true; 490 ElementBitwidth = 64; 491 break; 492 case 'b': 493 BFloat = true; 494 ElementBitwidth = 16; 495 break; 496 default: 497 llvm_unreachable("Unhandled type code!"); 498 } 499 } 500 assert(ElementBitwidth != ~0U && "Bad element bitwidth!"); 501 } 502 503 void SVEType::applyModifier(char Mod) { 504 switch (Mod) { 505 case '2': 506 NumVectors = 2; 507 break; 508 case '3': 509 NumVectors = 3; 510 break; 511 case '4': 512 NumVectors = 4; 513 break; 514 case 'v': 515 Void = true; 516 break; 517 case 'd': 518 DefaultType = true; 519 break; 520 case 'c': 521 Constant = true; 522 LLVM_FALLTHROUGH; 523 case 'p': 524 Pointer = true; 525 Bitwidth = ElementBitwidth; 526 NumVectors = 0; 527 break; 528 case 'e': 529 Signed = false; 530 ElementBitwidth /= 2; 531 break; 532 case 'h': 533 ElementBitwidth /= 2; 534 break; 535 case 'q': 536 ElementBitwidth /= 4; 537 break; 538 case 'b': 539 Signed = false; 540 Float = false; 541 ElementBitwidth /= 4; 542 break; 543 case 'o': 544 ElementBitwidth *= 4; 545 break; 546 case 'P': 547 Signed = true; 548 Float = false; 549 BFloat = false; 550 Predicate = true; 551 Bitwidth = 16; 552 ElementBitwidth = 1; 553 break; 554 case 's': 555 case 'a': 556 Bitwidth = ElementBitwidth; 557 NumVectors = 0; 558 break; 559 case 'R': 560 ElementBitwidth /= 2; 561 NumVectors = 0; 562 break; 563 case 'r': 564 ElementBitwidth /= 4; 565 NumVectors = 0; 566 break; 567 case '@': 568 Signed = false; 569 Float = false; 570 ElementBitwidth /= 4; 571 NumVectors = 0; 572 break; 573 case 'K': 574 Signed = true; 575 Float = false; 576 Bitwidth = ElementBitwidth; 577 NumVectors = 0; 578 break; 579 case 'L': 580 Signed = false; 581 Float = false; 582 Bitwidth = ElementBitwidth; 583 NumVectors = 0; 584 break; 585 case 'u': 586 Predicate = false; 587 Signed = false; 588 Float = false; 589 break; 590 case 'x': 591 Predicate = false; 592 Signed = true; 593 Float = false; 594 break; 595 case 'i': 596 Predicate = false; 597 Float = false; 598 ElementBitwidth = Bitwidth = 64; 599 NumVectors = 0; 600 Signed = false; 601 Immediate = true; 602 break; 603 case 'I': 604 Predicate = false; 605 Float = false; 606 ElementBitwidth = Bitwidth = 32; 607 NumVectors = 0; 608 Signed = true; 609 Immediate = true; 610 PredicatePattern = true; 611 break; 612 case 'J': 613 Predicate = false; 614 Float = false; 615 ElementBitwidth = Bitwidth = 32; 616 NumVectors = 0; 617 Signed = true; 618 Immediate = true; 619 PrefetchOp = true; 620 break; 621 case 'k': 622 Predicate = false; 623 Signed = true; 624 Float = false; 625 ElementBitwidth = Bitwidth = 32; 626 NumVectors = 0; 627 break; 628 case 'l': 629 Predicate = false; 630 Signed = true; 631 Float = false; 632 ElementBitwidth = Bitwidth = 64; 633 NumVectors = 0; 634 break; 635 case 'm': 636 Predicate = false; 637 Signed = false; 638 Float = false; 639 ElementBitwidth = Bitwidth = 32; 640 NumVectors = 0; 641 break; 642 case 'n': 643 Predicate = false; 644 Signed = false; 645 Float = false; 646 ElementBitwidth = Bitwidth = 64; 647 NumVectors = 0; 648 break; 649 case 'w': 650 ElementBitwidth = 64; 651 break; 652 case 'j': 653 ElementBitwidth = Bitwidth = 64; 654 NumVectors = 0; 655 break; 656 case 'f': 657 Signed = false; 658 ElementBitwidth = Bitwidth = 64; 659 NumVectors = 0; 660 break; 661 case 'g': 662 Signed = false; 663 Float = false; 664 ElementBitwidth = 64; 665 break; 666 case 't': 667 Signed = true; 668 Float = false; 669 ElementBitwidth = 32; 670 break; 671 case 'z': 672 Signed = false; 673 Float = false; 674 ElementBitwidth = 32; 675 break; 676 case 'O': 677 Predicate = false; 678 Float = true; 679 ElementBitwidth = 16; 680 break; 681 case 'M': 682 Predicate = false; 683 Float = true; 684 ElementBitwidth = 32; 685 break; 686 case 'N': 687 Predicate = false; 688 Float = true; 689 ElementBitwidth = 64; 690 break; 691 case 'Q': 692 Constant = true; 693 Pointer = true; 694 Void = true; 695 NumVectors = 0; 696 break; 697 case 'S': 698 Constant = true; 699 Pointer = true; 700 ElementBitwidth = Bitwidth = 8; 701 NumVectors = 0; 702 Signed = true; 703 break; 704 case 'W': 705 Constant = true; 706 Pointer = true; 707 ElementBitwidth = Bitwidth = 8; 708 NumVectors = 0; 709 Signed = false; 710 break; 711 case 'T': 712 Constant = true; 713 Pointer = true; 714 ElementBitwidth = Bitwidth = 16; 715 NumVectors = 0; 716 Signed = true; 717 break; 718 case 'X': 719 Constant = true; 720 Pointer = true; 721 ElementBitwidth = Bitwidth = 16; 722 NumVectors = 0; 723 Signed = false; 724 break; 725 case 'Y': 726 Constant = true; 727 Pointer = true; 728 ElementBitwidth = Bitwidth = 32; 729 NumVectors = 0; 730 Signed = false; 731 break; 732 case 'U': 733 Constant = true; 734 Pointer = true; 735 ElementBitwidth = Bitwidth = 32; 736 NumVectors = 0; 737 Signed = true; 738 break; 739 case 'A': 740 Pointer = true; 741 ElementBitwidth = Bitwidth = 8; 742 NumVectors = 0; 743 Signed = true; 744 break; 745 case 'B': 746 Pointer = true; 747 ElementBitwidth = Bitwidth = 16; 748 NumVectors = 0; 749 Signed = true; 750 break; 751 case 'C': 752 Pointer = true; 753 ElementBitwidth = Bitwidth = 32; 754 NumVectors = 0; 755 Signed = true; 756 break; 757 case 'D': 758 Pointer = true; 759 ElementBitwidth = Bitwidth = 64; 760 NumVectors = 0; 761 Signed = true; 762 break; 763 case 'E': 764 Pointer = true; 765 ElementBitwidth = Bitwidth = 8; 766 NumVectors = 0; 767 Signed = false; 768 break; 769 case 'F': 770 Pointer = true; 771 ElementBitwidth = Bitwidth = 16; 772 NumVectors = 0; 773 Signed = false; 774 break; 775 case 'G': 776 Pointer = true; 777 ElementBitwidth = Bitwidth = 32; 778 NumVectors = 0; 779 Signed = false; 780 break; 781 default: 782 llvm_unreachable("Unhandled character!"); 783 } 784 } 785 786 787 //===----------------------------------------------------------------------===// 788 // Intrinsic implementation 789 //===----------------------------------------------------------------------===// 790 791 Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy, 792 StringRef MergeSuffix, uint64_t MemoryElementTy, 793 StringRef LLVMName, uint64_t Flags, 794 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class, 795 SVEEmitter &Emitter, StringRef Guard) 796 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()), 797 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()), 798 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags), 799 ImmChecks(Checks.begin(), Checks.end()) { 800 // Types[0] is the return value. 801 for (unsigned I = 0; I < Proto.size(); ++I) { 802 SVEType T(BaseTypeSpec, Proto[I]); 803 Types.push_back(T); 804 805 // Add range checks for immediates 806 if (I > 0) { 807 if (T.isPredicatePattern()) 808 ImmChecks.emplace_back( 809 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31")); 810 else if (T.isPrefetchOp()) 811 ImmChecks.emplace_back( 812 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13")); 813 } 814 } 815 816 // Set flags based on properties 817 this->Flags |= Emitter.encodeTypeFlags(BaseType); 818 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy); 819 this->Flags |= Emitter.encodeMergeType(MergeTy); 820 if (hasSplat()) 821 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx()); 822 } 823 824 std::string Intrinsic::getBuiltinTypeStr() { 825 std::string S = getReturnType().builtin_str(); 826 for (unsigned I = 0; I < getNumParams(); ++I) 827 S += getParamType(I).builtin_str(); 828 829 return S; 830 } 831 832 std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS, 833 std::string Proto) const { 834 std::string Ret = Name; 835 while (Ret.find('{') != std::string::npos) { 836 size_t Pos = Ret.find('{'); 837 size_t End = Ret.find('}'); 838 unsigned NumChars = End - Pos + 1; 839 assert(NumChars == 3 && "Unexpected template argument"); 840 841 SVEType T; 842 char C = Ret[Pos+1]; 843 switch(C) { 844 default: 845 llvm_unreachable("Unknown predication specifier"); 846 case 'd': 847 T = SVEType(TS, 'd'); 848 break; 849 case '0': 850 case '1': 851 case '2': 852 case '3': 853 T = SVEType(TS, Proto[C - '0']); 854 break; 855 } 856 857 // Replace templated arg with the right suffix (e.g. u32) 858 std::string TypeCode; 859 if (T.isInteger()) 860 TypeCode = T.isSigned() ? 's' : 'u'; 861 else if (T.isPredicateVector()) 862 TypeCode = 'b'; 863 else if (T.isBFloat()) 864 TypeCode = "bf"; 865 else 866 TypeCode = 'f'; 867 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits())); 868 } 869 870 return Ret; 871 } 872 873 std::string Intrinsic::mangleName(ClassKind LocalCK) const { 874 std::string S = getName(); 875 876 if (LocalCK == ClassG) { 877 // Remove the square brackets and everything in between. 878 while (S.find("[") != std::string::npos) { 879 auto Start = S.find("["); 880 auto End = S.find(']'); 881 S.erase(Start, (End-Start)+1); 882 } 883 } else { 884 // Remove the square brackets. 885 while (S.find("[") != std::string::npos) { 886 auto BrPos = S.find('['); 887 if (BrPos != std::string::npos) 888 S.erase(BrPos, 1); 889 BrPos = S.find(']'); 890 if (BrPos != std::string::npos) 891 S.erase(BrPos, 1); 892 } 893 } 894 895 // Replace all {d} like expressions with e.g. 'u32' 896 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) + 897 getMergeSuffix(); 898 } 899 900 void Intrinsic::emitIntrinsic(raw_ostream &OS) const { 901 // Use the preprocessor to 902 if (getClassKind() != ClassG || getProto().size() <= 1) { 903 OS << "#define " << mangleName(getClassKind()) 904 << "(...) __builtin_sve_" << mangleName(ClassS) 905 << "(__VA_ARGS__)\n"; 906 } else { 907 std::string FullName = mangleName(ClassS); 908 std::string ProtoName = mangleName(ClassG); 909 910 OS << "__aio __attribute__((__clang_arm_builtin_alias(" 911 << "__builtin_sve_" << FullName << ")))\n"; 912 913 OS << getTypes()[0].str() << " " << ProtoName << "("; 914 for (unsigned I = 0; I < getTypes().size() - 1; ++I) { 915 if (I != 0) 916 OS << ", "; 917 OS << getTypes()[I + 1].str(); 918 } 919 OS << ");\n"; 920 } 921 } 922 923 //===----------------------------------------------------------------------===// 924 // SVEEmitter implementation 925 //===----------------------------------------------------------------------===// 926 uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) { 927 if (T.isFloat()) { 928 switch (T.getElementSizeInBits()) { 929 case 16: 930 return encodeEltType("EltTyFloat16"); 931 case 32: 932 return encodeEltType("EltTyFloat32"); 933 case 64: 934 return encodeEltType("EltTyFloat64"); 935 default: 936 llvm_unreachable("Unhandled float element bitwidth!"); 937 } 938 } 939 940 if (T.isBFloat()) { 941 assert(T.getElementSizeInBits() == 16 && "Not a valid BFloat."); 942 return encodeEltType("EltTyBFloat16"); 943 } 944 945 if (T.isPredicateVector()) { 946 switch (T.getElementSizeInBits()) { 947 case 8: 948 return encodeEltType("EltTyBool8"); 949 case 16: 950 return encodeEltType("EltTyBool16"); 951 case 32: 952 return encodeEltType("EltTyBool32"); 953 case 64: 954 return encodeEltType("EltTyBool64"); 955 default: 956 llvm_unreachable("Unhandled predicate element bitwidth!"); 957 } 958 } 959 960 switch (T.getElementSizeInBits()) { 961 case 8: 962 return encodeEltType("EltTyInt8"); 963 case 16: 964 return encodeEltType("EltTyInt16"); 965 case 32: 966 return encodeEltType("EltTyInt32"); 967 case 64: 968 return encodeEltType("EltTyInt64"); 969 default: 970 llvm_unreachable("Unhandled integer element bitwidth!"); 971 } 972 } 973 974 void SVEEmitter::createIntrinsic( 975 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) { 976 StringRef Name = R->getValueAsString("Name"); 977 StringRef Proto = R->getValueAsString("Prototype"); 978 StringRef Types = R->getValueAsString("Types"); 979 StringRef Guard = R->getValueAsString("ArchGuard"); 980 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic"); 981 uint64_t Merge = R->getValueAsInt("Merge"); 982 StringRef MergeSuffix = R->getValueAsString("MergeSuffix"); 983 uint64_t MemEltType = R->getValueAsInt("MemEltType"); 984 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags"); 985 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks"); 986 987 int64_t Flags = 0; 988 for (auto FlagRec : FlagsList) 989 Flags |= FlagRec->getValueAsInt("Value"); 990 991 // Create a dummy TypeSpec for non-overloaded builtins. 992 if (Types.empty()) { 993 assert((Flags & getEnumValueForFlag("IsOverloadNone")) && 994 "Expect TypeSpec for overloaded builtin!"); 995 Types = "i"; 996 } 997 998 // Extract type specs from string 999 SmallVector<TypeSpec, 8> TypeSpecs; 1000 TypeSpec Acc; 1001 for (char I : Types) { 1002 Acc.push_back(I); 1003 if (islower(I)) { 1004 TypeSpecs.push_back(TypeSpec(Acc)); 1005 Acc.clear(); 1006 } 1007 } 1008 1009 // Remove duplicate type specs. 1010 llvm::sort(TypeSpecs); 1011 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()), 1012 TypeSpecs.end()); 1013 1014 // Create an Intrinsic for each type spec. 1015 for (auto TS : TypeSpecs) { 1016 // Collate a list of range/option checks for the immediates. 1017 SmallVector<ImmCheck, 2> ImmChecks; 1018 for (auto *R : ImmCheckList) { 1019 int64_t Arg = R->getValueAsInt("Arg"); 1020 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg"); 1021 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value"); 1022 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative"); 1023 1024 unsigned ElementSizeInBits = 0; 1025 if (EltSizeArg >= 0) 1026 ElementSizeInBits = 1027 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1]) 1028 .getElementSizeInBits(); 1029 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits)); 1030 } 1031 1032 Out.push_back(std::make_unique<Intrinsic>( 1033 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks, 1034 TS, ClassS, *this, Guard)); 1035 1036 // Also generate the short-form (e.g. svadd_m) for the given type-spec. 1037 if (Intrinsic::isOverloadedIntrinsic(Name)) 1038 Out.push_back(std::make_unique<Intrinsic>( 1039 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, 1040 ImmChecks, TS, ClassG, *this, Guard)); 1041 } 1042 } 1043 1044 void SVEEmitter::createHeader(raw_ostream &OS) { 1045 OS << "/*===---- arm_sve.h - ARM SVE intrinsics " 1046 "-----------------------------------===\n" 1047 " *\n" 1048 " *\n" 1049 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM " 1050 "Exceptions.\n" 1051 " * See https://llvm.org/LICENSE.txt for license information.\n" 1052 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" 1053 " *\n" 1054 " *===-----------------------------------------------------------------" 1055 "------===\n" 1056 " */\n\n"; 1057 1058 OS << "#ifndef __ARM_SVE_H\n"; 1059 OS << "#define __ARM_SVE_H\n\n"; 1060 1061 OS << "#if !defined(__ARM_FEATURE_SVE)\n"; 1062 OS << "#error \"SVE support not enabled\"\n"; 1063 OS << "#else\n\n"; 1064 1065 OS << "#if !defined(__LITTLE_ENDIAN__)\n"; 1066 OS << "#error \"Big endian is currently not supported for arm_sve.h\"\n"; 1067 OS << "#endif\n"; 1068 1069 OS << "#include <stdint.h>\n\n"; 1070 OS << "#ifdef __cplusplus\n"; 1071 OS << "extern \"C\" {\n"; 1072 OS << "#else\n"; 1073 OS << "#include <stdbool.h>\n"; 1074 OS << "#endif\n\n"; 1075 1076 OS << "typedef __fp16 float16_t;\n"; 1077 OS << "typedef float float32_t;\n"; 1078 OS << "typedef double float64_t;\n"; 1079 1080 OS << "typedef __SVInt8_t svint8_t;\n"; 1081 OS << "typedef __SVInt16_t svint16_t;\n"; 1082 OS << "typedef __SVInt32_t svint32_t;\n"; 1083 OS << "typedef __SVInt64_t svint64_t;\n"; 1084 OS << "typedef __SVUint8_t svuint8_t;\n"; 1085 OS << "typedef __SVUint16_t svuint16_t;\n"; 1086 OS << "typedef __SVUint32_t svuint32_t;\n"; 1087 OS << "typedef __SVUint64_t svuint64_t;\n"; 1088 OS << "typedef __SVFloat16_t svfloat16_t;\n"; 1089 OS << "typedef __SVBFloat16_t svbfloat16_t;\n\n"; 1090 1091 OS << "#ifdef __ARM_FEATURE_BF16_SCALAR_ARITHMETIC\n"; 1092 OS << "typedef __bf16 bfloat16_t;\n"; 1093 OS << "#endif\n\n"; 1094 1095 OS << "typedef __SVFloat32_t svfloat32_t;\n"; 1096 OS << "typedef __SVFloat64_t svfloat64_t;\n"; 1097 OS << "typedef __clang_svint8x2_t svint8x2_t;\n"; 1098 OS << "typedef __clang_svint16x2_t svint16x2_t;\n"; 1099 OS << "typedef __clang_svint32x2_t svint32x2_t;\n"; 1100 OS << "typedef __clang_svint64x2_t svint64x2_t;\n"; 1101 OS << "typedef __clang_svuint8x2_t svuint8x2_t;\n"; 1102 OS << "typedef __clang_svuint16x2_t svuint16x2_t;\n"; 1103 OS << "typedef __clang_svuint32x2_t svuint32x2_t;\n"; 1104 OS << "typedef __clang_svuint64x2_t svuint64x2_t;\n"; 1105 OS << "typedef __clang_svfloat16x2_t svfloat16x2_t;\n"; 1106 OS << "typedef __clang_svfloat32x2_t svfloat32x2_t;\n"; 1107 OS << "typedef __clang_svfloat64x2_t svfloat64x2_t;\n"; 1108 OS << "typedef __clang_svint8x3_t svint8x3_t;\n"; 1109 OS << "typedef __clang_svint16x3_t svint16x3_t;\n"; 1110 OS << "typedef __clang_svint32x3_t svint32x3_t;\n"; 1111 OS << "typedef __clang_svint64x3_t svint64x3_t;\n"; 1112 OS << "typedef __clang_svuint8x3_t svuint8x3_t;\n"; 1113 OS << "typedef __clang_svuint16x3_t svuint16x3_t;\n"; 1114 OS << "typedef __clang_svuint32x3_t svuint32x3_t;\n"; 1115 OS << "typedef __clang_svuint64x3_t svuint64x3_t;\n"; 1116 OS << "typedef __clang_svfloat16x3_t svfloat16x3_t;\n"; 1117 OS << "typedef __clang_svfloat32x3_t svfloat32x3_t;\n"; 1118 OS << "typedef __clang_svfloat64x3_t svfloat64x3_t;\n"; 1119 OS << "typedef __clang_svint8x4_t svint8x4_t;\n"; 1120 OS << "typedef __clang_svint16x4_t svint16x4_t;\n"; 1121 OS << "typedef __clang_svint32x4_t svint32x4_t;\n"; 1122 OS << "typedef __clang_svint64x4_t svint64x4_t;\n"; 1123 OS << "typedef __clang_svuint8x4_t svuint8x4_t;\n"; 1124 OS << "typedef __clang_svuint16x4_t svuint16x4_t;\n"; 1125 OS << "typedef __clang_svuint32x4_t svuint32x4_t;\n"; 1126 OS << "typedef __clang_svuint64x4_t svuint64x4_t;\n"; 1127 OS << "typedef __clang_svfloat16x4_t svfloat16x4_t;\n"; 1128 OS << "typedef __clang_svfloat32x4_t svfloat32x4_t;\n"; 1129 OS << "typedef __clang_svfloat64x4_t svfloat64x4_t;\n"; 1130 OS << "typedef __SVBool_t svbool_t;\n\n"; 1131 1132 OS << "typedef enum\n"; 1133 OS << "{\n"; 1134 OS << " SV_POW2 = 0,\n"; 1135 OS << " SV_VL1 = 1,\n"; 1136 OS << " SV_VL2 = 2,\n"; 1137 OS << " SV_VL3 = 3,\n"; 1138 OS << " SV_VL4 = 4,\n"; 1139 OS << " SV_VL5 = 5,\n"; 1140 OS << " SV_VL6 = 6,\n"; 1141 OS << " SV_VL7 = 7,\n"; 1142 OS << " SV_VL8 = 8,\n"; 1143 OS << " SV_VL16 = 9,\n"; 1144 OS << " SV_VL32 = 10,\n"; 1145 OS << " SV_VL64 = 11,\n"; 1146 OS << " SV_VL128 = 12,\n"; 1147 OS << " SV_VL256 = 13,\n"; 1148 OS << " SV_MUL4 = 29,\n"; 1149 OS << " SV_MUL3 = 30,\n"; 1150 OS << " SV_ALL = 31\n"; 1151 OS << "} sv_pattern;\n\n"; 1152 1153 OS << "typedef enum\n"; 1154 OS << "{\n"; 1155 OS << " SV_PLDL1KEEP = 0,\n"; 1156 OS << " SV_PLDL1STRM = 1,\n"; 1157 OS << " SV_PLDL2KEEP = 2,\n"; 1158 OS << " SV_PLDL2STRM = 3,\n"; 1159 OS << " SV_PLDL3KEEP = 4,\n"; 1160 OS << " SV_PLDL3STRM = 5,\n"; 1161 OS << " SV_PSTL1KEEP = 8,\n"; 1162 OS << " SV_PSTL1STRM = 9,\n"; 1163 OS << " SV_PSTL2KEEP = 10,\n"; 1164 OS << " SV_PSTL2STRM = 11,\n"; 1165 OS << " SV_PSTL3KEEP = 12,\n"; 1166 OS << " SV_PSTL3STRM = 13\n"; 1167 OS << "} sv_prfop;\n\n"; 1168 1169 OS << "/* Function attributes */\n"; 1170 OS << "#define __aio static inline __attribute__((__always_inline__, " 1171 "__nodebug__, __overloadable__))\n\n"; 1172 1173 // Add reinterpret functions. 1174 for (auto ShortForm : { false, true } ) 1175 for (const ReinterpretTypeInfo &From : Reinterprets) 1176 for (const ReinterpretTypeInfo &To : Reinterprets) { 1177 if (ShortForm) { 1178 OS << "__aio " << From.Type << " svreinterpret_" << From.Suffix; 1179 OS << "(" << To.Type << " op) {\n"; 1180 OS << " return __builtin_sve_reinterpret_" << From.Suffix << "_" 1181 << To.Suffix << "(op);\n"; 1182 OS << "}\n\n"; 1183 } else 1184 OS << "#define svreinterpret_" << From.Suffix << "_" << To.Suffix 1185 << "(...) __builtin_sve_reinterpret_" << From.Suffix << "_" 1186 << To.Suffix << "(__VA_ARGS__)\n"; 1187 } 1188 1189 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; 1190 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 1191 for (auto *R : RV) 1192 createIntrinsic(R, Defs); 1193 1194 // Sort intrinsics in header file by following order/priority: 1195 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES) 1196 // - Class (is intrinsic overloaded or not) 1197 // - Intrinsic name 1198 std::stable_sort( 1199 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A, 1200 const std::unique_ptr<Intrinsic> &B) { 1201 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) { 1202 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName()); 1203 }; 1204 return ToTuple(A) < ToTuple(B); 1205 }); 1206 1207 StringRef InGuard = ""; 1208 for (auto &I : Defs) { 1209 // Emit #endif/#if pair if needed. 1210 if (I->getGuard() != InGuard) { 1211 if (!InGuard.empty()) 1212 OS << "#endif //" << InGuard << "\n"; 1213 InGuard = I->getGuard(); 1214 if (!InGuard.empty()) 1215 OS << "\n#if " << InGuard << "\n"; 1216 } 1217 1218 // Actually emit the intrinsic declaration. 1219 I->emitIntrinsic(OS); 1220 } 1221 1222 if (!InGuard.empty()) 1223 OS << "#endif //" << InGuard << "\n"; 1224 1225 OS << "#if defined(__ARM_FEATURE_SVE2)\n"; 1226 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n"; 1227 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n"; 1228 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n"; 1229 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n"; 1230 1231 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n"; 1232 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n"; 1233 1234 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n"; 1235 1236 OS << "#ifdef __cplusplus\n"; 1237 OS << "} // extern \"C\"\n"; 1238 OS << "#endif\n\n"; 1239 OS << "#endif /*__ARM_FEATURE_SVE */\n\n"; 1240 OS << "#endif /* __ARM_SVE_H */\n"; 1241 } 1242 1243 void SVEEmitter::createBuiltins(raw_ostream &OS) { 1244 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 1245 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; 1246 for (auto *R : RV) 1247 createIntrinsic(R, Defs); 1248 1249 // The mappings must be sorted based on BuiltinID. 1250 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, 1251 const std::unique_ptr<Intrinsic> &B) { 1252 return A->getMangledName() < B->getMangledName(); 1253 }); 1254 1255 OS << "#ifdef GET_SVE_BUILTINS\n"; 1256 for (auto &Def : Defs) { 1257 // Only create BUILTINs for non-overloaded intrinsics, as overloaded 1258 // declarations only live in the header file. 1259 if (Def->getClassKind() != ClassG) 1260 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \"" 1261 << Def->getBuiltinTypeStr() << "\", \"n\")\n"; 1262 } 1263 1264 // Add reinterpret builtins 1265 for (const ReinterpretTypeInfo &From : Reinterprets) 1266 for (const ReinterpretTypeInfo &To : Reinterprets) 1267 OS << "BUILTIN(__builtin_sve_reinterpret_" << From.Suffix << "_" 1268 << To.Suffix << +", \"" << From.BuiltinType << To.BuiltinType 1269 << "\", \"n\")\n"; 1270 1271 OS << "#endif\n\n"; 1272 } 1273 1274 void SVEEmitter::createCodeGenMap(raw_ostream &OS) { 1275 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 1276 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; 1277 for (auto *R : RV) 1278 createIntrinsic(R, Defs); 1279 1280 // The mappings must be sorted based on BuiltinID. 1281 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, 1282 const std::unique_ptr<Intrinsic> &B) { 1283 return A->getMangledName() < B->getMangledName(); 1284 }); 1285 1286 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n"; 1287 for (auto &Def : Defs) { 1288 // Builtins only exist for non-overloaded intrinsics, overloaded 1289 // declarations only live in the header file. 1290 if (Def->getClassKind() == ClassG) 1291 continue; 1292 1293 uint64_t Flags = Def->getFlags(); 1294 auto FlagString = std::to_string(Flags); 1295 1296 std::string LLVMName = Def->getLLVMName(); 1297 std::string Builtin = Def->getMangledName(); 1298 if (!LLVMName.empty()) 1299 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString 1300 << "),\n"; 1301 else 1302 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n"; 1303 } 1304 OS << "#endif\n\n"; 1305 } 1306 1307 void SVEEmitter::createRangeChecks(raw_ostream &OS) { 1308 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 1309 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs; 1310 for (auto *R : RV) 1311 createIntrinsic(R, Defs); 1312 1313 // The mappings must be sorted based on BuiltinID. 1314 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A, 1315 const std::unique_ptr<Intrinsic> &B) { 1316 return A->getMangledName() < B->getMangledName(); 1317 }); 1318 1319 1320 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n"; 1321 1322 // Ensure these are only emitted once. 1323 std::set<std::string> Emitted; 1324 1325 for (auto &Def : Defs) { 1326 if (Emitted.find(Def->getMangledName()) != Emitted.end() || 1327 Def->getImmChecks().empty()) 1328 continue; 1329 1330 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n"; 1331 for (auto &Check : Def->getImmChecks()) 1332 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", " 1333 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n"; 1334 OS << " break;\n"; 1335 1336 Emitted.insert(Def->getMangledName()); 1337 } 1338 1339 OS << "#endif\n\n"; 1340 } 1341 1342 /// Create the SVETypeFlags used in CGBuiltins 1343 void SVEEmitter::createTypeFlags(raw_ostream &OS) { 1344 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n"; 1345 for (auto &KV : FlagTypes) 1346 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n"; 1347 OS << "#endif\n\n"; 1348 1349 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n"; 1350 for (auto &KV : EltTypes) 1351 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; 1352 OS << "#endif\n\n"; 1353 1354 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n"; 1355 for (auto &KV : MemEltTypes) 1356 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; 1357 OS << "#endif\n\n"; 1358 1359 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n"; 1360 for (auto &KV : MergeTypes) 1361 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; 1362 OS << "#endif\n\n"; 1363 1364 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n"; 1365 for (auto &KV : ImmCheckTypes) 1366 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n"; 1367 OS << "#endif\n\n"; 1368 } 1369 1370 namespace clang { 1371 void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) { 1372 SVEEmitter(Records).createHeader(OS); 1373 } 1374 1375 void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) { 1376 SVEEmitter(Records).createBuiltins(OS); 1377 } 1378 1379 void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) { 1380 SVEEmitter(Records).createCodeGenMap(OS); 1381 } 1382 1383 void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) { 1384 SVEEmitter(Records).createRangeChecks(OS); 1385 } 1386 1387 void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) { 1388 SVEEmitter(Records).createTypeFlags(OS); 1389 } 1390 1391 } // End namespace clang 1392