Skip to content
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

Initial new INITIAL_HEAP setting #21071

Merged
merged 29 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b43181e
Initial INITIAL_HEAP support
SingleAccretion Jan 12, 2024
79bf8d7
Comment out the memory size assert
SingleAccretion Jan 13, 2024
4def2c7
Adjust a couple tests
SingleAccretion Jan 13, 2024
5ddd199
Rebaseline a couple code size tests
SingleAccretion Jan 13, 2024
d893b4d
A better INITIAL_HEAP description
SingleAccretion Jan 28, 2024
79958b2
Remove the INITIAL_MEMORY assert
SingleAccretion Jan 28, 2024
a4e3cfc
Single quotes in python
SingleAccretion Jan 28, 2024
f994f46
Prettify the code a little
SingleAccretion Jan 28, 2024
1433dd8
Make test_sysconf_phys_pages the default initial memory test
SingleAccretion Jan 28, 2024
c8b285e
Add a change log entry
SingleAccretion Jan 28, 2024
eae41e0
Recommend INITIAL_HEAP
SingleAccretion Jan 28, 2024
3da94a8
Also update the doc
SingleAccretion Jan 28, 2024
0906505
Merge branch 'main' into Initial-Heap
SingleAccretion Jan 28, 2024
a3786ec
Merge fix...
SingleAccretion Jan 28, 2024
3c76f5b
Merge branch 'main' into Initial-Heap
SingleAccretion Jan 30, 2024
db45ed2
Rebaseline once more
SingleAccretion Jan 30, 2024
d71d239
More rebaselining
SingleAccretion Jan 30, 2024
47ec651
Work around the WASM2JS regression
SingleAccretion Jan 30, 2024
b443e06
Better comments and documentation
SingleAccretion Jan 30, 2024
0dc6457
Merge branch 'main' into Initial-Heap
SingleAccretion Feb 15, 2024
9a885f1
Fix up the change log
SingleAccretion Feb 15, 2024
d2cdd4e
Use @parameterized for the test
SingleAccretion Feb 15, 2024
a37cafe
Brush-ups
SingleAccretion Feb 15, 2024
1ce8cd6
Delete some files commited by mistake
SingleAccretion Feb 17, 2024
a36cf25
Merge branch 'main' into Initial-Heap
SingleAccretion Mar 1, 2024
d89c454
Use --no-growable-memory when memory growth is not allowed
SingleAccretion Mar 1, 2024
ea3b557
Change log fixup
SingleAccretion Mar 1, 2024
d688893
Rebaseline a test
SingleAccretion Mar 1, 2024
37eeea5
Test nits and fixes
SingleAccretion Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion ChangeLog.md
Expand Up @@ -49,7 +49,10 @@ See docs/process.md for more on how version tagging works.
loaded on demand using the syntax `--use-port=/path/to/my_port.py` (#21316)
- Allow comments in response files. Any line starting with `#` is now ignored.
This is useful when listing exported symbols. (#21330)

- `INITIAL_HEAP` setting is introduced to control the amount of initial
memory available for dynamic allocation without capping it. If you are
using `INITIAL_MEMORY`, consider switching to `INITIAL_HEAP`. Note that
it is currently not supported in all configurations (#21071).

3.1.53 - 01/29/24
-----------------
Expand Down
16 changes: 16 additions & 0 deletions site/source/docs/tools_reference/settings_reference.rst
Expand Up @@ -151,6 +151,19 @@ Note that this setting does not affect the behavior of operator new in C++.
This function will always abort on allocation failure if exceptions are disabled.
If you want new to return 0 on failure, use it with std::nothrow.

.. _initial_heap:

INITIAL_HEAP
============

The initial amount of heap memory available to the program. This is the
memory region available for dynamic allocations via `sbrk`, `malloc` and `new`.

Unlike INITIAL_MEMORY, this setting allows the static and dynamic regions of
your programs memory to independently grow. In most cases we recommend using
this setting rather than `INITIAL_MEMORY`. However, this setting does not work
for imported memories (e.g. when dynamic linking is used).

.. _initial_memory:

INITIAL_MEMORY
Expand All @@ -162,6 +175,9 @@ we need to copy the old heap into a new one in that case.
If ALLOW_MEMORY_GROWTH is set, this initial amount of memory can increase
later; if not, then it is the final and total amount of memory.

By default, this value is calculated based on INITIAL_HEAP, STACK_SIZE,
as well the size of static data in input modules.
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved

(This option was formerly called TOTAL_MEMORY.)

.. _maximum_memory:
Expand Down
1 change: 0 additions & 1 deletion src/postamble_minimal.js
Expand Up @@ -216,7 +216,6 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
wasmMemory = wasmExports['memory'];
#if ASSERTIONS
assert(wasmMemory);
assert(wasmMemory.buffer.byteLength === {{{ INITIAL_MEMORY }}});
#endif
updateMemoryViews();
#endif
Expand Down
4 changes: 0 additions & 4 deletions src/preamble.js
Expand Up @@ -983,10 +983,6 @@ function createWasm() {
{{{ receivedSymbol('wasmMemory') }}}
#if ASSERTIONS
assert(wasmMemory, 'memory not found in wasm exports');
// This assertion doesn't hold when emscripten is run in --post-link
// mode.
// TODO(sbc): Read INITIAL_MEMORY out of the wasm file in post-link mode.
//assert(wasmMemory.buffer.byteLength === {{{ INITIAL_MEMORY }}});
#endif
updateMemoryViews();
#endif
Expand Down
16 changes: 15 additions & 1 deletion src/settings.js
Expand Up @@ -154,15 +154,29 @@ var MALLOC = "dlmalloc";
// [link]
var ABORTING_MALLOC = true;

// The initial amount of heap memory available to the program. This is the
// memory region available for dynamic allocations via `sbrk`, `malloc` and `new`.
//
// Unlike INITIAL_MEMORY, this setting allows the static and dynamic regions of
// your programs memory to independently grow. In most cases we recommend using
// this setting rather than `INITIAL_MEMORY`. However, this setting does not work
// for imported memories (e.g. when dynamic linking is used).
//
// [link]
var INITIAL_HEAP = 16777216;

// The initial amount of memory to use. Using more memory than this will
// cause us to expand the heap, which can be costly with typed arrays:
// we need to copy the old heap into a new one in that case.
// If ALLOW_MEMORY_GROWTH is set, this initial amount of memory can increase
// later; if not, then it is the final and total amount of memory.
//
// By default, this value is calculated based on INITIAL_HEAP, STACK_SIZE,
// as well the size of static data in input modules.
//
// (This option was formerly called TOTAL_MEMORY.)
// [link]
var INITIAL_MEMORY = 16777216;
var INITIAL_MEMORY = -1;

// Set the maximum size of memory in the wasm module (in bytes). This is only
// relevant when ALLOW_MEMORY_GROWTH is set, as without growth, the size of
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 7080,
"a.js.gz": 2999,
"a.wasm": 11433,
"a.wasm.gz": 5725,
"total": 19186,
"total_gz": 9155
"a.wasm": 11434,
"a.wasm.gz": 5726,
"total": 19187,
"total_gz": 9156
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4589,
"a.js.gz": 2341,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15609,
"total_gz": 9444
"a.wasm": 10452,
"a.wasm.gz": 6725,
"total": 15610,
"total_gz": 9445
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 379,
"a.js": 4075,
"a.js.gz": 2170,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15095,
"total_gz": 9273
"a.wasm": 10452,
"a.wasm.gz": 6725,
"total": 15096,
"total_gz": 9274
}
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm.json
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 291,
"a.js.gz": 249,
"a.wasm": 103,
"a.wasm.gz": 112,
"total": 1067,
"total_gz": 792
"a.wasm": 104,
"a.wasm.gz": 114,
"total": 1068,
"total_gz": 794
}
8 changes: 4 additions & 4 deletions test/code_size/math_wasm.json
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 431,
"a.js": 110,
"a.js.gz": 125,
"a.wasm": 2724,
"a.wasm.gz": 1667,
"total": 3507,
"total_gz": 2223
"a.wasm": 2725,
"a.wasm.gz": 1669,
"total": 3508,
"total_gz": 2225
}
3 changes: 2 additions & 1 deletion test/test_core.py
Expand Up @@ -2109,7 +2109,7 @@ def test_memorygrowth_geometric_step(self):
if not self.is_wasm():
self.skipTest('wasm memory specific test')

self.emcc_args += ['-sALLOW_MEMORY_GROWTH', '-sMEMORY_GROWTH_GEOMETRIC_STEP=8.5', '-sMEMORY_GROWTH_GEOMETRIC_CAP=32MB']
self.emcc_args += ['-sINITIAL_MEMORY=16MB', '-sALLOW_MEMORY_GROWTH', '-sMEMORY_GROWTH_GEOMETRIC_STEP=8.5', '-sMEMORY_GROWTH_GEOMETRIC_CAP=32MB']
self.do_core_test('test_memorygrowth_geometric_step.c')

def test_memorygrowth_3_force_fail_reallocBuffer(self):
Expand Down Expand Up @@ -6007,6 +6007,7 @@ def test_unistd_sysconf_phys_pages(self):
assert self.get_setting('INITIAL_MEMORY') == '2200mb'
expected = (2200 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE
else:
self.set_setting('INITIAL_MEMORY', '16mb')
expected = 16 * 1024 * 1024 // webassembly.WASM_PAGE_SIZE
self.do_runf(filename, str(expected) + ', errno: 0')

Expand Down
33 changes: 30 additions & 3 deletions test/test_other.py
Expand Up @@ -6538,7 +6538,7 @@ def test_massive_alloc(self, wasm):
return x == 0; // can't alloc it, but don't fail catastrophically, expect null
}
''')
cmd = [EMCC, 'main.c', '-sALLOW_MEMORY_GROWTH']
cmd = [EMCC, 'main.c', '-sALLOW_MEMORY_GROWTH', '-sINITIAL_MEMORY=16MB']
if not wasm:
cmd += ['-sWASM=0']
self.run_process(cmd)
Expand Down Expand Up @@ -6597,7 +6597,7 @@ def test_failing_alloc(self):
printf("managed another malloc!\n");
}
''' % (pre_fail, post_fail))
args = [EMXX, 'main.cpp', '-sEXPORTED_FUNCTIONS=_main,_sbrk'] + opts + aborting_args
args = [EMXX, 'main.cpp', '-sEXPORTED_FUNCTIONS=_main,_sbrk', '-sINITIAL_MEMORY=16MB'] + opts + aborting_args
args += ['-sTEST_MEMORY_GROWTH_FAILS'] # In this test, force memory growing to fail
if growth:
args += ['-sALLOW_MEMORY_GROWTH']
Expand Down Expand Up @@ -8132,6 +8132,30 @@ def test_binaryen_warn_mem(self):
self.run_process([EMCC, test_file('hello_world.c'), '-sINITIAL_MEMORY=' + str(16 * 1024 * 1024), '--pre-js', 'pre.js', '-sALLOW_MEMORY_GROWTH', '-sWASM_ASYNC_COMPILATION=0', '-sIMPORTED_MEMORY'])
self.assertContained('hello, world!', self.run_js('a.out.js'))

