[Git][ghc/ghc][wip/andreask/inlineable-threshold] Store guidance for stable unfoldings

Andreas Klebinger (@AndreasK) gitlab at gitlab.haskell.org
Mon Dec 12 17:32:42 UTC 2022



Andreas Klebinger pushed to branch wip/andreask/inlineable-threshold at Glasgow Haskell Compiler / GHC


Commits:
8da7e63d by Andreas Klebinger at 2022-12-12T18:30:53+01:00
Store guidance for stable unfoldings

- - - - -


6 changed files:

- compiler/GHC/Core/Opt/Simplify/Iteration.hs
- compiler/GHC/Core/Unfold.hs
- compiler/GHC/Core/Unfold/Make.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/IfaceToCore.hs


Changes:

=====================================
compiler/GHC/Core/Opt/Simplify/Iteration.hs
=====================================
@@ -4280,7 +4280,7 @@ simplStableUnfolding env bind_cxt id rhs_ty id_arity unf opt_rhs
                   -- based on the size of the optimized core. After all this is what the unfolding
                   -- will optimize to eventually!
                   _other -> do
-                    let rhs_guide = calcUnfoldingGuidance uf_opts (is_top_lvl && is_bottoming) src opt_rhs
+                    let rhs_guide = calcUnfoldingGuidance uf_opts (is_top_lvl && is_bottoming) opt_rhs
                     return (mkCoreUnfolding src is_top_lvl unf_expr' rhs_guide)
 
                     -- return $ CoreUnfolding { uf_tmpl = unf_expr', uf_src = src, uf_guidance = rhs_guide }


=====================================
compiler/GHC/Core/Unfold.hs
=====================================
@@ -229,13 +229,12 @@ inlineBoringOk e
 calcUnfoldingGuidance
         :: UnfoldingOpts
         -> Bool          -- Definitely a top-level, bottoming binding
-        -> UnfoldingSource -- Tells us if this is a stable unfolding
         -> CoreExpr      -- Expression to look at
         -> UnfoldingGuidance
-calcUnfoldingGuidance opts is_top_bottoming src (Tick t expr)
+calcUnfoldingGuidance opts is_top_bottoming (Tick t expr)
   | not (tickishIsCode t)  -- non-code ticks don't matter for unfolding
-  = calcUnfoldingGuidance opts is_top_bottoming src expr
-calcUnfoldingGuidance opts is_top_bottoming src expr
+  = calcUnfoldingGuidance opts is_top_bottoming expr
+calcUnfoldingGuidance opts is_top_bottoming expr
   = case sizeExpr opts bOMB_OUT_SIZE val_bndrs body of
       TooBig -> UnfNever
       SizeIs size cased_bndrs scrut_discount


=====================================
compiler/GHC/Core/Unfold/Make.hs
=====================================
@@ -107,7 +107,7 @@ mkWorkerUnfolding opts work_fn
   = mkCoreUnfolding src top_lvl new_tmpl guidance
   where
     new_tmpl = simpleOptExpr opts (work_fn tmpl)
-    guidance = calcUnfoldingGuidance (so_uf_opts opts) False src new_tmpl
+    guidance = calcUnfoldingGuidance (so_uf_opts opts) False new_tmpl
 
 mkWorkerUnfolding _ _ _ = noUnfolding
 
@@ -317,7 +317,7 @@ mkUnfolding opts src top_lvl is_bottoming expr
   = mkCoreUnfolding src top_lvl expr guidance
   where
     is_top_bottoming = top_lvl && is_bottoming
-    guidance         = calcUnfoldingGuidance opts is_top_bottoming src expr
+    guidance         = calcUnfoldingGuidance opts is_top_bottoming expr
         -- NB: *not* (calcUnfoldingGuidance (occurAnalyseExpr expr))!
         -- See Note [Calculate unfolding guidance on the non-occ-anal'd expression]
 


=====================================
compiler/GHC/CoreToIface.hs
=====================================
@@ -535,6 +535,8 @@ toIfGuidance :: UnfoldingSource -> UnfoldingGuidance -> IfGuidance
 toIfGuidance src guidance
   | UnfWhen arity unsat_ok boring_ok <- guidance
   , isStableSource src = IfWhen arity unsat_ok boring_ok
+  | UnfIfGoodArgs args size res <- guidance
+  , isStableSource src = IfStableGuidance args size res
   | otherwise          = IfNoGuidance
 
 {-


=====================================
compiler/GHC/Iface/Syntax.hs
=====================================
@@ -371,6 +371,10 @@ data IfaceUnfolding
 data IfGuidance
   = IfNoGuidance            -- Compute it from the IfaceExpr
   | IfWhen Arity Bool Bool  -- Just like UnfWhen in Core.UnfoldingGuidance
+  | IfStableGuidance  { iug_args :: [Int]
+                      , iug_size :: Int
+                      , iug_res :: Int
+                      }
 
 -- We only serialise the IdDetails of top-level Ids, and even then
 -- we only need a very limited selection.  Notably, none of the
@@ -1530,6 +1534,8 @@ instance Outputable IfaceUnfolding where
 instance Outputable IfGuidance where
   ppr IfNoGuidance   = empty
   ppr (IfWhen a u b) = angleBrackets (ppr a <> comma <> ppr u <> ppr b)
+  ppr (IfStableGuidance a s r) =
+    angleBrackets (ppr a <> comma <> ppr s <> comma <> ppr r)
 
 {-
 ************************************************************************
@@ -2323,14 +2329,23 @@ instance Binary IfGuidance where
         put_ bh a
         put_ bh b
         put_ bh c
+    put_ bh (IfStableGuidance a s r ) = do
+        putByte bh 2
+        put_ bh a
+        put_ bh s
+        put_ bh r
     get bh = do
         h <- getByte bh
         case h of
             0 -> return IfNoGuidance
-            _ -> do a <- get bh
+            1 -> do a <- get bh
                     b <- get bh
                     c <- get bh
                     return (IfWhen a b c)
+            _ -> do a <- get bh
+                    s <- get bh
+                    r <- get bh
+                    return (IfStableGuidance a s r)
 
 instance Binary IfaceAlt where
     put_ bh (IfaceAlt a b c) = do
@@ -2685,6 +2700,7 @@ instance NFData IfGuidance where
   rnf = \case
     IfNoGuidance -> ()
     IfWhen a b c -> a `seq` b `seq` c `seq` ()
+    IfStableGuidance a b c -> a `seq` b `seq` c `seq` ()
 
 instance NFData IfaceUnfolding where
   rnf = \case


=====================================
compiler/GHC/IfaceToCore.hs
=====================================
@@ -1781,7 +1781,8 @@ tcUnfolding toplvl name _ info (IfCoreUnfold src if_guidance if_expr)
         ; expr <- tcUnfoldingRhs (isCompulsorySource src) toplvl name if_expr
         ; let guidance = case if_guidance of
                  IfWhen arity unsat_ok boring_ok -> UnfWhen arity unsat_ok boring_ok
-                 IfNoGuidance -> calcUnfoldingGuidance uf_opts is_top_bottoming src expr
+                 IfStableGuidance args size res -> UnfIfGoodArgs args size res
+                 IfNoGuidance -> calcUnfoldingGuidance uf_opts is_top_bottoming expr
         ; return $ mkCoreUnfolding src True expr guidance }
   where
     -- Strictness should occur before unfolding!



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8da7e63dfcefa07dd4f9ae070bd8252c0f8ced46

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8da7e63dfcefa07dd4f9ae070bd8252c0f8ced46
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/20221212/d540a50f/attachment-0001.html>


More information about the ghc-commits mailing list