[Git][ghc/ghc][wip/export-finaliser-exceptions] 6 commits: Adjust AArch64 stackFrameHeaderSize

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Wed May 10 18:23:12 UTC 2023



Ben Gamari pushed to branch wip/export-finaliser-exceptions at Glasgow Haskell Compiler / GHC


Commits:
0657b482 by Sven Tennie at 2023-05-09T22:22:42-04:00
Adjust AArch64 stackFrameHeaderSize

The prologue of each stack frame are the saved LR and FP registers, 8
byte each. I.e. the size of the stack frame header is 2 * 8 byte.

- - - - -
7788c09c by konsumlamm at 2023-05-09T22:23:23-04:00
Make `(&)` representation polymorphic in the return type

- - - - -
b3195922 by Ben Gamari at 2023-05-10T05:06:45-04:00
ghc-prim: Generalize keepAlive#/touch# in state token type

Closes #23163.

- - - - -
1e6861dd by Cheng Shao at 2023-05-10T05:07:25-04:00
Bump hsc2hs submodule

Fixes #22981.

- - - - -
f839c815 by Ben Gamari at 2023-05-10T14:21:47-04:00
base: Export {get,set}ExceptionFinalizer from System.Mem.Weak

As proposed in CLC Proposal #126 [1].

[1]: https://github.com/haskell/core-libraries-committee/issues/126

- - - - -
3fa0ea83 by Ben Gamari at 2023-05-10T14:22:59-04:00
base: Add printToStderrFinalizerExceptionHandler

- - - - -


12 changed files:

- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/CmmToAsm/AArch64/Instr.hs
- libraries/base/Data/Function.hs
- libraries/base/GHC/Weak.hs
- libraries/base/GHC/Weak/Finalize.hs
- + libraries/base/GHC/Weak/FinalizeIO.hs
- + libraries/base/GHC/Weak/FinalizeIO.hs-boot
- libraries/base/System/Mem/Weak.hs
- libraries/base/base.cabal
- libraries/base/changelog.md
- libraries/ghc-prim/changelog.md
- utils/hsc2hs


Changes:

=====================================
compiler/GHC/Builtin/primops.txt.pp
=====================================
@@ -3407,7 +3407,7 @@ primop  FinalizeWeakOp "finalizeWeak#" GenPrimOp
    out_of_line      = True
 
 primop TouchOp "touch#" GenPrimOp
-   v -> State# RealWorld -> State# RealWorld
+   v -> State# s -> State# s
    with
    code_size = { 0 }
    has_side_effects = True
@@ -3723,7 +3723,7 @@ section "Controlling object lifetime"
 -- and "p" is the same as "b" except representation-polymorphic.
 -- See Note [Levity and representation polymorphic primops]
 primop KeepAliveOp "keepAlive#" GenPrimOp
