我偶然发现了一个我无法理解的有趣问题。LLVM编译优化bug还是什么?
背景是:上的XCode
- LLVM 4.2编译器与C++ 11支持编译
- 编译
-Os
- 编译对ARMv7/armv7s架构现在
我意识到,在编译启用优化时存在一些代码存在问题。
的代码,逐字:
static int foo(int tx, int sx, int w)
{
int vs = 60;
if (sx < vs*2 && tx > w - vs*2)
return (sx + w - tx);
else if (sx > w - vs*2 && tx < vs*2)
return -(w - sx + tx);
else
return sx - tx;
}
现在,通过与LLDB去我走到码追查一个奇怪的错误,这使我认识到的第一个分支,如果采取输入
sx = 648
tx = 649
w = 768
vs = 60
(这些值是从直接取自XCode中当地人表,我不能查询LLDB约vs
,因为我猜它得到优化。)
然后第一个分支是if (648 < 120 && ...
,所以应该没有办法采取它,但它实际上发生。如果我用-O0进行编译,那么这个bug就会消失。
另一个有趣的事实是,sx = 647
和tx = 648
这个错误不会发生。
现在,事情是两件事:或者我错过了一些如此明显的事情,10小时的调试禁止我看到或在优化中存在某种错误。
任何线索?
一些更多的背景,这就是ASM生成:
.private_extern __ZN5Utils12wrapDistanceEiii
.globl __ZN5Utils12wrapDistanceEiii
.align 2
.code 16 @ @_ZN5Utils12wrapDistanceEiii
.thumb_func __ZN5Utils12wrapDistanceEiii
__ZN5Utils12wrapDistanceEiii:
.cfi_startproc
Lfunc_begin9:
@ BB#0:
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
@DEBUG_VALUE: vs <- 60+0
sub.w r3, r2, #120
cmp r1, #119
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
it le
cmple r3, r0
Ltmp42:
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
ittt lt
sublt r0, r1, r0
Ltmp43:
addlt r0, r2
@DEBUG_VALUE: vs <- 60+0
bxlt lr
Ltmp44:
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
@DEBUG_VALUE: vs <- 60+0
cmp r3, r1
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
it lt
cmplt r0, #119
Ltmp45:
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: wrapDistance:w <- R2+0
itttt le
suble r1, r2, r1
Ltmp46:
addle r0, r1
Ltmp47:
rsble r0, r0, #0
@DEBUG_VALUE: vs <- 60+0
bxle lr
Ltmp48:
@DEBUG_VALUE: wrapDistance:tx <- R0+0
@DEBUG_VALUE: wrapDistance:sx <- R1+0
@DEBUG_VALUE: vs <- 60+0
subs r0, r1, r0
Ltmp49:
@DEBUG_VALUE: vs <- 60+0
bx lr
Ltmp50:
Lfunc_end9:
.cfi_endproc
如果我把打印,例如printf("%d < %d - %d",sx,vs*2,sx < vs*2)
if子句在此之前的bug消失。
这个简单的测试用例exibits问题:
for (int i = 0; i < 767; ++i)
{
printf("test: %d, %d, %d",i,i+1,Utils::wrapDistance(i+1, i, 768))
}
...
test: 641, 642, -1
test: 642, 643, -1
test: 643, 644, -1
test: 644, 645, -1
test: 645, 646, -1
test: 646, 647, -1
test: 647, 648, -1
test: 648, 649, -769
test: 649, 650, -1
test: 650, 651, -1
test: 651, 652, -1
test: 652, 653, -1
test: 653, 654, -1
test: 654, 655, -1
...
EDIT2
我设法再现一个独立的程序错误,我只是创建了一个空的iOS项目,那么我所定义的函数两次,一次在AppDelegate.mm中直接从同一个文件中调用,另一个在单独的文件中调用:
Test.h
#ifndef TEST_H_
#define TEST_H_
class Utils
{
public:
static int wrapDistance(int tx, int sx, int w);
};
#endif
测试。CPP
#include "Test.h"
int Utils::wrapDistance(int tx, int sx, int w)
{
int vs = 60;
if (sx < vs*2 && tx > w - vs*2)
return (sx + w - tx);
else if (sx > w - vs*2 && tx < vs*2)
return -(w - sx + tx);
else
return sx - tx;
}
AppDelegate.mm
#import "AppDelegate.h"
#include "Test.h"
int wrapDistance(int tx, int sx, int w)
{
int vs = 60;
if (sx < vs*2 && tx > w - vs*2)
return (sx + w - tx);
else if (sx > w - vs*2 && tx < vs*2)
return -(w - sx + tx);
else
return sx - tx;
}
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
for (int i = 0; i < 767; ++i)
{
NSLog(@"test inside: %d, %d, %d",i,i+1,wrapDistance(i+1, i, 768));
NSLog(@"test outside: %d, %d, %d",i,i+1,Utils::wrapDistance(i+1, i, 768));
}
return YES;
}
...
输出
test inside: 644, 645, -1
test outside: 644, 645, -1
test inside: 645, 646, -1
test outside: 645, 646, -1
test inside: 646, 647, -1
test outside: 646, 647, -1
test inside: 647, 648, -1
test outside: 647, 648, -1
test inside: 648, 649, -1
test outside: 648, 649, -769
test inside: 649, 650, -1
test outside: 649, 650, -1
test inside: 650, 651, -1
test outside: 650, 651, -1
test inside: 651, 652, -1
test outside: 651, 652, -1
正如你所看到的,就是里面的文件定义的函数的行为与这所谓的是正确的,但同样的事情不适用于另一个,这表明了同样的错误。如果我强制不与__attribute__ ((noinline))
内联函数,那么这两个函数都会失败。我真的摸索着黑暗。
这极有可能是你看到引起未定义行为通过代码中的其他地方的问题。你能构建一个完整的测试用例吗? –
这是一个完整的测试用例,该函数不依赖于任何外部输入,它是一个静态效用函数,用于计算包装环境中两个图块之间的距离。这些错误总是出现在这些输入值中。我应该尝试从项目中隔离它或检查ASM代码,我猜。 – Jack
“测试用例”是指包含驱动程序代码(即单元测试或显示行为所需的任何内容)的[SSCCE](http://sscce.org)。正如我确信你知道的那样,许多错误有一个修复自己的习惯,一旦麻烦的代码与程序的其余部分分离开来;) –