Skip to content

Refactor system_libs.py for easier combinations - #8780

Merged
quantum5 merged 42 commits into
emscripten-core:incomingfrom
quantum5:system-libs
Jun 17, 2019
Merged

quantum5 merged 42 commits into
emscripten-core:incomingfrom
quantum5:system-libs

Conversation

@quantum5

@quantum5 quantum5 commented Jun 11, 2019 •

Copy link
Copy Markdown
Member

No description provided.

@quantum5
quantum5 requested review from aheejin and tlively June 12, 2019 22:00

@kripken kripken 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.

Very nice work! Skipping to the end, I like how the definitions of the libraries look - much better than before. I didn't read this all yet, but here are some initial comments.

I have mixed feelings about the name changes caused by this, like embuilder now expects libdlmalloc instead of dlmalloc. On the one hand this might break some users. On the other, the new names are all lib* which is more consistent? Also we've changed names in the past with no bug reports. So I think this is fine, but curious to hear what others think.

At minimum we should add a clear note in the changelog, that some internal library names may change, including in a visible way on embuilder. Also the recursive dependencies are not done any more.

A large comment on top of the new code would help, I think, to explain the overall design. In particular, I started to read and noticed I didn't realize why cls is used so much - why are classes the base of everything (and not instances), what inheritance means, etc.

Comment thread embuilder.py Outdated
print('Skipping building of native-optimizer; EMSCRIPTEN_NATIVE_OPTIMIZER set in .emscripten config.')
else:
tasks += ['native_optimizer']
tasks += ['struct_info']

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 is this needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

generated_struct_info.json is no longer automatically built due to the build system change, so we make this task run explicitly when building all libraries.

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.

please add a comment explaining that, as otherwise it's very surprising to see it singled out for special treatment. something like "is necessary for building anything else". but that's not the full explanation, since isn't libc in a similar situation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It might make more sense for this to be listed in system targets.