@parameterized({
'': ([], 16 * 1024 * 1024), # Default behavior: 16MB initial heap
'with_initial_memory': (['-sINITIAL_MEMORY=40MB'], 0), # Backwards compatibility: no initial heap (we can't tell if it'll fit)
'with_maximum_memory': (['-sMAXIMUM_MEMORY=40MB'], 0), # Backwards compatibility: no initial heap (we can't tell if it'll fit)
'with_initial_heap': (['-sINITIAL_HEAP=64KB'], 64 * 1024), # Explicitly set initial heap is passed
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
'with_all': (['-sINITIAL_HEAP=128KB', '-sINITIAL_MEMORY=20MB', '-sMAXIMUM_MEMORY=40MB'], 128 * 1024),
'limited_by_initial_memory': (['-sINITIAL_HEAP=10MB', '-sINITIAL_MEMORY=10MB'], -1), # Not enough space for stack
'limited_by_maximum_memory': (['-sINITIAL_HEAP=5MB', '-sMAXIMUM_MEMORY=5MB'], -1), # Not enough space for stack
})
def test_initial_heap(self, args, expected_initial_heap):
cmd = [EMCC, test_file('hello_world.c'), '-v'] + args
print(' '.join(cmd))
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved

if expected_initial_heap < 0:
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
out = self.expect_fail(cmd)
self.assertContained('wasm-ld: error:', out)
return

