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

Clang compiler crash when using -flto #350

Closed
mad-rain opened this issue Mar 30, 2017 · 7 comments
Closed

Clang compiler crash when using -flto #350

mad-rain opened this issue Mar 30, 2017 · 7 comments
Assignees
Labels
Milestone

Comments

@mad-rain
Copy link

mad-rain commented Mar 30, 2017

Description

We have tried to enable LTO, but got compiler crash during linking phase. Here is what we see in gdb:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6143cb3 in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
(gdb) 
(gdb) where
#0  0x00007ffff6143cb3 in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#1  0x00007ffff613dfb3 in llvm::LoopVectorizePass::processLoop(llvm::Loop*) ()
   from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#2  0x00007ffff6147b55 in llvm::LoopVectorizePass::runImpl(llvm::Function&, llvm::ScalarEvolution&, llvm::LoopInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::BlockFrequencyInfo&, llvm::TargetLibraryInfo*, llvm::DemandedBits&, llvm::AAResults&, llvm::AssumptionCache&, std::__1::function<llvm::LoopAccessInfo const& (llvm::Loop&)>&) () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#3  0x00007ffff614860e in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#4  0x00007ffff601ba73 in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
   from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#5  0x00007ffff601bc6b in llvm::FPPassManager::runOnModule(llvm::Module&) ()
   from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#6  0x00007ffff601c0c1 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
   from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/../lib64/libLLVM.so
#7  0x00007ffff7e79c13 in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so
#8  0x00007ffff7e77855 in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so
#9  0x00007ffff7e75295 in ?? () from /android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so

We had replaced '-Os' with '-O2' due to this bug: 'fatal error: Optimization level must be between 0 and 3'

Environment Details

  • NDK Version: 14.1.3816874.
  • Build sytem: CMake
  • Host OS: Ubuntu Linux
  • Compiler: Clang
  • ABI: armeabi-v7a
  • STL: gnustl_static
  • NDK API level: 16
@DanAlbert
Copy link
Member

Thanks for the report!

@pirama-arumuga-nainar: Is this fixed in the update?

@pirama-arumuga-nainar
Copy link
Collaborator

@mad-rain Can you share repro steps?

@mad-rain
Copy link
Author

mad-rain commented Apr 6, 2017

@pirama-arumuga-nainar steps to reproduce:

  1. Create default Android Studio project with C++ support

  2. Replace native-lib.cpp content with this:

#include <jni.h>

#include <thread>
#include <vector>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_rain_androidltobug_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {

    std::vector<std::thread> Threads;
    Threads.resize(4);

    return nullptr;
}
  1. Add to CMakeLists.txt this:
set (CMAKE_CXX_STANDARD 14)

SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto -O2")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -flto -O2")
  1. externalNativeBuild & ndk params in build.gradle:
        ndk {
            abiFilters "armeabi-v7a"
        }
        externalNativeBuild {
            cmake {
                cppFlags ""
                arguments "-DANDROID_ARM_NEON=1"
            }
        }
  1. run ./gradlew assembleRelease

Then you should see clang crash:

clang++: error: linker command failed with exit code 1 (use -v to see invocation)

@mad-rain
Copy link
Author

Any comments?

@pirama-arumuga-nainar
Copy link
Collaborator

@mad-rain Sorry I didn't have a chance to look into this yet. I primarily work on the Android platform so I don't have Android Studio or Gradle set up. Would it be possible to just share the compile and link commands?

@mad-rain
Copy link
Author

@pirama-arumuga-nainar, no problem.

native-lib.cpp:

#include <jni.h>

#include <thread>
#include <vector>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_rain_androidltobug_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {

    std::vector<std::thread> Threads;
    Threads.resize(4);

    return nullptr;
}

Compile:

/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  --target=armv7-none-linux-androideabi --gcc-toolchain=/android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/android/sdk/ndk-bundle/platforms/android-15/arch-arm  -Dnative_lib_EXPORTS -isystem /android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -isystem /android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security  -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security   -Os -DNDEBUG -Os -DNDEBUG  -flto -O2 -fPIC   -std=gnu++14 -MD -MT native-lib.cpp.o -MF native-lib.cpp.o.d -o native-lib.cpp.o -c native-lib.cpp

Link (should crash):

/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  --target=armv7-none-linux-androideabi --gcc-toolchain=/android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/android/sdk/ndk-bundle/platforms/android-15/arch-arm -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security  -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security   -Os -DNDEBUG -Os -DNDEBUG  -flto -O2  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o libnative-lib.so native-lib.cpp.o  -llog -lm "/android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a"

Please replace /android/sdk/ndk-bundle with your NDK location.

@pirama-arumuga-nainar
Copy link
Collaborator

I was able to reproduce in the clang in the beta. The stack trace for crash inside libLLVMGold.so is below. It crashes in the loop vectorizer. Good news is that it doesn't reproduce in the next clang that will be in r15. I'll look for an upstream change that may have fixed it.

Stack trace on old clang:
#0 0x00007ffff586baf3 in ?? ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#1 0x00007ffff5865df3 in llvm::LoopVectorizePass::processLoop(llvm::Loop*) ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#2 0x00007ffff586f995 in llvm::LoopVectorizePass::runImpl(llvm::Function&, llvm::ScalarEvolution&, llvm::LoopInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::BlockFrequencyInfo&, llvm::TargetLibraryInfo*, llvm::DemandedBits&, llvm::AAResults&, llvm::AssumptionCache&, std::__1::function<llvm::LoopAccessInfo const& (llvm::Loop&)>&) ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#3 0x00007ffff587044e in ?? ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#4 0x00007ffff57438b3 in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#5 0x00007ffff5743aab in llvm::FPPassManager::runOnModule(llvm::Module&) ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#6 0x00007ffff5743f01 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/../lib64/libLLVM.so
#7 0x00007ffff7fb6c13 in ?? ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/LLVMgold.so
#8 0x00007ffff7fb4855 in ?? ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/LLVMgold.so
#9 0x00007ffff7fb2295 in ?? ()
from /ssd2/pirama/aosp3/prebuilts/clang/host/linux-x86/clang-3289846/bin/../lib64/LLVMgold.so
#10 0x0000000000653d6f in ?? ()
#11 0x0000000000653ea4 in ?? ()
#12 0x00000000006a35f5 in ?? ()
#13 0x00000000006a395a in ?? ()

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

3 participants