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 Progress [skip ci] - - - - - 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: ===================================== compiler/GHC/Core/DataCon.hs ===================================== @@ -52,7 +52,6 @@ module GHC.Core.DataCon ( -- ** Predicates on DataCons isNullarySrcDataCon, isNullaryRepDataCon, - isLazyDataConRep, isTupleDataCon, isBoxedTupleDataCon, isUnboxedTupleDataCon, isUnboxedSumDataCon, isCovertGadtDataCon, isUnaryClassDataCon, isVanillaDataCon, isNewDataCon, isTypeDataCon, isBoxingDataCon, @@ -524,21 +523,27 @@ data DataCon -- See Note [Bangs on data constructor arguments] dcStricts :: [StrictnessMark], - -- One mark for every field of the DataCon worker; - -- if it's empty, then all fields are lazy, - -- otherwise 1-1 with dataConRepArgTys. + -- One mark for every field of the DataCon /worker/ + -- Lines up 1-1 with dataConRepArgTys + -- Relates only to user-specified strictness annotations, + -- not to liftedness or othewrise + -- E.g. data T a = MkT {-# UNPACK #-} !(S a) Int# !a + -- data S a = MkS a !a + -- Then the worker for MkT has four fields, the first two arising from + -- unpacking S. The corresponding strictness marks are [-,!,-,!]; again + -- the first two come from MkS -- See also Note [Strict fields in Core] in GHC.Core - -- for the effect on the strictness signature + -- for the effect on the strictness signature dcFields :: [FieldLabel], -- Field labels for this constructor, in the -- same order as the dcOrigArgTys; -- length = 0 (if not a record) or dataConSourceArity. - dcIsLazy :: Bool + dcIsLazy :: Bool, -- True if (a) all fields of the data con /worker/ are definitely lifted -- (b) no field has a `StrictnessMark` that forces evaluation - -- So if `dcIsLazy` is True then all fields are lazy thunks + -- So if `dcIsLazy` is True then all fields of the worker are lazy thunks -- The curried worker function that corresponds to the constructor: -- It doesn't have an unfolding; the code generator saturates these Ids @@ -836,7 +841,7 @@ data DataConRep , dcr_boxer :: DataConBoxer - , dcr_arg_tys :: [Scaled Type] -- Final, representation argument types, + , dcr_arg_tys :: [Scaled Type] -- Final, representation /value/ argument types, -- after unboxing and flattening, -- and *including* all evidence args @@ -1139,30 +1144,39 @@ mkDataCon name declared_infix prom_info = con where is_vanilla = null ex_tvs && null eq_spec && null theta - str_marks' | no_strict_marks = [] - | otherwise = str_marks - no_strict_marks = not $ any isMarkedStrict str_marks - is_lazy = no_str_marks && all definitelyLiftedType rep_arg_tys - - con = MkData {dcName = name, dcUnique = nameUnique name, - dcVanilla = is_vanilla, dcInfix = declared_infix, - dcUnivTyVars = univ_tvs, - dcExTyCoVars = ex_tvs, - dcConcreteTyVars = conc_tvs, - dcUserTyVarBinders = user_tvbs, - dcEqSpec = eq_spec, - dcOtherTheta = theta, - dcStupidTheta = stupid_theta, - dcOrigArgTys = orig_arg_tys, dcOrigResTy = orig_res_ty, - dcRepTyCon = rep_tycon, - dcSrcBangs = arg_stricts, dcImplBangs = impl_bangs, - dcStricts = str_marks', dcIsLazy = is_lazy, - dcFields = fields, dcTag = tag, dcRepType = rep_ty, - dcWorkId = work_id, - dcRep = rep, - dcSourceArity = length orig_arg_tys, - dcRepArity = length rep_arg_tys + count isCoVar ex_tvs, - dcPromoted = promoted } + is_lazy = not (any isMarkedStrict str_marks) + && all (definitelyLiftedType . scaledThing) rep_arg_tys + + con = MkData { dcName = name + , dcUnique = nameUnique name + , dcInfix = declared_infix + , dcTag = tag + , dcVanilla = is_vanilla + + , dcUnivTyVars = univ_tvs + , dcExTyCoVars = ex_tvs + , dcConcreteTyVars = conc_tvs + , dcUserTyVarBinders = user_tvbs + , dcEqSpec = eq_spec + , dcOtherTheta = theta + , dcStupidTheta = stupid_theta + , dcOrigArgTys = orig_arg_tys + , dcOrigResTy = orig_res_ty + + , dcRep = rep + , dcRepType = rep_ty + , dcRepTyCon = rep_tycon + , dcSrcBangs = arg_stricts + , dcImplBangs = impl_bangs + , dcStricts = assertPpr (equalLength str_marks rep_arg_tys) + (ppr name <+> ppr str_marks) + str_marks + , dcIsLazy = is_lazy + , dcFields = fields + , dcWorkId = work_id + , dcSourceArity = length orig_arg_tys + , dcRepArity = length rep_arg_tys + count isCoVar ex_tvs + , dcPromoted = promoted } -- The 'arg_stricts' passed to mkDataCon are simply those for the -- source-language arguments. We add extra ones for the @@ -1170,8 +1184,7 @@ mkDataCon name declared_infix prom_info rep_arg_tys = dataConRepArgTys con - rep_ty = - case rep of + rep_ty = case rep of -- If the DataCon has no wrapper, then the worker's type *is* the -- user-facing type, so we can simply use dataConWrapperType. NoDataConRep -> dataConWrapperType con @@ -1407,23 +1420,13 @@ isNullarySrcDataCon dc = dataConSourceArity dc == 0 isNullaryRepDataCon :: DataCon -> Bool isNullaryRepDataCon dc = dataConRepArity dc == 0 -isLazyDataConRep :: DataCon -> Bool --- ^ True <==> All fields are lazy -isLazyDataConRep dc = null (dcStricts dc) - dataConRepStrictness :: DataCon -> [StrictnessMark] -- ^ Give the demands on the runtime arguments of a Core DataCon worker --- application. --- The length of the list matches `dataConRepArgTys` (e.g., the number --- of runtime arguments). -dataConRepStrictness dc - | isLazyDataConRep dc - = replicate (dataConRepArity dc) NotMarkedStrict - | otherwise - = dcStricts dc +-- application. Maches 1-1 with `dataConRepArgTys` +dataConRepStrictness = dcStricts -dataConRepLazy :: DataCon -> Bool -dataConRepLazy = dcIsLazy +dataConRepIsLazy :: DataCon -> Bool +dataConRepIsLazy = dcIsLazy dataConImplBangs :: DataCon -> [HsImplBang] -- The implementation decisions about the strictness/unpack of each ===================================== compiler/GHC/Core/SimpleOpt.hs ===================================== @@ -1726,10 +1726,8 @@ exprIsConApp_maybe ise@(ISE in_scope id_unf) expr -- See Note [Strict fields in Core] for what a field seq is and (SFC2) for -- why we insert them mkFieldSeqFloats in_scope dc args - | isLazyDataConRep dc - = (in_scope, nilOL, args) - | otherwise - = (in_scope', floats', ty_args ++ val_args') + | dataConRepIsLazy dc = (in_scope, nilOL, args) -- Short cut in common case + | otherwise = (in_scope', floats', ty_args ++ val_args') where (ty_args, val_args) = splitAtList (dataConUnivAndExTyCoVars dc) args (in_scope', floats', val_args') = foldr do_one (in_scope, nilOL, []) $ ===================================== compiler/GHC/Core/TyCon.hs ===================================== @@ -1504,14 +1504,16 @@ There are a number of wrinkles (UCM2) The TyCon for a unary class is /not/ identified as a newtype. Rather, it has its own AlgTyConRhs, namely `UnaryClassTyCon` -(UCM3) Unlike non-unary classes, a value of type (C ty), where `C` is a unary +(UCM3a) Unlike non-unary classes, a value of type (C ty), where `C` is a unary class, might be bottom, because it is represented by the method type alone. See GHC.Core.Type.isTerminatingType, and Note [NON-BOTTOM-DICTS invariant] in GHC.Core - Similarly in exprOkForSpeculation/exprOkToDiscard/exprOkForSpecEval, - in GHC.Core.Utils. In the utility funcion `app_ok` we need a special - case for the DFunIds; they generally terminate, but not for unary classes. +(UCM3b) We treat the data constructor as if it had a strictness annotation + data C a = MkC !(a->a) + so that in exprOkForSpeculation/exprOkToDiscard/exprOkForSpecEval, and + in exprIsHNF/exprIsConLike, we evaluate the argument. This strictness flag + is set in `GHC.Tc.TyCl.buildClass (UMC4) To avoid regressions, in Core we want to remember that (MkUC x) is really just x ===================================== compiler/GHC/Core/Utils.hs ===================================== @@ -2648,9 +2648,9 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e is_hnf_like e where is_hnf_like (Var v) -- NB: There are no value args at this point - = id_app_is_value v [] -- Catches nullary constructors, - -- so that [] and () are values, for example - -- and (e.g.) primops that don't have unfoldings + = check_app v [] -- Catches nullary constructors, + -- so that [] and () are values, for example + -- and (e.g.) primops that don't have unfoldings || is_con_unf (idUnfolding v) -- Check the thing's unfolding; it might be bound to a value -- or to a guaranteed-evaluated variable (isEvaldUnfolding) @@ -2685,46 +2685,37 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e = is_hnf_like rhs is_hnf_like _ = False - -- Collect arguments through Casts and Ticks and call id_app_is_value + -- Collect arguments through Casts and Ticks and call check_app app_is_value :: CoreExpr -> [CoreArg] -> Bool - app_is_value (Var f) as = id_app_is_value f as + app_is_value (Var f) as = check_app f as app_is_value (Tick _ f) as = app_is_value f as app_is_value (Cast f _) as = app_is_value f as app_is_value (App f a) as | isValArg a = app_is_value f (a:as) | otherwise = app_is_value f as app_is_value _ _ = False - id_app_is_value id val_args - | Just dc <- isDataConWorkId_maybe id - , isUnaryClassDataCon dc - = all is_hnf_like val_args -- Look through unary class data cons - | otherwise - -- See Note [exprIsHNF for function applications] - -- for the specification and examples + -- check_app: See Note [exprIsHNF for function applications] + -- for the specification and examples + check_app id val_args = case compare (idArity id) (length val_args) of - EQ | is_con id -> -- Saturated app of a DataCon/CONLIKE Id - case mb_str_marks id of - Just str_marks -> -- with strict fields; see (SFC1) of Note [Strict fields in Core] - assert (val_args `equalLength` str_marks) $ - fields_hnf str_marks - Nothing -> -- without strict fields: like PAP - args_hnf -- NB: CONLIKEs are lazy! - - GT -> -- PAP: Check unlifted val_args - args_hnf - - _ -> False + EQ -> check_saturated id val_args -- Saturated application + GT -> check_args id val_args -- Partial application + LT -> False -- Over-saturated + check_saturated id val_args + | Just dc <- isDataConWorkId_maybe id + = -- Saturated data constructors; must account for the strictness flags + all3Prefix check_field val_args (val_arg_tys id) (dataConStrictness dc) + | is_con id -- Saturated CONLIKE: check the arguments + = check_args id val_args + | otherwise -- Saturated function, not a constructor => not HNF + = False + + -- check_args: check that any unlifted args are hnf-like + check_args id val_args = assert (sameLength val_args arg_tys) $ + all2Prefix check_arg val_args arg_tys where - -- Saturated, Strict DataCon: Check unlifted val_args and strict fields - fields_hnf str_marks = all3Prefix check_field val_arg_tys str_marks val_args - - -- PAP: Check unlifted val_args - args_hnf = all2Prefix check_arg val_arg_tys val_args - - val_arg_tys = splitUninstantiatedValArgTys (idType id) - -- val_arg_tys = map exprType val_args, but much less costly. - -- The obvious definition regresses T16577 by 30% so we don't do it. + arg_tys = val_arg_tys id check_arg a_ty a | mightBeUnliftedType a_ty = is_hnf_like a @@ -2735,18 +2726,18 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e -- NB: We check arity (and CONLIKEness) first because it's cheaper -- and we reject quickly on saturated apps. - check_field a_ty str a - | mightBeUnliftedType a_ty = is_hnf_like a - | isMarkedStrict str = is_hnf_like a - | otherwise = True - -- isMarkedStrict: Respect Note [Strict fields in Core] - - mb_str_marks id - | Just dc <- isDataConWorkId_maybe id - , not (isLazyDataConRep dc) - = Just (dataConRepStrictness dc) - | otherwise - = Nothing + -- check_field: used for a data constructor worker: + -- check unlifted val_args and strict fields + -- See (SFC1) of Note [Strict fields in Core] + check_field a a_ty str + | mightBeUnliftedType a_ty = is_hnf_like a + | isMarkedStrict str = is_hnf_like a + | otherwise = True + + val_arg_tys id = splitUninstantiatedValArgTys (idType id) + -- val_arg_tys = map exprType val_args, but much less costly. + -- The obvious definition regresses T16577 by 30% so we don't do it. + {- Note [exprIsHNF Tick] ===================================== compiler/GHC/Tc/TyCl/Build.hs ===================================== @@ -331,6 +331,9 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef univ_bndrs = tyVarSpecToBinders $ tyConInvisTVBinders binders univ_tvs = binderVars univ_bndrs bang_opts = FixedBangOpts (map (const HsLazy) args) + bangs :: [HsSrcBang] + bangs | unary_class = [yes_bang] -- See (UCM3b) in Note [Unary class magic] + | otherwise = map (const no_bang) args ; rep_nm <- newTyConRepName datacon_name ; dict_con <- buildDataCon (panic "buildClass: FamInstEnvs") @@ -338,7 +341,7 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef datacon_name False -- Not declared infix rep_nm - (map (const no_bang) args) + bangs [{- No fields -}] univ_tvs [{- no existentials -}] @@ -376,7 +379,8 @@ buildClass tycon_name kind binders roles fds sc_theta at_items sig_stuff mindef ; traceIf (text "buildClass" <+> ppr tycon) ; return result } where - no_bang = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict + no_bang = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict + yes_bang = HsSrcBang NoSourceText NoSrcUnpack SrcStrict mk_op_item :: Class -> TcMethInfo -> TcRnIf n m ClassOpItem mk_op_item rec_clas (op_name, _, dm_spec) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e832b4d2113aabe5ba69efabfbb82ec7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/e832b4d2113aabe5ba69efabfbb82ec7... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help