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

[NEON] Shift left requires constant integer #95

Closed
lawben opened this issue Jun 12, 2024 · 5 comments · Fixed by #97
Closed

[NEON] Shift left requires constant integer #95

lawben opened this issue Jun 12, 2024 · 5 comments · Fixed by #97
Assignees
Labels
bug Something isn't working

Comments

@lawben
Copy link

lawben commented Jun 12, 2024

When downloading the current tar.gz from v0.1.9-rc5 (and probably ones before that but I didn't check), I cannot compile the NEON stuff on Mac with LLVM/Clang 18. I get the following error multiple times.

cmake-build-release/_deps/tsl_gen-src/generate_tsl_neon-asimd/include/generated/definitions/binary/binary_neon.hpp:1117:104: error: argument to '__builtin_neon_vshlq_n_v' must be a constant integer
 1117 |                 return __extension__ ({ uint8x16_t __ret; uint8x16_t __s0 = data; __ret = (uint8x16_t) __builtin_neon_vshlq_n_v((int8x16_t)__s0, shift, 48); __ret; });

which is the expanded macro for:

[[nodiscard]] 
TSL_FORCE_INLINE 
static typename Vec::register_type apply(
    const typename Vec::register_type data, const unsigned int shift
) {
    return vshlq_n_u8(data, shift);  // <-- this is a macro in Clang
}

When looking at the NEON specs, this error is correct, as vshlq_n_u8 requires a const int as the second argument. I'm not sure how this should be handled, but probably this needs a vdup_n_* for the runtime value before the shift.

@JPietrzykTUD
Copy link
Collaborator

The most straightforward way (yet, probably not very efficient) would be to fallback to vshlq_u8:

/*...*/
return vshlq_u8(data, vdupq_n_u8(shift));

Another approach would be to change the interface and pass the shift value as an integral constant.

@JPietrzykTUD JPietrzykTUD added bug Something isn't working labels Jun 12, 2024
@JPietrzykTUD JPietrzykTUD pinned this issue Jun 12, 2024
@lawben
Copy link
Author

lawben commented Jun 12, 2024

FYI: This also hold for vshrq_n_* (right shift). The common fix for this is to use a left shift with negative values.

@JPietrzykTUD JPietrzykTUD linked a pull request Jun 12, 2024 that will close this issue
@JPietrzykTUD
Copy link
Collaborator

This should be fixed with #97.

@alexKrauseTUD
Copy link
Collaborator

Thank you for pointing out the issue!
As for the common fix: AFAIK left-shift using negative values is undefined behavior and not standard conforming.

  • Stated at cppreference/Bitwise shift operators:
    "If the value of rhs is negative or is not less than the number of bits in lhs, the behavior is undefined."
  • Stated in the C standard
    "The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined."

I would thus not include the workaround and rather rely on the vdup for now until we come up with something better.

@lawben
Copy link
Author

lawben commented Jun 13, 2024

That may be true fur C++ as a language, but specifically in NEON, this is well-defined and documented:

If the signed integer value is negative, it results in a right shift.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants