This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: handle llvm.used properly for COFF
ClosedPublic

Authored by compnerd on Jan 17 2018, 3:06 PM.

Details

Reviewers
rnk
Summary

llvm.used contains a list of pointers to named values which the
compiler, assembler, and linker are required to treat as if there is a
reference that they cannot see. Ensure that the symbols are preserved
by adding an explicit -include reference to the linker command.

Diff Detail

Repository
rL LLVM

Event Timeline

compnerd created this revision.Jan 17 2018, 3:06 PM
compnerd updated this revision to Diff 130295.Jan 17 2018, 3:25 PM
rnk added a comment.Jan 17 2018, 3:32 PM

Duplicating this in the ARM and X86 AsmPrinters is lame. Maybe we should hoist the COFF-specific handling up to the next level? I'm going to look into doing that for -export flags.

rnk added a comment.Jan 17 2018, 4:01 PM

Cool, look at rL322788.

BTW, afterwards this is a great opportunity to fix this bug:

template <typename T> struct Foo { static int x; };
int f();
template <typename T> int Foo<T>::x = f();
template struct Foo<int>;

MSVC emits "/include:?x@?$Foo@H@@2HA" to make sure that x survives dead stripping. Once we have this llvm.used functionality, we have an easy IR-only way to get that /include directive.

Awesome; I was thinking about doing that as a follow up. I'll hoist this into that new layer. Completely agree on the fixing the emission after this infrastructure is in place. I can't believe that we never implemented this.

pcc added a subscriber: pcc.Jan 17 2018, 4:57 PM

This doesn't seem like it will do the right thing if the global has internal linkage. Do we care about that case? I guess we could handle it by emitting dummy ABSOLUTE relocations from a non-COMDAT section to the section or symbol that we want to preserve.

compnerd updated this revision to Diff 130323.Jan 17 2018, 4:59 PM

rebase and emit one entry per line

compnerd updated this revision to Diff 130689.Jan 19 2018, 2:04 PM

ignore internal globals as per IRC conversion with @rnk , @pcc, @smeenai

Patch is missing context.

lib/CodeGen/AsmPrinter/AsmPrinter.cpp
1459

Add a comment about why internal symbols are ignored?

compnerd updated this revision to Diff 130699.Jan 19 2018, 2:43 PM

context + comment

compnerd marked an inline comment as done.Jan 19 2018, 2:44 PM
rnk accepted this revision.Jan 19 2018, 3:06 PM

lgtm

test/CodeGen/X86/coff-no-dead-strip.ll
7

Maybe test a vectorcallcc function to test that we're using the GlobalValue overload of getNameWithPrefix.

13

CHECK-ULP-NOT: " /INCLUDE:_k" maybe?

This revision is now accepted and ready to land.Jan 19 2018, 3:06 PM
compnerd marked an inline comment as done.Jan 19 2018, 3:37 PM
compnerd added inline comments.
test/CodeGen/X86/coff-no-dead-strip.ll
7

Sounds good.

13

Hah, I forgot to adjust the commit. I have that added and tested with and without the user label prefix.

compnerd closed this revision.Jan 19 2018, 5:12 PM
compnerd marked an inline comment as done.

SVN r323017