RemoveUnusedModuleElements: Track CallRef/RefFunc more precisely by kripken · Pull Request #4621 · WebAssembly/binaryen (original) (raw)

If we see (ref.func <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mi>o</mi><mi>o</mi><mo stretchy="false">)</mo><mi mathvariant="normal">‘</mi><mi>t</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi>d</mi><mi>o</mi><mi>e</mi><mi>s</mi><mi>n</mi><mi>o</mi><mi>t</mi><mi>m</mi><mi>e</mi><mi>a</mi><mi>n</mi><mi>t</mi><mi>h</mi><mi>a</mi><mi>t</mi><mi mathvariant="normal">‘</mi></mrow><annotation encoding="application/x-tex">foo) that does not mean that </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">oo</span><span class="mclose">)</span><span class="mord">‘</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span><span class="mord mathnormal">d</span><span class="mord mathnormal">oes</span><span class="mord mathnormal">n</span><span class="mord mathnormal">o</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">an</span><span class="mord mathnormal">t</span><span class="mord mathnormal">ha</span><span class="mord mathnormal">t</span><span class="mord">‘</span></span></span></span>foo is reachable - we
must also see a (call_ref ..) of the proper type. Only after seeing both should
we mark the function as reachable, which this PR does.

This adds some complexity as we need to track intermediate state as we go,
since we could see the RefFunc before the CallRef or vice versa. We also
need to handle the case of a RefFunc without a CallRef properly: We cannot
remove the function, as the RefFunc must refer to it, but at least we can
empty out the body since we know it is never reached.

This removes an old wasm-opt test which is now superseded by a new lit
test.

On J2Wasm output this removes 3% of all functions, which account for
2.5% of total code size.