Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ship with LTO default libs on Linux & macOS #2640

Merged
merged 7 commits into from
Apr 17, 2018
Merged

Conversation

kinke
Copy link
Member

@kinke kinke commented Mar 31, 2018

This allows additional bitcode libs to be generated simultaneously with the normal druntime/Phobos libs (i.e., single compilation) by including something like -DBUILD_LTO_LIBS=ON -DD_FLAGS="-w;-flto=thin" in the CMake command line (additional D_FLAGS entry here to include the ThinLTO module summaries in the bitcode files; not needed for FullLTO).

This then allows linking against the LTO default libs via ldc2 -flto=<thin|full> ... -defaultlib=phobos2-ldc-lto,druntime-ldc-lto; unfortunately, I couldn't test this yet, as my LLVM 6 Linux build has a 0-byte LLVMgold.so...

@kinke
Copy link
Member Author

kinke commented Mar 31, 2018

Tested it now; it's principally working, but the C and asm parts of druntime/Phobos are missing in the bitcode libs. We don't have these object files explicitly, CMake takes care of compiling them, so that's blocking this LTO-libs-for-free scheme.

@kinke
Copy link
Member Author

kinke commented Mar 31, 2018

Working now. A Phobos-hello-world can be linked against the normal libs as well as with both thin & full LTO against the ThinLTO bitcode libs (incl. C/ASM parts in machine code):

void main()
{
    import std.stdio;
    writefln("Hello world, %d bits", cast(int) size_t.sizeof * 8);
}
bin/ldc2 -O -release ../hello.d -of=hello_normal -linker=gold
  => 833 KB, 1.6 secs
bin/ldc2 -O -release ../hello.d -of=hello_full -flto=full -defaultlib=phobos2-ldc-bc,druntime-ldc-bc -linker=gold
  => 691 KB, 6.9 secs
bin/ldc2 -O -release ../hello.d -of=hello_thin -flto=thin -defaultlib=phobos2-ldc-bc,druntime-ldc-bc -linker=gold
  => 690 KB, 6.2 secs

@kinke kinke force-pushed the ltolibs branch 2 times, most recently from ce67728 to 7036593 Compare March 31, 2018 22:33
@kinke kinke changed the title Prepare for shipping with LTO default libs Ship with LTO default libs on Linux & macOS Mar 31, 2018
@kinke
Copy link
Member Author

kinke commented Mar 31, 2018

I'd prefer an -lto suffix instead of -bc (and BUILD_LTO_LIBS over BUILD_BC_LIBS); I don't think there's much room for backwards compatibility concerns (considering that BUILD_BC_LIBS=ON was broken for default BUILD_SHARED_LIBS=BOTH etc.).

@@ -258,7 +270,7 @@ jobs:
- LLVM_VERSION: 5.0.1
- HOST_LDC_VERSION: 1.6.0
- BOOTSTRAP_CMAKE_FLAGS: "-DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
- EXTRA_CMAKE_FLAGS: "-DMULTILIB=ON -DBUILD_BC_LIBS=ON -DD_FLAGS='-w;-flto=thin' -DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS=-lc++"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding -flto to D_FLAGS also builds the normal libs with that option, right? But then how do the tests pass: how does linking succeed without specifying the LLVMgold plugin on the linker line for normal tests without LTO ?

Copy link
Member Author

@kinke kinke Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding -flto to D_FLAGS also builds the normal libs with that option, right?

In combination with BUILD_BC_LIBS=ON, this yields a command line ldc2 -c -output-o -output-bc -flto=thin ...; producing both the normal object file in machine code and the LTO bitcode file (-flto=thin only affecting the module summaries in the bitcode files in this case). After this PR's first commit that is.

how does linking succeed without specifying the LLVMgold plugin

We use LDC to firstly compile the modules, one object (and bitcode file for BUILD_BC_LIBS) per druntime/Phobos module (and D_FLAGS only affect compilation). We then archive them to static libs via separate LDC invokations - normal libs containing machine code only, and bitcode/LTO libs containing bitcode for the D parts and machine code for the C/ASM parts. The shared libs are obviously only generated for the normal machine-code objects.

So specifying -flto=... for linking is only required when choosing the bitcode/LTO libs explicitly via -defaultlib=..., which is only tested by the CircleCI integration test so far (Linux only; linker crashing on OSX for both thin and full LTO tests).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scheme allows the user to choose LTO for his code only via -flto=... and LTO incl. default libs via -flto=... -defaultlib=....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

producing both the normal object file in machine code and the LTO bitcode file

OK got it.

@JohanEngelen
Copy link
Member

This is pretty neat.

I'd prefer an -lto suffix instead of -bc (and BUILD_LTO_LIBS over BUILD_BC_LIBS)

Me too.

@kinke
Copy link
Member Author

kinke commented Apr 11, 2018

@JohanEngelen: Do you have any clues as to why both trivial LTO tests fail on Circle OSX (Assertion failed: (WasErased && "Expected to drop a reference"))? Do we not default to using the shipped-with LTO plugin on OSX?

@kinke
Copy link
Member Author

kinke commented Apr 14, 2018

Btw the CircleCI package sizes are increased by about 10%.

@JohanEngelen
Copy link
Member

I've seen the "Expected to drop a reference" failure before, I think when trying to create LTO version of ldc release package. Back then, I couldn't figure out what the bug is.
On OSX we explicitly pass the LDC LTO plugin to the linker.

Need to find time to look at it closely again...

kinke added 6 commits April 16, 2018 22:13
Feasible by using LDC for archiving (via 'D' linker language), opening
up the door for including C/ASM parts by source file (and another
compilation of the few of them), as used for the normal druntime/Phobos
libs.

As LDC doesn't support .bc files as pure archiving/linking input
directly (they are injected into the first compiled module/object, but
if there is none, they are ignored), the .bc files need to be renamed to
.bc.o files before creating the static libs.

While at it, get rid of redundancies by extending
set_common_library_properties().
LTO with debug libs probably isn't very useful.
@kinke
Copy link
Member Author

kinke commented Apr 16, 2018

Oh wow, the CircleCI macOS LTO integration tests now work with LLVM 6. :)

@kinke kinke merged commit d5871a1 into ldc-developers:master Apr 17, 2018
@kinke kinke deleted the ltolibs branch April 17, 2018 01:09
@JohanEngelen
Copy link
Member

Oh wow, the CircleCI macOS LTO integration tests now work with LLVM 6. :)

There was an XCode update recently, perhaps related?

@kinke
Copy link
Member Author

kinke commented Apr 18, 2018

I don't think so, we're using the XCode 9.0 CircleCI image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants