blob: 1da69bf1604191e158aa59e554d6fc4de92fd846 [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.
Yi Kong8ea56f92021-10-14 01:07:42 +080052 FullDep bool `blueprint:"mutated"`
53 ThinDep bool `blueprint:"mutated"`
54 NoLtoDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070055
56 // Use clang lld instead of gnu ld.
57 Use_clang_lld *bool
Yi Kong2d01fe22020-09-21 01:18:32 +080058
59 // Use -fwhole-program-vtables cflag.
60 Whole_program_vtables *bool
Stephen Craneba090d12017-05-09 15:44:35 -070061}
62
63type lto struct {
64 Properties LTOProperties
65}
66
67func (lto *lto) props() []interface{} {
68 return []interface{}{&lto.Properties}
69}
70
71func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080072 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
Liz Kammerb2fc4702021-06-25 14:53:40 -040073 lto.Properties.Lto.Never = proptools.BoolPtr(true)
Yi Kong03d383d2018-01-31 15:15:08 -080074 }
Stephen Craneba090d12017-05-09 15:44:35 -070075}
76
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070077func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
78 if lto.Properties.Use_clang_lld != nil {
79 return Bool(lto.Properties.Use_clang_lld)
80 }
Dan Willemsenfa2aee12018-10-21 19:47:01 -070081 return true
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070082}
83
Stephen Craneba090d12017-05-09 15:44:35 -070084func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Mitch Phillips5007c4a2022-03-02 01:25:22 +000085 // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
86 // LTO breaks fuzzer builds.
87 if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
88 return flags
89 }
90
Yi Kong8ea56f92021-10-14 01:07:42 +080091 if lto.LTO(ctx) {
92 var ltoCFlag string
93 var ltoLdFlag string
Yi Kong93718e02020-09-21 21:41:03 +080094 if lto.ThinLTO() {
Yi Kong8ea56f92021-10-14 01:07:42 +080095 ltoCFlag = "-flto=thin -fsplit-lto-unit"
96 } else if lto.FullLTO() {
97 ltoCFlag = "-flto"
Yi Kong244bf072017-08-29 11:10:09 +080098 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +080099 ltoCFlag = "-flto=thin -fsplit-lto-unit"
100 ltoLdFlag = "-Wl,--lto-O0"
Yi Kong244bf072017-08-29 11:10:09 +0800101 }
102
Yi Kong8ea56f92021-10-14 01:07:42 +0800103 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
104 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
105 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +0800106
Yi Kong2d01fe22020-09-21 01:18:32 +0800107 if Bool(lto.Properties.Whole_program_vtables) {
108 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
109 }
110
Yi Kong8ea56f92021-10-14 01:07:42 +0800111 if (lto.DefaultThinLTO(ctx) || lto.ThinLTO()) && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) {
Yi Kong8aeaa712018-02-16 20:36:16 +0800112 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700113 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800114 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800115 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800116
117 // Limit the size of the ThinLTO cache to the lesser of 10% of available
118 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700119 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800120 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800121 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800122 }
123
Yi Kong2f5f16d2020-09-23 00:54:50 +0800124 // If the module does not have a profile, be conservative and limit cross TU inline
125 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
Yi Kong4ef54592022-02-14 20:00:10 +0800126 if !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800127 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800128 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800129 }
Stephen Craneba090d12017-05-09 15:44:35 -0700130 }
131 return flags
132}
133
Yi Kong8ea56f92021-10-14 01:07:42 +0800134func (lto *lto) LTO(ctx BaseModuleContext) bool {
135 return lto.ThinLTO() || lto.FullLTO() || lto.DefaultThinLTO(ctx)
136}
Stephen Craneba090d12017-05-09 15:44:35 -0700137
Yi Kong8ea56f92021-10-14 01:07:42 +0800138func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool {
Yi Kong2121d162022-08-17 03:54:54 +0800139 lib32 := ctx.Arch().ArchType.Multilib == "lib32"
Yi Kong8ea56f92021-10-14 01:07:42 +0800140 host := ctx.Host()
141 vndk := ctx.isVndk() // b/169217596
Yi Kong2121d162022-08-17 03:54:54 +0800142 return GlobalThinLTO(ctx) && !lto.Never() && !lib32 && !host && !vndk
Yi Kong93718e02020-09-21 21:41:03 +0800143}
144
145func (lto *lto) FullLTO() bool {
Yi Kong8ea56f92021-10-14 01:07:42 +0800146 return lto != nil && Bool(lto.Properties.Lto.Full)
Yi Kong93718e02020-09-21 21:41:03 +0800147}
148
149func (lto *lto) ThinLTO() bool {
Yi Kong8ea56f92021-10-14 01:07:42 +0800150 return lto != nil && Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700151}
152
Yi Kongf43ff052020-09-28 14:41:50 +0800153func (lto *lto) Never() bool {
Yi Kong8ea56f92021-10-14 01:07:42 +0800154 return lto != nil && Bool(lto.Properties.Lto.Never)
155}
156
157func GlobalThinLTO(ctx android.BaseModuleContext) bool {
158 return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
Stephen Crane10cd1872017-09-27 17:01:15 -0700159}
160
Stephen Craneba090d12017-05-09 15:44:35 -0700161// Propagate lto requirements down from binaries
162func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800163 globalThinLTO := GlobalThinLTO(mctx)
164
165 if m, ok := mctx.Module().(*Module); ok {
Yi Kong93718e02020-09-21 21:41:03 +0800166 full := m.lto.FullLTO()
167 thin := m.lto.ThinLTO()
Yi Kong8ea56f92021-10-14 01:07:42 +0800168 never := m.lto.Never()
Yi Kong244bf072017-08-29 11:10:09 +0800169 if full && thin {
170 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
171 }
172
Stephen Crane10cd1872017-09-27 17:01:15 -0700173 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
174 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700175 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700176
177 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700178 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700179 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700180 return false
181 }
182 } else {
183 if tag != objDepTag && tag != reuseObjTag {
184 return false
185 }
186 }
187
Yi Kong8ea56f92021-10-14 01:07:42 +0800188 if dep, ok := dep.(*Module); ok {
Yi Kong93718e02020-09-21 21:41:03 +0800189 if full && !dep.lto.FullLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700190 dep.lto.Properties.FullDep = true
191 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800192 if !globalThinLTO && thin && !dep.lto.ThinLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700193 dep.lto.Properties.ThinDep = true
194 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800195 if globalThinLTO && never && !dep.lto.Never() {
196 dep.lto.Properties.NoLtoDep = true
197 }
Colin Cross6e511a92020-07-27 21:26:48 -0700198 }
199
200 // Recursively walk static dependencies
201 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700202 })
203 }
204}
205
206// Create lto variants for modules that need them
207func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800208 globalThinLTO := GlobalThinLTO(mctx)
209
Stephen Crane10cd1872017-09-27 17:01:15 -0700210 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
211 // Create variations for LTO types required as static
212 // dependencies
213 variationNames := []string{""}
Yi Kong93718e02020-09-21 21:41:03 +0800214 if m.lto.Properties.FullDep && !m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700215 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700216 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800217 if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700218 variationNames = append(variationNames, "lto-thin")
219 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800220 if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() {
221 variationNames = append(variationNames, "lto-none")
222 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700223
224 // Use correct dependencies if LTO property is explicitly set
225 // (mutually exclusive)
Yi Kong93718e02020-09-21 21:41:03 +0800226 if m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700227 mctx.SetDependencyVariation("lto-full")
228 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800229 if !globalThinLTO && m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700230 mctx.SetDependencyVariation("lto-thin")
231 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800232 // Never must be the last, it overrides Thin or Full.
233 if globalThinLTO && m.lto.Never() {
234 mctx.SetDependencyVariation("lto-none")
235 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700236
237 if len(variationNames) > 1 {
238 modules := mctx.CreateVariations(variationNames...)
239 for i, name := range variationNames {
240 variation := modules[i].(*Module)
241 // Default module which will be
242 // installed. Variation set above according to
243 // explicit LTO properties
244 if name == "" {
245 continue
246 }
247
248 // LTO properties for dependencies
249 if name == "lto-full" {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400250 variation.lto.Properties.Lto.Full = proptools.BoolPtr(true)
Stephen Crane10cd1872017-09-27 17:01:15 -0700251 }
252 if name == "lto-thin" {
Liz Kammerb2fc4702021-06-25 14:53:40 -0400253 variation.lto.Properties.Lto.Thin = proptools.BoolPtr(true)
Stephen Crane10cd1872017-09-27 17:01:15 -0700254 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800255 if name == "lto-none" {
256 variation.lto.Properties.Lto.Never = proptools.BoolPtr(true)
257 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700258 variation.Properties.PreventInstall = true
259 variation.Properties.HideFromMake = true
260 variation.lto.Properties.FullDep = false
261 variation.lto.Properties.ThinDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800262 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700263 }
264 }
Stephen Craneba090d12017-05-09 15:44:35 -0700265 }
266}