[llvm] [RISCV][NFC] Refactor Vendor Reloc Declarations (PR #138226)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Fri May 2 15:55:00 PDT 2025


https://github.com/lenary updated https://github.com/llvm/llvm-project/pull/138226

>From 9e4923cb8d161868b3c19c28ac2ff2a57709bf9d Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Thu, 1 May 2025 19:06:34 -0700
Subject: [PATCH 1/2] [RISCV][NFC] Refactor Vendor Relocs

This change makes it much easier for external projects to opt-in to
supporting relocations from specific vendors (or with specific tags),
rather than having to support all of them at once.
---
 llvm/include/llvm/BinaryFormat/ELF.h          |  4 +-
 .../ELFRelocs/RISCV_nonstandard.def           | 45 ++++++++++++-------
 .../RISCV/MCTargetDesc/RISCVAsmBackend.cpp    |  4 +-
 3 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 1f3cea4bd1ae6..77077dad01f60 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -700,9 +700,9 @@ enum : unsigned {
 // ELF Relocation types for RISC-V
 enum {
 #include "ELFRelocs/RISCV.def"
-#define ELF_RISCV_NONSTANDARD_RELOC(_vendor, name, value) name = value,
+#define ELF_RISCV_NONSTANDARD_RELOC_ALL(name, value) name = value,
 #include "ELFRelocs/RISCV_nonstandard.def"
-#undef ELF_RISCV_NONSTANDARD_RELOC
+#undef ELF_RISCV_NONSTANDARD_RELOC_ALL
 };
 
 enum {
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
index 7ae3d3f205772..a9c635ae13592 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
@@ -6,23 +6,34 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef ELF_RISCV_NONSTANDARD_RELOC
-#error "ELF_RISCV_NONSTANDARD_RELOC must be defined"
+// This file defines information about RISC-V's nonstandard relocation codes.
+// This can be used when parsing relocations, or when printing them, to provide
+// better information.
+//
+// Unlike the mappings provided in RISCV.def via `ELF_RELOC`, these are not
+// expected to be 1:1 mappings - multiple vendors may reuse relocation IDs.
+
+// For ease of use, `ELF_RISCV_NONSTANDARD_RELOC_ALL` invokes the macros for
+// each vendor.
+
+#ifdef ELF_RISCV_NONSTANDARD_RELOC_ALL
+#define ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(NAME, ID) ELF_RISCV_NONSTANDARD_RELOC_ALL(NAME, ID)
 #endif
 
-// ELF_RISCV_NONSTANDARD_RELOC(VENDOR, NAME, ID) defines information about
-// nonstandard relocation codes. This can be used when parsing relocations, or
-// when printing them, to provide better information.
-//
-// VENDOR should be the symbol name expected in the associated `R_RISCV_VENDOR`
-// relocation. NAME and ID work like `ELF_RELOC` but the mapping is not expected
-// to be 1:1.
-//
-// The mapping in RISCV.def is 1:1, and should be used when the only information
-// available is the relocation enum value.
+// For each Vendor identifier, VENDOR, as associated with an `R_RISCV_VENDOR`,
+// the `ELF_RISCV_NONSTANDARD_RELOC_<VENDOR>(NAME, ID)` macro defines the
+// relocations available for that vendor identifier.
 
-// Qualcomm Nonstandard Relocations
-ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_ABS20_U,    192)
-ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_BRANCH,   193)
-ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_32,       194)
-ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_JUMP_PLT, 195)
+// QUALCOMM Nonstandard Relocations
+#ifndef ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM
+#define ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(_NAME, _ID)
+#endif
+
+ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_ABS20_U,    192)
+ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_BRANCH,   193)
+ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_32,       194)
+ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_JUMP_PLT, 195)
+
+#ifdef ELF_RISCV_NONSTANDARD_RELOC_ALL
+#undef ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM
+#endif
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index d8cfeb07e52b6..60c9eb4645842 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -48,9 +48,9 @@ std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
 #define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
 #undef ELF_RELOC
-#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
+#define ELF_RISCV_NONSTANDARD_RELOC_ALL(NAME, ID) .Case(#NAME, ID)
 #include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
-#undef ELF_RISCV_NONSTANDARD_RELOC
+#undef ELF_RISCV_NONSTANDARD_RELOC_ALL
                .Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
                .Case("BFD_RELOC_32", ELF::R_RISCV_32)
                .Case("BFD_RELOC_64", ELF::R_RISCV_64)

>From 04a66e740eac359c3b26f226a8a42665ad7f8468 Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Fri, 2 May 2025 15:54:46 -0700
Subject: [PATCH 2/2] Undefine all macros at the end of the file

---
 llvm/include/llvm/BinaryFormat/ELF.h                          | 1 -
 .../include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def | 4 ++--
 llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp        | 1 -
 3 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/ELF.h b/llvm/include/llvm/BinaryFormat/ELF.h
index 77077dad01f60..f298babfb9bb0 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -702,7 +702,6 @@ enum {
 #include "ELFRelocs/RISCV.def"
 #define ELF_RISCV_NONSTANDARD_RELOC_ALL(name, value) name = value,
 #include "ELFRelocs/RISCV_nonstandard.def"
-#undef ELF_RISCV_NONSTANDARD_RELOC_ALL
 };
 
 enum {
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
index a9c635ae13592..ce46ec029758d 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
@@ -34,6 +34,6 @@ ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_BRANCH,   193)
 ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_32,       194)
 ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM(R_RISCV_QC_E_JUMP_PLT, 195)
 
-#ifdef ELF_RISCV_NONSTANDARD_RELOC_ALL
+
+#undef ELF_RISCV_NONSTANDARD_RELOC_ALL
 #undef ELF_RISCV_NONSTANDARD_RELOC_QUALCOMM
-#endif
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 60c9eb4645842..4d4eb6808cf62 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -50,7 +50,6 @@ std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
 #undef ELF_RELOC
 #define ELF_RISCV_NONSTANDARD_RELOC_ALL(NAME, ID) .Case(#NAME, ID)
 #include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
-#undef ELF_RISCV_NONSTANDARD_RELOC_ALL
                .Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
                .Case("BFD_RELOC_32", ELF::R_RISCV_32)
                .Case("BFD_RELOC_64", ELF::R_RISCV_64)



More information about the llvm-commits mailing list