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