Skip to content

Commit d27654f

Browse files
authored
[WebAssembly] Remove FAKE_USEs before ExplicitLocals (#160228)
`FAKE_USE`s are essentially no-ops, so they have to be removed before running ExplicitLocals so that `drop`s will be correctly inserted to drop those values used by the `FAKE_USE`s. Fixes emscripten-core/emscripten#25301.
1 parent e5acda7 commit d27654f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyExplicitLocals.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ static MachineInstr *findStartOfTree(MachineOperand &MO,
216216
return Def;
217217
}
218218

219+
// FAKE_USEs are no-ops, so remove them here so that the values used by them
220+
// will be correctly dropped later.
221+
static void removeFakeUses(MachineFunction &MF) {
222+
SmallVector<MachineInstr *> ToDelete;
223+
for (auto &MBB : MF)
224+
for (auto &MI : MBB)
225+
if (MI.isFakeUse())
226+
ToDelete.push_back(&MI);
227+
for (auto *MI : ToDelete)
228+
MI->eraseFromParent();
229+
}
230+
219231
bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
220232
LLVM_DEBUG(dbgs() << "********** Make Locals Explicit **********\n"
221233
"********** Function: "
@@ -226,6 +238,8 @@ bool WebAssemblyExplicitLocals::runOnMachineFunction(MachineFunction &MF) {
226238
WebAssemblyFunctionInfo &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
227239
const auto *TII = MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
228240

241+
removeFakeUses(MF);
242+
229243
// Map non-stackified virtual registers to their local ids.
230244
DenseMap<unsigned, unsigned> Reg2Local;
231245

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: llc < %s | llvm-mc -triple=wasm32-unknown-unknown
2+
3+
target triple = "wasm32-unknown-unknown"
4+
5+
define void @fake_use_test() {
6+
%t = call i32 @foo()
7+
tail call void (...) @llvm.fake.use(i32 %t)
8+
ret void
9+
}
10+
11+
declare void @foo()
12+
; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite)
13+
declare void @llvm.fake.use(...) #0
14+
15+
attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite) }

0 commit comments

Comments
 (0)