From acf69ddd0fd1069fc39db86a8fee3644c5747c5a Mon Sep 17 00:00:00 2001 From: Alexander Frick Date: Wed, 17 Apr 2024 05:38:20 -0500 Subject: [PATCH] PGO optimizations --- arm/build/config/compiler/BUILD.gn | 2 +- other/AVX2/build/config/compiler/BUILD.gn | 2 +- other/CrOS/build/config/compiler/BUILD.gn | 2 +- other/SSE2/build/config/compiler/BUILD.gn | 2 +- other/SSE3/build/config/compiler/BUILD.gn | 2 +- other/SSE4.1/build/config/compiler/BUILD.gn | 2 +- src/build/config/compiler/BUILD.gn | 2 +- src/build/config/compiler/pgo/BUILD.gn | 183 ++++++++++++++++++++ 8 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 src/build/config/compiler/pgo/BUILD.gn diff --git a/arm/build/config/compiler/BUILD.gn b/arm/build/config/compiler/BUILD.gn index 419abdc6f..a6df91161 100644 --- a/arm/build/config/compiler/BUILD.gn +++ b/arm/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/other/AVX2/build/config/compiler/BUILD.gn b/other/AVX2/build/config/compiler/BUILD.gn index ee3499227..14c296b8c 100644 --- a/other/AVX2/build/config/compiler/BUILD.gn +++ b/other/AVX2/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/other/CrOS/build/config/compiler/BUILD.gn b/other/CrOS/build/config/compiler/BUILD.gn index 3dd79f2fd..3c4cc193a 100644 --- a/other/CrOS/build/config/compiler/BUILD.gn +++ b/other/CrOS/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/other/SSE2/build/config/compiler/BUILD.gn b/other/SSE2/build/config/compiler/BUILD.gn index 587e96e06..cc10f81f8 100644 --- a/other/SSE2/build/config/compiler/BUILD.gn +++ b/other/SSE2/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/other/SSE3/build/config/compiler/BUILD.gn b/other/SSE3/build/config/compiler/BUILD.gn index 0440db72f..ebcfcfd3c 100644 --- a/other/SSE3/build/config/compiler/BUILD.gn +++ b/other/SSE3/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/other/SSE4.1/build/config/compiler/BUILD.gn b/other/SSE4.1/build/config/compiler/BUILD.gn index 6a4aed193..7a027693d 100644 --- a/other/SSE4.1/build/config/compiler/BUILD.gn +++ b/other/SSE4.1/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/src/build/config/compiler/BUILD.gn b/src/build/config/compiler/BUILD.gn index c9fc742c5..c32f26a16 100644 --- a/src/build/config/compiler/BUILD.gn +++ b/src/build/config/compiler/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright 2024 The Chromium Authors and Alex313031 +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. diff --git a/src/build/config/compiler/pgo/BUILD.gn b/src/build/config/compiler/pgo/BUILD.gn new file mode 100644 index 000000000..b8e2c0901 --- /dev/null +++ b/src/build/config/compiler/pgo/BUILD.gn @@ -0,0 +1,183 @@ +# Copyright 2024 The Chromium Authors, RobRich999, and Alex313031 +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//build/config/chromeos/ui_mode.gni") +import("//build/config/clang/clang.gni") +import("//build/config/compiler/compiler.gni") +import("//build/config/compiler/pgo/pgo.gni") +import("//build/config/features.gni") +import("//build/toolchain/toolchain.gni") + +# Configuration that enables PGO instrumentation. +config("pgo_instrumentation_flags") { + visibility = [ ":default_pgo_flags" ] + + # Only add flags when chrome_pgo_phase == 1, so that variables we would use + # are not required to be defined when we're not actually using PGO. + if (chrome_pgo_phase == 1 && is_clang && !is_nacl && is_a_target_toolchain) { + cflags = [ "-fprofile-generate" ] + if (!is_win) { + # Windows directly calls link.exe instead of the compiler driver when + # linking, and embeds the path to the profile runtime library as + # dependent library into each object file. + ldflags = [ "-fprofile-generate" ] + } + } +} + +# Configuration that enables optimization using profile data. +config("pgo_optimization_flags") { + visibility = [ ":default_pgo_flags" ] + + # Only add flags when chrome_pgo_phase == 2, so that variables we would use + # are not required to be defined when we're not actually using PGO. + if (chrome_pgo_phase == 2 && is_clang && !is_nacl && is_a_target_toolchain) { + _pgo_target = "" + + # There are txt files used by //tools/update_pgo_profiles.py to decide which + # profiles to use, adding them as inputs so that analyzer recognizes the + # dependencies. + inputs = [] + + if (is_win) { + if (target_cpu == "arm64") { + _pgo_target = "win-arm64" + } else if (target_cpu == "x64") { + _pgo_target = "win64" + } else { + _pgo_target = "win32" + } + } else if (is_mac) { + if (target_cpu == "arm64") { + _pgo_target = "mac-arm" + } else { + _pgo_target = "mac" + } + } else if (is_linux) { + _pgo_target = "linux" + } else if (is_chromeos_lacros) { + if (target_cpu == "arm") { + # We don't have the arm device to train arm pgo data. So Lacros arm + # would use arm64 profile. + _pgo_target = "lacros-arm64" + } else if (target_cpu == "arm64") { + _pgo_target = "lacros-arm64" + } else { + _pgo_target = "lacros64" + } + } else if (is_android) { + # Use |current_cpu| and not |target_cpu|; for Android we may built both. + if (current_cpu == "arm64") { + _pgo_target = "android-arm64" + } else { + _pgo_target = "android-arm32" + } + } else if (is_fuchsia) { + if (target_cpu == "arm64") { + _pgo_target = "mac-arm" + } else { + _pgo_target = "mac" + } + } else if (is_ios && use_blink) { + if (target_cpu == "arm64") { + _pgo_target = "mac-arm" + } else { + _pgo_target = "mac" + } + } + + if (_pgo_target == "win-arm64") { + inputs = [ "//chrome/build/win-arm64.pgo.txt" ] + } else if (_pgo_target == "win64") { + inputs = [ "//chrome/build/win64.pgo.txt" ] + } else if (_pgo_target == "win32") { + inputs = [ "//chrome/build/win32.pgo.txt" ] + } else if (_pgo_target == "mac-arm") { + inputs = [ "//chrome/build/mac-arm.pgo.txt" ] + } else if (_pgo_target == "mac") { + inputs = [ "//chrome/build/mac.pgo.txt" ] + } else if (_pgo_target == "linux") { + inputs = [ "//chrome/build/linux.pgo.txt" ] + } else if (_pgo_target == "lacros64") { + inputs = [ "//chrome/build/lacros64.pgo.txt" ] + } else if (_pgo_target == "lacros-arm") { + inputs = [ "//chrome/build/lacros-arm.pgo.txt" ] + } else if (_pgo_target == "lacros-arm64") { + inputs = [ "//chrome/build/lacros-arm64.pgo.txt" ] + } else if (_pgo_target == "android-arm32") { + inputs = [ "//chrome/build/android-arm32.pgo.txt" ] + } else if (_pgo_target == "android-arm64") { + inputs = [ "//chrome/build/android-arm64.pgo.txt" ] + } + + if (_pgo_target != "" && pgo_data_path == "") { + pgo_data_path = exec_script("//tools/update_pgo_profiles.py", + [ + "--target", + _pgo_target, + "get_profile_path", + ], + "value") + } + assert(pgo_data_path != "", + "Please set pgo_data_path to point at the profile data") + cflags = [ + "-fprofile-use=" + rebase_path(pgo_data_path, root_build_dir), + + # It's possible to have some profile data legitimately missing, + # and at least some profile data always ends up being considered + # out of date, so make sure we don't error for those cases. + "-Wno-profile-instr-unprofiled", + "-Wno-profile-instr-out-of-date", + + # Some hashing conflict results in a lot of warning like this when doing + # a PGO build: + # warning: foo.cc: Function control flow change detected (hash mismatch) + # [-Wbackend-plugin] + # See https://crbug.com/978401 + "-Wno-backend-plugin", + ] + + # Enable basic block layout based on the extended TSP problem. This aims to + # improve icache utilization and reduce the binary size. + if (use_thin_lto) { + if (is_win) { + ldflags = [ + "-mllvm:-enable-ext-tsp-block-placement" + "-mllvm:-enable-split-machine-functions" + ] + } else { + ldflags = [ + "-Wl,-mllvm,-enable-ext-tsp-block-placement", + "-Wl,-mllvm,-enable-split-machine-functions", + ] + } + } else { + if (is_win) { + cflags += [ + "-mllvm", "-enable-ext-tsp-block-placement", + "-fsplit-machine-functions", + ] + } else { + cflags += [ + "-mllvm", "-enable-ext-tsp-block-placement", + "-fsplit-machine-functions", + ] + } + } + } +} + +# Applies flags necessary when profile-guided optimization is used. +# Flags are only added if PGO is enabled, so that this config is safe to +# include by default. +config("default_pgo_flags") { + if (chrome_pgo_phase == 0) { + # Nothing. This config should be a no-op when chrome_pgo_phase == 0. + } else if (chrome_pgo_phase == 1) { + configs = [ ":pgo_instrumentation_flags" ] + } else if (chrome_pgo_phase == 2) { + configs = [ ":pgo_optimization_flags" ] + } +}