Skip to content

Add shared memories#1069

Merged
dschuff merged 11 commits intomasterfrom
shared_mem
Jun 27, 2017
Merged

Add shared memories#1069
dschuff merged 11 commits intomasterfrom
shared_mem

Conversation

@dschuff
Copy link
Member

@dschuff dschuff commented Jun 27, 2017

For now, just IR, wast, and binary support.

uint32_t flags = hasMaximum ? 1 : 0;
void WasmBinaryWriter::writeResizableLimits(Address initial, Address maximum,
bool hasMaximum, bool shared) {
uint32_t flags = (uint32_t) hasMaximum | (uint32_t) shared << 1;
Copy link
Member Author

Choose a reason for hiding this comment

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

@binji This uses the 2nd bit as the "shared" bit, i.e
0x03 is the flags value for shared. https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md#spec-changes says it's 0x11 but I'm wondering if you meant 0b11?

Copy link
Member

Choose a reason for hiding this comment

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

👍

@kripken
Copy link
Member

kripken commented Jun 27, 2017

What are shared memories? Is this something new in wasm? Where are they supported?

@dschuff
Copy link
Member Author

dschuff commented Jun 27, 2017

@kripken as specified in the threading proposal https://github.com/WebAssembly/threads

@dschuff
Copy link
Member Author

dschuff commented Jun 27, 2017

// gets a name in the combined function import+defined function space
Name getFunctionIndexName(Index i);
void getResizableLimits(Address& initial, Address& max, Address defaultIfNoMax);
void getResizableLimits(Address& initial, Address& max, bool& shared, Address defaultIfNoMax);
Copy link
Member

Choose a reason for hiding this comment

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

why not put it at the end? seems more natural there at first glance, maybe there's something I'm missing?

Copy link
Member Author

Choose a reason for hiding this comment

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

The first 3 parameters are out-parameters, and the last one is an in-parameter, so I wanted to keep them together.

Copy link
Member

Choose a reason for hiding this comment

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

I see. lgtm

auto flags = getU32LEB();
initial = getU32LEB();
bool hasMax = flags & 0x1;
bool is_shared = flags & 0x2;
Copy link
Member

Choose a reason for hiding this comment

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

isShared

@kripken
Copy link
Member

kripken commented Jun 27, 2017

Thanks, now I understand the context here.

f.close()
cmd = ['mozjs', 'a.js']
out = run_command(cmd, stderr=subprocess.STDOUT)
open(os.path.join('test', 'binaryen.js', s + '.txt'), 'w').write(out)
Copy link
Member

Choose a reason for hiding this comment

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

Should we not be consistently using "with ... open as:" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we probably should, but it's not common enough in this codebase. I'll fix this instance though since I'm touching it anyway.

(module
(memory $0 23 256 shared)
)

Copy link
Member

Choose a reason for hiding this comment

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

Why do some of these have a trailing newline?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's because wasm-dis prints an extra newline, whereas wasm-opt --print does not. Should maybe fix that in another PR.

f.write(open(os.path.join('bin', 'binaryen.js')).read())
f.write(open(os.path.join('test', 'binaryen.js', s)).read())
f.close()
cmd = ['mozjs', 'a.js']
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably be cmd = [MOZJS, 'a.js']?

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, but If I make any more changes to this code I'm going to have to go build mozjs just so I can test it =-O

uint32_t flags = hasMaximum ? 1 : 0;
void WasmBinaryWriter::writeResizableLimits(Address initial, Address maximum,
bool hasMaximum, bool shared) {
uint32_t flags = (uint32_t) hasMaximum | (uint32_t) shared << 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of two bools in the signature, would it make sense to have a MemoryFlags enum?

enum MemoryFlags {
  memoryHasMaximum = 1 << 0,
  memoryIsShared = 1 << 1
};

As it is now we're basically inlining the knowledge of the flag layout, which is probably less obvious in the future.
Failing that a link in a comment to the threads proposal might be good, because otherwise that's implicit knowledge.

Copy link
Member Author

Choose a reason for hiding this comment

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

I dunno, the point of having the bools in the signature is to abstract away the encoding details from the caller (as with initial and maximum). But having bits explicit is good; I moved that to BinaryConsts

case ExternalKind::Table: {
o << S32LEB(BinaryConsts::EncodedType::AnyFunc);
writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize);
writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of calling naked falses, would defaulting the param to false make sense?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm I actually like the pattern of
writeResizeableLimits(..., .., ..., /*shared=*/false) here.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is also pretty reasonable

cmd = ['mozjs', 'a.js']
out = run_command(cmd, stderr=subprocess.STDOUT)
open(os.path.join('test', 'binaryen.js', s + '.txt'), 'w').write(out)
with open(os.path.join('test', 'binaryen.js', s + '.txt'), 'w') as o: o.write(out)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: o.write(out) on its own line

Copy link
Member Author

Choose a reason for hiding this comment

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

Every other instance of with in this file uses this pattern. I think it may have been a compromise between `you should use with instead of just open().write!' and 'but verbosity!'

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah consistency > perfection. Seems fine

@dschuff dschuff merged commit 21e08ee into master Jun 27, 2017
@dschuff dschuff deleted the shared_mem branch June 27, 2017 21:24
@dschuff dschuff mentioned this pull request Jul 10, 2017
10 tasks
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.

5 participants