AnsweredHot!How to prevent optimization of a variable

Author
grambo
Elite code monkey
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2017/01/04 11:38:49
  • Location: near Manchester NH, USA
  • Status: offline
2017/09/12 11:01:12 (permalink)
0

How to prevent optimization of a variable

I've had good luck with:
volatile uint16_t keep_this_var = 0;
uint16_t foo(const uint16_t bar)
{
    keep_this_var = bar >>1;
    return (keep_this_var);
}

...but not with this:
uint16_t foo2(const uint16_t bar)
{
     volatile uint16_t didnt_keep_this_var;
     didnt_keep_this_var = bar >>1;
     return (didnt_keep_this_var);
}

In the latter case the volatile defined inside the function can get optimized out quite often.  This is the super-simple case, the case that I end up angry about is if I'm in the middle of a large switch statement and I have a var local to a single case which can get nuked.  Is there any kind of a:
volatile uint16_t __really_keep_this_var__ my_debugging_var;

that will prevent the compiler from optimizing out "my_debugging_var"?
#1
Jim Nickerson
User 452 _
  • Total Posts : 4033
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:08:09 (permalink) ☼ Best Answerby grambo 2017/09/12 12:37:59
+2 (2)
Have you added static to the variable within the function ?
#2
Michael.W.Mann
Super Member
  • Total Posts : 217
  • Reward points : 0
  • Joined: 2011/01/24 09:58:24
  • Location: Chandler, Arizona
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:23:25 (permalink)
-1 (1)
I'm guessing you want the compiler to leave variables in to help debug the code.  In that case, you might consider changing the compiler optimization for the .c file to optimization Zero.  This will allow easier debugging and will keep variables in the code that might be eliminated under higher optimization levels.
 
To do this:
Do a right mouse click on the file.  Choose Properties.  Click on Override Build Options.  Select the compiler.  Under the Optimization category change optimization to Zero.
 
When done debugging you can uncheck Override Build Options for the file.

Michael W. Mann
Principal Applications Engineer
MCU32 Applications, Microchip
#3
aschen0866
Super Member
  • Total Posts : 4113
  • Reward points : 0
  • Joined: 2006/01/08 22:18:32
  • Location: San Diego
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:26:47 (permalink)
-1 (1)
grambo
 
...but not with this:
uint16_t foo2(const uint16_t bar)
 
{
 
     volatile uint16_t didnt_keep_this_var;
 
     didnt_keep_this_var = bar >>1;
 
     return (didnt_keep_this_var);
 
}

In the latter case the volatile defined inside the function can get optimized out quite often.  ...


You need to generate a demo project and report to the tech support. If the XC32 compiler does what you said, it would be a compiler bug because the compiler does not respect the "unknown side effect" part of ANSI C volatile requirement. 
#4
grambo
Elite code monkey
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2017/01/04 11:38:49
  • Location: near Manchester NH, USA
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:30:13 (permalink)
+1 (1)
@Michael, good thought, but if the code doesn't fit in the device without optimization=2, then that's a non-starter
 
@Jim good call, "static volatile" will do the trick, either keyword by themselves will not prevent the compiler from optimizing it out:
Declaring it "volatile" gives me an "out of scope" message for that variable
Declaring it "static" gives me a clever "- Unavailable (Optimized?) -" message for that var.
 
 
#5
andersm
Super Member
  • Total Posts : 2174
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:42:39 (permalink)
+1 (1)
grambothe case that I end up angry about is if I'm in the middle of a large switch statement and I have a var local to a single case which can get nuked.

If the variable is no longer in scope, it should be destroyed. If you want it to have a longer lifetime, move it to an outer scope.
#6
grambo
Elite code monkey
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2017/01/04 11:38:49
  • Location: near Manchester NH, USA
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:44:31 (permalink)
0
aschen0866
You need to generate a demo project and report to the tech support. If the XC32 compiler does what you said, it would be a compiler bug because the compiler does not respect the "unknown side effect" part of ANSI C volatile requirement. 



