Search code examples
androidoptimizationandroid-ndkneon

How to set optimization level for a specific file in Android NDK?


I have a native library for Android that has some files that include NEON assembly code. I've inherited this code from some other coder, and given my knowledge regarding NEON assembly coding (or any assembly, for that matter) is skimpy, to say the least. Anyhow, I've noticed the following problem: when I compile with 'ndk-build NDK_DEBUG=1', all is fine. When I compile for release, 'ndk-build NDK_DEBUG=0', the compiler optimizes away the assembly code. I've managed to work around the problem by hacking the ndk build scripts, and splitting my library into two, where one lib has all the assembly files in it - for this lib I set optimization to '-O0' in a very hacky way. So the question is: how can I specify an optimization level for a specific file? setting APP_OPTIM is done in Application.mk, that affects all the compiled files. So does the NDK_DEBUG flag.

EDIT: as per Alex's request, here's the Android.mk I ended up using, splitting the lib into two: one part with assembly code (and -O0), other part with regular C code (and -O2):

LOCAL_PATH := $(call my-dir)

# assembly_neon_code_here (neon) module - turn optimization off
include $(CLEAR_VARS)

LOCAL_MODULE := assembly_neon_code_here
LOCAL_SRC_FILES := assembly_neon_code_here.cpp
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
    LOCAL_ARM_NEON := true
endif

LOCAL_CFLAGS := -O0
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog 
include $(BUILD_SHARED_LIBRARY)

# main module
include $(CLEAR_VARS)

LOCAL_MODULE    := complete_lib
LOCAL_SRC_FILES := regular_src1.cpp regular_src2.cpp regular_src3.cpp 
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
    LOCAL_ARM_NEON := true
endif

# allow logcat calls
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lz
LOCAL_SHARED_LIBRARIES := assembly_neon_code_here
include $(BUILD_SHARED_LIBRARY)

Solution

  • GCC >= 4.4 seems to support #pragma GCC optimize to change the optimization level mid-file, and also attribute optimize to set it per function.

    See http://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Function-Specific-Option-Pragmas.html#Function-Specific-Option-Pragmas and http://gcc.gnu.org/onlinedocs/gcc-4.4.7/gcc/Function-Attributes.html#Function-Attributes.

    According to those links, putting #pragma GCC optimize ("O0") at the top of the file causing the problem should do the trick.