Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIMD extract functions return unexpected types #11419

Closed
nemequ opened this issue Jun 15, 2020 · 4 comments
Closed

SIMD extract functions return unexpected types #11419

nemequ opened this issue Jun 15, 2020 · 4 comments
Labels

Comments

@nemequ
Copy link

nemequ commented Jun 15, 2020

This might be intentional, if so sorry for the noise. I really hope not, though.

Most of the integer wasm_*_extract_lane function-like macros return int or, in the case of i64, long long. This causes a bit of awkwardness when attempting to use them with -Wimplicit-int-conversion active. For example,

int8_t e = wasm_i8x16_extract_lane(v, 0);

Triggers a diagnostic.

It's straightforward enough to work around with a cast, but I feel like the cast should go in the extract_lane macros in wasm_simd128.h (or maybe even the return type of the builtins it uses), not on the caller side.

CC @tlively since it's a SIMD thing.

@tlively
Copy link
Member

tlively commented Jun 15, 2020

This is intentional, but is also open for discussion. The idea when first implementing the intrinsics was to match the types of the underlying WebAssembly instructions as closely as possible. WebAssembly doesn't have i8 or i16 types, so the instructions return i32 and the intrinsics reflect that.

@nemequ
Copy link
Author

nemequ commented Jun 15, 2020

That makes sense to me in theory, but is there any practical benefit? I'm having trouble thinking of examples where it solves a problem.

It may be consistent with the WASM types, but it's not consistent with C-level APIs. For example, wasm_i8x16_make takes a bunch of int8_t parameters, not ints. I know the load and store functions take void* so they can be anything, but if you want to load a vector of 16 8-bit values they're probably going to be represented in memory as an array of 16 × int8_t (or maybe char, signed char, etc.). They're definitely not going to be an array of 16 × int.

And, of course, you're much less likely to get a warning from the compiler about implicitly converting to a wider type than to a narrower type. I know clang (at least) has a double-promotion warning which is triggered by assigning a float to a double, but I don't think there is anything similar for assigning, for example, an int8_t to an int. So if wasm_i8x16_extract_lane returned an int8_t you could still assign the result to an int if you wanted to.

I guess it just feels like with the current API I'm fighting against the type system, when it should be helping me find bugs.

// Possible warning (depending on flags)
int8_t e = wasm_i8x16_extract_lane(v, 0);
// Fix that with a cast.
int8_t e = (int8_t) wasm_i8x16_extract_lane(v, 0);
// That's ugly, but at least it works.  Unfortunately, it is too broad
// and creates a dangerous pattern:
int8_t e = (int8_t) wasm_i16x8_extract_lane(v, 0);
// Even without the cast, this generates (or doesn't) the same
// diagnostic as the first one
int8_t e1 = wasm_i16x8_extract_lane(v, 0);

These are exactly the sort of issues the type system can help with. I don't see a reason not to let it.

@tlively
Copy link
Member

tlively commented Jun 16, 2020

Yeah, definitely makes sense to me. I'll file an issue on the SIMD repo to see if anyone else has thoughts about this, but I'd be in favor of making this change.

@stale
Copy link

stale bot commented Jun 16, 2021

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Jun 16, 2021
@stale stale bot closed this as completed Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants