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 ( |
Liz Kammer | b2fc470 | 2021-06-25 14:53:40 -0400 | [diff] [blame] | 18 | "github.com/google/blueprint/proptools" |
| 19 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | ) |
| 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 | |
| 41 | type LTOProperties struct { |
| 42 | // Lto must violate capitialization style for acronyms so that it can be |
| 43 | // referred to in blueprint files as "lto" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 44 | Lto struct { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 45 | Never *bool `android:"arch_variant"` |
| 46 | Full *bool `android:"arch_variant"` |
| 47 | Thin *bool `android:"arch_variant"` |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 48 | } `android:"arch_variant"` |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 49 | |
| 50 | // Dep properties indicate that this module needs to be built with LTO |
| 51 | // since it is an object dependency of an LTO module. |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 52 | FullDep bool `blueprint:"mutated"` |
| 53 | ThinDep bool `blueprint:"mutated"` |
| 54 | NoLtoDep bool `blueprint:"mutated"` |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 55 | |
| 56 | // Use clang lld instead of gnu ld. |
| 57 | Use_clang_lld *bool |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 58 | |
| 59 | // Use -fwhole-program-vtables cflag. |
| 60 | Whole_program_vtables *bool |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | type lto struct { |
| 64 | Properties LTOProperties |
| 65 | } |
| 66 | |
| 67 | func (lto *lto) props() []interface{} { |
| 68 | return []interface{}{<o.Properties} |
| 69 | } |
| 70 | |
| 71 | func (lto *lto) begin(ctx BaseModuleContext) { |
Yi Kong | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 72 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { |
Liz Kammer | b2fc470 | 2021-06-25 14:53:40 -0400 | [diff] [blame] | 73 | lto.Properties.Lto.Never = proptools.BoolPtr(true) |
Yi Kong | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 74 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 77 | func (lto *lto) useClangLld(ctx BaseModuleContext) bool { |
| 78 | if lto.Properties.Use_clang_lld != nil { |
| 79 | return Bool(lto.Properties.Use_clang_lld) |
| 80 | } |
Dan Willemsen | fa2aee1 | 2018-10-21 19:47:01 -0700 | [diff] [blame] | 81 | return true |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 84 | func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags { |
Mitch Phillips | 5007c4a | 2022-03-02 01:25:22 +0000 | [diff] [blame] | 85 | // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations. |
| 86 | // LTO breaks fuzzer builds. |
| 87 | if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) { |
| 88 | return flags |
| 89 | } |
| 90 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 91 | if lto.LTO(ctx) { |
| 92 | var ltoCFlag string |
| 93 | var ltoLdFlag string |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 94 | if lto.ThinLTO() { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 95 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
| 96 | } else if lto.FullLTO() { |
| 97 | ltoCFlag = "-flto" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 98 | } else { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 99 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
| 100 | ltoLdFlag = "-Wl,--lto-O0" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 103 | flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag) |
| 104 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag) |
| 105 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag) |
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 | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 111 | if (lto.DefaultThinLTO(ctx) || 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 | 4ef5459 | 2022-02-14 20:00:10 +0800 | [diff] [blame] | 126 | if !ctx.isPgoCompile() && !ctx.isAfdoCompile() { |
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 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 134 | func (lto *lto) LTO(ctx BaseModuleContext) bool { |
| 135 | return lto.ThinLTO() || lto.FullLTO() || lto.DefaultThinLTO(ctx) |
| 136 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 137 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 138 | func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool { |
| 139 | host := ctx.Host() |
| 140 | vndk := ctx.isVndk() // b/169217596 |
Yi Kong | 04e459d | 2021-10-29 17:53:27 +0000 | [diff] [blame] | 141 | return GlobalThinLTO(ctx) && !lto.Never() && !host && !vndk |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | func (lto *lto) FullLTO() bool { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 145 | return lto != nil && Bool(lto.Properties.Lto.Full) |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | func (lto *lto) ThinLTO() bool { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 149 | return lto != nil && Bool(lto.Properties.Lto.Thin) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame] | 152 | func (lto *lto) Never() bool { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 153 | return lto != nil && Bool(lto.Properties.Lto.Never) |
| 154 | } |
| 155 | |
| 156 | func GlobalThinLTO(ctx android.BaseModuleContext) bool { |
| 157 | return ctx.Config().IsEnvTrue("GLOBAL_THINLTO") |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 160 | // Propagate lto requirements down from binaries |
| 161 | func ltoDepsMutator(mctx android.TopDownMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 162 | globalThinLTO := GlobalThinLTO(mctx) |
| 163 | |
| 164 | if m, ok := mctx.Module().(*Module); ok { |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 165 | full := m.lto.FullLTO() |
| 166 | thin := m.lto.ThinLTO() |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 167 | never := m.lto.Never() |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 168 | if full && thin { |
| 169 | mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive") |
| 170 | } |
| 171 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 172 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 173 | tag := mctx.OtherModuleDependencyTag(dep) |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 174 | libTag, isLibTag := tag.(libraryDependencyTag) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 175 | |
| 176 | // Do not recurse down non-static dependencies |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 177 | if isLibTag { |
Colin Cross | 4fbb5e0 | 2020-07-29 12:55:55 -0700 | [diff] [blame] | 178 | if !libTag.static() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 179 | return false |
| 180 | } |
| 181 | } else { |
| 182 | if tag != objDepTag && tag != reuseObjTag { |
| 183 | return false |
| 184 | } |
| 185 | } |
| 186 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 187 | if dep, ok := dep.(*Module); ok { |
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 | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 191 | if !globalThinLTO && thin && !dep.lto.ThinLTO() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 192 | dep.lto.Properties.ThinDep = true |
| 193 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 194 | if globalThinLTO && never && !dep.lto.Never() { |
| 195 | dep.lto.Properties.NoLtoDep = true |
| 196 | } |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // Recursively walk static dependencies |
| 200 | return true |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 201 | }) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Create lto variants for modules that need them |
| 206 | func ltoMutator(mctx android.BottomUpMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 207 | globalThinLTO := GlobalThinLTO(mctx) |
| 208 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 209 | if m, ok := mctx.Module().(*Module); ok && m.lto != nil { |
| 210 | // Create variations for LTO types required as static |
| 211 | // dependencies |
| 212 | variationNames := []string{""} |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 213 | if m.lto.Properties.FullDep && !m.lto.FullLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 214 | variationNames = append(variationNames, "lto-full") |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 215 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 216 | if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 217 | variationNames = append(variationNames, "lto-thin") |
| 218 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 219 | if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() { |
| 220 | variationNames = append(variationNames, "lto-none") |
| 221 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 222 | |
| 223 | // Use correct dependencies if LTO property is explicitly set |
| 224 | // (mutually exclusive) |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 225 | if m.lto.FullLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 226 | mctx.SetDependencyVariation("lto-full") |
| 227 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 228 | if !globalThinLTO && m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 229 | mctx.SetDependencyVariation("lto-thin") |
| 230 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 231 | // Never must be the last, it overrides Thin or Full. |
| 232 | if globalThinLTO && m.lto.Never() { |
| 233 | mctx.SetDependencyVariation("lto-none") |
| 234 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 235 | |
| 236 | if len(variationNames) > 1 { |
| 237 | modules := mctx.CreateVariations(variationNames...) |
| 238 | for i, name := range variationNames { |
| 239 | variation := modules[i].(*Module) |
| 240 | // Default module which will be |
| 241 | // installed. Variation set above according to |
| 242 | // explicit LTO properties |
| 243 | if name == "" { |
| 244 | continue |
| 245 | } |
| 246 | |
| 247 | // LTO properties for dependencies |
| 248 | if name == "lto-full" { |
Liz Kammer | b2fc470 | 2021-06-25 14:53:40 -0400 | [diff] [blame] | 249 | variation.lto.Properties.Lto.Full = proptools.BoolPtr(true) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 250 | } |
| 251 | if name == "lto-thin" { |
Liz Kammer | b2fc470 | 2021-06-25 14:53:40 -0400 | [diff] [blame] | 252 | variation.lto.Properties.Lto.Thin = proptools.BoolPtr(true) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 253 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 254 | if name == "lto-none" { |
| 255 | variation.lto.Properties.Lto.Never = proptools.BoolPtr(true) |
| 256 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 257 | variation.Properties.PreventInstall = true |
| 258 | variation.Properties.HideFromMake = true |
| 259 | variation.lto.Properties.FullDep = false |
| 260 | variation.lto.Properties.ThinDep = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 261 | variation.lto.Properties.NoLtoDep = false |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 264 | } |
| 265 | } |