Skip to content

[WebAssembly] Problems of forcing mem2reg in reference-types #81575

Closed
@aheejin

Description

@aheejin

I recently noticed that we force mem2reg optimization to run unconditionally, even in -O0, when reference-types feature is enabled:

if (Subtarget->hasReferenceTypes()) {
// We need to remove allocas for reference types
addPass(createPromoteMemoryToRegisterPass(true));
}

This was added in 890146b.

I think this can have some problems:

  1. 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.

  2. 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 a catchswitch. PHIs in catchswitch BBs have to be removed (= demoted) because catchswitchs are removed in ISel and catchswitch BBs are removed as well, so they can't have other instructions. But when reference-types is enabled, we force to run mem2reg in addISelPrepare, which runs after WinEHPrepare, re-promoting the demoted PHIs in WinEHPrepare, which crashes the compiler. This is a problem even if we have a way to promote only reference types because they can be before a catchswitch too.

Any thoughts or ideas?
cc @pmatos

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions