blob: a8bed235f16ed20f67cbd9673140bc82a0617fd1 [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
49 // Dep properties indicate that this module needs to be built with LTO
50 // since it is an object dependency of an LTO module.
Liz Kammer729aaf42022-09-16 12:39:42 -040051 ThinEnabled bool `blueprint:"mutated"`
52 NoLtoEnabled bool `blueprint:"mutated"`
Liz Kammer729aaf42022-09-16 12:39:42 -040053 ThinDep 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 Kong03d383d2018-01-31 15:15:08 -080069 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
Liz Kammer729aaf42022-09-16 12:39:42 -040070 lto.Properties.NoLtoEnabled = true
Yi Kong03d383d2018-01-31 15:15:08 -080071 }
Stephen Craneba090d12017-05-09 15:44:35 -070072}
73
Stephen Craneba090d12017-05-09 15:44:35 -070074func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Mitch Phillips5007c4a2022-03-02 01:25:22 +000075 // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
76 // LTO breaks fuzzer builds.
77 if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
78 return flags
79 }
80
Yi Kong8ea56f92021-10-14 01:07:42 +080081 if lto.LTO(ctx) {
82 var ltoCFlag string
83 var ltoLdFlag string
Yi Kong93718e02020-09-21 21:41:03 +080084 if lto.ThinLTO() {
Yi Kong8ea56f92021-10-14 01:07:42 +080085 ltoCFlag = "-flto=thin -fsplit-lto-unit"
Yi Kong244bf072017-08-29 11:10:09 +080086 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +080087 ltoCFlag = "-flto=thin -fsplit-lto-unit"
88 ltoLdFlag = "-Wl,--lto-O0"
Yi Kong244bf072017-08-29 11:10:09 +080089 }
90
Yi Kong8ea56f92021-10-14 01:07:42 +080091 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
Yi Kong5e0f4052022-09-09 01:58:18 +080092 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag)
Yi Kong8ea56f92021-10-14 01:07:42 +080093 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
94 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +080095
Yi Kong2d01fe22020-09-21 01:18:32 +080096 if Bool(lto.Properties.Whole_program_vtables) {
97 flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
98 }
99
Yi Konga7a345d2023-05-28 20:19:39 -0700100 if (lto.DefaultThinLTO(ctx) || lto.ThinLTO()) && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +0800101 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700102 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800103 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Colin Cross4af21ed2019-11-04 09:37:55 -0800104 flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800105
106 // Limit the size of the ThinLTO cache to the lesser of 10% of available
107 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700108 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800109 policy := "cache_size=10%:cache_size_bytes=10g"
Colin Cross4af21ed2019-11-04 09:37:55 -0800110 flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800111 }
112
Yi Kong2f5f16d2020-09-23 00:54:50 +0800113 // If the module does not have a profile, be conservative and limit cross TU inline
114 // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
Yi Kong4ef54592022-02-14 20:00:10 +0800115 if !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800116 flags.Local.LdFlags = append(flags.Local.LdFlags,
Yi Kong2f5f16d2020-09-23 00:54:50 +0800117 "-Wl,-plugin-opt,-import-instr-limit=5")
Yi Kong7e53c572018-02-14 18:16:12 +0800118 }
Stephen Craneba090d12017-05-09 15:44:35 -0700119 }
120 return flags
121}
122
Yi Kong8ea56f92021-10-14 01:07:42 +0800123func (lto *lto) LTO(ctx BaseModuleContext) bool {
Yi Kong0713e332023-05-31 14:15:22 +0900124 return lto.ThinLTO() || lto.DefaultThinLTO(ctx)
Yi Kong8ea56f92021-10-14 01:07:42 +0800125}
Stephen Craneba090d12017-05-09 15:44:35 -0700126
Yi Kong8ea56f92021-10-14 01:07:42 +0800127func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool {
Yi Kongc702ebd2022-08-19 16:02:45 +0800128 // LP32 has many subtle issues and less test coverage.
Yi Kong2121d162022-08-17 03:54:54 +0800129 lib32 := ctx.Arch().ArchType.Multilib == "lib32"
Yi Kong0713e332023-05-31 14:15:22 +0900130 // CFI adds LTO flags by itself.
Yi Kongc702ebd2022-08-19 16:02:45 +0800131 cfi := ctx.isCfi()
Yi Kong56fc1b62022-09-06 16:24:00 +0800132 // Performance and binary size are less important for host binaries and tests.
Yi Kong8ea56f92021-10-14 01:07:42 +0800133 host := ctx.Host()
Yi Kong56fc1b62022-09-06 16:24:00 +0800134 test := ctx.testBinary() || ctx.testLibrary()
Yi Kongc702ebd2022-08-19 16:02:45 +0800135 // FIXME: ThinLTO for VNDK produces different output.
136 // b/169217596
137 vndk := ctx.isVndk()
Yi Kong56fc1b62022-09-06 16:24:00 +0800138 return GlobalThinLTO(ctx) && !lto.Never() && !lib32 && !cfi && !host && !test && !vndk
Yi Kong93718e02020-09-21 21:41:03 +0800139}
140
Yi Kong93718e02020-09-21 21:41:03 +0800141func (lto *lto) ThinLTO() bool {
Liz Kammer729aaf42022-09-16 12:39:42 -0400142 return lto != nil && (proptools.Bool(lto.Properties.Lto.Thin) || lto.Properties.ThinEnabled)
Stephen Craneba090d12017-05-09 15:44:35 -0700143}
144
Yi Kongf43ff052020-09-28 14:41:50 +0800145func (lto *lto) Never() bool {
Liz Kammer729aaf42022-09-16 12:39:42 -0400146 return lto != nil && (proptools.Bool(lto.Properties.Lto.Never) || lto.Properties.NoLtoEnabled)
Yi Kong8ea56f92021-10-14 01:07:42 +0800147}
148
149func GlobalThinLTO(ctx android.BaseModuleContext) bool {
Yi Kong7689c642022-08-23 09:24:48 +0000150 return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
Stephen Crane10cd1872017-09-27 17:01:15 -0700151}
152
Stephen Craneba090d12017-05-09 15:44:35 -0700153// Propagate lto requirements down from binaries
154func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800155 globalThinLTO := GlobalThinLTO(mctx)
156
157 if m, ok := mctx.Module().(*Module); ok {
Yi Kong93718e02020-09-21 21:41:03 +0800158 thin := m.lto.ThinLTO()
Yi Kong8ea56f92021-10-14 01:07:42 +0800159 never := m.lto.Never()
Yi Kong244bf072017-08-29 11:10:09 +0800160
Stephen Crane10cd1872017-09-27 17:01:15 -0700161 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
162 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700163 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700164
165 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700166 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700167 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700168 return false
169 }
170 } else {
171 if tag != objDepTag && tag != reuseObjTag {
172 return false
173 }
174 }
175
Yi Kong8ea56f92021-10-14 01:07:42 +0800176 if dep, ok := dep.(*Module); ok {
Yi Kong8ea56f92021-10-14 01:07:42 +0800177 if !globalThinLTO && thin && !dep.lto.ThinLTO() {
Colin Cross6e511a92020-07-27 21:26:48 -0700178 dep.lto.Properties.ThinDep = true
179 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800180 if globalThinLTO && never && !dep.lto.Never() {
181 dep.lto.Properties.NoLtoDep = true
182 }
Colin Cross6e511a92020-07-27 21:26:48 -0700183 }
184
185 // Recursively walk static dependencies
186 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700187 })
188 }
189}
190
191// Create lto variants for modules that need them
192func ltoMutator(mctx android.BottomUpMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800193 globalThinLTO := GlobalThinLTO(mctx)
194
Stephen Crane10cd1872017-09-27 17:01:15 -0700195 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
196 // Create variations for LTO types required as static
197 // dependencies
198 variationNames := []string{""}
Yi Kong8ea56f92021-10-14 01:07:42 +0800199 if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700200 variationNames = append(variationNames, "lto-thin")
201 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800202 if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() {
203 variationNames = append(variationNames, "lto-none")
204 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700205
206 // Use correct dependencies if LTO property is explicitly set
207 // (mutually exclusive)
Yi Kong8ea56f92021-10-14 01:07:42 +0800208 if !globalThinLTO && m.lto.ThinLTO() {
Stephen Crane10cd1872017-09-27 17:01:15 -0700209 mctx.SetDependencyVariation("lto-thin")
210 }
Yi Kong0713e332023-05-31 14:15:22 +0900211 // Never must be the last, it overrides Thin.
Yi Kong8ea56f92021-10-14 01:07:42 +0800212 if globalThinLTO && m.lto.Never() {
213 mctx.SetDependencyVariation("lto-none")
214 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700215
216 if len(variationNames) > 1 {
217 modules := mctx.CreateVariations(variationNames...)
218 for i, name := range variationNames {
219 variation := modules[i].(*Module)
220 // Default module which will be
221 // installed. Variation set above according to
222 // explicit LTO properties
223 if name == "" {
224 continue
225 }
226
227 // LTO properties for dependencies
Stephen Crane10cd1872017-09-27 17:01:15 -0700228 if name == "lto-thin" {
Liz Kammer729aaf42022-09-16 12:39:42 -0400229 variation.lto.Properties.ThinEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700230 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800231 if name == "lto-none" {
Liz Kammer729aaf42022-09-16 12:39:42 -0400232 variation.lto.Properties.NoLtoEnabled = true
Yi Kong8ea56f92021-10-14 01:07:42 +0800233 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700234 variation.Properties.PreventInstall = true
235 variation.Properties.HideFromMake = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700236 variation.lto.Properties.ThinDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800237 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700238 }
239 }
Stephen Craneba090d12017-05-09 15:44:35 -0700240 }
241}