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" |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 25 | ) |
| 26 | |
Vinh Tran | 056effb | 2023-06-27 14:03:25 +0000 | [diff] [blame] | 27 | // This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering |
Yi Kong | 0880a82 | 2023-11-14 23:49:40 +0000 | [diff] [blame] | 28 | const afdoFlagsFormat = "-fprofile-sample-use=%s -fprofile-sample-accurate" |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 29 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 30 | type AfdoProperties struct { |
Alix | 40216ae | 2022-04-07 20:47:01 +0000 | [diff] [blame] | 31 | // Afdo allows developers self-service enroll for |
| 32 | // automatic feedback-directed optimization using profile data. |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 33 | Afdo bool |
| 34 | |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 35 | FdoProfilePath *string `blueprint:"mutated"` |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | type afdo struct { |
| 39 | Properties AfdoProperties |
| 40 | } |
| 41 | |
| 42 | func (afdo *afdo) props() []interface{} { |
| 43 | return []interface{}{&afdo.Properties} |
| 44 | } |
| 45 | |
Yi Kong | 9723e33 | 2023-12-04 14:52:53 +0900 | [diff] [blame] | 46 | func (afdo *afdo) begin(ctx BaseModuleContext) { |
| 47 | // Disable on eng builds for faster build. |
| 48 | if ctx.Config().Eng() { |
| 49 | afdo.Properties.Afdo = false |
| 50 | } |
| 51 | } |
| 52 | |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 53 | // afdoEnabled returns true for binaries and shared libraries |
| 54 | // that set afdo prop to True and there is a profile available |
| 55 | func (afdo *afdo) afdoEnabled() bool { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 56 | return afdo != nil && afdo.Properties.Afdo |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 57 | } |
| 58 | |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 59 | func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 15fa814 | 2024-02-07 15:09:08 -0800 | [diff] [blame] | 60 | if ctx.Host() { |
| 61 | return flags |
| 62 | } |
| 63 | |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 64 | if afdo.Properties.Afdo { |
Yabin Cui | 01c4456 | 2023-04-20 14:07:29 -0700 | [diff] [blame] | 65 | // We use `-funique-internal-linkage-names` to associate profiles to the right internal |
| 66 | // functions. This option should be used before generating a profile. Because a profile |
| 67 | // generated for a binary without unique names doesn't work well building a binary with |
| 68 | // unique names (they have different internal function names). |
| 69 | // To avoid a chicken-and-egg problem, we enable `-funique-internal-linkage-names` when |
| 70 | // `afdo=true`, whether a profile exists or not. |
| 71 | // The profile can take effect in three steps: |
| 72 | // 1. Add `afdo: true` in Android.bp, and build the binary. |
| 73 | // 2. Collect an AutoFDO profile for the binary. |
| 74 | // 3. Make the profile searchable by the build system. So it's used the next time the binary |
| 75 | // is built. |
| 76 | flags.Local.CFlags = append([]string{"-funique-internal-linkage-names"}, flags.Local.CFlags...) |
Yi Kong | bc2d02a | 2023-10-16 16:57:53 +0900 | [diff] [blame] | 77 | // Flags for Flow Sensitive AutoFDO |
Yi Kong | b33ced0 | 2023-10-10 14:11:50 +0900 | [diff] [blame] | 78 | 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] | 79 | // TODO(b/266595187): Remove the following feature once it is enabled in LLVM by default. |
| 80 | 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] | 81 | } |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 82 | if path := afdo.Properties.FdoProfilePath; path != nil { |
| 83 | // The flags are prepended to allow overriding. |
| 84 | profileUseFlag := fmt.Sprintf(afdoFlagsFormat, *path) |
| 85 | flags.Local.CFlags = append([]string{profileUseFlag}, flags.Local.CFlags...) |
| 86 | 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] | 87 | |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 88 | // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt |
| 89 | // if profileFile gets updated |
| 90 | pathForSrc := android.PathForSource(ctx, *path) |
| 91 | flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc) |
| 92 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc) |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return flags |
| 96 | } |
| 97 | |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 98 | // FdoProfileMutator reads the FdoProfileProvider from a direct dep with FdoProfileTag |
| 99 | // assigns FdoProfileInfo.Path to the FdoProfilePath mutated property |
| 100 | func (c *Module) fdoProfileMutator(ctx android.BottomUpMutatorContext) { |
| 101 | if !c.Enabled() { |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | if !c.afdo.afdoEnabled() { |
| 106 | return |
| 107 | } |
| 108 | |
Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame^] | 109 | if c.Host() { |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | if c.static() && !c.staticBinary() { |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | if c, ok := ctx.Module().(*Module); ok && c.Enabled() { |
| 118 | if fdoProfileName, err := ctx.DeviceConfig().AfdoProfile(ctx.ModuleName()); fdoProfileName != "" && err == nil { |
| 119 | deps := ctx.AddFarVariationDependencies( |
| 120 | []blueprint.Variation{ |
| 121 | {Mutator: "arch", Variation: ctx.Target().ArchVariation()}, |
| 122 | {Mutator: "os", Variation: "android"}, |
| 123 | }, |
| 124 | FdoProfileTag, |
| 125 | fdoProfileName) |
| 126 | if len(deps) > 0 && deps[0] != nil { |
| 127 | if info, ok := android.OtherModuleProvider(ctx, deps[0], FdoProfileProvider); ok { |
| 128 | c.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String()) |
| 129 | } |
| 130 | } |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 131 | } |
Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame^] | 132 | } |
Vinh Tran | 44cb78c | 2023-03-09 22:07:19 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Ke-Yu Lu | 351d364 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 135 | var _ FdoProfileMutatorInterface = (*Module)(nil) |
| 136 | |
Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame^] | 137 | func afdoPropagateViaDepTag(tag blueprint.DependencyTag) bool { |
| 138 | libTag, isLibTag := tag.(libraryDependencyTag) |
| 139 | // Do not recurse down non-static dependencies |
| 140 | if isLibTag { |
| 141 | return libTag.static() |
| 142 | } else { |
| 143 | return tag == objDepTag || tag == reuseObjTag || tag == staticVariantTag |
Ke-Yu Lu | 0be9d60 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame^] | 147 | // afdoTransitionMutator creates afdo variants of cc modules. |
| 148 | type afdoTransitionMutator struct{} |
| 149 | |
| 150 | func (a *afdoTransitionMutator) Split(ctx android.BaseModuleContext) []string { |
| 151 | return []string{""} |
| 152 | } |
| 153 | |
| 154 | func (a *afdoTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { |
| 155 | if ctx.Host() { |
| 156 | return "" |
| 157 | } |
| 158 | |
| 159 | if m, ok := ctx.Module().(*Module); ok && m.afdo != nil { |
| 160 | if !afdoPropagateViaDepTag(ctx.DepTag()) { |
| 161 | return "" |
| 162 | } |
| 163 | |
| 164 | if sourceVariation != "" { |
| 165 | return sourceVariation |
| 166 | } |
| 167 | |
| 168 | if !m.afdo.afdoEnabled() { |
| 169 | return "" |
| 170 | } |
| 171 | |
| 172 | // TODO(b/324141705): this is designed to prevent propagating AFDO from static libraries that have afdo: true set, but |
| 173 | // it should be m.static() && !m.staticBinary() so that static binaries use AFDO variants of dependencies. |
| 174 | if m.static() { |
| 175 | return "" |
| 176 | } |
| 177 | |
| 178 | return encodeTarget(ctx.Module().Name()) |
| 179 | } |
| 180 | return "" |
| 181 | } |
| 182 | |
| 183 | func (a *afdoTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { |
| 184 | if m, ok := ctx.Module().(*Module); ok && m.afdo != nil { |
| 185 | return incomingVariation |
| 186 | } |
| 187 | return "" |
| 188 | } |
| 189 | |
| 190 | func (a *afdoTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { |
| 191 | if variation == "" { |
Colin Cross | 15fa814 | 2024-02-07 15:09:08 -0800 | [diff] [blame] | 192 | return |
| 193 | } |
| 194 | |
Colin Cross | d38feb0 | 2024-01-23 16:38:06 -0800 | [diff] [blame^] | 195 | if m, ok := ctx.Module().(*Module); ok && m.afdo != nil { |
| 196 | m.Properties.PreventInstall = true |
| 197 | m.Properties.HideFromMake = true |
| 198 | m.afdo.Properties.Afdo = true |
| 199 | if fdoProfileName, err := ctx.DeviceConfig().AfdoProfile(decodeTarget(variation)); fdoProfileName != "" && err == nil { |
| 200 | deps := ctx.AddFarVariationDependencies( |
| 201 | []blueprint.Variation{ |
| 202 | {Mutator: "arch", Variation: ctx.Target().ArchVariation()}, |
| 203 | {Mutator: "os", Variation: "android"}, |
| 204 | }, |
| 205 | FdoProfileTag, |
| 206 | fdoProfileName) |
| 207 | if len(deps) > 0 && deps[0] != nil { |
| 208 | if info, ok := android.OtherModuleProvider(ctx, deps[0], FdoProfileProvider); ok { |
| 209 | m.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String()) |
Ke-Yu Lu | 0be9d60 | 2024-02-06 02:15:03 +0000 | [diff] [blame] | 210 | } |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 211 | } |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Encode target name to variation name. |
| 217 | func encodeTarget(target string) string { |
| 218 | if target == "" { |
| 219 | return "" |
| 220 | } |
| 221 | return "afdo-" + target |
| 222 | } |
| 223 | |
| 224 | // Decode target name from variation name. |
| 225 | func decodeTarget(variation string) string { |
| 226 | if variation == "" { |
| 227 | return "" |
| 228 | } |
| 229 | return strings.TrimPrefix(variation, "afdo-") |
| 230 | } |