This is an archive of the discontinued LLVM Phabricator instance.

ELF: Make sections with KeepUnique bit eligible for ICF.
ClosedPublic

Authored by pcc on Jul 20 2018, 3:55 PM.

Details

Summary

The only restriction is that we cannot merge more than one KeepUnique
section together. This matches gold's behaviour and reduces code size
when using --icf=safe.

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

pcc created this revision.Jul 20 2018, 3:55 PM
ruiu added inline comments.Jul 20 2018, 4:06 PM
lld/ELF/ICF.cpp
471 ↗(On Diff #156628)

I guess this could be a bit simpler:

for (size_t I = Begin + 1; I < End; ++I) {
  if (Sections[Begin]->KeepUnique && Sections[I]->KeepUnique)
    Begin = I;
  else
    Sections[Begin]->replace(Sections[I]);
}
pcc added inline comments.Jul 20 2018, 4:41 PM
lld/ELF/ICF.cpp
471 ↗(On Diff #156628)

It doesn't look like this is correct. With this input:

.globl _start
_start:
call f1
call f2
call f3

.section .text.f1,"ax"
.globl f1
f1:
ret

.section .text.f2,"ax"
.globl f2
f2:
ret

.section .text.f3,"ax"
.globl f3
f3:
ret

I get:

$ ra/bin/ld.lld foo.o --icf=all --print-icf-sections --keep-unique=f2 --keep-unique=f3
$ nm a.out
000000000020100f T f1
000000000020100f T f2
000000000020100f T f3
0000000000201000 T _start

I'll add some comments to my version.

pcc updated this revision to Diff 156649.Jul 20 2018, 5:03 PM

Add comment

ruiu accepted this revision.Jul 20 2018, 5:17 PM

LGTM

This revision is now accepted and ready to land.Jul 20 2018, 5:17 PM
This revision was automatically updated to reflect the committed changes.

Hi Peter,

Thanks for your address significance tables work!

Specific to this change:

I was under the impression that the intention of --keep-unique was to prevent *all* folding?

I found this open bug against gold that supports this interpretation: https://sourceware.org/bugzilla/show_bug.cgi?id=18865

If I --keep-unique a symbol it seems like I am asking for a strong guarantee on the address of the symbol. The original behaviour matches this strong guarantee.