NDK clang编译器的一个bug

问题代码

float32_t Sum_float(float32_t *data, const int count)
{
float32x4_t res = vdupq_n_f32(0.0f);
for(int i = 0; i < (count & (~15)); i += 16)
{
#if 01
float32x4x4_t v0 = vld1q_f32_x4(data + i);
float32x4_t v00 = v0.val[0];
float32x4_t v01 = v0.val[1];
float32x4_t v02 = v0.val[2];
float32x4_t v03 = v0.val[3]; #else
float32x4_t v00 = vld1q_f32(data + i);
float32x4_t v01 = vld1q_f32(data + i + 4);
float32x4_t v02 = vld1q_f32(data + i + 8);
float32x4_t v03 = vld1q_f32(data + i + 12); #endif v00 = vaddq_f32(v00, v02);
v01 = vaddq_f32(v01, v03);
res = vaddq_f32(res, vaddq_f32(v00, v01)); }
float32x2_t res1 = vadd_f32(vget_low_f32(res), vget_high_f32(res)); float32_t v0[2];
vst1_f32(v0, res1);
v0[0] += v0[1];
for(int i = count & (~15); i < count; ++i){
v0[0] += data[i];
} return v0[0];
}

编译测试

首先,查阅了https://static.docs.arm.com/ihi0073/c/IHI0073C_arm_neon_intrinsics_ref.pdf,对于vld1q_f32_x4这个指令,v7/A32/A64都是支持的。

不同编译器版本结果:首先,对于所有的版本,如果使用#else块的代码,都是可以编译成功的,对于使用#if 01块的代码,结果如下:

armeabi-v7a with o1 armeabi=v7a with o0 arm64-v8a
r20c clang++: error: clang frontend command failed due to signal (use -v to see invocation) ok ok
r19c ok ok ok
r15c error: use of undeclared identifier 'vld1q_f32_x4' error: use of undeclared identifier 'vld1q_f32_x4' ok

不仅仅vld1q_f32_x4,对于vld1_u8_x2;vst1q_f32_x4等类似指令都存在这样的问题。

性能对比

测试代码:

int main()
{
const size_t len = 1024*1024 * 16;
float32_t *data = new float32_t[len];
for(size_t i = 0; i < len; ++i) {
data[i] = std::rand() / 100.0;
} clock_t t0 = std::clock();
float32_t sum = Sum_float(data, len);
printf("sum=%f , time cost=%f \n", sum, 1000.0 * (double)(std::clock() - t0) / CLOCKS_PER_SEC);
return 0;
}

测试了使用三种NDK版本编译arm64-v8a测试,同时使用r19c编译了armeabi-v7a,分别使用#if#else分之,发现耗时都是在3.55ms左右,无明显差别。

类似问题:https://github.com/mattgodbolt/compiler-explorer/issues/1906

地址对齐

虽然使用r19c的版本编译armeabi-v7a成功,或者使用不优化的r20c也一样,但是执行时发生了crash。原因是执行vldN(q)_type_xN指令时,地址不对齐导致的crash。

而对于arm64-v8a版本,把所有传给vldN(q)_type_xN的地址打印出来,同样发现也有0x7350800001这样的地址,而且地址末位为0到E的都有,但是却没有报错。也即,对于该指令只有armeabi-v7a有地址对齐要求,而arm64-v8a却没有?

同时,常规的vldN(q)_type指令则没有地址对齐的要求,所以最好不要使用vldN(q)_type_xN

在代码中因为地址对齐而导致的crash日志:

libc    : Fatal signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xf0900001 in tid 27659 (ClarityOpt), pid 27659 (ClarityOpt)
crash_dump32: obtaining output fd from tombstoned, type: kDebuggerdTombstone
crash_dump32: performing dump of process 27659 (target tid = 27659)
DEBUG : Process name is /data/local/tmp/ClarityOpt, not key_process
DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
DEBUG : Build fingerprint: 'OPPO/PCCM00/OP4A7A:10/QKQ1.191222.002/1584699103:user/release-keys'
DEBUG : Revision: '0'
DEBUG : ABI: 'arm'
DEBUG : Timestamp: 2020-05-09 15:15:16+0800
DEBUG : pid: 27659, tid: 27659, name: ClarityOpt >>> /data/local/tmp/ClarityOpt <<<
DEBUG : uid: 0
crash_dump32: type=1400 audit(0.0:27044): avc: denied { read } for name="ClarityOpt" dev="sda11" ino=30524 scontext=u:r:crash_dump:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=1
crash_dump32: type=1400 audit(0.0:27045): avc: denied { open } for path="/data/local/tmp/ClarityOpt" dev="sda11" ino=30524 scontext=u:r:crash_dump:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=1
crash_dump32: type=1400 audit(0.0:27046): avc: denied { getattr } for path="/data/local/tmp/ClarityOpt" dev="sda11" ino=30524 scontext=u:r:crash_dump:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=1
crash_dump32: type=1400 audit(0.0:27047): avc: denied { map } for path="/data/local/tmp/ClarityOpt" dev="sda11" ino=30524 scontext=u:r:crash_dump:s0 tcontext=u:object_r:shell_data_file:s0 tclass=file permissive=1
DEBUG : signal 7 (SIGBUS), code 1 (BUS_ADRALN), fault addr 0xf0900001
DEBUG : r0 00000043 r1 00000000 r2 a9a5ac6f r3 00000003
DEBUG : r4 f0900001 r5 ffcb0a00 r6 ffcb0a40 r7 ffcb0b60
DEBUG : r8 f0900007 r9 00000001 r10 f0900000 r11 f0900000
DEBUG : ip ffcb0500 sp ffcb09f0 lr 00000004 pc 021d265e
DEBUG :
DEBUG : backtrace:
DEBUG : #00 pc 0000365e /data/local/tmp/ClarityOpt (BuildId: fb1d8b990741386becb60ff1c8b10583efb05f70)
DEBUG : #01 pc 00004271 /data/local/tmp/ClarityOpt (BuildId: fb1d8b990741386becb60ff1c8b10583efb05f70)
DEBUG : #02 pc 00004c9f /data/local/tmp/ClarityOpt (BuildId: fb1d8b990741386becb60ff1c8b10583efb05f70)
DEBUG : #03 pc 00004dd3 /data/local/tmp/ClarityOpt (BuildId: fb1d8b990741386becb60ff1c8b10583efb05f70)
DEBUG : #04 pc 000513bb /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+66) (BuildId: 8e41d0dce7911ae25a51deb63aa9720c)
DEBUG : #05 pc 00002a98 /data/local/tmp/ClarityOpt (BuildId: fb1d8b990741386becb60ff1c8b10583efb05f70)

