inlining defeats seq
Consider this module:
```
module Q (tcExtendIdEnv2) where
-- Interesting code:
tcExtendIdEnv2 :: M a
tcExtendIdEnv2 = do env <- getEnv
let level :: Int
level = thLevel (tcl_th_ctxt env)
level `seq` tc_extend_local_id_env level
{-# NOINLINE tc_extend_local_id_env #-}
tc_extend_local_id_env :: Int -> M a
tc_extend_local_id_env th_lvl = if read "foo"
then th_lvl `seq` return undefined
else return undefined
thLevel :: ThStage -> Int
thLevel Comp = 0
thLevel (Splice l) = l
thLevel (Brack l) = l
-- Dull code:
type M a = IOEnv TcLclEnv a
data TcLclEnv = TcLclEnv { tcl_th_ctxt :: !ThStage }
data ThStage = Comp | Splice Int | Brack Int
getEnv :: IOEnv env env
getEnv = IOEnv (\ env -> return env)
newtype IOEnv env a = IOEnv { unIOEnv :: env -> IO a }
instance Monad (IOEnv m) where
IOEnv m >>= f = IOEnv (\ env -> do r <- m env
unIOEnv (f r) env )
return a = IOEnv (\ _ -> return a)
fail = error
```
Compiling with
```
ghc -O -ddump-simpl -ddump-stg -c Q.hs
```
we get, in the STG,
```
Q.$wa =
\r srt:(0,*bitmap*) [ww_sDx w_sDO]
case
case ww_sDx of wild_sEs {
Q.Comp -> Q.lvl;
Q.Splice l_sDA -> l_sDA;
Q.Brack l_sDC -> l_sDC;
}
of
tpl_sEt
{ GHC.Base.I# ipv_sEu ->
let { sat_sDN = NO_CCS Q.TcLclEnv! [ww_sDx]; } in
let {
sat_sDL =
\u []
case ww_sDx of wild_sEv {
Q.Comp -> Q.lvl;
Q.Splice l_sDH -> l_sDH;
Q.Brack l_sDJ -> l_sDJ;
};
} in Q.tc_extend_local_id_env sat_sDL sat_sDN w_sDO;
};
```
GHC seems to have inlined `level`, forced it (due to the seq), but passed along a second, inlined, unevaluated copy to `tc_extend_local_id_env`. So the whole environment is retained, defeating the purpose of the seq!
If I mark `level` as `NOINLINE` then the STG looks like this:
```
Q.a5 =
\r srt:(0,*bitmap*) [env_sD1 eta_sDh]
case env_sD1 of tpl_sDg {
Q.TcLclEnv ipv_sD5 ->
case
case ipv_sD5 of wild_sDN {
Q.Comp -> Q.lvl;
Q.Splice l_sD8 -> l_sD8;
Q.Brack l_sDa -> l_sDa;
}
of
level_sDc
{ __DEFAULT ->
case level_sDc of tpl1_sDf {
GHC.Base.I# ipv1_sDO -> Q.tc_extend_local_id_env tpl1_sDf tpl_sDg eta_sDh;
};
};
};
```
which fixes the env-retained problem, although I don't understand why two cases are done.
It would be nice not to have to resort to this level of trickery, though!
<details><summary>Trac metadata</summary>
| Trac field | Value |
| ---------------------- | ------------ |
| Version | 6.9 |
| Type | Bug |
| TypeOfFailure | OtherFailure |
| Priority | normal |
| Resolution | Unresolved |
| Component | Compiler |
| Test case | |
| Differential revisions | |
| BlockedBy | |
| Related | |
| Blocking | |
| CC | |
| Operating system | Unknown |
| Architecture | Unknown |
</details>
<!-- {"blocked_by":[],"summary":"inlining defeats seq","status":"New","operating_system":"Unknown","component":"Compiler","related":[],"milestone":"","resolution":"Unresolved","owner":{"tag":"Unowned"},"version":"6.9","keywords":[],"differentials":[],"test_case":"","architecture":"Unknown","cc":[""],"type":"Bug","description":"Consider this module:\r\n{{{\r\nmodule Q (tcExtendIdEnv2) where\r\n\r\n-- Interesting code:\r\n\r\ntcExtendIdEnv2 :: M a\r\ntcExtendIdEnv2 = do env <- getEnv\r\n let level :: Int\r\n level = thLevel (tcl_th_ctxt env)\r\n level `seq` tc_extend_local_id_env level\r\n\r\n{-# NOINLINE tc_extend_local_id_env #-}\r\ntc_extend_local_id_env :: Int -> M a\r\ntc_extend_local_id_env th_lvl = if read \"foo\"\r\n then th_lvl `seq` return undefined\r\n else return undefined\r\n\r\nthLevel :: ThStage -> Int\r\nthLevel Comp = 0\r\nthLevel (Splice l) = l\r\nthLevel (Brack l) = l\r\n\r\n-- Dull code:\r\n\r\ntype M a = IOEnv TcLclEnv a\r\n\r\ndata TcLclEnv = TcLclEnv { tcl_th_ctxt :: !ThStage }\r\n\r\ndata ThStage = Comp | Splice Int | Brack Int\r\n\r\ngetEnv :: IOEnv env env\r\ngetEnv = IOEnv (\\ env -> return env)\r\n\r\nnewtype IOEnv env a = IOEnv { unIOEnv :: env -> IO a }\r\n\r\ninstance Monad (IOEnv m) where\r\n IOEnv m >>= f = IOEnv (\\ env -> do r <- m env\r\n unIOEnv (f r) env )\r\n return a = IOEnv (\\ _ -> return a)\r\n fail = error\r\n}}}\r\nCompiling with\r\n{{{\r\nghc -O -ddump-simpl -ddump-stg -c Q.hs\r\n}}}\r\nwe get, in the STG,\r\n{{{\r\nQ.$wa =\r\n \\r srt:(0,*bitmap*) [ww_sDx w_sDO]\r\n case\r\n case ww_sDx of wild_sEs {\r\n Q.Comp -> Q.lvl;\r\n Q.Splice l_sDA -> l_sDA;\r\n Q.Brack l_sDC -> l_sDC;\r\n }\r\n of\r\n tpl_sEt\r\n { GHC.Base.I# ipv_sEu ->\r\n let { sat_sDN = NO_CCS Q.TcLclEnv! [ww_sDx]; } in\r\n let {\r\n sat_sDL =\r\n \\u []\r\n case ww_sDx of wild_sEv {\r\n Q.Comp -> Q.lvl;\r\n Q.Splice l_sDH -> l_sDH;\r\n Q.Brack l_sDJ -> l_sDJ;\r\n };\r\n } in Q.tc_extend_local_id_env sat_sDL sat_sDN w_sDO;\r\n };\r\n}}}\r\nGHC seems to have inlined `level`, forced it (due to the seq), but passed along a second, inlined, unevaluated copy to `tc_extend_local_id_env`. So the whole environment is retained, defeating the purpose of the seq!\r\n\r\nIf I mark `level` as `NOINLINE` then the STG looks like this:\r\n{{{\r\nQ.a5 =\r\n \\r srt:(0,*bitmap*) [env_sD1 eta_sDh]\r\n case env_sD1 of tpl_sDg {\r\n Q.TcLclEnv ipv_sD5 ->\r\n case\r\n case ipv_sD5 of wild_sDN {\r\n Q.Comp -> Q.lvl;\r\n Q.Splice l_sD8 -> l_sD8;\r\n Q.Brack l_sDa -> l_sDa;\r\n }\r\n of\r\n level_sDc\r\n { __DEFAULT ->\r\n case level_sDc of tpl1_sDf {\r\n GHC.Base.I# ipv1_sDO -> Q.tc_extend_local_id_env tpl1_sDf tpl_sDg eta_sDh;\r\n };\r\n };\r\n };\r\n}}}\r\nwhich fixes the env-retained problem, although I don't understand why two cases are done.\r\n\r\nIt would be nice not to have to resort to this level of trickery, though!\r\n","type_of_failure":"OtherFailure","blocking":[]} -->
issue