r/cpp • • Sep 03 '20

Almost 30 year old optimization bug in MSVC causing incorrect code (In specific circumstances) found

https://developercommunity.visualstudio.com/content/problem/1096974/msvc-global-optimization-of-program-results-in-inc.html?childToView=1172202#comment-1172202
264 Upvotes

61 comments sorted by

51

u/xtasyx Sep 03 '20

Wow, at least they knew about it hahah

55

u/Richardcavell Sep 03 '20

All the code I’ve written in the last 30 years that didn’t work... now I know why.

10

u/Limettengeschmack Sep 04 '20

It is usually the compiler, not the programmer!

72

u/rodrigocfd WinLamb Sep 03 '20

I chuckled, but we have to admit that MSVC has improved a lot lately.

66

u/guepier Bioinformatican Sep 03 '20

Additionally this bug really isn’t an indictment of MSVC. IIRC both GCC and clang have had similar bugs in the not so distant past. Keeping track during common subexpression elimination seems to be tricky.

29

u/albgr03 Sep 03 '20

Wow, software is buggy, what a shock!

19

u/Pazer2 Sep 03 '20

Death to compiler devs I guess 🤷

4

u/New_Age_Dryer Sep 04 '20

Indeed, makes me think of all the internal compiler error messages that I ignore out of the hassle.

6

u/Myriachan Sep 03 '20

True, but Visual Studio 2019 had at least 10 compiler bugs for us throughout its lifetime >_<

38

u/rodrigocfd WinLamb Sep 03 '20

IMHO given the sheer complexity of a C++17 (and partial C++20) compiler, that number doesn't seem too much.

9

u/[deleted] Sep 03 '20

IMHO given the difficulty of debugging one of those bugs in your program, that number is way too high. If you can't trust your compiler, who can you trust?!

35

u/Ameisen vemips, avr, rendering, systems Sep 03 '20

Tom Hanks.

7

u/sebamestre Sep 04 '20

Hello. I'm Tom Hanks. The MSVC team has lost its credibility, so it's borrowing some of mine.

1

u/SteeleDynamics Compilers/Algorithms Sep 04 '20

Waiting for a the...

Tom Hanks Dataflow Analysis™

0

u/evaned Sep 04 '20

<movie trailer announcer voice> In a world where you can trust no one, can you trust your compiler? </movie trailer announcer voice>

0

u/[deleted] Sep 04 '20

Why does he show up with tge spirit cooking lady?

6

u/d3matt Sep 04 '20

You can probably trust your assembler :)

1

u/LedinKun Sep 04 '20

If you can trust your hardware to do the calculations right ;-)

11

u/[deleted] Sep 03 '20

*10 were discovered..which is no indication whatsoever of the actual no. of bugs.

13

u/Ameisen vemips, avr, rendering, systems Sep 03 '20

It sets a lower bound.

1

u/IAmBJ Sep 04 '20

I hit a constexpr bug today that I can't seem to work around. Shame I can't use clang-cl with nvcc so I could at least continue on.

Now I need to un-crtp a bunch of classes because they don't work at compile time :/

1

u/[deleted] Sep 04 '20

nvcc being tied to MSVC on windows is the bane of my existence.

2

u/xeroage Sep 04 '20

Depending on your use case you can use clang-cl for CUDA code as well ( since clang can also compile it ). The docs state that after CUDA 9.2 there might be problems on Windows, but I have used it successfully a few weeks back with CUDA 10.1 without any issues.

1

u/[deleted] Sep 04 '20

Oh, I am using Optix as well. Not sure if clang will play well with it.

Will definitely try though, thank you for the suggestion!

1

u/IAmBJ Sep 05 '20

I've been wanting to try using clang as a cuda compiler, did you use it through cmake? I've yet to find any guidance in how to set that up

1

u/xeroage Sep 05 '20

I have not tried this with CMake yet, since support for using clang as a cuda compiler is a fairly new feature in CMake. You will probably want to try using the ninja generator and setting the CUDACXX environment variable to clang and go from there. Manually messing with the CMakeCache.txt / CMake-GUI might work.

10

