diff options
author | Sebastian Graf <sebastian.graf@kit.edu> | 2020-03-25 13:49:14 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-04-02 01:46:34 -0400 |
commit | 42d68364f66846969edf029f878875c10cdfe0b2 (patch) | |
tree | f84ad0c7e595f753055a61a14dfcc9759b1d963a /compiler/GHC | |
parent | b943b25d0786da64031ac63ddf9b4574182057bb (diff) | |
download | haskell-42d68364f66846969edf029f878875c10cdfe0b2.tar.gz |
Preserve precise exceptions in strictness analysis
Fix #13380 and #17676 by
1. Changing `raiseIO#` to have `topDiv` instead of `botDiv`
2. Give it special treatment in `Simplifier.Util.mkArgInfo`, treating it
as if it still had `botDiv`, to recover dead code elimination.
This is the first commit of the plan outlined in
https://gitlab.haskell.org/ghc/ghc/-/merge_requests/2525#note_260886.
Diffstat (limited to 'compiler/GHC')
-rw-r--r-- | compiler/GHC/Core/Op/Simplify/Utils.hs | 9 | ||||
-rw-r--r-- | compiler/GHC/Types/Demand.hs | 48 |
2 files changed, 54 insertions, 3 deletions
diff --git a/compiler/GHC/Core/Op/Simplify/Utils.hs b/compiler/GHC/Core/Op/Simplify/Utils.hs index 1b8c21f81b..17f2a73416 100644 --- a/compiler/GHC/Core/Op/Simplify/Utils.hs +++ b/compiler/GHC/Core/Op/Simplify/Utils.hs @@ -56,12 +56,13 @@ import GHC.Types.Id import GHC.Types.Id.Info import GHC.Types.Var import GHC.Types.Demand +import GHC.Types.Var.Set +import GHC.Types.Basic +import PrimOp import GHC.Core.Op.Simplify.Monad import GHC.Core.Type hiding( substTy ) import GHC.Core.Coercion hiding( substCo ) import GHC.Core.DataCon ( dataConWorkId, isNullaryRepDataCon ) -import GHC.Types.Var.Set -import GHC.Types.Basic import Util import OrdList ( isNilOL ) import MonadUtils @@ -500,7 +501,9 @@ mkArgInfo env fun rules n_val_args call_cont -- calls to error. But now we are more careful about -- inlining lone variables, so it's ok -- (see GHC.Core.Op.Simplify.Utils.analyseCont) - if isBotDiv result_info then + -- See Note [Precise exceptions and strictness analysis] in Demand.hs + -- for the special case on raiseIO# + if isBotDiv result_info || isPrimOpId_maybe fun == Just RaiseIOOp then map isStrictDmd demands -- Finite => result is bottom else map isStrictDmd demands ++ vanilla_stricts diff --git a/compiler/GHC/Types/Demand.hs b/compiler/GHC/Types/Demand.hs index f9ca821872..fe82d473b2 100644 --- a/compiler/GHC/Types/Demand.hs +++ b/compiler/GHC/Types/Demand.hs @@ -931,6 +931,54 @@ instance Outputable Divergence where ppr Diverges = char 'b' ppr Dunno = empty +{- Note [Precise vs imprecise exceptions] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +An exception is considered to be /precise/ when it is thrown by the 'raiseIO#' +primop. It follows that all other primops (such as 'raise#' or +division-by-zero) throw /imprecise/ exceptions. Note that the actual type of +the exception thrown doesn't have any impact! + +GHC undertakes some effort not to apply an optimisation that would mask a +/precise/ exception with some other source of nontermination, such as genuine +divergence or an imprecise exception, so that the user can reliably +intercept the precise exception with a catch handler before and after +optimisations. + +See also the wiki page on precise exceptions: +https://gitlab.haskell.org/ghc/ghc/wikis/exceptions/precise-exceptions +Section 5 of "Tackling the awkward squad" talks about semantic concerns. +Imprecise exceptions are actually more interesting than precise ones (which are +fairly standard) from the perspective of semantics. See the paper "A Semantics +for Imprecise Exceptions" for more details. + +Note [Precise exceptions and strictness analysis] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +raiseIO# raises a *precise* exception, in contrast to raise# which +raise an *imprecise* exception. See Note [Precise vs imprecise exceptions] +in XXXX. + +Unlike raise# (which returns botDiv), we want raiseIO# to return topDiv. +Here's why. Consider this example from #13380 (similarly #17676): + f x y | x>0 = raiseIO Exc + | y>0 = return 1 + | otherwise = return 2 +Is 'f' strict in 'y'? One might be tempted to say yes! But that plays fast and +loose with the precise exception; after optimisation, (f 42 (error "boom")) +turns from throwing the precise Exc to throwing the imprecise user error +"boom". So, the defaultDmd of raiseIO# should be lazy (topDmd), which can be +achieved by giving it divergence topDiv. + +But if it returns topDiv, the simplifier will fail to discard raiseIO#'s +continuation in + case raiseIO# x s of { (# s', r #) -> <BIG> } +which we'd like to optimise to + raiseIO# x s +Temporary hack solution: special treatment for raiseIO# in +Simplifier.Utils.mkArgInfo. For the non-hack solution, see +https://gitlab.haskell.org/ghc/ghc/wikis/fixing-precise-exceptions#replacing-hacks-by-principled-program-analyses +-} + + ------------------------------------------------------------------------ -- Combined demand result -- ------------------------------------------------------------------------ |