Skip to content

Commit

Permalink
[lld][WebAssembly] Delay creation of internal __wasm_memory_init func…
Browse files Browse the repository at this point in the history
…tion

This also allows for its creation to be conditional so it is completely
elided when not needed.

Differential Revision: https://reviews.llvm.org/D93035
  • Loading branch information
sbc100 committed Dec 10, 2020
1 parent 47e7ecd commit 1994970
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 168 deletions.
8 changes: 4 additions & 4 deletions lld/test/wasm/data-segment-merging.ll
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@
; PASSIVE-MERGE-NEXT: - Index: 0
; PASSIVE-MERGE-NEXT: Name: __wasm_call_ctors
; PASSIVE-MERGE-NEXT: - Index: 1
; PASSIVE-MERGE-NEXT: Name: __wasm_init_memory
; PASSIVE-MERGE-NEXT: - Index: 2
; PASSIVE-MERGE-NEXT: Name: __wasm_init_tls
; PASSIVE-MERGE-NEXT: - Index: 2
; PASSIVE-MERGE-NEXT: Name: __wasm_init_memory

; RUN: wasm-ld -no-gc-sections --no-entry --shared-memory --max-memory=131072 -no-merge-data-segments -o %t.separate.passive.wasm %t.passive.o
; RUN: obj2yaml %t.separate.passive.wasm | FileCheck %s --check-prefix=PASSIVE-SEPARATE
Expand Down Expand Up @@ -135,6 +135,6 @@
; PASSIVE-SEPARATE-NEXT: - Index: 0
; PASSIVE-SEPARATE-NEXT: Name: __wasm_call_ctors
; PASSIVE-SEPARATE-NEXT: - Index: 1
; PASSIVE-SEPARATE-NEXT: Name: __wasm_init_memory
; PASSIVE-SEPARATE-NEXT: - Index: 2
; PASSIVE-SEPARATE-NEXT: Name: __wasm_init_tls
; PASSIVE-SEPARATE-NEXT: - Index: 2
; PASSIVE-SEPARATE-NEXT: Name: __wasm_init_memory
25 changes: 12 additions & 13 deletions lld/test/wasm/data-segments.ll
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
; ACTIVE-NEXT: Name: __wasm_call_ctors

; PASSIVE-LABEL: - Type: START
; PASSIVE-NEXT: StartFunction: 1
; PASSIVE-NEXT: StartFunction: 2
; PASSIVE-LABEL: - Type: DATACOUNT
; PASSIVE-NEXT: Count: 2
; PASSIVE-LABEL: - Type: CODE
Expand All @@ -74,12 +74,11 @@
; PASSIVE-NEXT: Body: 0B
; PASSIVE-NEXT: - Index: 1
; PASSIVE-NEXT: Locals: []
; PASSIVE-NEXT: Body: 0B
; PASSIVE-NEXT: - Index: 2
; PASSIVE-NEXT: Locals: []
; PASSIVE32-NEXT: Body: 41B4D60041004101FE480200044041B4D6004101427FFE0102001A054180084100410DFC08000041900841004114FC08010041B4D6004102FE17020041B4D600417FFE0002001A0BFC0900FC09010B
; PASSIVE64-NEXT: Body: 42B4D60041004101FE480200044042B4D6004101427FFE0102001A054280084100410DFC08000042900841004114FC08010042B4D6004102FE17020042B4D600417FFE0002001A0BFC0900FC09010B

; PASSIVE-NEXT: - Index: 2
; PASSIVE-NEXT: Locals: []
; PASSIVE-NEXT: Body: 0B
; PASSIVE-NEXT: - Type: DATA
; PASSIVE-NEXT: Segments:
; PASSIVE-NEXT: - SectionOffset: 3
Expand All @@ -94,12 +93,12 @@
; PASSIVE-NEXT: - Index: 0
; PASSIVE-NEXT: Name: __wasm_call_ctors
; PASSIVE-NEXT: - Index: 1
; PASSIVE-NEXT: Name: __wasm_init_memory
; PASSIVE-NEXT: - Index: 2
; PASSIVE-NEXT: Name: __wasm_init_tls
; PASSIVE-NEXT: - Index: 2
; PASSIVE-NEXT: Name: __wasm_init_memory

