Simon Peyton Jones pushed to branch wip/T27910 at Glasgow Haskell Compiler / GHC Commits: 8c947573 by Simon Peyton Jones at 2026-10-16T13:11:53+01:00 Improve exprOkForSpeculation Fixes #27910, which exposed an infelicity in exprOkForSpeculation The new story is carefully described in new Note [exprOkForSpeculation], esp (a-c) and (EOKS1). - - - - - 6 changed files: - compiler/GHC/Core.hs - compiler/GHC/Core/Type.hs - compiler/GHC/Core/Utils.hs - + testsuite/tests/simplCore/should_compile/T27910.hs - + testsuite/tests/simplCore/should_compile/T27910.stderr - testsuite/tests/simplCore/should_compile/all.T Changes: ===================================== compiler/GHC/Core.hs ===================================== @@ -490,7 +490,7 @@ This is a well-kinded type, in an environment-based scheme. But `typeKind` will panic, because the kind of the body of a forall must not mention the forall'd variable (and the inner forall's body appears to do so). -It might be possible to make `exprType` and `typeKind`` more complicated, so that +It might be possible to make `exprType` and `typeKind` more complicated, so that they do renaming on the fly. But instead we impose INVARIANT (NoTypeShadowing): ===================================== compiler/GHC/Core/Type.hs ===================================== @@ -55,7 +55,7 @@ module GHC.Core.Type ( splitForAllForAllTyBinders, splitForAllForAllTyBinder_maybe, splitForAllTyCoVar_maybe, splitForAllTyCoVar, splitForAllTyVar_maybe, splitForAllCoVar_maybe, - splitPiTy_maybe, splitPiTy, splitPiTys, collectPiTyBinders, + splitPiTy_maybe, splitPiTy, splitPiTys, splitUninstantiatedValArgTys, getRuntimeArgTys, mkTyConBindersPreferAnon, mkPiTy, mkPiTys, @@ -263,7 +263,6 @@ import GHC.Utils.Monad.StrictReader import GHC.Data.FastString import GHC.Data.Maybe ( orElse, isJust ) -import GHC.List (build) -- $type_classification -- #type_classification# @@ -1599,17 +1598,20 @@ splitPiTys ty = split ty ty [] split orig_ty ty bs | Just ty' <- coreView ty = split orig_ty ty' bs split orig_ty _ bs = (reverse bs, orig_ty) -collectPiTyBinders :: Type -> [PiTyBinder] -collectPiTyBinders ty = build $ \c n -> - let - split (ForAllTy b res) = Named b `c` split res - split (FunTy { ft_af = af, ft_mult = w, ft_arg = arg, ft_res = res }) - = Anon (Scaled w arg) af `c` split res - split ty | Just ty' <- coreView ty = split ty' - split _ = n - in - split ty -{-# INLINE collectPiTyBinders #-} +splitUninstantiatedValArgTys :: Type -> [Type] +-- This function takes a type apart, returning all its /value/ +-- argument types (including (forall (co::t1~t2).t) +-- doing no instantiation at all. +-- E.g. splitUinstantiatedValArgTys (forall a. a -> Int -> T a) +-- returns [a, Int] +-- It's a pretty specialised function, hence its long name +splitUninstantiatedValArgTys ty = go ty + where + go (ForAllTy (Bndr b _) res) | isCoVar b = varType b : go res + | otherwise = go res + go (FunTy { ft_arg = arg, ft_res = res }) = arg : go res + go ty | Just ty' <- coreView ty = go ty' + go _ = [] -- | Extracts a list of run-time arguments from a function type, -- looking through newtypes to the right of arrows. ===================================== compiler/GHC/Core/Utils.hs ===================================== @@ -2207,6 +2207,8 @@ fun_always_ok _ = True -- | A special version of 'exprOkForSpeculation' used during -- Note [Speculative evaluation]. When the predicate arg `fun_ok` returns False -- for `b`, then `b` is never considered ok-for-spec. +-- +-- This is the whole reason for the fun_ok arg to expr_ok. exprOkForSpecEval :: (Id -> Bool) -> CoreExpr -> Bool exprOkForSpecEval fun_ok = expr_ok fun_ok primOpOkForSpeculation @@ -2240,10 +2242,9 @@ expr_ok fun_ok primop_ok (Case scrut bndr _ alts) && altsAreExhaustive alts expr_ok fun_ok primop_ok other_expr - | (expr, args) <- collectArgs other_expr + | (expr, val_args) <- collectValArgs other_expr = case stripTicksTopE (not . tickishCounts) expr of - Var f -> - app_ok fun_ok primop_ok f args + Var f -> app_ok fun_ok primop_ok f val_args -- 'LitRubbish' is the only literal that can occur in the head of an -- application and will not be matched by the above case (Var /= Lit). @@ -2258,9 +2259,10 @@ expr_ok fun_ok primop_ok other_expr ----------------------------- app_ok :: (Id -> Bool) -> (PrimOp -> Bool) -> Id -> [CoreArg] -> Bool -app_ok fun_ok primop_ok fun args +app_ok fun_ok primop_ok fun val_args | not (fun_ok fun) - = False -- This code path is only taken for Note [Speculative evaluation] + = False -- This code path is only taken for exprOkForSpecEval + -- See Note [Speculative evaluation] | idArity fun > n_val_args -- Partial application: just check passing the arguments is OK @@ -2281,17 +2283,18 @@ app_ok fun_ok primop_ok fun args ClassOpId _ is_terminating_result | is_terminating_result -- See Note [exprOkForSpeculation and type classes] - -> assertPpr (n_val_args == 1) (ppr fun $$ ppr args) $ + -> assertPpr (n_val_args == 1) (ppr fun $$ ppr val_args) $ True -- assert: terminating result type => can't be applied; -- c.f the _other case below PrimOpId op _ | primOpIsDiv op - , Lit divisor <- last args + , Lit divisor <- last val_args -- there can be 2 args (most div primops) or 3 args -- (WordQuotRem2Op), hence the use of last/init - -> not (isZeroLit divisor) && all (expr_ok fun_ok primop_ok) (init args) + -> not (isZeroLit divisor) && + all ok_for_spec (init val_args) -- Special case for dividing operations that fail -- In general they are NOT ok-for-speculation -- (which primop_ok will catch), but they ARE OK @@ -2300,12 +2303,14 @@ app_ok fun_ok primop_ok fun args -- can get rid of a thunk in an inner loop | otherwise -> primop_ok op && args_ok + -- Assumes that the primop does not evaluate its + -- definitely-lifted arguments _other -- Unlifted and terminating types; -- Also c.f. the Var case of exprIsHNF | isTerminatingType fun_ty -- See Note [exprOkForSpeculation and type classes] || definitelyUnliftedType fun_ty - -> assertPpr (n_val_args == 0) (ppr fun $$ ppr args) + -> assertPpr (n_val_args == 0) (ppr fun $$ ppr val_args) True -- Both terminating types (e.g. Eq a), and unlifted types (e.g. Int#) -- are non-functions and so will have no value args. The assert is -- just to check this. @@ -2321,32 +2326,43 @@ app_ok fun_ok primop_ok fun args -- for evaluated-ness of the fun; -- see Note [exprOkForSpeculation and evaluated variables] where - fun_ty = idType fun - n_val_args = valArgCount args - (arg_tys, _) = splitPiTys fun_ty + ok_for_spec = expr_ok fun_ok primop_ok -- Used for recursive calls + + fun_ty = idType fun + n_val_args = length val_args + val_arg_tys = splitUninstantiatedValArgTys fun_ty + -- val_arg_tys are /uninstantiated/; see (EOKS1) -- Even if a function call itself is OK, any unlifted -- args are still evaluated eagerly and must be checked - args_ok = all2Prefix arg_ok arg_tys args - arg_ok :: PiTyVarBinder -> CoreExpr -> Bool - arg_ok (Named _) _ = True -- A type argument - arg_ok (Anon ty _) arg -- A term argument - | definitelyLiftedType (scaledThing ty) - = True -- lifted args are not evaluated eagerly - | otherwise - = expr_ok fun_ok primop_ok arg + -- arg_ok bndr arg = field_ok bndr NotMarkedStrict arg + -- See Note [exprOkForSpeculation] (a,b) + args_ok = all2Prefix arg_ok val_arg_tys val_args + arg_ok :: Type -> CoreExpr -> Bool + arg_ok arg_ty arg = ok_for_spec arg || definitely_lifted arg_ty arg -- Used for strict DataCon worker arguments -- See (SFC1) of Note [Strict fields in Core] - fields_ok str_marks = all3Prefix field_ok arg_tys str_marks args - field_ok :: PiTyVarBinder -> StrictnessMark -> CoreExpr -> Bool - field_ok (Named _) _ _ = True - field_ok (Anon ty _) str arg - | NotMarkedStrict <- str -- iff it's a lazy field - , definitelyLiftedType (scaledThing ty) -- and its type is lifted - = True -- then the worker app does not eval - | otherwise - = expr_ok fun_ok primop_ok arg + -- See Note [exprOkForSpeculation] (a,b) + fields_ok str_marks = all3Prefix field_ok val_arg_tys str_marks val_args + field_ok :: Type -> StrictnessMark -> CoreExpr -> Bool + field_ok arg_ty str arg + = ok_for_spec arg || (definitely_lifted arg_ty arg && str==NotMarkedStrict) + -- The (ok_for_spec arg) test works for /both/ for unlifted types, /and/ + -- for lifted but strict fields. E.g. K (I# 3) is ok-for-spec, + -- even if K is strict + + -- We want to see if the argument type is definitely lifted. + -- But consider + -- (#,#) @rep1 @rep2 @ty1 @ty2 arg1 arg2 + -- We don't /instantiate/ the type of (#,#) (although we could) so we don't + -- know if the argument type is lifted or not. So we look at the type of + -- the /actual/ argument (it's usually a variable) instead. + definitely_lifted uninstantiated_arg_ty arg + = case typeLevity_maybe uninstantiated_arg_ty of + Just Lifted -> True + Just Unlifted -> False + Nothing -> definitelyLiftedType (exprType arg) ----------------------------- altsAreExhaustive :: [Alt b] -> Bool @@ -2390,8 +2406,42 @@ cantEtaReduceFun fun -- Don't undersaturate StrictWorkerIds. -- See Note [CBV Function Ids: overview] in GHC.Types.Id.Info. -{- Note [exprOkForSpeculation and type classes] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +{- Note [exprOkForSpeculation] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Suppose we have + data T a = K !a Int# a + +Is the call (K e1 e2 e3) ok-for-speculation? Look at the arguments e1..e3, +and their types. The call is ok-for-speculation if each argument satisfies +`field_ok`, namely: + (a) (exprOkForSpeculation arg) holds, OR + (b) Arg type is definitely lifted and it is not marked strict + +For (a) we conservatively assume we are going to evaluate the argument. +If it is ok-for-spec, then fine. + +But if the argument is of a lifted type, and not marked strict (notably by +a data constructor) then (b) holds. For example + Just (reverse xs) +is ok-for-spec. See (EOKS1) for the argument liftedness test. + +If we have a plain variable `x`, is that ok-for-spec? Not if it is lifted: +it might be a thunk! Hence the (definitelyUnliftedType fun_ty) test. +Remember, a naked variable `x` will land in `app_ok x []` with no arguments. +See Note [exprOkForSpeculation and evaluated variables]. + +(EOKS1) To get the lifted-ness or otherwise of the arguments we just look at the + /uninstantiated/ type of the data constructor or primop. We'd get better information + if we instantiated, but it's also more expensive. Example: + (#,#) :: forall r1 r2 (a::TYPE r1) (b::TYPE r2). a -> b -> (# a, b #) + If we have a call + (#,#) @LiftedRep @IntRep @Int @Int# 3 4# + then we could see accurate liftedness by instantiation, but we don't try. + Instead we get the actual type from (exprType arg). That could be expensive, + but it really only happens for unboxed tuples. + +Note [exprOkForSpeculation and type classes] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Consider (#22745, #15205) \(d :: C a b). case eq_sel (sc_sel d) of @@ -2674,8 +2724,7 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e -- PAP: Check unlifted val_args args_hnf = all2Prefix check_arg val_arg_tys val_args - fun_ty = idType id - val_arg_tys = mapMaybe anonPiTyBinderType_maybe (collectPiTyBinders fun_ty) + 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. @@ -2700,7 +2749,6 @@ exprIsHNFlike is_con is_con_unf rubbish_lit_result e | otherwise = Nothing - {- Note [exprIsHNF Tick] ~~~~~~~~~~~~~~~~~~~~~ ===================================== testsuite/tests/simplCore/should_compile/T27910.hs ===================================== @@ -0,0 +1,17 @@ +{-# LANGUAGE UnboxedTuples, MagicHash #-} +module T27910 where +import GHC.Exts + +k1 :: (# Int, Bool #) -> (# Int, Bool #) -> Int +{-# NOINLINE k1 #-} +k1 _ _ = 3 + +k2 :: (# Int#, Int# #) -> (# Int#, Int# #) -> Int +{-# NOINLINE k2 #-} +k2 _ _ = 3 + +lifted :: Int -> Bool -> Int +lifted x y = case (# x, y #) of r -> k1 r r + +unlifted :: Int# -> Int# -> Int +unlifted x y = case (# x, y #) of r -> k2 r r ===================================== testsuite/tests/simplCore/should_compile/T27910.stderr ===================================== @@ -0,0 +1,49 @@ + +==================== FloatOut stats: ==================== +2 Lets floated to top level; 0 Lets floated elsewhere; from 4 Lambda groups + + + +==================== FloatOut stats: ==================== +4 Lets floated to top level; 0 Lets floated elsewhere; from 6 Lambda groups + + + +==================== Grand total simplifier statistics ==================== +Total ticks: 30 + +8 PreInlineUnconditionally + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds +2 PostInlineUnconditionally + 1 r + 1 r +4 UnfoldingDone + 2 GHC.Internal.Prim.void# + 1 T27910.k1 + 1 T27910.k2 +8 BetaReduction + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds + 1 ds +4 KnownBranch + 2 wild + 1 ww + 1 ww +4 CaseElim + 2 ww + 2 ww +9 SimplifierDone 9 + + ===================================== testsuite/tests/simplCore/should_compile/all.T ===================================== @@ -627,5 +627,5 @@ test('T27749', compile, ['-O -g3 -ddump-simpl -dsuppress-uniques']) test('T27629', only_ways(['optasm']), makefile_test, ['T27629']) - test('T27936', normal, compile, ['']) +test('T27910', grep_errmsg(r'KnownBranch'), compile, ['-O -dno-typeable-binds -ddump-simpl-stats -dsuppress-uniques']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8c9475738abd6403dec943653f5cd48a... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8c9475738abd6403dec943653f5cd48a... 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
participants (1)
-
Simon Peyton Jones (@simonpj)