Skip to content

Commit e8023f1

Browse files
authored
Remove STATIC* JS vars and STATIC_BUMP setting (#12176)
These used to make sense when we had static allocations in JS. We'd use the "bump" to track the total size, which was increased by the JS compiler, and at runtime we'd have STATIC_BASE and STATICTOP. Now that we disallow static allocations from the JS compiler, we don't need them. Note that we do still track "staticBump" in the metadata from finalize. That is the total size of static memory from lld, which never changes. We use it to compute where the stack begins (which determines the rest of memory layout). As a followup we may be able to use a link map or other mechanism to just read that info instead of computing it in emscripten.py See #11860
1 parent 79da9f9 commit e8023f1

12 files changed

+13
-46
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ def check(input_file):
14141414

14151415
shared.Settings.EXPORTED_FUNCTIONS += ['_stackSave', '_stackRestore', '_stackAlloc']
14161416
# We need to preserve the __data_end symbol so that wasm-emscripten-finalize can determine
1417-
# the STATIC_BUMP value.
1417+
# where static data ends (and correspondingly where the stack begins).
14181418
shared.Settings.EXPORTED_FUNCTIONS += ['___data_end']
14191419
if not shared.Settings.STANDALONE_WASM:
14201420
# in standalone mode, crt1 will call the constructors from inside the wasm

emscripten.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,6 @@ def align_memory(addr):
116116
return (addr + 15) & -16
117117

118118

119-
def align_static_bump(metadata):
120-
# TODO: remove static bump entirely
121-
metadata['staticBump'] = align_memory(metadata['staticBump'])
122-
return metadata['staticBump']
123-
124-
125119
def update_settings_glue(metadata, DEBUG):
126120
optimize_syscalls(metadata['declares'], DEBUG)
127121

@@ -159,7 +153,7 @@ def read_proxied_function_signatures(asmConsts):
159153

160154
shared.Settings.PROXIED_FUNCTION_SIGNATURES = read_proxied_function_signatures(metadata['asmConsts'])
161155

162-
shared.Settings.STATIC_BUMP = align_static_bump(metadata)
156+
metadata['staticBump'] = align_memory(metadata['staticBump'])
163157

164158
shared.Settings.BINARYEN_FEATURES = metadata['features']
165159
shared.Settings.WASM_TABLE_SIZE = metadata['tableSize']
@@ -188,7 +182,6 @@ def apply_static_code_hooks(code):
188182
def apply_forwarded_data(forwarded_data):
189183
forwarded_json = json.loads(forwarded_data)
190184
# Be aware of JS static allocations
191-
shared.Settings.STATIC_BUMP = forwarded_json['STATIC_BUMP']
192185
# Be aware of JS static code hooks
193186
StaticCodeHooks.atinits = str(forwarded_json['ATINITS'])
194187
StaticCodeHooks.atmains = str(forwarded_json['ATMAINS'])
@@ -216,15 +209,15 @@ def compile_settings(temp_files):
216209

217210

218211
class Memory():
219-
def __init__(self):
212+
def __init__(self, metadata):
220213
# Note: if RELOCATABLE, then only relative sizes can be computed, and we don't
221214
# actually write out any absolute memory locations ({{{ STACK_BASE }}}
222215
# does not exist, etc.)
223216

224217
# Memory layout:
225218
# * first the static globals
226219
self.global_base = shared.Settings.GLOBAL_BASE
227-
self.static_bump = shared.Settings.STATIC_BUMP
220+
self.static_bump = metadata['staticBump']
228221
# * then the stack (up on fastcomp, down on upstream)
229222
self.stack_low = align_memory(self.global_base + self.static_bump)
230223
self.stack_high = align_memory(self.stack_low + shared.Settings.TOTAL_STACK)
@@ -237,9 +230,9 @@ def __init__(self):
237230
exit_with_error('Memory is not large enough for static data (%d) plus the stack (%d), please increase INITIAL_MEMORY (%d) to at least %d' % (self.static_bump, shared.Settings.TOTAL_STACK, shared.Settings.INITIAL_MEMORY, self.dynamic_base))
238231

239232

240-
def apply_memory(js):
233+
def apply_memory(js, metadata):
241234
# Apply the statically-at-compile-time computed memory locations.
242-
memory = Memory()
235+
memory = Memory(metadata)
243236

244237
# Write it all out
245238
js = js.replace('{{{ STATIC_BUMP }}}', str(memory.static_bump))
@@ -447,8 +440,6 @@ def emscript(infile, outfile, memfile, temp_files, DEBUG):
447440

448441
global_initializers = ', '.join('{ func: function() { %s() } }' % i for i in metadata['initializers'])
449442

450-
staticbump = shared.Settings.STATIC_BUMP
451-
452443
if shared.Settings.MINIMAL_RUNTIME:
453444
# In minimal runtime, global initializers are run after the Wasm Module instantiation has finished.
454445
global_initializers = ''
@@ -458,11 +449,9 @@ def emscript(infile, outfile, memfile, temp_files, DEBUG):
458449
''' % ('if (!ENVIRONMENT_IS_PTHREAD)' if shared.Settings.USE_PTHREADS else '',
459450
global_initializers)
460451

461-
pre = pre.replace('STATICTOP = STATIC_BASE + 0;', '''STATICTOP = STATIC_BASE + %d;
462-
%s
463-
''' % (staticbump, global_initializers))
452+
pre += '\n' + global_initializers + '\n'
464453

465-
pre = apply_memory(pre)
454+
pre = apply_memory(pre, metadata)
466455
pre = apply_static_code_hooks(pre) # In regular runtime, atinits etc. exist in the preamble part
467456
post = apply_static_code_hooks(post) # In MINIMAL_RUNTIME, atinit exists in the postamble part
468457

src/jsifier.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,6 @@ function JSify(data, functionsOnly) {
394394
//
395395

396396
if (!mainPass) {
397-
if (!Variables.generatedGlobalBase) {
398-
Variables.generatedGlobalBase = true;
399-
// Globals are done, here is the rest of static memory
400-
// emit "metadata" in a comment. FIXME make this nicer
401-
print('// STATICTOP = STATIC_BASE + ' + alignMemory(Variables.nextIndexedOffset) + ';\n');
402-
}
403397
var generated = itemsDict.function.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.GlobalVariable);
404398
print(generated.map(function(item) { return item.JS; }).join('\n'));
405399

src/library_trace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ var LibraryTracing = {
262262
emscripten_trace_report_memory_layout: function() {
263263
if (EmscriptenTrace.postEnabled) {
264264
var memory_layout = {
265-
'static_base': STATIC_BASE,
265+
'static_base': {{{ GLOBAL_BASE }}},
266266
'stack_base': STACK_BASE,
267267
'stack_top': STACKTOP,
268268
'stack_max': STACK_MAX,

src/memoryprofiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ var emscriptenMemoryProfiler = {
476476

477477
var width = (nBits(HEAP8.length) + 3) / 4; // Pointer 'word width'
478478
var html = 'Total HEAP size: ' + self.formatBytes(HEAP8.length) + '.';
479-
html += '<br />' + colorBar('#202020') + 'STATIC memory area size: ' + self.formatBytes(Math.min(STACK_BASE, STACK_MAX) - STATIC_BASE);
480-
html += '. STATIC_BASE: ' + toHex(STATIC_BASE, width);
479+
html += '<br />' + colorBar('#202020') + 'STATIC memory area size: ' + self.formatBytes(Math.min(STACK_BASE, STACK_MAX) - {{{ GLOBAL_BASE }}});
480+
html += '. {{{ GLOBAL_BASE }}}: ' + toHex({{{ GLOBAL_BASE }}}, width);
481481

482482
html += '<br />' + colorBar('#FF8080') + 'STACK memory area size: ' + self.formatBytes(Math.abs(STACK_MAX - STACK_BASE));
483483
html += '. STACK_BASE: ' + toHex(STACK_BASE, width);

src/modules.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ var PassManager = {
544544
print('\n//FORWARDED_DATA:' + JSON.stringify({
545545
Functions: Functions,
546546
EXPORTED_FUNCTIONS: EXPORTED_FUNCTIONS,
547-
STATIC_BUMP: STATIC_BUMP, // updated with info from JS
548547
ATINITS: ATINITS.join('\n'),
549548
ATMAINS: ATMAINS.join('\n'),
550549
ATEXITS: ATEXITS.join('\n'),

src/preamble.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ function updateGlobalBufferAndViews(buf) {
329329
Module['HEAPF64'] = HEAPF64 = new Float64Array(buf);
330330
}
331331

332-
var STATIC_BASE = {{{ GLOBAL_BASE }}},
333-
STACK_BASE = {{{ getQuoted('STACK_BASE') }}},
332+
var STACK_BASE = {{{ getQuoted('STACK_BASE') }}},
334333
STACKTOP = STACK_BASE,
335334
STACK_MAX = {{{ getQuoted('STACK_MAX') }}},
336335
DYNAMIC_BASE = {{{ getQuoted('DYNAMIC_BASE') }}};

src/preamble_minimal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ Module['wasm'] = base64Decode('{{{ getQuoted("WASM_BINARY_DATA") }}}');
5656
#include "runtime_strings.js"
5757

5858
#if USE_PTHREADS
59-
var STATIC_BASE = {{{ GLOBAL_BASE }}};
60-
6159
if (!ENVIRONMENT_IS_PTHREAD) {
6260
#endif
6361

6462
var GLOBAL_BASE = {{{ GLOBAL_BASE }}},
6563
TOTAL_STACK = {{{ TOTAL_STACK }}},
66-
#if !USE_PTHREADS
67-
STATIC_BASE = {{{ GLOBAL_BASE }}},
68-
#endif
6964
STACK_BASE = {{{ getQuoted('STACK_BASE') }}},
7065
STACKTOP = STACK_BASE,
7166
STACK_MAX = {{{ getQuoted('STACK_MAX') }}}

src/settings_internal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ var MEM_INIT_IN_WASM = 0;
129129
// This is set internally when needed (SINGLE_FILE)
130130
var SUPPORT_BASE64_EMBEDDING = 0;
131131

132-
// the total static allocation, that is, how much to bump the start of memory
133-
// for static globals. received from the backend, and possibly increased due
134-
// to JS static allocations
135-
var STATIC_BUMP = -1;
136-
137132
// the total initial wasm table size.
138133
var WASM_TABLE_SIZE = 0;
139134

src/shell_minimal.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ var ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_PTHREAD = typeof importScripts === 'f
144144
#if MODULARIZE
145145
if (ENVIRONMENT_IS_WORKER) {
146146
var buffer = {{{EXPORT_NAME}}}.buffer;
147-
var STATICTOP = {{{EXPORT_NAME}}}.STATICTOP;
148147
var STACK_BASE = {{{EXPORT_NAME}}}.STACK_BASE;
149148
var STACKTOP = {{{EXPORT_NAME}}}.STACKTOP;
150149
var STACK_MAX = {{{EXPORT_NAME}}}.STACK_MAX;

tests/test_core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7676,8 +7676,7 @@ def test_memprof_requirements(self):
76767676
create_test_file('lib.js', '''
76777677
mergeInto(LibraryManager.library, {
76787678
check_memprof_requirements: function() {
7679-
if (typeof STATIC_BASE === 'number' &&
7680-
typeof STACK_BASE === 'number' &&
7679+
if (typeof STACK_BASE === 'number' &&
76817680
typeof STACK_MAX === 'number' &&
76827681
typeof STACKTOP === 'number' &&
76837682
typeof DYNAMIC_BASE === 'number') {

tools/shared.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,6 @@ class JS(object):
10731073
memory_initializer_pattern = r'/\* memory initializer \*/ allocate\(\[([\d, ]*)\], "i8", ALLOC_NONE, ([\d+\.GLOBAL_BASEHgb]+)\);'
10741074
no_memory_initializer_pattern = r'/\* no memory initializer \*/'
10751075

1076-
memory_staticbump_pattern = r'STATICTOP = STATIC_BASE \+ (\d+);'
1077-
10781076
global_initializers_pattern = r'/\* global initializers \*/ __ATINIT__.push\((.+)\);'
10791077

10801078
emscripten_license = '''\

0 commit comments

Comments
 (0)