Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 18 | "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 | |
| 39 | type LTOProperties struct { |
| 40 | // Lto must violate capitialization style for acronyms so that it can be |
| 41 | // referred to in blueprint files as "lto" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 42 | Lto struct { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 43 | Never *bool `android:"arch_variant"` |
| 44 | Full *bool `android:"arch_variant"` |
| 45 | Thin *bool `android:"arch_variant"` |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 46 | } `android:"arch_variant"` |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 47 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 48 | GlobalThin *bool `blueprint:"mutated"` |
| 49 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 50 | // Dep properties indicate that this module needs to be built with LTO |
| 51 | // since it is an object dependency of an LTO module. |
| 52 | FullDep bool `blueprint:"mutated"` |
| 53 | ThinDep bool `blueprint:"mutated"` |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 54 | |
| 55 | // Use clang lld instead of gnu ld. |
| 56 | Use_clang_lld *bool |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 57 | |
| 58 | // Use -fwhole-program-vtables cflag. |
| 59 | Whole_program_vtables *bool |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type lto struct { |
| 63 | Properties LTOProperties |
| 64 | } |
| 65 | |
| 66 | func (lto *lto) props() []interface{} { |
| 67 | return []interface{}{<o.Properties} |
| 68 | } |
| 69 | |
| 70 | func (lto *lto) begin(ctx BaseModuleContext) { |
Yi Kong | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 71 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { |
| 72 | lto.Properties.Lto.Never = boolPtr(true) |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 73 | } else if ctx.Config().IsEnvTrue("GLOBAL_THINLTO") { |
| 74 | lto.Properties.GlobalThin = boolPtr(true) |
Yi Kong | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 75 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func (lto *lto) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 79 | return deps |
| 80 | } |
| 81 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 82 | func (lto *lto) useClangLld(ctx BaseModuleContext) bool { |
| 83 | if lto.Properties.Use_clang_lld != nil { |
| 84 | return Bool(lto.Properties.Use_clang_lld) |
| 85 | } |
Dan Willemsen | fa2aee1 | 2018-10-21 19:47:01 -0700 | [diff] [blame] | 86 | return true |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 89 | func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags { |
Mitch Phillips | 34b493f | 2019-08-02 16:57:55 -0700 | [diff] [blame] | 90 | // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations. |
| 91 | // LTO breaks fuzzer builds. |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 92 | if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) { |
Mitch Phillips | 34b493f | 2019-08-02 16:57:55 -0700 | [diff] [blame] | 93 | return flags |
| 94 | } |
| 95 | |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 96 | if lto.LTO() { |
| 97 | var ltoFlag string |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 98 | if lto.ThinLTO() { |
Yi Kong | 6925d2b | 2019-03-05 14:11:41 -0800 | [diff] [blame] | 99 | ltoFlag = "-flto=thin -fsplit-lto-unit" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 100 | } else { |
| 101 | ltoFlag = "-flto" |
| 102 | } |
| 103 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 104 | flags.Local.CFlags = append(flags.Local.CFlags, ltoFlag) |
| 105 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoFlag) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 106 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 107 | if Bool(lto.Properties.Whole_program_vtables) { |
| 108 | flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables") |
| 109 | } |
| 110 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 111 | if lto.ThinLTO() && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) { |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 112 | // Set appropriate ThinLTO cache policy |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 113 | cacheDirFormat := "-Wl,--thinlto-cache-dir=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 114 | cacheDir := android.PathForOutput(ctx, "thinlto-cache").String() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 115 | flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 116 | |
| 117 | // Limit the size of the ThinLTO cache to the lesser of 10% of available |
| 118 | // disk space and 10GB. |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 119 | cachePolicyFormat := "-Wl,--thinlto-cache-policy=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 120 | policy := "cache_size=10%:cache_size_bytes=10g" |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 121 | flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 124 | // If the module does not have a profile, be conservative and limit cross TU inline |
| 125 | // limit to 5 LLVM IR instructions, to balance binary size increase and performance. |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 126 | if !ctx.isPgoCompile() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 127 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 128 | "-Wl,-plugin-opt,-import-instr-limit=5") |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 129 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 130 | } |
| 131 | return flags |
| 132 | } |
| 133 | |
| 134 | // Can be called with a null receiver |
| 135 | func (lto *lto) LTO() bool { |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame^] | 136 | if lto == nil || lto.Never() { |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 137 | return false |
| 138 | } |
| 139 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 140 | return lto.FullLTO() || lto.ThinLTO() |
| 141 | } |
| 142 | |
| 143 | func (lto *lto) FullLTO() bool { |
| 144 | return Bool(lto.Properties.Lto.Full) |
| 145 | } |
| 146 | |
| 147 | func (lto *lto) ThinLTO() bool { |
| 148 | if Bool(lto.Properties.GlobalThin) { |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame^] | 149 | if !lto.Never() && !lto.FullLTO() { |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 150 | return true |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return Bool(lto.Properties.Lto.Thin) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 157 | // Is lto.never explicitly set to true? |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame^] | 158 | func (lto *lto) Never() bool { |
| 159 | return Bool(lto.Properties.Lto.Never) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 162 | // Propagate lto requirements down from binaries |
| 163 | func ltoDepsMutator(mctx android.TopDownMutatorContext) { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 164 | if m, ok := mctx.Module().(*Module); ok && m.lto.LTO() { |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 165 | full := m.lto.FullLTO() |
| 166 | thin := m.lto.ThinLTO() |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 167 | if full && thin { |
| 168 | mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive") |
| 169 | } |
| 170 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 171 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 172 | tag := mctx.OtherModuleDependencyTag(dep) |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 173 | libTag, isLibTag := tag.(libraryDependencyTag) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 174 | |
| 175 | // Do not recurse down non-static dependencies |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 176 | if isLibTag { |
Colin Cross | 4fbb5e0 | 2020-07-29 12:55:55 -0700 | [diff] [blame] | 177 | if !libTag.static() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 178 | return false |
| 179 | } |
| 180 | } else { |
| 181 | if tag != objDepTag && tag != reuseObjTag { |
| 182 | return false |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if dep, ok := dep.(*Module); ok && dep.lto != nil && |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame^] | 187 | !dep.lto.Never() { |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 188 | if full && !dep.lto.FullLTO() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 189 | dep.lto.Properties.FullDep = true |
| 190 | } |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 191 | if thin && !dep.lto.ThinLTO() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 192 | dep.lto.Properties.ThinDep = true |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Recursively walk static dependencies |
| 197 | return true |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 198 | }) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Create lto variants for modules that need them |
| 203 | func ltoMutator(mctx android.BottomUpMutatorContext) { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 204 | if m, ok := mctx.Module().(*Module); ok && m.lto != nil { |
| 205 | // Create variations for LTO types required as static |
| 206 | // dependencies |
| 207 | variationNames := []string{""} |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 208 | if m.lto.Properties.FullDep && !m.lto.FullLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 209 | variationNames = append(variationNames, "lto-full") |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 210 | } |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 211 | if m.lto.Properties.ThinDep && !m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 212 | variationNames = append(variationNames, "lto-thin") |
| 213 | } |
| 214 | |
| 215 | // Use correct dependencies if LTO property is explicitly set |
| 216 | // (mutually exclusive) |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 217 | if m.lto.FullLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 218 | mctx.SetDependencyVariation("lto-full") |
| 219 | } |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 220 | if m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 221 | mctx.SetDependencyVariation("lto-thin") |
| 222 | } |
| 223 | |
| 224 | if len(variationNames) > 1 { |
| 225 | modules := mctx.CreateVariations(variationNames...) |
| 226 | for i, name := range variationNames { |
| 227 | variation := modules[i].(*Module) |
| 228 | // Default module which will be |
| 229 | // installed. Variation set above according to |
| 230 | // explicit LTO properties |
| 231 | if name == "" { |
| 232 | continue |
| 233 | } |
| 234 | |
| 235 | // LTO properties for dependencies |
| 236 | if name == "lto-full" { |
| 237 | variation.lto.Properties.Lto.Full = boolPtr(true) |
| 238 | variation.lto.Properties.Lto.Thin = boolPtr(false) |
| 239 | } |
| 240 | if name == "lto-thin" { |
| 241 | variation.lto.Properties.Lto.Full = boolPtr(false) |
| 242 | variation.lto.Properties.Lto.Thin = boolPtr(true) |
| 243 | } |
| 244 | variation.Properties.PreventInstall = true |
| 245 | variation.Properties.HideFromMake = true |
| 246 | variation.lto.Properties.FullDep = false |
| 247 | variation.lto.Properties.ThinDep = false |
| 248 | } |
| 249 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 250 | } |
| 251 | } |