3 ms·
It's C++, but UB allows the compiler to call a function the programmer never called https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never
by dirtydroog 7y ago
It's C++, but UB allows the compiler to call a function the programmer never called
https://kristerw.blogspot.com/2017/09/why-undefined-behavior-may-call-never.html https://kristerw.blogspot.com/2017/09/why-undefined-behavior...
I'm not sure how any compiler writer can condone this behaviour.
- aw1621107 7y agoThat particular behavior (and some other examples of pathological optimizer behavior) is probably an emergent property of how several optimizations combine as opposed to being a specifically targeted optimization. I can imagine something the optimizer doing something like: 1. Determine possible function call targets 2. Eliminate invalid targets from call set 3. If only one target remains, replace indirect call with direct call That might be useful for something like devirtualization, but also could lead to the behavior exhibited in that blog post. Indeed, the second blog post [0] shows that if there are two possible call targets, LLVM could generate an indirect call like what you may expect. [0]: https://kristerw.blogspot.com/2017/09/follow-up-on-why-undefined-behavior-may.html https://kristerw.blogspot.com/2017/09/follow-up-on-why-undef...
- AshamedCaptain 7y agoThat is not "never used" as the programmer argues, and I find it misleading that the title of the blogpost tries to sell it as "a never used function is getting called". The NeverCalled function has external visibility so it could be eventually called by another CU, and it definitely references (or takes the address of) the supposedly unused "EraseAll" function. In order to be a legal program, some other CU has to call the NeverCalled at some point before main(), and the compiler is just assuming that.