[dmd-internals] Errors emitted by the optimizer and the as-if rule

David Nadlinger code at klickverbot.at
Sat Aug 18 12:01:23 PDT 2012

On Sat, Aug 18, 2012 at 8:38 PM, kenji hara <k.hara.pg at gmail.com> wrote:
> My answer is: optimizer doesn't affect the result of semantic analysis.
> In other words, flow analysis by the optimizer also doesn't affect to
> the compilation.

I think you ares  completely missing the point missing the point. Flow
analysis performed by the optimizer _does_ affect compilation, because
it leads to code being treated as illegal which would be perfectly
fine otherwise. How much bigger of an effect on language semantics can
there be?

> The "used before set"  error disappears if you don't specify -O.
> Therefore it is backend optimizer's work, and is not a part of the
> spec.

Which is a big problem. Whether a program compiles or not should be
clear from the spec, and not be dependent on the compiler writer's
mood. If the optimizer detects undefined behavior, it can enable all
sorts of crazy optimizations – for example, LLVM routinely optimizes
down big chunks of code with just a trap if it detects undefined
values –, but it can't change language semantics: if the spec says
that something is undefined behavior, executing the code results in,
well, undefined behavior, but the code is still perfectly legal, the
compiler should _not_ be allowed to generate an error.

To illustrate my point, consider the following example. It contains
code which exhibits undefined behavior, but since this code is never
invoked, there should be no problem:

void foo() {
   int a = void;
   import std.stdio;
void main() {}


More information about the dmd-internals mailing list