[llvm-branch-commits] MC: Refactor FT_Align fragments when linker relaxation is enabled (PR #149465)

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Jul 19 17:25:28 PDT 2025


================
@@ -230,22 +230,24 @@ uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const {
   case MCFragment::FT_Align: {
     unsigned Offset = F.Offset + F.getFixedSize();
     unsigned Size = offsetToAlignment(Offset, F.getAlignment());
-
-    // Insert extra Nops for code alignment if the target define
-    // shouldInsertExtraNopBytesForCodeAlign target hook.
-    if (F.getParent()->useCodeAlign() && F.hasAlignEmitNops() &&
-        getBackend().shouldInsertExtraNopBytesForCodeAlign(F, Size))
-      return F.getFixedSize() + Size;
-
-    // If we are padding with nops, force the padding to be larger than the
-    // minimum nop size.
-    if (Size > 0 && F.hasAlignEmitNops()) {
-      while (Size % getBackend().getMinimumNopSize())
-        Size += F.getAlignment().value();
+    auto &Frag = const_cast<MCFragment &>(F);
+    // In the nops mode, RISC-V style linker relaxation might adjust the size
+    // and add a fixup, even if `Size` is originally 0.
+    bool AlignFixup = false;
+    if (F.hasAlignEmitNops()) {
+      AlignFixup = getBackend().relaxAlign(Frag, Size);
+      // If the backend does not handle the fragment specially, pad with nops,
+      // but ensure that the padding is larger than the minimum nop size.
+      if (!AlignFixup)
+        while (Size % getBackend().getMinimumNopSize())
+          Size += F.getAlignment().value();
     }
-    if (Size > F.getAlignMaxBytesToEmit())
+    if (!AlignFixup && Size > F.getAlignMaxBytesToEmit())
       Size = 0;
-    return F.getFixedSize() + Size;
+    Frag.VarContentEnd = F.VarContentStart + Size;
+    if (Frag.VarContentEnd > Frag.getParent()->ContentStorage.size())
+      Frag.getParent()->ContentStorage.resize(Frag.VarContentEnd);
----------------
MaskRay wrote:

Added comment "// Update the variable tail size. The content is ignored."

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


More information about the llvm-branch-commits mailing list