[llvm] ccc18ca - [RISCV] Move VK_GOTPCREL and VK_PLT to RISCVMCExpr::Specifier

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 09:24:16 PDT 2025


Author: Fangrui Song
Date: 2025-03-22T09:24:11-07:00
New Revision: ccc18ca1479dee73b0fdafa75d9403c50c17ee28

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

LOG: [RISCV] Move VK_GOTPCREL and VK_PLT to RISCVMCExpr::Specifier

to migrate away from the deprecated MCSymbolRefExpr::VariantKind. In the
future, @GOTPCREL and @PLT in data directives should be encoded as part
of RISCVMCExpr instead of MCSymbolRefExpr.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
    llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
index ccaa8ac1c7dbb..6db857c7906bb 100644
--- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
+++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
@@ -2343,7 +2343,7 @@ ParseStatus RISCVAsmParser::parsePseudoJumpSymbol(OperandVector &Operands) {
     return ParseStatus::Failure;
 
   if (Res->getKind() != MCExpr::ExprKind::SymbolRef ||
-      cast<MCSymbolRefExpr>(Res)->getKind() == MCSymbolRefExpr::VK_PLT)
+      getSpecifier(cast<MCSymbolRefExpr>(Res)) == RISCVMCExpr::VK_PLT)
     return Error(S, "operand must be a valid jump target");
 
   Res = RISCVMCExpr::create(Res, RISCVMCExpr::VK_CALL, getContext());

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index e752b0ec5f58c..57bac87ae30da 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -556,7 +556,7 @@ bool RISCVAsmBackend::evaluateTargetFixup(const MCAssembler &Asm,
 
   const MCSymbolRefExpr *A = AUIPCTarget.getSymA();
   const MCSymbolELF &SA = cast<MCSymbolELF>(A->getSymbol());
-  if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined())
+  if (getSpecifier(A) != RISCVMCExpr::VK_None || SA.isUndefined())
     return false;
 
   bool IsResolved = &SA.getSection() == AUIPCDF->getParent() &&

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
index acf9c156ef917..f8841dd657568 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp
@@ -73,7 +73,7 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
       return ELF::R_RISCV_NONE;
     case FK_Data_4:
     case FK_PCRel_4:
-      return Target.getAccessVariant() == MCSymbolRefExpr::VK_PLT
+      return uint8_t(Target.getAccessVariant()) == RISCVMCExpr::VK_PLT
                  ? ELF::R_RISCV_PLT32
                  : ELF::R_RISCV_32_PCREL;
     case RISCV::fixup_riscv_pcrel_hi20:
@@ -132,7 +132,7 @@ unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
     if (Expr->getKind() == MCExpr::Target &&
         cast<RISCVMCExpr>(Expr)->getSpecifier() == RISCVMCExpr::VK_32_PCREL)
       return ELF::R_RISCV_32_PCREL;
