Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

7 changed files:

Changes:

  • compiler/GHC/Stg/Pipeline.hs
    ... ... @@ -143,7 +143,7 @@ stg2stg logger extra_vars opts this_mod binds
    143 143
               StgUnarise -> do
    
    144 144
                 us <- getUniqueSupplyM
    
    145 145
                 liftIO (stg_linter False "Pre-unarise" binds)
    
    146
    -            let binds' = {-# SCC "StgUnarise" #-} unarise us (stgPipeline_allowTopLevelConApp opts this_mod) binds
    
    146
    +            let binds' = {-# SCC "StgUnarise" #-} unarise (stgPlatform opts) us (stgPipeline_allowTopLevelConApp opts this_mod) binds
    
    147 147
                 liftIO (dump_when Opt_D_dump_stg_unarised "Unarised STG:" binds')
    
    148 148
                 liftIO (stg_linter True "Unarise" binds')
    
    149 149
                 return binds'
    

  • compiler/GHC/Stg/Unarise.hs
    ... ... @@ -413,6 +413,7 @@ import Data.Maybe (mapMaybe)
    413 413
     import qualified Data.IntMap as IM
    
    414 414
     import GHC.Builtin.PrimOps
    
    415 415
     import GHC.Builtin.PrimOps.Casts
    
    416
    +import GHC.Platform
    
    416 417
     import Data.List (mapAccumL)
    
    417 418
     
    
    418 419
     -- import GHC.Utils.Trace
    
    ... ... @@ -441,12 +442,13 @@ import Data.List (mapAccumL)
    441 442
     --            (i.e. no unboxed tuples, sums or voids)
    
    442 443
     --
    
    443 444
     data UnariseEnv = UnariseEnv
    
    444
    -  { ue_rho                 :: (VarEnv UnariseVal)
    
    445
    +  { ue_platform            :: !Platform
    
    446
    +  , ue_rho                 :: VarEnv UnariseVal
    
    445 447
       , ue_allow_static_conapp :: DataCon -> [StgArg] -> Bool
    
    446 448
       }
    
    447 449
     
    
    448
    -initUnariseEnv :: VarEnv UnariseVal -> (DataCon -> [StgArg] -> Bool) -> UnariseEnv
    
    449
    -initUnariseEnv = UnariseEnv
    
    450
    +initUnariseEnv :: Platform -> VarEnv UnariseVal -> (DataCon -> [StgArg] -> Bool) -> UnariseEnv
    
    451
    +initUnariseEnv platform rho is_dll = UnariseEnv platform rho is_dll
    
    450 452
     
    
    451 453
     data UnariseVal
    
    452 454
       = MultiVal [OutStgArg] -- MultiVal to tuple. Can be empty list (void).
    
    ... ... @@ -479,8 +481,8 @@ lookupRho env v = lookupVarEnv (ue_rho env) v
    479 481
     
    
    480 482
     --------------------------------------------------------------------------------
    
    481 483
     
    
    482
    -unarise :: UniqSupply -> (DataCon -> [StgArg] -> Bool) -> [StgTopBinding] -> [StgTopBinding]
    
    483
    -unarise us is_dll_con_app binds = initUs_ us (mapM (unariseTopBinding (initUnariseEnv emptyVarEnv is_dll_con_app)) binds)
    
    484
    +unarise :: Platform -> UniqSupply -> (DataCon -> [StgArg] -> Bool) -> [StgTopBinding] -> [StgTopBinding]
    
    485
    +unarise platform us is_dll_con_app binds = initUs_ us (mapM (unariseTopBinding (initUnariseEnv platform emptyVarEnv is_dll_con_app)) binds)
    
    484 486
     
    
    485 487
     unariseTopBinding :: UnariseEnv -> StgTopBinding -> UniqSM StgTopBinding
    
    486 488
     unariseTopBinding rho (StgTopLifted bind)
    
    ... ... @@ -627,7 +629,7 @@ unariseUbxSumOrTupleArgs rho us dc args ty_args
    627 629
     
    
    628 630
       | isUnboxedSumDataCon dc
    
    629 631
       , let args1 = assert (isSingleton args) (unariseConArgs rho args)
    
    630
    -  = let (args2, cast_wrapper) = mkUbxSum dc ty_args args1 us
    
    632
    +  = let (args2, cast_wrapper) = mkUbxSum (ue_platform rho) dc ty_args args1 us
    
    631 633
         in (args2, Just cast_wrapper)
    
    632 634
     
    
    633 635
       | otherwise
    
    ... ... @@ -848,29 +850,29 @@ mapSumIdBinders alt_bndr args rhs rho0
    848 850
           -- right type.
    
    849 851
           -- Select only the args which contain parts of the current field.
    
    850 852
           id_arg_exprs   = [ args !! i | i <- layout1 ]
    
    851
    -      id_vars   = [v | StgVarArg v <- id_arg_exprs]
    
    852 853
     
    
    853
    -      typed_id_arg_input = assert (equalLength id_vars fld_reps) $
    
    854
    -                           zip3 id_vars fld_reps uss
    
    855
    -
    
    856
    -      mkCastInput :: (Id,PrimRep,UniqSupply) -> ([(PrimOp,Type,Unique)],Id,Id)
    
    857
    -      mkCastInput (id,rep,bndr_us) =
    
    858
    -        let (ops,types) = unzip $ getCasts (typePrimRepU $ idType id) rep
    
    854
    +      typed_id_arg_input = assert (equalLength id_arg_exprs fld_reps) $
    
    855
    +                           zip3 id_arg_exprs fld_reps uss
    
    856
    +
    
    857
    +      -- Process each (arg, target rep, unique supply) to produce
    
    858
    +      -- (rhs wrapper, typed arg). Handles both literal and variable args.
    
    859
    +      -- Literal args can arise after constant-folding in mkUbxSum
    
    860
    +      -- (see Note [Constant-folding during unarisation]).
    
    861
    +      mkCastArg :: (StgArg, PrimRep, UniqSupply) -> (StgExpr -> StgExpr, StgArg)
    
    862
    +      mkCastArg (StgLitArg lit, rep, _us)
    
    863
    +        | Just lit' <- castLiteralArg (ue_platform rho0) rep lit
    
    864
    +        = (id, StgLitArg lit')
    
    865
    +        | otherwise = pprPanic "mapSumIdBinders: cannot cast literal" (ppr lit $$ ppr rep)
    
    866
    +      mkCastArg (StgVarArg v, rep, bndr_us) =
    
    867
    +        let (ops,types) = unzip $ getCasts (typePrimRepU $ idType v) rep
    
    859 868
                 cst_opts = zip3 ops types $ uniqsFromSupply bndr_us
    
    860 869
                 out_id = case cst_opts of
    
    861
    -              [] -> id
    
    862
    -              _ ->  let (_,ty,uq) = last cst_opts
    
    863
    -                    in mkCastVar uq ty
    
    864
    -        in (cst_opts,id,out_id)
    
    865
    -
    
    866
    -      cast_inputs = map mkCastInput typed_id_arg_input
    
    867
    -      (rhs_with_casts,typed_ids) = mapAccumL cast_arg (\x->x) cast_inputs
    
    868
    -        where
    
    869
    -          cast_arg rhs_in (cast_ops,in_id,out_id) =
    
    870
    -            let rhs_out = castArgRename cast_ops (StgVarArg in_id)
    
    871
    -            in (rhs_in . rhs_out, out_id)
    
    870
    +              [] -> v
    
    871
    +              _ ->  let (_,ty,uq) = last cst_opts in mkCastVar uq ty
    
    872
    +        in (castArgRename cst_opts (StgVarArg v), StgVarArg out_id)
    
    872 873
     
    
    873
    -      typed_id_args = map StgVarArg typed_ids
    
    874
    +      (wrappers, typed_id_args) = unzip $ map mkCastArg typed_id_arg_input
    
    875
    +      rhs_with_casts = foldr (.) id wrappers
    
    874 876
     
    
    875 877
         if isMultiValBndr alt_bndr
    
    876 878
           then return (extendRho rho0 alt_bndr (MultiVal typed_id_args), rhs_with_casts rhs)
    
    ... ... @@ -913,14 +915,15 @@ mkCast arg_in cast_op out_id out_ty in_rhs =
    913 915
     --
    
    914 916
     mkUbxSum
    
    915 917
       :: HasDebugCallStack
    
    916
    -  => DataCon      -- Sum data con
    
    918
    +  => Platform     -- For compile-time constant-folding
    
    919
    +  -> DataCon      -- Sum data con
    
    917 920
       -> [[PrimRep]]  -- Representations of type arguments of the sum data con
    
    918 921
       -> [OutStgArg]  -- Actual arguments of the alternative.
    
    919 922
       -> UniqSupply
    
    920 923
       -> ([OutStgArg] -- Final tuple arguments
    
    921 924
          ,(StgExpr->StgExpr) -- We might need to cast the args first
    
    922 925
          )
    
    923
    -mkUbxSum dc ty_args args0 us
    
    926
    +mkUbxSum platform dc ty_args args0 us
    
    924 927
       = let
    
    925 928
           tag_slot :| sum_slots = ubxSumRepType ty_args
    
    926 929
           -- drop tag slot
    
    ... ... @@ -961,6 +964,11 @@ mkUbxSum dc ty_args args0 us
    961 964
                 , ubxSumRubbishArg slot)
    
    962 965
     
    
    963 966
           castArg :: UniqSupply -> SlotTy -> StgArg -> Maybe (StgArg,UniqSupply,StgExpr -> StgExpr)
    
    967
    +      castArg us slot_ty arg@(StgLitArg lit)
    
    968
    +        -- See Note [Constant-folding during unarisation]
    
    969
    +        | slotPrimRep slot_ty /= stgArgRepU arg
    
    970
    +        , Just lit' <- castLiteralArg platform (slotPrimRep slot_ty) lit
    
    971
    +        = Just (StgLitArg lit', us, id)
    
    964 972
           castArg us slot_ty arg
    
    965 973
             -- Cast the argument to the type of the slot if required
    
    966 974
             | slotPrimRep slot_ty /= stgArgRepU arg
    
    ... ... @@ -1006,6 +1014,101 @@ ubxSumRubbishArg DoubleSlot = StgLitArg (LitDouble 0)
    1006 1014
     ubxSumRubbishArg (VecSlot n e)   = StgLitArg (LitRubbish TypeLike vec_rep)
    
    1007 1015
       where vec_rep = primRepToRuntimeRep (VecRep n e)
    
    1008 1016
     
    
    1017
    +{-
    
    1018
    +Note [Constant-folding during unarisation]
    
    1019
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1020
    +See #25650.
    
    1021
    +
    
    1022
    +Goal: ensure that top-level bindings whose unboxed-sum fields are literals
    
    1023
    +become statically allocated closures (i.e. compile-time constants in the
    
    1024
    +object file) rather than CAFs.
    
    1025
    +
    
    1026
    +Background: A top-level RHS is statically allocated when it is a plain
    
    1027
    +`StgRhsCon`: a data constructor applied to arguments with no surrounding
    
    1028
    +expression.  Any `StgCase` wrapper, even one that is a no-op at runtime, turns
    
    1029
    +the RHS into a CAF.
    
    1030
    +
    
    1031
    +The problem: When `mkUbxSum` builds an unboxed sum whose argument PrimRep does
    
    1032
    +not match the slot PrimRep, the general `castArg` path emits a runtime conversion
    
    1033
    +wrapper:
    
    1034
    +
    
    1035
    +  case <conversion_primop> arg of x' -> <rhs using x'>
    
    1036
    +
    
    1037
    +For a *variable* argument this is unavoidable, the value is not known at
    
    1038
    +compile time.  For a *literal* argument, however, the conversion can be performed
    
    1039
    +at compile time, avoiding the `StgCase` wrapper entirely.
    
    1040
    +
    
    1041
    +Example
    
    1042
    +~~~~~~~
    
    1043
    +Consider:
    
    1044
    +
    
    1045
    +  data A = MkA (# Int16# | Int32# #)
    
    1046
    +  foo = MkA (# 10#Int16 | #)
    
    1047
    +
    
    1048
    +By the time this gets to the end of the Simplifier pipeline, this still looks
    
    1049
    +like:
    
    1050
    +   foo = MkA (# 10#Int16 | #)
    
    1051
    +That is: the worker for the data constructor takes an unboxed sum as its
    
    1052
    +argument.
    
    1053
    +
    
    1054
    +The Unarise pass, which works on STG, decides that
    
    1055
    +    (# 10#Int16 | #) :: (# Int16# | Int32# #)
    
    1056
    +should be represented as an pair of an integer tag (of type `Int8#`) and a payload
    
    1057
    +value (of type `Word32#`).  But to do that it has to convert `10#Int16` into
    
    1058
    +`Word32#`, and that conversion is not a no-op. So without constant-folding we
    
    1059
    +get:
    
    1060
    +
    
    1061
    + foo =
    
    1062
    +  \u []
    
    1063
    +      case int16ToWord16# [10#Int16] of cst_sum_gio {
    
    1064
    +      __DEFAULT ->
    
    1065
    +      case word16ToWord# [cst_sum_gio] of cst_sum_gip {
    
    1066
    +      __DEFAULT -> MkA [1# cst_sum_gip];
    
    1067
    +      };
    
    1068
    +      };
    
    1069
    +
    
    1070
    +Note that in the output of the unarise pass, the worker `MkA` takes two
    
    1071
    +arguments: the tag and the payload of our unboxed sum..
    
    1072
    +
    
    1073
    +However it's a bit silly to generate a CAF here because with some
    
    1074
    +constant-folding we can easily avoid this thunk and generate a static datacon
    
    1075
    +instead. That's why the literal clause of `castArg` intercepts `Int16# 10`,
    
    1076
    +calls `castLiteralArg` to compute `Word32# 10` at compile time, and returns the
    
    1077
    +identity wrapper.  The result is:
    
    1078
    +
    
    1079
    +  foo = MkA! [1#Word8 10#Word32];
    
    1080
    +
    
    1081
    +
    
    1082
    +Note that `castLiteralArg` uses `mkLitNumberWrap`, which matches the
    
    1083
    +semantics of GHC's integer-conversion primops (zero/sign extension to the target
    
    1084
    +width) — exactly the same transformation the runtime conversion would have
    
    1085
    +performed.
    
    1086
    +
    
    1087
    +-}
    
    1088
    +
    
    1089
    +-- | Try to convert a numeric literal to a new PrimRep at compile time.
    
    1090
    +-- Uses wrapping semantics (same as GHC's integer conversion primops).
    
    1091
    +-- Returns Nothing for non-numeric literals or unsupported PrimReps.
    
    1092
    +-- See Note [Constant-folding during unarisation].
    
    1093
    +castLiteralArg :: Platform -> PrimRep -> Literal -> Maybe Literal
    
    1094
    +castLiteralArg platform to_rep (LitNumber _ n)
    
    1095
    +  | Just to_ty <- litNumTypeFromPrimRep to_rep
    
    1096
    +  = Just (mkLitNumberWrap platform to_ty n)
    
    1097
    +castLiteralArg _ _ _ = Nothing
    
    1098
    +
    
    1099
    +litNumTypeFromPrimRep :: PrimRep -> Maybe LitNumType
    
    1100
    +litNumTypeFromPrimRep WordRep   = Just LitNumWord
    
    1101
    +litNumTypeFromPrimRep Word8Rep  = Just LitNumWord8
    
    1102
    +litNumTypeFromPrimRep Word16Rep = Just LitNumWord16
    
    1103
    +litNumTypeFromPrimRep Word32Rep = Just LitNumWord32
    
    1104
    +litNumTypeFromPrimRep Word64Rep = Just LitNumWord64
    
    1105
    +litNumTypeFromPrimRep IntRep    = Just LitNumInt
    
    1106
    +litNumTypeFromPrimRep Int8Rep   = Just LitNumInt8
    
    1107
    +litNumTypeFromPrimRep Int16Rep  = Just LitNumInt16
    
    1108
    +litNumTypeFromPrimRep Int32Rep  = Just LitNumInt32
    
    1109
    +litNumTypeFromPrimRep Int64Rep  = Just LitNumInt64
    
    1110
    +litNumTypeFromPrimRep _         = Nothing
    
    1111
    +
    
    1009 1112
     --------------------------------------------------------------------------------
    
    1010 1113
     
    
    1011 1114
     {-
    

  • testsuite/tests/codeGen/should_compile/Makefile
    ... ... @@ -80,3 +80,6 @@ T17648:
    80 80
     
    
    81 81
     T25166:
    
    82 82
     	'$(TEST_HC)' $(TEST_HC_OPTS) -O2 -dno-typeable-binds -ddump-cmm T25166.hs | awk '/foo_closure/{flag=1}/}]/{flag=0}flag'
    
    83
    +
    
    84
    +T25650:
    
    85
    +	'$(TEST_HC)' $(TEST_HC_OPTS) -O2 -dno-typeable-binds -ddump-cmm T25650.hs | awk '/baz_foo_closure|baz_bar_closure/{flag=1}/}]/{flag=0}flag'

  • testsuite/tests/codeGen/should_compile/T25650.hs
    1
    +module T25650 (baz_foo, baz_bar) where
    
    2
    +
    
    3
    +import Data.Word
    
    4
    +
    
    5
    +data A
    
    6
    +  = A1 {-# UNPACK #-} !Word32
    
    7
    +  | A2 {-# UNPACK #-} !B
    
    8
    +
    
    9
    +data B = B1 | B2
    
    10
    +
    
    11
    +foo = A1 10
    
    12
    +bar = A2 B2
    
    13
    +
    
    14
    +data C = C {-# UNPACK #-} !A
    
    15
    +
    
    16
    +baz_foo = C foo
    
    17
    +baz_bar = C bar

  • testsuite/tests/codeGen/should_compile/T25650.stdout-ws-32
    1
    +[section ""data" . T25650.baz_foo_closure" {
    
    2
    +     T25650.baz_foo_closure:
    
    3
    +         const T25650.C_con_info;
    
    4
    +         const 10;
    
    5
    +         const 1 :: W8;
    
    6
    +         const 0 :: W8;
    
    7
    +         const 0 :: W16;
    
    8
    +[section ""data" . T25650.baz_bar_closure" {
    
    9
    +     T25650.baz_bar_closure:
    
    10
    +         const T25650.C_con_info;
    
    11
    +         const 2;
    
    12
    +         const 2 :: W8;
    
    13
    +         const 0 :: W8;
    
    14
    +         const 0 :: W16;

  • testsuite/tests/codeGen/should_compile/T25650.stdout-ws-64
    1
    +[section ""data" . T25650.baz_foo_closure" {
    
    2
    +     T25650.baz_foo_closure:
    
    3
    +         const T25650.C_con_info;
    
    4
    +         const 10 :: W32;
    
    5
    +         const 1 :: W8;
    
    6
    +         const 0 :: W8;
    
    7
    +         const 0 :: W16;
    
    8
    +[section ""data" . T25650.baz_bar_closure" {
    
    9
    +     T25650.baz_bar_closure:
    
    10
    +         const T25650.C_con_info;
    
    11
    +         const 2 :: W32;
    
    12
    +         const 2 :: W8;
    
    13
    +         const 0 :: W8;
    
    14
    +         const 0 :: W16;

  • testsuite/tests/codeGen/should_compile/all.T
    ... ... @@ -140,6 +140,7 @@ test('callee-no-local', [
    140 140
     )
    
    141 141
     
    
    142 142
     test('T25166', [req_cmm], makefile_test, [])
    
    143
    +test('T25650', [req_cmm], makefile_test, [])
    
    143 144
     
    
    144 145
     # dump Core to ensure that d is defined as: d = D 10## RUBBISH(IntRep)
    
    145 146
     test('T25177', normal, compile, ['-O2 -dno-typeable-binds -ddump-simpl -dsuppress-all -dsuppress-uniques -v0'])