Open dsnet opened 6 years ago
I assume these things happen while inlining (and as we get more aggressive in inlining, they will be more and more common).
The type conversion is lowered during buildssa into runtime calls. I've already attempted to write some code to delay lowering in a similar case, but I couldn't find a good way to layout a runtime function call after buildssa (eg: stack slots). It's way above my head.
It would help if somebody more familiar with the compiler could come up with some utility to add a function call during a SSA pass. That would be very useful for me to start working towards delaying lowering of runtime calls, exploiting optimizations opportunities that higher-level semantics bring.
How much real-world code actually does these impossible assertions? (That is, what's the potential impact of the optimization?)
I assume that this mostly occurs in generated code, but in that case wouldn't it be even better — particularly in terms of compile times — to avoid emitting the redundant assertions in the first place?
@bcmills the point that @rasky makes about this happening because of inlining I think is probably the main source of these occurrences already today. See e.g. https://godbolt.org/g/GzDvaU or https://godbolt.org/g/N5W6Ua (in both cases the call to f()
is inlined, but the type assertion remains)
(Also, but this is a vastly less important argument in the context of this discussion, requiring code generators to be that smart would vastly complicate them)
Tracking concrete type behind an interface will also help https://github.com/golang/go/issues/19361, thus lowering call overhead if methods are invoked, and potentially lowering GC overhead through not letting call arguments to escape.
I think that, wrt inlining, we're in a sort of catch-22: inlining does not seem to help much because we're bad at exploiting many of these opportunities in our optimizers; and adding optimizations like this one is going to have a tool speed impact without great benefits because we don't inline nearly as much as we should.
Consider the following:
In this situation, the compiler knows that the
MyMessage
type has noCustomUnmarshal
method, so the type assertion cannot possibly succeed. In this case, the condition of the if statement can be statically determined and the body be considered dead code.However, a tip build of the compiler continues to emit code for the assertion.