<div dir="ltr"><div>Your option 3 is the preferred way to handle your example, because it allows one to reason about the behaviour of the program. As a bonus, it does so without interfering in any way with optimisation.</div><div><br></div><div>What most users really want - and what many of them /expect/ even though it's currently not actually the case - is for C to behave like high-level assembly language, for code to translate straightforwardly into machine operations. Yes, it would be nice to get consistent behaviour between compilers. But that's not going to happen. Failing that, given that there is no way to improve all compilers, improving one compiler would be much better than nothing.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 1, 2015 at 5:58 PM, Renato Golin <span dir="ltr"><<a href="mailto:renato.golin@linaro.org" target="_blank">renato.golin@linaro.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 1 July 2015 at 17:15, Russell Wallace <<a href="mailto:russell.wallace@gmail.com">russell.wallace@gmail.com</a>> wrote:<br>
> I'm proposing that LLVM unilaterally replace most undefined behaviour with<br>
> implementation-defined behaviour.<br>
<br>
</span>That's precisely the problem. Which behaviour?<br>
<br>
Let's have an example:<br>
<br>
struct Foo {<br>
  long a[95];<br>
  char b[4];<br>
  double c[2];<br>
};<br>
<br>
void fuzz(Foo &F) {<br>
  for (int i=0; i<100; i++)<br>
    F.a[i] = 123;<br>
}<br>
<br>
There are many ways I can do this "right":<br>
<br>
1. Only go up to 95, since you're using an integer to set the value.<br>
2. Go up to 96, since char is an integer type.<br>
2. Go all the way to 100, but casting "123" to double from 97 onwards, in pairs<br>
3. Go all the way to 100, and set integer 123 bitwise (for whatever fp<br>
representation that is) from 97<br>
4. Do any of above, and emit a warning<br>
5. Bail on error<br>
<br>
Compilers prefer not to bail on error, since the standard permits it.<br>
A warning would be a good thing, though.<br>
<br>
Now, since it's a warning, I *have* to output something. What? Even<br>
considering one compiler, you'll have to convince *most* <compilerX><br>
engineers to agree on something, and that's not trivial.<br>
<br>
Moreover, this loop is very easy to vectorise, and that would give me<br>
4x speed improvements for 4-way vectorization. That's too much for<br>
compilers to pass.<br>
<br>
If I create a vectorised loop that goes all the way to 92, I'll have<br>
to create a tail loop. If I don't want to create a tail loop, I have<br>
to override 'b' (and probably 'c') on a vector write. If I implement<br>
the variations where I can do that, the vectoriser will be very happy.<br>
People generally like when the vectoriser is happy.<br>
<br>
Now, you have a "safe mode" where these things don't happen. Let's say<br>
you and me agree that it should only go to 95, since this is "probably<br>
what the user wants". But some programmers *use* that as a feature,<br>
and the standard allow it, so we *have* to implement it *both*.<br>
<br>
Best case scenario, you have now implemented two completely different<br>
behaviours for every undefined behaviour in each standard. Worse<br>
still, you have divided the programmers in two classes: those that<br>
play it safe, and those that don't, essentially creating two different<br>
programming languages. Code that compiles and work with<br>
compilerA+safe_mode will not necessarily compile/work with<br>
compilerB+safe_mode or compilerA+full_mode either.<br>
<br>
C and C++ are already complicated enough, with so many standard levels<br>
to implement (C90, C99, C11, C++03, C++11, C++14, etc) that<br>
duplicating each and everyone of them, *per compiler*, is not<br>
something you want to do.<br>
<br>
That will, ultimately, move compilers away from each other, which is<br>
not what most users really want.<br>
<br>
cheers,<br>
--renato<br>
</blockquote></div><br></div>