[llvm] [RISCV] Track Linker Relaxable through Assembly Relaxation (PR #152602)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 12:25:43 PDT 2025


================
@@ -861,25 +896,24 @@ bool RISCVAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
     return false;
   }
 
-  // If linker relaxation is enabled and supported by the current relocation,
-  // generate a relocation and then append a RELAX.
-  if (Fixup.isLinkerRelaxable())
+  // If linker relaxation is enabled and supported by the current fixup, then we
+  // always want to generate a relocation.
+  if (Fixup.isLinkerRelaxable() && fixupGetsRelaxRelocation(Fixup.getKind()))
     IsResolved = false;
+
   if (IsResolved && Fixup.isPCRel())
     IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);
 
   if (!IsResolved) {
-    // Some Fixups require a vendor relocation, record it (directly) before we
+    // Some Fixups require a VENDOR relocation, record it (directly) before we
     // add the relocation.
     maybeAddVendorReloc(F, Fixup);
 
     Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);
-  }
 
-  if (Fixup.isLinkerRelaxable()) {
-    auto FA = MCFixup::create(Fixup.getOffset(), nullptr, ELF::R_RISCV_RELAX);
-    Asm->getWriter().recordRelocation(F, FA, MCValue::get(nullptr),
-                                      FixedValueA);
+    // Some Fixups may get a RELAX relocation, record it (directly) after we
+    // add the relocation.
+    maybeAddRelaxReloc(F, Fixup);
----------------
MaskRay wrote:

The outlined function seems unnecessary. The previous open coding seems clearer.

```cpp
  // If linker relaxation is enabled and supported by the current fixup, then we
  // always want to generate a relocation.
  bool needsRelax = Fixup.isLinkerRelaxable() && fixupGetsRelaxRelocation(Fixup.getKind());
  if (needsRelax)
    IsResolved = false;

  if (IsResolved && Fixup.isPCRel())
    IsResolved = isPCRelFixupResolved(Target.getAddSym(), F);

  if (!IsResolved) {
    // Some Fixups require a VENDOR relocation, record it (directly) before we
    // add the relocation.
    maybeAddVendorReloc(F, Fixup);

    Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue);

    if (needsRelax) {
      ...
    }
  }
```

https://github.com/llvm/llvm-project/pull/152602


More information about the llvm-commits mailing list