RISCVVEmitter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #include "clang/Support/RISCVVIntrinsicUtils.h"
  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/StringSwitch.h"
  23. #include "llvm/ADT/Twine.h"
  24. #include "llvm/TableGen/Error.h"
  25. #include "llvm/TableGen/Record.h"
  26. #include <numeric>
  27. #include <optional>
  28. using namespace llvm;
  29. using namespace clang::RISCV;
  30. namespace {
  31. struct SemaRecord {
  32. // Intrinsic name, e.g. vadd_vv
  33. std::string Name;
  34. // Overloaded intrinsic name, could be empty if can be computed from Name
  35. // e.g. vadd
  36. std::string OverloadedName;
  37. // Supported type, mask of BasicType.
  38. unsigned TypeRangeMask;
  39. // Supported LMUL.
  40. unsigned Log2LMULMask;
  41. // Required extensions for this intrinsic.
  42. unsigned RequiredExtensions;
  43. // Prototype for this intrinsic.
  44. SmallVector<PrototypeDescriptor> Prototype;
  45. // Suffix of intrinsic name.
  46. SmallVector<PrototypeDescriptor> Suffix;
  47. // Suffix of overloaded intrinsic name.
  48. SmallVector<PrototypeDescriptor> OverloadedSuffix;
  49. // BitMask for supported policies.
  50. uint16_t PolicyBitMask;
  51. // Number of field, large than 1 if it's segment load/store.
  52. unsigned NF;
  53. bool HasMasked :1;
  54. bool HasVL :1;
  55. bool HasMaskedOffOperand :1;
  56. bool HasTailPolicy : 1;
  57. bool HasMaskPolicy : 1;
  58. uint8_t UnMaskedPolicyScheme : 2;
  59. uint8_t MaskedPolicyScheme : 2;
  60. };
  61. // Compressed function signature table.
  62. class SemaSignatureTable {
  63. private:
  64. std::vector<PrototypeDescriptor> SignatureTable;
  65. void insert(ArrayRef<PrototypeDescriptor> Signature);
  66. public:
  67. static constexpr unsigned INVALID_INDEX = ~0U;
  68. // Create compressed signature table from SemaRecords.
  69. void init(ArrayRef<SemaRecord> SemaRecords);
  70. // Query the Signature, return INVALID_INDEX if not found.
  71. unsigned getIndex(ArrayRef<PrototypeDescriptor> Signature);
  72. /// Print signature table in RVVHeader Record to \p OS
  73. void print(raw_ostream &OS);
  74. };
  75. class RVVEmitter {
  76. private:
  77. RecordKeeper &Records;
  78. RVVTypeCache TypeCache;
  79. public:
  80. RVVEmitter(RecordKeeper &R) : Records(R) {}
  81. /// Emit riscv_vector.h
  82. void createHeader(raw_ostream &o);
  83. /// Emit all the __builtin prototypes and code needed by Sema.
  84. void createBuiltins(raw_ostream &o);
  85. /// Emit all the information needed to map builtin -> LLVM IR intrinsic.
  86. void createCodeGen(raw_ostream &o);
  87. /// Emit all the information needed by SemaRISCVVectorLookup.cpp.
  88. /// We've large number of intrinsic function for RVV, creating a customized
  89. /// could speed up the compilation time.
  90. void createSema(raw_ostream &o);
  91. private:
  92. /// Create all intrinsics and add them to \p Out and SemaRecords.
  93. void createRVVIntrinsics(std::vector<std::unique_ptr<RVVIntrinsic>> &Out,
  94. std::vector<SemaRecord> *SemaRecords = nullptr);
  95. /// Create all intrinsic records and SemaSignatureTable from SemaRecords.
  96. void createRVVIntrinsicRecords(std::vector<RVVIntrinsicRecord> &Out,
  97. SemaSignatureTable &SST,
  98. ArrayRef<SemaRecord> SemaRecords);
  99. /// Print HeaderCode in RVVHeader Record to \p Out
  100. void printHeaderCode(raw_ostream &OS);
  101. };
  102. } // namespace
  103. static BasicType ParseBasicType(char c) {
  104. switch (c) {
  105. case 'c':
  106. return BasicType::Int8;
  107. break;
  108. case 's':
  109. return BasicType::Int16;
  110. break;
  111. case 'i':
  112. return BasicType::Int32;
  113. break;
  114. case 'l':
  115. return BasicType::Int64;
  116. break;
  117. case 'x':
  118. return BasicType::Float16;
  119. break;
  120. case 'f':
  121. return BasicType::Float32;
  122. break;
  123. case 'd':
  124. return BasicType::Float64;
  125. break;
  126. default:
  127. return BasicType::Unknown;
  128. }
  129. }
  130. void emitCodeGenSwitchBody(const RVVIntrinsic *RVVI, raw_ostream &OS) {
  131. if (!RVVI->getIRName().empty())
  132. OS << " ID = Intrinsic::riscv_" + RVVI->getIRName() + ";\n";
  133. if (RVVI->getNF() >= 2)
  134. OS << " NF = " + utostr(RVVI->getNF()) + ";\n";
  135. OS << " PolicyAttrs = " << RVVI->getPolicyAttrsBits() << ";\n";
  136. if (RVVI->hasManualCodegen()) {
  137. OS << "IsMasked = " << (RVVI->isMasked() ? "true" : "false") << ";\n";
  138. OS << RVVI->getManualCodegen();
  139. OS << "break;\n";
  140. return;
  141. }
  142. // Cast pointer operand of vector load intrinsic.
  143. for (const auto &I : enumerate(RVVI->getInputTypes())) {
  144. if (I.value()->isPointer()) {
  145. assert(RVVI->getIntrinsicTypes().front() == -1 &&
  146. "RVVI should be vector load intrinsic.");
  147. OS << " Ops[" << I.index() << "] = Builder.CreateBitCast(Ops[";
  148. OS << I.index() << "], ResultType->getPointerTo());\n";
  149. }
  150. }
  151. if (RVVI->isMasked()) {
  152. if (RVVI->hasVL()) {
  153. OS << " std::rotate(Ops.begin(), Ops.begin() + 1, Ops.end() - 1);\n";
  154. if (RVVI->hasPolicyOperand())
  155. OS << " Ops.push_back(ConstantInt::get(Ops.back()->getType(),"
  156. " PolicyAttrs));\n";
  157. if (RVVI->hasMaskedOffOperand() && RVVI->getPolicyAttrs().isTAMAPolicy())
  158. OS << " Ops.insert(Ops.begin(), "
  159. "llvm::PoisonValue::get(ResultType));\n";
  160. // Masked reduction cases.
  161. if (!RVVI->hasMaskedOffOperand() && RVVI->hasPassthruOperand() &&
  162. RVVI->getPolicyAttrs().isTAMAPolicy())
  163. OS << " Ops.insert(Ops.begin(), "
  164. "llvm::PoisonValue::get(ResultType));\n";
  165. } else {
  166. OS << " std::rotate(Ops.begin(), Ops.begin() + 1, Ops.end());\n";
  167. }
  168. } else {
  169. if (RVVI->hasPolicyOperand())
  170. OS << " Ops.push_back(ConstantInt::get(Ops.back()->getType(), "
  171. "PolicyAttrs));\n";
  172. else if (RVVI->hasPassthruOperand() && RVVI->getPolicyAttrs().isTAPolicy())
  173. OS << " Ops.insert(Ops.begin(), llvm::PoisonValue::get(ResultType));\n";
  174. }
  175. OS << " IntrinsicTypes = {";
  176. ListSeparator LS;
  177. for (const auto &Idx : RVVI->getIntrinsicTypes()) {
  178. if (Idx == -1)
  179. OS << LS << "ResultType";
  180. else
  181. OS << LS << "Ops[" << Idx << "]->getType()";
  182. }
  183. // VL could be i64 or i32, need to encode it in IntrinsicTypes. VL is
  184. // always last operand.
  185. if (RVVI->hasVL())
  186. OS << ", Ops.back()->getType()";
  187. OS << "};\n";
  188. OS << " break;\n";
  189. }
  190. //===----------------------------------------------------------------------===//
  191. // SemaSignatureTable implementation
  192. //===----------------------------------------------------------------------===//
  193. void SemaSignatureTable::init(ArrayRef<SemaRecord> SemaRecords) {
  194. // Sort signature entries by length, let longer signature insert first, to
  195. // make it more possible to reuse table entries, that can reduce ~10% table
  196. // size.
  197. struct Compare {
  198. bool operator()(const SmallVector<PrototypeDescriptor> &A,
  199. const SmallVector<PrototypeDescriptor> &B) const {
  200. if (A.size() != B.size())
  201. return A.size() > B.size();
  202. size_t Len = A.size();
  203. for (size_t i = 0; i < Len; ++i) {
  204. if (A[i] != B[i])
  205. return A[i] < B[i];
  206. }
  207. return false;
  208. }
  209. };
  210. std::set<SmallVector<PrototypeDescriptor>, Compare> Signatures;
  211. auto InsertToSignatureSet =
  212. [&](const SmallVector<PrototypeDescriptor> &Signature) {
  213. if (Signature.empty())
  214. return;
  215. Signatures.insert(Signature);
  216. };
  217. assert(!SemaRecords.empty());
  218. llvm::for_each(SemaRecords, [&](const SemaRecord &SR) {
  219. InsertToSignatureSet(SR.Prototype);
  220. InsertToSignatureSet(SR.Suffix);
  221. InsertToSignatureSet(SR.OverloadedSuffix);
  222. });
  223. llvm::for_each(Signatures, [this](auto &Sig) { insert(Sig); });
  224. }
  225. void SemaSignatureTable::insert(ArrayRef<PrototypeDescriptor> Signature) {
  226. if (getIndex(Signature) != INVALID_INDEX)
  227. return;
  228. // Insert Signature into SignatureTable if not found in the table.
  229. SignatureTable.insert(SignatureTable.begin(), Signature.begin(),
  230. Signature.end());
  231. }
  232. unsigned SemaSignatureTable::getIndex(ArrayRef<PrototypeDescriptor> Signature) {
  233. // Empty signature could be point into any index since there is length
  234. // field when we use, so just always point it to 0.
  235. if (Signature.empty())
  236. return 0;
  237. // Checking Signature already in table or not.
  238. if (Signature.size() < SignatureTable.size()) {
  239. size_t Bound = SignatureTable.size() - Signature.size() + 1;
  240. for (size_t Index = 0; Index < Bound; ++Index) {
  241. using namespace std;
  242. if (equal(Signature.begin(), Signature.end(),
  243. SignatureTable.begin() + Index))
  244. return Index;
  245. }
  246. }
  247. return INVALID_INDEX;
  248. }
  249. void SemaSignatureTable::print(raw_ostream &OS) {
  250. for (const auto &Sig : SignatureTable)
  251. OS << "PrototypeDescriptor(" << static_cast<int>(Sig.PT) << ", "
  252. << static_cast<int>(Sig.VTM) << ", " << static_cast<int>(Sig.TM)
  253. << "),\n";
  254. }
  255. //===----------------------------------------------------------------------===//
  256. // RVVEmitter implementation
  257. //===----------------------------------------------------------------------===//
  258. void RVVEmitter::createHeader(raw_ostream &OS) {
  259. OS << "/*===---- riscv_vector.h - RISC-V V-extension RVVIntrinsics "
  260. "-------------------===\n"
  261. " *\n"
  262. " *\n"
  263. " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
  264. "Exceptions.\n"
  265. " * See https://llvm.org/LICENSE.txt for license information.\n"
  266. " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
  267. " *\n"
  268. " *===-----------------------------------------------------------------"
  269. "------===\n"
  270. " */\n\n";
  271. OS << "#ifndef __RISCV_VECTOR_H\n";
  272. OS << "#define __RISCV_VECTOR_H\n\n";
  273. OS << "#include <stdint.h>\n";
  274. OS << "#include <stddef.h>\n\n";
  275. OS << "#ifndef __riscv_vector\n";
  276. OS << "#error \"Vector intrinsics require the vector extension.\"\n";
  277. OS << "#endif\n\n";
  278. OS << "#ifdef __cplusplus\n";
  279. OS << "extern \"C\" {\n";
  280. OS << "#endif\n\n";
  281. OS << "#pragma clang riscv intrinsic vector\n\n";
  282. printHeaderCode(OS);
  283. auto printType = [&](auto T) {
  284. OS << "typedef " << T->getClangBuiltinStr() << " " << T->getTypeStr()
  285. << ";\n";
  286. };
  287. constexpr int Log2LMULs[] = {-3, -2, -1, 0, 1, 2, 3};
  288. // Print RVV boolean types.
  289. for (int Log2LMUL : Log2LMULs) {
  290. auto T = TypeCache.computeType(BasicType::Int8, Log2LMUL,
  291. PrototypeDescriptor::Mask);
  292. if (T)
  293. printType(*T);
  294. }
  295. // Print RVV int/float types.
  296. for (char I : StringRef("csil")) {
  297. BasicType BT = ParseBasicType(I);
  298. for (int Log2LMUL : Log2LMULs) {
  299. auto T = TypeCache.computeType(BT, Log2LMUL, PrototypeDescriptor::Vector);
  300. if (T) {
  301. printType(*T);
  302. auto UT = TypeCache.computeType(
  303. BT, Log2LMUL,
  304. PrototypeDescriptor(BaseTypeModifier::Vector,
  305. VectorTypeModifier::NoModifier,
  306. TypeModifier::UnsignedInteger));
  307. printType(*UT);
  308. }
  309. }
  310. }
  311. OS << "#if defined(__riscv_zvfh)\n";
  312. for (int Log2LMUL : Log2LMULs) {
  313. auto T = TypeCache.computeType(BasicType::Float16, Log2LMUL,
  314. PrototypeDescriptor::Vector);
  315. if (T)
  316. printType(*T);
  317. }
  318. OS << "#endif\n";
  319. OS << "#if (__riscv_v_elen_fp >= 32)\n";
  320. for (int Log2LMUL : Log2LMULs) {
  321. auto T = TypeCache.computeType(BasicType::Float32, Log2LMUL,
  322. PrototypeDescriptor::Vector);
  323. if (T)
  324. printType(*T);
  325. }
  326. OS << "#endif\n";
  327. OS << "#if (__riscv_v_elen_fp >= 64)\n";
  328. for (int Log2LMUL : Log2LMULs) {
  329. auto T = TypeCache.computeType(BasicType::Float64, Log2LMUL,
  330. PrototypeDescriptor::Vector);
  331. if (T)
  332. printType(*T);
  333. }
  334. OS << "#endif\n\n";
  335. OS << "#define __riscv_v_intrinsic_overloading 1\n";
  336. OS << "\n#ifdef __cplusplus\n";
  337. OS << "}\n";
  338. OS << "#endif // __cplusplus\n";
  339. OS << "#endif // __RISCV_VECTOR_H\n";
  340. }
  341. void RVVEmitter::createBuiltins(raw_ostream &OS) {
  342. std::vector<std::unique_ptr<RVVIntrinsic>> Defs;
  343. createRVVIntrinsics(Defs);
  344. // Map to keep track of which builtin names have already been emitted.
  345. StringMap<RVVIntrinsic *> BuiltinMap;
  346. OS << "#if defined(TARGET_BUILTIN) && !defined(RISCVV_BUILTIN)\n";
  347. OS << "#define RISCVV_BUILTIN(ID, TYPE, ATTRS) TARGET_BUILTIN(ID, TYPE, "
  348. "ATTRS, \"zve32x\")\n";
  349. OS << "#endif\n";
  350. for (auto &Def : Defs) {
  351. auto P =
  352. BuiltinMap.insert(std::make_pair(Def->getBuiltinName(), Def.get()));
  353. if (!P.second) {
  354. // Verf that this would have produced the same builtin definition.
  355. if (P.first->second->hasBuiltinAlias() != Def->hasBuiltinAlias())
  356. PrintFatalError("Builtin with same name has different hasAutoDef");
  357. else if (!Def->hasBuiltinAlias() &&
  358. P.first->second->getBuiltinTypeStr() != Def->getBuiltinTypeStr())
  359. PrintFatalError("Builtin with same name has different type string");
  360. continue;
  361. }
  362. OS << "RISCVV_BUILTIN(__builtin_rvv_" << Def->getBuiltinName() << ",\"";
  363. if (!Def->hasBuiltinAlias())
  364. OS << Def->getBuiltinTypeStr();
  365. OS << "\", \"n\")\n";
  366. }
  367. OS << "#undef RISCVV_BUILTIN\n";
  368. }
  369. void RVVEmitter::createCodeGen(raw_ostream &OS) {
  370. std::vector<std::unique_ptr<RVVIntrinsic>> Defs;
  371. createRVVIntrinsics(Defs);
  372. // IR name could be empty, use the stable sort preserves the relative order.
  373. llvm::stable_sort(Defs, [](const std::unique_ptr<RVVIntrinsic> &A,
  374. const std::unique_ptr<RVVIntrinsic> &B) {
  375. if (A->getIRName() == B->getIRName())
  376. return (A->getPolicyAttrs() < B->getPolicyAttrs());
  377. return (A->getIRName() < B->getIRName());
  378. });
  379. // Map to keep track of which builtin names have already been emitted.
  380. StringMap<RVVIntrinsic *> BuiltinMap;
  381. // Print switch body when the ir name, ManualCodegen or policy changes from
  382. // previous iteration.
  383. RVVIntrinsic *PrevDef = Defs.begin()->get();
  384. for (auto &Def : Defs) {
  385. StringRef CurIRName = Def->getIRName();
  386. if (CurIRName != PrevDef->getIRName() ||
  387. (Def->getManualCodegen() != PrevDef->getManualCodegen()) ||
  388. (Def->getPolicyAttrs() != PrevDef->getPolicyAttrs())) {
  389. emitCodeGenSwitchBody(PrevDef, OS);
  390. }
  391. PrevDef = Def.get();
  392. auto P =
  393. BuiltinMap.insert(std::make_pair(Def->getBuiltinName(), Def.get()));
  394. if (P.second) {
  395. OS << "case RISCVVector::BI__builtin_rvv_" << Def->getBuiltinName()
  396. << ":\n";
  397. continue;
  398. }
  399. if (P.first->second->getIRName() != Def->getIRName())
  400. PrintFatalError("Builtin with same name has different IRName");
  401. else if (P.first->second->getManualCodegen() != Def->getManualCodegen())
  402. PrintFatalError("Builtin with same name has different ManualCodegen");
  403. else if (P.first->second->getNF() != Def->getNF())
  404. PrintFatalError("Builtin with same name has different NF");
  405. else if (P.first->second->isMasked() != Def->isMasked())
  406. PrintFatalError("Builtin with same name has different isMasked");
  407. else if (P.first->second->hasVL() != Def->hasVL())
  408. PrintFatalError("Builtin with same name has different hasVL");
  409. else if (P.first->second->getPolicyScheme() != Def->getPolicyScheme())
  410. PrintFatalError("Builtin with same name has different getPolicyScheme");
  411. else if (P.first->second->getIntrinsicTypes() != Def->getIntrinsicTypes())
  412. PrintFatalError("Builtin with same name has different IntrinsicTypes");
  413. }
  414. emitCodeGenSwitchBody(Defs.back().get(), OS);
  415. OS << "\n";
  416. }
  417. void RVVEmitter::createRVVIntrinsics(
  418. std::vector<std::unique_ptr<RVVIntrinsic>> &Out,
  419. std::vector<SemaRecord> *SemaRecords) {
  420. std::vector<Record *> RV = Records.getAllDerivedDefinitions("RVVBuiltin");
  421. for (auto *R : RV) {
  422. StringRef Name = R->getValueAsString("Name");
  423. StringRef SuffixProto = R->getValueAsString("Suffix");
  424. StringRef OverloadedName = R->getValueAsString("OverloadedName");
  425. StringRef OverloadedSuffixProto = R->getValueAsString("OverloadedSuffix");
  426. StringRef Prototypes = R->getValueAsString("Prototype");
  427. StringRef TypeRange = R->getValueAsString("TypeRange");
  428. bool HasMasked = R->getValueAsBit("HasMasked");
  429. bool HasMaskedOffOperand = R->getValueAsBit("HasMaskedOffOperand");
  430. bool HasVL = R->getValueAsBit("HasVL");
  431. Record *MPSRecord = R->getValueAsDef("MaskedPolicyScheme");
  432. auto MaskedPolicyScheme =
  433. static_cast<PolicyScheme>(MPSRecord->getValueAsInt("Value"));
  434. Record *UMPSRecord = R->getValueAsDef("UnMaskedPolicyScheme");
  435. auto UnMaskedPolicyScheme =
  436. static_cast<PolicyScheme>(UMPSRecord->getValueAsInt("Value"));
  437. std::vector<int64_t> Log2LMULList = R->getValueAsListOfInts("Log2LMUL");
  438. bool HasTailPolicy = R->getValueAsBit("HasTailPolicy");
  439. bool HasMaskPolicy = R->getValueAsBit("HasMaskPolicy");
  440. bool SupportOverloading = R->getValueAsBit("SupportOverloading");
  441. bool HasBuiltinAlias = R->getValueAsBit("HasBuiltinAlias");
  442. StringRef ManualCodegen = R->getValueAsString("ManualCodegen");
  443. std::vector<int64_t> IntrinsicTypes =
  444. R->getValueAsListOfInts("IntrinsicTypes");
  445. std::vector<StringRef> RequiredFeatures =
  446. R->getValueAsListOfStrings("RequiredFeatures");
  447. StringRef IRName = R->getValueAsString("IRName");
  448. StringRef MaskedIRName = R->getValueAsString("MaskedIRName");
  449. unsigned NF = R->getValueAsInt("NF");
  450. const Policy DefaultPolicy;
  451. SmallVector<Policy> SupportedUnMaskedPolicies =
  452. RVVIntrinsic::getSupportedUnMaskedPolicies();
  453. SmallVector<Policy> SupportedMaskedPolicies =
  454. RVVIntrinsic::getSupportedMaskedPolicies(HasTailPolicy, HasMaskPolicy);
  455. // Parse prototype and create a list of primitive type with transformers
  456. // (operand) in Prototype. Prototype[0] is output operand.
  457. SmallVector<PrototypeDescriptor> BasicPrototype =
  458. parsePrototypes(Prototypes);
  459. SmallVector<PrototypeDescriptor> SuffixDesc = parsePrototypes(SuffixProto);
  460. SmallVector<PrototypeDescriptor> OverloadedSuffixDesc =
  461. parsePrototypes(OverloadedSuffixProto);
  462. // Compute Builtin types
  463. auto Prototype = RVVIntrinsic::computeBuiltinTypes(
  464. BasicPrototype, /*IsMasked=*/false,
  465. /*HasMaskedOffOperand=*/false, HasVL, NF, UnMaskedPolicyScheme,
  466. DefaultPolicy);
  467. auto MaskedPrototype = RVVIntrinsic::computeBuiltinTypes(
  468. BasicPrototype, /*IsMasked=*/true, HasMaskedOffOperand, HasVL, NF,
  469. MaskedPolicyScheme, DefaultPolicy);
  470. // Create Intrinsics for each type and LMUL.
  471. for (char I : TypeRange) {
  472. for (int Log2LMUL : Log2LMULList) {
  473. BasicType BT = ParseBasicType(I);
  474. std::optional<RVVTypes> Types =
  475. TypeCache.computeTypes(BT, Log2LMUL, NF, Prototype);
  476. // Ignored to create new intrinsic if there are any illegal types.
  477. if (!Types)
  478. continue;
  479. auto SuffixStr =
  480. RVVIntrinsic::getSuffixStr(TypeCache, BT, Log2LMUL, SuffixDesc);
  481. auto OverloadedSuffixStr = RVVIntrinsic::getSuffixStr(
  482. TypeCache, BT, Log2LMUL, OverloadedSuffixDesc);
  483. // Create a unmasked intrinsic
  484. Out.push_back(std::make_unique<RVVIntrinsic>(
  485. Name, SuffixStr, OverloadedName, OverloadedSuffixStr, IRName,
  486. /*IsMasked=*/false, /*HasMaskedOffOperand=*/false, HasVL,
  487. UnMaskedPolicyScheme, SupportOverloading, HasBuiltinAlias,
  488. ManualCodegen, *Types, IntrinsicTypes, RequiredFeatures, NF,
  489. DefaultPolicy));
  490. if (UnMaskedPolicyScheme != PolicyScheme::SchemeNone)
  491. for (auto P : SupportedUnMaskedPolicies) {
  492. SmallVector<PrototypeDescriptor> PolicyPrototype =
  493. RVVIntrinsic::computeBuiltinTypes(
  494. BasicPrototype, /*IsMasked=*/false,
  495. /*HasMaskedOffOperand=*/false, HasVL, NF,
  496. UnMaskedPolicyScheme, P);
  497. std::optional<RVVTypes> PolicyTypes =
  498. TypeCache.computeTypes(BT, Log2LMUL, NF, PolicyPrototype);
  499. Out.push_back(std::make_unique<RVVIntrinsic>(
  500. Name, SuffixStr, OverloadedName, OverloadedSuffixStr, IRName,
  501. /*IsMask=*/false, /*HasMaskedOffOperand=*/false, HasVL,
  502. UnMaskedPolicyScheme, SupportOverloading, HasBuiltinAlias,
  503. ManualCodegen, *PolicyTypes, IntrinsicTypes, RequiredFeatures,
  504. NF, P));
  505. }
  506. if (!HasMasked)
  507. continue;
  508. // Create a masked intrinsic
  509. std::optional<RVVTypes> MaskTypes =
  510. TypeCache.computeTypes(BT, Log2LMUL, NF, MaskedPrototype);
  511. Out.push_back(std::make_unique<RVVIntrinsic>(
  512. Name, SuffixStr, OverloadedName, OverloadedSuffixStr, MaskedIRName,
  513. /*IsMasked=*/true, HasMaskedOffOperand, HasVL, MaskedPolicyScheme,
  514. SupportOverloading, HasBuiltinAlias, ManualCodegen, *MaskTypes,
  515. IntrinsicTypes, RequiredFeatures, NF, DefaultPolicy));
  516. if (MaskedPolicyScheme == PolicyScheme::SchemeNone)
  517. continue;
  518. for (auto P : SupportedMaskedPolicies) {
  519. SmallVector<PrototypeDescriptor> PolicyPrototype =
  520. RVVIntrinsic::computeBuiltinTypes(
  521. BasicPrototype, /*IsMasked=*/true, HasMaskedOffOperand, HasVL,
  522. NF, MaskedPolicyScheme, P);
  523. std::optional<RVVTypes> PolicyTypes =
  524. TypeCache.computeTypes(BT, Log2LMUL, NF, PolicyPrototype);
  525. Out.push_back(std::make_unique<RVVIntrinsic>(
  526. Name, SuffixStr, OverloadedName, OverloadedSuffixStr,
  527. MaskedIRName, /*IsMasked=*/true, HasMaskedOffOperand, HasVL,
  528. MaskedPolicyScheme, SupportOverloading, HasBuiltinAlias,
  529. ManualCodegen, *PolicyTypes, IntrinsicTypes, RequiredFeatures, NF,
  530. P));
  531. }
  532. } // End for Log2LMULList
  533. } // End for TypeRange
  534. // We don't emit vsetvli and vsetvlimax for SemaRecord.
  535. // They are written in riscv_vector.td and will emit those marco define in
  536. // riscv_vector.h
  537. if (Name == "vsetvli" || Name == "vsetvlimax")
  538. continue;
  539. if (!SemaRecords)
  540. continue;
  541. // Create SemaRecord
  542. SemaRecord SR;
  543. SR.Name = Name.str();
  544. SR.OverloadedName = OverloadedName.str();
  545. BasicType TypeRangeMask = BasicType::Unknown;
  546. for (char I : TypeRange)
  547. TypeRangeMask |= ParseBasicType(I);
  548. SR.TypeRangeMask = static_cast<unsigned>(TypeRangeMask);
  549. unsigned Log2LMULMask = 0;
  550. for (int Log2LMUL : Log2LMULList)
  551. Log2LMULMask |= 1 << (Log2LMUL + 3);
  552. SR.Log2LMULMask = Log2LMULMask;
  553. SR.RequiredExtensions = 0;
  554. for (auto RequiredFeature : RequiredFeatures) {
  555. RVVRequire RequireExt = StringSwitch<RVVRequire>(RequiredFeature)
  556. .Case("RV64", RVV_REQ_RV64)
  557. .Case("FullMultiply", RVV_REQ_FullMultiply)
  558. .Default(RVV_REQ_None);
  559. assert(RequireExt != RVV_REQ_None && "Unrecognized required feature?");
  560. SR.RequiredExtensions |= RequireExt;
  561. }
  562. SR.NF = NF;
  563. SR.HasMasked = HasMasked;
  564. SR.HasVL = HasVL;
  565. SR.HasMaskedOffOperand = HasMaskedOffOperand;
  566. SR.HasTailPolicy = HasTailPolicy;
  567. SR.HasMaskPolicy = HasMaskPolicy;
  568. SR.UnMaskedPolicyScheme = static_cast<uint8_t>(UnMaskedPolicyScheme);
  569. SR.MaskedPolicyScheme = static_cast<uint8_t>(MaskedPolicyScheme);
  570. SR.Prototype = std::move(BasicPrototype);
  571. SR.Suffix = parsePrototypes(SuffixProto);
  572. SR.OverloadedSuffix = parsePrototypes(OverloadedSuffixProto);
  573. SemaRecords->push_back(SR);
  574. }
  575. }
  576. void RVVEmitter::printHeaderCode(raw_ostream &OS) {
  577. std::vector<Record *> RVVHeaders =
  578. Records.getAllDerivedDefinitions("RVVHeader");
  579. for (auto *R : RVVHeaders) {
  580. StringRef HeaderCodeStr = R->getValueAsString("HeaderCode");
  581. OS << HeaderCodeStr.str();
  582. }
  583. }
  584. void RVVEmitter::createRVVIntrinsicRecords(std::vector<RVVIntrinsicRecord> &Out,
  585. SemaSignatureTable &SST,
  586. ArrayRef<SemaRecord> SemaRecords) {
  587. SST.init(SemaRecords);
  588. for (const auto &SR : SemaRecords) {
  589. Out.emplace_back(RVVIntrinsicRecord());
  590. RVVIntrinsicRecord &R = Out.back();
  591. R.Name = SR.Name.c_str();
  592. R.OverloadedName = SR.OverloadedName.c_str();
  593. R.PrototypeIndex = SST.getIndex(SR.Prototype);
  594. R.SuffixIndex = SST.getIndex(SR.Suffix);
  595. R.OverloadedSuffixIndex = SST.getIndex(SR.OverloadedSuffix);
  596. R.PrototypeLength = SR.Prototype.size();
  597. R.SuffixLength = SR.Suffix.size();
  598. R.OverloadedSuffixSize = SR.OverloadedSuffix.size();
  599. R.RequiredExtensions = SR.RequiredExtensions;
  600. R.TypeRangeMask = SR.TypeRangeMask;
  601. R.Log2LMULMask = SR.Log2LMULMask;
  602. R.NF = SR.NF;
  603. R.HasMasked = SR.HasMasked;
  604. R.HasVL = SR.HasVL;
  605. R.HasMaskedOffOperand = SR.HasMaskedOffOperand;
  606. R.HasTailPolicy = SR.HasTailPolicy;
  607. R.HasMaskPolicy = SR.HasMaskPolicy;
  608. R.UnMaskedPolicyScheme = SR.UnMaskedPolicyScheme;
  609. R.MaskedPolicyScheme = SR.MaskedPolicyScheme;
  610. assert(R.PrototypeIndex !=
  611. static_cast<uint16_t>(SemaSignatureTable::INVALID_INDEX));
  612. assert(R.SuffixIndex !=
  613. static_cast<uint16_t>(SemaSignatureTable::INVALID_INDEX));
  614. assert(R.OverloadedSuffixIndex !=
  615. static_cast<uint16_t>(SemaSignatureTable::INVALID_INDEX));
  616. }
  617. }
  618. void RVVEmitter::createSema(raw_ostream &OS) {
  619. std::vector<std::unique_ptr<RVVIntrinsic>> Defs;
  620. std::vector<RVVIntrinsicRecord> RVVIntrinsicRecords;
  621. SemaSignatureTable SST;
  622. std::vector<SemaRecord> SemaRecords;
  623. createRVVIntrinsics(Defs, &SemaRecords);
  624. createRVVIntrinsicRecords(RVVIntrinsicRecords, SST, SemaRecords);
  625. // Emit signature table for SemaRISCVVectorLookup.cpp.
  626. OS << "#ifdef DECL_SIGNATURE_TABLE\n";
  627. SST.print(OS);
  628. OS << "#endif\n";
  629. // Emit RVVIntrinsicRecords for SemaRISCVVectorLookup.cpp.
  630. OS << "#ifdef DECL_INTRINSIC_RECORDS\n";
  631. for (const RVVIntrinsicRecord &Record : RVVIntrinsicRecords)
  632. OS << Record;
  633. OS << "#endif\n";
  634. }
  635. namespace clang {
  636. void EmitRVVHeader(RecordKeeper &Records, raw_ostream &OS) {
  637. RVVEmitter(Records).createHeader(OS);
  638. }
  639. void EmitRVVBuiltins(RecordKeeper &Records, raw_ostream &OS) {
  640. RVVEmitter(Records).createBuiltins(OS);
  641. }
  642. void EmitRVVBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
  643. RVVEmitter(Records).createCodeGen(OS);
  644. }
  645. void EmitRVVBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
  646. RVVEmitter(Records).createSema(OS);
  647. }
  648. } // End namespace clang