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