[PATCH] D102090: [CMake][ELF] Add -fno-semantic-interposition and -Bsymbolic-functions

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 7 13:06:22 PDT 2021


MaskRay created this revision.
Herald added subscribers: pengfei, mgorny.
MaskRay requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

In an ELF shared object, a default visibility defined symbol is preemptible by default.
This creates some missed optimization opportunities. -fno-semantic-interposition can

- in Clang: avoid GOT/PLT cost for variable access/function calls to external linkage definition in the same TU
- in GCC: enable interprocedural optimizations (including inlining) and avoid PLT

-Bsymbolic-functions can

- avoid PLT for cross-TU function calls && reduce dynamic symbol lookup
- reduce dynamic symbol lookup for taking function addresses and optimize out GOT/TOC on x86-64/ppc64

With both options, the libLLVM.so and libclang-cpp.so performance should
be closer to PIE binary linking against `libLLVM*.a` and `libclang*.a`

(In a -DLLVM_TARGETS_TO_BUILD=X86 build, the number of JUMP_SLOT decreases from 12716 to 1628, and the number of GLOB_DAT decreases from 1918 to 1313)

The two options should be safe since they bring the behavior closer to
Mach-O dylib (without `-flat_namespace` or `-U symbol` => no interposition)
and Windows dll.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102090

Files:
  clang/tools/clang-shlib/CMakeLists.txt
  llvm/cmake/modules/HandleLLVMOptions.cmake
  llvm/tools/llvm-shlib/CMakeLists.txt


Index: llvm/tools/llvm-shlib/CMakeLists.txt
===================================================================
--- llvm/tools/llvm-shlib/CMakeLists.txt
+++ llvm/tools/llvm-shlib/CMakeLists.txt
@@ -50,6 +50,8 @@
       # Solaris ld does not accept global: *; so there is no way to version *all* global symbols
       set(LIB_NAMES -Wl,--version-script,${LLVM_LIBRARY_DIR}/tools/llvm-shlib/simple_version_script.map ${LIB_NAMES})
     endif()
+    # Optimize function calls and global variable access for default visibility definitions to avoid GOT/PLT.
+    target_link_options(LLVM PRIVATE LINKER:-Bsymbolic-functions)
   elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
     set(LIB_NAMES -Wl,-all_load ${LIB_NAMES})
   endif()
Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -305,6 +305,7 @@
     # On Windows all code is PIC. MinGW warns if -fPIC is used.
   else()
     add_flag_or_print_warning("-fPIC" FPIC)
+    add_flag_if_supported("-fno-semantic-interposition" FNO_SEMANTIC_INTERPOSITION)
   endif()
   # GCC for MIPS can miscompile LLVM due to PR37701.
   if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND
Index: clang/tools/clang-shlib/CMakeLists.txt
===================================================================
--- clang/tools/clang-shlib/CMakeLists.txt
+++ clang/tools/clang-shlib/CMakeLists.txt
@@ -48,3 +48,5 @@
                   ${_OBJECTS}
                   LINK_LIBS
                   ${_DEPS})
+# Optimize function calls and global variable access for default visibility definitions to avoid GOT/PLT.
+target_link_options(clang-cpp PRIVATE LINKER:-Bsymbolic-functions)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102090.343744.patch
Type: text/x-patch
Size: 1786 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210507/b7411d5b/attachment.bin>


More information about the cfe-commits mailing list