[llvm] f120b0d - [MC] Remove MCSymbolRefExpr::VK_Invalid in favor of getVaraintKindForName returning std::optional

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 11 00:21:37 PDT 2025


Author: Fangrui Song
Date: 2025-03-11T00:21:31-07:00
New Revision: f120b0d6d2629e226e6fa75974fbd17f46206bca

URL: https://github.com/llvm/llvm-project/commit/f120b0d6d2629e226e6fa75974fbd17f46206bca
DIFF: https://github.com/llvm/llvm-project/commit/f120b0d6d2629e226e6fa75974fbd17f46206bca.diff

LOG: [MC] Remove MCSymbolRefExpr::VK_Invalid in favor of getVaraintKindForName returning std::optional

so that when the enum members are moved to XXXTargetExpr::VariantKind,,
they do not need to implement an invalid value.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAsmInfo.h
    llvm/include/llvm/MC/MCExpr.h
    llvm/lib/MC/MCAsmInfo.cpp
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index aba8eaed82d1a..7f03097eba060 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -709,7 +709,7 @@ class MCAsmInfo {
   bool shouldUseMotorolaIntegers() const { return UseMotorolaIntegers; }
 
   StringRef getVariantKindName(uint32_t Kind) const;
-  uint32_t getVariantKindForName(StringRef Name) const;
+  std::optional<uint32_t> getVariantKindForName(StringRef Name) const;
 };
 
 } // end namespace llvm

diff  --git a/llvm/include/llvm/MC/MCExpr.h b/llvm/include/llvm/MC/MCExpr.h
index ab96b39d9f296..a302a81ac468a 100644
--- a/llvm/include/llvm/MC/MCExpr.h
+++ b/llvm/include/llvm/MC/MCExpr.h
@@ -198,7 +198,6 @@ class MCSymbolRefExpr : public MCExpr {
   // cleaner approach.
   enum VariantKind : uint16_t {
     VK_None,
-    VK_Invalid,
 
     VK_GOT,
     VK_GOTENT,

diff  --git a/llvm/lib/MC/MCAsmInfo.cpp b/llvm/lib/MC/MCAsmInfo.cpp
index 9821bb298d7d4..69374f69bc8e5 100644
--- a/llvm/lib/MC/MCAsmInfo.cpp
+++ b/llvm/lib/MC/MCAsmInfo.cpp
@@ -145,9 +145,9 @@ StringRef MCAsmInfo::getVariantKindName(uint32_t Kind) const {
   return It->second;
 }
 
-uint32_t MCAsmInfo::getVariantKindForName(StringRef Name) const {
+std::optional<uint32_t> MCAsmInfo::getVariantKindForName(StringRef Name) const {
   auto It = NameToVariantKind.find(Name.lower());
   if (It != NameToVariantKind.end())
     return It->second;
-  return MCSymbolRefExpr::VK_Invalid;
+  return {};
 }

diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 1e1b63717ded5..0cd691e9298ed 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1228,10 +1228,10 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
 
     // Lookup the symbol variant if used.
     if (!Split.second.empty()) {
-      Variant =
-          MCSymbolRefExpr::VariantKind(MAI.getVariantKindForName(Split.second));
-      if (Variant != MCSymbolRefExpr::VK_Invalid) {
+      auto MaybeVariant = MAI.getVariantKindForName(Split.second);
+      if (MaybeVariant) {
         SymbolName = Split.first;
+        Variant = MCSymbolRefExpr::VariantKind(*MaybeVariant);
       } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) {
         Variant = MCSymbolRefExpr::VK_None;
       } else {
@@ -1279,11 +1279,11 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
       std::pair<StringRef, StringRef> Split = IDVal.split('@');
       MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
       if (Split.first.size() != IDVal.size()) {
-        Variant = MCSymbolRefExpr::VariantKind(
-            MAI.getVariantKindForName(Split.second));
-        if (Variant == MCSymbolRefExpr::VK_Invalid)
+        auto MaybeVariant = MAI.getVariantKindForName(Split.second);
+        if (!MaybeVariant)
           return TokError("invalid variant '" + Split.second + "'");
         IDVal = Split.first;
+        Variant = MCSymbolRefExpr::VariantKind(*MaybeVariant);
       }
       if (IDVal == "f" || IDVal == "b") {
         MCSymbol *Sym =
@@ -1470,12 +1470,12 @@ bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
     if (Lexer.isNot(AsmToken::Identifier))
       return TokError("unexpected symbol modifier following '@'");
 
-    auto Variant = MCSymbolRefExpr::VariantKind(
-        MAI.getVariantKindForName(getTok().getIdentifier()));
-    if (Variant == MCSymbolRefExpr::VK_Invalid)
+    auto Variant = MAI.getVariantKindForName(getTok().getIdentifier());
+    if (!Variant)
       return TokError("invalid variant '" + getTok().getIdentifier() + "'");
 
-    const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant);
+    const MCExpr *ModifiedRes =
+        applyModifierToExpr(Res, MCSymbolRefExpr::VariantKind(*Variant));
     if (!ModifiedRes) {
       return TokError("invalid modifier '" + getTok().getIdentifier() +
                       "' (no symbols present)");

diff  --git a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
index cd0704cad2462..be0df763029a5 100644
--- a/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp
@@ -109,10 +109,10 @@ struct DenseMapInfo<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>> {
   using TOCKey = std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>;
 
   static inline TOCKey getEmptyKey() {
-    return {nullptr, MCSymbolRefExpr::VariantKind::VK_None};
+    return {nullptr, MCSymbolRefExpr::VK_None};
   }
   static inline TOCKey getTombstoneKey() {
-    return {nullptr, MCSymbolRefExpr::VariantKind::VK_Invalid};
+    return {(const MCSymbol *)1, MCSymbolRefExpr::VK_None};
   }
   static unsigned getHashValue(const TOCKey &PairVal) {
     return detail::combineHashValue(


        


More information about the llvm-commits mailing list