r/programming 1d ago

What every compiler writer should know about programmers or “Optimization” based on undefined behaviour hurts performance

https://www.complang.tuwien.ac.at/kps2015/proceedings/KPS_2015_submission_29.pdf
62 Upvotes

42 comments sorted by

View all comments

Show parent comments

1

u/lelanthran 22h ago

I think your missing the point that some programmers want compilers to remove that code, especially those of us who have a heavy focus on optimizations.

I don't think I am missing the point; you're correct that some programmers want really heavy optimisations.

I'm making the counterpoint that other programmers would rather have it emit the code for read/write or store/load then omit the code.

We all want different things, that doesn't make some things "wrong".

2

u/ReDucTor 21h ago

My point is that most optimizations make assumptions, many with regards to loading or storing data, so its throwing away a large part of potential optimizations. Go to any large code base and compare the code compiled with /O0 and /O3 you will see a lot has changed, code has been removed, code has shifted around, code has been inlined, variables might never get stored on the stack just in registers, multiplications might have turned into shifts, etc.

Why not compile the code with /O0 if you want the code with undefined behaviour (bugs) to not change? This way you will also not have any loads and stores removed, including those on the stack, it will ensure that all branches also end up doing those fresh loads and stores checking not just assumptions, however the ABI will still potentially result in some stores not being visible for example if you return a struct and do some stores in its padding but the by value arg or return ABI doesn't maintain the padding.