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 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | globalAfdoProfileProjects = []string{ |
| 28 | "vendor/google_data/pgo_profile/sampling/", |
| 29 | "toolchain/pgo-profiles/sampling/", |
| 30 | } |
| 31 | ) |
| 32 | |
| 33 | var afdoProfileProjectsConfigKey = android.NewOnceKey("AfdoProfileProjects") |
| 34 | |
Yi Kong | 71198ac | 2022-02-10 15:08:36 +0800 | [diff] [blame] | 35 | const afdoCFlagsFormat = "-funique-internal-linkage-names -fprofile-sample-accurate -fprofile-sample-use=%s" |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 36 | |
| 37 | func getAfdoProfileProjects(config android.DeviceConfig) []string { |
| 38 | return config.OnceStringSlice(afdoProfileProjectsConfigKey, func() []string { |
| 39 | return append(globalAfdoProfileProjects, config.AfdoAdditionalProfileDirs()...) |
| 40 | }) |
| 41 | } |
| 42 | |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 43 | func recordMissingAfdoProfileFile(ctx android.BaseModuleContext, missing string) { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 44 | getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true) |
| 45 | } |
| 46 | |
| 47 | type AfdoProperties struct { |
| 48 | Afdo bool |
| 49 | |
| 50 | AfdoTarget *string `blueprint:"mutated"` |
| 51 | AfdoDeps []string `blueprint:"mutated"` |
| 52 | } |
| 53 | |
| 54 | type afdo struct { |
| 55 | Properties AfdoProperties |
| 56 | } |
| 57 | |
| 58 | func (afdo *afdo) props() []interface{} { |
| 59 | return []interface{}{&afdo.Properties} |
| 60 | } |
| 61 | |
| 62 | func (afdo *afdo) AfdoEnabled() bool { |
| 63 | return afdo != nil && afdo.Properties.Afdo && afdo.Properties.AfdoTarget != nil |
| 64 | } |
| 65 | |
| 66 | // Get list of profile file names, ordered by level of specialisation. For example: |
| 67 | // 1. libfoo_arm64.afdo |
| 68 | // 2. libfoo.afdo |
| 69 | // Add more specialisation as needed. |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 70 | func getProfileFiles(ctx android.BaseModuleContext, moduleName string) []string { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 71 | var files []string |
| 72 | files = append(files, moduleName+"_"+ctx.Arch().ArchType.String()+".afdo") |
| 73 | files = append(files, moduleName+".afdo") |
| 74 | return files |
| 75 | } |
| 76 | |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 77 | func (props *AfdoProperties) GetAfdoProfileFile(ctx android.BaseModuleContext, module string) android.OptionalPath { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 78 | // Test if the profile_file is present in any of the Afdo profile projects |
| 79 | for _, profileFile := range getProfileFiles(ctx, module) { |
| 80 | for _, profileProject := range getAfdoProfileProjects(ctx.DeviceConfig()) { |
| 81 | path := android.ExistentPathForSource(ctx, profileProject, profileFile) |
| 82 | if path.Valid() { |
| 83 | return path |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Record that this module's profile file is absent |
| 89 | missing := ctx.ModuleDir() + ":" + module |
| 90 | recordMissingAfdoProfileFile(ctx, missing) |
| 91 | |
| 92 | return android.OptionalPathForPath(nil) |
| 93 | } |
| 94 | |
| 95 | func (afdo *afdo) begin(ctx BaseModuleContext) { |
Yi Kong | 88e632e | 2022-01-25 03:12:48 +0800 | [diff] [blame] | 96 | if ctx.Host() { |
| 97 | return |
| 98 | } |
| 99 | if ctx.static() && !ctx.staticBinary() { |
| 100 | return |
| 101 | } |
| 102 | if afdo.Properties.Afdo { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 103 | module := ctx.ModuleName() |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 104 | if afdo.Properties.GetAfdoProfileFile(ctx, module).Valid() { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 105 | afdo.Properties.AfdoTarget = proptools.StringPtr(module) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags { |
| 111 | if profile := afdo.Properties.AfdoTarget; profile != nil { |
Yi Kong | 46c6e59 | 2022-01-20 22:55:00 +0800 | [diff] [blame] | 112 | if profileFile := afdo.Properties.GetAfdoProfileFile(ctx, *profile); profileFile.Valid() { |
Yi Kong | eb8efc9 | 2021-12-09 18:06:29 +0800 | [diff] [blame] | 113 | profileFilePath := profileFile.Path() |
| 114 | |
| 115 | profileUseFlag := fmt.Sprintf(afdoCFlagsFormat, profileFile) |
| 116 | flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlag) |
| 117 | flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlag) |
| 118 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true") |
| 119 | |
| 120 | // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt |
| 121 | // if profileFile gets updated |
| 122 | flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath) |
| 123 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return flags |
| 128 | } |
| 129 | |
| 130 | // Propagate afdo requirements down from binaries |
| 131 | func afdoDepsMutator(mctx android.TopDownMutatorContext) { |
| 132 | if m, ok := mctx.Module().(*Module); ok && m.afdo.AfdoEnabled() { |
| 133 | afdoTarget := *m.afdo.Properties.AfdoTarget |
| 134 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 135 | tag := mctx.OtherModuleDependencyTag(dep) |
| 136 | libTag, isLibTag := tag.(libraryDependencyTag) |
| 137 | |
| 138 | // Do not recurse down non-static dependencies |
| 139 | if isLibTag { |
| 140 | if !libTag.static() { |
| 141 | return false |
| 142 | } |
| 143 | } else { |
| 144 | if tag != objDepTag && tag != reuseObjTag { |
| 145 | return false |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if dep, ok := dep.(*Module); ok { |
| 150 | dep.afdo.Properties.AfdoDeps = append(dep.afdo.Properties.AfdoDeps, afdoTarget) |
| 151 | } |
| 152 | |
| 153 | return true |
| 154 | }) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Create afdo variants for modules that need them |
| 159 | func afdoMutator(mctx android.BottomUpMutatorContext) { |
| 160 | if m, ok := mctx.Module().(*Module); ok && m.afdo != nil { |
| 161 | if m.afdo.AfdoEnabled() && !m.static() { |
| 162 | afdoTarget := *m.afdo.Properties.AfdoTarget |
| 163 | mctx.SetDependencyVariation(encodeTarget(afdoTarget)) |
| 164 | } |
| 165 | |
| 166 | variationNames := []string{""} |
| 167 | afdoDeps := android.FirstUniqueStrings(m.afdo.Properties.AfdoDeps) |
| 168 | for _, dep := range afdoDeps { |
| 169 | variationNames = append(variationNames, encodeTarget(dep)) |
| 170 | } |
| 171 | if len(variationNames) > 1 { |
| 172 | modules := mctx.CreateVariations(variationNames...) |
| 173 | for i, name := range variationNames { |
| 174 | if name == "" { |
| 175 | continue |
| 176 | } |
| 177 | variation := modules[i].(*Module) |
| 178 | variation.Properties.PreventInstall = true |
| 179 | variation.Properties.HideFromMake = true |
| 180 | variation.afdo.Properties.AfdoTarget = proptools.StringPtr(decodeTarget(name)) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Encode target name to variation name. |
| 187 | func encodeTarget(target string) string { |
| 188 | if target == "" { |
| 189 | return "" |
| 190 | } |
| 191 | return "afdo-" + target |
| 192 | } |
| 193 | |
| 194 | // Decode target name from variation name. |
| 195 | func decodeTarget(variation string) string { |
| 196 | if variation == "" { |
| 197 | return "" |
| 198 | } |
| 199 | return strings.TrimPrefix(variation, "afdo-") |
| 200 | } |