blob: 79fbae1575160b8ba50c96bd9cc6ed10b0d2d4d1 [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"
Ke-Yu Lu351d3642024-02-06 02:15:03 +000024 "github.com/google/blueprint/proptools"
Yi Kongeb8efc92021-12-09 18:06:29 +080025)
26
Vinh Tran056effb2023-06-27 14:03:25 +000027// This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering
Yi Kong0880a822023-11-14 23:49:40 +000028const afdoFlagsFormat = "-fprofile-sample-use=%s -fprofile-sample-accurate"
Yi Kongeb8efc92021-12-09 18:06:29 +080029
Ke-Yu Lu0be9d602024-02-06 02:15:03 +000030func recordMissingAfdoProfileFile(ctx android.BaseModuleContext, missing string) {
31 getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
32}
33
34type afdoRdep struct {
35 VariationName *string
36 ProfilePath *string
37}
38
Yi Kongeb8efc92021-12-09 18:06:29 +080039type AfdoProperties struct {
Alix40216ae2022-04-07 20:47:01 +000040 // Afdo allows developers self-service enroll for
41 // automatic feedback-directed optimization using profile data.
Yi Kongeb8efc92021-12-09 18:06:29 +080042 Afdo bool
43
Ke-Yu Lu351d3642024-02-06 02:15:03 +000044 FdoProfilePath *string `blueprint:"mutated"`
Ke-Yu Lu0be9d602024-02-06 02:15:03 +000045
46 AfdoRDeps []afdoRdep `blueprint:"mutated"`
Yi Kongeb8efc92021-12-09 18:06:29 +080047}
48
49type afdo struct {
50 Properties AfdoProperties
51}
52
53func (afdo *afdo) props() []interface{} {
54 return []interface{}{&afdo.Properties}
55}
56
Yi Kong9723e332023-12-04 14:52:53 +090057func (afdo *afdo) begin(ctx BaseModuleContext) {
58 // Disable on eng builds for faster build.
59 if ctx.Config().Eng() {
60 afdo.Properties.Afdo = false
61 }
62}
63
Vinh Tran44cb78c2023-03-09 22:07:19 -050064// afdoEnabled returns true for binaries and shared libraries
65// that set afdo prop to True and there is a profile available
66func (afdo *afdo) afdoEnabled() bool {
Yabin Cui01c44562023-04-20 14:07:29 -070067 return afdo != nil && afdo.Properties.Afdo
Yi Kongeb8efc92021-12-09 18:06:29 +080068}
69
Yi Kongeb8efc92021-12-09 18:06:29 +080070func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
Ke-Yu Lu351d3642024-02-06 02:15:03 +000071 if afdo.Properties.Afdo {
Yabin Cui01c44562023-04-20 14:07:29 -070072 // We use `-funique-internal-linkage-names` to associate profiles to the right internal
73 // functions. This option should be used before generating a profile. Because a profile
74 // generated for a binary without unique names doesn't work well building a binary with
75 // unique names (they have different internal function names).
76 // To avoid a chicken-and-egg problem, we enable `-funique-internal-linkage-names` when
77 // `afdo=true`, whether a profile exists or not.
78 // The profile can take effect in three steps:
79 // 1. Add `afdo: true` in Android.bp, and build the binary.
80 // 2. Collect an AutoFDO profile for the binary.
81 // 3. Make the profile searchable by the build system. So it's used the next time the binary
82 // is built.
83 flags.Local.CFlags = append([]string{"-funique-internal-linkage-names"}, flags.Local.CFlags...)
Yi Kongbc2d02a2023-10-16 16:57:53 +090084 // Flags for Flow Sensitive AutoFDO
Yi Kongb33ced02023-10-10 14:11:50 +090085 flags.Local.CFlags = append([]string{"-mllvm", "-enable-fs-discriminator=true"}, flags.Local.CFlags...)
Yi Kongbc2d02a2023-10-16 16:57:53 +090086 // TODO(b/266595187): Remove the following feature once it is enabled in LLVM by default.
87 flags.Local.CFlags = append([]string{"-mllvm", "-improved-fs-discriminator=true"}, flags.Local.CFlags...)
Yabin Cui01c44562023-04-20 14:07:29 -070088 }
Ke-Yu Lu351d3642024-02-06 02:15:03 +000089 if path := afdo.Properties.FdoProfilePath; path != nil {
90 // The flags are prepended to allow overriding.
91 profileUseFlag := fmt.Sprintf(afdoFlagsFormat, *path)
92 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
Ke-Yu Lu351d3642024-02-06 02:15:03 +000095 // 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
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000105func (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
Ke-Yu Lu351d3642024-02-06 02:15:03 +0000128// FdoProfileMutator reads the FdoProfileProvider from a direct dep with FdoProfileTag
129// assigns FdoProfileInfo.Path to the FdoProfilePath mutated property
130func (c *Module) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
131 if !c.Enabled() {
132 return
133 }
134
135 if !c.afdo.afdoEnabled() {
136 return
137 }
138
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000139 ctx.VisitDirectDepsWithTag(FdoProfileTag, func(m android.Module) {
140 if info, ok := android.OtherModuleProvider(ctx, m, FdoProfileProvider); ok {
141 c.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String())
Ke-Yu Lu351d3642024-02-06 02:15:03 +0000142 }
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000143 })
Vinh Tran44cb78c2023-03-09 22:07:19 -0500144}
145
Ke-Yu Lu351d3642024-02-06 02:15:03 +0000146var _ FdoProfileMutatorInterface = (*Module)(nil)
147
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000148// Propagate afdo requirements down from binaries and shared libraries
149func afdoDepsMutator(mctx android.TopDownMutatorContext) {
150 if m, ok := mctx.Module().(*Module); ok && m.afdo.afdoEnabled() {
151 path := m.afdo.Properties.FdoProfilePath
152 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
153 tag := mctx.OtherModuleDependencyTag(dep)
154 libTag, isLibTag := tag.(libraryDependencyTag)
Yi Kongeb8efc92021-12-09 18:06:29 +0800155
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000156 // Do not recurse down non-static dependencies
157 if isLibTag {
158 if !libTag.static() {
159 return false
Ke-Yu Lu351d3642024-02-06 02:15:03 +0000160 }
Ke-Yu Lu0be9d602024-02-06 02:15:03 +0000161 } else {
162 if tag != objDepTag && tag != reuseObjTag {
163 return false
164 }
165 }
166
167 if dep, ok := dep.(*Module); ok {
168 dep.afdo.Properties.AfdoRDeps = append(
169 dep.afdo.Properties.AfdoRDeps,
170 afdoRdep{
171 VariationName: proptools.StringPtr(encodeTarget(m.Name())),
172 ProfilePath: path,
173 },
174 )
175 }
176
177 return true
178 })
179 }
180}
181
182// Create afdo variants for modules that need them
183func afdoMutator(mctx android.BottomUpMutatorContext) {
184 if m, ok := mctx.Module().(*Module); ok && m.afdo != nil {
185 if !m.static() && m.afdo.Properties.Afdo {
186 mctx.SetDependencyVariation(encodeTarget(m.Name()))
187 return
188 }
189
190 variationNames := []string{""}
191
192 variantNameToProfilePath := make(map[string]*string)
193
194 for _, afdoRDep := range m.afdo.Properties.AfdoRDeps {
195 variantName := *afdoRDep.VariationName
196 // An rdep can be set twice in AfdoRDeps because there can be
197 // more than one path from an afdo-enabled module to
198 // a static dep such as
199 // afdo_enabled_foo -> static_bar ----> static_baz
200 // \ ^
201 // ----------------------|
202 // We only need to create one variant per unique rdep
203 if _, exists := variantNameToProfilePath[variantName]; !exists {
204 variationNames = append(variationNames, variantName)
205 variantNameToProfilePath[variantName] = afdoRDep.ProfilePath
206 }
207 }
208
209 if len(variationNames) > 1 {
210 modules := mctx.CreateVariations(variationNames...)
211 for i, name := range variationNames {
212 if name == "" {
213 continue
214 }
215 variation := modules[i].(*Module)
216 variation.Properties.PreventInstall = true
217 variation.Properties.HideFromMake = true
218 variation.afdo.Properties.Afdo = true
219 variation.afdo.Properties.FdoProfilePath = variantNameToProfilePath[name]
Yi Kongeb8efc92021-12-09 18:06:29 +0800220 }
Yi Kongeb8efc92021-12-09 18:06:29 +0800221 }
222 }
223}
224
225// Encode target name to variation name.
226func encodeTarget(target string) string {
227 if target == "" {
228 return ""
229 }
230 return "afdo-" + target
231}
232
233// Decode target name from variation name.
234func decodeTarget(variation string) string {
235 if variation == "" {
236 return ""
237 }
238 return strings.TrimPrefix(variation, "afdo-")
239}