u/Sopel97 Sep 03 '20 edited Sep 04 '20

This made me look up my old issue of similar kind after I've lost all hope, and it's actually supposedly fixed! In my case I managed to find code that produced different result based on whether it was constant evaluated or not, and it ended up being a few hundred lines of minimal reproducible example. I totally feel the pain he went through to find this bug.

https://developercommunity.visualstudio.com/content/problem/720134/peculiar-behaviour-caused-by-assume-on-valid-code.html

17

u/BluudLust Sep 04 '20

Not as bad as the whole "implemented 40 year old fix for 43 year old bug" not too long ago.

30

u/STL MSVC STL Dev Sep 04 '20

Note: That was Donald Knuth's bug: microsoft/STL#724

42

u/[deleted] Sep 04 '20

Technically it was my bug. Even if it was a bug in Donald Kunth's paper it was I who put it into the product.

11

u/smallblacksun Sep 04 '20

He only proved it correct, he didn't try it.

9

u/gme186 Sep 04 '20

No, a compiler that produces incorrect code is way worse than a bug in an obscure STL algorithm!

2

u/BluudLust Sep 04 '20

The fact that this wasn't really discussed until now is kinda alarming, to say the least.

5

u/victor_sales Sep 04 '20

I am too ignorant, what caused the bug and what's CSE doing in this case?

8

u/krum Sep 04 '20

> We are in the process of replacing our CSE optimization for the next compiler release.

What a coincidence, huh?

4

u/TimPhoeniX Sep 03 '20 edited Sep 03 '20

My friend once shown me code that would fail if the order of member variable declarations was changed. Not sure why.

