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

[BUG] clang++ optimizer crash (segmentation fault) with neon intrinsics #1303

Closed
SimonSuckut opened this issue Jul 1, 2020 · 12 comments
Closed
Assignees

Comments

@SimonSuckut
Copy link

SimonSuckut commented Jul 1, 2020

Description

I encountered a bug in the optimizer of clang++ which leads to a segmentation fault when working with neon intrinsic. This only happens when I run a release build in android studio. I played a bit with the clang++ command and found out that this happens due to the optimization level (-Oz) of the release build.

I provided a small test case that shows the issue:
https://github.com/SimonSuckut/ndk_clang-_bug

with build_debug.sh (contains the clang++ command of the debug build) the compilation works perfectly fine, with build.sh (contains the clang++ command of the release build) the compile process ends with an segmentation fault:

1. <eof> parser at end of file 2. Code generation 3. Running pass 'Function Pass Manager' on module '/home/simon/AndroidStudioProjects/llvm_bug/llvm_bug.cpp'. 4. Running pass 'ARM Instruction Selection' on function '@_GLOBAL__sub_I_llvm_bug.cpp' #0 0x0000000002e6e2b5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/home/simon/Dokumente/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang+++0x2e6e2b5) #1 0x0000000002e6e63e llvm::sys::RunSignalHandlers() (/home/simon/Dokumente/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang+++0x2e6e63e) #2 0x0000000002e6e4e8 (/home/simon/Dokumente/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang+++0x2e6e4e8) #3 0x00007f5b9b5453c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0) #4 0x000000000266a480 llvm::TargetLowering::DAGCombinerInfo::CombineTo(llvm::SDNode*, llvm::ArrayRef<llvm::SDValue>, bool) (/home/simon/Dokumente/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang+++0x266a480) #5 0x0000000004bc6968 clang++: error: unable to execute command: Segmentation fault (core dumped) clang++: error: clang frontend command failed due to signal (use -v to see invocation) Android (5900059 based on r365631c) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 207d7abc1a2abf3ef8d4301736d6a7ebc224a290) (based on LLVM 9.0.8svn) Target: armv7-none-linux-android19 Thread model: posix

Environment Details

  • NDK Version: 21.3.6528147
  • Build system: clang++
  • Host OS: Ubuntu 20.04
  • ABI: armeabi-v7a
  • NDK API level: 19
@SimonSuckut SimonSuckut added the bug label Jul 1, 2020
@DanAlbert
Copy link
Member

Do you know if this crashed on r20?

@DanAlbert
Copy link
Member

(I'll check if you don't know, but lmk if you do)

@SimonSuckut
Copy link
Author

It seem to compile successfully on r20 (tested with 20.0.5594570).

@DanAlbert DanAlbert added this to the r21e milestone Jul 1, 2020
@DanAlbert
Copy link
Member

Thanks, triaging for r21d then.

@stephenhines
Copy link
Collaborator

This is a crash on invalid code actually. If I run with a more recent clang, I get the following diagnostic:

srhines@ringworld:__:aosp-master-plus-llvm$ /disk/llvm/build/bin/clang++ --target=armv7-none-linux-androideabi19 -c -Oz llvm_bug.cpp 
llvm_bug.cpp:38:31: error: argument value 2 is outside the valid range [0, 1]
    result.smid_data.val[0] = vmlaq_lane_f32(result.smid_data.val[0], m1.smid_data.val[3], vget_high_f32(m2.smid_data.val[0]), 2);
                              ^                                                                                                ~
/disk/llvm/build/lib/clang/11.0.0/include/arm_neon.h:14507:34: note: expanded from macro 'vmlaq_lane_f32'
  __ret_46 = __s0_46 + __s1_46 * splatq_lane_f32(__s2_46, __p3_46); \
                                 ^                        ~~~~~~~
/disk/llvm/build/lib/clang/11.0.0/include/arm_neon.h:703:25: note: expanded from macro 'splatq_lane_f32'
  __ret = (float32x4_t) __builtin_neon_splatq_lane_v((int8x8_t)__s0, __p1, 9); \
                        ^                                            ~~~~
1 error generated.

We can backport the fix for this, but the actual code as written uses an invalid index. I'll continue to track that down, but I wanted to post this in case you feel blocked on writing your own code with the current NDK.

@SimonSuckut
Copy link
Author

Oh thanks, thats really nice to know.
There is actually a typo in there. Only an index in the range [0, 1] makes sense here.
But when I chagne to the correct index, which is 1 in this case, the crash is still there.

@stephenhines
Copy link
Collaborator

Reduced and filed https://bugs.llvm.org/show_bug.cgi?id=46615 upstream with the ARM maintainers.

@stephenhines
Copy link
Collaborator

stephenhines commented Nov 19, 2020

This needs more than a prebuilt update, @DanAlbert . I do need to cherry-pick this for r21e if we're going to take it there. This is only in our toolchain-testing branch (and r22) right now.

@stephenhines
Copy link
Collaborator

Actually, it's in r22. It's only missing from r21e.

@stephenhines
Copy link
Collaborator

https://android-review.googlesource.com/q/topic:%22fix_1303%22+(status:open%20OR%20status:merged) cherry-picks the fix. We then need to check-in prebuilts so that you can get them for the LTS NDK.

@DanAlbert DanAlbert removed this from the r21e milestone Nov 19, 2020
@rprichard
Copy link
Collaborator

For posterity, this bug was fixed in r22's r399163b clang, in https://android-review.googlesource.com/c/toolchain/llvm_android/+/1440863.

@DanAlbert
Copy link
Member

Was a dup of #1248.

hjiayz pushed a commit to hjiayz/llvm_android that referenced this issue Feb 1, 2021
5f1cad4d29 [ARM] Skip combining base updates for vld1x NEON intrinsics

Bug: http://b/155835175
Bug: android/ndk#1248
Bug: android/ndk#1303
Test: ./build.py
Change-Id: Id1e025241c062e8db2ea179ae141ae1ba572e85c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants