Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM loop optimization bug?

In my application I have the following Objective-C code:

-(void)layoutPages
{
    NSMutableArray* sections = [NSMutableArray array];
    [sections addObject:[[NSAttributedString alloc] initWithString:@"Hello world"]];

    for (NSAttributedString* contentSection in sections) {
        NSLog(@"%@",contentSection);
    }
}

enter image description here

Console output: 2014-04-22 14:11:01.505 MyApp[24784:830b] Hello world{}

If I compile for x86_64 architecture using -Os optimization, LLVM then silently optimizes out the loop variable 'contentSection'. When I use -O0, the bug disappears. This is the output when I try to print the description of the contentSection variable:

(lldb) po contentSection
error: Couldn't materialize struct: the variable 'contentSection' has no location, it may have been optimized out
Errored out in Execute, couldn't PrepareToExecuteJITExpression

How is that possible? From my point of view a loop variable should never be optimized out when used inside the loop. I have seen that other people have a similar issue with LLVM but not with a loop variable. Could this be a compiler bug?

like image 482
Sbhklr Avatar asked Apr 22 '14 11:04

Sbhklr


1 Answers

This is probably a compiler settings issue. First you'll want to check that your run scheme is not in release mode. Go to "Edit scheme..." -> "Run" -> "Info" -> "Build Configuration". Make sure the value is set to "Debug".

If that is not the problem, then make sure your debug build settings don't have compiler optimization turned on. Make sure "Optimization Level" is set to "none" for debug. Also make sure there is no other place where the compiler optimization levels might be set, such as in the "Other C Flags" setting.

like image 136
ThomasW Avatar answered Oct 03 '22 21:10

ThomasW