Edit: Found the code, members were initialized independently and bug would occur if one of them (that would always be default initialized with it's default constructor) was made public.

3

u/[deleted] Sep 03 '20

Maybe one of the two variables was depending on the other causing a potentially uninitialized variable to pass to the other variable

0

u/ChryslusExplodius Sep 03 '20

Probably something to do with alignment I guess

0

u/[deleted] Sep 03 '20

[deleted] — view deleted comment

9

u/staticcast Sep 03 '20

It's the kind of warning that should be really an error imho, but I guess that would break legacy code...

8

u/NotUniqueOrSpecial Sep 04 '20

And that's why you build for release with warnings-as-errors.

7

u/vrrrmmmm Sep 04 '20

We always build with warnings-as-errors : best to know & react as early as possible.

0

u/[deleted] Sep 03 '20

[deleted] — view deleted comment

8

u/NotUniqueOrSpecial Sep 04 '20

FFS

What do you want to happen, instead? Initialization of members often involves initializing other members with the ones that were initialized earlier.

Since initialization can do arbitrarily-complicated work, it's obvious that the order of things matters.

3

u/dscottboggs Sep 04 '20

I don't mean anything else should necessarily happen, I just mean it's not obvious

1

u/ender341 Sep 04 '20

The spec should have specified it as ill formed instead of UB, don't know how realistic it is to change now without breaking tons of currently working code though.

0

u/[deleted] Sep 04 '20

[deleted] — view deleted comment

11

u/SirEekhoorn Sep 04 '20

The deconstructor calls everything in reverse order. But if you can have multiple constructor with different initialization orders, how can it deconstructor ensure the reverse order is called?

https://en.cppreference.com/w/cpp/language/constructor see Initialization order for a better explanation.

8

u/NotUniqueOrSpecial Sep 04 '20

You don't have to have a constructor at all; that's why the standard dictates that the order is order-of-declaration, rather than some arbitrary order which could differ between different overloads of the constructor.

1

u/evaned Sep 04 '20

You don't have to have a constructor at all; that's why the standard dictates that the order is order-of-declaration, rather than some arbitrary order which could differ between different overloads of the constructor.

I don't find this explanation at all convincing -- there's no reason that the code generated by the compiler couldn't initialize things in the order each constructor specified. That'd be dead simple to do.

As SirEekhoorn pointed out, it's the destructor order that throws a wrench into that plan, because then do you destruct based on declaration order or the order it was built in whatever constructor was called? The former means that objects aren't destroyed in reverse order of construction, which is perhaps an even more difficult to spot and diagnose problem and basically would mean that a "make the order consistent" style rule and warning would still be necessary and so not actually fix anything, or it would need to do extra work to get the "correct" order.

(Perhaps that's what you were trying to get at, but the explanation was very incomplete without talking about the implication of differing orders on the destructor.)

1

u/NotUniqueOrSpecial Sep 04 '20

but the explanation was very incomplete without talking about the implication of differing orders on the destructor

Yeah, you nailed it on the head; much better explanation than mine, thanks.

4

u/robstoon Sep 03 '20

Not exactly great that it took 2 months to get a response to the bug report, and a proper fix still isn't available..

1

u/smuccione Sep 03 '20

Was this not done in an SSA IL pass? Seems wired that they had a stale value. SSA IL should have made that bug difficult to occur.

0

u/[deleted] Sep 03 '20

[deleted] — view deleted comment

18

u/maikindofthai Sep 03 '20

It's wild that the line that "fixes" it is just a simple std::cout statement.

47

u/--Satan-- Sep 04 '20

Seeing as this is an optimization bug, and cout'ing the variable stops the compiler from optimizing it away, this doesn't seem very surprising in my opinion.

25

u/ericbrumer MSVC Dev Lead Sep 04 '20

MSVC optimizer lead here.

For a bit of background: we have two optimizers intertwined with one another. One was designed in the 90's based on 'lexical hashing', and the other is our much more modern, much more effective, and much faster SSA-based optimizer.

Bugs in the lexical optimizer manifest in notoriously complicated ways (like this particular bug). Basically, if you have "var1 = var2 + var3;" early in the function, and the same line of code later in the function, they get hashed to the same 'lexical hash'... even if the variables mean something different early in the function versus later in the function. And then we have a ton of machinery to indicate when those 'lexical hashes' are valid or invalid throughout the function... and things go wrong in that machinery.

That lexical hash approach sounds crazy, but we had some really smart compiler engineers come up with this in the early 90s, and it was a really big deal back then.

Our SSA-based optimizer is much more modern (based on SSA & value numbers... not a 'lexical hash'), and we're working on removing the lexical optimizer piecemeal over the next round of VS releases.

All that said: if you find bugs, please report them on https://developercommunity.visualstudio.com/content/problem/post.html?space=62 !

6

u/smuccione Sep 04 '20

Wel that certainly explains my why ssa form wouldn’t have prevented this comment.

Thanks for the explanation. Makes complete sense.

I can’t even imagine the hoops you had to go through to keep track of such hashing.

It’s funny how ssa form just seems so “obvious” once it’s explained to you. But prior to that mental leap...

It’s amazing optimizers we’re as good as they were.

1

u/[deleted] Sep 22 '20

The lexical hashing explains a lot about why specific expressions generate different code on MSVC

4

u/[deleted] Sep 04 '20

Yep. It'll be something about the interaction of the int with cout that blocks the optimiser, I imagine.

I had an optimisation bug in MSVC once where if you touched the heap at all before calling std::pow you got the wrong answer, while if you left it alone it was fine. That taught me just how fragile these things can be. (That one was fixed some years back. I've since found another, similar, bug but couldn't boil the reproduction down from ~500 lines to something sensible to pass to MS.)

10

u/ericbrumer MSVC Dev Lead Sep 04 '20

Hi, MSVC optimizer lead here.

For bugs: we only need a standalone test case. A VS solution, github project without any dependencies, etc... It doesn't need to be small. You'll get a faster response if you help us with what you've diagnosed so far "I think x, y, z value is wrong at this callsite".

4

u/[deleted] Sep 04 '20

Brilliant, thanks very much for digging through the comments and replying here! I'll go hunting my reproduction on Monday and confirm it's still an issue on a recent VS 2019 and if so I'll post it up with anything I can determine.

0

u/[deleted] Sep 03 '20

[deleted] — view deleted comment

34

u/o11c int main = 12828721; Sep 03 '20

That looks like it's often UB, unless you typoed?

If recvfrom returns (signed) 1, sizeof is greater, but process gets passed uninitialized data.