blob: d2a43d27b0e4eaa0b6a66388a76968d878c04d4c [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//
Yi Kong577a73a2023-10-05 05:03:42 +000038// This file adds support to soong to automatically propagate LTO options to a
Stephen Craneba090d12017-05-09 15:44:35 -070039// new variant of all static dependencies for each module with LTO enabled.
40
41type LTOProperties struct {
Yi Kong577a73a2023-10-05 05:03:42 +000042 // Lto must violate capitalization style for acronyms so that it can be
Stephen Craneba090d12017-05-09 15:44:35 -070043 // 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 Kong577a73a2023-10-05 05:03:42 +000070 // First, determine the module independent default LTO mode.
Yi Kong950c1742023-10-09 19:49:45 +090071 ltoDefault := true
Yi Kongadd63752023-06-26 17:39:46 +090072 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
73 ltoDefault = false
Yi Kong577a73a2023-10-05 05:03:42 +000074 } else if lto.Never() {
75 ltoDefault = false
Yi Kongadd63752023-06-26 17:39:46 +090076 } else if ctx.Host() {
77 // Performance and binary size are less important for host binaries.
78 ltoDefault = false
Yi Kong13beeed2023-07-15 03:09:00 +090079 } else if ctx.Arch().ArchType.Multilib == "lib32" {
80 // LP32 has many subtle issues and less test coverage.
81 ltoDefault = false
Yi Kongadd63752023-06-26 17:39:46 +090082 }
83
84 // Then, determine the actual LTO mode to use. If different from `ltoDefault`, a variant needs
85 // to be created.
86 ltoEnabled := ltoDefault
87 if lto.Never() {
88 ltoEnabled = false
89 } else if lto.ThinLTO() {
90 // Module explicitly requests for LTO.
91 ltoEnabled = true
92 } else if ctx.testBinary() || ctx.testLibrary() {
93 // Do not enable LTO for tests for better debugging.
94 ltoEnabled = false
95 } else if ctx.isVndk() {
96 // FIXME: ThinLTO for VNDK produces different output.
97 // b/169217596
98 ltoEnabled = false
99 }
100
101 lto.Properties.LtoDefault = ltoDefault
102 lto.Properties.LtoEnabled = ltoEnabled
Stephen Craneba090d12017-05-09 15:44:35 -0700103}
104
Stephen Craneba090d12017-05-09 15:44:35 -0700105func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong895d2412023-06-08 01:48:42 +0900106 // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves.
107 // This has be checked late because these properties can be mutated.
108 if ctx.isCfi() || ctx.isFuzzer() {
Mitch Phillips5007c4a2022-03-02 01:25:22 +0000109 return flags
110 }
Yi Kong895d2412023-06-08 01:48:42 +0900111 if lto.Properties.LtoEnabled {
Yi Kongb9d50462023-07-03 16:59:33 +0900112 ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"}
113 var ltoLdFlags []string
114
115 // The module did not explicitly turn on LTO. Only leverage LTO's
Yi Kong8f9ca232023-07-14 06:15:51 +0000116 // better dead code elimination and CFG simplification, but do
Yi Kongb9d50462023-07-03 16:59:33 +0900117 // not perform costly optimizations for a balance between compile
118 // time, binary size and performance.
119 if !lto.ThinLTO() {
120 ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0")
Yi Kong244bf072017-08-29 11:10:09 +0800121 }
122
Yi Kong2d01fe22020-09-21 01:18:32 +0800123 if Bool(lto.Properties.Whole_program_vtables) {
Yi Kongb9d50462023-07-03 16:59:33 +0900124 ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables")
Yi Kong2d01fe22020-09-21 01:18:32 +0800125 }
126
Yi Kong895d2412023-06-08 01:48:42 +0900127 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
Yi Kong8aeaa712018-02-16 20:36:16 +0800128 // Set appropriate ThinLTO cache policy
Yi Kong630b9602019-03-22 21:28:39 -0700129 cacheDirFormat := "-Wl,--thinlto-cache-dir="
Yi Kong8aeaa712018-02-16 20:36:16 +0800130 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
Yi Kongb9d50462023-07-03 16:59:33 +0900131 ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir)
Yi Kong8aeaa712018-02-16 20:36:16 +0800132
133 // Limit the size of the ThinLTO cache to the lesser of 10% of available
134 // disk space and 10GB.
Yi Kong630b9602019-03-22 21:28:39 -0700135 cachePolicyFormat := "-Wl,--thinlto-cache-policy="
Yi Kong8aeaa712018-02-16 20:36:16 +0800136 policy := "cache_size=10%:cache_size_bytes=10g"
Yi Kongb9d50462023-07-03 16:59:33 +0900137 ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy)
Yi Kong8aeaa712018-02-16 20:36:16 +0800138 }
139
Yi Kongd6ab48c2023-08-01 14:12:39 +0900140 // Reduce the inlining threshold for a better balance of binary size and
141 // performance.
142 if !ctx.Darwin() {
Yi Kong9c3f4332023-11-24 17:14:27 +0900143 if ctx.isAfdoCompile() {
Yi Kongd6ab48c2023-08-01 14:12:39 +0900144 ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=40")
145 } else {
146 ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5")
147 }
Yi Kong7e53c572018-02-14 18:16:12 +0800148 }
Yi Kongb9d50462023-07-03 16:59:33 +0900149
AdityaK76c73852023-11-14 10:24:59 -0800150 if !ctx.Config().IsEnvFalse("THINLTO_USE_MLGO") {
151 // Register allocation MLGO flags for ARM64.
152 if ctx.Arch().ArchType == android.Arm64 {
AdityaK76c73852023-11-14 10:24:59 -0800153 ltoLdFlags = append(ltoLdFlags, "-Wl,-mllvm,-regalloc-enable-advisor=release")
154 }
Yi Kong0fa503d2023-11-07 14:12:51 +0900155 // Flags for training MLGO model.
156 if ctx.Config().IsEnvTrue("THINLTO_EMIT_INDEXES_AND_IMPORTS") {
157 ltoLdFlags = append(ltoLdFlags, "-Wl,--save-temps=import")
158 ltoLdFlags = append(ltoLdFlags, "-Wl,--thinlto-emit-index-files")
159 }
Yi Kongb8eaee62023-10-31 21:58:42 +0900160 }
161
Yi Kongb9d50462023-07-03 16:59:33 +0900162 flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...)
163 flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...)
164 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...)
165 flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...)
Stephen Craneba090d12017-05-09 15:44:35 -0700166 }
167 return flags
168}
169
Yi Kong93718e02020-09-21 21:41:03 +0800170func (lto *lto) ThinLTO() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900171 return lto != nil && proptools.Bool(lto.Properties.Lto.Thin)
Stephen Craneba090d12017-05-09 15:44:35 -0700172}
173
Yi Kongf43ff052020-09-28 14:41:50 +0800174func (lto *lto) Never() bool {
Yi Kong895d2412023-06-08 01:48:42 +0900175 return lto != nil && proptools.Bool(lto.Properties.Lto.Never)
Yi Kong8ea56f92021-10-14 01:07:42 +0800176}
177
Stephen Craneba090d12017-05-09 15:44:35 -0700178// Propagate lto requirements down from binaries
179func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Yi Kong8ea56f92021-10-14 01:07:42 +0800180 if m, ok := mctx.Module().(*Module); ok {
Yi Kongadd63752023-06-26 17:39:46 +0900181 if m.lto == nil || m.lto.Properties.LtoEnabled == m.lto.Properties.LtoDefault {
Yi Kong895d2412023-06-08 01:48:42 +0900182 return
183 }
Yi Kong244bf072017-08-29 11:10:09 +0800184
Stephen Crane10cd1872017-09-27 17:01:15 -0700185 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
186 tag := mctx.OtherModuleDependencyTag(dep)
Colin Cross6e511a92020-07-27 21:26:48 -0700187 libTag, isLibTag := tag.(libraryDependencyTag)
Stephen Crane10cd1872017-09-27 17:01:15 -0700188
189 // Do not recurse down non-static dependencies
Colin Cross6e511a92020-07-27 21:26:48 -0700190 if isLibTag {
Colin Cross4fbb5e02020-07-29 12:55:55 -0700191 if !libTag.static() {
Colin Cross6e511a92020-07-27 21:26:48 -0700192 return false
193 }
194 } else {
195 if tag != objDepTag && tag != reuseObjTag {
196 return false
197 }
198 }
199
Yi Kong8ea56f92021-10-14 01:07:42 +0800200 if dep, ok := dep.(*Module); ok {
Yi Kong895d2412023-06-08 01:48:42 +0900201 if m.lto.Properties.LtoEnabled {
202 dep.lto.Properties.LtoDep = true
203 } else {
Yi Kong8ea56f92021-10-14 01:07:42 +0800204 dep.lto.Properties.NoLtoDep = true
205 }
Colin Cross6e511a92020-07-27 21:26:48 -0700206 }
207
208 // Recursively walk static dependencies
209 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700210 })
211 }
212}
213
214// Create lto variants for modules that need them
215func ltoMutator(mctx android.BottomUpMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700216 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
217 // Create variations for LTO types required as static
218 // dependencies
219 variationNames := []string{""}
Yi Kong895d2412023-06-08 01:48:42 +0900220 if m.lto.Properties.LtoDep {
Stephen Crane10cd1872017-09-27 17:01:15 -0700221 variationNames = append(variationNames, "lto-thin")
222 }
Yi Kong895d2412023-06-08 01:48:42 +0900223 if m.lto.Properties.NoLtoDep {
Yi Kong8ea56f92021-10-14 01:07:42 +0800224 variationNames = append(variationNames, "lto-none")
225 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700226
Yi Kong950c1742023-10-09 19:49:45 +0900227 if !m.lto.Properties.LtoEnabled {
Yi Kong8ea56f92021-10-14 01:07:42 +0800228 mctx.SetDependencyVariation("lto-none")
229 }
Yi Kong950c1742023-10-09 19:49:45 +0900230 if m.lto.Properties.LtoEnabled {
Yi Kong895d2412023-06-08 01:48:42 +0900231 mctx.SetDependencyVariation("lto-thin")
232 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700233
234 if len(variationNames) > 1 {
235 modules := mctx.CreateVariations(variationNames...)
236 for i, name := range variationNames {
237 variation := modules[i].(*Module)
238 // Default module which will be
239 // installed. Variation set above according to
240 // explicit LTO properties
241 if name == "" {
242 continue
243 }
244
245 // LTO properties for dependencies
Stephen Crane10cd1872017-09-27 17:01:15 -0700246 if name == "lto-thin" {
Yi Kong895d2412023-06-08 01:48:42 +0900247 variation.lto.Properties.LtoEnabled = true
Stephen Crane10cd1872017-09-27 17:01:15 -0700248 }
Yi Kong8ea56f92021-10-14 01:07:42 +0800249 if name == "lto-none" {
Yi Kong895d2412023-06-08 01:48:42 +0900250 variation.lto.Properties.LtoEnabled = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800251 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700252 variation.Properties.PreventInstall = true
253 variation.Properties.HideFromMake = true
Yi Kongadd63752023-06-26 17:39:46 +0900254 variation.lto.Properties.LtoDefault = m.lto.Properties.LtoDefault
Yi Kong895d2412023-06-08 01:48:42 +0900255 variation.lto.Properties.LtoDep = false
Yi Kong8ea56f92021-10-14 01:07:42 +0800256 variation.lto.Properties.NoLtoDep = false
Stephen Crane10cd1872017-09-27 17:01:15 -0700257 }
258 }
Stephen Craneba090d12017-05-09 15:44:35 -0700259 }
260}