Interoperability between Clang, GCC and LTO
Asked Answered
V

1

5

I know that Clang and GCC are more or less compatible C/C++ compilers as long as one takes care of things like architecture flags, predefines and linking the right libraries. Creating libraries with one compiler and linking them with objects created by the other is actually pretty easy (at least on x86).

Here is a little test project doing exactly that: https://gitlab.com/higaski/Interoperability

I was wondering however if Link Time Optimization (LTO) could somehow work across compilers? I know that LTO requires some form of intermediate representation like LLVMs bitcode or GCCs GIMPLE but maybe there is a workflow in which both of them could be utilized?

Vizor answered 10/7, 2018 at 7:21 Comment(3)
Creating libraries with one compiler and linking them with objects created by the other is actually not a good idea. Even if those compilers are the different versions of the particular compiler.Rode
If you use what gcc calls FAT LTO objects, then you produce objects that contain both the internal representation (that gcc can use) and the regular, compiled code (that other compilers can use).Coast
@MarcGlisse GCCs -ffat-lto-objects indeed produces object files that can be linked to Clang objects, even if LTO is enabled. The following combination worked for me: GCC with -flto -ffat-lto-objects -> Clang with -flto -fuse-ld=gold (ld or lld gave me errors when linking) Is there a way for Clang to produce "FAT" objects as well? Currently this option only makes it possible to use GCC objects when compiling with Clang and LTO, but not the other way round?Vizor
H
2

No, LTO can not be shared across compilers, precisely because it's effectively a dump of compiler's internal representation. Such representation is unstable even across releases of the same compiler.

Hillis answered 29/10, 2018 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.