; PASSIVE-PIC: - Type: START
; PASSIVE-PIC-NEXT: StartFunction: 2
; PASSIVE-PIC-NEXT: StartFunction: 3
; PASSIVE-PIC-NEXT: - Type: DATACOUNT
; PASSIVE-PIC-NEXT: Count: 1
; PASSIVE-PIC-NEXT: - Type: CODE
Expand All @@ -111,15 +110,15 @@
; PASSIVE-PIC-NEXT: Locals: []
; PASSIVE-PIC-NEXT: Body: 0B
; PASSIVE-PIC-NEXT: - Index: 2
; PASSIVE-PIC-NEXT: Locals: []
; PASSIVE-PIC-NEXT: Body: 0B
; PASSIVE-PIC-NEXT: - Index: 3
; PASSIVE-PIC-NEXT: Locals:
; PASSIVE32-PIC-NEXT: - Type: I32
; PASSIVE64-PIC-NEXT: - Type: I64
; PASSIVE-PIC-NEXT: Count: 1
; PASSIVE32-PIC-NEXT: Body: 230141B4CE006A2100200041004101FE480200044020004101427FFE0102001A05410023016A410041B1CE00FC08000020004102FE1702002000417FFE0002001A0BFC09000B
; PASSIVE64-PIC-NEXT: Body: 230142B4CE006A2100200041004101FE480200044020004101427FFE0102001A05420023016A410041B1CE00FC08000020004102FE1702002000417FFE0002001A0BFC09000B
; PASSIVE-PIC-NEXT: - Index: 3
; PASSIVE-PIC-NEXT: Locals: []
; PASSIVE-PIC-NEXT: Body: 0B
; PASSIVE-PIC-NEXT: - Type: DATA
; PASSIVE-PIC-NEXT: Segments:
; PASSIVE-PIC-NEXT: - SectionOffset: 4
Expand All @@ -133,6 +132,6 @@
; PASSIVE-PIC-NEXT: - Index: 1
; PASSIVE-PIC-NEXT: Name: __wasm_apply_relocs
; PASSIVE-PIC-NEXT: - Index: 2
; PASSIVE-PIC-NEXT: Name: __wasm_init_memory
; PASSIVE-PIC-NEXT: - Index: 3
; PASSIVE-PIC-NEXT: Name: __wasm_init_tls
; PASSIVE-PIC-NEXT: - Index: 3
; PASSIVE-PIC-NEXT: Name: __wasm_init_memory
2 changes: 1 addition & 1 deletion lld/test/wasm/no-tls.s
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ _start:
# CHECK-NEXT: Mutable: true
# CHECK-NEXT: InitExpr:
# CHECK-NEXT: Opcode: I32_CONST
# CHECK-NEXT: Value: 66576
# CHECK-NEXT: Value: 66560

# __tls_base
# CHECK-NEXT: - Index: 1
Expand Down
14 changes: 7 additions & 7 deletions lld/test/wasm/tls.s
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ tls3:
# CHECK-NEXT: Mutable: true
# CHECK-NEXT: InitExpr:
# CHECK-NEXT: Opcode: I32_CONST
# CHECK-NEXT: Value: 66592
# CHECK-NEXT: Value: 66576

# __tls_base
# CHECK-NEXT: - Index: 1
Expand Down Expand Up @@ -112,8 +112,8 @@ tls3:

# CHECK: - Type: CODE
# CHECK-NEXT: Functions:
# Skip __wasm_call_ctors and __wasm_init_memory
# CHECK: - Index: 2
# Skip __wasm_call_ctors
# CHECK: - Index: 1
# CHECK-NEXT: Locals: []
# CHECK-NEXT: Body: 2000240120004100410CFC0800000B

Expand All @@ -126,7 +126,7 @@ tls3:
# memory.init 1, 0
# end

# CHECK-NEXT: - Index: 3
# CHECK-NEXT: - Index: 2
# CHECK-NEXT: Locals: []
# CHECK-NEXT: Body: 2381808080004180808080006A0B

Expand All @@ -136,7 +136,7 @@ tls3:
# i32.add
# end

# CHECK-NEXT: - Index: 4
# CHECK-NEXT: - Index: 3
# CHECK-NEXT: Locals: []
# CHECK-NEXT: Body: 2381808080004184808080006A0B

Expand All @@ -146,7 +146,7 @@ tls3:
# i32.add
# end

# CHECK-NEXT: - Index: 5
# CHECK-NEXT: - Index: 4
# CHECK-NEXT: Locals: []
# CHECK-NEXT: Body: 2381808080004188808080006A0B

Expand All @@ -156,7 +156,7 @@ tls3:
# i32.add
# end

# CHECK-NEXT: - Index: 6
# CHECK-NEXT: - Index: 5
# CHECK-NEXT: Locals: []
# CHECK-NEXT: Body: 2383808080000B

Expand Down
9 changes: 0 additions & 9 deletions lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,15 +646,6 @@ static void createSyntheticSymbols() {
}

if (config->sharedMemory) {
// Passive segments are used to avoid memory being reinitialized on each
// thread's instantiation. These passive segments are initialized and
// dropped in __wasm_init_memory, which is registered as the start function
WasmSym::initMemory = symtab->addSyntheticFunction(
"__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
make<SyntheticFunction>(nullSignature, "__wasm_init_memory"));
WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol(
"__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN);
assert(WasmSym::initMemoryFlag);
WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
Expand Down
3 changes: 0 additions & 3 deletions lld/wasm/MarkLive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,6 @@ void MarkLive::run() {
if (WasmSym::applyRelocs)
enqueue(WasmSym::applyRelocs);

if (WasmSym::initMemory)
enqueue(WasmSym::initMemory);

// Enqueue constructors in objects explicitly live from the command-line.
for (const ObjFile *obj : symtab->objectFiles)
if (obj->isLive())
Expand Down
7 changes: 1 addition & 6 deletions lld/wasm/SyntheticSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,9 @@ class ExportSection : public SyntheticSection {

class StartSection : public SyntheticSection {
public:
StartSection(bool hasInitializedSegments)
: SyntheticSection(llvm::wasm::WASM_SEC_START),
hasInitializedSegments(hasInitializedSegments) {}
StartSection() : SyntheticSection(llvm::wasm::WASM_SEC_START) {}
bool isNeeded() const override;
void writeBody() override;

protected:
bool hasInitializedSegments;
};

class ElemSection : public SyntheticSection {
Expand Down
Loading

0 comments on commit 1994970

Please sign in to comment.