如何在Android NDK中为特定文件设置优化级别? [英] How to set optimization level for a specific file in Android NDK?

查看:483
本文介绍了如何在Android NDK中为特定文件设置优化级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android的本地库,其中包含一些包含NEON汇编代码的文件.我已经从其他编码器那里继承了此代码,并且至少可以说,由于我对NEON程序集编码(或任何程序集)的了解是轻率的.无论如何,我注意到了以下问题:当我使用"ndk-build NDK_DEBUG = 1"进行编译时,一切都很好.当我为版本"ndk-build NDK_DEBUG = 0"进行编译时,编译器会优化掉汇编代码.我设法解决了这个问题,方法是破解ndk构建脚本,然后将我的库拆分为两个,其中一个库中包含所有程序集文件-为此,我以一种非常hacky的方式将优化设置为"-O0" . 所以问题是:如何为特定文件指定优化级别?设置APP_OPTIM是在Application.mk中完成的,这会影响所有已编译的文件. NDK_DEBUG标志也是如此.

根据Alex的要求,这是我最终使用的Android.mk,将lib分为两部分:一部分是汇编代码(和-O0),另一部分是常规C代码(和-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)

解决方案

GCC> = 4.4似乎支持#pragma GCC optimize来更改优化级别的中间文件,并且还支持optimize来针对每个功能进行设置.

请参见放在引起问题的文件顶部应该可以解决问题.

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)

解决方案

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.

这篇关于如何在Android NDK中为特定文件设置优化级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