[PATCH] D44543: [AsmPrinter] Emit .weak directive for weak linkage on COFF

Rafael Avila de Espindola via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 15 17:37:37 PDT 2018


espindola added a comment.

> I think my perspective is informed by the text in LangRef and whatever LLVM used to do for COFF when I started working on it. Meaning, weak and linkonce linkages just create a comdat, nothing else. The weak vs. linkonce bit indicates what's discardable. The ODR bit indicates whether you can optimize.
> 
> Being able to have undefined weak symbols was always just some weird ELF feature that wasn't spelled out in LangRef and didn't work on COFF so it must not be inside the model. It's entirely possible that I've given that old model too much weight and we should update the LangRef to talk about how weak declarations can be undefined at link time.
> 
> Now that we have comdats, weak_odr and linkonce_odr are pretty weird linkages. We could, for example, migrate weak_odr+comdat things to just external+comdat, and that would work pretty well. External GVs aren't discardable, but most of the optimizer still reasons from the linkage without looking at the comdat, so this would be a pretty big change.

We would still need a can_be_dropped bit. A comdat can be dropped if all of its members can be dropped.

For ELF and COFF removing weak_odr would work in practice. Replacing weak_odr with external would allow the same optimizations and in practice I don't think anyone uses the fact that the template instantiations are weak. That is, I don't expect anyone  to have a C file with a _Z<something> function that is not weak and not in a comdat an expects to be able to use that to replace the result of a template instantiation during link.

For MachO dropping weak_odr is harder. We could use comdats and have codegen map "in a comdat" to weak, but that is a bit of a hack.

So what I would suggest is

- Add a can_drop bit to GVs.
- Remove linkonce and linkonce_odr. They become just weak+can_drop and weak_odr+can_drop.
- Change clang to use external+can_drop instead of weak_odr+can_drop when targeting COFF.
- Apply this patch.


Repository:
  rL LLVM

https://reviews.llvm.org/D44543





More information about the llvm-commits mailing list