[Git][ghc/ghc][wip/marge_bot_batch_merge_job] 4 commits: Refactor: store [[PrimRep]] rather than [Type] in STG

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Wed Jan 3 10:57:58 UTC 2024



Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC


Commits:
d55216ad by Krzysztof Gogolewski at 2024-01-01T12:05:49-05:00
Refactor: store [[PrimRep]] rather than [Type] in STG

StgConApp stored a list of types. This list was used exclusively
during unarisation of unboxed sums (mkUbxSum).
However, this is at a wrong level of abstraction:
STG shouldn't be concerned with Haskell types, only PrimReps.
Update the code to store a [[PrimRep]]. Also, there's no point in storing
this list when we're not dealing with an unboxed sum.

- - - - -
8b340bc7 by Ömer Sinan Ağacan at 2024-01-01T12:06:29-05:00
Kind signatures docs: mention that they're allowed in newtypes

- - - - -
5d53e28b by Zubin Duggal at 2024-01-03T05:57:28-05:00
ci: Ensure we use the correct bindist name for the test artifact when generating
release ghcup metadata

Fixes #24268

- - - - -
493460bf by Krzysztof Gogolewski at 2024-01-03T05:57:28-05:00
Refactor: remove calls to typePrimRepArgs

The function typePrimRepArgs is just a thin wrapper around
typePrimRep, adding a VoidRep if the list is empty.
However, in StgToByteCode, we were discarding that VoidRep anyway,
so there's no point in calling it.

- - - - -


6 changed files:

- .gitlab/rel_eng/mk-ghcup-metadata/mk_ghcup_metadata.py
- compiler/GHC/CoreToStg.hs
- compiler/GHC/Stg/Syntax.hs
- compiler/GHC/Stg/Unarise.hs
- compiler/GHC/StgToByteCode.hs
- docs/users_guide/exts/kind_signatures.rst


Changes:

=====================================
.gitlab/rel_eng/mk-ghcup-metadata/mk_ghcup_metadata.py
=====================================
@@ -141,8 +141,14 @@ def mk_one_metadata(release_mode, version, job_map, artifact):
     # In --release-mode, the URL in the metadata needs to point into the downloads folder
     # rather then the pipeline.
     if release_mode:
+        # the test artifact is bundled with the source artifact, so it doesn't have its own job name
+        # So we must manually set the name of the bindist location
+        if artifact == test_artifact:
+            bindist_name = "testsuite"
+        else
+            bindist_name = fetch_gitlab.job_triple(artifact.job_name)
         final_url = release_base.format( version=version
-                                       , bindistName=urllib.parse.quote_plus(f"{fetch_gitlab.job_triple(artifact.job_name)}.tar.xz"))
+                                       , bindistName=urllib.parse.quote_plus(f"{bindist_name}.tar.xz"))
     else:
         final_url = url
 


=====================================
compiler/GHC/CoreToStg.hs
=====================================
@@ -56,7 +56,6 @@ import GHC.Utils.Misc (HasDebugCallStack)
 import GHC.Utils.Panic
 
 import Control.Monad (ap)
-import Data.Maybe (fromMaybe)
 
 -- Note [Live vs free]
 -- ~~~~~~~~~~~~~~~~~~~
@@ -531,8 +530,10 @@ coreToStgApp f args ticks = do
         res_ty = exprType (mkApps (Var f) args)
         app = case idDetails f of
                 DataConWorkId dc
-                  | saturated    -> StgConApp dc NoNumber args'
-                                      (dropRuntimeRepArgs (fromMaybe [] (tyConAppArgs_maybe res_ty)))
+                  | saturated    -> if isUnboxedSumDataCon dc then
+                                      StgConApp dc NoNumber args' (sumPrimReps args)
+                                    else
+                                      StgConApp dc NoNumber args' []
 
                 -- Some primitive operator that might be implemented as a library call.
                 -- As noted by Note [Eta expanding primops] in GHC.Builtin.PrimOps
@@ -560,6 +561,16 @@ coreToStgApp f args ticks = do
     -- profiling for #4367
     app `seq` return tapp
 
