blob: ff2bf5a0c4a1c3eabb8594962d97665dfeaa2da0 [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
77 }
78
79 // Then, determine the actual LTO mode to use. If different from `ltoDefault`, a variant needs
80 // to be created.
81 ltoEnabled := ltoDefault
82 if lto.Never() {
83 ltoEnabled = false
84 } else if lto.ThinLTO() {
85 // Module explicitly requests for LTO.
86 ltoEnabled = true
87 } else if ctx.testBinary() || ctx.testLibrary() {
88 // Do not enable LTO for tests for better debugging.
89 ltoEnabled = false
90 } else if ctx.isVndk() {
91 // FIXME: ThinLTO for VNDK produces different output.
92 // b/169217596
93 ltoEnabled = false
94 }
95
96 lto.Properties.LtoDefault = ltoDefault
97 lto.Properties.LtoEnabled = ltoEnabled
Stephen Craneba090d12017-05-09 15:44:35 -070098}
99
Stephen Craneba090d12017-05-09 15:44:35 -0700100func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong895d2412023-06-08 01:48:42 +0900101 // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves.
102 // This has be checked late because these properties can be mutated.
103 if ctx.isCfi() || ctx.isFuzzer() {
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000104 return flags
105 }
Yi Kong895d2412023-06-08 01:48:42 +0900106 if lto.Properties.LtoEnabled {
Yi Kongb9d50462023-07-03 16:59:33 +0900107 ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"}
108 var ltoLdFlags []string
109
110 // The module did not explicitly turn on LTO. Only leverage LTO's
111 // better dead code elinmination and CFG simplification, but do
112 // not perform costly optimizations for a balance between compile
113 // time, binary size and performance.
114 if !lto.ThinLTO() {
115 ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0")
Yi Kong244bf072017-08-29 11:10:09 +0800116 }
117
Yi Kong2d01fe22020-09-21 01:18:32 +0800118 if Bool(lto.Properties.Whole_program_vtables) {
Yi Kongb9d50462023-07-03 16:59:33 +0900119 ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables")
Yi Kong2d01fe22020-09-21 01:18:32 +0800120 }
121
Yi Kong895d2412023-06-08 01:48:42 +0900122 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +0800123 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700124 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800125 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Yi Kongb9d50462023-07-03 16:59:33 +0900126 ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800127
128 // Limit the size of the ThinLTO cache to the lesser of 10% of available
129 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700130 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800131 policy := "cache_size=10%:cache_size_bytes=10g"
Yi Kongb9d50462023-07-03 16:59:33 +0900132 ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800133 }
134
Yi Kong2f5f16d2020-09-23 00:54:50 +0800135 // If the module does not have a profile, be conservative and limit cross TU inline
136 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
A. Cody Schuffelen7188b902023-06-27 19:10:27 -0700137 if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Yi Kongb9d50462023-07-03 16:59:33 +0900138 ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800139 }
Yi Kongb9d50462023-07-03 16:59:33 +0900140
141 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...)
142 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...)
143 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...)
144 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...)
Stephen Craneba090d12017-05-09 15:44:35 -0700145 }
146 return flags
147}
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 Kongadd63752023-06-26 17:39:46 +0900158 return !ctx.Config().IsEnvFalse("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 Kong8ea56f92021-10-14 01:07:42 +0800163 if m, ok := mctx.Module().(*Module); ok {
Yi Kongadd63752023-06-26 17:39:46 +0900164 if m.lto == nil || m.lto.Properties.LtoEnabled == m.lto.Properties.LtoDefault {
Yi Kong895d2412023-06-08 01:48:42 +0900165 return
166 }
Yi Kong244bf072017-08-29 11:10:09 +0800167
Stephen Crane10cd1872017-09-27 17:01:15 -0700168 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
169 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700170 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700171
172 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700173 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700174 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700175 return false
176 }
177 } else {
178 if tag != objDepTag && tag != reuseObjTag {
179 return false
180 }
181 }
182
Yi Kong8ea56f92021-10-14 01:07:42 +0800183 if dep, ok := dep.(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900184 if m.lto.Properties.LtoEnabled {
185 dep.lto.Properties.LtoDep = true
186 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800187 dep.lto.Properties.NoLtoDep = true
188 }
Colin Cross6e511a92020-07-27 21:26:48 -0700189 }
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) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800199 globalThinLTO := GlobalThinLTO(mctx)
200
Stephen Crane10cd1872017-09-27 17:01:15 -0700201 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
202 // Create variations for LTO types required as static
203 // dependencies
204 variationNames := []string{""}
Yi Kong895d2412023-06-08 01:48:42 +0900205 if m.lto.Properties.LtoDep {
Stephen Crane10cd1872017-09-27 17:01:15 -0700206 variationNames = append(variationNames, "lto-thin")
207 }
Yi Kong895d2412023-06-08 01:48:42 +0900208 if m.lto.Properties.NoLtoDep {
Yi Kong8ea56f92021-10-14 01:07:42 +0800209 variationNames = append(variationNames, "lto-none")
210 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700211
Yi Kong895d2412023-06-08 01:48:42 +0900212 if globalThinLTO && !m.lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +0800213 mctx.SetDependencyVariation("lto-none")
214 }
Yi Kong895d2412023-06-08 01:48:42 +0900215 if !globalThinLTO && m.lto.Properties.LtoEnabled {
216 mctx.SetDependencyVariation("lto-thin")
217 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700218
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
Stephen Crane10cd1872017-09-27 17:01:15 -0700231 if name == "lto-thin" {
Yi Kong895d2412023-06-08 01:48:42 +0900232 variation.lto.Properties.LtoEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700233 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800234 if name == "lto-none" {
Yi Kong895d2412023-06-08 01:48:42 +0900235 variation.lto.Properties.LtoEnabled = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800236 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700237 variation.Properties.PreventInstall = true
238 variation.Properties.HideFromMake = true
Yi Kongadd63752023-06-26 17:39:46 +0900239 variation.lto.Properties.LtoDefault = m.lto.Properties.LtoDefault
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}