Simplify LTO flags handling

... and add more comments about the use of --lto-O0.

Test: presubmit
Change-Id: I2ef7bf4f100a8d7559a6b738e9211c596d579dc6
This commit is contained in:
Yi Kong
2023-07-03 16:59:33 +09:00
parent 222995b2c1
commit b9d5046d8b

View File

@@ -76,43 +76,44 @@ func (lto *lto) flags(ctx BaseModuleContext, flags Flags) Flags {
return flags return flags
} }
if lto.Properties.LtoEnabled { if lto.Properties.LtoEnabled {
var ltoCFlag string ltoCFlags := []string{"-flto=thin", "-fsplit-lto-unit"}
var ltoLdFlag string var ltoLdFlags []string
if lto.ThinLTO() {
ltoCFlag = "-flto=thin -fsplit-lto-unit" // The module did not explicitly turn on LTO. Only leverage LTO's
} else { // better dead code elinmination and CFG simplification, but do
ltoCFlag = "-flto=thin -fsplit-lto-unit" // not perform costly optimizations for a balance between compile
ltoLdFlag = "-Wl,--lto-O0" // time, binary size and performance.
if !lto.ThinLTO() {
ltoLdFlags = append(ltoLdFlags, "-Wl,--lto-O0")
} }
flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlag)
flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlag)
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlag)
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlag)
if Bool(lto.Properties.Whole_program_vtables) { if Bool(lto.Properties.Whole_program_vtables) {
flags.Local.CFlags = append(flags.Local.CFlags, "-fwhole-program-vtables") ltoCFlags = append(ltoCFlags, "-fwhole-program-vtables")
} }
if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") { if ctx.Config().IsEnvTrue("USE_THINLTO_CACHE") {
// Set appropriate ThinLTO cache policy // Set appropriate ThinLTO cache policy
cacheDirFormat := "-Wl,--thinlto-cache-dir=" cacheDirFormat := "-Wl,--thinlto-cache-dir="
cacheDir := android.PathForOutput(ctx, "thinlto-cache").String() cacheDir := android.PathForOutput(ctx, "thinlto-cache").String()
flags.Local.LdFlags = append(flags.Local.LdFlags, cacheDirFormat+cacheDir) ltoLdFlags = append(ltoLdFlags, cacheDirFormat+cacheDir)
// Limit the size of the ThinLTO cache to the lesser of 10% of available // Limit the size of the ThinLTO cache to the lesser of 10% of available
// disk space and 10GB. // disk space and 10GB.
cachePolicyFormat := "-Wl,--thinlto-cache-policy=" cachePolicyFormat := "-Wl,--thinlto-cache-policy="
policy := "cache_size=10%:cache_size_bytes=10g" policy := "cache_size=10%:cache_size_bytes=10g"
flags.Local.LdFlags = append(flags.Local.LdFlags, cachePolicyFormat+policy) ltoLdFlags = append(ltoLdFlags, cachePolicyFormat+policy)
} }
// If the module does not have a profile, be conservative and limit cross TU inline // 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. // limit to 5 LLVM IR instructions, to balance binary size increase and performance.
if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() { if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags = append(ltoLdFlags, "-Wl,-plugin-opt,-import-instr-limit=5")
"-Wl,-plugin-opt,-import-instr-limit=5")
} }
flags.Local.CFlags = append(flags.Local.CFlags, ltoCFlags...)
flags.Local.AsFlags = append(flags.Local.AsFlags, ltoCFlags...)
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoCFlags...)
flags.Local.LdFlags = append(flags.Local.LdFlags, ltoLdFlags...)
} }
return flags return flags
} }