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