1 //===- RISCVVEmitter.cpp - Generate riscv_vector.h for use with clang -----===// 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 riscv_vector.h which 10 // includes a declaration and definition of each intrinsic functions specified 11 // in https://github.com/riscv/rvv-intrinsic-doc. 12 // 13 // See also the documentation in include/clang/Basic/riscv_vector.td. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/SmallSet.h" 19 #include "llvm/ADT/StringExtras.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringSet.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/TableGen/Error.h" 24 #include "llvm/TableGen/Record.h" 25 #include <numeric> 26 27 using namespace llvm; 28 using BasicType = char; 29 using VScaleVal = Optional<unsigned>; 30 31 namespace { 32 33 // Exponential LMUL 34 struct LMULType { 35 int Log2LMUL; 36 LMULType(int Log2LMUL); 37 // Return the C/C++ string representation of LMUL 38 std::string str() const; 39 Optional<unsigned> getScale(unsigned ElementBitwidth) const; 40 void MulLog2LMUL(int Log2LMUL); 41 LMULType &operator*=(uint32_t RHS); 42 }; 43 44 // This class is compact representation of a valid and invalid RVVType. 45 class RVVType { 46 enum ScalarTypeKind : uint32_t { 47 Void, 48 Size_t, 49 Ptrdiff_t, 50 UnsignedLong, 51 SignedLong, 52 Boolean, 53 SignedInteger, 54 UnsignedInteger, 55 Float, 56 Invalid, 57 }; 58 BasicType BT; 59 ScalarTypeKind ScalarType = Invalid; 60 LMULType LMUL; 61 bool IsPointer = false; 62 // IsConstant indices are "int", but have the constant expression. 63 bool IsImmediate = false; 64 // Const qualifier for pointer to const object or object of const type. 65 bool IsConstant = false; 66 unsigned ElementBitwidth = 0; 67 VScaleVal Scale = 0; 68 bool Valid; 69 70 std::string BuiltinStr; 71 std::string ClangBuiltinStr; 72 std::string Str; 73 std::string ShortStr; 74 75 public: 76 RVVType() : RVVType(BasicType(), 0, StringRef()) {} 77 RVVType(BasicType BT, int Log2LMUL, StringRef prototype); 78 79 // Return the string representation of a type, which is an encoded string for 80 // passing to the BUILTIN() macro in Builtins.def. 81 const std::string &getBuiltinStr() const { return BuiltinStr; } 82 83 // Return the clang buitlin type for RVV vector type which are used in the 84 // riscv_vector.h header file. 85 const std::string &getClangBuiltinStr() const { return ClangBuiltinStr; } 86 87 // Return the C/C++ string representation of a type for use in the 88 // riscv_vector.h header file. 89 const std::string &getTypeStr() const { return Str; } 90 91 // Return the short name of a type for C/C++ name suffix. 92 const std::string &getShortStr() { 93 // Not all types are used in short name, so compute the short name by 94 // demanded. 95 if (ShortStr.empty()) 96 initShortStr(); 97 return ShortStr; 98 } 99 100 bool isValid() const { return Valid; } 101 bool isScalar() const { return Scale.hasValue() && Scale.getValue() == 0; } 102 bool isVector() const { return Scale.hasValue() && Scale.getValue() != 0; } 103 bool isFloat() const { return ScalarType == ScalarTypeKind::Float; } 104 bool isSignedInteger() const { 105 return ScalarType == ScalarTypeKind::SignedInteger; 106 } 107 bool isFloatVector(unsigned Width) const { 108 return isVector() && isFloat() && ElementBitwidth == Width; 109 } 110 bool isFloat(unsigned Width) const { 111 return isFloat() && ElementBitwidth == Width; 112 } 113 114 private: 115 // Verify RVV vector type and set Valid. 116 bool verifyType() const; 117 118 // Creates a type based on basic types of TypeRange 119 void applyBasicType(); 120 121 // Applies a prototype modifier to the current type. The result maybe an 122 // invalid type. 123 void applyModifier(StringRef prototype); 124 125 // Compute and record a string for legal type. 126 void initBuiltinStr(); 127 // Compute and record a builtin RVV vector type string. 128 void initClangBuiltinStr(); 129 // Compute and record a type string for used in the header. 130 void initTypeStr(); 131 // Compute and record a short name of a type for C/C++ name suffix. 132 void initShortStr(); 133 }; 134 135 using RVVTypePtr = RVVType *; 136 using RVVTypes = std::vector<RVVTypePtr>; 137 138 enum RISCVExtension : uint8_t { 139 Basic = 0, 140 F = 1 << 1, 141 D = 1 << 2, 142 Zfh = 1 << 3, 143 Zvamo = 1 << 4, 144 Zvlsseg = 1 << 5, 145 }; 146 147 // TODO refactor RVVIntrinsic class design after support all intrinsic 148 // combination. This represents an instantiation of an intrinsic with a 149 // particular type and prototype 150 class RVVIntrinsic { 151 152 private: 153 std::string Name; // Builtin name 154 std::string MangledName; 155 std::string IRName; 156 bool HasSideEffects; 157 bool IsMask; 158 bool HasMaskedOffOperand; 159 bool HasVL; 160 bool HasNoMaskedOverloaded; 161 bool HasAutoDef; // There is automiatic definition in header 162 std::string ManualCodegen; 163 RVVTypePtr OutputType; // Builtin output type 164 RVVTypes InputTypes; // Builtin input types 165 // The types we use to obtain the specific LLVM intrinsic. They are index of 166 // InputTypes. -1 means the return type. 167 std::vector<int64_t> IntrinsicTypes; 168 uint8_t RISCVExtensions = 0; 169 unsigned NF = 1; 170 171 public: 172 RVVIntrinsic(StringRef Name, StringRef Suffix, StringRef MangledName, 173 StringRef IRName, bool HasSideEffects, bool IsMask, 174 bool HasMaskedOffOperand, bool HasVL, bool HasNoMaskedOverloaded, 175 bool HasAutoDef, StringRef ManualCodegen, const RVVTypes &Types, 176 const std::vector<int64_t> &IntrinsicTypes, 177 StringRef RequiredExtension, unsigned NF); 178 ~RVVIntrinsic() = default; 179 180 StringRef getName() const { return Name; } 181 StringRef getMangledName() const { return MangledName; } 182 bool hasSideEffects() const { return HasSideEffects; } 183 bool hasMaskedOffOperand() const { return HasMaskedOffOperand; } 184 bool hasVL() const { return HasVL; } 185 bool hasNoMaskedOverloaded() const { return HasNoMaskedOverloaded; } 186 bool hasManualCodegen() const { return !ManualCodegen.empty(); } 187 bool hasAutoDef() const { return HasAutoDef; } 188 bool isMask() const { return IsMask; } 189 StringRef getIRName() const { return IRName; } 190 StringRef getManualCodegen() const { return ManualCodegen; } 191 uint8_t getRISCVExtensions() const { return RISCVExtensions; } 192 unsigned getNF() const { return NF; } 193 194 // Return the type string for a BUILTIN() macro in Builtins.def. 195 std::string getBuiltinTypeStr() const; 196 197 // Emit the code block for switch body in EmitRISCVBuiltinExpr, it should 198 // init the RVVIntrinsic ID and IntrinsicTypes. 199 void emitCodeGenSwitchBody(raw_ostream &o) const; 200 201 // Emit the macros for mapping C/C++ intrinsic function to builtin functions. 202 void emitIntrinsicMacro(raw_ostream &o) const; 203 204 // Emit the mangled function definition. 205 void emitMangledFuncDef(raw_ostream &o) const; 206 }; 207 208 class RVVEmitter { 209 private: 210 RecordKeeper &Records; 211 std::string HeaderCode; 212 // Concat BasicType, LMUL and Proto as key 213 StringMap<RVVType> LegalTypes; 214 StringSet<> IllegalTypes; 215 216 public: 217 RVVEmitter(RecordKeeper &R) : Records(R) {} 218 219 /// Emit riscv_vector.h 220 void createHeader(raw_ostream &o); 221 222 /// Emit all the __builtin prototypes and code needed by Sema. 223 void createBuiltins(raw_ostream &o); 224 225 /// Emit all the information needed to map builtin -> LLVM IR intrinsic. 226 void createCodeGen(raw_ostream &o); 227 228 std::string getSuffixStr(char Type, int Log2LMUL, StringRef Prototypes); 229 230 private: 231 /// Create all intrinsics and add them to \p Out 232 void createRVVIntrinsics(std::vector<std::unique_ptr<RVVIntrinsic>> &Out); 233 /// Compute output and input types by applying different config (basic type 234 /// and LMUL with type transformers). It also record result of type in legal 235 /// or illegal set to avoid compute the same config again. The result maybe 236 /// have illegal RVVType. 237 Optional<RVVTypes> computeTypes(BasicType BT, int Log2LMUL, unsigned NF, 238 ArrayRef<std::string> PrototypeSeq); 239 Optional<RVVTypePtr> computeType(BasicType BT, int Log2LMUL, StringRef Proto); 240 241 /// Emit Acrh predecessor definitions and body, assume the element of Defs are 242 /// sorted by extension. 243 void emitArchMacroAndBody( 244 std::vector<std::unique_ptr<RVVIntrinsic>> &Defs, raw_ostream &o, 245 std::function<void(raw_ostream &, const RVVIntrinsic &)>); 246 247 // Emit the architecture preprocessor definitions. Return true when emits 248 // non-empty string. 249 bool emitExtDefStr(uint8_t Extensions, raw_ostream &o); 250 // Slice Prototypes string into sub prototype string and process each sub 251 // prototype string individually in the Handler. 252 void parsePrototypes(StringRef Prototypes, 253 std::function<void(StringRef)> Handler); 254 }; 255 256 } // namespace 257 258 //===----------------------------------------------------------------------===// 259 // Type implementation 260 //===----------------------------------------------------------------------===// 261 262 LMULType::LMULType(int NewLog2LMUL) { 263 // Check Log2LMUL is -3, -2, -1, 0, 1, 2, 3 264 assert(NewLog2LMUL <= 3 && NewLog2LMUL >= -3 && "Bad LMUL number!"); 265 Log2LMUL = NewLog2LMUL; 266 } 267 268 std::string LMULType::str() const { 269 if (Log2LMUL < 0) 270 return "mf" + utostr(1ULL << (-Log2LMUL)); 271 return "m" + utostr(1ULL << Log2LMUL); 272 } 273 274 VScaleVal LMULType::getScale(unsigned ElementBitwidth) const { 275 int Log2ScaleResult = 0; 276 switch (ElementBitwidth) { 277 default: 278 break; 279 case 8: 280 Log2ScaleResult = Log2LMUL + 3; 281 break; 282 case 16: 283 Log2ScaleResult = Log2LMUL + 2; 284 break; 285 case 32: 286 Log2ScaleResult = Log2LMUL + 1; 287 break; 288 case 64: 289 Log2ScaleResult = Log2LMUL; 290 break; 291 } 292 // Illegal vscale result would be less than 1 293 if (Log2ScaleResult < 0) 294 return None; 295 return 1 << Log2ScaleResult; 296 } 297 298 void LMULType::MulLog2LMUL(int log2LMUL) { Log2LMUL += log2LMUL; } 299 300 LMULType &LMULType::operator*=(uint32_t RHS) { 301 assert(isPowerOf2_32(RHS)); 302 this->Log2LMUL = this->Log2LMUL + Log2_32(RHS); 303 return *this; 304 } 305 306 RVVType::RVVType(BasicType BT, int Log2LMUL, StringRef prototype) 307 : BT(BT), LMUL(LMULType(Log2LMUL)) { 308 applyBasicType(); 309 applyModifier(prototype); 310 Valid = verifyType(); 311 if (Valid) { 312 initBuiltinStr(); 313 initTypeStr(); 314 if (isVector()) { 315 initClangBuiltinStr(); 316 } 317 } 318 } 319 320 // clang-format off 321 // boolean type are encoded the ratio of n (SEW/LMUL) 322 // SEW/LMUL | 1 | 2 | 4 | 8 | 16 | 32 | 64 323 // c type | vbool64_t | vbool32_t | vbool16_t | vbool8_t | vbool4_t | vbool2_t | vbool1_t 324 // IR type | nxv1i1 | nxv2i1 | nxv4i1 | nxv8i1 | nxv16i1 | nxv32i1 | nxv64i1 325 326 // type\lmul | 1/8 | 1/4 | 1/2 | 1 | 2 | 4 | 8 327 // -------- |------ | -------- | ------- | ------- | -------- | -------- | -------- 328 // i64 | N/A | N/A | N/A | nxv1i64 | nxv2i64 | nxv4i64 | nxv8i64 329 // i32 | N/A | N/A | nxv1i32 | nxv2i32 | nxv4i32 | nxv8i32 | nxv16i32 330 // i16 | N/A | nxv1i16 | nxv2i16 | nxv4i16 | nxv8i16 | nxv16i16 | nxv32i16 331 // i8 | nxv1i8 | nxv2i8 | nxv4i8 | nxv8i8 | nxv16i8 | nxv32i8 | nxv64i8 332 // double | N/A | N/A | N/A | nxv1f64 | nxv2f64 | nxv4f64 | nxv8f64 333 // float | N/A | N/A | nxv1f32 | nxv2f32 | nxv4f32 | nxv8f32 | nxv16f32 334 // half | N/A | nxv1f16 | nxv2f16 | nxv4f16 | nxv8f16 | nxv16f16 | nxv32f16 335 // clang-format on 336 337 bool RVVType::verifyType() const { 338 if (ScalarType == Invalid) 339 return false; 340 if (isScalar()) 341 return true; 342 if (!Scale.hasValue()) 343 return false; 344 if (isFloat() && ElementBitwidth == 8) 345 return false; 346 unsigned V = Scale.getValue(); 347 switch (ElementBitwidth) { 348 case 1: 349 case 8: 350 // Check Scale is 1,2,4,8,16,32,64 351 return (V <= 64 && isPowerOf2_32(V)); 352 case 16: 353 // Check Scale is 1,2,4,8,16,32 354 return (V <= 32 && isPowerOf2_32(V)); 355 case 32: 356 // Check Scale is 1,2,4,8,16 357 return (V <= 16 && isPowerOf2_32(V)); 358 case 64: 359 // Check Scale is 1,2,4,8 360 return (V <= 8 && isPowerOf2_32(V)); 361 } 362 return false; 363 } 364 365 void RVVType::initBuiltinStr() { 366 assert(isValid() && "RVVType is invalid"); 367 switch (ScalarType) { 368 case ScalarTypeKind::Void: 369 BuiltinStr = "v"; 370 return; 371 case ScalarTypeKind::Size_t: 372 BuiltinStr = "z"; 373 if (IsImmediate) 374 BuiltinStr = "I" + BuiltinStr; 375 if (IsPointer) 376 BuiltinStr += "*"; 377 return; 378 case ScalarTypeKind::Ptrdiff_t: 379 BuiltinStr = "Y"; 380 return; 381 case ScalarTypeKind::UnsignedLong: 382 BuiltinStr = "ULi"; 383 return; 384 case ScalarTypeKind::SignedLong: 385 BuiltinStr = "Li"; 386 return; 387 case ScalarTypeKind::Boolean: 388 assert(ElementBitwidth == 1); 389 BuiltinStr += "b"; 390 break; 391 case ScalarTypeKind::SignedInteger: 392 case ScalarTypeKind::UnsignedInteger: 393 switch (ElementBitwidth) { 394 case 8: 395 BuiltinStr += "c"; 396 break; 397 case 16: 398 BuiltinStr += "s"; 399 break; 400 case 32: 401 BuiltinStr += "i"; 402 break; 403 case 64: 404 BuiltinStr += "Wi"; 405 break; 406 default: 407 llvm_unreachable("Unhandled ElementBitwidth!"); 408 } 409 if (isSignedInteger()) 410 BuiltinStr = "S" + BuiltinStr; 411 else 412 BuiltinStr = "U" + BuiltinStr; 413 break; 414 case ScalarTypeKind::Float: 415 switch (ElementBitwidth) { 416 case 16: 417 BuiltinStr += "h"; 418 break; 419 case 32: 420 BuiltinStr += "f"; 421 break; 422 case 64: 423 BuiltinStr += "d"; 424 break; 425 default: 426 llvm_unreachable("Unhandled ElementBitwidth!"); 427 } 428 break; 429 default: 430 llvm_unreachable("ScalarType is invalid!"); 431 } 432 if (IsImmediate) 433 BuiltinStr = "I" + BuiltinStr; 434 if (isScalar()) { 435 if (IsConstant) 436 BuiltinStr += "C"; 437 if (IsPointer) 438 BuiltinStr += "*"; 439 return; 440 } 441 BuiltinStr = "q" + utostr(Scale.getValue()) + BuiltinStr; 442 // Pointer to vector types. Defined for Zvlsseg load intrinsics. 443 // Zvlsseg load intrinsics have pointer type arguments to store the loaded 444 // vector values. 445 if (IsPointer) 446 BuiltinStr += "*"; 447 } 448 449 void RVVType::initClangBuiltinStr() { 450 assert(isValid() && "RVVType is invalid"); 451 assert(isVector() && "Handle Vector type only"); 452 453 ClangBuiltinStr = "__rvv_"; 454 switch (ScalarType) { 455 case ScalarTypeKind::Boolean: 456 ClangBuiltinStr += "bool" + utostr(64 / Scale.getValue()) + "_t"; 457 return; 458 case ScalarTypeKind::Float: 459 ClangBuiltinStr += "float"; 460 break; 461 case ScalarTypeKind::SignedInteger: 462 ClangBuiltinStr += "int"; 463 break; 464 case ScalarTypeKind::UnsignedInteger: 465 ClangBuiltinStr += "uint"; 466 break; 467 default: 468 llvm_unreachable("ScalarTypeKind is invalid"); 469 } 470 ClangBuiltinStr += utostr(ElementBitwidth) + LMUL.str() + "_t"; 471 } 472 473 void RVVType::initTypeStr() { 474 assert(isValid() && "RVVType is invalid"); 475 476 if (IsConstant) 477 Str += "const "; 478 479 auto getTypeString = [&](StringRef TypeStr) { 480 if (isScalar()) 481 return Twine(TypeStr + Twine(ElementBitwidth) + "_t").str(); 482 return Twine("v" + TypeStr + Twine(ElementBitwidth) + LMUL.str() + "_t") 483 .str(); 484 }; 485 486 switch (ScalarType) { 487 case ScalarTypeKind::Void: 488 Str = "void"; 489 return; 490 case ScalarTypeKind::Size_t: 491 Str = "size_t"; 492 if (IsPointer) 493 Str += " *"; 494 return; 495 case ScalarTypeKind::Ptrdiff_t: 496 Str = "ptrdiff_t"; 497 return; 498 case ScalarTypeKind::UnsignedLong: 499 Str = "unsigned long"; 500 return; 501 case ScalarTypeKind::SignedLong: 502 Str = "long"; 503 return; 504 case ScalarTypeKind::Boolean: 505 if (isScalar()) 506 Str += "bool"; 507 else 508 // Vector bool is special case, the formulate is 509 // `vbool<N>_t = MVT::nxv<64/N>i1` ex. vbool16_t = MVT::4i1 510 Str += "vbool" + utostr(64 / Scale.getValue()) + "_t"; 511 break; 512 case ScalarTypeKind::Float: 513 if (isScalar()) { 514 if (ElementBitwidth == 64) 515 Str += "double"; 516 else if (ElementBitwidth == 32) 517 Str += "float"; 518 assert((ElementBitwidth == 32 || ElementBitwidth == 64) && 519 "Unhandled floating type"); 520 } else 521 Str += getTypeString("float"); 522 break; 523 case ScalarTypeKind::SignedInteger: 524 Str += getTypeString("int"); 525 break; 526 case ScalarTypeKind::UnsignedInteger: 527 Str += getTypeString("uint"); 528 break; 529 default: 530 llvm_unreachable("ScalarType is invalid!"); 531 } 532 if (IsPointer) 533 Str += " *"; 534 } 535 536 void RVVType::initShortStr() { 537 switch (ScalarType) { 538 case ScalarTypeKind::Boolean: 539 assert(isVector()); 540 ShortStr = "b" + utostr(64 / Scale.getValue()); 541 return; 542 case ScalarTypeKind::Float: 543 ShortStr = "f" + utostr(ElementBitwidth); 544 break; 545 case ScalarTypeKind::SignedInteger: 546 ShortStr = "i" + utostr(ElementBitwidth); 547 break; 548 case ScalarTypeKind::UnsignedInteger: 549 ShortStr = "u" + utostr(ElementBitwidth); 550 break; 551 default: 552 PrintFatalError("Unhandled case!"); 553 } 554 if (isVector()) 555 ShortStr += LMUL.str(); 556 } 557 558 void RVVType::applyBasicType() { 559 switch (BT) { 560 case 'c': 561 ElementBitwidth = 8; 562 ScalarType = ScalarTypeKind::SignedInteger; 563 break; 564 case 's': 565 ElementBitwidth = 16; 566 ScalarType = ScalarTypeKind::SignedInteger; 567 break; 568 case 'i': 569 ElementBitwidth = 32; 570 ScalarType = ScalarTypeKind::SignedInteger; 571 break; 572 case 'l': 573 ElementBitwidth = 64; 574 ScalarType = ScalarTypeKind::SignedInteger; 575 break; 576 case 'h': 577 ElementBitwidth = 16; 578 ScalarType = ScalarTypeKind::Float; 579 break; 580 case 'f': 581 ElementBitwidth = 32; 582 ScalarType = ScalarTypeKind::Float; 583 break; 584 case 'd': 585 ElementBitwidth = 64; 586 ScalarType = ScalarTypeKind::Float; 587 break; 588 default: 589 PrintFatalError("Unhandled type code!"); 590 } 591 assert(ElementBitwidth != 0 && "Bad element bitwidth!"); 592 } 593 594 void RVVType::applyModifier(StringRef Transformer) { 595 if (Transformer.empty()) 596 return; 597 // Handle primitive type transformer 598 auto PType = Transformer.back(); 599 switch (PType) { 600 case 'e': 601 Scale = 0; 602 break; 603 case 'v': 604 Scale = LMUL.getScale(ElementBitwidth); 605 break; 606 case 'w': 607 ElementBitwidth *= 2; 608 LMUL *= 2; 609 Scale = LMUL.getScale(ElementBitwidth); 610 break; 611 case 'q': 612 ElementBitwidth *= 4; 613 LMUL *= 4; 614 Scale = LMUL.getScale(ElementBitwidth); 615 break; 616 case 'o': 617 ElementBitwidth *= 8; 618 LMUL *= 8; 619 Scale = LMUL.getScale(ElementBitwidth); 620 break; 621 case 'm': 622 ScalarType = ScalarTypeKind::Boolean; 623 Scale = LMUL.getScale(ElementBitwidth); 624 ElementBitwidth = 1; 625 break; 626 case '0': 627 ScalarType = ScalarTypeKind::Void; 628 break; 629 case 'z': 630 ScalarType = ScalarTypeKind::Size_t; 631 break; 632 case 't': 633 ScalarType = ScalarTypeKind::Ptrdiff_t; 634 break; 635 case 'u': 636 ScalarType = ScalarTypeKind::UnsignedLong; 637 break; 638 case 'l': 639 ScalarType = ScalarTypeKind::SignedLong; 640 break; 641 default: 642 PrintFatalError("Illegal primitive type transformers!"); 643 } 644 Transformer = Transformer.drop_back(); 645 646 // Extract and compute complex type transformer. It can only appear one time. 647 if (Transformer.startswith("(")) { 648 size_t Idx = Transformer.find(')'); 649 assert(Idx != StringRef::npos); 650 StringRef ComplexType = Transformer.slice(1, Idx); 651 Transformer = Transformer.drop_front(Idx + 1); 652 assert(Transformer.find('(') == StringRef::npos && 653 "Only allow one complex type transformer"); 654 655 auto UpdateAndCheckComplexProto = [&]() { 656 Scale = LMUL.getScale(ElementBitwidth); 657 const StringRef VectorPrototypes("vwqom"); 658 if (!VectorPrototypes.contains(PType)) 659 PrintFatalError("Complex type transformer only supports vector type!"); 660 if (Transformer.find_first_of("PCKWS") != StringRef::npos) 661 PrintFatalError( 662 "Illegal type transformer for Complex type transformer"); 663 }; 664 auto ComputeFixedLog2LMUL = 665 [&](StringRef Value, 666 std::function<bool(const int32_t &, const int32_t &)> Compare) { 667 int32_t Log2LMUL; 668 Value.getAsInteger(10, Log2LMUL); 669 if (!Compare(Log2LMUL, LMUL.Log2LMUL)) { 670 ScalarType = Invalid; 671 return false; 672 } 673 // Update new LMUL 674 LMUL = LMULType(Log2LMUL); 675 UpdateAndCheckComplexProto(); 676 return true; 677 }; 678 auto ComplexTT = ComplexType.split(":"); 679 if (ComplexTT.first == "Log2EEW") { 680 uint32_t Log2EEW; 681 ComplexTT.second.getAsInteger(10, Log2EEW); 682 // update new elmul = (eew/sew) * lmul 683 LMUL.MulLog2LMUL(Log2EEW - Log2_32(ElementBitwidth)); 684 // update new eew 685 ElementBitwidth = 1 << Log2EEW; 686 ScalarType = ScalarTypeKind::SignedInteger; 687 UpdateAndCheckComplexProto(); 688 } else if (ComplexTT.first == "FixedSEW") { 689 uint32_t NewSEW; 690 ComplexTT.second.getAsInteger(10, NewSEW); 691 // Set invalid type if src and dst SEW are same. 692 if (ElementBitwidth == NewSEW) { 693 ScalarType = Invalid; 694 return; 695 } 696 // Update new SEW 697 ElementBitwidth = NewSEW; 698 UpdateAndCheckComplexProto(); 699 } else if (ComplexTT.first == "LFixedLog2LMUL") { 700 // New LMUL should be larger than old 701 if (!ComputeFixedLog2LMUL(ComplexTT.second, std::greater<int32_t>())) 702 return; 703 } else if (ComplexTT.first == "SFixedLog2LMUL") { 704 // New LMUL should be smaller than old 705 if (!ComputeFixedLog2LMUL(ComplexTT.second, std::less<int32_t>())) 706 return; 707 } else { 708 PrintFatalError("Illegal complex type transformers!"); 709 } 710 } 711 712 // Compute the remain type transformers 713 for (char I : Transformer) { 714 switch (I) { 715 case 'P': 716 if (IsConstant) 717 PrintFatalError("'P' transformer cannot be used after 'C'"); 718 if (IsPointer) 719 PrintFatalError("'P' transformer cannot be used twice"); 720 IsPointer = true; 721 break; 722 case 'C': 723 if (IsConstant) 724 PrintFatalError("'C' transformer cannot be used twice"); 725 IsConstant = true; 726 break; 727 case 'K': 728 IsImmediate = true; 729 break; 730 case 'U': 731 ScalarType = ScalarTypeKind::UnsignedInteger; 732 break; 733 case 'I': 734 ScalarType = ScalarTypeKind::SignedInteger; 735 break; 736 case 'F': 737 ScalarType = ScalarTypeKind::Float; 738 break; 739 case 'S': 740 LMUL = LMULType(0); 741 // Update ElementBitwidth need to update Scale too. 742 Scale = LMUL.getScale(ElementBitwidth); 743 break; 744 default: 745 PrintFatalError("Illegal non-primitive type transformer!"); 746 } 747 } 748 } 749 750 //===----------------------------------------------------------------------===// 751 // RVVIntrinsic implementation 752 //===----------------------------------------------------------------------===// 753 RVVIntrinsic::RVVIntrinsic(StringRef NewName, StringRef Suffix, 754 StringRef NewMangledName, StringRef IRName, 755 bool HasSideEffects, bool IsMask, 756 bool HasMaskedOffOperand, bool HasVL, 757 bool HasNoMaskedOverloaded, bool HasAutoDef, 758 StringRef ManualCodegen, const RVVTypes &OutInTypes, 759 const std::vector<int64_t> &NewIntrinsicTypes, 760 StringRef RequiredExtension, unsigned NF) 761 : IRName(IRName), HasSideEffects(HasSideEffects), IsMask(IsMask), 762 HasMaskedOffOperand(HasMaskedOffOperand), HasVL(HasVL), 763 HasNoMaskedOverloaded(HasNoMaskedOverloaded), HasAutoDef(HasAutoDef), 764 ManualCodegen(ManualCodegen.str()), NF(NF) { 765 766 // Init Name and MangledName 767 Name = NewName.str(); 768 if (NewMangledName.empty()) 769 MangledName = NewName.split("_").first.str(); 770 else 771 MangledName = NewMangledName.str(); 772 if (!Suffix.empty()) 773 Name += "_" + Suffix.str(); 774 if (IsMask) { 775 Name += "_m"; 776 } 777 // Init RISC-V extensions 778 for (const auto &T : OutInTypes) { 779 if (T->isFloatVector(16) || T->isFloat(16)) 780 RISCVExtensions |= RISCVExtension::Zfh; 781 else if (T->isFloatVector(32) || T->isFloat(32)) 782 RISCVExtensions |= RISCVExtension::F; 783 else if (T->isFloatVector(64) || T->isFloat(64)) 784 RISCVExtensions |= RISCVExtension::D; 785 } 786 if (RequiredExtension == "Zvamo") 787 RISCVExtensions |= RISCVExtension::Zvamo; 788 if (RequiredExtension == "Zvlsseg") 789 RISCVExtensions |= RISCVExtension::Zvlsseg; 790 791 // Init OutputType and InputTypes 792 OutputType = OutInTypes[0]; 793 InputTypes.assign(OutInTypes.begin() + 1, OutInTypes.end()); 794 795 // IntrinsicTypes is nonmasked version index. Need to update it 796 // if there is maskedoff operand (It is always in first operand). 797 IntrinsicTypes = NewIntrinsicTypes; 798 if (IsMask && HasMaskedOffOperand) { 799 for (auto &I : IntrinsicTypes) { 800 if (I >= 0) 801 I += NF; 802 } 803 } 804 } 805 806 std::string RVVIntrinsic::getBuiltinTypeStr() const { 807 std::string S; 808 S += OutputType->getBuiltinStr(); 809 for (const auto &T : InputTypes) { 810 S += T->getBuiltinStr(); 811 } 812 return S; 813 } 814 815 void RVVIntrinsic::emitCodeGenSwitchBody(raw_ostream &OS) const { 816 if (!getIRName().empty()) 817 OS << " ID = Intrinsic::riscv_" + getIRName() + ";\n"; 818 if (NF >= 2) 819 OS << " NF = " + utostr(getNF()) + ";\n"; 820 if (hasManualCodegen()) { 821 OS << ManualCodegen; 822 OS << "break;\n"; 823 return; 824 } 825 826 if (isMask()) { 827 if (hasVL()) { 828 OS << " std::rotate(Ops.begin(), Ops.begin() + 1, Ops.end() - 1);\n"; 829 } else { 830 OS << " std::rotate(Ops.begin(), Ops.begin() + 1, Ops.end());\n"; 831 } 832 } 833 834 OS << " IntrinsicTypes = {"; 835 ListSeparator LS; 836 for (const auto &Idx : IntrinsicTypes) { 837 if (Idx == -1) 838 OS << LS << "ResultType"; 839 else 840 OS << LS << "Ops[" << Idx << "]->getType()"; 841 } 842 843 // VL could be i64 or i32, need to encode it in IntrinsicTypes. VL is 844 // always last operand. 845 if (hasVL()) 846 OS << ", Ops.back()->getType()"; 847 OS << "};\n"; 848 OS << " break;\n"; 849 } 850 851 void RVVIntrinsic::emitIntrinsicMacro(raw_ostream &OS) const { 852 OS << "#define " << getName() << "("; 853 if (!InputTypes.empty()) { 854 ListSeparator LS; 855 for (unsigned i = 0, e = InputTypes.size(); i != e; ++i) 856 OS << LS << "op" << i; 857 } 858 OS << ") \\\n"; 859 OS << "__builtin_rvv_" << getName() << "("; 860 if (!InputTypes.empty()) { 861 ListSeparator LS; 862 for (unsigned i = 0, e = InputTypes.size(); i != e; ++i) 863 OS << LS << "(" << InputTypes[i]->getTypeStr() << ")(op" << i << ")"; 864 } 865 OS << ")\n"; 866 } 867 868 void RVVIntrinsic::emitMangledFuncDef(raw_ostream &OS) const { 869 OS << "__attribute__((clang_builtin_alias("; 870 OS << "__builtin_rvv_" << getName() << ")))\n"; 871 OS << OutputType->getTypeStr() << " " << getMangledName() << "("; 872 // Emit function arguments 873 if (!InputTypes.empty()) { 874 ListSeparator LS; 875 for (unsigned i = 0; i < InputTypes.size(); ++i) 876 OS << LS << InputTypes[i]->getTypeStr() << " op" << i; 877 } 878 OS << ");\n\n"; 879 } 880 881 //===----------------------------------------------------------------------===// 882 // RVVEmitter implementation 883 //===----------------------------------------------------------------------===// 884 void RVVEmitter::createHeader(raw_ostream &OS) { 885 886 OS << "/*===---- riscv_vector.h - RISC-V V-extension RVVIntrinsics " 887 "-------------------===\n" 888 " *\n" 889 " *\n" 890 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM " 891 "Exceptions.\n" 892 " * See https://llvm.org/LICENSE.txt for license information.\n" 893 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" 894 " *\n" 895 " *===-----------------------------------------------------------------" 896 "------===\n" 897 " */\n\n"; 898 899 OS << "#ifndef __RISCV_VECTOR_H\n"; 900 OS << "#define __RISCV_VECTOR_H\n\n"; 901 902 OS << "#include <stdint.h>\n"; 903 OS << "#include <stddef.h>\n\n"; 904 905 OS << "#ifndef __riscv_vector\n"; 906 OS << "#error \"Vector intrinsics require the vector extension.\"\n"; 907 OS << "#endif\n\n"; 908 909 OS << "#ifdef __cplusplus\n"; 910 OS << "extern \"C\" {\n"; 911 OS << "#endif\n\n"; 912 913 std::vector<std::unique_ptr<RVVIntrinsic>> Defs; 914 createRVVIntrinsics(Defs); 915 916 // Print header code 917 if (!HeaderCode.empty()) { 918 OS << HeaderCode; 919 } 920 921 auto printType = [&](auto T) { 922 OS << "typedef " << T->getClangBuiltinStr() << " " << T->getTypeStr() 923 << ";\n"; 924 }; 925 926 constexpr int Log2LMULs[] = {-3, -2, -1, 0, 1, 2, 3}; 927 // Print RVV boolean types. 928 for (int Log2LMUL : Log2LMULs) { 929 auto T = computeType('c', Log2LMUL, "m"); 930 if (T.hasValue()) 931 printType(T.getValue()); 932 } 933 // Print RVV int/float types. 934 for (char I : StringRef("csil")) { 935 for (int Log2LMUL : Log2LMULs) { 936 auto T = computeType(I, Log2LMUL, "v"); 937 if (T.hasValue()) { 938 printType(T.getValue()); 939 auto UT = computeType(I, Log2LMUL, "Uv"); 940 printType(UT.getValue()); 941 } 942 } 943 } 944 OS << "#if defined(__riscv_zfh)\n"; 945 for (int Log2LMUL : Log2LMULs) { 946 auto T = computeType('h', Log2LMUL, "v"); 947 if (T.hasValue()) 948 printType(T.getValue()); 949 } 950 OS << "#endif\n"; 951 952 OS << "#if defined(__riscv_f)\n"; 953 for (int Log2LMUL : Log2LMULs) { 954 auto T = computeType('f', Log2LMUL, "v"); 955 if (T.hasValue()) 956 printType(T.getValue()); 957 } 958 OS << "#endif\n"; 959 960 OS << "#if defined(__riscv_d)\n"; 961 for (int Log2LMUL : Log2LMULs) { 962 auto T = computeType('d', Log2LMUL, "v"); 963 if (T.hasValue()) 964 printType(T.getValue()); 965 } 966 OS << "#endif\n\n"; 967 968 // The same extension include in the same arch guard marco. 969 std::stable_sort(Defs.begin(), Defs.end(), 970 [](const std::unique_ptr<RVVIntrinsic> &A, 971 const std::unique_ptr<RVVIntrinsic> &B) { 972 return A->getRISCVExtensions() < B->getRISCVExtensions(); 973 }); 974 975 // Print intrinsic functions with macro 976 emitArchMacroAndBody(Defs, OS, [](raw_ostream &OS, const RVVIntrinsic &Inst) { 977 Inst.emitIntrinsicMacro(OS); 978 }); 979 980 OS << "#define __riscv_v_intrinsic_overloading 1\n"; 981 982 // Print Overloaded APIs 983 OS << "#define __rvv_overloaded static inline " 984 "__attribute__((__always_inline__, __nodebug__, __overloadable__))\n"; 985 986 emitArchMacroAndBody(Defs, OS, [](raw_ostream &OS, const RVVIntrinsic &Inst) { 987 if (!Inst.isMask() && !Inst.hasNoMaskedOverloaded()) 988 return; 989 OS << "__rvv_overloaded "; 990 Inst.emitMangledFuncDef(OS); 991 }); 992 993 OS << "\n#ifdef __cplusplus\n"; 994 OS << "}\n"; 995 OS << "#endif // __riscv_vector\n"; 996 OS << "#endif // __RISCV_VECTOR_H\n"; 997 } 998 999 void RVVEmitter::createBuiltins(raw_ostream &OS) { 1000 std::vector<std::unique_ptr<RVVIntrinsic>> Defs; 1001 createRVVIntrinsics(Defs); 1002 1003 OS << "#if defined(TARGET_BUILTIN) && !defined(RISCVV_BUILTIN)\n"; 1004 OS << "#define RISCVV_BUILTIN(ID, TYPE, ATTRS) TARGET_BUILTIN(ID, TYPE, " 1005 "ATTRS, \"experimental-v\")\n"; 1006 OS << "#endif\n"; 1007 for (auto &Def : Defs) { 1008 OS << "RISCVV_BUILTIN(__builtin_rvv_" << Def->getName() << ",\"" 1009 << Def->getBuiltinTypeStr() << "\", "; 1010 if (!Def->hasSideEffects()) 1011 OS << "\"n\")\n"; 1012 else 1013 OS << "\"\")\n"; 1014 } 1015 OS << "#undef RISCVV_BUILTIN\n"; 1016 } 1017 1018 void RVVEmitter::createCodeGen(raw_ostream &OS) { 1019 std::vector<std::unique_ptr<RVVIntrinsic>> Defs; 1020 createRVVIntrinsics(Defs); 1021 // IR name could be empty, use the stable sort preserves the relative order. 1022 std::stable_sort(Defs.begin(), Defs.end(), 1023 [](const std::unique_ptr<RVVIntrinsic> &A, 1024 const std::unique_ptr<RVVIntrinsic> &B) { 1025 return A->getIRName() < B->getIRName(); 1026 }); 1027 // Print switch body when the ir name or ManualCodegen changes from previous 1028 // iteration. 1029 RVVIntrinsic *PrevDef = Defs.begin()->get(); 1030 for (auto &Def : Defs) { 1031 StringRef CurIRName = Def->getIRName(); 1032 if (CurIRName != PrevDef->getIRName() || 1033 (Def->getManualCodegen() != PrevDef->getManualCodegen())) { 1034 PrevDef->emitCodeGenSwitchBody(OS); 1035 } 1036 PrevDef = Def.get(); 1037 OS << "case RISCV::BI__builtin_rvv_" << Def->getName() << ":\n"; 1038 } 1039 Defs.back()->emitCodeGenSwitchBody(OS); 1040 OS << "\n"; 1041 } 1042 1043 void RVVEmitter::parsePrototypes(StringRef Prototypes, 1044 std::function<void(StringRef)> Handler) { 1045 const StringRef Primaries("evwqom0ztul"); 1046 while (!Prototypes.empty()) { 1047 size_t Idx = 0; 1048 // Skip over complex prototype because it could contain primitive type 1049 // character. 1050 if (Prototypes[0] == '(') 1051 Idx = Prototypes.find_first_of(')'); 1052 Idx = Prototypes.find_first_of(Primaries, Idx); 1053 assert(Idx != StringRef::npos); 1054 Handler(Prototypes.slice(0, Idx + 1)); 1055 Prototypes = Prototypes.drop_front(Idx + 1); 1056 } 1057 } 1058 1059 std::string RVVEmitter::getSuffixStr(char Type, int Log2LMUL, 1060 StringRef Prototypes) { 1061 SmallVector<std::string> SuffixStrs; 1062 parsePrototypes(Prototypes, [&](StringRef Proto) { 1063 auto T = computeType(Type, Log2LMUL, Proto); 1064 SuffixStrs.push_back(T.getValue()->getShortStr()); 1065 }); 1066 return join(SuffixStrs, "_"); 1067 } 1068 1069 void RVVEmitter::createRVVIntrinsics( 1070 std::vector<std::unique_ptr<RVVIntrinsic>> &Out) { 1071 std::vector<Record *> RV = Records.getAllDerivedDefinitions("RVVBuiltin"); 1072 for (auto *R : RV) { 1073 StringRef Name = R->getValueAsString("Name"); 1074 StringRef SuffixProto = R->getValueAsString("Suffix"); 1075 StringRef MangledName = R->getValueAsString("MangledName"); 1076 StringRef Prototypes = R->getValueAsString("Prototype"); 1077 StringRef TypeRange = R->getValueAsString("TypeRange"); 1078 bool HasMask = R->getValueAsBit("HasMask"); 1079 bool HasMaskedOffOperand = R->getValueAsBit("HasMaskedOffOperand"); 1080 bool HasVL = R->getValueAsBit("HasVL"); 1081 bool HasNoMaskedOverloaded = R->getValueAsBit("HasNoMaskedOverloaded"); 1082 bool HasSideEffects = R->getValueAsBit("HasSideEffects"); 1083 std::vector<int64_t> Log2LMULList = R->getValueAsListOfInts("Log2LMUL"); 1084 StringRef ManualCodegen = R->getValueAsString("ManualCodegen"); 1085 StringRef ManualCodegenMask = R->getValueAsString("ManualCodegenMask"); 1086 std::vector<int64_t> IntrinsicTypes = 1087 R->getValueAsListOfInts("IntrinsicTypes"); 1088 StringRef RequiredExtension = R->getValueAsString("RequiredExtension"); 1089 StringRef IRName = R->getValueAsString("IRName"); 1090 StringRef IRNameMask = R->getValueAsString("IRNameMask"); 1091 unsigned NF = R->getValueAsInt("NF"); 1092 1093 StringRef HeaderCodeStr = R->getValueAsString("HeaderCode"); 1094 bool HasAutoDef = HeaderCodeStr.empty(); 1095 if (!HeaderCodeStr.empty()) { 1096 HeaderCode += HeaderCodeStr.str(); 1097 } 1098 // Parse prototype and create a list of primitive type with transformers 1099 // (operand) in ProtoSeq. ProtoSeq[0] is output operand. 1100 SmallVector<std::string> ProtoSeq; 1101 parsePrototypes(Prototypes, [&ProtoSeq](StringRef Proto) { 1102 ProtoSeq.push_back(Proto.str()); 1103 }); 1104 1105 // Compute Builtin types 1106 SmallVector<std::string> ProtoMaskSeq = ProtoSeq; 1107 if (HasMask) { 1108 // If HasMaskedOffOperand, insert result type as first input operand. 1109 if (HasMaskedOffOperand) { 1110 if (NF == 1) { 1111 ProtoMaskSeq.insert(ProtoMaskSeq.begin() + 1, ProtoSeq[0]); 1112 } else { 1113 // Convert 1114 // (void, op0 address, op1 address, ...) 1115 // to 1116 // (void, op0 address, op1 address, ..., maskedoff0, maskedoff1, ...) 1117 for (unsigned I = 0; I < NF; ++I) 1118 ProtoMaskSeq.insert( 1119 ProtoMaskSeq.begin() + NF + 1, 1120 ProtoSeq[1].substr(1)); // Use substr(1) to skip '*' 1121 } 1122 } 1123 if (HasMaskedOffOperand && NF > 1) { 1124 // Convert 1125 // (void, op0 address, op1 address, ..., maskedoff0, maskedoff1, ...) 1126 // to 1127 // (void, op0 address, op1 address, ..., mask, maskedoff0, maskedoff1, 1128 // ...) 1129 ProtoMaskSeq.insert(ProtoMaskSeq.begin() + NF + 1, "m"); 1130 } else { 1131 // If HasMask, insert 'm' as first input operand. 1132 ProtoMaskSeq.insert(ProtoMaskSeq.begin() + 1, "m"); 1133 } 1134 } 1135 // If HasVL, append 'z' to last operand 1136 if (HasVL) { 1137 ProtoSeq.push_back("z"); 1138 ProtoMaskSeq.push_back("z"); 1139 } 1140 1141 // Create Intrinsics for each type and LMUL. 1142 for (char I : TypeRange) { 1143 for (int Log2LMUL : Log2LMULList) { 1144 Optional<RVVTypes> Types = computeTypes(I, Log2LMUL, NF, ProtoSeq); 1145 // Ignored to create new intrinsic if there are any illegal types. 1146 if (!Types.hasValue()) 1147 continue; 1148 1149 auto SuffixStr = getSuffixStr(I, Log2LMUL, SuffixProto); 1150 // Create a non-mask intrinsic 1151 Out.push_back(std::make_unique<RVVIntrinsic>( 1152 Name, SuffixStr, MangledName, IRName, HasSideEffects, 1153 /*IsMask=*/false, /*HasMaskedOffOperand=*/false, HasVL, 1154 HasNoMaskedOverloaded, HasAutoDef, ManualCodegen, Types.getValue(), 1155 IntrinsicTypes, RequiredExtension, NF)); 1156 if (HasMask) { 1157 // Create a mask intrinsic 1158 Optional<RVVTypes> MaskTypes = 1159 computeTypes(I, Log2LMUL, NF, ProtoMaskSeq); 1160 Out.push_back(std::make_unique<RVVIntrinsic>( 1161 Name, SuffixStr, MangledName, IRNameMask, HasSideEffects, 1162 /*IsMask=*/true, HasMaskedOffOperand, HasVL, 1163 HasNoMaskedOverloaded, HasAutoDef, ManualCodegenMask, 1164 MaskTypes.getValue(), IntrinsicTypes, RequiredExtension, NF)); 1165 } 1166 } // end for Log2LMULList 1167 } // end for TypeRange 1168 } 1169 } 1170 1171 Optional<RVVTypes> 1172 RVVEmitter::computeTypes(BasicType BT, int Log2LMUL, unsigned NF, 1173 ArrayRef<std::string> PrototypeSeq) { 1174 // LMUL x NF must be less than or equal to 8. 1175 if ((Log2LMUL >= 1) && (1 << Log2LMUL) * NF > 8) 1176 return llvm::None; 1177 1178 RVVTypes Types; 1179 for (const std::string &Proto : PrototypeSeq) { 1180 auto T = computeType(BT, Log2LMUL, Proto); 1181 if (!T.hasValue()) 1182 return llvm::None; 1183 // Record legal type index 1184 Types.push_back(T.getValue()); 1185 } 1186 return Types; 1187 } 1188 1189 Optional<RVVTypePtr> RVVEmitter::computeType(BasicType BT, int Log2LMUL, 1190 StringRef Proto) { 1191 std::string Idx = Twine(Twine(BT) + Twine(Log2LMUL) + Proto).str(); 1192 // Search first 1193 auto It = LegalTypes.find(Idx); 1194 if (It != LegalTypes.end()) 1195 return &(It->second); 1196 if (IllegalTypes.count(Idx)) 1197 return llvm::None; 1198 // Compute type and record the result. 1199 RVVType T(BT, Log2LMUL, Proto); 1200 if (T.isValid()) { 1201 // Record legal type index and value. 1202 LegalTypes.insert({Idx, T}); 1203 return &(LegalTypes[Idx]); 1204 } 1205 // Record illegal type index. 1206 IllegalTypes.insert(Idx); 1207 return llvm::None; 1208 } 1209 1210 void RVVEmitter::emitArchMacroAndBody( 1211 std::vector<std::unique_ptr<RVVIntrinsic>> &Defs, raw_ostream &OS, 1212 std::function<void(raw_ostream &, const RVVIntrinsic &)> PrintBody) { 1213 uint8_t PrevExt = (*Defs.begin())->getRISCVExtensions(); 1214 bool NeedEndif = emitExtDefStr(PrevExt, OS); 1215 for (auto &Def : Defs) { 1216 uint8_t CurExt = Def->getRISCVExtensions(); 1217 if (CurExt != PrevExt) { 1218 if (NeedEndif) 1219 OS << "#endif\n\n"; 1220 NeedEndif = emitExtDefStr(CurExt, OS); 1221 PrevExt = CurExt; 1222 } 1223 if (Def->hasAutoDef()) 1224 PrintBody(OS, *Def); 1225 } 1226 if (NeedEndif) 1227 OS << "#endif\n\n"; 1228 } 1229 1230 bool RVVEmitter::emitExtDefStr(uint8_t Extents, raw_ostream &OS) { 1231 if (Extents == RISCVExtension::Basic) 1232 return false; 1233 OS << "#if "; 1234 ListSeparator LS(" && "); 1235 if (Extents & RISCVExtension::F) 1236 OS << LS << "defined(__riscv_f)"; 1237 if (Extents & RISCVExtension::D) 1238 OS << LS << "defined(__riscv_d)"; 1239 if (Extents & RISCVExtension::Zfh) 1240 OS << LS << "defined(__riscv_zfh)"; 1241 if (Extents & RISCVExtension::Zvamo) 1242 OS << LS << "defined(__riscv_zvamo)"; 1243 if (Extents & RISCVExtension::Zvlsseg) 1244 OS << LS << "defined(__riscv_zvlsseg)"; 1245 OS << "\n"; 1246 return true; 1247 } 1248 1249 namespace clang { 1250 void EmitRVVHeader(RecordKeeper &Records, raw_ostream &OS) { 1251 RVVEmitter(Records).createHeader(OS); 1252 } 1253 1254 void EmitRVVBuiltins(RecordKeeper &Records, raw_ostream &OS) { 1255 RVVEmitter(Records).createBuiltins(OS); 1256 } 1257 1258 void EmitRVVBuiltinCG(RecordKeeper &Records, raw_ostream &OS) { 1259 RVVEmitter(Records).createCodeGen(OS); 1260 } 1261 1262 } // End namespace clang 1263