Skip to content

Add internal CAN_ADDRESS_2GB flag which is set for 2GB+ heap support - #10601

Merged
kripken merged 28 commits into
masterfrom
unsign
Mar 26, 2020
Merged

kripken merged 28 commits into
masterfrom
unsign

Conversation

@kripken

@kripken kripken commented Feb 28, 2020 •

Copy link
Copy Markdown
Member

To support that mode add a JS optimizer pass that fixes up
pointers in JS libraries. Doing this as a pass is nice because
our JS libraries can still be written with signed pointers,
which is smaller and may be faster on some VMs (signed
values fit in 31-bit smis). This will fix up user code as well,
automatically.

The pass looks for HEAP8 etc. symbols on which we do
HEAP8[something] and ensures the pointer is unsigned. It
also fixes up .set() and other calls that receive pointers.

One compromise the pass makes is that it looks not just
for the canonical HEAP8 etc. names but also HEAP and
heap. Those are used in our JS library code a bunch, so
fixing those up too prevents a lot more work in that code, and
can help user code too. There is some risk to doing so, if
a user has code with that name but actually doesn't want
the unsigning; however, the risk is small (we just force their
indexes on an array to be non-negative; likely they don't
want that anyhow!), and I think the reverse risk (a user has
code that should be changed, but forgets) is much more
likely, so this seems safest. A later PR will add our JS library
fixes, which will show that even with this extension there is
still code that must be handled manually, so I think it's best
to do as much automatically as we can.

This also changes MAXIMUM_MEMORY to be 2GB. That
is effectively no change from the previous -1 (no limit)
since 2GB was the absolute limit anyhow. This has two benefits:
first, people must opt-in explicitly to getting 2GB+ heap support
with the code size downside, and also we expect only some
browsers will support 2GB+ heaps for now, so requiring explicit
opt-in from devs is safer to avoid web compat issues (i.e., only
users that really need such heaps will set them, and then they
may only work in some browsers, but at least that won't happen
for users that could actually run in all browsers).

Note that emmalloc is not designed to work with 2GB+ heaps,
so show a useful error in that case. This is a minor breaking
change for users in that building with emmalloc + memory growth
will hit the error, forcing the user to set a maximum to the growth.

This is part of a set of 3+ PRs for #6566, together with #10600

@kripken
kripken requested a review from juj March 5, 2020 17:00
Comment thread src/settings_internal.js Outdated
// sections.
// This has no effect if DWARF is not being emitted.
var SEPARATE_DWARF = 0;
var SEPARATE_DWARF = 0; No newline at end of file

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sam will get you on this :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, isn't this what @sbc100 prefers - no extra newline at the end?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#10478 (comment) was a recent conversation around that

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.. now I'm confused... but I'll remove this change 😄

@juj

juj commented Mar 5, 2020

Copy link
Copy Markdown
Collaborator

Hmm, I am a bit torn on this, like I was on the other PR. It feels to me that this is fixing in the wrong place, but rather we should be fixing the inputs we get from wasm so that all the numbers have the right value in JS throughout.

If we wanted to do an acorn pass, I'd rather have a pass that turns HEAP8[x] into HEAP8[requireUnsignedPointer(x)] in CAN_ADDRESS_2GB && (ASSERTIONS || SAFE_HEAP) builds, where

function requireUnsignedPointer(x) {
   if (x < 0) abort('x should be an unsigned pointer!');
   return x;
}

that would prompt the actual code to be fixed so that values received from wasm are treated appropriately. That would avoid redundant shifts, e.g. in

