Skip to content

[lld][WebAssembly] Add an --initial-heap option #75594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lld/docs/WebAssembly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,13 @@ WebAssembly-specific options:
is not possible for undefined data symbols. Undefined data symbols will
still be reported as normal (in accordance with ``--unresolved-symbols``).

.. option:: --initial-heap=<value>

Initial size of the heap. Default: zero.

.. option:: --initial-memory=<value>

Initial size of the linear memory. Default: static data size.
Initial size of the linear memory. Default: the sum of stack, static data and heap sizes.

.. option:: --max-memory=<value>

Expand Down
27 changes: 27 additions & 0 deletions lld/test/wasm/initial-heap.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/start.s -o %t.o

; The initial heap size will be added to the stack size
RUN: wasm-ld %t.o -o %t1.wasm --stack-first -z stack-size=65536 --initial-heap=131072
RUN: obj2yaml %t1.wasm | FileCheck %s --check-prefixes=CHECK,CHECK-2P

; Also test that we can parse and process a large size correctly
RUN: wasm-ld %t.o -o %t2.wasm --stack-first -z stack-size=65536 --initial-heap=4294901760
RUN: obj2yaml %t2.wasm | FileCheck %s --check-prefixes=CHECK,CHECK-4G

CHECK: - Type: MEMORY
CHECK-NEXT: Memories:
CHECK-2P-NEXT: Minimum: 0x3
CHECK-4G-NEXT: Minimum: 0x10000

; Test various error cases.
RUN: not wasm-ld %t.o -o %t3.wasm --initial-heap=131073 2>&1 | FileCheck %s --check-prefix NOT-PAGE-MULTIPLE
RUN: not wasm-ld %t.o -o %t4.wasm --stack-first -z stack-size=65536 --initial-heap=4295032832 2>&1 | FileCheck %s --check-prefix TOO-LARGE-BY-ITSELF
RUN: not wasm-ld %t.o -o %t5.wasm --stack-first -z stack-size=131072 --initial-heap=4294901760 2>&1 | FileCheck %s --check-prefix TOO-LARGE-WITH-STACK
RUN: not wasm-ld %t.o -o %t6.wasm --stack-first -z stack-size=65536 --initial-heap=131072 --initial-memory=131072 2>&1 | FileCheck %s --check-prefix INITIAL-MEMORY-TOO-SMALL
RUN: not wasm-ld %t.o -o %t7.wasm --stack-first -z stack-size=65536 --initial-heap=131072 --max-memory=131072 2>&1 | FileCheck %s --check-prefix MAX-MEMORY-TOO-SMALL

NOT-PAGE-MULTIPLE: initial heap must be 65536-byte aligned
TOO-LARGE-BY-ITSELF: initial heap too large, cannot be greater than 4294901760
TOO-LARGE-WITH-STACK: initial heap too large, cannot be greater than 4294836224
INITIAL-MEMORY-TOO-SMALL: initial memory too small, 196608 bytes needed
MAX-MEMORY-TOO-SMALL: maximum memory too small, 196608 bytes needed
1 change: 1 addition & 0 deletions lld/wasm/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct Configuration {
bool isStatic = false;
bool trace;
uint64_t globalBase;
uint64_t initialHeap;
uint64_t initialMemory;
uint64_t maxMemory;
uint64_t zStackSize;
Expand Down
5 changes: 3 additions & 2 deletions lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,10 @@ static void readConfigs(opt::InputArgList &args) {
errorHandler().verbose = args.hasArg(OPT_verbose);
LLVM_DEBUG(errorHandler().verbose = true);

config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
config->globalBase = args::getInteger(args, OPT_global_base, 0);
config->tableBase = args::getInteger(args, OPT_table_base, 0);
config->globalBase = args::getInteger(args, OPT_global_base, 0);
config->initialHeap = args::getInteger(args, OPT_initial_heap, 0);
config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
config->zStackSize =
args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
Expand Down
3 changes: 3 additions & 0 deletions lld/wasm/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ defm soname: Eq<"soname", "Set the module name in the generated name section">;
def import_table: FF<"import-table">,
HelpText<"Import function table from the environment">;

def initial_heap: JJ<"initial-heap=">,
HelpText<"Initial size of the heap">;

def initial_memory: JJ<"initial-memory=">,
HelpText<"Initial size of the linear memory">;

Expand Down
10 changes: 10 additions & 0 deletions lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ void Writer::layoutMemory() {
maxMemorySetting = 1ULL << 34;
}

if (config->initialHeap != 0) {
if (config->initialHeap != alignTo(config->initialHeap, WasmPageSize))
error("initial heap must be " + Twine(WasmPageSize) + "-byte aligned");
uint64_t maxInitialHeap = maxMemorySetting - memoryPtr;
if (config->initialHeap > maxInitialHeap)
error("initial heap too large, cannot be greater than " +
Twine(maxInitialHeap));
memoryPtr += config->initialHeap;
}

if (config->initialMemory != 0) {
if (config->initialMemory != alignTo(config->initialMemory, WasmPageSize))
error("initial memory must be " + Twine(WasmPageSize) + "-byte aligned");
Expand Down