Skip to content

Commit 8a40a7b

Browse files
committed
COFF: When generating code for LTO, use static reloc model on 32-bit x86.
Fixes PR34306. This is because it usually results in more compact code, and because there are also known code generation bugs when using the PIC model (see bug). Based on a patch by Carlo Kok. Differential Revision: https://reviews.llvm.org/D38769 llvm-svn: 315400
1 parent 85b1da1 commit 8a40a7b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lld/COFF/LTO.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ static void saveBuffer(StringRef Buffer, const Twine &Path) {
6464
static std::unique_ptr<lto::LTO> createLTO() {
6565
lto::Config Conf;
6666
Conf.Options = InitTargetOptionsFromCodeGenFlags();
67-
Conf.RelocModel = Reloc::PIC_;
67+
// Use static reloc model on 32-bit x86 because it usually results in more
68+
// compact code, and because there are also known code generation bugs when
69+
// using the PIC model (see PR34306).
70+
if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
71+
Conf.RelocModel = Reloc::Static;
72+
else
73+
Conf.RelocModel = Reloc::PIC_;
6874
Conf.DisableVerify = true;
6975
Conf.DiagHandler = diagnosticHandler;
7076
Conf.OptLevel = Config->LTOOptLevel;

lld/test/COFF/lto-reloc-model.ll

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llvm-as -o %t %s
2+
; RUN: lld-link /entry:main /subsystem:console /out:%t.exe %t
3+
; RUN: llvm-objdump -d %t.exe | FileCheck %s
4+
5+
target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
6+
target triple = "i686-pc-windows-msvc"
7+
8+
@foo = thread_local global i8 0
9+
10+
module asm "__tls_index = 1"
11+
module asm "__tls_array = 2"
12+
13+
define i8* @main() {
14+
; CHECK: movl 1, %eax
15+
; CHECK: movl %fs:2, %ecx
16+
; CHECK: movl (%ecx,%eax,4), %eax
17+
; CHECK: leal (%eax), %eax
18+
ret i8* @foo
19+
}

0 commit comments

Comments
 (0)