[Bug c++/49651] New: [C++0x] nested lambdas and -O3 produced incorrect integer variable increments

2011-07-05 Thread pedro.larroy at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49651

   Summary: [C++0x] nested lambdas and -O3 produced incorrect
integer variable increments
   Product: gcc
   Version: 4.6.1
Status: UNCONFIRMED
  Severity: major
  Priority: P3
 Component: c++
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: pedro.lar...@gmail.com


Hi. I'm trying to reproduce this outside 'production' code but haven't been
successful yet. But the observation is the following:

I have 3 level nested lambdas iterating some vectors. Inside, printing and
incrementing this 'sentinel' variable, the output with -O3 shows the wrong
output:

0. sentinel: deadbee5
2. sentinel: deadbee6
1. sentinel: deadbee7
1. sentinel: deadbee8
3. sentinel: deadbee7
4. sentinel: deadbee8
1. sentinel: deadbee9
5. sentinel: deadbee9
6. sentinel: deadbeea
2. sentinel: deadbeeb

With -O0 the values are consecutive. The code is similar to:

u32 sentinel=0xdeadbee0;
auto run = [&](Class& c) {
   for_each(v3.begin(), v3.end(), [&](ClassC& cc) {
   print(sentinel++);
   });
   print(sentinel++);
};
for_each(v.begin(), v.end(), [&](ClassB& b) {

   for_each(v2.begin(), v2.end(), run);
   print(sentinel++);
});


I have observed that the sentinel variable for example, is captured inside the
lambda and the address is not the same as the outermost sentinel...   could
that be the problem that leads to the wrong optimizations?


[Bug c++/49651] [C++0x] nested lambdas and -O3 produced incorrect integer variable increments

2011-07-05 Thread pedro.larroy at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49651

--- Comment #1 from Pedro Larroy  2011-07-05 
20:30:13 UTC ---
I think I'm able to reproduce with the following:

the output spotted is:


run.for_each.a: 20633
run.a: 20634
1 for_each.a: 20631
1 for_each.a: 20632

Compiled with:
g++ -Wall -std=c++0x -O3 -o test test.cc

Code follows:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

typedef uint32_t u32;
using namespace std;
vector g;
void f(u32 a, u32 b) 
{   
g.push_back(b);
}

int main(int argc, char *argv[])
{
u32 a = 0;
vector vi;
vi.push_back(0);
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);


vector vo;
vo.push_back(5);
vo.push_back(5);

vector ve;
ve.push_back(5);


vector v3;
vector v4;
vector v5;
vector v6;
vector v7;

f(32, a++);
cout << "bmain.a: " << a << endl;

f(32, a++);
cout << "bmain.a: " << a << endl;


auto run = [&](int& i) {
// inside this lambda &a is not the same as the &a in the first line
v3.push_back(i);
v4.push_back(i);
v5.push_back(i);
v6.push_back(i);
v7.push_back(i);
f(32, a++);
cout << "run.a: " << a << endl;

v3.push_back(i);
v4.push_back(i);
v5.push_back(i);
v6.push_back(i);
v7.push_back(i);
f(32, a++);
cout << "run.a: " << a << endl;



for_each(ve.begin(), ve.end(), [&](int& xi) {
v7.push_back(xi);
f(32, a++);
cout << "run.for_each.a: " << a << endl;

});

f(32, a++);
cout << "run.a: " << a << endl;
};

f(32, a++);
cout << "main.a: " << a << endl;

while(true) {
for_each(vi.begin(), vi.end(), [&](int& xi) {
f(32, a++);
cout << "1 for_each.a: " << a << endl;


for_each(vo.begin(), vo.end(), run);

f(32, a++);
cout << "1 for_each.a: " << a << endl;


for_each(ve.begin(), ve.end(), run);

f(32, a++);
cout << "1 for_each.a: " << a << endl;


});
}
cout << "main.a: " << a << endl;
}


[Bug c++/49651] [C++0x] nested lambdas and -O3 produced incorrect integer variable increments

2011-07-05 Thread pedro.larroy at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49651

--- Comment #3 from Pedro Larroy  2011-07-05 
22:24:26 UTC ---
(In reply to comment #2)
> Can you produce a testcase that aborts/fails if the problem occurs? Otherwise 
> I
> seem to need to inspect thousands of lines to look for non-consecutive values!
> 
> Could this be the same underlying issue as PR 49598 and so (very recently)
> fixed?

It doesn't look the same to me since the variable doesn't have garbage, but I
don't have enough insight to judge. Can you point to which particular source
file to check to investigate this?

Here is the program that asserts on error. Works with -O0 and fails with -O3
quite soon (12 iterations):


g++-4.6 -Wall -std=c++0x -O3 -o test test.cc




#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

typedef uint32_t u32;
using namespace std;
vector g;
void f(u32 a, u32 b) 
{   
g.push_back(b);
for(size_t i=1; i vi;
vi.push_back(0);
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
vi.push_back(4);


vector vo;
vo.push_back(5);
vo.push_back(5);

vector ve;
ve.push_back(5);


vector v3;
vector v4;
vector v5;
vector v6;
vector v7;

f(32, a++);
cout << "bmain.a: " << a << endl;

f(32, a++);
cout << "bmain.a: " << a << endl;


auto run = [&](int& i) {
// inside this lambda &a is not the same as the &a in the first line
v3.push_back(i);
v4.push_back(i);
v5.push_back(i);
v6.push_back(i);
v7.push_back(i);
f(32, a++);
cout << "run.a: " << a << endl;

v3.push_back(i);
v4.push_back(i);
v5.push_back(i);
v6.push_back(i);
v7.push_back(i);
f(32, a++);
cout << "run.a: " << a << endl;



for_each(ve.begin(), ve.end(), [&](int& xi) {
v7.push_back(xi);
f(32, a++);
cout << "run.for_each.a: " << a << endl;

});

f(32, a++);
cout << "run.a: " << a << endl;
};

f(32, a++);
cout << "main.a: " << a << endl;

while(true) {
for_each(vi.begin(), vi.end(), [&](int& xi) {
f(32, a++);
cout << "1 for_each.a: " << a << endl;


for_each(vo.begin(), vo.end(), run);

f(32, a++);
cout << "1 for_each.a: " << a << endl;


for_each(ve.begin(), ve.end(), run);

f(32, a++);
cout << "1 for_each.a: " << a << endl;


});
}
cout << "main.a: " << a << endl;
}


[Bug tree-optimization/49651] [4.4/4.5 Regression] nested lambdas and -O3 produced incorrect integer variable increments

2011-08-01 Thread pedro.larroy at gmail dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49651

--- Comment #14 from Pedro Larroy  2011-08-01 
15:59:23 UTC ---
(In reply to comment #13)
> Fixed for 4.6.2 and trunk sofar.

great!  thanks.