ldc-developers / ldc

The LLVM-based D Compiler.
http://wiki.dlang.org/LDC
Other
1.22k stars 262 forks source link

Dummy program without Phobos deps still links in Phobos modules (lld-specific) #4258

Open kinke opened 2 years ago

kinke commented 2 years ago

Ubuntu 22, LDC v1.30.0, lld v14:

void main() {
    import core.stdc.stdio;
    foreach (m; ModuleInfo)
        printf("%.*s\n", cast(int) m.name.length, m.name.ptr);
}

Built without any explicit flags, this results in an almost 1.2 MB program, with 156 linked D modules (./hello | wc -l), incl. Phobos modules.

When building/linking with -defaultlib=druntime-ldc, the size is almost halved, and 80 druntime modules are linked.

With DMD v2.100.1, the size is almost 900 KB, while only including 37 modules (druntime only). [Some extra intra-dependencies in LDC's druntime are to be expected.] And DMD merges druntime and Phobos to a single library.

I guess this might be caused by the linker pulling in template instantiation symbols (instantiated and also available in druntime) from Phobos object files (Phobos library listed before druntime in linker cmdline, as Phobos depends on druntime). And just as I'm filing this issue, I've retried with bfd and gold, and yep, the same 80 modules (and ~620 KB size) without explicit -defaultlib=druntime-ldc, so it's lld-specific.

kinke commented 2 years ago

Apparently even much worse on Mac: https://github.com/ldc-developers/ldc/discussions/4256

D-a-n-i-l-o commented 1 year ago

See my answer at #4256