wrong 64bit function return value with Apple LLVM 3.1 compiler with -mthumb

Originator:syncluz
Number:rdar://11116189 Date Originated:2012-03-25
Status:Resolved Resolved:2012-05-18
Product:XCode Product Version:4.3.2 (4E2002)
Classification:Severe Bug Reproducible:Yes
 
Summary:

XCode Version 4.3.2 (4E2002) with Apple LLVM 3.1 creates corrupted results from C function returning 64-bit integer when compiled with -mthumb (which is the implicit default when setting up a new project in XCode)


Steps to Reproduce:

Compile and run the attached problem demo app as RELEASE for iOS and run it on an ARMv7 device.
See below under notes for the function that fails.


Expected Results:

All three calculations should render the same result (1333152000000000)


Actual Results:

With the following conditions met:
- compiled for ARMv7
- with THUMB enabled (default setting in XCode generated app template or CFlags -mthumb)
- RELEASE build (full optimisation)
- actual function call (inline prevented)

...the function result is severely wrong (1333151995705032)


Regression:

Problem has appeared by upgrading project (which was fine in fall 2011) according to XCode suggestions, which included switching to Apple LLVM 3.1 compiler.
Insofar it is a regression, as this type of miscalculation was caused only by taking the recommended upgrade path to current developer tools and settings.


Notes:

The problem is heavily dependent on the actual calculation done inside the function, and apparently also dependent on the value passed in.

The function is:

signed long long failingWithTHUMB_whenNotInlined(signed long long aInput) 
{
  // shows the problem called as a real function (NOT inlined)
  return (aInput-210866803200000)*1000000ll/1000ll;  
}


- Called with aInput 212199955200000, it should return 1333152000000000 (and does so in non-THUMB, and also when inlined)

- But actually actually called as function (in the sample, I enforced this with  __attribute__ ((noinline)), in the real program the problem appeared caller and callee were in separate .c files), the function returns the completely wrong result 1333151995705032

Comments

Fixed in XCode 4.4 DP 5

Thanks for the update - I verified that the problem as shown by the sampe app is indeed resolved when the app is build with XCode 4.4 DP 5 on 10.8.

So from my point of view this issue is resolved and can be closed.

Fixed in upstream Clang on March 26

http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120326/139715.html

You might want to file this with LLVM: http://llvm.org/bugs/

By sdfisher at March 29, 2012, 3 a.m. (reply...)

Could you post the generated Thumb code?

Same result with LLVM 4.0 from Xcode 4.4 DP 2


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!