Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) Setting Compiler Optimizations (Read 12317 times)
Richard Arndt
Junior Member
**
Offline


Posts: 25
Joined: May 3rd, 2014
Setting Compiler Optimizations
Sep 14th, 2016 at 6:12pm
Print Post  
I teach Microcontrollers in a university environment. I migrated from Atmel Studio not using Visual Micro, to Visual Studio using Visual Micro in my course Jan 2016.  I had ported Arduino Core code as a library under Atmel Studio 6.0.  Atmel Studio is just a wrapper around Visual Studio DLL's.  I had sooo many problems with AS, like restarting unexpectedly, which lost code changes, or corrupted solution/project files.   

I have found that Visual Studio is far more stable than Atmel Studio. And I love the fact that Visual Micro opens the doors to any Arduino IDE compatible hardware platform, where Atmel Studio only supports Atmel products.

However, what  I am missing with VM is the ability to set compiler optimization levels for debug/release modes.  I had a code profiling project on Atmel Studio where I could easily demonstrate the impact on code/data size and code speed between various optimization levels, and even demonstrate how to write faster 'C' by utilizing proper data types and coding techniques for the specific CPU.  This was an eye opener for the students.  However, I can not find a GUI method to set optimization levels using Visual Micro.

I can make changes to Visual Studio project properties manually, but they appear to get overwritten.  And for some reasons many of my students don't have the "Project->Properties" showing up.  In addition, regardless of choosing a debug or release build, there appears to be no difference in code/data size, or measurable differences in code speed.  So I am gathering VM always has the same optimization level turned on, regardless of debug/release build.

Am I missing something?  Is there a way to change optimization levels for the project, libraries, core code?

Rich
« Last Edit: Sep 14th, 2016 at 6:15pm by Richard Arndt »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Setting Compiler Optimizations
Reply #1 - Sep 14th, 2016 at 6:27pm
Print Post  
Hi,

Your students needs to ensure the project is selected i the solution explorer before the properties will show. The vMicro>project properties command attempts to select the project then show the properties.

The properties window is a standard VS feature for every file. You see different properties depending on what is selected in the solution explorer so to say there are no properties is confusing. Even without Visual Micro there should still e some properties showing so please look into that to find the confusion.

Arduino doesn't have debug (not easily anyway) and the visual micro debugger uses Serial alongside a standard Arduino build so we would not expect to see any speed difference.

You havn't said which hardware you are using so I will assume AVR

The compiler properties are taken from the standard arduino system which you can see in arduinoide\hardware\arduino\avr\platform.txt

For example you can see this line in the file:-

Code
Select All
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto
 



Notice that the optimization is hard coded so I am considering a "replacement" system that will switch out the -Os based on user selected optimization settings.

The issue is compounded because the arduino fails to build for most hardware with optimization switched off so I am considering providing different optimization settings for project/core and library compilation.

None of that helps you right now so I suggest editing the platform.txt then clicking "tools>visual Micro>reload toolchains" or restarting the ide.

Does that help?
  
Back to top
IP Logged
 
Richard Arndt
Junior Member
**
Offline


Posts: 25
Joined: May 3rd, 2014
Re: Setting Compiler Optimizations
Reply #2 - Sep 14th, 2016 at 7:40pm
Print Post  
I teach primarily Arduino UNO.  In respect to project properties... they do exist.  What I was referring to was the "C/C++" properties of the properties page.  This section of the properties does not show up on many of my projects.  I have attached PIC of two projects, one with and one without.  I do not recall how I created these projects, whether it was an add new project from the "Solution Explorer->Solution" window, or from "VMicro->NewArduinoProject" tab.  But seems neither right now will show a C/C++ Properties in the properties window.  It may be that an update, older to newer VM, may have caused this?  In either case, I currently have VM projects that show C/C++ and some that don't (see attachments).

For the projects that do show the C/C++ property section, I can go in and change the "Optimization" level and set "Whole Program Optimization" to YES, and get the following for debug then release.

/od
Program size: 5,840 bytes (used 18% of a 32,256 byte maximum) (8.60 secs)
Minimum Memory Usage: 1287 bytes (63% of a 2048 byte maximum)

/o2
Program size: 5,490 bytes (used 17% of a 32,256 byte maximum) (5.23 secs)
Minimum Memory Usage: 1218 bytes (59% of a 2048 byte maximum)

I do not know what was included in this optimization, whether it was just the project source code, or if it also include Arduino Core or libraries.

However, if I reboot Visual Studio, debug/release are both set to /0d (disabled).

Rich
« Last Edit: Sep 14th, 2016 at 7:57pm by Richard Arndt »  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Setting Compiler Optimizations
Reply #3 - Sep 14th, 2016 at 8:03pm
Print Post  
Oh I see. Yes it's confusing in VS there are "Property Pages" and "Properties"

The property pages show as a dialog. The project properties show as a dockable tool window

Property Pages (Consider as read-only)

Microsoft for some reason unknown to me only show the Property Pages if at least one .cpp file is included in the project.

I am sure that is the confusion for students it confused me at first!

You should also know that not one property in the property pages affect the compilation of an arduino/gcc program. VS does not know about Arduino or Gcc and does not provide a compiler for it. Therefore Visual Micro uses the property pages ONLY to force intellisense to work.

Visual Micro entirely handles the build process based on the arduino definitions mentioned previous (platform.txt and also boards.txt) which is an alien system for Visual Studio (VS)

Project Properties - Property Window

Visual Micro provides a number of compiler and linker options via the Project Property (tool window) system. The Visual Micro project properties extend the Arduino build process in various small ways.

When a project is open in Visual Studio the vMicro menu shows a "Project Properties" command which actually just selects the project in the solution explorer and then automatically clicks the standard Visual Studio command View>Property Window.

