[llvm] 04b49b1 - [MCExpr] Remove generic getVariantKindName and getVariantKindForName

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 2 22:05:52 PST 2025


Author: Fangrui Song
Date: 2025-03-02T22:05:47-08:00
New Revision: 04b49b11a8f70424263a3fc1f9c5bc69a9f46844

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

LOG: [MCExpr] Remove generic getVariantKindName and getVariantKindForName

They are error-prone as MCParser may parse a variant kind,
which cannot be handled by the target.

The replacement in MCAsmInfo should be used instead.

Follow-up to f244b8eed37a12539fb11b76e19ec7a7eb41dccc

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCExpr.h
    llvm/lib/MC/MCAsmInfo.cpp
    llvm/lib/MC/MCExpr.cpp
    llvm/lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
    llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCExpr.h b/llvm/include/llvm/MC/MCExpr.h
index ece51ebecdd9c..b1105a253c276 100644
--- a/llvm/include/llvm/MC/MCExpr.h
+++ b/llvm/include/llvm/MC/MCExpr.h
@@ -418,14 +418,6 @@ class MCSymbolRefExpr : public MCExpr {
     return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
   }
 
-  /// @}
-  /// \name Static Utility Functions
-  /// @{
-
-  static StringRef getVariantKindName(VariantKind Kind);
-
-  static VariantKind getVariantKindForName(StringRef Name);
-
   /// @}
 
   static bool classof(const MCExpr *E) {

diff  --git a/llvm/lib/MC/MCAsmInfo.cpp b/llvm/lib/MC/MCAsmInfo.cpp
index 2ad411d200a17..f7270e87a6d07 100644
--- a/llvm/lib/MC/MCAsmInfo.cpp
+++ b/llvm/lib/MC/MCAsmInfo.cpp
@@ -140,15 +140,13 @@ void MCAsmInfo::initializeVariantKinds(ArrayRef<VariantKindDesc> Descs) {
 }
 
 StringRef MCAsmInfo::getVariantKindName(uint32_t Kind) const {
-  if (!VariantKindToName.empty())
-    return VariantKindToName.find(Kind)->second;
-  return MCSymbolRefExpr::getVariantKindName(
-      MCSymbolRefExpr::VariantKind(Kind));
+  auto It = VariantKindToName.find(Kind);
+  assert(It != VariantKindToName.end() &&
+         "ensure the VariantKind is set in initializeVariantKinds");
+  return It->second;
 }
 
 uint32_t MCAsmInfo::getVariantKindForName(StringRef Name) const {
-  if (NameToVariantKind.empty())
-    return MCSymbolRefExpr::getVariantKindForName(Name);
   auto It = NameToVariantKind.find(Name.lower());
   if (It != NameToVariantKind.end())
     return It->second;

diff  --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index 2cbb26cb1caf5..90536dce9aa6b 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -86,12 +86,10 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
 
     const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
     if (Kind != MCSymbolRefExpr::VK_None) {
-      if (MAI && MAI->useParensForSymbolVariant()) // ARM
+      if (MAI->useParensForSymbolVariant()) // ARM
         OS << '(' << MAI->getVariantKindName(Kind) << ')';
-      else if (MAI)
-        OS << '@' << MAI->getVariantKindName(Kind);
       else
-        OS << '@' << MCSymbolRefExpr::getVariantKindName(Kind);
+        OS << '@' << MAI->getVariantKindName(Kind);
     }
 
     return;
@@ -248,84 +246,6 @@ const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
   return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
 }
 
-// TODO: Move target-specific Kinds to lib/Target/*/MCTargetDesc/*AsmInfo.cpp.
-StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
-  switch (Kind) {
-    // clang-format off
-  case VK_Invalid: default: return "<<invalid>>";
-  case VK_None: return "<<none>>";
-
-  case VK_DTPOFF: return "DTPOFF";
-  case VK_DTPREL: return "DTPREL";
-  case VK_GOT: return "GOT";
-  case VK_GOTENT: return "GOTENT";
-  case VK_GOTOFF: return "GOTOFF";
-  case VK_GOTREL: return "GOTREL";
-  case VK_PCREL: return "PCREL";
-  case VK_GOTPCREL: return "GOTPCREL";
-  case VK_GOTTPOFF: return "GOTTPOFF";
-  case VK_INDNTPOFF: return "INDNTPOFF";
-  case VK_NTPOFF: return "NTPOFF";
-  case VK_GOTNTPOFF: return "GOTNTPOFF";
-  case VK_PLT: return "PLT";
-  case VK_TLSGD: return "TLSGD";
-  case VK_TLSLD: return "TLSLD";
-  case VK_TLSLDM: return "TLSLDM";
-  case VK_TPOFF: return "TPOFF";
-  case VK_TPREL: return "TPREL";
-  case VK_TLSCALL: return "tlscall";
-  case VK_TLSDESC: return "tlsdesc";
-  case VK_TLVP: return "TLVP";
-  case VK_TLVPPAGE: return "TLVPPAGE";
-  case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
-  case VK_PAGE: return "PAGE";
-  case VK_PAGEOFF: return "PAGEOFF";
-  case VK_GOTPAGE: return "GOTPAGE";
-  case VK_GOTPAGEOFF: return "GOTPAGEOFF";
-  case VK_SECREL: return "SECREL32";
-  case VK_WEAKREF: return "WEAKREF";
-  case VK_COFF_IMGREL32: return "IMGREL";
-    // clang-format on
-  }
-  llvm_unreachable("Invalid variant kind");
-}
-
-// FIXME: All variant kinds are target-specific. Move them to
-// *AsmParser::getVariantKindForName and remove this function.
-MCSymbolRefExpr::VariantKind
-MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
-  return StringSwitch<VariantKind>(Name.lower())
-      .Case("dtprel", VK_DTPREL)
-      .Case("dtpoff", VK_DTPOFF)
-      .Case("got", VK_GOT)
-      .Case("gotent", VK_GOTENT)
-      .Case("gotoff", VK_GOTOFF)
-      .Case("gotrel", VK_GOTREL)
-      .Case("pcrel", VK_PCREL)
-      .Case("gotpcrel", VK_GOTPCREL)
-      .Case("gottpoff", VK_GOTTPOFF)
-      .Case("indntpoff", VK_INDNTPOFF)
-      .Case("ntpoff", VK_NTPOFF)
-      .Case("plt", VK_PLT)
-      .Case("tlscall", VK_TLSCALL)
-      .Case("tlsdesc", VK_TLSDESC)
-      .Case("tlsgd", VK_TLSGD)
-      .Case("tlsld", VK_TLSLD)
-      .Case("tlsldm", VK_TLSLDM)
-      .Case("tpoff", VK_TPOFF)
-      .Case("tprel", VK_TPREL)
-      .Case("tlvp", VK_TLVP)
-      .Case("tlvppage", VK_TLVPPAGE)
-      .Case("tlvppageoff", VK_TLVPPAGEOFF)
-      .Case("page", VK_PAGE)
-      .Case("pageoff", VK_PAGEOFF)
-      .Case("gotpage", VK_GOTPAGE)
-      .Case("gotpageoff", VK_GOTPAGEOFF)
-      .Case("imgrel", VK_COFF_IMGREL32)
-      .Case("secrel32", VK_SECREL)
-      .Default(VK_Invalid);
-}
-
 /* *** */
 
 void MCTargetExpr::anchor() {}

diff  --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
index b66fde007accc..90fa70c5e7e4d 100644
--- a/llvm/lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
+++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMMCAsmInfo.cpp
@@ -129,6 +129,8 @@ ARMCOFFMCAsmInfoMicrosoft::ARMCOFFMCAsmInfoMicrosoft() {
 
   // Conditional Thumb 4-byte instructions can have an implicit IT.
   MaxInstLength = 6;
+
+  initializeVariantKinds(variantKindDescs);
 }
 
 void ARMCOFFMCAsmInfoGNU::anchor() { }

diff  --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
index ef2d043519a4d..be909bc419c36 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
@@ -53,6 +53,8 @@ SystemZMCAsmInfoGOFF::SystemZMCAsmInfoGOFF(const Triple &TT) {
   IsLittleEndian = false;
   MaxInstLength = 6;
   SupportsDebugInformation = true;
+
+  initializeVariantKinds(variantKindDescs);
 }
 
 bool SystemZMCAsmInfoGOFF::isAcceptableChar(char C) const {


        


More information about the llvm-commits mailing list