[PATCH] D102453: [CMake][ELF] Add -fno-semantic-interposition for GCC

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 13 17:00:32 PDT 2021


MaskRay created this revision.
MaskRay added reviewers: tstellar, sylvestre.ledru, phosek, foutrelis, mgorny.
Herald added a subscriber: pengfei.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In a `-DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_LINK_LLVM_DYLIB=on -DCLANG_LINK_CLANG_DYLIB=on`
build, libLLVM-13git.so is 2% smaller and libclang-cpp.so is 1% smaller.
There may be some small performance improvement as well because GCC
-fPIC suppresses interprocedural optimizations for non-inline
definitions by default.

Note: we cannot add -fno-semantic-interposition for Clang.  Clang's
implementation additionally optimizes global variables, which is incompatible
with unfortunate ELF -fno-pic default: direct access relocations for external
data. If the executable has a -fno-pic object file referencing a global variable
declared in a public header, the direct access relocation will cause a copy
relocation. The executable and libLLVM.so/libclang-cpp.so will disagree on the
address.

GCC -fno-semantic-interposition doesn't optimize global variables so there is no
issue. (It probably makes sense to add another option
-fno-semantic-interposition-function to work with the unfortunate ELF -fno-pic
default.)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102453

Files:
  llvm/cmake/modules/HandleLLVMOptions.cmake


Index: llvm/cmake/modules/HandleLLVMOptions.cmake
===================================================================
--- llvm/cmake/modules/HandleLLVMOptions.cmake
+++ llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -305,6 +305,16 @@
     # On Windows all code is PIC. MinGW warns if -fPIC is used.
   else()
     add_flag_or_print_warning("-fPIC" FPIC)
+    # Enable interprocedural optimizations for non-inline functions which would
+    # otherwise be disabled due to GCC -fPIC's default.
+    #
+    # Note: Clang allows IPO for -fPIC so this optimization is less effective.
+    # On ELF targets, -fno-pic compiled user programs may cause copy relocations
+    # if they access global/static variables declared in LLVM's public headers.
+    # This is incompatible with Clang's extra optimization on global variables.
+    if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+      add_flag_if_supported("-fno-semantic-interposition" FNO_SEMANTIC_INTERPOSITION)
+    endif()
   endif()
   # GCC for MIPS can miscompile LLVM due to PR37701.
   if(CMAKE_COMPILER_IS_GNUCXX AND LLVM_NATIVE_ARCH STREQUAL "Mips" AND


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102453.345315.patch
Type: text/x-patch
Size: 1106 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210514/5703c45d/attachment-0001.bin>


More information about the llvm-commits mailing list