[cfe-dev] Using Clang -O4 with CMake

Rafael Espindola espindola at google.com
Sun Jun 20 05:42:21 PDT 2010


> Reading that document,  that's not quite correct.  In the example given, foo2() should not be removed by the optimiser in the general case, because it is externally visible.  There is a special case here - you are compiling an executable, not a library.  Running the internalize pass when you run opt on the linked bitcode will have the same effect, allowing the optimizer to remove everything that is not reachable from main().

It is correct when building an executable. The nice thing about this
is that the linker knows you are building an executable :-) With gold
at least, when building a shared library only hidden functions are
removed.

Internalizing everything but main is a very particular case of this.
That is exactly what the linker will do, *if* main is the only symbol
being accessed from outside the bitcode.

> Of course, this is not the correct thing to do in the general case.  For example, if you provide some helper functions in your main executable that are only used by loadable modules, then LTO will 'helpfully' remove them for you if it takes the list of public symbols from the ones that the linker knows are used.

For that to work (even without LTO) in linux you have to pass
--export_dynamic to the linker. Again, gold notices the option and
does the right think: does not remove functions a shared library might
use.

> If you use the internalize pass instead, then you can provide a list of functions that should not be removed, removing this problem.

And you have to maintain it :-)
With the linker plugin solution the linker computes that list for you.

> One of the things on my to-do list is allowing clang to drive the internalize pass based on the contents of a header, using clang's ability to parse C++ decls and mangle the names, so only symbols declared in a given header are exported from the resulting library.

Might be useful for archs without a liker plugin (if you must use gnu
ld for example), but looks redundant otherwise. With the linker plugin
(gold, not sure what the details are for MACHO), you just do the same
you would do without LTO:

*) Set -fdefault-visibilit=hidden
*) Mark the API functions/classes with default visibility

If you like an external symbol list better, you can use a version
script. I have tested that the first case works. Should probably test
the linker script too, but I try to avoid using those.

> David
>
> -- Sent from my Apple II
>
>


Cheers,
-- 
Rafael Ávila de Espíndola




More information about the cfe-dev mailing list