diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/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