NO

Author Topic: New compiler switch ... Enable Stack Optimizations  (Read 5017 times)

CommonTater

  • Guest
New compiler switch ... Enable Stack Optimizations
« on: December 19, 2012, 10:58:35 AM »
In the current compiler setup stack optimizations are enabled whenever any other optimization is turned on, but disabled when optimizations are off.
 
It would be very helpful to have the stack optimizations enabled by a separate compiler switch so they can be controlled independently of other optimization strategies.
 
 

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: New compiler switch ... Enable Stack Optimizations
« Reply #1 on: December 19, 2012, 01:15:51 PM »
Tater we forget about the stack check probe:
Code: [Select]
#pragma check_stack( [ { on | off } ] ) Activable also with /Gs switch on MS compiler.
The point is that it shouldn't be available for 64bits. Maybe it could be usefull.
« Last Edit: December 19, 2012, 01:24:35 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: New compiler switch ... Enable Stack Optimizations
« Reply #2 on: December 19, 2012, 02:34:16 PM »
Thanks Frankie... it is at least worth experimenting with. 
 
But from the description in the help file I don't think it does quite the same thing as what I'm asking about...

With all optimizations off, every variable in a function, regardless of scope, occupies it's own stack space for the duration of the function.  This, in effect, transforms complex functions with inner scopes into standard functions, as far as the stack is concerned. (And don't forget that bracketing used with if, for, while, etc. constitutes a scope)

With any of the optimizations on (Os, Ot or Ox) the compiler switches gears and stack space for nested scopes is reused after the scope exits.  The "big enough for the biggest" blob of stack memory is cleared at the end of the function.

What I'm suggesting is that these two stack strategies should be managed by a separate compiler switch so we can have more control over how the compiler manages the stack. Or, perhaps, the second strategy should be "always on".



« Last Edit: December 19, 2012, 02:42:35 PM by CommonTater »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: New compiler switch ... Enable Stack Optimizations
« Reply #3 on: December 19, 2012, 02:44:33 PM »
Yes Tater, but at least informs you of stack problems.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

CommonTater

  • Guest
Re: New compiler switch ... Enable Stack Optimizations
« Reply #4 on: December 19, 2012, 03:01:41 PM »
Yes Tater, but at least informs you of stack problems.

Yes, there is that....

Below is the result of this source code...
Code: [Select]
#include <stdio.h>
typedef struct t_test
  {
    char str[256];
    int x;
  } test;
 
typedef struct t_test2
  {
    int x;
    char str[256];
  } test2;
 

int _cdecl main (void)
  {
   
    puts("Hello");
    {
      test t = {0};
      t.x = 42;
      printf("%p\n", &t);
    }
    {
      test2 u = {0};
      u.x = 13;
      printf("%p\n", &u);
    }

    puts("bye");
    return 0;
  }

The left section is no optimizations, no stack check.
The center one is with no optimizations and stack check enabled.
The right one is with optimizations (any optimization) on and no stack check
 
As you can see the only one that reuses stack space is the +optimizations on case, which further fortifies my request for a new compiler switch... We should be able to turn on stack optimizations even if not using any other...
 
EDIT: Or, put another way... turning size and speed optimizations off should not cause a program to crash it's own stack.
« Last Edit: December 19, 2012, 04:15:23 PM by CommonTater »