blob: d8df92f2074de079d38e9ea102e27f7f6398598a [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:
240 RecordKeeper &Records;
241 llvm::StringMap<uint64_t> EltTypes;
242 llvm::StringMap<uint64_t> MemEltTypes;
243 llvm::StringMap<uint64_t> FlagTypes;
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100244 llvm::StringMap<uint64_t> MergeTypes;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100245 llvm::StringMap<uint64_t> ImmCheckTypes;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000246
Sander de Smalenc5b81462020-03-18 11:07:20 +0000247public:
248 SVEEmitter(RecordKeeper &R) : Records(R) {
249 for (auto *RV : Records.getAllDerivedDefinitions("EltType"))
250 EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
251 for (auto *RV : Records.getAllDerivedDefinitions("MemEltType"))
252 MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
253 for (auto *RV : Records.getAllDerivedDefinitions("FlagType"))
254 FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100255 for (auto *RV : Records.getAllDerivedDefinitions("MergeType"))
256 MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100257 for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType"))
258 ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
259 }
260
261 /// Returns the enum value for the immcheck type
262 unsigned getEnumValueForImmCheck(StringRef C) const {
263 auto It = ImmCheckTypes.find(C);
264 if (It != ImmCheckTypes.end())
265 return It->getValue();
266 llvm_unreachable("Unsupported imm check");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000267 }
268
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100269 /// Returns the enum value for the flag type
270 uint64_t getEnumValueForFlag(StringRef C) const {
271 auto Res = FlagTypes.find(C);
272 if (Res != FlagTypes.end())
273 return Res->getValue();
274 llvm_unreachable("Unsupported flag");
275 }
276
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100277 // Returns the SVETypeFlags for a given value and mask.
278 uint64_t encodeFlag(uint64_t V, StringRef MaskName) const {
279 auto It = FlagTypes.find(MaskName);
280 if (It != FlagTypes.end()) {
281 uint64_t Mask = It->getValue();
282 unsigned Shift = llvm::countTrailingZeros(Mask);
283 return (V << Shift) & Mask;
284 }
285 llvm_unreachable("Unsupported flag");
286 }
287
288 // Returns the SVETypeFlags for the given element type.
289 uint64_t encodeEltType(StringRef EltName) {
290 auto It = EltTypes.find(EltName);
291 if (It != EltTypes.end())
292 return encodeFlag(It->getValue(), "EltTypeMask");
293 llvm_unreachable("Unsupported EltType");
294 }
295
296 // Returns the SVETypeFlags for the given memory element type.
297 uint64_t encodeMemoryElementType(uint64_t MT) {
298 return encodeFlag(MT, "MemEltTypeMask");
299 }
300
301 // Returns the SVETypeFlags for the given merge type.
302 uint64_t encodeMergeType(uint64_t MT) {
303 return encodeFlag(MT, "MergeTypeMask");
304 }
305
Sander de Smalen41d52662020-04-22 13:58:35 +0100306 // Returns the SVETypeFlags for the given splat operand.
307 unsigned encodeSplatOperand(unsigned SplatIdx) {
308 assert(SplatIdx < 7 && "SplatIdx out of encodable range");
309 return encodeFlag(SplatIdx + 1, "SplatOperandMask");
310 }
311
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100312 // Returns the SVETypeFlags value for the given SVEType.
313 uint64_t encodeTypeFlags(const SVEType &T);
314
Sander de Smalenc5b81462020-03-18 11:07:20 +0000315 /// Emit arm_sve.h.
316 void createHeader(raw_ostream &o);
317
318 /// Emit all the __builtin prototypes and code needed by Sema.
319 void createBuiltins(raw_ostream &o);
320
321 /// Emit all the information needed to map builtin -> LLVM IR intrinsic.
322 void createCodeGenMap(raw_ostream &o);
323
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100324 /// Emit all the range checks for the immediates.
325 void createRangeChecks(raw_ostream &o);
326
Sander de Smalenc5b81462020-03-18 11:07:20 +0000327 /// Create the SVETypeFlags used in CGBuiltins
328 void createTypeFlags(raw_ostream &o);
329
330 /// Create intrinsic and add it to \p Out
331 void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out);
Sander de Smalen5087ace2020-03-15 14:29:45 +0000332};
333
334} // end anonymous namespace
335
336
337//===----------------------------------------------------------------------===//
Sander de Smalenc5b81462020-03-18 11:07:20 +0000338// Type implementation
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000339//===----------------------------------------------------------------------===//
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000340
Sander de Smalenc5b81462020-03-18 11:07:20 +0000341std::string SVEType::builtin_str() const {
342 std::string S;
343 if (isVoid())
344 return "v";
345
346 if (isVoidPointer())
347 S += "v";
348 else if (!Float)
349 switch (ElementBitwidth) {
350 case 1: S += "b"; break;
351 case 8: S += "c"; break;
352 case 16: S += "s"; break;
353 case 32: S += "i"; break;
354 case 64: S += "Wi"; break;
355 case 128: S += "LLLi"; break;
356 default: llvm_unreachable("Unhandled case!");
357 }
358 else
359 switch (ElementBitwidth) {
360 case 16: S += "h"; break;
361 case 32: S += "f"; break;
362 case 64: S += "d"; break;
363 default: llvm_unreachable("Unhandled case!");
364 }
365
366 if (!isFloat()) {
367 if ((isChar() || isPointer()) && !isVoidPointer()) {
368 // Make chars and typed pointers explicitly signed.
369 if (Signed)
370 S = "S" + S;
371 else if (!Signed)
372 S = "U" + S;
373 } else if (!isVoidPointer() && !Signed) {
374 S = "U" + S;
375 }
376 }
377
378 // Constant indices are "int", but have the "constant expression" modifier.
379 if (isImmediate()) {
380 assert(!isFloat() && "fp immediates are not supported");
381 S = "I" + S;
382 }
383
384 if (isScalar()) {
385 if (Constant) S += "C";
386 if (Pointer) S += "*";
387 return S;
388 }
389
390 assert(isScalableVector() && "Unsupported type");
391 return "q" + utostr(getNumElements() * NumVectors) + S;
392}
393
Sander de Smalen981f0802020-03-18 15:05:08 +0000394std::string SVEType::str() const {
395 if (isPredicatePattern())
396 return "sv_pattern";
397
398 if (isPrefetchOp())
399 return "sv_prfop";
400
401 std::string S;
402 if (Void)
403 S += "void";
404 else {
405 if (isScalableVector())
406 S += "sv";
407 if (!Signed && !Float)
408 S += "u";
409
410 if (Float)
411 S += "float";
Sander de Smalenaed6bd62020-05-05 09:16:57 +0100412 else if (isScalarPredicate() || isPredicateVector())
Sander de Smalen981f0802020-03-18 15:05:08 +0000413 S += "bool";
414 else
415 S += "int";
416
Sander de Smalenaed6bd62020-05-05 09:16:57 +0100417 if (!isScalarPredicate() && !isPredicateVector())
Sander de Smalen981f0802020-03-18 15:05:08 +0000418 S += utostr(ElementBitwidth);
419 if (!isScalableVector() && isVector())
420 S += "x" + utostr(getNumElements());
421 if (NumVectors > 1)
422 S += "x" + utostr(NumVectors);
423 S += "_t";
424 }
425
426 if (Constant)
427 S += " const";
428 if (Pointer)
429 S += " *";
430
431 return S;
432}
Sander de Smalenc5b81462020-03-18 11:07:20 +0000433void SVEType::applyTypespec() {
434 for (char I : TS) {
435 switch (I) {
436 case 'P':
437 Predicate = true;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000438 break;
439 case 'U':
440 Signed = false;
441 break;
442 case 'c':
443 ElementBitwidth = 8;
444 break;
445 case 's':
446 ElementBitwidth = 16;
447 break;
448 case 'i':
449 ElementBitwidth = 32;
450 break;
451 case 'l':
452 ElementBitwidth = 64;
453 break;
454 case 'h':
455 Float = true;
456 ElementBitwidth = 16;
457 break;
458 case 'f':
459 Float = true;
460 ElementBitwidth = 32;
461 break;
462 case 'd':
463 Float = true;
464 ElementBitwidth = 64;
465 break;
466 default:
467 llvm_unreachable("Unhandled type code!");
468 }
469 }
470 assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
471}
472
473void SVEType::applyModifier(char Mod) {
474 switch (Mod) {
475 case 'v':
476 Void = true;
477 break;
478 case 'd':
479 DefaultType = true;
480 break;
481 case 'c':
482 Constant = true;
483 LLVM_FALLTHROUGH;
484 case 'p':
485 Pointer = true;
486 Bitwidth = ElementBitwidth;
487 NumVectors = 0;
488 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100489 case 'e':
490 Signed = false;
491 ElementBitwidth /= 2;
492 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100493 case 'h':
494 ElementBitwidth /= 2;
495 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100496 case 'q':
497 ElementBitwidth /= 4;
498 break;
499 case 'o':
500 ElementBitwidth *= 4;
501 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000502 case 'P':
503 Signed = true;
504 Float = false;
505 Predicate = true;
506 Bitwidth = 16;
507 ElementBitwidth = 1;
508 break;
Sander de Smalen03f419f2020-04-26 12:47:17 +0100509 case 's':
Sander de Smalen41d52662020-04-22 13:58:35 +0100510 case 'a':
511 Bitwidth = ElementBitwidth;
512 NumVectors = 0;
513 break;
Sander de Smalen1a720d42020-05-01 17:34:42 +0100514 case 'K':
515 Signed = true;
516 Float = false;
517 Bitwidth = ElementBitwidth;
518 NumVectors = 0;
519 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100520 case 'L':
521 Signed = false;
522 Float = false;
523 Bitwidth = ElementBitwidth;
524 NumVectors = 0;
525 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100526 case 'u':
527 Predicate = false;
528 Signed = false;
529 Float = false;
530 break;
Andrzej Warzynski72f56582020-04-07 11:09:01 +0100531 case 'x':
532 Predicate = false;
533 Signed = true;
534 Float = false;
535 break;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100536 case 'i':
537 Predicate = false;
538 Float = false;
539 ElementBitwidth = Bitwidth = 64;
540 NumVectors = 0;
541 Signed = false;
542 Immediate = true;
543 break;
544 case 'I':
545 Predicate = false;
546 Float = false;
547 ElementBitwidth = Bitwidth = 32;
548 NumVectors = 0;
549 Signed = true;
550 Immediate = true;
551 PredicatePattern = true;
552 break;
Sander de Smalen823e2a62020-04-24 11:31:34 +0100553 case 'J':
554 Predicate = false;
555 Float = false;
556 ElementBitwidth = Bitwidth = 32;
557 NumVectors = 0;
558 Signed = true;
559 Immediate = true;
560 PrefetchOp = true;
561 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100562 case 'k':
563 Predicate = false;
564 Signed = true;
565 Float = false;
566 ElementBitwidth = Bitwidth = 32;
567 NumVectors = 0;
568 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100569 case 'l':
570 Predicate = false;
571 Signed = true;
572 Float = false;
573 ElementBitwidth = Bitwidth = 64;
574 NumVectors = 0;
575 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100576 case 'm':
577 Predicate = false;
578 Signed = false;
579 Float = false;
580 ElementBitwidth = Bitwidth = 32;
581 NumVectors = 0;
582 break;
583 case 'n':
584 Predicate = false;
585 Signed = false;
586 Float = false;
587 ElementBitwidth = Bitwidth = 64;
588 NumVectors = 0;
589 break;
Sander de Smalen0ddb2032020-04-24 11:31:34 +0100590 case 'w':
591 ElementBitwidth = 64;
592 break;
593 case 'j':
594 ElementBitwidth = Bitwidth = 64;
595 NumVectors = 0;
596 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100597 case 'f':
598 Signed = false;
599 ElementBitwidth = Bitwidth = 64;
600 NumVectors = 0;
601 break;
602 case 'g':
603 Signed = false;
604 Float = false;
605 ElementBitwidth = 64;
606 break;
Sander de Smalena5e03892020-04-23 10:53:23 +0100607 case 't':
608 Signed = true;
609 Float = false;
610 ElementBitwidth = 32;
611 break;
612 case 'z':
613 Signed = false;
614 Float = false;
615 ElementBitwidth = 32;
616 break;
Sander de Smalen00216442020-04-23 10:45:13 +0100617 case 'O':
618 Predicate = false;
619 Float = true;
620 ElementBitwidth = 16;
621 break;
622 case 'M':
623 Predicate = false;
624 Float = true;
625 ElementBitwidth = 32;
626 break;
627 case 'N':
628 Predicate = false;
629 Float = true;
630 ElementBitwidth = 64;
631 break;
Sander de Smalen42a56bf2020-04-29 11:36:41 +0100632 case 'Q':
633 Constant = true;
634 Pointer = true;
635 Void = true;
636 NumVectors = 0;
637 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100638 case 'S':
639 Constant = true;
640 Pointer = true;
641 ElementBitwidth = Bitwidth = 8;
642 NumVectors = 0;
643 Signed = true;
644 break;
645 case 'W':
646 Constant = true;
647 Pointer = true;
648 ElementBitwidth = Bitwidth = 8;
649 NumVectors = 0;
650 Signed = false;
651 break;
652 case 'T':
653 Constant = true;
654 Pointer = true;
655 ElementBitwidth = Bitwidth = 16;
656 NumVectors = 0;
657 Signed = true;
658 break;
659 case 'X':
660 Constant = true;
661 Pointer = true;
662 ElementBitwidth = Bitwidth = 16;
663 NumVectors = 0;
664 Signed = false;
665 break;
666 case 'Y':
667 Constant = true;
668 Pointer = true;
669 ElementBitwidth = Bitwidth = 32;
670 NumVectors = 0;
671 Signed = false;
672 break;
673 case 'U':
674 Constant = true;
675 Pointer = true;
676 ElementBitwidth = Bitwidth = 32;
677 NumVectors = 0;
678 Signed = true;
679 break;
680 case 'A':
681 Pointer = true;
682 ElementBitwidth = Bitwidth = 8;
683 NumVectors = 0;
684 Signed = true;
685 break;
686 case 'B':
687 Pointer = true;
688 ElementBitwidth = Bitwidth = 16;
689 NumVectors = 0;
690 Signed = true;
691 break;
692 case 'C':
693 Pointer = true;
694 ElementBitwidth = Bitwidth = 32;
695 NumVectors = 0;
696 Signed = true;
697 break;
698 case 'D':
699 Pointer = true;
700 ElementBitwidth = Bitwidth = 64;
701 NumVectors = 0;
702 Signed = true;
703 break;
704 case 'E':
705 Pointer = true;
706 ElementBitwidth = Bitwidth = 8;
707 NumVectors = 0;
708 Signed = false;
709 break;
710 case 'F':
711 Pointer = true;
712 ElementBitwidth = Bitwidth = 16;
713 NumVectors = 0;
714 Signed = false;
715 break;
716 case 'G':
717 Pointer = true;
718 ElementBitwidth = Bitwidth = 32;
719 NumVectors = 0;
720 Signed = false;
721 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000722 default:
723 llvm_unreachable("Unhandled character!");
724 }
725}
726
727
728//===----------------------------------------------------------------------===//
729// Intrinsic implementation
730//===----------------------------------------------------------------------===//
731
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100732Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
733 StringRef MergeSuffix, uint64_t MemoryElementTy,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100734 StringRef LLVMName, uint64_t Flags,
735 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class,
736 SVEEmitter &Emitter, StringRef Guard)
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100737 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()),
738 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()),
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100739 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags),
740 ImmChecks(Checks.begin(), Checks.end()) {
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100741
742 // Types[0] is the return value.
743 for (unsigned I = 0; I < Proto.size(); ++I) {
744 SVEType T(BaseTypeSpec, Proto[I]);
745 Types.push_back(T);
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100746
747 // Add range checks for immediates
748 if (I > 0) {
749 if (T.isPredicatePattern())
750 ImmChecks.emplace_back(
751 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31"));
Sander de Smalen823e2a62020-04-24 11:31:34 +0100752 else if (T.isPrefetchOp())
753 ImmChecks.emplace_back(
754 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13"));
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100755 }
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100756 }
757
758 // Set flags based on properties
759 this->Flags |= Emitter.encodeTypeFlags(BaseType);
760 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy);
761 this->Flags |= Emitter.encodeMergeType(MergeTy);
Sander de Smalen41d52662020-04-22 13:58:35 +0100762 if (hasSplat())
763 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx());
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100764}
765
Sander de Smalenc5b81462020-03-18 11:07:20 +0000766std::string Intrinsic::getBuiltinTypeStr() {
767 std::string S;
768
769 SVEType RetT = getReturnType();
770 // Since the return value must be one type, return a vector type of the
771 // appropriate width which we will bitcast. An exception is made for
772 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
773 // fashion, storing them to a pointer arg.
774 if (RetT.getNumVectors() > 1) {
775 S += "vv*"; // void result with void* first argument
776 } else
777 S += RetT.builtin_str();
778
779 for (unsigned I = 0; I < getNumParams(); ++I)
780 S += getParamType(I).builtin_str();
781
782 return S;
783}
784
785std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS,
786 std::string Proto) const {
787 std::string Ret = Name;
788 while (Ret.find('{') != std::string::npos) {
789 size_t Pos = Ret.find('{');
790 size_t End = Ret.find('}');
791 unsigned NumChars = End - Pos + 1;
792 assert(NumChars == 3 && "Unexpected template argument");
793
794 SVEType T;
795 char C = Ret[Pos+1];
796 switch(C) {
797 default:
798 llvm_unreachable("Unknown predication specifier");
799 case 'd':
800 T = SVEType(TS, 'd');
801 break;
802 case '0':
803 case '1':
804 case '2':
805 case '3':
806 T = SVEType(TS, Proto[C - '0']);
807 break;
808 }
809
810 // Replace templated arg with the right suffix (e.g. u32)
811 std::string TypeCode;
812 if (T.isInteger())
813 TypeCode = T.isSigned() ? 's' : 'u';
814 else if (T.isPredicateVector())
815 TypeCode = 'b';
816 else
817 TypeCode = 'f';
818 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits()));
819 }
820
821 return Ret;
822}
823
Sander de Smalenc5b81462020-03-18 11:07:20 +0000824std::string Intrinsic::mangleName(ClassKind LocalCK) const {
825 std::string S = getName();
826
827 if (LocalCK == ClassG) {
828 // Remove the square brackets and everything in between.
829 while (S.find("[") != std::string::npos) {
830 auto Start = S.find("[");
831 auto End = S.find(']');
832 S.erase(Start, (End-Start)+1);
833 }
834 } else {
835 // Remove the square brackets.
836 while (S.find("[") != std::string::npos) {
837 auto BrPos = S.find('[');
838 if (BrPos != std::string::npos)
839 S.erase(BrPos, 1);
840 BrPos = S.find(']');
841 if (BrPos != std::string::npos)
842 S.erase(BrPos, 1);
843 }
844 }
845
846 // Replace all {d} like expressions with e.g. 'u32'
847 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) +
848 getMergeSuffix();
849}
850
851void Intrinsic::emitIntrinsic(raw_ostream &OS) const {
852 // Use the preprocessor to
853 if (getClassKind() != ClassG || getProto().size() <= 1) {
854 OS << "#define " << mangleName(getClassKind())
855 << "(...) __builtin_sve_" << mangleName(ClassS)
856 << "(__VA_ARGS__)\n";
857 } else {
Sander de Smalen981f0802020-03-18 15:05:08 +0000858 std::string FullName = mangleName(ClassS);
859 std::string ProtoName = mangleName(ClassG);
860
861 OS << "__aio __attribute__((__clang_arm_builtin_alias("
862 << "__builtin_sve_" << FullName << ")))\n";
863
864 OS << getTypes()[0].str() << " " << ProtoName << "(";
865 for (unsigned I = 0; I < getTypes().size() - 1; ++I) {
866 if (I != 0)
867 OS << ", ";
868 OS << getTypes()[I + 1].str();
869 }
870 OS << ");\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000871 }
872}
873
874//===----------------------------------------------------------------------===//
875// SVEEmitter implementation
876//===----------------------------------------------------------------------===//
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100877uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) {
878 if (T.isFloat()) {
879 switch (T.getElementSizeInBits()) {
880 case 16:
881 return encodeEltType("EltTyFloat16");
882 case 32:
883 return encodeEltType("EltTyFloat32");
884 case 64:
885 return encodeEltType("EltTyFloat64");
886 default:
887 llvm_unreachable("Unhandled float element bitwidth!");
888 }
889 }
890
891 if (T.isPredicateVector()) {
892 switch (T.getElementSizeInBits()) {
893 case 8:
894 return encodeEltType("EltTyBool8");
895 case 16:
896 return encodeEltType("EltTyBool16");
897 case 32:
898 return encodeEltType("EltTyBool32");
899 case 64:
900 return encodeEltType("EltTyBool64");
901 default:
902 llvm_unreachable("Unhandled predicate element bitwidth!");
903 }
904 }
905
906 switch (T.getElementSizeInBits()) {
907 case 8:
908 return encodeEltType("EltTyInt8");
909 case 16:
910 return encodeEltType("EltTyInt16");
911 case 32:
912 return encodeEltType("EltTyInt32");
913 case 64:
914 return encodeEltType("EltTyInt64");
915 default:
916 llvm_unreachable("Unhandled integer element bitwidth!");
917 }
918}
919
Sander de Smalenc5b81462020-03-18 11:07:20 +0000920void SVEEmitter::createIntrinsic(
921 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) {
922 StringRef Name = R->getValueAsString("Name");
923 StringRef Proto = R->getValueAsString("Prototype");
924 StringRef Types = R->getValueAsString("Types");
925 StringRef Guard = R->getValueAsString("ArchGuard");
926 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100927 uint64_t Merge = R->getValueAsInt("Merge");
928 StringRef MergeSuffix = R->getValueAsString("MergeSuffix");
929 uint64_t MemEltType = R->getValueAsInt("MemEltType");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000930 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100931 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000932
933 int64_t Flags = 0;
934 for (auto FlagRec : FlagsList)
935 Flags |= FlagRec->getValueAsInt("Value");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000936
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100937 // Create a dummy TypeSpec for non-overloaded builtins.
938 if (Types.empty()) {
939 assert((Flags & getEnumValueForFlag("IsOverloadNone")) &&
940 "Expect TypeSpec for overloaded builtin!");
941 Types = "i";
942 }
943
Sander de Smalenc5b81462020-03-18 11:07:20 +0000944 // Extract type specs from string
945 SmallVector<TypeSpec, 8> TypeSpecs;
946 TypeSpec Acc;
947 for (char I : Types) {
948 Acc.push_back(I);
949 if (islower(I)) {
950 TypeSpecs.push_back(TypeSpec(Acc));
951 Acc.clear();
952 }
953 }
954
955 // Remove duplicate type specs.
Benjamin Kramer4065e922020-03-28 19:19:55 +0100956 llvm::sort(TypeSpecs);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000957 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()),
958 TypeSpecs.end());
959
960 // Create an Intrinsic for each type spec.
961 for (auto TS : TypeSpecs) {
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100962 // Collate a list of range/option checks for the immediates.
963 SmallVector<ImmCheck, 2> ImmChecks;
964 for (auto *R : ImmCheckList) {
Christopher Tetreault464a0692020-04-15 15:16:17 -0700965 int64_t Arg = R->getValueAsInt("Arg");
966 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg");
967 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value");
968 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100969
970 unsigned ElementSizeInBits = 0;
971 if (EltSizeArg >= 0)
972 ElementSizeInBits =
973 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1])
974 .getElementSizeInBits();
975 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits));
976 }
977
978 Out.push_back(std::make_unique<Intrinsic>(
979 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks,
980 TS, ClassS, *this, Guard));
Sander de Smalen981f0802020-03-18 15:05:08 +0000981
982 // Also generate the short-form (e.g. svadd_m) for the given type-spec.
983 if (Intrinsic::isOverloadedIntrinsic(Name))
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100984 Out.push_back(std::make_unique<Intrinsic>(
985 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags,
986 ImmChecks, TS, ClassG, *this, Guard));
Sander de Smalenc5b81462020-03-18 11:07:20 +0000987 }
988}
989
990void SVEEmitter::createHeader(raw_ostream &OS) {
Sander de Smalen5087ace2020-03-15 14:29:45 +0000991 OS << "/*===---- arm_sve.h - ARM SVE intrinsics "
992 "-----------------------------------===\n"
993 " *\n"
994 " *\n"
995 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
996 "Exceptions.\n"
997 " * See https://llvm.org/LICENSE.txt for license information.\n"
998 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
999 " *\n"
1000 " *===-----------------------------------------------------------------"
1001 "------===\n"
1002 " */\n\n";
1003
1004 OS << "#ifndef __ARM_SVE_H\n";
1005 OS << "#define __ARM_SVE_H\n\n";
1006
1007 OS << "#if !defined(__ARM_FEATURE_SVE)\n";
1008 OS << "#error \"SVE support not enabled\"\n";
1009 OS << "#else\n\n";
1010
1011 OS << "#include <stdint.h>\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001012 OS << "#ifdef __cplusplus\n";
1013 OS << "extern \"C\" {\n";
1014 OS << "#else\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001015 OS << "#include <stdbool.h>\n";
1016 OS << "#endif\n\n";
1017
1018 OS << "typedef __fp16 float16_t;\n";
1019 OS << "typedef float float32_t;\n";
1020 OS << "typedef double float64_t;\n";
1021 OS << "typedef bool bool_t;\n\n";
1022
1023 OS << "typedef __SVInt8_t svint8_t;\n";
1024 OS << "typedef __SVInt16_t svint16_t;\n";
1025 OS << "typedef __SVInt32_t svint32_t;\n";
1026 OS << "typedef __SVInt64_t svint64_t;\n";
1027 OS << "typedef __SVUint8_t svuint8_t;\n";
1028 OS << "typedef __SVUint16_t svuint16_t;\n";
1029 OS << "typedef __SVUint32_t svuint32_t;\n";
1030 OS << "typedef __SVUint64_t svuint64_t;\n";
1031 OS << "typedef __SVFloat16_t svfloat16_t;\n";
1032 OS << "typedef __SVFloat32_t svfloat32_t;\n";
1033 OS << "typedef __SVFloat64_t svfloat64_t;\n";
1034 OS << "typedef __SVBool_t svbool_t;\n\n";
1035
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001036 OS << "typedef enum\n";
1037 OS << "{\n";
1038 OS << " SV_POW2 = 0,\n";
1039 OS << " SV_VL1 = 1,\n";
1040 OS << " SV_VL2 = 2,\n";
1041 OS << " SV_VL3 = 3,\n";
1042 OS << " SV_VL4 = 4,\n";
1043 OS << " SV_VL5 = 5,\n";
1044 OS << " SV_VL6 = 6,\n";
1045 OS << " SV_VL7 = 7,\n";
1046 OS << " SV_VL8 = 8,\n";
1047 OS << " SV_VL16 = 9,\n";
1048 OS << " SV_VL32 = 10,\n";
1049 OS << " SV_VL64 = 11,\n";
1050 OS << " SV_VL128 = 12,\n";
1051 OS << " SV_VL256 = 13,\n";
1052 OS << " SV_MUL4 = 29,\n";
1053 OS << " SV_MUL3 = 30,\n";
1054 OS << " SV_ALL = 31\n";
1055 OS << "} sv_pattern;\n\n";
1056
Sander de Smalen823e2a62020-04-24 11:31:34 +01001057 OS << "typedef enum\n";
1058 OS << "{\n";
1059 OS << " SV_PLDL1KEEP = 0,\n";
1060 OS << " SV_PLDL1STRM = 1,\n";
1061 OS << " SV_PLDL2KEEP = 2,\n";
1062 OS << " SV_PLDL2STRM = 3,\n";
1063 OS << " SV_PLDL3KEEP = 4,\n";
1064 OS << " SV_PLDL3STRM = 5,\n";
1065 OS << " SV_PSTL1KEEP = 8,\n";
1066 OS << " SV_PSTL1STRM = 9,\n";
1067 OS << " SV_PSTL2KEEP = 10,\n";
1068 OS << " SV_PSTL2STRM = 11,\n";
1069 OS << " SV_PSTL3KEEP = 12,\n";
1070 OS << " SV_PSTL3STRM = 13\n";
1071 OS << "} sv_prfop;\n\n";
1072
Sander de Smalen981f0802020-03-18 15:05:08 +00001073 OS << "/* Function attributes */\n";
1074 OS << "#define __aio static inline __attribute__((__always_inline__, "
1075 "__nodebug__, __overloadable__))\n\n";
1076
Sander de Smalenc5b81462020-03-18 11:07:20 +00001077 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1078 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1079 for (auto *R : RV)
1080 createIntrinsic(R, Defs);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001081
Sander de Smalenc5b81462020-03-18 11:07:20 +00001082 // Sort intrinsics in header file by following order/priority:
1083 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES)
1084 // - Class (is intrinsic overloaded or not)
1085 // - Intrinsic name
1086 std::stable_sort(
1087 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A,
1088 const std::unique_ptr<Intrinsic> &B) {
Eric Fiselieraf2968e2020-04-16 18:35:31 -04001089 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) {
1090 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName());
1091 };
1092 return ToTuple(A) < ToTuple(B);
Sander de Smalenc5b81462020-03-18 11:07:20 +00001093 });
1094
1095 StringRef InGuard = "";
1096 for (auto &I : Defs) {
1097 // Emit #endif/#if pair if needed.
1098 if (I->getGuard() != InGuard) {
1099 if (!InGuard.empty())
1100 OS << "#endif //" << InGuard << "\n";
1101 InGuard = I->getGuard();
1102 if (!InGuard.empty())
1103 OS << "\n#if " << InGuard << "\n";
1104 }
1105
1106 // Actually emit the intrinsic declaration.
1107 I->emitIntrinsic(OS);
1108 }
1109
1110 if (!InGuard.empty())
1111 OS << "#endif //" << InGuard << "\n";
1112
Sander de Smalen00216442020-04-23 10:45:13 +01001113 OS << "#if defined(__ARM_FEATURE_SVE2)\n";
1114 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n";
1115 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n";
1116 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n";
1117 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n";
1118
1119 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n";
1120 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n";
1121
1122 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n";
1123
Sander de Smalenc5b81462020-03-18 11:07:20 +00001124 OS << "#ifdef __cplusplus\n";
1125 OS << "} // extern \"C\"\n";
1126 OS << "#endif\n\n";
1127 OS << "#endif /*__ARM_FEATURE_SVE */\n\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001128 OS << "#endif /* __ARM_SVE_H */\n";
1129}
1130
Sander de Smalenc5b81462020-03-18 11:07:20 +00001131void SVEEmitter::createBuiltins(raw_ostream &OS) {
1132 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1133 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1134 for (auto *R : RV)
1135 createIntrinsic(R, Defs);
1136
1137 // The mappings must be sorted based on BuiltinID.
1138 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1139 const std::unique_ptr<Intrinsic> &B) {
1140 return A->getMangledName() < B->getMangledName();
1141 });
1142
1143 OS << "#ifdef GET_SVE_BUILTINS\n";
1144 for (auto &Def : Defs) {
1145 // Only create BUILTINs for non-overloaded intrinsics, as overloaded
1146 // declarations only live in the header file.
1147 if (Def->getClassKind() != ClassG)
1148 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \""
1149 << Def->getBuiltinTypeStr() << "\", \"n\")\n";
1150 }
1151 OS << "#endif\n\n";
1152}
1153
1154void SVEEmitter::createCodeGenMap(raw_ostream &OS) {
1155 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1156 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1157 for (auto *R : RV)
1158 createIntrinsic(R, Defs);
1159
1160 // The mappings must be sorted based on BuiltinID.
1161 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1162 const std::unique_ptr<Intrinsic> &B) {
1163 return A->getMangledName() < B->getMangledName();
1164 });
1165
1166 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n";
1167 for (auto &Def : Defs) {
1168 // Builtins only exist for non-overloaded intrinsics, overloaded
1169 // declarations only live in the header file.
1170 if (Def->getClassKind() == ClassG)
1171 continue;
1172
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001173 uint64_t Flags = Def->getFlags();
Sander de Smalenc5b81462020-03-18 11:07:20 +00001174 auto FlagString = std::to_string(Flags);
1175
1176 std::string LLVMName = Def->getLLVMName();
1177 std::string Builtin = Def->getMangledName();
1178 if (!LLVMName.empty())
1179 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString
1180 << "),\n";
1181 else
1182 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n";
1183 }
1184 OS << "#endif\n\n";
1185}
1186
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001187void SVEEmitter::createRangeChecks(raw_ostream &OS) {
1188 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1189 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1190 for (auto *R : RV)
1191 createIntrinsic(R, Defs);
1192
1193 // The mappings must be sorted based on BuiltinID.
1194 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1195 const std::unique_ptr<Intrinsic> &B) {
1196 return A->getMangledName() < B->getMangledName();
1197 });
1198
1199
1200 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n";
1201
1202 // Ensure these are only emitted once.
1203 std::set<std::string> Emitted;
1204
1205 for (auto &Def : Defs) {
1206 if (Emitted.find(Def->getMangledName()) != Emitted.end() ||
1207 Def->getImmChecks().empty())
1208 continue;
1209
1210 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n";
1211 for (auto &Check : Def->getImmChecks())
1212 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", "
1213 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n";
1214 OS << " break;\n";
1215
1216 Emitted.insert(Def->getMangledName());
1217 }
1218
1219 OS << "#endif\n\n";
1220}
1221
Sander de Smalenc5b81462020-03-18 11:07:20 +00001222/// Create the SVETypeFlags used in CGBuiltins
1223void SVEEmitter::createTypeFlags(raw_ostream &OS) {
1224 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n";
1225 for (auto &KV : FlagTypes)
1226 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n";
1227 OS << "#endif\n\n";
1228
1229 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n";
1230 for (auto &KV : EltTypes)
1231 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1232 OS << "#endif\n\n";
1233
1234 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n";
1235 for (auto &KV : MemEltTypes)
1236 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1237 OS << "#endif\n\n";
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001238
1239 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n";
1240 for (auto &KV : MergeTypes)
1241 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1242 OS << "#endif\n\n";
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001243
1244 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n";
1245 for (auto &KV : ImmCheckTypes)
1246 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1247 OS << "#endif\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001248}
1249
Sander de Smalen5087ace2020-03-15 14:29:45 +00001250namespace clang {
1251void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) {
Sander de Smalenc5b81462020-03-18 11:07:20 +00001252 SVEEmitter(Records).createHeader(OS);
1253}
1254
1255void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1256 SVEEmitter(Records).createBuiltins(OS);
1257}
1258
1259void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
1260 SVEEmitter(Records).createCodeGenMap(OS);
1261}
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001262
1263void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) {
1264 SVEEmitter(Records).createRangeChecks(OS);
1265}
1266
Sander de Smalenc5b81462020-03-18 11:07:20 +00001267void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) {
1268 SVEEmitter(Records).createTypeFlags(OS);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001269}
1270
1271} // End namespace clang