2021-12-09 11:06:29 +01:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"android/soong/android"
|
2023-03-10 04:07:19 +01:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2021-12-09 11:06:29 +01:00
|
|
|
)
|
|
|
|
|
2023-06-27 16:03:25 +02:00
|
|
|
// This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering
|
2023-11-15 00:49:40 +01:00
|
|
|
const afdoFlagsFormat = "-fprofile-sample-use=%s -fprofile-sample-accurate"
|
2021-12-09 11:06:29 +01:00
|
|
|
|
|
|
|
type AfdoProperties struct {
|
2022-04-07 22:47:01 +02:00
|
|
|
// Afdo allows developers self-service enroll for
|
|
|
|
// automatic feedback-directed optimization using profile data.
|
2021-12-09 11:06:29 +01:00
|
|
|
Afdo bool
|
|
|
|
|
2024-01-24 23:44:47 +01:00
|
|
|
AfdoDep bool `blueprint:"mutated"`
|
2021-12-09 11:06:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type afdo struct {
|
|
|
|
Properties AfdoProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (afdo *afdo) props() []interface{} {
|
|
|
|
return []interface{}{&afdo.Properties}
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:52:53 +01:00
|
|
|
func (afdo *afdo) begin(ctx BaseModuleContext) {
|
|
|
|
// Disable on eng builds for faster build.
|
|
|
|
if ctx.Config().Eng() {
|
|
|
|
afdo.Properties.Afdo = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 04:07:19 +01:00
|
|
|
// afdoEnabled returns true for binaries and shared libraries
|
2024-01-24 23:44:47 +01:00
|
|
|
// that set afdo prop to True.
|
2023-03-10 04:07:19 +01:00
|
|
|
func (afdo *afdo) afdoEnabled() bool {
|
2023-04-20 23:07:29 +02:00
|
|
|
return afdo != nil && afdo.Properties.Afdo
|
2021-12-09 11:06:29 +01:00
|
|
|
}
|
|
|
|
|
2024-01-24 23:44:47 +01:00
|
|
|
func (afdo *afdo) isAfdoCompile(ctx ModuleContext) bool {
|
|
|
|
fdoProfilePath := getFdoProfilePathFromDep(ctx)
|
|
|
|
return !ctx.Host() && (afdo.Properties.Afdo || afdo.Properties.AfdoDep) && (fdoProfilePath != "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFdoProfilePathFromDep(ctx ModuleContext) string {
|
|
|
|
fdoProfileDeps := ctx.GetDirectDepsWithTag(FdoProfileTag)
|
|
|
|
if len(fdoProfileDeps) > 0 && fdoProfileDeps[0] != nil {
|
|
|
|
if info, ok := android.OtherModuleProvider(ctx, fdoProfileDeps[0], FdoProfileProvider); ok {
|
|
|
|
return info.Path.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-03-10 04:07:19 +01:00
|
|
|
func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
|
2024-02-08 00:09:08 +01:00
|
|
|
if ctx.Host() {
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:44:47 +01:00
|
|
|
if afdo.Properties.Afdo || afdo.Properties.AfdoDep {
|
2023-04-20 23:07:29 +02:00
|
|
|
// We use `-funique-internal-linkage-names` to associate profiles to the right internal
|
|
|
|
// functions. This option should be used before generating a profile. Because a profile
|
|
|
|
// generated for a binary without unique names doesn't work well building a binary with
|
|
|
|
// unique names (they have different internal function names).
|
|
|
|
// To avoid a chicken-and-egg problem, we enable `-funique-internal-linkage-names` when
|
|
|
|
// `afdo=true`, whether a profile exists or not.
|
|
|
|
// The profile can take effect in three steps:
|
|
|
|
// 1. Add `afdo: true` in Android.bp, and build the binary.
|
|
|
|
// 2. Collect an AutoFDO profile for the binary.
|
|
|
|
// 3. Make the profile searchable by the build system. So it's used the next time the binary
|
|
|
|
// is built.
|
|
|
|
flags.Local.CFlags = append([]string{"-funique-internal-linkage-names"}, flags.Local.CFlags...)
|
2023-10-16 09:57:53 +02:00
|
|
|
// Flags for Flow Sensitive AutoFDO
|
2023-10-10 07:11:50 +02:00
|
|
|
flags.Local.CFlags = append([]string{"-mllvm", "-enable-fs-discriminator=true"}, flags.Local.CFlags...)
|
2023-10-16 09:57:53 +02:00
|
|
|
// TODO(b/266595187): Remove the following feature once it is enabled in LLVM by default.
|
|
|
|
flags.Local.CFlags = append([]string{"-mllvm", "-improved-fs-discriminator=true"}, flags.Local.CFlags...)
|
2023-04-20 23:07:29 +02:00
|
|
|
}
|
2024-01-24 23:44:47 +01:00
|
|
|
if fdoProfilePath := getFdoProfilePathFromDep(ctx); fdoProfilePath != "" {
|
2024-02-06 03:15:03 +01:00
|
|
|
// The flags are prepended to allow overriding.
|
2024-01-24 23:44:47 +01:00
|
|
|
profileUseFlag := fmt.Sprintf(afdoFlagsFormat, fdoProfilePath)
|
2024-02-06 03:15:03 +01:00
|
|
|
flags.Local.CFlags = append([]string{profileUseFlag}, flags.Local.CFlags...)
|
|
|
|
flags.Local.LdFlags = append([]string{profileUseFlag, "-Wl,-mllvm,-no-warn-sample-unused=true"}, flags.Local.LdFlags...)
|
|
|
|
|
|
|
|
// Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
|
|
|
|
// if profileFile gets updated
|
2024-01-24 23:44:47 +01:00
|
|
|
pathForSrc := android.PathForSource(ctx, fdoProfilePath)
|
2024-02-06 03:15:03 +01:00
|
|
|
flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc)
|
|
|
|
flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc)
|
2023-03-10 04:07:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:44:47 +01:00
|
|
|
func (a *afdo) addDep(ctx android.BottomUpMutatorContext, fdoProfileTarget string) {
|
|
|
|
if fdoProfileName, err := ctx.DeviceConfig().AfdoProfile(fdoProfileTarget); fdoProfileName != "" && err == nil {
|
|
|
|
ctx.AddFarVariationDependencies(
|
|
|
|
[]blueprint.Variation{
|
|
|
|
{Mutator: "arch", Variation: ctx.Target().ArchVariation()},
|
|
|
|
{Mutator: "os", Variation: "android"},
|
|
|
|
},
|
|
|
|
FdoProfileTag,
|
|
|
|
fdoProfileName)
|
2021-12-09 11:06:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
func afdoPropagateViaDepTag(tag blueprint.DependencyTag) bool {
|
|
|
|
libTag, isLibTag := tag.(libraryDependencyTag)
|
|
|
|
// Do not recurse down non-static dependencies
|
|
|
|
if isLibTag {
|
|
|
|
return libTag.static()
|
|
|
|
} else {
|
|
|
|
return tag == objDepTag || tag == reuseObjTag || tag == staticVariantTag
|
2024-02-06 03:15:03 +01:00
|
|
|
}
|
2024-01-24 01:38:06 +01:00
|
|
|
}
|
2021-12-09 11:06:29 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
// afdoTransitionMutator creates afdo variants of cc modules.
|
|
|
|
type afdoTransitionMutator struct{}
|
2023-04-20 23:07:29 +02:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
func (a *afdoTransitionMutator) Split(ctx android.BaseModuleContext) []string {
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *afdoTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
|
|
|
|
if ctx.Host() {
|
|
|
|
return ""
|
2024-02-08 00:09:08 +01:00
|
|
|
}
|
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
if m, ok := ctx.Module().(*Module); ok && m.afdo != nil {
|
|
|
|
if !afdoPropagateViaDepTag(ctx.DepTag()) {
|
|
|
|
return ""
|
|
|
|
}
|
2024-02-06 03:15:03 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
if sourceVariation != "" {
|
|
|
|
return sourceVariation
|
|
|
|
}
|
2024-01-24 01:38:06 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
if !m.afdo.afdoEnabled() {
|
|
|
|
return ""
|
|
|
|
}
|
2021-12-09 11:06:29 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
// TODO(b/324141705): this is designed to prevent propagating AFDO from static libraries that have afdo: true set, but
|
|
|
|
// it should be m.static() && !m.staticBinary() so that static binaries use AFDO variants of dependencies.
|
|
|
|
if m.static() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return encodeTarget(ctx.Module().Name())
|
2024-01-24 01:38:06 +01:00
|
|
|
}
|
2024-01-24 01:38:06 +01:00
|
|
|
return ""
|
2024-01-24 01:38:06 +01:00
|
|
|
}
|
2021-12-09 11:06:29 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
func (a *afdoTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
|
|
|
|
if m, ok := ctx.Module().(*Module); ok && m.afdo != nil {
|
|
|
|
return incomingVariation
|
2024-02-08 00:09:08 +01:00
|
|
|
}
|
2024-01-24 01:38:06 +01:00
|
|
|
return ""
|
|
|
|
}
|
2024-02-08 00:09:08 +01:00
|
|
|
|
2024-01-24 01:38:06 +01:00
|
|
|
func (a *afdoTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
|
|
|
|
if m, ok := ctx.Module().(*Module); ok && m.afdo != nil {
|
2024-01-24 23:44:47 +01:00
|
|
|
if variation == "" {
|
|
|
|
// The empty variation is either a module that has enabled AFDO for itself, or the non-AFDO
|
|
|
|
// variant of a dependency.
|
|
|
|
if m.afdo.afdoEnabled() && !(m.static() && !m.staticBinary()) && !m.Host() {
|
|
|
|
m.afdo.addDep(ctx, ctx.ModuleName())
|
2021-12-09 11:06:29 +01:00
|
|
|
}
|
2024-01-24 23:44:47 +01:00
|
|
|
} else {
|
|
|
|
// The non-empty variation is the AFDO variant of a dependency of a module that enabled AFDO
|
|
|
|
// for itself.
|
|
|
|
m.Properties.PreventInstall = true
|
|
|
|
m.Properties.HideFromMake = true
|
|
|
|
m.afdo.Properties.AfdoDep = true
|
|
|
|
m.afdo.addDep(ctx, decodeTarget(variation))
|
2021-12-09 11:06:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode target name to variation name.
|
|
|
|
func encodeTarget(target string) string {
|
|
|
|
if target == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "afdo-" + target
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode target name from variation name.
|
|
|
|
func decodeTarget(variation string) string {
|
|
|
|
if variation == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.TrimPrefix(variation, "afdo-")
|
|
|
|
}
|