Jump to content

Boostc 1.6alpha Optimization Bug...


Recommended Posts

If cout exists in a line, the entire line is optimized out instead of just the call to cout.

 

example code:

 

#include <system.h>

 

char globalCount = 0;

 

char DoSomething(char aValue)

{

globalCount = aValue + 1;

return 'A';

}

 

void main()

{

char myValue = globalCount;

cout << DoSomething(myValue);

if (0 == myValue)

{

/* whoops, call to DoSomething

was incorrectly optimized out. */

}

else

{

myValue = globalCount;

}

}

Link to post
Share on other sites

SnakeByte,

 

BoostC is not a C++ compiler, its a C compiler.

cout is an instance of a C++ class, so can't exist in BoostC project.

 

Consider the following

 

int glbcnt = 0;

 

char fooy()

{

glbcnt++;

return 1;

}

 

void main()

{

char y;

x << fooy();

}

 

 

The value resulting from x << fooy(); is not used so it is optimised out.

 

 

So I see the real two actual problems here as:

1) The optimisation has cause the function to not get called.

2) cout doesn't exist, therefore the compiler should generate a warning.

 

 

Regards

Dave

Link to post
Share on other sites

Hey Dave,

 

Thanks for responding.

 

Yeah, I though it was weird that:

A: it would optimize code out that it shouldn't even know about

(It should have stated that cout was undeclared)

and

B: it would optimize out the entire line.

 

I re-wrote my example and defined cout to be a char, and still I got:

 

compilertest2.c(15:7): warning: expression was optimized out

 

This is valid ANSI C code, right? gcc -ansi didn't complain from linux.

 

#include <system.h>

 

char globalValue = 0;

 

char DoSomething(char aValue)

{

globalValue = aValue + 1;

return 'A';

}

 

void main()

{

char myValue = globalValue;

int cout;

cout << DoSomething(myValue);

if (0 == myValue)

{

/* whoops, call to DoSomething

was incorrectly optimized out. */

}

else

{

myValue = globalValue;

}

}

 

 

SnakeByte,

 

BoostC is not a C++ compiler, its a C compiler.

cout is an instance of a C++ class, so can't exist in BoostC project.

 

Consider the following

 

int glbcnt = 0;

 

char fooy()

{

    glbcnt++;

    return 1;

}

 

void main()

{

    char y;

    x << fooy();

}

 

 

The value resulting from x << fooy(); is not used so it is optimised out.

 

 

So I see the two real two actual problems here as:

1) The optimisation has cause the function to not get called.

2) cout doesn't exist, therefore the compiler should generate a warning.

 

 

Regards

Dave

Link to post
Share on other sites
...

A: it would optimize code out that it shouldn't even know about

(It should have stated that cout was undeclared)

and

B: it would optimize out the entire line.

 

I re-wrote my example and defined cout to be a char, and still I got:

 

compilertest2.c(15:7): warning: expression was optimized out

 

This is valid ANSI C code, right?  gcc -ansi didn't complain from linux.

 

#include <system.h>

char globalValue = 0;

char DoSomething(char aValue)
{
       globalValue = aValue + 1;
       return 'A';
}

void main()
{
       char myValue = globalValue;
       int cout;
       cout << DoSomething(myValue);
       if (0 == myValue)
       {
               /* whoops, call to DoSomething
                  was incorrectly optimized out. */
       }
       else
       {
               myValue = globalValue;
       }
}

 

The expression "cout << DoSomething(myValue);" doesn't have any destination and doesn't change any variable in the code. That's why compiler optimizes it out. This optimization happens at parsing stage when variables aren't known yet. This is why you don't get any error if cout isn't declared.

 

Regards,

Pavel

Link to post
Share on other sites
...

A: it would optimize code out that it shouldn't even know about

(It should have stated that cout was undeclared)

and

B: it would optimize out the entire line.

 

I re-wrote my example and defined cout to be a char, and still I got:

 

compilertest2.c(15:7): warning: expression was optimized out

 

This is valid ANSI C code, right?  gcc -ansi didn't complain from linux.

 

#include <system.h>

char globalValue = 0;

char DoSomething(char aValue)
{
       globalValue = aValue + 1;
       return 'A';
}

void main()
{
       char myValue = globalValue;
       int cout;
       cout << DoSomething(myValue);
       if (0 == myValue)
       {
               /* whoops, call to DoSomething
                  was incorrectly optimized out. */
       }
       else
       {
               myValue = globalValue;
       }
}

 

The expression "cout << DoSomething(myValue);" doesn't have any destination and doesn't change any variable in the code. That's why compiler optimizes it out. This optimization happens at parsing stage when variables aren't known yet. This is why you don't get any error if cout isn't declared.

 

Regards,

Pavel

 

Hey Pavel,

 

There's a bug in the example code above, but regardless, DoSomething(myValue); changes a global variable in the above code. The part that was supposed to be optimized out was the part where the return value of DoSomething is discarded, not the call to DoSomething.

 

GCC shows things to work as expected. The asm generated by boostc cuts out the entire Dosomething function.

 

Here's "correct" example code:

 

#include <system.h>
//#include <stdio.h>

char globalValue = 0;

char DoSomething(char aValue)
{
       globalValue = aValue + 1;
       return 'A';
}

void main()
{
       char myValue = globalValue;
       int cout;
       cout << DoSomething(myValue);
       if (0 == globalValue)
       {
               /* whoops, call to DoSomething
                  was incorrectly optimized out. */
        //    this breakpoints in SourceBoostIDE
                myValue = globalValue;
       //        this would print a 0 but it doesn't in linux
       //        printf("%d", globalValue);
       }
       else
       {
               myValue = globalValue;
         //   This executes in linux
         //    and prints a 1 to the screen.
        //       printf("%d", globalValue);
       }
}

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...