Skip to content

[hexagon] Prevent alignment search beyond a label #130631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025

Conversation

quic-akaryaki
Copy link
Contributor

When searching for packets to .align, don't consider ones which would require padding beyond a label.

There are two problems with padding beyond a label:

  • the distance between labels may increase for some offsets to become too large;
  • u/sleb128 values that encode a difference will not be updated because they are computed before the align command is handled.

This is more a short-term fix/hack. The proper solution would be to unify .align and .falign handling and move it to the layout loop.

@llvmbot llvmbot added backend:Hexagon mc Machine (object) code labels Mar 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2025

@llvm/pr-subscribers-mc

@llvm/pr-subscribers-backend-hexagon

Author: Alexey Karyakin (quic-akaryaki)

Changes

When searching for packets to .align, don't consider ones which would require padding beyond a label.

There are two problems with padding beyond a label:

  • the distance between labels may increase for some offsets to become too large;
  • u/sleb128 values that encode a difference will not be updated because they are computed before the align command is handled.

This is more a short-term fix/hack. The proper solution would be to unify .align and .falign handling and move it to the layout loop.


Full diff: https://github.com/llvm/llvm-project/pull/130631.diff

3 Files Affected:

  • (modified) llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp (+18)
  • (added) llvm/test/MC/Hexagon/align-leb128.s (+18)
  • (modified) llvm/test/MC/Hexagon/align.s (+13)
diff --git a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
index 98b1dde8fa3fc..725067e0c9bdd 100644
--- a/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
+++ b/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
@@ -728,6 +728,24 @@ class HexagonAsmBackend : public MCAsmBackend {
               MCContext &Context = Asm.getContext();
               auto &RF = cast<MCRelaxableFragment>(*Frags[K]);
               auto &Inst = const_cast<MCInst &>(RF.getInst());
+
+              const bool WouldTraverseLabel = llvm::any_of(
+                  Asm.symbols(), [&Asm, &RF, &Inst](MCSymbol const &sym) {
+                    uint64_t Offset = 0;
+                    const bool HasOffset = Asm.getSymbolOffset(sym, Offset);
+                    const unsigned PacketSizeBytes =
+                        HexagonMCInstrInfo::bundleSize(Inst) *
+                        HEXAGON_INSTR_SIZE;
+                    const bool OffsetPastSym =
+                        Offset <= (Asm.getFragmentOffset(RF) + PacketSizeBytes);
+                    return !sym.isVariable() && Offset != 0 && HasOffset &&
+                           OffsetPastSym;
+                  });
+              if (WouldTraverseLabel) {
+                Size = 0;
+                break;
+              }
+
               while (Size > 0 &&
                      HexagonMCInstrInfo::bundleSize(Inst) < MaxPacketSize) {
                 MCInst *Nop = Context.createMCInst();
diff --git a/llvm/test/MC/Hexagon/align-leb128.s b/llvm/test/MC/Hexagon/align-leb128.s
new file mode 100644
index 0000000000000..65cbf6c24cf07
--- /dev/null
+++ b/llvm/test/MC/Hexagon/align-leb128.s
@@ -0,0 +1,18 @@
+# RUN: llvm-mc -triple=hexagon -filetype=obj %s | llvm-readelf -x .data - \
+# RUN:   | FileCheck %s --match-full-lines
+
+# Illustrate the case when padding packets across labels also breaks leb128
+# relocations. This happens because .align padding is inserted once at the
+# very end of the section layout.
+L1:
+  nop
+L2:
+.size L1, L2-L1
+.align 16
+  nop
+.data
+.word L2-L1
+.uleb128 L2-L1
+
+# CHECK: Hex dump of section '.data':
+# CHECK-NEXT: 0x00000000 04000000 04
diff --git a/llvm/test/MC/Hexagon/align.s b/llvm/test/MC/Hexagon/align.s
index 9c2978df71373..e17d09cfd8c96 100644
--- a/llvm/test/MC/Hexagon/align.s
+++ b/llvm/test/MC/Hexagon/align.s
@@ -58,3 +58,16 @@ r0 = vextract(v0, r0)
   r1 = sub (##1, r1) }
 .align 16
 { r0 = sub (#1, r0) }
+
+# Don't search backwards to pad packets beyond a label:
+{ r1 = add(r1, r0) }
+# CHECK-NEXT: { r1 = add(r1,r0)
+# CHECK-NOT:  nop
+
+post_label:
+.align 16
+# CHECK-LABEL: post_label
+# CHECK-NEXT: { nop
+# CHECK-NEXT:   nop }
+# CHECK-NEXT: { r1 = sub(#1,r1) }
+{ r1 = sub(#1, r1) }

@quic-akaryaki quic-akaryaki self-assigned this Mar 10, 2025
When searching for packets to .align, don't consider
ones which would require padding beyond a label.

There are two problems with padding beyond a label:
- the distance between labels may increase for some
  offsets to become too large;
- u/sleb128 values that encode a difference will not
  be updated because they are computed before the
  align command is handled.

This is more a short-term fix/hack. The proper solution
would be to unify `.align` and `.falign` handling and
move it to the layout loop.
@quic-akaryaki quic-akaryaki force-pushed the hexagon-align-beyond-label branch from 608bae5 to 378fa27 Compare March 10, 2025 21:11
@iajbar iajbar self-requested a review March 10, 2025 21:29
@quic-akaryaki quic-akaryaki merged commit 1fe4631 into llvm:main Mar 11, 2025
11 checks passed
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Mar 25, 2025
When searching for packets to .align, don't consider ones which would
require padding beyond a label.

There are two problems with padding beyond a label:
- the distance between labels may increase for some offsets to become
too large;
- u/sleb128 values that encode a difference will not be updated because
they are computed before the align command is handled.

This is more a short-term fix/hack. The proper solution would be to
unify `.align` and `.falign` handling and move it to the layout loop.

(cherry picked from commit 1fe4631)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:Hexagon mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants