Skip to content

Commit fbe5f0e

Browse files
committed
[X86] Do not use movq in -mcmodel=kernel on an out of range abs global
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` attached and the range cannot fit within 32 bits, we should reject the MOV64ri32. The pattern matching logic will eventually lead to a match for MOV64ri which emits the `movabs`.
1 parent 3f3ffed commit fbe5f0e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

llvm/lib/Target/X86/X86InstrCompiler.td

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,20 @@ 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 has an absolute_symbol range, we should assert it would
1261+
// fit into a 32-bit offset.
1262+
def inRange32GlobalAddr : PatLeaf<(tglobaladdr:$dst), [{
1263+
auto *GA = cast<GlobalAddressSDNode>(N);
1264+
std::optional<ConstantRange> CR = GA->getGlobal()->getAbsoluteSymbolRange();
1265+
if (!CR)
1266+
return true;
1267+
return CR->getSignedMin().sge(-1ull << 32) &&
1268+
CR->getSignedMax().slt(1ull << 32);
1269+
}]>;
1270+
def : Pat<(i64 (X86Wrapper inRange32GlobalAddr:$dst)),
1271+
(MOV64ri32 tglobaladdr:$dst)>, Requires<[KernelCode]>;
1272+
12611273
def : Pat<(i64 (X86Wrapper texternalsym:$dst)),
12621274
(MOV64ri32 texternalsym:$dst)>, Requires<[KernelCode]>;
12631275
def : Pat<(i64 (X86Wrapper mcsym:$dst)),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_abs_sym
6+
define i64 @func_abs_sym() nounwind {
7+
; CHECK: movabsq $abs_sym, %rax
8+
%1 = ptrtoint ptr @abs_sym to i64
9+
ret i64 %1
10+
}
11+
12+
; CHECK-LABEL: func_no_abs_sym
13+
define i64 @func_no_abs_sym() nounwind {
14+
; CHECK: movq $no_abs_sym, %rax
15+
%1 = ptrtoint ptr @no_abs_sym to i64
16+
ret i64 %1
17+
}
18+
19+
@abs_sym = external hidden global [0 x i8], !absolute_symbol !0
20+
@no_abs_sym = external hidden global [0 x i8]
21+
22+
!0 = !{i64 -1, i64 -1}

0 commit comments

Comments
 (0)