Register corrupted after routine invocation

21 views
Skip to first unread message

uja...@yahoo.com

unread,
Jun 2, 2014, 5:37:48 AM6/2/14
to andro...@googlegroups.com
Hi,

I have a problem that causes a register to get corrupted and eventually a wrong result.

Description
The routine that delivers wrong results goes like:

someRoutine()
{
    for loop
    {
        aRoutineThatUsesNeon(someArguments);
        // some computations
        float a = 3 * b - c / 2;
        // other computations
        // dummyRoutine();
    }
}

Diving into the assembly reveals that 3 * b is miscomputed, since the constant 3 is set to register s22 before the loop, but the invocation of aRoutineThatUsesNeon changed this value so the overall result is wrong.
Apparently the compiler assumes that s22 will not be changed during the loop.
Curiously, if the call to dummyRoutine is uncommented, the problem goes away (this is my current workaround). 

More Info
The problem happens in release mode only.
The NDK version is r9d, running on Windows 8, 64 bit.
aRoutineThatUsesNeon uses the NE10 library.

Questions
Is it a problem with the compiler - too aggressive optimization, or with the called routine - messing up with s22 register, or some other reason?
How to solve the problem other than calling dummyRoutine?

Thanks.

David Turner

unread,
Jun 2, 2014, 11:21:12 AM6/2/14
to andro...@googlegroups.com
On Mon, Jun 2, 2014 at 11:37 AM, ujamshy via android-ndk <andro...@googlegroups.com> wrote:
Hi,

I have a problem that causes a register to get corrupted and eventually a wrong result.

Description
The routine that delivers wrong results goes like:

someRoutine()
{
    for loop
    {
        aRoutineThatUsesNeon(someArguments);
        // some computations
        float a = 3 * b - c / 2;
        // other computations
        // dummyRoutine();
    }
}

Diving into the assembly reveals that 3 * b is miscomputed, since the constant 3 is set to register s22 before the loop, but the invocation of aRoutineThatUsesNeon changed this value so the overall result is wrong.

This aRoutineThatUsesNeon should not change the value of the register upon return. Per the ARM ABI, only registers d0-d7 (a.k.a. s0-s15) can be modified by a called routine, anything else should be preserved by the callee.

What is the content of this routine? Was it compiled with GCC or is it hand-written assembly? In the latter case, it's probably buggy and should preserve the register values. In the former case, that might be a GCC bug and it would be interesting to see the actual source code and generated assembly for it to reproduce this (it would also be very unfortunate :-( ).

Apparently the compiler assumes that s22 will not be changed during the loop.

Yes, this is correct. 
 
Curiously, if the call to dummyRoutine is uncommented, the problem goes away (this is my current workaround). 

It might be that the call prevents the compiler from making assumptions that lead him to host the constant 3 into 's22'.

Hope this helps
 
More Info
The problem happens in release mode only.
The NDK version is r9d, running on Windows 8, 64 bit.
aRoutineThatUsesNeon uses the NE10 library.

Questions
Is it a problem with the compiler - too aggressive optimization, or with the called routine - messing up with s22 register, or some other reason?
How to solve the problem other than calling dummyRoutine?

Thanks.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/d/optout.

uja...@yahoo.com

unread,
Jun 8, 2014, 8:04:49 AM6/8/14
to andro...@googlegroups.com
 I posted a reply a few days ago but somehow it got lost.

The routine is written in C, however it uses routines from the NE10 library, some written in assembly.
I suspect that the problem is there.- I will look into it.

Your answer was most helpful,

Thanks,


Reply all
Reply to author
Forward
0 new messages