[Git][ghc/ghc][wip/romes/isNullaryRepDataCon] WIP #23158

Rodrigo Mesquita (@alt-romes) gitlab at gitlab.haskell.org
Fri Apr 21 13:09:32 UTC 2023



Rodrigo Mesquita pushed to branch wip/romes/isNullaryRepDataCon at Glasgow Haskell Compiler / GHC


Commits:
61633df8 by Rodrigo Mesquita at 2023-04-21T14:09:12+01:00
WIP #23158

- - - - -


5 changed files:

- compiler/GHC/Cmm/CLabel.hs
- compiler/GHC/StgToCmm.hs
- compiler/GHC/StgToCmm/Bind.hs
- compiler/GHC/StgToCmm/DataCon.hs
- compiler/GHC/StgToCmm/Layout.hs


Changes:

=====================================
compiler/GHC/Cmm/CLabel.hs
=====================================
@@ -30,6 +30,7 @@ module GHC.Cmm.CLabel (
         mkApEntryLabel,
         mkApInfoTableLabel,
         mkClosureTableLabel,
+        mkShortConAppLabel,
         mkBytesLabel,
 
         mkLocalBlockLabel,
@@ -521,6 +522,8 @@ data IdLabelInfo
 
   | ClosureTable        -- ^ Table of closures for Enum tycons
 
+  | ShortConApp         -- ^ Temporary name, temporary documentation. A special static closure for nullarydatacons
+
   | Bytes               -- ^ Content of a string literal. See
                         -- Note [Bytes label].
   | BlockInfoTable      -- ^ Like LocalInfoTable but for a proc-point block
@@ -554,6 +557,7 @@ instance Outputable IdLabelInfo where
   ppr (ConEntry mn) = text "ConEntry" <+> ppr mn
   ppr (ConInfoTable mn) = text "ConInfoTable" <+> ppr mn
   ppr ClosureTable = text "ClosureTable"
+  ppr ShortConApp  = text "ShortConApp" -- ROMES:TODO: name
   ppr Bytes        = text "Bytes"
   ppr BlockInfoTable  = text "BlockInfoTable"
   ppr (IdTickyInfo info) = text "IdTickyInfo" <+> ppr info
@@ -619,6 +623,7 @@ mkClosureLabel              :: Name -> CafInfo -> CLabel
 mkInfoTableLabel            :: Name -> CafInfo -> CLabel
 mkEntryLabel                :: Name -> CafInfo -> CLabel
 mkClosureTableLabel         :: Name -> CafInfo -> CLabel
+mkShortConAppLabel          :: Name -> CafInfo -> CLabel
 mkConInfoTableLabel         :: Name -> ConInfoTableLocation -> CLabel
 mkBytesLabel                :: Name -> CLabel
 mkClosureLabel name         c     = IdLabel name c Closure
@@ -628,6 +633,7 @@ mkInfoTableLabel name       c
   | otherwise           = IdLabel name c LocalInfoTable
 mkEntryLabel name           c     = IdLabel name c Entry
 mkClosureTableLabel name    c     = IdLabel name c ClosureTable
+mkShortConAppLabel name     c     = IdLabel name c ShortConApp
 -- Special case for the normal 'DefinitionSite' case so that the 'ConInfoTable' application can be floated to a CAF.
 mkConInfoTableLabel name DefinitionSite = IdLabel name NoCafRefs (ConInfoTable DefinitionSite)
 mkConInfoTableLabel name k = IdLabel name NoCafRefs (ConInfoTable k)
@@ -780,6 +786,7 @@ isForeignLabel _lbl = False
 isStaticClosureLabel :: CLabel -> Bool
 -- Closure defined in haskell (.hs)
 isStaticClosureLabel (IdLabel _ _ Closure) = True
+isStaticClosureLabel (IdLabel _ _ ShortConApp) = True
 -- Closure defined in cmm
 isStaticClosureLabel (CmmLabel _ _ _ CmmClosure) = True
 isStaticClosureLabel _lbl = False
@@ -1251,6 +1258,7 @@ idInfoLabelType info =
     ConInfoTable {} -> DataLabel
     ClosureTable  -> DataLabel
     IdTickyInfo{} -> DataLabel
+    ShortConApp   -> DataLabel
     Bytes         -> DataLabel
     _             -> CodeLabel
 
@@ -1655,6 +1663,7 @@ ppIdFlavor x = pp_cSEP <> case x of
       UsageSite m n ->
         pprModule m <> pp_cSEP <> int n <> pp_cSEP <> text "con_info"
    ClosureTable     -> text "closure_tbl"
+   ShortConApp      -> text "special_con_app" -- ROMES:TODO: Name
    Bytes            -> text "bytes"
    BlockInfoTable   -> text "info"
 


=====================================
compiler/GHC/StgToCmm.hs
=====================================
@@ -233,15 +233,16 @@ mkModuleInit cost_centre_info this_mod hpc_info
 cgEnumerationTyCon :: TyCon -> FCode ()
 cgEnumerationTyCon tycon
   = do platform <- getPlatform
+       -- ROMES:TODO: Kind of code I need to emit conapp declarations for nullary gadt
        emitRODataLits (mkClosureTableLabel (tyConName tycon) NoCafRefs)
              [ CmmLabelOff (mkClosureLabel (dataConName con) NoCafRefs)
                            (tagForCon platform con)
              | con <- tyConDataCons tycon]
 
 
+-- | Generate the entry code and associated info table for a constructor.
+-- Where are generating the static closure at all?
 cgDataCon :: ConInfoTableLocation -> DataCon -> FCode ()
--- Generate the entry code, info tables, and (for niladic constructor)
--- the static closure, for a constructor.
 cgDataCon mn data_con
   = do  { massert (not (isUnboxedTupleDataCon data_con || isUnboxedSumDataCon data_con))
         ; profile <- getProfile
@@ -264,6 +265,12 @@ cgDataCon mn data_con
                        , rep_ty <- typePrimRep (scaledThing ty)
                        , not (isVoidRep rep_ty) ]
 
+        -- In the case of a data con that isn't nullary in its core
+        -- representation, but that has no zero-width args, we generate a
+        -- special static closure alongside its normal _closure
+        -- ROMES:TODO: Write a long note about it
+        ; when (not (isNullaryRepDataCon data_con) && hasNoNonZeroWidthArgs data_con) (cgNullaryDataConApp data_con)
+
         ; emitClosureAndInfoTable platform dyn_info_tbl NativeDirectCall [] $
             -- NB: the closure pointer is assumed *untagged* on
             -- entry to a constructor.  If the pointer is tagged,


=====================================
compiler/GHC/StgToCmm/Bind.hs
=====================================
@@ -92,6 +92,24 @@ cgTopRhsClosure platform rec id ccs upd_flag args body =
 
   gen_code :: LambdaFormInfo -> CLabel -> FCode ()
 
+  -- special case for a datacon worker only takes zero-width arguments:
+  --
+  -- since the RHS is a fully saturated application of the worker (the void
+  -- args are dropped after unarisation), and because we generate precomputed
+  -- static closures for these nullary data constructors that take zero-width
+  -- arguments, the closure's body must be the pointer to the con_info table
+  -- ROMES:TODO: I think this path no longer works
+  -- gen_code _ closure_label
+  --   | StgConApp con _ conargs _ <- body
+  --   , null (assertNonVoidStgArgs conargs) -- After unarisation, StgConApp are guaranteed
+  --                                         -- not to be applied to void arguments, but we
+  --                                         -- might not have run unarisation yet at this
+  --                                         -- point. Right? Otherwise, we could have a match
+  --                                         -- on empty list of args
+  --   = assert (hasNoNonZeroWidthArgs con) $
+  --     assert (null (nonVoidIds args))    $
+  --       emitDataCon closure_label indStaticInfoTable ccs []
+
   -- special case for a indirection (f = g).  We create an IND_STATIC
   -- closure pointing directly to the indirectee.  This is exactly
   -- what the CAF will eventually evaluate to anyway, we're just


=====================================
compiler/GHC/StgToCmm/DataCon.hs
=====================================
@@ -12,7 +12,8 @@
 -----------------------------------------------------------------------------
 
 module GHC.StgToCmm.DataCon (
-        cgTopRhsCon, buildDynCon, bindConArgs
+        cgTopRhsCon, buildDynCon, bindConArgs,
+        cgNullaryDataConApp
     ) where
 
 import GHC.Prelude
@@ -328,7 +329,15 @@ precomputedStaticConInfo_maybe :: StgToCmmConfig -> Id -> DataCon -> [NonVoid St
 precomputedStaticConInfo_maybe cfg binder con []
   -- Nullary constructors (list of nonvoid args is null)
   = assert (hasNoNonZeroWidthArgs con) $
-    Just $ litIdInfo (stgToCmmPlatform cfg) binder (mkConLFInfo con)
+    if isDataConWorkId binder && not (isNullaryRepDataCon con)
+       -- this data con worker (TODO: what about wrappers?) only has zero-width
+       -- args, so we point to its short con app closure instead of its
+       -- function ( rememeber, we generate 2 closures for a data con that only
+       -- has zero-width args: one for the function it actually is, another for
+       -- the precomputed static closure we can still have for it )
+       then Just $ litIdInfo (stgToCmmPlatform cfg) binder (mkConLFInfo con)
+                      (CmmLabel (mkShortConAppLabel (dataConName con) NoCafRefs))
+       else Just $ litIdInfo (stgToCmmPlatform cfg) binder (mkConLFInfo con)
                 (CmmLabel (mkClosureLabel (dataConName con) NoCafRefs))
 precomputedStaticConInfo_maybe cfg binder con [arg]
   -- Int/Char values with existing closures in the RTS
@@ -373,6 +382,14 @@ precomputedStaticConInfo_maybe cfg binder con [arg]
 
 precomputedStaticConInfo_maybe _ _ _ _ = Nothing
 
+-- Closely related to precomputed static things,,, write long note: ROMES:TODO
+cgNullaryDataConApp :: DataCon -> FCode ()
+cgNullaryDataConApp con
+  = emitRODataLits (mkShortConAppLabel (dataConName con) NoCafRefs)
+      [ CmmLabel (mkConInfoTableLabel (dataConName con) DefinitionSite)
+      ]
+
+
 ---------------------------------------------------------------
 --      Binding constructor arguments
 ---------------------------------------------------------------


=====================================
compiler/GHC/StgToCmm/Layout.hs
=====================================
@@ -653,6 +653,6 @@ emitClosureAndInfoTable
    :: Platform -> CmmInfoTable -> Convention -> [LocalReg] -> FCode () -> FCode ()
 emitClosureAndInfoTable platform info_tbl conv args body
   = do { (_, blks) <- getCodeScoped body
-       ; let entry_lbl = toEntryLbl platform (cit_lbl info_tbl)
+       ; let entry_lbl = toEntryLbl platform (cit_lbl info_tbl) -- _entry label for this declaration
        ; emitProcWithConvention conv (Just info_tbl) entry_lbl args blks
        }



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/61633df8f4c0d85c8eb13c10be2b20932def4004

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/61633df8f4c0d85c8eb13c10be2b20932def4004
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/20230421/4442ea27/attachment-0001.html>


More information about the ghc-commits mailing list