[LLVMdev] Incorrect loop optimization when building the Linux kernel

Nick Lewycky nicholas at mxc.ca
Sun Dec 7 23:16:57 PST 2014


Chengyu Song wrote:
>> It's difficult to say without a full example, but I'm very suspicious
>> of those global declarations. I think the compiler would be entirely
>> justified in assuming you could *never* get from __start_builtin_fw to
>> __end_builtin_fw, let alone on the first iteration: they're distinct
>> array objects and by definition (within C99) can't overlap.
>
> I think this should be the root cause. Once I changed the declaration to pointers (instead of arrays):
>
> extern struct builtin_fw* __start_builtin_fw;
> extern struct builtin_fw* __end_builtin_fw;
>
> The generated code will not skip the first comparison.
>
> Sadly, Linux kernel may contain more such declarations.

That's a great point. Maybe clang could issue a warning on these, any 
for loop that declares a pointer to the address of a non-weak object, 
compares it to a different object, and increments only the pointer 
(without doing other changes to the pointer inside the loop body). We 
know that's a buggy pattern. Could you file a feature request for this 
on llvm.org/bugs ?

Nick



More information about the llvm-dev mailing list