I think it's a nuance (and a nuisance) of what "volatile" means...  In my case I'm performing some bitwise operations on a member of a volatile instance of a struct.  So I think it's OK for the compiler to nuke my local reference if it's using something that's already enforced as volatile.  (OK from a rule standpoint, still annoying to me)  The trick with the "static volatile" combo is that the static forces a memory location to be assigned, and then volatile makes this new copy with a known location adhere to the volatile rules.
 
...someone with insight into the compiler should chime in, but this is my current understanding.
#7
grambo
Elite code monkey
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2017/01/04 11:38:49
  • Location: near Manchester NH, USA
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/12 11:46:41 (permalink)
+1 (1)
andersm
If the variable is no longer in scope, it should be destroyed. If you want it to have a longer lifetime, move it to an outer scope.

That is the case, but if I hit a breakpoint within the scoping area (i.e., on the line where it is used in an "if"), I want to see it.  Using "static" xor "volatile" does not get the job done.
#8
du00000001
Just Some Member
  • Total Posts : 1170
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: How to prevent optimization of a variable 2017/09/12 12:12:31 (permalink) ☄ Helpfulby grambo 2017/09/12 12:37:09
+2 (2)
This is how these RISCy compilers work. volatile is not intended to make some variable 'sticky' - just to make sure it's read every time it's tested. IN the second case it is absolutely clear that no side-effect could happen - thus the variable is implemented as "register-only". (No - it is not gone or "optimized away". It's simply not available "memory-mapped".)
I admit that it is a bit annoying to debug such code (always with the assembly listing aside). But it is not impossible.

Most times the bug is in front of the keyboard.
#9
jdeguire
Super Member
  • Total Posts : 346
  • Reward points : 0
  • Joined: 2012/01/13 07:48:44
  • Location: United States
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/13 07:26:49 (permalink)
+1 (1)
Try adding "__attribute__((optimize(0)))" to the function's declaration to have remove optimization only for that function.
#10
grambo
Elite code monkey
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2017/01/04 11:38:49
  • Location: near Manchester NH, USA
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/13 07:33:27 (permalink)
0
@jdeguire that may work for functions, but it does not  work for variables.  Compiled OK, but I get "Out of Scope" in the watch window.
#11
du00000001
Just Some Member
  • Total Posts : 1170
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: How to prevent optimization of a variable 2017/09/13 08:24:05 (permalink)
0
"Out of Scope" - as the registers are not memory-mapped.
While the watch-window works exclusively via memory addresses. That's reality sad

Most times the bug is in front of the keyboard.
#12
NKurzman
A Guy on the Net
  • Total Posts : 14678
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: How to prevent optimization of a variable 2017/09/13 08:29:50 (permalink)
0
Out of Scope is more complicated.  the "elf file" gives MplabX info on when it is the variable.  This could be a single line.  The Number is still in RAM, but MplabX will not display it.  This "solves" the Issue of showing a RAM location that now has a different value stored in it.  But creates your problem.  Static should solve this.
#13
du00000001
Just Some Member
  • Total Posts : 1170
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: How to prevent optimization of a variable 2017/09/13 08:45:13 (permalink)
+1 (1)
@ NKurzman
The .elf may be the source of information, but is is that simple:
fro each "symbol" to inspect (jump-targets, functions, variables and many, many other things), the name is associated with some address. If something doesn't have an address (registers in the RISCy machines that do not have some memory address): no way to show in the watch window. And try to get some address-of.
 
BWT:   m a y b e   it is possible to brute the compiler into memory-mapping the variable:
what about a second (pointer) variable that's accessing the VariableOfInterest ?
ptr_to_var = &VariableOfInterest;

Might be worth to try...

Most times the bug is in front of the keyboard.
#14
Jump to:
© 2017 APG vNext Commercial Version 4.5