This is an archive of the discontinued LLVM Phabricator instance.

[X86] Fix tls variable lowering issue with large code model
ClosedPublic

Authored by LuoYuanke on Feb 17 2019, 5:06 PM.

Details

Summary

The problem here is the lowering for tls variable. Below is the DAG for the code.
SelectionDAG has 11 nodes:

t0: ch = EntryToken

    t8: i64,ch = load<(load 8 from `i8 addrspace(257)* null`, addrspace 257)> t0, Constant:i64<0>, undef:i64
      t10: i64 = X86ISD::WrapperRIP TargetGlobalTLSAddress:i64<i32* @x> 0 [TF=10]
    t11: i64,ch = load<(load 8 from got)> t0, t10, undef:i64
  t12: i64 = add t8, t11
t4: i32,ch = load<(dereferenceable load 4 from @x)> t0, t12, undef:i64

t6: ch = CopyToReg t0, Register:i32 %0, t4
And when mcmodel is large, below instruction can NOT be folded.

t10: i64 = X86ISD::WrapperRIP TargetGlobalTLSAddress:i64<i32* @x> 0 [TF=10]

t11: i64,ch = load<(load 8 from got)> t0, t10, undef:i64
So "t11: i64,ch = load<(load 8 from got)> t0, t10, undef:i64" is lowered to " Morphed node: t11: i64,ch = MOV64rm<Mem:(load 8 from got)> t10, TargetConstant:i8<1>, Register:i64 $noreg, TargetConstant:i32<0>, Register:i32 $noreg, t0"

When llvm start to lower "t10: i64 = X86ISD::WrapperRIP TargetGlobalTLSAddress:i64<i32* @x> 0 [TF=10]", it fails.

The patch is to fold the load and X86ISD::WrapperRIP.

Fixes PR26906

Diff Detail

Repository
rL LLVM

Event Timeline

LuoYuanke created this revision.Feb 17 2019, 5:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 17 2019, 5:06 PM

Hi rnk,
Could you help to review? Thanks

rnk added inline comments.Feb 20 2019, 10:41 AM
lib/Target/X86/X86ISelDAGToDAG.cpp
1150 ↗(On Diff #187182)

"a mode"

test/CodeGen/X86/code-model-elf.ll
378 ↗(On Diff #187182)

These all look the same. Is that correct for the large code model? I thought in the large code model the GOT would be in a PIC register, which I don't see below. Is that known? Maybe there should be a FIXME comment here if you don't consider that to be in scope of this change.

411 ↗(On Diff #187182)

Wouldn't this load need to be through the PIC register?

LuoYuanke marked an inline comment as done.Feb 20 2019, 5:37 PM
LuoYuanke added inline comments.
test/CodeGen/X86/code-model-elf.ll
411 ↗(On Diff #187182)

Seems no, because in the test case, it specify the pie option "!2 = !{i32 7, !"PIE Level", i32 2}". We can generate the same result with gcc.

bash-4.2$ cat t.c
extern __thread int x;
int f(void) { return x; }

bash-4.2$ gcc -S -fpic -fpie t.c -mcmodel=large -o -

.file   "t.c"
.text
.globl  f
.type   f, @function

f:
.LFB0:

.cfi_startproc
pushq   %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq    %rsp, %rbp
.cfi_def_cfa_register 6
movq    x@gottpoff(%rip), %rax
movl    %fs:(%rax), %eax
popq    %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc

.LFE0:

.size   f, .-f
.ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-16)"
.section        .note.GNU-stack,"",@progbits
LuoYuanke updated this revision to Diff 187704.Feb 20 2019, 5:47 PM

Fix typo of comments.

LuoYuanke marked an inline comment as done.Feb 20 2019, 5:49 PM
LuoYuanke added inline comments.
lib/Target/X86/X86ISelDAGToDAG.cpp
1150 ↗(On Diff #187182)

Fixed. Thanks.

LuoYuanke marked an inline comment as done.Feb 20 2019, 5:58 PM
LuoYuanke added inline comments.
test/CodeGen/X86/code-model-elf.ll
378 ↗(On Diff #187182)

The result is same because we specify -fpie option in the test code. I notice tls large model for pic support is still not accurate, but it should be fixed in another patch. The patch focus on static tls or initial exec tls. The idea to add a FIXME comment is good. I'll do it.

LuoYuanke updated this revision to Diff 187715.Feb 20 2019, 6:56 PM

Add "FIXME" for tls compiled with pic but without pie option.

LuoYuanke added a comment.EditedFeb 21 2019, 5:27 PM

@rnk
Do you agree to just fix the " X86ISD::WrapperRIP TargetGlobalTLSAddress" issue in this patch? Do you have some other concerns or comments for the patch?

craig.topper edited the summary of this revision. (Show Details)Feb 21 2019, 5:38 PM
rnk accepted this revision.Feb 22 2019, 4:03 PM

lgtm

test/CodeGen/X86/code-model-elf.ll
411 ↗(On Diff #187182)

Hm, perhaps we really ought to fix the "PIC" checks to say "PIE".

This revision is now accepted and ready to land.Feb 22 2019, 4:03 PM
This revision was automatically updated to reflect the committed changes.