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