You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucy.apache.org by Logan Bell <lo...@gmail.com> on 2012/03/20 06:43:49 UTC

[lucy-dev] Optimization Bug On Mac OS X

Devs,

Today Marvin helped me track down what we believe is a compiler
optimization bug associated with the latest version of xcode and Mac OS
10.7.3 (Lion), compiler clan version 3.1. To summarize, I was getting the
following unit test failure when after compiling Lucy with Perl 5.14.2
(perl brew install):

not ok 2 - catch overflow in token position calculation

#   Failed test 'catch overflow in token position calculation'
#   at t/152-inversion.t line 70.
#                   ''
#     doesn't match '(?^:position)'

After dropping in various debug statements the unit test would magically
begin to pass. At this point I turned off optimization and the test would
always pass. Based on this evidence it would be safe to surmise that the
optimizer is making a mistake somewhere. After further discussions on IRC
with Marvin, we speculated that 32 bit integers that are being accumulated
into 64 bit registers are *not* being truncated back to 32 bit prior to a
comparison operation. Apparently during these stages we're speculating that
the optimizer is running afoul.

One idea that was floated on IRC was to convert the associated integers in
question to int64_t, since support for 64 bit integers is required anyways
for Lucy. However, we wanted to throw this issue up to the list and see if
anyone else can maybe a) replicate this and b) has any other thoughtful
solutions.

Cheers,
Logan

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Logan Bell <lo...@gmail.com>.
Yes, and a suggested fix as well! I implemented their solution in my
environment and it fixes the problem.

Thanks,
Logan

On Fri, Mar 23, 2012 at 3:52 AM, Nick Wellnhofer <we...@aevum.de>wrote:

> On 23/03/2012 10:50, Nick Wellnhofer wrote:
>
>> This is definitely a compiler bug.
>>
>
> Seems that I was wrong. I filed a bug with LLVM and got a very quick reply:
>
> http://llvm.org/bugs/show_bug.**cgi?id=12335#c1<http://llvm.org/bugs/show_bug.cgi?id=12335#c1>
>
> Nick
>

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Nick Wellnhofer <we...@aevum.de>.
On 23/03/2012 10:50, Nick Wellnhofer wrote:
> This is definitely a compiler bug.

Seems that I was wrong. I filed a bug with LLVM and got a very quick reply:

http://llvm.org/bugs/show_bug.cgi?id=12335#c1

Nick

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Nick Wellnhofer <we...@aevum.de>.
On 23/03/2012 00:59, Logan Bell wrote:
> Nick,
>
> I went ahead and created a JIRA story with the original e-mail of this
> thread:
> https://issues.apache.org/jira/browse/LUCY-216
>
> On this ticket you should see 4 attachments:
>
> Inversion.sO0   19kb (Without optimization)
> Inversion.sO0   4kb (Ignore, this was a mistake unable to figure out how to
> delete from Jira)
> Inversion.sO1   16kb (With optimization)

Thanks, Logan. This is definitely a compiler bug. Here's the relevant 
part of the assembler output for the inner loop of Inversion_invert 
(comments are mine):

LBB12_4:
	movq	(%rbx), %rax        # cur_token = *tokens;
	movl	%r9d, 48(%rax)      # cur_token->pos = token_pos;
	movl	44(%rax), %eax      # %eax = cur_token->pos_inc
	testl	%eax, %eax
	leal	(%rax,%r9), %r15d   # %r15d = %eax + token_pos
	jns	LBB12_6
	[ code for THROW(...) ]
LBB12_6:
	addq	$8, %rbx
	cmpq	%r14, %rbx
	movl	%r15d, %r9d
	jb	LBB12_4

The "if" statement in the loop is compiled to the following instructions:

	testl	%eax, %eax
	jns	LBB12_6

Where %eax contains cur_token->pos_inc. So the "if" statement basically 
becomes:

	if (cur_token->pos_inc < 0)

Which is of course bogus. It really makes me wonder why the compiler 
gets this very simple loop wrong. My best guess is that clang tries to 
optimize the standard way to test for integer overflow in C and somehow 
gets it wrong. Since overflows rarely happen, this could explain why 
this bug wasn't noticed early on.

Nick

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Logan Bell <lo...@gmail.com>.
Nick,

I went ahead and created a JIRA story with the original e-mail of this
thread:
https://issues.apache.org/jira/browse/LUCY-216

On this ticket you should see 4 attachments:

Inversion.sO0   19kb (Without optimization)
Inversion.sO0   4kb (Ignore, this was a mistake unable to figure out how to
delete from Jira)
Inversion.sO1   16kb (With optimization)