function _emscripten_webgl_do_create_context(target, attributes) {
 var contextAttributes = {};
 var a = attributes >> 2;
 contextAttributes["alpha"] = !!HEAP32[a];
 contextAttributes["depth"] = !!HEAP32[a + 1];
 contextAttributes["stencil"] = !!HEAP32[a + 2];
 contextAttributes["antialias"] = !!HEAP32[a + 3];
 contextAttributes["premultipliedAlpha"] = !!HEAP32[a + 4];
 contextAttributes["preserveDrawingBuffer"] = !!HEAP32[a + 5];
 var powerPreference = HEAP32[a + 6];
 contextAttributes["powerPreference"] = __emscripten_webgl_power_preferences[powerPreference];
 contextAttributes["failIfMajorPerformanceCaveat"] = !!HEAP32[a + 7];
 contextAttributes.majorVersion = HEAP32[a + 8];
 contextAttributes.minorVersion = HEAP32[a + 9];
 contextAttributes.enableExtensionsByDefault = HEAP32[a + 10];
 contextAttributes.explicitSwapControl = HEAP32[a + 11];
 contextAttributes.proxyContextToMainThread = HEAP32[a + 12];
 contextAttributes.renderViaOffscreenBackBuffer = HEAP32[a + 13];

one could have attributes >>>= 2; there and that is enough, but this pass will add >>> 0 to all those heap accesses.

I think it is ok to land this, but it does feel like a temporary solution.

Sidenote: I can look at making emmalloc support 4GB, that will not be a big change.

Btw, which shells/browsers currently support 4GB addressing?

@kripken

kripken commented Mar 5, 2020

Copy link
Copy Markdown
Member Author

Hmm, I am a bit torn on this, like I was on the other PR. It feels to me that this is fixing in the wrong place, but rather we should be fixing the inputs we get from wasm so that all the numbers have the right value in JS throughout.

The only thing we can maybe avoid is if there wasn't a shift at all earlier - good point there. But otherwise, we tend to have >>, and we must change those to >>>, that isn't optional. I think that's the more common case, but I didn't measure.

If we wanted to do an acorn pass, I'd rather have a pass that turns HEAP8[x] into HEAP8[requireUnsignedPointer(x)] in CAN_ADDRESS_2GB && (ASSERTIONS || SAFE_HEAP) build

Ah, good idea! Yeah, I can look into extending SAFE_HEAP to check this (separate from this PR).

Overall, yeah, I see your point about avoiding extra casts later, as in your example. I suggest we leave that to further optimization, together with the SAFE_HEAP stuff - I'd like to land this first (and the dependent PR) to get any 2GB+ support working at all.

Sidenote: I can look at making emmalloc support 4GB, that will not be a big change.

That might be nice, but if it increases emmalloc's size, it might not be worth it - I imagine the apps that want 2GB+ heaps are rare, and tend to be large, where emmalloc isn't a big help anyhow percentage wise.

Btw, which shells/browsers currently support 4GB addressing?

V8 just landed this support about a week ago in trunk. It appears to work great! I know others are working on it, but there is no support outside of V8 AFAIK.

@kripken

kripken commented Mar 6, 2020

Copy link
Copy Markdown
Member Author

Thinking some more on this, I had an idea: let's change MAXIMUM_MEMORY (the maximum we grow to, if growth is enabled) to 2GB. This is not really noticeable by users since over 2GB was never supported anyhow. It means that users must opt-in explicitly to getting a heap of over 2GB in size. That has 2 big benefits:

  • Building with memory growth with no additional flags (i.e. not specifying a max mem size) will not require unsigned pointers. That means no code size regression for people just using memory growth in the simple way: we only change >> to >>> when users opt-in to a powerful new feature.
  • It helps address a concern @lars-t-hansen has, that if only one browser has >2GB support, then there is a risk of people creating apps that only work in that browser. If people must explicitly opt-in to such heaps, that will be much more rare, and happen just when they need it.

On the other hand, I did some more thinking about the SAFE_HEAP idea you had @juj and I'm a little worried it might not be easy to do. SAFE_HEAP does stuff like this:

HEAP32[x + y >> 2]
  =>
SAFE_HEAP(x + y | 0, ..)

Note how the shift goes away, replaced by | 0. We have to remove the shift because we want to check alignment, so we want the raw pointer, and we have to | 0 because the shift was doing the operation of making sure it's a 32-bit value - it's fine if it overflows or was a float, etc. But if we change that to >>> 0 we would have nothing to check...

In other words, >2GB heaps is the first time we care about the sign of pointers, and SAFE_HEAP isn't designed for that. We may need some other tool there, let's keep thinking about that separately from this PR. (Btw, another idea is to allow manual JS library code which shifts once and not later, and we can disable automatic >>> 0 in that function with an annotation - might be worth it, not sure.)

But in any case, given the first part of this comment, I'm not that worried about code size myself: if we only add a little size to builds the user explicitly asked for >2GB heaps, that's likely ok as those builds are probably big applications anyhow.

Comment thread tests/test_other.py
shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), 'screenshot.jpg')
Building.emcc(path_from_root('tests', 'jpeg_test.c'), ['--embed-file', 'screenshot.jpg', '-s', 'USE_LIBJPEG=1'], output_filename='a.out.js')
self.assertContained('Image is 600 by 450 with 3 components', run_process(JS_ENGINES[0] + ['a.out.js', 'screenshot.jpg'], stdout=PIPE, stderr=PIPE).stdout)
self.assertContained('Image is 600 by 450 with 3 components', run_js('a.out.js', args=['screenshot.jpg'], stdout=PIPE, stderr=PIPE))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a tiny cleanup/simplification I noticed while working on this.

