Skip to content

WIP: codeGen: Optimize continuation arguments

Ben Gamari requested to merge wip/T16098 into master

This diff implements code generation for continuation arguments as proposed in #16098 . For primops like catch#/with#/mask/... which are defined in the runtime-system there is no mechanism for inlining them. This often leads to otherwise unnecessary allocations of closures.

See also #17760 (closed)

This patch introduces the notion of continuation arguments to code generation, including:

  • A way to control CorePrep to not ANFize certain primops. That is, leaving arguments of the form State# s -> (# State# s, a #) in defined positions.
  • Teaching CoreToStg how to translate these to STG by extending STG language. Namely the GenStgArg type.
  • Inline primops and continuation in code generation.

This patch happily inlines catch#:

I64[Sp - 24] = PicBaseReg + stg_catch_frame_info;
I64[Sp - 16] = %MO_UU_Conv_W32_W64(I32[I64[BaseReg + 872] + 28]);
I64[Sp - 8] = PicBaseReg + (Test.someHandler_closure+2);


  • ... call to runtime system
  • ... allocation
  • ... copying of free variables


Currently this is implemented only for catch# primop. Once we agree to merge this I will bring in the rest.

P.S. Also StgCse and Unarise are broken for continuation arguments. I will fix that in the coming days.

Note that this was originally authored by Alex Biehl. I am simply posting a rebased version of his patch (Phab:D4647).

Edited by Simon Peyton Jones

Merge request reports