Skip to content

Fix failure of AVX2-accelerated mb_check_encoding on 32-bit MS Windows #10776

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

Closed
wants to merge 1 commit into from

Conversation

alexdowad
Copy link
Contributor

Not sure if this will do the trick, just opening a PR to see what happens in CI run...

* (Bad 7/8-byte UTF-8 strings would be wrongly passed through as 'valid')
* It is not clear whether the root cause is a problem with MS Visual C++'s handling
* of AVX2 intrinsics when building 32-bit binaries, or something else */
operand = _mm256_set_epi32(0, 0, 0, 0, 0, 0, ((int32_t*)p)[1], ((int32_t*)p)[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

same instruction is used in

{ __m256i const seed = _mm256_set_epi64x((xxh_i64)(0U - seed64), (xxh_i64)seed64, (xxh_i64)(0U - seed64), (xxh_i64)seed64);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed.

I don't know if the AVX2-accelerated code you are showing from xxhash is used on 32-bit Windows hosts or not. If it is, that indicates that using _mm256_set_epi64x is not necessarily a problem in of itself. Whatever the case, a root-cause analysis cannot be done without access to a test machine, and I don't have one. However, the change in this PR seems to have fixed a problem.

If anyone else has more insight into the issue at hand, I would love to hear their comments.

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 use an #if and keep using _mm256_set_epi64x for everything but Windows x86 32-bit?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@mvorisek Good find, that explains it 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mvorisek Great find!!

As a suggestion, I think the first thing would be to add a test case which exposes the problem on 32-bit Windows. Then it will be easy to verify the fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we use an #if and keep using _mm256_set_epi64x for everything but Windows x86 32-bit?

I don't think so.

If this particular line was performance-critical, then I would say yes, but I think the performance loss here will be minute. (If the compiler is smart enough to generate optimal code on x86-64, we won't lose anything.) The effect will only be on 7 and 8 byte UTF-8 strings, and for those, I expect the overhead of entering/exiting a function from PHP code will dominate this tiny bit of overhead.

Having a lot of #ifs in C code is a bad thing. I'm sure you know all the reasons why that is so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iluuu1994 Please note, I would be happy to be proved wrong on the above statement.

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't consider an #if an issue, but I'm fine either way 🙂

@alexdowad
Copy link
Contributor Author

Hmm, well, the Windows x86 CI build has passed!

@iluuu1994 Have a look!

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

@alexdowad Thanks a lot for having a look! that's very odd, but I don't know much about the peculiarities of Windows and don't own a Windows machine. The reason this wasn't previously caught by CI is that the new Windows job on GitHub actions tests intrinsics on 32-bit instead of 64-bit.

* (Bad 7/8-byte UTF-8 strings would be wrongly passed through as 'valid')
* It is not clear whether the root cause is a problem with MS Visual C++'s handling
* of AVX2 intrinsics when building 32-bit binaries, or something else */
operand = _mm256_set_epi32(0, 0, 0, 0, 0, 0, ((int32_t*)p)[1], ((int32_t*)p)[0]);
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 use an #if and keep using _mm256_set_epi64x for everything but Windows x86 32-bit?

@alexdowad
Copy link
Contributor Author

I think I should edit that StackOverflow link into this PR. It will be useful for others who work on this code down the road.

Thanks to Ilija Tovilo for noticing and reporting this problem. Thanks
also to Michael Voříšek for finding the StackOverflow post which
explained the reason for the failure.
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

Thank you!

@alexdowad alexdowad closed this Mar 4, 2023
@alexdowad alexdowad deleted the fixwin32 branch March 4, 2023 18:43
@alexdowad
Copy link
Contributor Author

Landed on master.

nielsdos added a commit to nielsdos/php-src that referenced this pull request Nov 19, 2023
Similar bug as before in php#10776, but now in other code.
nielsdos added a commit that referenced this pull request Nov 19, 2023
Similar bug as before in #10776, but now in other code.

Closes GH-12726.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants