Repository navigation
Port to big-endian platforms (s390x but others can be trivially added) - #1557
Conversation
This comment has been minimized.
This comment has been minimized.
|
we're gonna just ignore that one failing test and pretend it doesn't exist |
binji
left a comment
There was a problem hiding this comment.
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.
| static_assert(sizeof(T) <= sizeof(v), "Invalid cast!"); | ||
| assert((lane + 1) * sizeof(T) <= sizeof(v)); | ||
| T result; | ||
| #if WABT_BIG_ENDIAN |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
common.h seems like the best choice (maybe near Bitcast, etc). It is a lot of arguments, but I think that will be OK.
| 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++) { |
There was a problem hiding this comment.
Refactor into SwapBytesInRange (or better name?) function
| return Result::Error; | ||
| } | ||
|
|
||
| #if WABT_BIG_ENDIAN |
There was a problem hiding this comment.
(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. .-.)
There was a problem hiding this comment.
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.
|
|
||
| // 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) \ |
There was a problem hiding this comment.
Does this need to be a macro, or can it be an inline function instead?
There was a problem hiding this comment.
it could be an inline function? but namespaces? this needs to be used in v128 but v128 is not in the namespace.
There was a problem hiding this comment.
(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...)
| 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]; |
|
|
||
| inline void SwapBytesSized(void *addr, size_t size) { | ||
| auto bytes = static_cast<uint8_t*>(addr); | ||
| for (size_t i = 0; i < (size >> 1); i++) { |
There was a problem hiding this comment.
nit: use size / 2 instead for clarity
|
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
left a comment
There was a problem hiding this comment.
A few small comments, but otherwise lgtm. Sorry for the long review, and thanks for sticking with it!
| if (new_data == NULL) { | ||
| return (uint32_t)-1; | ||
| } | ||
| #if WABT_BIG_ENDIAN |
There was a problem hiding this comment.
#if !WABT_BIG_ENDIAN instead of using else block?
| return Result::Error; | ||
| } | ||
|
|
||
| #if WABT_BIG_ENDIAN |
There was a problem hiding this comment.
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.
|
okay so we figured out what about the opcodecnt test is failing.
tl;dr: it has to do with this: https://github.com/WebAssembly/wabt/blob/master/src/binary-reader-opcnt.cc#L140-L181 |
Oh, nasty :) Thanks for the fix. |
| " 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" |
There was a problem hiding this comment.
There was a problem hiding this comment.
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?
closes #1063
61594022131071 failing tests https://gist.github.com/SoniEx2/4379beb5909d0d39d52b48a53b7247b7This 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 ):
Other than these, everything else passes.