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?
Optimization
-
- Respected Member
- Posts: 395
- Joined: Tue May 18, 2010 12:25 am
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
Different levels of optimisation can cause an incorrect C program (also known asrp181 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?
"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).
I think you mean something like -funroll-loops (or -funroll-all-loops). I'm not sureIn 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?
the current XC compiler has that option; the C compiler does though.
".../libexec/xcc1 --help" tells you some (all?) compiler options.
-
- Respected Member
- Posts: 395
- Joined: Tue May 18, 2010 12:25 am
Ok, thanks. Could you give an example of an error that the compiler wouldn't catch?
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
Here's an artificial example in C (I don't know what the rules here are for XC):
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.)
Code: Select all
int i;
for (i = 0; i >= 0; i++) {
/* stuff... */
}
(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.)
-
- Respected Member
- Posts: 318
- Joined: Tue Dec 15, 2009 12:46 am
The XC compiler doesn't have that option but you can use a pragma to unroll specific loops. For example: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.
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 like the following article on undefined behavior in C and how it can interact with compiler optimizations:I read that in C, optimization can cause an other wise good program to be unreliable. Is this true in XC?
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).
-
- Respected Member
- Posts: 395
- Joined: Tue May 18, 2010 12:25 am
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, is there somewhere I can read about #pragma capabilities? Or is it the same as normal C?
-
- Respected Member
- Posts: 318
- Joined: Tue Dec 15, 2009 12:46 am
The pragmas are documented in the tools user guide (Language Support -> XC, C and C++ -> Pragma Directives).