[llvm] 9234d07 - MCAssembler: Optimize PCRel fixups

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 3 00:01:57 PDT 2025


Author: Fangrui Song
Date: 2025-07-03T00:01:53-07:00
New Revision: 9234d077522ea3718e9486a28df49e7cfb4c336a

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

LOG: MCAssembler: Optimize PCRel fixups

* MCAssembler::evaluateFixup sets MCFixup::PCRel.
* ELFObjectWriter retrieves the bit from the MCFixup argument.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAssembler.h
    llvm/include/llvm/MC/MCFixup.h
    llvm/lib/MC/ELFObjectWriter.cpp
    llvm/lib/MC/MCAssembler.cpp
    llvm/lib/MC/XCOFFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAssembler.h b/llvm/include/llvm/MC/MCAssembler.h
index 03744128cb639..bfd6375ec5bcc 100644
--- a/llvm/include/llvm/MC/MCAssembler.h
+++ b/llvm/include/llvm/MC/MCAssembler.h
@@ -101,7 +101,7 @@ class MCAssembler {
   /// out.
   /// \param RecordReloc Record relocation if needed.
   /// relocation.
-  bool evaluateFixup(const MCFragment &F, const MCFixup &Fixup, MCValue &Target,
+  bool evaluateFixup(const MCFragment &F, MCFixup &Fixup, MCValue &Target,
                      uint64_t &Value, bool RecordReloc,
                      MutableArrayRef<char> Contents) const;
 

diff  --git a/llvm/include/llvm/MC/MCFixup.h b/llvm/include/llvm/MC/MCFixup.h
index 7575202eaec57..ab2e47189fdb7 100644
--- a/llvm/include/llvm/MC/MCFixup.h
+++ b/llvm/include/llvm/MC/MCFixup.h
@@ -73,6 +73,8 @@ class MCFixup {
   /// determine how the operand value should be encoded into the instruction.
   MCFixupKind Kind = FK_NONE;
 
+  bool PCRel = false;
+
   /// Used by RISC-V style linker relaxation. Whether the fixup is
   /// linker-relaxable.
   bool LinkerRelaxable = false;
@@ -105,6 +107,8 @@ class MCFixup {
 
   const MCExpr *getValue() const { return Value; }
 
+  bool isPCRel() const { return PCRel; }
+  void setPCRel() { PCRel = true; }
   bool isLinkerRelaxable() const { return LinkerRelaxable; }
   void setLinkerRelaxable() { LinkerRelaxable = true; }
 

diff  --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index 3fed77fc1bb80..0d427b52dd863 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -1326,7 +1326,6 @@ bool ELFObjectWriter::checkRelocation(SMLoc Loc, const MCSectionELF *From,
 void ELFObjectWriter::recordRelocation(const MCFragment &F,
                                        const MCFixup &Fixup, MCValue Target,
                                        uint64_t &FixedValue) {
-  MCAsmBackend &Backend = Asm->getBackend();
   const MCSectionELF &Section = cast<MCSectionELF>(*F.getParent());
   MCContext &Ctx = getContext();
 
@@ -1337,8 +1336,7 @@ void ELFObjectWriter::recordRelocation(const MCFragment &F,
   if (DwoOS && !checkRelocation(Fixup.getLoc(), &Section, SecA))
     return;
 
-  bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
-                 MCFixupKindInfo::FKF_IsPCRel;
+  bool IsPCRel = Fixup.isPCRel();
   uint64_t FixupOffset = Asm->getFragmentOffset(F) + Fixup.getOffset();
   uint64_t Addend = Target.getConstant();
   if (auto *RefB = Target.getSubSym()) {

diff  --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 98225c0061284..cc21cf41c2660 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -142,7 +142,7 @@ bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
   return true;
 }
 
-bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
+bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup,
                                 MCValue &Target, uint64_t &Value,
                                 bool RecordReloc,
                                 MutableArrayRef<char> Contents) const {
@@ -163,6 +163,7 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
 
   bool IsResolved = false;
   unsigned FixupFlags = getBackend().getFixupKindInfo(Fixup.getKind()).Flags;
+  bool IsPCRel = FixupFlags & MCFixupKindInfo::FKF_IsPCRel;
   if (FixupFlags & MCFixupKindInfo::FKF_IsTarget) {
     IsResolved = getBackend().evaluateTargetFixup(Fixup, Target, Value);
   } else {
@@ -174,7 +175,6 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
     if (Sub && Sub->isDefined())
       Value -= getSymbolOffset(*Sub);
 
-    bool IsPCRel = FixupFlags & MCFixupKindInfo::FKF_IsPCRel;
     bool ShouldAlignPC =
         FixupFlags & MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
     if (IsPCRel) {
@@ -202,6 +202,8 @@ bool MCAssembler::evaluateFixup(const MCFragment &F, const MCFixup &Fixup,
 
   if (IsResolved && mc::isRelocRelocation(Fixup.getKind()))
     IsResolved = false;
+  if (IsPCRel)
+    Fixup.setPCRel();
   getBackend().applyFixup(F, Fixup, Target, Contents, Value, IsResolved);
   return true;
 }
@@ -875,7 +877,7 @@ void MCAssembler::layout() {
       // Process fragments with fixups here.
       if (auto *F = dyn_cast<MCEncodedFragment>(&Frag)) {
         auto Contents = F->getContents();
-        for (const MCFixup &Fixup : F->getFixups()) {
+        for (MCFixup &Fixup : F->getFixups()) {
           uint64_t FixedValue;
           MCValue Target;
           evaluateFixup(Frag, Fixup, Target, FixedValue,

diff  --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp
index ddb2ca8a03e2e..bfc8c5058b67e 100644
--- a/llvm/lib/MC/XCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/XCOFFObjectWriter.cpp
@@ -686,15 +686,10 @@ void XCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
   };
 
   const MCSymbol *const SymA = Target.getAddSym();
-
-  MCAsmBackend &Backend = Asm->getBackend();
-  bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
-                 MCFixupKindInfo::FKF_IsPCRel;
-
   uint8_t Type;
   uint8_t SignAndSize;
-  std::tie(Type, SignAndSize) =
-      TargetObjectWriter->getRelocTypeAndSignSize(Target, Fixup, IsPCRel);
+  std::tie(Type, SignAndSize) = TargetObjectWriter->getRelocTypeAndSignSize(
+      Target, Fixup, Fixup.isPCRel());
 
   const MCSectionXCOFF *SymASec = getContainingCsect(cast<MCSymbolXCOFF>(SymA));
   assert(SectionMap.contains(SymASec) &&


        


More information about the llvm-commits mailing list