clang 23.0.0git
RISCVVIntrinsicUtils.h
Go to the documentation of this file.
1//===--- RISCVVIntrinsicUtils.h - RISC-V Vector Intrinsic Utils -*- 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#ifndef CLANG_SUPPORT_RISCVVINTRINSICUTILS_H
10#define CLANG_SUPPORT_RISCVVINTRINSICUTILS_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/BitmaskEnum.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
16#include <cstdint>
17#include <optional>
18#include <set>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23namespace llvm {
24class raw_ostream;
25} // end namespace llvm
26
27namespace clang {
28namespace RISCV {
29
30using VScaleVal = std::optional<unsigned>;
31
32// Modifier for vector type.
76
77// Similar to basic type but used to describe what's kind of type related to
78// basic vector type, used to compute type info of arguments.
90
91// Modifier for type, used for both scalar and vector types.
92enum class TypeModifier : uint8_t {
94 Pointer = 1 << 0,
95 Const = 1 << 1,
96 Immediate = 1 << 2,
98 SignedInteger = 1 << 4,
99 Float = 1 << 5,
100 BFloat = 1 << 6,
101 // LMUL1 should be kind of VectorTypeModifier, but that might come with
102 // Widening2XVector for widening reduction.
103 // However that might require VectorTypeModifier become bitmask rather than
104 // simple enum, so we decide keek LMUL1 in TypeModifier for code size
105 // optimization of clang binary size.
106 LMUL1 = 1 << 7,
109};
110
111class Policy {
112public:
117
118private:
119 // The default assumption for an RVV instruction is TAMA, as an undisturbed
120 // policy generally will affect the performance of an out-of-order core.
121 const PolicyType TailPolicy = Agnostic;
122 const PolicyType MaskPolicy = Agnostic;
123
124public:
125 Policy() = default;
126 Policy(PolicyType TailPolicy) : TailPolicy(TailPolicy) {}
127 Policy(PolicyType TailPolicy, PolicyType MaskPolicy)
128 : TailPolicy(TailPolicy), MaskPolicy(MaskPolicy) {}
129
130 bool isTAMAPolicy() const {
131 return TailPolicy == Agnostic && MaskPolicy == Agnostic;
132 }
133
134 bool isTAMUPolicy() const {
135 return TailPolicy == Agnostic && MaskPolicy == Undisturbed;
136 }
137
138 bool isTUMAPolicy() const {
139 return TailPolicy == Undisturbed && MaskPolicy == Agnostic;
140 }
141
142 bool isTUMUPolicy() const {
143 return TailPolicy == Undisturbed && MaskPolicy == Undisturbed;
144 }
145
146 bool isTAPolicy() const { return TailPolicy == Agnostic; }
147
148 bool isTUPolicy() const { return TailPolicy == Undisturbed; }
149
150 bool isMAPolicy() const { return MaskPolicy == Agnostic; }
151
152 bool isMUPolicy() const { return MaskPolicy == Undisturbed; }
153
154 bool operator==(const Policy &Other) const {
155 return TailPolicy == Other.TailPolicy && MaskPolicy == Other.MaskPolicy;
156 }
157
158 bool operator!=(const Policy &Other) const { return !(*this == Other); }
159
160 bool operator<(const Policy &Other) const {
161 // Just for maintain the old order for quick test.
162 if (MaskPolicy != Other.MaskPolicy)
163 return Other.MaskPolicy < MaskPolicy;
164 return TailPolicy < Other.TailPolicy;
165 }
166};
167
168// PrototypeDescriptor is used to compute type info of arguments or return
169// value.
171 constexpr PrototypeDescriptor() = default;
177 constexpr PrototypeDescriptor(uint8_t PT, uint8_t VTM, uint8_t TM)
178 : PT(static_cast<BaseTypeModifier>(PT)),
179 VTM(static_cast<VectorTypeModifier>(VTM)),
180 TM(static_cast<TypeModifier>(TM)) {}
181
185
186 bool operator!=(const PrototypeDescriptor &PD) const {
187 return !(*this == PD);
188 }
189 bool operator==(const PrototypeDescriptor &PD) const {
190 return PD.PT == PT && PD.VTM == VTM && PD.TM == TM;
191 }
192 bool operator<(const PrototypeDescriptor &PD) const {
193 return std::tie(PT, VTM, TM) < std::tie(PD.PT, PD.VTM, PD.TM);
194 }
198 static std::optional<PrototypeDescriptor>
199 parsePrototypeDescriptor(llvm::StringRef PrototypeStr);
200};
201
203parsePrototypes(llvm::StringRef Prototypes);
204
205// Basic type of vector type.
206enum class BasicType : uint16_t {
208 Int8 = 1 << 0,
209 Int16 = 1 << 1,
210 Int32 = 1 << 2,
211 Int64 = 1 << 3,
212 BFloat16 = 1 << 4,
213 Float16 = 1 << 5,
214 Float32 = 1 << 6,
215 Float64 = 1 << 7,
216 F8E4M3 = 1 << 8,
217 F8E5M2 = 1 << 9,
220};
221
222// Type of vector type.
239
240// Exponential LMUL
241struct LMULType {
243 LMULType(int Log2LMUL);
244 // Return the C/C++ string representation of LMUL
245 std::string str() const;
246 std::optional<unsigned> getScale(unsigned ElementBitwidth) const;
247 void MulLog2LMUL(int Log2LMUL);
248};
249
250class RVVType;
252using RVVTypes = std::vector<RVVTypePtr>;
253class RVVTypeCache;
254
255// This class is compact representation of a valid and invalid RVVType.
256class RVVType {
257 friend class RVVTypeCache;
258
259 BasicType BT;
260 ScalarTypeKind ScalarType = Undefined;
261 LMULType LMUL;
262 bool IsPointer = false;
263 // IsConstant indices are "int", but have the constant expression.
264 bool IsImmediate = false;
265 // Const qualifier for pointer to const object or object of const type.
266 bool IsConstant = false;
267 unsigned ElementBitwidth = 0;
268 VScaleVal Scale = 0;
269 bool Valid;
270 bool IsTuple = false;
271 unsigned NF = 0;
272
273 std::string BuiltinStr;
274 std::string ClangBuiltinStr;
275 std::string Str;
276 std::string ShortStr;
277
278 enum class FixedLMULType { LargerThan, SmallerThan, SmallerOrEqual };
279
280 RVVType(BasicType BT, int Log2LMUL, const PrototypeDescriptor &Profile);
281
282public:
283 // Return the string representation of a type, which is an encoded string for
284 // passing to the BUILTIN() macro in Builtins.def.
285 const std::string &getBuiltinStr() const { return BuiltinStr; }
286
287 // Return the clang builtin type for RVV vector type which are used in the
288 // riscv_vector.h header file.
289 const std::string &getClangBuiltinStr() const { return ClangBuiltinStr; }
290
291 // Return the C/C++ string representation of a type for use in the
292 // riscv_vector.h header file.
293 const std::string &getTypeStr() const { return Str; }
294
295 // Return the short name of a type for C/C++ name suffix.
296 const std::string &getShortStr() {
297 // Not all types are used in short name, so compute the short name by
298 // demanded.
299 if (ShortStr.empty())
300 initShortStr();
301 return ShortStr;
302 }
303
304 bool isValid() const { return Valid; }
305 bool isScalar() const { return Scale && *Scale == 0; }
306 bool isVector() const { return Scale && *Scale != 0; }
307 bool isVector(unsigned Width) const {
308 return isVector() && ElementBitwidth == Width;
309 }
310 bool isFloat() const { return ScalarType == ScalarTypeKind::Float; }
311 bool isBFloat() const { return ScalarType == ScalarTypeKind::BFloat; }
312 bool isSignedInteger() const {
313 return ScalarType == ScalarTypeKind::SignedInteger;
314 }
315 bool isFloatVector(unsigned Width) const {
316 return isVector() && isFloat() && ElementBitwidth == Width;
317 }
318 bool isFloat(unsigned Width) const {
319 return isFloat() && ElementBitwidth == Width;
320 }
321 bool isConstant() const { return IsConstant; }
322 bool isPointer() const { return IsPointer; }
323 bool isTuple() const { return IsTuple; }
324 unsigned getElementBitwidth() const { return ElementBitwidth; }
325
326 ScalarTypeKind getScalarType() const { return ScalarType; }
327 VScaleVal getScale() const { return Scale; }
328 unsigned getNF() const {
329 assert(NF > 1 && NF <= 8 && "Only legal NF should be fetched");
330 return NF;
331 }
332
333private:
334 // Verify RVV vector type and set Valid.
335 bool verifyType() const;
336
337 // Creates a type based on basic types of TypeRange
338 void applyBasicType();
339
340 // Applies a prototype modifier to the current type. The result maybe an
341 // invalid type.
342 void applyModifier(const PrototypeDescriptor &prototype);
343
344 void applyLog2EEW(unsigned Log2EEW);
345 void applyFixedSEW(unsigned NewSEW);
346 void applyFixedLog2LMUL(int Log2LMUL, enum FixedLMULType Type);
347
348 // Compute and record a string for legal type.
349 void initBuiltinStr();
350 // Compute and record a builtin RVV vector type string.
351 void initClangBuiltinStr();
352 // Compute and record a type string for used in the header.
353 void initTypeStr();
354 // Compute and record a short name of a type for C/C++ name suffix.
355 void initShortStr();
356};
357
358// This class is used to manage RVVType, RVVType should only created by this
359// class, also provided thread-safe cache capability.
361private:
362 std::unordered_map<uint64_t, RVVType> LegalTypes;
363 std::set<uint64_t> IllegalTypes;
364
365public:
366 /// Compute output and input types by applying different config (basic type
367 /// and LMUL with type transformers). It also record result of type in legal
368 /// or illegal set to avoid compute the same config again. The result maybe
369 /// have illegal RVVType.
370 std::optional<RVVTypes>
371 computeTypes(BasicType BT, int Log2LMUL, unsigned NF,
373 std::optional<RVVTypePtr> computeType(BasicType BT, int Log2LMUL,
374 PrototypeDescriptor Proto);
375};
376
377enum PolicyScheme : uint8_t {
379 // Passthru operand is at first parameter in C builtin.
382};
383
384llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, enum PolicyScheme PS);
385
386// TODO refactor RVVIntrinsic class design after support all intrinsic
387// combination. This represents an instantiation of an intrinsic with a
388// particular type and prototype
390
391private:
392 std::string BuiltinName; // Builtin name
393 std::string Name; // C intrinsic name.
394 std::string OverloadedName;
395 std::string IRName;
396 bool IsMasked;
397 bool HasMaskedOffOperand;
398 bool HasVL;
399 PolicyScheme Scheme;
400 bool SupportOverloading;
401 bool HasBuiltinAlias;
402 std::string ManualCodegen;
403 RVVTypePtr OutputType; // Builtin output type
404 RVVTypes InputTypes; // Builtin input types
405 // The types we use to obtain the specific LLVM intrinsic. They are index of
406 // InputTypes. -1 means the return type.
407 std::vector<int64_t> IntrinsicTypes;
408 unsigned NF = 1;
409 Policy PolicyAttrs;
410 unsigned TWiden = 0;
411
412public:
413 RVVIntrinsic(llvm::StringRef Name, llvm::StringRef Suffix,
414 llvm::StringRef OverloadedName, llvm::StringRef OverloadedSuffix,
415 llvm::StringRef IRName, bool IsMasked, bool HasMaskedOffOperand,
416 bool HasVL, PolicyScheme Scheme, bool SupportOverloading,
417 bool HasBuiltinAlias, llvm::StringRef ManualCodegen,
418 const RVVTypes &Types,
419 const std::vector<int64_t> &IntrinsicTypes, unsigned NF,
420 Policy PolicyAttrs, bool HasFRMRoundModeOp, unsigned TWiden,
421 bool AltFmt);
422 ~RVVIntrinsic() = default;
423
424 RVVTypePtr getOutputType() const { return OutputType; }
425 const RVVTypes &getInputTypes() const { return InputTypes; }
426 llvm::StringRef getBuiltinName() const { return BuiltinName; }
427 bool hasMaskedOffOperand() const { return HasMaskedOffOperand; }
428 bool hasVL() const { return HasVL; }
429 bool hasPolicy() const { return Scheme != PolicyScheme::SchemeNone; }
430 bool hasPassthruOperand() const {
431 return Scheme == PolicyScheme::HasPassthruOperand;
432 }
433 bool hasPolicyOperand() const {
434 return Scheme == PolicyScheme::HasPolicyOperand;
435 }
436 bool supportOverloading() const { return SupportOverloading; }
437 bool hasBuiltinAlias() const { return HasBuiltinAlias; }
438 bool hasManualCodegen() const { return !ManualCodegen.empty(); }
439 bool isMasked() const { return IsMasked; }
440 llvm::StringRef getOverloadedName() const { return OverloadedName; }
441 llvm::StringRef getIRName() const { return IRName; }
442 llvm::StringRef getManualCodegen() const { return ManualCodegen; }
443 PolicyScheme getPolicyScheme() const { return Scheme; }
444 unsigned getNF() const { return NF; }
445 unsigned getTWiden() const { return TWiden; }
446 const std::vector<int64_t> &getIntrinsicTypes() const {
447 return IntrinsicTypes;
448 }
450 return PolicyAttrs;
451 }
452 unsigned getPolicyAttrsBits() const {
453 // CGBuiltin.cpp
454 // The 0th bit simulates the `vta` of RVV
455 // The 1st bit simulates the `vma` of RVV
456 // int PolicyAttrs = 0;
457
458 if (PolicyAttrs.isTUMAPolicy())
459 return 2;
460 if (PolicyAttrs.isTAMAPolicy())
461 return 3;
462 if (PolicyAttrs.isTUMUPolicy())
463 return 0;
464 if (PolicyAttrs.isTAMUPolicy())
465 return 1;
466
467 llvm_unreachable("unsupport policy");
468 return 0;
469 }
470
471 // Return the type string for a BUILTIN() macro in Builtins.def.
472 std::string getBuiltinTypeStr() const;
473
474 static std::string
475 getSuffixStr(RVVTypeCache &TypeCache, BasicType Type, int Log2LMUL,
476 llvm::ArrayRef<PrototypeDescriptor> PrototypeDescriptors);
477
480 bool IsMasked, bool HasMaskedOffOperand, bool HasVL,
481 unsigned NF, PolicyScheme DefaultScheme,
482 Policy PolicyAttrs, bool IsTuple);
483
486 getSupportedMaskedPolicies(bool HasTailPolicy, bool HasMaskPolicy);
487
488 static void updateNamesAndPolicy(bool IsMasked, bool HasPolicy,
489 std::string &Name, std::string &BuiltinName,
490 std::string &OverloadedName,
491 Policy &PolicyAttrs, bool HasFRMRoundModeOp,
492 bool AltFmt);
493};
494
495// Raw RVV intrinsic info, used to expand later.
496// This struct is highly compact for minimized code size.
498 // Intrinsic name, e.g. vadd_vv
499 const char *Name;
500
501 // Overloaded intrinsic name, could be empty if it can be computed from Name.
502 // e.g. vadd
503 const char *OverloadedName;
504
505 // Required target features for this intrinsic.
507
508 // Prototype for this intrinsic, index of RVVSignatureTable.
510
511 // Suffix of intrinsic name, index of RVVSignatureTable.
512 uint16_t SuffixIndex;
513
514 // Suffix of overloaded intrinsic name, index of RVVSignatureTable.
516
517 // Length of the prototype.
519
520 // Length of intrinsic name suffix.
522
523 // Length of overloaded intrinsic suffix.
525
526 // Supported type, mask of BasicType.
528
529 // Supported LMUL.
531
532 // Number of fields, greater than 1 if it's segment load/store.
533 uint8_t NF;
534
535 bool HasMasked : 1;
536 bool HasVL : 1;
541 bool AltFmt : 1;
542 bool IsTuple : 1;
543 LLVM_PREFERRED_TYPE(PolicyScheme)
545 LLVM_PREFERRED_TYPE(PolicyScheme)
547};
548
549llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
550 const RVVIntrinsicRecord &RVVInstrRecord);
551
553} // end namespace RISCV
554
555} // end namespace clang
556
557#endif // CLANG_SUPPORT_RISCVVINTRINSICUTILS_H
Policy(PolicyType TailPolicy)
bool operator==(const Policy &Other) const
bool operator!=(const Policy &Other) const
bool operator<(const Policy &Other) const
Policy(PolicyType TailPolicy, PolicyType MaskPolicy)
static llvm::SmallVector< Policy > getSupportedMaskedPolicies(bool HasTailPolicy, bool HasMaskPolicy)
llvm::StringRef getBuiltinName() const
llvm::StringRef getIRName() const
const std::vector< int64_t > & getIntrinsicTypes() const
static llvm::SmallVector< PrototypeDescriptor > computeBuiltinTypes(llvm::ArrayRef< PrototypeDescriptor > Prototype, bool IsMasked, bool HasMaskedOffOperand, bool HasVL, unsigned NF, PolicyScheme DefaultScheme, Policy PolicyAttrs, bool IsTuple)
llvm::StringRef getOverloadedName() const
static std::string getSuffixStr(RVVTypeCache &TypeCache, BasicType Type, int Log2LMUL, llvm::ArrayRef< PrototypeDescriptor > PrototypeDescriptors)
RVVIntrinsic(llvm::StringRef Name, llvm::StringRef Suffix, llvm::StringRef OverloadedName, llvm::StringRef OverloadedSuffix, llvm::StringRef IRName, bool IsMasked, bool HasMaskedOffOperand, bool HasVL, PolicyScheme Scheme, bool SupportOverloading, bool HasBuiltinAlias, llvm::StringRef ManualCodegen, const RVVTypes &Types, const std::vector< int64_t > &IntrinsicTypes, unsigned NF, Policy PolicyAttrs, bool HasFRMRoundModeOp, unsigned TWiden, bool AltFmt)
static void updateNamesAndPolicy(bool IsMasked, bool HasPolicy, std::string &Name, std::string &BuiltinName, std::string &OverloadedName, Policy &PolicyAttrs, bool HasFRMRoundModeOp, bool AltFmt)
static llvm::SmallVector< Policy > getSupportedUnMaskedPolicies()
llvm::StringRef getManualCodegen() const
const RVVTypes & getInputTypes() const
PolicyScheme getPolicyScheme() const
std::optional< RVVTypePtr > computeType(BasicType BT, int Log2LMUL, PrototypeDescriptor Proto)
std::optional< RVVTypes > computeTypes(BasicType BT, int Log2LMUL, unsigned NF, llvm::ArrayRef< PrototypeDescriptor > Prototype)
Compute output and input types by applying different config (basic type and LMUL with type transforme...
const std::string & getTypeStr() const
ScalarTypeKind getScalarType() const
bool isFloatVector(unsigned Width) const
const std::string & getShortStr()
const std::string & getClangBuiltinStr() const
bool isFloat(unsigned Width) const
bool isVector(unsigned Width) const
unsigned getElementBitwidth() const
const std::string & getBuiltinStr() const
The base class of the type hierarchy.
Definition TypeBase.h:1866
RISCV builtins.
std::optional< unsigned > VScaleVal
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, enum PolicyScheme PS)
llvm::SmallVector< PrototypeDescriptor > parsePrototypes(llvm::StringRef Prototypes)
std::vector< RVVTypePtr > RVVTypes
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
Definition Decl.h:1761
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
std::optional< unsigned > getScale(unsigned ElementBitwidth) const
bool operator!=(const PrototypeDescriptor &PD) const
static std::optional< PrototypeDescriptor > parsePrototypeDescriptor(llvm::StringRef PrototypeStr)
static const PrototypeDescriptor VL
constexpr PrototypeDescriptor()=default
static const PrototypeDescriptor Mask
constexpr PrototypeDescriptor(uint8_t PT, uint8_t VTM, uint8_t TM)
static const PrototypeDescriptor Vector
constexpr PrototypeDescriptor(BaseTypeModifier PT, VectorTypeModifier VTM=VectorTypeModifier::NoModifier, TypeModifier TM=TypeModifier::NoModifier)
bool operator==(const PrototypeDescriptor &PD) const
bool operator<(const PrototypeDescriptor &PD) const