Revert "Remove fdoProfileMutator"
Revert submission 2925209
Reason for revert: b/323975183
Reverted changes: /q/submissionid:2925209
Change-Id: I33b2e71ba7b36e12e5c491e0ad14a7f7df3edd78
diff --git a/cc/afdo.go b/cc/afdo.go
index 0de1d4b..6155084 100644
--- a/cc/afdo.go
+++ b/cc/afdo.go
@@ -21,6 +21,7 @@
"android/soong/android"
"github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
)
// This flag needs to be in both CFlags and LdFlags to ensure correct symbol ordering
@@ -31,7 +32,7 @@
// automatic feedback-directed optimization using profile data.
Afdo bool
- AfdoDep bool `blueprint:"mutated"`
+ FdoProfilePath *string `blueprint:"mutated"`
}
type afdo struct {
@@ -56,7 +57,7 @@
}
func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
- if afdo.Properties.Afdo || afdo.Properties.AfdoDep {
+ if afdo.Properties.Afdo {
// 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
@@ -74,39 +75,61 @@
// 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...)
}
- fdoProfileDeps := ctx.GetDirectDepsWithTag(FdoProfileTag)
- if len(fdoProfileDeps) > 0 && fdoProfileDeps[0] != nil {
- if info, ok := android.OtherModuleProvider(ctx, fdoProfileDeps[0], FdoProfileProvider); ok {
- fdoProfilePath := info.Path.String()
+ if path := afdo.Properties.FdoProfilePath; path != nil {
+ // The flags are prepended to allow overriding.
+ profileUseFlag := fmt.Sprintf(afdoFlagsFormat, *path)
+ 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...)
- // The flags are prepended to allow overriding.
- profileUseFlag := fmt.Sprintf(afdoFlagsFormat, fdoProfilePath)
- 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
- pathForSrc := android.PathForSource(ctx, fdoProfilePath)
- flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc)
- flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc)
- }
+ // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
+ // if profileFile gets updated
+ pathForSrc := android.PathForSource(ctx, *path)
+ flags.CFlagsDeps = append(flags.CFlagsDeps, pathForSrc)
+ flags.LdFlagsDeps = append(flags.LdFlagsDeps, pathForSrc)
}
return flags
}
-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)
+// FdoProfileMutator reads the FdoProfileProvider from a direct dep with FdoProfileTag
+// assigns FdoProfileInfo.Path to the FdoProfilePath mutated property
+func (c *Module) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
+ if !c.Enabled() {
+ return
+ }
+
+ if !c.afdo.afdoEnabled() {
+ return
+ }
+
+ if c.Host() {
+ return
+ }
+
+ if c.static() && !c.staticBinary() {
+ return
+ }
+
+ if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
+ if fdoProfileName, err := ctx.DeviceConfig().AfdoProfile(ctx.ModuleName()); fdoProfileName != "" && err == nil {
+ deps := ctx.AddFarVariationDependencies(
+ []blueprint.Variation{
+ {Mutator: "arch", Variation: ctx.Target().ArchVariation()},
+ {Mutator: "os", Variation: "android"},
+ },
+ FdoProfileTag,
+ fdoProfileName)
+ if len(deps) > 0 && deps[0] != nil {
+ if info, ok := android.OtherModuleProvider(ctx, deps[0], FdoProfileProvider); ok {
+ c.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String())
+ }
+ }
+ }
}
}
+var _ FdoProfileMutatorInterface = (*Module)(nil)
+
func afdoPropagateViaDepTag(tag blueprint.DependencyTag) bool {
libTag, isLibTag := tag.(libraryDependencyTag)
// Do not recurse down non-static dependencies
@@ -149,16 +172,27 @@
}
func (a *afdoTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
+ if variation == "" {
+ return
+ }
+
if m, ok := ctx.Module().(*Module); ok && m.afdo != nil {
- if variation == "" {
- if m.afdo.afdoEnabled() && !(m.static() && !m.staticBinary()) && !m.Host() {
- m.afdo.addDep(ctx, ctx.ModuleName())
+ m.Properties.PreventInstall = true
+ m.Properties.HideFromMake = true
+ m.afdo.Properties.Afdo = true
+ if fdoProfileName, err := ctx.DeviceConfig().AfdoProfile(decodeTarget(variation)); fdoProfileName != "" && err == nil {
+ deps := ctx.AddFarVariationDependencies(
+ []blueprint.Variation{
+ {Mutator: "arch", Variation: ctx.Target().ArchVariation()},
+ {Mutator: "os", Variation: "android"},
+ },
+ FdoProfileTag,
+ fdoProfileName)
+ if len(deps) > 0 && deps[0] != nil {
+ if info, ok := android.OtherModuleProvider(ctx, deps[0], FdoProfileProvider); ok {
+ m.afdo.Properties.FdoProfilePath = proptools.StringPtr(info.Path.String())
+ }
}
- } else {
- m.Properties.PreventInstall = true
- m.Properties.HideFromMake = true
- m.afdo.Properties.AfdoDep = true
- m.afdo.addDep(ctx, decodeTarget(variation))
}
}
}
diff --git a/cc/cc.go b/cc/cc.go
index 480d314..cac52cf 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -55,6 +55,7 @@
ctx.BottomUp("test_per_src", TestPerSrcMutator).Parallel()
ctx.BottomUp("version", versionMutator).Parallel()
ctx.BottomUp("begin", BeginMutator).Parallel()
+ ctx.BottomUp("fdo_profile", fdoProfileMutator).Parallel()
})
ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
@@ -1381,7 +1382,7 @@
func (c *Module) isAfdoCompile() bool {
if afdo := c.afdo; afdo != nil {
- return afdo.Properties.Afdo || afdo.Properties.AfdoDep
+ return afdo.Properties.FdoProfilePath != nil
}
return false
}
diff --git a/cc/fdo_profile.go b/cc/fdo_profile.go
index 1a33957..0893da5 100644
--- a/cc/fdo_profile.go
+++ b/cc/fdo_profile.go
@@ -43,10 +43,23 @@
}
// FdoProfileProvider is used to provide path to an fdo profile
-var FdoProfileProvider = blueprint.NewProvider[FdoProfileInfo]()
+var FdoProfileProvider = blueprint.NewMutatorProvider[FdoProfileInfo]("fdo_profile")
+
+// FdoProfileMutatorInterface is the interface implemented by fdo_profile module type
+// module types that can depend on an fdo_profile module
+type FdoProfileMutatorInterface interface {
+ // FdoProfileMutator eithers set or get FdoProfileProvider
+ fdoProfileMutator(ctx android.BottomUpMutatorContext)
+}
+
+var _ FdoProfileMutatorInterface = (*fdoProfile)(nil)
// GenerateAndroidBuildActions of fdo_profile does not have any build actions
-func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
+
+// FdoProfileMutator sets FdoProfileProvider to fdo_profile module
+// or sets afdo.Properties.FdoProfilePath to path in FdoProfileProvider of the depended fdo_profile
+func (fp *fdoProfile) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
if fp.properties.Profile != nil {
path := android.PathForModuleSrc(ctx, *fp.properties.Profile)
android.SetProvider(ctx, FdoProfileProvider, FdoProfileInfo{
@@ -55,6 +68,14 @@
}
}
+// fdoProfileMutator calls the generic fdoProfileMutator function of fdoProfileMutator
+// which is implemented by cc and cc.FdoProfile
+func fdoProfileMutator(ctx android.BottomUpMutatorContext) {
+ if f, ok := ctx.Module().(FdoProfileMutatorInterface); ok {
+ f.fdoProfileMutator(ctx)
+ }
+}
+
func FdoProfileFactory() android.Module {
m := &fdoProfile{}
m.AddProperties(&m.properties)