clang 23.0.0git
RISCVVIntrinsicUtils.cpp
Go to the documentation of this file.
1//===- RISCVVIntrinsicUtils.cpp - 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
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/Support/ErrorHandling.h"
14#include "llvm/Support/raw_ostream.h"
15#include <optional>
16
17using namespace llvm;
18
19namespace clang {
20namespace RISCV {
21
28
29//===----------------------------------------------------------------------===//
30// Type implementation
31//===----------------------------------------------------------------------===//
32
33LMULType::LMULType(int NewLog2LMUL) {
34 // Check Log2LMUL is -3, -2, -1, 0, 1, 2, 3
35 assert(NewLog2LMUL <= 3 && NewLog2LMUL >= -3 && "Bad LMUL number!");
36 Log2LMUL = NewLog2LMUL;
37}
38
39std::string LMULType::str() const {
40 if (Log2LMUL < 0)
41 return "mf" + utostr(1ULL << (-Log2LMUL));
42 return "m" + utostr(1ULL << Log2LMUL);
43}
44
45VScaleVal LMULType::getScale(unsigned ElementBitwidth) const {
46 int Log2ScaleResult = 0;
47 switch (ElementBitwidth) {
48 default:
49 break;
50 case 8:
51 Log2ScaleResult = Log2LMUL + 3;
52 break;
53 case 16:
54 Log2ScaleResult = Log2LMUL + 2;
55 break;
56 case 32:
57 Log2ScaleResult = Log2LMUL + 1;
58 break;
59 case 64:
60 Log2ScaleResult = Log2LMUL;
61 break;
62 }
63 // Illegal vscale result would be less than 1
64 if (Log2ScaleResult < 0)
65 return std::nullopt;
66 return 1 << Log2ScaleResult;
67}
68
69void LMULType::MulLog2LMUL(int log2LMUL) { Log2LMUL += log2LMUL; }
70
71RVVType::RVVType(BasicType BT, int Log2LMUL,
72 const PrototypeDescriptor &prototype)
73 : BT(BT), LMUL(LMULType(Log2LMUL)) {
74 applyBasicType();
75 applyModifier(prototype);
76 Valid = verifyType();
77 if (Valid) {
78 initBuiltinStr();
79 initTypeStr();
80 if (isVector()) {
81 initClangBuiltinStr();
82 }
83 }
84}
85
86// clang-format off
87// boolean type are encoded the ratio of n (SEW/LMUL)
88// SEW/LMUL | 1 | 2 | 4 | 8 | 16 | 32 | 64
89// c type | vbool64_t | vbool32_t | vbool16_t | vbool8_t | vbool4_t | vbool2_t | vbool1_t
90// IR type | nxv1i1 | nxv2i1 | nxv4i1 | nxv8i1 | nxv16i1 | nxv32i1 | nxv64i1
91
92// type\lmul | 1/8 | 1/4 | 1/2 | 1 | 2 | 4 | 8
93// -------- |------ | -------- | ------- | ------- | -------- | -------- | --------
94// i64 | N/A | N/A | N/A | nxv1i64 | nxv2i64 | nxv4i64 | nxv8i64
95// i32 | N/A | N/A | nxv1i32 | nxv2i32 | nxv4i32 | nxv8i32 | nxv16i32
96// i16 | N/A | nxv1i16 | nxv2i16 | nxv4i16 | nxv8i16 | nxv16i16 | nxv32i16
97// i8 | nxv1i8 | nxv2i8 | nxv4i8 | nxv8i8 | nxv16i8 | nxv32i8 | nxv64i8
98// double | N/A | N/A | N/A | nxv1f64 | nxv2f64 | nxv4f64 | nxv8f64
99// float | N/A | N/A | nxv1f32 | nxv2f32 | nxv4f32 | nxv8f32 | nxv16f32
100// half | N/A | nxv1f16 | nxv2f16 | nxv4f16 | nxv8f16 | nxv16f16 | nxv32f16
101// bfloat16 | N/A | nxv1bf16 | nxv2bf16| nxv4bf16| nxv8bf16 | nxv16bf16| nxv32bf16
102// clang-format on
103
104bool RVVType::verifyType() const {
105 if (ScalarType == Invalid)
106 return false;
107 if (isScalar())
108 return true;
109 if (!Scale)
110 return false;
111 if (isFloat() && ElementBitwidth == 8)
112 return false;
113 if (isBFloat() && ElementBitwidth != 16)
114 return false;
115 if (IsTuple && (NF == 1 || NF > 8))
116 return false;
117 if (IsTuple && (1 << std::max(0, LMUL.Log2LMUL)) * NF > 8)
118 return false;
119 unsigned V = *Scale;
120 switch (ElementBitwidth) {
121 case 1:
122 case 8:
123 // Check Scale is 1,2,4,8,16,32,64
124 return (V <= 64 && isPowerOf2_32(V));
125 case 16:
126 // Check Scale is 1,2,4,8,16,32
127 return (V <= 32 && isPowerOf2_32(V));
128 case 32:
129 // Check Scale is 1,2,4,8,16
130 return (V <= 16 && isPowerOf2_32(V));
131 case 64:
132 // Check Scale is 1,2,4,8
133 return (V <= 8 && isPowerOf2_32(V));
134 }
135 return false;
136}
137
138void RVVType::initBuiltinStr() {
139 assert(isValid() && "RVVType is invalid");
140 switch (ScalarType) {
142 BuiltinStr = "v";
143 return;
145 BuiltinStr = "z";
146 if (IsImmediate)
147 BuiltinStr = "I" + BuiltinStr;
148 if (IsPointer)
149 BuiltinStr += "*";
150 return;
152 BuiltinStr = "Y";
153 return;
155 BuiltinStr = "ULi";
156 return;
158 BuiltinStr = "Li";
159 return;
161 assert(ElementBitwidth == 1);
162 BuiltinStr += "b";
163 break;
166 switch (ElementBitwidth) {
167 case 8:
168 BuiltinStr += "c";
169 break;
170 case 16:
171 BuiltinStr += "s";
172 break;
173 case 32:
174 BuiltinStr += "i";
175 break;
176 case 64:
177 BuiltinStr += "Wi";
178 break;
179 default:
180 llvm_unreachable("Unhandled ElementBitwidth!");
181 }
182 if (isSignedInteger())
183 BuiltinStr = "S" + BuiltinStr;
184 else
185 BuiltinStr = "U" + BuiltinStr;
186 break;
188 switch (ElementBitwidth) {
189 case 16:
190 BuiltinStr += "x";
191 break;
192 case 32:
193 BuiltinStr += "f";
194 break;
195 case 64:
196 BuiltinStr += "d";
197 break;
198 default:
199 llvm_unreachable("Unhandled ElementBitwidth!");
200 }
201 break;
203 BuiltinStr += "y";
204 break;
206 BuiltinStr += "a";
207 break;
209 BuiltinStr += "b";
210 break;
211 default:
212 llvm_unreachable("ScalarType is invalid!");
213 }
214 if (IsImmediate)
215 BuiltinStr = "I" + BuiltinStr;
216 if (isScalar()) {
217 if (IsConstant)
218 BuiltinStr += "C";
219 if (IsPointer)
220 BuiltinStr += "*";
221 return;
222 }
223 BuiltinStr = "q" + utostr(*Scale) + BuiltinStr;
224 // Pointer to vector types. Defined for segment load intrinsics.
225 // segment load intrinsics have pointer type arguments to store the loaded
226 // vector values.
227 if (IsPointer)
228 BuiltinStr += "*";
229
230 if (IsTuple)
231 BuiltinStr = "T" + utostr(NF) + BuiltinStr;
232}
233
234void RVVType::initClangBuiltinStr() {
235 assert(isValid() && "RVVType is invalid");
236 assert(isVector() && "Handle Vector type only");
237
238 ClangBuiltinStr = "__rvv_";
239 switch (ScalarType) {
241 ClangBuiltinStr += "bool" + utostr(64 / *Scale) + "_t";
242 return;
244 ClangBuiltinStr += "float";
245 break;
247 ClangBuiltinStr += "bfloat";
248 break;
250 ClangBuiltinStr += "int";
251 break;
253 ClangBuiltinStr += "uint";
254 break;
256 ClangBuiltinStr += "float8e4m3" + LMUL.str() + "_t";
257 return;
259 ClangBuiltinStr += "float8e5m2" + LMUL.str() + "_t";
260 return;
261 default:
262 llvm_unreachable("ScalarTypeKind is invalid");
263 }
264 ClangBuiltinStr += utostr(ElementBitwidth) + LMUL.str() +
265 (IsTuple ? "x" + utostr(NF) : "") + "_t";
266}
267
268void RVVType::initTypeStr() {
269 assert(isValid() && "RVVType is invalid");
270
271 if (IsConstant)
272 Str += "const ";
273
274 auto getTypeString = [&](StringRef TypeStr) {
275 if (isScalar())
276 return Twine(TypeStr + Twine(ElementBitwidth) + "_t").str();
277 return Twine("v" + TypeStr + Twine(ElementBitwidth) + LMUL.str() +
278 (IsTuple ? "x" + utostr(NF) : "") + "_t")
279 .str();
280 };
281
282 switch (ScalarType) {
284 Str = "void";
285 return;
287 Str = "size_t";
288 if (IsPointer)
289 Str += " *";
290 return;
292 Str = "ptrdiff_t";
293 return;
295 Str = "unsigned long";
296 return;
298 Str = "long";
299 return;
301 if (isScalar())
302 Str += "bool";
303 else
304 // Vector bool is special case, the formulate is
305 // `vbool<N>_t = MVT::nxv<64/N>i1` ex. vbool16_t = MVT::4i1
306 Str += "vbool" + utostr(64 / *Scale) + "_t";
307 break;
309 if (isScalar()) {
310 if (ElementBitwidth == 64)
311 Str += "double";
312 else if (ElementBitwidth == 32)
313 Str += "float";
314 else if (ElementBitwidth == 16)
315 Str += "_Float16";
316 else
317 llvm_unreachable("Unhandled floating type.");
318 } else
319 Str += getTypeString("float");
320 break;
322 if (isScalar()) {
323 if (ElementBitwidth == 16)
324 Str += "__bf16";
325 else
326 llvm_unreachable("Unhandled floating type.");
327 } else
328 Str += getTypeString("bfloat");
329 break;
331 Str += getTypeString("int");
332 break;
334 Str += getTypeString("uint");
335 break;
337 Str += "vfloat8e4m3" + LMUL.str() + "_t";
338 break;
340 Str += "vfloat8e5m2" + LMUL.str() + "_t";
341 break;
342 default:
343 llvm_unreachable("ScalarType is invalid!");
344 }
345 if (IsPointer)
346 Str += " *";
347}
348
349void RVVType::initShortStr() {
350 switch (ScalarType) {
352 assert(isVector());
353 ShortStr = "b" + utostr(64 / *Scale);
354 return;
356 ShortStr = "f" + utostr(ElementBitwidth);
357 break;
359 ShortStr = "bf" + utostr(ElementBitwidth);
360 break;
362 ShortStr = "i" + utostr(ElementBitwidth);
363 break;
365 ShortStr = "u" + utostr(ElementBitwidth);
366 break;
368 ShortStr = "f8e4m3";
369 break;
371 ShortStr = "f8e5m2";
372 break;
373 default:
374 llvm_unreachable("Unhandled case!");
375 }
376 if (isVector())
377 ShortStr += LMUL.str();
378 if (isTuple())
379 ShortStr += "x" + utostr(NF);
380}
381
382static VectorTypeModifier getTupleVTM(unsigned NF) {
383 assert(2 <= NF && NF <= 8 && "2 <= NF <= 8");
384 return static_cast<VectorTypeModifier>(
385 static_cast<uint8_t>(VectorTypeModifier::Tuple2) + (NF - 2));
386}
387
388void RVVType::applyBasicType() {
389 switch (BT) {
390 case BasicType::Int8:
391 ElementBitwidth = 8;
393 break;
394 case BasicType::Int16:
395 ElementBitwidth = 16;
397 break;
398 case BasicType::Int32:
399 ElementBitwidth = 32;
401 break;
402 case BasicType::Int64:
403 ElementBitwidth = 64;
405 break;
407 ElementBitwidth = 16;
408 ScalarType = ScalarTypeKind::Float;
409 break;
411 ElementBitwidth = 32;
412 ScalarType = ScalarTypeKind::Float;
413 break;
415 ElementBitwidth = 64;
416 ScalarType = ScalarTypeKind::Float;
417 break;
419 ElementBitwidth = 16;
420 ScalarType = ScalarTypeKind::BFloat;
421 break;
423 ElementBitwidth = 8;
424 ScalarType = ScalarTypeKind::FloatE4M3;
425 break;
427 ElementBitwidth = 8;
428 ScalarType = ScalarTypeKind::FloatE5M2;
429 break;
430 default:
431 llvm_unreachable("Unhandled type code!");
432 }
433 assert(ElementBitwidth != 0 && "Bad element bitwidth!");
434}
435
436std::optional<PrototypeDescriptor>
438 llvm::StringRef PrototypeDescriptorStr) {
442
443 if (PrototypeDescriptorStr.empty())
444 return PD;
445
446 // Handle base type modifier
447 auto PType = PrototypeDescriptorStr.back();
448 switch (PType) {
449 case 'e':
451 break;
452 case 'v':
454 break;
455 case 'w':
458 break;
459 case 'd':
462 break;
463 case 'q':
466 break;
467 case 'o':
470 break;
471 case 'm':
474 break;
475 case '0':
477 break;
478 case 'z':
480 break;
481 case 't':
483 break;
484 case 'u':
486 break;
487 case 'l':
489 break;
490 case 'f':
492 break;
493 default:
494 llvm_unreachable("Illegal primitive type transformers!");
495 }
496 PD.PT = PT;
497 PrototypeDescriptorStr = PrototypeDescriptorStr.drop_back();
498
499 // Compute the vector type transformers, it can only appear one time.
500 if (PrototypeDescriptorStr.starts_with("(")) {
502 "VectorTypeModifier should only have one modifier");
503 size_t Idx = PrototypeDescriptorStr.find(')');
504 assert(Idx != StringRef::npos);
505 StringRef ComplexType = PrototypeDescriptorStr.slice(1, Idx);
506 PrototypeDescriptorStr = PrototypeDescriptorStr.drop_front(Idx + 1);
507 assert(!PrototypeDescriptorStr.contains('(') &&
508 "Only allow one vector type modifier");
509
510 auto ComplexTT = ComplexType.split(":");
511 if (ComplexTT.first == "Log2EEW") {
512 uint32_t Log2EEW;
513 if (ComplexTT.second.getAsInteger(10, Log2EEW)) {
514 llvm_unreachable("Invalid Log2EEW value!");
515 return std::nullopt;
516 }
517 switch (Log2EEW) {
518 case 3:
520 break;
521 case 4:
523 break;
524 case 5:
526 break;
527 case 6:
529 break;
530 default:
531 llvm_unreachable("Invalid Log2EEW value, should be [3-6]");
532 return std::nullopt;
533 }
534 } else if (ComplexTT.first == "FixedSEW") {
535 uint32_t NewSEW;
536 if (ComplexTT.second.getAsInteger(10, NewSEW)) {
537 llvm_unreachable("Invalid FixedSEW value!");
538 return std::nullopt;
539 }
540 switch (NewSEW) {
541 case 8:
543 break;
544 case 16:
546 break;
547 case 32:
549 break;
550 case 64:
552 break;
553 default:
554 llvm_unreachable("Invalid FixedSEW value, should be 8, 16, 32 or 64");
555 return std::nullopt;
556 }
557 } else if (ComplexTT.first == "LFixedLog2LMUL") {
558 int32_t Log2LMUL;
559 if (ComplexTT.second.getAsInteger(10, Log2LMUL)) {
560 llvm_unreachable("Invalid LFixedLog2LMUL value!");
561 return std::nullopt;
562 }
563 switch (Log2LMUL) {
564 case -3:
566 break;
567 case -2:
569 break;
570 case -1:
572 break;
573 case 0:
575 break;
576 case 1:
578 break;
579 case 2:
581 break;
582 case 3:
584 break;
585 default:
586 llvm_unreachable("Invalid LFixedLog2LMUL value, should be [-3, 3]");
587 return std::nullopt;
588 }
589 } else if (ComplexTT.first == "SFixedLog2LMUL") {
590 int32_t Log2LMUL;
591 if (ComplexTT.second.getAsInteger(10, Log2LMUL)) {
592 llvm_unreachable("Invalid SFixedLog2LMUL value!");
593 return std::nullopt;
594 }
595 switch (Log2LMUL) {
596 case -3:
598 break;
599 case -2:
601 break;
602 case -1:
604 break;
605 case 0:
607 break;
608 case 1:
610 break;
611 case 2:
613 break;
614 case 3:
616 break;
617 default:
618 llvm_unreachable("Invalid LFixedLog2LMUL value, should be [-3, 3]");
619 return std::nullopt;
620 }
621
622 } else if (ComplexTT.first == "SEFixedLog2LMUL") {
623 int32_t Log2LMUL;
624 if (ComplexTT.second.getAsInteger(10, Log2LMUL)) {
625 llvm_unreachable("Invalid SEFixedLog2LMUL value!");
626 return std::nullopt;
627 }
628 switch (Log2LMUL) {
629 case -3:
631 break;
632 case -2:
634 break;
635 case -1:
637 break;
638 case 0:
640 break;
641 case 1:
643 break;
644 case 2:
646 break;
647 case 3:
649 break;
650 default:
651 llvm_unreachable("Invalid LFixedLog2LMUL value, should be [-3, 3]");
652 return std::nullopt;
653 }
654 } else if (ComplexTT.first == "Tuple") {
655 unsigned NF = 0;
656 if (ComplexTT.second.getAsInteger(10, NF)) {
657 llvm_unreachable("Invalid NF value!");
658 return std::nullopt;
659 }
660 VTM = getTupleVTM(NF);
661 } else {
662 llvm_unreachable("Illegal complex type transformers!");
663 }
664 }
665 PD.VTM = VTM;
666
667 // Compute the remain type transformers
669 for (char I : PrototypeDescriptorStr) {
670 switch (I) {
671 case 'P':
673 llvm_unreachable("'P' transformer cannot be used after 'C'");
675 llvm_unreachable("'P' transformer cannot be used twice");
677 break;
678 case 'C':
680 break;
681 case 'K':
683 break;
684 case 'U':
686 break;
687 case 'I':
689 break;
690 case 'F':
692 break;
693 case 'Y':
695 break;
696 case 'S':
698 break;
699 case 'A':
701 break;
702 default:
703 llvm_unreachable("Illegal non-primitive type transformer!");
704 }
705 }
706 PD.TM = TM;
707
708 return PD;
709}
710
711void RVVType::applyModifier(const PrototypeDescriptor &Transformer) {
712 // Handle primitive type transformer
713 switch (static_cast<BaseTypeModifier>(Transformer.PT)) {
715 Scale = 0;
716 break;
718 Scale = LMUL.getScale(ElementBitwidth);
719 break;
721 ScalarType = ScalarTypeKind::Void;
722 break;
724 ScalarType = ScalarTypeKind::Size_t;
725 break;
727 ScalarType = ScalarTypeKind::Ptrdiff_t;
728 break;
730 ScalarType = ScalarTypeKind::UnsignedLong;
731 break;
733 ScalarType = ScalarTypeKind::SignedLong;
734 break;
736 ElementBitwidth = 32;
737 ScalarType = ScalarTypeKind::Float;
738 break;
740 ScalarType = ScalarTypeKind::Invalid;
741 return;
742 }
743
744 switch (static_cast<VectorTypeModifier>(Transformer.VTM)) {
746 ElementBitwidth *= 2;
747 LMUL.MulLog2LMUL(1);
748 Scale = LMUL.getScale(ElementBitwidth);
749 if (ScalarType == ScalarTypeKind::BFloat)
750 ScalarType = ScalarTypeKind::Float;
751 if (ScalarType == ScalarTypeKind::FloatE4M3 ||
752 ScalarType == ScalarTypeKind::FloatE5M2)
753 ScalarType = ScalarTypeKind::BFloat;
754 break;
756 LMUL.MulLog2LMUL(1);
757 Scale = LMUL.getScale(ElementBitwidth);
758 break;
760 ElementBitwidth *= 4;
761 LMUL.MulLog2LMUL(2);
762 Scale = LMUL.getScale(ElementBitwidth);
763 if (ScalarType == ScalarTypeKind::FloatE4M3 ||
764 ScalarType == ScalarTypeKind::FloatE5M2)
765 ScalarType = ScalarTypeKind::Float;
766 break;
768 ElementBitwidth *= 8;
769 LMUL.MulLog2LMUL(3);
770 Scale = LMUL.getScale(ElementBitwidth);
771 break;
773 ScalarType = ScalarTypeKind::Boolean;
774 Scale = LMUL.getScale(ElementBitwidth);
775 ElementBitwidth = 1;
776 break;
778 applyLog2EEW(3);
779 break;
781 applyLog2EEW(4);
782 break;
784 applyLog2EEW(5);
785 break;
787 applyLog2EEW(6);
788 break;
790 applyFixedSEW(8);
791 break;
793 applyFixedSEW(16);
794 break;
796 applyFixedSEW(32);
797 break;
799 applyFixedSEW(64);
800 break;
802 applyFixedLog2LMUL(-3, FixedLMULType::LargerThan);
803 break;
805 applyFixedLog2LMUL(-2, FixedLMULType::LargerThan);
806 break;
808 applyFixedLog2LMUL(-1, FixedLMULType::LargerThan);
809 break;
811 applyFixedLog2LMUL(0, FixedLMULType::LargerThan);
812 break;
814 applyFixedLog2LMUL(1, FixedLMULType::LargerThan);
815 break;
817 applyFixedLog2LMUL(2, FixedLMULType::LargerThan);
818 break;
820 applyFixedLog2LMUL(3, FixedLMULType::LargerThan);
821 break;
823 applyFixedLog2LMUL(-3, FixedLMULType::SmallerThan);
824 break;
826 applyFixedLog2LMUL(-2, FixedLMULType::SmallerThan);
827 break;
829 applyFixedLog2LMUL(-1, FixedLMULType::SmallerThan);
830 break;
832 applyFixedLog2LMUL(0, FixedLMULType::SmallerThan);
833 break;
835 applyFixedLog2LMUL(1, FixedLMULType::SmallerThan);
836 break;
838 applyFixedLog2LMUL(2, FixedLMULType::SmallerThan);
839 break;
841 applyFixedLog2LMUL(3, FixedLMULType::SmallerThan);
842 break;
844 applyFixedLog2LMUL(-3, FixedLMULType::SmallerOrEqual);
845 break;
847 applyFixedLog2LMUL(-2, FixedLMULType::SmallerOrEqual);
848 break;
850 applyFixedLog2LMUL(-1, FixedLMULType::SmallerOrEqual);
851 break;
853 applyFixedLog2LMUL(0, FixedLMULType::SmallerOrEqual);
854 break;
856 applyFixedLog2LMUL(1, FixedLMULType::SmallerOrEqual);
857 break;
859 applyFixedLog2LMUL(2, FixedLMULType::SmallerOrEqual);
860 break;
862 applyFixedLog2LMUL(3, FixedLMULType::SmallerOrEqual);
863 break;
871 IsTuple = true;
872 NF = 2 + static_cast<uint8_t>(Transformer.VTM) -
873 static_cast<uint8_t>(VectorTypeModifier::Tuple2);
874 break;
875 }
877 break;
878 }
879
880 // Early return if the current type modifier is already invalid.
881 if (ScalarType == Invalid)
882 return;
883
884 for (unsigned TypeModifierMaskShift = 0;
885 TypeModifierMaskShift <= static_cast<unsigned>(TypeModifier::MaxOffset);
886 ++TypeModifierMaskShift) {
887 unsigned TypeModifierMask = 1 << TypeModifierMaskShift;
888 if ((static_cast<unsigned>(Transformer.TM) & TypeModifierMask) !=
889 TypeModifierMask)
890 continue;
891 switch (static_cast<TypeModifier>(TypeModifierMask)) {
893 IsPointer = true;
894 break;
896 IsConstant = true;
897 break;
899 IsImmediate = true;
900 IsConstant = true;
901 break;
904 break;
907 break;
909 ScalarType = ScalarTypeKind::Float;
910 break;
912 ScalarType = ScalarTypeKind::BFloat;
913 break;
915 LMUL = LMULType(0);
916 // Update ElementBitwidth need to update Scale too.
917 Scale = LMUL.getScale(ElementBitwidth);
918 break;
920 if (ScalarType == ScalarTypeKind::FloatE4M3)
921 ScalarType = ScalarTypeKind::FloatE5M2;
922 else if (ScalarType == ScalarTypeKind::FloatE5M2)
923 ScalarType = ScalarTypeKind::FloatE4M3;
924 else
925 llvm_unreachable("AltFP8 modifier requires an OFP8 base type");
926 break;
927 default:
928 llvm_unreachable("Unknown type modifier mask!");
929 }
930 }
931}
932
933void RVVType::applyLog2EEW(unsigned Log2EEW) {
934 // update new elmul = (eew/sew) * lmul
935 LMUL.MulLog2LMUL(Log2EEW - Log2_32(ElementBitwidth));
936 // update new eew
937 ElementBitwidth = 1 << Log2EEW;
939 Scale = LMUL.getScale(ElementBitwidth);
940}
941
942void RVVType::applyFixedSEW(unsigned NewSEW) {
943 // Set invalid type if src and dst SEW are same.
944 if (ElementBitwidth == NewSEW) {
945 ScalarType = ScalarTypeKind::Invalid;
946 return;
947 }
948 // Update new SEW
949 ElementBitwidth = NewSEW;
950 Scale = LMUL.getScale(ElementBitwidth);
951}
952
953void RVVType::applyFixedLog2LMUL(int Log2LMUL, enum FixedLMULType Type) {
954 switch (Type) {
955 case FixedLMULType::LargerThan:
956 if (Log2LMUL <= LMUL.Log2LMUL) {
957 ScalarType = ScalarTypeKind::Invalid;
958 return;
959 }
960 break;
961 case FixedLMULType::SmallerThan:
962 if (Log2LMUL >= LMUL.Log2LMUL) {
963 ScalarType = ScalarTypeKind::Invalid;
964 return;
965 }
966 break;
967 case FixedLMULType::SmallerOrEqual:
968 if (Log2LMUL > LMUL.Log2LMUL) {
969 ScalarType = ScalarTypeKind::Invalid;
970 return;
971 }
972 break;
973 }
974
975 // Update new LMUL
976 LMUL = LMULType(Log2LMUL);
977 Scale = LMUL.getScale(ElementBitwidth);
978}
979
980std::optional<RVVTypes>
981RVVTypeCache::computeTypes(BasicType BT, int Log2LMUL, unsigned NF,
983 RVVTypes Types;
984 for (const PrototypeDescriptor &Proto : Prototype) {
985 auto T = computeType(BT, Log2LMUL, Proto);
986 if (!T)
987 return std::nullopt;
988 // Record legal type index
989 Types.push_back(*T);
990 }
991 return Types;
992}
993
994// Compute the hash value of RVVType, used for cache the result of computeType.
995static uint64_t computeRVVTypeHashValue(BasicType BT, int Log2LMUL,
996 PrototypeDescriptor Proto) {
997 // Layout of hash value:
998 // 0 8 24 32 40 48
999 // | Log2LMUL + 3 | BT | Proto.PT | Proto.TM | Proto.VTM |
1000 assert(Log2LMUL >= -3 && Log2LMUL <= 3);
1001 return (Log2LMUL + 3) | (static_cast<uint64_t>(BT) & 0xffff) << 8 |
1002 (static_cast<uint64_t>(Proto.PT) << 24) |
1003 (static_cast<uint64_t>(Proto.TM) << 32) |
1004 (static_cast<uint64_t>(Proto.VTM) << 40);
1005}
1006
1007std::optional<RVVTypePtr> RVVTypeCache::computeType(BasicType BT, int Log2LMUL,
1008 PrototypeDescriptor Proto) {
1009 uint64_t Idx = computeRVVTypeHashValue(BT, Log2LMUL, Proto);
1010 // Search first
1011 auto It = LegalTypes.find(Idx);
1012 if (It != LegalTypes.end())
1013 return &(It->second);
1014
1015 if (IllegalTypes.count(Idx))
1016 return std::nullopt;
1017
1018 // Compute type and record the result.
1019 RVVType T(BT, Log2LMUL, Proto);
1020 if (T.isValid()) {
1021 // Record legal type index and value.
1022 std::pair<std::unordered_map<uint64_t, RVVType>::iterator, bool>
1023 InsertResult = LegalTypes.insert({Idx, T});
1024 return &(InsertResult.first->second);
1025 }
1026 // Record illegal type index.
1027 IllegalTypes.insert(Idx);
1028 return std::nullopt;
1029}
1030
1031//===----------------------------------------------------------------------===//
1032// RVVIntrinsic implementation
1033//===----------------------------------------------------------------------===//
1035 StringRef NewName, StringRef Suffix, StringRef NewOverloadedName,
1036 StringRef OverloadedSuffix, StringRef IRName, bool IsMasked,
1037 bool HasMaskedOffOperand, bool HasVL, PolicyScheme Scheme,
1038 bool SupportOverloading, bool HasBuiltinAlias, StringRef ManualCodegen,
1039 const RVVTypes &OutInTypes, const std::vector<int64_t> &NewIntrinsicTypes,
1040 unsigned NF, bool HasSegInstSEW, Policy NewPolicyAttrs,
1041 bool HasFRMRoundModeOp, unsigned TWiden, bool AltFmt)
1042 : IRName(IRName), IsMasked(IsMasked),
1043 HasMaskedOffOperand(HasMaskedOffOperand), HasVL(HasVL), Scheme(Scheme),
1044 SupportOverloading(SupportOverloading), HasBuiltinAlias(HasBuiltinAlias),
1045 ManualCodegen(ManualCodegen.str()), NF(NF), HasSegInstSEW(HasSegInstSEW),
1046 PolicyAttrs(NewPolicyAttrs), TWiden(TWiden) {
1047
1048 // Init BuiltinName, Name and OverloadedName
1049 BuiltinName = NewName.str();
1050 Name = BuiltinName;
1051 if (NewOverloadedName.empty())
1052 OverloadedName = NewName.split("_").first.str();
1053 else
1054 OverloadedName = NewOverloadedName.str();
1055 if (!Suffix.empty())
1056 Name += "_" + Suffix.str();
1057 if (!OverloadedSuffix.empty())
1058 OverloadedName += "_" + OverloadedSuffix.str();
1059
1060 updateNamesAndPolicy(IsMasked, hasPolicy(), Name, BuiltinName, OverloadedName,
1061 PolicyAttrs, HasFRMRoundModeOp, AltFmt);
1062
1063 // Init OutputType and InputTypes
1064 OutputType = OutInTypes[0];
1065 InputTypes.assign(OutInTypes.begin() + 1, OutInTypes.end());
1066
1067 // IntrinsicTypes is unmasked TA version index. Need to update it
1068 // if there is merge operand (It is always in first operand).
1069 IntrinsicTypes = NewIntrinsicTypes;
1070 if ((IsMasked && hasMaskedOffOperand()) ||
1071 (!IsMasked && hasPassthruOperand())) {
1072 for (auto &I : IntrinsicTypes) {
1073 if (I >= 0)
1074 I += 1;
1075 }
1076 }
1077}
1078
1080 std::string S;
1081 S += OutputType->getBuiltinStr();
1082 for (const auto &T : InputTypes) {
1083 S += T->getBuiltinStr();
1084 }
1085 return S;
1086}
1087
1089 RVVTypeCache &TypeCache, BasicType Type, int Log2LMUL,
1090 llvm::ArrayRef<PrototypeDescriptor> PrototypeDescriptors) {
1091 SmallVector<std::string> SuffixStrs;
1092 for (auto PD : PrototypeDescriptors) {
1093 auto T = TypeCache.computeType(Type, Log2LMUL, PD);
1094 SuffixStrs.push_back((*T)->getShortStr());
1095 }
1096 return join(SuffixStrs, "_");
1097}
1098
1101 bool HasMaskedOffOperand, bool HasVL, unsigned NF,
1102 PolicyScheme DefaultScheme, Policy PolicyAttrs, bool IsTuple) {
1104 bool HasPassthruOp = DefaultScheme == PolicyScheme::HasPassthruOperand;
1105 if (IsMasked) {
1106 // If HasMaskedOffOperand, insert result type as first input operand if
1107 // need.
1108 if (HasMaskedOffOperand && !PolicyAttrs.isTAMAPolicy()) {
1109 if (NF == 1) {
1110 NewPrototype.insert(NewPrototype.begin() + 1, NewPrototype[0]);
1111 } else if (NF > 1) {
1112 if (IsTuple) {
1113 PrototypeDescriptor BasePtrOperand = Prototype[1];
1114 PrototypeDescriptor MaskoffType =
1116 BasePtrOperand.TM & ~TypeModifier::Pointer);
1117 NewPrototype.insert(NewPrototype.begin() + 1, MaskoffType);
1118 } else {
1119 // Convert
1120 // (void, op0 address, op1 address, ...)
1121 // to
1122 // (void, op0 address, op1 address, ..., maskedoff0, maskedoff1, ...)
1123 PrototypeDescriptor MaskoffType = NewPrototype[1];
1124 MaskoffType.TM &= ~TypeModifier::Pointer;
1125 NewPrototype.insert(NewPrototype.begin() + NF + 1, NF, MaskoffType);
1126 }
1127 }
1128 }
1129 if (HasMaskedOffOperand && NF > 1) {
1130 // Convert
1131 // (void, op0 address, op1 address, ..., maskedoff0, maskedoff1, ...)
1132 // to
1133 // (void, op0 address, op1 address, ..., mask, maskedoff0, maskedoff1,
1134 // ...)
1135 if (IsTuple)
1136 NewPrototype.insert(NewPrototype.begin() + 1,
1138 else
1139 NewPrototype.insert(NewPrototype.begin() + NF + 1,
1141 } else {
1142 // If IsMasked, insert PrototypeDescriptor:Mask as first input operand.
1143 NewPrototype.insert(NewPrototype.begin() + 1, PrototypeDescriptor::Mask);
1144 }
1145 } else {
1146 if (NF == 1) {
1147 if (PolicyAttrs.isTUPolicy() && HasPassthruOp)
1148 NewPrototype.insert(NewPrototype.begin(), NewPrototype[0]);
1149 } else if (PolicyAttrs.isTUPolicy() && HasPassthruOp) {
1150 if (IsTuple) {
1151 PrototypeDescriptor BasePtrOperand = Prototype[0];
1152 PrototypeDescriptor MaskoffType =
1154 BasePtrOperand.TM & ~TypeModifier::Pointer);
1155 NewPrototype.insert(NewPrototype.begin(), MaskoffType);
1156 } else {
1157 // NF > 1 cases for segment load operations.
1158 // Convert
1159 // (void, op0 address, op1 address, ...)
1160 // to
1161 // (void, op0 address, op1 address, maskedoff0, maskedoff1, ...)
1162 PrototypeDescriptor MaskoffType = Prototype[1];
1163 MaskoffType.TM &= ~TypeModifier::Pointer;
1164 NewPrototype.insert(NewPrototype.begin() + NF + 1, NF, MaskoffType);
1165 }
1166 }
1167 }
1168
1169 // If HasVL, append PrototypeDescriptor:VL to last operand
1170 if (HasVL)
1171 NewPrototype.push_back(PrototypeDescriptor::VL);
1172
1173 return NewPrototype;
1174}
1175
1179
1182 bool HasMaskPolicy) {
1183 if (HasTailPolicy && HasMaskPolicy)
1190 if (HasTailPolicy && !HasMaskPolicy)
1193 if (!HasTailPolicy && HasMaskPolicy)
1196 llvm_unreachable("An RVV instruction should not be without both tail policy "
1197 "and mask policy");
1198}
1199
1200void RVVIntrinsic::updateNamesAndPolicy(bool IsMasked, bool HasPolicy,
1201 std::string &Name,
1202 std::string &BuiltinName,
1203 std::string &OverloadedName,
1204 Policy &PolicyAttrs,
1205 bool HasFRMRoundModeOp, bool AltFmt) {
1206
1207 auto appendPolicySuffix = [&](const std::string &suffix) {
1208 Name += suffix;
1209 BuiltinName += suffix;
1210 OverloadedName += suffix;
1211 };
1212
1213 if (HasFRMRoundModeOp) {
1214 Name += "_rm";
1215 BuiltinName += "_rm";
1216 }
1217
1218 if (AltFmt)
1219 BuiltinName += "_alt";
1220
1221 if (IsMasked) {
1222 if (PolicyAttrs.isTUMUPolicy())
1223 appendPolicySuffix("_tumu");
1224 else if (PolicyAttrs.isTUMAPolicy())
1225 appendPolicySuffix("_tum");
1226 else if (PolicyAttrs.isTAMUPolicy())
1227 appendPolicySuffix("_mu");
1228 else if (PolicyAttrs.isTAMAPolicy()) {
1229 Name += "_m";
1230 BuiltinName += "_m";
1231 } else
1232 llvm_unreachable("Unhandled policy condition");
1233 } else {
1234 if (PolicyAttrs.isTUPolicy())
1235 appendPolicySuffix("_tu");
1236 else if (PolicyAttrs.isTAPolicy()) // no suffix needed
1237 return;
1238 else
1239 llvm_unreachable("Unhandled policy condition");
1240 }
1241}
1242
1244 SmallVector<PrototypeDescriptor> PrototypeDescriptors;
1245 const StringRef Primaries("evwdqom0ztulf");
1246 while (!Prototypes.empty()) {
1247 size_t Idx = 0;
1248 // Skip over complex prototype because it could contain primitive type
1249 // character.
1250 if (Prototypes[0] == '(')
1251 Idx = Prototypes.find_first_of(')');
1252 Idx = Prototypes.find_first_of(Primaries, Idx);
1253 assert(Idx != StringRef::npos);
1255 Prototypes.slice(0, Idx + 1));
1256 if (!PD)
1257 llvm_unreachable("Error during parsing prototype.");
1258 PrototypeDescriptors.push_back(*PD);
1259 Prototypes = Prototypes.drop_front(Idx + 1);
1260 }
1261 return PrototypeDescriptors;
1262}
1263
1264#define STRINGIFY(NAME) \
1265 case NAME: \
1266 OS << #NAME; \
1267 break;
1268
1269llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, enum PolicyScheme PS) {
1270 switch (PS) {
1274 }
1275 return OS;
1276}
1277
1278#undef STRINGIFY
1279
1280raw_ostream &operator<<(raw_ostream &OS, const RVVIntrinsicRecord &Record) {
1281 OS << "{";
1282 OS << "/*Name=*/\"" << Record.Name << "\", ";
1283 if (Record.OverloadedName == nullptr ||
1284 StringRef(Record.OverloadedName).empty())
1285 OS << "/*OverloadedName=*/nullptr, ";
1286 else
1287 OS << "/*OverloadedName=*/\"" << Record.OverloadedName << "\", ";
1288 OS << "/*RequiredExtensions=*/\"" << Record.RequiredExtensions << "\", ";
1289 OS << "/*PrototypeIndex=*/" << Record.PrototypeIndex << ", ";
1290 OS << "/*SuffixIndex=*/" << Record.SuffixIndex << ", ";
1291 OS << "/*OverloadedSuffixIndex=*/" << Record.OverloadedSuffixIndex << ", ";
1292 OS << "/*PrototypeLength=*/" << (int)Record.PrototypeLength << ", ";
1293 OS << "/*SuffixLength=*/" << (int)Record.SuffixLength << ", ";
1294 OS << "/*OverloadedSuffixSize=*/" << (int)Record.OverloadedSuffixSize << ", ";
1295 OS << "/*TypeRangeMask=*/" << (int)Record.TypeRangeMask << ", ";
1296 OS << "/*Log2LMULMask=*/" << (int)Record.Log2LMULMask << ", ";
1297 OS << "/*NF=*/" << (int)Record.NF << ", ";
1298 OS << "/*HasMasked=*/" << (int)Record.HasMasked << ", ";
1299 OS << "/*HasVL=*/" << (int)Record.HasVL << ", ";
1300 OS << "/*HasMaskedOffOperand=*/" << (int)Record.HasMaskedOffOperand << ", ";
1301 OS << "/*HasTailPolicy=*/" << (int)Record.HasTailPolicy << ", ";
1302 OS << "/*HasMaskPolicy=*/" << (int)Record.HasMaskPolicy << ", ";
1303 OS << "/*HasFRMRoundModeOp=*/" << (int)Record.HasFRMRoundModeOp << ", ";
1304 OS << "/*AltFmt=*/" << (int)Record.AltFmt << ",";
1305 OS << "/*IsTuple=*/" << (int)Record.IsTuple << ", ";
1306 OS << "/*UnMaskedPolicyScheme=*/" << (PolicyScheme)Record.UnMaskedPolicyScheme
1307 << ", ";
1308 OS << "/*MaskedPolicyScheme=*/" << (PolicyScheme)Record.MaskedPolicyScheme
1309 << ", ";
1310 OS << "},\n";
1311 return OS;
1312}
1313
1314} // end namespace RISCV
1315} // end namespace clang
#define V(N, I)
static bool getTypeString(SmallStringEnc &Enc, const Decl *D, const CodeGen::CodeGenModule &CGM, TypeStringCache &TSC)
The XCore ABI includes a type information section that communicates symbol type information to the li...
Definition XCore.cpp:630
llvm::MachO::Record Record
Definition MachO.h:31
#define STRINGIFY(NAME)
static bool isVector(QualType QT, QualType ElementType)
This helper function returns true if QT is a vector type that has element type ElementType.
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3339
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, bool HasSegInstSEW, Policy PolicyAttrs, bool HasFRMRoundModeOp, unsigned TWiden, bool AltFmt)
static llvm::SmallVector< Policy > getSupportedMaskedPolicies(bool HasTailPolicy, bool HasMaskPolicy)
static llvm::SmallVector< PrototypeDescriptor > computeBuiltinTypes(llvm::ArrayRef< PrototypeDescriptor > Prototype, bool IsMasked, bool HasMaskedOffOperand, bool HasVL, unsigned NF, PolicyScheme DefaultScheme, Policy PolicyAttrs, bool IsTuple)
static std::string getSuffixStr(RVVTypeCache &TypeCache, BasicType Type, int Log2LMUL, llvm::ArrayRef< PrototypeDescriptor > PrototypeDescriptors)
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()
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...
The base class of the type hierarchy.
Definition TypeBase.h:1875
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
static uint64_t computeRVVTypeHashValue(BasicType BT, int Log2LMUL, PrototypeDescriptor Proto)
static VectorTypeModifier getTupleVTM(unsigned NF)
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
@ Type
The name was classified as a type.
Definition Sema.h:564
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
std::optional< unsigned > getScale(unsigned ElementBitwidth) const
static std::optional< PrototypeDescriptor > parsePrototypeDescriptor(llvm::StringRef PrototypeStr)
static const PrototypeDescriptor VL
constexpr PrototypeDescriptor()=default
static const PrototypeDescriptor Mask
static const PrototypeDescriptor Vector