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 | // |
| 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"` |
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"` |
| 50 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 51 | // Dep properties indicate that this module needs to be built with LTO |
| 52 | // since it is an object dependency of an LTO module. |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 53 | LtoDep bool `blueprint:"mutated"` |
| 54 | NoLtoDep bool `blueprint:"mutated"` |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 55 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 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 | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 69 | lto.Properties.LtoEnabled = lto.LTO(ctx) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 72 | func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 73 | // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves. |
| 74 | // This has be checked late because these properties can be mutated. |
| 75 | if ctx.isCfi() || ctx.isFuzzer() { |
Mitch Phillips | 5007c4a | 2022-03-02 01:25:22 +0000 | [diff] [blame] | 76 | return flags |
| 77 | } |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 78 | if lto.Properties.LtoEnabled { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 79 | var ltoCFlag string |
| 80 | var ltoLdFlag string |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 81 | if lto.ThinLTO() { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 82 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 83 | } else { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 84 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
| 85 | ltoLdFlag = "-Wl,--lto-O0" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 86 | } |
| 87 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 88 | flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag) |
Yi Kong | 5e0f405 | 2022-09-09 01:58:18 +0800 | [diff] [blame] | 89 | flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 90 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag) |
| 91 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 92 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 93 | if Bool(lto.Properties.Whole_program_vtables) { |
| 94 | flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables") |
| 95 | } |
| 96 | |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 97 | if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") { |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 98 | // Set appropriate ThinLTO cache policy |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 99 | cacheDirFormat := "-Wl,--thinlto-cache-dir=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 100 | cacheDir := android.PathForOutput(ctx, "thinlto-cache").String() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 101 | flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 102 | |
| 103 | // Limit the size of the ThinLTO cache to the lesser of 10% of available |
| 104 | // disk space and 10GB. |
Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 105 | cachePolicyFormat := "-Wl,--thinlto-cache-policy=" |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 106 | policy := "cache_size=10%:cache_size_bytes=10g" |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 107 | flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 110 | // If the module does not have a profile, be conservative and limit cross TU inline |
| 111 | // limit to 5 LLVM IR instructions, to balance binary size increase and performance. |
A. Cody Schuffelen | 7188b90 | 2023-06-27 19:10:27 -0700 | [diff] [blame] | 112 | if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 113 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 114 | "-Wl,-plugin-opt,-import-instr-limit=5") |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 115 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 116 | } |
| 117 | return flags |
| 118 | } |
| 119 | |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 120 | // Determine which LTO mode to use for the given module. |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 121 | func (lto *lto) LTO(ctx BaseModuleContext) bool { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 122 | if lto.Never() { |
| 123 | return false |
| 124 | } |
| 125 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { |
| 126 | return false |
| 127 | } |
| 128 | // Module explicitly requests for LTO. |
| 129 | if lto.ThinLTO() { |
| 130 | return true |
| 131 | } |
Yi Kong | c702ebd | 2022-08-19 16:02:45 +0800 | [diff] [blame] | 132 | // LP32 has many subtle issues and less test coverage. |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 133 | if ctx.Arch().ArchType.Multilib == "lib32" { |
| 134 | return false |
| 135 | } |
Yi Kong | 56fc1b6 | 2022-09-06 16:24:00 +0800 | [diff] [blame] | 136 | // Performance and binary size are less important for host binaries and tests. |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 137 | if ctx.Host() || ctx.testBinary() || ctx.testLibrary() { |
| 138 | return false |
| 139 | } |
Yi Kong | c702ebd | 2022-08-19 16:02:45 +0800 | [diff] [blame] | 140 | // FIXME: ThinLTO for VNDK produces different output. |
| 141 | // b/169217596 |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 142 | if ctx.isVndk() { |
| 143 | return false |
| 144 | } |
| 145 | return GlobalThinLTO(ctx) |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 148 | func (lto *lto) ThinLTO() bool { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 149 | return lto != nil && proptools.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 | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 153 | return lto != nil && proptools.Bool(lto.Properties.Lto.Never) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func GlobalThinLTO(ctx android.BaseModuleContext) bool { |
Yi Kong | 7689c64 | 2022-08-23 09:24:48 +0000 | [diff] [blame] | 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 | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 162 | defaultLTOMode := GlobalThinLTO(mctx) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 163 | |
| 164 | if m, ok := mctx.Module().(*Module); ok { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 165 | if m.lto == nil || m.lto.Properties.LtoEnabled == defaultLTOMode { |
| 166 | return |
| 167 | } |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 168 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 169 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 170 | tag := mctx.OtherModuleDependencyTag(dep) |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 171 | libTag, isLibTag := tag.(libraryDependencyTag) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 172 | |
| 173 | // Do not recurse down non-static dependencies |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 174 | if isLibTag { |
Colin Cross | 4fbb5e0 | 2020-07-29 12:55:55 -0700 | [diff] [blame] | 175 | if !libTag.static() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 176 | return false |
| 177 | } |
| 178 | } else { |
| 179 | if tag != objDepTag && tag != reuseObjTag { |
| 180 | return false |
| 181 | } |
| 182 | } |
| 183 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 184 | if dep, ok := dep.(*Module); ok { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 185 | if m.lto.Properties.LtoEnabled { |
| 186 | dep.lto.Properties.LtoDep = true |
| 187 | } else { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 188 | dep.lto.Properties.NoLtoDep = true |
| 189 | } |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Recursively walk static dependencies |
| 193 | return true |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 194 | }) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Create lto variants for modules that need them |
| 199 | func ltoMutator(mctx android.BottomUpMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 200 | globalThinLTO := GlobalThinLTO(mctx) |
| 201 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 202 | if m, ok := mctx.Module().(*Module); ok && m.lto != nil { |
| 203 | // Create variations for LTO types required as static |
| 204 | // dependencies |
| 205 | variationNames := []string{""} |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 206 | if m.lto.Properties.LtoDep { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 207 | variationNames = append(variationNames, "lto-thin") |
| 208 | } |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 209 | if m.lto.Properties.NoLtoDep { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 210 | variationNames = append(variationNames, "lto-none") |
| 211 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 212 | |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 213 | if globalThinLTO && !m.lto.Properties.LtoEnabled { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 214 | mctx.SetDependencyVariation("lto-none") |
| 215 | } |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 216 | if !globalThinLTO && m.lto.Properties.LtoEnabled { |
| 217 | mctx.SetDependencyVariation("lto-thin") |
| 218 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 219 | |
| 220 | if len(variationNames) > 1 { |
| 221 | modules := mctx.CreateVariations(variationNames...) |
| 222 | for i, name := range variationNames { |
| 223 | variation := modules[i].(*Module) |
| 224 | // Default module which will be |
| 225 | // installed. Variation set above according to |
| 226 | // explicit LTO properties |
| 227 | if name == "" { |
| 228 | continue |
| 229 | } |
| 230 | |
| 231 | // LTO properties for dependencies |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 232 | if name == "lto-thin" { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 233 | variation.lto.Properties.LtoEnabled = true |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 234 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 235 | if name == "lto-none" { |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 236 | variation.lto.Properties.LtoEnabled = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 237 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 238 | variation.Properties.PreventInstall = true |
| 239 | variation.Properties.HideFromMake = true |
Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 240 | variation.lto.Properties.LtoDep = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 241 | variation.lto.Properties.NoLtoDep = false |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 244 | } |
| 245 | } |