Skip to content

Commit 3bea8bc

Browse files
committedNov 6, 2018
[WebAssembly] Support creation and import of shared memories
Used for WebAssembly threads proposal. Add a flag --shared-memory which sets the IS_SHARED bit in WasmLimits Differential Revision: https://reviews.llvm.org/D54130 llvm-svn: 346248
1 parent 05620d8 commit 3bea8bc

File tree

6 files changed

+39
-2
lines changed

6 files changed

+39
-2
lines changed
 

‎lld/test/wasm/data-layout.ll

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ target triple = "wasm32-unknown-unknown"
6868
; CHECK-MAX-NEXT: Initial: 0x00000002
6969
; CHECK-MAX-NEXT: Maximum: 0x00000002
7070

71+
; RUN: wasm-ld -no-gc-sections --allow-undefined --no-entry --shared-memory \
72+
; RUN: --initial-memory=131072 --max-memory=131072 -o %t_max.wasm %t.o \
73+
; RUN: %t.hello.o
74+
; RUN: obj2yaml %t_max.wasm | FileCheck %s -check-prefix=CHECK-SHARED
75+
76+
; CHECK-SHARED: - Type: MEMORY
77+
; CHECK-SHARED-NEXT: Memories:
78+
; CHECK-SHARED-NEXT: - Flags: [ HAS_MAX, IS_SHARED ]
79+
; CHECK-SHARED-NEXT: Initial: 0x00000002
80+
; CHECK-SHARED-NEXT: Maximum: 0x00000002
7181

7282
; RUN: wasm-ld --relocatable -o %t_reloc.wasm %t.o %t.hello.o
7383
; RUN: obj2yaml %t_reloc.wasm | FileCheck %s -check-prefix=RELOC

‎lld/test/wasm/import-memory.test

+17
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,20 @@
3131
# CHECK-MAX-NEXT: Initial: 0x00000004
3232
# CHECK-MAX-NEXT: Maximum: 0x00000005
3333
# CHECK-MAX-NEXT: - Type:
34+
35+
# RUN: wasm-ld --import-memory --shared-memory --initial-memory=262144 \
36+
# RUN: --max-memory=327680 -o %t.max.wasm %t.start.o
37+
# RUN: obj2yaml %t.max.wasm | FileCheck -check-prefix=CHECK-SHARED %s
38+
39+
# Verify the --shared-memory flag works with imports
40+
41+
# CHECK-SHARED: - Type: IMPORT
42+
# CHECK-SHARED-NEXT: Imports:
43+
# CHECK-SHARED-NEXT: - Module: env
44+
# CHECK-SHARED-NEXT: Field: memory
45+
# CHECK-SHARED-NEXT: Kind: MEMORY
46+
# CHECK-SHARED-NEXT: Memory:
47+
# CHECK-SHARED-NEXT: Flags: [ HAS_MAX, IS_SHARED ]
48+
# CHECK-SHARED-NEXT: Initial: 0x00000004
49+
# CHECK-SHARED-NEXT: Maximum: 0x00000005
50+
# CHECK-SHARED-NEXT: - Type:

‎lld/wasm/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct Configuration {
2828
bool ExportTable;
2929
bool GcSections;
3030
bool ImportMemory;
31+
bool SharedMemory;
3132
bool ImportTable;
3233
bool MergeDataSegments;
3334
bool PrintGcSections;

‎lld/wasm/Driver.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
381381
errorHandler().FatalWarnings =
382382
Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
383383
Config->ImportMemory = Args.hasArg(OPT_import_memory);
384+
Config->SharedMemory = Args.hasArg(OPT_shared_memory);
384385
Config->ImportTable = Args.hasArg(OPT_import_table);
385386
Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
386387
Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);

‎lld/wasm/Options.td

+3
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ def global_base: J<"global-base=">,
123123
def import_memory: F<"import-memory">,
124124
HelpText<"Import memory from the environment">;
125125

126+
def shared_memory: F<"shared-memory">,
127+
HelpText<"Use shared linear memory">;
128+
126129
def import_table: F<"import-table">,
127130
HelpText<"Import function table from the environment">;
128131

‎lld/wasm/Writer.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ void Writer::createImportSection() {
155155
Import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
156156
Import.Memory.Maximum = MaxMemoryPages;
157157
}
158+
if (Config->SharedMemory) {
159+
Import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
160+
}
158161
writeImport(OS, Import);
159162
}
160163

@@ -214,8 +217,10 @@ void Writer::createMemorySection() {
214217

215218
bool HasMax = MaxMemoryPages != 0;
216219
writeUleb128(OS, 1, "memory count");
217-
writeUleb128(OS, HasMax ? static_cast<unsigned>(WASM_LIMITS_FLAG_HAS_MAX) : 0,
218-
"memory limits flags");
220+
unsigned Flags = HasMax ? static_cast<unsigned>(WASM_LIMITS_FLAG_HAS_MAX) : 0;
221+
if (Config->SharedMemory)
222+
Flags |= WASM_LIMITS_FLAG_IS_SHARED;
223+
writeUleb128(OS, Flags, "memory limits flags");
219224
writeUleb128(OS, NumMemoryPages, "initial pages");
220225
if (HasMax)
221226
writeUleb128(OS, MaxMemoryPages, "max pages");

0 commit comments

Comments
 (0)
Please sign in to comment.