-   v -> State# RealWorld -> (State# RealWorld -> p) -> p
+   v -> State# s -> (State# s -> p) -> p
    { @'keepAlive#' x s k@ keeps the value @x@ alive during the execution
      of the computation @k at .
 


=====================================
compiler/GHC/CmmToAsm/AArch64/Instr.hs
=====================================
@@ -32,9 +32,9 @@ import Data.Maybe (fromMaybe)
 
 import GHC.Stack
 
--- | TODO: verify this!
-stackFrameHeaderSize :: Platform -> Int
-stackFrameHeaderSize _ = 64
+-- | LR and FP (8 byte each) are the prologue of each stack frame
+stackFrameHeaderSize :: Int
+stackFrameHeaderSize = 2 * 8
 
 -- | All registers are 8 byte wide.
 spillSlotSize :: Int
@@ -49,14 +49,13 @@ stackAlign = 16
 maxSpillSlots :: NCGConfig -> Int
 maxSpillSlots config
 --  = 0 -- set to zero, to see when allocMoreStack has to fire.
-    = let platform = ncgPlatform config
-      in ((ncgSpillPreallocSize config - stackFrameHeaderSize platform)
+    = ((ncgSpillPreallocSize config - stackFrameHeaderSize)
          `div` spillSlotSize) - 1
 
 -- | Convert a spill slot number to a *byte* offset, with no sign.
 spillSlotToOffset :: NCGConfig -> Int -> Int
-spillSlotToOffset config slot
-   = stackFrameHeaderSize (ncgPlatform config) + spillSlotSize * slot
+spillSlotToOffset _ slot
+   = stackFrameHeaderSize + spillSlotSize * slot
 
 -- | Get the registers that are being used by this instruction.
 -- regUsage doesn't need to do any trickery for jumps and such.


=====================================
libraries/base/Data/Function.hs
=====================================
@@ -1,3 +1,5 @@
+{-# LANGUAGE ExplicitForAll #-}
+{-# LANGUAGE PolyKinds #-}
 {-# LANGUAGE Trustworthy #-}
 {-# LANGUAGE NoImplicitPrelude #-}
 {-# OPTIONS_HADDOCK print-explicit-runtime-reps #-}
@@ -28,7 +30,7 @@ module Data.Function
   , applyWhen
   ) where
 
-import GHC.Base ( ($), (.), id, const, flip )
+import GHC.Base ( TYPE, ($), (.), id, const, flip )
 import Data.Bool ( Bool(..) )
 
 infixl 0 `on`
@@ -120,7 +122,7 @@ on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
 -- "6"
 --
 -- @since 4.8.0.0
-(&) :: a -> (a -> b) -> b
+(&) :: forall r a (b :: TYPE r). a -> (a -> b) -> b
 x & f = f x
 
 -- | 'applyWhen' applies a function to a value if a condition is true,


=====================================
libraries/base/GHC/Weak.hs
=====================================
@@ -31,7 +31,8 @@ module GHC.Weak (
         -- 'setFinalizerExceptionHandler'. Note that any exceptions thrown by
         -- this handler will be ignored.
         setFinalizerExceptionHandler,
-        getFinalizerExceptionHandler
+        getFinalizerExceptionHandler,
+        printToStderrFinalizerExceptionHandler
     ) where
 
 import GHC.Base


=====================================
libraries/base/GHC/Weak/Finalize.hs
=====================================
@@ -11,6 +11,7 @@ module GHC.Weak.Finalize
       -- this handler will be ignored.
       setFinalizerExceptionHandler
     , getFinalizerExceptionHandler
+    , printToStderrFinalizerExceptionHandler
       -- * Internal
     , runFinalizerBatch
     ) where
@@ -20,6 +21,7 @@ import GHC.Exception
 import GHC.IORef
 import {-# SOURCE #-} GHC.Conc.Sync (labelThreadByteArray#, myThreadId)
 import GHC.IO (catchException, unsafePerformIO)
+import {-# SOURCE #-} GHC.Weak.FinalizeIO ( hPutStrLnStderr )
 import GHC.Encoding.UTF8 (utf8EncodeByteArray#)
 
 data ByteArray = ByteArray ByteArray#
@@ -79,3 +81,13 @@ getFinalizerExceptionHandler = readIORef finalizerExceptionHandler
 -- @since 4.18.0.0
 setFinalizerExceptionHandler :: (SomeException -> IO ()) -> IO ()
 setFinalizerExceptionHandler = writeIORef finalizerExceptionHandler
+
+-- | An exception handler for 'Handle' finalization that prints the error to
+-- @stderr@, but doesn't rethrow it.
+--
+-- @since 4.18.0.0
+printToStderrFinalizerExceptionHandler :: SomeException -> IO ()
+printToStderrFinalizerExceptionHandler se =
+    hPutStrStderr msg `catchException` (\(SomeException _) -> return ())
+  where
+    msg = "Exception during weak pointer finalization (ignored): " ++ displayException se ++ "\n"


=====================================
libraries/base/GHC/Weak/FinalizeIO.hs
=====================================
@@ -0,0 +1,12 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+-- | This internal module exists solely to allow us to break the cycle introduced
+-- by the use of 'Handle' in
+-- 'GHC.Weak.Finalize.printToStderrFinalizeExceptionHandler'
+module GHC.Weak.FinalizeIO ( hPutStrLnStderr ) where
+
+import GHC.Base
+import System.IO
+
+hPutStrLnStderr :: String -> IO ()
+hPutStrLnStderr = hPutStrLn stderr


=====================================
libraries/base/GHC/Weak/FinalizeIO.hs-boot
=====================================
@@ -0,0 +1,7 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+module GHC.Weak.FinalizeIO ( hPutStrLnStderr ) where
+
+import GHC.Base
+
+hPutStrLnStderr :: String -> IO ()


=====================================
libraries/base/System/Mem/Weak.hs
=====================================
@@ -64,6 +64,15 @@ module System.Mem.Weak (
         mkWeakPair,
         -- replaceFinaliser
 
+        -- * Handling exceptions
+        -- | When an exception is thrown by a finalizer called by the
+        -- garbage collector, GHC calls a global handler which can be set with
+        -- 'setFinalizerExceptionHandler'. Note that any exceptions thrown by
+        -- this handler will be ignored.
+        setFinalizerExceptionHandler,
+        getFinalizerExceptionHandler,
+        printToStderrFinalizerExceptionHandler,
+
         -- * A precise semantics
 
         -- $precise


=====================================
libraries/base/base.cabal
=====================================
@@ -359,6 +359,7 @@ Library
         GHC.Unicode.Internal.Char.UnicodeData.SimpleTitleCaseMapping
         GHC.Unicode.Internal.Char.UnicodeData.SimpleUpperCaseMapping
         GHC.Unicode.Internal.Version
+        GHC.Weak.FinalizeIO
         System.Environment.ExecutablePath
         System.CPUTime.Utils
 


=====================================
libraries/base/changelog.md
=====================================
@@ -13,6 +13,8 @@
   * Add `Type.Reflection.decTypeRep`, `Data.Typeable.decT` and `Data.Typeable.hdecT` equality decisions functions.
       ([CLC proposal #98](https://github.com/haskell/core-libraries-committee/issues/98))
   * Add `Data.Functor.unzip` ([CLC proposal #88](https://github.com/haskell/core-libraries-committee/issues/88))
+  * Add `System.Mem.Weak.{get,set}FinalizerExceptionHandler`, which allows the user to set the global handler invoked by when a `Weak` pointer finalizer throws an exception. ([CLC proposal #126](https://github.com/haskell/core-libraries-committee/issues/126))
+  * Add `System.Mem.Weak.printToStderrFinalizerExceptionHandler`, which can be used with `setFinalizerExceptionHandler` to print exceptions thrown by finalizers to `stderr`.  ([CLC proposal #126](https://github.com/haskell/core-libraries-committee/issues/126))
   * Implement more members of `instance Foldable (Compose f g)` explicitly.
       ([CLC proposal #57](https://github.com/haskell/core-libraries-committee/issues/57))
   * Add `Eq` and `Ord` instances for `SSymbol`, `SChar`, and `SNat`.
@@ -21,9 +23,10 @@
       ([CLC proposal #149](https://github.com/haskell/core-libraries-committee/issues/149))
   * Make `($)` representation polymorphic ([CLC proposal #132](https://github.com/haskell/core-libraries-committee/issues/132))
   * Implemented [GHC Proposal #433](https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0433-unsatisfiable.rst),
-    adding the class `Unsatisfiable :: ErrorMessage -> TypeError`` to `GHC.TypeError`,
+    adding the class `Unsatisfiable :: ErrorMessage -> TypeError` to `GHC.TypeError`,
     which provides a mechanism for custom type errors that reports the errors in
-    a more predictable behaviour than ``TypeError``.
+    a more predictable behaviour than `TypeError`.
+  * Make `(&)` representation polymorphic in the return type ([CLC proposal #158](https://github.com/haskell/core-libraries-committee/issues/158))
 
 ## 4.18.0.0 *March 2023*
   * Shipped with GHC 9.6.1


=====================================
libraries/ghc-prim/changelog.md
=====================================
@@ -14,6 +14,8 @@
     - `sameMutVar#`, `sameTVar#`, `sameMVar#`
     - `sameIOPort#`, `eqStableName#`.
 
+- `keepAlive#` and `touch#` are now polymorphic in their state token (#23163; [CLC#152](https://github.com/haskell/core-libraries-committee/issues/152))
+
 - Several new primops were added:
 
   - `copyMutableByteArrayNonOverlapping#`


=====================================
utils/hsc2hs
=====================================
@@ -1 +1 @@
-Subproject commit 1ba092932f86c1fda15091d355ba7975b8554437
+Subproject commit f70b360b295298e4da10afe02ebf022b21342008



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/638fe9281a621ffadc9e1e5972538e475b53b144...3fa0ea83ae1c33fa8812a3aa55682819656ca90a

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/638fe9281a621ffadc9e1e5972538e475b53b144...3fa0ea83ae1c33fa8812a3aa55682819656ca90a
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/20230510/fe508a86/attachment-0001.html>


More information about the ghc-commits mailing list