Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do programmers ensure that compilers create correct code?

Reading this fascinating (and highest voted question) on SO, Why is it faster to process a sorted array than an unsorted array? made me wonder about compiler code correctness.

For example, the answer states that:

Intel Compiler 11 does something miraculous. It interchanges the two loops...

How does a compiler programmer know when it's OK to interchange loops?

And, in general, do they use mathematical proofs to demonstrate conclusions?

How does a compiler programmer know that their compiler will generate correct code? How do they test their conclusion? Do they have to write a test suite that runs the compiler, and checks that the generated code is correct?

like image 227
B Seven Avatar asked Jul 14 '12 02:07

B Seven


1 Answers

How does a compiler programmer know when it's OK to interchange loops?

The compiler runs a series of checks on the code to determine if it's safe to interchange loops. For example, if the code doesn't fully inline, it probably won't be able to interchange the loops. If the code modifies a volatile variable, it won't interchange the loops. If the code stores values that are computed in previous loop iterations, the compiler won't interchange the loops. If they can be sure it's safe because none of these conditions are triggered, the compile can interchange the loops.

And, in general, do they use mathematical proofs to demonstrate conclusions?

No. They just work out an optimization and a set of conservative tests to ensure that optimizations is safe. Over time, they develop more optimizations and more sophisticated algorithms to detect when the optimization is safe even in cases where it's less obvious.

How does a compiler programmer know that their compiler will generate correct code?

They do the best they can. Occasionally they make mistakes. People submit bug reports, and they fix it.

How do they test their conclusion? Do they have to write a test suite that runs the compiler, and checks that the generated code is correct?

They absolutely do use test suites. When a bug is detected in GCC, a test is specifically added to the test suite to make sure that bug is fixed and not re-introduced.

like image 51
David Schwartz Avatar answered Sep 20 '22 12:09

David Schwartz