Skip to content

Commit a9a262a

Browse files
authored
Merge pull request #1078 from CohenArthur/remove-clone-on-nop-filling
Remove clone() when removing extra stack operations
2 parents 18348b1 + cb38689 commit a9a262a

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ byteorder = "1.2.7"
2323
indexmap = "1.0.2"
2424
cfg-if = "0.1.10"
2525
libloading = { version = "0.6.0", optional = true }
26+
hashbrown = "0.8.1"
2627

2728
# Uncomment to use local checkout of cranelift
2829
#[patch."https://github.com/bytecodealliance/wasmtime/"]

src/optimize/stack2reg.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ use std::collections::BTreeMap;
1313
use std::fmt;
1414
use std::ops::Not;
1515

16-
use rustc_data_structures::fx::FxHashSet;
16+
use rustc_data_structures::fx::{FxHashSet, FxHasher};
1717

1818
use cranelift_codegen::cursor::{Cursor, FuncCursor};
1919
use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
2020
use cranelift_codegen::ir::immediates::Offset32;
2121

22+
use hashbrown::HashSet;
23+
use std::hash::BuildHasherDefault;
24+
2225
use crate::prelude::*;
2326

2427
/// Workaround for `StackSlot` not implementing `Ord`.
@@ -45,9 +48,9 @@ impl Ord for OrdStackSlot {
4548

4649
#[derive(Debug, Default)]
4750
struct StackSlotUsage {
48-
stack_addr: FxHashSet<Inst>,
49-
stack_load: FxHashSet<Inst>,
50-
stack_store: FxHashSet<Inst>,
51+
stack_addr: HashSet<Inst, BuildHasherDefault<FxHasher>>,
52+
stack_load: HashSet<Inst, BuildHasherDefault<FxHasher>>,
53+
stack_store: HashSet<Inst, BuildHasherDefault<FxHasher>>,
5154
}
5255

5356
impl StackSlotUsage {
@@ -79,16 +82,14 @@ impl StackSlotUsage {
7982
}).collect::<Vec<Inst>>()
8083
}
8184

82-
fn remove_unused_stack_addr(&mut self, func: &mut Function, inst: Inst) {
85+
fn remove_unused_stack_addr(func: &mut Function, inst: Inst) {
8386
func.dfg.detach_results(inst);
8487
func.dfg.replace(inst).nop();
85-
self.stack_addr.remove(&inst);
8688
}
8789

88-
fn remove_unused_load(&mut self, func: &mut Function, load: Inst) {
90+
fn remove_unused_load(func: &mut Function, load: Inst) {
8991
func.dfg.detach_results(load);
9092
func.dfg.replace(load).nop();
91-
self.stack_load.remove(&load);
9293
}
9394

9495
fn remove_dead_store(&mut self, func: &mut Function, store: Inst) {
@@ -314,19 +315,21 @@ fn remove_unused_stack_addr_and_stack_load(opt_ctx: &mut OptimizeContext<'_>) {
314315
}
315316

316317
// Replace all unused stack_addr and stack_load instructions with nop.
317-
for stack_slot_users in opt_ctx.stack_slot_usage_map.values_mut() {
318-
// FIXME remove clone
319-
for &inst in stack_slot_users.stack_addr.clone().iter() {
320-
if stack_addr_load_insts_users.get(&inst).map(|users| users.is_empty()).unwrap_or(true) {
321-
stack_slot_users.remove_unused_stack_addr(&mut opt_ctx.ctx.func, inst);
322-
}
323-
}
318+
let mut func = &mut opt_ctx.ctx.func;
324319

325-
for &inst in stack_slot_users.stack_load.clone().iter() {
326-
if stack_addr_load_insts_users.get(&inst).map(|users| users.is_empty()).unwrap_or(true) {
327-
stack_slot_users.remove_unused_load(&mut opt_ctx.ctx.func, inst);
328-
}
329-
}
320+
// drain_filter() on hashbrown::HashSet drains the items that do *not* match the
321+
// predicate. Once hashbrown gets updated to match the behaviour of std::drain_filter
322+
// (0.8.2), the predicate will have to be reversed
323+
for stack_slot_users in opt_ctx.stack_slot_usage_map.values_mut() {
324+
stack_slot_users
325+
.stack_addr
326+
.drain_filter(|inst| !(stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true)))
327+
.for_each(|inst| StackSlotUsage::remove_unused_stack_addr(&mut func, inst));
328+
329+
stack_slot_users
330+
.stack_load
331+
.drain_filter(|inst| !(stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true)))
332+
.for_each(|inst| StackSlotUsage::remove_unused_load(&mut func, inst));
330333
}
331334
}
332335

0 commit comments

Comments
 (0)