Skip to content

Commit 4ba45dd

Browse files
committed
[WebAssembly] Unstackify registers with no uses in ExplicitLocals
There are cases we end up removing some intructions that use stackified registers after RegStackify. For example, ```wasm bb.0: %0 = ... ;; %0 is stackified br_if %bb.1, %0 bb.1: ``` In this code, br_if will be removed in CFGSort, so we should unstackify %0 so that it can be correctly dropped in ExplicitLocals. Rather than handling this in case-by-case basis, this PR just unstackifies all stackifies register with no uses, so that they can be correctly dropped. Fixes #149097.
1 parent f56211e commit 4ba45dd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,17 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
256256

257257
// Precompute the set of registers that are unused, so that we can insert
258258
// drops to their defs.
259+
// And unstackify any stackified registers that don't have any uses, so that
260+
// they can be dropped later. This can happen when transformations after
261+
// RegStackify removes instructions that use stackified registers.
259262
BitVector UseEmpty(MRI.getNumVirtRegs());
260-
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I)
261-
UseEmpty[I] = MRI.use_empty(Register::index2VirtReg(I));
263+
for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
264+
Register Reg = Register::index2VirtReg(I);
265+
if (MRI.use_empty(Reg)) {
266+
UseEmpty[I] = true;
267+
MFI.unstackifyVReg(Reg);
268+
}
269+
}
262270

263271
// Visit each instruction in the function.
264272
for (MachineBasicBlock &MBB : MF) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc -O0 < %s
2+
3+
target triple = "wasm32-unknown-unknown"
4+
5+
define void @test(i1 %x) {
6+
%y = xor i1 %x, true
7+
; This br_if's operand (%y) is stackified in RegStackify. But this terminator
8+
; will be removed in CFGSort after that. We need to make sure we unstackify %y
9+
; so that it can be dropped in ExplicitLocals.
10+
br i1 %y, label %exit, label %exit
11+
12+
exit:
13+
ret void
14+
}

0 commit comments

Comments
 (0)