xref: /aosp_15_r20/external/cronet/build/config/compiler/pgo/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import("//build/config/chromeos/ui_mode.gni")
6import("//build/config/clang/clang.gni")
7import("//build/config/compiler/compiler.gni")
8import("//build/config/compiler/pgo/pgo.gni")
9import("//build/config/features.gni")
10import("//build/toolchain/toolchain.gni")
11
12# Configuration that enables PGO instrumentation.
13config("pgo_instrumentation_flags") {
14  visibility = [ ":default_pgo_flags" ]
15
16  # Only add flags when chrome_pgo_phase == 1, so that variables we would use
17  # are not required to be defined when we're not actually using PGO.
18  if (chrome_pgo_phase == 1 && is_clang && !is_nacl && is_a_target_toolchain) {
19    cflags = [ "-fprofile-generate" ]
20    if (!is_win) {
21      # Windows directly calls link.exe instead of the compiler driver when
22      # linking, and embeds the path to the profile runtime library as
23      # dependent library into each object file.
24      ldflags = [ "-fprofile-generate" ]
25    }
26  }
27}
28
29# Configuration that enables optimization using profile data.
30config("pgo_optimization_flags") {
31  visibility = [ ":default_pgo_flags" ]
32
33  # Only add flags when chrome_pgo_phase == 2, so that variables we would use
34  # are not required to be defined when we're not actually using PGO.
35  if (chrome_pgo_phase == 2 && is_clang && !is_nacl && is_a_target_toolchain) {
36    _pgo_target = ""
37
38    # There are txt files used by //tools/update_pgo_profiles.py to decide which
39    # profiles to use, adding them as inputs so that analyzer recognizes the
40    # dependencies.
41    inputs = []
42
43    if (is_win) {
44      if (target_cpu == "arm64") {
45        _pgo_target = "win-arm64"
46      } else if (target_cpu == "x64") {
47        _pgo_target = "win64"
48      } else {
49        _pgo_target = "win32"
50      }
51    } else if (is_mac) {
52      if (target_cpu == "arm64") {
53        _pgo_target = "mac-arm"
54      } else {
55        _pgo_target = "mac"
56      }
57    } else if (is_linux) {
58      _pgo_target = "linux"
59    } else if (is_chromeos_lacros) {
60      if (target_cpu == "arm") {
61        # We don't have the arm device to train arm pgo data. So Lacros arm
62        # would use arm64 profile.
63        _pgo_target = "lacros-arm64"
64      } else if (target_cpu == "arm64") {
65        _pgo_target = "lacros-arm64"
66      } else {
67        _pgo_target = "lacros64"
68      }
69    } else if (is_android) {
70      # Use |current_cpu| and not |target_cpu|; for Android we may built both.
71      if (current_cpu == "arm64") {
72        _pgo_target = "android-arm64"
73      } else {
74        _pgo_target = "android-arm32"
75      }
76    } else if (is_fuchsia) {
77      if (target_cpu == "arm64") {
78        _pgo_target = "mac-arm"
79      } else {
80        _pgo_target = "mac"
81      }
82    } else if (is_ios && use_blink) {
83      if (target_cpu == "arm64") {
84        _pgo_target = "mac-arm"
85      } else {
86        _pgo_target = "mac"
87      }
88    }
89
90    if (_pgo_target == "win-arm64") {
91      inputs = [ "//chrome/build/win-arm64.pgo.txt" ]
92    } else if (_pgo_target == "win64") {
93      inputs = [ "//chrome/build/win64.pgo.txt" ]
94    } else if (_pgo_target == "win32") {
95      inputs = [ "//chrome/build/win32.pgo.txt" ]
96    } else if (_pgo_target == "mac-arm") {
97      inputs = [ "//chrome/build/mac-arm.pgo.txt" ]
98    } else if (_pgo_target == "mac") {
99      inputs = [ "//chrome/build/mac.pgo.txt" ]
100    } else if (_pgo_target == "linux") {
101      inputs = [ "//chrome/build/linux.pgo.txt" ]
102    } else if (_pgo_target == "lacros64") {
103      inputs = [ "//chrome/build/lacros64.pgo.txt" ]
104    } else if (_pgo_target == "lacros-arm") {
105      inputs = [ "//chrome/build/lacros-arm.pgo.txt" ]
106    } else if (_pgo_target == "lacros-arm64") {
107      inputs = [ "//chrome/build/lacros-arm64.pgo.txt" ]
108    } else if (_pgo_target == "android-arm32") {
109      inputs = [ "//chrome/build/android-arm32.pgo.txt" ]
110    } else if (_pgo_target == "android-arm64") {
111      inputs = [ "//chrome/build/android-arm64.pgo.txt" ]
112    }
113
114    if (_pgo_target != "" && pgo_data_path == "") {
115      pgo_data_path = exec_script("//tools/update_pgo_profiles.py",
116                                  [
117                                    "--target",
118                                    _pgo_target,
119                                    "get_profile_path",
120                                  ],
121                                  "value")
122    }
123    assert(pgo_data_path != "",
124           "Please set pgo_data_path to point at the profile data")
125    cflags = [
126      "-fprofile-use=" + rebase_path(pgo_data_path, root_build_dir),
127
128      # It's possible to have some profile data legitimately missing,
129      # and at least some profile data always ends up being considered
130      # out of date, so make sure we don't error for those cases.
131      "-Wno-profile-instr-unprofiled",
132      "-Wno-profile-instr-out-of-date",
133
134      # Some hashing conflict results in a lot of warning like this when doing
135      # a PGO build:
136      #   warning: foo.cc: Function control flow change detected (hash mismatch)
137      #   [-Wbackend-plugin]
138      # See https://crbug.com/978401
139      "-Wno-backend-plugin",
140    ]
141
142    # Enable basic block layout based on the extended TSP problem. This aims to
143    # improve icache utilization and reduce the binary size.
144    if (use_thin_lto) {
145      if (is_win) {
146        ldflags = [ "-mllvm:-enable-ext-tsp-block-placement=1" ]
147      } else {
148        ldflags = [ "-Wl,-mllvm,-enable-ext-tsp-block-placement=1" ]
149      }
150    } else {
151      cflags += [
152        "-mllvm",
153        "-enable-ext-tsp-block-placement=1",
154      ]
155    }
156  }
157}
158
159# Applies flags necessary when profile-guided optimization is used.
160# Flags are only added if PGO is enabled, so that this config is safe to
161# include by default.
162config("default_pgo_flags") {
163  if (chrome_pgo_phase == 0) {
164    # Nothing. This config should be a no-op when chrome_pgo_phase == 0.
165  } else if (chrome_pgo_phase == 1) {
166    configs = [ ":pgo_instrumentation_flags" ]
167  } else if (chrome_pgo_phase == 2) {
168    configs = [ ":pgo_optimization_flags" ]
169  }
170}
171