blob: c5e4e86fd277518b9b7fe0cb1acbacd6ec193fca [file] [log] [blame]
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001// Copyright 2017 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
21 "android/soong/android"
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070022 "android/soong/cc/config"
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070023)
24
25var (
26 // Add flags to ignore warnings that profiles are old or missing for
27 // some functions
Pirama Arumuga Nainarf4c0baf2017-09-28 14:35:15 -070028 profileUseOtherFlags = []string{"-Wno-backend-plugin"}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070029)
30
31const pgoProfileProject = "toolchain/pgo-profiles"
32
33const profileInstrumentFlag = "-fprofile-generate=/data/local/tmp"
34const profileSamplingFlag = "-gline-tables-only"
35const profileUseInstrumentFormat = "-fprofile-use=%s"
36const profileUseSamplingFormat = "-fprofile-sample-use=%s"
37
38type PgoProperties struct {
39 Pgo struct {
40 Instrumentation *bool
41 Sampling *bool
42 Profile_file *string `android:"arch_variant"`
43 Benchmarks []string
44 } `android:"arch_variant"`
45
46 PgoPresent bool `blueprint:"mutated"`
47 ShouldProfileModule bool `blueprint:"mutated"`
48}
49
50type pgo struct {
51 Properties PgoProperties
52}
53
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070054func (props *PgoProperties) isInstrumentation() bool {
55 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
56}
57
58func (props *PgoProperties) isSampling() bool {
59 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
60}
61
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070062func (pgo *pgo) props() []interface{} {
63 return []interface{}{&pgo.Properties}
64}
65
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070066func (props *PgoProperties) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
67 if props.isInstrumentation() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070068 flags.CFlags = append(flags.CFlags, profileInstrumentFlag)
69 // The profile runtime is added below in deps(). Add the below
70 // flag, which is the only other link-time action performed by
71 // the Clang driver during link.
72 flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070073 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070074 if props.isSampling() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070075 flags.CFlags = append(flags.CFlags, profileSamplingFlag)
76 flags.LdFlags = append(flags.LdFlags, profileSamplingFlag)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070077 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070078 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070079}
80
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070081func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
82 if props.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070083 return fmt.Sprintf(profileUseInstrumentFormat, file)
84 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070085 if props.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070086 return fmt.Sprintf(profileUseSamplingFormat, file)
87 }
88 return ""
89}
90
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070091func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string {
92 flags := []string{props.profileUseFlag(ctx, file)}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070093 flags = append(flags, profileUseOtherFlags...)
94 return flags
95}
96
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -070097func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
98 // If the PGO profiles project is found, and this module has PGO
99 // enabled, add flags to use the profile
100 if profilesDir := getPgoProfilesDir(ctx); props.PgoPresent && profilesDir.Valid() {
101 profileFile := android.PathForSource(ctx, profilesDir.String(), *props.Pgo.Profile_file)
102 profileUseFlags := props.profileUseFlags(ctx, profileFile.String())
103
104 flags.CFlags = append(flags.CFlags, profileUseFlags...)
105 flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
106
107 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
108 // if profileFile gets updated
109 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFile)
110 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFile)
111 }
112 return flags
113}
114
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700115func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700116 isInstrumentation := props.isInstrumentation()
117 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700118
119 profileKindPresent := isInstrumentation || isSampling
120 filePresent := props.Pgo.Profile_file != nil
121 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
122
123 // If all three properties are absent, PGO is OFF for this module
124 if !profileKindPresent && !filePresent && !benchmarksPresent {
125 return false
126 }
127
128 // If at least one property exists, validate that all properties exist
129 if !profileKindPresent || !filePresent || !benchmarksPresent {
130 var missing []string
131 if !profileKindPresent {
132 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
133 }
134 if !filePresent {
135 missing = append(missing, "profile_file property")
136 }
137 if !benchmarksPresent {
138 missing = append(missing, "non-empty benchmarks property")
139 }
140 missingProps := strings.Join(missing, ", ")
141 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
142 }
143
144 // Sampling not supported yet
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700145 if isSampling {
146 ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)")
147 }
148
Pirama Arumuga Nainar6fc8d912017-10-05 10:25:00 -0700149 if isSampling && isInstrumentation {
150 ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set")
151 }
152
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700153 return true
154}
155
156func getPgoProfilesDir(ctx ModuleContext) android.OptionalPath {
157 return android.ExistentPathForSource(ctx, "", pgoProfileProject)
158}
159
160func (pgo *pgo) begin(ctx BaseModuleContext) {
161 // TODO Evaluate if we need to support PGO for host modules
162 if ctx.Host() {
163 return
164 }
165
166 // Check if PGO is needed for this module
167 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
168
169 if !pgo.Properties.PgoPresent {
170 return
171 }
172
173 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
174 // and includes a benchmark listed for this module
175 //
176 // TODO Validate that each benchmark instruments at least one module
177 pgo.Properties.ShouldProfileModule = false
178 pgoBenchmarks := ctx.AConfig().Getenv("ANDROID_PGO_INSTRUMENT")
179 pgoBenchmarksMap := make(map[string]bool)
180 for _, b := range strings.Split(pgoBenchmarks, ",") {
181 pgoBenchmarksMap[b] = true
182 }
183
184 for _, b := range pgo.Properties.Pgo.Benchmarks {
185 if pgoBenchmarksMap[b] == true {
186 pgo.Properties.ShouldProfileModule = true
187 break
188 }
189 }
190}
191
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700192func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
193 if pgo.Properties.ShouldProfileModule {
194 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
195 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
196 }
197 return deps
198}
199
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700200func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
201 if ctx.Host() {
202 return flags
203 }
204
205 props := pgo.Properties
206
207 // Add flags to profile this module based on its profile_kind
208 if props.ShouldProfileModule {
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700209 return props.addProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700210 }
211
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700212 if !ctx.AConfig().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
213 return props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700214 }
215
216 return flags
217}