LLVM编译器优化bug还是什么?

时间:2013-07-08 03:33:41

标签: c++ xcode llvm compiler-optimization

我偶然发现了一个我无法理解的有趣问题。

背景是:

  • LLVM 4.2 XCode上的编译器
  • 使用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,我逐步执行代码来追踪一个奇怪的错误,这让我意识到if的第一个分支是带输入的

sx = 648
tx = 649
w = 768
vs = 60

(这些值直接来自XCode中的locals表,我无法查询lldb关于vs,因为我猜它会得到优化。)

然后第一个分支是if (648 < 120 && ...所以应该没有办法接受它,但它实际上发生了。如果我用-O0编译,那么bug就会消失。

另一个有趣的事实是,对于sx = 647tx = 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

如果我在if子句之前放置一个打印,例如printf("%d < %d - %d",sx,vs*2,sx < vs*2),那么该bug就会消失。

这个简单的测试用例解决了这个问题:

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

我设法在一个独立的程序中重现了这个bug,我刚刚创建了一个空的iOS项目,然后我定义了两次该函数,一次在AppDelegate.mm中直接从同一个文件中调用,另一个在一个单独的文件中文件:

Test.h

#ifndef TEST_H_
#define TEST_H_

class Utils
{
  public:
    static int wrapDistance(int tx, int sx, int w);
};

#endif

Test.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))内联函数,则两个函数都会失败。我真的在摸索着黑暗。

1 个答案:

答案 0 :(得分:5)

首先,您的测试用例意味着它实际上错误地占用了else if分支。

但我把它拿回来;结果ASM似乎确实存在错误。 *

以下是ASM的格式化/注释版本,用于失败的测试:

% r0 = tx = 649
% r1 = sx = 648
% r2 = w  = 768

% w - vs*2
sub.w   r3, r2, #120         % r3 = 648

% if (sx < vs*2)
cmp r1, #119
it  le                       % (1) Not taken
  % if ((w - vs*2) < tx)
  cmple   r3, r0
ittt    lt                   % (2) Not taken
  % return (sx + w - tx)
  sublt   r0, r1, r0
  addlt   r0, r2
  bxlt    lr

% if ((w - vs*2) < sx)
cmp r3, r1
it  lt                       % (3) Not taken
  % if (tx < vs*2)
  cmplt   r0, #119
itttt   le                   % (4) Taken!  <<<<<<<<<<<<<
  % return -(w - sx + tx)
  suble   r1, r2, r1
  addle   r0, r1
  rsble   r0, r0, #0
  bxle    lr

% return sx - tx
subs    r0, r1, r0
bx  lr

条件(3)和(4)应该一起工作以实现两个子表达式的逻辑AND。理论上,块(4)仅在执行块(3)时执行,后续比较设置适当的状态标志。 **

但是,这是错误的。比较(3)设置Z,这意味着不触发条件(3)(它需要N!=V),因此不执行条件(4)。好到目前为止。但 足以触发条件(4)(它需要(Z==1) || (N!=V)),导致您看到的问题。

总而言之,这里有四种可能性:

  1. 针对ARM7的LLVM后端确实存在一个错误。
  2. 您提供的C代码实际上并不是您正在编译的代码。
  3. 您在其他地方有一些无效的C代码触发了未定义的行为,导致无意义的ASM被生成为副作用。
  4. 我上面的分析不正确!
  5. <小时/> *虽然现在是12:40,但我可能会误会......

    ** http://blogs.arm.com/software-enablement/206-condition-codes-1-condition-flags-and-codes/