Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 19 | "path/filepath" |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
Yi Kong | ca610d2 | 2018-04-24 10:42:02 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | // Add flags to ignore warnings that profiles are old or missing for |
Pirama Arumuga Nainar | 3a25405 | 2019-06-14 09:54:23 -0700 | [diff] [blame] | 29 | // some functions. |
Yi Kong | 69c1ed9 | 2019-03-21 14:28:13 -0700 | [diff] [blame] | 30 | profileUseOtherFlags = []string{ |
| 31 | "-Wno-backend-plugin", |
Yi Kong | 69c1ed9 | 2019-03-21 14:28:13 -0700 | [diff] [blame] | 32 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 33 | |
Pirama Arumuga Nainar | 4954080 | 2018-01-29 23:11:42 -0800 | [diff] [blame] | 34 | globalPgoProfileProjects = []string{ |
Yi Kong | 88b94ea | 2022-04-07 23:54:28 +0800 | [diff] [blame] | 35 | "toolchain/pgo-profiles/pgo", |
| 36 | "vendor/google_data/pgo_profile/pgo", |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 37 | } |
| 38 | ) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 39 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 40 | var pgoProfileProjectsConfigKey = android.NewOnceKey("PgoProfileProjects") |
| 41 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 42 | const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp" |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 43 | const profileUseInstrumentFormat = "-fprofile-use=%s" |
Yi Kong | b6ec66a | 2020-01-31 12:36:12 +0800 | [diff] [blame] | 44 | const profileUseSamplingFormat = "-fprofile-sample-accurate -fprofile-sample-use=%s" |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 45 | |
Pirama Arumuga Nainar | 4954080 | 2018-01-29 23:11:42 -0800 | [diff] [blame] | 46 | func getPgoProfileProjects(config android.DeviceConfig) []string { |
| 47 | return config.OnceStringSlice(pgoProfileProjectsConfigKey, func() []string { |
| 48 | return append(globalPgoProfileProjects, config.PgoAdditionalProfileDirs()...) |
| 49 | }) |
| 50 | } |
| 51 | |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 52 | func recordMissingProfileFile(ctx BaseModuleContext, missing string) { |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 53 | getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true) |
Pirama Arumuga Nainar | 28316d4 | 2018-01-29 09:18:45 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 56 | type PgoProperties struct { |
| 57 | Pgo struct { |
Pirama Arumuga Nainar | 6aeed8b | 2017-10-16 13:31:40 -0700 | [diff] [blame] | 58 | Instrumentation *bool |
Pirama Arumuga Nainar | 8195cdf | 2021-06-16 14:31:19 -0700 | [diff] [blame] | 59 | Sampling *bool `android:"arch_variant"` |
Pirama Arumuga Nainar | 6aeed8b | 2017-10-16 13:31:40 -0700 | [diff] [blame] | 60 | Profile_file *string `android:"arch_variant"` |
| 61 | Benchmarks []string |
| 62 | Enable_profile_use *bool `android:"arch_variant"` |
Pirama Arumuga Nainar | 690ed55 | 2017-12-13 16:48:20 -0800 | [diff] [blame] | 63 | // Additional compiler flags to use when building this module |
| 64 | // for profiling (either instrumentation or sampling). |
| 65 | Cflags []string `android:"arch_variant"` |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 66 | } `android:"arch_variant"` |
| 67 | |
| 68 | PgoPresent bool `blueprint:"mutated"` |
| 69 | ShouldProfileModule bool `blueprint:"mutated"` |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 70 | PgoCompile bool `blueprint:"mutated"` |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 71 | PgoInstrLink bool `blueprint:"mutated"` |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | type pgo struct { |
| 75 | Properties PgoProperties |
| 76 | } |
| 77 | |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 78 | func (props *PgoProperties) isInstrumentation() bool { |
| 79 | return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true |
| 80 | } |
| 81 | |
| 82 | func (props *PgoProperties) isSampling() bool { |
| 83 | return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true |
| 84 | } |
| 85 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 86 | func (pgo *pgo) props() []interface{} { |
| 87 | return []interface{}{&pgo.Properties} |
| 88 | } |
| 89 | |
Yi Kong | ceb5b76 | 2020-03-20 15:22:27 +0800 | [diff] [blame] | 90 | func (props *PgoProperties) addInstrumentationProfileGatherFlags(ctx ModuleContext, flags Flags) Flags { |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 91 | // Add to C flags iff PGO is explicitly enabled for this module. |
| 92 | if props.ShouldProfileModule { |
| 93 | flags.Local.CFlags = append(flags.Local.CFlags, props.Pgo.Cflags...) |
| 94 | flags.Local.CFlags = append(flags.Local.CFlags, profileInstrumentFlag) |
| 95 | } |
| 96 | flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrumentFlag) |
Yi Kong | ceb5b76 | 2020-03-20 15:22:27 +0800 | [diff] [blame] | 97 | return flags |
| 98 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 99 | |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 100 | func (props *PgoProperties) getPgoProfileFile(ctx BaseModuleContext) android.OptionalPath { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 101 | profileFile := *props.Pgo.Profile_file |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 102 | |
Pirama Arumuga Nainar | 4954080 | 2018-01-29 23:11:42 -0800 | [diff] [blame] | 103 | // Test if the profile_file is present in any of the PGO profile projects |
| 104 | for _, profileProject := range getPgoProfileProjects(ctx.DeviceConfig()) { |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 105 | // Bug: http://b/74395273 If the profile_file is unavailable, |
| 106 | // use a versioned file named |
| 107 | // <profile_file>.<arbitrary-version> when available. This |
| 108 | // works around an issue where ccache serves stale cache |
| 109 | // entries when the profile file has changed. |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 110 | globPattern := filepath.Join(profileProject, profileFile+".*") |
| 111 | versionedProfiles, err := ctx.GlobWithDeps(globPattern, nil) |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 112 | if err != nil { |
| 113 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 114 | } |
| 115 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 116 | path := android.ExistentPathForSource(ctx, profileProject, profileFile) |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 117 | if path.Valid() { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 118 | if len(versionedProfiles) != 0 { |
| 119 | ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+filepath.Join(profileProject, profileFile)+", "+strings.Join(versionedProfiles, ", ")) |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 120 | } |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 121 | return path |
| 122 | } |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 123 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 124 | if len(versionedProfiles) > 1 { |
| 125 | ctx.PropertyErrorf("pgo.profile_file", "Profile_file has multiple versions: "+strings.Join(versionedProfiles, ", ")) |
| 126 | } else if len(versionedProfiles) == 1 { |
| 127 | return android.OptionalPathForPath(android.PathForSource(ctx, versionedProfiles[0])) |
Pirama Arumuga Nainar | 8aed42c | 2018-03-08 22:56:37 -0800 | [diff] [blame] | 128 | } |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Pirama Arumuga Nainar | 28316d4 | 2018-01-29 09:18:45 -0800 | [diff] [blame] | 131 | // Record that this module's profile file is absent |
| 132 | missing := *props.Pgo.Profile_file + ":" + ctx.ModuleDir() + "/Android.bp:" + ctx.ModuleName() |
| 133 | recordMissingProfileFile(ctx, missing) |
| 134 | |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 135 | return android.OptionalPathForPath(nil) |
| 136 | } |
| 137 | |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 138 | func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string { |
| 139 | if props.isInstrumentation() { |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 140 | return fmt.Sprintf(profileUseInstrumentFormat, file) |
| 141 | } |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 142 | if props.isSampling() { |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 143 | return fmt.Sprintf(profileUseSamplingFormat, file) |
| 144 | } |
| 145 | return "" |
| 146 | } |
| 147 | |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 148 | func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string { |
| 149 | flags := []string{props.profileUseFlag(ctx, file)} |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 150 | flags = append(flags, profileUseOtherFlags...) |
| 151 | return flags |
| 152 | } |
| 153 | |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 154 | func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags { |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 155 | // Return if 'pgo' property is not present in this module. |
| 156 | if !props.PgoPresent { |
| 157 | return flags |
| 158 | } |
| 159 | |
Yi Kong | ca610d2 | 2018-04-24 10:42:02 -0700 | [diff] [blame] | 160 | if props.PgoCompile { |
| 161 | profileFile := props.getPgoProfileFile(ctx) |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 162 | profileFilePath := profileFile.Path() |
| 163 | profileUseFlags := props.profileUseFlags(ctx, profileFilePath.String()) |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 164 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 165 | flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlags...) |
| 166 | flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlags...) |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 167 | |
| 168 | // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt |
| 169 | // if profileFile gets updated |
Pirama Arumuga Nainar | 64946fe | 2018-01-17 14:00:53 -0800 | [diff] [blame] | 170 | flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath) |
| 171 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath) |
Yi Kong | 92474e5 | 2020-01-16 17:04:38 -0800 | [diff] [blame] | 172 | |
| 173 | if props.isSampling() { |
| 174 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true") |
| 175 | } |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 176 | } |
| 177 | return flags |
| 178 | } |
| 179 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 180 | func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool { |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 181 | isInstrumentation := props.isInstrumentation() |
| 182 | isSampling := props.isSampling() |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 183 | |
| 184 | profileKindPresent := isInstrumentation || isSampling |
| 185 | filePresent := props.Pgo.Profile_file != nil |
| 186 | benchmarksPresent := len(props.Pgo.Benchmarks) > 0 |
| 187 | |
| 188 | // If all three properties are absent, PGO is OFF for this module |
| 189 | if !profileKindPresent && !filePresent && !benchmarksPresent { |
| 190 | return false |
| 191 | } |
| 192 | |
Yi Kong | 84803c5 | 2020-07-21 15:38:23 +0800 | [diff] [blame] | 193 | // profileKindPresent and filePresent are mandatory properties. |
| 194 | if !profileKindPresent || !filePresent { |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 195 | var missing []string |
| 196 | if !profileKindPresent { |
| 197 | missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)") |
| 198 | } |
| 199 | if !filePresent { |
| 200 | missing = append(missing, "profile_file property") |
| 201 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 202 | missingProps := strings.Join(missing, ", ") |
| 203 | ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps) |
| 204 | } |
| 205 | |
Yi Kong | 84803c5 | 2020-07-21 15:38:23 +0800 | [diff] [blame] | 206 | // Benchmark property is mandatory for instrumentation PGO. |
| 207 | if isInstrumentation && !benchmarksPresent { |
| 208 | ctx.ModuleErrorf("Instrumentation PGO specification is missing benchmark property") |
| 209 | } |
| 210 | |
Yi Kong | 470f76f | 2022-01-26 02:34:34 +0800 | [diff] [blame] | 211 | if isSampling { |
| 212 | ctx.ModuleErrorf("Sampling PGO is deprecated, use AFDO instead") |
| 213 | } |
| 214 | |
Pirama Arumuga Nainar | 6fc8d91 | 2017-10-05 10:25:00 -0700 | [diff] [blame] | 215 | if isSampling && isInstrumentation { |
| 216 | ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set") |
| 217 | } |
| 218 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 219 | return true |
| 220 | } |
| 221 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 222 | func (pgo *pgo) begin(ctx BaseModuleContext) { |
| 223 | // TODO Evaluate if we need to support PGO for host modules |
| 224 | if ctx.Host() { |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | // Check if PGO is needed for this module |
| 229 | pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx) |
| 230 | |
| 231 | if !pgo.Properties.PgoPresent { |
| 232 | return |
| 233 | } |
| 234 | |
| 235 | // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set |
Pirama Arumuga Nainar | e236b5a | 2018-01-22 19:10:19 -0800 | [diff] [blame] | 236 | // and includes 'all', 'ALL' or a benchmark listed for this module. |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 237 | // |
| 238 | // TODO Validate that each benchmark instruments at least one module |
| 239 | pgo.Properties.ShouldProfileModule = false |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 240 | pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT") |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 241 | pgoBenchmarksMap := make(map[string]bool) |
| 242 | for _, b := range strings.Split(pgoBenchmarks, ",") { |
| 243 | pgoBenchmarksMap[b] = true |
| 244 | } |
| 245 | |
Pirama Arumuga Nainar | e236b5a | 2018-01-22 19:10:19 -0800 | [diff] [blame] | 246 | if pgoBenchmarksMap["all"] == true || pgoBenchmarksMap["ALL"] == true { |
| 247 | pgo.Properties.ShouldProfileModule = true |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 248 | pgo.Properties.PgoInstrLink = pgo.Properties.isInstrumentation() |
Pirama Arumuga Nainar | e236b5a | 2018-01-22 19:10:19 -0800 | [diff] [blame] | 249 | } else { |
| 250 | for _, b := range pgo.Properties.Pgo.Benchmarks { |
| 251 | if pgoBenchmarksMap[b] == true { |
| 252 | pgo.Properties.ShouldProfileModule = true |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 253 | pgo.Properties.PgoInstrLink = pgo.Properties.isInstrumentation() |
Pirama Arumuga Nainar | e236b5a | 2018-01-22 19:10:19 -0800 | [diff] [blame] | 254 | break |
| 255 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 258 | |
Pirama Arumuga Nainar | 807d49b | 2020-02-12 13:57:37 -0800 | [diff] [blame] | 259 | // PGO profile use is not feasible for a Clang coverage build because |
| 260 | // -fprofile-use and -fprofile-instr-generate are incompatible. |
| 261 | if ctx.DeviceConfig().ClangCoverageEnabled() { |
| 262 | return |
| 263 | } |
| 264 | |
Yi Kong | ca610d2 | 2018-04-24 10:42:02 -0700 | [diff] [blame] | 265 | if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") && |
| 266 | proptools.BoolDefault(pgo.Properties.Pgo.Enable_profile_use, true) { |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 267 | if profileFile := pgo.Properties.getPgoProfileFile(ctx); profileFile.Valid() { |
| 268 | pgo.Properties.PgoCompile = true |
| 269 | } |
| 270 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags { |
| 274 | if ctx.Host() { |
| 275 | return flags |
| 276 | } |
| 277 | |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 278 | // Deduce PgoInstrLink property i.e. whether this module needs to be |
| 279 | // linked with profile-generation flags. Here, we're setting it if any |
| 280 | // dependency needs PGO instrumentation. It is initially set in |
| 281 | // begin() if PGO is directly enabled for this module. |
| 282 | if ctx.static() && !ctx.staticBinary() { |
| 283 | // For static libraries, check if any whole_static_libs are |
| 284 | // linked with profile generation |
| 285 | ctx.VisitDirectDeps(func(m android.Module) { |
| 286 | if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok { |
| 287 | if depTag.static() && depTag.wholeStatic { |
| 288 | if cc, ok := m.(*Module); ok { |
| 289 | if cc.pgo.Properties.PgoInstrLink { |
| 290 | pgo.Properties.PgoInstrLink = true |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | }) |
| 296 | } else { |
| 297 | // For executables and shared libraries, check all static dependencies. |
| 298 | ctx.VisitDirectDeps(func(m android.Module) { |
| 299 | if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok { |
| 300 | if depTag.static() { |
| 301 | if cc, ok := m.(*Module); ok { |
| 302 | if cc.pgo.Properties.PgoInstrLink { |
| 303 | pgo.Properties.PgoInstrLink = true |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | }) |
| 309 | } |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 310 | |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 311 | props := pgo.Properties |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 312 | // Add flags to profile this module based on its profile_kind |
Pirama Arumuga Nainar | 1150fd7 | 2020-09-21 22:04:25 -0700 | [diff] [blame] | 313 | if (props.ShouldProfileModule && props.isInstrumentation()) || props.PgoInstrLink { |
Yi Kong | a575ff3 | 2020-07-22 01:41:58 +0800 | [diff] [blame] | 314 | // Instrumentation PGO use and gather flags cannot coexist. |
Pirama Arumuga Nainar | fe1da75 | 2020-09-02 17:44:06 +0000 | [diff] [blame] | 315 | return props.addInstrumentationProfileGatherFlags(ctx, flags) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 318 | if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") { |
Pirama Arumuga Nainar | fe1da75 | 2020-09-02 17:44:06 +0000 | [diff] [blame] | 319 | flags = props.addProfileUseFlags(ctx, flags) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return flags |
| 323 | } |