blob: bdaeac7ef55fa712c684668011dae72da683ee8a [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; }
97 bool isScalarPredicate() const { return !Float && ElementBitwidth == 1; }
98 bool isPredicateVector() const { return Predicate; }
99 bool isPredicatePattern() const { return PredicatePattern; }
100 bool isPrefetchOp() const { return PrefetchOp; }
101 bool isConstant() const { return Constant; }
102 unsigned getElementSizeInBits() const { return ElementBitwidth; }
103 unsigned getNumVectors() const { return NumVectors; }
104
105 unsigned getNumElements() const {
106 assert(ElementBitwidth != ~0U);
107 return Bitwidth / ElementBitwidth;
108 }
109 unsigned getSizeInBits() const {
110 return Bitwidth;
111 }
112
113 /// Return the string representation of a type, which is an encoded
114 /// string for passing to the BUILTIN() macro in Builtins.def.
115 std::string builtin_str() const;
116
Sander de Smalen981f0802020-03-18 15:05:08 +0000117 /// Return the C/C++ string representation of a type for use in the
118 /// arm_sve.h header file.
119 std::string str() const;
120
Sander de Smalenc5b81462020-03-18 11:07:20 +0000121private:
122 /// Creates the type based on the typespec string in TS.
123 void applyTypespec();
124
125 /// Applies a prototype modifier to the type.
126 void applyModifier(char Mod);
127};
128
129
130class SVEEmitter;
131
132/// The main grunt class. This represents an instantiation of an intrinsic with
133/// a particular typespec and prototype.
134class Intrinsic {
135 /// The unmangled name.
136 std::string Name;
137
138 /// The name of the corresponding LLVM IR intrinsic.
139 std::string LLVMName;
140
141 /// Intrinsic prototype.
142 std::string Proto;
143
144 /// The base type spec for this intrinsic.
145 TypeSpec BaseTypeSpec;
146
147 /// The base class kind. Most intrinsics use ClassS, which has full type
148 /// info for integers (_s32/_u32), or ClassG which is used for overloaded
149 /// intrinsics.
150 ClassKind Class;
151
152 /// The architectural #ifdef guard.
153 std::string Guard;
154
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100155 // The merge suffix such as _m, _x or _z.
156 std::string MergeSuffix;
157
Sander de Smalenc5b81462020-03-18 11:07:20 +0000158 /// The types of return value [0] and parameters [1..].
159 std::vector<SVEType> Types;
160
161 /// The "base type", which is VarType('d', BaseTypeSpec).
162 SVEType BaseType;
163
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100164 uint64_t Flags;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000165
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100166 SmallVector<ImmCheck, 2> ImmChecks;
167
Sander de Smalenc5b81462020-03-18 11:07:20 +0000168public:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100169 Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
170 StringRef MergeSuffix, uint64_t MemoryElementTy, StringRef LLVMName,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100171 uint64_t Flags, ArrayRef<ImmCheck> ImmChecks, TypeSpec BT,
172 ClassKind Class, SVEEmitter &Emitter, StringRef Guard);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000173
174 ~Intrinsic()=default;
175
176 std::string getName() const { return Name; }
177 std::string getLLVMName() const { return LLVMName; }
178 std::string getProto() const { return Proto; }
179 TypeSpec getBaseTypeSpec() const { return BaseTypeSpec; }
180 SVEType getBaseType() const { return BaseType; }
181
182 StringRef getGuard() const { return Guard; }
183 ClassKind getClassKind() const { return Class; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000184
185 SVEType getReturnType() const { return Types[0]; }
186 ArrayRef<SVEType> getTypes() const { return Types; }
187 SVEType getParamType(unsigned I) const { return Types[I + 1]; }
188 unsigned getNumParams() const { return Proto.size() - 1; }
189
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100190 uint64_t getFlags() const { return Flags; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000191 bool isFlagSet(uint64_t Flag) const { return Flags & Flag;}
192
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100193 ArrayRef<ImmCheck> getImmChecks() const { return ImmChecks; }
194
Sander de Smalenc5b81462020-03-18 11:07:20 +0000195 /// Return the type string for a BUILTIN() macro in Builtins.def.
196 std::string getBuiltinTypeStr();
197
198 /// Return the name, mangled with type information. The name is mangled for
199 /// ClassS, so will add type suffixes such as _u32/_s32.
200 std::string getMangledName() const { return mangleName(ClassS); }
201
202 /// Returns true if the intrinsic is overloaded, in that it should also generate
203 /// a short form without the type-specifiers, e.g. 'svld1(..)' instead of
204 /// 'svld1_u32(..)'.
205 static bool isOverloadedIntrinsic(StringRef Name) {
206 auto BrOpen = Name.find("[");
207 auto BrClose = Name.find(']');
208 return BrOpen != std::string::npos && BrClose != std::string::npos;
209 }
210
Sander de Smalen41d52662020-04-22 13:58:35 +0100211 /// Return true if the intrinsic takes a splat operand.
212 bool hasSplat() const {
213 // These prototype modifiers are described in arm_sve.td.
214 return Proto.find_first_of("ajfrKLR") != std::string::npos;
215 }
216
217 /// Return the parameter index of the splat operand.
218 unsigned getSplatIdx() const {
219 // These prototype modifiers are described in arm_sve.td.
220 auto Idx = Proto.find_first_of("ajfrKLR");
221 assert(Idx != std::string::npos && Idx > 0 &&
222 "Prototype has no splat operand");
223 return Idx - 1;
224 }
225
Sander de Smalenc5b81462020-03-18 11:07:20 +0000226 /// Emits the intrinsic declaration to the ostream.
227 void emitIntrinsic(raw_ostream &OS) const;
228
229private:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100230 std::string getMergeSuffix() const { return MergeSuffix; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000231 std::string mangleName(ClassKind LocalCK) const;
232 std::string replaceTemplatedArgs(std::string Name, TypeSpec TS,
233 std::string Proto) const;
234};
235
236class SVEEmitter {
237private:
238 RecordKeeper &Records;
239 llvm::StringMap<uint64_t> EltTypes;
240 llvm::StringMap<uint64_t> MemEltTypes;
241 llvm::StringMap<uint64_t> FlagTypes;
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100242 llvm::StringMap<uint64_t> MergeTypes;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100243 llvm::StringMap<uint64_t> ImmCheckTypes;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000244
Sander de Smalenc5b81462020-03-18 11:07:20 +0000245public:
246 SVEEmitter(RecordKeeper &R) : Records(R) {
247 for (auto *RV : Records.getAllDerivedDefinitions("EltType"))
248 EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
249 for (auto *RV : Records.getAllDerivedDefinitions("MemEltType"))
250 MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
251 for (auto *RV : Records.getAllDerivedDefinitions("FlagType"))
252 FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100253 for (auto *RV : Records.getAllDerivedDefinitions("MergeType"))
254 MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100255 for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType"))
256 ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
257 }
258
259 /// Returns the enum value for the immcheck type
260 unsigned getEnumValueForImmCheck(StringRef C) const {
261 auto It = ImmCheckTypes.find(C);
262 if (It != ImmCheckTypes.end())
263 return It->getValue();
264 llvm_unreachable("Unsupported imm check");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000265 }
266
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100267 /// Returns the enum value for the flag type
268 uint64_t getEnumValueForFlag(StringRef C) const {
269 auto Res = FlagTypes.find(C);
270 if (Res != FlagTypes.end())
271 return Res->getValue();
272 llvm_unreachable("Unsupported flag");
273 }
274
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100275 // Returns the SVETypeFlags for a given value and mask.
276 uint64_t encodeFlag(uint64_t V, StringRef MaskName) const {
277 auto It = FlagTypes.find(MaskName);
278 if (It != FlagTypes.end()) {
279 uint64_t Mask = It->getValue();
280 unsigned Shift = llvm::countTrailingZeros(Mask);
281 return (V << Shift) & Mask;
282 }
283 llvm_unreachable("Unsupported flag");
284 }
285
286 // Returns the SVETypeFlags for the given element type.
287 uint64_t encodeEltType(StringRef EltName) {
288 auto It = EltTypes.find(EltName);
289 if (It != EltTypes.end())
290 return encodeFlag(It->getValue(), "EltTypeMask");
291 llvm_unreachable("Unsupported EltType");
292 }
293
294 // Returns the SVETypeFlags for the given memory element type.
295 uint64_t encodeMemoryElementType(uint64_t MT) {
296 return encodeFlag(MT, "MemEltTypeMask");
297 }
298
299 // Returns the SVETypeFlags for the given merge type.
300 uint64_t encodeMergeType(uint64_t MT) {
301 return encodeFlag(MT, "MergeTypeMask");
302 }
303
Sander de Smalen41d52662020-04-22 13:58:35 +0100304 // Returns the SVETypeFlags for the given splat operand.
305 unsigned encodeSplatOperand(unsigned SplatIdx) {
306 assert(SplatIdx < 7 && "SplatIdx out of encodable range");
307 return encodeFlag(SplatIdx + 1, "SplatOperandMask");
308 }
309
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100310 // Returns the SVETypeFlags value for the given SVEType.
311 uint64_t encodeTypeFlags(const SVEType &T);
312
Sander de Smalenc5b81462020-03-18 11:07:20 +0000313 /// Emit arm_sve.h.
314 void createHeader(raw_ostream &o);
315
316 /// Emit all the __builtin prototypes and code needed by Sema.
317 void createBuiltins(raw_ostream &o);
318
319 /// Emit all the information needed to map builtin -> LLVM IR intrinsic.
320 void createCodeGenMap(raw_ostream &o);
321
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100322 /// Emit all the range checks for the immediates.
323 void createRangeChecks(raw_ostream &o);
324
Sander de Smalenc5b81462020-03-18 11:07:20 +0000325 /// Create the SVETypeFlags used in CGBuiltins
326 void createTypeFlags(raw_ostream &o);
327
328 /// Create intrinsic and add it to \p Out
329 void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out);
Sander de Smalen5087ace2020-03-15 14:29:45 +0000330};
331
332} // end anonymous namespace
333
334
335//===----------------------------------------------------------------------===//
Sander de Smalenc5b81462020-03-18 11:07:20 +0000336// Type implementation
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000337//===----------------------------------------------------------------------===//
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000338
Sander de Smalenc5b81462020-03-18 11:07:20 +0000339std::string SVEType::builtin_str() const {
340 std::string S;
341 if (isVoid())
342 return "v";
343
344 if (isVoidPointer())
345 S += "v";
346 else if (!Float)
347 switch (ElementBitwidth) {
348 case 1: S += "b"; break;
349 case 8: S += "c"; break;
350 case 16: S += "s"; break;
351 case 32: S += "i"; break;
352 case 64: S += "Wi"; break;
353 case 128: S += "LLLi"; break;
354 default: llvm_unreachable("Unhandled case!");
355 }
356 else
357 switch (ElementBitwidth) {
358 case 16: S += "h"; break;
359 case 32: S += "f"; break;
360 case 64: S += "d"; break;
361 default: llvm_unreachable("Unhandled case!");
362 }
363
364 if (!isFloat()) {
365 if ((isChar() || isPointer()) && !isVoidPointer()) {
366 // Make chars and typed pointers explicitly signed.
367 if (Signed)
368 S = "S" + S;
369 else if (!Signed)
370 S = "U" + S;
371 } else if (!isVoidPointer() && !Signed) {
372 S = "U" + S;
373 }
374 }
375
376 // Constant indices are "int", but have the "constant expression" modifier.
377 if (isImmediate()) {
378 assert(!isFloat() && "fp immediates are not supported");
379 S = "I" + S;
380 }
381
382 if (isScalar()) {
383 if (Constant) S += "C";
384 if (Pointer) S += "*";
385 return S;
386 }
387
388 assert(isScalableVector() && "Unsupported type");
389 return "q" + utostr(getNumElements() * NumVectors) + S;
390}
391
Sander de Smalen981f0802020-03-18 15:05:08 +0000392std::string SVEType::str() const {
393 if (isPredicatePattern())
394 return "sv_pattern";
395
396 if (isPrefetchOp())
397 return "sv_prfop";
398
399 std::string S;
400 if (Void)
401 S += "void";
402 else {
403 if (isScalableVector())
404 S += "sv";
405 if (!Signed && !Float)
406 S += "u";
407
408 if (Float)
409 S += "float";
410 else if (isScalarPredicate())
411 S += "bool";
412 else
413 S += "int";
414
415 if (!isScalarPredicate())
416 S += utostr(ElementBitwidth);
417 if (!isScalableVector() && isVector())
418 S += "x" + utostr(getNumElements());
419 if (NumVectors > 1)
420 S += "x" + utostr(NumVectors);
421 S += "_t";
422 }
423
424 if (Constant)
425 S += " const";
426 if (Pointer)
427 S += " *";
428
429 return S;
430}
Sander de Smalenc5b81462020-03-18 11:07:20 +0000431void SVEType::applyTypespec() {
432 for (char I : TS) {
433 switch (I) {
434 case 'P':
435 Predicate = true;
436 ElementBitwidth = 1;
437 break;
438 case 'U':
439 Signed = false;
440 break;
441 case 'c':
442 ElementBitwidth = 8;
443 break;
444 case 's':
445 ElementBitwidth = 16;
446 break;
447 case 'i':
448 ElementBitwidth = 32;
449 break;
450 case 'l':
451 ElementBitwidth = 64;
452 break;
453 case 'h':
454 Float = true;
455 ElementBitwidth = 16;
456 break;
457 case 'f':
458 Float = true;
459 ElementBitwidth = 32;
460 break;
461 case 'd':
462 Float = true;
463 ElementBitwidth = 64;
464 break;
465 default:
466 llvm_unreachable("Unhandled type code!");
467 }
468 }
469 assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
470}
471
472void SVEType::applyModifier(char Mod) {
473 switch (Mod) {
474 case 'v':
475 Void = true;
476 break;
477 case 'd':
478 DefaultType = true;
479 break;
480 case 'c':
481 Constant = true;
482 LLVM_FALLTHROUGH;
483 case 'p':
484 Pointer = true;
485 Bitwidth = ElementBitwidth;
486 NumVectors = 0;
487 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100488 case 'e':
489 Signed = false;
490 ElementBitwidth /= 2;
491 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100492 case 'h':
493 ElementBitwidth /= 2;
494 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100495 case 'q':
496 ElementBitwidth /= 4;
497 break;
498 case 'o':
499 ElementBitwidth *= 4;
500 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000501 case 'P':
502 Signed = true;
503 Float = false;
504 Predicate = true;
505 Bitwidth = 16;
506 ElementBitwidth = 1;
507 break;
Sander de Smalen03f419f2020-04-26 12:47:17 +0100508 case 's':
Sander de Smalen41d52662020-04-22 13:58:35 +0100509 case 'a':
510 Bitwidth = ElementBitwidth;
511 NumVectors = 0;
512 break;
Sander de Smalen1a720d42020-05-01 17:34:42 +0100513 case 'K':
514 Signed = true;
515 Float = false;
516 Bitwidth = ElementBitwidth;
517 NumVectors = 0;
518 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100519 case 'u':
520 Predicate = false;
521 Signed = false;
522 Float = false;
523 break;
Andrzej Warzynski72f56582020-04-07 11:09:01 +0100524 case 'x':
525 Predicate = false;
526 Signed = true;
527 Float = false;
528 break;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100529 case 'i':
530 Predicate = false;
531 Float = false;
532 ElementBitwidth = Bitwidth = 64;
533 NumVectors = 0;
534 Signed = false;
535 Immediate = true;
536 break;
537 case 'I':
538 Predicate = false;
539 Float = false;
540 ElementBitwidth = Bitwidth = 32;
541 NumVectors = 0;
542 Signed = true;
543 Immediate = true;
544 PredicatePattern = true;
545 break;
Sander de Smalen823e2a62020-04-24 11:31:34 +0100546 case 'J':
547 Predicate = false;
548 Float = false;
549 ElementBitwidth = Bitwidth = 32;
550 NumVectors = 0;
551 Signed = true;
552 Immediate = true;
553 PrefetchOp = true;
554 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100555 case 'k':
556 Predicate = false;
557 Signed = true;
558 Float = false;
559 ElementBitwidth = Bitwidth = 32;
560 NumVectors = 0;
561 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100562 case 'l':
563 Predicate = false;
564 Signed = true;
565 Float = false;
566 ElementBitwidth = Bitwidth = 64;
567 NumVectors = 0;
568 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100569 case 'm':
570 Predicate = false;
571 Signed = false;
572 Float = false;
573 ElementBitwidth = Bitwidth = 32;
574 NumVectors = 0;
575 break;
576 case 'n':
577 Predicate = false;
578 Signed = false;
579 Float = false;
580 ElementBitwidth = Bitwidth = 64;
581 NumVectors = 0;
582 break;
Sander de Smalen0ddb2032020-04-24 11:31:34 +0100583 case 'w':
584 ElementBitwidth = 64;
585 break;
586 case 'j':
587 ElementBitwidth = Bitwidth = 64;
588 NumVectors = 0;
589 break;
Sander de Smalena5e03892020-04-23 10:53:23 +0100590 case 't':
591 Signed = true;
592 Float = false;
593 ElementBitwidth = 32;
594 break;
595 case 'z':
596 Signed = false;
597 Float = false;
598 ElementBitwidth = 32;
599 break;
Sander de Smalen00216442020-04-23 10:45:13 +0100600 case 'O':
601 Predicate = false;
602 Float = true;
603 ElementBitwidth = 16;
604 break;
605 case 'M':
606 Predicate = false;
607 Float = true;
608 ElementBitwidth = 32;
609 break;
610 case 'N':
611 Predicate = false;
612 Float = true;
613 ElementBitwidth = 64;
614 break;
Sander de Smalen42a56bf2020-04-29 11:36:41 +0100615 case 'Q':
616 Constant = true;
617 Pointer = true;
618 Void = true;
619 NumVectors = 0;
620 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100621 case 'S':
622 Constant = true;
623 Pointer = true;
624 ElementBitwidth = Bitwidth = 8;
625 NumVectors = 0;
626 Signed = true;
627 break;
628 case 'W':
629 Constant = true;
630 Pointer = true;
631 ElementBitwidth = Bitwidth = 8;
632 NumVectors = 0;
633 Signed = false;
634 break;
635 case 'T':
636 Constant = true;
637 Pointer = true;
638 ElementBitwidth = Bitwidth = 16;
639 NumVectors = 0;
640 Signed = true;
641 break;
642 case 'X':
643 Constant = true;
644 Pointer = true;
645 ElementBitwidth = Bitwidth = 16;
646 NumVectors = 0;
647 Signed = false;
648 break;
649 case 'Y':
650 Constant = true;
651 Pointer = true;
652 ElementBitwidth = Bitwidth = 32;
653 NumVectors = 0;
654 Signed = false;
655 break;
656 case 'U':
657 Constant = true;
658 Pointer = true;
659 ElementBitwidth = Bitwidth = 32;
660 NumVectors = 0;
661 Signed = true;
662 break;
663 case 'A':
664 Pointer = true;
665 ElementBitwidth = Bitwidth = 8;
666 NumVectors = 0;
667 Signed = true;
668 break;
669 case 'B':
670 Pointer = true;
671 ElementBitwidth = Bitwidth = 16;
672 NumVectors = 0;
673 Signed = true;
674 break;
675 case 'C':
676 Pointer = true;
677 ElementBitwidth = Bitwidth = 32;
678 NumVectors = 0;
679 Signed = true;
680 break;
681 case 'D':
682 Pointer = true;
683 ElementBitwidth = Bitwidth = 64;
684 NumVectors = 0;
685 Signed = true;
686 break;
687 case 'E':
688 Pointer = true;
689 ElementBitwidth = Bitwidth = 8;
690 NumVectors = 0;
691 Signed = false;
692 break;
693 case 'F':
694 Pointer = true;
695 ElementBitwidth = Bitwidth = 16;
696 NumVectors = 0;
697 Signed = false;
698 break;
699 case 'G':
700 Pointer = true;
701 ElementBitwidth = Bitwidth = 32;
702 NumVectors = 0;
703 Signed = false;
704 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000705 default:
706 llvm_unreachable("Unhandled character!");
707 }
708}
709
710
711//===----------------------------------------------------------------------===//
712// Intrinsic implementation
713//===----------------------------------------------------------------------===//
714
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100715Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
716 StringRef MergeSuffix, uint64_t MemoryElementTy,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100717 StringRef LLVMName, uint64_t Flags,
718 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class,
719 SVEEmitter &Emitter, StringRef Guard)
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100720 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()),
721 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()),
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100722 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags),
723 ImmChecks(Checks.begin(), Checks.end()) {
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100724
725 // Types[0] is the return value.
726 for (unsigned I = 0; I < Proto.size(); ++I) {
727 SVEType T(BaseTypeSpec, Proto[I]);
728 Types.push_back(T);
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100729
730 // Add range checks for immediates
731 if (I > 0) {
732 if (T.isPredicatePattern())
733 ImmChecks.emplace_back(
734 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31"));
Sander de Smalen823e2a62020-04-24 11:31:34 +0100735 else if (T.isPrefetchOp())
736 ImmChecks.emplace_back(
737 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13"));
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100738 }
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100739 }
740
741 // Set flags based on properties
742 this->Flags |= Emitter.encodeTypeFlags(BaseType);
743 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy);
744 this->Flags |= Emitter.encodeMergeType(MergeTy);
Sander de Smalen41d52662020-04-22 13:58:35 +0100745 if (hasSplat())
746 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx());
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100747}
748
Sander de Smalenc5b81462020-03-18 11:07:20 +0000749std::string Intrinsic::getBuiltinTypeStr() {
750 std::string S;
751
752 SVEType RetT = getReturnType();
753 // Since the return value must be one type, return a vector type of the
754 // appropriate width which we will bitcast. An exception is made for
755 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
756 // fashion, storing them to a pointer arg.
757 if (RetT.getNumVectors() > 1) {
758 S += "vv*"; // void result with void* first argument
759 } else
760 S += RetT.builtin_str();
761
762 for (unsigned I = 0; I < getNumParams(); ++I)
763 S += getParamType(I).builtin_str();
764
765 return S;
766}
767
768std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS,
769 std::string Proto) const {
770 std::string Ret = Name;
771 while (Ret.find('{') != std::string::npos) {
772 size_t Pos = Ret.find('{');
773 size_t End = Ret.find('}');
774 unsigned NumChars = End - Pos + 1;
775 assert(NumChars == 3 && "Unexpected template argument");
776
777 SVEType T;
778 char C = Ret[Pos+1];
779 switch(C) {
780 default:
781 llvm_unreachable("Unknown predication specifier");
782 case 'd':
783 T = SVEType(TS, 'd');
784 break;
785 case '0':
786 case '1':
787 case '2':
788 case '3':
789 T = SVEType(TS, Proto[C - '0']);
790 break;
791 }
792
793 // Replace templated arg with the right suffix (e.g. u32)
794 std::string TypeCode;
795 if (T.isInteger())
796 TypeCode = T.isSigned() ? 's' : 'u';
797 else if (T.isPredicateVector())
798 TypeCode = 'b';
799 else
800 TypeCode = 'f';
801 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits()));
802 }
803
804 return Ret;
805}
806
Sander de Smalenc5b81462020-03-18 11:07:20 +0000807std::string Intrinsic::mangleName(ClassKind LocalCK) const {
808 std::string S = getName();
809
810 if (LocalCK == ClassG) {
811 // Remove the square brackets and everything in between.
812 while (S.find("[") != std::string::npos) {
813 auto Start = S.find("[");
814 auto End = S.find(']');
815 S.erase(Start, (End-Start)+1);
816 }
817 } else {
818 // Remove the square brackets.
819 while (S.find("[") != std::string::npos) {
820 auto BrPos = S.find('[');
821 if (BrPos != std::string::npos)
822 S.erase(BrPos, 1);
823 BrPos = S.find(']');
824 if (BrPos != std::string::npos)
825 S.erase(BrPos, 1);
826 }
827 }
828
829 // Replace all {d} like expressions with e.g. 'u32'
830 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) +
831 getMergeSuffix();
832}
833
834void Intrinsic::emitIntrinsic(raw_ostream &OS) const {
835 // Use the preprocessor to
836 if (getClassKind() != ClassG || getProto().size() <= 1) {
837 OS << "#define " << mangleName(getClassKind())
838 << "(...) __builtin_sve_" << mangleName(ClassS)
839 << "(__VA_ARGS__)\n";
840 } else {
Sander de Smalen981f0802020-03-18 15:05:08 +0000841 std::string FullName = mangleName(ClassS);
842 std::string ProtoName = mangleName(ClassG);
843
844 OS << "__aio __attribute__((__clang_arm_builtin_alias("
845 << "__builtin_sve_" << FullName << ")))\n";
846
847 OS << getTypes()[0].str() << " " << ProtoName << "(";
848 for (unsigned I = 0; I < getTypes().size() - 1; ++I) {
849 if (I != 0)
850 OS << ", ";
851 OS << getTypes()[I + 1].str();
852 }
853 OS << ");\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000854 }
855}
856
857//===----------------------------------------------------------------------===//
858// SVEEmitter implementation
859//===----------------------------------------------------------------------===//
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100860uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) {
861 if (T.isFloat()) {
862 switch (T.getElementSizeInBits()) {
863 case 16:
864 return encodeEltType("EltTyFloat16");
865 case 32:
866 return encodeEltType("EltTyFloat32");
867 case 64:
868 return encodeEltType("EltTyFloat64");
869 default:
870 llvm_unreachable("Unhandled float element bitwidth!");
871 }
872 }
873
874 if (T.isPredicateVector()) {
875 switch (T.getElementSizeInBits()) {
876 case 8:
877 return encodeEltType("EltTyBool8");
878 case 16:
879 return encodeEltType("EltTyBool16");
880 case 32:
881 return encodeEltType("EltTyBool32");
882 case 64:
883 return encodeEltType("EltTyBool64");
884 default:
885 llvm_unreachable("Unhandled predicate element bitwidth!");
886 }
887 }
888
889 switch (T.getElementSizeInBits()) {
890 case 8:
891 return encodeEltType("EltTyInt8");
892 case 16:
893 return encodeEltType("EltTyInt16");
894 case 32:
895 return encodeEltType("EltTyInt32");
896 case 64:
897 return encodeEltType("EltTyInt64");
898 default:
899 llvm_unreachable("Unhandled integer element bitwidth!");
900 }
901}
902
Sander de Smalenc5b81462020-03-18 11:07:20 +0000903void SVEEmitter::createIntrinsic(
904 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) {
905 StringRef Name = R->getValueAsString("Name");
906 StringRef Proto = R->getValueAsString("Prototype");
907 StringRef Types = R->getValueAsString("Types");
908 StringRef Guard = R->getValueAsString("ArchGuard");
909 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100910 uint64_t Merge = R->getValueAsInt("Merge");
911 StringRef MergeSuffix = R->getValueAsString("MergeSuffix");
912 uint64_t MemEltType = R->getValueAsInt("MemEltType");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000913 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100914 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000915
916 int64_t Flags = 0;
917 for (auto FlagRec : FlagsList)
918 Flags |= FlagRec->getValueAsInt("Value");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000919
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100920 // Create a dummy TypeSpec for non-overloaded builtins.
921 if (Types.empty()) {
922 assert((Flags & getEnumValueForFlag("IsOverloadNone")) &&
923 "Expect TypeSpec for overloaded builtin!");
924 Types = "i";
925 }
926
Sander de Smalenc5b81462020-03-18 11:07:20 +0000927 // Extract type specs from string
928 SmallVector<TypeSpec, 8> TypeSpecs;
929 TypeSpec Acc;
930 for (char I : Types) {
931 Acc.push_back(I);
932 if (islower(I)) {
933 TypeSpecs.push_back(TypeSpec(Acc));
934 Acc.clear();
935 }
936 }
937
938 // Remove duplicate type specs.
Benjamin Kramer4065e922020-03-28 19:19:55 +0100939 llvm::sort(TypeSpecs);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000940 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()),
941 TypeSpecs.end());
942
943 // Create an Intrinsic for each type spec.
944 for (auto TS : TypeSpecs) {
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100945 // Collate a list of range/option checks for the immediates.
946 SmallVector<ImmCheck, 2> ImmChecks;
947 for (auto *R : ImmCheckList) {
Christopher Tetreault464a0692020-04-15 15:16:17 -0700948 int64_t Arg = R->getValueAsInt("Arg");
949 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg");
950 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value");
951 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100952
953 unsigned ElementSizeInBits = 0;
954 if (EltSizeArg >= 0)
955 ElementSizeInBits =
956 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1])
957 .getElementSizeInBits();
958 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits));
959 }
960
961 Out.push_back(std::make_unique<Intrinsic>(
962 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks,
963 TS, ClassS, *this, Guard));
Sander de Smalen981f0802020-03-18 15:05:08 +0000964
965 // Also generate the short-form (e.g. svadd_m) for the given type-spec.
966 if (Intrinsic::isOverloadedIntrinsic(Name))
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100967 Out.push_back(std::make_unique<Intrinsic>(
968 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags,
969 ImmChecks, TS, ClassG, *this, Guard));
Sander de Smalenc5b81462020-03-18 11:07:20 +0000970 }
971}
972
973void SVEEmitter::createHeader(raw_ostream &OS) {
Sander de Smalen5087ace2020-03-15 14:29:45 +0000974 OS << "/*===---- arm_sve.h - ARM SVE intrinsics "
975 "-----------------------------------===\n"
976 " *\n"
977 " *\n"
978 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
979 "Exceptions.\n"
980 " * See https://llvm.org/LICENSE.txt for license information.\n"
981 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
982 " *\n"
983 " *===-----------------------------------------------------------------"
984 "------===\n"
985 " */\n\n";
986
987 OS << "#ifndef __ARM_SVE_H\n";
988 OS << "#define __ARM_SVE_H\n\n";
989
990 OS << "#if !defined(__ARM_FEATURE_SVE)\n";
991 OS << "#error \"SVE support not enabled\"\n";
992 OS << "#else\n\n";
993
994 OS << "#include <stdint.h>\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000995 OS << "#ifdef __cplusplus\n";
996 OS << "extern \"C\" {\n";
997 OS << "#else\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +0000998 OS << "#include <stdbool.h>\n";
999 OS << "#endif\n\n";
1000
1001 OS << "typedef __fp16 float16_t;\n";
1002 OS << "typedef float float32_t;\n";
1003 OS << "typedef double float64_t;\n";
1004 OS << "typedef bool bool_t;\n\n";
1005
1006 OS << "typedef __SVInt8_t svint8_t;\n";
1007 OS << "typedef __SVInt16_t svint16_t;\n";
1008 OS << "typedef __SVInt32_t svint32_t;\n";
1009 OS << "typedef __SVInt64_t svint64_t;\n";
1010 OS << "typedef __SVUint8_t svuint8_t;\n";
1011 OS << "typedef __SVUint16_t svuint16_t;\n";
1012 OS << "typedef __SVUint32_t svuint32_t;\n";
1013 OS << "typedef __SVUint64_t svuint64_t;\n";
1014 OS << "typedef __SVFloat16_t svfloat16_t;\n";
1015 OS << "typedef __SVFloat32_t svfloat32_t;\n";
1016 OS << "typedef __SVFloat64_t svfloat64_t;\n";
1017 OS << "typedef __SVBool_t svbool_t;\n\n";
1018
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001019 OS << "typedef enum\n";
1020 OS << "{\n";
1021 OS << " SV_POW2 = 0,\n";
1022 OS << " SV_VL1 = 1,\n";
1023 OS << " SV_VL2 = 2,\n";
1024 OS << " SV_VL3 = 3,\n";
1025 OS << " SV_VL4 = 4,\n";
1026 OS << " SV_VL5 = 5,\n";
1027 OS << " SV_VL6 = 6,\n";
1028 OS << " SV_VL7 = 7,\n";
1029 OS << " SV_VL8 = 8,\n";
1030 OS << " SV_VL16 = 9,\n";
1031 OS << " SV_VL32 = 10,\n";
1032 OS << " SV_VL64 = 11,\n";
1033 OS << " SV_VL128 = 12,\n";
1034 OS << " SV_VL256 = 13,\n";
1035 OS << " SV_MUL4 = 29,\n";
1036 OS << " SV_MUL3 = 30,\n";
1037 OS << " SV_ALL = 31\n";
1038 OS << "} sv_pattern;\n\n";
1039
Sander de Smalen823e2a62020-04-24 11:31:34 +01001040 OS << "typedef enum\n";
1041 OS << "{\n";
1042 OS << " SV_PLDL1KEEP = 0,\n";
1043 OS << " SV_PLDL1STRM = 1,\n";
1044 OS << " SV_PLDL2KEEP = 2,\n";
1045 OS << " SV_PLDL2STRM = 3,\n";
1046 OS << " SV_PLDL3KEEP = 4,\n";
1047 OS << " SV_PLDL3STRM = 5,\n";
1048 OS << " SV_PSTL1KEEP = 8,\n";
1049 OS << " SV_PSTL1STRM = 9,\n";
1050 OS << " SV_PSTL2KEEP = 10,\n";
1051 OS << " SV_PSTL2STRM = 11,\n";
1052 OS << " SV_PSTL3KEEP = 12,\n";
1053 OS << " SV_PSTL3STRM = 13\n";
1054 OS << "} sv_prfop;\n\n";
1055
Sander de Smalen981f0802020-03-18 15:05:08 +00001056 OS << "/* Function attributes */\n";
1057 OS << "#define __aio static inline __attribute__((__always_inline__, "
1058 "__nodebug__, __overloadable__))\n\n";
1059
Sander de Smalenc5b81462020-03-18 11:07:20 +00001060 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1061 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1062 for (auto *R : RV)
1063 createIntrinsic(R, Defs);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001064
Sander de Smalenc5b81462020-03-18 11:07:20 +00001065 // Sort intrinsics in header file by following order/priority:
1066 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES)
1067 // - Class (is intrinsic overloaded or not)
1068 // - Intrinsic name
1069 std::stable_sort(
1070 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A,
1071 const std::unique_ptr<Intrinsic> &B) {
Eric Fiselieraf2968e2020-04-16 18:35:31 -04001072 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) {
1073 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName());
1074 };
1075 return ToTuple(A) < ToTuple(B);
Sander de Smalenc5b81462020-03-18 11:07:20 +00001076 });
1077
1078 StringRef InGuard = "";
1079 for (auto &I : Defs) {
1080 // Emit #endif/#if pair if needed.
1081 if (I->getGuard() != InGuard) {
1082 if (!InGuard.empty())
1083 OS << "#endif //" << InGuard << "\n";
1084 InGuard = I->getGuard();
1085 if (!InGuard.empty())
1086 OS << "\n#if " << InGuard << "\n";
1087 }
1088
1089 // Actually emit the intrinsic declaration.
1090 I->emitIntrinsic(OS);
1091 }
1092
1093 if (!InGuard.empty())
1094 OS << "#endif //" << InGuard << "\n";
1095
Sander de Smalen00216442020-04-23 10:45:13 +01001096 OS << "#if defined(__ARM_FEATURE_SVE2)\n";
1097 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n";
1098 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n";
1099 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n";
1100 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n";
1101
1102 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n";
1103 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n";
1104
1105 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n";
1106
Sander de Smalenc5b81462020-03-18 11:07:20 +00001107 OS << "#ifdef __cplusplus\n";
1108 OS << "} // extern \"C\"\n";
1109 OS << "#endif\n\n";
1110 OS << "#endif /*__ARM_FEATURE_SVE */\n\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001111 OS << "#endif /* __ARM_SVE_H */\n";
1112}
1113
Sander de Smalenc5b81462020-03-18 11:07:20 +00001114void SVEEmitter::createBuiltins(raw_ostream &OS) {
1115 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1116 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1117 for (auto *R : RV)
1118 createIntrinsic(R, Defs);
1119
1120 // The mappings must be sorted based on BuiltinID.
1121 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1122 const std::unique_ptr<Intrinsic> &B) {
1123 return A->getMangledName() < B->getMangledName();
1124 });
1125
1126 OS << "#ifdef GET_SVE_BUILTINS\n";
1127 for (auto &Def : Defs) {
1128 // Only create BUILTINs for non-overloaded intrinsics, as overloaded
1129 // declarations only live in the header file.
1130 if (Def->getClassKind() != ClassG)
1131 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \""
1132 << Def->getBuiltinTypeStr() << "\", \"n\")\n";
1133 }
1134 OS << "#endif\n\n";
1135}
1136
1137void SVEEmitter::createCodeGenMap(raw_ostream &OS) {
1138 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1139 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1140 for (auto *R : RV)
1141 createIntrinsic(R, Defs);
1142
1143 // The mappings must be sorted based on BuiltinID.
1144 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1145 const std::unique_ptr<Intrinsic> &B) {
1146 return A->getMangledName() < B->getMangledName();
1147 });
1148
1149 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n";
1150 for (auto &Def : Defs) {
1151 // Builtins only exist for non-overloaded intrinsics, overloaded
1152 // declarations only live in the header file.
1153 if (Def->getClassKind() == ClassG)
1154 continue;
1155
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001156 uint64_t Flags = Def->getFlags();
Sander de Smalenc5b81462020-03-18 11:07:20 +00001157 auto FlagString = std::to_string(Flags);
1158
1159 std::string LLVMName = Def->getLLVMName();
1160 std::string Builtin = Def->getMangledName();
1161 if (!LLVMName.empty())
1162 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString
1163 << "),\n";
1164 else
1165 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n";
1166 }
1167 OS << "#endif\n\n";
1168}
1169
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001170void SVEEmitter::createRangeChecks(raw_ostream &OS) {
1171 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1172 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1173 for (auto *R : RV)
1174 createIntrinsic(R, Defs);
1175
1176 // The mappings must be sorted based on BuiltinID.
1177 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1178 const std::unique_ptr<Intrinsic> &B) {
1179 return A->getMangledName() < B->getMangledName();
1180 });
1181
1182
1183 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n";
1184
1185 // Ensure these are only emitted once.
1186 std::set<std::string> Emitted;
1187
1188 for (auto &Def : Defs) {
1189 if (Emitted.find(Def->getMangledName()) != Emitted.end() ||
1190 Def->getImmChecks().empty())
1191 continue;
1192
1193 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n";
1194 for (auto &Check : Def->getImmChecks())
1195 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", "
1196 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n";
1197 OS << " break;\n";
1198
1199 Emitted.insert(Def->getMangledName());
1200 }
1201
1202 OS << "#endif\n\n";
1203}
1204
Sander de Smalenc5b81462020-03-18 11:07:20 +00001205/// Create the SVETypeFlags used in CGBuiltins
1206void SVEEmitter::createTypeFlags(raw_ostream &OS) {
1207 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n";
1208 for (auto &KV : FlagTypes)
1209 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n";
1210 OS << "#endif\n\n";
1211
1212 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n";
1213 for (auto &KV : EltTypes)
1214 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1215 OS << "#endif\n\n";
1216
1217 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n";
1218 for (auto &KV : MemEltTypes)
1219 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1220 OS << "#endif\n\n";
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001221
1222 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n";
1223 for (auto &KV : MergeTypes)
1224 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1225 OS << "#endif\n\n";
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001226
1227 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n";
1228 for (auto &KV : ImmCheckTypes)
1229 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1230 OS << "#endif\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001231}
1232
Sander de Smalen5087ace2020-03-15 14:29:45 +00001233namespace clang {
1234void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) {
Sander de Smalenc5b81462020-03-18 11:07:20 +00001235 SVEEmitter(Records).createHeader(OS);
1236}
1237
1238void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1239 SVEEmitter(Records).createBuiltins(OS);
1240}
1241
1242void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
1243 SVEEmitter(Records).createCodeGenMap(OS);
1244}
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001245
1246void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) {
1247 SVEEmitter(Records).createRangeChecks(OS);
1248}
1249
Sander de Smalenc5b81462020-03-18 11:07:20 +00001250void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) {
1251 SVEEmitter(Records).createTypeFlags(OS);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001252}
1253
1254} // End namespace clang