@kripken
kripken merged commit 57cb2a4 into master Mar 26, 2020
@kripken
kripken deleted the unsign branch March 26, 2020 00:40
kripken added a commit that referenced this pull request Mar 26, 2020
Almost all of these are behind the new flag, but a handful would
require annoying refactoring, so I just use >>> for those.

This is part of a set of 3+ PRs for #6566, together with #10600, #10601
sbc100 added a commit that referenced this pull request Aug 5, 2021
It was previously possible for MAXIMUM_MEMORY to be set to -1. In
fact it was the default.  However since #10601, it is no longer the
default and there is no way to set it to -1.

Also, error out if MAXIMUM_MEMORY is specified without
ALLOW_MEMORY_GROWTH.
sbc100 added a commit that referenced this pull request Aug 5, 2021
It was previously possible for MAXIMUM_MEMORY to be set to -1. In
fact it was the default.  However since #10601, it is no longer the
default and there is no way to set it to -1.

Also, error out if MAXIMUM_MEMORY is specified without
ALLOW_MEMORY_GROWTH.
sbc100 added a commit that referenced this pull request Aug 5, 2021
It was previously possible for MAXIMUM_MEMORY to be set to -1. In
fact it was the default.  However since #10601, it is no longer the
default and there is no way to set it to -1.
sbc100 added a commit to sbc100/emscripten that referenced this pull request Jan 15, 2026
This test was originally used to test emmalloc for its initial
limitations when it was added back in emscripten-core#10601.

However, not it seems to basically do nothing.

We have test_emmalloc in test core which runs in 2gb and 4gb modes and
actually test allocations in the high region.  This test just runs hello
world and doesn't actually test any allocations.
sbc100 added a commit that referenced this pull request Jan 15, 2026
This test was originally used to test emmalloc for its initial
limitations when it was added back in #10601.

However, not it seems to basically do nothing.

We have test_emmalloc in test core which runs in 2gb and 4gb modes and
actually test allocations in the high region. This test just runs hello
world and doesn't actually test any allocations.
inolen pushed a commit to inolen/emscripten that referenced this pull request Feb 13, 2026
This test was originally used to test emmalloc for its initial
limitations when it was added back in emscripten-core#10601.

However, not it seems to basically do nothing.

We have test_emmalloc in test core which runs in 2gb and 4gb modes and
actually test allocations in the high region. This test just runs hello
world and doesn't actually test any allocations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants