3 ms·
I'm coming round to the idea that entirely eliminating lines of code in the optimizer is something that should be a warning instead, to be fed back to the user.
by pjc50 2y ago
I'm coming round to the idea that entirely eliminating lines of code in the optimizer is something that should be a warning instead, to be fed back to the user.
(NB entirely. That is, it's reasonable to coalesce arithmetic expressions together or inline functions such that a single operation in the output has subsumed multiple lines of code. However, if for a source S you can remove one or more entire lines of code to produce source S' with the same output, then something is wrong with the intent of the program or the compiler has misinterpreted it)
- UncleMeat 2y agoThis would produce at least one warning for almost every nontrivial function in your program, even with your concession towards simplifying arithmetic expressions.
- ben_w 2y agoCan you give an example? This is surprising, and I can't imagine such… but then, I never wrote a compiler or needed to examine the assembly from one.
- UncleMeat 2y agoCopy elision and dead store elimination are extremely common examples. Redundant bounds check elimination on containers is another. I'd consider hoisting outside of a loop also to be eliminating code. I suppose some of this depends on what you mean by "eliminating a line of code", as an optimizing compiler pretty quickly transforms the program into a state where any kind of 1:1 mapping between lines of code and instructions is impossible.
- ben_w 2y agoI suspect I have misunderstood the point you were making in the previous comment, are not the first two already things one can get warnings for, and the third a useful thing to know you're doing redundantly? I don't think hoisting would be covered by this suggestion, pjc50 suggested deletion rather than moving. I read the proposal very literally, which also means I'm unclear what options you have in your final paragraph: Compile function as is; For each line, make a copy with that line missing and recompile; when this identical to the original, flag that line as "warning: optimised out of existence"
- UncleMeat 2y agoThis approach would increase compile times by an outrageous amount, since you now need to compile each function N times. Optimizing compilers can also produce different output but with identical semantics depending on a lot of heuristics. What happens when deleting a line of code produces different inlining or specialization decisions? What is a rigorous definition of "identical"? "Deleting a line" is also not the only way that unexpected and perhaps unwanted behavior from UB reasoning appears. Unexpected code motion is absolutely a thing that people have complained about. I don't think you can so quickly insist that things like hoisting are out of scope here.
- jcranmer 2y ago... what does this even mean? As a compiler--indeed, optimization--developer, one of the things that continually frustrates me about discussions on UB is that so many suggestions are based on just complete incomprehension of how compilers work. One of the first things compilers do is throw away the original source code; outside of initial front-end codegen, there is no reference to the original AST, and instead everything is based on a pseudo-assembly IR. A concept like an "entire line of code" just doesn't exist in IR; even mapping instructions back to line of codes is a difficult process because frequently they aren't even attributable to a line.
- pjc50 2y ago> even mapping instructions back to line of codes is a difficult process Enough is retained to allow line-step debugging. Godbolt will even do a nice mapping for you between source and object lines. Yes, a lot of it gets thrown away. My point is "this line of code has no effect on the output" is a problem, and the developer should be warned rather than lulled into a false sense of security that their check is doing something. (Code coverage has the same issue of doing line attribution, plus the difficulty of hitting error conditions in practice, but it would also show unexecuted lines of code)
- gpderetta 2y agoYou are vastly underestimating the amount of false positives under inlining and interprocedural optimizations.
- compiler-guy 2y ago> Enough is retained to allow line-step debugging. Only without optimization. Every compiler I have ever used (and I have used a lot of them!) starts having difficulty with step-debugging at the lowest-level of optimization, typically -O1. Some functions work, most don't.
- estebank 2y ago> One of the first things compilers do is throw away the original source code; outside of initial front-end codegen, there is no reference to the original AST That doesn't have to be an immutable fact for all implementations of compilers for all languages. The Rust compiler has multiple IRs (AST, HIR, THIR, MIR & LLVM IR), and for all but the last one (codegen), we keep around, if not a reference to the HIR node that allows us to navigate its context, at least a Span that points at the user's code (sometimes a whole struct or linked list of "facts" we've collected so far, each containing Spans). rustc also throws away information to reduce memory consumption, much to my dismay, but we still perform later checks to re evaluate (sometimes as an approximation) what the reason for a certain "decision" was, so that we can talk to the user in terms they understand, and not in terms of compiler internals. The ideal design would be to have a single implementation of all logic with two modes of operation: the low memory consumption one (what you state as the way compilers work), and a "track everything without discarding metadata until the end" mode, that can provide better human readable output.
- pm215 2y agoIt's quite common in large C programs to have setups where some code might be under an if() statement and the if() condition is something that is compile-time 0 if support for a feature wasn't compiled in. (The idea is that you get syntax checking in a way that you don't for just ifdeffing-out the code, and you don't get weird nesting of preprocessor ifdefs with C ifs.) If you warn about the elimination of code like that, it makes that technique rather awkward. (For instance, in QEMU we have code like: "if (kvm_enabled()) { do_something_kvm_specific(); }". In a no-KVM build, kvm_enabled() evaluates to compile-time false, and in a KVM build it does a runtime check of whether the user turned on KVM for this run.)