Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 1 | // Copyright 2021 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 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 21 | "android/soong/android" |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint" |
| 24 | "github.com/google/blueprint/proptools" |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 25 | ) |
| 26 | |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 27 | // TODO(b/267229066): Remove globalAfdoProfileProjects after implementing bp2build converter for fdo_profile |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 28 | var ( |
| 29 | globalAfdoProfileProjects = []string{ |
| 30 | "vendor/google_data/pgo_profile/sampling/", |
| 31 | "toolchain/pgo-profiles/sampling/", |
| 32 | } |
| 33 | ) |
| 34 | |
| 35 | var afdoProfileProjectsConfigKey = android.NewOnceKey("AfdoProfileProjects") |
| 36 | |
Vinh Tran | 056effb | 2023-06-27 14:03:25 +0000 | [diff] [blame] | 37 | // This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering |
| 38 | const afdoFlagsFormat = "-fprofile-sample-use=%s" |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 39 | |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 40 | func recordMissingAfdoProfileFile(ctx android.BaseModuleContext, missing string) { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 41 | getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true) |
| 42 | } |
| 43 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 44 | type afdoRdep struct { |
| 45 | VariationName *string |
| 46 | ProfilePath *string |
| 47 | } |
| 48 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 49 | type AfdoProperties struct { |
Alix | 40216ae | 2022-04-07 20:47:01 +0000 | [diff] [blame] | 50 | // Afdo allows developers self-service enroll for |
| 51 | // automatic feedback-directed optimization using profile data. |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 52 | Afdo bool |
| 53 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 54 | FdoProfilePath *string `blueprint:"mutated"` |
| 55 | |
| 56 | AfdoRDeps []afdoRdep `blueprint:"mutated"` |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type afdo struct { |
| 60 | Properties AfdoProperties |
| 61 | } |
| 62 | |
| 63 | func (afdo *afdo) props() []interface{} { |
| 64 | return []interface{}{&afdo.Properties} |
| 65 | } |
| 66 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 67 | // afdoEnabled returns true for binaries and shared libraries |
| 68 | // that set afdo prop to True and there is a profile available |
| 69 | func (afdo *afdo) afdoEnabled() bool { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 70 | return afdo != nil && afdo.Properties.Afdo |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 71 | } |
| 72 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 73 | func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 74 | if afdo.Properties.Afdo { |
| 75 | // We use `-funique-internal-linkage-names` to associate profiles to the right internal |
| 76 | // functions. This option should be used before generating a profile. Because a profile |
| 77 | // generated for a binary without unique names doesn't work well building a binary with |
| 78 | // unique names (they have different internal function names). |
| 79 | // To avoid a chicken-and-egg problem, we enable `-funique-internal-linkage-names` when |
| 80 | // `afdo=true`, whether a profile exists or not. |
| 81 | // The profile can take effect in three steps: |
| 82 | // 1. Add `afdo: true` in Android.bp, and build the binary. |
| 83 | // 2. Collect an AutoFDO profile for the binary. |
| 84 | // 3. Make the profile searchable by the build system. So it's used the next time the binary |
| 85 | // is built. |
| 86 | flags.Local.CFlags = append([]string{"-funique-internal-linkage-names"}, flags.Local.CFlags...) |
Yi Kong | bc2d02a | 2023-10-16 16:57:53 +0900 | [diff] [blame^] | 87 | // Flags for Flow Sensitive AutoFDO |
Yi Kong | b33ced0 | 2023-10-10 14:11:50 +0900 | [diff] [blame] | 88 | flags.Local.CFlags = append([]string{"-mllvm", "-enable-fs-discriminator=true"}, flags.Local.CFlags...) |
Yi Kong | bc2d02a | 2023-10-16 16:57:53 +0900 | [diff] [blame^] | 89 | // TODO(b/266595187): Remove the following feature once it is enabled in LLVM by default. |
| 90 | flags.Local.CFlags = append([]string{"-mllvm", "-improved-fs-discriminator=true"}, flags.Local.CFlags...) |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 91 | } |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 92 | if path := afdo.Properties.FdoProfilePath; path != nil { |
Yi Kong | a1961e7 | 2023-04-05 11:33:11 +0900 | [diff] [blame] | 93 | // The flags are prepended to allow overriding. |
Vinh Tran | 056effb | 2023-06-27 14:03:25 +0000 | [diff] [blame] | 94 | profileUseFlag := fmt.Sprintf(afdoFlagsFormat, *path) |
Yi Kong | a1961e7 | 2023-04-05 11:33:11 +0900 | [diff] [blame] | 95 | flags.Local.CFlags = append([]string{profileUseFlag}, flags.Local.CFlags...) |
| 96 | flags.Local.LdFlags = append([]string{profileUseFlag, "-Wl,-mllvm,-no-warn-sample-unused=true"}, flags.Local.LdFlags...) |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 97 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 98 | // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt |
| 99 | // if profileFile gets updated |
| 100 | pathForSrc := android.PathForSource(ctx, *path) |
| 101 | flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc) |
| 102 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc) |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return flags |
| 106 | } |
| 107 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 108 | func (afdo *afdo) addDep(ctx BaseModuleContext, actx android.BottomUpMutatorContext) { |
| 109 | if ctx.Host() { |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | if ctx.static() && !ctx.staticBinary() { |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | if c, ok := ctx.Module().(*Module); ok && c.Enabled() { |
| 118 | if fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName()); fdoProfileName != nil && err == nil { |
| 119 | actx.AddFarVariationDependencies( |
| 120 | []blueprint.Variation{ |
| 121 | {Mutator: "arch", Variation: actx.Target().ArchVariation()}, |
| 122 | {Mutator: "os", Variation: "android"}, |
| 123 | }, |
| 124 | FdoProfileTag, |
| 125 | []string{*fdoProfileName}..., |
| 126 | ) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // FdoProfileMutator reads the FdoProfileProvider from a direct dep with FdoProfileTag |
| 132 | // assigns FdoProfileInfo.Path to the FdoProfilePath mutated property |
Vinh Tran | cde1016 | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 133 | func (c *Module) fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 134 | if !c.Enabled() { |
| 135 | return |
| 136 | } |
| 137 | |
Yi Kong | 2dbe160 | 2023-07-27 14:04:05 +0900 | [diff] [blame] | 138 | if !c.afdo.afdoEnabled() { |
| 139 | return |
| 140 | } |
| 141 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 142 | ctx.VisitDirectDepsWithTag(FdoProfileTag, func(m android.Module) { |
| 143 | if ctx.OtherModuleHasProvider(m, FdoProfileProvider) { |
| 144 | info := ctx.OtherModuleProvider(m, FdoProfileProvider).(FdoProfileInfo) |
| 145 | c.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String()) |
| 146 | } |
| 147 | }) |
| 148 | } |
| 149 | |
| 150 | var _ FdoProfileMutatorInterface = (*Module)(nil) |
| 151 | |
| 152 | // Propagate afdo requirements down from binaries and shared libraries |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 153 | func afdoDepsMutator(mctx android.TopDownMutatorContext) { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 154 | if m, ok := mctx.Module().(*Module); ok && m.afdo.afdoEnabled() { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 155 | path := m.afdo.Properties.FdoProfilePath |
| 156 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 157 | tag := mctx.OtherModuleDependencyTag(dep) |
| 158 | libTag, isLibTag := tag.(libraryDependencyTag) |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 159 | |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 160 | // Do not recurse down non-static dependencies |
| 161 | if isLibTag { |
| 162 | if !libTag.static() { |
| 163 | return false |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 164 | } |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 165 | } else { |
| 166 | if tag != objDepTag && tag != reuseObjTag { |
| 167 | return false |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 168 | } |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 169 | } |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 170 | |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 171 | if dep, ok := dep.(*Module); ok { |
| 172 | dep.afdo.Properties.AfdoRDeps = append( |
| 173 | dep.afdo.Properties.AfdoRDeps, |
| 174 | afdoRdep{ |
| 175 | VariationName: proptools.StringPtr(encodeTarget(m.Name())), |
| 176 | ProfilePath: path, |
| 177 | }, |
| 178 | ) |
| 179 | } |
| 180 | |
| 181 | return true |
| 182 | }) |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | // Create afdo variants for modules that need them |
| 187 | func afdoMutator(mctx android.BottomUpMutatorContext) { |
| 188 | if m, ok := mctx.Module().(*Module); ok && m.afdo != nil { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 189 | if !m.static() && m.afdo.Properties.Afdo { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 190 | mctx.SetDependencyVariation(encodeTarget(m.Name())) |
| 191 | return |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | variationNames := []string{""} |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 195 | |
| 196 | variantNameToProfilePath := make(map[string]*string) |
| 197 | |
| 198 | for _, afdoRDep := range m.afdo.Properties.AfdoRDeps { |
| 199 | variantName := *afdoRDep.VariationName |
| 200 | // An rdep can be set twice in AfdoRDeps because there can be |
| 201 | // more than one path from an afdo-enabled module to |
| 202 | // a static dep such as |
| 203 | // afdo_enabled_foo -> static_bar ----> static_baz |
| 204 | // \ ^ |
| 205 | // ----------------------| |
| 206 | // We only need to create one variant per unique rdep |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 207 | if _, exists := variantNameToProfilePath[variantName]; !exists { |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 208 | variationNames = append(variationNames, variantName) |
| 209 | variantNameToProfilePath[variantName] = afdoRDep.ProfilePath |
| 210 | } |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 211 | } |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 212 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 213 | if len(variationNames) > 1 { |
| 214 | modules := mctx.CreateVariations(variationNames...) |
| 215 | for i, name := range variationNames { |
| 216 | if name == "" { |
| 217 | continue |
| 218 | } |
| 219 | variation := modules[i].(*Module) |
| 220 | variation.Properties.PreventInstall = true |
| 221 | variation.Properties.HideFromMake = true |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 222 | variation.afdo.Properties.Afdo = true |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 223 | variation.afdo.Properties.FdoProfilePath = variantNameToProfilePath[name] |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Encode target name to variation name. |
| 230 | func encodeTarget(target string) string { |
| 231 | if target == "" { |
| 232 | return "" |
| 233 | } |
| 234 | return "afdo-" + target |
| 235 | } |
| 236 | |
| 237 | // Decode target name from variation name. |
| 238 | func decodeTarget(variation string) string { |
| 239 | if variation == "" { |
| 240 | return "" |
| 241 | } |
| 242 | return strings.TrimPrefix(variation, "afdo-") |
| 243 | } |