Skip to content

Port to big-endian platforms (s390x but others can be trivially added) - #1557

Merged
binji merged 15 commits into
WebAssembly:masterfrom
SoniEx2:feature/s390x
Dec 7, 2020
Merged

binji merged 15 commits into
WebAssembly:masterfrom
SoniEx2:feature/s390x

Conversation

@SoniEx2

@SoniEx2 SoniEx2 commented Oct 9, 2020 •

Copy link
Copy Markdown
Collaborator

closes #1063

61 59 40 22 13 10 7 1 failing tests https://gist.github.com/SoniEx2/4379beb5909d0d39d52b48a53b7247b7

This ports wabt to big-endian platforms, and also ports the output of wasm2c to big-endian platforms (for cross-compilation needs). It's not specific to s390x other than the makefile changes.

Additionally the following unittests and c-api-tests fail, as they interact with wasm memory but haven't been adjusted to take memory ordering into account (and we think that should be a separate pull request and/or blocked on further specs like e.g. WebAssembly/wasm-c-api#156 ):

unittests:
/home/linux1/wabt/src/test-interp.cc:545: Failure
Expected equality of these values:
  "Uryyb, JroNffrzoyl!"
  string_data
    Which is: "Hello, WebAssembly!"
[  FAILED  ] InterpTest.Rot13 (0 ms)


c-api-tests:
Running.. /home/linux1/wabt/out/wasm-c-api-memory
Failed with returncode=1
b'CAPI: [wasm_byte_vec_new_uninitialized] 92\nCAPI: [wasm_byte_vec_copy] 92\nCAPI: [wasm_byte_vec_new_uninitialized] 92\nCAPI: [wasm_byte_vec_delete] \nCAPI: [wasm_instance_new] 0x1e5beee0 0x1e5c0650\nCAPI: [wasm_extern_vec_new_uninitialized] 4\nCAPI: [wasm_instance_exports] 4\nCAPI: [wasm_module_delete] \nCAPI: [wasm_byte_vec_delete] \nCAPI: [wasm_memory_copy] \nCAPI: [wasm_memory_same] \nCAPI: [wasm_memory_delete] \nInitializing...\nLoading binary...\nCompiling module...\nInstantiating module...\nExtracting exports...\nChecking memory...\n> Error, expected success\n'
FAIL(1): /home/linux1/wabt/out/wasm-c-api-memory

Other than these, everything else passes.

@SoniEx2

This comment has been minimized.

Comment thread src/interp/interp.cc Resolved
@SoniEx2

SoniEx2 commented Oct 15, 2020

Copy link
Copy Markdown
Collaborator Author

we're gonna just ignore that one failing test and pretend it doesn't exist

@SoniEx2 SoniEx2 changed the title [DO NOT MERGE!][WIP] Feature/s390x Feature/s390x Oct 15, 2020
@SoniEx2
SoniEx2 marked this pull request as ready for review October 15, 2020 00:18
@SoniEx2 SoniEx2 changed the title Feature/s390x Port to big-endian platforms (s390x but others can be trivially added) Oct 15, 2020

@binji binji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this! In general, looks pretty good. I'm mostly concerned about the code duplication, and that it is a bit hard to tell at first that big-endian stores all the data backward. It might be nice to refactor this into a set of common functions for operating on memory, and add some documentation making this clearer.

Comment thread src/common.h Outdated
static_assert(sizeof(T) <= sizeof(v), "Invalid cast!");
assert((lane + 1) * sizeof(T) <= sizeof(v));
T result;
#if WABT_BIG_ENDIAN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Refactor this into shared code?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Shared with...?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems that this pattern is common in many of the changes below (using the negative offset for WABT_BIG_ENDIAN on memcpy). Can you make a function that does this, MemcpyEndianAware or some other better name?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So like a MemcpyEndianAwareAt that takes "source object", "destination object", "source size", "destination size", "source offset", "destination offset", and "copy size"?

(this is a lot of arguments... should it be inline? where should we put it?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

common.h seems like the best choice (maybe near Bitcast, etc). It is a lot of arguments, but I think that will be OK.

Comment thread src/interp/interp.cc Outdated
src.desc().data.begin() + src_offset + size,
data_.end() - dst_offset - size);
auto base = data_.end() - dst_offset - size;
for (size_t i = 0; i < (size>>1); i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Refactor into SwapBytesInRange (or better name?) function

Comment thread src/wast-parser.cc Outdated
return Result::Error;
}