-    if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_GOTPCREL)
+    if (getSpecifier(Target.getSymA()) == RISCVMCExpr::VK_GOTPCREL)
       return ELF::R_RISCV_GOT32_PCREL;
     return ELF::R_RISCV_32;
   case FK_Data_8:

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp
index 280612b4fe95b..c327f9d5f0f1e 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCAsmInfo.cpp
@@ -19,8 +19,8 @@
 using namespace llvm;
 
 const MCAsmInfo::VariantKindDesc variantKindDescs[] = {
-    {MCSymbolRefExpr::VK_GOTPCREL, "GOTPCREL"},
-    {MCSymbolRefExpr::VK_PLT, "PLT"},
+    {RISCVMCExpr::VK_GOTPCREL, "GOTPCREL"},
+    {RISCVMCExpr::VK_PLT, "PLT"},
 };
 
 void RISCVMCAsmInfo::anchor() {}

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
index 3ff3368f02461..e589e6171d010 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
@@ -478,7 +478,9 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
     switch (RVExpr->getSpecifier()) {
     case RISCVMCExpr::VK_None:
     case RISCVMCExpr::VK_32_PCREL:
-      llvm_unreachable("Unhandled fixup kind!");
+    case RISCVMCExpr::VK_GOTPCREL:
+    case RISCVMCExpr::VK_PLT:
+      llvm_unreachable("unhandled specifier");
     case RISCVMCExpr::VK_TPREL_ADD:
       // tprel_add is only used to indicate that a relocation should be emitted
       // for an add instruction used in TP-relative addressing. It should not be
@@ -556,8 +558,8 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
       break;
     }
   } else if ((Kind == MCExpr::SymbolRef &&
-                 cast<MCSymbolRefExpr>(Expr)->getKind() ==
-                     MCSymbolRefExpr::VK_None) ||
+              getSpecifier(cast<MCSymbolRefExpr>(Expr)) ==
+                  RISCVMCExpr::VK_None) ||
              Kind == MCExpr::Binary) {
     // FIXME: Sub kind binary exprs have chance of underflow.
     if (MIFrm == RISCVII::InstFormatJ) {

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
index ff38769177340..7de53e6d3f479 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.cpp
@@ -125,7 +125,9 @@ RISCVMCExpr::getSpecifierForName(StringRef name) {
 StringRef RISCVMCExpr::getSpecifierName(Specifier S) {
   switch (S) {
   case VK_None:
-    llvm_unreachable("Invalid ELF symbol kind");
+  case VK_PLT:
+  case VK_GOTPCREL:
+    llvm_unreachable("not used as %specifier()");
   case VK_LO:
     return "lo";
   case VK_HI:

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
index be2cfeb78163b..af178ffabce13 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCExpr.h
@@ -37,6 +37,8 @@ class RISCVMCExpr : public MCTargetExpr {
     VK_CALL,
     VK_CALL_PLT,
     VK_32_PCREL,
+    VK_GOTPCREL,
+    VK_PLT,
     VK_TLSDESC_HI,
     VK_TLSDESC_LOAD_LO,
     VK_TLSDESC_ADD_LO,
@@ -85,6 +87,9 @@ class RISCVMCExpr : public MCTargetExpr {
   static StringRef getSpecifierName(Specifier Kind);
 };
 
+static inline RISCVMCExpr::Specifier getSpecifier(const MCSymbolRefExpr *SRE) {
+  return RISCVMCExpr::Specifier(SRE->getKind());
+}
 } // end namespace llvm.
 
 #endif

diff  --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index fc9160768a7b0..e546815b70935 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "RISCVTargetObjectFile.h"
+#include "MCTargetDesc/RISCVMCExpr.h"
 #include "MCTargetDesc/RISCVMCObjectFileInfo.h"
 #include "RISCVTargetMachine.h"
 #include "llvm/BinaryFormat/ELF.h"
@@ -26,7 +27,7 @@ void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
                                           const TargetMachine &TM) {
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
 
-  PLTRelativeSpecifier = MCSymbolRefExpr::VK_PLT;
+  PLTRelativeSpecifier = RISCVMCExpr::VK_PLT;
   SupportIndirectSymViaGOTPCRel = true;
 
   SmallDataSection = getContext().getELFSection(
@@ -50,7 +51,7 @@ const MCExpr *RISCVELFTargetObjectFile::getIndirectSymViaGOTPCRel(
     int64_t Offset, MachineModuleInfo *MMI, MCStreamer &Streamer) const {
   int64_t FinalOffset = Offset + MV.getConstant();
   const MCExpr *Res =
-      MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
+      MCSymbolRefExpr::create(Sym, RISCVMCExpr::VK_GOTPCREL, getContext());
   const MCExpr *Off = MCConstantExpr::create(FinalOffset, getContext());
   return MCBinaryExpr::createAdd(Res, Off, getContext());
 }


        


More information about the llvm-commits mailing list