afdo.go 6.0 KB

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