This is an archive of the discontinued LLVM Phabricator instance.

[llvm] Use hidden visibility when building for MinGW with Clang
ClosedPublic

Authored by mstorsjo on Aug 11 2022, 2:12 AM.

Details

Summary

Since c5b3de6745c37dd991430b9b88ff97c35b6fc455 (git main,
August 11th), Clang does generate working hidden visibility
on MinGW targets. Using that reduces the number of exports from
a dylib build of LLVM significantly, which is vital for fitting
within the limit of 64k exported symbols from a DLL.

It's essential that if we set CMAKE_CXX_VISIBILITY_PRESET=hidden
(which passes -fvisibility=hidden on the command line), we also
must define LLVM_EXTERNAL_VISIBILITY consistently to override
it. (If there are mismatches, e.g. setting hidden visibility generally
but never overriding it back to default for the symbols that do need
to be exported, we'd get broken builds in such configurations.)

We don't want to be using attribute((visibility("hidden"))) on
MinGW with GCC, because GCC produces a warning about it. (GCC hasn't
warned about the command line options that set hidden visibility
though.) Clang has historically not warned about either of them, so
it is harmless to use the hidden visibility when building with older
Clang (so we don't need to detect the exact version of Clang/LLVM where
it has an effect).

This reduces the number of exported symbols for a dylib build of LLVM;
previously libLLVM exported around 64650 symbols (when the maximum is

  1. when the ARM, AArch64 and X86 targets were enabled. If enabling

more targets (or if building with e.g. assertions enabled), it would
exceed the limit. Now with visibility flags in use, the same build
with ARM, AArch64 and X86 ends up at around 35k exported symbols.

Diff Detail

Event Timeline

mstorsjo created this revision.Aug 11 2022, 2:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 2:12 AM
mstorsjo requested review of this revision.Aug 11 2022, 2:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2022, 2:12 AM
MaskRay accepted this revision.Aug 11 2022, 12:14 PM
This revision is now accepted and ready to land.Aug 11 2022, 12:14 PM
mati865 accepted this revision.Aug 11 2022, 1:17 PM

Thanks, confirmed the built compiler still works (bootstrapped it twice with itself) and with all architectures enabled we are at 35289 exported symbols.