blob: 022f2833bee8b38164e919bc99cee9664692457e [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
35const afdoCFlagsFormat = "-fprofile-sample-accurate -fprofile-sample-use=%s"
36
37func getAfdoProfileProjects(config android.DeviceConfig) []string {
38 return config.OnceStringSlice(afdoProfileProjectsConfigKey, func() []string {
39 return append(globalAfdoProfileProjects, config.AfdoAdditionalProfileDirs()...)
40 })
41}
42
43func recordMissingAfdoProfileFile(ctx BaseModuleContext, missing string) {
44 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.
70func getProfileFiles(ctx BaseModuleContext, moduleName string) []string {
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
77func (props *AfdoProperties) getAfdoProfileFile(ctx BaseModuleContext, module string) android.OptionalPath {
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
95func (afdo *afdo) begin(ctx BaseModuleContext) {
96 if afdo.Properties.Afdo && !ctx.static() && !ctx.Host() {
97 module := ctx.ModuleName()
98 if afdo.Properties.getAfdoProfileFile(ctx, module).Valid() {
99 afdo.Properties.AfdoTarget = proptools.StringPtr(module)
100 }
101 }
102}
103
104func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
105 if profile := afdo.Properties.AfdoTarget; profile != nil {
106 if profileFile := afdo.Properties.getAfdoProfileFile(ctx, *profile); profileFile.Valid() {
107 profileFilePath := profileFile.Path()
108
109 profileUseFlag := fmt.Sprintf(afdoCFlagsFormat, profileFile)
110 flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlag)
111 flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlag)
112 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true")
113
114 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
115 // if profileFile gets updated
116 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
117 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
118 }
119 }
120
121 return flags
122}
123
124// Propagate afdo requirements down from binaries
125func afdoDepsMutator(mctx android.TopDownMutatorContext) {
126 if m, ok := mctx.Module().(*Module); ok && m.afdo.AfdoEnabled() {
127 afdoTarget := *m.afdo.Properties.AfdoTarget
128 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
129 tag := mctx.OtherModuleDependencyTag(dep)
130 libTag, isLibTag := tag.(libraryDependencyTag)
131
132 // Do not recurse down non-static dependencies
133 if isLibTag {
134 if !libTag.static() {
135 return false
136 }
137 } else {
138 if tag != objDepTag && tag != reuseObjTag {
139 return false
140 }
141 }
142
143 if dep, ok := dep.(*Module); ok {
144 dep.afdo.Properties.AfdoDeps = append(dep.afdo.Properties.AfdoDeps, afdoTarget)
145 }
146
147 return true
148 })
149 }
150}
151
152// Create afdo variants for modules that need them
153func afdoMutator(mctx android.BottomUpMutatorContext) {
154 if m, ok := mctx.Module().(*Module); ok && m.afdo != nil {
155 if m.afdo.AfdoEnabled() && !m.static() {
156 afdoTarget := *m.afdo.Properties.AfdoTarget
157 mctx.SetDependencyVariation(encodeTarget(afdoTarget))
158 }
159
160 variationNames := []string{""}
161 afdoDeps := android.FirstUniqueStrings(m.afdo.Properties.AfdoDeps)
162 for _, dep := range afdoDeps {
163 variationNames = append(variationNames, encodeTarget(dep))
164 }
165 if len(variationNames) > 1 {
166 modules := mctx.CreateVariations(variationNames...)
167 for i, name := range variationNames {
168 if name == "" {
169 continue
170 }
171 variation := modules[i].(*Module)
172 variation.Properties.PreventInstall = true
173 variation.Properties.HideFromMake = true
174 variation.afdo.Properties.AfdoTarget = proptools.StringPtr(decodeTarget(name))
175 }
176 }
177 }
178}
179
180// Encode target name to variation name.
181func encodeTarget(target string) string {
182 if target == "" {
183 return ""
184 }
185 return "afdo-" + target
186}
187
188// Decode target name from variation name.
189func decodeTarget(variation string) string {
190 if variation == "" {
191 return ""
192 }
193 return strings.TrimPrefix(variation, "afdo-")
194}