Skip to content

Commit 7080012

Browse files
authored
[NFC] Assert we do not use --low-memory-unused when STACK_FIRST (#21291)
Assuming low memory was unused when the stack was first - which is where low memory is - would be wrong. We never did that wrong thing as we only use that optimization when not optimizing. Add an assertion + tests to prevent regressions.
1 parent 7bba399 commit 7080012

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

test/test_other.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def uses_canonical_tmp(func):
6363
test to satisfy the leak detector.
6464
"""
6565
@wraps(func)
66-
def decorated(self):
66+
def decorated(self, *args, **kwargs):
6767
# Before running the test completely remove the canonical_tmp
6868
if os.path.exists(self.canonical_temp_dir):
6969
shutil.rmtree(self.canonical_temp_dir)
7070
try:
71-
func(self)
71+
func(self, *args, **kwargs)
7272
finally:
7373
# Make sure the test isn't lying about the fact that it uses
7474
# canonical_tmp
@@ -1206,6 +1206,25 @@ def test_wl_stackfirst(self):
12061206
err = self.expect_fail(cmd + ['-sGLOBAL_BASE=1024'])
12071207
self.assertContained('error: --stack-first is not compatible with -sGLOBAL_BASE', err)
12081208

1209+
@parameterized({
1210+
# In a simple -O0 build we do not set --low-memory-unused (as the stack is
1211+
# first, which is nice for debugging but bad for code size (larger globals)
1212+
# and bad for the low-memory-unused trick.
1213+
'': ([], False),
1214+
# When we optimize, we do.
1215+
'O2': (['-O2'], True),
1216+
# But a low global base prevents it.
1217+
'O2_GB_512': (['-O2', '-sGLOBAL_BASE=512'], False),
1218+
# A large-enough global base allows it.
1219+
'O2_GB_1024': (['-O2', '-sGLOBAL_BASE=1024'], True),
1220+
# Forcing the stack to be first in the linker prevents it.
1221+
'linker_flag': (['-O2', '-Wl,--stack-first'], False),
1222+
})
1223+
def test_binaryen_low_memory_unused(self, args, low_memory_unused):
1224+
cmd = [EMCC, test_file('hello_world.c'), '-v'] + args
1225+
err = self.run_process(cmd, stdout=PIPE, stderr=PIPE).stderr
1226+
self.assertContainedIf('--low-memory-unused ', err, low_memory_unused)
1227+
12091228
def test_l_link(self):
12101229
# Linking with -lLIBNAME and -L/DIRNAME should work, also should work with spaces
12111230
create_file('main.c', '''

tools/link.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ def get_binaryen_passes(memfile):
357357
if optimizing:
358358
passes += [building.opt_level_to_str(settings.OPT_LEVEL, settings.SHRINK_LEVEL)]
359359
# when optimizing, use the fact that low memory is never used (1024 is a
360-
# hardcoded value in the binaryen pass)
361-
if optimizing and settings.GLOBAL_BASE >= 1024:
360+
# hardcoded value in the binaryen pass). we also cannot do it when the stack
361+
# is first, as then the stack is in the low memory that should be unused.
362+
if optimizing and settings.GLOBAL_BASE >= 1024 and not settings.STACK_FIRST:
362363
passes += ['--low-memory-unused']
363364
if settings.AUTODEBUG:
364365
# adding '--flatten' here may make these even more effective

0 commit comments

Comments
 (0)