Use ld.gold by default to link in CI (#57061)

Summary:
This adds an option to CMake to use `ld.gold` to link rather than `ld` (which symlinks to `ld.bfd` on Ubuntu by default). This shouldn't change any functionality, only a mild improvement on link times during builds (shaves off 1 minute) on CI.

Verify by searching for `ld.gold is available` in [the logs](https://circleci.com/api/v1.1/project/github/pytorch/pytorch/13046834/output/105/0?file=true&allocation-id=608c434338107e5b6cf938a1-0-build%2F7BDA2FF1)
](https://our.intern.facebook.com/intern/diff/28123522/)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/57061

Pulled By: driazati

Reviewed By: janeyx99

Differential Revision: D28123522

fbshipit-source-id: 5a60798ca4785427fd92bbf3b3aa5f63730e9b20
This commit is contained in:
davidriazati@fb.com 2021-05-03 10:03:07 -07:00 committed by Facebook GitHub Bot
parent c0d39ba680
commit 264d87985a
3 changed files with 29 additions and 0 deletions

View file

@ -168,6 +168,8 @@ export CIRCLE_SHA1="$CIRCLE_SHA1"
export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-}"
export CIRCLE_BRANCH="$CIRCLE_BRANCH"
export CIRCLE_WORKFLOW_ID="$CIRCLE_WORKFLOW_ID"
export USE_GOLD_LINKER=1
# =================== The above code will be executed inside Docker container ===================
EOL

View file

@ -343,6 +343,7 @@ option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF)
option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF)
option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF)
option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF)
option(USE_GOLD_LINKER "Use ld.gold to link" OFF)
if(USE_SYSTEM_LIBS)
set(USE_SYSTEM_CPUINFO ON)
set(USE_SYSTEM_SLEEF ON)
@ -684,6 +685,31 @@ if(NOT MSVC)
# Suppress "The ABI for passing parameters with 64-byte alignment has changed in GCC 4.6"
string(APPEND CMAKE_CXX_FLAGS " -Wno-psabi")
endif()
# Use ld.gold if available, fall back to ld.bfd (the default ld) if not
if(USE_GOLD_LINKER)
if(USE_DISTRIBUTED AND USE_MPI)
# Same issue as here with default MPI on Ubuntu
# https://bugs.launchpad.net/ubuntu/+source/deal.ii/+bug/1841577
message(WARNING "Refusing to use gold when USE_MPI=1")
else()
execute_process(
COMMAND
"${CMAKE_C_COMPILER}" -fuse-ld=gold -Wl,--version
ERROR_QUIET
OUTPUT_VARIABLE LD_VERSION)
if(NOT "${LD_VERSION}" MATCHES "GNU gold")
message(WARNING "USE_GOLD_LINKER was set but ld.gold isn't available, turning it off")
set(USE_GOLD_LINKER OFF)
else()
message(STATUS "ld.gold is available, using it to link")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -fuse-ld=gold")
endif()
endif()
endif()
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=pedantic")
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=redundant-decls")
string(APPEND CMAKE_CXX_FLAGS " -Wno-error=old-style-cast")

View file

@ -19,6 +19,7 @@ function(caffe2_print_configuration_summary)
message(STATUS " Compile definitions : ${tmp}")
message(STATUS " CMAKE_PREFIX_PATH : ${CMAKE_PREFIX_PATH}")
message(STATUS " CMAKE_INSTALL_PREFIX : ${CMAKE_INSTALL_PREFIX}")
message(STATUS " USE_GOLD_LINKER : ${USE_GOLD_LINKER}")
message(STATUS "")
message(STATUS " TORCH_VERSION : ${TORCH_VERSION}")