Comment thread tests/test_other.py Resolved
Comment thread tools/system_libs.py Resolved
Comment thread tools/system_libs.py Resolved
Comment thread tools/system_libs.py Resolved
Comment thread ChangeLog.md Outdated
- Add a new system for managing system libraries. (#8780)
This may require minor changes when performing certain operations:
- When using `embuilder.py` to build a specific library, the name may have
changed: all library names are prefixed with lib now.

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.

please add for consistency (to explain why the names changed)

Comment thread ChangeLog.md Outdated
`libdlmalloc` or `libpthreads_stub`. These names will link in the correct
version of the library: if the build is configured to use `emmalloc`, `libmalloc`
will mean `libemmalloc`, and if thread support is disabled, `libpthreads` will
mean `libpthreads_stub`.

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.

please explain the benefit here too.

Comment thread embuilder.py
def is_flag(arg):
return arg.startswith('--')

shared.check_sanity()

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 vaguely remember this was here for a reason - that we must check here so the cache is invalidated up front, rather than later? please add a comment explaining this. also, I think this should run even without main - how about putting it at the top level at the bottom of the file before checking if we are in a main program?

Comment thread embuilder.py Outdated
print('Skipping building of native-optimizer; EMSCRIPTEN_NATIVE_OPTIMIZER set in .emscripten config.')
else:
tasks += ['native_optimizer']
tasks += ['struct_info']

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.

please add a comment explaining that, as otherwise it's very surprising to see it singled out for special treatment. something like "is necessary for building anything else". but that's not the full explanation, since isn't libc in a similar situation?

Comment thread tools/system_libs.py Outdated
# emmalloc doesn't actually use errno, but it's easier to build it again
extra += '_noerrno'
if self.is_mt:
extra += '_threadsafe'

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.

doesn't inheriting from MTLibrary handle adding the -mt suffix for us?

if this is overriding just to keep the same names as before, i think it's ok to change the name if it simplifies the code (which it would if just one place adds -mt or such to the name).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Somewhat unfortunately, due to the way inheritance work, we cannot actually call super if we want use the prefix libdlmalloc or libemmalloc, which is the reason why we are handling it here manually.

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 probably don't understand yet. why doesn't this problem happen with all the other uses of MTLibrary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All other uses of MTLibrary are fine because they end up calling Library's get_base_name, returning the name class attribute. However, the name attribute on libmalloc is libmalloc, so using inheritance, we'd be forced to have libmalloc-mt-dlmalloc..., which looks terrible.

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 see, thanks.

@kripken kripken Jun 16, 2019 •

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.

Thinking on this some more, I'm not sure malloc is that special, and this issue may happen more over time. If inheritance is clumsy here, perhaps an option is to avoid it? That is, instead of

class SomeLibrary(MTLibrary, OtherLibrary):
  ..

there could be a single class for all library types, and "Modifiers" (VariantGenerators? Variators?) that are added to a list,

class SomeLibrary(Library):
  modifiers = [MTLibrary, OtherLibrary]
  ..

then the "Modifiers" would be called in the proper places?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually now that I think about it, we can still do this with inheritance. For this use case, we just need a method that returns self.name that can be overridden. I suppose we might as well do this for libmalloc as an example for the future.

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.

IMO using multiple inheritance makes the implementation harder to follow but makes the resulting client code more elegant. The code is documented well enough that I don't think the complexity of the implementation will be a maintenance issue going forward, so I'm happy with the current design.

@kripken I agree that your proposed solution would have a simpler implementation, so if you feel strongly about this then I would be happy going in that direction as well.

@kripken

kripken commented Jun 15, 2019

Copy link
Copy Markdown
Member

Very nice work!

Overall I think this is good enough to land after the last changes, and we can iterate on improvements from there, so lgtm from me. However, since this is a big change and the others are away, let's wait til next week and see if we can get feedback from some of them.

Comment thread ChangeLog.md
will mean `libemmalloc`, and if thread support is disabled, `libpthreads` will
mean `libpthreads_stub`. This allows you to say `libmalloc` or `libpthreads` without
worrying about which implementation is supposed to be used, and avoid duplicate
symbols if you used the wrong implementation.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The above comment only pertains to EMCC_FORCE_STDLIBS and not to embuilder.py command line usage, right?

One core use case of embuilder.py is to drive a prebuild of specific system libraries at compiler toolchain package authoring time, so that these system libraries will not be attempted to be built on demand: this helps give fast first time experience to users, e.g. with Unity when they export their first HTML5 project, on a weak laptop building the system libraries would add several minutes to first build - leading users to perceive that web exports are really slow. Also another reason is that installation is done in 'Program Files' folder, where everything is read-only from the get-go. Building content on demand inside 'Program Files' would not work.

To achieve the above, we use embuilder.py to prebuild libemmalloc and libdlmalloc for example, so that still works? (Briefly looking it appears to me that it does, but just verifying)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, embuilder.py is meant to be able to build any version of the library you want. EMCC_FORCE_STDLIBS now uses the right variant for the current build.

@juj

juj commented Jun 16, 2019

Copy link
Copy Markdown
Collaborator

Hmm, there's some conflict upcoming with this PR I'm hoping can land soon #7672 . I hope that's easy to factor in to the structure that this PR brings?

Comment thread embuilder.py Resolved
Comment thread tools/system_libs.py
# included at the same time as compiler-rt.
# Note that this also includes things that may be depended on by those
# functions - fmin uses signbit, for example, so signbit must be here (so if
# fmin is added by codegen, it will have all it needs).

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 did you verify that you got all the dependencies?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This wasn't my code. I just moved this function outside of calculate.

Comment thread tools/system_libs.py Outdated
def vary_on(cls):
"""
Returns a list of boolean constructor arguments that defines the variations
of this library.

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.

It would be good to clarify that these are strings. When I initially read this I thought "boolean contructor arguments" meant bools.

Comment thread tools/system_libs.py Resolved
Comment thread tools/system_libs.py Resolved
Comment thread tools/system_libs.py
def create_libc(libname):
logger.debug(' building libc for cache')
class CXXLibrary(Library):
emcc = shared.EMXX

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.

This is very elegant 👍

Comment thread tools/system_libs.py
if shared.Settings.DEBUG_LEVEL >= 3:
self.is_debug = kwargs.pop('is_debug')
self.use_errno = kwargs.pop('use_errno')
self.is_tracing = kwargs.pop('is_tracing')

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.

What would happen if some abstract library wanted to use the same kwargs? Should these have a malloc_ prefix or something like that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That would only be a problem if libmalloc inherits from that abstract library. If that were the case, we probably wouldn't need this line.

If we really want to, we could use the __ name mangling, but I don't feel it's necessary.

Comment thread tools/system_libs.py
src_glob = '*.cc'


def calculate(temp_files, in_temp, stdout_, stderr_, forced=[]):

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.

This may not be appropriate for this PR, but it would be great if this function had a more descriptive name.

quantum5 referenced this pull request Jun 17, 2019
Changes:

* Made `sanitizer_common` and `ubsan` runtimes work:
  * Add `SANITIZER_EMSCRIPTEN` platform
  * Add `sanitizer_emscripten.cc` for Emscripten-specific implementations of
    helper and abstraction functions
  * Stub out functions that do not work on Emscripten. Using these will be
    abort the program. These will be implemented later.
* Add `emscripten_get_module_name` to get the WASM module name
* Changed `embuilder` to build `ubsan` and `sanitizer_common` runtimes
* Add `ubsan`-related unit tests
* Remove old JS-based `ubsan` hack and its unit tests

Known limitations:

* Stack traces do not work.
@quantum5
quantum5 merged commit 08382c7 into emscripten-core:incoming Jun 17, 2019
@sbc100

sbc100 commented Jun 19, 2019 •

Copy link
Copy Markdown
Collaborator

I'm a little concerned that we may have lost the ability to control the order of libraries on the command line. Were they in declaration order before and now alphabetical? I am a little surprised that alphabetical order is sufficient but the tests pass so I guess its OK so far.

I suppose really we want to build a dependency DAG such that any dependent libraries always come after on the command line. But I feel like the complexity here is already getting out of control.

@quantum5

Copy link
Copy Markdown
Member Author

@sbc100 The actual order of libraries we pass to wasm-ld is handled by add_library, which adds the dependencies of the current library if it hasn't already been added to the command line. This behaviour did not change compared to before.

The thing that changed is that we now try to resolve the undefined symbols in our object files by going through our libraries in alphabetical order, instead of the declaration order from before.

Even before this change, the libraries were not linked in topological order based on dependencies. Compiling a C++ hello world program before this PR passes the libraries on the command line in this order:

~/.emscripten_cache/wasm-obj/libc++_noexcept.a
~/.emscripten_cache/wasm-obj/libc++abi.a
~/.emscripten_cache/wasm-obj/libc.a
~/.emscripten_cache/wasm-obj/libcompiler_rt.a
~/.emscripten_cache/wasm-obj/libc-wasm.a
~/.emscripten_cache/wasm-obj/libdlmalloc.a
~/.emscripten_cache/wasm-obj/libpthreads_stub.a
~/.emscripten_cache/wasm-obj/libcompiler_rt_wasm.a
~/.emscripten_cache/wasm-obj/libc_rt_wasm.a

We note clearly that libpthreads_stub and libcompiler_rt are declared to be dependent on libc, yet there are listed after libc, violating topological order.

If the libraries were supposed to be passed in topological order, we can add a topological sort. However, libcompiler_rt and libc actually depend on each other, both before and after this PR, so we aren't dealing with a DAG.

@quantum5

Copy link
Copy Markdown
Member Author

Checking the man page for ld.lld(1), it appears that lld keeps track of previously seen symbols and links them in if a subsequent library depends on it, so it doesn't actually matter which order we specify the .a files on the command line.

@sbc100

sbc100 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

The order matters when one two libraries both define the same symbol. The first one on the command line will generally win. I'm not sure we take advantage of that anywhere.

The order in which add_library gets call does change under the new scheme doesn't it? Because system_libs used to be an array that was manually constructed and now its built from the inheritance graph and then sorted? For example, previously libhtml5 would always come before libgl simply becasue of the way they were listed. Not I'm pretty sure libgl would come first because its alphabetical.

As you say not a huge deal probably, especially for lld. Note that the "linking" we implement for fastcomp doesn't ahve the ability to look back so order matters there more, but only for .a and not for .bc so again it doesn't matter much.

I guess the only time it will really matter is if we want to have a library that overrides symbols from another library. Until that time maybe we can ignore this issue. In that case it would be "depends" so much as "comes_before" maybe?

@quantum5

Copy link
Copy Markdown
Member Author

If we do find ourselves needing the libraries to appear in certain orders, we could actually support having the libraries appear in the order they appear in system_libs.py by using a metaclass to keep track of the definition order, and then sorting based on that instead of the name.

belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
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.

5 participants