blob: 5c587c0b0d2a67ba705663a28d4ce91b80c537a3 [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
Yi Kongeb8efc92021-12-09 18:06:29 +080021 "android/soong/android"
Vinh Tran44cb78c2023-03-09 22:07:19 -050022
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
Yi Kongeb8efc92021-12-09 18:06:29 +080025)
26
Vinh Trancde10162023-03-09 22:07:19 -050027// TODO(b/267229066): Remove globalAfdoProfileProjects after implementing bp2build converter for fdo_profile
Yi Kongeb8efc92021-12-09 18:06:29 +080028var (
29 globalAfdoProfileProjects = []string{
30 "vendor/google_data/pgo_profile/sampling/",
31 "toolchain/pgo-profiles/sampling/",
32 }
33)
34
35var afdoProfileProjectsConfigKey = android.NewOnceKey("AfdoProfileProjects")
36
Vinh Tran056effb2023-06-27 14:03:25 +000037// This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering
38const afdoFlagsFormat = "-fprofile-sample-use=%s"
Yi Kongeb8efc92021-12-09 18:06:29 +080039
Yi Kong46c6e592022-01-20 22:55:00 +080040func recordMissingAfdoProfileFile(ctx android.BaseModuleContext, missing string) {
Yi Kongeb8efc92021-12-09 18:06:29 +080041 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
42}
43
Vinh Tran44cb78c2023-03-09 22:07:19 -050044type afdoRdep struct {
45 VariationName *string
46 ProfilePath *string
47}
48
Yi Kongeb8efc92021-12-09 18:06:29 +080049type AfdoProperties struct {
Alix40216ae2022-04-07 20:47:01 +000050 // Afdo allows developers self-service enroll for
51 // automatic feedback-directed optimization using profile data.
Yi Kongeb8efc92021-12-09 18:06:29 +080052 Afdo bool
53
Vinh Tran44cb78c2023-03-09 22:07:19 -050054 FdoProfilePath *string `blueprint:"mutated"`
55
56 AfdoRDeps []afdoRdep `blueprint:"mutated"`
Yi Kongeb8efc92021-12-09 18:06:29 +080057}
58
59type afdo struct {
60 Properties AfdoProperties
61}
62
63func (afdo *afdo) props() []interface{} {
64 return []interface{}{&afdo.Properties}
65}
66
Vinh Tran44cb78c2023-03-09 22:07:19 -050067// afdoEnabled returns true for binaries and shared libraries
68// that set afdo prop to True and there is a profile available
69func (afdo *afdo) afdoEnabled() bool {
Yabin Cui01c44562023-04-20 14:07:29 -070070 return afdo != nil && afdo.Properties.Afdo
Yi Kongeb8efc92021-12-09 18:06:29 +080071}
72
Yi Kongeb8efc92021-12-09 18:06:29 +080073func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
Yabin Cui01c44562023-04-20 14:07:29 -070074 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 Kongb33ced02023-10-10 14:11:50 +090087 flags.Local.CFlags = append([]string{"-mllvm", "-enable-fs-discriminator=true"}, flags.Local.CFlags...)
Yabin Cui01c44562023-04-20 14:07:29 -070088 }
Vinh Tran44cb78c2023-03-09 22:07:19 -050089 if path := afdo.Properties.FdoProfilePath; path != nil {
Yi Konga1961e72023-04-05 11:33:11 +090090 // The flags are prepended to allow overriding.
Vinh Tran056effb2023-06-27 14:03:25 +000091 profileUseFlag := fmt.Sprintf(afdoFlagsFormat, *path)
Yi Konga1961e72023-04-05 11:33:11 +090092 flags.Local.CFlags = append([]string{profileUseFlag}, flags.Local.CFlags...)
93 flags.Local.LdFlags = append([]string{profileUseFlag, "-Wl,-mllvm,-no-warn-sample-unused=true"}, flags.Local.LdFlags...)
Yi Kongeb8efc92021-12-09 18:06:29 +080094
Vinh Tran44cb78c2023-03-09 22:07:19 -050095 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
96 // if profileFile gets updated
97 pathForSrc := android.PathForSource(ctx, *path)
98 flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc)
99 flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc)
Yi Kongeb8efc92021-12-09 18:06:29 +0800100 }
101
102 return flags
103}
104
Vinh Tran44cb78c2023-03-09 22:07:19 -0500105func (afdo *afdo) addDep(ctx BaseModuleContext, actx android.BottomUpMutatorContext) {
106 if ctx.Host() {
107 return
108 }
109
110 if ctx.static() && !ctx.staticBinary() {
111 return
112 }
113
114 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
115 if fdoProfileName, err := actx.DeviceConfig().AfdoProfile(actx.ModuleName()); fdoProfileName != nil && err == nil {
116 actx.AddFarVariationDependencies(
117 []blueprint.Variation{
118 {Mutator: "arch", Variation: actx.Target().ArchVariation()},
119 {Mutator: "os", Variation: "android"},
120 },
121 FdoProfileTag,
122 []string{*fdoProfileName}...,
123 )
124 }
125 }
126}
127
128// FdoProfileMutator reads the FdoProfileProvider from a direct dep with FdoProfileTag
129// assigns FdoProfileInfo.Path to the FdoProfilePath mutated property
Vinh Trancde10162023-03-09 22:07:19 -0500130func (c *Module) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500131 if !c.Enabled() {
132 return
133 }
134
Yi Kong2dbe1602023-07-27 14:04:05 +0900135 if !c.afdo.afdoEnabled() {
136 return
137 }
138
Vinh Tran44cb78c2023-03-09 22:07:19 -0500139 ctx.VisitDirectDepsWithTag(FdoProfileTag, func(m android.Module) {
140 if ctx.OtherModuleHasProvider(m, FdoProfileProvider) {
141 info := ctx.OtherModuleProvider(m, FdoProfileProvider).(FdoProfileInfo)
142 c.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String())
143 }
144 })
145}
146
147var _ FdoProfileMutatorInterface = (*Module)(nil)
148
149// Propagate afdo requirements down from binaries and shared libraries
Yi Kongeb8efc92021-12-09 18:06:29 +0800150func afdoDepsMutator(mctx android.TopDownMutatorContext) {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500151 if m, ok := mctx.Module().(*Module); ok && m.afdo.afdoEnabled() {
Yabin Cui01c44562023-04-20 14:07:29 -0700152 path := m.afdo.Properties.FdoProfilePath
153 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
154 tag := mctx.OtherModuleDependencyTag(dep)
155 libTag, isLibTag := tag.(libraryDependencyTag)
Yi Kongeb8efc92021-12-09 18:06:29 +0800156
Yabin Cui01c44562023-04-20 14:07:29 -0700157 // Do not recurse down non-static dependencies
158 if isLibTag {
159 if !libTag.static() {
160 return false
Yi Kongeb8efc92021-12-09 18:06:29 +0800161 }
Yabin Cui01c44562023-04-20 14:07:29 -0700162 } else {
163 if tag != objDepTag && tag != reuseObjTag {
164 return false
Yi Kongeb8efc92021-12-09 18:06:29 +0800165 }
Yabin Cui01c44562023-04-20 14:07:29 -0700166 }
Yi Kongeb8efc92021-12-09 18:06:29 +0800167
Yabin Cui01c44562023-04-20 14:07:29 -0700168 if dep, ok := dep.(*Module); ok {
169 dep.afdo.Properties.AfdoRDeps = append(
170 dep.afdo.Properties.AfdoRDeps,
171 afdoRdep{
172 VariationName: proptools.StringPtr(encodeTarget(m.Name())),
173 ProfilePath: path,
174 },
175 )
176 }
177
178 return true
179 })
Yi Kongeb8efc92021-12-09 18:06:29 +0800180 }
181}
182
183// Create afdo variants for modules that need them
184func afdoMutator(mctx android.BottomUpMutatorContext) {
185 if m, ok := mctx.Module().(*Module); ok && m.afdo != nil {
Yabin Cui01c44562023-04-20 14:07:29 -0700186 if !m.static() && m.afdo.Properties.Afdo {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500187 mctx.SetDependencyVariation(encodeTarget(m.Name()))
188 return
Yi Kongeb8efc92021-12-09 18:06:29 +0800189 }
190
191 variationNames := []string{""}
Vinh Tran44cb78c2023-03-09 22:07:19 -0500192
193 variantNameToProfilePath := make(map[string]*string)
194
195 for _, afdoRDep := range m.afdo.Properties.AfdoRDeps {
196 variantName := *afdoRDep.VariationName
197 // An rdep can be set twice in AfdoRDeps because there can be
198 // more than one path from an afdo-enabled module to
199 // a static dep such as
200 // afdo_enabled_foo -> static_bar ----> static_baz
201 // \ ^
202 // ----------------------|
203 // We only need to create one variant per unique rdep
Yabin Cui01c44562023-04-20 14:07:29 -0700204 if _, exists := variantNameToProfilePath[variantName]; !exists {
Vinh Tran44cb78c2023-03-09 22:07:19 -0500205 variationNames = append(variationNames, variantName)
206 variantNameToProfilePath[variantName] = afdoRDep.ProfilePath
207 }
Yi Kongeb8efc92021-12-09 18:06:29 +0800208 }
Vinh Tran44cb78c2023-03-09 22:07:19 -0500209
Yi Kongeb8efc92021-12-09 18:06:29 +0800210 if len(variationNames) > 1 {
211 modules := mctx.CreateVariations(variationNames...)
212 for i, name := range variationNames {
213 if name == "" {
214 continue
215 }
216 variation := modules[i].(*Module)
217 variation.Properties.PreventInstall = true
218 variation.Properties.HideFromMake = true
Yabin Cui01c44562023-04-20 14:07:29 -0700219 variation.afdo.Properties.Afdo = true
Vinh Tran44cb78c2023-03-09 22:07:19 -0500220 variation.afdo.Properties.FdoProfilePath = variantNameToProfilePath[name]
Yi Kongeb8efc92021-12-09 18:06:29 +0800221 }
222 }
223 }
224}
225
226// Encode target name to variation name.
227func encodeTarget(target string) string {
228 if target == "" {
229 return ""
230 }
231 return "afdo-" + target
232}
233
234// Decode target name from variation name.
235func decodeTarget(variation string) string {
236 if variation == "" {
237 return ""
238 }
239 return strings.TrimPrefix(variation, "afdo-")
240}