plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
07e9bb2
— Peter John
2026-11-10T11:31:55+05:30
wasm codegen: inline tiny straight-line functions at their call sites
compiler/wasm-codegen/src/module.rs
CHANGED
|
@@ -189,6 +189,8 @@ impl WasmModule {
|
|
|
189
189
|
}
|
|
190
190
|
let saved = (self.functions.clone(), self.exports.clone(), self.start_function, self.table_elements.clone(), self.func_names.clone());
|
|
191
191
|
let r = crate::optimize::removeRedundantCasts(self)
|
|
192
|
+
.and_then(|_| crate::optimize::inlineLeaves(self))
|
|
193
|
+
.and_then(|_| crate::optimize::removeRedundantCasts(self))
|
|
192
194
|
.and_then(|_| if keepDeadFns { Ok(0) } else { crate::optimize::removeDeadFunctions(self) })
|
|
193
195
|
.and_then(|_| crate::optimize::validates(self));
|
|
194
196
|
if let Err(e) = r {
|
compiler/wasm-codegen/src/optimize.rs
CHANGED
|
@@ -211,3 +211,109 @@ pub(crate) fn removeDeadFunctions(module: &mut WasmModule) -> Result<usize, Stri
|
|
|
211
211
|
}
|
|
212
212
|
Ok(removed)
|
|
213
213
|
}
|
|
214
|
+
|
|
215
|
+
/// Largest body (in bytes, after its parameter loads) `inlineLeaves` copies into a caller.
|
|
216
|
+
pub(crate) const INLINE_LIMIT: usize = 20;
|
|
217
|
+
|
|
218
|
+
/// Copies the body of every small straight-line function into each place that calls it.
|
|
219
|
+
/// A candidate declares no locals, starts by loading its parameters in order
|
|
220
|
+
/// (`local.get 0 .. local.get n-1`, exactly what its caller has just pushed), then runs at
|
|
221
|
+
/// most `INLINE_LIMIT` bytes that touch no local and hold no block, branch or return: so
|
|
222
|
+
/// the bytes after the loads, put where the `call` was, do the same work on the same
|
|
223
|
+
/// stack (a `return_call` becomes those bytes and a `return`). Accessors such as
|
|
224
|
+
/// `Str.length` or `Array.get` go this way. Returns the number of call sites inlined.
|
|
225
|
+
pub(crate) fn inlineLeaves(module: &mut WasmModule) -> Result<usize, String> {
|
|
226
|
+
let bytes = module.encode();
|
|
227
|
+
let imports = module.func_import_count;
|
|
228
|
+
let n = module.functions.len();
|
|
229
|
+
struct Scan {
|
|
230
|
+
params: u32,
|
|
231
|
+
loaded: u32,
|
|
232
|
+
rest: Option<usize>,
|
|
233
|
+
end: Option<usize>,
|
|
234
|
+
ok: bool,
|
|
235
|
+
}
|
|
236
|
+
let mut scans: Vec<Scan> = (0..n).map(|_| Scan { params: 0, loaded: 0, rest: None, end: None, ok: false }).collect();
|
|
237
|
+
// Per defined function: (callee, op start, op end, is a return_call).
|
|
238
|
+
let mut calls: Vec<Vec<(u32, usize, usize, bool)>> = vec![Vec::new(); n];
|
|
239
|
+
let mut cur = usize::MAX;
|
|
240
|
+
walk(&bytes, |fn_i, v, op, start, end| {
|
|
241
|
+
match op {
|
|
242
|
+
Operator::Call { function_index } => calls[fn_i].push((*function_index, start, end, false)),
|
|
243
|
+
Operator::ReturnCall { function_index } => calls[fn_i].push((*function_index, start, end, true)),
|
|
244
|
+
_ => {}
|
|
245
|
+
}
|
|
246
|
+
let sc = &mut scans[fn_i];
|
|
247
|
+
if cur != fn_i {
|
|
248
|
+
cur = fn_i;
|
|
249
|
+
// No declared locals: the body bytes start with a zero count of local groups.
|
|
250
|
+
sc.ok = module.functions[fn_i].1.first() == Some(&0);
|
|
251
|
+
sc.params = v.len_locals();
|
|
252
|
+
}
|
|
253
|
+
if !sc.ok || sc.end.is_some() {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if sc.rest.is_none() {
|
|
257
|
+
if sc.loaded < sc.params {
|
|
258
|
+
match op {
|
|
259
|
+
Operator::LocalGet { local_index } if *local_index == sc.loaded => sc.loaded += 1,
|
|
260
|
+
_ => sc.ok = false,
|
|
261
|
+
}
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
sc.rest = Some(start);
|
|
265
|
+
}
|
|
266
|
+
match op {
|
|
267
|
+
Operator::End => sc.end = Some(start),
|
|
268
|
+
Operator::LocalGet { .. }
|
|
269
|
+
| Operator::LocalSet { .. }
|
|
270
|
+
| Operator::LocalTee { .. }
|
|
271
|
+
| Operator::Block { .. }
|
|
272
|
+
| Operator::Loop { .. }
|
|
273
|
+
| Operator::If { .. }
|
|
274
|
+
| Operator::Else
|
|
275
|
+
| Operator::Try { .. }
|
|
276
|
+
| Operator::TryTable { .. }
|
|
277
|
+
| Operator::Catch { .. }
|
|
278
|
+
| Operator::CatchAll
|
|
279
|
+
| Operator::Delegate { .. }
|
|
280
|
+
| Operator::Rethrow { .. }
|
|
281
|
+
| Operator::Br { .. }
|
|
282
|
+
| Operator::BrIf { .. }
|
|
283
|
+
| Operator::BrTable { .. }
|
|
284
|
+
| Operator::BrOnNull { .. }
|
|
285
|
+
| Operator::BrOnNonNull { .. }
|
|
286
|
+
| Operator::BrOnCast { .. }
|
|
287
|
+
| Operator::BrOnCastFail { .. }
|
|
288
|
+
| Operator::Return
|
|
289
|
+
| Operator::ReturnCall { .. }
|
|
290
|
+
| Operator::ReturnCallIndirect { .. }
|
|
291
|
+
| Operator::ReturnCallRef { .. } => sc.ok = false,
|
|
292
|
+
_ => {}
|
|
293
|
+
}
|
|
294
|
+
})?;
|
|
295
|
+
let inline: Vec<Option<Vec<u8>>> = scans
|
|
296
|
+
.iter()
|
|
297
|
+
.enumerate()
|
|
298
|
+
.map(|(d, sc)| match (sc.ok, sc.rest, sc.end) {
|
|
299
|
+
(true, Some(r), Some(e)) if e - r <= INLINE_LIMIT => Some(module.functions[d].1[r..e].to_vec()),
|
|
300
|
+
_ => None,
|
|
301
|
+
})
|
|
302
|
+
.collect();
|
|
303
|
+
let mut edits: Vec<Vec<Edit>> = (0..n).map(|_| Vec::new()).collect();
|
|
304
|
+
for (d, cs) in calls.iter().enumerate() {
|
|
305
|
+
for &(f, start, end, tail) in cs {
|
|
306
|
+
let Some(callee) = f.checked_sub(imports).map(|c| c as usize) else { continue };
|
|
307
|
+
let Some(Some(code)) = inline.get(callee) else { continue };
|
|
308
|
+
if callee == d {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
let mut with = code.clone();
|
|
312
|
+
if tail {
|
|
313
|
+
with.push(0x0F);
|
|
314
|
+
}
|
|
315
|
+
edits[d].push(Edit { start, end, with });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
Ok(applyAll(module, edits))
|
|
319
|
+
}
|
compiler/wasm-codegen/tests/wasm_codegen/optimize_test.rs
CHANGED
|
@@ -137,6 +137,27 @@ fn functionTypesAreDeclaredOnce() {
|
|
|
137
137
|
assert!(seen.len() > 3, "{seen:?}");
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
/// A small straight-line accessor (`Str.length`: load the parameter, read a field, wrap)
|
|
141
|
+
/// is copied into its callers, and with no call left it is not in the module.
|
|
142
|
+
#[test]
|
|
143
|
+
fn tinyAccessorIsInlined() {
|
|
144
|
+
let src = "\
|
|
145
|
+
import std/Int
|
|
146
|
+
import std/Str
|
|
147
|
+
|
|
148
|
+
fun count(s: Str, n: Int) -> Int =
|
|
149
|
+
if n == 0 then s.length() else count(s, n - 1) + s.length()
|
|
150
|
+
|
|
151
|
+
fun main() -> Int =
|
|
152
|
+
count(\"hello\", 2)
|
|
153
|
+
";
|
|
154
|
+
let all = bodies(&compile("inline", src));
|
|
155
|
+
let names: Vec<&str> = all.iter().map(|b| b.0.as_str()).collect();
|
|
156
|
+
assert!(!names.contains(&"Str.length"), "{names:?}");
|
|
157
|
+
assert!(names.contains(&"count"), "{names:?}");
|
|
158
|
+
assert_eq!(runInt("inline", src), 15);
|
|
159
|
+
}
|
|
160
|
+
|
|
140
161
|
/// A fn's closures get distinct names: `f.closure`, `f.closure2`.
|
|
141
162
|
#[test]
|
|
142
163
|
fn closuresOfOneFnHaveDistinctNames() {
|