Skip to content

Commit

Permalink
[#10852, #10880, #12312] Clang 13 and 14 support, use thirdparty buil…
Browse files Browse the repository at this point in the history
…t with LTO, fix a static initialization order issue

Summary:
LLVM 13 and LLVM 14 have been released and we need to update our code accordingly. [#10852]

Also allowing the use of third-party packages built with LTO. LTO type becomes part of the build directory name, replacing `dynamic` with `thin-lto` or `full-lto`. It can also be controlled using the YB_LINKING_TYPE environment variable. To build with LTO, specify e.g. `./yb_build.sh --lto full`. thirdparty_tool has been updated to understand the LTO-related part of third-party package tags.

Also found an ASAN issue, #10880, caused by indeterminate order of static initialization, which manifests itself on Clang 13 and Clang 14. Fixed by using plain-old arrays of enum values and adding an iterator wrapper on top of that.

Had to replace `atomic_flag` with `atomic<bool>` for the `outstanding_report_failure_task_` field (used to be `std::atomic_flag outstanding_report_failure_task_ = ATOMIC_FLAG_INIT;`) because ATOMIC_FLAG_INIT is deprecated in newer libc++ versions, but its absence silently produces incorrect behavior (initialization with a garbage value) in older versions of libc++, so it is better to avoid it altogether.

Additional third-party packages being added also include Amazon Linux 2. [#12312]

Updated mac_library_packager.py to avoid assuming that the additional libraries required by Postgres are in the `installed/common` directory. Some of them have moved to `installed/uninstrumented`.

Test Plan:
Jenkins

Build the code with Clang 13 and Clang 14 on an AlmaLinux 8 under x86_64 and aarch64.
Build types: debug, release, fastdebug, asan, tsan.

Reviewers: steve.varnau, sergei

Reviewed By: sergei

Subscribers: bogdan, ybase

Differential Revision: https://phabricator.dev.yugabyte.com/D14326
  • Loading branch information
mbautin committed Apr 29, 2022
1 parent b98b577 commit 33618fa
Show file tree
Hide file tree
Showing 37 changed files with 722 additions and 203 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
# Optimize for Graviton on Linux/aarch64 (not mac/arm64)
ADD_CXX_FLAGS("-march=armv8.2-a+fp16+rcpc+dotprod+crypto")
ADD_CXX_FLAGS("-mtune=neoverse-n1")
ADD_CXX_FLAGS("-mno-outline-atomics")
endif()

if (NOT "$ENV{YB_TARGET_ARCH}" STREQUAL "" AND
Expand Down
44 changes: 30 additions & 14 deletions build-support/common-build-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,16 @@ readonly -a VALID_COMPILER_TYPES=(
clang10
clang11
clang12
clang13
clang14
zapcc
)
make_regex_from_list VALID_COMPILER_TYPES "${VALID_COMPILER_TYPES[@]}"

readonly -a VALID_LINKING_TYPES=(
dynamic
static
thin-lto
full-lto
)
make_regex_from_list VALID_LINKING_TYPES "${VALID_LINKING_TYPES[@]}"

Expand Down Expand Up @@ -382,7 +385,7 @@ set_build_root() {
BUILD_ROOT+="-linuxbrew"
fi

BUILD_ROOT+="-dynamic"
BUILD_ROOT+="-${YB_LINKING_TYPE:-dynamic}"
if is_apple_silicon; then
# Append the target architecture (x86_64 or arm64).
BUILD_ROOT+=-${YB_TARGET_ARCH}
Expand Down Expand Up @@ -1964,7 +1967,6 @@ handle_predefined_build_root() {
local _dash_linuxbrew=${BASH_REMATCH[$group_idx]}

(( group_idx+=2 ))
# _linking_type is unused. We always use dynamic linking, but we plan to support static linking.
# shellcheck disable=SC2034
local _linking_type=${BASH_REMATCH[$group_idx]}

Expand Down Expand Up @@ -2002,6 +2004,17 @@ handle_predefined_build_root() {
"does not match YB_COMPILER_TYPE ('$YB_COMPILER_TYPE')."
fi

if [[ -z ${YB_LINKING_TYPE:-} ]]; then
export YB_LINKING_TYPE=$_linking_type
if ! "$handle_predefined_build_root_quietly"; then
log "Automatically setting linking type to '$YB_LINKING_TYPE' based on predefined build" \
"root ('$basename')"
fi
elif [[ $YB_LINKING_TYPE != "$_linking_type" ]]; then
fatal "Compiler type from the build root ('$_linking_type' from '$predefined_build_root') " \
"does not match YB_LINKING_TYPE ('$YB_LINKING_TYPE')."
fi

local use_linuxbrew
if [[ -z ${_dash_linuxbrew:-} ]]; then
use_linuxbrew=false
Expand Down Expand Up @@ -2438,18 +2451,21 @@ set_prebuilt_thirdparty_url() {
rm -f "$thirdparty_url_file_path"
fi
local is_linuxbrew_arg=""
local thirdparty_tool_cmd_line=(
"$YB_BUILD_SUPPORT_DIR/thirdparty_tool"
--save-thirdparty-url-to-file "$thirdparty_url_file_path"
--save-llvm-url-to-file "$llvm_url_file_path"
--compiler-type "$YB_COMPILER_TYPE"
)
if [[ -n ${YB_USE_LINUXBREW:-} ]]; then
if [[ $YB_USE_LINUXBREW == "1" ]]; then
is_linuxbrew_arg="--is-linuxbrew=true"
else
is_linuxbrew_arg="--is-linuxbrew=false"
fi
# See arg_str_to_bool in Python code for how the boolean parameter is interpreted.
thirdparty_tool_cmd_line+=( "--is-linuxbrew=$YB_USE_LINUXBREW" )
fi
if [[ ${YB_LINKING_TYPE:-dynamic} != "dynamic" ]]; then
# Transform "thin-lto" or "full-lto" into "thin" or "full" respectively.
thirdparty_tool_cmd_line+=( "--lto=${YB_LINKING_TYPE%%-lto}" )
fi
"$YB_BUILD_SUPPORT_DIR/thirdparty_tool" \
--save-thirdparty-url-to-file "$thirdparty_url_file_path" \
--save-llvm-url-to-file "$llvm_url_file_path" \
--compiler-type "$YB_COMPILER_TYPE" \
$is_linuxbrew_arg
"${thirdparty_tool_cmd_line[@]}"
YB_THIRDPARTY_URL=$(<"$BUILD_ROOT/thirdparty_url.txt")
export YB_THIRDPARTY_URL
yb_thirdparty_url_origin=" (determined automatically based on the OS and compiler type)"
Expand Down Expand Up @@ -2582,7 +2598,7 @@ is_apple_silicon() {
}

should_use_lto() {
using_linuxbrew && [[ "${YB_COMPILER_TYPE}" == "clang12" && "${build_type}" == "release" ]]
using_linuxbrew && [[ "${YB_COMPILER_TYPE}" =~ "clang1[234]" && "${build_type}" == "release" ]]
}

# -------------------------------------------------------------------------------------------------
Expand Down
15 changes: 13 additions & 2 deletions build-support/jenkins/build-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ if [[ ${YB_DOWNLOAD_THIRDPARTY:-auto} == "auto" ]]; then
fi
log "YB_DOWNLOAD_THIRDPARTY=$YB_DOWNLOAD_THIRDPARTY"

if [[ -z ${YB_LINKING_TYPE:-} ]]; then
if should_use_lto; then
YB_LINKING_TYPE=full-lto
else
YB_LINKING_TYPE=dynamic
fi
export YB_LINKING_TYPE
fi
log "YB_LINKING_TYPE=${YB_LINKING_TYPE}"

# -------------------------------------------------------------------------------------------------
# Build root setup and build directory cleanup
# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -612,7 +622,7 @@ if [[ $YB_BUILD_JAVA == "1" && $YB_SKIP_BUILD != "1" ]]; then
fi

# It is important to do these LTO linking steps before building the package.
if should_use_lto; then
if [[ ${YB_LINKING_TYPE} == *-lto ]]; then
log "Using LTO. Replacing the yb-tserver binary with an LTO-enabled one."
log "See below for the file size and linked shared libraries."
(
Expand All @@ -621,12 +631,13 @@ if should_use_lto; then
--build-root "$BUILD_ROOT" \
--file-regex "^.*/yb-tserver$" \
--lto-output-suffix="" \
"--lto-type=${YB_LINKING_TYPE%-lto}" \
link-whole-program
ls -l "$BUILD_ROOT/bin/yb-tserver"
ldd "$BUILD_ROOT/bin/yb-tserver"
)
else
log "Not using LTO: YB_COMPILER_TYPE=${YB_COMPILER_TYPE}, build_type=${build_type}"
log "Not using LTO: YB_LINKING_TYPE=${YB_LINKING_TYPE}"
fi

# -------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion build-support/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cleanup() {
# shellcheck disable=SC2119
kill_stuck_processes

if [[ -n ${YB_TEST_INVOCATION_ID:-} ]]; then
if [[ -n ${YB_TEST_INVOCATION_ID:-} && "${YB_NO_TEST_INVOCATION_FLAG_FILE:-}" != "1" ]]; then
mkdir -p /tmp/yb_completed_tests
touch "$YB_COMPLETED_TEST_FLAG_DIR/$YB_TEST_INVOCATION_ID"
fi
Expand Down
Loading

0 comments on commit 33618fa

Please sign in to comment.