Description
I recently noticed that we force mem2reg optimization to run unconditionally, even in -O0
, when reference-types feature is enabled:
llvm-project/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
Lines 479 to 482 in 2dd5204
This was added in 890146b.
I think this can have some problems:
-
This means we run some optimizations even when we are not supposed to. We will handle debug value differently and debug info can be lost even in
-O0
, if reference-types is enabled. And the newest EH spec requires reference-types to be enabled, so this means enabling EH means running some optimizations even in-O0
. Also, as it happens, we've been even discussing enabling reference-types by default. ([WebAssembly] Enable multivalue and reference-types in generic CPU config #80923) It'd be good if there's a way to promote only reference type values to registers. -
When Wasm EH is used, we demote certain PHIs from registers to memory in
WinEHPrepare
. (Wasm EH uses WinEHPrepare pass, because it uses WinEH IR) Specifically, we cannot have a PHI before acatchswitch
. PHIs incatchswitch
BBs have to be removed (= demoted) becausecatchswitch
s are removed in ISel andcatchswitch
BBs are removed as well, so they can't have other instructions. But when reference-types is enabled, we force to run mem2reg inaddISelPrepare
, which runs afterWinEHPrepare
, re-promoting the demoted PHIs inWinEHPrepare
, which crashes the compiler. This is a problem even if we have a way to promote only reference types because they can be before acatchswitch
too.
Any thoughts or ideas?
cc @pmatos