Skip to content

Commit bea4e92

Browse files
abidhmemfrob
authored andcommitted
[AMDGPU] Handle s_branch to another section.
Currently, if target of s_branch instruction is in another section, it will fail with the error of undefined label. Although in this case, the label is not undefined but present in another section. This patch tries to handle this issue. So while handling fixup_si_sopp_br fixup in getRelocType, if the target label is undefined we issue an error as before. If it is defined, a new relocation type R_AMDGPU_REL16 is returned. This issue has been reported in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100181 and https://bugs.llvm.org/show_bug.cgi?id=45887. Before https://reviews.llvm.org/D79943, we used to get an crash for this scenario. The crash is fixed now but the we still get an undefined label error. Jumps to other section can arise with hold/cold splitting. A patch to handle the relocation in lld will follow shortly. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D105760
1 parent 5b724c3 commit bea4e92

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

llvm/docs/AMDGPUUsage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@ The following relocation types are supported:
15741574
``R_AMDGPU_REL32_HI`` Static 11 ``word32`` (S + A - P) >> 32
15751575
*reserved* 12
15761576
``R_AMDGPU_RELATIVE64`` Dynamic 13 ``word64`` B + A
1577+
``R_AMDGPU_REL16`` Static 14 ``word16`` ((S + A - P) - 4) / 4
15771578
========================== ======= ===== ========== ==============================
15781579

15791580
``R_AMDGPU_ABS32_LO`` and ``R_AMDGPU_ABS32_HI`` are only supported by

llvm/include/llvm/BinaryFormat/ELFRelocs/AMDGPU.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ ELF_RELOC(R_AMDGPU_GOTPCREL32_HI, 9)
1515
ELF_RELOC(R_AMDGPU_REL32_LO, 10)
1616
ELF_RELOC(R_AMDGPU_REL32_HI, 11)
1717
ELF_RELOC(R_AMDGPU_RELATIVE64, 13)
18+
ELF_RELOC(R_AMDGPU_REL16, 14)

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
8080
const auto *SymA = Target.getSymA();
8181
assert(SymA);
8282

83-
Ctx.reportError(Fixup.getLoc(),
84-
Twine("undefined label '") + SymA->getSymbol().getName() + "'");
85-
return ELF::R_AMDGPU_NONE;
83+
if (SymA->getSymbol().isUndefined()) {
84+
Ctx.reportError(Fixup.getLoc(), Twine("undefined label '") +
85+
SymA->getSymbol().getName() + "'");
86+
return ELF::R_AMDGPU_NONE;
87+
}
88+
return ELF::R_AMDGPU_REL16;
8689
}
8790

8891
llvm_unreachable("unhandled relocation type");

llvm/test/MC/AMDGPU/reloc.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// CHECK: R_AMDGPU_GOTPCREL32_HI global_var2
1010
// CHECK: R_AMDGPU_REL32_LO global_var3
1111
// CHECK: R_AMDGPU_REL32_HI global_var4
12+
// CHECK: R_AMDGPU_REL16 .text.unlikely
1213
// CHECK: R_AMDGPU_ABS32 var
1314
// CHECK: }
1415
// CHECK: .rel.data {
@@ -25,6 +26,11 @@ kernel:
2526
s_mov_b32 s4, global_var2@gotpcrel32@hi
2627
s_mov_b32 s5, global_var3@rel32@lo
2728
s_mov_b32 s6, global_var4@rel32@hi
29+
s_branch cold
30+
31+
.section .text.unlikely
32+
cold:
33+
s_add_i32 s15, s15, 1
2834

2935
.globl global_var0
3036
.globl global_var1

llvm/test/tools/llvm-readobj/ELF/reloc-types-elf-amdgpu.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# CHECK-NEXT: 0x0 R_AMDGPU_REL32_LO - 0x0
1919
# CHECK-NEXT: 0x0 R_AMDGPU_REL32_HI - 0x0
2020
# CHECK-NEXT: 0x0 R_AMDGPU_RELATIVE64 - 0x0
21+
# CHECK-NEXT: 0x0 R_AMDGPU_REL16 - 0x0
2122
# CHECK-NEXT: }
2223

2324
!ELF
@@ -43,3 +44,4 @@ Sections:
4344
- Type: R_AMDGPU_REL32_LO
4445
- Type: R_AMDGPU_REL32_HI
4546
- Type: R_AMDGPU_RELATIVE64
47+
- Type: R_AMDGPU_REL16

0 commit comments

Comments
 (0)