This is an archive of the discontinued LLVM Phabricator instance.

[lli] Make sure the exported __chkstk functions are included when exporting them
ClosedPublic

Authored by mstorsjo on Aug 29 2023, 5:27 AM.

Details

Summary

The trick we use (since cbc2a0623a39461b56bd9eeb308ca535f03793f8)
for exporting the __chkstk function (with various per-arch names)
that is defined in a different object file, relies on the function
already being linked in (by some function referencing it).

This function does end up referenced if there's a function that
allocates more than 4 KB on the stack. In most cases, it's referenced
somewhere, but in the case of builds with LLVM_LINK_LLVM_DYLIB
enabled (so most of the code resides in a separate libLLVM-<ver>.dll)
the only code in lli.exe is the lli tool specific code and the
mingw-w64 crt startup code. In the case of GCC based MinGW i386
builds with LLVM_LINK_LLVM_DYLIB, nothing else references it though.

Manually add a reference to the function to make sure it is linked
in (from libgcc or compiler-rt builtins) so that it can be exported.

This fixes one build issue encountered in
https://github.com/msys2/MINGW-packages/pull/18002.

Diff Detail

Event Timeline

mstorsjo created this revision.Aug 29 2023, 5:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 5:27 AM
mstorsjo requested review of this revision.Aug 29 2023, 5:27 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 5:27 AM
mati865 added inline comments.Aug 29 2023, 8:59 AM
llvm/tools/lli/lli.cpp
1204

Should we update this comment?
This workaround is also required for linking to shared libraries.

mstorsjo added inline comments.Aug 29 2023, 1:07 PM
llvm/tools/lli/lli.cpp
1204

Do you mean if building an user application that does JIT, against the LLVM libraries? Yeah, the same issue applies there as well (regardless of whether it's linked shared or statically) - doing it within the lli executable just fixes it for the executable and none of the lib users. There's a TODO down below that this really should be handled by ORC.

And ideally we shouldn't just be exporting this function, ideally ORC should find the right libclang_rt.builtins*.a and link in that, it's just that the chkstk functions are the most blatant examples of it.

Put another way - I wasn't really planning on working on the JIT stuff, I just wanted to make check-llvm run successfully in mingw builds; first I tried just mark most of the JIT tests unsupported for mingw configs, but @lhames made and suggested some fixes that brought us to this point instead. The root issue isn't really solved but the tests at least run.

mati865 added inline comments.Sep 1 2023, 8:41 AM
llvm/tools/lli/lli.cpp
1204

Sorry, I've put this comment in the wrong place, I meant lines 1206-1207.

Do you mean if building an user application that does JIT, against the LLVM libraries? Yeah, the same issue applies there as well (regardless of whether it's linked shared or statically) - doing it within the lli executable just fixes it for the executable and none of the lib users. There's a TODO down below that this really should be handled by ORC.

I mean without this workaround lli is not buildable when linking to shared LLVM libs (IIUC this is what happened at MSYS2). I don't expect fixing ORC here.

mstorsjo added inline comments.Sep 1 2023, 8:50 AM
llvm/tools/lli/lli.cpp
1204

Ah - indeed, this fix is precisely for the shared LLVM libs issue found in MSYS2.

I don't see how this change affects the comments on lines 1206-1207. If the JIT would link in the real libclang_rt.builtins*.a at runtime, it would find __chkstk the right way from there. Since JIT doesn't do that at the moment, we need to make sure there's another exported __chkstk symbol somewhere in a loaded DLL, that the JIT can find - hence the dllexport trick. And here we have a tweak to make the dllexport trick work "when no functions allocate a large enough stack area" - which practically is when the majority of other code is in a different DLL, i.e. the shared libs or dylibs build modes.

mati865 accepted this revision.Sep 1 2023, 9:13 AM
mati865 added inline comments.
llvm/tools/lli/lli.cpp
1204

Ah, so without this hack there would be no build error. I had missed it somehow.

This revision is now accepted and ready to land.Sep 1 2023, 9:13 AM
lhames accepted this revision.Sep 7 2023, 8:18 AM

LGTM. Thanks @mstorsjo!

This revision was landed with ongoing or failed builds.Sep 7 2023, 1:33 PM
This revision was automatically updated to reflect the committed changes.

Could this be backported to LLVM 17?

Could this be backported to LLVM 17?

The threshold for what to backport right now, as we're past RC4 and expecting the final 17.0.0 next, is rather high.

But I guess this might qualify as it is a fix for a build break. Let's leave it in git main for a few days in case something shows up somewhere (but I guess there's not many other continuous mingw builds running other than mine), but I guess we could file a request for backporting this next week and still make it in for 17.0.0.