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

llvm: Use mimalloc for lld/llvm/clang #4591

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

ReillyBrogan
Copy link
Contributor

@ReillyBrogan ReillyBrogan commented Dec 17, 2024

EXPERIMENTAL DO NOT MERGE

I saw the developer of MOLD mention that using mimalloc with LLD improved performance by about 10%, so I set about seeing how that could be implemented in our package.

There were basically three methods that I could use:

  1. Build a static libmimalloc.a and force it to be linked in (GROSS)
  2. Use LD_PRELOAD to force libmimalloc.so to be loaded before each binary invocation. This would be annoying to do as it would likely involve a wrapper script of some kind.
  3. Modify the code so that it was directly used as a library. This appeared to be the best fit for our use as a distribution package. (This is what I chose)

This PR essentially modifies LLVM to create a new LLVM library libLLVMMimalloc.a and then statically links that into the clang, lld, and llvm-ar binaries (as those seemed to be where we would most want to see speed benefits). This library essentially causes mimalloc to replace the glibc malloc and uses mimalloc APIs to suppress mimalloc logging output (it's possible to re-enable via envvar). This code is called as close to the start of main for each of these binaries as seemed reasonable. This is basically how MOLD implements it BTW.

Note that an earlier attempt at this added this functionality to LLVMSupport which is linked into libLLVM. An unfortunate consequence of that approach is that it caused everything linked against libLLVM to use mimalloc which wouldn't have been so bad if that didn't include mesa (replacing the memory allocator for mesa seemed unwise to me).

Also, this patch includes changes to the bolt source as this patch is intended to be applied to that as well.

Testing

I've done a few test builds with this patch applied and results seem somewhat promising. It shaved a few seconds off of the qt6-base build though it really should be tested with something like qt6-webengine to really put it through its paces.

You can see if this is working by doing something like MIMALLOC_VERBOSE=1 clang --version which will print additional information if MIMALLOC is being used.

EXPERIMENTAL

Signed-off-by: Reilly Brogan <reilly@reillybrogan.com>
@ReillyBrogan
Copy link
Contributor Author

@joebonrichie , how would you go about testing this to see if it's worth it?

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

Successfully merging this pull request may close these issues.

1 participant