ManticoreProject / manticore

Parallel ML compiler
http://manticore.cs.uchicago.edu
MIT License
71 stars 7 forks source link

Wrapping captures in the context of an unused return cont #2

Closed kavon closed 6 years ago

kavon commented 6 years ago

Before wrap-captures we have

fun contFib_uncurried<C6EA>#4.4 (unboxP<C6F5>#9:int,param<C6F6>#4.4:cont(any) / retK<C6F7>#0:cont(int),_exh<C6F8>#4:cont(any)) =
(* ... *)
  cont currentContinuation<C717>#1 (ccArg<C718>#1:any) =
      let c<C712>#1:[int] = ([int])ccArg<C718>#1
      let _t<C713>#1:int = #0(c<C70B>#1)
      let _t<C714>#1:int = #0(c<C712>#1)
      let _t<C715>#1:int = I32Add(_t<C713>#1,_t<C714>#1)
      let res<C716>#1:[int] = alloc (_t<C715>#1)
      throw param<C6F6>#4.4 res<C716>#1
  (**** End currentContinuation<C717>#1 ****)
  apply contFib_uncurried<C6EA>#4.4 (_t<C70D>#1,currentContinuation<C717>#1 / letJoinK<C85D>#1,_exh<C6F8>#4)

Note that contFib_uncurried does not return. We're currently generating the following wrapping

fun manipK2<C93C>#1 (currentContinuation<C939>#1:cont(any) / landingPadK<C93A>#1.1:cont(int,any),deadExnK<C93B>#0:cont(any)) =
    cont invokeRetk<C93D>#0 (param<C93E>#1:any) =
        let t<C93F>#1:int = 1:int
        throw landingPadK<C93A>#1.1 (t<C93F>#1,param<C93E>#1)
    (* ... *)

cont setjmpLandingPad<C933>#1 (regularRet<C934>#1:int,arg<C935>#2:any) =
    let t<C938>#1:int = 0:int
    if I32NEq(regularRet<C934>#1,t<C938>#1) then
      let arg<C936>#1:int = (int)arg<C935>#2  (* invalid cast! *)
      throw retK<C6F7>#2.2 arg<C936>#1        (* this return never will happen *)
    else
      let arg<C937>#1:any = (any)arg<C935>#2
      throw currentContinuation<C717>#1.1 arg<C937>#1

callec (manipK2<C93C>#1 / setjmpLandingPad<C933>#1,deadExh<C941>#1)

Previously this was just a bit inefficient, but it's also incorrect since an we don't know how to match up the types even if a return did occur (the unboxing of the int).

It's better to just omit the landing pad in this case and just generate:

callec (manipK2<C93C>#1 / currentContinuation<C717>#1,deadExh<C941>#1)

so that C717 turns into a return cont, instead of a join cont.

kavon commented 6 years ago

This problem was fixed in the following series of commits:

https://github.com/ManticoreProject/manticore/compare/a3db8ed129b95b9780de06dcb60126c738e98bf8...08e604ff3d51d9490e416c50c7be55b82d1f4483