<https://issues.apache.org/jira/secure/attachment/12519523/Inversion.s-O0O1.diff>Inversion.s-O0O1.diff
4kb

The diff has been modified to only include the assembly output of
Inversion_invert. Let me know if this works for you.

Thanks,
Logan


On Thu, Mar 22, 2012 at 2:11 AM, Nick Wellnhofer <we...@aevum.de>wrote:

> On 22/03/2012 00:15, Marvin Humphrey wrote:
>
>> The hypothesis is:
>>
>>   * The sum is being accumulated into a register that is larger than 32
>> bits.
>>   * An extra op is necessary to determine that 32-bit overflow has
>> occurred.
>>   * This op has been inappriately optimized away by the compiler.
>>
>
> Or something with sign extension goes wrong. I can't test this on OS X
> myself, but I'd love to have a look at the compiled assembler code. Can you
> post it to the list?
>
> Nick
>

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Nick Wellnhofer <we...@aevum.de>.
On 22/03/2012 00:15, Marvin Humphrey wrote:
> The hypothesis is:
>
>    * The sum is being accumulated into a register that is larger than 32 bits.
>    * An extra op is necessary to determine that 32-bit overflow has occurred.
>    * This op has been inappriately optimized away by the compiler.

Or something with sign extension goes wrong. I can't test this on OS X 
myself, but I'd love to have a look at the compiled assembler code. Can 
you post it to the list?

Nick

Re: [lucy-dev] Optimization Bug On Mac OS X

Posted by Marvin Humphrey <ma...@rectangular.com>.
On Mon, Mar 19, 2012 at 10:43 PM, Logan Bell <lo...@gmail.com> wrote:
> To summarize, I was getting the following unit test failure when after
> compiling Lucy with Perl 5.14.2 (perl brew install):

The fact that this occurred only under perlbrew is notable because while the
system Perl on Lion appears to have been compiled with GCC, the default
compiler "cc" has been switched to Clang and thus perlbrew Perls are compiled
with Clang.  We appear to have encountered an optimization bug in Clang.

> not ok 2 - catch overflow in token position calculation
>
> #   Failed test 'catch overflow in token position calculation'
> #   at t/152-inversion.t line 70.
> #                   ''
> #     doesn't match '(?^:position)'

The test is designed to cover this section of code from
core/Lucy/Analysis/Inversion.c:

    // Assign token positions.
    for (; tokens < limit; tokens++) {
        Token *const cur_token = *tokens;
        cur_token->pos = token_pos;
        token_pos += cur_token->pos_inc;
        if (token_pos < cur_token->pos) {
            THROW(ERR, "Token positions out of order: %i32 %i32",
                  cur_token->pos, token_pos);
        }
    }

It is trying to trigger the exception, but failing on this particular Perl.

> After dropping in various debug statements the unit test would magically
> begin to pass.

If we insert the following debugging WARN statement immediately *above* the
"if" clause, things start to work as intended.

    WARN("token_pos: %i32 cur_token->pos: %i32", token_pos, cur_token->pos);

If we insert the WARN statement directly *below* the "if" test, the problem
continues to manifest, but the WARN statement reveals values for the
variables which clearly should have triggered the exception.

> At this point I turned off optimization and the test would
> always pass. Based on this evidence it would be safe to surmise that the
> optimizer is making a mistake somewhere. After further discussions on IRC
> with Marvin, we speculated that 32 bit integers that are being accumulated
> into 64 bit registers are *not* being truncated back to 32 bit prior to a
> comparison operation.

It is the behavior when the WARN statement is inserted which spurs this
speculation.

Without the WARN statement, the last thing which happens before the comparison
operation is the addition to token_pos.  The compiler can leave that value in
a register and then compare against it.

When the WARN statement is inserted, the register where token_pos had been
accumulated will be needed for other things, so the value of token_pos must be
moved out of the register to temporary storage, then restored so that the
comparison can take place.

The hypothesis is:

  * The sum is being accumulated into a register that is larger than 32 bits.
  * An extra op is necessary to determine that 32-bit overflow has occurred.
  * This op has been inappriately optimized away by the compiler.

Moving the value out of the register and then restoring it has the side
effect of truncating it.  For what it's worth, we worked around an "excess
floating point precision" problem using a similar brute force truncation in
SortCollector.c:

                    if (*(int32_t*)&score == *(int32_t*)&self->bubble_score) {
                        break;
                    }

> One idea that was floated on IRC was to convert the associated integers in
> question to int64_t, since support for 64 bit integers is required anyways
> for Lucy.

If we go 64-bit, we still need to guard against overflow.  The only advantage
is that it might work around this apparent optimizer bug without us needing to
do stupid stuff like probe for the bug and #ifdef the problematic section.

Marvin Humphrey