<div dir="ltr"><div style>After looking at the Livermore for a while, we found the issue that was causing LTO to produce a different result.<br></div><div><br></div>Consider the code below [1]. setup() doesn't touch bar/baz, main() doesn't reference foo. LTO finds, correctly, that it can remove the setup(), but the result is different.<div>
<br></div><div style>The code is clearly wrong, but the compiler has no right to fix user's stupidity, even at that level. The only think I can think that would mitigate the problem would be to have a warning or out-of-bounds access when it's that obvious.</div>
<div style><br></div><div style>Clang folks,</div><div style><br></div><div style>Correct me it I'm wrong, but I humbly think this should be done in the front-end, so it's easy to print out line information without requiring debug symbols to be present. How easy would be to do that in Clang's AST?</div>
<div style><br></div><div style>I assume it's only a matter of checking the stride against the bounds of the array, if all is knowns at compile time. That would also help against segmentation fault.</div><div style><br>
</div><div style>It wouldn't, however, work with variable length arrays, but VLA tend to segfault more often than silently overwrite other global variables.</div><div style><br></div><div style>cheers,</div><div style>
--renato</div><div><br></div><div>[1]</div><div><div>#include <stdio.h></div><div><br></div><div>struct {</div><div> int foo[20];</div><div> int bar[20];</div><div> int baz[20];</div><div>} S;</div><div><br></div>
<div>void setup() {</div><div> int i;</div><div style> // OVERWRITES ALL THREE ARRAYS</div><div> for (i=0; i<60; i++)</div><div> S.foo[i] = i; // ONLY REFERENCES FOO</div><div>}</div><div><br></div><div style>// DOESN'T USE FOO</div>
<div>int main() {</div><div> int i;</div><div> setup();</div><div> printf("Bar: ");<br></div><div> for (i=0; i<20; i++)</div><div> printf("%d ", S.bar[i]);</div><div> printf("\nBaz: ");</div>
<div> for (i=0; i<20; i++)</div><div> printf("%d ", S.baz[i]);</div><div> puts("");</div><div><br></div><div> return 0;</div><div>}</div></div><div><br></div></div>