waffle/passes/
unmem.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::util::{add_start, new_sig};
use std::{collections::BTreeMap, iter::empty};
use crate::{
    Block, ExportKind, Func, FuncDecl, FunctionBody, Import, ImportKind, Memory, MemoryArg,
    MemoryData, Module, Operator, Signature, SignatureData, Table, Type, Value, ValueDef,
};

// use super::reload::{ImportedCfg, ImportedMemoriesToFunc};

pub fn fuse_iter(m: &mut Module, x: impl Iterator<Item = (usize, u8)>, mem: Memory) {
    let null = new_sig(
        m,
        SignatureData {
            params: vec![],
            returns: vec![],
        },
    );
    let mut b = FunctionBody::new(m, null);
    let vz = b.arg_pool.from_iter(empty());
    let tz = b.type_pool.from_iter(empty());
    let ti = b.type_pool.from_iter(vec![Type::I32].into_iter());
    let ia = b.add_value(ValueDef::Operator(Operator::I32Const { value: 0 }, vz, ti));
    b.append_to_block(b.entry, ia);
    for (a, c) in x {
        // let ia = b.add_value(ValueDef::Operator(Operator::I32Const { value: a as u32 }, vz, ti));
        // b.append_to_block(b.entry, ia);
        let ic = b.add_value(ValueDef::Operator(
            Operator::I32Const { value: c as u32 },
            vz,
            ti,
        ));
        b.append_to_block(b.entry, ic);
        let ia = b.add_value(ValueDef::Operator(
            Operator::I32Const { value: a as u32 },
            vz,
            ti,
        ));
        b.append_to_block(b.entry, ia);
        let vs = b.arg_pool.from_iter(vec![ia, ic].into_iter());
        let j = b.add_value(ValueDef::Operator(
            Operator::I32Store8 {
                memory: MemoryArg {
                    align: 0,
                    offset: 0,
                    memory: mem,
                },
            },
            vs,
            tz,
        ));
        b.append_to_block(b.entry, j);
    }
    b.set_terminator(b.entry, crate::Terminator::Return { values: vec![] });
    let f = m.funcs.push(FuncDecl::Body(null, format!("z"), b));
    add_start(m, f);
}
pub fn metafuse_iter(m: &mut Module, x: &[(usize, u8)], mem: Memory) {
    for w in x.chunks(4096) {
        fuse_iter(m, w.iter().map(|a| *a), mem);
    }
}
pub fn metafuse(m: &mut Module, mem: Memory, dat: MemoryData) {
    let null = new_sig(
        m,
        SignatureData {
            params: vec![],
            returns: vec![],
        },
    );
    let mut v = vec![];
    for s in dat.segments.iter() {
        v.extend(s.data.iter().enumerate().map(|(a, b)| (a + s.offset, *b)));
    }
    metafuse_iter(m, &v, mem);
    let mut b = FunctionBody::new(m, null);
    let vz = b.arg_pool.from_iter(empty());
    let tz = b.type_pool.from_iter(empty());
    let ti = b.type_pool.from_iter(vec![Type::I32].into_iter());
    let ia = b.add_value(ValueDef::Operator(
        Operator::I32Const {
            value: dat.initial_pages as u32,
        },
        vz,
        ti,
    ));
    b.append_to_block(b.entry, ia);
    let vs = b.arg_pool.from_iter(vec![ia].into_iter());
    let ib = b.add_value(ValueDef::Operator(
        Operator::MemoryGrow { mem: mem },
        vs,
        tz,
    ));
    b.append_to_block(b.entry, ib);
    b.set_terminator(b.entry, crate::Terminator::Return { values: vec![] });
    let f = m.funcs.push(FuncDecl::Body(null, format!("z"), b));
    add_start(m, f);
}
pub fn metafuse_all(m: &mut Module, cfg: &mut impl Cfg) {
    let mut b = BTreeMap::new();
    for mem in m
        .memories
        .entries_mut()
        .collect::<Vec<_>>()
        .into_iter()
        .rev()
    {
        let memory64 = mem.1.memory64;
        let shared = mem.1.shared;
        b.insert(
            mem.0,
            std::mem::replace(
                mem.1,
                MemoryData {
                    initial_pages: 0,
                    maximum_pages: None,
                    segments: vec![],
                    memory64,
                    shared
                },
            ),
        );
    }
    for mem in m.memories.iter().collect::<Vec<_>>() {
        if !cfg.unmemmable(m, mem) {
            m.memories[mem] = b.remove(&mem).unwrap();
        }
    }
    for (c, d) in b.into_iter() {
        metafuse(m, c, d);
    }
}
pub trait Cfg {
    fn unmemmable(&mut self, module: &mut Module, mem: Memory) -> bool;
}
pub struct All {}
impl Cfg for All {
    fn unmemmable(&mut self, module: &mut Module, mem: Memory) -> bool {
        true
    }
}

pub struct ImportsOnly{}
impl Cfg for ImportsOnly{
    fn unmemmable(&mut self, module: &mut Module, mem: Memory) -> bool {
        for i in module.imports.iter().map(|a| a.clone()).collect::<Vec<_>>() {
            // if self.cfg.do_lower(&i.module, &i.name) {
                if i.kind == ImportKind::Memory(mem) {
                    return true;
                }
            // }
        }
        false
    }
}