+
+-- Given Core arguments to an unboxed sum datacon, return the 'PrimRep's
+-- of every alternative. For example, in (#_|#) @LiftedRep @IntRep @Int @Int# 0
+-- the arguments are [Type LiftedRep, Type IntRep, Type Int, Type Int#, 0]
+-- and we return the list [[LiftedRep], [IntRep]].
+-- See Note [Representations in StgConApp] in GHC.Stg.Unarise.
+sumPrimReps :: [CoreArg] -> [[PrimRep]]
+sumPrimReps (Type ty : args) | isRuntimeRepKindedTy ty
+  = runtimeRepPrimRep (text "sumPrimReps") ty : sumPrimReps args
+sumPrimReps _ = []
 -- ---------------------------------------------------------------------------
 -- Argument lists
 -- This is the guy that turns applications into A-normal form


=====================================
compiler/GHC/Stg/Syntax.hs
=====================================
@@ -310,7 +310,7 @@ for the details of this transformation.
   | StgConApp   DataCon
                 ConstructorNumber
                 [StgArg] -- Saturated. See Note [Constructor applications in STG]
-                [Type]   -- See Note [Types in StgConApp] in GHC.Stg.Unarise
+                [[PrimRep]]   -- See Note [Representations in StgConApp] in GHC.Stg.Unarise
 
   | StgOpApp    StgOp    -- Primitive op or foreign call
                 [StgArg] -- Saturated.


=====================================
compiler/GHC/Stg/Unarise.hs
=====================================
@@ -166,8 +166,8 @@ avoid #19645. Other alternatives considered include:
     way to fix what is ultimately a corner-case.
 
 
-Note [Types in StgConApp]
-~~~~~~~~~~~~~~~~~~~~~~~~~
+Note [Representations in StgConApp]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Suppose we have this unboxed sum term:
 
   (# 123 | #)
@@ -180,9 +180,21 @@ type of this term. For example, these are all valid tuples for this:
   (# 1#, 123, rubbish, rubbish #)
                          -- when type is (# Int | (# Int, Int, Int #) #)
 
-So we pass type arguments of the DataCon's TyCon in StgConApp to decide what
-layout to use. Note that unlifted values can't be let-bound, so we don't need
-types in StgRhsCon.
+Therefore, in StgConApp we store a list [[PrimRep]] of representations
+to decide what layout to use.
+Given (# T_1 | ... | T_n #), this list will be
+[typePrimRep T_1, ..., typePrimRep T_n].
+For example, given type
+  (# Int | String #)              we will store [[LiftedRep], [LiftedRep]]
+  (# Int | Float# #)              we will store [[LiftedRep], [FloatRep]]
+  (# Int | (# Int, Int, Int #) #) we will store [[LiftedRep], [LiftedRep, LiftedRep, LiftedRep]].
+
+This field is used for unboxed sums only and it's an empty list otherwise.
+Perhaps it would be more elegant to have a separate StgUnboxedSumCon,
+but that would require duplication of code in cases where the logic is shared.
+
+Note that unlifted values can't be let-bound, so we don't need
+representations in StgRhsCon.
 
 Note [Casting slot arguments]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -527,7 +539,7 @@ unariseExpr rho (StgConApp dc n args ty_args)
           -> return $ (mkTuple args')
   | otherwise =
       let args' = unariseConArgs rho args in
-      return $ (StgConApp dc n args' (map stgArgType args'))
+      return $ (StgConApp dc n args' [])
 
 unariseExpr rho (StgOpApp op args ty)
   = return (StgOpApp op (unariseFunArgs rho args) ty)
@@ -572,7 +584,7 @@ unariseExpr rho (StgTick tick e)
   = StgTick tick <$> unariseExpr rho e
 
 -- Doesn't return void args.
-unariseUbxSumOrTupleArgs :: UnariseEnv -> UniqSupply -> DataCon -> [InStgArg] -> [Type]
+unariseUbxSumOrTupleArgs :: UnariseEnv -> UniqSupply -> DataCon -> [InStgArg] -> [[PrimRep]]
                    -> ( [OutStgArg]           -- Arguments representing the unboxed sum
                       , Maybe (StgExpr -> StgExpr)) -- Transformation to apply to the arguments, to bring them
                                                     -- into the right Rep
@@ -860,7 +872,7 @@ mkCast arg_in cast_op out_id out_ty in_rhs =
 --
 -- Example, for (# x | #) :: (# (# #) | Int #) we call
 --
---   mkUbxSum (# _ | #) [ (# #), Int ] [ voidPrimId ]
+--   mkUbxSum (# _ | #) [ [], [LiftedRep] ] [ voidPrimId ]
 --
 -- which returns
 --
@@ -869,7 +881,7 @@ mkCast arg_in cast_op out_id out_ty in_rhs =
 mkUbxSum
   :: HasDebugCallStack
   => DataCon      -- Sum data con
-  -> [Type]       -- Type arguments of the sum data con
+  -> [[PrimRep]]  -- Representations of type arguments of the sum data con
   -> [OutStgArg]  -- Actual arguments of the alternative.
   -> UniqSupply
   -> ([OutStgArg] -- Final tuple arguments
@@ -877,7 +889,7 @@ mkUbxSum
      )
 mkUbxSum dc ty_args args0 us
   = let
-      _ :| sum_slots = ubxSumRepType (map typePrimRep ty_args)
+      _ :| sum_slots = ubxSumRepType ty_args
       -- drop tag slot
       field_slots = (mapMaybe (repSlotTy . stgArgRep) args0)
       tag = dataConTag dc
@@ -1121,7 +1133,7 @@ isUnboxedTupleBndr :: Id -> Bool
 isUnboxedTupleBndr = isUnboxedTupleType . idType
 
 mkTuple :: [StgArg] -> StgExpr
-mkTuple args = StgConApp (tupleDataCon Unboxed (length args)) NoNumber args (map stgArgType args)
+mkTuple args = StgConApp (tupleDataCon Unboxed (length args)) NoNumber args []
 
 tagAltTy :: AltType
 tagAltTy = PrimAlt IntRep


=====================================
compiler/GHC/StgToByteCode.hs
=====================================
@@ -83,7 +83,6 @@ import Data.IntMap (IntMap)
 import Data.List.NonEmpty (NonEmpty(..))
 import qualified Data.Map as Map
 import qualified Data.IntMap as IntMap
-import qualified Data.List.NonEmpty as NE
 import qualified GHC.Data.FiniteMap as Map
 import Data.Ord
 import GHC.Stack.CCS
@@ -297,11 +296,6 @@ argBits platform (rep : args)
   | isFollowableArg rep  = False : argBits platform args
   | otherwise = replicate (argRepSizeW platform rep) True ++ argBits platform args
 
-non_void :: NonEmpty ArgRep -> [ArgRep]
-non_void = NE.filter nv
-  where nv V = False
-        nv _ = True
-
 -- -----------------------------------------------------------------------------
 -- schemeTopBind
 
@@ -495,11 +489,9 @@ returnUnliftedAtom
     -> StgArg
     -> BcM BCInstrList
 returnUnliftedAtom d s p e = do
-    let reps = case e of
-                 StgLitArg lit -> typePrimRepArgs (literalType lit)
-                 StgVarArg i   -> bcIdPrimReps i
+    let reps = stgArgRep e
     (push, szb) <- pushAtom d p e
-    ret <- returnUnliftedReps d s szb (NE.toList $! reps)
+    ret <- returnUnliftedReps d s szb reps
     return (push `appOL` ret)
 
 -- return an unlifted value from the top of the stack
@@ -512,9 +504,7 @@ returnUnliftedReps
 returnUnliftedReps d s szb reps = do
     profile <- getProfile
     let platform = profilePlatform profile
-        non_void VoidRep = False
-        non_void _ = True
-    ret <- case filter non_void reps of
+    ret <- case reps of
              -- use RETURN for nullary/unary representations
              []    -> return (unitOL $ RETURN V)
              [rep] -> return (unitOL $ RETURN (toArgRep platform rep))
@@ -549,10 +539,12 @@ returnUnboxedTuple d s p es = do
                                          massert (off == dd + szb)
                                          go (dd + szb) (push:pushes) cs
     pushes <- go d [] tuple_components
+    let non_void VoidRep = False
+        non_void _ = True
     ret <- returnUnliftedReps d
                               s
                               (wordsToBytes platform $ nativeCallSize call_info)
-                              (map atomPrimRep es)
+                              (filter non_void $ map atomPrimRep es)
     return (mconcat pushes `appOL` ret)
 
 -- Compile code to apply the given expression to the remaining args
@@ -866,7 +858,7 @@ doCase d s p scrut bndr alts
         -- have the same runtime rep. We have more efficient specialized
         -- return frames for the situations with one non-void element.
 
-        non_void_arg_reps = non_void (typeArgReps platform bndr_ty)
+        non_void_arg_reps = typeArgReps platform bndr_ty
         ubx_tuple_frame =
           (isUnboxedTupleType bndr_ty || isUnboxedSumType bndr_ty) &&
           length non_void_arg_reps > 1
@@ -899,7 +891,7 @@ doCase d s p scrut bndr alts
         (bndr_size, call_info, args_offsets)
            | ubx_tuple_frame =
                let bndr_ty = primRepCmmType platform
-                   bndr_reps = NE.filter (not.isVoidRep) (bcIdPrimReps bndr)
+                   bndr_reps = typePrimRep (idType bndr)
                    (call_info, args_offsets) =
                        layoutNativeCall profile NativeTupleReturn 0 bndr_ty bndr_reps
                in ( wordsToBytes platform (nativeCallSize call_info)
@@ -1695,19 +1687,15 @@ maybe_getCCallReturnRep :: Type -> Maybe PrimRep
 maybe_getCCallReturnRep fn_ty
    = let
        (_a_tys, r_ty) = splitFunTys (dropForAlls fn_ty)
-       r_reps = typePrimRepArgs r_ty
-
-       blargh :: a -- Used at more than one type
-       blargh = pprPanic "maybe_getCCallReturn: can't handle:"
-                         (pprType fn_ty)
      in
-       case r_reps of
-         VoidRep :| [] -> Nothing
-         rep     :| [] -> Just rep
+       case typePrimRep r_ty of
+         [] -> Nothing
+         [rep] -> Just rep
 
                  -- if it was, it would be impossible to create a
                  -- valid return value placeholder on the stack
-         _             -> blargh
+         _ -> pprPanic "maybe_getCCallReturn: can't handle:"
+                         (pprType fn_ty)
 
 maybe_is_tagToEnum_call :: CgStgExpr -> Maybe (Id, [Name])
 -- Detect and extract relevant info for the tagToEnum kludge.
@@ -2147,7 +2135,7 @@ idSizeCon platform var
     isUnboxedSumType (idType var) =
     wordsToBytes platform .
     WordOff . sum . map (argRepSizeW platform . toArgRep platform) .
-    NE.toList . bcIdPrimReps $ var
+    typePrimRep . idType $ var
   | otherwise = ByteOff (primRepSizeB platform (bcIdPrimRep var))
 
 bcIdArgRep :: Platform -> Id -> ArgRep
@@ -2160,10 +2148,6 @@ bcIdPrimRep id
   | otherwise
   = pprPanic "bcIdPrimRep" (ppr id <+> dcolon <+> ppr (idType id))
 
-
-bcIdPrimReps :: Id -> NonEmpty PrimRep
-bcIdPrimReps id = typePrimRepArgs (idType id)
-
 repSizeWords :: Platform -> PrimRep -> WordOff
 repSizeWords platform rep = WordOff $ argRepSizeW platform (toArgRep platform rep)
 
@@ -2214,8 +2198,8 @@ atomRep platform e = toArgRep platform (atomPrimRep e)
 mkStackOffsets :: ByteOff -> [ByteOff] -> [ByteOff]
 mkStackOffsets original_depth szsb = tail (scanl' (+) original_depth szsb)
 
-typeArgReps :: Platform -> Type -> NonEmpty ArgRep
-typeArgReps platform = NE.map (toArgRep platform) . typePrimRepArgs
+typeArgReps :: Platform -> Type -> [ArgRep]
+typeArgReps platform = map (toArgRep platform) . typePrimRep
 
 -- -----------------------------------------------------------------------------
 -- The bytecode generator's monad


=====================================
docs/users_guide/exts/kind_signatures.rst
=====================================
@@ -37,6 +37,10 @@ This extension enables kind signatures in the following places:
 
          data Set (cxt :: Type -> Type) a = Set [a]
 
+-  ``newtype`` declarations: ::
+
+         newtype Set (cxt :: Type -> Type) a = Set [a]
+
 -  ``type`` declarations: ::
 
          type T (f :: Type -> Type) = f Int



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/efe9bca30f7f989d28485332434f4ea236e57111...493460bf77f708cbe81acfc31e7213c451174ae2

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/efe9bca30f7f989d28485332434f4ea236e57111...493460bf77f708cbe81acfc31e7213c451174ae2
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240103/16327d6d/attachment-0001.html>


More information about the ghc-commits mailing list