Skip to content

Commit bd6ed29

Browse files
authored
[X86] Do not use movq in -mcmodel=kernel on an out of range abs global (#163323)
CFI can lead to some `relocation R_X86_64_32S out of range` errors when using thinlto. We have an instance of an `inline_bits` alias with a value of 0x8000008000000001 which cannot fit into a signed 32-bit reloc hence the error. This reloc is used because the instruction for reading the alias is `movq` which uses a signed 32-bit immediate. The proper instruction to use in this instance is `movabs` for the full 64-bit reloc. Under the kernel model, a signed 32-bit immediate was always used but if the target is a global with `absolue_symbol` we should reject the MOV64ri32. The pattern matching logic will eventually lead to a match for MOV64ri which emits the `movabs`.
1 parent 1673501 commit bd6ed29

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

llvm/lib/Target/X86/X86InstrCompiler.td

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,17 @@ def : Pat<(i64 (X86Wrapper tconstpool :$dst)),
12561256
(MOV64ri32 tconstpool :$dst)>, Requires<[KernelCode]>;
12571257
def : Pat<(i64 (X86Wrapper tjumptable :$dst)),
12581258
(MOV64ri32 tjumptable :$dst)>, Requires<[KernelCode]>;
1259-
def : Pat<(i64 (X86Wrapper tglobaladdr :$dst)),
1260-
(MOV64ri32 tglobaladdr :$dst)>, Requires<[KernelCode]>;
1259+
1260+
// If the globaladdr is an absolute_symbol, don't bother using the sign extending
1261+
// instruction since there's no benefit to using it with absolute symbols.
1262+
def globalAddrNoAbsSym : PatLeaf<(tglobaladdr:$dst), [{
1263+
auto *GA = cast<GlobalAddressSDNode>(N);
1264+
return !GA->getGlobal()->getAbsoluteSymbolRange();
1265+
}]>;
1266+
def : Pat<(i64 (X86Wrapper globalAddrNoAbsSym:$dst)),
1267+
(MOV64ri32 tglobaladdr:$dst)>,
1268+
Requires<[KernelCode]>;
1269+
12611270
def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
12621271
(MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>;
12631272
def : Pat<(i64 (X86Wrapper mcsym:$dst)),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; RUN: llc --code-model=kernel < %s -asm-verbose=0 | FileCheck %s
2+
3+
target triple = "x86_64-unknown-linux-gnu"
4+
5+
; CHECK-LABEL: func_no_abs_sym
6+
define i64 @func_no_abs_sym() nounwind {
7+
; CHECK: movq $no_abs_sym, %rax
8+
%1 = ptrtoint ptr @no_abs_sym to i64
9+
ret i64 %1
10+
}
11+
12+
; CHECK-LABEL: func_abs_sym
13+
define i64 @func_abs_sym() nounwind {
14+
; CHECK: movabsq $abs_sym, %rax
15+
%1 = ptrtoint ptr @abs_sym to i64
16+
ret i64 %1
17+
}
18+
19+
; CHECK-LABEL: func_abs_sym_in_range
20+
define i64 @func_abs_sym_in_range() nounwind {
21+
;; The absolute_symbol range fits in 32 bits but we still use movabs
22+
;; since there's no benefit to using the sign extending instruction
23+
;; with absolute symbols.
24+
; CHECK: movabsq $abs_sym_in_range, %rax
25+
%1 = ptrtoint ptr @abs_sym_in_range to i64
26+
ret i64 %1
27+
}
28+
29+
@no_abs_sym = external hidden global [0 x i8]
30+
@abs_sym = external hidden global [0 x i8], !absolute_symbol !0
31+
@abs_sym_in_range = external hidden global [0 x i8], !absolute_symbol !1
32+
33+
!0 = !{i64 -1, i64 -1} ;; Full range
34+
!1 = !{i64 -2147483648, i64 2147483648} ;; In range

0 commit comments

Comments
 (0)