2017-05-10 00:44:35 +02:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
2021-06-25 20:53:40 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
|
2017-05-10 00:44:35 +02:00
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LTO (link-time optimization) allows the compiler to optimize and generate
|
|
|
|
// code for the entire module at link time, rather than per-compilation
|
|
|
|
// unit. LTO is required for Clang CFI and other whole-program optimization
|
|
|
|
// techniques. LTO also allows cross-compilation unit optimizations that should
|
|
|
|
// result in faster and smaller code, at the expense of additional compilation
|
|
|
|
// time.
|
|
|
|
//
|
|
|
|
// To properly build a module with LTO, the module and all recursive static
|
|
|
|
// dependencies should be compiled with -flto which directs the compiler to emit
|
|
|
|
// bitcode rather than native object files. These bitcode files are then passed
|
|
|
|
// by the linker to the LLVM plugin for compilation at link time. Static
|
|
|
|
// dependencies not built as bitcode will still function correctly but cannot be
|
|
|
|
// optimized at link time and may not be compatible with features that require
|
|
|
|
// LTO, such as CFI.
|
|
|
|
//
|
|
|
|
// This file adds support to soong to automatically propogate LTO options to a
|
|
|
|
// new variant of all static dependencies for each module with LTO enabled.
|
|
|
|
|
|
|
|
type LTOProperties struct {
|
|
|
|
// Lto must violate capitialization style for acronyms so that it can be
|
|
|
|
// referred to in blueprint files as "lto"
|
2017-08-29 05:10:09 +02:00
|
|
|
Lto struct {
|
2017-09-28 02:01:15 +02:00
|
|
|
Never *bool `android:"arch_variant"`
|
|
|
|
Full *bool `android:"arch_variant"`
|
|
|
|
Thin *bool `android:"arch_variant"`
|
2017-08-29 05:10:09 +02:00
|
|
|
} `android:"arch_variant"`
|
2017-09-28 02:01:15 +02:00
|
|
|
|
|
|
|
// Dep properties indicate that this module needs to be built with LTO
|
|
|
|
// since it is an object dependency of an LTO module.
|
2021-10-13 19:07:42 +02:00
|
|
|
FullDep bool `blueprint:"mutated"`
|
|
|
|
ThinDep bool `blueprint:"mutated"`
|
|
|
|
NoLtoDep bool `blueprint:"mutated"`
|
2018-04-03 20:33:34 +02:00
|
|
|
|
|
|
|
// Use clang lld instead of gnu ld.
|
|
|
|
Use_clang_lld *bool
|
2020-09-20 19:18:32 +02:00
|
|
|
|
|
|
|
// Use -fwhole-program-vtables cflag.
|
|
|
|
Whole_program_vtables *bool
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type lto struct {
|
|
|
|
Properties LTOProperties
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lto *lto) props() []interface{} {
|
|
|
|
return []interface{}{<o.Properties}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lto *lto) begin(ctx BaseModuleContext) {
|
2018-02-01 00:15:08 +01:00
|
|
|
if ctx.Config().IsEnvTrue("DISABLE_LTO") {
|
2021-06-25 20:53:40 +02:00
|
|
|
lto.Properties.Lto.Never = proptools.BoolPtr(true)
|
2018-02-01 00:15:08 +01:00
|
|
|
}
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 20:33:34 +02:00
|
|
|
func (lto *lto) useClangLld(ctx BaseModuleContext) bool {
|
|
|
|
if lto.Properties.Use_clang_lld != nil {
|
|
|
|
return Bool(lto.Properties.Use_clang_lld)
|
|
|
|
}
|
2018-10-22 04:47:01 +02:00
|
|
|
return true
|
2018-04-03 20:33:34 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 00:44:35 +02:00
|
|
|
func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
|
2022-03-02 02:25:22 +01:00
|
|
|
// TODO(b/131771163): Disable LTO when using explicit fuzzing configurations.
|
|
|
|
// LTO breaks fuzzer builds.
|
|
|
|
if inList("-fsanitize=fuzzer-no-link", flags.Local.CFlags) {
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
if lto.LTO(ctx) {
|
|
|
|
var ltoCFlag string
|
|
|
|
var ltoLdFlag string
|
2020-09-21 15:41:03 +02:00
|
|
|
if lto.ThinLTO() {
|
2021-10-13 19:07:42 +02:00
|
|
|
ltoCFlag = "-flto=thin -fsplit-lto-unit"
|
|
|
|
} else if lto.FullLTO() {
|
|
|
|
ltoCFlag = "-flto"
|
2017-08-29 05:10:09 +02:00
|
|
|
} else {
|
2021-10-13 19:07:42 +02:00
|
|
|
ltoCFlag = "-flto=thin -fsplit-lto-unit"
|
|
|
|
ltoLdFlag = "-Wl,--lto-O0"
|
2017-08-29 05:10:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
|
2018-02-16 13:36:16 +01:00
|
|
|
|
2020-09-20 19:18:32 +02:00
|
|
|
if Bool(lto.Properties.Whole_program_vtables) {
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables")
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
if (lto.DefaultThinLTO(ctx) || lto.ThinLTO()) && ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") && lto.useClangLld(ctx) {
|
2018-02-16 13:36:16 +01:00
|
|
|
// Set appropriate ThinLTO cache policy
|
2019-03-23 05:28:39 +01:00
|
|
|
cacheDirFormat := "-Wl,--thinlto-cache-dir="
|
2018-02-16 13:36:16 +01:00
|
|
|
cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir)
|
2018-02-16 13:36:16 +01:00
|
|
|
|
|
|
|
// Limit the size of the ThinLTO cache to the lesser of 10% of available
|
|
|
|
// disk space and 10GB.
|
2019-03-23 05:28:39 +01:00
|
|
|
cachePolicyFormat := "-Wl,--thinlto-cache-policy="
|
2018-02-16 13:36:16 +01:00
|
|
|
policy := "cache_size=10%:cache_size_bytes=10g"
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy)
|
2018-02-16 13:36:16 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 18:54:50 +02:00
|
|
|
// If the module does not have a profile, be conservative and limit cross TU inline
|
|
|
|
// limit to 5 LLVM IR instructions, to balance binary size increase and performance.
|
2022-02-14 13:00:10 +01:00
|
|
|
if !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
|
2019-11-04 18:37:55 +01:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags,
|
2020-09-22 18:54:50 +02:00
|
|
|
"-Wl,-plugin-opt,-import-instr-limit=5")
|
2018-02-14 11:16:12 +01:00
|
|
|
}
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
func (lto *lto) LTO(ctx BaseModuleContext) bool {
|
|
|
|
return lto.ThinLTO() || lto.FullLTO() || lto.DefaultThinLTO(ctx)
|
|
|
|
}
|
2017-05-10 00:44:35 +02:00
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
func (lto *lto) DefaultThinLTO(ctx BaseModuleContext) bool {
|
2022-08-16 21:54:54 +02:00
|
|
|
lib32 := ctx.Arch().ArchType.Multilib == "lib32"
|
2021-10-13 19:07:42 +02:00
|
|
|
host := ctx.Host()
|
|
|
|
vndk := ctx.isVndk() // b/169217596
|
2022-08-16 21:54:54 +02:00
|
|
|
return GlobalThinLTO(ctx) && !lto.Never() && !lib32 && !host && !vndk
|
2020-09-21 15:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lto *lto) FullLTO() bool {
|
2021-10-13 19:07:42 +02:00
|
|
|
return lto != nil && Bool(lto.Properties.Lto.Full)
|
2020-09-21 15:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lto *lto) ThinLTO() bool {
|
2021-10-13 19:07:42 +02:00
|
|
|
return lto != nil && Bool(lto.Properties.Lto.Thin)
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 08:41:50 +02:00
|
|
|
func (lto *lto) Never() bool {
|
2021-10-13 19:07:42 +02:00
|
|
|
return lto != nil && Bool(lto.Properties.Lto.Never)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GlobalThinLTO(ctx android.BaseModuleContext) bool {
|
2022-08-18 16:52:08 +02:00
|
|
|
return ctx.Config().IsEnvTrue("GLOBAL_THINLTO")
|
2017-09-28 02:01:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 00:44:35 +02:00
|
|
|
// Propagate lto requirements down from binaries
|
|
|
|
func ltoDepsMutator(mctx android.TopDownMutatorContext) {
|
2021-10-13 19:07:42 +02:00
|
|
|
globalThinLTO := GlobalThinLTO(mctx)
|
|
|
|
|
|
|
|
if m, ok := mctx.Module().(*Module); ok {
|
2020-09-21 15:41:03 +02:00
|
|
|
full := m.lto.FullLTO()
|
|
|
|
thin := m.lto.ThinLTO()
|
2021-10-13 19:07:42 +02:00
|
|
|
never := m.lto.Never()
|
2017-08-29 05:10:09 +02:00
|
|
|
if full && thin {
|
|
|
|
mctx.PropertyErrorf("LTO", "FullLTO and ThinLTO are mutually exclusive")
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:01:15 +02:00
|
|
|
mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
|
|
|
|
tag := mctx.OtherModuleDependencyTag(dep)
|
2020-07-28 06:26:48 +02:00
|
|
|
libTag, isLibTag := tag.(libraryDependencyTag)
|
|
|
|
|
|
|
|
// Do not recurse down non-static dependencies
|
|
|
|
if isLibTag {
|
2020-07-29 21:55:55 +02:00
|
|
|
if !libTag.static() {
|
2020-07-28 06:26:48 +02:00
|
|
|
return false
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
2020-07-28 06:26:48 +02:00
|
|
|
} else {
|
|
|
|
if tag != objDepTag && tag != reuseObjTag {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 02:01:15 +02:00
|
|
|
|
2021-10-13 19:07:42 +02:00
|
|
|
if dep, ok := dep.(*Module); ok {
|
2020-09-21 15:41:03 +02:00
|
|
|
if full && !dep.lto.FullLTO() {
|
2020-07-28 06:26:48 +02:00
|
|
|
dep.lto.Properties.FullDep = true
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if !globalThinLTO && thin && !dep.lto.ThinLTO() {
|
2020-07-28 06:26:48 +02:00
|
|
|
dep.lto.Properties.ThinDep = true
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if globalThinLTO && never && !dep.lto.Never() {
|
|
|
|
dep.lto.Properties.NoLtoDep = true
|
|
|
|
}
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
2017-09-28 02:01:15 +02:00
|
|
|
|
2020-07-28 06:26:48 +02:00
|
|
|
// Recursively walk static dependencies
|
|
|
|
return true
|
2017-05-10 00:44:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create lto variants for modules that need them
|
|
|
|
func ltoMutator(mctx android.BottomUpMutatorContext) {
|
2021-10-13 19:07:42 +02:00
|
|
|
globalThinLTO := GlobalThinLTO(mctx)
|
|
|
|
|
2017-09-28 02:01:15 +02:00
|
|
|
if m, ok := mctx.Module().(*Module); ok && m.lto != nil {
|
|
|
|
// Create variations for LTO types required as static
|
|
|
|
// dependencies
|
|
|
|
variationNames := []string{""}
|
2020-09-21 15:41:03 +02:00
|
|
|
if m.lto.Properties.FullDep && !m.lto.FullLTO() {
|
2017-09-28 02:01:15 +02:00
|
|
|
variationNames = append(variationNames, "lto-full")
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if !globalThinLTO && m.lto.Properties.ThinDep && !m.lto.ThinLTO() {
|
2017-09-28 02:01:15 +02:00
|
|
|
variationNames = append(variationNames, "lto-thin")
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if globalThinLTO && m.lto.Properties.NoLtoDep && !m.lto.Never() {
|
|
|
|
variationNames = append(variationNames, "lto-none")
|
|
|
|
}
|
2017-09-28 02:01:15 +02:00
|
|
|
|
|
|
|
// Use correct dependencies if LTO property is explicitly set
|
|
|
|
// (mutually exclusive)
|
2020-09-21 15:41:03 +02:00
|
|
|
if m.lto.FullLTO() {
|
2017-09-28 02:01:15 +02:00
|
|
|
mctx.SetDependencyVariation("lto-full")
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if !globalThinLTO && m.lto.ThinLTO() {
|
2017-09-28 02:01:15 +02:00
|
|
|
mctx.SetDependencyVariation("lto-thin")
|
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
// Never must be the last, it overrides Thin or Full.
|
|
|
|
if globalThinLTO && m.lto.Never() {
|
|
|
|
mctx.SetDependencyVariation("lto-none")
|
|
|
|
}
|
2017-09-28 02:01:15 +02:00
|
|
|
|
|
|
|
if len(variationNames) > 1 {
|
|
|
|
modules := mctx.CreateVariations(variationNames...)
|
|
|
|
for i, name := range variationNames {
|
|
|
|
variation := modules[i].(*Module)
|
|
|
|
// Default module which will be
|
|
|
|
// installed. Variation set above according to
|
|
|
|
// explicit LTO properties
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// LTO properties for dependencies
|
|
|
|
if name == "lto-full" {
|
2021-06-25 20:53:40 +02:00
|
|
|
variation.lto.Properties.Lto.Full = proptools.BoolPtr(true)
|
2017-09-28 02:01:15 +02:00
|
|
|
}
|
|
|
|
if name == "lto-thin" {
|
2021-06-25 20:53:40 +02:00
|
|
|
variation.lto.Properties.Lto.Thin = proptools.BoolPtr(true)
|
2017-09-28 02:01:15 +02:00
|
|
|
}
|
2021-10-13 19:07:42 +02:00
|
|
|
if name == "lto-none" {
|
|
|
|
variation.lto.Properties.Lto.Never = proptools.BoolPtr(true)
|
|
|
|
}
|
2017-09-28 02:01:15 +02:00
|
|
|
variation.Properties.PreventInstall = true
|
|
|
|
variation.Properties.HideFromMake = true
|
|
|
|
variation.lto.Properties.FullDep = false
|
|
|
|
variation.lto.Properties.ThinDep = false
|
2021-10-13 19:07:42 +02:00
|
|
|
variation.lto.Properties.NoLtoDep = false
|
2017-09-28 02:01:15 +02:00
|
|
|
}
|
2017-05-10 00:44:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|