#if WABT_BIG_ENDIAN

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

(side note: arguably this should just write the v128 directly, instead of doing this and then bitcasting. but it was done like this and there's probably a reason it was done like this but we personally don't know that reason and we don't think this makes sense. .-.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you switch this over to writing to the v128 directly, do any tests fail? I agree that this looks a little weird as it is.

Comment thread src/common.h Outdated

// Footgun: len may evaluate many times, and sizes may not be evaluated at all.
#if WABT_BIG_ENDIAN
#define WABT_MEMCPY_ENDIAN_AWARE(dst, src, dsize, ssize, doff, soff, len) \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this need to be a macro, or can it be an inline function instead?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it could be an inline function? but namespaces? this needs to be used in v128 but v128 is not in the namespace.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

(sorry, we don't really know C++. altho this does avoid evaluating data_.size() at all on little-endian? but compiler optimizations would probably optimize that out anyway...)

Comment thread src/common.h Outdated
inline void SwapBytesSized(void *addr, size_t size) {
auto bytes = static_cast<uint8_t*>(addr);
for (size_t i = 0; i < (size >> 1); i++) {
uint8_t tmp = bytes[i];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

use std::swap() instead

Comment thread src/common.h Outdated

inline void SwapBytesSized(void *addr, size_t size) {
auto bytes = static_cast<uint8_t*>(addr);
for (size_t i = 0; i < (size >> 1); i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: use size / 2 instead for clarity

@SoniEx2

SoniEx2 commented Nov 8, 2020

Copy link
Copy Markdown
Collaborator Author

Now this is pretty much just blocked on figuring out the C API stuff, either adjusting the C API tests to take memory order into account or making the C API copy stuff in the correct order into test-owned buffers and stuff. But at least the interpreter itself and wasm2c work.

@binji binji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few small comments, but otherwise lgtm. Sorry for the long review, and thanks for sticking with it!

Comment thread wasm2c/wasm-rt-impl.c Outdated
if (new_data == NULL) {
return (uint32_t)-1;
}
#if WABT_BIG_ENDIAN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#if !WABT_BIG_ENDIAN instead of using else block?

Comment thread src/wast-parser.cc Outdated
return Result::Error;
}

#if WABT_BIG_ENDIAN

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If you switch this over to writing to the v128 directly, do any tests fail? I agree that this looks a little weird as it is.

@SoniEx2

SoniEx2 commented Nov 25, 2020 •

Copy link
Copy Markdown
Collaborator Author

okay so we figured out what about the opcodecnt test is failing.

we're not fixing that. 😄 edit: we "fixed" it. :p

tl;dr: it has to do with this: https://github.com/WebAssembly/wabt/blob/master/src/binary-reader-opcnt.cc#L140-L181

>>> struct.pack("<f", 1.0)
b'\x00\x00\x80?'
>>> struct.pack("<f", 2.0)
b'\x00\x00\x00@'
>>> struct.pack(">f", 1.0)
b'?\x80\x00\x00'
>>> struct.pack(">f", 2.0)
b'@\x00\x00\x00'

@binji

binji commented Dec 7, 2020

Copy link
Copy Markdown
Member

okay so we figured out what about the opcodecnt test is failing.
we're not fixing that. smile edit: we "fixed" it. :p

Oh, nasty :) Thanks for the fix.

@binji
binji merged commit 3c4bad0 into WebAssembly:master Dec 7, 2020
" static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \\\n"
" MEMCHECK(mem, addr, t1); \\\n"
" t1 result; \\\n"
" __builtin_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], sizeof(t1)); \\\n"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@SoniEx2 @binji Maybe I'm missing something here, but is there any particular reason why the memory is addressed like this, instead of addressing it with &mem->data[addr] like in the little-endian?

Also, related, is there any way to make the load_* functions accessible?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This avoids a byteswap. Whether it has any effects on performance on s390x specifically is questionable, but it can make a difference on other big-endian platforms.

And, the load_* functions?

@SoniEx2
SoniEx2 deleted the feature/s390x branch March 5, 2023 20:13
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.

247 test failures on big-endian arches (ppc64 and s390x)

3 participants