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" |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 19 | |
| 20 | "github.com/google/blueprint/proptools" |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 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 | // |
Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 38 | // This file adds support to soong to automatically propagate LTO options to a |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 39 | // new variant of all static dependencies for each module with LTO enabled. |
| 40 | |
| 41 | type LTOProperties struct { |
Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 42 | // Lto must violate capitalization style for acronyms so that it can be |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 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"` |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 46 | Thin *bool `android:"arch_variant"` |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 47 | } `android:"arch_variant"` |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 48 | |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 49 | LtoEnabled bool `blueprint:"mutated"` |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 50 | LtoDefault bool `blueprint:"mutated"` |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 51 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 52 | // Dep properties indicate that this module needs to be built with LTO |
| 53 | // since it is an object dependency of an LTO module. |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 54 | LtoDep bool `blueprint:"mutated"` |
| 55 | NoLtoDep bool `blueprint:"mutated"` |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 56 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 57 | // Use -fwhole-program-vtables cflag. |
| 58 | Whole_program_vtables *bool |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | type lto struct { |
| 62 | Properties LTOProperties |
| 63 | } |
| 64 | |
| 65 | func (lto *lto) props() []interface{} { |
| 66 | return []interface{}{<o.Properties} |
| 67 | } |
| 68 | |
| 69 | func (lto *lto) begin(ctx BaseModuleContext) { |
Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 70 | // First, determine the module independent default LTO mode. |
Yi Kong | 950c174 | 2023-10-09 19:49:45 +0900 | [diff] [blame] | 71 | ltoDefault := true |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 72 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { |
| 73 | ltoDefault = false |
Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 74 | } else if lto.Never() { |
| 75 | ltoDefault = false |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 76 | } else if ctx.Host() { |
| 77 | // Performance and binary size are less important for host binaries. |
| 78 | ltoDefault = false |
Yi Kong | 13beeed | 2023-07-15 03:09:00 +0900 | [diff] [blame] | 79 | } else if ctx.Arch().ArchType.Multilib == "lib32" { |
| 80 | // LP32 has many subtle issues and less test coverage. |
| 81 | ltoDefault = false |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Then, determine the actual LTO mode to use. If different from `ltoDefault`, a variant needs |
| 85 | // to be created. |
| 86 | ltoEnabled := ltoDefault |
| 87 | if lto.Never() { |
| 88 | ltoEnabled = false |
| 89 | } else if lto.ThinLTO() { |
| 90 | // Module explicitly requests for LTO. |
| 91 | ltoEnabled = true |
| 92 | } else if ctx.testBinary() || ctx.testLibrary() { |
| 93 | // Do not enable LTO for tests for better debugging. |
| 94 | ltoEnabled = false |
| 95 | } else if ctx.isVndk() { |
| 96 | // FIXME: ThinLTO for VNDK produces different output. |
| 97 | // b/169217596 |
| 98 | ltoEnabled = false |
| 99 | } |
| 100 | |
| 101 | lto.Properties.LtoDefault = ltoDefault |
| 102 | lto.Properties.LtoEnabled = ltoEnabled |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 105 | func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 106 | // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves. |
| 107 | // This has be checked late because these properties can be mutated. |
| 108 | if ctx.isCfi() || ctx.isFuzzer() { |
Mitch Phillips | 5007c4a | 2022-03-02 01:25:22 +0000 | [diff] [blame] | 109 | return flags |
| 110 | } |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 111 | if lto.Properties.LtoEnabled { |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 112 | ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"} |
| 113 | var ltoLdFlags []string |
| 114 | |
| 115 | // The module did not explicitly turn on LTO. Only leverage LTO's |
Yi Kong | 8f9ca23 | 2023-07-14 06:15:51 +0000 | [diff] [blame] | 116 | // better dead code elimination and CFG simplification, but do |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 117 | // not perform costly optimizations for a balance between compile |
| 118 | // time, binary size and performance. |
Yi Kong | 9723e33 | 2023-12-04 14:52:53 +0900 | [diff] [blame] | 119 | // Apply the same for Eng builds as well. |
| 120 | if !lto.ThinLTO() || ctx.Config().Eng() { |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 121 | ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0") |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 124 | if Bool(lto.Properties.Whole_program_vtables) { |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 125 | ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables") |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 128 | if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") { |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 129 | // Set appropriate ThinLTO cache policy |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 130 | cacheDirFormat := "-Wl,--thinlto-cache-dir=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 131 | cacheDir := android.PathForOutput(ctx, "thinlto-cache").String() |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 132 | ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 133 | |
| 134 | // Limit the size of the ThinLTO cache to the lesser of 10% of available |
| 135 | // disk space and 10GB. |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 136 | cachePolicyFormat := "-Wl,--thinlto-cache-policy=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 137 | policy := "cache_size=10%:cache_size_bytes=10g" |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 138 | ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Yi Kong | d6ab48c | 2023-08-01 14:12:39 +0900 | [diff] [blame] | 141 | // Reduce the inlining threshold for a better balance of binary size and |
| 142 | // performance. |
| 143 | if !ctx.Darwin() { |
Yi Kong | 9c3f433 | 2023-11-24 17:14:27 +0900 | [diff] [blame] | 144 | if ctx.isAfdoCompile() { |
Yi Kong | d6ab48c | 2023-08-01 14:12:39 +0900 | [diff] [blame] | 145 | ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=40") |
| 146 | } else { |
| 147 | ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5") |
| 148 | } |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 149 | } |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 150 | |
AdityaK | 76c7385 | 2023-11-14 10:24:59 -0800 | [diff] [blame] | 151 | if !ctx.Config().IsEnvFalse("THINLTO_USE_MLGO") { |
| 152 | // Register allocation MLGO flags for ARM64. |
| 153 | if ctx.Arch().ArchType == android.Arm64 { |
AdityaK | 76c7385 | 2023-11-14 10:24:59 -0800 | [diff] [blame] | 154 | ltoLdFlags = append(ltoLdFlags, "-Wl,-mllvm,-regalloc-enable-advisor=release") |
| 155 | } |
Yi Kong | 0fa503d | 2023-11-07 14:12:51 +0900 | [diff] [blame] | 156 | // Flags for training MLGO model. |
| 157 | if ctx.Config().IsEnvTrue("THINLTO_EMIT_INDEXES_AND_IMPORTS") { |
| 158 | ltoLdFlags = append(ltoLdFlags, "-Wl,--save-temps=import") |
| 159 | ltoLdFlags = append(ltoLdFlags, "-Wl,--thinlto-emit-index-files") |
| 160 | } |
Yi Kong | b8eaee6 | 2023-10-31 21:58:42 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 163 | flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...) |
| 164 | flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...) |
| 165 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...) |
| 166 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 167 | } |
| 168 | return flags |
| 169 | } |
| 170 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 171 | func (lto *lto) ThinLTO() bool { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 172 | return lto != nil && proptools.Bool(lto.Properties.Lto.Thin) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame] | 175 | func (lto *lto) Never() bool { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 176 | return lto != nil && proptools.Bool(lto.Properties.Lto.Never) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 179 | // Propagate lto requirements down from binaries |
| 180 | func ltoDepsMutator(mctx android.TopDownMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 181 | if m, ok := mctx.Module().(*Module); ok { |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 182 | if m.lto == nil || m.lto.Properties.LtoEnabled == m.lto.Properties.LtoDefault { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 183 | return |
| 184 | } |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 185 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 186 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 187 | tag := mctx.OtherModuleDependencyTag(dep) |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 188 | libTag, isLibTag := tag.(libraryDependencyTag) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 189 | |
| 190 | // Do not recurse down non-static dependencies |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 191 | if isLibTag { |
Colin Cross | 4fbb5e0 | 2020-07-29 12:55:55 -0700 | [diff] [blame] | 192 | if !libTag.static() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 193 | return false |
| 194 | } |
| 195 | } else { |
| 196 | if tag != objDepTag && tag != reuseObjTag { |
| 197 | return false |
| 198 | } |
| 199 | } |
| 200 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 201 | if dep, ok := dep.(*Module); ok { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 202 | if m.lto.Properties.LtoEnabled { |
| 203 | dep.lto.Properties.LtoDep = true |
| 204 | } else { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 205 | dep.lto.Properties.NoLtoDep = true |
| 206 | } |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Recursively walk static dependencies |
| 210 | return true |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 211 | }) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Create lto variants for modules that need them |
| 216 | func ltoMutator(mctx android.BottomUpMutatorContext) { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 217 | if m, ok := mctx.Module().(*Module); ok && m.lto != nil { |
| 218 | // Create variations for LTO types required as static |
| 219 | // dependencies |
| 220 | variationNames := []string{""} |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 221 | if m.lto.Properties.LtoDep { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 222 | variationNames = append(variationNames, "lto-thin") |
| 223 | } |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 224 | if m.lto.Properties.NoLtoDep { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 225 | variationNames = append(variationNames, "lto-none") |
| 226 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 227 | |
Yi Kong | 950c174 | 2023-10-09 19:49:45 +0900 | [diff] [blame] | 228 | if !m.lto.Properties.LtoEnabled { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 229 | mctx.SetDependencyVariation("lto-none") |
| 230 | } |
Yi Kong | 950c174 | 2023-10-09 19:49:45 +0900 | [diff] [blame] | 231 | if m.lto.Properties.LtoEnabled { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 232 | mctx.SetDependencyVariation("lto-thin") |
| 233 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 234 | |
| 235 | if len(variationNames) > 1 { |
| 236 | modules := mctx.CreateVariations(variationNames...) |
| 237 | for i, name := range variationNames { |
| 238 | variation := modules[i].(*Module) |
| 239 | // Default module which will be |
| 240 | // installed. Variation set above according to |
| 241 | // explicit LTO properties |
| 242 | if name == "" { |
| 243 | continue |
| 244 | } |
| 245 | |
| 246 | // LTO properties for dependencies |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 247 | if name == "lto-thin" { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 248 | variation.lto.Properties.LtoEnabled = true |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 249 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 250 | if name == "lto-none" { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 251 | variation.lto.Properties.LtoEnabled = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 252 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 253 | variation.Properties.PreventInstall = true |
| 254 | variation.Properties.HideFromMake = true |
Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 255 | variation.lto.Properties.LtoDefault = m.lto.Properties.LtoDefault |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 256 | variation.lto.Properties.LtoDep = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 257 | variation.lto.Properties.NoLtoDep = false |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 260 | } |
| 261 | } |