blob: 0638a216c386dc54370b34546e9917f15670f991 [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 Smalen3cb8b4c2020-05-07 11:22:39 +0100531 case 'R':
532 ElementBitwidth /= 2;
533 NumVectors = 0;
534 break;
Sander de Smalen1a720d42020-05-01 17:34:42 +0100535 case 'K':
536 Signed = true;
537 Float = false;
538 Bitwidth = ElementBitwidth;
539 NumVectors = 0;
540 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100541 case 'L':
542 Signed = false;
543 Float = false;
544 Bitwidth = ElementBitwidth;
545 NumVectors = 0;
546 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100547 case 'u':
548 Predicate = false;
549 Signed = false;
550 Float = false;
551 break;
Andrzej Warzynski72f56582020-04-07 11:09:01 +0100552 case 'x':
553 Predicate = false;
554 Signed = true;
555 Float = false;
556 break;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100557 case 'i':
558 Predicate = false;
559 Float = false;
560 ElementBitwidth = Bitwidth = 64;
561 NumVectors = 0;
562 Signed = false;
563 Immediate = true;
564 break;
565 case 'I':
566 Predicate = false;
567 Float = false;
568 ElementBitwidth = Bitwidth = 32;
569 NumVectors = 0;
570 Signed = true;
571 Immediate = true;
572 PredicatePattern = true;
573 break;
Sander de Smalen823e2a62020-04-24 11:31:34 +0100574 case 'J':
575 Predicate = false;
576 Float = false;
577 ElementBitwidth = Bitwidth = 32;
578 NumVectors = 0;
579 Signed = true;
580 Immediate = true;
581 PrefetchOp = true;
582 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100583 case 'k':
584 Predicate = false;
585 Signed = true;
586 Float = false;
587 ElementBitwidth = Bitwidth = 32;
588 NumVectors = 0;
589 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100590 case 'l':
591 Predicate = false;
592 Signed = true;
593 Float = false;
594 ElementBitwidth = Bitwidth = 64;
595 NumVectors = 0;
596 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100597 case 'm':
598 Predicate = false;
599 Signed = false;
600 Float = false;
601 ElementBitwidth = Bitwidth = 32;
602 NumVectors = 0;
603 break;
604 case 'n':
605 Predicate = false;
606 Signed = false;
607 Float = false;
608 ElementBitwidth = Bitwidth = 64;
609 NumVectors = 0;
610 break;
Sander de Smalen0ddb2032020-04-24 11:31:34 +0100611 case 'w':
612 ElementBitwidth = 64;
613 break;
614 case 'j':
615 ElementBitwidth = Bitwidth = 64;
616 NumVectors = 0;
617 break;
Sander de Smalen334931f2020-05-01 21:39:16 +0100618 case 'f':
619 Signed = false;
620 ElementBitwidth = Bitwidth = 64;
621 NumVectors = 0;
622 break;
623 case 'g':
624 Signed = false;
625 Float = false;
626 ElementBitwidth = 64;
627 break;
Sander de Smalena5e03892020-04-23 10:53:23 +0100628 case 't':
629 Signed = true;
630 Float = false;
631 ElementBitwidth = 32;
632 break;
633 case 'z':
634 Signed = false;
635 Float = false;
636 ElementBitwidth = 32;
637 break;
Sander de Smalen00216442020-04-23 10:45:13 +0100638 case 'O':
639 Predicate = false;
640 Float = true;
641 ElementBitwidth = 16;
642 break;
643 case 'M':
644 Predicate = false;
645 Float = true;
646 ElementBitwidth = 32;
647 break;
648 case 'N':
649 Predicate = false;
650 Float = true;
651 ElementBitwidth = 64;
652 break;
Sander de Smalen42a56bf2020-04-29 11:36:41 +0100653 case 'Q':
654 Constant = true;
655 Pointer = true;
656 Void = true;
657 NumVectors = 0;
658 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100659 case 'S':
660 Constant = true;
661 Pointer = true;
662 ElementBitwidth = Bitwidth = 8;
663 NumVectors = 0;
664 Signed = true;
665 break;
666 case 'W':
667 Constant = true;
668 Pointer = true;
669 ElementBitwidth = Bitwidth = 8;
670 NumVectors = 0;
671 Signed = false;
672 break;
673 case 'T':
674 Constant = true;
675 Pointer = true;
676 ElementBitwidth = Bitwidth = 16;
677 NumVectors = 0;
678 Signed = true;
679 break;
680 case 'X':
681 Constant = true;
682 Pointer = true;
683 ElementBitwidth = Bitwidth = 16;
684 NumVectors = 0;
685 Signed = false;
686 break;
687 case 'Y':
688 Constant = true;
689 Pointer = true;
690 ElementBitwidth = Bitwidth = 32;
691 NumVectors = 0;
692 Signed = false;
693 break;
694 case 'U':
695 Constant = true;
696 Pointer = true;
697 ElementBitwidth = Bitwidth = 32;
698 NumVectors = 0;
699 Signed = true;
700 break;
701 case 'A':
702 Pointer = true;
703 ElementBitwidth = Bitwidth = 8;
704 NumVectors = 0;
705 Signed = true;
706 break;
707 case 'B':
708 Pointer = true;
709 ElementBitwidth = Bitwidth = 16;
710 NumVectors = 0;
711 Signed = true;
712 break;
713 case 'C':
714 Pointer = true;
715 ElementBitwidth = Bitwidth = 32;
716 NumVectors = 0;
717 Signed = true;
718 break;
719 case 'D':
720 Pointer = true;
721 ElementBitwidth = Bitwidth = 64;
722 NumVectors = 0;
723 Signed = true;
724 break;
725 case 'E':
726 Pointer = true;
727 ElementBitwidth = Bitwidth = 8;
728 NumVectors = 0;
729 Signed = false;
730 break;
731 case 'F':
732 Pointer = true;
733 ElementBitwidth = Bitwidth = 16;
734 NumVectors = 0;
735 Signed = false;
736 break;
737 case 'G':
738 Pointer = true;
739 ElementBitwidth = Bitwidth = 32;
740 NumVectors = 0;
741 Signed = false;
742 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000743 default:
744 llvm_unreachable("Unhandled character!");
745 }
746}
747
748
749//===----------------------------------------------------------------------===//
750// Intrinsic implementation
751//===----------------------------------------------------------------------===//
752
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100753Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
754 StringRef MergeSuffix, uint64_t MemoryElementTy,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100755 StringRef LLVMName, uint64_t Flags,
756 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class,
757 SVEEmitter &Emitter, StringRef Guard)
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100758 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()),
759 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()),
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100760 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags),
761 ImmChecks(Checks.begin(), Checks.end()) {
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100762
763 // Types[0] is the return value.
764 for (unsigned I = 0; I < Proto.size(); ++I) {
765 SVEType T(BaseTypeSpec, Proto[I]);
766 Types.push_back(T);
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100767
768 // Add range checks for immediates
769 if (I > 0) {
770 if (T.isPredicatePattern())
771 ImmChecks.emplace_back(
772 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31"));
Sander de Smalen823e2a62020-04-24 11:31:34 +0100773 else if (T.isPrefetchOp())
774 ImmChecks.emplace_back(
775 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13"));
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100776 }
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100777 }
778
779 // Set flags based on properties
780 this->Flags |= Emitter.encodeTypeFlags(BaseType);
781 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy);
782 this->Flags |= Emitter.encodeMergeType(MergeTy);
Sander de Smalen41d52662020-04-22 13:58:35 +0100783 if (hasSplat())
784 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx());
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100785}
786
Sander de Smalenc5b81462020-03-18 11:07:20 +0000787std::string Intrinsic::getBuiltinTypeStr() {
788 std::string S;
789
790 SVEType RetT = getReturnType();
791 // Since the return value must be one type, return a vector type of the
792 // appropriate width which we will bitcast. An exception is made for
793 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
794 // fashion, storing them to a pointer arg.
795 if (RetT.getNumVectors() > 1) {
796 S += "vv*"; // void result with void* first argument
797 } else
798 S += RetT.builtin_str();
799
800 for (unsigned I = 0; I < getNumParams(); ++I)
801 S += getParamType(I).builtin_str();
802
803 return S;
804}
805
806std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS,
807 std::string Proto) const {
808 std::string Ret = Name;
809 while (Ret.find('{') != std::string::npos) {
810 size_t Pos = Ret.find('{');
811 size_t End = Ret.find('}');
812 unsigned NumChars = End - Pos + 1;
813 assert(NumChars == 3 && "Unexpected template argument");
814
815 SVEType T;
816 char C = Ret[Pos+1];
817 switch(C) {
818 default:
819 llvm_unreachable("Unknown predication specifier");
820 case 'd':
821 T = SVEType(TS, 'd');
822 break;
823 case '0':
824 case '1':
825 case '2':
826 case '3':
827 T = SVEType(TS, Proto[C - '0']);
828 break;
829 }
830
831 // Replace templated arg with the right suffix (e.g. u32)
832 std::string TypeCode;
833 if (T.isInteger())
834 TypeCode = T.isSigned() ? 's' : 'u';
835 else if (T.isPredicateVector())
836 TypeCode = 'b';
837 else
838 TypeCode = 'f';
839 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits()));
840 }
841
842 return Ret;
843}
844
Sander de Smalenc5b81462020-03-18 11:07:20 +0000845std::string Intrinsic::mangleName(ClassKind LocalCK) const {
846 std::string S = getName();
847
848 if (LocalCK == ClassG) {
849 // Remove the square brackets and everything in between.
850 while (S.find("[") != std::string::npos) {
851 auto Start = S.find("[");
852 auto End = S.find(']');
853 S.erase(Start, (End-Start)+1);
854 }
855 } else {
856 // Remove the square brackets.
857 while (S.find("[") != std::string::npos) {
858 auto BrPos = S.find('[');
859 if (BrPos != std::string::npos)
860 S.erase(BrPos, 1);
861 BrPos = S.find(']');
862 if (BrPos != std::string::npos)
863 S.erase(BrPos, 1);
864 }
865 }
866
867 // Replace all {d} like expressions with e.g. 'u32'
868 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) +
869 getMergeSuffix();
870}
871
872void Intrinsic::emitIntrinsic(raw_ostream &OS) const {
873 // Use the preprocessor to
874 if (getClassKind() != ClassG || getProto().size() <= 1) {
875 OS << "#define " << mangleName(getClassKind())
876 << "(...) __builtin_sve_" << mangleName(ClassS)
877 << "(__VA_ARGS__)\n";
878 } else {
Sander de Smalen981f0802020-03-18 15:05:08 +0000879 std::string FullName = mangleName(ClassS);
880 std::string ProtoName = mangleName(ClassG);
881
882 OS << "__aio __attribute__((__clang_arm_builtin_alias("
883 << "__builtin_sve_" << FullName << ")))\n";
884
885 OS << getTypes()[0].str() << " " << ProtoName << "(";
886 for (unsigned I = 0; I < getTypes().size() - 1; ++I) {
887 if (I != 0)
888 OS << ", ";
889 OS << getTypes()[I + 1].str();
890 }
891 OS << ");\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000892 }
893}
894
895//===----------------------------------------------------------------------===//
896// SVEEmitter implementation
897//===----------------------------------------------------------------------===//
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100898uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) {
899 if (T.isFloat()) {
900 switch (T.getElementSizeInBits()) {
901 case 16:
902 return encodeEltType("EltTyFloat16");
903 case 32:
904 return encodeEltType("EltTyFloat32");
905 case 64:
906 return encodeEltType("EltTyFloat64");
907 default:
908 llvm_unreachable("Unhandled float element bitwidth!");
909 }
910 }
911
912 if (T.isPredicateVector()) {
913 switch (T.getElementSizeInBits()) {
914 case 8:
915 return encodeEltType("EltTyBool8");
916 case 16:
917 return encodeEltType("EltTyBool16");
918 case 32:
919 return encodeEltType("EltTyBool32");
920 case 64:
921 return encodeEltType("EltTyBool64");
922 default:
923 llvm_unreachable("Unhandled predicate element bitwidth!");
924 }
925 }
926
927 switch (T.getElementSizeInBits()) {
928 case 8:
929 return encodeEltType("EltTyInt8");
930 case 16:
931 return encodeEltType("EltTyInt16");
932 case 32:
933 return encodeEltType("EltTyInt32");
934 case 64:
935 return encodeEltType("EltTyInt64");
936 default:
937 llvm_unreachable("Unhandled integer element bitwidth!");
938 }
939}
940
Sander de Smalenc5b81462020-03-18 11:07:20 +0000941void SVEEmitter::createIntrinsic(
942 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) {
943 StringRef Name = R->getValueAsString("Name");
944 StringRef Proto = R->getValueAsString("Prototype");
945 StringRef Types = R->getValueAsString("Types");
946 StringRef Guard = R->getValueAsString("ArchGuard");
947 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100948 uint64_t Merge = R->getValueAsInt("Merge");
949 StringRef MergeSuffix = R->getValueAsString("MergeSuffix");
950 uint64_t MemEltType = R->getValueAsInt("MemEltType");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000951 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100952 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000953
954 int64_t Flags = 0;
955 for (auto FlagRec : FlagsList)
956 Flags |= FlagRec->getValueAsInt("Value");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000957
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100958 // Create a dummy TypeSpec for non-overloaded builtins.
959 if (Types.empty()) {
960 assert((Flags & getEnumValueForFlag("IsOverloadNone")) &&
961 "Expect TypeSpec for overloaded builtin!");
962 Types = "i";
963 }
964
Sander de Smalenc5b81462020-03-18 11:07:20 +0000965 // Extract type specs from string
966 SmallVector<TypeSpec, 8> TypeSpecs;
967 TypeSpec Acc;
968 for (char I : Types) {
969 Acc.push_back(I);
970 if (islower(I)) {
971 TypeSpecs.push_back(TypeSpec(Acc));
972 Acc.clear();
973 }
974 }
975
976 // Remove duplicate type specs.
Benjamin Kramer4065e922020-03-28 19:19:55 +0100977 llvm::sort(TypeSpecs);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000978 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()),
979 TypeSpecs.end());
980
981 // Create an Intrinsic for each type spec.
982 for (auto TS : TypeSpecs) {
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100983 // Collate a list of range/option checks for the immediates.
984 SmallVector<ImmCheck, 2> ImmChecks;
985 for (auto *R : ImmCheckList) {
Christopher Tetreault464a0692020-04-15 15:16:17 -0700986 int64_t Arg = R->getValueAsInt("Arg");
987 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg");
988 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value");
989 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100990
991 unsigned ElementSizeInBits = 0;
992 if (EltSizeArg >= 0)
993 ElementSizeInBits =
994 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1])
995 .getElementSizeInBits();
996 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits));
997 }
998
999 Out.push_back(std::make_unique<Intrinsic>(
1000 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks,
1001 TS, ClassS, *this, Guard));
Sander de Smalen981f0802020-03-18 15:05:08 +00001002
1003 // Also generate the short-form (e.g. svadd_m) for the given type-spec.
1004 if (Intrinsic::isOverloadedIntrinsic(Name))
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001005 Out.push_back(std::make_unique<Intrinsic>(
1006 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags,
1007 ImmChecks, TS, ClassG, *this, Guard));
Sander de Smalenc5b81462020-03-18 11:07:20 +00001008 }
1009}
1010
1011void SVEEmitter::createHeader(raw_ostream &OS) {
Sander de Smalen5087ace2020-03-15 14:29:45 +00001012 OS << "/*===---- arm_sve.h - ARM SVE intrinsics "
1013 "-----------------------------------===\n"
1014 " *\n"
1015 " *\n"
1016 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
1017 "Exceptions.\n"
1018 " * See https://llvm.org/LICENSE.txt for license information.\n"
1019 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
1020 " *\n"
1021 " *===-----------------------------------------------------------------"
1022 "------===\n"
1023 " */\n\n";
1024
1025 OS << "#ifndef __ARM_SVE_H\n";
1026 OS << "#define __ARM_SVE_H\n\n";
1027
1028 OS << "#if !defined(__ARM_FEATURE_SVE)\n";
1029 OS << "#error \"SVE support not enabled\"\n";
1030 OS << "#else\n\n";
1031
Sander de Smalen5ba32902020-05-05 13:04:14 +01001032 OS << "#if !defined(__LITTLE_ENDIAN__)\n";
1033 OS << "#error \"Big endian is currently not supported for arm_sve.h\"\n";
1034 OS << "#endif\n";
1035
Sander de Smalen5087ace2020-03-15 14:29:45 +00001036 OS << "#include <stdint.h>\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001037 OS << "#ifdef __cplusplus\n";
1038 OS << "extern \"C\" {\n";
1039 OS << "#else\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001040 OS << "#include <stdbool.h>\n";
1041 OS << "#endif\n\n";
1042
1043 OS << "typedef __fp16 float16_t;\n";
1044 OS << "typedef float float32_t;\n";
1045 OS << "typedef double float64_t;\n";
1046 OS << "typedef bool bool_t;\n\n";
1047
1048 OS << "typedef __SVInt8_t svint8_t;\n";
1049 OS << "typedef __SVInt16_t svint16_t;\n";
1050 OS << "typedef __SVInt32_t svint32_t;\n";
1051 OS << "typedef __SVInt64_t svint64_t;\n";
1052 OS << "typedef __SVUint8_t svuint8_t;\n";
1053 OS << "typedef __SVUint16_t svuint16_t;\n";
1054 OS << "typedef __SVUint32_t svuint32_t;\n";
1055 OS << "typedef __SVUint64_t svuint64_t;\n";
1056 OS << "typedef __SVFloat16_t svfloat16_t;\n";
1057 OS << "typedef __SVFloat32_t svfloat32_t;\n";
1058 OS << "typedef __SVFloat64_t svfloat64_t;\n";
1059 OS << "typedef __SVBool_t svbool_t;\n\n";
1060
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001061 OS << "typedef enum\n";
1062 OS << "{\n";
1063 OS << " SV_POW2 = 0,\n";
1064 OS << " SV_VL1 = 1,\n";
1065 OS << " SV_VL2 = 2,\n";
1066 OS << " SV_VL3 = 3,\n";
1067 OS << " SV_VL4 = 4,\n";
1068 OS << " SV_VL5 = 5,\n";
1069 OS << " SV_VL6 = 6,\n";
1070 OS << " SV_VL7 = 7,\n";
1071 OS << " SV_VL8 = 8,\n";
1072 OS << " SV_VL16 = 9,\n";
1073 OS << " SV_VL32 = 10,\n";
1074 OS << " SV_VL64 = 11,\n";
1075 OS << " SV_VL128 = 12,\n";
1076 OS << " SV_VL256 = 13,\n";
1077 OS << " SV_MUL4 = 29,\n";
1078 OS << " SV_MUL3 = 30,\n";
1079 OS << " SV_ALL = 31\n";
1080 OS << "} sv_pattern;\n\n";
1081
Sander de Smalen823e2a62020-04-24 11:31:34 +01001082 OS << "typedef enum\n";
1083 OS << "{\n";
1084 OS << " SV_PLDL1KEEP = 0,\n";
1085 OS << " SV_PLDL1STRM = 1,\n";
1086 OS << " SV_PLDL2KEEP = 2,\n";
1087 OS << " SV_PLDL2STRM = 3,\n";
1088 OS << " SV_PLDL3KEEP = 4,\n";
1089 OS << " SV_PLDL3STRM = 5,\n";
1090 OS << " SV_PSTL1KEEP = 8,\n";
1091 OS << " SV_PSTL1STRM = 9,\n";
1092 OS << " SV_PSTL2KEEP = 10,\n";
1093 OS << " SV_PSTL2STRM = 11,\n";
1094 OS << " SV_PSTL3KEEP = 12,\n";
1095 OS << " SV_PSTL3STRM = 13\n";
1096 OS << "} sv_prfop;\n\n";
1097
Sander de Smalen981f0802020-03-18 15:05:08 +00001098 OS << "/* Function attributes */\n";
1099 OS << "#define __aio static inline __attribute__((__always_inline__, "
1100 "__nodebug__, __overloadable__))\n\n";
1101
Sander de Smalen5ba32902020-05-05 13:04:14 +01001102 // Add reinterpret functions.
1103 for (auto ShortForm : { false, true } )
1104 for (const ReinterpretTypeInfo &From : Reinterprets)
1105 for (const ReinterpretTypeInfo &To : Reinterprets) {
1106 if (ShortForm) {
1107 OS << "__aio " << From.Type << " svreinterpret_" << From.Suffix;
1108 OS << "(" << To.Type << " op) {\n";
1109 OS << " return __builtin_sve_reinterpret_" << From.Suffix << "_"
1110 << To.Suffix << "(op);\n";
1111 OS << "}\n\n";
1112 } else
1113 OS << "#define svreinterpret_" << From.Suffix << "_" << To.Suffix
1114 << "(...) __builtin_sve_reinterpret_" << From.Suffix << "_"
1115 << To.Suffix << "(__VA_ARGS__)\n";
1116 }
1117
Sander de Smalenc5b81462020-03-18 11:07:20 +00001118 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1119 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1120 for (auto *R : RV)
1121 createIntrinsic(R, Defs);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001122
Sander de Smalenc5b81462020-03-18 11:07:20 +00001123 // Sort intrinsics in header file by following order/priority:
1124 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES)
1125 // - Class (is intrinsic overloaded or not)
1126 // - Intrinsic name
1127 std::stable_sort(
1128 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A,
1129 const std::unique_ptr<Intrinsic> &B) {
Eric Fiselieraf2968e2020-04-16 18:35:31 -04001130 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) {
1131 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName());
1132 };
1133 return ToTuple(A) < ToTuple(B);
Sander de Smalenc5b81462020-03-18 11:07:20 +00001134 });
1135
1136 StringRef InGuard = "";
1137 for (auto &I : Defs) {
1138 // Emit #endif/#if pair if needed.
1139 if (I->getGuard() != InGuard) {
1140 if (!InGuard.empty())
1141 OS << "#endif //" << InGuard << "\n";
1142 InGuard = I->getGuard();
1143 if (!InGuard.empty())
1144 OS << "\n#if " << InGuard << "\n";
1145 }
1146
1147 // Actually emit the intrinsic declaration.
1148 I->emitIntrinsic(OS);
1149 }
1150
1151 if (!InGuard.empty())
1152 OS << "#endif //" << InGuard << "\n";
1153
Sander de Smalen00216442020-04-23 10:45:13 +01001154 OS << "#if defined(__ARM_FEATURE_SVE2)\n";
1155 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n";
1156 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n";
1157 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n";
1158 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n";
1159
1160 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n";
1161 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n";
1162
1163 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n";
1164
Sander de Smalenc5b81462020-03-18 11:07:20 +00001165 OS << "#ifdef __cplusplus\n";
1166 OS << "} // extern \"C\"\n";
1167 OS << "#endif\n\n";
1168 OS << "#endif /*__ARM_FEATURE_SVE */\n\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001169 OS << "#endif /* __ARM_SVE_H */\n";
1170}
1171
Sander de Smalenc5b81462020-03-18 11:07:20 +00001172void SVEEmitter::createBuiltins(raw_ostream &OS) {
1173 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1174 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1175 for (auto *R : RV)
1176 createIntrinsic(R, Defs);
1177
1178 // The mappings must be sorted based on BuiltinID.
1179 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1180 const std::unique_ptr<Intrinsic> &B) {
1181 return A->getMangledName() < B->getMangledName();
1182 });
1183
1184 OS << "#ifdef GET_SVE_BUILTINS\n";
1185 for (auto &Def : Defs) {
1186 // Only create BUILTINs for non-overloaded intrinsics, as overloaded
1187 // declarations only live in the header file.
1188 if (Def->getClassKind() != ClassG)
1189 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \""
1190 << Def->getBuiltinTypeStr() << "\", \"n\")\n";
1191 }
Sander de Smalen5ba32902020-05-05 13:04:14 +01001192
1193 // Add reinterpret builtins
1194 for (const ReinterpretTypeInfo &From : Reinterprets)
1195 for (const ReinterpretTypeInfo &To : Reinterprets)
1196 OS << "BUILTIN(__builtin_sve_reinterpret_" << From.Suffix << "_"
1197 << To.Suffix << +", \"" << From.BuiltinType << To.BuiltinType
1198 << "\", \"n\")\n";
1199
Sander de Smalenc5b81462020-03-18 11:07:20 +00001200 OS << "#endif\n\n";
Sander de Smalen5ba32902020-05-05 13:04:14 +01001201 }
Sander de Smalenc5b81462020-03-18 11:07:20 +00001202
1203void SVEEmitter::createCodeGenMap(raw_ostream &OS) {
1204 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1205 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1206 for (auto *R : RV)
1207 createIntrinsic(R, Defs);
1208
1209 // The mappings must be sorted based on BuiltinID.
1210 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1211 const std::unique_ptr<Intrinsic> &B) {
1212 return A->getMangledName() < B->getMangledName();
1213 });
1214
1215 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n";
1216 for (auto &Def : Defs) {
1217 // Builtins only exist for non-overloaded intrinsics, overloaded
1218 // declarations only live in the header file.
1219 if (Def->getClassKind() == ClassG)
1220 continue;
1221
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001222 uint64_t Flags = Def->getFlags();
Sander de Smalenc5b81462020-03-18 11:07:20 +00001223 auto FlagString = std::to_string(Flags);
1224
1225 std::string LLVMName = Def->getLLVMName();
1226 std::string Builtin = Def->getMangledName();
1227 if (!LLVMName.empty())
1228 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString
1229 << "),\n";
1230 else
1231 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n";
1232 }
1233 OS << "#endif\n\n";
1234}
1235
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001236void SVEEmitter::createRangeChecks(raw_ostream &OS) {
1237 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1238 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1239 for (auto *R : RV)
1240 createIntrinsic(R, Defs);
1241
1242 // The mappings must be sorted based on BuiltinID.
1243 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1244 const std::unique_ptr<Intrinsic> &B) {
1245 return A->getMangledName() < B->getMangledName();
1246 });
1247
1248
1249 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n";
1250
1251 // Ensure these are only emitted once.
1252 std::set<std::string> Emitted;
1253
1254 for (auto &Def : Defs) {
1255 if (Emitted.find(Def->getMangledName()) != Emitted.end() ||
1256 Def->getImmChecks().empty())
1257 continue;
1258
1259 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n";
1260 for (auto &Check : Def->getImmChecks())
1261 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", "
1262 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n";
1263 OS << " break;\n";
1264
1265 Emitted.insert(Def->getMangledName());
1266 }
1267
1268 OS << "#endif\n\n";
1269}
1270
Sander de Smalenc5b81462020-03-18 11:07:20 +00001271/// Create the SVETypeFlags used in CGBuiltins
1272void SVEEmitter::createTypeFlags(raw_ostream &OS) {
1273 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n";
1274 for (auto &KV : FlagTypes)
1275 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n";
1276 OS << "#endif\n\n";
1277
1278 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n";
1279 for (auto &KV : EltTypes)
1280 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1281 OS << "#endif\n\n";
1282
1283 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n";
1284 for (auto &KV : MemEltTypes)
1285 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1286 OS << "#endif\n\n";
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001287
1288 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n";
1289 for (auto &KV : MergeTypes)
1290 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1291 OS << "#endif\n\n";
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001292
1293 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n";
1294 for (auto &KV : ImmCheckTypes)
1295 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1296 OS << "#endif\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001297}
1298
Sander de Smalen5087ace2020-03-15 14:29:45 +00001299namespace clang {
1300void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) {
Sander de Smalenc5b81462020-03-18 11:07:20 +00001301 SVEEmitter(Records).createHeader(OS);
1302}
1303
1304void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1305 SVEEmitter(Records).createBuiltins(OS);
1306}
1307
1308void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
1309 SVEEmitter(Records).createCodeGenMap(OS);
1310}
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001311
1312void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) {
1313 SVEEmitter(Records).createRangeChecks(OS);
1314}
1315
Sander de Smalenc5b81462020-03-18 11:07:20 +00001316void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) {
1317 SVEEmitter(Records).createTypeFlags(OS);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001318}
1319
1320} // End namespace clang