4 ms·
I pretty strongly disagree with these quotes you have here. You don't need UB to do optimization, in fact because of UB in C many forms of optimization are impo
by mlindner 5y ago
I pretty strongly disagree with these quotes you have here. You don't need UB to do optimization, in fact because of UB in C many forms of optimization are impossible in C because of aliasing uncertainty. If things are defined to not alias then even though things are fully defined there is a lot more optimization that can be performed by not reloading variables from memory and instead using local cached-in-register copies of variables. This is why naive Rust can in some cases be faster than naive C (without using of restrict keyword) and this will only improve with time as compilers better support this.
- puffoflogic 5y ago> If things are defined to not alias ... What do you call it then when you put aliasing pointers into pointer variables "defined not to alias"? Can you explain how this reduces UB?
- foxfluff 5y agoI think they're trying to say that if your language prevents aliasing (i.e. pointers by definition do not alias and it is impossible to make them alias), then the compiler doesn't need to worry about it. C doesn't prevent aliasing, therefore the compiler has to worry about every case of aliasing that isn't undefined. But it's not the UB here that prevents optimization, it's precisely the fact that aliasing is (sometimes) defined. If it were always UB, the compiler wouldn't ever have to worry about it... but that'd require some interesting changes to the language to keep it usable. Definitely not backwards compatible changes.
- puffoflogic 5y ago
- andrewla 5y ago> pointers by definition do not alias and it is impossible to make them alias This is equivalent to saying that pointer arithmetic is disallowed. Pointers are by their nature in the C virtual machine offsets into a linear memory space, so for any two pointers, x and y, there exists a c such that (ptr_t)x+c == (ptr_t)y, and thus there can always be aliasing.
- gpderetta 5y agoActually no, the C abstract machine is not contiguous. If x and y point inside distinct objects (recursively) not part of the same object, there is no valid c that you can add to x to reach y. edit: allowing that would prevent even basic optimizations like register allocation. edit: s/virtual/abstract/
- andrewla 5y agoThe C abstract machine requires reversible transformations from pointer to integral types (7.18.1.4 in C99, 3.3.4 in C90, the first ISO standard). Practically speaking modern devices are in fact mostly flat, so you can of course do this, although you do brush up against undefined behavior to get there.
- Shikadi 5y agoWhat is the c virtual machine? I thought there wasn't one
- foxfluff 5y agoThey mean the abstract machine, in terms of which the semantics are defined.
- skissane 5y ago> Pointers are by their nature in the C virtual machine offsets into a linear memory space Historically, many platforms - such as Multics or Windows 3.x - didn’t have a linear memory space, they had some kind of memory segmentation. The industry has largely moved away from that towards the flat address space model. Go back to the 1980s, it was still a much bigger thing, and people used C on those platforms, and the standard was written to support them. The actual detailed control of memory segmentation is inherently non-portable so cannot be addressed by the standard, but the standard defines pointer arithmetic in such a way to support those platforms - pointer arithmetic on unrelated pointers is undefined, because if the pointers belong to different memory segments the results can be meaningless and useless.
- bee_rider 5y agoSee also decades of Fortran performance dominance, only overcome by throwing a gazillion C programmers at the problem.
- pjmlp 5y agoIn theory, because those gazillion C programmers happen to misuse restrict, so in the end it mostly ends up in flames.
- gpderetta 5y agobut doesn't Fortran get away with it exactly because argument aliasing is UB? You can use restrict to get Fortran semantics.
- bee_rider 5y agoActually yeah, you are right. I somehow misread it as a defense of UB because of the last sentence. Total a brain-fart on my part, sorry.
- aw1621107 5y ago> in fact because of UB in C many forms of optimization are impossible in C because of aliasing uncertainty. This doesn't seem right? Specific types of aliasing make optimization more difficult/impossible because they are specifically not UB. Rust's aliasing optimizations can (theoretically?) occur specifically because same-type aliasing is UB in Rust. I'm not sure how "UB prevents optimization" falls out of that?