blob: 63027486824ba57c65254250087f33ac444bc6ac [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"
19)
20
21// LTO (link-time optimization) allows the compiler to optimize and generate
22// code for the entire module at link time, rather than per-compilation
23// unit. LTO is required for Clang CFI and other whole-program optimization
24// techniques. LTO also allows cross-compilation unit optimizations that should
25// result in faster and smaller code, at the expense of additional compilation
26// time.
27//
28// To properly build a module with LTO, the module and all recursive static
29// dependencies should be compiled with -flto which directs the compiler to emit
30// bitcode rather than native object files. These bitcode files are then passed
31// by the linker to the LLVM plugin for compilation at link time. Static
32// dependencies not built as bitcode will still function correctly but cannot be
33// optimized at link time and may not be compatible with features that require
34// LTO, such as CFI.
35//
36// This file adds support to soong to automatically propogate LTO options to a
37// new variant of all static dependencies for each module with LTO enabled.
38
39type LTOProperties struct {
40 // Lto must violate capitialization style for acronyms so that it can be
41 // referred to in blueprint files as "lto"
Yi Kong244bf072017-08-29 11:10:09 +080042 Lto struct {
Stephen Crane10cd1872017-09-27 17:01:15 -070043 Never *bool `android:"arch_variant"`
44 Full *bool `android:"arch_variant"`
45 Thin *bool `android:"arch_variant"`
Yi Kong244bf072017-08-29 11:10:09 +080046 } `android:"arch_variant"`
Stephen Crane10cd1872017-09-27 17:01:15 -070047
48 // Dep properties indicate that this module needs to be built with LTO
49 // since it is an object dependency of an LTO module.
50 FullDep bool `blueprint:"mutated"`
51 ThinDep bool `blueprint:"mutated"`
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070052
53 // Use clang lld instead of gnu ld.
54 Use_clang_lld *bool
Stephen Craneba090d12017-05-09 15:44:35 -070055}
56
57type lto struct {
58 Properties LTOProperties
59}
60
61func (lto *lto) props() []interface{} {
62 return []interface{}{&lto.Properties}
63}
64
65func (lto *lto) begin(ctx BaseModuleContext) {
Yi Kong03d383d2018-01-31 15:15:08 -080066 if ctx.Config().IsEnvTrue("DISABLE_LTO") {
67 lto.Properties.Lto.Never = boolPtr(true)
68 }
Stephen Craneba090d12017-05-09 15:44:35 -070069}
70
71func (lto *lto) deps(ctx BaseModuleContext, deps Deps) Deps {
72 return deps
73}
74
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070075func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
76 if lto.Properties.Use_clang_lld != nil {
77 return Bool(lto.Properties.Use_clang_lld)
78 }
Dan Willemsenfa2aee12018-10-21 19:47:01 -070079 return true
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070080}
81
Stephen Craneba090d12017-05-09 15:44:35 -070082func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
Yi Kong244bf072017-08-29 11:10:09 +080083 if lto.LTO() {
84 var ltoFlag string
85 if Bool(lto.Properties.Lto.Thin) {
86 ltoFlag = "-flto=thin"
Yi Kong8aeaa712018-02-16 20:36:16 +080087
Yi Kong244bf072017-08-29 11:10:09 +080088 } else {
89 ltoFlag = "-flto"
90 }
91
92 flags.CFlags = append(flags.CFlags, ltoFlag)
93 flags.LdFlags = append(flags.LdFlags, ltoFlag)
Yi Kong8aeaa712018-02-16 20:36:16 +080094
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -070095 if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && Bool(lto.Properties.Lto.Thin) && !lto.useClangLld(ctx) {
Yi Kong8aeaa712018-02-16 20:36:16 +080096 // Set appropriate ThinLTO cache policy
97 cacheDirFormat := "-Wl,-plugin-opt,cache-dir="
98 cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
99 flags.LdFlags = append(flags.LdFlags, cacheDirFormat+cacheDir)
100
101 // Limit the size of the ThinLTO cache to the lesser of 10% of available
102 // disk space and 10GB.
103 cachePolicyFormat := "-Wl,-plugin-opt,cache-policy="
104 policy := "cache_size=10%:cache_size_bytes=10g"
105 flags.LdFlags = append(flags.LdFlags, cachePolicyFormat+policy)
106 }
107
Yi Kong7e53c572018-02-14 18:16:12 +0800108 // If the module does not have a profile, be conservative and do not inline
109 // or unroll loops during LTO, in order to prevent significant size bloat.
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700110 if !ctx.isPgoCompile() && !lto.useClangLld(ctx) {
Yi Kong7e53c572018-02-14 18:16:12 +0800111 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-inline-threshold=0")
112 flags.LdFlags = append(flags.LdFlags, "-Wl,-plugin-opt,-unroll-threshold=0")
113 }
Stephen Craneba090d12017-05-09 15:44:35 -0700114 }
115 return flags
116}
117
118// Can be called with a null receiver
119func (lto *lto) LTO() bool {
Stephen Crane10cd1872017-09-27 17:01:15 -0700120 if lto == nil || lto.Disabled() {
Stephen Craneba090d12017-05-09 15:44:35 -0700121 return false
122 }
123
Yi Kong244bf072017-08-29 11:10:09 +0800124 full := Bool(lto.Properties.Lto.Full)
125 thin := Bool(lto.Properties.Lto.Thin)
126 return full || thin
Stephen Craneba090d12017-05-09 15:44:35 -0700127}
128
Stephen Crane10cd1872017-09-27 17:01:15 -0700129// Is lto.never explicitly set to true?
130func (lto *lto) Disabled() bool {
131 return lto.Properties.Lto.Never != nil && *lto.Properties.Lto.Never
132}
133
Stephen Craneba090d12017-05-09 15:44:35 -0700134// Propagate lto requirements down from binaries
135func ltoDepsMutator(mctx android.TopDownMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700136 if m, ok := mctx.Module().(*Module); ok && m.lto.LTO() {
137 full := Bool(m.lto.Properties.Lto.Full)
138 thin := Bool(m.lto.Properties.Lto.Thin)
Yi Kong244bf072017-08-29 11:10:09 +0800139 if full && thin {
140 mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
141 }
142
Stephen Crane10cd1872017-09-27 17:01:15 -0700143 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
144 tag := mctx.OtherModuleDependencyTag(dep)
Stephen Craneba090d12017-05-09 15:44:35 -0700145 switch tag {
146 case staticDepTag, staticExportDepTag, lateStaticDepTag, wholeStaticDepTag, objDepTag, reuseObjTag:
Stephen Crane10cd1872017-09-27 17:01:15 -0700147 if dep, ok := dep.(*Module); ok && dep.lto != nil &&
148 !dep.lto.Disabled() {
149 if full && !Bool(dep.lto.Properties.Lto.Full) {
150 dep.lto.Properties.FullDep = true
151 }
152 if thin && !Bool(dep.lto.Properties.Lto.Thin) {
153 dep.lto.Properties.ThinDep = true
154 }
Stephen Craneba090d12017-05-09 15:44:35 -0700155 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700156
157 // Recursively walk static dependencies
158 return true
Stephen Craneba090d12017-05-09 15:44:35 -0700159 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700160
161 // Do not recurse down non-static dependencies
162 return false
Stephen Craneba090d12017-05-09 15:44:35 -0700163 })
164 }
165}
166
167// Create lto variants for modules that need them
168func ltoMutator(mctx android.BottomUpMutatorContext) {
Stephen Crane10cd1872017-09-27 17:01:15 -0700169 if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
170 // Create variations for LTO types required as static
171 // dependencies
172 variationNames := []string{""}
173 if m.lto.Properties.FullDep && !Bool(m.lto.Properties.Lto.Full) {
174 variationNames = append(variationNames, "lto-full")
Stephen Craneba090d12017-05-09 15:44:35 -0700175 }
Stephen Crane10cd1872017-09-27 17:01:15 -0700176 if m.lto.Properties.ThinDep && !Bool(m.lto.Properties.Lto.Thin) {
177 variationNames = append(variationNames, "lto-thin")
178 }
179
180 // Use correct dependencies if LTO property is explicitly set
181 // (mutually exclusive)
182 if Bool(m.lto.Properties.Lto.Full) {
183 mctx.SetDependencyVariation("lto-full")
184 }
185 if Bool(m.lto.Properties.Lto.Thin) {
186 mctx.SetDependencyVariation("lto-thin")
187 }
188
189 if len(variationNames) > 1 {
190 modules := mctx.CreateVariations(variationNames...)
191 for i, name := range variationNames {
192 variation := modules[i].(*Module)
193 // Default module which will be
194 // installed. Variation set above according to
195 // explicit LTO properties
196 if name == "" {
197 continue
198 }
199
200 // LTO properties for dependencies
201 if name == "lto-full" {
202 variation.lto.Properties.Lto.Full = boolPtr(true)
203 variation.lto.Properties.Lto.Thin = boolPtr(false)
204 }
205 if name == "lto-thin" {
206 variation.lto.Properties.Lto.Full = boolPtr(false)
207 variation.lto.Properties.Lto.Thin = boolPtr(true)
208 }
209 variation.Properties.PreventInstall = true
210 variation.Properties.HideFromMake = true
211 variation.lto.Properties.FullDep = false
212 variation.lto.Properties.ThinDep = false
213 }
214 }
Stephen Craneba090d12017-05-09 15:44:35 -0700215 }
216}