Optimization

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
rp181
Respected Member
Posts: 395
Joined: Tue May 18, 2010 12:25 am

Optimization

Post by rp181 »

What are the downsides to maximum compiler optimization, beyond memory usage? I read that in C, optimization can cause an other wise good program to be unreliable. Is this true in XC?

In my program, which is doing some relatively simple calculations, something like rewriting a for loop into inline code (loop of 4) made a huge difference. For 1 cycle, 1480 ns to 970 ns, or about 600kHz to 1 MHz. Obviously, this is very unreadable, and difficult to modify. Is there a compiler option to do this? Is there somewhere I can see all of the options and downsides?


User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

rp181 wrote:What are the downsides to maximum compiler optimization, beyond memory usage? I read that in C, optimization can cause an other wise good program to be unreliable. Is this true in XC?
Different levels of optimisation can cause an incorrect C program (also known as
"not actually a C program at all") to behave differently. A valid C program will
behave identical at all levels of optimisation (that is, identical in the C execution
model: the generated machine code is of course different, so some things can
happen in a different order, and timing differs (of course!), etc.)

Compilers for most languages cannot detect all errors (not even in theory, and
certainly not in practice). XC is one of those languages as far as I can see. So yes,
an XC program that does what you want at -O0 with a certain compiler can do things
you didn't intend at a different optimisation level, with a different compiler, or with a
different version of the same compiler (or with a different moon phase, it is rumoured).
In my program, which is doing some relatively simple calculations, something like rewriting a for loop into inline code (loop of 4) made a huge difference. For 1 cycle, 1480 ns to 970 ns, or about 600kHz to 1 MHz. Obviously, this is very unreadable, and difficult to modify. Is there a compiler option to do this? Is there somewhere I can see all of the options and downsides?
I think you mean something like -funroll-loops (or -funroll-all-loops). I'm not sure
the current XC compiler has that option; the C compiler does though.

".../libexec/xcc1 --help" tells you some (all?) compiler options.
User avatar
rp181
Respected Member
Posts: 395
Joined: Tue May 18, 2010 12:25 am

Post by rp181 »

Ok, thanks. Could you give an example of an error that the compiler wouldn't catch?
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

Here's an artificial example in C (I don't know what the rules here are for XC):

Code: Select all

int i;
for (i = 0; i >= 0; i++) {
  /* stuff... */
}
Without optimisation, this loop will terminate; with optimisation, it might not
(because signed overflow is undefined behaviour, and valid programs do
not contain undefined behaviour). The compiler might or might not detect
you _do_ have undefined behaviour here; in general, it cannot (there is the
halting problem, etc.)
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

segher wrote:I think you mean something like -funroll-loops (or -funroll-all-loops). I'm not sure
the current XC compiler has that option; the C compiler does though.
The XC compiler doesn't have that option but you can use a pragma to unroll specific loops. For example:

Code: Select all

  // Fully unroll loop (assuming number of iterations can be calculated at compile time)
  #pragma loop unroll
  for (i = 0; i < 10; i++) {
    ...
  }
  // Partially unroll loop by a factor of 2
  #pragma loop unroll(2)
  for (i = 0; i < 10; i++) {
    ...
  }
}
I read that in C, optimization can cause an other wise good program to be unreliable. Is this true in XC?
I like the following article on undefined behavior in C and how it can interact with compiler optimizations:

http://blog.regehr.org/archives/213

XC has less cases of undefined behavior than C (for example array bound checks will catch out of bounds memory accesses), but there are still cases where optimizations might change how a program executes (e.g. use of an uninitialized variable).
User avatar
rp181
Respected Member
Posts: 395
Joined: Tue May 18, 2010 12:25 am

Post by rp181 »

Ok thank's segher, that clears it up!

Richard, is there somewhere I can read about #pragma capabilities? Or is it the same as normal C?
richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

The pragmas are documented in the tools user guide (Language Support -> XC, C and C++ -> Pragma Directives).