If you take a look at the Visual Micro documentation index the "Project Properties" is available as a link which opens this page:-

http://www.visualmicro.com/page/User-Guide.aspx?doc=Project-Properties-Reference...

The page describes how to add macros and additional compiler switches. 

Be aware that manually adding -I include paths with a -I switch is not recommended because it will confuse less experienced users. This is because adding a -I switch simply adds an include path for the compiler but does not cause the .cpp files in the path to be compiled.


Compiler switches

I don't think adding an optimization compiler switch via Project Properties will have any effect because of what I said earlier about platform.txt but you can try. Otherwise you have to edit platfom.txt for your demo.

To see and understand more clearly what happens during an Arduino compile and to confirm if your Project Property overrides have been applied you can enable the vMicro>Compiler>Verbose option. To lean about all the Arduino build properties also switch on "vMicro>Compiler>Show Build Properties". These properties will give a verbose compiler output of the entire process.

Does this help?
« Last Edit: Sep 14th, 2016 at 8:39pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Richard Arndt
Junior Member
**
Offline


Posts: 25
Joined: May 3rd, 2014
Re: Setting Compiler Optimizations
Reply #4 - Sep 14th, 2016 at 8:44pm
Print Post  
Thank you for you prompt response today  Smiley  On some blogs it is not unusual to never get a response  Undecided

I will try to duplicate the optimization learning experience using your alternate suggestion.

By the way... Visual Micro... is awesome.  Keep up the good work!

Rich
« Last Edit: Sep 14th, 2016 at 8:44pm by Richard Arndt »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Setting Compiler Optimizations
Reply #5 - Sep 14th, 2016 at 8:46pm
Print Post  
Thanks!
  
Back to top
IP Logged
 
Richard Arndt
Junior Member
**
Offline


Posts: 25
Joined: May 3rd, 2014
Re: Setting Compiler Optimizations
Reply #6 - Sep 14th, 2016 at 9:07pm
Print Post  
One note I left off FYI... The "CodeProfiler" project I have been testing under VM does not utilize any CPP project code or Arduino user libraries, its just the INO.  Yet it still shows the C/C++ properties tab.  I did turn the compiler verbose output on, and all the files for this project were still compiled -Os, regardless of the /Od or /O2 optimization setting.  So you are correct, and the difference in code/data size remains a mystery!  This is just FYI... no response needed.  Thanks for you help.
« Last Edit: Sep 14th, 2016 at 9:08pm by Richard Arndt »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Setting Compiler Optimizations
Reply #7 - Sep 14th, 2016 at 9:39pm
Print Post  
Quote:
The "CodeProfiler" project I have been testing under VM does not utilize any CPP project code or Arduino user libraries, its just the INO.  Yet it still shows the C/C++ properties tab


Maybe some inconsistency in the project creation routine. When you use File>Open>Arduino Project to open an existing project Visual Micro probably sets the .ino to be a C++ type. You can see and change the type of a file in the File Properties. 

The File properties work the same way as the Project Properties. When project properties are open you will see the properties change as you click between Project and .ino file in the solution explorer. So the same tool window simply shows properties for whatever is selected in the Solution Explorer.

Quote:
So you are correct, and the difference in code/data size remains a mystery


If vMicro>Automatic Debugging is enabled I would expect to see a difference in code size between Debug and Release. The difference depends on what the project code does.

The Visual Micro debugger has to inject Serial statements into a temp copy of the code prior to compilation. That's how the debugger works because Arduino doesn't have easy debug through normal means.

If the project does not use Serial then using the debugger will cause the memory to be used that Serial normally requires and will cause the compile and linker to use the parts of the Arduino core required for Serial communications such as HardwareSerial.cpp.

If the project already uses Serial the difference between Debug and Release will be significantly smaller.

If you see no difference at all between debug and release, and never see "Compiling Debug Versions of 'xyz'" then the debugger is either disabled or you are using the free version of Visual Micro.

If you follow the guide above to alter platform.txt and change the optimization from -Os to your preferences then use "tools>visual Micro>reload tool chains" then you should see a difference. If not then click "Build>clean solution"and then build again. the "clean" simply clears temp build files so you get a full compile but VM should detect the change to platform.txt.

You can also experiment with the extraFlags, extraCppFlags or extraElf2HexFlags etc.in the Visual Micro Project Properties as described above. 

As an aside, you can also set #defines in the properties based on configuration (release/debug) or globally for the project.
« Last Edit: Sep 14th, 2016 at 9:41pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Richard Arndt
Junior Member
**
Offline


Posts: 25
Joined: May 3rd, 2014
Re: Setting Compiler Optimizations
Reply #8 - Sep 15th, 2016 at 8:19pm
Print Post  
That makes total sense Tim.  Thanks.

For my purpose, I have found that overriding compiler options within the project code (.ino) is sufficient to demonstrate the effect of compiler optimizations (-O0, -O1, -O2, -Os, etc.).  The example below overrides the Arduino -Os optimization, setting it to disabled -O0.  This is a more direct approach for students to twiddle with, than to mess with the platform.txt file.

#pragma GCC push_options
#pragma GCC optimize ("O0")

/**** code here ****/

#pragma GCC pop_option

Rich
« Last Edit: Sep 15th, 2016 at 8:20pm by Richard Arndt »  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Setting Compiler Optimizations
Reply #9 - Sep 15th, 2016 at 9:36pm
Print Post  
Oh yes good idea

By the way I tested the .ino file type =C++ today and the C++ options did not show.

Is it possible your test project had a c++ file in it at some point in the past that you have since deleted?I can only assume VS remembers that fact.
  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint