Skip to content

Commit 473ef10

Browse files
authored
[WebAssembly] Demote PHIs in catchswitch BB only (#81570)
`DemoteCatchSwitchPHIOnly` option in `WinEHPrepare` pass was added in 99d60e0, because Wasm EH uses `WinEHPrepare`, but it doesn't need to demote all PHIs. PHIs in `catchswitch` BBs have to be removed (= demoted) because `catchswitch`s are removed in ISel and `catchswitch` BBs are removed as well, so they can't have other instructions. But because Wasm EH doesn't use funclets, so PHIs in `catchpad` or `cleanuppad` BBs don't need to be demoted. That was the reason `DemoteCatchSwitchPHIOnly` option was added, in order not to demote more instructions unnecessarily. The problem is it should have been set to `true` for Wasm EH. (Its default value is `false` for WinEH) And I mistakenly set it to `false` and wasn't aware about this for more than 5 years. This was not the end of the world; it just means we've been demoting more instructions than we should, possibly huting code size. In practice I think it would've had hardly any effect in real performance given that the occurrence of PHIs in `catchpad` or `cleanuppad` BBs are not very frequent and many people run other optimizers like Binaryen anyway.
1 parent 8c56e78 commit 473ef10

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
249249
"WinEHPrepare failed to remove PHIs from imaginary BBs");
250250
continue;
251251
}
252-
if (isa<FuncletPadInst>(PadInst))
252+
if (isa<FuncletPadInst>(PadInst) &&
253+
Personality != EHPersonality::Wasm_CXX)
253254
assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
254255
}
255256

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ void TargetPassConfig::addPassesToHandleExceptions() {
918918
// on catchpads and cleanuppads because it does not outline them into
919919
// funclets. Catchswitch blocks are not lowered in SelectionDAG, so we
920920
// should remove PHIs there.
921-
addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/false));
921+
addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/true));
922922
addPass(createWasmEHPass());
923923
break;
924924
case ExceptionHandling::None:

llvm/test/CodeGen/WebAssembly/wasm-eh-prepare.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: opt < %s -win-eh-prepare -demote-catchswitch-only -wasm-eh-prepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
33
; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S | FileCheck %s
44
; RUN: opt < %s -passes='win-eh-prepare<demote-catchswitch-only>,wasm-eh-prepare' -S --mattr=+atomics,+bulk-memory | FileCheck %s
5+
; RUN: llc < %s -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -stop-after=wasm-eh-prepare | FileCheck %s
56

67
target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
78
target triple = "wasm32-unknown-unknown"
@@ -245,7 +246,6 @@ bb.true: ; preds = %entry
245246
bb.true.0: ; preds = %bb.true
246247
br label %merge
247248

248-
; CHECK: bb.false
249249
bb.false: ; preds = %entry
250250
br label %merge
251251

0 commit comments

Comments
 (0)