blob: fccb5972a0462dff44e8b7ce763ac42cf8d5d16e [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"
19)
20
21// LTO (link-time optimization) allows the compiler to optimize and generate
22// code for the entire module at link time, rather than per-compilation
23// unit. LTO is required for Clang CFI and other whole-program optimization
24// techniques. LTO also allows cross-compilation unit optimizations that should
25// result in faster and smaller code, at the expense of additional compilation
26// time.
27//
28// To properly build a module with LTO, the module and all recursive static
29// dependencies should be compiled with -flto which directs the compiler to emit
30// bitcode rather than native object files. These bitcode files are then passed
31// by the linker to the LLVM plugin for compilation at link time. Static
32// dependencies not built as bitcode will still function correctly but cannot be
33// optimized at link time and may not be compatible with features that require
34// LTO, such as CFI.
35//
36// This file adds support to soong to automatically propogate LTO options to a
37// new variant of all static dependencies for each module with LTO enabled.
38
39type LTOProperties struct {
40 // Lto must violate capitialization style for acronyms so that it can be
41 // referred to in blueprint files as "lto"
Yi Kong244bf072017-08-29 11:10:09 +080042 Lto struct {
Stephen Crane10cd1872017-09-27 17:01:15 -070043 Never *bool `android:"arch_variant"`
44 Full *bool `android:"arch_variant"`
45 Thin *bool `android:"arch_variant"`
Yi Kong244bf072017-08-29 11:10:09 +080046 } `android:"arch_variant"`
Stephen Crane10cd1872017-09-27 17:01:15 -070047
48 // Dep properties indicate that this module needs to be built with LTO
49 // since it is an object dependency of an LTO module.
50 FullDep bool `blueprint:"mutated"`
51 ThinDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070052
53 // Use clang lld instead of gnu ld.
54 Use_clang_lld *bool
Yi Kong2d01fe22020-09-21 01:18:32 +080055
56 // Use -fwhole-program-vtables cflag.
57 Whole_program_vtables *bool
Stephen Craneba090d12017-05-09 15:44:35 -070058}
59
60type lto struct {
61 Properties LTOProperties
62}
63
64func (lto *lto) props() []interface{} {
65 return []interface{}{&lto.Properties}
66}
67
68func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080069 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
70 lto.Properties.Lto.Never = boolPtr(true)
Yi Kong93718e02020-09-21 21:41:03 +080071 } else if ctx.Config().IsEnvTrue("GLOBAL_THINLTO") {
Yi Konge2577142020-09-29 08:44:45 +080072 staticLib := ctx.static() && !ctx.staticBinary()
73 hostBin := ctx.Host()
Yi Kong134161f2020-10-09 22:05:59 +080074 vndk := ctx.isVndk() // b/169217596
75 if !staticLib && !hostBin && !vndk {
Yi Konge2577142020-09-29 08:44:45 +080076 if !lto.Never() && !lto.FullLTO() {
77 lto.Properties.Lto.Thin = boolPtr(true)
78 }
79 }
Yi Kong03d383d2018-01-31 15:15:08 -080080 }
Stephen Craneba090d12017-05-09 15:44:35 -070081}
82
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070083func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
84 if lto.Properties.Use_clang_lld != nil {
85 return Bool(lto.Properties.Use_clang_lld)
86 }
Dan Willemsenfa2aee12018-10-21 19:47:01 -070087 return true
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070088}
89
Stephen Craneba090d12017-05-09 15:44:35 -070090func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Mitch Phillips34b493f2019-08-02 16:57:55 -070091 // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
92 // LTO breaks fuzzer builds.
Colin Cross4af21ed2019-11-04 09:37:55 -080093 if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
Mitch Phillips34b493f2019-08-02 16:57:55 -070094 return flags
95 }
96
Yi Kong244bf072017-08-29 11:10:09 +080097 if lto.LTO() {
98 var ltoFlag string
Yi Kong93718e02020-09-21 21:41:03 +080099 if lto.ThinLTO() {
Yi Kong6925d2b2019-03-05 14:11:41 -0800100 ltoFlag = "-flto=thin -fsplit-lto-unit"
Yi Kong244bf072017-08-29 11:10:09 +0800101 } else {
102 ltoFlag = "-flto"
103 }
104
Colin Cross4af21ed2019-11-04 09:37:55 -0800105 flags.Local.CFlags = append(flags.Local.CFlags, ltoFlag)
106 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +0800107
Yi Kong2d01fe22020-09-21 01:18:32 +0800108 if Bool(lto.Properties.Whole_program_vtables) {
109 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
110 }
111
Yi Kong93718e02020-09-21 21:41:03 +0800112 if lto.ThinLTO() && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) {
Yi Kong8aeaa712018-02-16 20:36:16 +0800113 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700114 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800115 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800116 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800117
118 // Limit the size of the ThinLTO cache to the lesser of 10% of available
119 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700120 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800121 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800122 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800123 }
124
Yi Kong2f5f16d2020-09-23 00:54:50 +0800125 // If the module does not have a profile, be conservative and limit cross TU inline
126 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
Yi Kong630b9602019-03-22 21:28:39 -0700127 if !ctx.isPgoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800128 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800129 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800130 }
Stephen Craneba090d12017-05-09 15:44:35 -0700131 }
132 return flags
133}
134
135// Can be called with a null receiver
136func (lto *lto) LTO() bool {
Yi Kongf43ff052020-09-28 14:41:50 +0800137 if lto == nil || lto.Never() {
Stephen Craneba090d12017-05-09 15:44:35 -0700138 return false
139 }
140
Yi Kong93718e02020-09-21 21:41:03 +0800141 return lto.FullLTO() || lto.ThinLTO()
142}
143
144func (lto *lto) FullLTO() bool {
145 return Bool(lto.Properties.Lto.Full)
146}
147
148func (lto *lto) ThinLTO() bool {
Yi Kong93718e02020-09-21 21:41:03 +0800149 return Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700150}
151
Stephen Crane10cd1872017-09-27 17:01:15 -0700152// Is lto.never explicitly set to true?
Yi Kongf43ff052020-09-28 14:41:50 +0800153func (lto *lto) Never() bool {
154 return Bool(lto.Properties.Lto.Never)
Stephen Crane10cd1872017-09-27 17:01:15 -0700155}
156
Stephen Craneba090d12017-05-09 15:44:35 -0700157// Propagate lto requirements down from binaries
158func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700159 if m, ok := mctx.Module().(*Module); ok && m.lto.LTO() {
Yi Kong93718e02020-09-21 21:41:03 +0800160 full := m.lto.FullLTO()
161 thin := m.lto.ThinLTO()
Yi Kong244bf072017-08-29 11:10:09 +0800162 if full && thin {
163 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
164 }
165
Stephen Crane10cd1872017-09-27 17:01:15 -0700166 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
167 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700168 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700169
170 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700171 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700172 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700173 return false
174 }
175 } else {
176 if tag != objDepTag && tag != reuseObjTag {
177 return false
178 }
179 }
180
181 if dep, ok := dep.(*Module); ok && dep.lto != nil &&
Yi Kongf43ff052020-09-28 14:41:50 +0800182 !dep.lto.Never() {
Yi Kong93718e02020-09-21 21:41:03 +0800183 if full && !dep.lto.FullLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700184 dep.lto.Properties.FullDep = true
185 }
Yi Kong93718e02020-09-21 21:41:03 +0800186 if thin && !dep.lto.ThinLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700187 dep.lto.Properties.ThinDep = true
188 }
189 }
190
191 // Recursively walk static dependencies
192 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700193 })
194 }
195}
196
197// Create lto variants for modules that need them
198func ltoMutator(mctx android.BottomUpMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700199 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
200 // Create variations for LTO types required as static
201 // dependencies
202 variationNames := []string{""}
Yi Kong93718e02020-09-21 21:41:03 +0800203 if m.lto.Properties.FullDep && !m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700204 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700205 }
Yi Kong93718e02020-09-21 21:41:03 +0800206 if m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700207 variationNames = append(variationNames, "lto-thin")
208 }
209
210 // Use correct dependencies if LTO property is explicitly set
211 // (mutually exclusive)
Yi Kong93718e02020-09-21 21:41:03 +0800212 if m.lto.FullLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700213 mctx.SetDependencyVariation("lto-full")
214 }
Yi Kong93718e02020-09-21 21:41:03 +0800215 if m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700216 mctx.SetDependencyVariation("lto-thin")
217 }
218
219 if len(variationNames) > 1 {
220 modules := mctx.CreateVariations(variationNames...)
221 for i, name := range variationNames {
222 variation := modules[i].(*Module)
223 // Default module which will be
224 // installed. Variation set above according to
225 // explicit LTO properties
226 if name == "" {
227 continue
228 }
229
230 // LTO properties for dependencies
231 if name == "lto-full" {
232 variation.lto.Properties.Lto.Full = boolPtr(true)
233 variation.lto.Properties.Lto.Thin = boolPtr(false)
234 }
235 if name == "lto-thin" {
236 variation.lto.Properties.Lto.Full = boolPtr(false)
237 variation.lto.Properties.Lto.Thin = boolPtr(true)
238 }
239 variation.Properties.PreventInstall = true
240 variation.Properties.HideFromMake = true
241 variation.lto.Properties.FullDep = false
242 variation.lto.Properties.ThinDep = false
243 }
244 }
Stephen Craneba090d12017-05-09 15:44:35 -0700245 }
246}