Skip to content
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

release/20.x: [hexagon] Prevent alignment search beyond a label (#130631) #132336

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp
Original file line number Diff line number Diff line change
@@ -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();
18 changes: 18 additions & 0 deletions llvm/test/MC/Hexagon/align-leb128.s
Original file line number Diff line number Diff line change
@@ -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 .....
13 changes: 13 additions & 0 deletions llvm/test/MC/Hexagon/align.s
Original file line number Diff line number Diff line change
@@ -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) }
Loading