This is an archive of the discontinued LLVM Phabricator instance.

[X86] Implement more of x86-64 large and medium PIC code models
ClosedPublic

Authored by rnk on May 22 2018, 11:29 AM.

Details

Summary

The large code model allows code and data segments to exceed 2GB, which
means that some symbol references may require a displacement that cannot
be encoded as a displacement from RIP. The large PIC model even relaxes
the assumption that the GOT itself is within 2GB of all code. Therefore,
we need a special code sequence to materialize it:

.LtmpN:
  leaq .LtmpN(%rip), %rbx
  movabsq $_GLOBAL_OFFSET_TABLE_-.LtmpN, %rax # Scratch
  addq %rax, %rbx # GOT base reg

From that, non-local references go through the GOT base register instead
of being PC-relative loads. Local references typically use GOTOFF
symbols, like this:

movq extern_gv@GOT(%rbx), %rax
movq local_gv@GOTOFF(%rbx), %rax

All calls end up being indirect:

movabsq $local_fn@GOTOFF, %rax
addq %rbx, %rax
callq *%rax

The medium code model retains the assumption that the code segment is
less than 2GB, so calls are once again direct, and the RIP-relative
loads can be used to access the GOT. Materializing the GOT is easy:

leaq _GLOBAL_OFFSET_TABLE_(%rip), %rbx # GOT base reg

DSO local data accesses will use it:

movq local_gv@GOTOFF(%rbx), %rax

Non-local data accesses will use RIP-relative addressing, which means we
may not always need to materialize the GOT base:

movq extern_gv@GOTPCREL(%rip), %rax

Direct calls are basically the same as they are in the small code model:
They use direct, PC-relative addressing, and the PLT is used for calls
to non-local functions.

This patch adds reasonably comprehensive testing of LEA, but there are
lots of interesting folding opportunities that are unimplemented.

Diff Detail

Repository
rL LLVM

Event Timeline

rnk created this revision.May 22 2018, 11:29 AM
rnk updated this revision to Diff 152383.Jun 21 2018, 2:45 PM

rebase

This revision was not accepted when it landed; it landed in state Needs Review.Jun 21 2018, 2:59 PM
This revision was automatically updated to reflect the committed changes.

Sorry about the late review. I've added a lot of comment and documentation requests for the code and a couple of questions.

llvm/trunk/lib/Target/X86/X86ISelDAGToDAG.cpp
946

That's a lot of negatives :)

Also doesn't entirely look like the comment describes what's going on here - there's no comment about medium and riprel here. Otherwise you should be able to just use "M == CodeModel:Large" or something?

Thoughts?

llvm/trunk/lib/Target/X86/X86InstrInfo.cpp
12726

Comment update?

llvm/trunk/lib/Target/X86/X86Subtarget.cpp
117

Comment update.

139

This could probably use a comment as well.

llvm/trunk/lib/Target/X86/X86TargetMachine.cpp
159

Not sure I understand this change?

rnk reopened this revision.Jun 22 2018, 4:12 PM
rnk marked an inline comment as done.
rnk added a subscriber: Jianping.
rnk added inline comments.
llvm/trunk/lib/Target/X86/X86ISelDAGToDAG.cpp
946

Yeah, I think I wrote the comment, and then edited the code so that it was stale. @Jianping made the same change in D48417, so I think it's right.

llvm/trunk/lib/Target/X86/X86InstrInfo.cpp
12726

I actually did update this one, but it missed the phab upload: rL335298 I'll be in the reland.

llvm/trunk/lib/Target/X86/X86TargetMachine.cpp
159

Many JIT tests use the large code model because object file sections may be spread out across memory. They don't need PIC codegen, though, since they are running locally in process memory and they don't need to be relocated. This was the cleanest way I could come up with to make them default to static codegen instead of PIC codegen.

However, that didn't save me from (Linux only) tests that explicitly exercise the PIC model:
llvm/test/ExecutionEngine/MCJIT/eh-lg-pic.ll

And I guess I broke a bunch of Darwin stuff :(
https://reviews.llvm.org/P8087
http://green.lab.llvm.org/green/job/clang-stage1-cmake-RA-incremental/49895/consoleFull

I've fixed the ELF MCJIT, but I'm not sure what to do about the macho things.

rnk updated this revision to Diff 152558.Jun 22 2018, 5:15 PM
  • rebase over ELF rtdyld changes
This revision was not accepted when it landed; it landed in state Needs Review.Jun 25 2018, 11:21 AM
This revision was automatically updated to reflect the committed changes.