Skip to content

[WebAssembly] Unstackify operands for removed terminators #149432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion llvm/lib/Target/WebAssembly/WebAssemblyCFGSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "WebAssembly.h"
#include "WebAssemblyExceptionInfo.h"
#include "WebAssemblyMachineFunctionInfo.h"
#include "WebAssemblySortRegion.h"
#include "WebAssemblyUtilities.h"
#include "llvm/ADT/PriorityQueue.h"
Expand Down Expand Up @@ -97,8 +98,40 @@ static void maybeUpdateTerminator(MachineBasicBlock *MBB) {
? MF->getBlockNumbered(MBB->getNumber() + 1)
: nullptr;

if (AllAnalyzable)
if (AllAnalyzable) {
// There are cases we end up removing a conditional branch with a stackified
// register condition operand. For example:
// bb.0:
// %0 = ... ;; %0 is stackified
// br_if %bb.1, %0
// bb.1:
//
// In this code, br_if will be removed, so we should unstackify %0 so that
// it can be correctly dropped in ExplicitLocals.
//
// There seems no good method to determine which registers to unstackify
// without analyzing branches thoroughly, which is supposed to be done
// within updateTerminator(). So we just unstackify all of them and
// restackify the still-remaining registers after updateTerminator().
SmallSet<Register, 2> StackifiedRegs;
auto *MF = MBB->getParent();
WebAssemblyFunctionInfo *MFI = MF->getInfo<WebAssemblyFunctionInfo>();
for (const MachineInstr &Term : MBB->terminators()) {
for (auto &MO : Term.explicit_uses()) {
if (MO.isReg() && MFI->isVRegStackified(MO.getReg())) {
StackifiedRegs.insert(MO.getReg());
MFI->unstackifyVReg(MO.getReg());
}
}
}

MBB->updateTerminator(OriginalSuccessor);

for (const MachineInstr &Term : MBB->terminators())
for (auto &MO : Term.explicit_uses())
if (MO.isReg() && StackifiedRegs.contains(MO.getReg()))
MFI->stackifyVReg(MF->getRegInfo(), MO.getReg());
}
}

namespace {
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/CodeGen/WebAssembly/removed-terminator.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; RUN: llc -O0 < %s

target triple = "wasm32-unknown-unknown"

define void @test(i1 %x) {
%y = xor i1 %x, true
; This br_if's operand (%y) is stackified in RegStackify. But this terminator
; will be removed in CFGSort after that. We need to make sure we unstackify %y
; so that it can be dropped in ExplicitLocals.
br i1 %y, label %exit, label %exit

exit:
ret void
}
Loading