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 | |
| 49 | // Dep properties indicate that this module needs to be built with LTO |
| 50 | // since it is an object dependency of an LTO module. |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 51 | ThinEnabled bool `blueprint:"mutated"` |
| 52 | NoLtoEnabled bool `blueprint:"mutated"` |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 53 | ThinDep 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 | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 69 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 70 | lto.Properties.NoLtoEnabled = true |
Yi Kong | 03d383d | 2018-01-31 15:15:08 -0800 | [diff] [blame] | 71 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 74 | func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags { |
Mitch Phillips | 5007c4a | 2022-03-02 01:25:22 +0000 | [diff] [blame] | 75 | // TODO(b/131771163): Disable LTO when using explicit fuzzing configurations. |
| 76 | // LTO breaks fuzzer builds. |
| 77 | if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) { |
| 78 | return flags |
| 79 | } |
| 80 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 81 | if lto.LTO(ctx) { |
| 82 | var ltoCFlag string |
| 83 | var ltoLdFlag string |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 84 | if lto.ThinLTO() { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 85 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 86 | } else { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 87 | ltoCFlag = "-flto=thin -fsplit-lto-unit" |
| 88 | ltoLdFlag = "-Wl,--lto-O0" |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 91 | flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag) |
Yi Kong | 5e0f405 | 2022-09-09 01:58:18 +0800 | [diff] [blame] | 92 | flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 93 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag) |
| 94 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 95 | |
Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 96 | if Bool(lto.Properties.Whole_program_vtables) { |
| 97 | flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables") |
| 98 | } |
| 99 | |
Yi Kong | a7a345d | 2023-05-28 20:19:39 -0700 | [diff] [blame] | 100 | if (lto.DefaultThinLTO(ctx) || lto.ThinLTO()) && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") { |
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() |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 104 | flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 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" |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 110 | flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy) |
Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 113 | // If the module does not have a profile, be conservative and limit cross TU inline |
| 114 | // 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] | 115 | if !ctx.isPgoCompile() && !ctx.isAfdoCompile() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 116 | flags.Local.LdFlags = append(flags.Local.LdFlags, |
Yi Kong | 2f5f16d | 2020-09-23 00:54:50 +0800 | [diff] [blame] | 117 | "-Wl,-plugin-opt,-import-instr-limit=5") |
Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 118 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 119 | } |
| 120 | return flags |
| 121 | } |
| 122 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 123 | func (lto *lto) LTO(ctx BaseModuleContext) bool { |
Yi Kong | 0713e33 | 2023-05-31 14:15:22 +0900 | [diff] [blame] | 124 | return lto.ThinLTO() || lto.DefaultThinLTO(ctx) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 125 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 126 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 127 | func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool { |
Yi Kong | c702ebd | 2022-08-19 16:02:45 +0800 | [diff] [blame] | 128 | // LP32 has many subtle issues and less test coverage. |
Yi Kong | 2121d16 | 2022-08-17 03:54:54 +0800 | [diff] [blame] | 129 | lib32 := ctx.Arch().ArchType.Multilib == "lib32" |
Yi Kong | 0713e33 | 2023-05-31 14:15:22 +0900 | [diff] [blame] | 130 | // CFI adds LTO flags by itself. |
Yi Kong | c702ebd | 2022-08-19 16:02:45 +0800 | [diff] [blame] | 131 | cfi := ctx.isCfi() |
Yi Kong | 56fc1b6 | 2022-09-06 16:24:00 +0800 | [diff] [blame] | 132 | // Performance and binary size are less important for host binaries and tests. |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 133 | host := ctx.Host() |
Yi Kong | 56fc1b6 | 2022-09-06 16:24:00 +0800 | [diff] [blame] | 134 | test := ctx.testBinary() || ctx.testLibrary() |
Yi Kong | c702ebd | 2022-08-19 16:02:45 +0800 | [diff] [blame] | 135 | // FIXME: ThinLTO for VNDK produces different output. |
| 136 | // b/169217596 |
| 137 | vndk := ctx.isVndk() |
Yi Kong | 56fc1b6 | 2022-09-06 16:24:00 +0800 | [diff] [blame] | 138 | return GlobalThinLTO(ctx) && !lto.Never() && !lib32 && !cfi && !host && !test && !vndk |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 141 | func (lto *lto) ThinLTO() bool { |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 142 | return lto != nil && (proptools.Bool(lto.Properties.Lto.Thin) || lto.Properties.ThinEnabled) |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame] | 145 | func (lto *lto) Never() bool { |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 146 | return lto != nil && (proptools.Bool(lto.Properties.Lto.Never) || lto.Properties.NoLtoEnabled) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func GlobalThinLTO(ctx android.BaseModuleContext) bool { |
Yi Kong | 7689c64 | 2022-08-23 09:24:48 +0000 | [diff] [blame] | 150 | return ctx.Config().IsEnvTrue("GLOBAL_THINLTO") |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 153 | // Propagate lto requirements down from binaries |
| 154 | func ltoDepsMutator(mctx android.TopDownMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 155 | globalThinLTO := GlobalThinLTO(mctx) |
| 156 | |
| 157 | if m, ok := mctx.Module().(*Module); ok { |
Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 158 | thin := m.lto.ThinLTO() |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 159 | never := m.lto.Never() |
Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 160 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 161 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 162 | tag := mctx.OtherModuleDependencyTag(dep) |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 163 | libTag, isLibTag := tag.(libraryDependencyTag) |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 164 | |
| 165 | // Do not recurse down non-static dependencies |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 166 | if isLibTag { |
Colin Cross | 4fbb5e0 | 2020-07-29 12:55:55 -0700 | [diff] [blame] | 167 | if !libTag.static() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 168 | return false |
| 169 | } |
| 170 | } else { |
| 171 | if tag != objDepTag && tag != reuseObjTag { |
| 172 | return false |
| 173 | } |
| 174 | } |
| 175 | |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 176 | if dep, ok := dep.(*Module); ok { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 177 | if !globalThinLTO && thin && !dep.lto.ThinLTO() { |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 178 | dep.lto.Properties.ThinDep = true |
| 179 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 180 | if globalThinLTO && never && !dep.lto.Never() { |
| 181 | dep.lto.Properties.NoLtoDep = true |
| 182 | } |
Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Recursively walk static dependencies |
| 186 | return true |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Create lto variants for modules that need them |
| 192 | func ltoMutator(mctx android.BottomUpMutatorContext) { |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 193 | globalThinLTO := GlobalThinLTO(mctx) |
| 194 | |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 195 | if m, ok := mctx.Module().(*Module); ok && m.lto != nil { |
| 196 | // Create variations for LTO types required as static |
| 197 | // dependencies |
| 198 | variationNames := []string{""} |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 199 | if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 200 | variationNames = append(variationNames, "lto-thin") |
| 201 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 202 | if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() { |
| 203 | variationNames = append(variationNames, "lto-none") |
| 204 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 205 | |
| 206 | // Use correct dependencies if LTO property is explicitly set |
| 207 | // (mutually exclusive) |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 208 | if !globalThinLTO && m.lto.ThinLTO() { |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 209 | mctx.SetDependencyVariation("lto-thin") |
| 210 | } |
Yi Kong | 0713e33 | 2023-05-31 14:15:22 +0900 | [diff] [blame] | 211 | // Never must be the last, it overrides Thin. |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 212 | if globalThinLTO && m.lto.Never() { |
| 213 | mctx.SetDependencyVariation("lto-none") |
| 214 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 215 | |
| 216 | if len(variationNames) > 1 { |
| 217 | modules := mctx.CreateVariations(variationNames...) |
| 218 | for i, name := range variationNames { |
| 219 | variation := modules[i].(*Module) |
| 220 | // Default module which will be |
| 221 | // installed. Variation set above according to |
| 222 | // explicit LTO properties |
| 223 | if name == "" { |
| 224 | continue |
| 225 | } |
| 226 | |
| 227 | // LTO properties for dependencies |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 228 | if name == "lto-thin" { |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 229 | variation.lto.Properties.ThinEnabled = true |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 230 | } |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 231 | if name == "lto-none" { |
Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 232 | variation.lto.Properties.NoLtoEnabled = true |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 233 | } |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 234 | variation.Properties.PreventInstall = true |
| 235 | variation.Properties.HideFromMake = true |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 236 | variation.lto.Properties.ThinDep = false |
Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 237 | variation.lto.Properties.NoLtoDep = false |
Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 240 | } |
| 241 | } |