diff options
author | Sebastian Graf <sebastian.graf@kit.edu> | 2023-01-31 17:16:01 +0100 |
---|---|---|
committer | Sebastian Graf <sebastian.graf@kit.edu> | 2023-03-10 18:43:00 +0100 |
commit | c870da6a6309282b829748c9ac8bed72f295f1af (patch) | |
tree | c1aa11d01f47e6b3fa1edd13b3cef7586cf04595 /compiler/GHC/Utils/Misc.hs | |
parent | 8ca0c05b598353177cec46d4a508ea725d282f09 (diff) | |
download | haskell-wip/T20749.tar.gz |
Make DataCon workers strict in strict fields (#20749)wip/T20749
This patch tweaks `exprIsConApp_maybe`, `exprIsHNF` and friends, and Demand
Analysis so that they exploit and maintain strictness of DataCon workers. See
`Note [Strict fields in Core]` for details.
Very little needed to change, and it puts field seq insertion done by Tag
Inference into a new perspective: That of *implementing* strict field semantics.
Before Tag Inference, DataCon workers are strict. Afterwards they are
effectively lazy and field seqs happen around use sites. History has shown
that there is no other way to guarantee taggedness and thus the STG Strict Field
Invariant.
Knock-on changes:
* `exprIsHNF` previously used `exprOkForSpeculation` on unlifted arguments
instead of recursing into `exprIsHNF`. That regressed the termination
analysis in CPR analysis (which simply calls out to `exprIsHNF`), so I made
it call `exprOkForSpeculation`, too.
* There's a small regression in Demand Analysis, visible in the changed test
output of T16859: Previously, a field seq on a variable would give that
variable a "used exactly once" demand, now it's "used at least once",
because `dmdTransformDataConSig` accounts for future uses of the field
that actually all go through the case binder (and hence won't re-enter the
potential thunk). The difference should hardly be observable.
* The Simplifier's fast path for data constructors only applies to lazy
data constructors now. I observed regressions involving Data.Binary.Put's
`Pair` data type.
* Unfortunately, T21392 does no longer reproduce after this patch, so I marked
it as "not broken" in order to track whether we regress again in the future.
Fixes #20749, the satisfying conclusion of an annoying saga (cf. the ideas
in #21497 and #22475).
Diffstat (limited to 'compiler/GHC/Utils/Misc.hs')
-rw-r--r-- | compiler/GHC/Utils/Misc.hs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/compiler/GHC/Utils/Misc.hs b/compiler/GHC/Utils/Misc.hs index af32a679bb..8dd60b5b64 100644 --- a/compiler/GHC/Utils/Misc.hs +++ b/compiler/GHC/Utils/Misc.hs @@ -27,7 +27,7 @@ module GHC.Utils.Misc ( dropWhileEndLE, spanEnd, last2, lastMaybe, onJust, - List.foldl1', foldl2, count, countWhile, all2, + List.foldl1', foldl2, count, countWhile, all2, all2Prefix, all3Prefix, lengthExceeds, lengthIs, lengthIsNot, lengthAtLeast, lengthAtMost, lengthLessThan, @@ -646,6 +646,25 @@ all2 _ [] [] = True all2 p (x:xs) (y:ys) = p x y && all2 p xs ys all2 _ _ _ = False +all2Prefix :: (a -> b -> Bool) -> [a] -> [b] -> Bool +-- ^ `all2Prefix p xs ys` is a fused version of `and $ zipWith2 p xs ys`. +-- So if one list is shorter than the other, `p` is assumed to be `True` for the +-- suffix. +all2Prefix p xs ys = go xs ys + where go (x:xs) (y:ys) = p x y && go xs ys + go _ _ = True +{-# INLINABLE all2Prefix #-} + +all3Prefix :: (a -> b -> c -> Bool) -> [a] -> [b] -> [c] -> Bool +-- ^ `all3Prefix p xs ys zs` is a fused version of `and $ zipWith3 p xs ys zs`. +-- So if one list is shorter than the others, `p` is assumed to be `True` for +-- the suffix. +all3Prefix p xs ys zs = go xs ys zs + where + go (x:xs) (y:ys) (z:zs) = p x y z && go xs ys zs + go _ _ _ = True +{-# INLINABLE all3Prefix #-} + -- Count the number of times a predicate is true count :: (a -> Bool) -> [a] -> Int |