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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -5155,7 +5155,12 @@ static bool mb_fast_check_utf8_avx2(zend_string *str)
goto check_operand;
case 7:
case 8:
operand = _mm256_set_epi64x(0, 0, 0, *((int64_t*)p));
/* This was originally: operand = _mm256_set_epi64x(0, 0, 0, *((int64_t*)p));
* However, that caused test failures on 32-bit MS Windows
* (Bad 7/8-byte UTF-8 strings would be wrongly passed through as 'valid')
* It seems this is caused by a bug in MS Visual C++
* Ref: https://stackoverflow.com/questions/37509129/potential-bug-in-visual-studio-c-compiler-or-in-intel-intrinsics-avx2-mm256-s */
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 🙂

goto check_operand;
case 9:
operand = _mm256_set_m128i(_mm_setzero_si128(), _mm_srli_si128(_mm_loadu_si128((__m128i*)(p - 6)), 6));
Expand Down
1 change: 0 additions & 1 deletion ext/mbstring/tests/utf_encodings.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mbstring
--SKIPIF--
<?php
if (getenv("SKIP_SLOW_TESTS")) die("skip slow test");
if (substr(PHP_OS, 0, 3) === 'WIN' && PHP_INT_SIZE === 4) die("skip not for Windows x86");
?>
--FILE--
<?php
Expand Down