Skip to content

Commit e0e8071

Browse files
quic-akaryakitstellar
authored andcommitted
[hexagon] Prevent alignment search beyond a label (llvm#130631)
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)
1 parent 2198410 commit e0e8071

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,24 @@ class HexagonAsmBackend : public MCAsmBackend {
728728
MCContext &Context = Asm.getContext();
729729
auto &RF = cast<MCRelaxableFragment>(*Frags[K]);
730730
auto &Inst = const_cast<MCInst &>(RF.getInst());
731+
732+
const bool WouldTraverseLabel = llvm::any_of(
733+
Asm.symbols(), [&Asm, &RF, &Inst](MCSymbol const &sym) {
734+
uint64_t Offset = 0;
735+
const bool HasOffset = Asm.getSymbolOffset(sym, Offset);
736+
const unsigned PacketSizeBytes =
737+
HexagonMCInstrInfo::bundleSize(Inst) *
738+
HEXAGON_INSTR_SIZE;
739+
const bool OffsetPastSym =
740+
Offset <= (Asm.getFragmentOffset(RF) + PacketSizeBytes);
741+
return !sym.isVariable() && Offset != 0 && HasOffset &&
742+
OffsetPastSym;
743+
});
744+
if (WouldTraverseLabel) {
745+
Size = 0;
746+
break;
747+
}
748+
731749
while (Size > 0 &&
732750
HexagonMCInstrInfo::bundleSize(Inst) < MaxPacketSize) {
733751
MCInst *Nop = Context.createMCInst();

llvm/test/MC/Hexagon/align-leb128.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# RUN: llvm-mc -triple=hexagon -filetype=obj %s | llvm-readelf -x .data - \
2+
# RUN: | FileCheck %s --match-full-lines
3+
4+
# Illustrate the case when padding packets across labels also breaks leb128
5+
# relocations. This happens because .align padding is inserted once at the
6+
# very end of the section layout.
7+
L1:
8+
nop
9+
L2:
10+
.size L1, L2-L1
11+
.align 16
12+
nop
13+
.data
14+
.word L2-L1
15+
.uleb128 L2-L1
16+
17+
# CHECK: Hex dump of section '.data':
18+
# CHECK-NEXT: 0x00000000 04000000 04 .....

llvm/test/MC/Hexagon/align.s

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,16 @@ r0 = vextract(v0, r0)
5858
r1 = sub (##1, r1) }
5959
.align 16
6060
{ r0 = sub (#1, r0) }
61+
62+
# Don't search backwards to pad packets beyond a label:
63+
{ r1 = add(r1, r0) }
64+
# CHECK-NEXT: { r1 = add(r1,r0)
65+
# CHECK-NOT: nop
66+
67+
post_label:
68+
.align 16
69+
# CHECK-LABEL: post_label
70+
# CHECK-NEXT: { nop
71+
# CHECK-NEXT: nop }
72+
# CHECK-NEXT: { r1 = sub(#1,r1) }
73+
{ r1 = sub(#1, r1) }

0 commit comments

Comments
 (0)