<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 23, 2021 at 3:52 PM Fāng-ruì Sòng <<a href="mailto:maskray@google.com">maskray@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On Wed, Jun 23, 2021 at 3:43 PM Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a>> wrote:<br>
><br>
><br>
><br>
> On Tue, Jun 22, 2021 at 11:09 PM Fāng-ruì Sòng via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>><br>
>> On Tue, Jun 22, 2021 at 10:20 PM Petr Hosek <<a href="mailto:phosek@google.com" target="_blank">phosek@google.com</a>> wrote:<br>
>> ><br>
>> > I guess this depends on a particular implementation of the distributed build system. In the case of Goma, we only supply the compiler binary which was invoked as the command (that binary links glibc as a shared library but we assume that one is supplied by the host system), all other files like headers are passed together with the compiler invocation as inputs. If we used dynamic linking, Goma would need to figure out what other shared libraries need to be sent to the server. It's certainly doable but it's an extra complexity we would like to avoid.<br>
>><br>
>> For non-clang executables, -DLLVM_LINK_LLVM_DYLIB=on just adds one<br>
>> more DT_NEEDED.<br>
>> The DT_NEEDED entry can use a $ORIGIN based DT_RUNPATH. Can Goma<br>
>> detect the libraries shipped with the tools?<br>
>> I asked because I feel this could be an artificial limitation which<br>
>> could be straightforwardly addressed in Goma.<br>
>> A toolchain executable using a accompanying shared object is not rare<br>
>> (thinking of plugins).<br>
>><br>
>> Multiplexing LLVM tools is one alternative but I am a bit concerned<br>
>> with the extra complexity and the new configuration the build system<br>
>> needs to support.<br>
>><br>
>> <a href="https://lists.llvm.org/pipermail/llvm-dev/2021-June/151338.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2021-June/151338.html</a><br>
>> mentioned another approach which doesn't require intrusive<br>
>> modification to the tools.<br>
>><br>
>> As for PGO+LTO, you can apply them to libLLVM-13git.so as well.<br>
><br>
><br>
> Some thoughts if we're getting into PGO+LTO territory, I feel that both methods presented here will be at a disadvantage compared to building clang and lld into their own binaries.<br>
> For example I remember that on Mac an important optimization for clang builds was to order the functions in the binary roughly in the order in which they are first encountered during execution, assuming the same behavior for lld you can see the conflicting optimization goal... You can also think about how libSupport may be differently "hot" on a clang PGO profile compared to lld and would result in different optimization.<br>
<br>
If PGO+LTO is desired, the executables can be split this way, assuming<br>
the performance of<br>
llvm-{ar,cov,cxxfilt,nm,objcopy,objdump,readobj,size,strings,symbolizer}<br>
doesn't matter.<br>
<br>
* clang (libLLVM*.a)<br>
* lld + llvm-{ar,cov,cxxfilt,nm,objcopy,objdump,readobj,size,strings,symbolizer}<br>
(libLLVM-13git.so)<br>
<br>
> LTO also benefits from "internalizing", basically building a static binary where only `main` is exported and everything else becomes an internal linkage is the best case: pointer escaping, global analysis, etc all become more powerful. Optimizing a shared library kind of makes every symbol public, and I suspect the busybox approach may be better on this aspect (you get back to a single public main, but it can reach much more code though).<br>
<br>
With --version-script we can internalize shared object symbols as<br>
well. For example, this has been used to facilitate whole-program<br>
devirtualization (<a href="https://reviews.llvm.org/D98686" rel="noreferrer" target="_blank">https://reviews.llvm.org/D98686</a>).<br>
With <a href="https://lists.llvm.org/pipermail/llvm-dev/2021-June/151338.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2021-June/151338.html</a><br>
we can get a list of roots which need to be exported.<br>
A thin executable plus a -fvisibility-inlines-hidden +<br>
-Bsymbolic-functions shared object is almost identical to a PIE.<br></blockquote><div><br></div><div>You can get closer to it but note that:</div><div><br></div><div>- You have some non-trivial and non-standard build setup and scripts to workaround the problem (finding roots, etc.), the busybox solution is much more "clean" from this point of view if one can structure it in "normal" C++.</div><div>- How does it work on non-ELF platforms?</div><div>- It still isn't equivalent: you're still having a large surface API exported by the .so which limits what the optimizer can do (alias analysis, etc.). You won't be able to inject context from the callers there, or inline across the libLLVM.so boundary.</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div><br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
><br>
>><br>
>><br>
>> > On Tue, Jun 22, 2021 at 10:09 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br>
>> >><br>
>> >> On Tue, Jun 22, 2021 at 10:00 PM Petr Hosek via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> >>><br>
>> >>> From our perspective as a toolchain vendor, even if using shared libraries could get us closer to static linking in terms of performance, we'd still prefer static linking for the ease of distribution. Dealing with a single statically linked executable is much easier than dealing with multiple shared libraries. This is especially important in distributed compilation environments like Goma.<br>
>> >><br>
>> >><br>
>> >> What makes it especially complicated for distributed compilation environments? (I'd expect a toolchain contains so many files that whether it's one binary, or a binary and a handful of shared libraries wouldn't change the general implementation complexity of a distributed build system?)<br>
>> >><br>
>> >>><br>
>> >>><br>
>> >>> When comparing performance between static and dynamic linking, I'd also recommend doing a comparison between binaries built with PGO+LTO. Plain -O3 leaves a lot of performance on the table and as far as I'm aware, most toolchain vendors use PGO+LTO.<br>
>> >>><br>
>> >>> On Tue, Jun 22, 2021 at 5:00 PM Fangrui Song via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> >>>><br>
>> >>>> On 2021-06-22, Leonard Chan via llvm-dev wrote:<br>
>> >>>> >Small update: I have a WIP prototype of the tool at<br>
>> >>>> ><a href="https://reviews.llvm.org/D104686" rel="noreferrer" target="_blank">https://reviews.llvm.org/D104686</a>. The prototype only includes llvm-objcopy<br>
>> >>>> >and llvm-objdump packed together, but we're seeing size benefits from<br>
>> >>>> >busyboxing those two compared against having two separate tools. (More<br>
>> >>>> >details in the prototype's description.) I don't plan on landing this as-is<br>
>> >>>> >anytime soon and there's still some things I'd like to improve/change and<br>
>> >>>> >get feedback on.<br>
>> >>>> ><br>
>> >>>> >To answer some replies:<br>
>> >>>> ><br>
>> >>>> >- Ideally, we could start off with an incremental approach and not package<br>
>> >>>> >large tools like clang/lld off the bat. The llvm-* tools seem like a good<br>
>> >>>> >place to start since they're generally a bunch of relatively small binaries<br>
>> >>>> >that all share a subset of functions in libLLVM, but don't necessarily use<br>
>> >>>> >all of libLLVM, so statically linking them together (with --gc-sections)<br>
>> >>>> >can help dedup a lot of shared components vs having separate statically<br>
>> >>>> >compiled tools. In my measurements, the busybox tool containing<br>
>> >>>> >llvm-objcopy+objdump is negligibly larger than llvm-objdump on its own (a<br>
>> >>>> >couple KB difference) indicating a lot of shared code between objdump and<br>
>> >>>> >objcopy.<br>
>> >>>> ><br>
>> >>>> >- Will Dietz's multiplexing tool looks like a good place to start from. The<br>
>> >>>> >only concern I can see though is mostly the amount of work needed to update<br>
>> >>>> >it to LLVM 13.<br>
>> >>>> ><br>
>> >>>> >- We don't have plans for windows support now, but it's not off the table.<br>
>> >>>> >(Been mostly focusing on *nix for now). Depending on overall traction for<br>
>> >>>> >this idea, we could approach incrementally and add support for different<br>
>> >>>> >platforms over time.<br>
>> >>>><br>
>> >>>> -DLLVM_LINK_LLVM_DYLIB=on -DCLANG_LINK_CLANG_DYLIB=on -DLLVM_TARGETS_TO_BUILD=X86 (custom1)<br>
>> >>>> vs<br>
>> >>>> -DLLVM_TARGETS_TO_BUILD=X86 (custom2)<br>
>> >>>><br>
>> >>>><br>
>> >>>> # This is the lower bound for any multiplexing approach. clang is the largest executable.<br>
>> >>>> % stat -c %s /tmp/out/custom2/bin/clang-13<br>
>> >>>> 102900408<br>
>> >>>><br>
>> >>>> I have built clang, lld and a bunch of ELF binary utilities.<br>
>> >>>><br>
>> >>>> % stat -c %s /tmp/out/custom1/lib/libLLVM-13git.so /tmp/out/custom1/lib/libclang-cpp.so.13git /tmp/out/custom1/bin/{clang-13,lld,llvm-{ar,cov,cxxfilt,nm,objcopy,objdump,readobj,size,strings,symbolizer}} | awk '{s+=$1}END{print s}'<br>
>> >>>> 138896544<br>
>> >>>><br>
>> >>>> % stat -c %s /tmp/out/custom2/bin/{clang-13,lld,llvm-{ar,cov,cxxfilt,nm,objcopy,objdump,readobj,size,strings,symbolizer}} | awk '{s+=$1}END{print s}'<br>
>> >>>> 209054440<br>
>> >>>><br>
>> >>>><br>
>> >>>> The -DLLVM_LINK_LLVM_DYLIB=on -DCLANG_LINK_CLANG_DYLIB=on build is doing a really good job.<br>
>> >>>><br>
>> >>>> A multiplexing approach can squeeze some bytes from 138896544 toward 102900408,<br>
>> >>>> but how much can it do?<br>
>> >>>><br>
>> >>>><br>
>> >>>> >- I'm starting to think the `cl::opt` to `OptTable` issue might be<br>
>> >>>> >orthogonal to the busybox implementation. The tool essentially dispatches<br>
>> >>>> >to different "main" functions in different tools, but as long as we don't<br>
>> >>>> >do anything within busybox after exiting that tool's main, then the global<br>
>> >>>> >state issues we weren't sure of with `cl::opt` might not be of any concern<br>
>> >>>> >now. It may be an issue down the line if, let's say, the tool flags moved<br>
>> >>>> >from being "owned" by the tools themselves to instead being "owned" by<br>
>> >>>> >busybox, and then we'd have to merge similarly-named flags together. In<br>
>> >>>> >that case, migrating these tools to use `OptTable` may be necessary since<br>
>> >>>> >(I think) `OptTable` should handle this. This may be a tedious task, but<br>
>> >>>> >this is just to say that busybox won't need to be immediately blocked on it.<br>
>> >>>><br>
>> >>>> Such improvement is useful even if we don't do multiplexing.<br>
>> >>>> I switched llvm-symbolizer. thakis switched llvm-objdump.<br>
>> >>>> I can look at some binary utilities.<br>
>> >>>><br>
>> >>>> >- I haven't seen any issues with colliding symbols when linking (although<br>
>> >>>> >I've only merged two tools for now). I suspect that with small-ish llvm-*<br>
>> >>>> >tools, the bulk of their code is shared from libLLVM, and they have their<br>
>> >>>> >own distinct logic built on top of it, which could mean a low chance of<br>
>> >>>> >conflicting internal ABIs.<br>
>> >>>> ><br>
>> >>>> >On Mon, Jun 21, 2021 at 10:54 AM Leonard Chan <<a href="mailto:leonardchan@google.com" target="_blank">leonardchan@google.com</a>><br>
>> >>>> >wrote:<br>
>> >>>> ><br>
>> >>>> >> Hello all,<br>
>> >>>> >><br>
>> >>>> >> When building LLVM tools, including Clang and lld, it's currently possible<br>
>> >>>> >> to use either static or shared linking for LLVM libraries. The latter can<br>
>> >>>> >> significantly reduce the size of the toolchain since we aren't duplicating<br>
>> >>>> >> the same code in every binary, but the dynamic relocations can affect<br>
>> >>>> >> performance. The former doesn't affect performance but significantly<br>
>> >>>> >> increases the size of our toolchain.<br>
>> >>>> >><br>
>> >>>> >> We would like to implement a support for a third approach which we call,<br>
>> >>>> >> for a lack of better term, "busybox" feature, where everything is compiled<br>
>> >>>> >> into a single binary which then dispatches into an appropriate tool<br>
>> >>>> >> depending on the first command. This approach can significantly reduce the<br>
>> >>>> >> size by deduplicating all of the shared code without affecting the<br>
>> >>>> >> performance.<br>
>> >>>> >><br>
>> >>>> >> In terms of implementation, the build would produce a single binary called<br>
>> >>>> >> `llvm` and the first command would identify the tool. For example, instead<br>
>> >>>> >> of invoking `llvm-nm` you'd invoke `llvm nm`. Ideally we would also support<br>
>> >>>> >> creation of `llvm-nm` symlink which redirects to `llvm` for backwards<br>
>> >>>> >> compatibility.<br>
>> >>>> >> This functionality would ideally be implemented as an option in the CMake<br>
>> >>>> >> build that toolchain vendors can opt into.<br>
>> >>>> >><br>
>> >>>> >> The implementation would have to replace `main` function of each tool with<br>
>> >>>> >> an entrypoint regular function which is registered into a tool registry.<br>
>> >>>> >> This could be wrapped in a macro for convenience. When the "busybox"<br>
>> >>>> >> feature is disabled, the macro would expand to a `main` function as before<br>
>> >>>> >> and redirect to the entrypoint function. When the "busybox" feature is<br>
>> >>>> >> enabled, it would register the entrypoint function into the registry, which<br>
>> >>>> >> would be responsible for the dispatching based on the tool name. Ideally,<br>
>> >>>> >> toolchain maintainers would also be able to control which tools they could<br>
>> >>>> >> add to the "busybox" binary via CMake build options, so toolchains will<br>
>> >>>> >> only include the tools they use.<br>
>> >>>> >><br>
>> >>>> >> One implementation detail we think will be an issue is merging arguments<br>
>> >>>> >> in individual tools that use `cl::opt`. `cl::opt` works by maintaining a<br>
>> >>>> >> global state of flags, but we aren’t confident of what the resulting<br>
>> >>>> >> behavior will be when merging them together in the dispatching `main`. What<br>
>> >>>> >> we would like to avoid is having flags used by one specific tool available<br>
>> >>>> >> on other tools. To address this issue, we would like to migrate all tools<br>
>> >>>> >> to use `OptTable` which doesn't have this issue and has been the general<br>
>> >>>> >> direction most tools have been already moving into.<br>
>> >>>> >><br>
>> >>>> >> A second issue would be resolving symlinks. For example, llvm-objcopy will<br>
>> >>>> >> check argv[0] and behave as llvm-strip (ie. use the right flags +<br>
>> >>>> >> configuration) if it is called via a symlink that “looks like” a strip<br>
>> >>>> >> tool, but for all other cases it will run under the default objcopy mode.<br>
>> >>>> >> The “looks like” function is usually an `Is` function copied in multiple<br>
>> >>>> >> tools that is essentially a substring check: so symlinks like `llvm-strip`,<br>
>> >>>> >> strip.exe, and `gnu-llvm-strip-10` all result in using the strip “mode”<br>
>> >>>> >> while all other names use the objcopy mode. To replicate the same behavior,<br>
>> >>>> >> we will need to take great care in making sure symlinks to the busybox tool<br>
>> >>>> >> dispatch correctly to the appropriate llvm tool, which might mean exposing<br>
>> >>>> >> and merging these `Is` functions.<br>
>> >>>> >><br>
>> >>>> >> Some open questions:<br>
>> >>>> >> - People's initial thoughts/opinions?<br>
>> >>>> >> - Are there existing tools in LLVM that already do this?<br>
>> >>>> >> - Other implementation details/global states that we would also need to<br>
>> >>>> >> account for?<br>
>> >>>> >><br>
>> >>>> >> - Leonard<br>
>> >>>> >><br>
>> >>>><br>
>> >>>> >_______________________________________________<br>
>> >>>> >LLVM Developers mailing list<br>
>> >>>> ><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> >>>> ><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>> >>>><br>
>> >>>> _______________________________________________<br>
>> >>>> LLVM Developers mailing list<br>
>> >>>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> >>>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>> >>><br>
>> >>> _______________________________________________<br>
>> >>> LLVM Developers mailing list<br>
>> >>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> >>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
>><br>
>><br>
>><br>
>> --<br>
>> 宋方睿<br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
>> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
<br>
<br>
-- <br>
宋方睿<br>
</blockquote></div></div>