[PATCH] D19995: Optimize access to global variable references in PIE mode when linker supports copy relocations for PIE

Sriraman Tallam via llvm-commits llvm-commits at lists.llvm.org
Thu May 5 14:19:46 PDT 2016


tmsriram created this revision.
tmsriram added a reviewer: rnk.
tmsriram added subscribers: llvm-commits, davidxl, rafael.

For X86, in PIE mode,  when accessing extern global variables, an extra load from the GOT can be avoided if the linker allows copy relocations for PIE.  This patch makes it possible.  This is already available in the GCC compiler.   

This patch looks for "PIE Copy Relocs" in the module flags to optimize the access to the global variable.  I am preparing a Clang patch that adds a new option "-mpiecopyrelocs" to indicate copy relocation support and add the module flag when the option is passed.

GCC patch discussing this : https://gcc.gnu.org/ml/gcc-patches/2014-05/msg01215.html

Short summary:

* When linking executables in non-PIE mode, references to external globals do not use the GOT. They are treated as if the globals were defined in the executable.  If it so happens that the global is not defined in the executable, a copy relocation is used to duplicate the definition in the executable.
* Currently, in PIE mode, all accesses to external globals use an extra load from the GOT to get its address which is then dereferenced.  This is particularly inefficient if it turns out at link time that the global ends up being defined in the executable from a different compilation unit.  It turns out that copy relocations can be used here just like non-PIE mode to always avoid the extra load from the GOT and directly access the global.
* Gold and GNU ld started supporting copy relocations for pie only recently.  Gold supports via this patch : https://sourceware.org/ml/binutils/2014-05/msg00092.html
* Since this technique cannot be used to avoid the extra load if the linker does not support it, I am creating a clang patch to add an extra option '-mpiecopyrelocs' which can be used to specify support.
* Support for linker copy relocations will be specified in bit code as a module flag "PIE Copy Relocations".
* This technique can be used for 32 and 64 bit x86.  However, gold still does not support copy relocations for 32-bit PIE. I will do that separately.


http://reviews.llvm.org/D19995

Files:
  include/llvm/IR/Module.h
  lib/IR/Module.cpp
  lib/Target/X86/X86Subtarget.cpp
  test/CodeGen/X86/global-access-pie-copyrelocs.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19995.56344.patch
Type: text/x-patch
Size: 6089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160505/19c814b5/attachment.bin>


More information about the llvm-commits mailing list