blob: c888213272a0b61418ce371d86b3e3a2b5de9f52 [file] [log] [blame]
Yi Kongeb8efc92021-12-09 18:06:29 +08001// 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
15package cc
16
17import (
18 "fmt"
19 "strings"
20
21 "github.com/google/blueprint/proptools"
22
23 "android/soong/android"
24)
25
26var (
27 globalAfdoProfileProjects = []string{
28 "vendor/google_data/pgo_profile/sampling/",
29 "toolchain/pgo-profiles/sampling/",
30 }
31)
32
33var afdoProfileProjectsConfigKey = android.NewOnceKey("AfdoProfileProjects")
34
Yi Kong71198ac2022-02-10 15:08:36 +080035const afdoCFlagsFormat = "-funique-internal-linkage-names -fprofile-sample-accurate -fprofile-sample-use=%s"
Yi Kongeb8efc92021-12-09 18:06:29 +080036
37func getAfdoProfileProjects(config android.DeviceConfig) []string {
38 return config.OnceStringSlice(afdoProfileProjectsConfigKey, func() []string {
39 return append(globalAfdoProfileProjects, config.AfdoAdditionalProfileDirs()...)
40 })
41}
42
Yi Kong46c6e592022-01-20 22:55:00 +080043func recordMissingAfdoProfileFile(ctx android.BaseModuleContext, missing string) {
Yi Kongeb8efc92021-12-09 18:06:29 +080044 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
45}
46
47type AfdoProperties struct {
48 Afdo bool
49
50 AfdoTarget *string `blueprint:"mutated"`
51 AfdoDeps []string `blueprint:"mutated"`
52}
53
54type afdo struct {
55 Properties AfdoProperties
56}
57
58func (afdo *afdo) props() []interface{} {
59 return []interface{}{&afdo.Properties}
60}
61
62func (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 Kong46c6e592022-01-20 22:55:00 +080070func getProfileFiles(ctx android.BaseModuleContext, moduleName string) []string {
Yi Kongeb8efc92021-12-09 18:06:29 +080071 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 Kong46c6e592022-01-20 22:55:00 +080077func (props *AfdoProperties) GetAfdoProfileFile(ctx android.BaseModuleContext, module string) android.OptionalPath {
Yi Kongeb8efc92021-12-09 18:06:29 +080078 // 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
95func (afdo *afdo) begin(ctx BaseModuleContext) {
Yi Kong88e632e2022-01-25 03:12:48 +080096 if ctx.Host() {
97 return
98 }
99 if ctx.static() && !ctx.staticBinary() {
100 return
101 }
102 if afdo.Properties.Afdo {
Yi Kongeb8efc92021-12-09 18:06:29 +0800103 module := ctx.ModuleName()
Yi Kong46c6e592022-01-20 22:55:00 +0800104 if afdo.Properties.GetAfdoProfileFile(ctx, module).Valid() {
Yi Kongeb8efc92021-12-09 18:06:29 +0800105 afdo.Properties.AfdoTarget = proptools.StringPtr(module)
106 }
107 }
108}
109
110func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
111 if profile := afdo.Properties.AfdoTarget; profile != nil {
Yi Kong46c6e592022-01-20 22:55:00 +0800112 if profileFile := afdo.Properties.GetAfdoProfileFile(ctx, *profile); profileFile.Valid() {
Yi Kongeb8efc92021-12-09 18:06:29 +0800113 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
131func 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
159func 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.
187func encodeTarget(target string) string {
188 if target == "" {
189 return ""
190 }
191 return "afdo-" + target
192}
193
194// Decode target name from variation name.
195func decodeTarget(variation string) string {
196 if variation == "" {
197 return ""
198 }
199 return strings.TrimPrefix(variation, "afdo-")
200}