MveEmitter.cpp 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. //===- MveEmitter.cpp - Generate arm_mve.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 set of linked tablegen backends is responsible for emitting the bits
  10. // and pieces that implement <arm_mve.h>, which is defined by the ACLE standard
  11. // and provides a set of types and functions for (more or less) direct access
  12. // to the MVE instruction set, including the scalar shifts as well as the
  13. // vector instructions.
  14. //
  15. // MVE's standard intrinsic functions are unusual in that they have a system of
  16. // polymorphism. For example, the function vaddq() can behave like vaddq_u16(),
  17. // vaddq_f32(), vaddq_s8(), etc., depending on the types of the vector
  18. // arguments you give it.
  19. //
  20. // This constrains the implementation strategies. The usual approach to making
  21. // the user-facing functions polymorphic would be to either use
  22. // __attribute__((overloadable)) to make a set of vaddq() functions that are
  23. // all inline wrappers on the underlying clang builtins, or to define a single
  24. // vaddq() macro which expands to an instance of _Generic.
  25. //
  26. // The inline-wrappers approach would work fine for most intrinsics, except for
  27. // the ones that take an argument required to be a compile-time constant,
  28. // because if you wrap an inline function around a call to a builtin, the
  29. // constant nature of the argument is not passed through.
  30. //
  31. // The _Generic approach can be made to work with enough effort, but it takes a
  32. // lot of machinery, because of the design feature of _Generic that even the
  33. // untaken branches are required to pass all front-end validity checks such as
  34. // type-correctness. You can work around that by nesting further _Generics all
  35. // over the place to coerce things to the right type in untaken branches, but
  36. // what you get out is complicated, hard to guarantee its correctness, and
  37. // worst of all, gives _completely unreadable_ error messages if the user gets
  38. // the types wrong for an intrinsic call.
  39. //
  40. // Therefore, my strategy is to introduce a new __attribute__ that allows a
  41. // function to be mapped to a clang builtin even though it doesn't have the
  42. // same name, and then declare all the user-facing MVE function names with that
  43. // attribute, mapping each one directly to the clang builtin. And the
  44. // polymorphic ones have __attribute__((overloadable)) as well. So once the
  45. // compiler has resolved the overload, it knows the internal builtin ID of the
  46. // selected function, and can check the immediate arguments against that; and
  47. // if the user gets the types wrong in a call to a polymorphic intrinsic, they
  48. // get a completely clear error message showing all the declarations of that
  49. // function in the header file and explaining why each one doesn't fit their
  50. // call.
  51. //
  52. // The downside of this is that if every clang builtin has to correspond
  53. // exactly to a user-facing ACLE intrinsic, then you can't save work in the
  54. // frontend by doing it in the header file: CGBuiltin.cpp has to do the entire
  55. // job of converting an ACLE intrinsic call into LLVM IR. So the Tablegen
  56. // description for an MVE intrinsic has to contain a full description of the
  57. // sequence of IRBuilder calls that clang will need to make.
  58. //
  59. //===----------------------------------------------------------------------===//
  60. #include "llvm/ADT/APInt.h"
  61. #include "llvm/ADT/StringRef.h"
  62. #include "llvm/ADT/StringSwitch.h"
  63. #include "llvm/Support/Casting.h"
  64. #include "llvm/Support/raw_ostream.h"
  65. #include "llvm/TableGen/Error.h"
  66. #include "llvm/TableGen/Record.h"
  67. #include "llvm/TableGen/StringToOffsetTable.h"
  68. #include <cassert>
  69. #include <cstddef>
  70. #include <cstdint>
  71. #include <list>
  72. #include <map>
  73. #include <memory>
  74. #include <set>
  75. #include <string>
  76. #include <vector>
  77. using namespace llvm;
  78. namespace {
  79. class EmitterBase;
  80. class Result;
  81. // -----------------------------------------------------------------------------
  82. // A system of classes to represent all the types we'll need to deal with in
  83. // the prototypes of intrinsics.
  84. //
  85. // Query methods include finding out the C name of a type; the "LLVM name" in
  86. // the sense of a C++ code snippet that can be used in the codegen function;
  87. // the suffix that represents the type in the ACLE intrinsic naming scheme
  88. // (e.g. 's32' represents int32_t in intrinsics such as vaddq_s32); whether the
  89. // type is floating-point related (hence should be under #ifdef in the MVE
  90. // header so that it isn't included in integer-only MVE mode); and the type's
  91. // size in bits. Not all subtypes support all these queries.
  92. class Type {
  93. public:
  94. enum class TypeKind {
  95. // Void appears as a return type (for store intrinsics, which are pure
  96. // side-effect). It's also used as the parameter type in the Tablegen
  97. // when an intrinsic doesn't need to come in various suffixed forms like
  98. // vfooq_s8,vfooq_u16,vfooq_f32.
  99. Void,
  100. // Scalar is used for ordinary int and float types of all sizes.
  101. Scalar,
  102. // Vector is used for anything that occupies exactly one MVE vector
  103. // register, i.e. {uint,int,float}NxM_t.
  104. Vector,
  105. // MultiVector is used for the {uint,int,float}NxMxK_t types used by the
  106. // interleaving load/store intrinsics v{ld,st}{2,4}q.
  107. MultiVector,
  108. // Predicate is used by all the predicated intrinsics. Its C
  109. // representation is mve_pred16_t (which is just an alias for uint16_t).
  110. // But we give more detail here, by indicating that a given predicate
  111. // instruction is logically regarded as a vector of i1 containing the
  112. // same number of lanes as the input vector type. So our Predicate type
  113. // comes with a lane count, which we use to decide which kind of <n x i1>
  114. // we'll invoke the pred_i2v IR intrinsic to translate it into.
  115. Predicate,
  116. // Pointer is used for pointer types (obviously), and comes with a flag
  117. // indicating whether it's a pointer to a const or mutable instance of
  118. // the pointee type.
  119. Pointer,
  120. };
  121. private:
  122. const TypeKind TKind;
  123. protected:
  124. Type(TypeKind K) : TKind(K) {}
  125. public:
  126. TypeKind typeKind() const { return TKind; }
  127. virtual ~Type() = default;
  128. virtual bool requiresFloat() const = 0;
  129. virtual bool requiresMVE() const = 0;
  130. virtual unsigned sizeInBits() const = 0;
  131. virtual std::string cName() const = 0;
  132. virtual std::string llvmName() const {
  133. PrintFatalError("no LLVM type name available for type " + cName());
  134. }
  135. virtual std::string acleSuffix(std::string) const {
  136. PrintFatalError("no ACLE suffix available for this type");
  137. }
  138. };
  139. enum class ScalarTypeKind { SignedInt, UnsignedInt, Float };
  140. inline std::string toLetter(ScalarTypeKind kind) {
  141. switch (kind) {
  142. case ScalarTypeKind::SignedInt:
  143. return "s";
  144. case ScalarTypeKind::UnsignedInt:
  145. return "u";
  146. case ScalarTypeKind::Float:
  147. return "f";
  148. }
  149. llvm_unreachable("Unhandled ScalarTypeKind enum");
  150. }
  151. inline std::string toCPrefix(ScalarTypeKind kind) {
  152. switch (kind) {
  153. case ScalarTypeKind::SignedInt:
  154. return "int";
  155. case ScalarTypeKind::UnsignedInt:
  156. return "uint";
  157. case ScalarTypeKind::Float:
  158. return "float";
  159. }
  160. llvm_unreachable("Unhandled ScalarTypeKind enum");
  161. }
  162. class VoidType : public Type {
  163. public:
  164. VoidType() : Type(TypeKind::Void) {}
  165. unsigned sizeInBits() const override { return 0; }
  166. bool requiresFloat() const override { return false; }
  167. bool requiresMVE() const override { return false; }
  168. std::string cName() const override { return "void"; }
  169. static bool classof(const Type *T) { return T->typeKind() == TypeKind::Void; }
  170. std::string acleSuffix(std::string) const override { return ""; }
  171. };
  172. class PointerType : public Type {
  173. const Type *Pointee;
  174. bool Const;
  175. public:
  176. PointerType(const Type *Pointee, bool Const)
  177. : Type(TypeKind::Pointer), Pointee(Pointee), Const(Const) {}
  178. unsigned sizeInBits() const override { return 32; }
  179. bool requiresFloat() const override { return Pointee->requiresFloat(); }
  180. bool requiresMVE() const override { return Pointee->requiresMVE(); }
  181. std::string cName() const override {
  182. std::string Name = Pointee->cName();
  183. // The syntax for a pointer in C is different when the pointee is
  184. // itself a pointer. The MVE intrinsics don't contain any double
  185. // pointers, so we don't need to worry about that wrinkle.
  186. assert(!isa<PointerType>(Pointee) && "Pointer to pointer not supported");
  187. if (Const)
  188. Name = "const " + Name;
  189. return Name + " *";
  190. }
  191. std::string llvmName() const override {
  192. return "llvm::PointerType::getUnqual(" + Pointee->llvmName() + ")";
  193. }
  194. const Type *getPointeeType() const { return Pointee; }
  195. static bool classof(const Type *T) {
  196. return T->typeKind() == TypeKind::Pointer;
  197. }
  198. };
  199. // Base class for all the types that have a name of the form
  200. // [prefix][numbers]_t, like int32_t, uint16x8_t, float32x4x2_t.
  201. //
  202. // For this sub-hierarchy we invent a cNameBase() method which returns the
  203. // whole name except for the trailing "_t", so that Vector and MultiVector can
  204. // append an extra "x2" or whatever to their element type's cNameBase(). Then
  205. // the main cName() query method puts "_t" on the end for the final type name.
  206. class CRegularNamedType : public Type {
  207. using Type::Type;
  208. virtual std::string cNameBase() const = 0;
  209. public:
  210. std::string cName() const override { return cNameBase() + "_t"; }
  211. };
  212. class ScalarType : public CRegularNamedType {
  213. ScalarTypeKind Kind;
  214. unsigned Bits;
  215. std::string NameOverride;
  216. public:
  217. ScalarType(const Record *Record) : CRegularNamedType(TypeKind::Scalar) {
  218. Kind = StringSwitch<ScalarTypeKind>(Record->getValueAsString("kind"))
  219. .Case("s", ScalarTypeKind::SignedInt)
  220. .Case("u", ScalarTypeKind::UnsignedInt)
  221. .Case("f", ScalarTypeKind::Float);
  222. Bits = Record->getValueAsInt("size");
  223. NameOverride = std::string(Record->getValueAsString("nameOverride"));
  224. }
  225. unsigned sizeInBits() const override { return Bits; }
  226. ScalarTypeKind kind() const { return Kind; }
  227. std::string suffix() const { return toLetter(Kind) + utostr(Bits); }
  228. std::string cNameBase() const override {
  229. return toCPrefix(Kind) + utostr(Bits);
  230. }
  231. std::string cName() const override {
  232. if (NameOverride.empty())
  233. return CRegularNamedType::cName();
  234. return NameOverride;
  235. }
  236. std::string llvmName() const override {
  237. if (Kind == ScalarTypeKind::Float) {
  238. if (Bits == 16)
  239. return "HalfTy";
  240. if (Bits == 32)
  241. return "FloatTy";
  242. if (Bits == 64)
  243. return "DoubleTy";
  244. PrintFatalError("bad size for floating type");
  245. }
  246. return "Int" + utostr(Bits) + "Ty";
  247. }
  248. std::string acleSuffix(std::string overrideLetter) const override {
  249. return "_" + (overrideLetter.size() ? overrideLetter : toLetter(Kind))
  250. + utostr(Bits);
  251. }
  252. bool isInteger() const { return Kind != ScalarTypeKind::Float; }
  253. bool requiresFloat() const override { return !isInteger(); }
  254. bool requiresMVE() const override { return false; }
  255. bool hasNonstandardName() const { return !NameOverride.empty(); }
  256. static bool classof(const Type *T) {
  257. return T->typeKind() == TypeKind::Scalar;
  258. }
  259. };
  260. class VectorType : public CRegularNamedType {
  261. const ScalarType *Element;
  262. unsigned Lanes;
  263. public:
  264. VectorType(const ScalarType *Element, unsigned Lanes)
  265. : CRegularNamedType(TypeKind::Vector), Element(Element), Lanes(Lanes) {}
  266. unsigned sizeInBits() const override { return Lanes * Element->sizeInBits(); }
  267. unsigned lanes() const { return Lanes; }
  268. bool requiresFloat() const override { return Element->requiresFloat(); }
  269. bool requiresMVE() const override { return true; }
  270. std::string cNameBase() const override {
  271. return Element->cNameBase() + "x" + utostr(Lanes);
  272. }
  273. std::string llvmName() const override {
  274. return "llvm::FixedVectorType::get(" + Element->llvmName() + ", " +
  275. utostr(Lanes) + ")";
  276. }
  277. static bool classof(const Type *T) {
  278. return T->typeKind() == TypeKind::Vector;
  279. }
  280. };
  281. class MultiVectorType : public CRegularNamedType {
  282. const VectorType *Element;
  283. unsigned Registers;
  284. public:
  285. MultiVectorType(unsigned Registers, const VectorType *Element)
  286. : CRegularNamedType(TypeKind::MultiVector), Element(Element),
  287. Registers(Registers) {}
  288. unsigned sizeInBits() const override {
  289. return Registers * Element->sizeInBits();
  290. }
  291. unsigned registers() const { return Registers; }
  292. bool requiresFloat() const override { return Element->requiresFloat(); }
  293. bool requiresMVE() const override { return true; }
  294. std::string cNameBase() const override {
  295. return Element->cNameBase() + "x" + utostr(Registers);
  296. }
  297. // MultiVectorType doesn't override llvmName, because we don't expect to do
  298. // automatic code generation for the MVE intrinsics that use it: the {vld2,
  299. // vld4, vst2, vst4} family are the only ones that use these types, so it was
  300. // easier to hand-write the codegen for dealing with these structs than to
  301. // build in lots of extra automatic machinery that would only be used once.
  302. static bool classof(const Type *T) {
  303. return T->typeKind() == TypeKind::MultiVector;
  304. }
  305. };
  306. class PredicateType : public CRegularNamedType {
  307. unsigned Lanes;
  308. public:
  309. PredicateType(unsigned Lanes)
  310. : CRegularNamedType(TypeKind::Predicate), Lanes(Lanes) {}
  311. unsigned sizeInBits() const override { return 16; }
  312. std::string cNameBase() const override { return "mve_pred16"; }
  313. bool requiresFloat() const override { return false; };
  314. bool requiresMVE() const override { return true; }
  315. std::string llvmName() const override {
  316. return "llvm::FixedVectorType::get(Builder.getInt1Ty(), " + utostr(Lanes) +
  317. ")";
  318. }
  319. static bool classof(const Type *T) {
  320. return T->typeKind() == TypeKind::Predicate;
  321. }
  322. };
  323. // -----------------------------------------------------------------------------
  324. // Class to facilitate merging together the code generation for many intrinsics
  325. // by means of varying a few constant or type parameters.
  326. //
  327. // Most obviously, the intrinsics in a single parametrised family will have
  328. // code generation sequences that only differ in a type or two, e.g. vaddq_s8
  329. // and vaddq_u16 will look the same apart from putting a different vector type
  330. // in the call to CGM.getIntrinsic(). But also, completely different intrinsics
  331. // will often code-generate in the same way, with only a different choice of
  332. // _which_ IR intrinsic they lower to (e.g. vaddq_m_s8 and vmulq_m_s8), but
  333. // marshalling the arguments and return values of the IR intrinsic in exactly
  334. // the same way. And others might differ only in some other kind of constant,
  335. // such as a lane index.
  336. //
  337. // So, when we generate the IR-building code for all these intrinsics, we keep
  338. // track of every value that could possibly be pulled out of the code and
  339. // stored ahead of time in a local variable. Then we group together intrinsics
  340. // by textual equivalence of the code that would result if _all_ those
  341. // parameters were stored in local variables. That gives us maximal sets that
  342. // can be implemented by a single piece of IR-building code by changing
  343. // parameter values ahead of time.
  344. //
  345. // After we've done that, we do a second pass in which we only allocate _some_
  346. // of the parameters into local variables, by tracking which ones have the same
  347. // values as each other (so that a single variable can be reused) and which
  348. // ones are the same across the whole set (so that no variable is needed at
  349. // all).
  350. //
  351. // Hence the class below. Its allocParam method is invoked during code
  352. // generation by every method of a Result subclass (see below) that wants to
  353. // give it the opportunity to pull something out into a switchable parameter.
  354. // It returns a variable name for the parameter, or (if it's being used in the
  355. // second pass once we've decided that some parameters don't need to be stored
  356. // in variables after all) it might just return the input expression unchanged.
  357. struct CodeGenParamAllocator {
  358. // Accumulated during code generation
  359. std::vector<std::string> *ParamTypes = nullptr;
  360. std::vector<std::string> *ParamValues = nullptr;
  361. // Provided ahead of time in pass 2, to indicate which parameters are being
  362. // assigned to what. This vector contains an entry for each call to
  363. // allocParam expected during code gen (which we counted up in pass 1), and
  364. // indicates the number of the parameter variable that should be returned, or
  365. // -1 if this call shouldn't allocate a parameter variable at all.
  366. //
  367. // We rely on the recursive code generation working identically in passes 1
  368. // and 2, so that the same list of calls to allocParam happen in the same
  369. // order. That guarantees that the parameter numbers recorded in pass 1 will
  370. // match the entries in this vector that store what EmitterBase::EmitBuiltinCG
  371. // decided to do about each one in pass 2.
  372. std::vector<int> *ParamNumberMap = nullptr;
  373. // Internally track how many things we've allocated
  374. unsigned nparams = 0;
  375. std::string allocParam(StringRef Type, StringRef Value) {
  376. unsigned ParamNumber;
  377. if (!ParamNumberMap) {
  378. // In pass 1, unconditionally assign a new parameter variable to every
  379. // value we're asked to process.
  380. ParamNumber = nparams++;
  381. } else {
  382. // In pass 2, consult the map provided by the caller to find out which
  383. // variable we should be keeping things in.
  384. int MapValue = (*ParamNumberMap)[nparams++];
  385. if (MapValue < 0)
  386. return std::string(Value);
  387. ParamNumber = MapValue;
  388. }
  389. // If we've allocated a new parameter variable for the first time, store
  390. // its type and value to be retrieved after codegen.
  391. if (ParamTypes && ParamTypes->size() == ParamNumber)
  392. ParamTypes->push_back(std::string(Type));
  393. if (ParamValues && ParamValues->size() == ParamNumber)
  394. ParamValues->push_back(std::string(Value));
  395. // Unimaginative naming scheme for parameter variables.
  396. return "Param" + utostr(ParamNumber);
  397. }
  398. };
  399. // -----------------------------------------------------------------------------
  400. // System of classes that represent all the intermediate values used during
  401. // code-generation for an intrinsic.
  402. //
  403. // The base class 'Result' can represent a value of the LLVM type 'Value', or
  404. // sometimes 'Address' (for loads/stores, including an alignment requirement).
  405. //
  406. // In the case where the Tablegen provides a value in the codegen dag as a
  407. // plain integer literal, the Result object we construct here will be one that
  408. // returns true from hasIntegerConstantValue(). This allows the generated C++
  409. // code to use the constant directly in contexts which can take a literal
  410. // integer, such as Builder.CreateExtractValue(thing, 1), without going to the
  411. // effort of calling llvm::ConstantInt::get() and then pulling the constant
  412. // back out of the resulting llvm:Value later.
  413. class Result {
  414. public:
  415. // Convenient shorthand for the pointer type we'll be using everywhere.
  416. using Ptr = std::shared_ptr<Result>;
  417. private:
  418. Ptr Predecessor;
  419. std::string VarName;
  420. bool VarNameUsed = false;
  421. unsigned Visited = 0;
  422. public:
  423. virtual ~Result() = default;
  424. using Scope = std::map<std::string, Ptr>;
  425. virtual void genCode(raw_ostream &OS, CodeGenParamAllocator &) const = 0;
  426. virtual bool hasIntegerConstantValue() const { return false; }
  427. virtual uint32_t integerConstantValue() const { return 0; }
  428. virtual bool hasIntegerValue() const { return false; }
  429. virtual std::string getIntegerValue(const std::string &) {
  430. llvm_unreachable("non-working Result::getIntegerValue called");
  431. }
  432. virtual std::string typeName() const { return "Value *"; }
  433. // Mostly, when a code-generation operation has a dependency on prior
  434. // operations, it's because it uses the output values of those operations as
  435. // inputs. But there's one exception, which is the use of 'seq' in Tablegen
  436. // to indicate that operations have to be performed in sequence regardless of
  437. // whether they use each others' output values.
  438. //
  439. // So, the actual generation of code is done by depth-first search, using the
  440. // prerequisites() method to get a list of all the other Results that have to
  441. // be computed before this one. That method divides into the 'predecessor',
  442. // set by setPredecessor() while processing a 'seq' dag node, and the list
  443. // returned by 'morePrerequisites', which each subclass implements to return
  444. // a list of the Results it uses as input to whatever its own computation is
  445. // doing.
  446. virtual void morePrerequisites(std::vector<Ptr> &output) const {}
  447. std::vector<Ptr> prerequisites() const {
  448. std::vector<Ptr> ToRet;
  449. if (Predecessor)
  450. ToRet.push_back(Predecessor);
  451. morePrerequisites(ToRet);
  452. return ToRet;
  453. }
  454. void setPredecessor(Ptr p) {
  455. // If the user has nested one 'seq' node inside another, and this
  456. // method is called on the return value of the inner 'seq' (i.e.
  457. // the final item inside it), then we can't link _this_ node to p,
  458. // because it already has a predecessor. Instead, walk the chain
  459. // until we find the first item in the inner seq, and link that to
  460. // p, so that nesting seqs has the obvious effect of linking
  461. // everything together into one long sequential chain.
  462. Result *r = this;
  463. while (r->Predecessor)
  464. r = r->Predecessor.get();
  465. r->Predecessor = p;
  466. }
  467. // Each Result will be assigned a variable name in the output code, but not
  468. // all those variable names will actually be used (e.g. the return value of
  469. // Builder.CreateStore has void type, so nobody will want to refer to it). To
  470. // prevent annoying compiler warnings, we track whether each Result's
  471. // variable name was ever actually mentioned in subsequent statements, so
  472. // that it can be left out of the final generated code.
  473. std::string varname() {
  474. VarNameUsed = true;
  475. return VarName;
  476. }
  477. void setVarname(const StringRef s) { VarName = std::string(s); }
  478. bool varnameUsed() const { return VarNameUsed; }
  479. // Emit code to generate this result as a Value *.
  480. virtual std::string asValue() {
  481. return varname();
  482. }
  483. // Code generation happens in multiple passes. This method tracks whether a
  484. // Result has yet been visited in a given pass, without the need for a
  485. // tedious loop in between passes that goes through and resets a 'visited'
  486. // flag back to false: you just set Pass=1 the first time round, and Pass=2
  487. // the second time.
  488. bool needsVisiting(unsigned Pass) {
  489. bool ToRet = Visited < Pass;
  490. Visited = Pass;
  491. return ToRet;
  492. }
  493. };
  494. // Result subclass that retrieves one of the arguments to the clang builtin
  495. // function. In cases where the argument has pointer type, we call
  496. // EmitPointerWithAlignment and store the result in a variable of type Address,
  497. // so that load and store IR nodes can know the right alignment. Otherwise, we
  498. // call EmitScalarExpr.
  499. //
  500. // There are aggregate parameters in the MVE intrinsics API, but we don't deal
  501. // with them in this Tablegen back end: they only arise in the vld2q/vld4q and
  502. // vst2q/vst4q family, which is few enough that we just write the code by hand
  503. // for those in CGBuiltin.cpp.
  504. class BuiltinArgResult : public Result {
  505. public:
  506. unsigned ArgNum;
  507. bool AddressType;
  508. bool Immediate;
  509. BuiltinArgResult(unsigned ArgNum, bool AddressType, bool Immediate)
  510. : ArgNum(ArgNum), AddressType(AddressType), Immediate(Immediate) {}
  511. void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
  512. OS << (AddressType ? "EmitPointerWithAlignment" : "EmitScalarExpr")
  513. << "(E->getArg(" << ArgNum << "))";
  514. }
  515. std::string typeName() const override {
  516. return AddressType ? "Address" : Result::typeName();
  517. }
  518. // Emit code to generate this result as a Value *.
  519. std::string asValue() override {
  520. if (AddressType)
  521. return "(" + varname() + ".getPointer())";
  522. return Result::asValue();
  523. }
  524. bool hasIntegerValue() const override { return Immediate; }
  525. std::string getIntegerValue(const std::string &IntType) override {
  526. return "GetIntegerConstantValue<" + IntType + ">(E->getArg(" +
  527. utostr(ArgNum) + "), getContext())";
  528. }
  529. };
  530. // Result subclass for an integer literal appearing in Tablegen. This may need
  531. // to be turned into an llvm::Result by means of llvm::ConstantInt::get(), or
  532. // it may be used directly as an integer, depending on which IRBuilder method
  533. // it's being passed to.
  534. class IntLiteralResult : public Result {
  535. public:
  536. const ScalarType *IntegerType;
  537. uint32_t IntegerValue;
  538. IntLiteralResult(const ScalarType *IntegerType, uint32_t IntegerValue)
  539. : IntegerType(IntegerType), IntegerValue(IntegerValue) {}
  540. void genCode(raw_ostream &OS,
  541. CodeGenParamAllocator &ParamAlloc) const override {
  542. OS << "llvm::ConstantInt::get("
  543. << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName())
  544. << ", ";
  545. OS << ParamAlloc.allocParam(IntegerType->cName(), utostr(IntegerValue))
  546. << ")";
  547. }
  548. bool hasIntegerConstantValue() const override { return true; }
  549. uint32_t integerConstantValue() const override { return IntegerValue; }
  550. };
  551. // Result subclass representing a cast between different integer types. We use
  552. // our own ScalarType abstraction as the representation of the target type,
  553. // which gives both size and signedness.
  554. class IntCastResult : public Result {
  555. public:
  556. const ScalarType *IntegerType;
  557. Ptr V;
  558. IntCastResult(const ScalarType *IntegerType, Ptr V)
  559. : IntegerType(IntegerType), V(V) {}
  560. void genCode(raw_ostream &OS,
  561. CodeGenParamAllocator &ParamAlloc) const override {
  562. OS << "Builder.CreateIntCast(" << V->varname() << ", "
  563. << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) << ", "
  564. << ParamAlloc.allocParam("bool",
  565. IntegerType->kind() == ScalarTypeKind::SignedInt
  566. ? "true"
  567. : "false")
  568. << ")";
  569. }
  570. void morePrerequisites(std::vector<Ptr> &output) const override {
  571. output.push_back(V);
  572. }
  573. };
  574. // Result subclass representing a cast between different pointer types.
  575. class PointerCastResult : public Result {
  576. public:
  577. const PointerType *PtrType;
  578. Ptr V;
  579. PointerCastResult(const PointerType *PtrType, Ptr V)
  580. : PtrType(PtrType), V(V) {}
  581. void genCode(raw_ostream &OS,
  582. CodeGenParamAllocator &ParamAlloc) const override {
  583. OS << "Builder.CreatePointerCast(" << V->asValue() << ", "
  584. << ParamAlloc.allocParam("llvm::Type *", PtrType->llvmName()) << ")";
  585. }
  586. void morePrerequisites(std::vector<Ptr> &output) const override {
  587. output.push_back(V);
  588. }
  589. };
  590. // Result subclass representing a call to an IRBuilder method. Each IRBuilder
  591. // method we want to use will have a Tablegen record giving the method name and
  592. // describing any important details of how to call it, such as whether a
  593. // particular argument should be an integer constant instead of an llvm::Value.
  594. class IRBuilderResult : public Result {
  595. public:
  596. StringRef CallPrefix;
  597. std::vector<Ptr> Args;
  598. std::set<unsigned> AddressArgs;
  599. std::map<unsigned, std::string> IntegerArgs;
  600. IRBuilderResult(StringRef CallPrefix, std::vector<Ptr> Args,
  601. std::set<unsigned> AddressArgs,
  602. std::map<unsigned, std::string> IntegerArgs)
  603. : CallPrefix(CallPrefix), Args(Args), AddressArgs(AddressArgs),
  604. IntegerArgs(IntegerArgs) {}
  605. void genCode(raw_ostream &OS,
  606. CodeGenParamAllocator &ParamAlloc) const override {
  607. OS << CallPrefix;
  608. const char *Sep = "";
  609. for (unsigned i = 0, e = Args.size(); i < e; ++i) {
  610. Ptr Arg = Args[i];
  611. auto it = IntegerArgs.find(i);
  612. OS << Sep;
  613. Sep = ", ";
  614. if (it != IntegerArgs.end()) {
  615. if (Arg->hasIntegerConstantValue())
  616. OS << "static_cast<" << it->second << ">("
  617. << ParamAlloc.allocParam(it->second,
  618. utostr(Arg->integerConstantValue()))
  619. << ")";
  620. else if (Arg->hasIntegerValue())
  621. OS << ParamAlloc.allocParam(it->second,
  622. Arg->getIntegerValue(it->second));
  623. } else {
  624. OS << Arg->varname();
  625. }
  626. }
  627. OS << ")";
  628. }
  629. void morePrerequisites(std::vector<Ptr> &output) const override {
  630. for (unsigned i = 0, e = Args.size(); i < e; ++i) {
  631. Ptr Arg = Args[i];
  632. if (IntegerArgs.find(i) != IntegerArgs.end())
  633. continue;
  634. output.push_back(Arg);
  635. }
  636. }
  637. };
  638. // Result subclass representing making an Address out of a Value.
  639. class AddressResult : public Result {
  640. public:
  641. Ptr Arg;
  642. const Type *Ty;
  643. unsigned Align;
  644. AddressResult(Ptr Arg, const Type *Ty, unsigned Align)
  645. : Arg(Arg), Ty(Ty), Align(Align) {}
  646. void genCode(raw_ostream &OS,
  647. CodeGenParamAllocator &ParamAlloc) const override {
  648. OS << "Address(" << Arg->varname() << ", " << Ty->llvmName()
  649. << ", CharUnits::fromQuantity(" << Align << "))";
  650. }
  651. std::string typeName() const override {
  652. return "Address";
  653. }
  654. void morePrerequisites(std::vector<Ptr> &output) const override {
  655. output.push_back(Arg);
  656. }
  657. };
  658. // Result subclass representing a call to an IR intrinsic, which we first have
  659. // to look up using an Intrinsic::ID constant and an array of types.
  660. class IRIntrinsicResult : public Result {
  661. public:
  662. std::string IntrinsicID;
  663. std::vector<const Type *> ParamTypes;
  664. std::vector<Ptr> Args;
  665. IRIntrinsicResult(StringRef IntrinsicID, std::vector<const Type *> ParamTypes,
  666. std::vector<Ptr> Args)
  667. : IntrinsicID(std::string(IntrinsicID)), ParamTypes(ParamTypes),
  668. Args(Args) {}
  669. void genCode(raw_ostream &OS,
  670. CodeGenParamAllocator &ParamAlloc) const override {
  671. std::string IntNo = ParamAlloc.allocParam(
  672. "Intrinsic::ID", "Intrinsic::" + IntrinsicID);
  673. OS << "Builder.CreateCall(CGM.getIntrinsic(" << IntNo;
  674. if (!ParamTypes.empty()) {
  675. OS << ", {";
  676. const char *Sep = "";
  677. for (auto T : ParamTypes) {
  678. OS << Sep << ParamAlloc.allocParam("llvm::Type *", T->llvmName());
  679. Sep = ", ";
  680. }
  681. OS << "}";
  682. }
  683. OS << "), {";
  684. const char *Sep = "";
  685. for (auto Arg : Args) {
  686. OS << Sep << Arg->asValue();
  687. Sep = ", ";
  688. }
  689. OS << "})";
  690. }
  691. void morePrerequisites(std::vector<Ptr> &output) const override {
  692. output.insert(output.end(), Args.begin(), Args.end());
  693. }
  694. };
  695. // Result subclass that specifies a type, for use in IRBuilder operations such
  696. // as CreateBitCast that take a type argument.
  697. class TypeResult : public Result {
  698. public:
  699. const Type *T;
  700. TypeResult(const Type *T) : T(T) {}
  701. void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
  702. OS << T->llvmName();
  703. }
  704. std::string typeName() const override {
  705. return "llvm::Type *";
  706. }
  707. };
  708. // -----------------------------------------------------------------------------
  709. // Class that describes a single ACLE intrinsic.
  710. //
  711. // A Tablegen record will typically describe more than one ACLE intrinsic, by
  712. // means of setting the 'list<Type> Params' field to a list of multiple
  713. // parameter types, so as to define vaddq_{s8,u8,...,f16,f32} all in one go.
  714. // We'll end up with one instance of ACLEIntrinsic for *each* parameter type,
  715. // rather than a single one for all of them. Hence, the constructor takes both
  716. // a Tablegen record and the current value of the parameter type.
  717. class ACLEIntrinsic {
  718. // Structure documenting that one of the intrinsic's arguments is required to
  719. // be a compile-time constant integer, and what constraints there are on its
  720. // value. Used when generating Sema checking code.
  721. struct ImmediateArg {
  722. enum class BoundsType { ExplicitRange, UInt };
  723. BoundsType boundsType;
  724. int64_t i1, i2;
  725. StringRef ExtraCheckType, ExtraCheckArgs;
  726. const Type *ArgType;
  727. };
  728. // For polymorphic intrinsics, FullName is the explicit name that uniquely
  729. // identifies this variant of the intrinsic, and ShortName is the name it
  730. // shares with at least one other intrinsic.
  731. std::string ShortName, FullName;
  732. // Name of the architecture extension, used in the Clang builtin name
  733. StringRef BuiltinExtension;
  734. // A very small number of intrinsics _only_ have a polymorphic
  735. // variant (vuninitializedq taking an unevaluated argument).
  736. bool PolymorphicOnly;
  737. // Another rarely-used flag indicating that the builtin doesn't
  738. // evaluate its argument(s) at all.
  739. bool NonEvaluating;
  740. // True if the intrinsic needs only the C header part (no codegen, semantic
  741. // checks, etc). Used for redeclaring MVE intrinsics in the arm_cde.h header.
  742. bool HeaderOnly;
  743. const Type *ReturnType;
  744. std::vector<const Type *> ArgTypes;
  745. std::map<unsigned, ImmediateArg> ImmediateArgs;
  746. Result::Ptr Code;
  747. std::map<std::string, std::string> CustomCodeGenArgs;
  748. // Recursive function that does the internals of code generation.
  749. void genCodeDfs(Result::Ptr V, std::list<Result::Ptr> &Used,
  750. unsigned Pass) const {
  751. if (!V->needsVisiting(Pass))
  752. return;
  753. for (Result::Ptr W : V->prerequisites())
  754. genCodeDfs(W, Used, Pass);
  755. Used.push_back(V);
  756. }
  757. public:
  758. const std::string &shortName() const { return ShortName; }
  759. const std::string &fullName() const { return FullName; }
  760. StringRef builtinExtension() const { return BuiltinExtension; }
  761. const Type *returnType() const { return ReturnType; }
  762. const std::vector<const Type *> &argTypes() const { return ArgTypes; }
  763. bool requiresFloat() const {
  764. if (ReturnType->requiresFloat())
  765. return true;
  766. for (const Type *T : ArgTypes)
  767. if (T->requiresFloat())
  768. return true;
  769. return false;
  770. }
  771. bool requiresMVE() const {
  772. return ReturnType->requiresMVE() ||
  773. any_of(ArgTypes, [](const Type *T) { return T->requiresMVE(); });
  774. }
  775. bool polymorphic() const { return ShortName != FullName; }
  776. bool polymorphicOnly() const { return PolymorphicOnly; }
  777. bool nonEvaluating() const { return NonEvaluating; }
  778. bool headerOnly() const { return HeaderOnly; }
  779. // External entry point for code generation, called from EmitterBase.
  780. void genCode(raw_ostream &OS, CodeGenParamAllocator &ParamAlloc,
  781. unsigned Pass) const {
  782. assert(!headerOnly() && "Called genCode for header-only intrinsic");
  783. if (!hasCode()) {
  784. for (auto kv : CustomCodeGenArgs)
  785. OS << " " << kv.first << " = " << kv.second << ";\n";
  786. OS << " break; // custom code gen\n";
  787. return;
  788. }
  789. std::list<Result::Ptr> Used;
  790. genCodeDfs(Code, Used, Pass);
  791. unsigned varindex = 0;
  792. for (Result::Ptr V : Used)
  793. if (V->varnameUsed())
  794. V->setVarname("Val" + utostr(varindex++));
  795. for (Result::Ptr V : Used) {
  796. OS << " ";
  797. if (V == Used.back()) {
  798. assert(!V->varnameUsed());
  799. OS << "return "; // FIXME: what if the top-level thing is void?
  800. } else if (V->varnameUsed()) {
  801. std::string Type = V->typeName();
  802. OS << V->typeName();
  803. if (!StringRef(Type).endswith("*"))
  804. OS << " ";
  805. OS << V->varname() << " = ";
  806. }
  807. V->genCode(OS, ParamAlloc);
  808. OS << ";\n";
  809. }
  810. }
  811. bool hasCode() const { return Code != nullptr; }
  812. static std::string signedHexLiteral(const llvm::APInt &iOrig) {
  813. llvm::APInt i = iOrig.trunc(64);
  814. SmallString<40> s;
  815. i.toString(s, 16, true, true);
  816. return std::string(s.str());
  817. }
  818. std::string genSema() const {
  819. assert(!headerOnly() && "Called genSema for header-only intrinsic");
  820. std::vector<std::string> SemaChecks;
  821. for (const auto &kv : ImmediateArgs) {
  822. const ImmediateArg &IA = kv.second;
  823. llvm::APInt lo(128, 0), hi(128, 0);
  824. switch (IA.boundsType) {
  825. case ImmediateArg::BoundsType::ExplicitRange:
  826. lo = IA.i1;
  827. hi = IA.i2;
  828. break;
  829. case ImmediateArg::BoundsType::UInt:
  830. lo = 0;
  831. hi = llvm::APInt::getMaxValue(IA.i1).zext(128);
  832. break;
  833. }
  834. std::string Index = utostr(kv.first);
  835. // Emit a range check if the legal range of values for the
  836. // immediate is smaller than the _possible_ range of values for
  837. // its type.
  838. unsigned ArgTypeBits = IA.ArgType->sizeInBits();
  839. llvm::APInt ArgTypeRange = llvm::APInt::getMaxValue(ArgTypeBits).zext(128);
  840. llvm::APInt ActualRange = (hi-lo).trunc(64).sext(128);
  841. if (ActualRange.ult(ArgTypeRange))
  842. SemaChecks.push_back("SemaBuiltinConstantArgRange(TheCall, " + Index +
  843. ", " + signedHexLiteral(lo) + ", " +
  844. signedHexLiteral(hi) + ")");
  845. if (!IA.ExtraCheckType.empty()) {
  846. std::string Suffix;
  847. if (!IA.ExtraCheckArgs.empty()) {
  848. std::string tmp;
  849. StringRef Arg = IA.ExtraCheckArgs;
  850. if (Arg == "!lanesize") {
  851. tmp = utostr(IA.ArgType->sizeInBits());
  852. Arg = tmp;
  853. }
  854. Suffix = (Twine(", ") + Arg).str();
  855. }
  856. SemaChecks.push_back((Twine("SemaBuiltinConstantArg") +
  857. IA.ExtraCheckType + "(TheCall, " + Index +
  858. Suffix + ")")
  859. .str());
  860. }
  861. assert(!SemaChecks.empty());
  862. }
  863. if (SemaChecks.empty())
  864. return "";
  865. return join(std::begin(SemaChecks), std::end(SemaChecks),
  866. " ||\n ") +
  867. ";\n";
  868. }
  869. ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param);
  870. };
  871. // -----------------------------------------------------------------------------
  872. // The top-level class that holds all the state from analyzing the entire
  873. // Tablegen input.
  874. class EmitterBase {
  875. protected:
  876. // EmitterBase holds a collection of all the types we've instantiated.
  877. VoidType Void;
  878. std::map<std::string, std::unique_ptr<ScalarType>> ScalarTypes;
  879. std::map<std::tuple<ScalarTypeKind, unsigned, unsigned>,
  880. std::unique_ptr<VectorType>>
  881. VectorTypes;
  882. std::map<std::pair<std::string, unsigned>, std::unique_ptr<MultiVectorType>>
  883. MultiVectorTypes;
  884. std::map<unsigned, std::unique_ptr<PredicateType>> PredicateTypes;
  885. std::map<std::string, std::unique_ptr<PointerType>> PointerTypes;
  886. // And all the ACLEIntrinsic instances we've created.
  887. std::map<std::string, std::unique_ptr<ACLEIntrinsic>> ACLEIntrinsics;
  888. public:
  889. // Methods to create a Type object, or return the right existing one from the
  890. // maps stored in this object.
  891. const VoidType *getVoidType() { return &Void; }
  892. const ScalarType *getScalarType(StringRef Name) {
  893. return ScalarTypes[std::string(Name)].get();
  894. }
  895. const ScalarType *getScalarType(Record *R) {
  896. return getScalarType(R->getName());
  897. }
  898. const VectorType *getVectorType(const ScalarType *ST, unsigned Lanes) {
  899. std::tuple<ScalarTypeKind, unsigned, unsigned> key(ST->kind(),
  900. ST->sizeInBits(), Lanes);
  901. if (VectorTypes.find(key) == VectorTypes.end())
  902. VectorTypes[key] = std::make_unique<VectorType>(ST, Lanes);
  903. return VectorTypes[key].get();
  904. }
  905. const VectorType *getVectorType(const ScalarType *ST) {
  906. return getVectorType(ST, 128 / ST->sizeInBits());
  907. }
  908. const MultiVectorType *getMultiVectorType(unsigned Registers,
  909. const VectorType *VT) {
  910. std::pair<std::string, unsigned> key(VT->cNameBase(), Registers);
  911. if (MultiVectorTypes.find(key) == MultiVectorTypes.end())
  912. MultiVectorTypes[key] = std::make_unique<MultiVectorType>(Registers, VT);
  913. return MultiVectorTypes[key].get();
  914. }
  915. const PredicateType *getPredicateType(unsigned Lanes) {
  916. unsigned key = Lanes;
  917. if (PredicateTypes.find(key) == PredicateTypes.end())
  918. PredicateTypes[key] = std::make_unique<PredicateType>(Lanes);
  919. return PredicateTypes[key].get();
  920. }
  921. const PointerType *getPointerType(const Type *T, bool Const) {
  922. PointerType PT(T, Const);
  923. std::string key = PT.cName();
  924. if (PointerTypes.find(key) == PointerTypes.end())
  925. PointerTypes[key] = std::make_unique<PointerType>(PT);
  926. return PointerTypes[key].get();
  927. }
  928. // Methods to construct a type from various pieces of Tablegen. These are
  929. // always called in the context of setting up a particular ACLEIntrinsic, so
  930. // there's always an ambient parameter type (because we're iterating through
  931. // the Params list in the Tablegen record for the intrinsic), which is used
  932. // to expand Tablegen classes like 'Vector' which mean something different in
  933. // each member of a parametric family.
  934. const Type *getType(Record *R, const Type *Param);
  935. const Type *getType(DagInit *D, const Type *Param);
  936. const Type *getType(Init *I, const Type *Param);
  937. // Functions that translate the Tablegen representation of an intrinsic's
  938. // code generation into a collection of Value objects (which will then be
  939. // reprocessed to read out the actual C++ code included by CGBuiltin.cpp).
  940. Result::Ptr getCodeForDag(DagInit *D, const Result::Scope &Scope,
  941. const Type *Param);
  942. Result::Ptr getCodeForDagArg(DagInit *D, unsigned ArgNum,
  943. const Result::Scope &Scope, const Type *Param);
  944. Result::Ptr getCodeForArg(unsigned ArgNum, const Type *ArgType, bool Promote,
  945. bool Immediate);
  946. void GroupSemaChecks(std::map<std::string, std::set<std::string>> &Checks);
  947. // Constructor and top-level functions.
  948. EmitterBase(RecordKeeper &Records);
  949. virtual ~EmitterBase() = default;
  950. virtual void EmitHeader(raw_ostream &OS) = 0;
  951. virtual void EmitBuiltinDef(raw_ostream &OS) = 0;
  952. virtual void EmitBuiltinSema(raw_ostream &OS) = 0;
  953. void EmitBuiltinCG(raw_ostream &OS);
  954. void EmitBuiltinAliases(raw_ostream &OS);
  955. };
  956. const Type *EmitterBase::getType(Init *I, const Type *Param) {
  957. if (auto Dag = dyn_cast<DagInit>(I))
  958. return getType(Dag, Param);
  959. if (auto Def = dyn_cast<DefInit>(I))
  960. return getType(Def->getDef(), Param);
  961. PrintFatalError("Could not convert this value into a type");
  962. }
  963. const Type *EmitterBase::getType(Record *R, const Type *Param) {
  964. // Pass to a subfield of any wrapper records. We don't expect more than one
  965. // of these: immediate operands are used as plain numbers rather than as
  966. // llvm::Value, so it's meaningless to promote their type anyway.
  967. if (R->isSubClassOf("Immediate"))
  968. R = R->getValueAsDef("type");
  969. else if (R->isSubClassOf("unpromoted"))
  970. R = R->getValueAsDef("underlying_type");
  971. if (R->getName() == "Void")
  972. return getVoidType();
  973. if (R->isSubClassOf("PrimitiveType"))
  974. return getScalarType(R);
  975. if (R->isSubClassOf("ComplexType"))
  976. return getType(R->getValueAsDag("spec"), Param);
  977. PrintFatalError(R->getLoc(), "Could not convert this record into a type");
  978. }
  979. const Type *EmitterBase::getType(DagInit *D, const Type *Param) {
  980. // The meat of the getType system: types in the Tablegen are represented by a
  981. // dag whose operators select sub-cases of this function.
  982. Record *Op = cast<DefInit>(D->getOperator())->getDef();
  983. if (!Op->isSubClassOf("ComplexTypeOp"))
  984. PrintFatalError(
  985. "Expected ComplexTypeOp as dag operator in type expression");
  986. if (Op->getName() == "CTO_Parameter") {
  987. if (isa<VoidType>(Param))
  988. PrintFatalError("Parametric type in unparametrised context");
  989. return Param;
  990. }
  991. if (Op->getName() == "CTO_Vec") {
  992. const Type *Element = getType(D->getArg(0), Param);
  993. if (D->getNumArgs() == 1) {
  994. return getVectorType(cast<ScalarType>(Element));
  995. } else {
  996. const Type *ExistingVector = getType(D->getArg(1), Param);
  997. return getVectorType(cast<ScalarType>(Element),
  998. cast<VectorType>(ExistingVector)->lanes());
  999. }
  1000. }
  1001. if (Op->getName() == "CTO_Pred") {
  1002. const Type *Element = getType(D->getArg(0), Param);
  1003. return getPredicateType(128 / Element->sizeInBits());
  1004. }
  1005. if (Op->isSubClassOf("CTO_Tuple")) {
  1006. unsigned Registers = Op->getValueAsInt("n");
  1007. const Type *Element = getType(D->getArg(0), Param);
  1008. return getMultiVectorType(Registers, cast<VectorType>(Element));
  1009. }
  1010. if (Op->isSubClassOf("CTO_Pointer")) {
  1011. const Type *Pointee = getType(D->getArg(0), Param);
  1012. return getPointerType(Pointee, Op->getValueAsBit("const"));
  1013. }
  1014. if (Op->getName() == "CTO_CopyKind") {
  1015. const ScalarType *STSize = cast<ScalarType>(getType(D->getArg(0), Param));
  1016. const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(1), Param));
  1017. for (const auto &kv : ScalarTypes) {
  1018. const ScalarType *RT = kv.second.get();
  1019. if (RT->kind() == STKind->kind() && RT->sizeInBits() == STSize->sizeInBits())
  1020. return RT;
  1021. }
  1022. PrintFatalError("Cannot find a type to satisfy CopyKind");
  1023. }
  1024. if (Op->isSubClassOf("CTO_ScaleSize")) {
  1025. const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(0), Param));
  1026. int Num = Op->getValueAsInt("num"), Denom = Op->getValueAsInt("denom");
  1027. unsigned DesiredSize = STKind->sizeInBits() * Num / Denom;
  1028. for (const auto &kv : ScalarTypes) {
  1029. const ScalarType *RT = kv.second.get();
  1030. if (RT->kind() == STKind->kind() && RT->sizeInBits() == DesiredSize)
  1031. return RT;
  1032. }
  1033. PrintFatalError("Cannot find a type to satisfy ScaleSize");
  1034. }
  1035. PrintFatalError("Bad operator in type dag expression");
  1036. }
  1037. Result::Ptr EmitterBase::getCodeForDag(DagInit *D, const Result::Scope &Scope,
  1038. const Type *Param) {
  1039. Record *Op = cast<DefInit>(D->getOperator())->getDef();
  1040. if (Op->getName() == "seq") {
  1041. Result::Scope SubScope = Scope;
  1042. Result::Ptr PrevV = nullptr;
  1043. for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) {
  1044. // We don't use getCodeForDagArg here, because the argument name
  1045. // has different semantics in a seq
  1046. Result::Ptr V =
  1047. getCodeForDag(cast<DagInit>(D->getArg(i)), SubScope, Param);
  1048. StringRef ArgName = D->getArgNameStr(i);
  1049. if (!ArgName.empty())
  1050. SubScope[std::string(ArgName)] = V;
  1051. if (PrevV)
  1052. V->setPredecessor(PrevV);
  1053. PrevV = V;
  1054. }
  1055. return PrevV;
  1056. } else if (Op->isSubClassOf("Type")) {
  1057. if (D->getNumArgs() != 1)
  1058. PrintFatalError("Type casts should have exactly one argument");
  1059. const Type *CastType = getType(Op, Param);
  1060. Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
  1061. if (const auto *ST = dyn_cast<ScalarType>(CastType)) {
  1062. if (!ST->requiresFloat()) {
  1063. if (Arg->hasIntegerConstantValue())
  1064. return std::make_shared<IntLiteralResult>(
  1065. ST, Arg->integerConstantValue());
  1066. else
  1067. return std::make_shared<IntCastResult>(ST, Arg);
  1068. }
  1069. } else if (const auto *PT = dyn_cast<PointerType>(CastType)) {
  1070. return std::make_shared<PointerCastResult>(PT, Arg);
  1071. }
  1072. PrintFatalError("Unsupported type cast");
  1073. } else if (Op->getName() == "address") {
  1074. if (D->getNumArgs() != 2)
  1075. PrintFatalError("'address' should have two arguments");
  1076. Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
  1077. const Type *Ty = nullptr;
  1078. if (auto *DI = dyn_cast<DagInit>(D->getArg(0)))
  1079. if (auto *PTy = dyn_cast<PointerType>(getType(DI->getOperator(), Param)))
  1080. Ty = PTy->getPointeeType();
  1081. if (!Ty)
  1082. PrintFatalError("'address' pointer argument should be a pointer");
  1083. unsigned Alignment;
  1084. if (auto *II = dyn_cast<IntInit>(D->getArg(1))) {
  1085. Alignment = II->getValue();
  1086. } else {
  1087. PrintFatalError("'address' alignment argument should be an integer");
  1088. }
  1089. return std::make_shared<AddressResult>(Arg, Ty, Alignment);
  1090. } else if (Op->getName() == "unsignedflag") {
  1091. if (D->getNumArgs() != 1)
  1092. PrintFatalError("unsignedflag should have exactly one argument");
  1093. Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
  1094. if (!TypeRec->isSubClassOf("Type"))
  1095. PrintFatalError("unsignedflag's argument should be a type");
  1096. if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
  1097. return std::make_shared<IntLiteralResult>(
  1098. getScalarType("u32"), ST->kind() == ScalarTypeKind::UnsignedInt);
  1099. } else {
  1100. PrintFatalError("unsignedflag's argument should be a scalar type");
  1101. }
  1102. } else if (Op->getName() == "bitsize") {
  1103. if (D->getNumArgs() != 1)
  1104. PrintFatalError("bitsize should have exactly one argument");
  1105. Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
  1106. if (!TypeRec->isSubClassOf("Type"))
  1107. PrintFatalError("bitsize's argument should be a type");
  1108. if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
  1109. return std::make_shared<IntLiteralResult>(getScalarType("u32"),
  1110. ST->sizeInBits());
  1111. } else {
  1112. PrintFatalError("bitsize's argument should be a scalar type");
  1113. }
  1114. } else {
  1115. std::vector<Result::Ptr> Args;
  1116. for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i)
  1117. Args.push_back(getCodeForDagArg(D, i, Scope, Param));
  1118. if (Op->isSubClassOf("IRBuilderBase")) {
  1119. std::set<unsigned> AddressArgs;
  1120. std::map<unsigned, std::string> IntegerArgs;
  1121. for (Record *sp : Op->getValueAsListOfDefs("special_params")) {
  1122. unsigned Index = sp->getValueAsInt("index");
  1123. if (sp->isSubClassOf("IRBuilderAddrParam")) {
  1124. AddressArgs.insert(Index);
  1125. } else if (sp->isSubClassOf("IRBuilderIntParam")) {
  1126. IntegerArgs[Index] = std::string(sp->getValueAsString("type"));
  1127. }
  1128. }
  1129. return std::make_shared<IRBuilderResult>(Op->getValueAsString("prefix"),
  1130. Args, AddressArgs, IntegerArgs);
  1131. } else if (Op->isSubClassOf("IRIntBase")) {
  1132. std::vector<const Type *> ParamTypes;
  1133. for (Record *RParam : Op->getValueAsListOfDefs("params"))
  1134. ParamTypes.push_back(getType(RParam, Param));
  1135. std::string IntName = std::string(Op->getValueAsString("intname"));
  1136. if (Op->getValueAsBit("appendKind"))
  1137. IntName += "_" + toLetter(cast<ScalarType>(Param)->kind());
  1138. return std::make_shared<IRIntrinsicResult>(IntName, ParamTypes, Args);
  1139. } else {
  1140. PrintFatalError("Unsupported dag node " + Op->getName());
  1141. }
  1142. }
  1143. }
  1144. Result::Ptr EmitterBase::getCodeForDagArg(DagInit *D, unsigned ArgNum,
  1145. const Result::Scope &Scope,
  1146. const Type *Param) {
  1147. Init *Arg = D->getArg(ArgNum);
  1148. StringRef Name = D->getArgNameStr(ArgNum);
  1149. if (!Name.empty()) {
  1150. if (!isa<UnsetInit>(Arg))
  1151. PrintFatalError(
  1152. "dag operator argument should not have both a value and a name");
  1153. auto it = Scope.find(std::string(Name));
  1154. if (it == Scope.end())
  1155. PrintFatalError("unrecognized variable name '" + Name + "'");
  1156. return it->second;
  1157. }
  1158. // Sometimes the Arg is a bit. Prior to multiclass template argument
  1159. // checking, integers would sneak through the bit declaration,
  1160. // but now they really are bits.
  1161. if (auto *BI = dyn_cast<BitInit>(Arg))
  1162. return std::make_shared<IntLiteralResult>(getScalarType("u32"),
  1163. BI->getValue());
  1164. if (auto *II = dyn_cast<IntInit>(Arg))
  1165. return std::make_shared<IntLiteralResult>(getScalarType("u32"),
  1166. II->getValue());
  1167. if (auto *DI = dyn_cast<DagInit>(Arg))
  1168. return getCodeForDag(DI, Scope, Param);
  1169. if (auto *DI = dyn_cast<DefInit>(Arg)) {
  1170. Record *Rec = DI->getDef();
  1171. if (Rec->isSubClassOf("Type")) {
  1172. const Type *T = getType(Rec, Param);
  1173. return std::make_shared<TypeResult>(T);
  1174. }
  1175. }
  1176. PrintError("bad DAG argument type for code generation");
  1177. PrintNote("DAG: " + D->getAsString());
  1178. if (TypedInit *Typed = dyn_cast<TypedInit>(Arg))
  1179. PrintNote("argument type: " + Typed->getType()->getAsString());
  1180. PrintFatalNote("argument number " + Twine(ArgNum) + ": " + Arg->getAsString());
  1181. }
  1182. Result::Ptr EmitterBase::getCodeForArg(unsigned ArgNum, const Type *ArgType,
  1183. bool Promote, bool Immediate) {
  1184. Result::Ptr V = std::make_shared<BuiltinArgResult>(
  1185. ArgNum, isa<PointerType>(ArgType), Immediate);
  1186. if (Promote) {
  1187. if (const auto *ST = dyn_cast<ScalarType>(ArgType)) {
  1188. if (ST->isInteger() && ST->sizeInBits() < 32)
  1189. V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
  1190. } else if (const auto *PT = dyn_cast<PredicateType>(ArgType)) {
  1191. V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
  1192. V = std::make_shared<IRIntrinsicResult>("arm_mve_pred_i2v",
  1193. std::vector<const Type *>{PT},
  1194. std::vector<Result::Ptr>{V});
  1195. }
  1196. }
  1197. return V;
  1198. }
  1199. ACLEIntrinsic::ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param)
  1200. : ReturnType(ME.getType(R->getValueAsDef("ret"), Param)) {
  1201. // Derive the intrinsic's full name, by taking the name of the
  1202. // Tablegen record (or override) and appending the suffix from its
  1203. // parameter type. (If the intrinsic is unparametrised, its
  1204. // parameter type will be given as Void, which returns the empty
  1205. // string for acleSuffix.)
  1206. StringRef BaseName =
  1207. (R->isSubClassOf("NameOverride") ? R->getValueAsString("basename")
  1208. : R->getName());
  1209. StringRef overrideLetter = R->getValueAsString("overrideKindLetter");
  1210. FullName =
  1211. (Twine(BaseName) + Param->acleSuffix(std::string(overrideLetter))).str();
  1212. // Derive the intrinsic's polymorphic name, by removing components from the
  1213. // full name as specified by its 'pnt' member ('polymorphic name type'),
  1214. // which indicates how many type suffixes to remove, and any other piece of
  1215. // the name that should be removed.
  1216. Record *PolymorphicNameType = R->getValueAsDef("pnt");
  1217. SmallVector<StringRef, 8> NameParts;
  1218. StringRef(FullName).split(NameParts, '_');
  1219. for (unsigned i = 0, e = PolymorphicNameType->getValueAsInt(
  1220. "NumTypeSuffixesToDiscard");
  1221. i < e; ++i)
  1222. NameParts.pop_back();
  1223. if (!PolymorphicNameType->isValueUnset("ExtraSuffixToDiscard")) {
  1224. StringRef ExtraSuffix =
  1225. PolymorphicNameType->getValueAsString("ExtraSuffixToDiscard");
  1226. auto it = NameParts.end();
  1227. while (it != NameParts.begin()) {
  1228. --it;
  1229. if (*it == ExtraSuffix) {
  1230. NameParts.erase(it);
  1231. break;
  1232. }
  1233. }
  1234. }
  1235. ShortName = join(std::begin(NameParts), std::end(NameParts), "_");
  1236. BuiltinExtension = R->getValueAsString("builtinExtension");
  1237. PolymorphicOnly = R->getValueAsBit("polymorphicOnly");
  1238. NonEvaluating = R->getValueAsBit("nonEvaluating");
  1239. HeaderOnly = R->getValueAsBit("headerOnly");
  1240. // Process the intrinsic's argument list.
  1241. DagInit *ArgsDag = R->getValueAsDag("args");
  1242. Result::Scope Scope;
  1243. for (unsigned i = 0, e = ArgsDag->getNumArgs(); i < e; ++i) {
  1244. Init *TypeInit = ArgsDag->getArg(i);
  1245. bool Promote = true;
  1246. if (auto TypeDI = dyn_cast<DefInit>(TypeInit))
  1247. if (TypeDI->getDef()->isSubClassOf("unpromoted"))
  1248. Promote = false;
  1249. // Work out the type of the argument, for use in the function prototype in
  1250. // the header file.
  1251. const Type *ArgType = ME.getType(TypeInit, Param);
  1252. ArgTypes.push_back(ArgType);
  1253. // If the argument is a subclass of Immediate, record the details about
  1254. // what values it can take, for Sema checking.
  1255. bool Immediate = false;
  1256. if (auto TypeDI = dyn_cast<DefInit>(TypeInit)) {
  1257. Record *TypeRec = TypeDI->getDef();
  1258. if (TypeRec->isSubClassOf("Immediate")) {
  1259. Immediate = true;
  1260. Record *Bounds = TypeRec->getValueAsDef("bounds");
  1261. ImmediateArg &IA = ImmediateArgs[i];
  1262. if (Bounds->isSubClassOf("IB_ConstRange")) {
  1263. IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
  1264. IA.i1 = Bounds->getValueAsInt("lo");
  1265. IA.i2 = Bounds->getValueAsInt("hi");
  1266. } else if (Bounds->getName() == "IB_UEltValue") {
  1267. IA.boundsType = ImmediateArg::BoundsType::UInt;
  1268. IA.i1 = Param->sizeInBits();
  1269. } else if (Bounds->getName() == "IB_LaneIndex") {
  1270. IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
  1271. IA.i1 = 0;
  1272. IA.i2 = 128 / Param->sizeInBits() - 1;
  1273. } else if (Bounds->isSubClassOf("IB_EltBit")) {
  1274. IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
  1275. IA.i1 = Bounds->getValueAsInt("base");
  1276. const Type *T = ME.getType(Bounds->getValueAsDef("type"), Param);
  1277. IA.i2 = IA.i1 + T->sizeInBits() - 1;
  1278. } else {
  1279. PrintFatalError("unrecognised ImmediateBounds subclass");
  1280. }
  1281. IA.ArgType = ArgType;
  1282. if (!TypeRec->isValueUnset("extra")) {
  1283. IA.ExtraCheckType = TypeRec->getValueAsString("extra");
  1284. if (!TypeRec->isValueUnset("extraarg"))
  1285. IA.ExtraCheckArgs = TypeRec->getValueAsString("extraarg");
  1286. }
  1287. }
  1288. }
  1289. // The argument will usually have a name in the arguments dag, which goes
  1290. // into the variable-name scope that the code gen will refer to.
  1291. StringRef ArgName = ArgsDag->getArgNameStr(i);
  1292. if (!ArgName.empty())
  1293. Scope[std::string(ArgName)] =
  1294. ME.getCodeForArg(i, ArgType, Promote, Immediate);
  1295. }
  1296. // Finally, go through the codegen dag and translate it into a Result object
  1297. // (with an arbitrary DAG of depended-on Results hanging off it).
  1298. DagInit *CodeDag = R->getValueAsDag("codegen");
  1299. Record *MainOp = cast<DefInit>(CodeDag->getOperator())->getDef();
  1300. if (MainOp->isSubClassOf("CustomCodegen")) {
  1301. // Or, if it's the special case of CustomCodegen, just accumulate
  1302. // a list of parameters we're going to assign to variables before
  1303. // breaking from the loop.
  1304. CustomCodeGenArgs["CustomCodeGenType"] =
  1305. (Twine("CustomCodeGen::") + MainOp->getValueAsString("type")).str();
  1306. for (unsigned i = 0, e = CodeDag->getNumArgs(); i < e; ++i) {
  1307. StringRef Name = CodeDag->getArgNameStr(i);
  1308. if (Name.empty()) {
  1309. PrintFatalError("Operands to CustomCodegen should have names");
  1310. } else if (auto *II = dyn_cast<IntInit>(CodeDag->getArg(i))) {
  1311. CustomCodeGenArgs[std::string(Name)] = itostr(II->getValue());
  1312. } else if (auto *SI = dyn_cast<StringInit>(CodeDag->getArg(i))) {
  1313. CustomCodeGenArgs[std::string(Name)] = std::string(SI->getValue());
  1314. } else {
  1315. PrintFatalError("Operands to CustomCodegen should be integers");
  1316. }
  1317. }
  1318. } else {
  1319. Code = ME.getCodeForDag(CodeDag, Scope, Param);
  1320. }
  1321. }
  1322. EmitterBase::EmitterBase(RecordKeeper &Records) {
  1323. // Construct the whole EmitterBase.
  1324. // First, look up all the instances of PrimitiveType. This gives us the list
  1325. // of vector typedefs we have to put in arm_mve.h, and also allows us to
  1326. // collect all the useful ScalarType instances into a big list so that we can
  1327. // use it for operations such as 'find the unsigned version of this signed
  1328. // integer type'.
  1329. for (Record *R : Records.getAllDerivedDefinitions("PrimitiveType"))
  1330. ScalarTypes[std::string(R->getName())] = std::make_unique<ScalarType>(R);
  1331. // Now go through the instances of Intrinsic, and for each one, iterate
  1332. // through its list of type parameters making an ACLEIntrinsic for each one.
  1333. for (Record *R : Records.getAllDerivedDefinitions("Intrinsic")) {
  1334. for (Record *RParam : R->getValueAsListOfDefs("params")) {
  1335. const Type *Param = getType(RParam, getVoidType());
  1336. auto Intrinsic = std::make_unique<ACLEIntrinsic>(*this, R, Param);
  1337. ACLEIntrinsics[Intrinsic->fullName()] = std::move(Intrinsic);
  1338. }
  1339. }
  1340. }
  1341. /// A wrapper on raw_string_ostream that contains its own buffer rather than
  1342. /// having to point it at one elsewhere. (In other words, it works just like
  1343. /// std::ostringstream; also, this makes it convenient to declare a whole array
  1344. /// of them at once.)
  1345. ///
  1346. /// We have to set this up using multiple inheritance, to ensure that the
  1347. /// string member has been constructed before raw_string_ostream's constructor
  1348. /// is given a pointer to it.
  1349. class string_holder {
  1350. protected:
  1351. std::string S;
  1352. };
  1353. class raw_self_contained_string_ostream : private string_holder,
  1354. public raw_string_ostream {
  1355. public:
  1356. raw_self_contained_string_ostream() : raw_string_ostream(S) {}
  1357. };
  1358. const char LLVMLicenseHeader[] =
  1359. " *\n"
  1360. " *\n"
  1361. " * Part of the LLVM Project, under the Apache License v2.0 with LLVM"
  1362. " Exceptions.\n"
  1363. " * See https://llvm.org/LICENSE.txt for license information.\n"
  1364. " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
  1365. " *\n"
  1366. " *===-----------------------------------------------------------------"
  1367. "------===\n"
  1368. " */\n"
  1369. "\n";
  1370. // Machinery for the grouping of intrinsics by similar codegen.
  1371. //
  1372. // The general setup is that 'MergeableGroup' stores the things that a set of
  1373. // similarly shaped intrinsics have in common: the text of their code
  1374. // generation, and the number and type of their parameter variables.
  1375. // MergeableGroup is the key in a std::map whose value is a set of
  1376. // OutputIntrinsic, which stores the ways in which a particular intrinsic
  1377. // specializes the MergeableGroup's generic description: the function name and
  1378. // the _values_ of the parameter variables.
  1379. struct ComparableStringVector : std::vector<std::string> {
  1380. // Infrastructure: a derived class of vector<string> which comes with an
  1381. // ordering, so that it can be used as a key in maps and an element in sets.
  1382. // There's no requirement on the ordering beyond being deterministic.
  1383. bool operator<(const ComparableStringVector &rhs) const {
  1384. if (size() != rhs.size())
  1385. return size() < rhs.size();
  1386. for (size_t i = 0, e = size(); i < e; ++i)
  1387. if ((*this)[i] != rhs[i])
  1388. return (*this)[i] < rhs[i];
  1389. return false;
  1390. }
  1391. };
  1392. struct OutputIntrinsic {
  1393. const ACLEIntrinsic *Int;
  1394. std::string Name;
  1395. ComparableStringVector ParamValues;
  1396. bool operator<(const OutputIntrinsic &rhs) const {
  1397. if (Name != rhs.Name)
  1398. return Name < rhs.Name;
  1399. return ParamValues < rhs.ParamValues;
  1400. }
  1401. };
  1402. struct MergeableGroup {
  1403. std::string Code;
  1404. ComparableStringVector ParamTypes;
  1405. bool operator<(const MergeableGroup &rhs) const {
  1406. if (Code != rhs.Code)
  1407. return Code < rhs.Code;
  1408. return ParamTypes < rhs.ParamTypes;
  1409. }
  1410. };
  1411. void EmitterBase::EmitBuiltinCG(raw_ostream &OS) {
  1412. // Pass 1: generate code for all the intrinsics as if every type or constant
  1413. // that can possibly be abstracted out into a parameter variable will be.
  1414. // This identifies the sets of intrinsics we'll group together into a single
  1415. // piece of code generation.
  1416. std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroupsPrelim;
  1417. for (const auto &kv : ACLEIntrinsics) {
  1418. const ACLEIntrinsic &Int = *kv.second;
  1419. if (Int.headerOnly())
  1420. continue;
  1421. MergeableGroup MG;
  1422. OutputIntrinsic OI;
  1423. OI.Int = &Int;
  1424. OI.Name = Int.fullName();
  1425. CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues};
  1426. raw_string_ostream OS(MG.Code);
  1427. Int.genCode(OS, ParamAllocPrelim, 1);
  1428. OS.flush();
  1429. MergeableGroupsPrelim[MG].insert(OI);
  1430. }
  1431. // Pass 2: for each of those groups, optimize the parameter variable set by
  1432. // eliminating 'parameters' that are the same for all intrinsics in the
  1433. // group, and merging together pairs of parameter variables that take the
  1434. // same values as each other for all intrinsics in the group.
  1435. std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroups;
  1436. for (const auto &kv : MergeableGroupsPrelim) {
  1437. const MergeableGroup &MG = kv.first;
  1438. std::vector<int> ParamNumbers;
  1439. std::map<ComparableStringVector, int> ParamNumberMap;
  1440. // Loop over the parameters for this group.
  1441. for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
  1442. // Is this parameter the same for all intrinsics in the group?
  1443. const OutputIntrinsic &OI_first = *kv.second.begin();
  1444. bool Constant = all_of(kv.second, [&](const OutputIntrinsic &OI) {
  1445. return OI.ParamValues[i] == OI_first.ParamValues[i];
  1446. });
  1447. // If so, record it as -1, meaning 'no parameter variable needed'. Then
  1448. // the corresponding call to allocParam in pass 2 will not generate a
  1449. // variable at all, and just use the value inline.
  1450. if (Constant) {
  1451. ParamNumbers.push_back(-1);
  1452. continue;
  1453. }
  1454. // Otherwise, make a list of the values this parameter takes for each
  1455. // intrinsic, and see if that value vector matches anything we already
  1456. // have. We also record the parameter type, so that we don't accidentally
  1457. // match up two parameter variables with different types. (Not that
  1458. // there's much chance of them having textually equivalent values, but in
  1459. // _principle_ it could happen.)
  1460. ComparableStringVector key;
  1461. key.push_back(MG.ParamTypes[i]);
  1462. for (const auto &OI : kv.second)
  1463. key.push_back(OI.ParamValues[i]);
  1464. auto Found = ParamNumberMap.find(key);
  1465. if (Found != ParamNumberMap.end()) {
  1466. // Yes, an existing parameter variable can be reused for this.
  1467. ParamNumbers.push_back(Found->second);
  1468. continue;
  1469. }
  1470. // No, we need a new parameter variable.
  1471. int ExistingIndex = ParamNumberMap.size();
  1472. ParamNumberMap[key] = ExistingIndex;
  1473. ParamNumbers.push_back(ExistingIndex);
  1474. }
  1475. // Now we're ready to do the pass 2 code generation, which will emit the
  1476. // reduced set of parameter variables we've just worked out.
  1477. for (const auto &OI_prelim : kv.second) {
  1478. const ACLEIntrinsic *Int = OI_prelim.Int;
  1479. MergeableGroup MG;
  1480. OutputIntrinsic OI;
  1481. OI.Int = OI_prelim.Int;
  1482. OI.Name = OI_prelim.Name;
  1483. CodeGenParamAllocator ParamAlloc{&MG.ParamTypes, &OI.ParamValues,
  1484. &ParamNumbers};
  1485. raw_string_ostream OS(MG.Code);
  1486. Int->genCode(OS, ParamAlloc, 2);
  1487. OS.flush();
  1488. MergeableGroups[MG].insert(OI);
  1489. }
  1490. }
  1491. // Output the actual C++ code.
  1492. for (const auto &kv : MergeableGroups) {
  1493. const MergeableGroup &MG = kv.first;
  1494. // List of case statements in the main switch on BuiltinID, and an open
  1495. // brace.
  1496. const char *prefix = "";
  1497. for (const auto &OI : kv.second) {
  1498. OS << prefix << "case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
  1499. << "_" << OI.Name << ":";
  1500. prefix = "\n";
  1501. }
  1502. OS << " {\n";
  1503. if (!MG.ParamTypes.empty()) {
  1504. // If we've got some parameter variables, then emit their declarations...
  1505. for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
  1506. StringRef Type = MG.ParamTypes[i];
  1507. OS << " " << Type;
  1508. if (!Type.endswith("*"))
  1509. OS << " ";
  1510. OS << " Param" << utostr(i) << ";\n";
  1511. }
  1512. // ... and an inner switch on BuiltinID that will fill them in with each
  1513. // individual intrinsic's values.
  1514. OS << " switch (BuiltinID) {\n";
  1515. for (const auto &OI : kv.second) {
  1516. OS << " case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
  1517. << "_" << OI.Name << ":\n";
  1518. for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i)
  1519. OS << " Param" << utostr(i) << " = " << OI.ParamValues[i] << ";\n";
  1520. OS << " break;\n";
  1521. }
  1522. OS << " }\n";
  1523. }
  1524. // And finally, output the code, and close the outer pair of braces. (The
  1525. // code will always end with a 'return' statement, so we need not insert a
  1526. // 'break' here.)
  1527. OS << MG.Code << "}\n";
  1528. }
  1529. }
  1530. void EmitterBase::EmitBuiltinAliases(raw_ostream &OS) {
  1531. // Build a sorted table of:
  1532. // - intrinsic id number
  1533. // - full name
  1534. // - polymorphic name or -1
  1535. StringToOffsetTable StringTable;
  1536. OS << "static const IntrinToName MapData[] = {\n";
  1537. for (const auto &kv : ACLEIntrinsics) {
  1538. const ACLEIntrinsic &Int = *kv.second;
  1539. if (Int.headerOnly())
  1540. continue;
  1541. int32_t ShortNameOffset =
  1542. Int.polymorphic() ? StringTable.GetOrAddStringOffset(Int.shortName())
  1543. : -1;
  1544. OS << " { ARM::BI__builtin_arm_" << Int.builtinExtension() << "_"
  1545. << Int.fullName() << ", "
  1546. << StringTable.GetOrAddStringOffset(Int.fullName()) << ", "
  1547. << ShortNameOffset << "},\n";
  1548. }
  1549. OS << "};\n\n";
  1550. OS << "ArrayRef<IntrinToName> Map(MapData);\n\n";
  1551. OS << "static const char IntrinNames[] = {\n";
  1552. StringTable.EmitString(OS);
  1553. OS << "};\n\n";
  1554. }
  1555. void EmitterBase::GroupSemaChecks(
  1556. std::map<std::string, std::set<std::string>> &Checks) {
  1557. for (const auto &kv : ACLEIntrinsics) {
  1558. const ACLEIntrinsic &Int = *kv.second;
  1559. if (Int.headerOnly())
  1560. continue;
  1561. std::string Check = Int.genSema();
  1562. if (!Check.empty())
  1563. Checks[Check].insert(Int.fullName());
  1564. }
  1565. }
  1566. // -----------------------------------------------------------------------------
  1567. // The class used for generating arm_mve.h and related Clang bits
  1568. //
  1569. class MveEmitter : public EmitterBase {
  1570. public:
  1571. MveEmitter(RecordKeeper &Records) : EmitterBase(Records){};
  1572. void EmitHeader(raw_ostream &OS) override;
  1573. void EmitBuiltinDef(raw_ostream &OS) override;
  1574. void EmitBuiltinSema(raw_ostream &OS) override;
  1575. };
  1576. void MveEmitter::EmitHeader(raw_ostream &OS) {
  1577. // Accumulate pieces of the header file that will be enabled under various
  1578. // different combinations of #ifdef. The index into parts[] is made up of
  1579. // the following bit flags.
  1580. constexpr unsigned Float = 1;
  1581. constexpr unsigned UseUserNamespace = 2;
  1582. constexpr unsigned NumParts = 4;
  1583. raw_self_contained_string_ostream parts[NumParts];
  1584. // Write typedefs for all the required vector types, and a few scalar
  1585. // types that don't already have the name we want them to have.
  1586. parts[0] << "typedef uint16_t mve_pred16_t;\n";
  1587. parts[Float] << "typedef __fp16 float16_t;\n"
  1588. "typedef float float32_t;\n";
  1589. for (const auto &kv : ScalarTypes) {
  1590. const ScalarType *ST = kv.second.get();
  1591. if (ST->hasNonstandardName())
  1592. continue;
  1593. raw_ostream &OS = parts[ST->requiresFloat() ? Float : 0];
  1594. const VectorType *VT = getVectorType(ST);
  1595. OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
  1596. << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
  1597. << VT->cName() << ";\n";
  1598. // Every vector type also comes with a pair of multi-vector types for
  1599. // the VLD2 and VLD4 instructions.
  1600. for (unsigned n = 2; n <= 4; n += 2) {
  1601. const MultiVectorType *MT = getMultiVectorType(n, VT);
  1602. OS << "typedef struct { " << VT->cName() << " val[" << n << "]; } "
  1603. << MT->cName() << ";\n";
  1604. }
  1605. }
  1606. parts[0] << "\n";
  1607. parts[Float] << "\n";
  1608. // Write declarations for all the intrinsics.
  1609. for (const auto &kv : ACLEIntrinsics) {
  1610. const ACLEIntrinsic &Int = *kv.second;
  1611. // We generate each intrinsic twice, under its full unambiguous
  1612. // name and its shorter polymorphic name (if the latter exists).
  1613. for (bool Polymorphic : {false, true}) {
  1614. if (Polymorphic && !Int.polymorphic())
  1615. continue;
  1616. if (!Polymorphic && Int.polymorphicOnly())
  1617. continue;
  1618. // We also generate each intrinsic under a name like __arm_vfooq
  1619. // (which is in C language implementation namespace, so it's
  1620. // safe to define in any conforming user program) and a shorter
  1621. // one like vfooq (which is in user namespace, so a user might
  1622. // reasonably have used it for something already). If so, they
  1623. // can #define __ARM_MVE_PRESERVE_USER_NAMESPACE before
  1624. // including the header, which will suppress the shorter names
  1625. // and leave only the implementation-namespace ones. Then they
  1626. // have to write __arm_vfooq everywhere, of course.
  1627. for (bool UserNamespace : {false, true}) {
  1628. raw_ostream &OS = parts[(Int.requiresFloat() ? Float : 0) |
  1629. (UserNamespace ? UseUserNamespace : 0)];
  1630. // Make the name of the function in this declaration.
  1631. std::string FunctionName =
  1632. Polymorphic ? Int.shortName() : Int.fullName();
  1633. if (!UserNamespace)
  1634. FunctionName = "__arm_" + FunctionName;
  1635. // Make strings for the types involved in the function's
  1636. // prototype.
  1637. std::string RetTypeName = Int.returnType()->cName();
  1638. if (!StringRef(RetTypeName).endswith("*"))
  1639. RetTypeName += " ";
  1640. std::vector<std::string> ArgTypeNames;
  1641. for (const Type *ArgTypePtr : Int.argTypes())
  1642. ArgTypeNames.push_back(ArgTypePtr->cName());
  1643. std::string ArgTypesString =
  1644. join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
  1645. // Emit the actual declaration. All these functions are
  1646. // declared 'static inline' without a body, which is fine
  1647. // provided clang recognizes them as builtins, and has the
  1648. // effect that this type signature is used in place of the one
  1649. // that Builtins.def didn't provide. That's how we can get
  1650. // structure types that weren't defined until this header was
  1651. // included to be part of the type signature of a builtin that
  1652. // was known to clang already.
  1653. //
  1654. // The declarations use __attribute__(__clang_arm_builtin_alias),
  1655. // so that each function declared will be recognized as the
  1656. // appropriate MVE builtin in spite of its user-facing name.
  1657. //
  1658. // (That's better than making them all wrapper functions,
  1659. // partly because it avoids any compiler error message citing
  1660. // the wrapper function definition instead of the user's code,
  1661. // and mostly because some MVE intrinsics have arguments
  1662. // required to be compile-time constants, and that property
  1663. // can't be propagated through a wrapper function. It can be
  1664. // propagated through a macro, but macros can't be overloaded
  1665. // on argument types very easily - you have to use _Generic,
  1666. // which makes error messages very confusing when the user
  1667. // gets it wrong.)
  1668. //
  1669. // Finally, the polymorphic versions of the intrinsics are
  1670. // also defined with __attribute__(overloadable), so that when
  1671. // the same name is defined with several type signatures, the
  1672. // right thing happens. Each one of the overloaded
  1673. // declarations is given a different builtin id, which
  1674. // has exactly the effect we want: first clang resolves the
  1675. // overload to the right function, then it knows which builtin
  1676. // it's referring to, and then the Sema checking for that
  1677. // builtin can check further things like the constant
  1678. // arguments.
  1679. //
  1680. // One more subtlety is the newline just before the return
  1681. // type name. That's a cosmetic tweak to make the error
  1682. // messages legible if the user gets the types wrong in a call
  1683. // to a polymorphic function: this way, clang will print just
  1684. // the _final_ line of each declaration in the header, to show
  1685. // the type signatures that would have been legal. So all the
  1686. // confusing machinery with __attribute__ is left out of the
  1687. // error message, and the user sees something that's more or
  1688. // less self-documenting: "here's a list of actually readable
  1689. // type signatures for vfooq(), and here's why each one didn't
  1690. // match your call".
  1691. OS << "static __inline__ __attribute__(("
  1692. << (Polymorphic ? "__overloadable__, " : "")
  1693. << "__clang_arm_builtin_alias(__builtin_arm_mve_" << Int.fullName()
  1694. << ")))\n"
  1695. << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
  1696. }
  1697. }
  1698. }
  1699. for (auto &part : parts)
  1700. part << "\n";
  1701. // Now we've finished accumulating bits and pieces into the parts[] array.
  1702. // Put it all together to write the final output file.
  1703. OS << "/*===---- arm_mve.h - ARM MVE intrinsics "
  1704. "-----------------------------------===\n"
  1705. << LLVMLicenseHeader
  1706. << "#ifndef __ARM_MVE_H\n"
  1707. "#define __ARM_MVE_H\n"
  1708. "\n"
  1709. "#if !__ARM_FEATURE_MVE\n"
  1710. "#error \"MVE support not enabled\"\n"
  1711. "#endif\n"
  1712. "\n"
  1713. "#include <stdint.h>\n"
  1714. "\n"
  1715. "#ifdef __cplusplus\n"
  1716. "extern \"C\" {\n"
  1717. "#endif\n"
  1718. "\n";
  1719. for (size_t i = 0; i < NumParts; ++i) {
  1720. std::vector<std::string> conditions;
  1721. if (i & Float)
  1722. conditions.push_back("(__ARM_FEATURE_MVE & 2)");
  1723. if (i & UseUserNamespace)
  1724. conditions.push_back("(!defined __ARM_MVE_PRESERVE_USER_NAMESPACE)");
  1725. std::string condition =
  1726. join(std::begin(conditions), std::end(conditions), " && ");
  1727. if (!condition.empty())
  1728. OS << "#if " << condition << "\n\n";
  1729. OS << parts[i].str();
  1730. if (!condition.empty())
  1731. OS << "#endif /* " << condition << " */\n\n";
  1732. }
  1733. OS << "#ifdef __cplusplus\n"
  1734. "} /* extern \"C\" */\n"
  1735. "#endif\n"
  1736. "\n"
  1737. "#endif /* __ARM_MVE_H */\n";
  1738. }
  1739. void MveEmitter::EmitBuiltinDef(raw_ostream &OS) {
  1740. for (const auto &kv : ACLEIntrinsics) {
  1741. const ACLEIntrinsic &Int = *kv.second;
  1742. OS << "BUILTIN(__builtin_arm_mve_" << Int.fullName()
  1743. << ", \"\", \"n\")\n";
  1744. }
  1745. std::set<std::string> ShortNamesSeen;
  1746. for (const auto &kv : ACLEIntrinsics) {
  1747. const ACLEIntrinsic &Int = *kv.second;
  1748. if (Int.polymorphic()) {
  1749. StringRef Name = Int.shortName();
  1750. if (ShortNamesSeen.find(std::string(Name)) == ShortNamesSeen.end()) {
  1751. OS << "BUILTIN(__builtin_arm_mve_" << Name << ", \"vi.\", \"nt";
  1752. if (Int.nonEvaluating())
  1753. OS << "u"; // indicate that this builtin doesn't evaluate its args
  1754. OS << "\")\n";
  1755. ShortNamesSeen.insert(std::string(Name));
  1756. }
  1757. }
  1758. }
  1759. }
  1760. void MveEmitter::EmitBuiltinSema(raw_ostream &OS) {
  1761. std::map<std::string, std::set<std::string>> Checks;
  1762. GroupSemaChecks(Checks);
  1763. for (const auto &kv : Checks) {
  1764. for (StringRef Name : kv.second)
  1765. OS << "case ARM::BI__builtin_arm_mve_" << Name << ":\n";
  1766. OS << " return " << kv.first;
  1767. }
  1768. }
  1769. // -----------------------------------------------------------------------------
  1770. // Class that describes an ACLE intrinsic implemented as a macro.
  1771. //
  1772. // This class is used when the intrinsic is polymorphic in 2 or 3 types, but we
  1773. // want to avoid a combinatorial explosion by reinterpreting the arguments to
  1774. // fixed types.
  1775. class FunctionMacro {
  1776. std::vector<StringRef> Params;
  1777. StringRef Definition;
  1778. public:
  1779. FunctionMacro(const Record &R);
  1780. const std::vector<StringRef> &getParams() const { return Params; }
  1781. StringRef getDefinition() const { return Definition; }
  1782. };
  1783. FunctionMacro::FunctionMacro(const Record &R) {
  1784. Params = R.getValueAsListOfStrings("params");
  1785. Definition = R.getValueAsString("definition");
  1786. }
  1787. // -----------------------------------------------------------------------------
  1788. // The class used for generating arm_cde.h and related Clang bits
  1789. //
  1790. class CdeEmitter : public EmitterBase {
  1791. std::map<StringRef, FunctionMacro> FunctionMacros;
  1792. public:
  1793. CdeEmitter(RecordKeeper &Records);
  1794. void EmitHeader(raw_ostream &OS) override;
  1795. void EmitBuiltinDef(raw_ostream &OS) override;
  1796. void EmitBuiltinSema(raw_ostream &OS) override;
  1797. };
  1798. CdeEmitter::CdeEmitter(RecordKeeper &Records) : EmitterBase(Records) {
  1799. for (Record *R : Records.getAllDerivedDefinitions("FunctionMacro"))
  1800. FunctionMacros.emplace(R->getName(), FunctionMacro(*R));
  1801. }
  1802. void CdeEmitter::EmitHeader(raw_ostream &OS) {
  1803. // Accumulate pieces of the header file that will be enabled under various
  1804. // different combinations of #ifdef. The index into parts[] is one of the
  1805. // following:
  1806. constexpr unsigned None = 0;
  1807. constexpr unsigned MVE = 1;
  1808. constexpr unsigned MVEFloat = 2;
  1809. constexpr unsigned NumParts = 3;
  1810. raw_self_contained_string_ostream parts[NumParts];
  1811. // Write typedefs for all the required vector types, and a few scalar
  1812. // types that don't already have the name we want them to have.
  1813. parts[MVE] << "typedef uint16_t mve_pred16_t;\n";
  1814. parts[MVEFloat] << "typedef __fp16 float16_t;\n"
  1815. "typedef float float32_t;\n";
  1816. for (const auto &kv : ScalarTypes) {
  1817. const ScalarType *ST = kv.second.get();
  1818. if (ST->hasNonstandardName())
  1819. continue;
  1820. // We don't have float64x2_t
  1821. if (ST->kind() == ScalarTypeKind::Float && ST->sizeInBits() == 64)
  1822. continue;
  1823. raw_ostream &OS = parts[ST->requiresFloat() ? MVEFloat : MVE];
  1824. const VectorType *VT = getVectorType(ST);
  1825. OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
  1826. << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
  1827. << VT->cName() << ";\n";
  1828. }
  1829. parts[MVE] << "\n";
  1830. parts[MVEFloat] << "\n";
  1831. // Write declarations for all the intrinsics.
  1832. for (const auto &kv : ACLEIntrinsics) {
  1833. const ACLEIntrinsic &Int = *kv.second;
  1834. // We generate each intrinsic twice, under its full unambiguous
  1835. // name and its shorter polymorphic name (if the latter exists).
  1836. for (bool Polymorphic : {false, true}) {
  1837. if (Polymorphic && !Int.polymorphic())
  1838. continue;
  1839. if (!Polymorphic && Int.polymorphicOnly())
  1840. continue;
  1841. raw_ostream &OS =
  1842. parts[Int.requiresFloat() ? MVEFloat
  1843. : Int.requiresMVE() ? MVE : None];
  1844. // Make the name of the function in this declaration.
  1845. std::string FunctionName =
  1846. "__arm_" + (Polymorphic ? Int.shortName() : Int.fullName());
  1847. // Make strings for the types involved in the function's
  1848. // prototype.
  1849. std::string RetTypeName = Int.returnType()->cName();
  1850. if (!StringRef(RetTypeName).endswith("*"))
  1851. RetTypeName += " ";
  1852. std::vector<std::string> ArgTypeNames;
  1853. for (const Type *ArgTypePtr : Int.argTypes())
  1854. ArgTypeNames.push_back(ArgTypePtr->cName());
  1855. std::string ArgTypesString =
  1856. join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
  1857. // Emit the actual declaration. See MveEmitter::EmitHeader for detailed
  1858. // comments
  1859. OS << "static __inline__ __attribute__(("
  1860. << (Polymorphic ? "__overloadable__, " : "")
  1861. << "__clang_arm_builtin_alias(__builtin_arm_" << Int.builtinExtension()
  1862. << "_" << Int.fullName() << ")))\n"
  1863. << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
  1864. }
  1865. }
  1866. for (const auto &kv : FunctionMacros) {
  1867. StringRef Name = kv.first;
  1868. const FunctionMacro &FM = kv.second;
  1869. raw_ostream &OS = parts[MVE];
  1870. OS << "#define "
  1871. << "__arm_" << Name << "(" << join(FM.getParams(), ", ") << ") "
  1872. << FM.getDefinition() << "\n";
  1873. }
  1874. for (auto &part : parts)
  1875. part << "\n";
  1876. // Now we've finished accumulating bits and pieces into the parts[] array.
  1877. // Put it all together to write the final output file.
  1878. OS << "/*===---- arm_cde.h - ARM CDE intrinsics "
  1879. "-----------------------------------===\n"
  1880. << LLVMLicenseHeader
  1881. << "#ifndef __ARM_CDE_H\n"
  1882. "#define __ARM_CDE_H\n"
  1883. "\n"
  1884. "#if !__ARM_FEATURE_CDE\n"
  1885. "#error \"CDE support not enabled\"\n"
  1886. "#endif\n"
  1887. "\n"
  1888. "#include <stdint.h>\n"
  1889. "\n"
  1890. "#ifdef __cplusplus\n"
  1891. "extern \"C\" {\n"
  1892. "#endif\n"
  1893. "\n";
  1894. for (size_t i = 0; i < NumParts; ++i) {
  1895. std::string condition;
  1896. if (i == MVEFloat)
  1897. condition = "__ARM_FEATURE_MVE & 2";
  1898. else if (i == MVE)
  1899. condition = "__ARM_FEATURE_MVE";
  1900. if (!condition.empty())
  1901. OS << "#if " << condition << "\n\n";
  1902. OS << parts[i].str();
  1903. if (!condition.empty())
  1904. OS << "#endif /* " << condition << " */\n\n";
  1905. }
  1906. OS << "#ifdef __cplusplus\n"
  1907. "} /* extern \"C\" */\n"
  1908. "#endif\n"
  1909. "\n"
  1910. "#endif /* __ARM_CDE_H */\n";
  1911. }
  1912. void CdeEmitter::EmitBuiltinDef(raw_ostream &OS) {
  1913. for (const auto &kv : ACLEIntrinsics) {
  1914. if (kv.second->headerOnly())
  1915. continue;
  1916. const ACLEIntrinsic &Int = *kv.second;
  1917. OS << "BUILTIN(__builtin_arm_cde_" << Int.fullName()
  1918. << ", \"\", \"ncU\")\n";
  1919. }
  1920. }
  1921. void CdeEmitter::EmitBuiltinSema(raw_ostream &OS) {
  1922. std::map<std::string, std::set<std::string>> Checks;
  1923. GroupSemaChecks(Checks);
  1924. for (const auto &kv : Checks) {
  1925. for (StringRef Name : kv.second)
  1926. OS << "case ARM::BI__builtin_arm_cde_" << Name << ":\n";
  1927. OS << " Err = " << kv.first << " break;\n";
  1928. }
  1929. }
  1930. } // namespace
  1931. namespace clang {
  1932. // MVE
  1933. void EmitMveHeader(RecordKeeper &Records, raw_ostream &OS) {
  1934. MveEmitter(Records).EmitHeader(OS);
  1935. }
  1936. void EmitMveBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
  1937. MveEmitter(Records).EmitBuiltinDef(OS);
  1938. }
  1939. void EmitMveBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
  1940. MveEmitter(Records).EmitBuiltinSema(OS);
  1941. }
  1942. void EmitMveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
  1943. MveEmitter(Records).EmitBuiltinCG(OS);
  1944. }
  1945. void EmitMveBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
  1946. MveEmitter(Records).EmitBuiltinAliases(OS);
  1947. }
  1948. // CDE
  1949. void EmitCdeHeader(RecordKeeper &Records, raw_ostream &OS) {
  1950. CdeEmitter(Records).EmitHeader(OS);
  1951. }
  1952. void EmitCdeBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
  1953. CdeEmitter(Records).EmitBuiltinDef(OS);
  1954. }
  1955. void EmitCdeBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
  1956. CdeEmitter(Records).EmitBuiltinSema(OS);
  1957. }
  1958. void EmitCdeBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
  1959. CdeEmitter(Records).EmitBuiltinCG(OS);
  1960. }
  1961. void EmitCdeBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
  1962. CdeEmitter(Records).EmitBuiltinAliases(OS);
  1963. }
  1964. } // end namespace clang