Skip to content

Link system libraries by default - #10998

Merged
sbc100 merged 3 commits into
masterfrom
system_libs_by_default
May 10, 2020
Merged

Link system libraries by default#10998
sbc100 merged 3 commits into
masterfrom
system_libs_by_default

Conversation

@sbc100

@sbc100 sbc100 commented Apr 24, 2020

Copy link
Copy Markdown
Collaborator

After this change system library no longer linked on demand based
.symbols files, to simply included by default in all cases.

The .symbols file logic were originally added as an optimization
would allow only the libraries needed to be considered at link time.

However, this optimization I believe was put in place for fastcomp where
the build time cost (in build) of including a library was significant.

This optimization does not apply with the wasm backend where linking is
fast and .a libraries are handled efficiently. Also are basically no
real programs that don't use libc and for those programs we already have
the -nostdlib option.

The .symbols files are still used for some optional libraries such
as libhtml, libgl and libal. However the plan is to also remove that
logic in STRICT mode too. Once we do that we can avoid having to
run llvm-nm on each input object file.

See #8912.

@sbc100
sbc100 force-pushed the system_libs_by_default branch 3 times, most recently from 625dd71 to 507c80c Compare April 25, 2020 03:57
@aheejin

aheejin commented Apr 26, 2020

Copy link
Copy Markdown
Member
  • Nice! Now we link the default libraries whether or not we use them, to remove unused libraries (if any), should we run with optimizations so that they are DCE'd?
  • Is this a replacement of Always link system libraries #9457?

@sbc100

sbc100 commented Apr 26, 2020

Copy link
Copy Markdown
Collaborator Author
  • Nice! Now we link the default libraries whether or not we use them, to remove unused libraries (if any), should we run with optimizations so that they are DCE'd?

Actually in most cases the way libraries work is that you only link the parts that you use anyway.
In the base we use big BC files rather than library files so this was not true.

DCE can achieve even more of course, both the normal dce that that linker doees and the metadce that emscripten does.

Yes, this is a alternative less intrusive version of that change.

@sbc100
sbc100 force-pushed the system_libs_by_default branch 4 times, most recently from e87bae6 to c1ef1ae Compare May 1, 2020 02:21
sbc100 added a commit that referenced this pull request May 1, 2020
Sometime emcc wants to add extra libraries the link, but when it
does do there is no need to use `--whole-archive` which is what is
used for FORCE_STDLIBS

This change was split out from:
#10998
sbc100 added a commit that referenced this pull request May 2, 2020
Sometime emcc wants to add extra libraries the link, but when it
does do there is no need to use `--whole-archive` which is what is
used for FORCE_STDLIBS

This change was split out from:
#10998
@sbc100
sbc100 force-pushed the system_libs_by_default branch 4 times, most recently from 3cf16a8 to 15efd54 Compare May 2, 2020 17:32
@sbc100
sbc100 requested a review from kripken May 2, 2020 22:47

@aheejin aheejin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this change! Some minor questions and typo fixes.

Comment thread ChangeLog.md Outdated
Comment thread src/settings_internal.js Outdated
// Enabled when building C++ code (for example via em++ or via -c c++)
var USE_CXX = 0;

// Sometime was want to force linking with C++ libraries. For example, the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// Sometime was want to force linking with C++ libraries. For example, the
// Sometimes we want to force linking with C++ libraries. For example, the

Not sure this is what you were gonna write, but it looks like a typo?

Comment thread tests/test_other.py
# Regression test for issue #4522: Incorrect CC vs CXX detection
def test_incorrect_c_detection(self):
# This auto-detection only works for the compile phase.
# For linking you need to use `em++` or pass `-x c++`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How is the library change related to this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Because if you use emcc you don't get libc++ or libc++abi included in your link.. and you will have undefined symbols.

I could make that a separate change perhaps?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh I see. No I think this is fine as is.

Comment thread tools/system_libs.py


class libc_extras(MuslInternalLibrary):
class libc_extras(NoBCLibrary, MuslInternalLibrary):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why? Doesn't fastcomp use bc files?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

For some libraries yes. The advantage of the .bc library is: everything is in single file. The disadvantage is: The linker can't skip over it and will always reference it (including any static constructors therein). The whole point of libc_extras I believe is so that its not part of the main libc.bc file and can be selectively included. It only contains the single file: extras_fastcomp.c.