out = self.run_process(cmd, stderr=PIPE)
if expected_initial_heap != 0:
self.assertContained('--initial-heap=' + str(expected_initial_heap), out.stderr)
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
else:
self.assertNotContained('--initial-heap=', out.stderr)

def test_memory_size(self):
for args, expect_initial, expect_max in [
([], 320, 320),
Expand Down Expand Up @@ -8161,6 +8185,9 @@ def test_invalid_mem(self):
self.assertContained('hello, world!', self.run_js('a.out.js'))

# Must be a multiple of 64KB
ret = self.expect_fail([EMCC, test_file('hello_world.c'), '-sINITIAL_HEAP=32505857', '-sALLOW_MEMORY_GROWTH']) # 31MB + 1 byte
self.assertContained('INITIAL_HEAP must be a multiple of WebAssembly page size (64KiB)', ret)

ret = self.expect_fail([EMCC, test_file('hello_world.c'), '-sINITIAL_MEMORY=33554433']) # 32MB + 1 byte
self.assertContained('INITIAL_MEMORY must be a multiple of WebAssembly page size (64KiB)', ret)

Expand Down Expand Up @@ -8611,7 +8638,7 @@ def run(args, expected):
result = self.run_js('a.out.js').strip()
self.assertEqual(result, f'{expected}, errno: 0')

run([], 256)
run([], 258)
run(['-sINITIAL_MEMORY=32MB'], 512)
run(['-sINITIAL_MEMORY=32MB', '-sALLOW_MEMORY_GROWTH'], (2 * 1024 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE)
run(['-sINITIAL_MEMORY=32MB', '-sALLOW_MEMORY_GROWTH', '-sWASM=0'], (2 * 1024 * 1024 * 1024) // webassembly.WASM_PAGE_SIZE)
Expand Down
14 changes: 7 additions & 7 deletions tools/building.py
Expand Up @@ -220,13 +220,13 @@ def lld_flags_for_executable(external_symbols):
cmd.append('--growable-table')

if not settings.SIDE_MODULE:
# Export these two section start symbols so that we can extract the string
# data that they contain.
cmd += [
'-z', 'stack-size=%s' % settings.STACK_SIZE,
'--initial-memory=%d' % settings.INITIAL_MEMORY,
'--max-memory=%d' % settings.MAXIMUM_MEMORY,
]
cmd += ['-z', 'stack-size=%s' % settings.STACK_SIZE]
cmd += ['--max-memory=%d' % settings.MAXIMUM_MEMORY]
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved

if settings.INITIAL_HEAP != -1:
cmd += ['--initial-heap=%d' % settings.INITIAL_HEAP]
if settings.INITIAL_MEMORY != -1:
SingleAccretion marked this conversation as resolved.
Show resolved Hide resolved
cmd += ['--initial-memory=%d' % settings.INITIAL_MEMORY]

if settings.STANDALONE_WASM:
# when settings.EXPECT_MAIN is set we fall back to wasm-ld default of _start
Expand Down