blob: 5b734fe44e47a130c74095bab2607fdcb41e8664 [file] [log] [blame]
Sander de Smalen5087ace2020-03-15 14:29:45 +00001//===- SveEmitter.cpp - Generate arm_sve.h for use with clang -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This tablegen backend is responsible for emitting arm_sve.h, which includes
10// a declaration and definition of each function specified by the ARM C/C++
11// Language Extensions (ACLE).
12//
13// For details, visit:
14// https://developer.arm.com/architectures/system-architectures/software-standards/acle
15//
16// Each SVE instruction is implemented in terms of 1 or more functions which
17// are suffixed with the element type of the input vectors. Functions may be
18// implemented in terms of generic vector operations such as +, *, -, etc. or
19// by calling a __builtin_-prefixed function which will be handled by clang's
20// CodeGen library.
21//
22// See also the documentation in include/clang/Basic/arm_sve.td.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/ADT/STLExtras.h"
Sander de Smalenc5b81462020-03-18 11:07:20 +000027#include "llvm/ADT/StringMap.h"
Sander de Smalen5087ace2020-03-15 14:29:45 +000028#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/StringExtras.h"
30#include "llvm/TableGen/Record.h"
31#include "llvm/TableGen/Error.h"
32#include <string>
33#include <sstream>
34#include <set>
35#include <cctype>
Eric Fiselieraf2968e2020-04-16 18:35:31 -040036#include <tuple>
Sander de Smalen5087ace2020-03-15 14:29:45 +000037
38using namespace llvm;
39
Sander de Smalenc5b81462020-03-18 11:07:20 +000040enum ClassKind {
41 ClassNone,
42 ClassS, // signed/unsigned, e.g., "_s8", "_u8" suffix
43 ClassG, // Overloaded name without type suffix
44};
45
46using TypeSpec = std::string;
Sander de Smalen5087ace2020-03-15 14:29:45 +000047
48namespace {
49
Sander de Smalenc8a5b302020-04-14 15:56:36 +010050class ImmCheck {
51 unsigned Arg;
52 unsigned Kind;
53 unsigned ElementSizeInBits;
54
55public:
56 ImmCheck(unsigned Arg, unsigned Kind, unsigned ElementSizeInBits = 0)
57 : Arg(Arg), Kind(Kind), ElementSizeInBits(ElementSizeInBits) {}
58 ImmCheck(const ImmCheck &Other) = default;
59 ~ImmCheck() = default;
60
61 unsigned getArg() const { return Arg; }
62 unsigned getKind() const { return Kind; }
63 unsigned getElementSizeInBits() const { return ElementSizeInBits; }
64};
65
Sander de Smalenc5b81462020-03-18 11:07:20 +000066class SVEType {
67 TypeSpec TS;
68 bool Float, Signed, Immediate, Void, Constant, Pointer;
69 bool DefaultType, IsScalable, Predicate, PredicatePattern, PrefetchOp;
70 unsigned Bitwidth, ElementBitwidth, NumVectors;
71
Sander de Smalen8b409ea2020-03-16 10:14:05 +000072public:
Sander de Smalenc5b81462020-03-18 11:07:20 +000073 SVEType() : SVEType(TypeSpec(), 'v') {}
74
75 SVEType(TypeSpec TS, char CharMod)
76 : TS(TS), Float(false), Signed(true), Immediate(false), Void(false),
77 Constant(false), Pointer(false), DefaultType(false), IsScalable(true),
78 Predicate(false), PredicatePattern(false), PrefetchOp(false),
79 Bitwidth(128), ElementBitwidth(~0U), NumVectors(1) {
80 if (!TS.empty())
81 applyTypespec();
82 applyModifier(CharMod);
83 }
84
Sander de Smalenc5b81462020-03-18 11:07:20 +000085 bool isPointer() const { return Pointer; }
86 bool isVoidPointer() const { return Pointer && Void; }
87 bool isSigned() const { return Signed; }
88 bool isImmediate() const { return Immediate; }
89 bool isScalar() const { return NumVectors == 0; }
90 bool isVector() const { return NumVectors > 0; }
91 bool isScalableVector() const { return isVector() && IsScalable; }
92 bool isChar() const { return ElementBitwidth == 8; }
93 bool isVoid() const { return Void & !Pointer; }
94 bool isDefault() const { return DefaultType; }
95 bool isFloat() const { return Float; }
96 bool isInteger() const { return !Float && !Predicate; }
Sander de Smalenaed6bd62020-05-05 09:16:57 +010097 bool isScalarPredicate() const {
98 return !Float && Predicate && NumVectors == 0;
99 }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000100 bool isPredicateVector() const { return Predicate; }
101 bool isPredicatePattern() const { return PredicatePattern; }
102 bool isPrefetchOp() const { return PrefetchOp; }
103 bool isConstant() const { return Constant; }
104 unsigned getElementSizeInBits() const { return ElementBitwidth; }
105 unsigned getNumVectors() const { return NumVectors; }
106
107 unsigned getNumElements() const {
108 assert(ElementBitwidth != ~0U);
109 return Bitwidth / ElementBitwidth;
110 }
111 unsigned getSizeInBits() const {
112 return Bitwidth;
113 }
114
115 /// Return the string representation of a type, which is an encoded
116 /// string for passing to the BUILTIN() macro in Builtins.def.
117 std::string builtin_str() const;
118
Sander de Smalen981f0802020-03-18 15:05:08 +0000119 /// Return the C/C++ string representation of a type for use in the
120 /// arm_sve.h header file.
121 std::string str() const;
122
Sander de Smalenc5b81462020-03-18 11:07:20 +0000123private:
124 /// Creates the type based on the typespec string in TS.
125 void applyTypespec();
126
127 /// Applies a prototype modifier to the type.
128 void applyModifier(char Mod);
129};
130
131
132class SVEEmitter;
133
134/// The main grunt class. This represents an instantiation of an intrinsic with
135/// a particular typespec and prototype.
136class Intrinsic {
137 /// The unmangled name.
138 std::string Name;
139
140 /// The name of the corresponding LLVM IR intrinsic.
141 std::string LLVMName;
142
143 /// Intrinsic prototype.
144 std::string Proto;
145
146 /// The base type spec for this intrinsic.
147 TypeSpec BaseTypeSpec;
148
149 /// The base class kind. Most intrinsics use ClassS, which has full type
150 /// info for integers (_s32/_u32), or ClassG which is used for overloaded
151 /// intrinsics.
152 ClassKind Class;
153
154 /// The architectural #ifdef guard.
155 std::string Guard;
156
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100157 // The merge suffix such as _m, _x or _z.
158 std::string MergeSuffix;
159
Sander de Smalenc5b81462020-03-18 11:07:20 +0000160 /// The types of return value [0] and parameters [1..].
161 std::vector<SVEType> Types;
162
163 /// The "base type", which is VarType('d', BaseTypeSpec).
164 SVEType BaseType;
165
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100166 uint64_t Flags;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000167
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100168 SmallVector<ImmCheck, 2> ImmChecks;
169
Sander de Smalenc5b81462020-03-18 11:07:20 +0000170public:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100171 Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
172 StringRef MergeSuffix, uint64_t MemoryElementTy, StringRef LLVMName,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100173 uint64_t Flags, ArrayRef<ImmCheck> ImmChecks, TypeSpec BT,
174 ClassKind Class, SVEEmitter &Emitter, StringRef Guard);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000175
176 ~Intrinsic()=default;
177
178 std::string getName() const { return Name; }
179 std::string getLLVMName() const { return LLVMName; }
180 std::string getProto() const { return Proto; }
181 TypeSpec getBaseTypeSpec() const { return BaseTypeSpec; }
182 SVEType getBaseType() const { return BaseType; }
183
184 StringRef getGuard() const { return Guard; }
185 ClassKind getClassKind() const { return Class; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000186
187 SVEType getReturnType() const { return Types[0]; }
188 ArrayRef<SVEType> getTypes() const { return Types; }
189 SVEType getParamType(unsigned I) const { return Types[I + 1]; }
190 unsigned getNumParams() const { return Proto.size() - 1; }
191
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100192 uint64_t getFlags() const { return Flags; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000193 bool isFlagSet(uint64_t Flag) const { return Flags & Flag;}
194
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100195 ArrayRef<ImmCheck> getImmChecks() const { return ImmChecks; }
196
Sander de Smalenc5b81462020-03-18 11:07:20 +0000197 /// Return the type string for a BUILTIN() macro in Builtins.def.
198 std::string getBuiltinTypeStr();
199
200 /// Return the name, mangled with type information. The name is mangled for
201 /// ClassS, so will add type suffixes such as _u32/_s32.
202 std::string getMangledName() const { return mangleName(ClassS); }
203
204 /// Returns true if the intrinsic is overloaded, in that it should also generate
205 /// a short form without the type-specifiers, e.g. 'svld1(..)' instead of
206 /// 'svld1_u32(..)'.
207 static bool isOverloadedIntrinsic(StringRef Name) {
208 auto BrOpen = Name.find("[");
209 auto BrClose = Name.find(']');
210 return BrOpen != std::string::npos && BrClose != std::string::npos;
211 }
212
Sander de Smalen41d52662020-04-22 13:58:35 +0100213 /// Return true if the intrinsic takes a splat operand.
214 bool hasSplat() const {
215 // These prototype modifiers are described in arm_sve.td.
216 return Proto.find_first_of("ajfrKLR") != std::string::npos;
217 }
218
219 /// Return the parameter index of the splat operand.
220 unsigned getSplatIdx() const {
221 // These prototype modifiers are described in arm_sve.td.
222 auto Idx = Proto.find_first_of("ajfrKLR");
223 assert(Idx != std::string::npos && Idx > 0 &&
224 "Prototype has no splat operand");
225 return Idx - 1;
226 }
227
Sander de Smalenc5b81462020-03-18 11:07:20 +0000228 /// Emits the intrinsic declaration to the ostream.
229 void emitIntrinsic(raw_ostream &OS) const;
230
231private:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100232 std::string getMergeSuffix() const { return MergeSuffix; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000233 std::string mangleName(ClassKind LocalCK) const;
234 std::string replaceTemplatedArgs(std::string Name, TypeSpec TS,
235 std::string Proto) const;
236};
237
238class SVEEmitter {
239private:
Sander de Smalen5ba32902020-05-05 13:04:14 +0100240 // The reinterpret builtins are generated separately because they
241 // need the cross product of all types (121 functions in total),
242 // which is inconvenient to specify in the arm_sve.td file or
243 // generate in CGBuiltin.cpp.
244 struct ReinterpretTypeInfo {
245 const char *Suffix;
246 const char *Type;
247 const char *BuiltinType;
248 };
249 SmallVector<ReinterpretTypeInfo, 11> Reinterprets = {
250 {"s8", "svint8_t", "q16Sc"}, {"s16", "svint16_t", "q8Ss"},
251 {"s32", "svint32_t", "q4Si"}, {"s64", "svint64_t", "q2SWi"},
252 {"u8", "svuint8_t", "q16Uc"}, {"u16", "svuint16_t", "q8Us"},
253 {"u32", "svuint32_t", "q4Ui"}, {"u64", "svuint64_t", "q2UWi"},
254 {"f16", "svfloat16_t", "q8h"}, {"f32", "svfloat32_t", "q4f"},
255 {"f64", "svfloat64_t", "q2d"}};
256
Sander de Smalenc5b81462020-03-18 11:07:20 +0000257 RecordKeeper &Records;
258 llvm::StringMap<uint64_t> EltTypes;
259 llvm::StringMap<uint64_t> MemEltTypes;
260 llvm::StringMap<uint64_t> FlagTypes;
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100261 llvm::StringMap<uint64_t> MergeTypes;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100262 llvm::StringMap<uint64_t> ImmCheckTypes;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000263
Sander de Smalenc5b81462020-03-18 11:07:20 +0000264public:
265 SVEEmitter(RecordKeeper &R) : Records(R) {
266 for (auto *RV : Records.getAllDerivedDefinitions("EltType"))
267 EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
268 for (auto *RV : Records.getAllDerivedDefinitions("MemEltType"))
269 MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
270 for (auto *RV : Records.getAllDerivedDefinitions("FlagType"))
271 FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100272 for (auto *RV : Records.getAllDerivedDefinitions("MergeType"))
273 MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100274 for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType"))
275 ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
276 }
277
278 /// Returns the enum value for the immcheck type
279 unsigned getEnumValueForImmCheck(StringRef C) const {
280 auto It = ImmCheckTypes.find(C);
281 if (It != ImmCheckTypes.end())
282 return It->getValue();
283 llvm_unreachable("Unsupported imm check");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000284 }
285
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100286 /// Returns the enum value for the flag type
287 uint64_t getEnumValueForFlag(StringRef C) const {
288 auto Res = FlagTypes.find(C);
289 if (Res != FlagTypes.end())
290 return Res->getValue();
291 llvm_unreachable("Unsupported flag");
292 }
293
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100294 // Returns the SVETypeFlags for a given value and mask.
295 uint64_t encodeFlag(uint64_t V, StringRef MaskName) const {
296 auto It = FlagTypes.find(MaskName);
297 if (It != FlagTypes.end()) {
298 uint64_t Mask = It->getValue();
299 unsigned Shift = llvm::countTrailingZeros(Mask);
300 return (V << Shift) & Mask;
301 }
302 llvm_unreachable("Unsupported flag");
303 }
304
305 // Returns the SVETypeFlags for the given element type.
306 uint64_t encodeEltType(StringRef EltName) {
307 auto It = EltTypes.find(EltName);
308 if (It != EltTypes.end())
309 return encodeFlag(It->getValue(), "EltTypeMask");
310 llvm_unreachable("Unsupported EltType");
311 }
312
313 // Returns the SVETypeFlags for the given memory element type.
314 uint64_t encodeMemoryElementType(uint64_t MT) {
315 return encodeFlag(MT, "MemEltTypeMask");
316 }
317
318 // Returns the SVETypeFlags for the given merge type.
319 uint64_t encodeMergeType(uint64_t MT) {
320 return encodeFlag(MT, "MergeTypeMask");
321 }
322
Sander de Smalen41d52662020-04-22 13:58:35 +0100323 // Returns the SVETypeFlags for the given splat operand.
324 unsigned encodeSplatOperand(unsigned SplatIdx) {
325 assert(SplatIdx < 7 && "SplatIdx out of encodable range");
326 return encodeFlag(SplatIdx + 1, "SplatOperandMask");
327 }
328
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100329 // Returns the SVETypeFlags value for the given SVEType.
330 uint64_t encodeTypeFlags(const SVEType &T);
331
Sander de Smalenc5b81462020-03-18 11:07:20 +0000332 /// Emit arm_sve.h.
333 void createHeader(raw_ostream &o);
334
335 /// Emit all the __builtin prototypes and code needed by Sema.
336 void createBuiltins(raw_ostream &o);
337
338 /// Emit all the information needed to map builtin -> LLVM IR intrinsic.
339 void createCodeGenMap(raw_ostream &o);
340
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100341 /// Emit all the range checks for the immediates.
342 void createRangeChecks(raw_ostream &o);
343
Sander de Smalenc5b81462020-03-18 11:07:20 +0000344 /// Create the SVETypeFlags used in CGBuiltins
345 void createTypeFlags(raw_ostream &o);
346
347 /// Create intrinsic and add it to \p Out
348 void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out);
Sander de Smalen5087ace2020-03-15 14:29:45 +0000349};
350
351} // end anonymous namespace
352
353
354//===----------------------------------------------------------------------===//
Sander de Smalenc5b81462020-03-18 11:07:20 +0000355// Type implementation
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000356//===----------------------------------------------------------------------===//
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000357
Sander de Smalenc5b81462020-03-18 11:07:20 +0000358std::string SVEType::builtin_str() const {
359 std::string S;
360 if (isVoid())
361 return "v";
362
363 if (isVoidPointer())
364 S += "v";
365 else if (!Float)
366 switch (ElementBitwidth) {
367 case 1: S += "b"; break;
368 case 8: S += "c"; break;
369 case 16: S += "s"; break;
370 case 32: S += "i"; break;
371 case 64: S += "Wi"; break;
372 case 128: S += "LLLi"; break;
373 default: llvm_unreachable("Unhandled case!");
374 }
375 else
376 switch (ElementBitwidth) {
377 case 16: S += "h"; break;
378 case 32: S += "f"; break;
379 case 64: S += "d"; break;
380 default: llvm_unreachable("Unhandled case!");
381 }
382
383 if (!isFloat()) {
384 if ((isChar() || isPointer()) && !isVoidPointer()) {
385 // Make chars and typed pointers explicitly signed.
386 if (Signed)
387 S = "S" + S;
388 else if (!Signed)
389 S = "U" + S;
390 } else if (!isVoidPointer() && !Signed) {
391 S = "U" + S;
392 }
393 }
394
395 // Constant indices are "int", but have the "constant expression" modifier.
396 if (isImmediate()) {
397 assert(!isFloat() && "fp immediates are not supported");
398 S = "I" + S;
399 }
400
401 if (isScalar()) {
402 if (Constant) S += "C";
403 if (Pointer) S += "*";
404 return S;
405 }
406
407 assert(isScalableVector() && "Unsupported type");
408 return "q" + utostr(getNumElements() * NumVectors) + S;
409}
410
Sander de Smalen981f0802020-03-18 15:05:08 +0000411std::string SVEType::str() const {
412 if (isPredicatePattern())
413 return "sv_pattern";
414
415 if (isPrefetchOp())
416 return "sv_prfop";
417
418 std::string S;
419 if (Void)
420 S += "void";
421 else {
422 if (isScalableVector())
423 S += "sv";
424 if (!Signed && !Float)
425 S += "u";
426
427 if (Float)
428 S += "float";
Sander de Smalenaed6bd62020-05-05 09:16:57 +0100429 else if (isScalarPredicate() || isPredicateVector())
Sander de Smalen981f0802020-03-18 15:05:08 +0000430 S += "bool";
431 else
432 S += "int";
433
Sander de Smalenaed6bd62020-05-05 09:16:57 +0100434 if (!isScalarPredicate() && !isPredicateVector())
Sander de Smalen981f0802020-03-18 15:05:08 +0000435 S += utostr(ElementBitwidth);
436 if (!isScalableVector() && isVector())
437 S += "x" + utostr(getNumElements());
438 if (NumVectors > 1)
439 S += "x" + utostr(NumVectors);
440 S += "_t";
441 }
442
443 if (Constant)
444 S += " const";
445 if (Pointer)
446 S += " *";
447
448 return S;
449}
Sander de Smalenc5b81462020-03-18 11:07:20 +0000450void SVEType::applyTypespec() {
451 for (char I : TS) {
452 switch (I) {
453 case 'P':
454 Predicate = true;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000455 break;
456 case 'U':
457 Signed = false;
458 break;
459 case 'c':
460 ElementBitwidth = 8;
461 break;
462 case 's':
463 ElementBitwidth = 16;
464 break;
465 case 'i':
466 ElementBitwidth = 32;
467 break;
468 case 'l':
469 ElementBitwidth = 64;
470 break;
471 case 'h':
472 Float = true;
473 ElementBitwidth = 16;
474 break;
475 case 'f':
476 Float = true;
477 ElementBitwidth = 32;
478 break;
479 case 'd':
480 Float = true;
481 ElementBitwidth = 64;
482 break;
483 default:
484 llvm_unreachable("Unhandled type code!");
485 }
486 }
487 assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
488}
489
490void SVEType::applyModifier(char Mod) {
491 switch (Mod) {
492 case 'v':
493 Void = true;
494 break;
495 case 'd':
496 DefaultType = true;
497 break;
498 case 'c':
499 Constant = true;
500 LLVM_FALLTHROUGH;
501 case 'p':
502 Pointer = true;
503 Bitwidth = ElementBitwidth;
504 NumVectors = 0;
505 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100506 case 'e':
507 Signed = false;
508 ElementBitwidth /= 2;
509 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100510 case 'h':
511 ElementBitwidth /= 2;
512 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100513 case 'q':
514 ElementBitwidth /= 4;
515 break;
516 case 'o':
517 ElementBitwidth *= 4;
518 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000519 case 'P':
520 Signed = true;
521 Float = false;
522 Predicate = true;
523 Bitwidth = 16;
524 ElementBitwidth = 1;
525 break;
Sander de Smalen03f419f2020-04-26 12:47:17 +0100526 case 's':
Sander de Smalen41d52662020-04-22 13:58:35 +0100527 case 'a':
528 Bitwidth = ElementBitwidth;
529 NumVectors = 0;
530 break;
Sander de Smalen1a720d42020-05-01 17:34:42 +0100531 case 'K':
532 Signed = true;
533 Float = false;
534 Bitwidth = ElementBitwidth;
535 NumVectors = 0;
536 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100537 case 'L':
538 Signed = false;
539 Float = false;
540 Bitwidth = ElementBitwidth;
541 NumVectors = 0;
542 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100543 case 'u':
544 Predicate = false;
545 Signed = false;
546 Float = false;
547 break;
Andrzej Warzynski72f56582020-04-07 11:09:01 +0100548 case 'x':
549 Predicate = false;
550 Signed = true;
551 Float = false;
552 break;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100553 case 'i':
554 Predicate = false;
555 Float = false;
556 ElementBitwidth = Bitwidth = 64;
557 NumVectors = 0;
558 Signed = false;
559 Immediate = true;
560 break;
561 case 'I':
562 Predicate = false;
563 Float = false;
564 ElementBitwidth = Bitwidth = 32;
565 NumVectors = 0;
566 Signed = true;
567 Immediate = true;
568 PredicatePattern = true;
569 break;
Sander de Smalen823e2a62020-04-24 11:31:34 +0100570 case 'J':
571 Predicate = false;
572 Float = false;
573 ElementBitwidth = Bitwidth = 32;
574 NumVectors = 0;
575 Signed = true;
576 Immediate = true;
577 PrefetchOp = true;
578 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100579 case 'k':
580 Predicate = false;
581 Signed = true;
582 Float = false;
583 ElementBitwidth = Bitwidth = 32;
584 NumVectors = 0;
585 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100586 case 'l':
587 Predicate = false;
588 Signed = true;
589 Float = false;
590 ElementBitwidth = Bitwidth = 64;
591 NumVectors = 0;
592 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100593 case 'm':
594 Predicate = false;
595 Signed = false;
596 Float = false;
597 ElementBitwidth = Bitwidth = 32;
598 NumVectors = 0;
599 break;
600 case 'n':
601 Predicate = false;
602 Signed = false;
603 Float = false;
604 ElementBitwidth = Bitwidth = 64;
605 NumVectors = 0;
606 break;
Sander de Smalen0ddb2032020-04-24 11:31:34 +0100607 case 'w':
608 ElementBitwidth = 64;
609 break;
610 case 'j':
611 ElementBitwidth = Bitwidth = 64;
612 NumVectors = 0;
613 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100614 case 'f':
615 Signed = false;
616 ElementBitwidth = Bitwidth = 64;
617 NumVectors = 0;
618 break;
619 case 'g':
620 Signed = false;
621 Float = false;
622 ElementBitwidth = 64;
623 break;
Sander de Smalena5e03892020-04-23 10:53:23 +0100624 case 't':
625 Signed = true;
626 Float = false;
627 ElementBitwidth = 32;
628 break;
629 case 'z':
630 Signed = false;
631 Float = false;
632 ElementBitwidth = 32;
633 break;
Sander de Smalen00216442020-04-23 10:45:13 +0100634 case 'O':
635 Predicate = false;
636 Float = true;
637 ElementBitwidth = 16;
638 break;
639 case 'M':
640 Predicate = false;
641 Float = true;
642 ElementBitwidth = 32;
643 break;
644 case 'N':
645 Predicate = false;
646 Float = true;
647 ElementBitwidth = 64;
648 break;
Sander de Smalen42a56bf2020-04-29 11:36:41 +0100649 case 'Q':
650 Constant = true;
651 Pointer = true;
652 Void = true;
653 NumVectors = 0;
654 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100655 case 'S':
656 Constant = true;
657 Pointer = true;
658 ElementBitwidth = Bitwidth = 8;
659 NumVectors = 0;
660 Signed = true;
661 break;
662 case 'W':
663 Constant = true;
664 Pointer = true;
665 ElementBitwidth = Bitwidth = 8;
666 NumVectors = 0;
667 Signed = false;
668 break;
669 case 'T':
670 Constant = true;
671 Pointer = true;
672 ElementBitwidth = Bitwidth = 16;
673 NumVectors = 0;
674 Signed = true;
675 break;
676 case 'X':
677 Constant = true;
678 Pointer = true;
679 ElementBitwidth = Bitwidth = 16;
680 NumVectors = 0;
681 Signed = false;
682 break;
683 case 'Y':
684 Constant = true;
685 Pointer = true;
686 ElementBitwidth = Bitwidth = 32;
687 NumVectors = 0;
688 Signed = false;
689 break;
690 case 'U':
691 Constant = true;
692 Pointer = true;
693 ElementBitwidth = Bitwidth = 32;
694 NumVectors = 0;
695 Signed = true;
696 break;
697 case 'A':
698 Pointer = true;
699 ElementBitwidth = Bitwidth = 8;
700 NumVectors = 0;
701 Signed = true;
702 break;
703 case 'B':
704 Pointer = true;
705 ElementBitwidth = Bitwidth = 16;
706 NumVectors = 0;
707 Signed = true;
708 break;
709 case 'C':
710 Pointer = true;
711 ElementBitwidth = Bitwidth = 32;
712 NumVectors = 0;
713 Signed = true;
714 break;
715 case 'D':
716 Pointer = true;
717 ElementBitwidth = Bitwidth = 64;
718 NumVectors = 0;
719 Signed = true;
720 break;
721 case 'E':
722 Pointer = true;
723 ElementBitwidth = Bitwidth = 8;
724 NumVectors = 0;
725 Signed = false;
726 break;
727 case 'F':
728 Pointer = true;
729 ElementBitwidth = Bitwidth = 16;
730 NumVectors = 0;
731 Signed = false;
732 break;
733 case 'G':
734 Pointer = true;
735 ElementBitwidth = Bitwidth = 32;
736 NumVectors = 0;
737 Signed = false;
738 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000739 default:
740 llvm_unreachable("Unhandled character!");
741 }
742}
743
744
745//===----------------------------------------------------------------------===//
746// Intrinsic implementation
747//===----------------------------------------------------------------------===//
748
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100749Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
750 StringRef MergeSuffix, uint64_t MemoryElementTy,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100751 StringRef LLVMName, uint64_t Flags,
752 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class,
753 SVEEmitter &Emitter, StringRef Guard)
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100754 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()),
755 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()),
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100756 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags),
757 ImmChecks(Checks.begin(), Checks.end()) {
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100758
759 // Types[0] is the return value.
760 for (unsigned I = 0; I < Proto.size(); ++I) {
761 SVEType T(BaseTypeSpec, Proto[I]);
762 Types.push_back(T);
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100763
764 // Add range checks for immediates
765 if (I > 0) {
766 if (T.isPredicatePattern())
767 ImmChecks.emplace_back(
768 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31"));
Sander de Smalen823e2a62020-04-24 11:31:34 +0100769 else if (T.isPrefetchOp())
770 ImmChecks.emplace_back(
771 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13"));
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100772 }
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100773 }
774
775 // Set flags based on properties
776 this->Flags |= Emitter.encodeTypeFlags(BaseType);
777 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy);
778 this->Flags |= Emitter.encodeMergeType(MergeTy);
Sander de Smalen41d52662020-04-22 13:58:35 +0100779 if (hasSplat())
780 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx());
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100781}
782
Sander de Smalenc5b81462020-03-18 11:07:20 +0000783std::string Intrinsic::getBuiltinTypeStr() {
784 std::string S;
785
786 SVEType RetT = getReturnType();
787 // Since the return value must be one type, return a vector type of the
788 // appropriate width which we will bitcast. An exception is made for
789 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
790 // fashion, storing them to a pointer arg.
791 if (RetT.getNumVectors() > 1) {
792 S += "vv*"; // void result with void* first argument
793 } else
794 S += RetT.builtin_str();
795
796 for (unsigned I = 0; I < getNumParams(); ++I)
797 S += getParamType(I).builtin_str();
798
799 return S;
800}
801
802std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS,
803 std::string Proto) const {
804 std::string Ret = Name;
805 while (Ret.find('{') != std::string::npos) {
806 size_t Pos = Ret.find('{');
807 size_t End = Ret.find('}');
808 unsigned NumChars = End - Pos + 1;
809 assert(NumChars == 3 && "Unexpected template argument");
810
811 SVEType T;
812 char C = Ret[Pos+1];
813 switch(C) {
814 default:
815 llvm_unreachable("Unknown predication specifier");
816 case 'd':
817 T = SVEType(TS, 'd');
818 break;
819 case '0':
820 case '1':
821 case '2':
822 case '3':
823 T = SVEType(TS, Proto[C - '0']);
824 break;
825 }
826
827 // Replace templated arg with the right suffix (e.g. u32)
828 std::string TypeCode;
829 if (T.isInteger())
830 TypeCode = T.isSigned() ? 's' : 'u';
831 else if (T.isPredicateVector())
832 TypeCode = 'b';
833 else
834 TypeCode = 'f';
835 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits()));
836 }
837
838 return Ret;
839}
840
Sander de Smalenc5b81462020-03-18 11:07:20 +0000841std::string Intrinsic::mangleName(ClassKind LocalCK) const {
842 std::string S = getName();
843
844 if (LocalCK == ClassG) {
845 // Remove the square brackets and everything in between.
846 while (S.find("[") != std::string::npos) {
847 auto Start = S.find("[");
848 auto End = S.find(']');
849 S.erase(Start, (End-Start)+1);
850 }
851 } else {
852 // Remove the square brackets.
853 while (S.find("[") != std::string::npos) {
854 auto BrPos = S.find('[');
855 if (BrPos != std::string::npos)
856 S.erase(BrPos, 1);
857 BrPos = S.find(']');
858 if (BrPos != std::string::npos)
859 S.erase(BrPos, 1);
860 }
861 }
862
863 // Replace all {d} like expressions with e.g. 'u32'
864 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) +
865 getMergeSuffix();
866}
867
868void Intrinsic::emitIntrinsic(raw_ostream &OS) const {
869 // Use the preprocessor to
870 if (getClassKind() != ClassG || getProto().size() <= 1) {
871 OS << "#define " << mangleName(getClassKind())
872 << "(...) __builtin_sve_" << mangleName(ClassS)
873 << "(__VA_ARGS__)\n";
874 } else {
Sander de Smalen981f0802020-03-18 15:05:08 +0000875 std::string FullName = mangleName(ClassS);
876 std::string ProtoName = mangleName(ClassG);
877
878 OS << "__aio __attribute__((__clang_arm_builtin_alias("
879 << "__builtin_sve_" << FullName << ")))\n";
880
881 OS << getTypes()[0].str() << " " << ProtoName << "(";
882 for (unsigned I = 0; I < getTypes().size() - 1; ++I) {
883 if (I != 0)
884 OS << ", ";
885 OS << getTypes()[I + 1].str();
886 }
887 OS << ");\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000888 }
889}
890
891//===----------------------------------------------------------------------===//
892// SVEEmitter implementation
893//===----------------------------------------------------------------------===//
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100894uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) {
895 if (T.isFloat()) {
896 switch (T.getElementSizeInBits()) {
897 case 16:
898 return encodeEltType("EltTyFloat16");
899 case 32:
900 return encodeEltType("EltTyFloat32");
901 case 64:
902 return encodeEltType("EltTyFloat64");
903 default:
904 llvm_unreachable("Unhandled float element bitwidth!");
905 }
906 }
907
908 if (T.isPredicateVector()) {
909 switch (T.getElementSizeInBits()) {
910 case 8:
911 return encodeEltType("EltTyBool8");
912 case 16:
913 return encodeEltType("EltTyBool16");
914 case 32:
915 return encodeEltType("EltTyBool32");
916 case 64:
917 return encodeEltType("EltTyBool64");
918 default:
919 llvm_unreachable("Unhandled predicate element bitwidth!");
920 }
921 }
922
923 switch (T.getElementSizeInBits()) {
924 case 8:
925 return encodeEltType("EltTyInt8");
926 case 16:
927 return encodeEltType("EltTyInt16");
928 case 32:
929 return encodeEltType("EltTyInt32");
930 case 64:
931 return encodeEltType("EltTyInt64");
932 default:
933 llvm_unreachable("Unhandled integer element bitwidth!");
934 }
935}
936
Sander de Smalenc5b81462020-03-18 11:07:20 +0000937void SVEEmitter::createIntrinsic(
938 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) {
939 StringRef Name = R->getValueAsString("Name");
940 StringRef Proto = R->getValueAsString("Prototype");
941 StringRef Types = R->getValueAsString("Types");
942 StringRef Guard = R->getValueAsString("ArchGuard");
943 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100944 uint64_t Merge = R->getValueAsInt("Merge");
945 StringRef MergeSuffix = R->getValueAsString("MergeSuffix");
946 uint64_t MemEltType = R->getValueAsInt("MemEltType");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000947 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100948 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000949
950 int64_t Flags = 0;
951 for (auto FlagRec : FlagsList)
952 Flags |= FlagRec->getValueAsInt("Value");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000953
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100954 // Create a dummy TypeSpec for non-overloaded builtins.
955 if (Types.empty()) {
956 assert((Flags & getEnumValueForFlag("IsOverloadNone")) &&
957 "Expect TypeSpec for overloaded builtin!");
958 Types = "i";
959 }
960
Sander de Smalenc5b81462020-03-18 11:07:20 +0000961 // Extract type specs from string
962 SmallVector<TypeSpec, 8> TypeSpecs;
963 TypeSpec Acc;
964 for (char I : Types) {
965 Acc.push_back(I);
966 if (islower(I)) {
967 TypeSpecs.push_back(TypeSpec(Acc));
968 Acc.clear();
969 }
970 }
971
972 // Remove duplicate type specs.
Benjamin Kramer4065e922020-03-28 19:19:55 +0100973 llvm::sort(TypeSpecs);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000974 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()),
975 TypeSpecs.end());
976
977 // Create an Intrinsic for each type spec.
978 for (auto TS : TypeSpecs) {
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100979 // Collate a list of range/option checks for the immediates.
980 SmallVector<ImmCheck, 2> ImmChecks;
981 for (auto *R : ImmCheckList) {
Christopher Tetreault464a0692020-04-15 15:16:17 -0700982 int64_t Arg = R->getValueAsInt("Arg");
983 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg");
984 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value");
985 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100986
987 unsigned ElementSizeInBits = 0;
988 if (EltSizeArg >= 0)
989 ElementSizeInBits =
990 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1])
991 .getElementSizeInBits();
992 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits));
993 }
994
995 Out.push_back(std::make_unique<Intrinsic>(
996 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks,
997 TS, ClassS, *this, Guard));
Sander de Smalen981f0802020-03-18 15:05:08 +0000998
999 // Also generate the short-form (e.g. svadd_m) for the given type-spec.
1000 if (Intrinsic::isOverloadedIntrinsic(Name))
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001001 Out.push_back(std::make_unique<Intrinsic>(
1002 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags,
1003 ImmChecks, TS, ClassG, *this, Guard));
Sander de Smalenc5b81462020-03-18 11:07:20 +00001004 }
1005}
1006
1007void SVEEmitter::createHeader(raw_ostream &OS) {
Sander de Smalen5087ace2020-03-15 14:29:45 +00001008 OS << "/*===---- arm_sve.h - ARM SVE intrinsics "
1009 "-----------------------------------===\n"
1010 " *\n"
1011 " *\n"
1012 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
1013 "Exceptions.\n"
1014 " * See https://llvm.org/LICENSE.txt for license information.\n"
1015 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
1016 " *\n"
1017 " *===-----------------------------------------------------------------"
1018 "------===\n"
1019 " */\n\n";
1020
1021 OS << "#ifndef __ARM_SVE_H\n";
1022 OS << "#define __ARM_SVE_H\n\n";
1023
1024 OS << "#if !defined(__ARM_FEATURE_SVE)\n";
1025 OS << "#error \"SVE support not enabled\"\n";
1026 OS << "#else\n\n";
1027
Sander de Smalen5ba32902020-05-05 13:04:14 +01001028 OS << "#if !defined(__LITTLE_ENDIAN__)\n";
1029 OS << "#error \"Big endian is currently not supported for arm_sve.h\"\n";
1030 OS << "#endif\n";
1031
Sander de Smalen5087ace2020-03-15 14:29:45 +00001032 OS << "#include <stdint.h>\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001033 OS << "#ifdef __cplusplus\n";
1034 OS << "extern \"C\" {\n";
1035 OS << "#else\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001036 OS << "#include <stdbool.h>\n";
1037 OS << "#endif\n\n";
1038
1039 OS << "typedef __fp16 float16_t;\n";
1040 OS << "typedef float float32_t;\n";
1041 OS << "typedef double float64_t;\n";
1042 OS << "typedef bool bool_t;\n\n";
1043
1044 OS << "typedef __SVInt8_t svint8_t;\n";
1045 OS << "typedef __SVInt16_t svint16_t;\n";
1046 OS << "typedef __SVInt32_t svint32_t;\n";
1047 OS << "typedef __SVInt64_t svint64_t;\n";
1048 OS << "typedef __SVUint8_t svuint8_t;\n";
1049 OS << "typedef __SVUint16_t svuint16_t;\n";
1050 OS << "typedef __SVUint32_t svuint32_t;\n";
1051 OS << "typedef __SVUint64_t svuint64_t;\n";
1052 OS << "typedef __SVFloat16_t svfloat16_t;\n";
1053 OS << "typedef __SVFloat32_t svfloat32_t;\n";
1054 OS << "typedef __SVFloat64_t svfloat64_t;\n";
1055 OS << "typedef __SVBool_t svbool_t;\n\n";
1056
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001057 OS << "typedef enum\n";
1058 OS << "{\n";
1059 OS << " SV_POW2 = 0,\n";
1060 OS << " SV_VL1 = 1,\n";
1061 OS << " SV_VL2 = 2,\n";
1062 OS << " SV_VL3 = 3,\n";
1063 OS << " SV_VL4 = 4,\n";
1064 OS << " SV_VL5 = 5,\n";
1065 OS << " SV_VL6 = 6,\n";
1066 OS << " SV_VL7 = 7,\n";
1067 OS << " SV_VL8 = 8,\n";
1068 OS << " SV_VL16 = 9,\n";
1069 OS << " SV_VL32 = 10,\n";
1070 OS << " SV_VL64 = 11,\n";
1071 OS << " SV_VL128 = 12,\n";
1072 OS << " SV_VL256 = 13,\n";
1073 OS << " SV_MUL4 = 29,\n";
1074 OS << " SV_MUL3 = 30,\n";
1075 OS << " SV_ALL = 31\n";
1076 OS << "} sv_pattern;\n\n";
1077
Sander de Smalen823e2a62020-04-24 11:31:34 +01001078 OS << "typedef enum\n";
1079 OS << "{\n";
1080 OS << " SV_PLDL1KEEP = 0,\n";
1081 OS << " SV_PLDL1STRM = 1,\n";
1082 OS << " SV_PLDL2KEEP = 2,\n";
1083 OS << " SV_PLDL2STRM = 3,\n";
1084 OS << " SV_PLDL3KEEP = 4,\n";
1085 OS << " SV_PLDL3STRM = 5,\n";
1086 OS << " SV_PSTL1KEEP = 8,\n";
1087 OS << " SV_PSTL1STRM = 9,\n";
1088 OS << " SV_PSTL2KEEP = 10,\n";
1089 OS << " SV_PSTL2STRM = 11,\n";
1090 OS << " SV_PSTL3KEEP = 12,\n";
1091 OS << " SV_PSTL3STRM = 13\n";
1092 OS << "} sv_prfop;\n\n";
1093
Sander de Smalen981f0802020-03-18 15:05:08 +00001094 OS << "/* Function attributes */\n";
1095 OS << "#define __aio static inline __attribute__((__always_inline__, "
1096 "__nodebug__, __overloadable__))\n\n";
1097
Sander de Smalen5ba32902020-05-05 13:04:14 +01001098 // Add reinterpret functions.
1099 for (auto ShortForm : { false, true } )
1100 for (const ReinterpretTypeInfo &From : Reinterprets)
1101 for (const ReinterpretTypeInfo &To : Reinterprets) {
1102 if (ShortForm) {
1103 OS << "__aio " << From.Type << " svreinterpret_" << From.Suffix;
1104 OS << "(" << To.Type << " op) {\n";
1105 OS << " return __builtin_sve_reinterpret_" << From.Suffix << "_"
1106 << To.Suffix << "(op);\n";
1107 OS << "}\n\n";
1108 } else
1109 OS << "#define svreinterpret_" << From.Suffix << "_" << To.Suffix
1110 << "(...) __builtin_sve_reinterpret_" << From.Suffix << "_"
1111 << To.Suffix << "(__VA_ARGS__)\n";
1112 }
1113
Sander de Smalenc5b81462020-03-18 11:07:20 +00001114 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1115 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1116 for (auto *R : RV)
1117 createIntrinsic(R, Defs);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001118
Sander de Smalenc5b81462020-03-18 11:07:20 +00001119 // Sort intrinsics in header file by following order/priority:
1120 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES)
1121 // - Class (is intrinsic overloaded or not)
1122 // - Intrinsic name
1123 std::stable_sort(
1124 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A,
1125 const std::unique_ptr<Intrinsic> &B) {
Eric Fiselieraf2968e2020-04-16 18:35:31 -04001126 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) {
1127 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName());
1128 };
1129 return ToTuple(A) < ToTuple(B);
Sander de Smalenc5b81462020-03-18 11:07:20 +00001130 });
1131
1132 StringRef InGuard = "";
1133 for (auto &I : Defs) {
1134 // Emit #endif/#if pair if needed.
1135 if (I->getGuard() != InGuard) {
1136 if (!InGuard.empty())
1137 OS << "#endif //" << InGuard << "\n";
1138 InGuard = I->getGuard();
1139 if (!InGuard.empty())
1140 OS << "\n#if " << InGuard << "\n";
1141 }
1142
1143 // Actually emit the intrinsic declaration.
1144 I->emitIntrinsic(OS);
1145 }
1146
1147 if (!InGuard.empty())
1148 OS << "#endif //" << InGuard << "\n";
1149
Sander de Smalen00216442020-04-23 10:45:13 +01001150 OS << "#if defined(__ARM_FEATURE_SVE2)\n";
1151 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n";
1152 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n";
1153 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n";
1154 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n";
1155
1156 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n";
1157 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n";
1158
1159 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n";
1160
Sander de Smalenc5b81462020-03-18 11:07:20 +00001161 OS << "#ifdef __cplusplus\n";
1162 OS << "} // extern \"C\"\n";
1163 OS << "#endif\n\n";
1164 OS << "#endif /*__ARM_FEATURE_SVE */\n\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001165 OS << "#endif /* __ARM_SVE_H */\n";
1166}
1167
Sander de Smalenc5b81462020-03-18 11:07:20 +00001168void SVEEmitter::createBuiltins(raw_ostream &OS) {
1169 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1170 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1171 for (auto *R : RV)
1172 createIntrinsic(R, Defs);
1173
1174 // The mappings must be sorted based on BuiltinID.
1175 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1176 const std::unique_ptr<Intrinsic> &B) {
1177 return A->getMangledName() < B->getMangledName();
1178 });
1179
1180 OS << "#ifdef GET_SVE_BUILTINS\n";
1181 for (auto &Def : Defs) {
1182 // Only create BUILTINs for non-overloaded intrinsics, as overloaded
1183 // declarations only live in the header file.
1184 if (Def->getClassKind() != ClassG)
1185 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \""
1186 << Def->getBuiltinTypeStr() << "\", \"n\")\n";
1187 }
Sander de Smalen5ba32902020-05-05 13:04:14 +01001188
1189 // Add reinterpret builtins
1190 for (const ReinterpretTypeInfo &From : Reinterprets)
1191 for (const ReinterpretTypeInfo &To : Reinterprets)
1192 OS << "BUILTIN(__builtin_sve_reinterpret_" << From.Suffix << "_"
1193 << To.Suffix << +", \"" << From.BuiltinType << To.BuiltinType
1194 << "\", \"n\")\n";
1195
Sander de Smalenc5b81462020-03-18 11:07:20 +00001196 OS << "#endif\n\n";
Sander de Smalen5ba32902020-05-05 13:04:14 +01001197 }
Sander de Smalenc5b81462020-03-18 11:07:20 +00001198
1199void SVEEmitter::createCodeGenMap(raw_ostream &OS) {
1200 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1201 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1202 for (auto *R : RV)
1203 createIntrinsic(R, Defs);
1204
1205 // The mappings must be sorted based on BuiltinID.
1206 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1207 const std::unique_ptr<Intrinsic> &B) {
1208 return A->getMangledName() < B->getMangledName();
1209 });
1210
1211 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n";
1212 for (auto &Def : Defs) {
1213 // Builtins only exist for non-overloaded intrinsics, overloaded
1214 // declarations only live in the header file.
1215 if (Def->getClassKind() == ClassG)
1216 continue;
1217
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001218 uint64_t Flags = Def->getFlags();
Sander de Smalenc5b81462020-03-18 11:07:20 +00001219 auto FlagString = std::to_string(Flags);
1220
1221 std::string LLVMName = Def->getLLVMName();
1222 std::string Builtin = Def->getMangledName();
1223 if (!LLVMName.empty())
1224 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString
1225 << "),\n";
1226 else
1227 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n";
1228 }
1229 OS << "#endif\n\n";
1230}
1231
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001232void SVEEmitter::createRangeChecks(raw_ostream &OS) {
1233 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1234 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1235 for (auto *R : RV)
1236 createIntrinsic(R, Defs);
1237
1238 // The mappings must be sorted based on BuiltinID.
1239 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1240 const std::unique_ptr<Intrinsic> &B) {
1241 return A->getMangledName() < B->getMangledName();
1242 });
1243
1244
1245 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n";
1246
1247 // Ensure these are only emitted once.
1248 std::set<std::string> Emitted;
1249
1250 for (auto &Def : Defs) {
1251 if (Emitted.find(Def->getMangledName()) != Emitted.end() ||
1252 Def->getImmChecks().empty())
1253 continue;
1254
1255 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n";
1256 for (auto &Check : Def->getImmChecks())
1257 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", "
1258 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n";
1259 OS << " break;\n";
1260
1261 Emitted.insert(Def->getMangledName());
1262 }
1263
1264 OS << "#endif\n\n";
1265}
1266
Sander de Smalenc5b81462020-03-18 11:07:20 +00001267/// Create the SVETypeFlags used in CGBuiltins
1268void SVEEmitter::createTypeFlags(raw_ostream &OS) {
1269 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n";
1270 for (auto &KV : FlagTypes)
1271 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n";
1272 OS << "#endif\n\n";
1273
1274 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n";
1275 for (auto &KV : EltTypes)
1276 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1277 OS << "#endif\n\n";
1278
1279 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n";
1280 for (auto &KV : MemEltTypes)
1281 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1282 OS << "#endif\n\n";
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001283
1284 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n";
1285 for (auto &KV : MergeTypes)
1286 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1287 OS << "#endif\n\n";
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001288
1289 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n";
1290 for (auto &KV : ImmCheckTypes)
1291 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1292 OS << "#endif\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001293}
1294
Sander de Smalen5087ace2020-03-15 14:29:45 +00001295namespace clang {
1296void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) {
Sander de Smalenc5b81462020-03-18 11:07:20 +00001297 SVEEmitter(Records).createHeader(OS);
1298}
1299
1300void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1301 SVEEmitter(Records).createBuiltins(OS);
1302}
1303
1304void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
1305 SVEEmitter(Records).createCodeGenMap(OS);
1306}
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001307
1308void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) {
1309 SVEEmitter(Records).createRangeChecks(OS);
1310}
1311
Sander de Smalenc5b81462020-03-18 11:07:20 +00001312void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) {
1313 SVEEmitter(Records).createTypeFlags(OS);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001314}
1315
1316} // End namespace clang