blob: 581856b46e7e786ee4cd69acedd8c38fbcb0f21a [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 (
Stephen Craneba090d12017-05-09 15:44:35 -070018 "android/soong/android"
Liz Kammer729aaf42022-09-16 12:39:42 -040019
20 "github.com/google/blueprint/proptools"
Stephen Craneba090d12017-05-09 15:44:35 -070021)
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.
Liz Kammer729aaf42022-09-16 12:39:42 -040052 FullEnabled bool `blueprint:"mutated"`
53 ThinEnabled bool `blueprint:"mutated"`
54 NoLtoEnabled bool `blueprint:"mutated"`
55 FullDep bool `blueprint:"mutated"`
56 ThinDep bool `blueprint:"mutated"`
57 NoLtoDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070058
59 // Use clang lld instead of gnu ld.
60 Use_clang_lld *bool
Yi Kong2d01fe22020-09-21 01:18:32 +080061
62 // Use -fwhole-program-vtables cflag.
63 Whole_program_vtables *bool
Stephen Craneba090d12017-05-09 15:44:35 -070064}
65
66type lto struct {
67 Properties LTOProperties
68}
69
70func (lto *lto) props() []interface{} {
71 return []interface{}{&lto.Properties}
72}
73
74func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080075 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
Liz Kammer729aaf42022-09-16 12:39:42 -040076 lto.Properties.NoLtoEnabled = true
Yi Kong03d383d2018-01-31 15:15:08 -080077 }
Stephen Craneba090d12017-05-09 15:44:35 -070078}
79
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070080func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
81 if lto.Properties.Use_clang_lld != nil {
82 return Bool(lto.Properties.Use_clang_lld)
83 }
Dan Willemsenfa2aee12018-10-21 19:47:01 -070084 return true
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070085}
86
Stephen Craneba090d12017-05-09 15:44:35 -070087func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Mitch Phillips5007c4a2022-03-02 01:25:22 +000088 // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
89 // LTO breaks fuzzer builds.
90 if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
91 return flags
92 }
93
Yi Kong8ea56f92021-10-14 01:07:42 +080094 if lto.LTO(ctx) {
95 var ltoCFlag string
96 var ltoLdFlag string
Yi Kong93718e02020-09-21 21:41:03 +080097 if lto.ThinLTO() {
Yi Kong8ea56f92021-10-14 01:07:42 +080098 ltoCFlag = "-flto=thin -fsplit-lto-unit"
99 } else if lto.FullLTO() {
100 ltoCFlag = "-flto"
Yi Kong244bf072017-08-29 11:10:09 +0800101 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800102 ltoCFlag = "-flto=thin -fsplit-lto-unit"
103 ltoLdFlag = "-Wl,--lto-O0"
Yi Kong244bf072017-08-29 11:10:09 +0800104 }
105
Yi Kong8ea56f92021-10-14 01:07:42 +0800106 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
Yi Kong5e0f4052022-09-09 01:58:18 +0800107 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag)
Yi Kong8ea56f92021-10-14 01:07:42 +0800108 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
109 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +0800110
Yi Kong2d01fe22020-09-21 01:18:32 +0800111 if Bool(lto.Properties.Whole_program_vtables) {
112 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
113 }
114
Yi Kong8ea56f92021-10-14 01:07:42 +0800115 if (lto.DefaultThinLTO(ctx) || lto.ThinLTO()) && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) {
Yi Kong8aeaa712018-02-16 20:36:16 +0800116 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700117 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800118 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800119 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800120
121 // Limit the size of the ThinLTO cache to the lesser of 10% of available
122 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700123 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800124 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800125 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800126 }
127
Yi Kong2f5f16d2020-09-23 00:54:50 +0800128 // If the module does not have a profile, be conservative and limit cross TU inline
129 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
Yi Kong4ef54592022-02-14 20:00:10 +0800130 if !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800131 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800132 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800133 }
Stephen Craneba090d12017-05-09 15:44:35 -0700134 }
135 return flags
136}
137
Yi Kong8ea56f92021-10-14 01:07:42 +0800138func (lto *lto) LTO(ctx BaseModuleContext) bool {
139 return lto.ThinLTO() || lto.FullLTO() || lto.DefaultThinLTO(ctx)
140}
Stephen Craneba090d12017-05-09 15:44:35 -0700141
Yi Kong8ea56f92021-10-14 01:07:42 +0800142func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool {
Yi Kongc702ebd2022-08-19 16:02:45 +0800143 // LP32 has many subtle issues and less test coverage.
Yi Kong2121d162022-08-17 03:54:54 +0800144 lib32 := ctx.Arch().ArchType.Multilib == "lib32"
Yi Kongc702ebd2022-08-19 16:02:45 +0800145 // CFI enables full LTO.
146 cfi := ctx.isCfi()
Yi Kong56fc1b62022-09-06 16:24:00 +0800147 // Performance and binary size are less important for host binaries and tests.
Yi Kong8ea56f92021-10-14 01:07:42 +0800148 host := ctx.Host()
Yi Kong56fc1b62022-09-06 16:24:00 +0800149 test := ctx.testBinary() || ctx.testLibrary()
Yi Kongc702ebd2022-08-19 16:02:45 +0800150 // FIXME: ThinLTO for VNDK produces different output.
151 // b/169217596
152 vndk := ctx.isVndk()
Yi Kong56fc1b62022-09-06 16:24:00 +0800153 return GlobalThinLTO(ctx) && !lto.Never() && !lib32 && !cfi && !host && !test && !vndk
Yi Kong93718e02020-09-21 21:41:03 +0800154}
155
156func (lto *lto) FullLTO() bool {
Liz Kammer729aaf42022-09-16 12:39:42 -0400157 return lto != nil && (proptools.Bool(lto.Properties.Lto.Full) || lto.Properties.FullEnabled)
Yi Kong93718e02020-09-21 21:41:03 +0800158}
159
160func (lto *lto) ThinLTO() bool {
Liz Kammer729aaf42022-09-16 12:39:42 -0400161 return lto != nil && (proptools.Bool(lto.Properties.Lto.Thin) || lto.Properties.ThinEnabled)
Stephen Craneba090d12017-05-09 15:44:35 -0700162}
163
Yi Kongf43ff052020-09-28 14:41:50 +0800164func (lto *lto) Never() bool {
Liz Kammer729aaf42022-09-16 12:39:42 -0400165 return lto != nil && (proptools.Bool(lto.Properties.Lto.Never) || lto.Properties.NoLtoEnabled)
Yi Kong8ea56f92021-10-14 01:07:42 +0800166}
167
168func GlobalThinLTO(ctx android.BaseModuleContext) bool {
Yi Kong7689c642022-08-23 09:24:48 +0000169 return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
Stephen Crane10cd1872017-09-27 17:01:15 -0700170}
171
Stephen Craneba090d12017-05-09 15:44:35 -0700172// Propagate lto requirements down from binaries
173func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800174 globalThinLTO := GlobalThinLTO(mctx)
175
176 if m, ok := mctx.Module().(*Module); ok {
Yi Kong93718e02020-09-21 21:41:03 +0800177 full := m.lto.FullLTO()
178 thin := m.lto.ThinLTO()
Yi Kong8ea56f92021-10-14 01:07:42 +0800179 never := m.lto.Never()
Yi Kong244bf072017-08-29 11:10:09 +0800180 if full && thin {
181 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
182 }
183
Stephen Crane10cd1872017-09-27 17:01:15 -0700184 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
185 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700186 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700187
188 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700189 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700190 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700191 return false
192 }
193 } else {
194 if tag != objDepTag && tag != reuseObjTag {
195 return false
196 }
197 }
198
Yi Kong8ea56f92021-10-14 01:07:42 +0800199 if dep, ok := dep.(*Module); ok {
Yi Kong93718e02020-09-21 21:41:03 +0800200 if full && !dep.lto.FullLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700201 dep.lto.Properties.FullDep = true
202 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800203 if !globalThinLTO && thin && !dep.lto.ThinLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700204 dep.lto.Properties.ThinDep = true
205 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800206 if globalThinLTO && never && !dep.lto.Never() {
207 dep.lto.Properties.NoLtoDep = true
208 }
Colin Cross6e511a92020-07-27 21:26:48 -0700209 }
210
211 // Recursively walk static dependencies
212 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700213 })
214 }
215}
216
217// Create lto variants for modules that need them
218func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800219 globalThinLTO := GlobalThinLTO(mctx)
220
Stephen Crane10cd1872017-09-27 17:01:15 -0700221 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
222 // Create variations for LTO types required as static
223 // dependencies
224 variationNames := []string{""}
Yi Kong93718e02020-09-21 21:41:03 +0800225 if m.lto.Properties.FullDep && !m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700226 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700227 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800228 if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700229 variationNames = append(variationNames, "lto-thin")
230 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800231 if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() {
232 variationNames = append(variationNames, "lto-none")
233 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700234
235 // Use correct dependencies if LTO property is explicitly set
236 // (mutually exclusive)
Yi Kong93718e02020-09-21 21:41:03 +0800237 if m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700238 mctx.SetDependencyVariation("lto-full")
239 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800240 if !globalThinLTO && m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700241 mctx.SetDependencyVariation("lto-thin")
242 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800243 // Never must be the last, it overrides Thin or Full.
244 if globalThinLTO && m.lto.Never() {
245 mctx.SetDependencyVariation("lto-none")
246 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700247
248 if len(variationNames) > 1 {
249 modules := mctx.CreateVariations(variationNames...)
250 for i, name := range variationNames {
251 variation := modules[i].(*Module)
252 // Default module which will be
253 // installed. Variation set above according to
254 // explicit LTO properties
255 if name == "" {
256 continue
257 }
258
259 // LTO properties for dependencies
260 if name == "lto-full" {
Liz Kammer729aaf42022-09-16 12:39:42 -0400261 variation.lto.Properties.FullEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700262 }
263 if name == "lto-thin" {
Liz Kammer729aaf42022-09-16 12:39:42 -0400264 variation.lto.Properties.ThinEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700265 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800266 if name == "lto-none" {
Liz Kammer729aaf42022-09-16 12:39:42 -0400267 variation.lto.Properties.NoLtoEnabled = true
Yi Kong8ea56f92021-10-14 01:07:42 +0800268 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700269 variation.Properties.PreventInstall = true
270 variation.Properties.HideFromMake = true
271 variation.lto.Properties.FullDep = false
272 variation.lto.Properties.ThinDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800273 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700274 }
275 }
Stephen Craneba090d12017-05-09 15:44:35 -0700276 }
277}