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" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 22 | "android/soong/cc/config" |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | // Add flags to ignore warnings that profiles are old or missing for |
| 27 | // some functions |
Pirama Arumuga Nainar | f4c0baf | 2017-09-28 14:35:15 -0700 | [diff] [blame] | 28 | profileUseOtherFlags = []string{"-Wno-backend-plugin"} |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | const pgoProfileProject = "toolchain/pgo-profiles" |
| 32 | |
| 33 | const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp" |
| 34 | const profileSamplingFlag = "-gline-tables-only" |
| 35 | const profileUseInstrumentFormat = "-fprofile-use=%s" |
| 36 | const profileUseSamplingFormat = "-fprofile-sample-use=%s" |
| 37 | |
| 38 | type PgoProperties struct { |
| 39 | Pgo struct { |
Pirama Arumuga Nainar | 6aeed8b | 2017-10-16 13:31:40 -0700 | [diff] [blame] | 40 | Instrumentation *bool |
| 41 | Sampling *bool |
| 42 | Profile_file *string `android:"arch_variant"` |
| 43 | Benchmarks []string |
| 44 | Enable_profile_use *bool `android:"arch_variant"` |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 45 | } `android:"arch_variant"` |
| 46 | |
| 47 | PgoPresent bool `blueprint:"mutated"` |
| 48 | ShouldProfileModule bool `blueprint:"mutated"` |
| 49 | } |
| 50 | |
| 51 | type pgo struct { |
| 52 | Properties PgoProperties |
| 53 | } |
| 54 | |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 55 | func (props *PgoProperties) isInstrumentation() bool { |
| 56 | return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true |
| 57 | } |
| 58 | |
| 59 | func (props *PgoProperties) isSampling() bool { |
| 60 | return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true |
| 61 | } |
| 62 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 63 | func (pgo *pgo) props() []interface{} { |
| 64 | return []interface{}{&pgo.Properties} |
| 65 | } |
| 66 | |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 67 | func (props *PgoProperties) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags { |
| 68 | if props.isInstrumentation() { |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 69 | flags.CFlags = append(flags.CFlags, profileInstrumentFlag) |
| 70 | // The profile runtime is added below in deps(). Add the below |
| 71 | // flag, which is the only other link-time action performed by |
| 72 | // the Clang driver during link. |
| 73 | flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime") |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 74 | } |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 75 | if props.isSampling() { |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 76 | flags.CFlags = append(flags.CFlags, profileSamplingFlag) |
| 77 | flags.LdFlags = append(flags.LdFlags, profileSamplingFlag) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 78 | } |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 79 | return flags |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 82 | func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string { |
| 83 | if props.isInstrumentation() { |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 84 | return fmt.Sprintf(profileUseInstrumentFormat, file) |
| 85 | } |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 86 | if props.isSampling() { |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 87 | return fmt.Sprintf(profileUseSamplingFormat, file) |
| 88 | } |
| 89 | return "" |
| 90 | } |
| 91 | |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 92 | func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string { |
| 93 | flags := []string{props.profileUseFlag(ctx, file)} |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 94 | flags = append(flags, profileUseOtherFlags...) |
| 95 | return flags |
| 96 | } |
| 97 | |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 98 | func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags { |
Pirama Arumuga Nainar | 6aeed8b | 2017-10-16 13:31:40 -0700 | [diff] [blame] | 99 | // Skip -fprofile-use if 'enable_profile_use' property is set |
| 100 | if props.Pgo.Enable_profile_use != nil && *props.Pgo.Enable_profile_use == false { |
| 101 | return flags |
| 102 | } |
| 103 | |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 104 | // If the PGO profiles project is found, and this module has PGO |
| 105 | // enabled, add flags to use the profile |
| 106 | if profilesDir := getPgoProfilesDir(ctx); props.PgoPresent && profilesDir.Valid() { |
| 107 | profileFile := android.PathForSource(ctx, profilesDir.String(), *props.Pgo.Profile_file) |
| 108 | profileUseFlags := props.profileUseFlags(ctx, profileFile.String()) |
| 109 | |
| 110 | flags.CFlags = append(flags.CFlags, profileUseFlags...) |
| 111 | flags.LdFlags = append(flags.LdFlags, profileUseFlags...) |
| 112 | |
| 113 | // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt |
| 114 | // if profileFile gets updated |
| 115 | flags.CFlagsDeps = append(flags.CFlagsDeps, profileFile) |
| 116 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFile) |
| 117 | } |
| 118 | return flags |
| 119 | } |
| 120 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 121 | func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool { |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 122 | isInstrumentation := props.isInstrumentation() |
| 123 | isSampling := props.isSampling() |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 124 | |
| 125 | profileKindPresent := isInstrumentation || isSampling |
| 126 | filePresent := props.Pgo.Profile_file != nil |
| 127 | benchmarksPresent := len(props.Pgo.Benchmarks) > 0 |
| 128 | |
| 129 | // If all three properties are absent, PGO is OFF for this module |
| 130 | if !profileKindPresent && !filePresent && !benchmarksPresent { |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | // If at least one property exists, validate that all properties exist |
| 135 | if !profileKindPresent || !filePresent || !benchmarksPresent { |
| 136 | var missing []string |
| 137 | if !profileKindPresent { |
| 138 | missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)") |
| 139 | } |
| 140 | if !filePresent { |
| 141 | missing = append(missing, "profile_file property") |
| 142 | } |
| 143 | if !benchmarksPresent { |
| 144 | missing = append(missing, "non-empty benchmarks property") |
| 145 | } |
| 146 | missingProps := strings.Join(missing, ", ") |
| 147 | ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps) |
| 148 | } |
| 149 | |
| 150 | // Sampling not supported yet |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 151 | if isSampling { |
| 152 | ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)") |
| 153 | } |
| 154 | |
Pirama Arumuga Nainar | 6fc8d91 | 2017-10-05 10:25:00 -0700 | [diff] [blame] | 155 | if isSampling && isInstrumentation { |
| 156 | ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set") |
| 157 | } |
| 158 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 159 | return true |
| 160 | } |
| 161 | |
| 162 | func getPgoProfilesDir(ctx ModuleContext) android.OptionalPath { |
| 163 | return android.ExistentPathForSource(ctx, "", pgoProfileProject) |
| 164 | } |
| 165 | |
| 166 | func (pgo *pgo) begin(ctx BaseModuleContext) { |
| 167 | // TODO Evaluate if we need to support PGO for host modules |
| 168 | if ctx.Host() { |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | // Check if PGO is needed for this module |
| 173 | pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx) |
| 174 | |
| 175 | if !pgo.Properties.PgoPresent { |
| 176 | return |
| 177 | } |
| 178 | |
| 179 | // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set |
| 180 | // and includes a benchmark listed for this module |
| 181 | // |
| 182 | // TODO Validate that each benchmark instruments at least one module |
| 183 | pgo.Properties.ShouldProfileModule = false |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame^] | 184 | pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT") |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 185 | pgoBenchmarksMap := make(map[string]bool) |
| 186 | for _, b := range strings.Split(pgoBenchmarks, ",") { |
| 187 | pgoBenchmarksMap[b] = true |
| 188 | } |
| 189 | |
| 190 | for _, b := range pgo.Properties.Pgo.Benchmarks { |
| 191 | if pgoBenchmarksMap[b] == true { |
| 192 | pgo.Properties.ShouldProfileModule = true |
| 193 | break |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Pirama Arumuga Nainar | 49b53d5 | 2017-10-04 16:47:29 -0700 | [diff] [blame] | 198 | func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 199 | if pgo.Properties.ShouldProfileModule { |
| 200 | runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain()) |
| 201 | deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary) |
| 202 | } |
| 203 | return deps |
| 204 | } |
| 205 | |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 206 | func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags { |
| 207 | if ctx.Host() { |
| 208 | return flags |
| 209 | } |
| 210 | |
| 211 | props := pgo.Properties |
| 212 | |
| 213 | // Add flags to profile this module based on its profile_kind |
| 214 | if props.ShouldProfileModule { |
Pirama Arumuga Nainar | 3f5bb9c | 2017-10-10 10:47:41 -0700 | [diff] [blame] | 215 | return props.addProfileGatherFlags(ctx, flags) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame^] | 218 | if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") { |
Pirama Arumuga Nainar | 0fdfc45 | 2017-10-10 11:00:18 -0700 | [diff] [blame] | 219 | return props.addProfileUseFlags(ctx, flags) |
Pirama Arumuga Nainar | ada83ec | 2017-08-31 23:38:27 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return flags |
| 223 | } |