diff options
author | Alex Biehl <alexbiehl@gmail.com> | 2020-01-27 12:35:12 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2020-01-27 13:03:38 -0500 |
commit | aa9c36352c9349341ef546567dec6a3ea1ff7233 (patch) | |
tree | 6446e3b13f7351383421896e796c670f49d09394 /compiler/GHC/CoreToStg.hs | |
parent | 97d0b0a367e4c6a52a17c3299439ac7de129da24 (diff) | |
download | haskell-wip/T16098.tar.gz |
codeGen: Optimize continuation argumentswip/T16098
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.
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);
...
```
No...
* ... call to runtime system
* ... allocation
* ... copying of free variables
needed
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.
[1] https://gitlab.haskell.org/ghc/ghc/blob/4898df1cc25132dc9e2599d4fa4e1bbc9423cda5/rts/Exception.cmm#L393
Diffstat (limited to 'compiler/GHC/CoreToStg.hs')
-rw-r--r-- | compiler/GHC/CoreToStg.hs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/compiler/GHC/CoreToStg.hs b/compiler/GHC/CoreToStg.hs index 83799f6e49..c61f378504 100644 --- a/compiler/GHC/CoreToStg.hs +++ b/compiler/GHC/CoreToStg.hs @@ -582,12 +582,21 @@ coreToStgArgs (arg : args) = do -- Non-type argument (stg_args, ticks) <- coreToStgArgs args arg' <- coreToStgExpr arg let + arg_ty = exprType arg (aticks, arg'') = stripStgTicksTop tickishFloatable arg' stg_arg = case arg'' of StgApp v [] -> StgVarArg v StgConApp con [] _ -> StgVarArg (dataConWorkId con) StgLit lit -> StgLitArg lit - _ -> pprPanic "coreToStgArgs" (ppr arg) + StgLam bndrs body -> + let + bndr = case toList bndrs of + [x] -> x + xs -> + -- TODO: more informative error message + pprPanic "coreToStgArgs" (ppr arg'') + in StgContArg bndr body arg_ty + _ -> pprPanic "coreToStgArgs" (ppr arg'') -- WARNING: what if we have an argument like (v `cast` co) -- where 'co' changes the representation type? @@ -601,7 +610,7 @@ coreToStgArgs (arg : args) = do -- Non-type argument dflags <- getDynFlags let - arg_rep = typePrimRep (exprType arg) + arg_rep = typePrimRep arg_ty stg_arg_rep = typePrimRep (stgArgType stg_arg) bad_args = not (primRepsCompatible dflags arg_rep stg_arg_rep) |