summaryrefslogtreecommitdiff
path: root/cc/afdo.go
blob: 022f2833bee8b38164e919bc99cee9664692457e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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"

	"github.com/google/blueprint/proptools"

	"android/soong/android"
)

var (
	globalAfdoProfileProjects = []string{
		"vendor/google_data/pgo_profile/sampling/",
		"toolchain/pgo-profiles/sampling/",
	}
)

var afdoProfileProjectsConfigKey = android.NewOnceKey("AfdoProfileProjects")

const afdoCFlagsFormat = "-fprofile-sample-accurate -fprofile-sample-use=%s"

func getAfdoProfileProjects(config android.DeviceConfig) []string {
	return config.OnceStringSlice(afdoProfileProjectsConfigKey, func() []string {
		return append(globalAfdoProfileProjects, config.AfdoAdditionalProfileDirs()...)
	})
}

func recordMissingAfdoProfileFile(ctx BaseModuleContext, missing string) {
	getNamedMapForConfig(ctx.Config(), modulesMissingProfileFileKey).Store(missing, true)
}

type AfdoProperties struct {
	Afdo bool

	AfdoTarget *string  `blueprint:"mutated"`
	AfdoDeps   []string `blueprint:"mutated"`
}

type afdo struct {
	Properties AfdoProperties
}

func (afdo *afdo) props() []interface{} {
	return []interface{}{&afdo.Properties}
}

func (afdo *afdo) AfdoEnabled() bool {
	return afdo != nil && afdo.Properties.Afdo && afdo.Properties.AfdoTarget != nil
}

// Get list of profile file names, ordered by level of specialisation. For example:
//   1. libfoo_arm64.afdo
//   2. libfoo.afdo
// Add more specialisation as needed.
func getProfileFiles(ctx BaseModuleContext, moduleName string) []string {
	var files []string
	files = append(files, moduleName+"_"+ctx.Arch().ArchType.String()+".afdo")
	files = append(files, moduleName+".afdo")
	return files
}

func (props *AfdoProperties) getAfdoProfileFile(ctx BaseModuleContext, module string) android.OptionalPath {
	// Test if the profile_file is present in any of the Afdo profile projects
	for _, profileFile := range getProfileFiles(ctx, module) {
		for _, profileProject := range getAfdoProfileProjects(ctx.DeviceConfig()) {
			path := android.ExistentPathForSource(ctx, profileProject, profileFile)
			if path.Valid() {
				return path
			}
		}
	}

	// Record that this module's profile file is absent
	missing := ctx.ModuleDir() + ":" + module
	recordMissingAfdoProfileFile(ctx, missing)

	return android.OptionalPathForPath(nil)
}

func (afdo *afdo) begin(ctx BaseModuleContext) {
	if afdo.Properties.Afdo && !ctx.static() && !ctx.Host() {
		module := ctx.ModuleName()
		if afdo.Properties.getAfdoProfileFile(ctx, module).Valid() {
			afdo.Properties.AfdoTarget = proptools.StringPtr(module)
		}
	}
}

func (afdo *afdo) flags(ctx ModuleContext, flags Flags) Flags {
	if profile := afdo.Properties.AfdoTarget; profile != nil {
		if profileFile := afdo.Properties.getAfdoProfileFile(ctx, *profile); profileFile.Valid() {
			profileFilePath := profileFile.Path()

			profileUseFlag := fmt.Sprintf(afdoCFlagsFormat, profileFile)
			flags.Local.CFlags = append(flags.Local.CFlags, profileUseFlag)
			flags.Local.LdFlags = append(flags.Local.LdFlags, profileUseFlag)
			flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm,-no-warn-sample-unused=true")

			// Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
			// if profileFile gets updated
			flags.CFlagsDeps = append(flags.CFlagsDeps, profileFilePath)
			flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFilePath)
		}
	}

	return flags
}

// Propagate afdo requirements down from binaries
func afdoDepsMutator(mctx android.TopDownMutatorContext) {
	if m, ok := mctx.Module().(*Module); ok && m.afdo.AfdoEnabled() {
		afdoTarget := *m.afdo.Properties.AfdoTarget
		mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
			tag := mctx.OtherModuleDependencyTag(dep)
			libTag, isLibTag := tag.(libraryDependencyTag)

			// Do not recurse down non-static dependencies
			if isLibTag {
				if !libTag.static() {
					return false
				}
			} else {
				if tag != objDepTag && tag != reuseObjTag {
					return false
				}
			}

			if dep, ok := dep.(*Module); ok {
				dep.afdo.Properties.AfdoDeps = append(dep.afdo.Properties.AfdoDeps, afdoTarget)
			}

			return true
		})
	}
}

// Create afdo variants for modules that need them
func afdoMutator(mctx android.BottomUpMutatorContext) {
	if m, ok := mctx.Module().(*Module); ok && m.afdo != nil {
		if m.afdo.AfdoEnabled() && !m.static() {
			afdoTarget := *m.afdo.Properties.AfdoTarget
			mctx.SetDependencyVariation(encodeTarget(afdoTarget))
		}

		variationNames := []string{""}
		afdoDeps := android.FirstUniqueStrings(m.afdo.Properties.AfdoDeps)
		for _, dep := range afdoDeps {
			variationNames = append(variationNames, encodeTarget(dep))
		}
		if len(variationNames) > 1 {
			modules := mctx.CreateVariations(variationNames...)
			for i, name := range variationNames {
				if name == "" {
					continue
				}
				variation := modules[i].(*Module)
				variation.Properties.PreventInstall = true
				variation.Properties.HideFromMake = true
				variation.afdo.Properties.AfdoTarget = proptools.StringPtr(decodeTarget(name))
			}
		}
	}
}

// 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-")
}