blob: 547ebff4e5fd5c0306671511231ab004264d97eb [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"`
50
Stephen Crane10cd1872017-09-27 17:01:15 -070051 // Dep properties indicate that this module needs to be built with LTO
52 // since it is an object dependency of an LTO module.
Yi Kong895d2412023-06-08 01:48:42 +090053 LtoDep bool `blueprint:"mutated"`
54 NoLtoDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070055
Yi Kong2d01fe22020-09-21 01:18:32 +080056 // 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 Kong895d2412023-06-08 01:48:42 +090069 lto.Properties.LtoEnabled = lto.LTO(ctx)
Stephen Craneba090d12017-05-09 15:44:35 -070070}
71
Stephen Craneba090d12017-05-09 15:44:35 -070072func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong895d2412023-06-08 01:48:42 +090073 // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves.
74 // This has be checked late because these properties can be mutated.
75 if ctx.isCfi() || ctx.isFuzzer() {
Mitch Phillips5007c4a2022-03-02 01:25:22 +000076 return flags
77 }
Yi Kong895d2412023-06-08 01:48:42 +090078 if lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +080079 var ltoCFlag string
80 var ltoLdFlag string
Yi Kong93718e02020-09-21 21:41:03 +080081 if lto.ThinLTO() {
Yi Kong8ea56f92021-10-14 01:07:42 +080082 ltoCFlag = "-flto=thin -fsplit-lto-unit"
Yi Kong244bf072017-08-29 11:10:09 +080083 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +080084 ltoCFlag = "-flto=thin -fsplit-lto-unit"
85 ltoLdFlag = "-Wl,--lto-O0"
Yi Kong244bf072017-08-29 11:10:09 +080086 }
87
Yi Kong8ea56f92021-10-14 01:07:42 +080088 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
Yi Kong5e0f4052022-09-09 01:58:18 +080089 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag)
Yi Kong8ea56f92021-10-14 01:07:42 +080090 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
91 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +080092
Yi Kong2d01fe22020-09-21 01:18:32 +080093 if Bool(lto.Properties.Whole_program_vtables) {
94 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
95 }
96
Yi Kong895d2412023-06-08 01:48:42 +090097 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +080098 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -070099 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800100 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800101 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800102
103 // Limit the size of the ThinLTO cache to the lesser of 10% of available
104 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700105 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800106 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800107 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800108 }
109
Yi Kong2f5f16d2020-09-23 00:54:50 +0800110 // If the module does not have a profile, be conservative and limit cross TU inline
111 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
A. Cody Schuffelen7188b902023-06-27 19:10:27 -0700112 if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800113 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800114 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800115 }
Stephen Craneba090d12017-05-09 15:44:35 -0700116 }
117 return flags
118}
119
Yi Kong895d2412023-06-08 01:48:42 +0900120// Determine which LTO mode to use for the given module.
Yi Kong8ea56f92021-10-14 01:07:42 +0800121func (lto *lto) LTO(ctx BaseModuleContext) bool {
Yi Kong895d2412023-06-08 01:48:42 +0900122 if lto.Never() {
123 return false
124 }
125 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
126 return false
127 }
128 // Module explicitly requests for LTO.
129 if lto.ThinLTO() {
130 return true
131 }
Yi Kongc702ebd2022-08-19 16:02:45 +0800132 // LP32 has many subtle issues and less test coverage.
Yi Kong895d2412023-06-08 01:48:42 +0900133 if ctx.Arch().ArchType.Multilib == "lib32" {
134 return false
135 }
Yi Kong56fc1b62022-09-06 16:24:00 +0800136 // Performance and binary size are less important for host binaries and tests.
Yi Kong895d2412023-06-08 01:48:42 +0900137 if ctx.Host() || ctx.testBinary() || ctx.testLibrary() {
138 return false
139 }
Yi Kongc702ebd2022-08-19 16:02:45 +0800140 // FIXME: ThinLTO for VNDK produces different output.
141 // b/169217596
Yi Kong895d2412023-06-08 01:48:42 +0900142 if ctx.isVndk() {
143 return false
144 }
145 return GlobalThinLTO(ctx)
Yi Kong93718e02020-09-21 21:41:03 +0800146}
147
Yi Kong93718e02020-09-21 21:41:03 +0800148func (lto *lto) ThinLTO() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900149 return lto != nil && proptools.Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700150}
151
Yi Kongf43ff052020-09-28 14:41:50 +0800152func (lto *lto) Never() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900153 return lto != nil && proptools.Bool(lto.Properties.Lto.Never)
Yi Kong8ea56f92021-10-14 01:07:42 +0800154}
155
156func GlobalThinLTO(ctx android.BaseModuleContext) bool {
Yi Kong7689c642022-08-23 09:24:48 +0000157 return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
Stephen Crane10cd1872017-09-27 17:01:15 -0700158}
159
Stephen Craneba090d12017-05-09 15:44:35 -0700160// Propagate lto requirements down from binaries
161func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong895d2412023-06-08 01:48:42 +0900162 defaultLTOMode := GlobalThinLTO(mctx)
Yi Kong8ea56f92021-10-14 01:07:42 +0800163
164 if m, ok := mctx.Module().(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900165 if m.lto == nil || m.lto.Properties.LtoEnabled == defaultLTOMode {
166 return
167 }
Yi Kong244bf072017-08-29 11:10:09 +0800168
Stephen Crane10cd1872017-09-27 17:01:15 -0700169 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
170 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700171 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700172
173 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700174 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700175 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700176 return false
177 }
178 } else {
179 if tag != objDepTag && tag != reuseObjTag {
180 return false
181 }
182 }
183
Yi Kong8ea56f92021-10-14 01:07:42 +0800184 if dep, ok := dep.(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900185 if m.lto.Properties.LtoEnabled {
186 dep.lto.Properties.LtoDep = true
187 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800188 dep.lto.Properties.NoLtoDep = true
189 }
Colin Cross6e511a92020-07-27 21:26:48 -0700190 }
191
192 // Recursively walk static dependencies
193 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700194 })
195 }
196}
197
198// Create lto variants for modules that need them
199func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800200 globalThinLTO := GlobalThinLTO(mctx)
201
Stephen Crane10cd1872017-09-27 17:01:15 -0700202 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
203 // Create variations for LTO types required as static
204 // dependencies
205 variationNames := []string{""}
Yi Kong895d2412023-06-08 01:48:42 +0900206 if m.lto.Properties.LtoDep {
Stephen Crane10cd1872017-09-27 17:01:15 -0700207 variationNames = append(variationNames, "lto-thin")
208 }
Yi Kong895d2412023-06-08 01:48:42 +0900209 if m.lto.Properties.NoLtoDep {
Yi Kong8ea56f92021-10-14 01:07:42 +0800210 variationNames = append(variationNames, "lto-none")
211 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700212
Yi Kong895d2412023-06-08 01:48:42 +0900213 if globalThinLTO && !m.lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +0800214 mctx.SetDependencyVariation("lto-none")
215 }
Yi Kong895d2412023-06-08 01:48:42 +0900216 if !globalThinLTO && m.lto.Properties.LtoEnabled {
217 mctx.SetDependencyVariation("lto-thin")
218 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700219
220 if len(variationNames) > 1 {
221 modules := mctx.CreateVariations(variationNames...)
222 for i, name := range variationNames {
223 variation := modules[i].(*Module)
224 // Default module which will be
225 // installed. Variation set above according to
226 // explicit LTO properties
227 if name == "" {
228 continue
229 }
230
231 // LTO properties for dependencies
Stephen Crane10cd1872017-09-27 17:01:15 -0700232 if name == "lto-thin" {
Yi Kong895d2412023-06-08 01:48:42 +0900233 variation.lto.Properties.LtoEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700234 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800235 if name == "lto-none" {
Yi Kong895d2412023-06-08 01:48:42 +0900236 variation.lto.Properties.LtoEnabled = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800237 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700238 variation.Properties.PreventInstall = true
239 variation.Properties.HideFromMake = true
Yi Kong895d2412023-06-08 01:48:42 +0900240 variation.lto.Properties.LtoDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800241 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700242 }
243 }
Stephen Craneba090d12017-05-09 15:44:35 -0700244 }
245}