Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 4a118b5

Browse files
committed
[WebAssembly] Add a flag to control merging data segments
Merging data segments produces smaller code sizes because each segment has some boilerplate. Therefore, merging data segments is generally the right approach, especially with wasm where binaries are typically delivered over the network. However, when analyzing wasm binaries, it can be helpful to get a conservative picture of which functions are using which data segments[0]. Perhaps there is a large data segment that you didn't expect to be included in the wasm, introduced by some library you're using, and you'd like to know which library it was. In this scenario, merging data segments only makes the analysis worse. Alternatively, perhaps you will remove some dead functions by-hand[1] that can't be statically proven dead by the compiler or lld, and removing these functions might make some data garbage collect-able, and you'd like to run `--gc-sections` again so that this now-unused data can be collected. If the segments were originally merged, then a single use of the merged data segment will entrench all of the data. [0] https://github.com/rustwasm/twiggy [1] https://github.com/fitzgen/wasm-snip Patch by Nick Fitzgerald! Differential Revision: https://reviews.llvm.org/D46417 git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@332013 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b1a83d5 commit 4a118b5

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

test/wasm/data-segment-merging.ll

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
target triple = "wasm32-unknown-unknown"
2+
3+
@a = hidden global [6 x i8] c"hello\00", align 1
4+
@b = hidden global [8 x i8] c"goodbye\00", align 1
5+
@c = hidden global [9 x i8] c"whatever\00", align 1
6+
@d = hidden global i32 42, align 4
7+
8+
; RUN: llc -filetype=obj %s -o %t.data-segment-merging.o
9+
10+
; RUN: wasm-ld -no-gc-sections --allow-undefined -o %t.merged.wasm %t.data-segment-merging.o
11+
; RUN: obj2yaml %t.merged.wasm | FileCheck %s --check-prefix=MERGE
12+
; MERGE: - Type: DATA
13+
; MERGE-NEXT: Segments:
14+
; MERGE-NEXT: - SectionOffset: 7
15+
; MERGE-NEXT: MemoryIndex: 0
16+
; MERGE-NEXT: Offset:
17+
; MERGE-NEXT: Opcode: I32_CONST
18+
; MERGE-NEXT: Value: 1024
19+
; MERGE-NEXT: Content: 68656C6C6F00676F6F6462796500776861746576657200002A000000
20+
21+
; RUN: wasm-ld -no-gc-sections --allow-undefined --no-merge-data-segments -o %t.separate.wasm %t.data-segment-merging.o
22+
; RUN: obj2yaml %t.separate.wasm | FileCheck %s --check-prefix=SEPARATE
23+
; SEPARATE: - Type: DATA
24+
; SEPARATE-NEXT: Segments:
25+
; SEPARATE-NEXT: - SectionOffset: 7
26+
; SEPARATE-NEXT: MemoryIndex: 0
27+
; SEPARATE-NEXT: Offset:
28+
; SEPARATE-NEXT: Opcode: I32_CONST
29+
; SEPARATE-NEXT: Value: 1024
30+
; SEPARATE-NEXT: Content: 68656C6C6F00
31+
; SEPARATE-NEXT: - SectionOffset: 19
32+
; SEPARATE-NEXT: MemoryIndex: 0
33+
; SEPARATE-NEXT: Offset:
34+
; SEPARATE-NEXT: Opcode: I32_CONST
35+
; SEPARATE-NEXT: Value: 1030
36+
; SEPARATE-NEXT: Content: 676F6F6462796500
37+
; SEPARATE-NEXT: - SectionOffset: 33
38+
; SEPARATE-NEXT: MemoryIndex: 0
39+
; SEPARATE-NEXT: Offset:
40+
; SEPARATE-NEXT: Opcode: I32_CONST
41+
; SEPARATE-NEXT: Value: 1038
42+
; SEPARATE-NEXT: Content: '776861746576657200'
43+
; SEPARATE-NEXT: - SectionOffset: 48
44+
; SEPARATE-NEXT: MemoryIndex: 0
45+
; SEPARATE-NEXT: Offset:
46+
; SEPARATE-NEXT: Opcode: I32_CONST
47+
; SEPARATE-NEXT: Value: 1048
48+
; SEPARATE-NEXT: Content: 2A000000

wasm/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct Configuration {
2424
bool GcSections;
2525
bool ImportMemory;
2626
bool ImportTable;
27+
bool MergeDataSegments;
2728
bool PrintGcSections;
2829
bool Relocatable;
2930
bool StripAll;

wasm/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
292292
Config->Relocatable = Args.hasArg(OPT_relocatable);
293293
Config->GcSections =
294294
Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
295+
Config->MergeDataSegments =
296+
Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
297+
!Config->Relocatable);
295298
Config->PrintGcSections =
296299
Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
297300
Config->SearchPaths = args::getStrings(Args, OPT_L);

wasm/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ defm gc_sections: B<"gc-sections",
4040
"Enable garbage collection of unused sections",
4141
"Disable garbage collection of unused sections">;
4242

43+
defm merge_data_segments: B<"merge-data-segments",
44+
"Enable merging data segments",
45+
"Disable merging data segments">;
46+
4347
def help: F<"help">, HelpText<"Print option help">;
4448

4549
def l: JoinedOrSeparate<["-"], "l">, MetaVarName<"<libName>">,

wasm/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ void Writer::assignIndexes() {
914914
}
915915

916916
static StringRef getOutputDataSegmentName(StringRef Name) {
917-
if (Config->Relocatable)
917+
if (!Config->MergeDataSegments)
918918
return Name;
919919
if (Name.startswith(".text."))
920920
return ".text";

0 commit comments

Comments
 (0)