With this change we starting including it on the link line every time, which has the effect that it cannot be ignored.. by making it into a .a archive the linker process can ignore it when its not needed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry I might be mistaken, but does fastcomp use .a files? I thought it only uses .bc...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fastcomp and handle .a files, but the code that handles it is not very nice. It has to extract the archive and into a temp directory and inspect each object file. This is (I believe) why it uses .bc files for things like libc, a an optimization. I mean I can't think of any other reason to make libc into into big .bc file. Perhaps @kripken can remember the history here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think historically we just had a single .bc file for every library. That was easiest as we just let llvm-link do the linking.

Later we made some libraries be .a, where it mattered, like libc++ which has global ctors, and we wrote python code to extract the archive and pick the members to link etc. (as we couldn't use llvm-link there).

Comment thread tools/system_libs.py Outdated
Comment thread tools/system_libs.py Outdated
Comment thread tests/test_core.py
Comment thread tools/system_libs.py
Comment thread tools/system_libs.py
@kripken

kripken commented May 4, 2020

Copy link
Copy Markdown
Member

Does this affect build times with upstream? And how much slower is fastcomp?

(Can maybe compare bot times, but would be good to test locally too for less noise.

@sbc100

sbc100 commented May 4, 2020

Copy link
Copy Markdown
Collaborator Author

My expectation is that it won't actaully effect either backend much because the numbr of programs that don't need libc at all should be tiny. I'll look at the numbers though.

sbc100 added a commit that referenced this pull request May 4, 2020
Sometime emcc wants to add extra libraries the link, but when it
does do there is no need to use `--whole-archive` which is what is
used for FORCE_STDLIBS

This change was split out from:
#10998
@sbc100

sbc100 commented May 5, 2020

Copy link
Copy Markdown
Collaborator Author

Looking at the CI results we don't a huge impact:

                 after   before  diff
upstream-other   9.32    8.51    +7%
other            6.9     6.10    -1%
upstream-wasm2   8.32    8.39    -2%
test-ab          4.26    4.45    -7%

@sbc100
sbc100 force-pushed the system_libs_by_default branch from 15efd54 to 5dc19f1 Compare May 5, 2020 05:32
@kripken

kripken commented May 5, 2020

Copy link
Copy Markdown
Member

Testing locally I don't see much difference.

@sbc100

sbc100 commented May 10, 2020

Copy link
Copy Markdown
Collaborator Author

I managed to simplify again and default to linking C++ libraries by default for now.

sbc100 added 2 commits May 10, 2020 14:15
After this change system library no longer linked on demand based
`.symbols` files, to simply included by default in all cases.

The `.symbols` file logic were originally added as an optimization
would allow only the libraries needed to be considered at link time.

However, this optimization I believe was put in place for fastcomp where
the build time cost (in build) of including a library was significant.

This optimization does not apply with the wasm backend where linking is
fast and `.a` libraries are handled efficiently.  Also are basically no
real programs that don't use libc and for those programs we already have
the `-nostdlib` option.

The `.symbols` files are still used for some optional libraries such
as libhtml, libgl and libal.  However the plan is to also remove that
logic in STRICT mode too.   Once we do that we can avoid having to
run llvm-nm on each input object file.

See #8912.
@sbc100
sbc100 force-pushed the system_libs_by_default branch 2 times, most recently from 3779102 to 6b68e89 Compare May 10, 2020 21:16
@sbc100
sbc100 force-pushed the system_libs_by_default branch from 6b68e89 to ce1c0ce Compare May 10, 2020 21:31
@sbc100
sbc100 merged commit f9d40c0 into master May 10, 2020
@sbc100
sbc100 deleted the system_libs_by_default branch May 10, 2020 23:44
sbc100 added a commit that referenced this pull request May 12, 2020
The order of the system libraries is now fixed as of #10998
sbc100 added a commit that referenced this pull request May 12, 2020
The order of the system libraries is now fixed as of #10998
sbc100 added a commit that referenced this pull request May 12, 2020
The order of the system libraries is now fixed as of #10998
sbc100 added a commit that referenced this pull request May 13, 2020
The order of the system libraries is now fixed as of #10998
sbc100 added a commit that referenced this pull request Feb 18, 2021
This should have been removed in #10998.
sbc100 added a commit that referenced this pull request Feb 18, 2021
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.

3 participants