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