[PATCH] D63976: Allow clang -Os and -Oz to work with -flto and lld

Mehdi AMINI via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 6 10:59:32 PDT 2019


mehdi_amini added a comment.

> I assume I might be missing something here, though, since someone mentioned this above (I don't understand the response to it though).

There are two invocations in LTO: the first phase where we compile from source to bitcode, and the second phase which is invoked by the linker.

Phase 1: compile to bitcode
===========================

clang++ -Os -flto foo.c -o foo.o
clang++ -O2 -flto bar.c -o bar.o

Phase 2: LTO and CodeGen
========================

clang++  bar.o foo.o

When compiling foo.c, we use Os which add a function attribute to make each function in foo.o as such. Functions in bar.o won't have the same attributes.
During LTO we merge everything but functions keep their attributes, the optimization passes can adjust their heuristics to honor the fact that functions from foo.c will be "optimized for size" and the function from bar.c will be optimized "normally".

Specifying an optimization level for the LTO phase is a also bit awkward since the LTO optimization pipeline is already very different from the non-LTO one: https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/IPO/PassManagerBuilder.cpp#L1012

What remains are CodeGen heuristics, and there might still have some global flags in use: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/Support/CodeGen.h#L51 (note nothing specific to Os/Oz though), still in use for example for generating FMAs on AArch64 apparently: https://github.com/llvm/llvm-project/blob/master/llvm/lib/Target/AArch64/AArch64SelectionDAGInfo.cpp#L55

This is why specifying Oz/Os during LTO can be very confusing for the user: it would change very few things in the process without actually making function from bar.c having the right function attributes (nothing would override the lack of attribute as far as I know): `clang++ -flto -Oz bar.o foo.o` would *not* add the Oz annotation on functions defined in bar.o.

> I'm curious why we would want to force -O2/-O3 instead of just allowing -Os/-Oz to be used with LTO.

So I hope it is more clear that I don't think we're forcing O2 <https://reviews.llvm.org/owners/package/2/>/O3 <https://reviews.llvm.org/owners/package/3/> on LTO users, but it isn't obvious to expose a consistent UI for these flags with respect to LTO without being surprising to the user in some way.
(do you know that LTO use to be -O4?)


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63976/new/

https://reviews.llvm.org/D63976





More information about the cfe-commits mailing list