Simon Peyton Jones pushed to branch wip/T28169 at Glasgow Haskell Compiler / GHC
Commits:
-
e832b4d2
by Simon Peyton Jones at 2026-11-25T00:48:55+00:00
5 changed files:
- compiler/GHC/Core/DataCon.hs
- compiler/GHC/Core/SimpleOpt.hs
- compiler/GHC/Core/TyCon.hs
- compiler/GHC/Core/Utils.hs
- compiler/GHC/Tc/TyCl/Build.hs
Changes:
| ... | ... | @@ -52,7 +52,6 @@ module GHC.Core.DataCon ( |
| 52 | 52 | |
| 53 | 53 | -- ** Predicates on DataCons
|
| 54 | 54 | isNullarySrcDataCon, isNullaryRepDataCon,
|
| 55 | - isLazyDataConRep,
|
|
| 56 | 55 | isTupleDataCon, isBoxedTupleDataCon, isUnboxedTupleDataCon,
|
| 57 | 56 | isUnboxedSumDataCon, isCovertGadtDataCon, isUnaryClassDataCon,
|
| 58 | 57 | isVanillaDataCon, isNewDataCon, isTypeDataCon, isBoxingDataCon,
|
| ... | ... | @@ -524,21 +523,27 @@ data DataCon |
| 524 | 523 | -- See Note [Bangs on data constructor arguments]
|
| 525 | 524 | |
| 526 | 525 | dcStricts :: [StrictnessMark],
|
| 527 | - -- One mark for every field of the DataCon worker;
|
|
| 528 | - -- if it's empty, then all fields are lazy,
|
|
| 529 | - -- otherwise 1-1 with dataConRepArgTys.
|
|
| 526 | + -- One mark for every field of the DataCon /worker/
|
|
| 527 | + -- Lines up 1-1 with dataConRepArgTys
|
|
| 528 | + -- Relates only to user-specified strictness annotations,
|
|
| 529 | + -- not to liftedness or othewrise
|
|
| 530 | + -- E.g. data T a = MkT {-# UNPACK #-} !(S a) Int# !a
|
|
| 531 | + -- data S a = MkS a !a
|
|
| 532 | + -- Then the worker for MkT has four fields, the first two arising from
|
|
| 533 | + -- unpacking S. The corresponding strictness marks are [-,!,-,!]; again
|
|
| 534 | + -- the first two come from MkS
|
|
| 530 | 535 | -- See also Note [Strict fields in Core] in GHC.Core
|
| 531 | - -- for the effect on the strictness signature
|
|
| 536 | + -- for the effect on the strictness signature
|
|
| 532 | 537 | |
| 533 | 538 | dcFields :: [FieldLabel],
|
| 534 | 539 | -- Field labels for this constructor, in the
|
| 535 | 540 | -- same order as the dcOrigArgTys;
|
| 536 | 541 | -- length = 0 (if not a record) or dataConSourceArity.
|
| 537 | 542 | |
| 538 | - dcIsLazy :: Bool
|
|
| 543 | + dcIsLazy :: Bool,
|
|
| 539 | 544 | -- True if (a) all fields of the data con /worker/ are definitely lifted
|
| 540 | 545 | -- (b) no field has a `StrictnessMark` that forces evaluation
|
| 541 | - -- So if `dcIsLazy` is True then all fields are lazy thunks
|
|
| 546 | + -- So if `dcIsLazy` is True then all fields of the worker are lazy thunks
|
|
| 542 | 547 | |
| 543 | 548 | -- The curried worker function that corresponds to the constructor:
|
| 544 | 549 | -- It doesn't have an unfolding; the code generator saturates these Ids
|
| ... | ... | @@ -836,7 +841,7 @@ data DataConRep |
| 836 | 841 | |
| 837 | 842 | , dcr_boxer :: DataConBoxer
|
| 838 | 843 | |
| 839 | - , dcr_arg_tys :: [Scaled Type] -- Final, representation argument types,
|
|
| 844 | + , dcr_arg_tys :: [Scaled Type] -- Final, representation /value/ argument types,
|
|
| 840 | 845 | -- after unboxing and flattening,
|
| 841 | 846 | -- and *including* all evidence args
|
| 842 | 847 | |
| ... | ... | @@ -1139,30 +1144,39 @@ mkDataCon name declared_infix prom_info |
| 1139 | 1144 | = con
|
| 1140 | 1145 | where
|
| 1141 | 1146 | is_vanilla = null ex_tvs && null eq_spec && null theta
|
| 1142 | - str_marks' | no_strict_marks = []
|
|
| 1143 | - | otherwise = str_marks
|
|
| 1144 | - no_strict_marks = not $ any isMarkedStrict str_marks
|
|
| 1145 | - is_lazy = no_str_marks && all definitelyLiftedType rep_arg_tys
|
|
| 1146 | - |
|
| 1147 | - con = MkData {dcName = name, dcUnique = nameUnique name,
|
|
| 1148 | - dcVanilla = is_vanilla, dcInfix = declared_infix,
|
|
| 1149 | - dcUnivTyVars = univ_tvs,
|
|
| 1150 | - dcExTyCoVars = ex_tvs,
|
|
| 1151 | - dcConcreteTyVars = conc_tvs,
|
|
| 1152 | - dcUserTyVarBinders = user_tvbs,
|
|
| 1153 | - dcEqSpec = eq_spec,
|
|
| 1154 | - dcOtherTheta = theta,
|
|
| 1155 | - dcStupidTheta = stupid_theta,
|
|
| 1156 | - dcOrigArgTys = orig_arg_tys, dcOrigResTy = orig_res_ty,
|
|
| 1157 | - dcRepTyCon = rep_tycon,
|
|
| 1158 | - dcSrcBangs = arg_stricts, dcImplBangs = impl_bangs,
|
|
| 1159 | - dcStricts = str_marks', dcIsLazy = is_lazy,
|
|
| 1160 | - dcFields = fields, dcTag = tag, dcRepType = rep_ty,
|
|
| 1161 | - dcWorkId = work_id,
|
|
| 1162 | - dcRep = rep,
|
|
| 1163 | - dcSourceArity = length orig_arg_tys,
|
|
| 1164 | - dcRepArity = length rep_arg_tys + count isCoVar ex_tvs,
|
|
| 1165 | - dcPromoted = promoted }
|
|
| 1147 | + is_lazy = not (any isMarkedStrict str_marks)
|
|
| 1148 | + && all (definitelyLiftedType . scaledThing) rep_arg_tys
|
|
| 1149 | + |
|
| 1150 | + con = MkData { dcName = name
|
|
| 1151 | + , dcUnique = nameUnique name
|
|
| 1152 | + , dcInfix = declared_infix
|
|
| 1153 | + , dcTag = tag
|
|
| 1154 | + , dcVanilla = is_vanilla
|
|
| 1155 | + |
|
| 1156 | + , dcUnivTyVars = univ_tvs
|
|
| 1157 | + , dcExTyCoVars = ex_tvs
|
|
| 1158 | + , dcConcreteTyVars = conc_tvs
|
|
| 1159 | + , dcUserTyVarBinders = user_tvbs
|
|
| 1160 | + , dcEqSpec = eq_spec
|
|
| 1161 | + , dcOtherTheta = theta
|
|
| 1162 | + , dcStupidTheta = stupid_theta
|
|
| 1163 | + , dcOrigArgTys = orig_arg_tys
|
|
| 1164 | + , dcOrigResTy = orig_res_ty
|
|
| 1165 | + |
|
| 1166 | + , dcRep = rep
|
|
| 1167 | + , dcRepType = rep_ty
|
|
| 1168 | + , dcRepTyCon = rep_tycon
|
|
| 1169 | + , dcSrcBangs = arg_stricts
|
|
| 1170 | + , dcImplBangs = impl_bangs
|
|
| 1171 | + , dcStricts = assertPpr (equalLength str_marks rep_arg_tys)
|
|
| 1172 | + (ppr name <+> ppr str_marks)
|
|
| 1173 | + str_marks
|
|
| 1174 | + , dcIsLazy = is_lazy
|
|
| 1175 | + , dcFields = fields
|
|
| 1176 | + , dcWorkId = work_id
|
|
| 1177 | + , dcSourceArity = length orig_arg_tys
|
|
| 1178 | + , dcRepArity = length rep_arg_tys + count isCoVar ex_tvs
|
|
| 1179 | + , dcPromoted = promoted }
|
|
| 1166 | 1180 | |
| 1167 | 1181 | -- The 'arg_stricts' passed to mkDataCon are simply those for the
|
| 1168 | 1182 | -- source-language arguments. We add extra ones for the
|
| ... | ... | @@ -1170,8 +1184,7 @@ mkDataCon name declared_infix prom_info |
| 1170 | 1184 | |
| 1171 | 1185 | rep_arg_tys = dataConRepArgTys con
|
| 1172 | 1186 | |
| 1173 | - rep_ty =
|
|
| 1174 | - case rep of
|
|
| 1187 | + rep_ty = case rep of
|
|
| 1175 | 1188 | -- If the DataCon has no wrapper, then the worker's type *is* the
|
| 1176 | 1189 | -- user-facing type, so we can simply use dataConWrapperType.
|
| 1177 | 1190 | NoDataConRep -> dataConWrapperType con
|
| ... | ... | @@ -1407,23 +1420,13 @@ isNullarySrcDataCon dc = dataConSourceArity dc == 0 |
| 1407 | 1420 | isNullaryRepDataCon :: DataCon -> Bool
|
| 1408 | 1421 | isNullaryRepDataCon dc = dataConRepArity dc == 0
|
| 1409 | 1422 | |
| 1410 | -isLazyDataConRep :: DataCon -> Bool
|
|
| 1411 | --- ^ True <==> All fields are lazy
|
|
| 1412 | -isLazyDataConRep dc = null (dcStricts dc)
|
|
| 1413 | - |
|
| 1414 | 1423 | dataConRepStrictness :: DataCon -> [StrictnessMark]
|
| 1415 | 1424 | -- ^ Give the demands on the runtime arguments of a Core DataCon worker
|
| 1416 | --- application.
|
|
| 1417 | --- The length of the list matches `dataConRepArgTys` (e.g., the number
|
|
| 1418 | --- of runtime arguments).
|
|
| 1419 | -dataConRepStrictness dc
|
|
| 1420 | - | isLazyDataConRep dc
|
|
| 1421 | - = replicate (dataConRepArity dc) NotMarkedStrict
|
|
| 1422 | - | otherwise
|
|
| 1425 | +-- application. Maches 1-1 with `dataConRepArgTys`
|
|
| 1426 | +dataConRepStrictness = dcStricts
|
|
| 1423 | 1427 | |
| 1424 | - |
|
| 1425 | -dataConRepLazy :: DataCon -> Bool
|
|
| 1428 | +dataConRepIsLazy :: DataCon -> Bool
|
|
| 1429 | +dataConRepIsLazy = dcIsLazy
|
|
| 1426 | 1430 | |
| 1427 | 1431 | dataConImplBangs :: DataCon -> [HsImplBang]
|
| 1428 | 1432 | -- The implementation decisions about the strictness/unpack of each
|
| ... | ... | @@ -1726,10 +1726,8 @@ exprIsConApp_maybe ise@(ISE in_scope id_unf) expr |
| 1726 | 1726 | -- See Note [Strict fields in Core] for what a field seq is and (SFC2) for
|
| 1727 | 1727 | -- why we insert them
|
| 1728 | 1728 | mkFieldSeqFloats in_scope dc args
|
| 1729 | - | isLazyDataConRep dc
|
|
| 1730 | - = (in_scope, nilOL, args)
|
|
| 1731 | - | otherwise
|
|
| 1732 | - = (in_scope', floats', ty_args ++ val_args')
|
|
| 1729 | + | dataConRepIsLazy dc = (in_scope, nilOL, args) -- Short cut in common case
|
|
| 1730 | + | otherwise = (in_scope', floats', ty_args ++ val_args')
|
|
| 1733 | 1731 | where
|
| 1734 | 1732 | (ty_args, val_args) = splitAtList (dataConUnivAndExTyCoVars dc) args
|
| 1735 | 1733 | (in_scope', floats', val_args') = foldr do_one (in_scope, nilOL, []) $
|
| ... | ... | @@ -1504,14 +1504,16 @@ There are a number of wrinkles |
| 1504 | 1504 | (UCM2) The TyCon for a unary class is /not/ identified as a newtype.
|
| 1505 | 1505 | Rather, it has its own AlgTyConRhs, namely `UnaryClassTyCon`
|
| 1506 | 1506 | |
| 1507 | -(UCM3) Unlike non-unary classes, a value of type (C ty), where `C` is a unary
|
|
| 1507 | +(UCM3a) Unlike non-unary classes, a value of type (C ty), where `C` is a unary
|
|
| 1508 | 1508 | class, might be bottom, because it is represented by the method type alone.
|
| 1509 | 1509 | See GHC.Core.Type.isTerminatingType, and Note [NON-BOTTOM-DICTS invariant]
|
| 1510 | 1510 | in GHC.Core
|
| 1511 | 1511 | |
| 1512 | - Similarly in exprOkForSpeculation/exprOkToDiscard/exprOkForSpecEval,
|
|
| 1513 | - in GHC.Core.Utils. In the utility funcion `app_ok` we need a special
|
|
| 1514 | - case for the DFunIds; they generally terminate, but not for unary classes.
|
|
| 1512 | +(UCM3b) We treat the data constructor as if it had a strictness annotation
|
|
| 1513 | + data C a = MkC !(a->a)
|
|
| 1514 | + so that in exprOkForSpeculation/exprOkToDiscard/exprOkForSpecEval, and
|
|
| 1515 | + in exprIsHNF/exprIsConLike, we evaluate the argument. This strictness flag
|
|
| 1516 | + is set in `GHC.Tc.TyCl.buildClass
|
|
| 1515 | 1517 | |
| 1516 | 1518 | (UMC4) To avoid regressions, in Core we want to remember that
|
| 1517 | 1519 | (MkUC x) is really just x
|
| ... | ... | @@ -2648,9 +2648,9 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e |
| 2648 | 2648 | is_hnf_like e
|
| 2649 | 2649 | where
|
| 2650 | 2650 | is_hnf_like (Var v) -- NB: There are no value args at this point
|
| 2651 | - = id_app_is_value v [] -- Catches nullary constructors,
|
|
| 2652 | - -- so that [] and () are values, for example
|
|
| 2653 | - -- and (e.g.) primops that don't have unfoldings
|
|
| 2651 | + = check_app v [] -- Catches nullary constructors,
|
|
| 2652 | + -- so that [] and () are values, for example
|
|
| 2653 | + -- and (e.g.) primops that don't have unfoldings
|
|
| 2654 | 2654 | || is_con_unf (idUnfolding v)
|
| 2655 | 2655 | -- Check the thing's unfolding; it might be bound to a value
|
| 2656 | 2656 | -- or to a guaranteed-evaluated variable (isEvaldUnfolding)
|
| ... | ... | @@ -2685,46 +2685,37 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e |
| 2685 | 2685 | = is_hnf_like rhs
|
| 2686 | 2686 | is_hnf_like _ = False
|
| 2687 | 2687 | |
| 2688 | - -- Collect arguments through Casts and Ticks and call id_app_is_value
|
|
| 2688 | + -- Collect arguments through Casts and Ticks and call check_app
|
|
| 2689 | 2689 | app_is_value :: CoreExpr -> [CoreArg] -> Bool
|
| 2690 | - app_is_value (Var f) as = id_app_is_value f as
|
|
| 2690 | + app_is_value (Var f) as = check_app f as
|
|
| 2691 | 2691 | app_is_value (Tick _ f) as = app_is_value f as
|
| 2692 | 2692 | app_is_value (Cast f _) as = app_is_value f as
|
| 2693 | 2693 | app_is_value (App f a) as | isValArg a = app_is_value f (a:as)
|
| 2694 | 2694 | | otherwise = app_is_value f as
|
| 2695 | 2695 | app_is_value _ _ = False
|
| 2696 | 2696 | |
| 2697 | - id_app_is_value id val_args
|
|
| 2698 | - | Just dc <- isDataConWorkId_maybe id
|
|
| 2699 | - , isUnaryClassDataCon dc
|
|
| 2700 | - = all is_hnf_like val_args -- Look through unary class data cons
|
|
| 2701 | - | otherwise
|
|
| 2702 | - -- See Note [exprIsHNF for function applications]
|
|
| 2703 | - -- for the specification and examples
|
|
| 2697 | + -- check_app: See Note [exprIsHNF for function applications]
|
|
| 2698 | + -- for the specification and examples
|
|
| 2699 | + check_app id val_args
|
|
| 2704 | 2700 | = case compare (idArity id) (length val_args) of
|
| 2705 | - EQ | is_con id -> -- Saturated app of a DataCon/CONLIKE Id
|
|
| 2706 | - case mb_str_marks id of
|
|
| 2707 | - Just str_marks -> -- with strict fields; see (SFC1) of Note [Strict fields in Core]
|
|
| 2708 | - assert (val_args `equalLength` str_marks) $
|
|
| 2709 | - fields_hnf str_marks
|
|
| 2710 | - Nothing -> -- without strict fields: like PAP
|
|
| 2711 | - args_hnf -- NB: CONLIKEs are lazy!
|
|
| 2712 | - |
|
| 2713 | - GT -> -- PAP: Check unlifted val_args
|
|
| 2714 | - args_hnf
|
|
| 2715 | - |
|
| 2716 | - _ -> False
|
|
| 2701 | + EQ -> check_saturated id val_args -- Saturated application
|
|
| 2702 | + GT -> check_args id val_args -- Partial application
|
|
| 2703 | + LT -> False -- Over-saturated
|
|
| 2717 | 2704 | |
| 2705 | + check_saturated id val_args
|
|
| 2706 | + | Just dc <- isDataConWorkId_maybe id
|
|
| 2707 | + = -- Saturated data constructors; must account for the strictness flags
|
|
| 2708 | + all3Prefix check_field val_args (val_arg_tys id) (dataConStrictness dc)
|
|
| 2709 | + | is_con id -- Saturated CONLIKE: check the arguments
|
|
| 2710 | + = check_args id val_args
|
|
| 2711 | + | otherwise -- Saturated function, not a constructor => not HNF
|
|
| 2712 | + = False
|
|
| 2713 | + |
|
| 2714 | + -- check_args: check that any unlifted args are hnf-like
|
|
| 2715 | + check_args id val_args = assert (sameLength val_args arg_tys) $
|
|
| 2716 | + all2Prefix check_arg val_args arg_tys
|
|
| 2718 | 2717 | where
|
| 2719 | - -- Saturated, Strict DataCon: Check unlifted val_args and strict fields
|
|
| 2720 | - fields_hnf str_marks = all3Prefix check_field val_arg_tys str_marks val_args
|
|
| 2721 | - |
|
| 2722 | - -- PAP: Check unlifted val_args
|
|
| 2723 | - args_hnf = all2Prefix check_arg val_arg_tys val_args
|
|
| 2724 | - |
|
| 2725 | - val_arg_tys = splitUninstantiatedValArgTys (idType id)
|
|
| 2726 | - -- val_arg_tys = map exprType val_args, but much less costly.
|
|
| 2727 | - -- The obvious definition regresses T16577 by 30% so we don't do it.
|
|
| 2718 | + arg_tys = val_arg_tys id
|
|
| 2728 | 2719 | |
| 2729 | 2720 | check_arg a_ty a
|
| 2730 | 2721 | | mightBeUnliftedType a_ty = is_hnf_like a
|
| ... | ... | @@ -2735,18 +2726,18 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e |
| 2735 | 2726 | -- NB: We check arity (and CONLIKEness) first because it's cheaper
|
| 2736 | 2727 | -- and we reject quickly on saturated apps.
|
| 2737 | 2728 | |
| 2738 | - check_field a_ty str a
|
|
| 2739 | - | mightBeUnliftedType a_ty = is_hnf_like a
|
|
| 2740 | - | isMarkedStrict str = is_hnf_like a
|
|
| 2741 | - | otherwise = True
|
|
| 2742 | - -- isMarkedStrict: Respect Note [Strict fields in Core]
|
|
| 2743 | - |
|
| 2744 | - mb_str_marks id
|
|
| 2745 | - | Just dc <- isDataConWorkId_maybe id
|
|
| 2746 | - , not (isLazyDataConRep dc)
|
|
| 2747 | - = Just (dataConRepStrictness dc)
|
|
| 2748 | - | otherwise
|
|
| 2749 | - = Nothing
|
|
| 2729 | + -- check_field: used for a data constructor worker:
|
|
| 2730 | + -- check unlifted val_args and strict fields
|
|
| 2731 | + -- See (SFC1) of Note [Strict fields in Core]
|
|
| 2732 | + check_field a a_ty str
|
|
| 2733 | + | mightBeUnliftedType a_ty = is_hnf_like a
|
|
| 2734 | + | isMarkedStrict str = is_hnf_like a
|
|
| 2735 | + | otherwise = True
|
|
| 2736 | + |
|
| 2737 | + val_arg_tys id = splitUninstantiatedValArgTys (idType id)
|
|
| 2738 | + -- val_arg_tys = map exprType val_args, but much less costly.
|
|
| 2739 | + -- The obvious definition regresses T16577 by 30% so we don't do it.
|
|
| 2740 | + |
|
| 2750 | 2741 | |
| 2751 | 2742 | {-
|
| 2752 | 2743 | Note [exprIsHNF Tick]
|
| ... | ... | @@ -331,6 +331,9 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef |
| 331 | 331 | univ_bndrs = tyVarSpecToBinders $ tyConInvisTVBinders binders
|
| 332 | 332 | univ_tvs = binderVars univ_bndrs
|
| 333 | 333 | bang_opts = FixedBangOpts (map (const HsLazy) args)
|
| 334 | + bangs :: [HsSrcBang]
|
|
| 335 | + bangs | unary_class = [yes_bang] -- See (UCM3b) in Note [Unary class magic]
|
|
| 336 | + | otherwise = map (const no_bang) args
|
|
| 334 | 337 | |
| 335 | 338 | ; rep_nm <- newTyConRepName datacon_name
|
| 336 | 339 | ; dict_con <- buildDataCon (panic "buildClass: FamInstEnvs")
|
| ... | ... | @@ -338,7 +341,7 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef |
| 338 | 341 | datacon_name
|
| 339 | 342 | False -- Not declared infix
|
| 340 | 343 | rep_nm
|
| 341 | - (map (const no_bang) args)
|
|
| 344 | + bangs
|
|
| 342 | 345 | [{- No fields -}]
|
| 343 | 346 | univ_tvs
|
| 344 | 347 | [{- no existentials -}]
|
| ... | ... | @@ -376,7 +379,8 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef |
| 376 | 379 | ; traceIf (text "buildClass" <+> ppr tycon)
|
| 377 | 380 | ; return result }
|
| 378 | 381 | where
|
| 379 | - no_bang = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict
|
|
| 382 | + no_bang = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict
|
|
| 383 | + yes_bang = HsSrcBang NoSourceText NoSrcUnpack SrcStrict
|
|
| 380 | 384 | |
| 381 | 385 | mk_op_item :: Class -> TcMethInfo -> TcRnIf n m ClassOpItem
|
| 382 | 386 | mk_op_item rec_clas (op_name, _, dm_spec)
|