llvm-project/lld/test/MachO/literal-dedup.s

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.1 KiB
ArmAsm
Raw Normal View History

[lld-macho] Deduplicate fixed-width literals Conceptually, the implementation is pretty straightforward: we put each literal value into a hashtable, and then write out the keys of that hashtable at the end. In contrast with ELF, the Mach-O format does not support variable-length literals that aren't strings. Its literals are either 4, 8, or 16 bytes in length. LLD-ELF dedups its literals via sorting + uniq'ing, but since we don't need to worry about overly-long values, we should be able to do a faster job by just hashing. That said, the implementation right now is far from optimal, because we add to those hashtables serially. To parallelize this, we'll need a basic concurrent hashtable (only needs to support concurrent writes w/o interleave reads), which shouldn't be to hard to implement, but I'd like to punt on it for now. Numbers for linking chromium_framework on my 3.2 GHz 16-Core Intel Xeon W: N Min Max Median Avg Stddev x 20 4.27 4.39 4.315 4.3225 0.033225703 + 20 4.36 4.82 4.44 4.4845 0.13152846 Difference at 95.0% confidence 0.162 +/- 0.0613971 3.74783% +/- 1.42041% (Student's t, pooled s = 0.0959262) This corresponds to binary size savings of 2MB out of 335MB, or 0.6%. It's not a great tradeoff as-is, but as mentioned our implementation can be signficantly optimized, and literal dedup will unlock more opportunities for ICF to identify identical structures that reference the same literals. Reviewed By: #lld-macho, gkm Differential Revision: https://reviews.llvm.org/D103113
2021-06-12 07:49:50 +08:00
# REQUIRES: x86
# RUN: rm -rf %t; split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/qux.s -o %t/qux.o
# RUN: %lld -dylib --deduplicate-literals %t/test.o %t/qux.o -o %t/test
# RUN: llvm-objdump --macho --section="__TEXT,__literals" --section="__DATA,ptrs" --syms %t/test | FileCheck %s
# RUN: llvm-readobj --section-headers %t/test | FileCheck %s --check-prefix=HEADER
# CHECK: Contents of (__TEXT,__literals) section
# CHECK-NEXT: [[#%.16x,DEADBEEF16:]] ef be ad de ef be ad de ef be ad de ef be ad de
# CHECK-NEXT: [[#%.16x,FEEDFACE16:]] ce fa ed fe ce fa ed fe ce fa ed fe ce fa ed fe
# CHECK-NEXT: [[#%.16x,DEADBEEF8:]] ef be ad de ef be ad de ce fa ed fe ce fa ed fe
# CHECK-NEXT: [[#%.16x,DEADBEEF4:]] ef be ad de ce fa ed fe
# CHECK-NEXT: Contents of (__DATA,ptrs) section
# CHECK-NEXT: 0000000000001000 0x[[#%x,DEADBEEF16]]
# CHECK-NEXT: 0000000000001008 0x[[#%x,DEADBEEF16]]
# CHECK-NEXT: 0000000000001010 0x[[#%x,FEEDFACE16]]
# CHECK-NEXT: 0000000000001018 0x[[#%x,DEADBEEF16]]
# CHECK-NEXT: 0000000000001020 0x[[#%x,DEADBEEF8]]
# CHECK-NEXT: 0000000000001028 0x[[#%x,DEADBEEF8]]
# CHECK-NEXT: 0000000000001030 0x[[#%x,DEADBEEF8 + 8]]
# CHECK-NEXT: 0000000000001038 0x[[#%x,DEADBEEF8]]
# CHECK-NEXT: 0000000000001040 0x[[#%x,DEADBEEF4]]
# CHECK-NEXT: 0000000000001048 0x[[#%x,DEADBEEF4]]
# CHECK-NEXT: 0000000000001050 0x[[#%x,DEADBEEF4 + 4]]
# CHECK-NEXT: 0000000000001058 0x[[#%x,DEADBEEF4]]
## Make sure the symbol addresses are correct too.
# CHECK: SYMBOL TABLE:
# CHECK-DAG: [[#DEADBEEF16]] g O __TEXT,__literals _qux16
# CHECK-DAG: [[#DEADBEEF8]] g O __TEXT,__literals _qux8
# CHECK-DAG: [[#DEADBEEF4]] g O __TEXT,__literals _qux4
## Make sure we set the right alignment and flags.
# HEADER: Name: __literals
# HEADER-NEXT: Segment: __TEXT
# HEADER-NEXT: Address:
# HEADER-NEXT: Size:
# HEADER-NEXT: Offset:
# HEADER-NEXT: Alignment: 4
# HEADER-NEXT: RelocationOffset:
# HEADER-NEXT: RelocationCount: 0
# HEADER-NEXT: Type: Regular
# HEADER-NEXT: Attributes [ (0x0)
# HEADER-NEXT: ]
# HEADER-NEXT: Reserved1: 0x0
# HEADER-NEXT: Reserved2: 0x0
# HEADER-NEXT: Reserved3: 0x0
#--- test.s
.literal4
.p2align 2
L._foo4:
.long 0xdeadbeef
L._bar4:
.long 0xdeadbeef
L._baz4:
.long 0xfeedface
.literal8
L._foo8:
.quad 0xdeadbeefdeadbeef
L._bar8:
.quad 0xdeadbeefdeadbeef
L._baz8:
.quad 0xfeedfacefeedface
.literal16
L._foo16:
.quad 0xdeadbeefdeadbeef
.quad 0xdeadbeefdeadbeef
L._bar16:
.quad 0xdeadbeefdeadbeef
.quad 0xdeadbeefdeadbeef
L._baz16:
.quad 0xfeedfacefeedface
.quad 0xfeedfacefeedface
.section __DATA,ptrs,literal_pointers
.quad L._foo16
.quad L._bar16
.quad L._baz16
.quad _qux16
.quad L._foo8
.quad L._bar8
.quad L._baz8
.quad _qux8
.quad L._foo4
.quad L._bar4
.quad L._baz4
.quad _qux4
#--- qux.s
.globl _qux4, _qux8, _qux16
.literal4
.p2align 2
_qux4:
.long 0xdeadbeef
.literal8
_qux8:
.quad 0xdeadbeefdeadbeef
.literal16
_qux16:
.quad 0xdeadbeefdeadbeef
.quad 0xdeadbeefdeadbeef