From ace4c87395e891a916eeca09fe26d1d33c4cf373 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 4 Jun 2020 06:56:08 +0200 Subject: [PATCH 1/2] Increase default Wasm stack to 1MB This commit increases the default Wasm stack to 1MB from the default of 1 Wasm page which equal 64KB. This seems like a reasonable default size while at the same time not overly large. Also, Rust lang seems to be favouring this default as well: [rust-lang#50083]. [rust-lang#50083]: https://github.com/rust-lang/rust/pull/50083 --- src/link.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/link.cpp b/src/link.cpp index 546c5cabc136..72997b41561b 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -2100,6 +2100,10 @@ static void construct_linker_job_wasm(LinkJob *lj) { CodeGen *g = lj->codegen; lj->args.append("-error-limit=0"); + // Increase the default stack size to a more reasonable value of 1MB instead of + // the default of 1 Wasm page being 64KB. + lj->args.append("-z"); + lj->args.append("stack-size=1048576"); if (g->out_type != OutTypeExe) { lj->args.append("--no-entry"); // So lld doesn't look for _start. From 22edc8253b422b9b25dd2f44a7546e4c32a0f40c Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 5 Jun 2020 22:21:33 +0200 Subject: [PATCH 2/2] Add option for overriding the stack size This commit adds a `--stack [size]` link-time option to zig compiler allowing the user to override the default stack size set for the specified executable/library format. This is currently limited to ELF, COFF and Wasm however (i.e., Mach-O is excluded). --- src/link.cpp | 5 +++-- src/main.cpp | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/link.cpp b/src/link.cpp index 72997b41561b..0f27cee9ab78 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -2101,9 +2101,10 @@ static void construct_linker_job_wasm(LinkJob *lj) { lj->args.append("-error-limit=0"); // Increase the default stack size to a more reasonable value of 1MB instead of - // the default of 1 Wasm page being 64KB. + // the default of 1 Wasm page being 64KB, unless overriden by the user. + size_t stack_size = (g->stack_size_override == 0) ? 1048576 : g->stack_size_override; lj->args.append("-z"); - lj->args.append("stack-size=1048576"); + lj->args.append(buf_ptr(buf_sprintf("stack-size=%" ZIG_PRI_usize, stack_size))); if (g->out_type != OutTypeExe) { lj->args.append("--no-entry"); // So lld doesn't look for _start. diff --git a/src/main.cpp b/src/main.cpp index 41aade9431a5..6d5f1f3f7671 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -126,6 +126,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " -l[lib] alias for --library\n" " -rdynamic add all symbols to the dynamic symbol table\n" " -rpath [path] add directory to the runtime library search path\n" + " --stack [size] (linux, windows, Wasm) override default stack size\n" " --subsystem [subsystem] (windows) /SUBSYSTEM: to the linker\n" " -F[dir] (darwin) add search path for frameworks\n" " -framework [name] (darwin) link against framework\n" @@ -1231,6 +1232,8 @@ static int main0(int argc, char **argv) { ver_patch = atoi(argv[i]); } else if (strcmp(arg, "--test-cmd") == 0) { test_exec_args.append(argv[i]); + } else if (strcmp(arg, "--stack") == 0) { + stack_size_override = atoi(argv[i]); } else if (strcmp(arg, "--subsystem") == 0) { if (strcmp(argv[i], "console") == 0) { subsystem = TargetSubsystemConsole;