| 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 ( | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 18 | "fmt" | 
| Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 19 |  | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 20 | "github.com/google/blueprint" | 
| Liz Kammer | 729aaf4 | 2022-09-16 12:39:42 -0400 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 22 |  | 
|  | 23 | "android/soong/android" | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 24 | ) | 
|  | 25 |  | 
|  | 26 | // LTO (link-time optimization) allows the compiler to optimize and generate | 
|  | 27 | // code for the entire module at link time, rather than per-compilation | 
|  | 28 | // unit. LTO is required for Clang CFI and other whole-program optimization | 
|  | 29 | // techniques. LTO also allows cross-compilation unit optimizations that should | 
|  | 30 | // result in faster and smaller code, at the expense of additional compilation | 
|  | 31 | // time. | 
|  | 32 | // | 
|  | 33 | // To properly build a module with LTO, the module and all recursive static | 
|  | 34 | // dependencies should be compiled with -flto which directs the compiler to emit | 
|  | 35 | // bitcode rather than native object files. These bitcode files are then passed | 
|  | 36 | // by the linker to the LLVM plugin for compilation at link time. Static | 
|  | 37 | // dependencies not built as bitcode will still function correctly but cannot be | 
|  | 38 | // optimized at link time and may not be compatible with features that require | 
|  | 39 | // LTO, such as CFI. | 
|  | 40 | // | 
| Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 41 | // 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] | 42 | // new variant of all static dependencies for each module with LTO enabled. | 
|  | 43 |  | 
|  | 44 | type LTOProperties struct { | 
| Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 45 | // Lto must violate capitalization style for acronyms so that it can be | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 46 | // referred to in blueprint files as "lto" | 
| Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 47 | Lto struct { | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 48 | Never *bool `android:"arch_variant"` | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 49 | Thin  *bool `android:"arch_variant"` | 
| Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 50 | } `android:"arch_variant"` | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 51 |  | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 52 | LtoEnabled bool `blueprint:"mutated"` | 
| Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 53 | LtoDefault bool `blueprint:"mutated"` | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 54 |  | 
| Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 55 | // Use -fwhole-program-vtables cflag. | 
|  | 56 | Whole_program_vtables *bool | 
| Yi Kong | df0289b | 2024-03-28 06:10:30 +0000 | [diff] [blame] | 57 |  | 
|  | 58 | // Use --lto-O0 flag. | 
|  | 59 | Lto_O0 *bool | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | type lto struct { | 
|  | 63 | Properties LTOProperties | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | func (lto *lto) props() []interface{} { | 
|  | 67 | return []interface{}{<o.Properties} | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | func (lto *lto) begin(ctx BaseModuleContext) { | 
| Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 71 | // First, determine the module independent default LTO mode. | 
| Yi Kong | 950c174 | 2023-10-09 19:49:45 +0900 | [diff] [blame] | 72 | ltoDefault := true | 
| Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 73 | if ctx.Config().IsEnvTrue("DISABLE_LTO") { | 
|  | 74 | ltoDefault = false | 
| Yi Kong | 577a73a | 2023-10-05 05:03:42 +0000 | [diff] [blame] | 75 | } else if lto.Never() { | 
|  | 76 | ltoDefault = false | 
| Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 77 | } else if ctx.Host() { | 
|  | 78 | // Performance and binary size are less important for host binaries. | 
|  | 79 | ltoDefault = false | 
| Yi Kong | 13beeed | 2023-07-15 03:09:00 +0900 | [diff] [blame] | 80 | } else if ctx.Arch().ArchType.Multilib == "lib32" { | 
|  | 81 | // LP32 has many subtle issues and less test coverage. | 
|  | 82 | ltoDefault = false | 
| Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | // Then, determine the actual LTO mode to use. If different from `ltoDefault`, a variant needs | 
|  | 86 | // to be created. | 
|  | 87 | ltoEnabled := ltoDefault | 
|  | 88 | if lto.Never() { | 
|  | 89 | ltoEnabled = false | 
|  | 90 | } else if lto.ThinLTO() { | 
|  | 91 | // Module explicitly requests for LTO. | 
|  | 92 | ltoEnabled = true | 
|  | 93 | } else if ctx.testBinary() || ctx.testLibrary() { | 
|  | 94 | // Do not enable LTO for tests for better debugging. | 
|  | 95 | ltoEnabled = false | 
| Yi Kong | add6375 | 2023-06-26 17:39:46 +0900 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
|  | 98 | lto.Properties.LtoDefault = ltoDefault | 
|  | 99 | lto.Properties.LtoEnabled = ltoEnabled | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
| Colin Cross | 3513fb1 | 2024-01-24 14:44:47 -0800 | [diff] [blame] | 102 | func (lto *lto) flags(ctx ModuleContext, flags Flags) Flags { | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 103 | // TODO(b/131771163): CFI and Fuzzer controls LTO flags by themselves. | 
|  | 104 | // This has be checked late because these properties can be mutated. | 
|  | 105 | if ctx.isCfi() || ctx.isFuzzer() { | 
| Mitch Phillips | 5007c4a | 2022-03-02 01:25:22 +0000 | [diff] [blame] | 106 | return flags | 
|  | 107 | } | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 108 | if lto.Properties.LtoEnabled { | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 109 | ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"} | 
|  | 110 | var ltoLdFlags []string | 
|  | 111 |  | 
| Yi Kong | df0289b | 2024-03-28 06:10:30 +0000 | [diff] [blame] | 112 | // Do not perform costly LTO optimizations for Eng builds. | 
| Yi Kong | ae87072 | 2024-06-16 13:05:05 +0900 | [diff] [blame] | 113 | if Bool(lto.Properties.Lto_O0) || ctx.optimizeForSize() || ctx.Config().Eng() { | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 114 | ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0") | 
| Yi Kong | 244bf07 | 2017-08-29 11:10:09 +0800 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 117 | if Bool(lto.Properties.Whole_program_vtables) { | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 118 | ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables") | 
| Yi Kong | 2d01fe2 | 2020-09-21 01:18:32 +0800 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 121 | if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") { | 
| Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 122 | // Set appropriate ThinLTO cache policy | 
| Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 123 | cacheDirFormat := "-Wl,--thinlto-cache-dir=" | 
| Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 124 | cacheDir := android.PathForOutput(ctx, "thinlto-cache").String() | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 125 | ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir) | 
| Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 126 |  | 
|  | 127 | // Limit the size of the ThinLTO cache to the lesser of 10% of available | 
|  | 128 | // disk space and 10GB. | 
| Yi Kong | 630b960 | 2019-03-22 21:28:39 -0700 | [diff] [blame] | 129 | cachePolicyFormat := "-Wl,--thinlto-cache-policy=" | 
| Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 130 | policy := "cache_size=10%:cache_size_bytes=10g" | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 131 | ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy) | 
| Yi Kong | 8aeaa71 | 2018-02-16 20:36:16 +0800 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Yi Kong | d6ab48c | 2023-08-01 14:12:39 +0900 | [diff] [blame] | 134 | // Reduce the inlining threshold for a better balance of binary size and | 
|  | 135 | // performance. | 
|  | 136 | if !ctx.Darwin() { | 
| Colin Cross | 3513fb1 | 2024-01-24 14:44:47 -0800 | [diff] [blame] | 137 | if ctx.isAfdoCompile(ctx) { | 
| Yi Kong | d6ab48c | 2023-08-01 14:12:39 +0900 | [diff] [blame] | 138 | ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=40") | 
|  | 139 | } else { | 
|  | 140 | ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5") | 
|  | 141 | } | 
| Yi Kong | 7e53c57 | 2018-02-14 18:16:12 +0800 | [diff] [blame] | 142 | } | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 143 |  | 
| AdityaK | 76c7385 | 2023-11-14 10:24:59 -0800 | [diff] [blame] | 144 | if !ctx.Config().IsEnvFalse("THINLTO_USE_MLGO") { | 
|  | 145 | // Register allocation MLGO flags for ARM64. | 
| Yi Kong | 2cd77d6 | 2024-06-06 03:05:04 +0900 | [diff] [blame] | 146 | if ctx.Arch().ArchType == android.Arm64 && !ctx.optimizeForSize() { | 
| AdityaK | 76c7385 | 2023-11-14 10:24:59 -0800 | [diff] [blame] | 147 | ltoLdFlags = append(ltoLdFlags, "-Wl,-mllvm,-regalloc-enable-advisor=release") | 
|  | 148 | } | 
| Yi Kong | 0fa503d | 2023-11-07 14:12:51 +0900 | [diff] [blame] | 149 | // Flags for training MLGO model. | 
|  | 150 | if ctx.Config().IsEnvTrue("THINLTO_EMIT_INDEXES_AND_IMPORTS") { | 
|  | 151 | ltoLdFlags = append(ltoLdFlags, "-Wl,--save-temps=import") | 
|  | 152 | ltoLdFlags = append(ltoLdFlags, "-Wl,--thinlto-emit-index-files") | 
|  | 153 | } | 
| Yi Kong | b8eaee6 | 2023-10-31 21:58:42 +0900 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Yi Kong | b9d5046 | 2023-07-03 16:59:33 +0900 | [diff] [blame] | 156 | flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...) | 
|  | 157 | flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...) | 
|  | 158 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...) | 
|  | 159 | flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...) | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 160 | } | 
|  | 161 | return flags | 
|  | 162 | } | 
|  | 163 |  | 
| Yi Kong | 93718e0 | 2020-09-21 21:41:03 +0800 | [diff] [blame] | 164 | func (lto *lto) ThinLTO() bool { | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 165 | return lto != nil && proptools.Bool(lto.Properties.Lto.Thin) | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Yi Kong | f43ff05 | 2020-09-28 14:41:50 +0800 | [diff] [blame] | 168 | func (lto *lto) Never() bool { | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 169 | return lto != nil && proptools.Bool(lto.Properties.Lto.Never) | 
| Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 172 | func ltoPropagateViaDepTag(tag blueprint.DependencyTag) bool { | 
|  | 173 | libTag, isLibTag := tag.(libraryDependencyTag) | 
|  | 174 | // Do not recurse down non-static dependencies | 
|  | 175 | if isLibTag { | 
|  | 176 | return libTag.static() | 
|  | 177 | } else { | 
|  | 178 | return tag == objDepTag || tag == reuseObjTag || tag == staticVariantTag | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 182 | // ltoTransitionMutator creates LTO variants of cc modules.  Variant "" is the default variant, which may | 
|  | 183 | // or may not have LTO enabled depending on the config and the module's type and properties.  "lto-thin" or | 
|  | 184 | // "lto-none" variants are created when a module needs to compile in the non-default state for that module. | 
|  | 185 | type ltoTransitionMutator struct{} | 
|  | 186 |  | 
|  | 187 | const LTO_NONE_VARIATION = "lto-none" | 
|  | 188 | const LTO_THIN_VARIATION = "lto-thin" | 
|  | 189 |  | 
|  | 190 | func (l *ltoTransitionMutator) Split(ctx android.BaseModuleContext) []string { | 
|  | 191 | return []string{""} | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | func (l *ltoTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 195 | if m, ok := ctx.Module().(*Module); ok && m.lto != nil { | 
|  | 196 | if !ltoPropagateViaDepTag(ctx.DepTag()) { | 
|  | 197 | return "" | 
| Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 198 | } | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 199 |  | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 200 | if sourceVariation != "" { | 
|  | 201 | return sourceVariation | 
| Yi Kong | 8ea56f9 | 2021-10-14 01:07:42 +0800 | [diff] [blame] | 202 | } | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 203 |  | 
|  | 204 | // Always request an explicit variation, IncomingTransition will rewrite it back to the default variation | 
|  | 205 | // if necessary. | 
| Yi Kong | 950c174 | 2023-10-09 19:49:45 +0900 | [diff] [blame] | 206 | if m.lto.Properties.LtoEnabled { | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 207 | return LTO_THIN_VARIATION | 
|  | 208 | } else { | 
|  | 209 | return LTO_NONE_VARIATION | 
| Yi Kong | 895d241 | 2023-06-08 01:48:42 +0900 | [diff] [blame] | 210 | } | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 211 | } | 
|  | 212 | return "" | 
|  | 213 | } | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 214 |  | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 215 | func (l *ltoTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { | 
|  | 216 | if m, ok := ctx.Module().(*Module); ok && m.lto != nil { | 
|  | 217 | if m.lto.Never() { | 
|  | 218 | return "" | 
| Stephen Crane | 10cd187 | 2017-09-27 17:01:15 -0700 | [diff] [blame] | 219 | } | 
| Colin Cross | 6ac83a8 | 2024-01-23 11:23:10 -0800 | [diff] [blame] | 220 | // Rewrite explicit variations back to the default variation if the default variation matches. | 
|  | 221 | if incomingVariation == LTO_THIN_VARIATION && m.lto.Properties.LtoDefault { | 
|  | 222 | return "" | 
|  | 223 | } else if incomingVariation == LTO_NONE_VARIATION && !m.lto.Properties.LtoDefault { | 
|  | 224 | return "" | 
|  | 225 | } | 
|  | 226 | return incomingVariation | 
|  | 227 | } | 
|  | 228 | return "" | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | func (l *ltoTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { | 
|  | 232 | // Default module which will be installed. Variation set above according to explicit LTO properties. | 
|  | 233 | if variation == "" { | 
|  | 234 | return | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | if m, ok := ctx.Module().(*Module); ok && m.lto != nil { | 
|  | 238 | // Non-default variation, set the LTO properties to match the variation. | 
|  | 239 | switch variation { | 
|  | 240 | case LTO_THIN_VARIATION: | 
|  | 241 | m.lto.Properties.LtoEnabled = true | 
|  | 242 | case LTO_NONE_VARIATION: | 
|  | 243 | m.lto.Properties.LtoEnabled = false | 
|  | 244 | default: | 
|  | 245 | panic(fmt.Errorf("unknown variation %s", variation)) | 
|  | 246 | } | 
|  | 247 | // Non-default variations are never installed. | 
|  | 248 | m.Properties.PreventInstall = true | 
|  | 249 | m.Properties.HideFromMake = true | 
| Stephen Crane | ba090d1 | 2017-05-09 15:44:35 -0700 | [diff] [blame] | 250 | } | 
|  | 251 | } |