[lld] r332013 - [WebAssembly] Add a flag to control merging data segments

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Thu May 10 11:23:51 PDT 2018


Author: sbc
Date: Thu May 10 11:23:51 2018
New Revision: 332013

URL: http://llvm.org/viewvc/llvm-project?rev=332013&view=rev
Log:
[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

Added:
    lld/trunk/test/wasm/data-segment-merging.ll
Modified:
    lld/trunk/wasm/Config.h
    lld/trunk/wasm/Driver.cpp
    lld/trunk/wasm/Options.td
    lld/trunk/wasm/Writer.cpp

Added: lld/trunk/test/wasm/data-segment-merging.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/data-segment-merging.ll?rev=332013&view=auto
==============================================================================
--- lld/trunk/test/wasm/data-segment-merging.ll (added)
+++ lld/trunk/test/wasm/data-segment-merging.ll Thu May 10 11:23:51 2018
@@ -0,0 +1,48 @@
+target triple = "wasm32-unknown-unknown"
+
+ at a = hidden global [6 x i8] c"hello\00", align 1
+ at b = hidden global [8 x i8] c"goodbye\00", align 1
+ at c = hidden global [9 x i8] c"whatever\00", align 1
+ at d = hidden global i32 42, align 4
+
+; RUN: llc -filetype=obj %s -o %t.data-segment-merging.o
+
+; RUN: wasm-ld -no-gc-sections --allow-undefined -o %t.merged.wasm %t.data-segment-merging.o
+; RUN: obj2yaml %t.merged.wasm | FileCheck %s --check-prefix=MERGE
+; MERGE:       - Type:            DATA
+; MERGE-NEXT:    Segments:
+; MERGE-NEXT:      - SectionOffset:   7
+; MERGE-NEXT:        MemoryIndex:     0
+; MERGE-NEXT:        Offset:
+; MERGE-NEXT:          Opcode:          I32_CONST
+; MERGE-NEXT:          Value:           1024
+; MERGE-NEXT:        Content:         68656C6C6F00676F6F6462796500776861746576657200002A000000
+
+; RUN: wasm-ld -no-gc-sections --allow-undefined --no-merge-data-segments -o %t.separate.wasm %t.data-segment-merging.o
+; RUN: obj2yaml %t.separate.wasm | FileCheck %s --check-prefix=SEPARATE
+; SEPARATE:       - Type:            DATA
+; SEPARATE-NEXT:    Segments:
+; SEPARATE-NEXT:      - SectionOffset:   7
+; SEPARATE-NEXT:        MemoryIndex:     0
+; SEPARATE-NEXT:        Offset:
+; SEPARATE-NEXT:          Opcode:          I32_CONST
+; SEPARATE-NEXT:          Value:           1024
+; SEPARATE-NEXT:        Content:         68656C6C6F00
+; SEPARATE-NEXT:      - SectionOffset:   19
+; SEPARATE-NEXT:        MemoryIndex:     0
+; SEPARATE-NEXT:        Offset:
+; SEPARATE-NEXT:          Opcode:          I32_CONST
+; SEPARATE-NEXT:          Value:           1030
+; SEPARATE-NEXT:        Content:         676F6F6462796500
+; SEPARATE-NEXT:      - SectionOffset:   33
+; SEPARATE-NEXT:        MemoryIndex:     0
+; SEPARATE-NEXT:        Offset:
+; SEPARATE-NEXT:          Opcode:          I32_CONST
+; SEPARATE-NEXT:          Value:           1038
+; SEPARATE-NEXT:        Content:         '776861746576657200'
+; SEPARATE-NEXT:      - SectionOffset:   48
+; SEPARATE-NEXT:        MemoryIndex:     0
+; SEPARATE-NEXT:        Offset:
+; SEPARATE-NEXT:          Opcode:          I32_CONST
+; SEPARATE-NEXT:          Value:           1048
+; SEPARATE-NEXT:        Content:         2A000000

Modified: lld/trunk/wasm/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Config.h?rev=332013&r1=332012&r2=332013&view=diff
==============================================================================
--- lld/trunk/wasm/Config.h (original)
+++ lld/trunk/wasm/Config.h Thu May 10 11:23:51 2018
@@ -24,6 +24,7 @@ struct Configuration {
   bool GcSections;
   bool ImportMemory;
   bool ImportTable;
+  bool MergeDataSegments;
   bool PrintGcSections;
   bool Relocatable;
   bool StripAll;

Modified: lld/trunk/wasm/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Driver.cpp?rev=332013&r1=332012&r2=332013&view=diff
==============================================================================
--- lld/trunk/wasm/Driver.cpp (original)
+++ lld/trunk/wasm/Driver.cpp Thu May 10 11:23:51 2018
@@ -292,6 +292,9 @@ void LinkerDriver::link(ArrayRef<const c
   Config->Relocatable = Args.hasArg(OPT_relocatable);
   Config->GcSections =
       Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
+  Config->MergeDataSegments =
+      Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
+                   !Config->Relocatable);
   Config->PrintGcSections =
       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
   Config->SearchPaths = args::getStrings(Args, OPT_L);

Modified: lld/trunk/wasm/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Options.td?rev=332013&r1=332012&r2=332013&view=diff
==============================================================================
--- lld/trunk/wasm/Options.td (original)
+++ lld/trunk/wasm/Options.td Thu May 10 11:23:51 2018
@@ -40,6 +40,10 @@ defm gc_sections: B<"gc-sections",
     "Enable garbage collection of unused sections",
     "Disable garbage collection of unused sections">;
 
+defm merge_data_segments: B<"merge-data-segments",
+    "Enable merging data segments",
+    "Disable merging data segments">;
+
 def help: F<"help">, HelpText<"Print option help">;
 
 def l: JoinedOrSeparate<["-"], "l">, MetaVarName<"<libName>">,

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=332013&r1=332012&r2=332013&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Thu May 10 11:23:51 2018
@@ -914,7 +914,7 @@ void Writer::assignIndexes() {
 }
 
 static StringRef getOutputDataSegmentName(StringRef Name) {
-  if (Config->Relocatable)
+  if (!Config->MergeDataSegments)
     return Name;
   if (Name.startswith(".text."))
     return ".text";




More information about the llvm-commits mailing list