NDK clang编译器的一个bug的更多相关文章

  1. NDK中使用pthread多线程中自己写的一个BUG

    在使用pthread进行NDK中的多线程开发时,自己写了一个BUG, void *darkGrayThread(void *args) { ThreadParam *param = (ThreadPa ...

  2. 标准模板库(STL)的一个 bug

    今天敲代码的时候遇到 STL 的一个 bug,与 C++ 的类中的 const 成员变量有关.什么,明明提供了默认的构造函数和复制构造函数,竟然还要类提供赋值运算符重载.怎么会这样? 测试代码 Tes ...

  3. clang编译器简介

    本文部分内容引用: 中文维基百科. 结构化编译器前端--clang介绍. 什么是clang编译器? clang是LLVM编译器工具集的一个用于编译C.C++.Objective-C的前端.LLVM项目 ...

  4. 转:GCC,LLVM,Clang编译器对比

    GCC,LLVM,Clang编译器对比   转自: http://www.cnblogs.com/qoakzmxncb/archive/2013/04/18/3029105.html 在XCode中, ...

  5. VC2013的一个bug

    前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其它的非C++元素.这是一个比较新的 ...

  6. [置顶]VC2013的一个bug

    [置顶]VC2013的一个bug 前段时间在尝试使用一个C++的GUI库nana.这个库最大的特点在于使用现代C++风格去编写GUI程序,而不需要使用大量的比较丑陋的代码(如MFC中的各种宏),或者其 ...

  7. 深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...

  8. Tomcat一个BUG造成CLOSE_WAIT

    之前应该提过,我们线上架构整体重新架设了,应用层面使用的是Spring Boot,前段日子因为一些第三方的原因,略有些匆忙的提前开始线上的内测了.然后运维发现了个问题,服务器的HTTPS端口有大量的C ...

  9. MySQL关于exists的一个bug

    今天碰到一个很奇怪的问题,关于exists的, 第一个语句如下: SELECT ) FROM APPLY t WHERE EXISTS ( SELECT r.APPLY_ID FROM RECORD ...

随机推荐

  1. 前端学习笔记 --ES6新特性

    前言 这篇博客是我在b站进行学习es6课程时的笔记总结与补充. 此处贴出up主的教程视频地址:深入解读ES6系列(全18讲) 1.ES6学习之路 1.1 ES6新特性 1. 变量 2. 函数 3. 数 ...

  2. Web三维编程入门总结之一:WebGL与Threejs入门知识

    /*在这里对这段时间学习的3D编程知识做个总结,以备再次出发.计划分成“webgl与three.js基础介绍”.“面向对象的基础3D场景框架编写”.“模型导入与简单3D游戏编写”三个部分,其他零散知识 ...

  3. 复习python的__call__ __str__ __repr__ __getattr__函数 整理

    class Www: def __init__(self,name): self.name=name def __str__(self): return '名称 %s'%self.name #__re ...

  4. Python的炫技操作:条件语句的七种写法

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python极客社区 PS:如有需要Python学习资料的小伙伴可以 ...

  5. 百度AI开发平台简介

    AIstudio https://aistudio.baidu.com/aistudio/index 关于AI Studio AI Studio是基于百度深度学习平台飞桨的一站式AI开发平台,提供在线 ...

  6. django 分页器 Paginator 基础操作

    基于下面这个分页器,说明常用的属性 from django.core.paginator import Paginator #导入Paginator类 from sign.models import ...

  7. 刷题记录:[V&N2020 公开赛]TimeTravel

    题目复现链接:https://buuoj.cn/challenges 参考链接:2020 年 V&N 内部考核赛 WriteUp V&N公开赛2020 writeup httpoxy ...

  8. Java环境下 selenium webDriver + chrome浏览器搭建与调试

    一.首先下载selenium webDriver jar包,下载地址如下: http://selenium-release.storage.googleapis.com/index.html 二.下载 ...

  9. 详解PHP中instanceof关键字及instanceof关键字有什么作用

    来源:https://www.jb51.net/article/74409.htm PHP5的另一个新成员是instdnceof关键字.使用这个关键字可以确定一个对象是类的实例.类的子类,还是实现了某 ...

  10. RedHat 的 crontab

    Chapter 39. Automated Tasks In Linux, tasks can be configured to run automatically within a specifie ...