blob: 9fea15420ae43b521aea7adaa92a23efbd3287e8 [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 {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070040 Instrumentation *bool
41 Sampling *bool
42 Profile_file *string `android:"arch_variant"`
43 Benchmarks []string
44 Enable_profile_use *bool `android:"arch_variant"`
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070045 } `android:"arch_variant"`
46
47 PgoPresent bool `blueprint:"mutated"`
48 ShouldProfileModule bool `blueprint:"mutated"`
49}
50
51type pgo struct {
52 Properties PgoProperties
53}
54
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070055func (props *PgoProperties) isInstrumentation() bool {
56 return props.Pgo.Instrumentation != nil && *props.Pgo.Instrumentation == true
57}
58
59func (props *PgoProperties) isSampling() bool {
60 return props.Pgo.Sampling != nil && *props.Pgo.Sampling == true
61}
62
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070063func (pgo *pgo) props() []interface{} {
64 return []interface{}{&pgo.Properties}
65}
66
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070067func (props *PgoProperties) addProfileGatherFlags(ctx ModuleContext, flags Flags) Flags {
68 if props.isInstrumentation() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070069 flags.CFlags = append(flags.CFlags, profileInstrumentFlag)
70 // The profile runtime is added below in deps(). Add the below
71 // flag, which is the only other link-time action performed by
72 // the Clang driver during link.
73 flags.LdFlags = append(flags.LdFlags, "-u__llvm_profile_runtime")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070074 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070075 if props.isSampling() {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070076 flags.CFlags = append(flags.CFlags, profileSamplingFlag)
77 flags.LdFlags = append(flags.LdFlags, profileSamplingFlag)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070078 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -070079 return flags
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070080}
81
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070082func (props *PgoProperties) profileUseFlag(ctx ModuleContext, file string) string {
83 if props.isInstrumentation() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070084 return fmt.Sprintf(profileUseInstrumentFormat, file)
85 }
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070086 if props.isSampling() {
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070087 return fmt.Sprintf(profileUseSamplingFormat, file)
88 }
89 return ""
90}
91
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -070092func (props *PgoProperties) profileUseFlags(ctx ModuleContext, file string) []string {
93 flags := []string{props.profileUseFlag(ctx, file)}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -070094 flags = append(flags, profileUseOtherFlags...)
95 return flags
96}
97
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -070098func (props *PgoProperties) addProfileUseFlags(ctx ModuleContext, flags Flags) Flags {
Pirama Arumuga Nainar6aeed8b2017-10-16 13:31:40 -070099 // Skip -fprofile-use if 'enable_profile_use' property is set
100 if props.Pgo.Enable_profile_use != nil && *props.Pgo.Enable_profile_use == false {
101 return flags
102 }
103
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700104 // If the PGO profiles project is found, and this module has PGO
105 // enabled, add flags to use the profile
106 if profilesDir := getPgoProfilesDir(ctx); props.PgoPresent && profilesDir.Valid() {
107 profileFile := android.PathForSource(ctx, profilesDir.String(), *props.Pgo.Profile_file)
108 profileUseFlags := props.profileUseFlags(ctx, profileFile.String())
109
110 flags.CFlags = append(flags.CFlags, profileUseFlags...)
111 flags.LdFlags = append(flags.LdFlags, profileUseFlags...)
112
113 // Update CFlagsDeps and LdFlagsDeps so the module is rebuilt
114 // if profileFile gets updated
115 flags.CFlagsDeps = append(flags.CFlagsDeps, profileFile)
116 flags.LdFlagsDeps = append(flags.LdFlagsDeps, profileFile)
117 }
118 return flags
119}
120
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700121func (props *PgoProperties) isPGO(ctx BaseModuleContext) bool {
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700122 isInstrumentation := props.isInstrumentation()
123 isSampling := props.isSampling()
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700124
125 profileKindPresent := isInstrumentation || isSampling
126 filePresent := props.Pgo.Profile_file != nil
127 benchmarksPresent := len(props.Pgo.Benchmarks) > 0
128
129 // If all three properties are absent, PGO is OFF for this module
130 if !profileKindPresent && !filePresent && !benchmarksPresent {
131 return false
132 }
133
134 // If at least one property exists, validate that all properties exist
135 if !profileKindPresent || !filePresent || !benchmarksPresent {
136 var missing []string
137 if !profileKindPresent {
138 missing = append(missing, "profile kind (either \"instrumentation\" or \"sampling\" property)")
139 }
140 if !filePresent {
141 missing = append(missing, "profile_file property")
142 }
143 if !benchmarksPresent {
144 missing = append(missing, "non-empty benchmarks property")
145 }
146 missingProps := strings.Join(missing, ", ")
147 ctx.ModuleErrorf("PGO specification is missing properties: " + missingProps)
148 }
149
150 // Sampling not supported yet
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700151 if isSampling {
152 ctx.PropertyErrorf("pgo.sampling", "\"sampling\" is not supported yet)")
153 }
154
Pirama Arumuga Nainar6fc8d912017-10-05 10:25:00 -0700155 if isSampling && isInstrumentation {
156 ctx.PropertyErrorf("pgo", "Exactly one of \"instrumentation\" and \"sampling\" properties must be set")
157 }
158
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700159 return true
160}
161
162func getPgoProfilesDir(ctx ModuleContext) android.OptionalPath {
163 return android.ExistentPathForSource(ctx, "", pgoProfileProject)
164}
165
166func (pgo *pgo) begin(ctx BaseModuleContext) {
167 // TODO Evaluate if we need to support PGO for host modules
168 if ctx.Host() {
169 return
170 }
171
172 // Check if PGO is needed for this module
173 pgo.Properties.PgoPresent = pgo.Properties.isPGO(ctx)
174
175 if !pgo.Properties.PgoPresent {
176 return
177 }
178
179 // This module should be instrumented if ANDROID_PGO_INSTRUMENT is set
180 // and includes a benchmark listed for this module
181 //
182 // TODO Validate that each benchmark instruments at least one module
183 pgo.Properties.ShouldProfileModule = false
Colin Cross6510f912017-11-29 00:27:14 -0800184 pgoBenchmarks := ctx.Config().Getenv("ANDROID_PGO_INSTRUMENT")
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700185 pgoBenchmarksMap := make(map[string]bool)
186 for _, b := range strings.Split(pgoBenchmarks, ",") {
187 pgoBenchmarksMap[b] = true
188 }
189
190 for _, b := range pgo.Properties.Pgo.Benchmarks {
191 if pgoBenchmarksMap[b] == true {
192 pgo.Properties.ShouldProfileModule = true
193 break
194 }
195 }
196}
197
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700198func (pgo *pgo) deps(ctx BaseModuleContext, deps Deps) Deps {
199 if pgo.Properties.ShouldProfileModule {
200 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
201 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
202 }
203 return deps
204}
205
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700206func (pgo *pgo) flags(ctx ModuleContext, flags Flags) Flags {
207 if ctx.Host() {
208 return flags
209 }
210
211 props := pgo.Properties
212
213 // Add flags to profile this module based on its profile_kind
214 if props.ShouldProfileModule {
Pirama Arumuga Nainar3f5bb9c2017-10-10 10:47:41 -0700215 return props.addProfileGatherFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700216 }
217
Colin Cross6510f912017-11-29 00:27:14 -0800218 if !ctx.Config().IsEnvTrue("ANDROID_PGO_NO_PROFILE_USE") {
Pirama Arumuga Nainar0fdfc452017-10-10 11:00:18 -0700219 return props.addProfileUseFlags(ctx, flags)
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700220 }
221
222 return flags
223}