| @@ -125,6 +125,26 @@ impl Sinks { |
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | +/// The fields of `value` (a construction) that receive the same binding |
| 129 | +/// the release moved out of the same slot: their bits are already right, |
| 130 | +/// so the reuse leaves them unwritten (ADR 0040, kept fields; issue |
| 131 | +/// #193). Positional: the cell's field `j` and the constructor's, whatever |
| 132 | +/// the tags. |
| 133 | +fn keep_fields(value: &Value, released: &[(u32, VarId)]) -> Vec<u32> { |
| 134 | + let args: &[Atom] = match value { |
| 135 | + Value::Construct { args, .. } => args, |
| 136 | + Value::Tuple(args) => args, |
| 137 | + _ => return Vec::new(), |
| 138 | + }; |
| 139 | + args.iter() |
| 140 | + .enumerate() |
| 141 | + .filter(|(j, a)| { |
| 142 | + matches!(a, Atom::Var(b) if released.iter().any(|&(i, r)| i as usize == *j && r == *b)) |
| 143 | + }) |
| 144 | + .map(|(j, _)| j as u32) |
| 145 | + .collect() |
| 146 | +} |
| 147 | + |
| 128 | 148 | /// The sizes of the constructions in `e` that reuse no cell. |
| 129 | 149 | fn free_sizes(e: &Expr, out: &mut BTreeSet<u32>) { |
| 130 | 150 | match e { |
| @@ -167,6 +187,9 @@ pub(crate) struct Specialiser<'a> { |
| 167 | 187 | /// The next variable number (tokens are new variables). |
| 168 | 188 | pub(crate) n_vars: u32, |
| 169 | 189 | pub(crate) sinks: &'a mut Sinks, |
| 190 | + /// The union of the kept fields of the reuses the current `place` |
| 191 | + /// made: the release's own keep set (ADR 0040, issue #193). |
| 192 | + pub(crate) keep: Vec<u32>, |
| 170 | 193 | } |
| 171 | 194 | |
| 172 | 195 | impl Specialiser<'_> { |
| @@ -266,9 +289,12 @@ impl Specialiser<'_> { |
| 266 | 289 | let arity = self.arity_of(var, tags); |
| 267 | 290 | let fit = arity.map_or(Fit::AtLeast(min_fields), Fit::Exact); |
| 268 | 291 | let mut token = None; |
| 292 | + let mut keep = Vec::new(); |
| 269 | 293 | if cell && has_candidate(&e, fit) { |
| 270 | 294 | let t = self.fresh(); |
| 271 | | − e = self.place(e, t, fit); |
| 295 | + self.keep.clear(); |
| 296 | + e = self.place(e, t, fit, &fields); |
| 297 | + keep = std::mem::take(&mut self.keep); |
| 272 | 298 | token = Some(t); |
| 273 | 299 | } else if cell && let Some(n) = arity { |
| 274 | 300 | let t = self.fresh(); |
| @@ -290,6 +316,7 @@ impl Specialiser<'_> { |
| 290 | 316 | var, |
| 291 | 317 | fields, |
| 292 | 318 | token, |
| 319 | + keep, |
| 293 | 320 | rest: Box::new(e), |
| 294 | 321 | } |
| 295 | 322 | } |
| @@ -303,22 +330,47 @@ impl Specialiser<'_> { |
| 303 | 330 | /// token `t`; on a path without one, give it to a call, or drop it on |
| 304 | 331 | /// entry. `e` must have one |
| 305 | 332 | /// ([`has_candidate`]). |
| 306 | | − pub(crate) fn place(&mut self, e: Expr, t: VarId, fit: Fit) -> Expr { |
| 333 | + pub(crate) fn place(&mut self, e: Expr, t: VarId, fit: Fit, released: &[(u32, VarId)]) -> Expr { |
| 307 | 334 | match e { |
| 308 | 335 | Expr::Let(v, value, rest, span) => match *value { |
| 309 | | − value if fit.fits(&value) => Expr::Let( |
| 310 | | − v, |
| 311 | | − Box::new(Value::Reuse { |
| 312 | | − token: t, |
| 313 | | − value: Box::new(value), |
| 314 | | − sized: matches!(fit, Fit::Exact(_)), |
| 315 | | − }), |
| 316 | | − rest, |
| 317 | | − span, |
| 318 | | − ), |
| 336 | + value if fit.fits(&value) => { |
| 337 | + // Kept fields (ADR 0040 Revision 2, issue #193): |
| 338 | + // sized reuses only, and the release's token helper |
| 339 | + // carries the set in two words, so never past field |
| 340 | + // 127. The union over paths goes to the release. |
| 341 | + // LOUISE_NO_KEPT_REUSE turns it off. |
| 342 | + let keep = if matches!(fit, Fit::Exact(_)) |
| 343 | + && std::env::var_os("LOUISE_NO_KEPT_REUSE").is_none() |
| 344 | + { |
| 345 | + let keep = keep_fields(&value, released); |
| 346 | + if keep.iter().all(|&k| k < 128) { |
| 347 | + keep |
| 348 | + } else { |
| 349 | + Vec::new() |
| 350 | + } |
| 351 | + } else { |
| 352 | + Vec::new() |
| 353 | + }; |
| 354 | + for &k in &keep { |
| 355 | + if !self.keep.contains(&k) { |
| 356 | + self.keep.push(k); |
| 357 | + } |
| 358 | + } |
| 359 | + Expr::Let( |
| 360 | + v, |
| 361 | + Box::new(Value::Reuse { |
| 362 | + token: t, |
| 363 | + value: Box::new(value), |
| 364 | + sized: matches!(fit, Fit::Exact(_)), |
| 365 | + keep, |
| 366 | + }), |
| 367 | + rest, |
| 368 | + span, |
| 369 | + ) |
| 370 | + } |
| 319 | 371 | Value::Block(inner) if has_candidate(&inner, fit) => Expr::Let( |
| 320 | 372 | v, |
| 321 | | − Box::new(Value::Block(Box::new(self.place(*inner, t, fit)))), |
| 373 | + Box::new(Value::Block(Box::new(self.place(*inner, t, fit, released)))), |
| 322 | 374 | rest, |
| 323 | 375 | span, |
| 324 | 376 | ), |
| @@ -327,47 +379,52 @@ impl Specialiser<'_> { |
| 327 | 379 | Value::Block(inner) => Expr::Let( |
| 328 | 380 | v, |
| 329 | 381 | Box::new(Value::Block(Box::new(drop_on_exit(*inner, t)))), |
| 330 | | − Box::new(self.place(*rest, t, fit)), |
| 382 | + Box::new(self.place(*rest, t, fit, released)), |
| 331 | 383 | span, |
| 332 | 384 | ), |
| 333 | 385 | value => Expr::Let( |
| 334 | 386 | v, |
| 335 | 387 | Box::new(value), |
| 336 | | − Box::new(self.place(*rest, t, fit)), |
| 388 | + Box::new(self.place(*rest, t, fit, released)), |
| 337 | 389 | span, |
| 338 | 390 | ), |
| 339 | 391 | }, |
| 340 | | − Expr::LetN(vs, value, rest, span) => { |
| 341 | | − Expr::LetN(vs, value, Box::new(self.place(*rest, t, fit)), span) |
| 342 | | − } |
| 343 | | − Expr::Dup(v, rest) => Expr::Dup(v, Box::new(self.place(*rest, t, fit))), |
| 344 | | − Expr::Drop(v, rest) => Expr::Drop(v, Box::new(self.place(*rest, t, fit))), |
| 392 | + Expr::LetN(vs, value, rest, span) => Expr::LetN( |
| 393 | + vs, |
| 394 | + value, |
| 395 | + Box::new(self.place(*rest, t, fit, released)), |
| 396 | + span, |
| 397 | + ), |
| 398 | + Expr::Dup(v, rest) => Expr::Dup(v, Box::new(self.place(*rest, t, fit, released))), |
| 399 | + Expr::Drop(v, rest) => Expr::Drop(v, Box::new(self.place(*rest, t, fit, released))), |
| 345 | 400 | Expr::Release { |
| 346 | 401 | var, |
| 347 | 402 | fields, |
| 348 | 403 | token, |
| 404 | + keep, |
| 349 | 405 | rest, |
| 350 | 406 | } => Expr::Release { |
| 351 | 407 | var, |
| 352 | 408 | fields, |
| 353 | 409 | token, |
| 354 | | − rest: Box::new(self.place(*rest, t, fit)), |
| 410 | + keep, |
| 411 | + rest: Box::new(self.place(*rest, t, fit, released)), |
| 355 | 412 | }, |
| 356 | 413 | Expr::Switch { on, cases, default } => Expr::Switch { |
| 357 | 414 | on, |
| 358 | 415 | cases: cases |
| 359 | 416 | .into_iter() |
| 360 | | − .map(|(c, b)| (c, self.branch(b, t, fit))) |
| 417 | + .map(|(c, b)| (c, self.branch(b, t, fit, released))) |
| 361 | 418 | .collect(), |
| 362 | | − default: default.map(|d| Box::new(self.branch(*d, t, fit))), |
| 419 | + default: default.map(|d| Box::new(self.branch(*d, t, fit, released))), |
| 363 | 420 | }, |
| 364 | 421 | Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 365 | 422 | on, |
| 366 | 423 | cases: cases |
| 367 | 424 | .into_iter() |
| 368 | | − .map(|(l, b)| (l, self.branch(b, t, fit))) |
| 425 | + .map(|(l, b)| (l, self.branch(b, t, fit, released))) |
| 369 | 426 | .collect(), |
| 370 | | − default: Box::new(self.branch(*default, t, fit)), |
| 427 | + default: Box::new(self.branch(*default, t, fit, released)), |
| 371 | 428 | }, |
| 372 | 429 | Expr::Exit(_) | Expr::Return(_) | Expr::Panic(_) | Expr::ReturnN(_) => { |
| 373 | 430 | unreachable!("no construction to reuse") |
| @@ -376,9 +433,9 @@ impl Specialiser<'_> { |
| 376 | 433 | } |
| 377 | 434 | |
| 378 | 435 | /// A branch of a placement: its construction, or a call, or a drop. |
| 379 | | − fn branch(&mut self, b: Expr, t: VarId, fit: Fit) -> Expr { |
| 436 | + fn branch(&mut self, b: Expr, t: VarId, fit: Fit, released: &[(u32, VarId)]) -> Expr { |
| 380 | 437 | if has_candidate(&b, fit) { |
| 381 | | − return self.place(b, t, fit); |
| 438 | + return self.place(b, t, fit, released); |
| 382 | 439 | } |
| 383 | 440 | let b = match fit { |
| 384 | 441 | Fit::Exact(n) => match self.pass_to_call(b, t, n) { |
| @@ -453,11 +510,13 @@ impl Specialiser<'_> { |
| 453 | 510 | var, |
| 454 | 511 | fields, |
| 455 | 512 | token, |
| 513 | + keep, |
| 456 | 514 | rest, |
| 457 | 515 | } => Expr::Release { |
| 458 | 516 | var, |
| 459 | 517 | fields, |
| 460 | 518 | token, |
| 519 | + keep, |
| 461 | 520 | rest: Box::new(self.rewrite_call(*rest, k - 1, t, arity)), |
| 462 | 521 | }, |
| 463 | 522 | _ => unreachable!("the call is on the straight path"), |
| @@ -616,11 +675,13 @@ fn drop_on_exit(e: Expr, t: VarId) -> Expr { |
| 616 | 675 | var, |
| 617 | 676 | fields, |
| 618 | 677 | token, |
| 678 | + keep, |
| 619 | 679 | rest, |
| 620 | 680 | } => Expr::Release { |
| 621 | 681 | var, |
| 622 | 682 | fields, |
| 623 | 683 | token, |
| 684 | + keep, |
| 624 | 685 | rest: Box::new(drop_on_exit(*rest, t)), |
| 625 | 686 | }, |
| 626 | 687 | Expr::Switch { on, cases, default } => Expr::Switch { |