XC Compiler Optimizations

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
Jerry
Member++
Posts: 30
Joined: Sat Dec 12, 2009 7:14 pm
Location: Silicon Valley

XC Compiler Optimizations

Post by Jerry »

I'm building a simple application to "Release". The optimization level is set to -02 (generally the most I consider safe). When looking at the disassembled code in the debugger I see this:

Code: Select all

0x000100b2 <main+6>:  ldw   (ru6)       r0, sp[0x0]
0x000100b4 <main+8>:  setc  (ru6)      res[r0], 0x1 *
0x000100b6 <main+10>: ldw   (ru6)       r0, sp[0x0]
0x000100b8 <main+12>: in    (2r)         r0, res[r0] *
0x000100ba <main+14>: stw   (ru6)       r0, sp[0x1]
0x000100bc <main+16>: ldw   (ru6)       r0, sp[0x1]
0x000100be <main+18>: stw   (ru6)       r0, sp[0x1]
This code corresponds to:

Code: Select all

	timer tmr;
	unsigned t;

	tmr :> t;
Why all the redundant loads and stores, especially the last three?


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

Post by segher »

The option is -O2 (big letter Oh), not -02 (digit zero).


Segher
User avatar
Jerry
Member++
Posts: 30
Joined: Sat Dec 12, 2009 7:14 pm
Location: Silicon Valley

Post by Jerry »

That was a typo in my post. I do have the optimization setting set to -O2 (capital Oh).

My original question still stands: with this level of optimization turned on, why is the compiler generating so many redundant load/store instructions?
User avatar
Jamie
Experienced Member
Posts: 99
Joined: Mon Dec 14, 2009 1:01 pm
Contact:

Post by Jamie »

Code: Select all

int main() {
    timer tmr;
    unsigned t;
    tmr :> t;
    return 0;
}
Compiling the above with:

Code: Select all

xcc -O2 -S test.xc -o test.s
Gives you:

Code: Select all

main:
          getr      r1, 0x1
.L0:
          setc      res[r1], 0x1
.L4:
          in        r0, res[r1]
.L1:
          ldc       r0, 0x0
          freer     res[r1]
.L3:
          retsp     0x0
Which seems fine to me!
User avatar
Jerry
Member++
Posts: 30
Joined: Sat Dec 12, 2009 7:14 pm
Location: Silicon Valley

Post by Jerry »

That's what I would have expected. Perhaps I'm not settings optimizations properly. Here's how I'm doing it in the IDE:

1. Right-click on project name in the Project Explorer.
2. Choose Properties from the context menu.
3. Select C/XC Build->Settings
4. Click on XC Compiler->Optimization.
5. In the Optimization Level drop-down box, choose "Optimize more (-O2)"
6. Click "OK"
7. Clean project.
8. Build project (choosing Release rather than Debug).
tyler
Member
Posts: 15
Joined: Tue Jul 13, 2010 12:42 pm

Post by tyler »

Are you making sure to change optimization settings for the "Release" build configuration, as that is the configuration under which you are building?
[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo *Click*
User avatar
Jerry
Member++
Posts: 30
Joined: Sat Dec 12, 2009 7:14 pm
Location: Silicon Valley

Post by Jerry »

When I look into Debug Configurations I see that I have "Build Configuration" set to "Release". Yet if I delete the .xe file from the "Debug" folder, clicking on Debug results in a "Program file does not exist".

Why, if I have the Build Configuration set to Release in the Debug Configuration, does the debugger look for the binary in the Debug folder? Shouldn't it look for in in Release folder?
tyler
Member
Posts: 15
Joined: Tue Jul 13, 2010 12:42 pm

Post by tyler »

Not answering your question directly -- but I sometimes find that the IDE will run the wrong executable.

For example, my code will be in revision 1.1 and I can compile and run in Debug. Then make changes, say up to version 1.2. Switch to release, compile, and then run. After much confusion, realize that v1.1 debug-compiled code is running.

Cleaning the project does not fix this. I used to clean, then delete the Release and Debug folders within the IDE view, then rebuild. Now, I just never switch from Release to Debug or vice versa, I just pick a configuration and stay with it.

Also, the compiler thinks it is running the correct executable -- and will point to source code as it debugs that has nothing to do with the code actually running (extremely confusing -- as during the line "x++;" you'll notice in the watch window that some other variable has now changed).
[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo *Click*
User avatar
paul
XCore Addict
Posts: 169
Joined: Fri Jan 08, 2010 12:13 am
Contact:

Post by paul »

tyler wrote:Not answering your question directly -- but I sometimes find that the IDE will run the wrong executable.

For example, my code will be in revision 1.1 and I can compile and run in Debug. Then make changes, say up to version 1.2. Switch to release, compile, and then run. After much confusion, realize that v1.1 debug-compiled code is running.
This is because when you hit "Run" eclipse creates a run configuration for the binary that it finds first and runs it. It then remembers that configuration and will build it whenever you hit run. To ensure you are running a binary in a different configuration you need to create a separate "run configuration" for release and debug.

Check out section 4.3 of the 10.4 tools user guide
Paul

On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Post Reply