| 1 |
//===- NeonEmitter.cpp - Generate arm_neon.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_neon.h, which includes |
| 10 |
// a declaration and definition of each function specified by the ARM NEON |
| 11 |
// compiler interface. See ARM document DUI0348B. |
| 12 |
// |
| 13 |
// Each NEON instruction is implemented in terms of 1 or more functions which |
| 14 |
// are suffixed with the element type of the input vectors. Functions may be |
| 15 |
// implemented in terms of generic vector operations such as +, *, -, etc. or |
| 16 |
// by calling a __builtin_-prefixed function which will be handled by clang's |
| 17 |
// CodeGen library. |
| 18 |
// |
| 19 |
// Additional validation code can be generated by this file when runHeader() is |
| 20 |
// called, rather than the normal run() entry point. |
| 21 |
// |
| 22 |
// See also the documentation in include/clang/Basic/arm_neon.td. |
| 23 |
// |
| 24 |
//===----------------------------------------------------------------------===// |
| 25 |
|
| 26 |
#include "TableGenBackends.h" |
| 27 |
#include "llvm/ADT/ArrayRef.h" |
| 28 |
#include "llvm/ADT/DenseMap.h" |
| 29 |
#include "llvm/ADT/STLExtras.h" |
| 30 |
#include "llvm/ADT/SmallVector.h" |
| 31 |
#include "llvm/ADT/StringExtras.h" |
| 32 |
#include "llvm/ADT/StringRef.h" |
| 33 |
#include "llvm/Support/Casting.h" |
| 34 |
#include "llvm/Support/ErrorHandling.h" |
| 35 |
#include "llvm/Support/raw_ostream.h" |
| 36 |
#include "llvm/TableGen/Error.h" |
| 37 |
#include "llvm/TableGen/Record.h" |
| 38 |
#include "llvm/TableGen/SetTheory.h" |
| 39 |
#include <algorithm> |
| 40 |
#include <cassert> |
| 41 |
#include <cctype> |
| 42 |
#include <cstddef> |
| 43 |
#include <cstdint> |
| 44 |
#include <deque> |
| 45 |
#include <map> |
| 46 |
#include <optional> |
| 47 |
#include <set> |
| 48 |
#include <sstream> |
| 49 |
#include <string> |
| 50 |
#include <utility> |
| 51 |
#include <vector> |
| 52 |
|
| 53 |
using namespace llvm; |
| 54 |
|
| 55 |
namespace { |
| 56 |
|
| 57 |
// While globals are generally bad, this one allows us to perform assertions |
| 58 |
// liberally and somehow still trace them back to the def they indirectly |
| 59 |
// came from. |
| 60 |
static Record *CurrentRecord = nullptr; |
| 61 |
static void assert_with_loc(bool Assertion, const std::string &Str) { |
| 62 |
if (!Assertion) { |
| 63 |
if (CurrentRecord) |
| 64 |
PrintFatalError(CurrentRecord->getLoc(), Str); |
| 65 |
else |
| 66 |
PrintFatalError(Str); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
enum ClassKind { |
| 71 |
ClassNone, |
| 72 |
ClassI, // generic integer instruction, e.g., "i8" suffix |
| 73 |
ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix |
| 74 |
ClassW, // width-specific instruction, e.g., "8" suffix |
| 75 |
ClassB, // bitcast arguments with enum argument to specify type |
| 76 |
ClassL, // Logical instructions which are op instructions |
| 77 |
// but we need to not emit any suffix for in our |
| 78 |
// tests. |
| 79 |
ClassNoTest // Instructions which we do not test since they are |
| 80 |
// not TRUE instructions. |
| 81 |
}; |
| 82 |
|
| 83 |
/// NeonTypeFlags - Flags to identify the types for overloaded Neon |
| 84 |
/// builtins. These must be kept in sync with the flags in |
| 85 |
/// include/clang/Basic/TargetBuiltins.h. |
| 86 |
namespace NeonTypeFlags { |
| 87 |
|
| 88 |
enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 }; |
| 89 |
|
| 90 |
enum EltType { |
| 91 |
Int8, |
| 92 |
Int16, |
| 93 |
Int32, |
| 94 |
Int64, |
| 95 |
Poly8, |
| 96 |
Poly16, |
| 97 |
Poly64, |
| 98 |
Poly128, |
| 99 |
Float16, |
| 100 |
Float32, |
| 101 |
Float64, |
| 102 |
BFloat16 |
| 103 |
}; |
| 104 |
|
| 105 |
} // end namespace NeonTypeFlags |
| 106 |
|
| 107 |
class NeonEmitter; |
| 108 |
|
| 109 |
//===----------------------------------------------------------------------===// |
| 110 |
// TypeSpec |
| 111 |
//===----------------------------------------------------------------------===// |
| 112 |
|
| 113 |
/// A TypeSpec is just a simple wrapper around a string, but gets its own type |
| 114 |
/// for strong typing purposes. |
| 115 |
/// |
| 116 |
/// A TypeSpec can be used to create a type. |
| 117 |
class TypeSpec : public std::string { |
| 118 |
public: |
| 119 |
static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) { |
| 120 |
std::vector<TypeSpec> Ret; |
| 121 |
TypeSpec Acc; |
| 122 |
for (char I : Str.str()) { |
| 123 |
if (islower(I)) { |
| 124 |
Acc.push_back(I); |
| 125 |
Ret.push_back(TypeSpec(Acc)); |
| 126 |
Acc.clear(); |
| 127 |
} else { |
| 128 |
Acc.push_back(I); |
| 129 |
} |
| 130 |
} |
| 131 |
return Ret; |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
//===----------------------------------------------------------------------===// |
| 136 |
// Type |
| 137 |
//===----------------------------------------------------------------------===// |
| 138 |
|
| 139 |
/// A Type. Not much more to say here. |
| 140 |
class Type { |
| 141 |
private: |
| 142 |
TypeSpec TS; |
| 143 |
|
| 144 |
enum TypeKind { |
| 145 |
Void, |
| 146 |
Float, |
| 147 |
SInt, |
| 148 |
UInt, |
| 149 |
Poly, |
| 150 |
BFloat16, |
| 151 |
}; |
| 152 |
TypeKind Kind; |
| 153 |
bool Immediate, Constant, Pointer; |
| 154 |
// ScalarForMangling and NoManglingQ are really not suited to live here as |
| 155 |
// they are not related to the type. But they live in the TypeSpec (not the |
| 156 |
// prototype), so this is really the only place to store them. |
| 157 |
bool ScalarForMangling, NoManglingQ; |
| 158 |
unsigned Bitwidth, ElementBitwidth, NumVectors; |
| 159 |
|
| 160 |
public: |
| 161 |
Type() |
| 162 |
: Kind(Void), Immediate(false), Constant(false), |
| 163 |
Pointer(false), ScalarForMangling(false), NoManglingQ(false), |
| 164 |
Bitwidth(0), ElementBitwidth(0), NumVectors(0) {} |
| 165 |
|
| 166 |
Type(TypeSpec TS, StringRef CharMods) |
| 167 |
: TS(std::move(TS)), Kind(Void), Immediate(false), |
| 168 |
Constant(false), Pointer(false), ScalarForMangling(false), |
| 169 |
NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) { |
| 170 |
applyModifiers(CharMods); |
| 171 |
} |
| 172 |
|
| 173 |
/// Returns a type representing "void". |
| 174 |
static Type getVoid() { return Type(); } |
| 175 |
|
| 176 |
bool operator==(const Type &Other) const { return str() == Other.str(); } |
| 177 |
bool operator!=(const Type &Other) const { return !operator==(Other); } |
| 178 |
|
| 179 |
// |
| 180 |
// Query functions |
| 181 |
// |
| 182 |
bool isScalarForMangling() const { return ScalarForMangling; } |
| 183 |
bool noManglingQ() const { return NoManglingQ; } |
| 184 |
|
| 185 |
bool isPointer() const { return Pointer; } |
| 186 |
bool isValue() const { return !isVoid() && !isPointer(); } |
| 187 |
bool isScalar() const { return isValue() && NumVectors == 0; } |
| 188 |
bool isVector() const { return isValue() && NumVectors > 0; } |
| 189 |
bool isConstPointer() const { return Constant; } |
| 190 |
bool isFloating() const { return Kind == Float; } |
| 191 |
bool isInteger() const { return Kind == SInt || Kind == UInt; } |
| 192 |
bool isPoly() const { return Kind == Poly; } |
| 193 |
bool isSigned() const { return Kind == SInt; } |
| 194 |
bool isImmediate() const { return Immediate; } |
| 195 |
bool isFloat() const { return isFloating() && ElementBitwidth == 32; } |
| 196 |
bool isDouble() const { return isFloating() && ElementBitwidth == 64; } |
| 197 |
bool isHalf() const { return isFloating() && ElementBitwidth == 16; } |
| 198 |
bool isChar() const { return ElementBitwidth == 8; } |
| 199 |
bool isShort() const { return isInteger() && ElementBitwidth == 16; } |
| 200 |
bool isInt() const { return isInteger() && ElementBitwidth == 32; } |
| 201 |
bool isLong() const { return isInteger() && ElementBitwidth == 64; } |
| 202 |
bool isVoid() const { return Kind == Void; } |
| 203 |
bool isBFloat16() const { return Kind == BFloat16; } |
| 204 |
unsigned getNumElements() const { return Bitwidth / ElementBitwidth; } |
| 205 |
unsigned getSizeInBits() const { return Bitwidth; } |
| 206 |
unsigned getElementSizeInBits() const { return ElementBitwidth; } |
| 207 |
unsigned getNumVectors() const { return NumVectors; } |
| 208 |
|
| 209 |
// |
| 210 |
// Mutator functions |
| 211 |
// |
| 212 |
void makeUnsigned() { |
| 213 |
assert(!isVoid() && "not a potentially signed type"); |
| 214 |
Kind = UInt; |
| 215 |
} |
| 216 |
void makeSigned() { |
| 217 |
assert(!isVoid() && "not a potentially signed type"); |
| 218 |
Kind = SInt; |
| 219 |
} |
| 220 |
|
| 221 |
void makeInteger(unsigned ElemWidth, bool Sign) { |
| 222 |
assert(!isVoid() && "converting void to int probably not useful"); |
| 223 |
Kind = Sign ? SInt : UInt; |
| 224 |
Immediate = false; |
| 225 |
ElementBitwidth = ElemWidth; |
| 226 |
} |
| 227 |
|
| 228 |
void makeImmediate(unsigned ElemWidth) { |
| 229 |
Kind = SInt; |
| 230 |
Immediate = true; |
| 231 |
ElementBitwidth = ElemWidth; |
| 232 |
} |
| 233 |
|
| 234 |
void makeScalar() { |
| 235 |
Bitwidth = ElementBitwidth; |
| 236 |
NumVectors = 0; |
| 237 |
} |
| 238 |
|
| 239 |
void makeOneVector() { |
| 240 |
assert(isVector()); |
| 241 |
NumVectors = 1; |
| 242 |
} |
| 243 |
|
| 244 |
void make32BitElement() { |
| 245 |
assert_with_loc(Bitwidth > 32, "Not enough bits to make it 32!"); |
| 246 |
ElementBitwidth = 32; |
| 247 |
} |
| 248 |
|
| 249 |
void doubleLanes() { |
| 250 |
assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!"); |
| 251 |
Bitwidth = 128; |
| 252 |
} |
| 253 |
|
| 254 |
void halveLanes() { |
| 255 |
assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!"); |
| 256 |
Bitwidth = 64; |
| 257 |
} |
| 258 |
|
| 259 |
/// Return the C string representation of a type, which is the typename |
| 260 |
/// defined in stdint.h or arm_neon.h. |
| 261 |
std::string str() const; |
| 262 |
|
| 263 |
/// Return the string representation of a type, which is an encoded |
| 264 |
/// string for passing to the BUILTIN() macro in Builtins.def. |
| 265 |
std::string builtin_str() const; |
| 266 |
|
| 267 |
/// Return the value in NeonTypeFlags for this type. |
| 268 |
unsigned getNeonEnum() const; |
| 269 |
|
| 270 |
/// Parse a type from a stdint.h or arm_neon.h typedef name, |
| 271 |
/// for example uint32x2_t or int64_t. |
| 272 |
static Type fromTypedefName(StringRef Name); |
| 273 |
|
| 274 |
private: |
| 275 |
/// Creates the type based on the typespec string in TS. |
| 276 |
/// Sets "Quad" to true if the "Q" or "H" modifiers were |
| 277 |
/// seen. This is needed by applyModifier as some modifiers |
| 278 |
/// only take effect if the type size was changed by "Q" or "H". |
| 279 |
void applyTypespec(bool &Quad); |
| 280 |
/// Applies prototype modifiers to the type. |
| 281 |
void applyModifiers(StringRef Mods); |
| 282 |
}; |
| 283 |
|
| 284 |
//===----------------------------------------------------------------------===// |
| 285 |
// Variable |
| 286 |
//===----------------------------------------------------------------------===// |
| 287 |
|
| 288 |
/// A variable is a simple class that just has a type and a name. |
| 289 |
class Variable { |
| 290 |
Type T; |
| 291 |
std::string N; |
| 292 |
|
| 293 |
public: |
| 294 |
Variable() : T(Type::getVoid()) {} |
| 295 |
Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {} |
| 296 |
|
| 297 |
Type getType() const { return T; } |
| 298 |
std::string getName() const { return "__" + N; } |
| 299 |
}; |
| 300 |
|
| 301 |
//===----------------------------------------------------------------------===// |
| 302 |
// Intrinsic |
| 303 |
//===----------------------------------------------------------------------===// |
| 304 |
|
| 305 |
/// The main grunt class. This represents an instantiation of an intrinsic with |
| 306 |
/// a particular typespec and prototype. |
| 307 |
class Intrinsic { |
| 308 |
/// The Record this intrinsic was created from. |
| 309 |
Record *R; |
| 310 |
/// The unmangled name. |
| 311 |
std::string Name; |
| 312 |
/// The input and output typespecs. InTS == OutTS except when |
| 313 |
/// CartesianProductWith is non-empty - this is the case for vreinterpret. |
| 314 |
TypeSpec OutTS, InTS; |
| 315 |
/// The base class kind. Most intrinsics use ClassS, which has full type |
| 316 |
/// info for integers (s32/u32). Some use ClassI, which doesn't care about |
| 317 |
/// signedness (i32), while some (ClassB) have no type at all, only a width |
| 318 |
/// (32). |
| 319 |
ClassKind CK; |
| 320 |
/// The list of DAGs for the body. May be empty, in which case we should |
| 321 |
/// emit a builtin call. |
| 322 |
ListInit *Body; |
| 323 |
/// The architectural ifdef guard. |
| 324 |
std::string ArchGuard; |
| 325 |
/// The architectural target() guard. |
| 326 |
std::string TargetGuard; |
| 327 |
/// Set if the Unavailable bit is 1. This means we don't generate a body, |
| 328 |
/// just an "unavailable" attribute on a declaration. |
| 329 |
bool IsUnavailable; |
| 330 |
/// Is this intrinsic safe for big-endian? or does it need its arguments |
| 331 |
/// reversing? |
| 332 |
bool BigEndianSafe; |
| 333 |
|
| 334 |
/// The types of return value [0] and parameters [1..]. |
| 335 |
std::vector<Type> Types; |
| 336 |
/// The index of the key type passed to CGBuiltin.cpp for polymorphic calls. |
| 337 |
int PolymorphicKeyType; |
| 338 |
/// The local variables defined. |
| 339 |
std::map<std::string, Variable> Variables; |
| 340 |
/// NeededEarly - set if any other intrinsic depends on this intrinsic. |
| 341 |
bool NeededEarly; |
| 342 |
/// UseMacro - set if we should implement using a macro or unset for a |
| 343 |
/// function. |
| 344 |
bool UseMacro; |
| 345 |
/// The set of intrinsics that this intrinsic uses/requires. |
| 346 |
std::set<Intrinsic *> Dependencies; |
| 347 |
/// The "base type", which is Type('d', OutTS). InBaseType is only |
| 348 |
/// different if CartesianProductWith is non-empty (for vreinterpret). |
| 349 |
Type BaseType, InBaseType; |
| 350 |
/// The return variable. |
| 351 |
Variable RetVar; |
| 352 |
/// A postfix to apply to every variable. Defaults to "". |
| 353 |
std::string VariablePostfix; |
| 354 |
|
| 355 |
NeonEmitter &Emitter; |
| 356 |
std::stringstream OS; |
| 357 |
|
| 358 |
bool isBigEndianSafe() const { |
| 359 |
if (BigEndianSafe) |
| 360 |
return true; |
| 361 |
|
| 362 |
for (const auto &T : Types){ |
| 363 |
if (T.isVector() && T.getNumElements() > 1) |
| 364 |
return false; |
| 365 |
} |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
public: |
| 370 |
Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS, |
| 371 |
TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter, |
| 372 |
StringRef ArchGuard, StringRef TargetGuard, bool IsUnavailable, bool BigEndianSafe) |
| 373 |
: R(R), Name(Name.str()), OutTS(OutTS), InTS(InTS), CK(CK), Body(Body), |
| 374 |
ArchGuard(ArchGuard.str()), TargetGuard(TargetGuard.str()), IsUnavailable(IsUnavailable), |
| 375 |
BigEndianSafe(BigEndianSafe), PolymorphicKeyType(0), NeededEarly(false), |
| 376 |
UseMacro(false), BaseType(OutTS, "."), InBaseType(InTS, "."), |
| 377 |
Emitter(Emitter) { |
| 378 |
// Modify the TypeSpec per-argument to get a concrete Type, and create |
| 379 |
// known variables for each. |
| 380 |
// Types[0] is the return value. |
| 381 |
unsigned Pos = 0; |
| 382 |
Types.emplace_back(OutTS, getNextModifiers(Proto, Pos)); |
| 383 |
StringRef Mods = getNextModifiers(Proto, Pos); |
| 384 |
while (!Mods.empty()) { |
| 385 |
Types.emplace_back(InTS, Mods); |
| 386 |
if (Mods.contains('!')) |
| 387 |
PolymorphicKeyType = Types.size() - 1; |
| 388 |
|
| 389 |
Mods = getNextModifiers(Proto, Pos); |
| 390 |
} |
| 391 |
|
| 392 |
for (auto Type : Types) { |
| 393 |
// If this builtin takes an immediate argument, we need to #define it rather |
| 394 |
// than use a standard declaration, so that SemaChecking can range check |
| 395 |
// the immediate passed by the user. |
| 396 |
|
| 397 |
// Pointer arguments need to use macros to avoid hiding aligned attributes |
| 398 |
// from the pointer type. |
| 399 |
|
| 400 |
// It is not permitted to pass or return an __fp16 by value, so intrinsics |
| 401 |
// taking a scalar float16_t must be implemented as macros. |
| 402 |
if (Type.isImmediate() || Type.isPointer() || |
| 403 |
(Type.isScalar() && Type.isHalf())) |
| 404 |
UseMacro = true; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/// Get the Record that this intrinsic is based off. |
| 409 |
Record *getRecord() const { return R; } |
| 410 |
/// Get the set of Intrinsics that this intrinsic calls. |
| 411 |
/// this is the set of immediate dependencies, NOT the |
| 412 |
/// transitive closure. |
| 413 |
const std::set<Intrinsic *> &getDependencies() const { return Dependencies; } |
| 414 |
/// Get the architectural guard string (#ifdef). |
| 415 |
std::string getArchGuard() const { return ArchGuard; } |
| 416 |
std::string getTargetGuard() const { return TargetGuard; } |
| 417 |
/// Get the non-mangled name. |
| 418 |
std::string getName() const { return Name; } |
| 419 |
|
| 420 |
/// Return true if the intrinsic takes an immediate operand. |
| 421 |
bool hasImmediate() const { |
| 422 |
return llvm::any_of(Types, [](const Type &T) { return T.isImmediate(); }); |
| 423 |
} |
| 424 |
|
| 425 |
/// Return the parameter index of the immediate operand. |
| 426 |
unsigned getImmediateIdx() const { |
| 427 |
for (unsigned Idx = 0; Idx < Types.size(); ++Idx) |
| 428 |
if (Types[Idx].isImmediate()) |
| 429 |
return Idx - 1; |
| 430 |
llvm_unreachable("Intrinsic has no immediate"); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
unsigned getNumParams() const { return Types.size() - 1; } |
| 435 |
Type getReturnType() const { return Types[0]; } |
| 436 |
Type getParamType(unsigned I) const { return Types[I + 1]; } |
| 437 |
Type getBaseType() const { return BaseType; } |
| 438 |
Type getPolymorphicKeyType() const { return Types[PolymorphicKeyType]; } |
| 439 |
|
| 440 |
/// Return true if the prototype has a scalar argument. |
| 441 |
bool protoHasScalar() const; |
| 442 |
|
| 443 |
/// Return the index that parameter PIndex will sit at |
| 444 |
/// in a generated function call. This is often just PIndex, |
| 445 |
/// but may not be as things such as multiple-vector operands |
| 446 |
/// and sret parameters need to be taken into account. |
| 447 |
unsigned getGeneratedParamIdx(unsigned PIndex) { |
| 448 |
unsigned Idx = 0; |
| 449 |
if (getReturnType().getNumVectors() > 1) |
| 450 |
// Multiple vectors are passed as sret. |
| 451 |
++Idx; |
| 452 |
|
| 453 |
for (unsigned I = 0; I < PIndex; ++I) |
| 454 |
Idx += std::max(1U, getParamType(I).getNumVectors()); |
| 455 |
|
| 456 |
return Idx; |
| 457 |
} |
| 458 |
|
| 459 |
bool hasBody() const { return Body && !Body->getValues().empty(); } |
| 460 |
|
| 461 |
void setNeededEarly() { NeededEarly = true; } |
| 462 |
|
| 463 |
bool operator<(const Intrinsic &Other) const { |
| 464 |
// Sort lexicographically on a three-tuple (ArchGuard, TargetGuard, Name) |
| 465 |
if (ArchGuard != Other.ArchGuard) |
| 466 |
return ArchGuard < Other.ArchGuard; |
| 467 |
if (TargetGuard != Other.TargetGuard) |
| 468 |
return TargetGuard < Other.TargetGuard; |
| 469 |
return Name < Other.Name; |
| 470 |
} |
| 471 |
|
| 472 |
ClassKind getClassKind(bool UseClassBIfScalar = false) { |
| 473 |
if (UseClassBIfScalar && !protoHasScalar()) |
| 474 |
return ClassB; |
| 475 |
return CK; |
| 476 |
} |
| 477 |
|
| 478 |
/// Return the name, mangled with type information. |
| 479 |
/// If ForceClassS is true, use ClassS (u32/s32) instead |
| 480 |
/// of the intrinsic's own type class. |
| 481 |
std::string getMangledName(bool ForceClassS = false) const; |
| 482 |
/// Return the type code for a builtin function call. |
| 483 |
std::string getInstTypeCode(Type T, ClassKind CK) const; |
| 484 |
/// Return the type string for a BUILTIN() macro in Builtins.def. |
| 485 |
std::string getBuiltinTypeStr(); |
| 486 |
|
| 487 |
/// Generate the intrinsic, returning code. |
| 488 |
std::string generate(); |
| 489 |
/// Perform type checking and populate the dependency graph, but |
| 490 |
/// don't generate code yet. |
| 491 |
void indexBody(); |
| 492 |
|
| 493 |
private: |
| 494 |
StringRef getNextModifiers(StringRef Proto, unsigned &Pos) const; |
| 495 |
|
| 496 |
std::string mangleName(std::string Name, ClassKind CK) const; |
| 497 |
|
| 498 |
void initVariables(); |
| 499 |
std::string replaceParamsIn(std::string S); |
| 500 |
|
| 501 |
void emitBodyAsBuiltinCall(); |
| 502 |
|
| 503 |
void generateImpl(bool ReverseArguments, |
| 504 |
StringRef NamePrefix, StringRef CallPrefix); |
| 505 |
void emitReturn(); |
| 506 |
void emitBody(StringRef CallPrefix); |
| 507 |
void emitShadowedArgs(); |
| 508 |
void emitArgumentReversal(); |
| 509 |
void emitReturnVarDecl(); |
| 510 |
void emitReturnReversal(); |
| 511 |
void emitReverseVariable(Variable &Dest, Variable &Src); |
| 512 |
void emitNewLine(); |
| 513 |
void emitClosingBrace(); |
| 514 |
void emitOpeningBrace(); |
| 515 |
void emitPrototype(StringRef NamePrefix); |
| 516 |
|
| 517 |
class DagEmitter { |
| 518 |
Intrinsic &Intr; |
| 519 |
StringRef CallPrefix; |
| 520 |
|
| 521 |
public: |
| 522 |
DagEmitter(Intrinsic &Intr, StringRef CallPrefix) : |
| 523 |
Intr(Intr), CallPrefix(CallPrefix) { |
| 524 |
} |
| 525 |
std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName); |
| 526 |
std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI); |
| 527 |
std::pair<Type, std::string> emitDagSplat(DagInit *DI); |
| 528 |
std::pair<Type, std::string> emitDagDup(DagInit *DI); |
| 529 |
std::pair<Type, std::string> emitDagDupTyped(DagInit *DI); |
| 530 |
std::pair<Type, std::string> emitDagShuffle(DagInit *DI); |
| 531 |
std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast); |
| 532 |
std::pair<Type, std::string> emitDagCall(DagInit *DI, |
| 533 |
bool MatchMangledName); |
| 534 |
std::pair<Type, std::string> emitDagNameReplace(DagInit *DI); |
| 535 |
std::pair<Type, std::string> emitDagLiteral(DagInit *DI); |
| 536 |
std::pair<Type, std::string> emitDagOp(DagInit *DI); |
| 537 |
std::pair<Type, std::string> emitDag(DagInit *DI); |
| 538 |
}; |
| 539 |
}; |
| 540 |
|
| 541 |
//===----------------------------------------------------------------------===// |
| 542 |
// NeonEmitter |
| 543 |
//===----------------------------------------------------------------------===// |
| 544 |
|
| 545 |
class NeonEmitter { |
| 546 |
RecordKeeper &Records; |
| 547 |
DenseMap<Record *, ClassKind> ClassMap; |
| 548 |
std::map<std::string, std::deque<Intrinsic>> IntrinsicMap; |
| 549 |
unsigned UniqueNumber; |
| 550 |
|
| 551 |
void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out); |
| 552 |
void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs); |
| 553 |
void genOverloadTypeCheckCode(raw_ostream &OS, |
| 554 |
SmallVectorImpl<Intrinsic *> &Defs); |
| 555 |
void genIntrinsicRangeCheckCode(raw_ostream &OS, |
| 556 |
SmallVectorImpl<Intrinsic *> &Defs); |
| 557 |
|
| 558 |
public: |
| 559 |
/// Called by Intrinsic - this attempts to get an intrinsic that takes |
| 560 |
/// the given types as arguments. |
| 561 |
Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types, |
| 562 |
std::optional<std::string> MangledName); |
| 563 |
|
| 564 |
/// Called by Intrinsic - returns a globally-unique number. |
| 565 |
unsigned getUniqueNumber() { return UniqueNumber++; } |
| 566 |
|
| 567 |
NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) { |
| 568 |
Record *SI = R.getClass("SInst"); |
| 569 |
Record *II = R.getClass("IInst"); |
| 570 |
Record *WI = R.getClass("WInst"); |
| 571 |
Record *SOpI = R.getClass("SOpInst"); |
| 572 |
Record *IOpI = R.getClass("IOpInst"); |
| 573 |
Record *WOpI = R.getClass("WOpInst"); |
| 574 |
Record *LOpI = R.getClass("LOpInst"); |
| 575 |
Record *NoTestOpI = R.getClass("NoTestOpInst"); |
| 576 |
|
| 577 |
ClassMap[SI] = ClassS; |
| 578 |
ClassMap[II] = ClassI; |
| 579 |
ClassMap[WI] = ClassW; |
| 580 |
ClassMap[SOpI] = ClassS; |
| 581 |
ClassMap[IOpI] = ClassI; |
| 582 |
ClassMap[WOpI] = ClassW; |
| 583 |
ClassMap[LOpI] = ClassL; |
| 584 |
ClassMap[NoTestOpI] = ClassNoTest; |
| 585 |
} |
| 586 |
|
| 587 |
// Emit arm_neon.h.inc |
| 588 |
void run(raw_ostream &o); |
| 589 |
|
| 590 |
// Emit arm_fp16.h.inc |
| 591 |
void runFP16(raw_ostream &o); |
| 592 |
|
| 593 |
// Emit arm_bf16.h.inc |
| 594 |
void runBF16(raw_ostream &o); |
| 595 |
|
| 596 |
// Emit all the __builtin prototypes used in arm_neon.h, arm_fp16.h and |
| 597 |
// arm_bf16.h |
| 598 |
void runHeader(raw_ostream &o); |
| 599 |
}; |
| 600 |
|
| 601 |
} // end anonymous namespace |
| 602 |
|
| 603 |
//===----------------------------------------------------------------------===// |
| 604 |
// Type implementation |
| 605 |
//===----------------------------------------------------------------------===// |
| 606 |
|
| 607 |
std::string Type::str() const { |
| 608 |
if (isVoid()) |
| 609 |
return "void"; |
| 610 |
std::string S; |
| 611 |
|
| 612 |
if (isInteger() && !isSigned()) |
| 613 |
S += "u"; |
| 614 |
|
| 615 |
if (isPoly()) |
| 616 |
S += "poly"; |
| 617 |
else if (isFloating()) |
| 618 |
S += "float"; |
| 619 |
else if (isBFloat16()) |
| 620 |
S += "bfloat"; |
| 621 |
else |
| 622 |
S += "int"; |
| 623 |
|
| 624 |
S += utostr(ElementBitwidth); |
| 625 |
if (isVector()) |
| 626 |
S += "x" + utostr(getNumElements()); |
| 627 |
if (NumVectors > 1) |
| 628 |
S += "x" + utostr(NumVectors); |
| 629 |
S += "_t"; |
| 630 |
|
| 631 |
if (Constant) |
| 632 |
S += " const"; |
| 633 |
if (Pointer) |
| 634 |
S += " *"; |
| 635 |
|
| 636 |
return S; |
| 637 |
} |
| 638 |
|
| 639 |
std::string Type::builtin_str() const { |
| 640 |
std::string S; |
| 641 |
if (isVoid()) |
| 642 |
return "v"; |
| 643 |
|
| 644 |
if (isPointer()) { |
| 645 |
// All pointers are void pointers. |
| 646 |
S = "v"; |
| 647 |
if (isConstPointer()) |
| 648 |
S += "C"; |
| 649 |
S += "*"; |
| 650 |
return S; |
| 651 |
} else if (isInteger()) |
| 652 |
switch (ElementBitwidth) { |
| 653 |
case 8: S += "c"; break; |
| 654 |
case 16: S += "s"; break; |
| 655 |
case 32: S += "i"; break; |
| 656 |
case 64: S += "Wi"; break; |
| 657 |
case 128: S += "LLLi"; break; |
| 658 |
default: llvm_unreachable("Unhandled case!"); |
| 659 |
} |
| 660 |
else if (isBFloat16()) { |
| 661 |
assert(ElementBitwidth == 16 && "BFloat16 can only be 16 bits"); |
| 662 |
S += "y"; |
| 663 |
} else |
| 664 |
switch (ElementBitwidth) { |
| 665 |
case 16: S += "h"; break; |
| 666 |
case 32: S += "f"; break; |
| 667 |
case 64: S += "d"; break; |
| 668 |
default: llvm_unreachable("Unhandled case!"); |
| 669 |
} |
| 670 |
|
| 671 |
// FIXME: NECESSARY??????????????????????????????????????????????????????????????????????? |
| 672 |
if (isChar() && !isPointer() && isSigned()) |
| 673 |
// Make chars explicitly signed. |
| 674 |
S = "S" + S; |
| 675 |
else if (isInteger() && !isSigned()) |
| 676 |
S = "U" + S; |
| 677 |
|
| 678 |
// Constant indices are "int", but have the "constant expression" modifier. |
| 679 |
if (isImmediate()) { |
| 680 |
assert(isInteger() && isSigned()); |
| 681 |
S = "I" + S; |
| 682 |
} |
| 683 |
|
| 684 |
if (isScalar()) |
| 685 |
return S; |
| 686 |
|
| 687 |
std::string Ret; |
| 688 |
for (unsigned I = 0; I < NumVectors; ++I) |
| 689 |
Ret += "V" + utostr(getNumElements()) + S; |
| 690 |
|
| 691 |
return Ret; |
| 692 |
} |
| 693 |
|
| 694 |
unsigned Type::getNeonEnum() const { |
| 695 |
unsigned Addend; |
| 696 |
switch (ElementBitwidth) { |
| 697 |
case 8: Addend = 0; break; |
| 698 |
case 16: Addend = 1; break; |
| 699 |
case 32: Addend = 2; break; |
| 700 |
case 64: Addend = 3; break; |
| 701 |
case 128: Addend = 4; break; |
| 702 |
default: llvm_unreachable("Unhandled element bitwidth!"); |
| 703 |
} |
| 704 |
|
| 705 |
unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend; |
| 706 |
if (isPoly()) { |
| 707 |
// Adjustment needed because Poly32 doesn't exist. |
| 708 |
if (Addend >= 2) |
| 709 |
--Addend; |
| 710 |
Base = (unsigned)NeonTypeFlags::Poly8 + Addend; |
| 711 |
} |
| 712 |
if (isFloating()) { |
| 713 |
assert(Addend != 0 && "Float8 doesn't exist!"); |
| 714 |
Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1); |
| 715 |
} |
| 716 |
|
| 717 |
if (isBFloat16()) { |
| 718 |
assert(Addend == 1 && "BFloat16 is only 16 bit"); |
| 719 |
Base = (unsigned)NeonTypeFlags::BFloat16; |
| 720 |
} |
| 721 |
|
| 722 |
if (Bitwidth == 128) |
| 723 |
Base |= (unsigned)NeonTypeFlags::QuadFlag; |
| 724 |
if (isInteger() && !isSigned()) |
| 725 |
Base |= (unsigned)NeonTypeFlags::UnsignedFlag; |
| 726 |
|
| 727 |
return Base; |
| 728 |
} |
| 729 |
|
| 730 |
Type Type::fromTypedefName(StringRef Name) { |
| 731 |
Type T; |
| 732 |
T.Kind = SInt; |
| 733 |
|
| 734 |
if (Name.front() == 'u') { |
| 735 |
T.Kind = UInt; |
| 736 |
Name = Name.drop_front(); |
| 737 |
} |
| 738 |
|
| 739 |
if (Name.startswith("float")) { |
| 740 |
T.Kind = Float; |
| 741 |
Name = Name.drop_front(5); |
| 742 |
} else if (Name.startswith("poly")) { |
| 743 |
T.Kind = Poly; |
| 744 |
Name = Name.drop_front(4); |
| 745 |
} else if (Name.startswith("bfloat")) { |
| 746 |
T.Kind = BFloat16; |
| 747 |
Name = Name.drop_front(6); |
| 748 |
} else { |
| 749 |
assert(Name.startswith("int")); |
| 750 |
Name = Name.drop_front(3); |
| 751 |
} |
| 752 |
|
| 753 |
unsigned I = 0; |
| 754 |
for (I = 0; I < Name.size(); ++I) { |
| 755 |
if (!isdigit(Name[I])) |
| 756 |
break; |
| 757 |
} |
| 758 |
Name.substr(0, I).getAsInteger(10, T.ElementBitwidth); |
| 759 |
Name = Name.drop_front(I); |
| 760 |
|
| 761 |
T.Bitwidth = T.ElementBitwidth; |
| 762 |
T.NumVectors = 1; |
| 763 |
|
| 764 |
if (Name.front() == 'x') { |
| 765 |
Name = Name.drop_front(); |
| 766 |
unsigned I = 0; |
| 767 |
for (I = 0; I < Name.size(); ++I) { |
| 768 |
if (!isdigit(Name[I])) |
| 769 |
break; |
| 770 |
} |
| 771 |
unsigned NumLanes; |
| 772 |
Name.substr(0, I).getAsInteger(10, NumLanes); |
| 773 |
Name = Name.drop_front(I); |
| 774 |
T.Bitwidth = T.ElementBitwidth * NumLanes; |
| 775 |
} else { |
| 776 |
// Was scalar. |
| 777 |
T.NumVectors = 0; |
| 778 |
} |
| 779 |
if (Name.front() == 'x') { |
| 780 |
Name = Name.drop_front(); |
| 781 |
unsigned I = 0; |
| 782 |
for (I = 0; I < Name.size(); ++I) { |
| 783 |
if (!isdigit(Name[I])) |
| 784 |
break; |
| 785 |
} |
| 786 |
Name.substr(0, I).getAsInteger(10, T.NumVectors); |
| 787 |
Name = Name.drop_front(I); |
| 788 |
} |
| 789 |
|
| 790 |
assert(Name.startswith("_t") && "Malformed typedef!"); |
| 791 |
return T; |
| 792 |
} |
| 793 |
|
| 794 |
void Type::applyTypespec(bool &Quad) { |
| 795 |
std::string S = TS; |
| 796 |
ScalarForMangling = false; |
| 797 |
Kind = SInt; |
| 798 |
ElementBitwidth = ~0U; |
| 799 |
NumVectors = 1; |
| 800 |
|
| 801 |
for (char I : S) { |
| 802 |
switch (I) { |
| 803 |
case 'S': |
| 804 |
ScalarForMangling = true; |
| 805 |
break; |
| 806 |
case 'H': |
| 807 |
NoManglingQ = true; |
| 808 |
Quad = true; |
| 809 |
break; |
| 810 |
case 'Q': |
| 811 |
Quad = true; |
| 812 |
break; |
| 813 |
case 'P': |
| 814 |
Kind = Poly; |
| 815 |
break; |
| 816 |
case 'U': |
| 817 |
Kind = UInt; |
| 818 |
break; |
| 819 |
case 'c': |
| 820 |
ElementBitwidth = 8; |
| 821 |
break; |
| 822 |
case 'h': |
| 823 |
Kind = Float; |
| 824 |
[[fallthrough]]; |
| 825 |
case 's': |
| 826 |
ElementBitwidth = 16; |
| 827 |
break; |
| 828 |
case 'f': |
| 829 |
Kind = Float; |
| 830 |
[[fallthrough]]; |
| 831 |
case 'i': |
| 832 |
ElementBitwidth = 32; |
| 833 |
break; |
| 834 |
case 'd': |
| 835 |
Kind = Float; |
| 836 |
[[fallthrough]]; |
| 837 |
case 'l': |
| 838 |
ElementBitwidth = 64; |
| 839 |
break; |
| 840 |
case 'k': |
| 841 |
ElementBitwidth = 128; |
| 842 |
// Poly doesn't have a 128x1 type. |
| 843 |
if (isPoly()) |
| 844 |
NumVectors = 0; |
| 845 |
break; |
| 846 |
case 'b': |
| 847 |
Kind = BFloat16; |
| 848 |
ElementBitwidth = 16; |
| 849 |
break; |
| 850 |
default: |
| 851 |
llvm_unreachable("Unhandled type code!"); |
| 852 |
} |
| 853 |
} |
| 854 |
assert(ElementBitwidth != ~0U && "Bad element bitwidth!"); |
| 855 |
|
| 856 |
Bitwidth = Quad ? 128 : 64; |
| 857 |
} |
| 858 |
|
| 859 |
void Type::applyModifiers(StringRef Mods) { |
| 860 |
bool AppliedQuad = false; |
| 861 |
applyTypespec(AppliedQuad); |
| 862 |
|
| 863 |
for (char Mod : Mods) { |
| 864 |
switch (Mod) { |
| 865 |
case '.': |
| 866 |
break; |
| 867 |
case 'v': |
| 868 |
Kind = Void; |
| 869 |
break; |
| 870 |
case 'S': |
| 871 |
Kind = SInt; |
| 872 |
break; |
| 873 |
case 'U': |
| 874 |
Kind = UInt; |
| 875 |
break; |
| 876 |
case 'B': |
| 877 |
Kind = BFloat16; |
| 878 |
ElementBitwidth = 16; |
| 879 |
break; |
| 880 |
case 'F': |
| 881 |
Kind = Float; |
| 882 |
break; |
| 883 |
case 'P': |
| 884 |
Kind = Poly; |
| 885 |
break; |
| 886 |
case '>': |
| 887 |
assert(ElementBitwidth < 128); |
| 888 |
ElementBitwidth *= 2; |
| 889 |
break; |
| 890 |
case '<': |
| 891 |
assert(ElementBitwidth > 8); |
| 892 |
ElementBitwidth /= 2; |
| 893 |
break; |
| 894 |
case '1': |
| 895 |
NumVectors = 0; |
| 896 |
break; |
| 897 |
case '2': |
| 898 |
NumVectors = 2; |
| 899 |
break; |
| 900 |
case '3': |
| 901 |
NumVectors = 3; |
| 902 |
break; |
| 903 |
case '4': |
| 904 |
NumVectors = 4; |
| 905 |
break; |
| 906 |
case '*': |
| 907 |
Pointer = true; |
| 908 |
break; |
| 909 |
case 'c': |
| 910 |
Constant = true; |
| 911 |
break; |
| 912 |
case 'Q': |
| 913 |
Bitwidth = 128; |
| 914 |
break; |
| 915 |
case 'q': |
| 916 |
Bitwidth = 64; |
| 917 |
break; |
| 918 |
case 'I': |
| 919 |
Kind = SInt; |
| 920 |
ElementBitwidth = Bitwidth = 32; |
| 921 |
NumVectors = 0; |
| 922 |
Immediate = true; |
| 923 |
break; |
| 924 |
case 'p': |
| 925 |
if (isPoly()) |
| 926 |
Kind = UInt; |
| 927 |
break; |
| 928 |
case '!': |
| 929 |
// Key type, handled elsewhere. |
| 930 |
break; |
| 931 |
default: |
| 932 |
llvm_unreachable("Unhandled character!"); |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
//===----------------------------------------------------------------------===// |
| 938 |
// Intrinsic implementation |
| 939 |
//===----------------------------------------------------------------------===// |
| 940 |
|
| 941 |
StringRef Intrinsic::getNextModifiers(StringRef Proto, unsigned &Pos) const { |
| 942 |
if (Proto.size() == Pos) |
| 943 |
return StringRef(); |
| 944 |
else if (Proto[Pos] != '(') |
| 945 |
return Proto.substr(Pos++, 1); |
| 946 |
|
| 947 |
size_t Start = Pos + 1; |
| 948 |
size_t End = Proto.find(')', Start); |
| 949 |
assert_with_loc(End != StringRef::npos, "unmatched modifier group paren"); |
| 950 |
Pos = End + 1; |
| 951 |
return Proto.slice(Start, End); |
| 952 |
} |
| 953 |
|
| 954 |
std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const { |
| 955 |
char typeCode = '\0'; |
| 956 |
bool printNumber = true; |
| 957 |
|
| 958 |
if (CK == ClassB && TargetGuard == "") |
| 959 |
return ""; |
| 960 |
|
| 961 |
if (T.isBFloat16()) |
| 962 |
return "bf16"; |
| 963 |
|
| 964 |
if (T.isPoly()) |
| 965 |
typeCode = 'p'; |
| 966 |
else if (T.isInteger()) |
| 967 |
typeCode = T.isSigned() ? 's' : 'u'; |
| 968 |
else |
| 969 |
typeCode = 'f'; |
| 970 |
|
| 971 |
if (CK == ClassI) { |
| 972 |
switch (typeCode) { |
| 973 |
default: |
| 974 |
break; |
| 975 |
case 's': |
| 976 |
case 'u': |
| 977 |
case 'p': |
| 978 |
typeCode = 'i'; |
| 979 |
break; |
| 980 |
} |
| 981 |
} |
| 982 |
if (CK == ClassB && TargetGuard == "") { |
| 983 |
typeCode = '\0'; |
| 984 |
} |
| 985 |
|
| 986 |
std::string S; |
| 987 |
if (typeCode != '\0') |
| 988 |
S.push_back(typeCode); |
| 989 |
if (printNumber) |
| 990 |
S += utostr(T.getElementSizeInBits()); |
| 991 |
|
| 992 |
return S; |
| 993 |
} |
| 994 |
|
| 995 |
std::string Intrinsic::getBuiltinTypeStr() { |
| 996 |
ClassKind LocalCK = getClassKind(true); |
| 997 |
std::string S; |
| 998 |
|
| 999 |
Type RetT = getReturnType(); |
| 1000 |
if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() && |
| 1001 |
!RetT.isFloating() && !RetT.isBFloat16()) |
| 1002 |
RetT.makeInteger(RetT.getElementSizeInBits(), false); |
| 1003 |
|
| 1004 |
// Since the return value must be one type, return a vector type of the |
| 1005 |
// appropriate width which we will bitcast. An exception is made for |
| 1006 |
// returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 1007 |
// fashion, storing them to a pointer arg. |
| 1008 |
if (RetT.getNumVectors() > 1) { |
| 1009 |
S += "vv*"; // void result with void* first argument |
| 1010 |
} else { |
| 1011 |
if (RetT.isPoly()) |
| 1012 |
RetT.makeInteger(RetT.getElementSizeInBits(), false); |
| 1013 |
if (!RetT.isScalar() && RetT.isInteger() && !RetT.isSigned()) |
| 1014 |
RetT.makeSigned(); |
| 1015 |
|
| 1016 |
if (LocalCK == ClassB && RetT.isValue() && !RetT.isScalar()) |
| 1017 |
// Cast to vector of 8-bit elements. |
| 1018 |
RetT.makeInteger(8, true); |
| 1019 |
|
| 1020 |
S += RetT.builtin_str(); |
| 1021 |
} |
| 1022 |
|
| 1023 |
for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1024 |
Type T = getParamType(I); |
| 1025 |
if (T.isPoly()) |
| 1026 |
T.makeInteger(T.getElementSizeInBits(), false); |
| 1027 |
|
| 1028 |
if (LocalCK == ClassB && !T.isScalar()) |
| 1029 |
T.makeInteger(8, true); |
| 1030 |
// Halves always get converted to 8-bit elements. |
| 1031 |
if (T.isHalf() && T.isVector() && !T.isScalarForMangling()) |
| 1032 |
T.makeInteger(8, true); |
| 1033 |
|
| 1034 |
if (LocalCK == ClassI && T.isInteger()) |
| 1035 |
T.makeSigned(); |
| 1036 |
|
| 1037 |
if (hasImmediate() && getImmediateIdx() == I) |
| 1038 |
T.makeImmediate(32); |
| 1039 |
|
| 1040 |
S += T.builtin_str(); |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Extra constant integer to hold type class enum for this function, e.g. s8 |
| 1044 |
if (LocalCK == ClassB) |
| 1045 |
S += "i"; |
| 1046 |
|
| 1047 |
return S; |
| 1048 |
} |
| 1049 |
|
| 1050 |
std::string Intrinsic::getMangledName(bool ForceClassS) const { |
| 1051 |
// Check if the prototype has a scalar operand with the type of the vector |
| 1052 |
// elements. If not, bitcasting the args will take care of arg checking. |
| 1053 |
// The actual signedness etc. will be taken care of with special enums. |
| 1054 |
ClassKind LocalCK = CK; |
| 1055 |
if (!protoHasScalar()) |
| 1056 |
LocalCK = ClassB; |
| 1057 |
|
| 1058 |
return mangleName(Name, ForceClassS ? ClassS : LocalCK); |
| 1059 |
} |
| 1060 |
|
| 1061 |
std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const { |
| 1062 |
std::string typeCode = getInstTypeCode(BaseType, LocalCK); |
| 1063 |
std::string S = Name; |
| 1064 |
|
| 1065 |
if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" || |
| 1066 |
Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32" || |
| 1067 |
Name == "vcvt_f32_bf16") |
| 1068 |
return Name; |
| 1069 |
|
| 1070 |
if (!typeCode.empty()) { |
| 1071 |
// If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN. |
| 1072 |
if (Name.size() >= 3 && isdigit(Name.back()) && |
| 1073 |
Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_') |
| 1074 |
S.insert(S.length() - 3, "_" + typeCode); |
| 1075 |
else |
| 1076 |
S += "_" + typeCode; |
| 1077 |
} |
| 1078 |
|
| 1079 |
if (BaseType != InBaseType) { |
| 1080 |
// A reinterpret - out the input base type at the end. |
| 1081 |
S += "_" + getInstTypeCode(InBaseType, LocalCK); |
| 1082 |
} |
| 1083 |
|
| 1084 |
if (LocalCK == ClassB && TargetGuard == "") |
| 1085 |
S += "_v"; |
| 1086 |
|
| 1087 |
// Insert a 'q' before the first '_' character so that it ends up before |
| 1088 |
// _lane or _n on vector-scalar operations. |
| 1089 |
if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) { |
| 1090 |
size_t Pos = S.find('_'); |
| 1091 |
S.insert(Pos, "q"); |
| 1092 |
} |
| 1093 |
|
| 1094 |
char Suffix = '\0'; |
| 1095 |
if (BaseType.isScalarForMangling()) { |
| 1096 |
switch (BaseType.getElementSizeInBits()) { |
| 1097 |
case 8: Suffix = 'b'; break; |
| 1098 |
case 16: Suffix = 'h'; break; |
| 1099 |
case 32: Suffix = 's'; break; |
| 1100 |
case 64: Suffix = 'd'; break; |
| 1101 |
default: llvm_unreachable("Bad suffix!"); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
if (Suffix != '\0') { |
| 1105 |
size_t Pos = S.find('_'); |
| 1106 |
S.insert(Pos, &Suffix, 1); |
| 1107 |
} |
| 1108 |
|
| 1109 |
return S; |
| 1110 |
} |
| 1111 |
|
| 1112 |
std::string Intrinsic::replaceParamsIn(std::string S) { |
| 1113 |
while (S.find('$') != std::string::npos) { |
| 1114 |
size_t Pos = S.find('$'); |
| 1115 |
size_t End = Pos + 1; |
| 1116 |
while (isalpha(S[End])) |
| 1117 |
++End; |
| 1118 |
|
| 1119 |
std::string VarName = S.substr(Pos + 1, End - Pos - 1); |
| 1120 |
assert_with_loc(Variables.find(VarName) != Variables.end(), |
| 1121 |
"Variable not defined!"); |
| 1122 |
S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName()); |
| 1123 |
} |
| 1124 |
|
| 1125 |
return S; |
| 1126 |
} |
| 1127 |
|
| 1128 |
void Intrinsic::initVariables() { |
| 1129 |
Variables.clear(); |
| 1130 |
|
| 1131 |
// Modify the TypeSpec per-argument to get a concrete Type, and create |
| 1132 |
// known variables for each. |
| 1133 |
for (unsigned I = 1; I < Types.size(); ++I) { |
| 1134 |
char NameC = '0' + (I - 1); |
| 1135 |
std::string Name = "p"; |
| 1136 |
Name.push_back(NameC); |
| 1137 |
|
| 1138 |
Variables[Name] = Variable(Types[I], Name + VariablePostfix); |
| 1139 |
} |
| 1140 |
RetVar = Variable(Types[0], "ret" + VariablePostfix); |
| 1141 |
} |
| 1142 |
|
| 1143 |
void Intrinsic::emitPrototype(StringRef NamePrefix) { |
| 1144 |
if (UseMacro) { |
| 1145 |
OS << "#define "; |
| 1146 |
} else { |
| 1147 |
OS << "__ai "; |
| 1148 |
if (TargetGuard != "") |
| 1149 |
OS << "__attribute__((target(\"" << TargetGuard << "\"))) "; |
| 1150 |
OS << Types[0].str() << " "; |
| 1151 |
} |
| 1152 |
|
| 1153 |
OS << NamePrefix.str() << mangleName(Name, ClassS) << "("; |
| 1154 |
|
| 1155 |
for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1156 |
if (I != 0) |
| 1157 |
OS << ", "; |
| 1158 |
|
| 1159 |
char NameC = '0' + I; |
| 1160 |
std::string Name = "p"; |
| 1161 |
Name.push_back(NameC); |
| 1162 |
assert(Variables.find(Name) != Variables.end()); |
| 1163 |
Variable &V = Variables[Name]; |
| 1164 |
|
| 1165 |
if (!UseMacro) |
| 1166 |
OS << V.getType().str() << " "; |
| 1167 |
OS << V.getName(); |
| 1168 |
} |
| 1169 |
|
| 1170 |
OS << ")"; |
| 1171 |
} |
| 1172 |
|
| 1173 |
void Intrinsic::emitOpeningBrace() { |
| 1174 |
if (UseMacro) |
| 1175 |
OS << " __extension__ ({"; |
| 1176 |
else |
| 1177 |
OS << " {"; |
| 1178 |
emitNewLine(); |
| 1179 |
} |
| 1180 |
|
| 1181 |
void Intrinsic::emitClosingBrace() { |
| 1182 |
if (UseMacro) |
| 1183 |
OS << "})"; |
| 1184 |
else |
| 1185 |
OS << "}"; |
| 1186 |
} |
| 1187 |
|
| 1188 |
void Intrinsic::emitNewLine() { |
| 1189 |
if (UseMacro) |
| 1190 |
OS << " \\\n"; |
| 1191 |
else |
| 1192 |
OS << "\n"; |
| 1193 |
} |
| 1194 |
|
| 1195 |
void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) { |
| 1196 |
if (Dest.getType().getNumVectors() > 1) { |
| 1197 |
emitNewLine(); |
| 1198 |
|
| 1199 |
for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) { |
| 1200 |
OS << " " << Dest.getName() << ".val[" << K << "] = " |
| 1201 |
<< "__builtin_shufflevector(" |
| 1202 |
<< Src.getName() << ".val[" << K << "], " |
| 1203 |
<< Src.getName() << ".val[" << K << "]"; |
| 1204 |
for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) |
| 1205 |
OS << ", " << J; |
| 1206 |
OS << ");"; |
| 1207 |
emitNewLine(); |
| 1208 |
} |
| 1209 |
} else { |
| 1210 |
OS << " " << Dest.getName() |
| 1211 |
<< " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName(); |
| 1212 |
for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) |
| 1213 |
OS << ", " << J; |
| 1214 |
OS << ");"; |
| 1215 |
emitNewLine(); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
void Intrinsic::emitArgumentReversal() { |
| 1220 |
if (isBigEndianSafe()) |
| 1221 |
return; |
| 1222 |
|
| 1223 |
// Reverse all vector arguments. |
| 1224 |
for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1225 |
std::string Name = "p" + utostr(I); |
| 1226 |
std::string NewName = "rev" + utostr(I); |
| 1227 |
|
| 1228 |
Variable &V = Variables[Name]; |
| 1229 |
Variable NewV(V.getType(), NewName + VariablePostfix); |
| 1230 |
|
| 1231 |
if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1) |
| 1232 |
continue; |
| 1233 |
|
| 1234 |
OS << " " << NewV.getType().str() << " " << NewV.getName() << ";"; |
| 1235 |
emitReverseVariable(NewV, V); |
| 1236 |
V = NewV; |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
void Intrinsic::emitReturnVarDecl() { |
| 1241 |
assert(RetVar.getType() == Types[0]); |
| 1242 |
// Create a return variable, if we're not void. |
| 1243 |
if (!RetVar.getType().isVoid()) { |
| 1244 |
OS << " " << RetVar.getType().str() << " " << RetVar.getName() << ";"; |
| 1245 |
emitNewLine(); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
void Intrinsic::emitReturnReversal() { |
| 1250 |
if (isBigEndianSafe()) |
| 1251 |
return; |
| 1252 |
if (!getReturnType().isVector() || getReturnType().isVoid() || |
| 1253 |
getReturnType().getNumElements() == 1) |
| 1254 |
return; |
| 1255 |
emitReverseVariable(RetVar, RetVar); |
| 1256 |
} |
| 1257 |
|
| 1258 |
void Intrinsic::emitShadowedArgs() { |
| 1259 |
// Macro arguments are not type-checked like inline function arguments, |
| 1260 |
// so assign them to local temporaries to get the right type checking. |
| 1261 |
if (!UseMacro) |
| 1262 |
return; |
| 1263 |
|
| 1264 |
for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1265 |
// Do not create a temporary for an immediate argument. |
| 1266 |
// That would defeat the whole point of using a macro! |
| 1267 |
if (getParamType(I).isImmediate()) |
| 1268 |
continue; |
| 1269 |
// Do not create a temporary for pointer arguments. The input |
| 1270 |
// pointer may have an alignment hint. |
| 1271 |
if (getParamType(I).isPointer()) |
| 1272 |
continue; |
| 1273 |
|
| 1274 |
std::string Name = "p" + utostr(I); |
| 1275 |
|
| 1276 |
assert(Variables.find(Name) != Variables.end()); |
| 1277 |
Variable &V = Variables[Name]; |
| 1278 |
|
| 1279 |
std::string NewName = "s" + utostr(I); |
| 1280 |
Variable V2(V.getType(), NewName + VariablePostfix); |
| 1281 |
|
| 1282 |
OS << " " << V2.getType().str() << " " << V2.getName() << " = " |
| 1283 |
<< V.getName() << ";"; |
| 1284 |
emitNewLine(); |
| 1285 |
|
| 1286 |
V = V2; |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
bool Intrinsic::protoHasScalar() const { |
| 1291 |
return llvm::any_of( |
| 1292 |
Types, [](const Type &T) { return T.isScalar() && !T.isImmediate(); }); |
| 1293 |
} |
| 1294 |
|
| 1295 |
void Intrinsic::emitBodyAsBuiltinCall() { |
| 1296 |
std::string S; |
| 1297 |
|
| 1298 |
// If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 1299 |
// sret-like argument. |
| 1300 |
bool SRet = getReturnType().getNumVectors() >= 2; |
| 1301 |
|
| 1302 |
StringRef N = Name; |
| 1303 |
ClassKind LocalCK = CK; |
| 1304 |
if (!protoHasScalar()) |
| 1305 |
LocalCK = ClassB; |
| 1306 |
|
| 1307 |
if (!getReturnType().isVoid() && !SRet) |
| 1308 |
S += "(" + RetVar.getType().str() + ") "; |
| 1309 |
|
| 1310 |
S += "__builtin_neon_" + mangleName(std::string(N), LocalCK) + "("; |
| 1311 |
|
| 1312 |
if (SRet) |
| 1313 |
S += "&" + RetVar.getName() + ", "; |
| 1314 |
|
| 1315 |
for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1316 |
Variable &V = Variables["p" + utostr(I)]; |
| 1317 |
Type T = V.getType(); |
| 1318 |
|
| 1319 |
// Handle multiple-vector values specially, emitting each subvector as an |
| 1320 |
// argument to the builtin. |
| 1321 |
if (T.getNumVectors() > 1) { |
| 1322 |
// Check if an explicit cast is needed. |
| 1323 |
std::string Cast; |
| 1324 |
if (LocalCK == ClassB) { |
| 1325 |
Type T2 = T; |
| 1326 |
T2.makeOneVector(); |
| 1327 |
T2.makeInteger(8, /*Sign=*/true); |
| 1328 |
Cast = "(" + T2.str() + ")"; |
| 1329 |
} |
| 1330 |
|
| 1331 |
for (unsigned J = 0; J < T.getNumVectors(); ++J) |
| 1332 |
S += Cast + V.getName() + ".val[" + utostr(J) + "], "; |
| 1333 |
continue; |
| 1334 |
} |
| 1335 |
|
| 1336 |
std::string Arg = V.getName(); |
| 1337 |
Type CastToType = T; |
| 1338 |
|
| 1339 |
// Check if an explicit cast is needed. |
| 1340 |
if (CastToType.isVector() && |
| 1341 |
(LocalCK == ClassB || (T.isHalf() && !T.isScalarForMangling()))) { |
| 1342 |
CastToType.makeInteger(8, true); |
| 1343 |
Arg = "(" + CastToType.str() + ")" + Arg; |
| 1344 |
} else if (CastToType.isVector() && LocalCK == ClassI) { |
| 1345 |
if (CastToType.isInteger()) |
| 1346 |
CastToType.makeSigned(); |
| 1347 |
Arg = "(" + CastToType.str() + ")" + Arg; |
| 1348 |
} |
| 1349 |
|
| 1350 |
S += Arg + ", "; |
| 1351 |
} |
| 1352 |
|
| 1353 |
// Extra constant integer to hold type class enum for this function, e.g. s8 |
| 1354 |
if (getClassKind(true) == ClassB) { |
| 1355 |
S += utostr(getPolymorphicKeyType().getNeonEnum()); |
| 1356 |
} else { |
| 1357 |
// Remove extraneous ", ". |
| 1358 |
S.pop_back(); |
| 1359 |
S.pop_back(); |
| 1360 |
} |
| 1361 |
S += ");"; |
| 1362 |
|
| 1363 |
std::string RetExpr; |
| 1364 |
if (!SRet && !RetVar.getType().isVoid()) |
| 1365 |
RetExpr = RetVar.getName() + " = "; |
| 1366 |
|
| 1367 |
OS << " " << RetExpr << S; |
| 1368 |
emitNewLine(); |
| 1369 |
} |
| 1370 |
|
| 1371 |
void Intrinsic::emitBody(StringRef CallPrefix) { |
| 1372 |
std::vector<std::string> Lines; |
| 1373 |
|
| 1374 |
if (!Body || Body->getValues().empty()) { |
| 1375 |
// Nothing specific to output - must output a builtin. |
| 1376 |
emitBodyAsBuiltinCall(); |
| 1377 |
return; |
| 1378 |
} |
| 1379 |
|
| 1380 |
// We have a list of "things to output". The last should be returned. |
| 1381 |
for (auto *I : Body->getValues()) { |
| 1382 |
if (StringInit *SI = dyn_cast<StringInit>(I)) { |
| 1383 |
Lines.push_back(replaceParamsIn(SI->getAsString())); |
| 1384 |
} else if (DagInit *DI = dyn_cast<DagInit>(I)) { |
| 1385 |
DagEmitter DE(*this, CallPrefix); |
| 1386 |
Lines.push_back(DE.emitDag(DI).second + ";"); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
assert(!Lines.empty() && "Empty def?"); |
| 1391 |
if (!RetVar.getType().isVoid()) |
| 1392 |
Lines.back().insert(0, RetVar.getName() + " = "); |
| 1393 |
|
| 1394 |
for (auto &L : Lines) { |
| 1395 |
OS << " " << L; |
| 1396 |
emitNewLine(); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
void Intrinsic::emitReturn() { |
| 1401 |
if (RetVar.getType().isVoid()) |
| 1402 |
return; |
| 1403 |
if (UseMacro) |
| 1404 |
OS << " " << RetVar.getName() << ";"; |
| 1405 |
else |
| 1406 |
OS << " return " << RetVar.getName() << ";"; |
| 1407 |
emitNewLine(); |
| 1408 |
} |
| 1409 |
|
| 1410 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) { |
| 1411 |
// At this point we should only be seeing a def. |
| 1412 |
DefInit *DefI = cast<DefInit>(DI->getOperator()); |
| 1413 |
std::string Op = DefI->getAsString(); |
| 1414 |
|
| 1415 |
if (Op == "cast" || Op == "bitcast") |
| 1416 |
return emitDagCast(DI, Op == "bitcast"); |
| 1417 |
if (Op == "shuffle") |
| 1418 |
return emitDagShuffle(DI); |
| 1419 |
if (Op == "dup") |
| 1420 |
return emitDagDup(DI); |
| 1421 |
if (Op == "dup_typed") |
| 1422 |
return emitDagDupTyped(DI); |
| 1423 |
if (Op == "splat") |
| 1424 |
return emitDagSplat(DI); |
| 1425 |
if (Op == "save_temp") |
| 1426 |
return emitDagSaveTemp(DI); |
| 1427 |
if (Op == "op") |
| 1428 |
return emitDagOp(DI); |
| 1429 |
if (Op == "call" || Op == "call_mangled") |
| 1430 |
return emitDagCall(DI, Op == "call_mangled"); |
| 1431 |
if (Op == "name_replace") |
| 1432 |
return emitDagNameReplace(DI); |
| 1433 |
if (Op == "literal") |
| 1434 |
return emitDagLiteral(DI); |
| 1435 |
assert_with_loc(false, "Unknown operation!"); |
| 1436 |
return std::make_pair(Type::getVoid(), ""); |
| 1437 |
} |
| 1438 |
|
| 1439 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) { |
| 1440 |
std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1441 |
if (DI->getNumArgs() == 2) { |
| 1442 |
// Unary op. |
| 1443 |
std::pair<Type, std::string> R = |
| 1444 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1445 |
return std::make_pair(R.first, Op + R.second); |
| 1446 |
} else { |
| 1447 |
assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!"); |
| 1448 |
std::pair<Type, std::string> R1 = |
| 1449 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1450 |
std::pair<Type, std::string> R2 = |
| 1451 |
emitDagArg(DI->getArg(2), std::string(DI->getArgNameStr(2))); |
| 1452 |
assert_with_loc(R1.first == R2.first, "Argument type mismatch!"); |
| 1453 |
return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
std::pair<Type, std::string> |
| 1458 |
Intrinsic::DagEmitter::emitDagCall(DagInit *DI, bool MatchMangledName) { |
| 1459 |
std::vector<Type> Types; |
| 1460 |
std::vector<std::string> Values; |
| 1461 |
for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { |
| 1462 |
std::pair<Type, std::string> R = |
| 1463 |
emitDagArg(DI->getArg(I + 1), std::string(DI->getArgNameStr(I + 1))); |
| 1464 |
Types.push_back(R.first); |
| 1465 |
Values.push_back(R.second); |
| 1466 |
} |
| 1467 |
|
| 1468 |
// Look up the called intrinsic. |
| 1469 |
std::string N; |
| 1470 |
if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) |
| 1471 |
N = SI->getAsUnquotedString(); |
| 1472 |
else |
| 1473 |
N = emitDagArg(DI->getArg(0), "").second; |
| 1474 |
std::optional<std::string> MangledName; |
| 1475 |
if (MatchMangledName) { |
| 1476 |
if (Intr.getRecord()->getValueAsBit("isLaneQ")) |
| 1477 |
N += "q"; |
| 1478 |
MangledName = Intr.mangleName(N, ClassS); |
| 1479 |
} |
| 1480 |
Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types, MangledName); |
| 1481 |
|
| 1482 |
// Make sure the callee is known as an early def. |
| 1483 |
Callee.setNeededEarly(); |
| 1484 |
Intr.Dependencies.insert(&Callee); |
| 1485 |
|
| 1486 |
// Now create the call itself. |
| 1487 |
std::string S; |
| 1488 |
if (!Callee.isBigEndianSafe()) |
| 1489 |
S += CallPrefix.str(); |
| 1490 |
S += Callee.getMangledName(true) + "("; |
| 1491 |
for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { |
| 1492 |
if (I != 0) |
| 1493 |
S += ", "; |
| 1494 |
S += Values[I]; |
| 1495 |
} |
| 1496 |
S += ")"; |
| 1497 |
|
| 1498 |
return std::make_pair(Callee.getReturnType(), S); |
| 1499 |
} |
| 1500 |
|
| 1501 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI, |
| 1502 |
bool IsBitCast){ |
| 1503 |
// (cast MOD* VAL) -> cast VAL to type given by MOD. |
| 1504 |
std::pair<Type, std::string> R = |
| 1505 |
emitDagArg(DI->getArg(DI->getNumArgs() - 1), |
| 1506 |
std::string(DI->getArgNameStr(DI->getNumArgs() - 1))); |
| 1507 |
Type castToType = R.first; |
| 1508 |
for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) { |
| 1509 |
|
| 1510 |
// MOD can take several forms: |
| 1511 |
// 1. $X - take the type of parameter / variable X. |
| 1512 |
// 2. The value "R" - take the type of the return type. |
| 1513 |
// 3. a type string |
| 1514 |
// 4. The value "U" or "S" to switch the signedness. |
| 1515 |
// 5. The value "H" or "D" to half or double the bitwidth. |
| 1516 |
// 6. The value "8" to convert to 8-bit (signed) integer lanes. |
| 1517 |
if (!DI->getArgNameStr(ArgIdx).empty()) { |
| 1518 |
assert_with_loc(Intr.Variables.find(std::string( |
| 1519 |
DI->getArgNameStr(ArgIdx))) != Intr.Variables.end(), |
| 1520 |
"Variable not found"); |
| 1521 |
castToType = |
| 1522 |
Intr.Variables[std::string(DI->getArgNameStr(ArgIdx))].getType(); |
| 1523 |
} else { |
| 1524 |
StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx)); |
| 1525 |
assert_with_loc(SI, "Expected string type or $Name for cast type"); |
| 1526 |
|
| 1527 |
if (SI->getAsUnquotedString() == "R") { |
| 1528 |
castToType = Intr.getReturnType(); |
| 1529 |
} else if (SI->getAsUnquotedString() == "U") { |
| 1530 |
castToType.makeUnsigned(); |
| 1531 |
} else if (SI->getAsUnquotedString() == "S") { |
| 1532 |
castToType.makeSigned(); |
| 1533 |
} else if (SI->getAsUnquotedString() == "H") { |
| 1534 |
castToType.halveLanes(); |
| 1535 |
} else if (SI->getAsUnquotedString() == "D") { |
| 1536 |
castToType.doubleLanes(); |
| 1537 |
} else if (SI->getAsUnquotedString() == "8") { |
| 1538 |
castToType.makeInteger(8, true); |
| 1539 |
} else if (SI->getAsUnquotedString() == "32") { |
| 1540 |
castToType.make32BitElement(); |
| 1541 |
} else { |
| 1542 |
castToType = Type::fromTypedefName(SI->getAsUnquotedString()); |
| 1543 |
assert_with_loc(!castToType.isVoid(), "Unknown typedef"); |
| 1544 |
} |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
std::string S; |
| 1549 |
if (IsBitCast) { |
| 1550 |
// Emit a reinterpret cast. The second operand must be an lvalue, so create |
| 1551 |
// a temporary. |
| 1552 |
std::string N = "reint"; |
| 1553 |
unsigned I = 0; |
| 1554 |
while (Intr.Variables.find(N) != Intr.Variables.end()) |
| 1555 |
N = "reint" + utostr(++I); |
| 1556 |
Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix); |
| 1557 |
|
| 1558 |
Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = " |
| 1559 |
<< R.second << ";"; |
| 1560 |
Intr.emitNewLine(); |
| 1561 |
|
| 1562 |
S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + ""; |
| 1563 |
} else { |
| 1564 |
// Emit a normal (static) cast. |
| 1565 |
S = "(" + castToType.str() + ")(" + R.second + ")"; |
| 1566 |
} |
| 1567 |
|
| 1568 |
return std::make_pair(castToType, S); |
| 1569 |
} |
| 1570 |
|
| 1571 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){ |
| 1572 |
// See the documentation in arm_neon.td for a description of these operators. |
| 1573 |
class LowHalf : public SetTheory::Operator { |
| 1574 |
public: |
| 1575 |
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1576 |
ArrayRef<SMLoc> Loc) override { |
| 1577 |
SetTheory::RecSet Elts2; |
| 1578 |
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); |
| 1579 |
Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2)); |
| 1580 |
} |
| 1581 |
}; |
| 1582 |
|
| 1583 |
class HighHalf : public SetTheory::Operator { |
| 1584 |
public: |
| 1585 |
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1586 |
ArrayRef<SMLoc> Loc) override { |
| 1587 |
SetTheory::RecSet Elts2; |
| 1588 |
ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); |
| 1589 |
Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end()); |
| 1590 |
} |
| 1591 |
}; |
| 1592 |
|
| 1593 |
class Rev : public SetTheory::Operator { |
| 1594 |
unsigned ElementSize; |
| 1595 |
|
| 1596 |
public: |
| 1597 |
Rev(unsigned ElementSize) : ElementSize(ElementSize) {} |
| 1598 |
|
| 1599 |
void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1600 |
ArrayRef<SMLoc> Loc) override { |
| 1601 |
SetTheory::RecSet Elts2; |
| 1602 |
ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc); |
| 1603 |
|
| 1604 |
int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue(); |
| 1605 |
VectorSize /= ElementSize; |
| 1606 |
|
| 1607 |
std::vector<Record *> Revved; |
| 1608 |
for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) { |
| 1609 |
for (int LI = VectorSize - 1; LI >= 0; --LI) { |
| 1610 |
Revved.push_back(Elts2[VI + LI]); |
| 1611 |
} |
| 1612 |
} |
| 1613 |
|
| 1614 |
Elts.insert(Revved.begin(), Revved.end()); |
| 1615 |
} |
| 1616 |
}; |
| 1617 |
|
| 1618 |
class MaskExpander : public SetTheory::Expander { |
| 1619 |
unsigned N; |
| 1620 |
|
| 1621 |
public: |
| 1622 |
MaskExpander(unsigned N) : N(N) {} |
| 1623 |
|
| 1624 |
void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override { |
| 1625 |
unsigned Addend = 0; |
| 1626 |
if (R->getName() == "mask0") |
| 1627 |
Addend = 0; |
| 1628 |
else if (R->getName() == "mask1") |
| 1629 |
Addend = N; |
| 1630 |
else |
| 1631 |
return; |
| 1632 |
for (unsigned I = 0; I < N; ++I) |
| 1633 |
Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend))); |
| 1634 |
} |
| 1635 |
}; |
| 1636 |
|
| 1637 |
// (shuffle arg1, arg2, sequence) |
| 1638 |
std::pair<Type, std::string> Arg1 = |
| 1639 |
emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); |
| 1640 |
std::pair<Type, std::string> Arg2 = |
| 1641 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1642 |
assert_with_loc(Arg1.first == Arg2.first, |
| 1643 |
"Different types in arguments to shuffle!"); |
| 1644 |
|
| 1645 |
SetTheory ST; |
| 1646 |
SetTheory::RecSet Elts; |
| 1647 |
ST.addOperator("lowhalf", std::make_unique<LowHalf>()); |
| 1648 |
ST.addOperator("highhalf", std::make_unique<HighHalf>()); |
| 1649 |
ST.addOperator("rev", |
| 1650 |
std::make_unique<Rev>(Arg1.first.getElementSizeInBits())); |
| 1651 |
ST.addExpander("MaskExpand", |
| 1652 |
std::make_unique<MaskExpander>(Arg1.first.getNumElements())); |
| 1653 |
ST.evaluate(DI->getArg(2), Elts, std::nullopt); |
| 1654 |
|
| 1655 |
std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second; |
| 1656 |
for (auto &E : Elts) { |
| 1657 |
StringRef Name = E->getName(); |
| 1658 |
assert_with_loc(Name.startswith("sv"), |
| 1659 |
"Incorrect element kind in shuffle mask!"); |
| 1660 |
S += ", " + Name.drop_front(2).str(); |
| 1661 |
} |
| 1662 |
S += ")"; |
| 1663 |
|
| 1664 |
// Recalculate the return type - the shuffle may have halved or doubled it. |
| 1665 |
Type T(Arg1.first); |
| 1666 |
if (Elts.size() > T.getNumElements()) { |
| 1667 |
assert_with_loc( |
| 1668 |
Elts.size() == T.getNumElements() * 2, |
| 1669 |
"Can only double or half the number of elements in a shuffle!"); |
| 1670 |
T.doubleLanes(); |
| 1671 |
} else if (Elts.size() < T.getNumElements()) { |
| 1672 |
assert_with_loc( |
| 1673 |
Elts.size() == T.getNumElements() / 2, |
| 1674 |
"Can only double or half the number of elements in a shuffle!"); |
| 1675 |
T.halveLanes(); |
| 1676 |
} |
| 1677 |
|
| 1678 |
return std::make_pair(T, S); |
| 1679 |
} |
| 1680 |
|
| 1681 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) { |
| 1682 |
assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument"); |
| 1683 |
std::pair<Type, std::string> A = |
| 1684 |
emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); |
| 1685 |
assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument"); |
| 1686 |
|
| 1687 |
Type T = Intr.getBaseType(); |
| 1688 |
assert_with_loc(T.isVector(), "dup() used but default type is scalar!"); |
| 1689 |
std::string S = "(" + T.str() + ") {"; |
| 1690 |
for (unsigned I = 0; I < T.getNumElements(); ++I) { |
| 1691 |
if (I != 0) |
| 1692 |
S += ", "; |
| 1693 |
S += A.second; |
| 1694 |
} |
| 1695 |
S += "}"; |
| 1696 |
|
| 1697 |
return std::make_pair(T, S); |
| 1698 |
} |
| 1699 |
|
| 1700 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDupTyped(DagInit *DI) { |
| 1701 |
assert_with_loc(DI->getNumArgs() == 2, "dup_typed() expects two arguments"); |
| 1702 |
std::pair<Type, std::string> B = |
| 1703 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1704 |
assert_with_loc(B.first.isScalar(), |
| 1705 |
"dup_typed() requires a scalar as the second argument"); |
| 1706 |
Type T; |
| 1707 |
// If the type argument is a constant string, construct the type directly. |
| 1708 |
if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) { |
| 1709 |
T = Type::fromTypedefName(SI->getAsUnquotedString()); |
| 1710 |
assert_with_loc(!T.isVoid(), "Unknown typedef"); |
| 1711 |
} else |
| 1712 |
T = emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))).first; |
| 1713 |
|
| 1714 |
assert_with_loc(T.isVector(), "dup_typed() used but target type is scalar!"); |
| 1715 |
std::string S = "(" + T.str() + ") {"; |
| 1716 |
for (unsigned I = 0; I < T.getNumElements(); ++I) { |
| 1717 |
if (I != 0) |
| 1718 |
S += ", "; |
| 1719 |
S += B.second; |
| 1720 |
} |
| 1721 |
S += "}"; |
| 1722 |
|
| 1723 |
return std::make_pair(T, S); |
| 1724 |
} |
| 1725 |
|
| 1726 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) { |
| 1727 |
assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments"); |
| 1728 |
std::pair<Type, std::string> A = |
| 1729 |
emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); |
| 1730 |
std::pair<Type, std::string> B = |
| 1731 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1732 |
|
| 1733 |
assert_with_loc(B.first.isScalar(), |
| 1734 |
"splat() requires a scalar int as the second argument"); |
| 1735 |
|
| 1736 |
std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second; |
| 1737 |
for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) { |
| 1738 |
S += ", " + B.second; |
| 1739 |
} |
| 1740 |
S += ")"; |
| 1741 |
|
| 1742 |
return std::make_pair(Intr.getBaseType(), S); |
| 1743 |
} |
| 1744 |
|
| 1745 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) { |
| 1746 |
assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments"); |
| 1747 |
std::pair<Type, std::string> A = |
| 1748 |
emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); |
| 1749 |
|
| 1750 |
assert_with_loc(!A.first.isVoid(), |
| 1751 |
"Argument to save_temp() must have non-void type!"); |
| 1752 |
|
| 1753 |
std::string N = std::string(DI->getArgNameStr(0)); |
| 1754 |
assert_with_loc(!N.empty(), |
| 1755 |
"save_temp() expects a name as the first argument"); |
| 1756 |
|
| 1757 |
assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(), |
| 1758 |
"Variable already defined!"); |
| 1759 |
Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix); |
| 1760 |
|
| 1761 |
std::string S = |
| 1762 |
A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second; |
| 1763 |
|
| 1764 |
return std::make_pair(Type::getVoid(), S); |
| 1765 |
} |
| 1766 |
|
| 1767 |
std::pair<Type, std::string> |
| 1768 |
Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) { |
| 1769 |
std::string S = Intr.Name; |
| 1770 |
|
| 1771 |
assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!"); |
| 1772 |
std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1773 |
std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); |
| 1774 |
|
| 1775 |
size_t Idx = S.find(ToReplace); |
| 1776 |
|
| 1777 |
assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!"); |
| 1778 |
S.replace(Idx, ToReplace.size(), ReplaceWith); |
| 1779 |
|
| 1780 |
return std::make_pair(Type::getVoid(), S); |
| 1781 |
} |
| 1782 |
|
| 1783 |
std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){ |
| 1784 |
std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1785 |
std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); |
| 1786 |
return std::make_pair(Type::fromTypedefName(Ty), Value); |
| 1787 |
} |
| 1788 |
|
| 1789 |
std::pair<Type, std::string> |
| 1790 |
Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) { |
| 1791 |
if (!ArgName.empty()) { |
| 1792 |
assert_with_loc(!Arg->isComplete(), |
| 1793 |
"Arguments must either be DAGs or names, not both!"); |
| 1794 |
assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(), |
| 1795 |
"Variable not defined!"); |
| 1796 |
Variable &V = Intr.Variables[ArgName]; |
| 1797 |
return std::make_pair(V.getType(), V.getName()); |
| 1798 |
} |
| 1799 |
|
| 1800 |
assert(Arg && "Neither ArgName nor Arg?!"); |
| 1801 |
DagInit *DI = dyn_cast<DagInit>(Arg); |
| 1802 |
assert_with_loc(DI, "Arguments must either be DAGs or names!"); |
| 1803 |
|
| 1804 |
return emitDag(DI); |
| 1805 |
} |
| 1806 |
|
| 1807 |
std::string Intrinsic::generate() { |
| 1808 |
// Avoid duplicated code for big and little endian |
| 1809 |
if (isBigEndianSafe()) { |
| 1810 |
generateImpl(false, "", ""); |
| 1811 |
return OS.str(); |
| 1812 |
} |
| 1813 |
// Little endian intrinsics are simple and don't require any argument |
| 1814 |
// swapping. |
| 1815 |
OS << "#ifdef __LITTLE_ENDIAN__\n"; |
| 1816 |
|
| 1817 |
generateImpl(false, "", ""); |
| 1818 |
|
| 1819 |
OS << "#else\n"; |
| 1820 |
|
| 1821 |
// Big endian intrinsics are more complex. The user intended these |
| 1822 |
// intrinsics to operate on a vector "as-if" loaded by (V)LDR, |
| 1823 |
// but we load as-if (V)LD1. So we should swap all arguments and |
| 1824 |
// swap the return value too. |
| 1825 |
// |
| 1826 |
// If we call sub-intrinsics, we should call a version that does |
| 1827 |
// not re-swap the arguments! |
| 1828 |
generateImpl(true, "", "__noswap_"); |
| 1829 |
|
| 1830 |
// If we're needed early, create a non-swapping variant for |
| 1831 |
// big-endian. |
| 1832 |
if (NeededEarly) { |
| 1833 |
generateImpl(false, "__noswap_", "__noswap_"); |
| 1834 |
} |
| 1835 |
OS << "#endif\n\n"; |
| 1836 |
|
| 1837 |
return OS.str(); |
| 1838 |
} |
| 1839 |
|
| 1840 |
void Intrinsic::generateImpl(bool ReverseArguments, |
| 1841 |
StringRef NamePrefix, StringRef CallPrefix) { |
| 1842 |
CurrentRecord = R; |
| 1843 |
|
| 1844 |
// If we call a macro, our local variables may be corrupted due to |
| 1845 |
// lack of proper lexical scoping. So, add a globally unique postfix |
| 1846 |
// to every variable. |
| 1847 |
// |
| 1848 |
// indexBody() should have set up the Dependencies set by now. |
| 1849 |
for (auto *I : Dependencies) |
| 1850 |
if (I->UseMacro) { |
| 1851 |
VariablePostfix = "_" + utostr(Emitter.getUniqueNumber()); |
| 1852 |
break; |
| 1853 |
} |
| 1854 |
|
| 1855 |
initVariables(); |
| 1856 |
|
| 1857 |
emitPrototype(NamePrefix); |
| 1858 |
|
| 1859 |
if (IsUnavailable) { |
| 1860 |
OS << " __attribute__((unavailable));"; |
| 1861 |
} else { |
| 1862 |
emitOpeningBrace(); |
| 1863 |
// Emit return variable declaration first as to not trigger |
| 1864 |
// -Wdeclaration-after-statement. |
| 1865 |
emitReturnVarDecl(); |
| 1866 |
emitShadowedArgs(); |
| 1867 |
if (ReverseArguments) |
| 1868 |
emitArgumentReversal(); |
| 1869 |
emitBody(CallPrefix); |
| 1870 |
if (ReverseArguments) |
| 1871 |
emitReturnReversal(); |
| 1872 |
emitReturn(); |
| 1873 |
emitClosingBrace(); |
| 1874 |
} |
| 1875 |
OS << "\n"; |
| 1876 |
|
| 1877 |
CurrentRecord = nullptr; |
| 1878 |
} |
| 1879 |
|
| 1880 |
void Intrinsic::indexBody() { |
| 1881 |
CurrentRecord = R; |
| 1882 |
|
| 1883 |
initVariables(); |
| 1884 |
// Emit return variable declaration first as to not trigger |
| 1885 |
// -Wdeclaration-after-statement. |
| 1886 |
emitReturnVarDecl(); |
| 1887 |
emitBody(""); |
| 1888 |
OS.str(""); |
| 1889 |
|
| 1890 |
CurrentRecord = nullptr; |
| 1891 |
} |
| 1892 |
|
| 1893 |
//===----------------------------------------------------------------------===// |
| 1894 |
// NeonEmitter implementation |
| 1895 |
//===----------------------------------------------------------------------===// |
| 1896 |
|
| 1897 |
Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types, |
| 1898 |
std::optional<std::string> MangledName) { |
| 1899 |
// First, look up the name in the intrinsic map. |
| 1900 |
assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(), |
| 1901 |
("Intrinsic '" + Name + "' not found!").str()); |
| 1902 |
auto &V = IntrinsicMap.find(Name.str())->second; |
| 1903 |
std::vector<Intrinsic *> GoodVec; |
| 1904 |
|
| 1905 |
// Create a string to print if we end up failing. |
| 1906 |
std::string ErrMsg = "looking up intrinsic '" + Name.str() + "("; |
| 1907 |
for (unsigned I = 0; I < Types.size(); ++I) { |
| 1908 |
if (I != 0) |
| 1909 |
ErrMsg += ", "; |
| 1910 |
ErrMsg += Types[I].str(); |
| 1911 |
} |
| 1912 |
ErrMsg += ")'\n"; |
| 1913 |
ErrMsg += "Available overloads:\n"; |
| 1914 |
|
| 1915 |
// Now, look through each intrinsic implementation and see if the types are |
| 1916 |
// compatible. |
| 1917 |
for (auto &I : V) { |
| 1918 |
ErrMsg += " - " + I.getReturnType().str() + " " + I.getMangledName(); |
| 1919 |
ErrMsg += "("; |
| 1920 |
for (unsigned A = 0; A < I.getNumParams(); ++A) { |
| 1921 |
if (A != 0) |
| 1922 |
ErrMsg += ", "; |
| 1923 |
ErrMsg += I.getParamType(A).str(); |
| 1924 |
} |
| 1925 |
ErrMsg += ")\n"; |
| 1926 |
|
| 1927 |
if (MangledName && MangledName != I.getMangledName(true)) |
| 1928 |
continue; |
| 1929 |
|
| 1930 |
if (I.getNumParams() != Types.size()) |
| 1931 |
continue; |
| 1932 |
|
| 1933 |
unsigned ArgNum = 0; |
| 1934 |
bool MatchingArgumentTypes = llvm::all_of(Types, [&](const auto &Type) { |
| 1935 |
return Type == I.getParamType(ArgNum++); |
| 1936 |
}); |
| 1937 |
|
| 1938 |
if (MatchingArgumentTypes) |
| 1939 |
GoodVec.push_back(&I); |
| 1940 |
} |
| 1941 |
|
| 1942 |
assert_with_loc(!GoodVec.empty(), |
| 1943 |
"No compatible intrinsic found - " + ErrMsg); |
| 1944 |
assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg); |
| 1945 |
|
| 1946 |
return *GoodVec.front(); |
| 1947 |
} |
| 1948 |
|
| 1949 |
void NeonEmitter::createIntrinsic(Record *R, |
| 1950 |
SmallVectorImpl<Intrinsic *> &Out) { |
| 1951 |
std::string Name = std::string(R->getValueAsString("Name")); |
| 1952 |
std::string Proto = std::string(R->getValueAsString("Prototype")); |
| 1953 |
std::string Types = std::string(R->getValueAsString("Types")); |
| 1954 |
Record *OperationRec = R->getValueAsDef("Operation"); |
| 1955 |
bool BigEndianSafe = R->getValueAsBit("BigEndianSafe"); |
| 1956 |
std::string ArchGuard = std::string(R->getValueAsString("ArchGuard")); |
| 1957 |
std::string TargetGuard = std::string(R->getValueAsString("TargetGuard")); |
| 1958 |
bool IsUnavailable = OperationRec->getValueAsBit("Unavailable"); |
| 1959 |
std::string CartesianProductWith = std::string(R->getValueAsString("CartesianProductWith")); |
| 1960 |
|
| 1961 |
// Set the global current record. This allows assert_with_loc to produce |
| 1962 |
// decent location information even when highly nested. |
| 1963 |
CurrentRecord = R; |
| 1964 |
|
| 1965 |
ListInit *Body = OperationRec->getValueAsListInit("Ops"); |
| 1966 |
|
| 1967 |
std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types); |
| 1968 |
|
| 1969 |
ClassKind CK = ClassNone; |
| 1970 |
if (R->getSuperClasses().size() >= 2) |
| 1971 |
CK = ClassMap[R->getSuperClasses()[1].first]; |
| 1972 |
|
| 1973 |
std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs; |
| 1974 |
if (!CartesianProductWith.empty()) { |
| 1975 |
std::vector<TypeSpec> ProductTypeSpecs = TypeSpec::fromTypeSpecs(CartesianProductWith); |
| 1976 |
for (auto TS : TypeSpecs) { |
| 1977 |
Type DefaultT(TS, "."); |
| 1978 |
for (auto SrcTS : ProductTypeSpecs) { |
| 1979 |
Type DefaultSrcT(SrcTS, "."); |
| 1980 |
if (TS == SrcTS || |
| 1981 |
DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits()) |
| 1982 |
continue; |
| 1983 |
NewTypeSpecs.push_back(std::make_pair(TS, SrcTS)); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} else { |
| 1987 |
for (auto TS : TypeSpecs) { |
| 1988 |
NewTypeSpecs.push_back(std::make_pair(TS, TS)); |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
llvm::sort(NewTypeSpecs); |
| 1993 |
NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()), |
| 1994 |
NewTypeSpecs.end()); |
| 1995 |
auto &Entry = IntrinsicMap[Name]; |
| 1996 |
|
| 1997 |
for (auto &I : NewTypeSpecs) { |
| 1998 |
Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this, |
| 1999 |
ArchGuard, TargetGuard, IsUnavailable, BigEndianSafe); |
| 2000 |
Out.push_back(&Entry.back()); |
| 2001 |
} |
| 2002 |
|
| 2003 |
CurrentRecord = nullptr; |
| 2004 |
} |
| 2005 |
|
| 2006 |
/// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def |
| 2007 |
/// declaration of builtins, checking for unique builtin declarations. |
| 2008 |
void NeonEmitter::genBuiltinsDef(raw_ostream &OS, |
| 2009 |
SmallVectorImpl<Intrinsic *> &Defs) { |
| 2010 |
OS << "#ifdef GET_NEON_BUILTINS\n"; |
| 2011 |
|
| 2012 |
// We only want to emit a builtin once, and we want to emit them in |
| 2013 |
// alphabetical order, so use a std::set. |
| 2014 |
std::set<std::pair<std::string, std::string>> Builtins; |
| 2015 |
|
| 2016 |
for (auto *Def : Defs) { |
| 2017 |
if (Def->hasBody()) |
| 2018 |
continue; |
| 2019 |
|
| 2020 |
std::string S = "__builtin_neon_" + Def->getMangledName() + ", \""; |
| 2021 |
S += Def->getBuiltinTypeStr(); |
| 2022 |
S += "\", \"n\""; |
| 2023 |
|
| 2024 |
Builtins.emplace(S, Def->getTargetGuard()); |
| 2025 |
} |
| 2026 |
|
| 2027 |
for (auto &S : Builtins) { |
| 2028 |
if (S.second == "") |
| 2029 |
OS << "BUILTIN("; |
| 2030 |
else |
| 2031 |
OS << "TARGET_BUILTIN("; |
| 2032 |
OS << S.first; |
| 2033 |
if (S.second == "") |
| 2034 |
OS << ")\n"; |
| 2035 |
else |
| 2036 |
OS << ", \"" << S.second << "\")\n"; |
| 2037 |
} |
| 2038 |
|
| 2039 |
OS << "#endif\n\n"; |
| 2040 |
} |
| 2041 |
|
| 2042 |
/// Generate the ARM and AArch64 overloaded type checking code for |
| 2043 |
/// SemaChecking.cpp, checking for unique builtin declarations. |
| 2044 |
void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS, |
| 2045 |
SmallVectorImpl<Intrinsic *> &Defs) { |
| 2046 |
OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 2047 |
|
| 2048 |
// We record each overload check line before emitting because subsequent Inst |
| 2049 |
// definitions may extend the number of permitted types (i.e. augment the |
| 2050 |
// Mask). Use std::map to avoid sorting the table by hash number. |
| 2051 |
struct OverloadInfo { |
| 2052 |
uint64_t Mask; |
| 2053 |
int PtrArgNum; |
| 2054 |
bool HasConstPtr; |
| 2055 |
OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {} |
| 2056 |
}; |
| 2057 |
std::map<std::string, OverloadInfo> OverloadMap; |
| 2058 |
|
| 2059 |
for (auto *Def : Defs) { |
| 2060 |
// If the def has a body (that is, it has Operation DAGs), it won't call |
| 2061 |
// __builtin_neon_* so we don't need to generate a definition for it. |
| 2062 |
if (Def->hasBody()) |
| 2063 |
continue; |
| 2064 |
// Functions which have a scalar argument cannot be overloaded, no need to |
| 2065 |
// check them if we are emitting the type checking code. |
| 2066 |
if (Def->protoHasScalar()) |
| 2067 |
continue; |
| 2068 |
|
| 2069 |
uint64_t Mask = 0ULL; |
| 2070 |
Mask |= 1ULL << Def->getPolymorphicKeyType().getNeonEnum(); |
| 2071 |
|
| 2072 |
// Check if the function has a pointer or const pointer argument. |
| 2073 |
int PtrArgNum = -1; |
| 2074 |
bool HasConstPtr = false; |
| 2075 |
for (unsigned I = 0; I < Def->getNumParams(); ++I) { |
| 2076 |
const auto &Type = Def->getParamType(I); |
| 2077 |
if (Type.isPointer()) { |
| 2078 |
PtrArgNum = I; |
| 2079 |
HasConstPtr = Type.isConstPointer(); |
| 2080 |
} |
| 2081 |
} |
| 2082 |
|
| 2083 |
// For sret builtins, adjust the pointer argument index. |
| 2084 |
if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1) |
| 2085 |
PtrArgNum += 1; |
| 2086 |
|
| 2087 |
std::string Name = Def->getName(); |
| 2088 |
// Omit type checking for the pointer arguments of vld1_lane, vld1_dup, |
| 2089 |
// and vst1_lane intrinsics. Using a pointer to the vector element |
| 2090 |
// type with one of those operations causes codegen to select an aligned |
| 2091 |
// load/store instruction. If you want an unaligned operation, |
| 2092 |
// the pointer argument needs to have less alignment than element type, |
| 2093 |
// so just accept any pointer type. |
| 2094 |
if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") { |
| 2095 |
PtrArgNum = -1; |
| 2096 |
HasConstPtr = false; |
| 2097 |
} |
| 2098 |
|
| 2099 |
if (Mask) { |
| 2100 |
std::string Name = Def->getMangledName(); |
| 2101 |
OverloadMap.insert(std::make_pair(Name, OverloadInfo())); |
| 2102 |
OverloadInfo &OI = OverloadMap[Name]; |
| 2103 |
OI.Mask |= Mask; |
| 2104 |
OI.PtrArgNum |= PtrArgNum; |
| 2105 |
OI.HasConstPtr = HasConstPtr; |
| 2106 |
} |
| 2107 |
} |
| 2108 |
|
| 2109 |
for (auto &I : OverloadMap) { |
| 2110 |
OverloadInfo &OI = I.second; |
| 2111 |
|
| 2112 |
OS << "case NEON::BI__builtin_neon_" << I.first << ": "; |
| 2113 |
OS << "mask = 0x" << Twine::utohexstr(OI.Mask) << "ULL"; |
| 2114 |
if (OI.PtrArgNum >= 0) |
| 2115 |
OS << "; PtrArgNum = " << OI.PtrArgNum; |
| 2116 |
if (OI.HasConstPtr) |
| 2117 |
OS << "; HasConstPtr = true"; |
| 2118 |
OS << "; break;\n"; |
| 2119 |
} |
| 2120 |
OS << "#endif\n\n"; |
| 2121 |
} |
| 2122 |
|
| 2123 |
void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, |
| 2124 |
SmallVectorImpl<Intrinsic *> &Defs) { |
| 2125 |
OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 2126 |
|
| 2127 |
std::set<std::string> Emitted; |
| 2128 |
|
| 2129 |
for (auto *Def : Defs) { |
| 2130 |
if (Def->hasBody()) |
| 2131 |
continue; |
| 2132 |
// Functions which do not have an immediate do not need to have range |
| 2133 |
// checking code emitted. |
| 2134 |
if (!Def->hasImmediate()) |
| 2135 |
continue; |
| 2136 |
if (Emitted.find(Def->getMangledName()) != Emitted.end()) |
| 2137 |
continue; |
| 2138 |
|
| 2139 |
std::string LowerBound, UpperBound; |
| 2140 |
|
| 2141 |
Record *R = Def->getRecord(); |
| 2142 |
if (R->getValueAsBit("isVXAR")) { |
| 2143 |
//VXAR takes an immediate in the range [0, 63] |
| 2144 |
LowerBound = "0"; |
| 2145 |
UpperBound = "63"; |
| 2146 |
} else if (R->getValueAsBit("isVCVT_N")) { |
| 2147 |
// VCVT between floating- and fixed-point values takes an immediate |
| 2148 |
// in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16. |
| 2149 |
LowerBound = "1"; |
| 2150 |
if (Def->getBaseType().getElementSizeInBits() == 16 || |
| 2151 |
Def->getName().find('h') != std::string::npos) |
| 2152 |
// VCVTh operating on FP16 intrinsics in range [1, 16) |
| 2153 |
UpperBound = "15"; |
| 2154 |
else if (Def->getBaseType().getElementSizeInBits() == 32) |
| 2155 |
UpperBound = "31"; |
| 2156 |
else |
| 2157 |
UpperBound = "63"; |
| 2158 |
} else if (R->getValueAsBit("isScalarShift")) { |
| 2159 |
// Right shifts have an 'r' in the name, left shifts do not. Convert |
| 2160 |
// instructions have the same bounds and right shifts. |
| 2161 |
if (Def->getName().find('r') != std::string::npos || |
| 2162 |
Def->getName().find("cvt") != std::string::npos) |
| 2163 |
LowerBound = "1"; |
| 2164 |
|
| 2165 |
UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1); |
| 2166 |
} else if (R->getValueAsBit("isShift")) { |
| 2167 |
// Builtins which are overloaded by type will need to have their upper |
| 2168 |
// bound computed at Sema time based on the type constant. |
| 2169 |
|
| 2170 |
// Right shifts have an 'r' in the name, left shifts do not. |
| 2171 |
if (Def->getName().find('r') != std::string::npos) |
| 2172 |
LowerBound = "1"; |
| 2173 |
UpperBound = "RFT(TV, true)"; |
| 2174 |
} else if (Def->getClassKind(true) == ClassB) { |
| 2175 |
// ClassB intrinsics have a type (and hence lane number) that is only |
| 2176 |
// known at runtime. |
| 2177 |
if (R->getValueAsBit("isLaneQ")) |
| 2178 |
UpperBound = "RFT(TV, false, true)"; |
| 2179 |
else |
| 2180 |
UpperBound = "RFT(TV, false, false)"; |
| 2181 |
} else { |
| 2182 |
// The immediate generally refers to a lane in the preceding argument. |
| 2183 |
assert(Def->getImmediateIdx() > 0); |
| 2184 |
Type T = Def->getParamType(Def->getImmediateIdx() - 1); |
| 2185 |
UpperBound = utostr(T.getNumElements() - 1); |
| 2186 |
} |
| 2187 |
|
| 2188 |
// Calculate the index of the immediate that should be range checked. |
| 2189 |
unsigned Idx = Def->getNumParams(); |
| 2190 |
if (Def->hasImmediate()) |
| 2191 |
Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx()); |
| 2192 |
|
| 2193 |
OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": " |
| 2194 |
<< "i = " << Idx << ";"; |
| 2195 |
if (!LowerBound.empty()) |
| 2196 |
OS << " l = " << LowerBound << ";"; |
| 2197 |
if (!UpperBound.empty()) |
| 2198 |
OS << " u = " << UpperBound << ";"; |
| 2199 |
OS << " break;\n"; |
| 2200 |
|
| 2201 |
Emitted.insert(Def->getMangledName()); |
| 2202 |
} |
| 2203 |
|
| 2204 |
OS << "#endif\n\n"; |
| 2205 |
} |
| 2206 |
|
| 2207 |
/// runHeader - Emit a file with sections defining: |
| 2208 |
/// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def. |
| 2209 |
/// 2. the SemaChecking code for the type overload checking. |
| 2210 |
/// 3. the SemaChecking code for validation of intrinsic immediate arguments. |
| 2211 |
void NeonEmitter::runHeader(raw_ostream &OS) { |
| 2212 |
std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2213 |
|
| 2214 |
SmallVector<Intrinsic *, 128> Defs; |
| 2215 |
for (auto *R : RV) |
| 2216 |
createIntrinsic(R, Defs); |
| 2217 |
|
| 2218 |
// Generate shared BuiltinsXXX.def |
| 2219 |
genBuiltinsDef(OS, Defs); |
| 2220 |
|
| 2221 |
// Generate ARM overloaded type checking code for SemaChecking.cpp |
| 2222 |
genOverloadTypeCheckCode(OS, Defs); |
| 2223 |
|
| 2224 |
// Generate ARM range checking code for shift/lane immediates. |
| 2225 |
genIntrinsicRangeCheckCode(OS, Defs); |
| 2226 |
} |
| 2227 |
|
| 2228 |
static void emitNeonTypeDefs(const std::string& types, raw_ostream &OS) { |
| 2229 |
std::string TypedefTypes(types); |
| 2230 |
std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes); |
| 2231 |
|
| 2232 |
// Emit vector typedefs. |
| 2233 |
bool InIfdef = false; |
| 2234 |
for (auto &TS : TDTypeVec) { |
| 2235 |
bool IsA64 = false; |
| 2236 |
Type T(TS, "."); |
| 2237 |
if (T.isDouble()) |
| 2238 |
IsA64 = true; |
| 2239 |
|
| 2240 |
if (InIfdef && !IsA64) { |
| 2241 |
OS << "#endif\n"; |
| 2242 |
InIfdef = false; |
| 2243 |
} |
| 2244 |
if (!InIfdef && IsA64) { |
| 2245 |
OS << "#ifdef __aarch64__\n"; |
| 2246 |
InIfdef = true; |
| 2247 |
} |
| 2248 |
|
| 2249 |
if (T.isPoly()) |
| 2250 |
OS << "typedef __attribute__((neon_polyvector_type("; |
| 2251 |
else |
| 2252 |
OS << "typedef __attribute__((neon_vector_type("; |
| 2253 |
|
| 2254 |
Type T2 = T; |
| 2255 |
T2.makeScalar(); |
| 2256 |
OS << T.getNumElements() << "))) "; |
| 2257 |
OS << T2.str(); |
| 2258 |
OS << " " << T.str() << ";\n"; |
| 2259 |
} |
| 2260 |
if (InIfdef) |
| 2261 |
OS << "#endif\n"; |
| 2262 |
OS << "\n"; |
| 2263 |
|
| 2264 |
// Emit struct typedefs. |
| 2265 |
InIfdef = false; |
| 2266 |
for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) { |
| 2267 |
for (auto &TS : TDTypeVec) { |
| 2268 |
bool IsA64 = false; |
| 2269 |
Type T(TS, "."); |
| 2270 |
if (T.isDouble()) |
| 2271 |
IsA64 = true; |
| 2272 |
|
| 2273 |
if (InIfdef && !IsA64) { |
| 2274 |
OS << "#endif\n"; |
| 2275 |
InIfdef = false; |
| 2276 |
} |
| 2277 |
if (!InIfdef && IsA64) { |
| 2278 |
OS << "#ifdef __aarch64__\n"; |
| 2279 |
InIfdef = true; |
| 2280 |
} |
| 2281 |
|
| 2282 |
const char Mods[] = { static_cast<char>('2' + (NumMembers - 2)), 0}; |
| 2283 |
Type VT(TS, Mods); |
| 2284 |
OS << "typedef struct " << VT.str() << " {\n"; |
| 2285 |
OS << " " << T.str() << " val"; |
| 2286 |
OS << "[" << NumMembers << "]"; |
| 2287 |
OS << ";\n} "; |
| 2288 |
OS << VT.str() << ";\n"; |
| 2289 |
OS << "\n"; |
| 2290 |
} |
| 2291 |
} |
| 2292 |
if (InIfdef) |
| 2293 |
OS << "#endif\n"; |
| 2294 |
} |
| 2295 |
|
| 2296 |
/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 2297 |
/// is comprised of type definitions and function declarations. |
| 2298 |
void NeonEmitter::run(raw_ostream &OS) { |
| 2299 |
OS << "/*===---- arm_neon.h - ARM Neon intrinsics " |
| 2300 |
"------------------------------" |
| 2301 |
"---===\n" |
| 2302 |
" *\n" |
| 2303 |
" * Permission is hereby granted, free of charge, to any person " |
| 2304 |
"obtaining " |
| 2305 |
"a copy\n" |
| 2306 |
" * of this software and associated documentation files (the " |
| 2307 |
"\"Software\")," |
| 2308 |
" to deal\n" |
| 2309 |
" * in the Software without restriction, including without limitation " |
| 2310 |
"the " |
| 2311 |
"rights\n" |
| 2312 |
" * to use, copy, modify, merge, publish, distribute, sublicense, " |
| 2313 |
"and/or sell\n" |
| 2314 |
" * copies of the Software, and to permit persons to whom the Software " |
| 2315 |
"is\n" |
| 2316 |
" * furnished to do so, subject to the following conditions:\n" |
| 2317 |
" *\n" |
| 2318 |
" * The above copyright notice and this permission notice shall be " |
| 2319 |
"included in\n" |
| 2320 |
" * all copies or substantial portions of the Software.\n" |
| 2321 |
" *\n" |
| 2322 |
" * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " |
| 2323 |
"EXPRESS OR\n" |
| 2324 |
" * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
| 2325 |
"MERCHANTABILITY,\n" |
| 2326 |
" * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " |
| 2327 |
"SHALL THE\n" |
| 2328 |
" * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " |
| 2329 |
"OTHER\n" |
| 2330 |
" * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " |
| 2331 |
"ARISING FROM,\n" |
| 2332 |
" * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " |
| 2333 |
"DEALINGS IN\n" |
| 2334 |
" * THE SOFTWARE.\n" |
| 2335 |
" *\n" |
| 2336 |
" *===-----------------------------------------------------------------" |
| 2337 |
"---" |
| 2338 |
"---===\n" |
| 2339 |
" */\n\n"; |
| 2340 |
|
| 2341 |
OS << "#ifndef __ARM_NEON_H\n"; |
| 2342 |
OS << "#define __ARM_NEON_H\n\n"; |
| 2343 |
|
| 2344 |
OS << "#ifndef __ARM_FP\n"; |
| 2345 |
OS << "#error \"NEON intrinsics not available with the soft-float ABI. " |
| 2346 |
"Please use -mfloat-abi=softfp or -mfloat-abi=hard\"\n"; |
| 2347 |
OS << "#else\n\n"; |
| 2348 |
|
| 2349 |
OS << "#if !defined(__ARM_NEON)\n"; |
| 2350 |
OS << "#error \"NEON support not enabled\"\n"; |
| 2351 |
OS << "#else\n\n"; |
| 2352 |
|
| 2353 |
OS << "#include <stdint.h>\n\n"; |
| 2354 |
|
| 2355 |
OS << "#include <arm_bf16.h>\n"; |
| 2356 |
OS << "typedef __bf16 bfloat16_t;\n"; |
| 2357 |
|
| 2358 |
// Emit NEON-specific scalar typedefs. |
| 2359 |
OS << "typedef float float32_t;\n"; |
| 2360 |
OS << "typedef __fp16 float16_t;\n"; |
| 2361 |
|
| 2362 |
OS << "#ifdef __aarch64__\n"; |
| 2363 |
OS << "typedef double float64_t;\n"; |
| 2364 |
OS << "#endif\n\n"; |
| 2365 |
|
| 2366 |
// For now, signedness of polynomial types depends on target |
| 2367 |
OS << "#ifdef __aarch64__\n"; |
| 2368 |
OS << "typedef uint8_t poly8_t;\n"; |
| 2369 |
OS << "typedef uint16_t poly16_t;\n"; |
| 2370 |
OS << "typedef uint64_t poly64_t;\n"; |
| 2371 |
OS << "typedef __uint128_t poly128_t;\n"; |
| 2372 |
OS << "#else\n"; |
| 2373 |
OS << "typedef int8_t poly8_t;\n"; |
| 2374 |
OS << "typedef int16_t poly16_t;\n"; |
| 2375 |
OS << "typedef int64_t poly64_t;\n"; |
| 2376 |
OS << "#endif\n"; |
| 2377 |
|
| 2378 |
emitNeonTypeDefs("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl", OS); |
| 2379 |
|
| 2380 |
emitNeonTypeDefs("bQb", OS); |
| 2381 |
|
| 2382 |
OS << "#define __ai static __inline__ __attribute__((__always_inline__, " |
| 2383 |
"__nodebug__))\n\n"; |
| 2384 |
|
| 2385 |
SmallVector<Intrinsic *, 128> Defs; |
| 2386 |
std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2387 |
for (auto *R : RV) |
| 2388 |
createIntrinsic(R, Defs); |
| 2389 |
|
| 2390 |
for (auto *I : Defs) |
| 2391 |
I->indexBody(); |
| 2392 |
|
| 2393 |
llvm::stable_sort(Defs, llvm::deref<std::less<>>()); |
| 2394 |
|
| 2395 |
// Only emit a def when its requirements have been met. |
| 2396 |
// FIXME: This loop could be made faster, but it's fast enough for now. |
| 2397 |
bool MadeProgress = true; |
| 2398 |
std::string InGuard; |
| 2399 |
while (!Defs.empty() && MadeProgress) { |
| 2400 |
MadeProgress = false; |
| 2401 |
|
| 2402 |
for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); |
| 2403 |
I != Defs.end(); /*No step*/) { |
| 2404 |
bool DependenciesSatisfied = true; |
| 2405 |
for (auto *II : (*I)->getDependencies()) { |
| 2406 |
if (llvm::is_contained(Defs, II)) |
| 2407 |
DependenciesSatisfied = false; |
| 2408 |
} |
| 2409 |
if (!DependenciesSatisfied) { |
| 2410 |
// Try the next one. |
| 2411 |
++I; |
| 2412 |
continue; |
| 2413 |
} |
| 2414 |
|
| 2415 |
// Emit #endif/#if pair if needed. |
| 2416 |
if ((*I)->getArchGuard() != InGuard) { |
| 2417 |
if (!InGuard.empty()) |
| 2418 |
OS << "#endif\n"; |
| 2419 |
InGuard = (*I)->getArchGuard(); |
| 2420 |
if (!InGuard.empty()) |
| 2421 |
OS << "#if " << InGuard << "\n"; |
| 2422 |
} |
| 2423 |
|
| 2424 |
// Actually generate the intrinsic code. |
| 2425 |
OS << (*I)->generate(); |
| 2426 |
|
| 2427 |
MadeProgress = true; |
| 2428 |
I = Defs.erase(I); |
| 2429 |
} |
| 2430 |
} |
| 2431 |
assert(Defs.empty() && "Some requirements were not satisfied!"); |
| 2432 |
if (!InGuard.empty()) |
| 2433 |
OS << "#endif\n"; |
| 2434 |
|
| 2435 |
OS << "\n"; |
| 2436 |
OS << "#undef __ai\n\n"; |
| 2437 |
OS << "#endif /* if !defined(__ARM_NEON) */\n"; |
| 2438 |
OS << "#endif /* ifndef __ARM_FP */\n"; |
| 2439 |
OS << "#endif /* __ARM_NEON_H */\n"; |
| 2440 |
} |
| 2441 |
|
| 2442 |
/// run - Read the records in arm_fp16.td and output arm_fp16.h. arm_fp16.h |
| 2443 |
/// is comprised of type definitions and function declarations. |
| 2444 |
void NeonEmitter::runFP16(raw_ostream &OS) { |
| 2445 |
OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics " |
| 2446 |
"------------------------------" |
| 2447 |
"---===\n" |
| 2448 |
" *\n" |
| 2449 |
" * Permission is hereby granted, free of charge, to any person " |
| 2450 |
"obtaining a copy\n" |
| 2451 |
" * of this software and associated documentation files (the " |
| 2452 |
"\"Software\"), to deal\n" |
| 2453 |
" * in the Software without restriction, including without limitation " |
| 2454 |
"the rights\n" |
| 2455 |
" * to use, copy, modify, merge, publish, distribute, sublicense, " |
| 2456 |
"and/or sell\n" |
| 2457 |
" * copies of the Software, and to permit persons to whom the Software " |
| 2458 |
"is\n" |
| 2459 |
" * furnished to do so, subject to the following conditions:\n" |
| 2460 |
" *\n" |
| 2461 |
" * The above copyright notice and this permission notice shall be " |
| 2462 |
"included in\n" |
| 2463 |
" * all copies or substantial portions of the Software.\n" |
| 2464 |
" *\n" |
| 2465 |
" * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " |
| 2466 |
"EXPRESS OR\n" |
| 2467 |
" * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
| 2468 |
"MERCHANTABILITY,\n" |
| 2469 |
" * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " |
| 2470 |
"SHALL THE\n" |
| 2471 |
" * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " |
| 2472 |
"OTHER\n" |
| 2473 |
" * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " |
| 2474 |
"ARISING FROM,\n" |
| 2475 |
" * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " |
| 2476 |
"DEALINGS IN\n" |
| 2477 |
" * THE SOFTWARE.\n" |
| 2478 |
" *\n" |
| 2479 |
" *===-----------------------------------------------------------------" |
| 2480 |
"---" |
| 2481 |
"---===\n" |
| 2482 |
" */\n\n"; |
| 2483 |
|
| 2484 |
OS << "#ifndef __ARM_FP16_H\n"; |
| 2485 |
OS << "#define __ARM_FP16_H\n\n"; |
| 2486 |
|
| 2487 |
OS << "#include <stdint.h>\n\n"; |
| 2488 |
|
| 2489 |
OS << "typedef __fp16 float16_t;\n"; |
| 2490 |
|
| 2491 |
OS << "#define __ai static __inline__ __attribute__((__always_inline__, " |
| 2492 |
"__nodebug__))\n\n"; |
| 2493 |
|
| 2494 |
SmallVector<Intrinsic *, 128> Defs; |
| 2495 |
std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2496 |
for (auto *R : RV) |
| 2497 |
createIntrinsic(R, Defs); |
| 2498 |
|
| 2499 |
for (auto *I : Defs) |
| 2500 |
I->indexBody(); |
| 2501 |
|
| 2502 |
llvm::stable_sort(Defs, llvm::deref<std::less<>>()); |
| 2503 |
|
| 2504 |
// Only emit a def when its requirements have been met. |
| 2505 |
// FIXME: This loop could be made faster, but it's fast enough for now. |
| 2506 |
bool MadeProgress = true; |
| 2507 |
std::string InGuard; |
| 2508 |
while (!Defs.empty() && MadeProgress) { |
| 2509 |
MadeProgress = false; |
| 2510 |
|
| 2511 |
for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); |
| 2512 |
I != Defs.end(); /*No step*/) { |
| 2513 |
bool DependenciesSatisfied = true; |
| 2514 |
for (auto *II : (*I)->getDependencies()) { |
| 2515 |
if (llvm::is_contained(Defs, II)) |
| 2516 |
DependenciesSatisfied = false; |
| 2517 |
} |
| 2518 |
if (!DependenciesSatisfied) { |
| 2519 |
// Try the next one. |
| 2520 |
++I; |
| 2521 |
continue; |
| 2522 |
} |
| 2523 |
|
| 2524 |
// Emit #endif/#if pair if needed. |
| 2525 |
if ((*I)->getArchGuard() != InGuard) { |
| 2526 |
if (!InGuard.empty()) |
| 2527 |
OS << "#endif\n"; |
| 2528 |
InGuard = (*I)->getArchGuard(); |
| 2529 |
if (!InGuard.empty()) |
| 2530 |
OS << "#if " << InGuard << "\n"; |
| 2531 |
} |
| 2532 |
|
| 2533 |
// Actually generate the intrinsic code. |
| 2534 |
OS << (*I)->generate(); |
| 2535 |
|
| 2536 |
MadeProgress = true; |
| 2537 |
I = Defs.erase(I); |
| 2538 |
} |
| 2539 |
} |
| 2540 |
assert(Defs.empty() && "Some requirements were not satisfied!"); |
| 2541 |
if (!InGuard.empty()) |
| 2542 |
OS << "#endif\n"; |
| 2543 |
|
| 2544 |
OS << "\n"; |
| 2545 |
OS << "#undef __ai\n\n"; |
| 2546 |
OS << "#endif /* __ARM_FP16_H */\n"; |
| 2547 |
} |
| 2548 |
|
| 2549 |
void NeonEmitter::runBF16(raw_ostream &OS) { |
| 2550 |
OS << "/*===---- arm_bf16.h - ARM BF16 intrinsics " |
| 2551 |
"-----------------------------------===\n" |
| 2552 |
" *\n" |
| 2553 |
" *\n" |
| 2554 |
" * Part of the LLVM Project, under the Apache License v2.0 with LLVM " |
| 2555 |
"Exceptions.\n" |
| 2556 |
" * See https://llvm.org/LICENSE.txt for license information.\n" |
| 2557 |
" * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" |
| 2558 |
" *\n" |
| 2559 |
" *===-----------------------------------------------------------------" |
| 2560 |
"------===\n" |
| 2561 |
" */\n\n"; |
| 2562 |
|
| 2563 |
OS << "#ifndef __ARM_BF16_H\n"; |
| 2564 |
OS << "#define __ARM_BF16_H\n\n"; |
| 2565 |
|
| 2566 |
OS << "typedef __bf16 bfloat16_t;\n"; |
| 2567 |
|
| 2568 |
OS << "#define __ai static __inline__ __attribute__((__always_inline__, " |
| 2569 |
"__nodebug__))\n\n"; |
| 2570 |
|
| 2571 |
SmallVector<Intrinsic *, 128> Defs; |
| 2572 |
std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2573 |
for (auto *R : RV) |
| 2574 |
createIntrinsic(R, Defs); |
| 2575 |
|
| 2576 |
for (auto *I : Defs) |
| 2577 |
I->indexBody(); |
| 2578 |
|
| 2579 |
llvm::stable_sort(Defs, llvm::deref<std::less<>>()); |
| 2580 |
|
| 2581 |
// Only emit a def when its requirements have been met. |
| 2582 |
// FIXME: This loop could be made faster, but it's fast enough for now. |
| 2583 |
bool MadeProgress = true; |
| 2584 |
std::string InGuard; |
| 2585 |
while (!Defs.empty() && MadeProgress) { |
| 2586 |
MadeProgress = false; |
| 2587 |
|
| 2588 |
for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); |
| 2589 |
I != Defs.end(); /*No step*/) { |
| 2590 |
bool DependenciesSatisfied = true; |
| 2591 |
for (auto *II : (*I)->getDependencies()) { |
| 2592 |
if (llvm::is_contained(Defs, II)) |
| 2593 |
DependenciesSatisfied = false; |
| 2594 |
} |
| 2595 |
if (!DependenciesSatisfied) { |
| 2596 |
// Try the next one. |
| 2597 |
++I; |
| 2598 |
continue; |
| 2599 |
} |
| 2600 |
|
| 2601 |
// Emit #endif/#if pair if needed. |
| 2602 |
if ((*I)->getArchGuard() != InGuard) { |
| 2603 |
if (!InGuard.empty()) |
| 2604 |
OS << "#endif\n"; |
| 2605 |
InGuard = (*I)->getArchGuard(); |
| 2606 |
if (!InGuard.empty()) |
| 2607 |
OS << "#if " << InGuard << "\n"; |
| 2608 |
} |
| 2609 |
|
| 2610 |
// Actually generate the intrinsic code. |
| 2611 |
OS << (*I)->generate(); |
| 2612 |
|
| 2613 |
MadeProgress = true; |
| 2614 |
I = Defs.erase(I); |
| 2615 |
} |
| 2616 |
} |
| 2617 |
assert(Defs.empty() && "Some requirements were not satisfied!"); |
| 2618 |
if (!InGuard.empty()) |
| 2619 |
OS << "#endif\n"; |
| 2620 |
|
| 2621 |
OS << "\n"; |
| 2622 |
OS << "#undef __ai\n\n"; |
| 2623 |
|
| 2624 |
OS << "#endif\n"; |
| 2625 |
} |
| 2626 |
|
| 2627 |
void clang::EmitNeon(RecordKeeper &Records, raw_ostream &OS) { |
| 2628 |
NeonEmitter(Records).run(OS); |
| 2629 |
} |
| 2630 |
|
| 2631 |
void clang::EmitFP16(RecordKeeper &Records, raw_ostream &OS) { |
| 2632 |
NeonEmitter(Records).runFP16(OS); |
| 2633 |
} |
| 2634 |
|
| 2635 |
void clang::EmitBF16(RecordKeeper &Records, raw_ostream &OS) { |
| 2636 |
NeonEmitter(Records).runBF16(OS); |
| 2637 |
} |
| 2638 |
|
| 2639 |
void clang::EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) { |
| 2640 |
NeonEmitter(Records).runHeader(OS); |
| 2641 |
} |
| 2642 |
|
| 2643 |
void clang::EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) { |
| 2644 |
llvm_unreachable("Neon test generation no longer implemented!"); |
| 2645 |
} |