blob: d9a01184085c6ea359372cf4b8b874367bb4af28 [file] [log] [blame]
Stephen Craneba090d12017-05-09 15:44:35 -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 (
Liz Kammerb2fc4702021-06-25 14:53:40 -040018 "github.com/google/blueprint/proptools"
19
Stephen Craneba090d12017-05-09 15:44:35 -070020 "android/soong/android"
21)
22
23// LTO (link-time optimization) allows the compiler to optimize and generate
24// code for the entire module at link time, rather than per-compilation
25// unit. LTO is required for Clang CFI and other whole-program optimization
26// techniques. LTO also allows cross-compilation unit optimizations that should
27// result in faster and smaller code, at the expense of additional compilation
28// time.
29//
30// To properly build a module with LTO, the module and all recursive static
31// dependencies should be compiled with -flto which directs the compiler to emit
32// bitcode rather than native object files. These bitcode files are then passed
33// by the linker to the LLVM plugin for compilation at link time. Static
34// dependencies not built as bitcode will still function correctly but cannot be
35// optimized at link time and may not be compatible with features that require
36// LTO, such as CFI.
37//
38// This file adds support to soong to automatically propogate LTO options to a
39// new variant of all static dependencies for each module with LTO enabled.
40
41type LTOProperties struct {
42 // Lto must violate capitialization style for acronyms so that it can be
43 // referred to in blueprint files as "lto"
Yi Kong244bf072017-08-29 11:10:09 +080044 Lto struct {
Stephen Crane10cd1872017-09-27 17:01:15 -070045 Never *bool `android:"arch_variant"`
46 Full *bool `android:"arch_variant"`
47 Thin *bool `android:"arch_variant"`
Yi Kong244bf072017-08-29 11:10:09 +080048 } `android:"arch_variant"`
Stephen Crane10cd1872017-09-27 17:01:15 -070049
50 // Dep properties indicate that this module needs to be built with LTO
51 // since it is an object dependency of an LTO module.
52 FullDep bool `blueprint:"mutated"`
53 ThinDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070054
55 // Use clang lld instead of gnu ld.
56 Use_clang_lld *bool
Yi Kong2d01fe22020-09-21 01:18:32 +080057
58 // Use -fwhole-program-vtables cflag.
59 Whole_program_vtables *bool
Stephen Craneba090d12017-05-09 15:44:35 -070060}
61
62type lto struct {
63 Properties LTOProperties
64}
65
66func (lto *lto) props() []interface{} {
67 return []interface{}{&lto.Properties}
68}
69
70func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080071 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
Liz Kammerb2fc4702021-06-25 14:53:40 -040072 lto.Properties.Lto.Never = proptools.BoolPtr(true)
Yi Kong93718e02020-09-21 21:41:03 +080073 } else if ctx.Config().IsEnvTrue("GLOBAL_THINLTO") {
Yi Konge2577142020-09-29 08:44:45 +080074 staticLib := ctx.static() && !ctx.staticBinary()
75 hostBin := ctx.Host()
Yi Kong134161f2020-10-09 22:05:59 +080076 vndk := ctx.isVndk() // b/169217596
77 if !staticLib && !hostBin && !vndk {
Yi Konge2577142020-09-29 08:44:45 +080078 if !lto.Never() && !lto.FullLTO() {
Liz Kammerb2fc4702021-06-25 14:53:40 -040079 lto.Properties.Lto.Thin = proptools.BoolPtr(true)
Yi Konge2577142020-09-29 08:44:45 +080080 }
81 }
Yi Kong03d383d2018-01-31 15:15:08 -080082 }
Stephen Craneba090d12017-05-09 15:44:35 -070083}
84
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070085func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
86 if lto.Properties.Use_clang_lld != nil {
87 return Bool(lto.Properties.Use_clang_lld)
88 }
Dan Willemsenfa2aee12018-10-21 19:47:01 -070089 return true
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070090}
91
Stephen Craneba090d12017-05-09 15:44:35 -070092func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Mitch Phillips34b493f2019-08-02 16:57:55 -070093 // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
94 // LTO breaks fuzzer builds.
Colin Cross4af21ed2019-11-04 09:37:55 -080095 if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
Mitch Phillips34b493f2019-08-02 16:57:55 -070096 return flags
97 }
98
Yi Kong244bf072017-08-29 11:10:09 +080099 if lto.LTO() {
100 var ltoFlag string
Yi Kong93718e02020-09-21 21:41:03 +0800101 if lto.ThinLTO() {
Yi Kong6925d2b2019-03-05 14:11:41 -0800102 ltoFlag = "-flto=thin -fsplit-lto-unit"
Yi Kong244bf072017-08-29 11:10:09 +0800103 } else {
104 ltoFlag = "-flto"
105 }
106
Colin Cross4af21ed2019-11-04 09:37:55 -0800107 flags.Local.CFlags = append(flags.Local.CFlags, ltoFlag)
108 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +0800109
Yi Kong2d01fe22020-09-21 01:18:32 +0800110 if Bool(lto.Properties.Whole_program_vtables) {
111 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
112 }
113
Yi Kong93718e02020-09-21 21:41:03 +0800114 if lto.ThinLTO() && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) {
Yi Kong8aeaa712018-02-16 20:36:16 +0800115 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700116 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800117 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800118 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800119
120 // Limit the size of the ThinLTO cache to the lesser of 10% of available
121 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700122 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800123 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800124 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800125 }
126
Yi Kong2f5f16d2020-09-23 00:54:50 +0800127 // If the module does not have a profile, be conservative and limit cross TU inline
128 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
Yi Kong630b9602019-03-22 21:28:39 -0700129 if !ctx.isPgoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800130 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800131 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800132 }
Stephen Craneba090d12017-05-09 15:44:35 -0700133 }
134 return flags
135}
136
137// Can be called with a null receiver
138func (lto *lto) LTO() bool {
Yi Kongf43ff052020-09-28 14:41:50 +0800139 if lto == nil || lto.Never() {
Stephen Craneba090d12017-05-09 15:44:35 -0700140 return false
141 }
142
Yi Kong93718e02020-09-21 21:41:03 +0800143 return lto.FullLTO() || lto.ThinLTO()
144}
145
146func (lto *lto) FullLTO() bool {
147 return Bool(lto.Properties.Lto.Full)
148}
149
150func (lto *lto) ThinLTO() bool {
Yi Kong93718e02020-09-21 21:41:03 +0800151 return Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700152}
153
Stephen Crane10cd1872017-09-27 17:01:15 -0700154// Is lto.never explicitly set to true?
Yi Kongf43ff052020-09-28 14:41:50 +0800155func (lto *lto) Never() bool {
156 return Bool(lto.Properties.Lto.Never)
Stephen Crane10cd1872017-09-27 17:01:15 -0700157}
158
Stephen Craneba090d12017-05-09 15:44:35 -0700159// Propagate lto requirements down from binaries
160func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700161 if m, ok := mctx.Module().(*Module); ok && m.lto.LTO() {
Yi Kong93718e02020-09-21 21:41:03 +0800162 full := m.lto.FullLTO()
163 thin := m.lto.ThinLTO()
Yi Kong244bf072017-08-29 11:10:09 +0800164 if full && thin {
165 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
166 }
167
Stephen Crane10cd1872017-09-27 17:01:15 -0700168 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
169 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700170 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700171
172 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700173 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700174 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700175 return false
176 }
177 } else {
178 if tag != objDepTag && tag != reuseObjTag {
179 return false
180 }
181 }
182
183 if dep, ok := dep.(*Module); ok && dep.lto != nil &&
Yi Kongf43ff052020-09-28 14:41:50 +0800184 !dep.lto.Never() {
Yi Kong93718e02020-09-21 21:41:03 +0800185 if full && !dep.lto.FullLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700186 dep.lto.Properties.FullDep = true
187 }
Yi Kong93718e02020-09-21 21:41:03 +0800188 if thin && !dep.lto.ThinLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700189 dep.lto.Properties.ThinDep = true
190 }
191 }
192
193 // Recursively walk static dependencies
194 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700195 })
196 }
197}
198
199// Create lto variants for modules that need them
200func ltoMutator(mctx android.BottomUpMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700201 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
202 // Create variations for LTO types required as static
203 // dependencies
204 variationNames := []string{""}
Yi Kong93718e02020-09-21 21:41:03 +0800205 if m.lto.Properties.FullDep && !m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700206 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700207 }
Yi Kong93718e02020-09-21 21:41:03 +0800208 if m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700209 variationNames = append(variationNames, "lto-thin")
210 }
211
212 // Use correct dependencies if LTO property is explicitly set
213 // (mutually exclusive)
Yi Kong93718e02020-09-21 21:41:03 +0800214 if m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700215 mctx.SetDependencyVariation("lto-full")
216 }
Yi Kong93718e02020-09-21 21:41:03 +0800217 if m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700218 mctx.SetDependencyVariation("lto-thin")
219 }
220
221 if len(variationNames) > 1 {
222 modules := mctx.CreateVariations(variationNames...)
223 for i, name := range variationNames {
224 variation := modules[i].(*Module)
225 // Default module which will be
226 // installed. Variation set above according to
227 // explicit LTO properties
228 if name == "" {
229 continue
230 }
231
232 // LTO properties for dependencies
233 if name == "lto-full" {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400234 variation.lto.Properties.Lto.Full = proptools.BoolPtr(true)
235 variation.lto.Properties.Lto.Thin = proptools.BoolPtr(false)
Stephen Crane10cd1872017-09-27 17:01:15 -0700236 }
237 if name == "lto-thin" {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400238 variation.lto.Properties.Lto.Full = proptools.BoolPtr(false)
239 variation.lto.Properties.Lto.Thin = proptools.BoolPtr(true)
Stephen Crane10cd1872017-09-27 17:01:15 -0700240 }
241 variation.Properties.PreventInstall = true
242 variation.Properties.HideFromMake = true
243 variation.lto.Properties.FullDep = false
244 variation.lto.Properties.ThinDep = false
245 }
246 }
Stephen Craneba090d12017-05-09 15:44:35 -0700247 }
248}