Skip to content

Commit 3146eaa

Browse files
committed
Refactor builtin_str()
1 parent ad46796 commit 3146eaa

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

clang/utils/TableGen/SveEmitter.cpp

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class SVEType {
136136

137137
/// Applies a prototype modifier to the type.
138138
void applyModifier(char Mod);
139+
140+
/// Get the builtin base for this SVEType, e.g, 'Wi' for svint64_t.
141+
std::string builtinBaseType() const;
139142
};
140143

141144
class SVEEmitter;
@@ -433,88 +436,82 @@ const std::array<SVEEmitter::ReinterpretTypeInfo, 12> SVEEmitter::Reinterprets =
433436
// Type implementation
434437
//===----------------------------------------------------------------------===//
435438

436-
std::string SVEType::builtin_str() const {
437-
std::string OutStr;
438-
439-
if (isScalarPredicate())
440-
return "b";
441-
442-
if (isSvcount())
439+
std::string SVEType::builtinBaseType() const {
440+
switch (Kind) {
441+
case TypeKind::Void:
442+
return "v";
443+
case TypeKind::Svcount:
443444
return "Qa";
444-
445-
if (isVoid()) {
446-
OutStr += "v";
447-
if (!isPointer())
448-
return OutStr;
449-
} else if (isFloat()) {
445+
case TypeKind::BFloat16:
446+
assert(ElementBitwidth == 16 && "Invalid BFloat16!");
447+
return "y";
448+
case TypeKind::MFloat8:
449+
assert(ElementBitwidth == 8 && "Invalid MFloat8!");
450+
return "c";
451+
case TypeKind::Float:
450452
switch (ElementBitwidth) {
451453
case 16:
452-
OutStr += "h";
453-
break;
454+
return "h";
454455
case 32:
455-
OutStr += "f";
456-
break;
456+
return "f";
457457
case 64:
458-
OutStr += "d";
459-
break;
458+
return "d";
460459
default:
461-
llvm_unreachable("Unhandled float type!");
460+
llvm_unreachable("Unhandled float width!");
462461
}
463-
} else if (isBFloat()) {
464-
assert(ElementBitwidth == 16 && "Not a valid BFloat.");
465-
OutStr += "y";
466-
} else if (isMFloat()) {
467-
assert(ElementBitwidth == 8 && "Not a valid MFloat.");
468-
OutStr += "m";
469-
} else {
462+
case TypeKind::Predicate:
463+
if (isScalar())
464+
return "b";
465+
[[fallthrough]];
466+
// SInt/UInt, PredicatePattern, PrefetchOp.
467+
default:
470468
switch (ElementBitwidth) {
471469
case 1:
472-
OutStr += "b";
473-
break;
470+
return "b";
474471
case 8:
475-
OutStr += "c";
476-
break;
472+
return "c";
477473
case 16:
478-
OutStr += "s";
479-
break;
474+
return "s";
480475
case 32:
481-
OutStr += "i";
482-
break;
476+
return "i";
483477
case 64:
484-
OutStr += "Wi";
485-
break;
478+
return "Wi";
486479
case 128:
487-
OutStr += "LLLi";
488-
break;
480+
return "LLLi";
489481
default:
490482
llvm_unreachable("Unhandled bitwidth!");
491483
}
492484
}
485+
}
493486

494-
// Make chars and integer pointers explicitly signed.
495-
if ((ElementBitwidth == 8 || isPointer()) && isSignedInteger())
496-
OutStr = "S" + OutStr;
497-
else if (isUnsignedInteger())
498-
OutStr = "U" + OutStr;
487+
std::string SVEType::builtin_str() const {
488+
489+
std::string Prefix;
499490

500-
// Constant indices are "int", but have the "constant expression" modifier.
501-
if (isImmediate()) {
491+
if (isScalableVector())
492+
Prefix = "q" + llvm::utostr(getNumElements() * NumVectors);
493+
else if (isFixedLengthVector())
494+
Prefix = "V" + llvm::utostr(getNumElements() * NumVectors);
495+
else if (isImmediate()) {
502496
assert(!isFloatingPoint() && "fp immediates are not supported");
503-
OutStr = "I" + OutStr;
497+
Prefix = "I";
504498
}
505499

506-
if (isScalar()) {
507-
if (Constant)
508-
OutStr += "C";
509-
if (Pointer)
510-
OutStr += "*";
511-
return OutStr;
512-
}
500+
// Make chars and integer pointers explicitly signed.
501+
if ((ElementBitwidth == 8 || isPointer()) && isSignedInteger())
502+
Prefix += "S";
503+
else if (isUnsignedInteger())
504+
Prefix += "U";
513505

514-
if (isFixedLengthVector())
515-
return "V" + utostr(getNumElements() * NumVectors) + OutStr;
516-
return "q" + utostr(getNumElements() * NumVectors) + OutStr;
506+
std::string BuiltinStr = Prefix + builtinBaseType();
507+
if (isConstant())
508+
BuiltinStr += "C";
509+
if (isPointer())
510+
BuiltinStr += "*";
511+
512+
return BuiltinStr;
517513
}
514+
518515
std::string SVEType::str() const {
519516
if (isPredicatePattern())
520517
return "enum svpattern";
@@ -623,6 +620,7 @@ void SVEType::applyModifier(char Mod) {
623620
switch (Mod) {
624621
case 'v':
625622
Kind = Void;
623+
NumVectors = 0;
626624
break;
627625
case 'd':
628626
DefaultType = true;

0 commit comments

Comments
 (0)