blob: 44361db1b01996bdeafc9fbf104d00e2815f79fb [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"`
Stephen Crane10cd1872017-09-27 17:01:15 -070046 Thin *bool `android:"arch_variant"`
Yi Kong244bf072017-08-29 11:10:09 +080047 } `android:"arch_variant"`
Stephen Crane10cd1872017-09-27 17:01:15 -070048
Yi Kong895d2412023-06-08 01:48:42 +090049 LtoEnabled bool `blueprint:"mutated"`
Yi Kongadd63752023-06-26 17:39:46 +090050 LtoDefault bool `blueprint:"mutated"`
Yi Kong895d2412023-06-08 01:48:42 +090051
Stephen Crane10cd1872017-09-27 17:01:15 -070052 // Dep properties indicate that this module needs to be built with LTO
53 // since it is an object dependency of an LTO module.
Yi Kong895d2412023-06-08 01:48:42 +090054 LtoDep bool `blueprint:"mutated"`
55 NoLtoDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070056
Yi Kong2d01fe22020-09-21 01:18:32 +080057 // Use -fwhole-program-vtables cflag.
58 Whole_program_vtables *bool
Stephen Craneba090d12017-05-09 15:44:35 -070059}
60
61type lto struct {
62 Properties LTOProperties
63}
64
65func (lto *lto) props() []interface{} {
66 return []interface{}{&lto.Properties}
67}
68
69func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kongadd63752023-06-26 17:39:46 +090070 // First, determine the module indepedent default LTO mode.
71 ltoDefault := GlobalThinLTO(ctx)
72 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
73 ltoDefault = false
74 } else if ctx.Host() {
75 // Performance and binary size are less important for host binaries.
76 ltoDefault = false
Yi Kong13beeed2023-07-15 03:09:00 +090077 } else if ctx.Arch().ArchType.Multilib == "lib32" {
78 // LP32 has many subtle issues and less test coverage.
79 ltoDefault = false
Yi Kongadd63752023-06-26 17:39:46 +090080 }
81
82 // Then, determine the actual LTO mode to use. If different from `ltoDefault`, a variant needs
83 // to be created.
84 ltoEnabled := ltoDefault
85 if lto.Never() {
86 ltoEnabled = false
87 } else if lto.ThinLTO() {
88 // Module explicitly requests for LTO.
89 ltoEnabled = true
90 } else if ctx.testBinary() || ctx.testLibrary() {
91 // Do not enable LTO for tests for better debugging.
92 ltoEnabled = false
93 } else if ctx.isVndk() {
94 // FIXME: ThinLTO for VNDK produces different output.
95 // b/169217596
96 ltoEnabled = false
97 }
98
99 lto.Properties.LtoDefault = ltoDefault
100 lto.Properties.LtoEnabled = ltoEnabled
Stephen Craneba090d12017-05-09 15:44:35 -0700101}
102
Stephen Craneba090d12017-05-09 15:44:35 -0700103func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong895d2412023-06-08 01:48:42 +0900104 // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves.
105 // This has be checked late because these properties can be mutated.
106 if ctx.isCfi() || ctx.isFuzzer() {
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000107 return flags
108 }
Yi Kong895d2412023-06-08 01:48:42 +0900109 if lto.Properties.LtoEnabled {
Yi Kongb9d50462023-07-03 16:59:33 +0900110 ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"}
111 var ltoLdFlags []string
112
113 // The module did not explicitly turn on LTO. Only leverage LTO's
Yi Kong8f9ca232023-07-14 06:15:51 +0000114 // better dead code elimination and CFG simplification, but do
Yi Kongb9d50462023-07-03 16:59:33 +0900115 // not perform costly optimizations for a balance between compile
116 // time, binary size and performance.
117 if !lto.ThinLTO() {
118 ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0")
Yi Kong244bf072017-08-29 11:10:09 +0800119 }
120
Yi Kong2d01fe22020-09-21 01:18:32 +0800121 if Bool(lto.Properties.Whole_program_vtables) {
Yi Kongb9d50462023-07-03 16:59:33 +0900122 ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables")
Yi Kong2d01fe22020-09-21 01:18:32 +0800123 }
124
Yi Kong895d2412023-06-08 01:48:42 +0900125 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +0800126 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700127 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800128 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Yi Kongb9d50462023-07-03 16:59:33 +0900129 ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800130
131 // Limit the size of the ThinLTO cache to the lesser of 10% of available
132 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700133 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800134 policy := "cache_size=10%:cache_size_bytes=10g"
Yi Kongb9d50462023-07-03 16:59:33 +0900135 ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800136 }
137
Yi Kong2f5f16d2020-09-23 00:54:50 +0800138 // If the module does not have a profile, be conservative and limit cross TU inline
139 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
A. Cody Schuffelen7188b902023-06-27 19:10:27 -0700140 if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Yi Kongb9d50462023-07-03 16:59:33 +0900141 ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800142 }
Yi Kongb9d50462023-07-03 16:59:33 +0900143
144 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...)
145 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...)
146 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...)
147 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...)
Stephen Craneba090d12017-05-09 15:44:35 -0700148 }
149 return flags
150}
151
Yi Kong93718e02020-09-21 21:41:03 +0800152func (lto *lto) ThinLTO() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900153 return lto != nil && proptools.Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700154}
155
Yi Kongf43ff052020-09-28 14:41:50 +0800156func (lto *lto) Never() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900157 return lto != nil && proptools.Bool(lto.Properties.Lto.Never)
Yi Kong8ea56f92021-10-14 01:07:42 +0800158}
159
160func GlobalThinLTO(ctx android.BaseModuleContext) bool {
Yi Kongadd63752023-06-26 17:39:46 +0900161 return !ctx.Config().IsEnvFalse("GLOBAL_THINLTO")
Stephen Crane10cd1872017-09-27 17:01:15 -0700162}
163
Stephen Craneba090d12017-05-09 15:44:35 -0700164// Propagate lto requirements down from binaries
165func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800166 if m, ok := mctx.Module().(*Module); ok {
Yi Kongadd63752023-06-26 17:39:46 +0900167 if m.lto == nil || m.lto.Properties.LtoEnabled == m.lto.Properties.LtoDefault {
Yi Kong895d2412023-06-08 01:48:42 +0900168 return
169 }
Yi Kong244bf072017-08-29 11:10:09 +0800170
Stephen Crane10cd1872017-09-27 17:01:15 -0700171 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
172 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700173 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700174
175 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700176 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700177 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700178 return false
179 }
180 } else {
181 if tag != objDepTag && tag != reuseObjTag {
182 return false
183 }
184 }
185
Yi Kong8ea56f92021-10-14 01:07:42 +0800186 if dep, ok := dep.(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900187 if m.lto.Properties.LtoEnabled {
188 dep.lto.Properties.LtoDep = true
189 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800190 dep.lto.Properties.NoLtoDep = true
191 }
Colin Cross6e511a92020-07-27 21:26:48 -0700192 }
193
194 // Recursively walk static dependencies
195 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700196 })
197 }
198}
199
200// Create lto variants for modules that need them
201func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800202 globalThinLTO := GlobalThinLTO(mctx)
203
Stephen Crane10cd1872017-09-27 17:01:15 -0700204 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
205 // Create variations for LTO types required as static
206 // dependencies
207 variationNames := []string{""}
Yi Kong895d2412023-06-08 01:48:42 +0900208 if m.lto.Properties.LtoDep {
Stephen Crane10cd1872017-09-27 17:01:15 -0700209 variationNames = append(variationNames, "lto-thin")
210 }
Yi Kong895d2412023-06-08 01:48:42 +0900211 if m.lto.Properties.NoLtoDep {
Yi Kong8ea56f92021-10-14 01:07:42 +0800212 variationNames = append(variationNames, "lto-none")
213 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700214
Yi Kong895d2412023-06-08 01:48:42 +0900215 if globalThinLTO && !m.lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +0800216 mctx.SetDependencyVariation("lto-none")
217 }
Yi Kong895d2412023-06-08 01:48:42 +0900218 if !globalThinLTO && m.lto.Properties.LtoEnabled {
219 mctx.SetDependencyVariation("lto-thin")
220 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700221
222 if len(variationNames) > 1 {
223 modules := mctx.CreateVariations(variationNames...)
224 for i, name := range variationNames {
225 variation := modules[i].(*Module)
226 // Default module which will be
227 // installed. Variation set above according to
228 // explicit LTO properties
229 if name == "" {
230 continue
231 }
232
233 // LTO properties for dependencies
Stephen Crane10cd1872017-09-27 17:01:15 -0700234 if name == "lto-thin" {
Yi Kong895d2412023-06-08 01:48:42 +0900235 variation.lto.Properties.LtoEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700236 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800237 if name == "lto-none" {
Yi Kong895d2412023-06-08 01:48:42 +0900238 variation.lto.Properties.LtoEnabled = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800239 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700240 variation.Properties.PreventInstall = true
241 variation.Properties.HideFromMake = true
Yi Kongadd63752023-06-26 17:39:46 +0900242 variation.lto.Properties.LtoDefault = m.lto.Properties.LtoDefault
Yi Kong895d2412023-06-08 01:48:42 +0900243 variation.lto.Properties.LtoDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800244 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700245 }
246 }
Stephen Craneba090d12017-05-09 15:44:35 -0700247 }
248}