blob: 510dd79de0b4ee3990961316e01b1f97507c8b4b [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 Kongb9d50462023-07-03 16:59:33 +090079 ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"}
80 var ltoLdFlags []string
81
82 // The module did not explicitly turn on LTO. Only leverage LTO's
83 // better dead code elinmination and CFG simplification, but do
84 // not perform costly optimizations for a balance between compile
85 // time, binary size and performance.
86 if !lto.ThinLTO() {
87 ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0")
Yi Kong244bf072017-08-29 11:10:09 +080088 }
89
Yi Kong2d01fe22020-09-21 01:18:32 +080090 if Bool(lto.Properties.Whole_program_vtables) {
Yi Kongb9d50462023-07-03 16:59:33 +090091 ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables")
Yi Kong2d01fe22020-09-21 01:18:32 +080092 }
93
Yi Kong895d2412023-06-08 01:48:42 +090094 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +080095 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -070096 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +080097 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Yi Kongb9d50462023-07-03 16:59:33 +090098 ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +080099
100 // Limit the size of the ThinLTO cache to the lesser of 10% of available
101 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700102 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800103 policy := "cache_size=10%:cache_size_bytes=10g"
Yi Kongb9d50462023-07-03 16:59:33 +0900104 ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800105 }
106
Yi Kong2f5f16d2020-09-23 00:54:50 +0800107 // If the module does not have a profile, be conservative and limit cross TU inline
108 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
A. Cody Schuffelen7188b902023-06-27 19:10:27 -0700109 if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Yi Kongb9d50462023-07-03 16:59:33 +0900110 ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800111 }
Yi Kongb9d50462023-07-03 16:59:33 +0900112
113 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...)
114 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...)
115 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...)
116 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...)
Stephen Craneba090d12017-05-09 15:44:35 -0700117 }
118 return flags
119}
120
Yi Kong895d2412023-06-08 01:48:42 +0900121// Determine which LTO mode to use for the given module.
Yi Kong8ea56f92021-10-14 01:07:42 +0800122func (lto *lto) LTO(ctx BaseModuleContext) bool {
Yi Kong895d2412023-06-08 01:48:42 +0900123 if lto.Never() {
124 return false
125 }
126 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
127 return false
128 }
129 // Module explicitly requests for LTO.
130 if lto.ThinLTO() {
131 return true
132 }
Yi Kongc702ebd2022-08-19 16:02:45 +0800133 // LP32 has many subtle issues and less test coverage.
Yi Kong895d2412023-06-08 01:48:42 +0900134 if ctx.Arch().ArchType.Multilib == "lib32" {
135 return false
136 }
Yi Kong56fc1b62022-09-06 16:24:00 +0800137 // Performance and binary size are less important for host binaries and tests.
Yi Kong895d2412023-06-08 01:48:42 +0900138 if ctx.Host() || ctx.testBinary() || ctx.testLibrary() {
139 return false
140 }
Yi Kongc702ebd2022-08-19 16:02:45 +0800141 // FIXME: ThinLTO for VNDK produces different output.
142 // b/169217596
Yi Kong895d2412023-06-08 01:48:42 +0900143 if ctx.isVndk() {
144 return false
145 }
146 return GlobalThinLTO(ctx)
Yi Kong93718e02020-09-21 21:41:03 +0800147}
148
Yi Kong93718e02020-09-21 21:41:03 +0800149func (lto *lto) ThinLTO() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900150 return lto != nil && proptools.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 Kong895d2412023-06-08 01:48:42 +0900154 return lto != nil && proptools.Bool(lto.Properties.Lto.Never)
Yi Kong8ea56f92021-10-14 01:07:42 +0800155}
156
157func GlobalThinLTO(ctx android.BaseModuleContext) bool {
Yi Kong7689c642022-08-23 09:24:48 +0000158 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 Kong895d2412023-06-08 01:48:42 +0900163 defaultLTOMode := GlobalThinLTO(mctx)
Yi Kong8ea56f92021-10-14 01:07:42 +0800164
165 if m, ok := mctx.Module().(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900166 if m.lto == nil || m.lto.Properties.LtoEnabled == defaultLTOMode {
167 return
168 }
Yi Kong244bf072017-08-29 11:10:09 +0800169
Stephen Crane10cd1872017-09-27 17:01:15 -0700170 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
171 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700172 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700173
174 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700175 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700176 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700177 return false
178 }
179 } else {
180 if tag != objDepTag && tag != reuseObjTag {
181 return false
182 }
183 }
184
Yi Kong8ea56f92021-10-14 01:07:42 +0800185 if dep, ok := dep.(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900186 if m.lto.Properties.LtoEnabled {
187 dep.lto.Properties.LtoDep = true
188 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800189 dep.lto.Properties.NoLtoDep = true
190 }
Colin Cross6e511a92020-07-27 21:26:48 -0700191 }
192
193 // Recursively walk static dependencies
194 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700195 })
196 }
197}
198
199// Create lto variants for modules that need them
200func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800201 globalThinLTO := GlobalThinLTO(mctx)
202
Stephen Crane10cd1872017-09-27 17:01:15 -0700203 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
204 // Create variations for LTO types required as static
205 // dependencies
206 variationNames := []string{""}
Yi Kong895d2412023-06-08 01:48:42 +0900207 if m.lto.Properties.LtoDep {
Stephen Crane10cd1872017-09-27 17:01:15 -0700208 variationNames = append(variationNames, "lto-thin")
209 }
Yi Kong895d2412023-06-08 01:48:42 +0900210 if m.lto.Properties.NoLtoDep {
Yi Kong8ea56f92021-10-14 01:07:42 +0800211 variationNames = append(variationNames, "lto-none")
212 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700213
Yi Kong895d2412023-06-08 01:48:42 +0900214 if globalThinLTO && !m.lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +0800215 mctx.SetDependencyVariation("lto-none")
216 }
Yi Kong895d2412023-06-08 01:48:42 +0900217 if !globalThinLTO && m.lto.Properties.LtoEnabled {
218 mctx.SetDependencyVariation("lto-thin")
219 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700220
221 if len(variationNames) > 1 {
222 modules := mctx.CreateVariations(variationNames...)
223 for i, name := range variationNames {
224 variation := modules[i].(*Module)
225 // Default module which will be
226 // installed. Variation set above according to
227 // explicit LTO properties
228 if name == "" {
229 continue
230 }
231
232 // LTO properties for dependencies
Stephen Crane10cd1872017-09-27 17:01:15 -0700233 if name == "lto-thin" {
Yi Kong895d2412023-06-08 01:48:42 +0900234 variation.lto.Properties.LtoEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700235 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800236 if name == "lto-none" {
Yi Kong895d2412023-06-08 01:48:42 +0900237 variation.lto.Properties.LtoEnabled = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800238 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700239 variation.Properties.PreventInstall = true
240 variation.Properties.HideFromMake = true
Yi Kong895d2412023-06-08 01:48:42 +0900241 variation.lto.Properties.LtoDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800242 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700243 }
244 }
Stephen Craneba090d12017-05-09 15:44:35 -0700245 }
246}