This is an archive of the discontinued LLVM Phabricator instance.

[LinkerWrapper] Fix static library symbol resolution
ClosedPublic

Authored by jhuber6 on May 31 2023, 1:48 PM.

Details

Summary

The linker wrapper performs its own very basic symbol resolution for the
purpose of supporting standard static library semantics. We do this here
because the Nvidia nvlink wrapper does not support static linking and
we have some offloading specific extensions.

Currently, we always place symbols in the "table" even if they aren't
extracted. This caused the logic to fail when many files were used that
referenced the same undefined variable. This patch changes the pass to
only add the symbols to the global "table" if the file is actually
extracted.

Diff Detail

Event Timeline

jhuber6 created this revision.May 31 2023, 1:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2023, 1:48 PM
jhuber6 requested review of this revision.May 31 2023, 1:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2023, 1:48 PM
tra added a comment.May 31 2023, 4:08 PM

LGTM in general.

clang/test/Driver/linker-wrapper-libs.c
28

How does this test test the functionality of the undefined symbol? E.g. how does it fail now, before the patch?

Is there an explicit check we could to do to make sure things work as intended as opposed to "there's no obvious error" which may also mean "we forgot to process *undefined.bc".

jhuber6 added inline comments.May 31 2023, 4:09 PM
clang/test/Driver/linker-wrapper-libs.c
28

Yeah, I wasn't sure how to define a good test for this. The problem I encountered before making this patch was that having another file that used an undefined symbol would override the NewSymbol check and then would prevent it from being extracted. So this checks that case.

tra added inline comments.May 31 2023, 4:19 PM
clang/test/Driver/linker-wrapper-libs.c
28

AFAICT, with -DUNDEFINED, the file would have only extern int sym;. CE says suggests that it produces an embty bitcode file: https://godbolt.org/z/EY9a8Pfeb

What exactly is supposed to be in the *.undefined.bc ? If it's intended to have an undefined reference to sym you need to add some sort of a reference to it.

jhuber6 updated this revision to Diff 527240.May 31 2023, 5:00 PM

Fix test

clang/test/Driver/linker-wrapper-libs.c
28

Good catch, forgot about that. It's why the other use of extern sym returns it.

tra accepted this revision.May 31 2023, 6:02 PM
This revision is now accepted and ready to land.May 31 2023, 6:02 PM
This revision was automatically updated to reflect the committed changes.