[Git][ghc/ghc][wip/romes/async-rpc] interp: do not terminate ext. interp. process on exception
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
27 Oct '26
Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC
Commits:
863e9a6a by Andrea Vezzosi at 2026-10-27T22:02:52+01:00
interp: do not terminate ext. interp. process on exception
`sendMessage` throwing an exception does not warrant terminating the external
interpreter process anymore, because communication can keep going no matter what.
`stopInterp` now resets the whole `Interp` state, so that a new
ExtInterpInstance can be later created and work correctly.
`withExtInterp` now calls `stopInterp` on a `PipeClosedException` and rethrows.
Fixes #18852
- - - - -
5 changed files:
- compiler/GHC/Runtime/Interpreter.hs
- compiler/GHC/Runtime/Interpreter/Process.hs
- compiler/GHC/Tc/Gen/Splice.hs
- libraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/Message/Async.hs
Changes:
=====================================
compiler/GHC/Runtime/Interpreter.hs
=====================================
@@ -105,7 +105,7 @@ import GHCi.Run
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
-import Control.Monad.Catch as MC (mask)
+import Control.Monad.Catch as MC (mask, handle, MonadThrow (..))
import Data.Binary
import Data.ByteString (ByteString)
import qualified Data.ByteString.Short as SBS
@@ -113,6 +113,7 @@ import qualified GHC.Exts.Heap as Heap
import GHC.Stack.CCS (CostCentre,CostCentreStack)
import System.Directory
import System.Process
+import System.Timeout (timeout)
import qualified GHC.InfoProv as InfoProv
import GHC.Builtin.Modules( gHC_PRIM, gHC_PRIMOPWRAPPERS )
@@ -196,14 +197,16 @@ interpCmd interp msg = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> run msg -- Just run it directly
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst ->
+ ExternalInterp ext -> withExtInterp interp ext $ \inst ->
sendMessage inst msg
-withExtInterp :: ExceptionMonad m => ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
-withExtInterp ext action = case ext of
+withExtInterp :: ExceptionMonad m => Interp -> ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
+withExtInterp interp ext action = stopInterpOnPipeClosed $ case ext of
ExtJS i -> withJSInterp i action
ExtWasm i -> withWasmInterp i action
ExtIServ i -> withIServ i action
+ where
+ stopInterpOnPipeClosed = MC.handle $ \ (e :: PipeClosedException) -> liftIO (stopInterp interp) >> MC.throwM e
withExtInterpStatus :: ExtInterp -> (forall d. ExtInterpStatusVar d -> m a) -> m a
withExtInterpStatus ext action = case ext of
@@ -631,18 +634,41 @@ stopInterp interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> pure ()
#endif
- ExternalInterp ext -> withExtInterpStatus ext $ \mstate -> do
- MC.mask $ \_restore -> modifyMVar_ mstate $ \state -> do
+ ExternalInterp ext -> (maybe (pure ()) throwIO =<<) $ withExtInterpStatus ext $ \mstate -> do
+ MC.mask $ \restore -> modifyMVar mstate $ \state -> do
case state of
- InterpPending -> pure state -- already stopped
+ InterpPending -> pure (state, Nothing) -- already stopped
InterpRunning i -> do
ex <- getProcessExitCode (interpHandle (instProcess i))
- if isJust ex
- then pure ()
- else sendMessage i Shutdown
- -- We asked the server to shutdown, so we will also shutdown any communication.
- closePipe (interpPipe (instProcess i))
- pure InterpPending
+ e <- if isJust ex
+ then gracefulShutdown i restore
+ else closePipe (interpPipe (instProcess i)) >> pure Nothing
+ modifyMVar_ (loader_state (interpLoader interp)) (const $ pure Nothing)
+ purgeInterpSymbolCache (interpSymbolCache interp)
+ modifyMVar_ (interpStringCache interp) (const $ pure emptyFsEnv)
+ pure (InterpPending,e)
+ where
+ gracefulShutdown i restore = do
+ x <- try (timeLimited restore $ sendMessage i Shutdown `finally` closePipe (interpPipe (instProcess i)))
+ let ph = interpHandle (instProcess i)
+ e <- case x of
+ -- happy path: ext. interp. should be shutting down of its own accord.
+ Right (Just ()) -> pure Nothing
+ -- timeout: ext. interp. couldn't handle Shutdown in time.
+ Right Nothing -> do
+ terminateProcess ph
+ pure Nothing
+ -- something went wrong, probably Pipe already closed.
+ Left e -> do
+ terminateProcess ph
+ returnUnexpectedException e
+ void $ timeLimited restore $ void $ waitForProcess ph
+ pure e
+ timeLimited restore m = timeout 1_000_000 (restore m)
+ returnUnexpectedException e
+ | Just (_ :: PipeClosedException) <- fromException e = return Nothing
+ | Just (_ :: IOException) <- fromException e = return Nothing
+ | otherwise = pure (Just e)
-- -----------------------------------------------------------------------------
{- Note [External GHCi pointers]
=====================================
compiler/GHC/Runtime/Interpreter/Process.hs
=====================================
@@ -12,13 +12,10 @@ import GHC.Prelude
import GHC.Runtime.Interpreter.Types
import GHCi.Message
-import GHC.IO (catchException)
import GHC.Utils.Panic
import GHC.Utils.Exception as Ex
import Data.Binary
-import System.Exit
-import System.Process
import Control.Concurrent
import Data.IORef
import Control.Concurrent.STM
@@ -34,7 +31,6 @@ import Control.Concurrent.STM
sendMessage :: Binary a => ExtInterpInstance d -> Message a -> IO a
sendMessage i msg =
remoteCall (interpPipe proc) msg
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure proc e
where
proc = instProcess i
@@ -77,18 +73,6 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
else loop
in loop
-handleInterpProcessFailure :: InterpProcess -> SomeException -> IO a
-handleInterpProcessFailure i e = do
- let hdl = interpHandle i
- ex <- getProcessExitCode hdl
- case ex of
- Just (ExitFailure n) ->
- throwIO (InstallationError ("External interpreter terminated (" ++ show n ++ ")"))
- _ -> do
- terminateProcess hdl
- _ <- waitForProcess hdl
- throw e
-
data CancelTHHandler = CancelTHHandler deriving Show
instance Exception CancelTHHandler where
fromException = asyncExceptionFromException
=====================================
compiler/GHC/Tc/Gen/Splice.hs
=====================================
@@ -1160,7 +1160,7 @@ runRemoteModFinalizers (ThModFinalizers finRefs) = do
runQinTcM $ sequence_ qs
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
tcg <- getGblEnv
th_state <- readTcRef (tcg_th_remote_state tcg)
case th_state of
@@ -1817,7 +1817,7 @@ runTH ty fhv = do
return r
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
-- Run it on the server. For an overview of how TH works with
-- Remote GHCi, see Note [Remote Template Haskell] in
-- libraries/ghci/GHCi/TH.hs.
=====================================
libraries/ghci/GHCi/Message.hs
=====================================
@@ -27,7 +27,7 @@ module GHCi.Message
, Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
, remoteCall, remoteTHCall
, handleRemoteCall, handleRemoteTHCall, writeReply
- , RemoteProcessException(..), PipeReadClosedException(..)
+ , RemoteProcessException(..), PipeClosedException(..)
, InboxId(..)
, newMailbox
=====================================
libraries/ghci/GHCi/Message/Async.hs
=====================================
@@ -7,7 +7,7 @@ module GHCi.Message.Async
(
-- * Pipes
Pipe(..), mkPipeFromHandles, mkPipeFromContinuations
- , closePipe, PipeReadClosedException(..)
+ , closePipe, PipeClosedException(..)
-- * Interface for async bidirectional RPC
, remoteAsyncReq, handleAsyncReq
@@ -145,13 +145,13 @@ closePipe p@Pipe{..} = do
unless writeAlreadyClosed
(throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
--- | We've closed the pipe read end on this process
-data PipeReadClosedException = PipeReadClosedException deriving Show
-instance Exception PipeReadClosedException
+data PipeClosedException
+ = PipeReadClosedException -- ^ We've closed the pipe read end on this process
+
+ | PipeWriteClosedException -- ^ We've closed the pipe write end on this process
+ deriving Show
+instance Exception PipeClosedException
--- | We've closed the pipe write end on this process
-data PipeWriteClosedException = PipeWriteClosedException deriving Show
-instance Exception PipeWriteClosedException
--------------------------------------------------------------------------------
-- ** Pipe thread-safe read/write
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/863e9a6a9a84c9b7626d441eeadca15…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/863e9a6a9a84c9b7626d441eeadca15…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/romes/async-rpc] 3 commits: interp: cancel requests if async exceptions while waiting for reply.
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
27 Oct '26
Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC
Commits:
73b952d2 by Andrea Vezzosi at 2026-10-27T19:56:51+01:00
interp: cancel requests if async exceptions while waiting for reply.
- - - - -
d748ef6a by Andrea Vezzosi at 2026-10-27T19:56:51+01:00
interp: do not terminate ext. interp. process on exception
`sendMessage` throwing an exception does not warrant terminating the external
interpreter process anymore, because communication can keep going no matter what.
`stopInterp` now resets the whole `Interp` state, so that a new
ExtInterpInstance can be later created and work correctly.
`withExtInterp` now calls `stopInterp` on a `PipeClosedException` and rethrows.
Fixes #18852
- - - - -
8f45d12a by Andrea Vezzosi at 2026-10-27T21:59:17+01:00
fixup! interp: do not terminate ext. interp. process on exception
- - - - -
7 changed files:
- compiler/GHC/Runtime/Interpreter.hs
- compiler/GHC/Runtime/Interpreter/Process.hs
- compiler/GHC/Tc/Gen/Splice.hs
- libraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/Message/Async.hs
- libraries/ghci/GHCi/Server.hs
- libraries/ghci/GHCi/Server/Threads.hs
Changes:
=====================================
compiler/GHC/Runtime/Interpreter.hs
=====================================
@@ -105,7 +105,7 @@ import GHCi.Run
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
-import Control.Monad.Catch as MC (mask)
+import Control.Monad.Catch as MC (mask, handle, MonadThrow (..))
import Data.Binary
import Data.ByteString (ByteString)
import qualified Data.ByteString.Short as SBS
@@ -113,6 +113,7 @@ import qualified GHC.Exts.Heap as Heap
import GHC.Stack.CCS (CostCentre,CostCentreStack)
import System.Directory
import System.Process
+import System.Timeout (timeout)
import qualified GHC.InfoProv as InfoProv
import GHC.Builtin.Modules( gHC_PRIM, gHC_PRIMOPWRAPPERS )
@@ -196,14 +197,16 @@ interpCmd interp msg = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> run msg -- Just run it directly
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst ->
+ ExternalInterp ext -> withExtInterp interp ext $ \inst ->
sendMessage inst msg
-withExtInterp :: ExceptionMonad m => ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
-withExtInterp ext action = case ext of
+withExtInterp :: ExceptionMonad m => Interp -> ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
+withExtInterp interp ext action = stopInterpOnPipeClosed $ case ext of
ExtJS i -> withJSInterp i action
ExtWasm i -> withWasmInterp i action
ExtIServ i -> withIServ i action
+ where
+ stopInterpOnPipeClosed = MC.handle $ \ (e :: PipeClosedException) -> liftIO (stopInterp interp) >> MC.throwM e
withExtInterpStatus :: ExtInterp -> (forall d. ExtInterpStatusVar d -> m a) -> m a
withExtInterpStatus ext action = case ext of
@@ -631,18 +634,41 @@ stopInterp interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> pure ()
#endif
- ExternalInterp ext -> withExtInterpStatus ext $ \mstate -> do
- MC.mask $ \_restore -> modifyMVar_ mstate $ \state -> do
+ ExternalInterp ext -> (maybe (pure ()) throwIO =<<) $ withExtInterpStatus ext $ \mstate -> do
+ MC.mask $ \restore -> modifyMVar mstate $ \state -> do
case state of
- InterpPending -> pure state -- already stopped
+ InterpPending -> pure (state, Nothing) -- already stopped
InterpRunning i -> do
ex <- getProcessExitCode (interpHandle (instProcess i))
- if isJust ex
- then pure ()
- else sendMessage i Shutdown
- -- We asked the server to shutdown, so we will also shutdown any communication.
- closePipe (interpPipe (instProcess i))
- pure InterpPending
+ e <- if isJust ex
+ then gracefulShutdown i restore
+ else closePipe (interpPipe (instProcess i)) >> pure Nothing
+ modifyMVar_ (loader_state (interpLoader interp)) (const $ pure Nothing)
+ purgeInterpSymbolCache (interpSymbolCache interp)
+ modifyMVar_ (interpStringCache interp) (const $ pure emptyFsEnv)
+ pure (InterpPending,e)
+ where
+ gracefulShutdown i restore = do
+ x <- try (timeLimited restore $ sendMessage i Shutdown `finally` closePipe (interpPipe (instProcess i)))
+ let ph = interpHandle (instProcess i)
+ e <- case x of
+ -- happy path: ext. interp. should be shutting down of its own accord.
+ Right (Just ()) -> pure Nothing
+ -- timeout: ext. interp. couldn't handle Shutdown in time.
+ Right Nothing -> do
+ terminateProcess ph
+ pure Nothing
+ -- something went wrong, probably Pipe already closed.
+ Left e -> do
+ terminateProcess ph
+ returnUnexpectedException e
+ void $ timeLimited restore $ void $ waitForProcess ph
+ pure e
+ timeLimited restore m = timeout 1_000_000 (restore m)
+ returnUnexpectedException e
+ | Just (_ :: PipeClosedException) <- fromException e = return Nothing
+ | Just (_ :: IOException) <- fromException e = return Nothing
+ | otherwise = pure (Just e)
-- -----------------------------------------------------------------------------
{- Note [External GHCi pointers]
=====================================
compiler/GHC/Runtime/Interpreter/Process.hs
=====================================
@@ -12,13 +12,10 @@ import GHC.Prelude
import GHC.Runtime.Interpreter.Types
import GHCi.Message
-import GHC.IO (catchException)
import GHC.Utils.Panic
import GHC.Utils.Exception as Ex
import Data.Binary
-import System.Exit
-import System.Process
import Control.Concurrent
import Data.IORef
import Control.Concurrent.STM
@@ -34,7 +31,6 @@ import Control.Concurrent.STM
sendMessage :: Binary a => ExtInterpInstance d -> Message a -> IO a
sendMessage i msg =
remoteCall (interpPipe proc) msg
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure proc e
where
proc = instProcess i
@@ -64,7 +60,7 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
where
proc = instProcess i
handleOne mbox doneRef = do
- handleRemoteTHCall (interpPipe proc) mbox $ \req -> do
+ handleRemoteTHCall (interpPipe proc) mbox $ \_setCancel req -> do
(stop, res) <- reqHandler req
writeIORef doneRef stop
return res
@@ -77,18 +73,6 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
else loop
in loop
-handleInterpProcessFailure :: InterpProcess -> SomeException -> IO a
-handleInterpProcessFailure i e = do
- let hdl = interpHandle i
- ex <- getProcessExitCode hdl
- case ex of
- Just (ExitFailure n) ->
- throwIO (InstallationError ("External interpreter terminated (" ++ show n ++ ")"))
- _ -> do
- terminateProcess hdl
- _ <- waitForProcess hdl
- throw e
-
data CancelTHHandler = CancelTHHandler deriving Show
instance Exception CancelTHHandler where
fromException = asyncExceptionFromException
=====================================
compiler/GHC/Tc/Gen/Splice.hs
=====================================
@@ -1160,7 +1160,7 @@ runRemoteModFinalizers (ThModFinalizers finRefs) = do
runQinTcM $ sequence_ qs
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
tcg <- getGblEnv
th_state <- readTcRef (tcg_th_remote_state tcg)
case th_state of
@@ -1817,7 +1817,7 @@ runTH ty fhv = do
return r
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
-- Run it on the server. For an overview of how TH works with
-- Remote GHCi, see Note [Remote Template Haskell] in
-- libraries/ghci/GHCi/TH.hs.
=====================================
libraries/ghci/GHCi/Message.hs
=====================================
@@ -27,7 +27,7 @@ module GHCi.Message
, Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
, remoteCall, remoteTHCall
, handleRemoteCall, handleRemoteTHCall, writeReply
- , RemoteProcessException(..), PipeReadClosedException(..)
+ , RemoteProcessException(..), PipeClosedException(..)
, InboxId(..)
, newMailbox
@@ -666,12 +666,12 @@ remoteCall pipe msg = either throwIO (pure . runGet get)
-- | Handle a single 'Message' request on the interpreter and reply to the host
handleRemoteCall :: Pipe
-> (Msg -> IO Msg) {-^ Msg hook -}
- -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r)
+ -> (forall a. (Show a, Binary a) => WriteReply a %1 -> (IO () -> IO ()) -> Message a -> IO r)
-> IO r
handleRemoteCall pipe hook k =
- handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 ->
+ handleAsyncReq pipe getMessage Nothing \(WriteReply writer) sa msg0 -> IO \s0 ->
case hook msg0 |#> s0 of -- unIO for linearity checking
- (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1
+ (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) sa msg |#> s1
-- | Send a 'THMessage' request to the host and wait for a reply from the host
-- to the interpreter.
@@ -687,9 +687,9 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get)
-- Symmterically to 'remoteTHCall', we always want to specify the 'InboxId' for
-- RunTH "reverse requests"
handleRemoteTHCall
- :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
+ :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => (IO () -> IO ()) -> THMessage a -> IO a) -> IO ()
handleRemoteTHCall pipe ii k =
- handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 ->
- case k msg |#> s0 of -- unIO for linearity checking
+ handleAsyncReq pipe getTHMessage (Just ii) \writer sa (THMsg msg) -> IO \s0 ->
+ case k sa msg |#> s0 of -- unIO for linearity checking
(# s1, a #) -> writeReply writer (put a) |#> s1
=====================================
libraries/ghci/GHCi/Message/Async.hs
=====================================
@@ -7,7 +7,7 @@ module GHCi.Message.Async
(
-- * Pipes
Pipe(..), mkPipeFromHandles, mkPipeFromContinuations
- , closePipe, PipeReadClosedException(..)
+ , closePipe, PipeClosedException(..)
-- * Interface for async bidirectional RPC
, remoteAsyncReq, handleAsyncReq
@@ -145,13 +145,13 @@ closePipe p@Pipe{..} = do
unless writeAlreadyClosed
(throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
--- | We've closed the pipe read end on this process
-data PipeReadClosedException = PipeReadClosedException deriving Show
-instance Exception PipeReadClosedException
+data PipeClosedException
+ = PipeReadClosedException -- ^ We've closed the pipe read end on this process
+
+ | PipeWriteClosedException -- ^ We've closed the pipe write end on this process
+ deriving Show
+instance Exception PipeClosedException
--- | We've closed the pipe write end on this process
-data PipeWriteClosedException = PipeWriteClosedException deriving Show
-instance Exception PipeWriteClosedException
--------------------------------------------------------------------------------
-- ** Pipe thread-safe read/write
@@ -160,14 +160,19 @@ instance Exception PipeWriteClosedException
-- and identify whether they are requests or replies.
writePipe :: Pipe -> IddMsg -> IO ()
-writePipe Pipe{..} msg = do
- var <- newEmptyTMVarIO
- atomically $ do
+writePipe p msg = do
+ var <- atomically $ writePipeAsync p msg
+ either throwIO pure =<< atomically (takeTMVar var)
+
+writePipeAsync :: Pipe -> IddMsg -> STM (TMVar (Either SomeException ()))
+writePipeAsync Pipe{..} msg = do
+ var <- newEmptyTMVar
readTVar pipeWriteClosed >>= \case
True -> throwSTM PipeWriteClosedException
False -> pure ()
writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
- either throwIO pure =<< atomically (takeTMVar var)
+ pure var
+
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
-- functions should wait for the matching reply to be delivered asynchronously.
@@ -239,14 +244,18 @@ remoteAsyncReq pipe msg ii = do
-- Write request
writePipe pipe (Request uq ii (runPut msg))
`onException`
- -- the req wasn't done, or no one will be reading the reply:
- -- in either case, drop it from the pending map.
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
+ -- the req wasn't done, or no one will be reading the reply:
+ -- in either case, drop it from the pending map.
+ atomically (modifyTVar pendingReplies (IntMap.delete uq))
-- Block waiting for reply
atomically (takeTMVar wv)
`onException`
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
+ atomically (do
+ modifyTVar pendingReplies (IntMap.delete uq)
+ -- we don't wait for Cancel to be written on the pipe,
+ -- and won't get a reply: nowhere to report it anyway.
+ writePipeAsync pipe (Cancel uq))
-- | Receive a single asynchronous request and send a matching reply
-- asynchronously (the handler may fork a computation that eventually replies
@@ -258,7 +267,7 @@ remoteAsyncReq pipe msg ii = do
-- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
--
-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
-handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
+handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> (IO () -> IO ()) -> a -> IO r) -> IO r
handleAsyncReq pipe getit mii k = mask $ \restore -> do
let am = pipeAsyncMngr pipe
ack <- newEmptyMVar
@@ -274,7 +283,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
Nothing -> tryReadTQueue $ requestsQueue am
Just ii -> tryReadTQueue =<< getMailbox am ii
case taken of
- Just req -> pure (Just req)
+ Just req@(uq,_) -> fetchedRequest am uq >> pure (Just req)
Nothing | isClosed -> pure Nothing -- empty and closed
| otherwise -> retry -- wait for msg
@@ -282,9 +291,9 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
Nothing -> throwIO PipeReadClosedException -- can't expect reqs
Just (uq, bs) -> do
let reply p = do
- writePipe pipe (Reply uq (bimap toSerializableException runPut p))
+ b <- atomically $ doneHandling am uq
+ when b $ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
putMVar ack ()
-
-- Run the continuation which must reply exactly once with 'writeReply'
case runGetOrFail getit bs of
Left (_, _, err) -> do
@@ -292,7 +301,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
reply (Left (toException exc))
throwIO exc
Right (_, _, it) ->
- restore (k (WriteReply reply) it)
+ restore (k (WriteReply reply) (setActive am uq) it)
-- If there's an exception before the reply is sent, reply with an error instead.
`catch` \(e :: SomeException) -> do
@@ -306,6 +315,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
reply (Left e)
throwIO e
+
-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
instance Exception DecodeAsyncReqException
@@ -347,6 +357,28 @@ getMailbox am (InboxId ii) = do
-- ** Async matching "internals" -----------------------------------------------
+-- | Tracks handling status of a received 'Request'. The values are states of a
+-- tiny state machine used to coordinate cancelations.
+--
+-- 'handleAsyncReq' will pop a request from the appropriate queue, setting the
+-- status to `Fetched`. Then one of two things can happen first:
+-- - we receive a 'Cancel', then status is set to `Canceling` so the cancel
+-- action will run when set.
+-- - handler sets a cancel action, then reception of a `Cancel` runs that
+-- action and deletes the RequestStatus.
+--
+-- If RequestStatus is `Canceling` or absent, then a `Reply` won't be sent.
+--
+-- The two possible transition paths:
+-- requestsQueue -> Fetched -> Active cancel -> gone
+-- requestsQueue -> Fetched -> Canceling -> gone
+
+data RequestStatus
+ = Fetched -- ^ 'Fetched' from a request queue, to pass to handler.
+ | Active (IO ()) -- ^ Handler set a cancel action, and has not sent a reply
+ -- yet. The cancel action must not block.
+ | Canceling -- ^ `Cancel` received while status is still `Fetched`.
+
data AsyncManager = AsyncManager
{ pendingReply :: !(TVar (IntMap (TMVar (Either SomeException LB.ByteString))))
-- ^ When a request is sent, keep track that we're expecting a reply. The
@@ -357,6 +389,9 @@ data AsyncManager = AsyncManager
-- /without/ a specific 'InboxId', queue it here. A request handler will
-- pop the next request from it. (Nothing except the 'drainPipe' thread
-- should read from the pipe). Read with @'handleAsyncReq' p get Nothing@.
+ , cancelStatus :: !(TVar (IntMap RequestStatus))
+ -- ^ When a `Cancel uq` is received, we lookup `uq` in this map and call the
+ -- callback if present.
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
-- ^ Requests for a specific 'InboxId' that can be read using
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
@@ -389,15 +424,21 @@ type CorrelationId = Int
-- exclusively.
data IddMsg = Request !CorrelationId !(Maybe InboxId) LB.ByteString
| Reply !CorrelationId (Either SerializableException LB.ByteString)
+ | Cancel !CorrelationId -- ^ Technically a Request, but no Reply
+ -- is expected, the CorrelationId is the
+ -- one of the Request being canceled.
instance Binary IddMsg where
put (Request i ii bs) = putWord8 0 >> put i >> put ii >> put bs
put (Reply i bs) = putWord8 1 >> put i >> put bs
+ put (Cancel i) = putWord8 2 >> put i
+
get = do
b <- getWord8
case b of
0 -> Request <$> get <*> get <*> get
1 -> Reply <$> get <*> get
+ 2 -> Cancel <$> get
_ -> fail $ "impossible: Binary IddMsg - " ++ show b
newAsyncManager :: Pipe -> IO AsyncManager
@@ -405,6 +446,7 @@ newAsyncManager pipe = do
asyncFreshId <- newIORef 0
pendingReply <- newTVarIO IntMap.empty
requestsQueue <- newTQueueIO
+ cancelStatus <- newTVarIO IntMap.empty
mailboxQueues <- newTVarIO IntMap.empty
writeQueue <- newTQueueIO
@@ -430,41 +472,85 @@ drainPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPendings where
loop = do
readPipe p >>= \case
- -- Receive a reply to a pending notification
- Just (Reply uq bs) -> do
- atomically $ do
- m <- readTVar (pendingReply am)
- case IntMap.lookup uq m of
- Nothing ->
- -- the req was dropped from pending bc there was an exception, and no
- -- one's listening any more. the reply is ignored
- pure ()
- Just wv -> do
- writeTVar (pendingReply am) (IntMap.delete uq m)
- -- signal matching request
- putTMVar wv (bimap (toException . RemoteProcessException) id bs)
- loop
- -- Receive a request and queue it
- Just (Request uq mbi bs) -> do
- atomically $ case mbi of
- Nothing ->
- -- "Global" request
- writeTQueue (requestsQueue am) (uq,bs)
- Just ii -> do
- -- "Specific mailbox" request
- q <- getMailbox am ii
- writeTQueue q (uq,bs)
+ Just msg -> do
+ handleMsg msg
loop
-- EOF on read handle
Nothing -> do
-- clean up pipe then exit loop
replyToPendings (toException PipeReadClosedException)
pure ()
+ handleMsg = \case
+ -- Receive a reply to a pending notification
+ Reply uq bs -> do
+ atomically $ do
+ m <- readTVar (pendingReply am)
+ case IntMap.lookup uq m of
+ Nothing ->
+ -- the req was dropped from pending bc there was an exception, and no
+ -- one's listening any more. the reply is ignored
+ pure ()
+ Just wv -> do
+ writeTVar (pendingReply am) (IntMap.delete uq m)
+ -- signal matching request
+ putTMVar wv (bimap (toException . RemoteProcessException) id bs)
+ -- Receive a request and queue it
+ Request uq mbi bs -> do
+ atomically $ case mbi of
+ Nothing ->
+ -- "Global" request
+ writeTQueue (requestsQueue am) (uq,bs)
+ Just ii -> do
+ -- "Specific mailbox" request
+ q <- getMailbox am ii
+ writeTQueue q (uq,bs)
+ Cancel uq -> cancelRequest am uq
replyToPendings e = atomically $ failPendingReplies p e
am = pipeAsyncMngr p
+cancelRequest :: AsyncManager -> CorrelationId -> IO ()
+cancelRequest am uq = join . atomically $ do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure (pure ())
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq Canceling cs) >> pure (pure ())
+ Just (Active cancel) -> writeTVar (cancelStatus am) (IntMap.delete uq cs) >> pure cancel
+ Just Canceling -> pure (pure ())
+
+-- | Returns @True@ when the handler should write its 'Reply'.
+doneHandling :: AsyncManager -> CorrelationId -> STM Bool
+doneHandling am uq = do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure False
+ Just s -> do
+ writeTVar (cancelStatus am) $ IntMap.delete uq cs
+ case s of
+ Canceling -> pure False
+ _ -> pure True
+
+setActive :: AsyncManager -> CorrelationId -> IO () -> IO ()
+setActive am uq cancel = join . atomically $ do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure (pure ()) -- reply already received.
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
+ Just (Active _) -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
+ Just Canceling -> pure cancel
+
+fetchedRequest :: AsyncManager -> CorrelationId -> STM ()
+fetchedRequest am uq = do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> writeTVar (cancelStatus am) (IntMap.insert uq Fetched cs)
+ Just Fetched -> error "impossible"
+ Just (Active _) -> error "impossible"
+ Just Canceling -> error "impossible"
+
+
+
{-
Note [Exceptions during serialization]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -553,7 +639,7 @@ runWriteReply (WriteReply r) = r
(|#>) (IO fs) s = fs s
(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
-(%>>=) = \ m f -> IO (\s0 ->
+(%>>=) = \ m -> \ f -> IO (\s0 ->
case m |#> s0 of -- unIO for linearity checking
(# s1, x #) -> f x |#> s1)
infixl 1 %>>=
=====================================
libraries/ghci/GHCi/Server.hs
=====================================
@@ -66,13 +66,13 @@ servWithCustom
-> IO ()
servWithCustom verbose hook pipe restore customHandler = do
ref <- newIORef False
- withThreadManager $ \mngr ->
+ withThreadManager $ \mngr -> do
loop ref mngr `finally` closePipe pipe
where
loop doneRef mngr = do
when verbose $ trace "reading pipe..."
- handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
+ handleRemoteCall pipe hook $ \wr setCancel (msg :: Message a) ->
discardCtrlC %>>= \_ ->
when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
@@ -82,9 +82,11 @@ servWithCustom verbose hook pipe restore customHandler = do
Shutdown -> writeIORef doneRef True >>
reply () %>>= writeReply wr
RunTH ii st q ty loc -> spawnFinally mngr (runWriteReply wr)
- (wrapRunTH ii (runTH pipe ii st q ty loc))
+ (restore $ wrapRunTH ii (runTH pipe ii st q ty loc))
+ (setCancel . void . forkIO . killThread)
RunModFinalizers ii st qs -> spawnFinally mngr (runWriteReply wr)
- (wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
+ (restore $ wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
+ (setCancel . void . forkIO . killThread)
_other -> run msg >>= reply %>>= writeReply wr
isDone <- readIORef doneRef
=====================================
libraries/ghci/GHCi/Server/Threads.hs
=====================================
@@ -41,8 +41,8 @@ withThreadManager k = do
-- | Linear and resource-safe because 'ThreadManager' is guaranteed to wait or
-- cancel all threads.
-spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> IO ()
-spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
+spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> (ThreadId -> IO b) -> IO b
+spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> \k ->
forkFinally
( mask $ \unmask -> do
tid <- myThreadId
@@ -57,6 +57,7 @@ spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
else throwIO ThreadKilled
)
( \ea -> fin ea )
+ >>= k
unsafe_linear :: (a -> b) %1 -> (a %1 -> b)
unsafe_linear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b))
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/9ee50b9f77bbf878fd2d84a28da5b3…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/9ee50b9f77bbf878fd2d84a28da5b3…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc] Pushed new branch wip/az/epa-tidy-locatedxxx-31
by Alan Zimmerman (@alanz) 27 Oct '26
by Alan Zimmerman (@alanz) 27 Oct '26
27 Oct '26
Alan Zimmerman pushed new branch wip/az/epa-tidy-locatedxxx-31 at Glasgow Haskell Compiler / GHC
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/tree/wip/az/epa-tidy-locatedxxx-31
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/T27737] 48 commits: EPA: Add TTG fields to FamilyInfo
by Simon Peyton Jones (@simonpj) 27 Oct '26
by Simon Peyton Jones (@simonpj) 27 Oct '26
27 Oct '26
Simon Peyton Jones pushed to branch wip/T27737 at Glasgow Haskell Compiler / GHC
Commits:
7dedcf13 by Alan Zimmerman at 2026-10-16T19:03:17-04:00
EPA: Add TTG fields to FamilyInfo
This commit is a simple add, with unused extension fields.
It lays the groundwork for using them for exact print annotations
- - - - -
26b9a8a0 by Arkissa at 2026-10-17T16:04:10+00:00
ghci: Prefer exact source spans in :type-at
Prefer expression types for identical source spans.
Fixes #27412.
- - - - -
70dc4e61 by sheaf at 2026-10-18T18:15:15-04:00
Carefully implement coercion zapping
This commit implements coercion zapping in the rewriter, controlled by
the -fzap-coercions flag (on by default). This significantly reduces
the size of coercions produced when rewriting type family applications,
resulting in much faster compilation. See Note [Coercion zapping]
in GHC.Tc.Solver.Rewrite for an explanation and further details.
The rewriter now keeps track of the `CoVar`s and `CoercionHole`s it uses
when rewriting, which allows computing the dependencies of a zapped
coercion without traversing the coercion the rewriter produced, as
explained in Note [Computing coercion dependencies] in GHC.Tc.Solver.Rewrite.
Accompanying changes:
- Rework how dependencies of 'UnivCo's are tracked, purely for
performance reasons.
We now use the 'UnivCoDeps' datatype instead of '[Coercion]', and
we now insist that 'uco_deps' also contains the CoVars that appear
in the LHS and RHS types of the UnivCo (as a caching mechanism).
- Several performance improvements to the rewriter (e.g. increased
strictness).
- Improvements to the family application cache, including:
- Better documentation.
- Only cache a whole chain of reduction steps.
- Don't put coercion holes in the cache, fixing #27518.
- Allow Core Lint to be called from within the typechecker (and from
within the coercion optimiser).
- Move out "annotation linting" from Core Lint into the general Core
pipeline, to avoid the dependency GHC.Core.Coercion.Opt->GHC.Core.Lint
causing a regression in CountDepsParser due to transitive
dependencies of GHC.Core.Opt.Monad and GHC.Unit.Module.ModGuts.
Fixes #8095 #27336 #27518
Summary of performance numbers
Compile-time byte allocated in GHC's testsuite (0 regressions)
CoOpt_Singletons(normal) ghc/alloc 634,040,332 595,448,728 -6.1% GOOD
FamAppCachePerf(normal) ghc/alloc 495,629,812 191,612,920 -61.3% GOOD
LargeRecord(normal) ghc/alloc 1,226,806,434 966,260,976 -21.2% GOOD
SimplCastPerf(normal) ghc/alloc 160,505,678 148,112,688 -7.7% GOOD
T12227(normal) ghc/alloc 244,668,982 233,037,216 -4.8% GOOD
T12545(normal) ghc/alloc 697,804,842 424,705,152 -39.1% GOOD
T13386(normal) ghc/alloc 422,871,076 35,213,656 -91.7% GOOD
T15703(normal) ghc/alloc 288,402,364 273,692,560 -5.1% GOOD
T26426(normal) ghc/alloc 86,002,052 78,152,104 -9.1% GOOD
T27336(normal) ghc/alloc 498,932,268 35,886,016 -92.8% GOOD
T5030(normal) ghc/alloc 117,057,706 88,785,648 -24.2% GOOD
T5321Fun(normal) ghc/alloc 252,552,628 243,820,176 -3.5% GOOD
T8095(normal) ghc/alloc 768,198,564 127,779,520 -83.4% GOOD
T9872a(normal) ghc/alloc 1,655,675,020 1,406,017,008 -15.1% GOOD
T9872b(normal) ghc/alloc 1,864,180,628 1,644,706,696 -11.8% GOOD
T9872b_defer(normal) ghc/alloc 2,834,585,468 1,657,729,760 -41.5% GOOD
T9872c(normal) ghc/alloc 1,572,573,116 1,354,055,424 -13.9% GOOD
T9872d(normal) ghc/alloc 351,516,004 331,071,424 -5.8% GOOD
TcPlugin_RewritePerf(normal) ghc/alloc 2,063,065,680 1,847,464,272 -10.5% GOOD
Chesskell (github.com/Proxymoron461/chesskell/issues)
Typechecking that the first N plies of kholmovVsBronsteinShort
are a valid chess game:
Plies | GHC 9.14 time/alloc | This commit time/alloc |
-----:|---------------------|------------------------|
2 | 41s/20GB | 0.2s/19MB |
4 | 95s/49GB | 0.3s/25MB |
6 | timed out | 0.6s/522MB |
8 | timed out | 7s/673MB |
10 | timed out | timed out |
Compilation of singletons-base: compile time -28%, allocations -25%
-------------------------
Metric Decrease:
CoOpt_Singletons
FamAppCachePerf
LargeRecord
SimplCastPerf
T12227
T12545
T13386
T15703
T24984
T26426
T27336
T5030
T5321Fun
T8095
T9872a
T9872b
T9872b_defer
T9872c
T9872d
TcPlugin_RewritePerf
-------------------------
Co-Authored-By: Sylvain Henry <sylvain(a)haskus.fr>
- - - - -
3d326d7d by Sylvain Henry at 2026-10-18T18:16:09-04:00
JS: fix stack corruption in h$ap_gen_fast's pushAllRegs (#27624)
The loop added in 9e46990976 ("JS: support more than 128 registers
(#26558)") pushed register N to stack[sp - N - 2] while the switch it
falls through into, and every reader of the h$ap_gen frame, uses
stack[sp - (N - 2)]. Registers >= R32 were therefore written 4 slots
too low: the slots the readers consult were left stale and the frame
below was silently corrupted.
Fix the loop to use the same slot formula as the switch, and add a
regression test: an unknown call to a thunk with 35 argument register
slots, which goes through the generic apply.
Co-Authored-By: Claude Fable 5 <noreply(a)anthropic.com>
- - - - -
6bbca569 by Andreas Klebinger at 2026-10-21T10:01:10+00:00
Fix a few tag inference bugs.
* Treat bottoming functions and nullary join points as functions.
* When generating bytecode don't rely on imported tag sigs.
Fixes #27831, #27841 and #27842.
- - - - -
71808e29 by Sylvain Henry at 2026-10-22T04:50:24-04:00
Add GHC.Internal.Unboxed.Do: do-notation for unboxed values (#27944)
QualifiedDo operators can be representation-polymorphic: a do-block is
expanded into ordinary applications before typechecking, and the only
check on a bound variable is the deferred fixed-runtime-rep check. A
representation-polymorphic (>>=) cannot be defined directly, though,
because its body would bind a variable of unknown representation.
The new module provides such operators for State#-passing code as
methods of a class indexed by RuntimeRep, with one instance per
representation. With optimisation the dictionaries disappear and a
block compiles to the usual nested case expressions:
Ubx.do { x <- readWordArray# mba 0# -- x :: Word#
; writeWordArray# mba 1# x -- unit actions are statements
; Ubx.pure x }
Also add users guide documentation and tests.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
33247780 by Sylvain Henry at 2026-10-22T04:50:24-04:00
Bignum: use Ubx.do for State#-passing code (#27944)
Rewrite the State#-passing code of the GHC.Internal.Bignum modules
(Primitives, WordArray, BigNat, Natural, Integer, Backend.Native,
Backend.GMP) with the qualified do-notation of GHC.Internal.Unboxed.Do.
Nested case chains such as
case newWordArray# sz s of { (# s, mwa #) ->
case act mwa s of { s ->
unsafeFreezeByteArray# mwa s }}
become
Ubx.do
mwa <- newWordArray# sz
act mwa
unsafeFreezeByteArray# mwa
The optimised Core is unchanged (compared per top-level binding).
BigNat now enables MonoLocalBinds: local loops written with Ubx.do
must stay monomorphic in the state type, otherwise GHC does not turn
them into join points.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
593f5f5b by Sylvain Henry at 2026-10-22T04:51:16-04:00
JS: remove archive padding after wasm objects (#25665)
On Darwin hosts, emar (llvm-ar) writes archives in the Darwin format when
the first member is not a known object file, which is the case for the
archives of the JS backend. This format pads each member to 8 bytes with
'\n' and includes the padding in the member size. Wasm objects do not
accept trailing bytes: wasm-ld failed with "section too large".
The JS linker now removes this padding when it extracts a wasm object from
an archive. In addition, Hadrian passes --format=gnu to emar for the JS
target. See Note [Wasm objects in Darwin archives].
Also document the format of Cc (wasm) objects in Note [Cc object format].
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
13dff5d0 by L0neGamer at 2026-10-22T21:37:31-04:00
add options for resizing bytearrays (#27244)
add five mutable bytearray resizing operations
for various kinds of pinnedness and aligning.
- prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default)
- prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``)
- prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``)
- prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``)
- ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``)
- - - - -
0c17201c by Sylvain Henry at 2026-10-22T21:38:23-04:00
JS: implement performMinorGC and performBlockingMajorGC (#28012)
GHC.Internal.System.Mem imports three C entry points: performMajorGC,
performGC (for performMinorGC) and performBlockingMajorGC. The JS RTS
only defined h$performMajorGC, so calling performMinorGC or
performBlockingMajorGC threw "ReferenceError: h$performGC is not
defined" at runtime.
The JS RTS has a single non-generational collector, so both missing
entry points are now aliases of h$performMajorGC.
Add a test that calls every GC entry point of System.Mem.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
7a631ff2 by L0neGamer at 2026-10-23T09:11:39-04:00
Mon(a|oi)d of No (Return|Mappend) progress (!15795)
Make non-canonical definitions of return and mappend
non-suppressible errors, and make canonical definitions
of those methods warnings.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/328
- - - - -
14656fd4 by Simon Jakobi at 2026-10-23T09:12:27-04:00
Cmm: keep the CBE substitution idempotent
When blocks are merged over multiple rounds during common block
elimination, a block that won a merge in an earlier round can still
lose a merge in a later round. The substitution map (Subst) used to
record this as a chain: the earlier loser mapped to a block that was
itself eliminated. In PLT terminology, the substitution became
*non-idempotent*. Applying such a substitution with a single lookup
left edges to unreachable blocks in the Cmm graph, eventually leading
to a setInfoTableStackMap panic (#27368).
With this patch, each round's fresh substitutions are applied to the old
ones as they are added (extendSubst), so every eliminated block maps
directly to its final replacement and the substitution stays idempotent.
Fixes #27368.
Assisted-by: Claude Fable 5.1
- - - - -
92848e61 by Evgeny Malyshev at 2026-10-23T20:26:04-04:00
hadrian: Include transitive dependencies in Haddock builds
Haddock needs indirect dependency interfaces to resolve references to
definitions outside a package's direct dependencies. Expose Cabal's
configured dependency closure through ContextData and use it alongside
the direct list, preserving direct-interface precedence.
Bump Shake's cache version for the serialized oracle change. Add a
Hadrian selftest requiring template-haskell's indirect ghc-prim interface.
Fixes #25850.
Fixes #27786.
Assisted-By: OpenAI Codex
Metric Increase:
haddock.Cabal
- - - - -
5e8ae81b by Zubin Duggal at 2026-10-23T20:26:47-04:00
WinIO: Don't confuse Win32 errors and NTSTATUS codes
WinIO misinterpreted NTSTATUS codes as Win32 errors, and had some ad hoc logic to deal with this.
However, this logic was incomplete and resulted in the mishandling of results of certain I/O operations
(see testcase).
Now the `ErrCode` argument in `CompletionCallback` is always the Win32 error code,
and we convert the NTSTATUS using RtlNtStatusToDosError.
Also when an overlapped/async operation such as ReadFile fails immediately,
with a Win32 error such as ERROR_BROKEN_PIPE, we don't get a further completion
notification, so we never recorded the failure. This worked by coincidence
earlier, because `STATUS_PENDING` happened to have the same numerical code as
`ERROR_NO_MORE_ITEMS`, but it doesn't now that we translate `STATUS_PENDING`
into the correct Win32 error code (`ERROR_IO_PENDING`). Fix this by using
the Win32 error value we obtained when an operation returns `STATUS_PENDING`.
Fixes #27971
- - - - -
c42ed66a by Martijn Bastiaan at 2026-10-24T16:00:42+02:00
Add single-threaded test for #27526 + #27123
Issue #27526 is a duplicate of #27123, which has already been fixed in
HEAD. The proposed patch in the former carries a "nicer" regression test
in that it doesn't rely on multi-threading to trigger.
Closes #27526
- - - - -
7f73f515 by Simon Peyton Jones at 2026-10-24T17:29:19-04:00
Refactor QuickLook a bit
This MR simply refactors QuickLook a bit. In particular, it
simplifies `tcApp`, and moves shared code into `finishApp`.
It also adds `Note [QuickLook: arguments before result]`
responding to #26599.
There should be no user-visible changes, except perhaps in error messages,
which improve marginally.
For some reason T24984 compiles 8% faster. I'll take the win!
Metric Decrease:
T24984
- - - - -
a4a3a964 by Alan Zimmerman at 2026-10-24T17:30:03-04:00
EPA: Use AnnList for XClosedTypeFamily
We now have TTG annotations for FamilyInfo.
Make use of it to store the AnnList annotations for a closed type
family.
- - - - -
953e4c7f by sheaf at 2026-10-26T18:19:10-04:00
Rework LogQueue to be a proper queue
Now 'LogQueue' uses a 'TQueue' with a separate 'TVar' to indicate
whether the log queue is finished logging, instead of the previous weird
data representation using a reverse-order '[Maybe Msg]' with 'Nothing'
denoting the end of the queue.
- - - - -
ec29c5fa by Rodrigo Mesquita at 2026-10-26T18:19:10-04:00
Refactor GHC.Driver.MakeAction
Pull out of 'runParPipelines' the logic for creating a 'MakeEnv' ready
to be used by multiple threads, as that will be useful for downsweep as
well (which doesn't fit the 'runPipelines' flow), rather than being just
for upsweep.
The code is moved and re-structured to match the export list, simplify
the sequentiality checks previously both in 'runPipelines' and
'runAllPipelines', which were weirdly similar and confusing; into the
two part step where we need these checks: (1) to construct the MakeEnv,
(2) to run the MakeActions in parallel. These two steps are separate and
used to be too mixed up.
Some additional little simplifications or clean ups here and there.
- - - - -
3fb22073 by Rodrigo Mesquita at 2026-10-26T18:19:11-04:00
Refactor GHC.Driver.Downsweep in preparation for parallel downsweep
Pure refactor to improve the code to facilitate implementing parallel
downsweep in the next commit.
This commit puts a MakeEnv into the DownsweepEnv, gives the fields
proper names and uses RecordWildcards to simplify, rather than passing
around all diagnostic wrappers, driver-message-things and using 10s of
positional fields.
No behavior changes here!
- - - - -
52f385a7 by sheaf at 2026-10-26T18:19:11-04:00
Driver: enableCodeGenForTH in load', not downsweep
enableCodeGenForTH modifies the module graph for planning, creating
fresh temporary paths. So it should only be run when we are about to
execute a build plan (e.g. in load'), so that consumers that want to
know facts about the module graph don't go looking for files that don't
exist yet (in particular 'haddock --no-compilation').
- - - - -
82402d14 by sheaf at 2026-10-26T18:19:11-04:00
2-phase Cache/Search Finder monad
This commit restructures the finder abstraction by introducing the
'FinderM' monad, which splits module lookup operations into two phases:
- a cache-only phase, performing no filesystem access,
- from the first cache miss onwards, a search action which may access
the filesystem.
This allows consumers to distinguish between quick cached results versus
more expensive filesystem search operations.
- - - - -
5c302b40 by sheaf at 2026-10-26T18:19:11-04:00
Separate Home/External finder caches
The caches for home modules and modules from external units have
different lifetimes, needing to be invalidated at different points.
This commit moves the cache for modules from external units into the
'UnitState' type, which keeps the cache correct whenever package flags
or package databases change (setSessionDynFlags, :set -package ...,
Backpack's addUnit).
This also makes it easier to clear the cache of home modules, as they
are now stored separately (obviating the 'isUnitEnvInstalledModule'
check we used to perform).
- - - - -
605fbb96 by sheaf at 2026-10-26T18:19:11-04:00
Add known home modules to the finder cache
The finder looks in the file system for the on-disk source that
corresponds to a Haskell module name. There are two kinds of modules
where we need to bypass this search:
- a file passed directly in the command line, which may not be in any
search path and whose module name may differ from the file name;
- Backpack signatures and modules, which do not exist as files at all.
This commit handles these two situations by adding an immutable
'KnownHomeModules' in the finder cache, which is always consulted first
before doing any search.
This removes the ad-hoc imperative logic which relied on mutating the
finder cache at careful points.
'setModuleGraph' now also takes the known home modules, so that the
module graph and the known home modules are always set consistently.
See Note [Known home modules] in GHC.Unit.Finder.Types.
- - - - -
82718f44 by sheaf at 2026-10-26T18:19:11-04:00
Make 'UnresolvedImport' into a suitable cache key
The goal of this commit is to make 'UnresolvedImport' suitable for use
as a cache key. Specifically, during downsweep we want to be able to
cache module resolution queries. We want to do this using some kind of
map, e.g. (roughly speaking):
Map UnresolvedImport ResolvedImport
For this to work, 'UnresolvedImport' must contain **precisely** the
input data to each resolution computation. In other words:
1. If something changes that materially affects resolution, it must
be stored in the cache key.
2. If changing some input does not materially affect the outcome,
then it must not be stored in the cache key.
To satisfy (1), we change 'UnitNode' to also keep track of the home
unit in whose unit state the unit's dependencies were looked up.
For (2), 'ImportResolution' now stores a 'ModuleLookupScope' instead of an
'UnresolvedImportOrigin'. Plugin imports are expressed with a new
'LookupPlugin' scope. The source location of an import, which is
immaterial to the resolution, is stored out of band outside
'UnresolvedImport', in 'ModSummary'.
- - - - -
d4635c7c by sheaf at 2026-10-26T18:19:11-04:00
Add GHC.Data.Dependent
This commit adds a minimal implementation of dependent sums
('Some', 'DSum') and dependent maps ('DMap').
The implementation is not entirely performance optimal, as there are a
few extraneous allocations compared to the full-blown implementation in
the 'dependent-map' package. For our use case (caching downsweep
computations), this does not matter much.
See GHC.Data.Dependent.
- - - - -
6759533e by sheaf at 2026-10-26T18:19:11-04:00
Driver: structured concurrent worker abstraction
This commit introduces a structured concurrency framework in the style
of the 'ki' library: a collection of threads within a scope.
We implement two kinds of concurrent workers on top of this framework:
- Independent workers cannot wait for one another at all. The only
scheduling operation is to wait for quiescence.
- Coordinating workers declare an STM readiness condition (waiting on
other workers to complete) which gates their start.
See Note [Deterministic concurrent workers] in GHC.Driver.Concurrency.
This commit ports upsweep to this new framework, with downsweep being
left as subsequent work.
Further changes along the way:
- Refactoring of how concurrency is acquired to avoid the footgun of
trying to use a no-op 'AbstractSem' as a lock in the serial case.
- The "re-run with -j1" logic for semaphore opening errors no longer
triggers on late semaphore failures (part-way through a lengthy
computation).
- Logger threads are properly cleaned up on exception, with each
concurrent worker's log queue and local TmpFs properly bracketed.
- The 'GhcMessage -> AnyGhcDiagnostic' and 'Maybe Messager'
arguments of 'depanalE', 'depanalPartial' and 'downsweep', which
were all dead in practice, have been dropped.
- Concurrent logging has been completely overhauled. Each worker has
its own log queue. Workers whose logs are printed as we go are
spawned in the order in which their logs must be printed, now using
a TQueue instead of the old complicated LogQueueQueue mechanism.
'logThread' is replaced by 'withLogPrinter', which runs the thread
that prints the log queues within a concurrent worker scope.
These changes allow us to fix issues with the exception handling:
we now always flush remaining log queues before exiting (even when
the action throws an exception), and exceptions that arise while
printing are properly rethrown. This fixes #28009.
- - - - -
38a99006 by sheaf at 2026-10-26T18:19:11-04:00
Rule-based deterministic concurrent downsweep
This commit rewrites downsweep as a single query-answering rule
(see 'DownsweepRule') that can be executed by concurrent worker threads.
The design allows every expensive operation (preprocessing files with CPP,
parsing headers, reading interfaces) to be performed concurrently
according to the -j<N>/-jsem flags.
See Note [Rules-based downsweep] in GHC.Driver.Downsweep.
The rules are run by "GHC.Driver.Concurrency.runRules" in a demand-driven
way: each worker can demand other workers to run, but never wait on
another worker. Every piece of work is done at most once.
Fixes #27514
Memory usage can increase due to doing more work concurrently:
-------------------------
Metric Increase:
MultiComponentModulesRecomp
MultiComponentModulesRecomp100
mhu-perf
-------------------------
- - - - -
9fa7b7be by sheaf at 2026-10-26T18:19:11-04:00
Acquire/release semaphore tokens more readily
This commit reworks GHC.Driver.MakeSem to use the scoped worker
abstraction of GHC.Utils.Concurrent.Scope. This allows us to get rid of
a lot of the tricky logic in GHC.Driver.MakeSem involving manual
exception handling, thread spawning and lifetime management, etc.
The main user-visible change is that token acquisition is no longer
rate limited (due to a bug, there previously was a 1s rate limit on
acquisition of semaphore tokens), with the release debounce period
shortened from 1s to 10ms.
Other changes:
- With -jsem, the number of capabilities is now capped at the number
of processors, bringing it in line with the -jN behaviour.
- Pending jobs whose wait for a token is interrupted by an exception
are now withdrawn from the jobserver, instead of acquiring a token
that it hung onto without doing useful work.
Fixes #27763
- - - - -
c83c6733 by Alan Zimmerman at 2026-10-26T18:19:55-04:00
EPA: Update AnnClsInstDecl to contain AnnList
It was already separately tracking the '{', '}' and leading ';' values.
This aligns it with layout-introducing parser productions ('vocurly'),
so the layout processing can be handled uniformly
- - - - -
7eba5b31 by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Don't push `case` inside `let` in Gentle simplification
Triggered by #27737 this MR arranges for the Simplifier NOT to do
K[ let binds in e ] --> let binds in K[ e ]
in when `sm_case_case` is on; that is, in the first "gentle"
simplication.
Needs better documentation, but see discussion on #27737.
- - - - -
54bc6046 by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Wibble
- - - - -
78d73842 by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Wibbles and documentation
- - - - -
b1a476c3 by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Try another way
let's see what CI says
- - - - -
056f8bda by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Fix join points
Split contininuations *first*
- - - - -
7c44678f by Simon Peyton Jones at 2026-10-27T11:44:39+00:00
Deal with runRW# too
ToDo: document this
- - - - -
89736343 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Wibbles
- - - - -
c0ae5eb6 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Eliminate accidental pprTrace
- - - - -
ca9f96fd by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Fix perf hole in exprIsConApp_maybe
in subst_bndr. ToDo: document
- - - - -
aa0d48ac by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Fix in-scope set violation
- - - - -
4cc6063e by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Attempt to deal with the perf issue in T18223 [skip ci]
see `rebuildWithFloats`
- - - - -
950fc6aa by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Work in progress on modifying the Simplifier's FloatBinds
...in collaboration with Claude
- - - - -
b52abbbc by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
More work in progress
...with Claude
Very nice refactor.
The hack does about what we hoped. Changes are relative to the original branch (casts always in inner), and only tests that moved by more than 0.5% are listed:
┌──────────────────┬───────────────────────┬───────────────────────────────────────────────────────────────────┐
│ Test │ Casts always in outer │ Hack (cast only into a single-alternative case or a non-join let) │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ CoOpt_Read │ +6.9% │ +5.6% (still a regression) │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ CoOpt_Singletons │ +6.1% │ 0.0% (fully recovered) │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T13253 │ +3.7% │ +0.1% (recovered) │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T13056 │ +1.1% │ +1.1% │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T5642 │ −1.9% │ −1.9% (fix kept) │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T16577 │ −1.5% │ −1.5% │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T21839c │ −1.0% │ −1.0% │
├──────────────────┼───────────────────────┼───────────────────────────────────────────────────────────────────┤
│ T19695 │ +0.8% │ −0.1% │
└──────────────────┴───────────────────────┴───────────────────────────────────────────────────────────────────┘
The other 56 tests are within 0.5%.
Relative to master, CoOpt_Read is still about 3% better (it was 9% better with casts always pushed inside). CoOpt_Read is the only real loss left. It presumably gains from pushing a cast into a multi-alternative case, or into a join point, which the hack now forbids.
The Core Lint bug. My reading is that it's caused by the hack, not latent: a cast pushed into a join point's RHS but not into a multi-alternative case in its body leaves a jump under a cast. The mightBeJoinBind test (no casts into a join-point let) fixes it, and the current tree builds cleanly with it. /tmp/tjoin/JoinCast.hs is a candidate reproducer of that shape: MkN (let j z = ... in case x of {0 -> j 1; 1 -> j 2; _ -> 3}). It passes Lint on master and on the current branch. I haven't yet seen it fail, because that needs the buggy state rebuilt, which means editing Iteration.hs. I'll do that only after your push, if you want it.
State you're committing:
- FloatBinds work, with addFloatBinds.
- Your getFF and Note [LetFloats] changes, plus my comment fixes and the let-can-float and soft-scope assertions.
- Soft-scoped ticks floated as FloatTicks.
- The push_casts flag, with mightBeJoinBind.
- Accepted outputs for T20510 and show-bytecode-hpc.
- Tests:
- The full testsuite was run before the push_casts change; the only unexpected failures were environmental ones and those two outputs, now accepted.
- The push_casts version has only been built, and measured with the perf harness. simplCore and the full testsuite haven't been rerun on it.
- - - - -
344d4a00 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Undo the push_casts experiment in splitContArgs
Go back to always putting casts in the `inner` continuation when
case-of-case is off. The push_casts flag (and mightBeJoinBind) fixed
the coercion duplication in T5642, but cost CoOpt_Read, and needed a
delicate side condition for join points.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
ff04f091 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Add a small regression test
- - - - -
e3f2c987 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Simplifier: tidy up case-floats, casts and float handling (#27737)
Casts
* splitContArgs: push a cast into `inner` only when it is followed by an
application (isAppCont). A cast suffix stays outside, so it is not
duplicated into every alternative of a multi-way case, and cast
worker/wrapper can still fire. Fixes the quadratic coercion blow-up
in T5642, T16577 (derived Generic `from`).
Note [Do not push a cast inwards unnecessarily]; invariant on CastIt.
Case-floats and single-alternative cases
* Restructure reallyRebuildCase: do prepareCase first, then split into
rebuildSingleAltCase (no mkDupableCont; returns a case-float) and
rebuildMultiAltCase. simplAlt now returns (AltCon, [OutVar], floats, rhs).
* rebuildSingleAltCase no longer calls mkCase (removing the buggy (FSC1)
fast path). Instead new simplWrapFloats wraps floats using mkCase for
each case-float, so case-merging, identity-case elimination etc still
happen. It replaces Env.wrapFloats at all Simplifier call sites.
Note [Wrapping case-floats with mkCase].
* Invariant (FFC1): a DEFAULT case-float never has a scrutinee for which
doCaseToLet holds (it should be a let-float). doCaseToLet moves to
Simplify.Env; asserted by okCaseFloat. rebuildSingleAltCase makes a
let-float when prepareAlts has pruned a case to a single DEFAULT.
Note [Case-floats], Note [Tick-floats].
* contIsDupable: treat a Select with "unpacking alternatives"
(case x of K a b -> b, possibly nested) as dupable, so CPR
unboxing cancels with the boxing instead of making a join point.
Fixes the extra Simplifier iteration in T15304.
Note [Duplicating unpacking continuations].
Float representation
* Flatten LetFloats into SimplFloats: sfLetFloats :: FloatBinds; no
per-group FloatFlag field. New `type LetFloats = OrdList OutBind`
(let-bindings only, satisfying let-can-float). New addLetFloat,
addCaseFloat, addLetFloats, mkLetSimplFloats; getFF_Case/getFF_Tick gone.
Note [LetFloats].
* prepareBinding floats only lazy or ok-for-spec let-bindings
(onlyLazyLetFloats, one pass); returns (LetFloats, InScopeSet, rhs).
No more strict_bind argument. abstractFloats takes LetFloats, so a
case-float can never reach it (was a panic: T27737a).
Note [prepareBinding], including the lost addIntC# opportunity (PB4).
* prepareRhs/makeTrivial return LetFloats; makeTrivial declines to bind
an unlifted, not-ok-for-spec expression (bindingOk).
Note [Cannot trivialise] (CT1).
Performance details
* mkSingleAltCase: skip occCheckExpand when no binder is a TyCoVar.
* Force a Case's type only when emitting it: mkCase3 renamed returnCase,
which does the seqType; callers don't. Update
Note [Avoiding space leaks in OutType].
* Record the (rejected) idea of floating a prefix of the floats:
Note [Floating a prefix of the floats].
Notes
* Note [LetFloats]: why we have FloatCase (LF1, LF2).
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
92315e75 by Simon Peyton Jones at 2026-10-27T11:44:40+00:00
Wibble
- - - - -
cb78c1aa by Simon Peyton Jones at 2026-10-27T17:47:18+00:00
Simplifier: simplify float handling; make the FloatCase invariant global
Float representation and prepareBinding
* SimplFloats fields renamed to sfBinds / sfJoins.
* doFloatFromRhs now takes the RHS floats and the new ANF let-bindings,
decides whether to float them, and returns the combined SimplFloats
(Maybe SimplFloats). It floats only if every float is a floatable
let-binding; see the new Note [doFloatFromRhs], (FFL1)-(FFL6):
- strict lifted bindings float only if their RHS is ok-for-spec
- literal strings float anywhere
- unlifted bindings float only when nested and non-recursive
- case-floats and tick-floats never float out of a RHS
(FFL6) records the lost opportunity of floating an ok-for-spec
case-float such as case addIntC# a b of (# r, c #) -> ...
* prepareBinding returns SimplFloats again (only let-bindings, by the
postcondition of doFloatFromRhs). The in-scope set is extended only
with the binders of the ANF floats. onlyLazyLetFloats,
mkLetSimplFloats and the FloatFlag machinery are gone.
* abstractFloats takes and returns SimplFloats, handles the
nothing-to-abstract case itself, starts its substitution from the
in-scope set of `env`, and adds the new polymorphic binders to it.
* Note [Using FloatBinds in SimplFloats] replaces Note [LetFloats].
* New GHC.Data.OrdList.allOL.
The FloatCase invariant
* A DEFAULT FloatCase never has a scrutinee for which doCaseToLet holds;
we use a let-float instead. This is now a global invariant of
GHC.Core.FloatCase, stated on the constructor, with
Note [The FloatCase invariant]. It is nice to have, not essential
for correctness: a let can be inlined, and a single FloatCase stops
a whole group of floats from leaving a RHS.
* doCaseToLet moves to GHC.Core.Utils, with new mkFloatCase (makes a
let-float when it can) and okFloatCase (checks the invariant, for
assertions).
* The Simplifier's addCaseFloat and exprIsConApp_maybe's case
look-through use mkFloatCase; rebuildSingleAltCase loses its special
DEFAULT let-float code.
* mkFieldSeqFloats (exprIsConApp_maybe) no longer evaluates a strict
field whose argument is unlifted and ok-for-speculation. Such fields
arise from unpacking a sum, e.g. MoveResult in Data.IntMap.Internal,
where the old test (exprIsHNF) made a pointless case-float that broke
the invariant and tripped the assertion.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
351 changed files:
- + changelog.d/25665
- + changelog.d/25850
- + changelog.d/27368
- + changelog.d/27624
- + changelog.d/27971
- + changelog.d/28012
- + changelog.d/T27412
- + changelog.d/T27763
- + changelog.d/T27831-bottoming-strict-field
- + changelog.d/T27842-bytecode-imported-tags
- + changelog.d/monad-of-no-return-canonical
- + changelog.d/parallel-downsweep
- + changelog.d/resizeMutableByteArray-pinnedness
- + changelog.d/unboxed-do
- + changelog.d/zap-coercions
- compiler/GHC.hs
- compiler/GHC/Builtin.hs
- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/ByteCode/Asm.hs
- compiler/GHC/Cmm/CommonBlockElim.hs
- compiler/GHC/CmmToAsm/CFG/Dominators.hs
- compiler/GHC/CmmToLlvm/CodeGen.hs
- compiler/GHC/Core.hs
- compiler/GHC/Core/Coercion.hs
- compiler/GHC/Core/Coercion.hs-boot
- compiler/GHC/Core/Coercion/Opt.hs
- compiler/GHC/Core/FamInstEnv.hs
- compiler/GHC/Core/Lint.hs
- + compiler/GHC/Core/Lint.hs-boot
- compiler/GHC/Core/Opt/Pipeline.hs
- compiler/GHC/Core/Opt/Simplify/Env.hs
- compiler/GHC/Core/Opt/Simplify/Iteration.hs
- compiler/GHC/Core/Opt/Simplify/Utils.hs
- compiler/GHC/Core/Reduction.hs
- compiler/GHC/Core/SimpleOpt.hs
- compiler/GHC/Core/TyCo/FVs.hs
- compiler/GHC/Core/TyCo/FVs.hs-boot
- compiler/GHC/Core/TyCo/Make.hs
- compiler/GHC/Core/TyCo/Rep.hs
- compiler/GHC/Core/TyCo/Rep.hs-boot
- compiler/GHC/Core/TyCo/Subst.hs
- compiler/GHC/Core/TyCo/Tidy.hs
- compiler/GHC/Core/Utils.hs
- compiler/GHC/CoreToIface.hs
- + compiler/GHC/Data/Dependent.hs
- compiler/GHC/Data/FastString.hs
- compiler/GHC/Data/OrdList.hs
- compiler/GHC/Data/Pair.hs
- compiler/GHC/Data/Word64Map/Internal.hs
- compiler/GHC/Data/Word64Set/Internal.hs
- compiler/GHC/Driver/Backpack.hs
- compiler/GHC/Driver/CmdLine.hs
- + compiler/GHC/Driver/Concurrency.hs
- compiler/GHC/Driver/Config.hs
- + compiler/GHC/Driver/Config/Concurrency.hs
- compiler/GHC/Driver/Config/Core/Lint.hs
- compiler/GHC/Driver/Config/Core/Opt/Simplify.hs
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/DynFlags.hs
- compiler/GHC/Driver/Env.hs
- compiler/GHC/Driver/Errors/Ppr.hs
- compiler/GHC/Driver/Errors/Types.hs
- compiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Main/Passes.hs
- compiler/GHC/Driver/Make.hs
- compiler/GHC/Driver/MakeAction.hs
- compiler/GHC/Driver/MakeFile.hs
- compiler/GHC/Driver/MakeSem.hs
- compiler/GHC/Driver/Pipeline/Execute.hs
- compiler/GHC/Driver/Pipeline/LogQueue.hs
- compiler/GHC/Driver/Session.hs
- compiler/GHC/Hs/Decls.hs
- compiler/GHC/Hs/Dump.hs
- compiler/GHC/Hs/Utils.hs
- compiler/GHC/HsToCore/Pmc/Types.hs
- compiler/GHC/HsToCore/Quote.hs
- compiler/GHC/Iface/Ext/Ast.hs
- compiler/GHC/Iface/Load.hs
- compiler/GHC/Iface/Recomp.hs
- compiler/GHC/Iface/Rename.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/JS/Opt/Simple.hs
- compiler/GHC/Linker/Deps.hs
- compiler/GHC/Parser.y
- compiler/GHC/Parser/Header.hs
- compiler/GHC/Parser/PostProcess.hs
- compiler/GHC/Rename/Env.hs
- compiler/GHC/Rename/Module.hs
- compiler/GHC/Runtime/Eval/Types.hs
- compiler/GHC/Runtime/Interpreter/JS.hs
- compiler/GHC/Runtime/Loader.hs
- compiler/GHC/Stg/EnforceEpt.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/EnforceEpt/Types.hs
- compiler/GHC/StgToCmm/Prim.hs
- compiler/GHC/StgToJS/Apply.hs
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- compiler/GHC/StgToJS/Prim.hs
- compiler/GHC/Tc/Deriv/Generate.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Gen/Splice.hs
- compiler/GHC/Tc/Instance/Family.hs
- compiler/GHC/Tc/Module.hs
- compiler/GHC/Tc/Plugin.hs
- compiler/GHC/Tc/Solver/Default.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Solver/InertSet.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Solver/Rewrite.hs
- compiler/GHC/Tc/Solver/Solve.hs
- compiler/GHC/Tc/TyCl.hs
- compiler/GHC/Tc/Types.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/Backpack.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/ThToHs.hs
- compiler/GHC/Types/Basic.hs
- compiler/GHC/Types/Error/Codes.hs
- compiler/GHC/Types/Hint.hs
- compiler/GHC/Types/Hint/Ppr.hs
- compiler/GHC/Types/Unique/DSet.hs
- compiler/GHC/Types/Unique/FM.hs
- compiler/GHC/Types/Unique/Map.hs
- compiler/GHC/Types/UnresolvedImport.hs
- compiler/GHC/Types/Var/Set.hs
- compiler/GHC/Unit/External/ModuleOrigin.hs
- compiler/GHC/Unit/External/Visibility.hs
- compiler/GHC/Unit/Finder.hs
- + compiler/GHC/Unit/Finder/Cache.hs
- compiler/GHC/Unit/Finder/Types.hs
- compiler/GHC/Unit/Module/Graph.hs
- compiler/GHC/Unit/Module/ModSummary.hs
- compiler/GHC/Unit/Module/WholeCoreBindings.hs
- compiler/GHC/Unit/State.hs
- compiler/GHC/Unit/Types.hs
- + compiler/GHC/Utils/Concurrent/Scope.hs
- compiler/GHC/Utils/Monad/Codensity.hs
- compiler/GHC/Utils/Ppr/Colour.hs
- compiler/GHC/Utils/TmpFs.hs
- compiler/Language/Haskell/Syntax/Decls.hs
- compiler/Language/Haskell/Syntax/Extension.hs
- compiler/ghc.cabal.in
- docs/users_guide/extending_ghc.rst
- docs/users_guide/exts/qualified_do.rst
- docs/users_guide/exts/type_families.rst
- docs/users_guide/using-warnings.rst
- ghc/GHCi/UI.hs
- ghc/GHCi/UI/Info.hs
- ghc/Main.hs
- ghc/ghc-bin.cabal.in
- hadrian/src/Hadrian/Haskell/Cabal/Parse.hs
- hadrian/src/Hadrian/Haskell/Cabal/Type.hs
- hadrian/src/Main.hs
- hadrian/src/Rules/Documentation.hs
- hadrian/src/Rules/Selftest.hs
- hadrian/src/Settings/Builders/Ar.hs
- hadrian/src/Settings/Warnings.hs
- libraries/base/src/GHC/Base.hs
- libraries/base/src/GHC/Exts.hs
- + libraries/base/tests/IO/T27971.hs
- + libraries/base/tests/IO/T27971.stdout
- libraries/base/tests/IO/all.T
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Base.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/GMP.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/Native.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/BigNat.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Integer.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Natural.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Primitives.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/WordArray.hs
- libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc
- libraries/ghc-internal/src/GHC/Internal/Event/Windows/FFI.hsc
- libraries/ghc-internal/src/GHC/Internal/IO/Windows/Handle.hsc
- + libraries/ghc-internal/src/GHC/Internal/Unboxed/Do.hs
- linters/lint-codes/LintCodes/Static.hs
- rts/PrimOps.cmm
- rts/RtsSymbols.c
- rts/include/stg/MiscClosures.h
- rts/js/environment.js
- testsuite/tests/ado/ado-optimal.hs
- testsuite/tests/ado/ado001.hs
- + testsuite/tests/bytecode/T27842.hs
- + testsuite/tests/bytecode/T27842.stdout
- + testsuite/tests/bytecode/T27842A.hs
- + testsuite/tests/bytecode/T27842N.hs
- testsuite/tests/bytecode/all.T
- + testsuite/tests/codeGen/should_compile/T27368.hs
- testsuite/tests/codeGen/should_compile/all.T
- + testsuite/tests/codeGen/should_run/T27624.hs
- + testsuite/tests/codeGen/should_run/T27624.stdout
- testsuite/tests/codeGen/should_run/all.T
- testsuite/tests/concurrent/T13615/Parallel.hs
- testsuite/tests/concurrent/should_run/T21651.hs
- testsuite/tests/concurrent/should_run/setnumcapabilities001.hs
- testsuite/tests/corelint/T27374.hs
- testsuite/tests/count-deps/CountDepsParser.stdout
- testsuite/tests/cpranal/should_run/T20598a.hs
- testsuite/tests/dependent/should_compile/T14729.stderr
- testsuite/tests/deriving/should_compile/T13758.hs
- testsuite/tests/deriving/should_run/all.T
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.hs
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.stderr
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.stdout
- testsuite/tests/determinism/determ017/A.hs
- testsuite/tests/diagnostic-codes/codes.stdout
- testsuite/tests/dmdanal/should_compile/T20510.stderr
- testsuite/tests/dmdanal/should_compile/T9208.hs
- testsuite/tests/driver/Makefile
- testsuite/tests/driver/T27461/Makefile
- + testsuite/tests/driver/T27461/T27461c.stderr
- testsuite/tests/driver/T27461/all.T
- + testsuite/tests/driver/T27461/src/Wrong.hs
- + testsuite/tests/driver/T28009.hs
- testsuite/tests/driver/all.T
- testsuite/tests/generics/T27245.hs
- testsuite/tests/generics/T27245.stdout
- testsuite/tests/ghc-api/downsweep/IncrementalDownsweep.hs
- testsuite/tests/ghc-api/downsweep/OldModLocation.hs
- testsuite/tests/ghc-api/downsweep/PartialDownsweep.hs
- testsuite/tests/ghc-api/fixed-nodes/FixedNodes.hs
- testsuite/tests/ghc-api/fixed-nodes/InterfaceModuleGraph.hs
- testsuite/tests/ghc-api/fixed-nodes/ModuleGraphInvariants.hs
- testsuite/tests/ghci.debugger/scripts/T14628.stderr
- testsuite/tests/ghci/prog-mhu001/prog-mhu001c.stdout
- testsuite/tests/ghci/prog-mhu002/prog-mhu002d.stdout
- testsuite/tests/ghci/scripts/T16575.script
- testsuite/tests/ghci/scripts/T16575.stdout
- testsuite/tests/ghci/scripts/T16804.stdout
- testsuite/tests/ghci/scripts/T20150.stdout
- + testsuite/tests/ghci/scripts/T27412.hs
- + testsuite/tests/ghci/scripts/T27412.script
- + testsuite/tests/ghci/scripts/T27412.stdout
- testsuite/tests/ghci/scripts/all.T
- testsuite/tests/haddock/perf/Fold.hs
- testsuite/tests/haddock/should_compile_flag_haddock/T17544.stderr
- testsuite/tests/hiefile/should_compile/hie002.hs
- testsuite/tests/hpc/raytrace/Eval.hs
- testsuite/tests/indexed-types/should_compile/T3787.hs
- + testsuite/tests/indexed-types/should_compile/ZapDeps.hs
- testsuite/tests/indexed-types/should_compile/all.T
- testsuite/tests/indexed-types/should_fail/T7729.hs
- testsuite/tests/indexed-types/should_fail/T7729a.hs
- testsuite/tests/interface-stability/ghc-experimental-exports.stdout
- testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32
- testsuite/tests/interface-stability/ghc-prim-exports.stdout
- testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32
- testsuite/tests/javascript/Makefile
- + testsuite/tests/javascript/T25665.hs
- + testsuite/tests/javascript/T25665.pkg
- + testsuite/tests/javascript/T25665.stdout
- + testsuite/tests/javascript/T25665Lib.hs
- + testsuite/tests/javascript/T25665_c.c
- + testsuite/tests/javascript/T25665_js.js
- testsuite/tests/javascript/all.T
- testsuite/tests/module/mod133.hs
- testsuite/tests/parser/should_compile/DumpParsedAst.stderr
- testsuite/tests/parser/should_compile/DumpRenamedAst.stderr
- testsuite/tests/parser/should_compile/KindSigs.stderr
- testsuite/tests/perf/compiler/T13253.hs
- testsuite/tests/perf/compiler/T14683.hs
- testsuite/tests/perf/compiler/T16473.hs
- testsuite/tests/perf/compiler/T18730_A.hs
- testsuite/tests/perf/compiler/T25723.hs
- testsuite/tests/plugins/defaulting-plugin/DefaultLifted.hs
- testsuite/tests/pmcheck/should_compile/T11276.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
- testsuite/tests/primops/should_run/all.T
- testsuite/tests/printer/Test24533.stdout
- testsuite/tests/profiling/should_compile/T19894/StreamK.hs
- testsuite/tests/profiling/should_run/T3001-2.hs
- testsuite/tests/programs/galois_raytrace/Eval.hs
- testsuite/tests/qualifieddo/should_compile/all.T
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.hs
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.stderr
- testsuite/tests/qualifieddo/should_fail/all.T
- + testsuite/tests/qualifieddo/should_fail/qdofail006.hs
- + testsuite/tests/qualifieddo/should_fail/qdofail006.stderr
- testsuite/tests/qualifieddo/should_run/all.T
- + testsuite/tests/qualifieddo/should_run/qdorun008.hs
- + testsuite/tests/qualifieddo/should_run/qdorun008.stdout
- testsuite/tests/rebindable/rebindable2.hs
- + testsuite/tests/rename/should_fail/Noncanonical.hs
- + testsuite/tests/rename/should_fail/Noncanonical.stderr
- testsuite/tests/rename/should_fail/all.T
- testsuite/tests/rename/should_fail/rnfail026.hs
- testsuite/tests/rep-poly/RepPolyRule3.stderr
- + testsuite/tests/rts/T27526.hs
- + testsuite/tests/rts/T27526.stdout
- + testsuite/tests/rts/T28012.hs
- + testsuite/tests/rts/T28012.stdout
- testsuite/tests/rts/all.T
- testsuite/tests/rts/ipe/IpeStats/Fold.hs
- testsuite/tests/show-bytecode/show-bytecode-hpc.stdout
- testsuite/tests/simplCore/should_compile/T14908_Deps.hs
- testsuite/tests/simplCore/should_compile/T26051.stderr
- testsuite/tests/simplCore/should_compile/T26903.stderr
- + testsuite/tests/simplCore/should_compile/T27737a.hs
- testsuite/tests/simplCore/should_compile/all.T
- testsuite/tests/simplCore/should_compile/simpl017.hs
- testsuite/tests/simplStg/should_compile/all.T
- + testsuite/tests/simplStg/should_compile/inferTags005.hs
- + testsuite/tests/simplStg/should_compile/inferTags005.stderr
- + testsuite/tests/simplStg/should_compile/inferTags006.hs
- + testsuite/tests/simplStg/should_compile/inferTags006.stderr
- + testsuite/tests/simplStg/should_run/InferTags007_A.hs
- + testsuite/tests/simplStg/should_run/T27831.hs
- + testsuite/tests/simplStg/should_run/T27831.stdout
- + testsuite/tests/simplStg/should_run/T27857.hs
- + testsuite/tests/simplStg/should_run/T27857.stdout
- testsuite/tests/simplStg/should_run/all.T
- + testsuite/tests/simplStg/should_run/inferTags007.hs
- + testsuite/tests/simplStg/should_run/inferTags007.stdout
- testsuite/tests/splice-imports/SI35.hs
- testsuite/tests/typecheck/should_compile/DeepSubsumption02.hs
- testsuite/tests/typecheck/should_compile/T4524.hs
- testsuite/tests/typecheck/should_compile/T4969.hs
- testsuite/tests/typecheck/should_fail/T22707.hs
- testsuite/tests/typecheck/should_fail/T22707.stderr
- testsuite/tests/typecheck/should_fail/T25950.stderr
- testsuite/tests/typecheck/should_fail/T7019.hs
- testsuite/tests/typecheck/should_fail/T8450.stderr
- testsuite/tests/typecheck/should_fail/T8603.hs
- + testsuite/tests/warnings/should_compile/RedundantCanonicalMethods.hs
- + testsuite/tests/warnings/should_compile/RedundantCanonicalMethods.stderr
- testsuite/tests/warnings/should_compile/WarnNoncanonical.hs
- testsuite/tests/warnings/should_compile/WarnNoncanonical.stderr
- testsuite/tests/warnings/should_compile/all.T
- utils/check-exact/ExactPrint.hs
- utils/check-ppr/Main.hs
- utils/haddock/haddock-api/haddock-api.cabal
- utils/haddock/haddock-api/src/Haddock/Backends/Hoogle.hs
- utils/haddock/haddock-api/src/Haddock/Backends/LaTeX.hs
- utils/haddock/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
- utils/haddock/haddock-api/src/Haddock/Convert.hs
- utils/haddock/haddock-api/src/Haddock/Interface.hs
- utils/haddock/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs
- utils/haddock/haddock-api/src/Haddock/Interface/Rename.hs
- utils/haddock/haddock-api/src/Haddock/Types.hs
- utils/haddock/haddock-api/src/Haddock/Utils/Json.hs
- utils/haddock/haddock-library/haddock-library.cabal
- utils/haddock/haddock.cabal
- utils/runghc/Main.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/e8ec242c517bf0cf3bf626025234c7…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/e8ec242c517bf0cf3bf626025234c7…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/case-bndr-unf] Use SimplClo for the argument of ApplyToVal
by Simon Peyton Jones (@simonpj) 27 Oct '26
by Simon Peyton Jones (@simonpj) 27 Oct '26
27 Oct '26
Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC
Commits:
24d1ab3b by Simon Peyton Jones at 2026-10-27T17:27:22+00:00
Use SimplClo for the argument of ApplyToVal
This patch renames SimplSR to SimplClo (a "closure"), and uses it not
only as the range of the Simplifier's substitution, but also as the
argument of an ApplyToVal continuation. ApplyToVal used to have three
fields
sc_env :: StaticEnv
sc_arg :: CoreExpr
sc_cast :: MOutCoercion
but now has just
sc_dup :: DupFlag
sc_arg :: SimplClo
An un-simplified argument is (ContEx env arg mco), and an already-simplified
one is (DoneEx arg _). See Note [The argument of ApplyToVal] in
GHC.Core.Opt.Simplify.Utils.
The main payoff is that, since the substitution maps an Id to a SimplClo,
beta-reduction can substitute the argument for the lambda binder without
further ado. Accordingly, preInlineUnconditionally is split into
* preInlineLetUnconditionally, for let-bindings
* preInlineBetaUnconditionally, for beta-redexes, which takes a SimplClo
The latter omits some checks that cannot fire for lambda binders.
Other notes
* The new (ContEx env arg mco) form makes it easy to push a cast into an
argument (mkCloCastMCo), whether or not it has been simplified.
See Note [The MOutCoercion field of ContEx].
* `preInlineUnconditionally` gets split into two variants, one for
let-bindings and one for beta-reduction (which takes a `SimplClo`).
There is a little bit of duplication here, but not much.
* Fixes #27962. The simplifier was previously dropping a cast when dealing
with runRW#. The new `SimplClo` makes that bug much harder to allow.
* Fixes #27963. contOutArgs, which supplies the arguments for Plan (BEFORE)
rule matching, ignored the sc_cast field of ApplyToVal. So in
(f |> co) n ==> f (n |> co1) -- co1 in sc_cast
a rule for `f` saw `f n` rather than `f (n |> co1)`, and failed to match
(the rule matcher checks the types of template variables). If f's
unfolding is active, f may then be inlined, and the rule is lost for
good. Now contOutArgs uses simplCloExpr, which includes the pending cast.
Almost no change in behaviour, except for the #27963 bug-fix.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
10 changed files:
- + changelog.d/T27962
- + changelog.d/T27963
- compiler/GHC/Core/Opt/Simplify/Env.hs
- compiler/GHC/Core/Opt/Simplify/Iteration.hs
- compiler/GHC/Core/Opt/Simplify/Utils.hs
- testsuite/tests/simplCore/should_compile/T15445.stderr
- + testsuite/tests/simplCore/should_compile/T27962.hs
- + testsuite/tests/simplCore/should_compile/T27963.hs
- + testsuite/tests/simplCore/should_compile/T27963.stderr
- testsuite/tests/simplCore/should_compile/all.T
Changes:
=====================================
changelog.d/T27962
=====================================
@@ -0,0 +1,7 @@
+section: compiler
+issues: #27962
+mrs: !16869
+synopsis:
+ Fix a Lint error when using `runRW#` and `coerce` together
+description:
+ Just an internal compiler bug, of long standing.
=====================================
changelog.d/T27963
=====================================
@@ -0,0 +1,7 @@
+section: compiler
+issues: #27963
+mrs: !16869
+synopsis:
+ Fix a rather obscure interaction of `coerce` and RULES
+description:
+ A `coerce` could prevent a RULE firing.
=====================================
compiler/GHC/Core/Opt/Simplify/Env.hs
=====================================
@@ -28,7 +28,7 @@ module GHC.Core.Opt.Simplify.Env (
SimplEnvIS, checkSimplEnvIS, pprBadSimplEnvIS,
-- * Substitution results
- SimplSR(..), substId, lookupRecBndr,
+ SimplClo(..), simplCloExpr, substId, lookupRecBndr,
-- * Simplifying 'Id' binders
simplNonRecBndr, simplNonRecJoinBndr, simplRecBndrs, simplRecJoinBndrs,
@@ -61,7 +61,7 @@ import GHC.Core.Opt.Simplify.Monad
import GHC.Core.Rules.Config ( RuleOpts(..) )
import GHC.Core
import GHC.Core.Utils
-import GHC.Core.Subst( substExprSC )
+import GHC.Core.Subst( substExpr )
import GHC.Core.Unfold
import GHC.Core.TyCo.Subst (emptyIdSubstEnv, mkSubst)
import GHC.Core.Multiplicity( Scaled(..), mkMultMul )
@@ -470,18 +470,26 @@ pprSimplEnv env
ppr_one v | isId v = ppr v <+> ppr (idUnfolding v)
| otherwise = ppr v
-type SimplIdSubst = IdEnv SimplSR -- IdId |--> OutExpr
+type SimplIdSubst = IdEnv SimplClo -- IdId |--> OutExpr
-- See Note [Extending the IdSubstEnv] in GHC.Core.Subst
--- | A substitution result.
-data SimplSR
+-- | A "closure" used in the Simplifier. It is used
+-- * as the range of the substitution, 'SimplIdSubst'
+-- * as the argument in an 'ApplyToVal' continuation
+-- Roughly: either an (InExpr, StaticEnv) pair for an
+-- as-yet-unsimplified expression
+-- or an OutExpr, for an already-simplified one
+-- It's a bit like thunk: we may "evaluate" the SimplClo in a ApplyToVal
+-- and thereby transform it from `ContEx` to `DoneEx`
+-- This happens in the `ApplyToVal` case of `mkDupableContWithDmds`
+-- See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils
+data SimplClo
= DoneEx OutExpr JoinPointHood
-- If x :-> DoneEx e ja is in the SimplIdSubst
-- then replace occurrences of x by e
-- and ja = Just a <=> x is a join-point of arity a
-- See Note [Join arity in SimplIdSubst]
-
| DoneId OutId
-- If x :-> DoneId v is in the SimplIdSubst
-- then replace occurrences of x by v
@@ -490,11 +498,11 @@ data SimplSR
| ContEx SimplEnv
InExpr
- MOutCoercion -- See Note [The sc_cast field of ApplyToVal]
+ MOutCoercion -- See Note [The MOutCoercion field of ContEx]
-- If x :-> ContEx static_env e mco is in the SimplISubst
-- then replace occurrences of x by (subst static_env e) |> mco
-instance Outputable SimplSR where
+instance Outputable SimplClo where
ppr (DoneId v) = text "DoneId" <+> ppr v
ppr (DoneEx e mj) = text "DoneEx" <> pp_mj <+> ppr e
where
@@ -637,7 +645,7 @@ reSimplifying :: SimplEnv -> Bool
reSimplifying (SimplEnv { seInlineDepth = n }) = n>0
---------------------
-extendIdSubst :: SimplEnv -> Id -> SimplSR -> SimplEnv
+extendIdSubst :: SimplEnv -> Id -> SimplClo -> SimplEnv
extendIdSubst env@(SimplEnv {seIdSubst = subst}) var res
= assertPpr (isId var && not (isCoVar var)) (ppr var) $
env { seIdSubst = extendVarEnv subst var res }
@@ -1032,7 +1040,7 @@ So we want to look up the inner X.g_34 in the substitution, where we'll
find that it has been substituted by b. (Or conceivably cloned.)
-}
-substId :: SimplEnv -> InId -> SimplSR
+substId :: SimplEnv -> InId -> SimplClo
-- Returns DoneEx only on a non-Var expression
substId (SimplEnv { seInScope = in_scope, seIdSubst = ids }) v
= case lookupVarEnv ids v of -- Note [Global Ids in the substitution]
@@ -1364,15 +1372,18 @@ getTCvSubst (SimplEnv { seInScope = in_scope, seTvSubst = tv_env, seCvSubst = cv
getFullSubst :: InScopeSet -> SimplEnv -> Subst
getFullSubst in_scope (SimplEnv { seIdSubst = id_env, seTvSubst = tv_env, seCvSubst = cv_env })
- = mkSubst in_scope (mapVarEnv to_expr id_env) tv_env cv_env
+ = mkSubst in_scope (mapVarEnv (simplCloExpr in_scope) id_env) tv_env cv_env
+
+simplCloExpr :: InScopeSet -> SimplClo -> OutExpr
+-- Turn a SimplClo into an OutExpr, by applying the substitution
+-- (if any) and the pending cast (if any)
+simplCloExpr _ (DoneEx e _) = e
+simplCloExpr _ (DoneId v) = Var v
+simplCloExpr in_scope (ContEx env e mco) = mkCastMCo e' mco
where
- to_expr :: SimplSR -> CoreExpr
- -- A tiresome impedence-matcher
- to_expr (DoneEx e _) = e
- to_expr (DoneId v) = Var v
- to_expr (ContEx env e mco) = mkCastMCo e' mco
- where
- e' = GHC.Core.Subst.substExprSC (getFullSubst in_scope env) e
+ e' = GHC.Core.Subst.substExpr (getFullSubst in_scope env) e
+ -- NB: we use substExpr, not substExprSC: we want to get the benefit of
+ -- knowing what is evaluated etc, via the in-scope set
substTy :: HasDebugCallStack => SimplEnv -> Type -> Type
substTy env ty = Type.substTy (getTCvSubst env) ty
=====================================
compiler/GHC/Core/Opt/Simplify/Iteration.hs
=====================================
@@ -285,8 +285,8 @@ simplRecOrTopPair :: SimplEnv
-> SimplM (SimplFloats, SimplEnv)
simplRecOrTopPair env bind_cxt old_bndr new_bndr rhs
- | Just env' <- preInlineUnconditionally env (bindContextLevel bind_cxt)
- old_bndr (UnSimplified env) rhs MRefl
+ | Just env' <- preInlineLetUnconditionally env (bindContextLevel bind_cxt)
+ old_bndr rhs env
= {-#SCC "simplRecOrTopPair-pre-inline-uncond" #-}
simplTrace "SimplBindr:inline-uncond1" (ppr old_bndr) $
do { tick (PreInlineUnconditionally old_bndr)
@@ -1226,17 +1226,8 @@ simplExprF1 env (Tick t expr) cont = {-#SCC "simplTick" #-} simplTick env t exp
simplExprF1 env (Coercion co) cont = {-#SCC "simplCoercionF" #-} simplCoercionF env co cont
simplExprF1 env (Cast body co) cont
- = do { co1 <- {-#SCC "simplCast-simplCoercion" #-}
- simplCoercion env co
-
- ; let cont1 = {-#SCC "simplCast-addCoerce" #-}
- if isReflCo co1
- then cont -- See Note [Optimising reflexivity]
- else pushCast env co1 cont
- -- True <=> co1 is optimised
-
- ; {-#SCC "simplCast-simplExprF" #-}
- simplExprF env body cont1 }
+ = do { co1 <- simplCoercion env co
+ ; simplExprF env body (pushCast env co1 cont) }
simplExprF1 env (App fun arg) cont
= {-#SCC "simplExprF1-App" #-} case arg of
@@ -1264,9 +1255,9 @@ simplExprF1 env (App fun arg) cont
-- observed the quadratic behavior, so this extra entanglement
-- seems not worthwhile.
simplExprF env fun $
- ApplyToVal { sc_arg = arg, sc_env = UnSimplified env
+ ApplyToVal { sc_arg = ContEx env arg MRefl, sc_dup = NoDup
, sc_hole_ty = substTy env (exprType fun)
- , sc_cast = MRefl, sc_cont = cont }
+ , sc_cont = cont }
simplExprF1 env expr@(Lam {}) cont
= {-#SCC "simplExprF1-Lam" #-}
@@ -1301,7 +1292,7 @@ simplExprF1 env (Let (NonRec bndr rhs) body) cont
do { ty' <- simplType env ty
; simplExprF (extendTvSubst env bndr ty') body cont }
- | Just env' <- preInlineUnconditionally env NotTopLevel bndr (UnSimplified env) rhs MRefl
+ | Just env' <- preInlineLetUnconditionally env NotTopLevel bndr rhs env
-- Because of the let-can-float invariant, it's ok to
-- inline freely, or to drop the binding if it is dead.
= do { simplTrace "SimplBindr:inline-uncond2" (ppr bndr) $
@@ -1637,9 +1628,8 @@ rebuild_go env expr cont
ApplyToTy { sc_arg_ty = ty, sc_cont = cont}
-> rebuild_go env (App expr (Type ty)) cont
- ApplyToVal { sc_arg = arg, sc_env = arg_se, sc_cast = arg_mco
- , sc_cont = cont, sc_hole_ty = fun_ty }
- -> do { arg' <- simplArg env Nothing fun_ty arg_se arg arg_mco
+ ApplyToVal { sc_arg = arg, sc_cont = cont, sc_hole_ty = fun_ty }
+ -> do { arg' <- simplArg env Nothing fun_ty arg
; rebuild_go env (App expr arg') cont }
completeBindX :: SimplEnv
@@ -1687,69 +1677,12 @@ completeBindX env from_what bndr rhs body cont
; let all_floats = rhs_floats `addFloats` bind_float `addFloats` body_floats
; return ( all_floats, body' ) }
-{-
-************************************************************************
+
+{- *********************************************************************
* *
-\subsection{Lambdas}
+ Arguments
* *
-************************************************************************
--}
-
-{- Note [Optimising reflexivity]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-It's important (for compiler performance) to get rid of reflexivity as soon
-as it appears. See #11735, #14737, and #15019.
-
-In particular, we want to behave well on
-
- * e |> co1 |> co2
- where the two happen to cancel out entirely. That is quite common;
- e.g. a newtype wrapping and unwrapping cancel.
-
-
- * (f |> co) @t1 @t2 ... @tn x1 .. xm
- Here we will use pushCoTyArg and pushCoValArg successively, which
- build up SelCo stacks. Silly to do that if co is reflexive.
-
-However, we don't want to call isReflexiveCo too much, because it uses
-type equality which is expensive on big types (#14737 comment:7).
-
-A good compromise (determined experimentally) seems to be to call
-isReflexiveCo
- * when composing casts, and
- * at the end
-
-In investigating this I saw missed opportunities for on-the-fly
-coercion shrinkage. See #15090.
-
-Note [Avoid re-simplifying coercions]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-In some benchmarks (with deeply nested cases) we successively push
-casts onto the SimplCont. We don't want to call the coercion optimiser
-on each successive composition -- that's at least quadratic. So:
-
-* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
- record whether the coercion optimiser has been applied to the coercion.
-
-* In `simplCast`, when we see (Cast e co), we simplify `co` to get
- an OutCoercion, and built a CastIt with sc_opt=True.
-
- Actually not quite: if we are simplifying the result of inlining an
- unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
- just /substitute/ which is cheaper. See `simplCoercion`.
-
-* In `pushCast` if we combine this new coercion with an existing one,
- we build a CastIt for (co1 ; co2) with sc_opt=False.
-
-* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
- composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
-
-* When duplicating a continuation in `mkDupableContWithDmds`, before
- duplicating a CastIt, optimise the coercion. Otherwise we'll end up
- optimising it separately in the duplicate copies.
-
-* See also Note [The sc_cast field of ApplyToVal] in GHC.Core.Opt.Simplify.Utils.
--}
+********************************************************************* -}
simplArg :: SimplEnvIS -- ^ Used only for its InScopeSet
-> Maybe ArgInfo -- ^ Just <=> This arg `ai` occurs in an app
@@ -1757,15 +1690,12 @@ simplArg :: SimplEnvIS -- ^ Used only for its InScopeSet
-- how `f` uses `ai`, affecting the Stop
-- continuation passed to 'simplExprC'
-> OutType -- ^ Type of the function applied to this arg
- -> StaticEnv -> CoreExpr -- ^ Expression with its static envt
- -> MOutCoercion -- Wrap this around the result
+ -> SimplClo -- ^ The argument
-> SimplM OutExpr
-simplArg _ _ _ (Simplified {}) arg mco
- = -- See Note [Avoid repeated simplification]
- case mco of
- MRefl -> return arg -- Vastly common case
- MCo co -> return (mkCast arg co)
-simplArg env mb_arg_info fun_ty (UnSimplified arg_se) arg mco
+-- See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils
+simplArg _ _ _ (DoneEx arg _) = return arg -- See Note [Avoid repeated simplification]
+simplArg _ _ _ (DoneId v) = return (Var v)
+simplArg env mb_arg_info fun_ty (ContEx arg_se arg mco)
= do { let arg_env' = arg_se `setInScopeFromE` env
arg_ty = funArgTy fun_ty
stop = case mb_arg_info of
@@ -1774,13 +1704,12 @@ simplArg env mb_arg_info fun_ty (UnSimplified arg_se) arg mco
cont = addCastMCo mco stop
; simplExprC arg_env' arg cont }
-{-
-************************************************************************
+
+{- *********************************************************************
* *
-\subsection{Lambdas}
+ Lambdas
* *
-************************************************************************
--}
+********************************************************************* -}
simplNonRecBody :: SimplEnv -> FromWhat
-> InExpr -> SimplCont
@@ -1807,13 +1736,9 @@ simpl_lam env bndr body (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = cont })
-- Value beta-reduction
-- This works for /coercion/ lambdas too
-simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_env = arg_se
- , sc_cast = arg_mco, sc_cont = cont
+simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_cont = cont
, sc_hole_ty = fun_ty})
- | Just env' <- preInlineUnconditionally env NotTopLevel bndr arg_se arg arg_mco
- , not (needsCaseBindingL arg_levity arg)
- -- Ok to test arg::InExpr in needsCaseBinding because
- -- exprOkForSpeculation is stable under simplification
+ | Just env' <- preInlineBetaUnconditionally env arg_levity bndr arg
= simplTrace "SimplBindr:inline-uncond3"
(ppr bndr <+> text ":=" <+> ppr arg) $
do { tick (BetaReduction bndr)
@@ -1824,14 +1749,14 @@ simpl_lam env bndr body (ApplyToVal { sc_arg = arg, sc_env = arg_se
= do { tick (BetaReduction bndr)
; let from_what = FromBeta arg_levity
- ; case (arg_se, arg_mco) of
- (UnSimplified arg_env, MRefl)
+ ; case arg of
+ ContEx arg_env arg_expr MRefl
-- Optimisation: when there is no wrapping coercion use simplNonRecE
-- so that the clever floating out of let-bindings can take place
-- in this round of the Simplifier
- -> simplNonRecE env from_what bndr (arg, arg_env) body cont
+ -> simplNonRecE env from_what bndr (arg_expr, arg_env) body cont
- _ -> do { arg' <- simplArg env Nothing fun_ty arg_se arg arg_mco
+ _ -> do { arg' <- simplArg env Nothing fun_ty arg
; completeBindX env from_what bndr arg' body cont }
}
where
@@ -2295,11 +2220,14 @@ simplOutId env fun cont
| fun `hasKey` runRWKey
, ApplyToTy { sc_cont = cont1 } <- cont
, ApplyToTy { sc_cont = cont2, sc_arg_ty = hole_ty } <- cont1
- , ApplyToVal { sc_cont = cont3, sc_arg = arg, sc_cast = arg_mco
- , sc_env = arg_se, sc_hole_ty = fun_ty } <- cont2
+ , ApplyToVal { sc_cont = cont3, sc_arg = arg_clo, sc_hole_ty = fun_ty } <- cont2
-- Do this even if (contIsStop cont), or if seCaseCase is off.
-- See Note [No eta-expansion in runRW#]
- = do { let (_saf,arg_env) = mkAltEnv env arg_se
+ = do { let (arg_env, arg, arg_mco)
+ = case arg_clo of
+ ContEx se e mco -> (se `setInScopeFromE` env, e, mco)
+ DoneEx e _ -> (env, e, MRefl)
+ DoneId v -> (env, Var v, MRefl)
overall_res_ty = contResultType cont3
-- hole_ty is the type of the current runRW# application
@@ -2314,23 +2242,32 @@ simplOutId env fun cont
-- * If we don't do this we get a beta-redex every time, so the
-- simplifier keeps doing more iterations.
-- * Even more important: see Note [No eta-expansion in runRW#]
+ --
+ -- But only if there is no pending cast on the argument (#27962);
+ -- if there is, the general case below deals with it properly
; arg' <- case arg of
- Lam s body -> do { (env', s') <- simplBinder arg_env s
- ; body' <- simplExprC env' body inner_cont
- ; return (Lam s' body') }
- -- Important: do not try to eta-expand this lambda
- -- See Note [No eta-expansion in runRW#]
+ Lam s body
+ | MRefl <- arg_mco
+ -> do { (env', s') <- simplBinder arg_env s
+ ; body' <- simplExprC env' body inner_cont
+ ; return (Lam s' body') }
+ -- Important: do not try to eta-expand this lambda
+ -- See Note [No eta-expansion in runRW#]
_ -> do { s' <- newId (fsLit "s") ManyTy realWorldStatePrimTy
; let (m,_,_) = splitFunTy fun_ty
env' = arg_env `addNewInScopeIds` [s']
hole_ty = mkVisFunTy m realWorldStatePrimTy new_runrw_res_ty
cont' = pushCastMCo env' arg_mco $
- ApplyToVal { sc_arg = Var s', sc_cast = MRefl
- , sc_env = Simplified OkDup
+ ApplyToVal { sc_arg = DoneId s'
+ , sc_dup = dup
, sc_cont = inner_cont
, sc_hole_ty = hole_ty }
-- cont' applies to s', then K
+ -- The argument (Var s') is small, but inner_cont may not be
+ -- dupable; see (AV2) in Note [The argument of ApplyToVal]
+ dup | contIsDupable inner_cont = OkDup
+ | otherwise = NoDup
; body' <- simplExprC env' arg cont'
; return (Lam s' body') }
@@ -2397,13 +2334,12 @@ rebuildCall env info (ApplyToTy { sc_arg_ty = arg_ty, sc_hole_ty = hole_ty, sc_c
---------- Simplify value arguments --------------------
rebuildCall env fun_info
- (ApplyToVal { sc_arg = arg, sc_env = arg_se, sc_cast = arg_mco
- , sc_hole_ty = fun_ty, sc_cont = cont })
- | UnSimplified arg_env <- arg_se
+ (ApplyToVal { sc_arg = arg, sc_hole_ty = fun_ty, sc_cont = cont })
+ | ContEx arg_env arg_expr arg_mco <- arg
, isStrictArgInfo fun_info -- Strict arguments
, seCaseCase env -- But only when case-of-case is on.
-- See Note [Case-of-case and full laziness]
- = simplExprF (arg_env `setInScopeFromE` env) arg $
+ = simplExprF (arg_env `setInScopeFromE` env) arg_expr $
addCastMCo arg_mco $
StrictArg { sc_fun = fun_info, sc_fun_ty = fun_ty
, sc_dup = NoDup
@@ -2415,7 +2351,7 @@ rebuildCall env fun_info
-- There is no benefit (unlike in a let-binding), and we'd
-- have to be very careful about bogus strictness through
-- floating a demanded let.
- = do { arg' <- simplArg env (Just fun_info) fun_ty arg_se arg arg_mco
+ = do { arg' <- simplArg env (Just fun_info) fun_ty arg
; rebuildCall env (addValArgTo fun_info arg' fun_ty) cont }
---------- No further useful info, revert to generic rebuild ------------
@@ -2563,11 +2499,11 @@ Then
* We simplify BIG before trying the rule; but the rule does not fire
(forcing this simplification is why we have the RULE in this example)
* We inline f = \x. g x, in `simpl_lam`
- * So if `simpl_lam` did preInlineUnconditionally we get (g BIG)
+ * So if `simpl_lam` did preInlineBetaUnconditionally we get (g BIG)
* Now if g has a RULE we'll simplify BIG again, and this whole thing can
iterate.
* However, if `f` did not have a RULE, so that BIG has /not/ already been
- simplified, we /want/ to do preInlineUnconditionally in simpl_lam.
+ simplified, we /want/ to do preInlineBetaUnconditionally in simpl_lam.
So we go to some effort to avoid repeatedly simplifying the same thing.
Suppose we are in the Plan (AFTER) case of rule application, for a rule
@@ -2576,8 +2512,9 @@ and a call
f (e1,e2)
Then:
-* The (sc_env :: StaticEnv) field of ApplyToVal records if the argument
- has been evaluated. See Note [StaticEnv] in GHC.Core.Opt.Simplify.Utils.
+* The (sc_arg :: SimplClo) field of ApplyToVal records if the argument
+ has been simplified: it is a DoneEx if so, and a ContEx if not.
+ See Note [The argument of ApplyToVal] in GHC.Core.Opt.Simplify.Utils.
* The rule fires returning
RuleMatch { rm_rhs = (\x y. ...x..), rm_args = [ e1, e2 ] }
@@ -2590,17 +2527,17 @@ Then:
is the reason that `RuleMatch` separates the two.
* `fireRuleAFTER` uses `pushOutArgs` to push [ e1, e2 ] back onto the
- continuation with sc_env=Simplified NoDup.
+ continuation as (DoneEx e1) and (DoneEx e2).
* Beta-reduction fires (in `simpl_lam`) for the \x.
* Suppose `x` occurs many times. Then we want to behave like `let x=e1 in ..`,
- but we use `simplArg`, which in turn uses that `sc_env` flag to avoid
+ but we use `simplArg`, which in turn uses the DoneEx to avoid
re-simplifying `e1`.
* Suppose `x` occurs just once or not at all; then the call to
- `preInlineUnconditionally` in `simpl_lam` will fire, and extend the
- substitution with
+ `preInlineBetaUnconditionally` in `simpl_lam` will fire, and extend the
+ substitution with the very same SimplClo
x :-> DoneEx e1
* At the /occurrence/ of `x`, the function `simplInId` calls `simplOutExpr`
@@ -2807,8 +2744,7 @@ trySeqRules in_env scrut rhs cont
, ValArg { as_arg = no_cast_scrut
, as_dmd = seqDmd
, as_hole_ty = res3_ty } ]
- rule_cont = ApplyToVal { sc_env = UnSimplified in_env, sc_arg = rhs
- , sc_cast = MRefl
+ rule_cont = ApplyToVal { sc_arg = ContEx in_env rhs MRefl, sc_dup = NoDup
, sc_cont = cont, sc_hole_ty = res4_ty }
out_args = [Type rhs_rep, Type scrut_ty, Type rhs_ty, no_cast_scrut]
@@ -3913,8 +3849,8 @@ knownCon env scrut dc dc_args case_bndr alt_bndrs rhs cont
return ( emptyFloats env
, extendIdSubst env case_bndr (DoneEx scrut NotJoinPoint))
- | Just env' <- preInlineUnconditionally env NotTopLevel case_bndr
- (UnSimplified env) con_app MRefl
+ | Just env' <- preInlineLetUnconditionally env NotTopLevel case_bndr
+ con_app env
= return (emptyFloats env', env')
| otherwise
@@ -4148,8 +4084,7 @@ mkDupableContWithDmds env dmds
, sc_arg_ty = arg_ty, sc_hole_ty = hole_ty }) }
mkDupableContWithDmds env dmds
- (ApplyToVal { sc_arg = arg, sc_env = se, sc_cast = arg_mco
- , sc_cont = cont, sc_hole_ty = hole_ty })
+ (ApplyToVal { sc_arg = arg, sc_cont = cont, sc_hole_ty = hole_ty })
= -- e.g. [...hole...] (...arg...)
-- ==>
-- let a = ...arg...
@@ -4161,12 +4096,12 @@ mkDupableContWithDmds env dmds
expectNonEmpty dmds
; (floats1, cont') <- mkDupableContWithDmds env cont_dmds cont
; let env' = env `setInScopeFromF` floats1
- ; arg' <- simplArg env' Nothing hole_ty se arg arg_mco
+ ; arg' <- simplArg env' Nothing hole_ty arg
; (let_floats2, triv_arg) <- makeTrivial env' NotTopLevel dmd (fsLit "karg") arg'
; let all_floats = floats1 `addLetFloats` let_floats2
; return ( all_floats
- , ApplyToVal { sc_arg = triv_arg, sc_env = Simplified OkDup
- , sc_cast = MRefl, sc_cont = cont', sc_hole_ty = hole_ty }) }
+ , ApplyToVal { sc_arg = DoneEx triv_arg NotJoinPoint, sc_dup = OkDup
+ , sc_cont = cont', sc_hole_ty = hole_ty }) }
mkDupableContWithDmds env _
(Select { sc_bndr = case_bndr, sc_alts = alts, sc_env = se, sc_cont = cont })
=====================================
compiler/GHC/Core/Opt/Simplify/Utils.hs
=====================================
@@ -12,7 +12,8 @@ module GHC.Core.Opt.Simplify.Utils (
tryEtaExpandRhs, wantEtaExpansion,
-- Inlining,
- preInlineUnconditionally, postInlineUnconditionally,
+ preInlineLetUnconditionally, preInlineBetaUnconditionally,
+ postInlineUnconditionally,
activeRule,
getUnfoldingInRuleMatch,
updModeForStableUnfoldings, updModeForRuleLHS, updModeForRuleRHS,
@@ -174,16 +175,14 @@ data SimplCont
-- Invariant: never an identity coercion
, sc_opt :: Bool -- True <=> sc_co has had optCoercion applied to it
-- See Note [Avoid re-simplifying coercions]
- -- in GHC.Core.Opt.Simplify.Iteration
, sc_cont :: SimplCont }
| ApplyToVal -- (ApplyToVal arg K)[e] = K[ e arg ]
{ sc_hole_ty :: OutType -- Type of the function, presumably (forall a. blah)
-- See Note [The hole type in ApplyToTy]
- , sc_env :: StaticEnv -- See Note [StaticEnv]
- , sc_arg :: CoreExpr -- The argument
- , sc_cast :: MOutCoercion -- Wrap this OutCoercion around the (simplified) argument
- -- See Note [The sc_cast field of ApplyToVal]
+ , sc_dup :: DupFlag -- See Note [The argument of ApplyToVal]
+ , sc_arg :: SimplClo -- The argument
+ -- Invariant: if sc_arg is a ContEx, then sc_dup = NoDup
, sc_cont :: SimplCont }
| ApplyToTy -- (ApplyToTy ty K)[e] = K[ e ty ]
@@ -253,49 +252,85 @@ perhapsSubstTy (UnSimplified env) ty = substTy env ty
{- Note [StaticEnv]
~~~~~~~~~~~~~~~~~~~
-Consider ApplyToVal, which has
- sc_env :: StaticEnv
- sc_arg :: CoreExpr
-Initially, `sc_arg` is an un-simplified InExpr, and `sc_env` is (UnSimplified env),
-where `env` gives meaning to the free variables of `sc_arg`; in particular, `env`
-may have substitutions that must apply to the argument.
-
-But sometimes we simplify the argument, and in that case, `sc_arg` is an OutExpr,
-the simplified argument, and `sc_env` is (Simplified NoDup) or (Simplified OkDup).
-The former is the safe, conservative option, but in `mkDupableCont` we want to make
-the continuation duplicable, so we make the argument small and tag it with
-(Simplified OkDup).
-
-We later simplify the argument, e.g. in `simplArg`. Then
- * If sc_env is Simplified, it's a no-op
- * If sc_env is (UnSimplified arg_env) we simplify `sc_arg` with
- - Its captured envt `arg_env`
+Consider Select, which has
+ sc_env :: StaticEnv
+ sc_bndr :: Id
+ sc_alts :: [CoreAlt]
+Initially, `sc_bndr` and `sc_alts` are un-simplified InThings, and `sc_env` is
+(UnSimplified env), where `env` gives meaning to their free variables; in
+particular, `env` may have substitutions that must apply to them.
+
+But sometimes (notably in `mkDupableCont`) we simplify them, and in that case
+`sc_bndr` and `sc_alts` are OutThings, and `sc_env` is (Simplified dup).
+
+When we finally simplify the alternatives (see `mkAltEnv`)
+ * If sc_env is Simplified, there is nothing to do
+ * If sc_env is (UnSimplified alt_env) we simplify them with
+ - Their captured envt `alt_env`
- but overriding its InScopeSet with the larger one at the
simplification point.
Why override the InScopeSet? Example:
- (let y = ey in f) ex
- By the time we simplify ex, 'y' will be in scope.
- All this is done in `simplArg`.
+ case (let y = ey in f) of alts
+ By the time we simplify alts, 'y' will be in scope.
Note that:
* In the Simplified case there is no environment, because the substitution has
already been applied.
-* We say sc_arg :: CoreExpr, rather than sc_arg :: InExpr or sc_arg :: OutExpr,
- because whether it is InExpr or OutExpr depends on `sc_env`
+* We say sc_alts :: [CoreAlt], rather than [InAlt] or [OutAlt],
+ because whether they are InAlts or OutAlts depends on `sc_env`
+
+* Same deal for StrictBind, where the StaticEnv scopes over sc_bndr and sc_body.
-* Same deal for Select, and StrictBind, but the StaticEnv scopes over
- * sc_bndr and sc_alts (for Select)
- * sc_bndr and sc_body (for StrictBind)
+* ApplyToVal is similar, but simpler: it uses a SimplClo instead.
+ See Note [The argument of ApplyToVal].
-* Even though the InScopeSet of an (UnSimplified se) is overridden in `simplArg`,
- that InScopeSet is not irrelevant: it should include all the free vars of
- applying the substitution to the InExpr. Reason: contHoleType uses perhapsSubstTy
- to apply the substitution to the expression, and that (rightly) gives ASSERT
- failures if the InScopeSet isn't big enough.
+* Even though the InScopeSet of an (UnSimplified se) is overridden, that
+ InScopeSet is not irrelevant: it should include all the free vars of
+ applying the substitution to the InThing. Reason: contHoleType uses
+ perhapsSubstTy to apply the substitution to the binder, and that
+ (rightly) gives ASSERT failures if the InScopeSet isn't big enough.
(SE1) If dup = OkToDup, then continuation k is also ok-to-dup
+
+Note [The argument of ApplyToVal]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ApplyToVal has
+ sc_dup :: DupFlag
+ sc_arg :: SimplClo
+
+Initially, `sc_arg` is (ContEx env arg MRefl), where `arg` is the un-simplified
+InExpr, and `env` gives meaning to the free variables of `arg`; in particular,
+`env` may have substitutions that must apply to the argument.
+
+But sometimes we simplify the argument, and in that case, `sc_arg` is
+(DoneEx arg' NotJoinPoint), where `arg'` is the simplified argument.
+
+The `sc_dup` flag says whether the continuation can be duplicated. NoDup is
+the safe, conservative option, but in `mkDupableCont` we want to make the
+continuation duplicable, so we make the argument small (and hence a DoneEx)
+and set `sc_dup` to OkDup. Invariants:
+
+ (AV1) If `sc_arg` is a ContEx, then `sc_dup` is NoDup.
+
+ (AV2) If `sc_dup` is OkDup then `sc_cont` is also ok-to-dup; see (SE1)
+ in Note [StaticEnv].
+
+We later simplify the argument, in `simplArg`. Then
+ * If `sc_arg` is a DoneEx (or DoneId), it's a no-op
+ * If `sc_arg` is (ContEx arg_env arg mco) we simplify `arg` with
+ - Its captured envt `arg_env`
+ - but overriding its InScopeSet with the larger one at the
+ simplification point.
+ Why override the InScopeSet? Example:
+ (let y = ey in f) ex
+ By the time we simplify ex, 'y' will be in scope.
+ Then we wrap the cast `mco` around the result.
+ See Note [The MOutCoercion field of ContEx].
+
+Using a SimplClo for the argument means that it can be substituted for a lambda
+binder, in `preInlineBetaUnconditionally`, without further ado.
-}
instance Outputable DupFlag where
@@ -313,9 +348,9 @@ instance Outputable SimplCont where
= (text "TickIt" <+> ppr t) $$ ppr cont
ppr (ApplyToTy { sc_arg_ty = ty, sc_cont = cont })
= (text "ApplyToTy" <+> pprParendType ty) $$ ppr cont
- ppr (ApplyToVal { sc_arg = arg, sc_env = env, sc_cont = cont, sc_hole_ty = hole_ty })
- = (hang (text "ApplyToVal" <> braces (ppr env) <+> text "hole-ty:" <+> pprParendType hole_ty)
- 2 (pprParendExpr arg))
+ ppr (ApplyToVal { sc_arg = arg, sc_dup = dup, sc_cont = cont, sc_hole_ty = hole_ty })
+ = (hang (text "ApplyToVal" <> braces (ppr dup) <+> text "hole-ty:" <+> pprParendType hole_ty)
+ 2 (ppr arg))
$$ ppr cont
ppr (StrictBind { sc_bndr = b, sc_cont = cont })
= (text "StrictBind" <+> ppr b) $$ ppr cont
@@ -434,8 +469,8 @@ pushOutArgs fun_ty (arg:args) cont
= ApplyToTy { sc_hole_ty = fun_ty, sc_arg_ty = ty
, sc_cont = pushOutArgs (piResultTy fun_ty ty) args cont }
| otherwise
- = ApplyToVal { sc_hole_ty = fun_ty, sc_cast = MRefl
- , sc_arg = arg, sc_env = Simplified NoDup
+ = ApplyToVal { sc_hole_ty = fun_ty
+ , sc_arg = DoneEx arg NotJoinPoint, sc_dup = NoDup
, sc_cont = pushOutArgs (funResultTy fun_ty) args cont}
pushArgSpecs :: [ArgSpec] -- In normal, forward order
@@ -446,7 +481,7 @@ pushArgSpec :: ArgSpec -> SimplCont -> SimplCont
pushArgSpec (TyArg { as_arg_ty = arg_ty, as_hole_ty = hole_ty }) cont
= ApplyToTy { sc_arg_ty = arg_ty, sc_hole_ty = hole_ty, sc_cont = cont }
pushArgSpec (ValArg { as_arg = arg, as_hole_ty = hole_ty }) cont
- = ApplyToVal { sc_arg = arg, sc_env = Simplified NoDup, sc_cast = MRefl
+ = ApplyToVal { sc_arg = DoneEx arg NotJoinPoint, sc_dup = NoDup
, sc_hole_ty = hole_ty, sc_cont = cont }
argSpecArg :: ArgSpec -> OutExpr
@@ -497,7 +532,12 @@ contIsStop _ = False
contIsDupable :: SimplCont -> Bool
contIsDupable (Stop {}) = True
contIsDupable (ApplyToTy { sc_cont = k }) = contIsDupable k
-contIsDupable (ApplyToVal { sc_env = se }) = okToDupSE se -- See (SE1) in Note [StaticEnv]
+contIsDupable (ApplyToVal { sc_dup = dup, sc_arg = arg })
+ = assertPpr (not (okToDup dup && is_cont_ex arg)) (ppr arg) $
+ okToDup dup -- See (AV1,AV2) in Note [The argument of ApplyToVal]
+ where
+ is_cont_ex (ContEx {}) = True
+ is_cont_ex _ = False
contIsDupable (Select { sc_env = se }) = okToDupSE se -- ...ditto...
contIsDupable (StrictBind { sc_env = se }) = okToDupSE se -- ...ditto...
contIsDupable (StrictArg { sc_dup = dup }) = okToDup dup -- ...ditto...
@@ -606,8 +646,8 @@ contArgs env cont
lone (CastIt {}) = False -- stops it being "lone"
lone _ = True
- go args (ApplyToVal { sc_arg = arg, sc_env = se, sc_cont = k })
- = go (interestingArg env se arg : args) k
+ go args (ApplyToVal { sc_arg = arg, sc_cont = k })
+ = go (interestingArg env arg : args) k
go args (ApplyToTy { sc_cont = k }) = go args k
go args (CastIt { sc_cont = k }) = go args k
go args k = (False, reverse args, k)
@@ -622,15 +662,13 @@ contOutArgs env cont
go (ApplyToTy { sc_arg_ty = ty, sc_cont = cont })
= Type ty : go cont
- go (ApplyToVal { sc_arg = arg, sc_env = se, sc_cont = cont })
- = case se of
- Simplified {} -> arg : go cont
- UnSimplified env -> GHC.Core.Subst.substExpr (getFullSubst in_scope env) arg : go cont
- -- Make sure we apply the static environment `sc_env` as a substitution
+ go (ApplyToVal { sc_arg = arg, sc_cont = cont })
+ = simplCloExpr in_scope arg : go cont
+ -- Make sure we apply the static environment of a ContEx as a substitution
-- to get an OutExpr. See (BF1) in Note [tryRules: plan (BEFORE)]
-- in GHC.Core.Opt.Simplify.Iteration
- -- NB: we use substExpr, not substExprSC: we want to get the benefit of
- -- knowing what is evaluated etc, via the in-scope set
+ -- simplCloExpr also wraps the pending cast (if any) around the argument;
+ -- omitting it can make a rule fail to match (#27963)
-- No more arguments
go _ = []
@@ -827,23 +865,86 @@ make use of the strictness info for the function.
* *
********************************************************************* -}
-{- Note [The sc_cast field of ApplyToVal]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+{- Note [The MOutCoercion field of ContEx]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the `ApplyToVal` case of `pushCast` we want to push an `OutCoercion` into the
-argument. But the argument may or may not have been simplified (see Note [StaticEnv])
-so we definitely can't just wrap the cast around the argument. Instead we store it
-in the `sc_cast` field -- this is the whole reason for the `sc_cast` field.
+argument. But the argument may or may not have been simplified (see Note [The
+argument of ApplyToVal]):
+
+* If it has been simplified, it is (DoneEx arg' _), and we can simply wrap the
+ cast around `arg'`.
+
+* If it has not, it is (ContEx env arg mco), and we definitely can't just wrap
+ the (Out)cast around the InExpr `arg`. Instead we compose it with `mco` --
+ this is the whole reason for the MOutCoercion field of `ContEx`.
+
+All this is done by `mkCloCastMCo`.
We deal with that pending cast mainly in `simplArg`.
But we /also/ sometimes want to substitute that argument for a lambda-binder, in
-`preInlineUnconditionally`. So we also have to put that pending cast into
-`ContEx`; hence the MOutCoercion field of `ContEx`. Then we deal with it
-appropriately when /applying/ the substitution in `simplInId`.
+`preInlineBetaUnconditionally`; and since the substitution maps an Id to a SimplClo,
+the pending cast comes along for free. Then we deal with it appropriately when
+/applying/ the substitution in `simplInId`.
+
+Note [Optimising reflexivity]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+It's important (for compiler performance) to get rid of reflexivity as soon
+as it appears. See #11735, #14737, and #15019.
+
+In particular, we want to behave well on
+
+ * e |> co1 |> co2
+ where the two happen to cancel out entirely. That is quite common;
+ e.g. a newtype wrapping and unwrapping cancel.
+
+
+ * (f |> co) @t1 @t2 ... @tn x1 .. xm
+ Here we will use pushCoTyArg and pushCoValArg successively, which
+ build up SelCo stacks. Silly to do that if co is reflexive.
+
+However, we don't want to call isReflexiveCo too much, because it uses
+type equality which is expensive on big types (#14737 comment:7).
+
+A good compromise (determined experimentally) seems to be to call
+isReflexiveCo
+ * when composing casts, and
+ * at the end
+
+In investigating this I saw missed opportunities for on-the-fly
+coercion shrinkage. See #15090.
+
+Note [Avoid re-simplifying coercions]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+In some benchmarks (with deeply nested cases) we successively push
+casts onto the SimplCont. We don't want to call the coercion optimiser
+on each successive composition -- that's at least quadratic. So:
+
+* The CastIt constructor in SimplCont has a `sc_opt :: Bool` flag to
+ record whether the coercion optimiser has been applied to the coercion.
+
+* In `simplCast`, when we see (Cast e co), we simplify `co` to get
+ an OutCoercion, and built a CastIt with sc_opt=True.
+
+ Actually not quite: if we are simplifying the result of inlining an
+ unfolding (seInlineDepth > 0), then instead of /optimising/ it again,
+ just /substitute/ which is cheaper. See `simplCoercion`.
+
+* In `pushCast` if we combine this new coercion with an existing one,
+ we build a CastIt for (co1 ; co2) with sc_opt=False.
+
+* When unpacking a CastIt, in `rebuild`, we optimise the (presumably
+ composed) coercion if sc_opt=False; this is done by `optOutCoercion`.
+
+* When duplicating a continuation in `mkDupableContWithDmds`, before
+ duplicating a CastIt, optimise the coercion. Otherwise we'll end up
+ optimising it separately in the duplicate copies.
+
+* See also Note [The MOutCoercion field of ContEx]
-}
optOutCoercion :: SimplEnvIS -> OutCoercion -> Bool -> OutCoercion
--- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
+-- See Note [Avoid re-simplifying coercions]
optOutCoercion env co already_optimised
| already_optimised = co -- Ditto
| otherwise = optCoercion opts empty_subst co
@@ -863,7 +964,8 @@ pushCastMCo env (MCo co) cont = pushCast env co cont
pushCast :: SimplEnv -> OutCoercion -> SimplCont -> SimplCont
pushCast env co cont
- = go co True cont
+ | isReflCo co = cont -- See Note [Optimising reflexivity]
+ | otherwise = go co True cont
where
-- ToDo: pushCast Refl (ApplylToVal arg1 (ApplyToVal arg2 ...))
@@ -872,7 +974,7 @@ pushCast env co cont
go co1 _ (CastIt { sc_co = co2, sc_cont = cont }) -- See Note [Optimising reflexivity] in GHC.Core.Opt.Simplify.Iteration
= go (mkTransCo co1 co2) False cont
-- False: (mkTransCo co1 co2) is not fully optimised
- -- See Note [Avoid re-simplifying coercions] in GHC.Core.Opt.Simplify.Iteration
+ -- See Note [Avoid re-simplifying coercions]
go co co_is_opt (ApplyToTy { sc_arg_ty = arg_ty, sc_cont = tail })
| Just (tyL, arg_ty', m_co') <- pushCoTyArg co arg_ty
@@ -887,21 +989,17 @@ pushCast env co cont
-- where co :: (s1->s2) ~ (t1->t2)
-- co1 :: t1 ~ s1
-- co2 :: s2 ~ t2
- go co co_is_opt cont@(ApplyToVal { sc_arg = arg, sc_env = arg_se
- , sc_cast = arg_mco, sc_cont = tail })
+ go co co_is_opt cont@(ApplyToVal { sc_arg = arg, sc_cont = tail })
| not co_is_opt
= -- pushCoValArg duplicates the coercion, so optimise first
go (optOutCoercion (zapSubstEnv env) co co_is_opt) True cont
| Just (tyL, m_co1, m_co2) <- pushCoValArg co
= {-#SCC "addCoerce-pushCoValArg" #-}
- -- Dealing with m_co1 is the entire reason for the sc_cast field
- -- of ApplyToVal; see Note [The sc_cast field of ApplyToVal]
- ApplyToVal { sc_arg = arg
- , sc_env = arg_se
- , sc_cast = arg_mco `mkTransMCo` m_co1
- , sc_cont = go_mco m_co2 True tail
- , sc_hole_ty = tyL }
+ -- See Note [The MOutCoercion field of ContEx]
+ cont { sc_arg = mkCloCastMCo arg m_co1
+ , sc_cont = go_mco m_co2 True tail
+ , sc_hole_ty = tyL }
go co co_is_opt cont
| isReflCo co = cont -- Having this at the end makes a huge
@@ -915,6 +1013,13 @@ pushCast env co cont
go_mco MRefl _ cont = cont
go_mco (MCo co) opt cont = go co opt cont
+mkCloCastMCo :: SimplClo -> MOutCoercion -> SimplClo
+-- See Note [The MOutCoercion field of ContEx]
+mkCloCastMCo clo MRefl = clo
+mkCloCastMCo (DoneId v) (MCo co) = DoneEx (mkCast (Var v) co) NotJoinPoint
+mkCloCastMCo (DoneEx e _) (MCo co) = DoneEx (mkCast e co) NotJoinPoint
+mkCloCastMCo (ContEx se e mco) (MCo co) = ContEx se e (mco `mkTransMCo` MCo co)
+
{-
************************************************************************
@@ -1228,19 +1333,17 @@ Wrinkles:
-}
-interestingArg :: SimplEnv -> StaticEnv -> CoreExpr -> ArgSummary
+interestingArg :: SimplEnv -> SimplClo -> ArgSummary
-- See Note [Interesting arguments]
-interestingArg env se e
- = case se of
- Simplified {} -> go env 0 e
- UnSimplified static_env -> go (static_env `setInScopeFromE` env) 0 e
+interestingArg env clo
+ = go_clo env 0 clo
where
+ go_clo _ n (DoneId v) = go_var n v
+ go_clo env n (DoneEx e _) = go (zapSubstEnv env) n e
+ go_clo env n (ContEx se e _) = go (se `setInScopeFromE` env) n e
+
-- n is # value args to which the expression is applied
- go env n (Var v)
- = case substId env v of
- DoneId v' -> go_var n v'
- DoneEx e _ -> go (zapSubstEnv env) n e
- ContEx se e _mco -> go (se `setInScopeFromE` env) n e
+ go env n (Var v) = go_clo env n (substId env v)
go _ _ (Lit l)
| isLitRubbish l = NonTrivArg -- See (IA3) in Note [Interesting arguments]
@@ -1739,15 +1842,15 @@ is a term (not a coercion) so we can't necessarily inline the latter in
the former.
-}
-preInlineUnconditionally
+preInlineLetUnconditionally
:: SimplEnv -> TopLevelFlag -> InId
- -> StaticEnv -> CoreExpr -> MOutCoercion -- The argument
+ -> InExpr -> SimplEnv -- These two go together
-> Maybe SimplEnv -- Returned env has extended substitution
-- Precondition: rhs satisfies the let-can-float invariant
-- See Note [Core let-can-float invariant] in GHC.Core
-- Reason: we don't want to inline single uses, or discard dead bindings,
-- for unlifted, side-effect-ful bindings
-preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
+preInlineLetUnconditionally env top_lvl bndr rhs rhs_env
| not pre_inline_unconditionally = Nothing
| not active = Nothing
| isTopLevel top_lvl && isDeadEndId bndr = Nothing -- Note [Top-level bottoming Ids]
@@ -1759,26 +1862,15 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
-- See Note [Stable unfoldings and preInlineUnconditionally]
| not (isInlinePragma inline_prag)
- , Just inl <- maybeUnfoldingTemplate unf = assertPpr (isReflMCo rhs_mco) (ppr bndr) $
- Just $! (extend_subst_with inl)
+ , Just inl <- maybeUnfoldingTemplate unf = Just $! (extend_subst_with inl)
| otherwise = Nothing
where
unf = idUnfolding bndr
- -- If the rhs is /not/ already simplified, extend the envt with ContEx, which captures
- -- the the lexical environment for us to restore in `simplInId`.
- -- If the rhs /is/ already simplified, then extend the envt with DoneEx;
- -- That makes `simplInId` call `simplOutExpr` which avoids re-simplifying
- -- the RHS.
- -- See Note [Avoid repeated simplification] in GHC.Core.Opt.Simplify.Iteration
- extend_subst_with inl_rhs
- = extendIdSubst env bndr $!
- case rhs_se of
- Simplified _ -> case rhs_mco of
- MRefl -> DoneEx inl_rhs NotJoinPoint -- Common case
- MCo co -> DoneEx (mkCast inl_rhs co) NotJoinPoint
- UnSimplified rhs_env -> ContEx rhs_env inl_rhs rhs_mco
+ -- The rhs is not yet simplified, so extend the envt with ContEx, which
+ -- captures the lexical environment for us to restore in `simplInId`.
+ extend_subst_with inl_rhs = extendIdSubst env bndr $! ContEx rhs_env inl_rhs MRefl
one_occ IAmDead = True -- Happens in ((\x.1) v)
one_occ OneOcc{ occ_n_br = 1
@@ -1794,39 +1886,6 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
-- See Note [pre/postInlineUnconditionally in gentle mode]
inline_prag = idInlinePragma bndr
--- Be very careful before inlining inside a lambda, because (a) we must not
--- invalidate occurrence information, and (b) we want to avoid pushing a
--- single allocation (here) into multiple allocations (inside lambda).
--- Inlining a *function* with a single *saturated* call would be ok, mind you.
--- || (if is_cheap && not (canInlineInLam rhs) then pprTrace "preinline" (ppr bndr <+> ppr rhs) ok else ok)
--- where
--- is_cheap = exprIsCheap rhs
--- ok = is_cheap && int_cxt
-
- -- int_cxt The context isn't totally boring
- -- E.g. let f = \ab.BIG in \y. map f xs
- -- Don't want to substitute for f, because then we allocate
- -- its closure every time the \y is called
- -- But: let f = \ab.BIG in \y. map (f y) xs
- -- Now we do want to substitute for f, even though it's not
- -- saturated, because we're going to allocate a closure for
- -- (f y) every time round the loop anyhow.
-
- -- canInlineInLam => free vars of rhs are (Once in_lam) or Many,
- -- so substituting rhs inside a lambda doesn't change the occ info.
- -- Sadly, not quite the same as exprIsHNF.
- canInlineInLam (Lit _) = True
- canInlineInLam (Cast e _) = canInlineInLam e
- canInlineInLam (Lam b e) = isRuntimeVar b || canInlineInLam e
- canInlineInLam (Tick t e) = not (tickishIsCode t) && canInlineInLam e
- canInlineInLam (Var v) = case idOccInfo v of
- OneOcc { occ_in_lam = IsInsideLam } -> True
- ManyOccs {} -> True
- _ -> False
- canInlineInLam _ = False
- -- not ticks. Counting ticks cannot be duplicated, and non-counting
- -- ticks around a Lam will disappear anyway.
-
early_phase =
case sePhase env of
SimplPhase p -> p /= FinalPhase
@@ -1857,6 +1916,97 @@ preInlineUnconditionally env top_lvl bndr rhs_se rhs rhs_mco
-- (Nor can we check for `exprIsExpandable rhs`, because that needs to look
-- at the non-existent unfolding for the `I# 2#` which is also floated out.)
+preInlineBetaUnconditionally
+ :: SimplEnv -> Levity -> InId
+ -> SimplClo -- The argument
+ -> Maybe SimplEnv -- Returned env has extended substitution
+-- Like preInlineLetUnconditionally, but for a beta-redex (\bndr. body) arg
+-- The argument may or may not be already simplified (DoneEx or ContEx)
+--
+-- The binder is a lambda binder and so lacks most IdInfo;
+-- see Note [Core binder IdInfo invariants]
+--
+-- Compared to preInlineLetUnconditionally we omit some checks:
+-- * The binder is never top-level, so no need for the
+-- Note [Top-level bottoming Ids] check, nor for `early_phase`
+-- * A lambda binder is never a join point, so no need for the
+-- Note [Do not inline exit join points] check
+-- * A lambda binder never has an inline pragma, a stable unfolding, or RULES
+-- (see `lamBndrInfoOK`), so no need to check activation, nor for
+-- Note [Stable unfoldings and preInlineUnconditionally]
+--
+-- And we add one extra one
+-- * Check the let-can-float invariant, and don't inline if not
+
+preInlineBetaUnconditionally env levity bndr arg
+ | assertPpr (lamBndrInfoOK bndr) (ppr bndr) $
+ not pre_inline_unconditionally = Nothing
+ | isCoVar bndr = Nothing -- Note [Do not inline CoVars unconditionally]
+ | not (one_occ (idOccInfo bndr)) = Nothing
+ | needs_case_binding = Nothing -- Enforce the let-can-float invariant
+ | otherwise = Just $! extendIdSubst env bndr arg
+ -- If the arg is not yet simplified, it is a ContEx, which captures the
+ -- lexical environment for us to restore in `simplInId`.
+ -- If the arg is already simplified, then it is a DoneEx;
+ -- That makes `simplInId` call `simplOutExpr` which avoids re-simplifying
+ -- the arg.
+ -- See Note [Avoid repeated simplification] in GHC.Core.Opt.Simplify.Iteration
+ where
+ pre_inline_unconditionally = sePreInline env
+
+ needs_case_binding = needsCaseBindingL levity rhs
+ -- Ok to test an InExpr or OutExpr in needsCaseBindingL because
+ -- exprOkForSpeculation is stable under simplification
+
+ rhs :: CoreExpr -- Can be InExpr or OutExpr, but here it does not matter
+ rhs = case arg of
+ ContEx _ e _ -> e -- InExpr
+ DoneEx e _ -> e -- OutExpr
+ DoneId v -> Var v -- OutExpr
+
+ one_occ IAmDead = True -- Happens in ((\x.1) v)
+ one_occ OneOcc{ occ_n_br = 1
+ , occ_in_lam = NotInsideLam } = True
+ one_occ OneOcc{ occ_n_br = 1
+ , occ_in_lam = IsInsideLam
+ , occ_int_cxt = IsInteresting } = canInlineInLam rhs
+ one_occ _ = False
+
+
+canInlineInLam :: CoreExpr -> Bool
+-- Be very careful before inlining inside a lambda, because (a) we must not
+-- invalidate occurrence information, and (b) we want to avoid pushing a
+-- single allocation (here) into multiple allocations (inside lambda).
+-- Inlining a *function* with a single *saturated* call would be ok, mind you.
+-- || (if is_cheap && not (canInlineInLam rhs) then pprTrace "preinline" (ppr bndr <+> ppr rhs) ok else ok)
+-- where
+-- is_cheap = exprIsCheap rhs
+-- ok = is_cheap && int_cxt
+
+ -- int_cxt The context isn't totally boring
+ -- E.g. let f = \ab.BIG in \y. map f xs
+ -- Don't want to substitute for f, because then we allocate
+ -- its closure every time the \y is called
+ -- But: let f = \ab.BIG in \y. map (f y) xs
+ -- Now we do want to substitute for f, even though it's not
+ -- saturated, because we're going to allocate a closure for
+ -- (f y) every time round the loop anyhow.
+
+ -- canInlineInLam => free vars of rhs are (Once in_lam) or Many,
+ -- so substituting rhs inside a lambda doesn't change the occ info.
+ -- Sadly, not quite the same as exprIsHNF.
+canInlineInLam (Lit _) = True
+canInlineInLam (Cast e _) = canInlineInLam e
+canInlineInLam (Lam b e) = isRuntimeVar b || canInlineInLam e
+canInlineInLam (Tick t e) = not (tickishIsCode t) && canInlineInLam e
+canInlineInLam (Var v) = case idOccInfo v of
+ OneOcc { occ_in_lam = IsInsideLam } -> True
+ ManyOccs {} -> True
+ _ -> False
+canInlineInLam _ = False
+ -- not ticks. Counting ticks cannot be duplicated, and non-counting
+ -- ticks around a Lam will disappear anyway.
+
{-
************************************************************************
* *
=====================================
testsuite/tests/simplCore/should_compile/T15445.stderr
=====================================
@@ -4,6 +4,7 @@ Rule fired: Int# -> Integer -> Int# (GHC.Internal.Bignum.Integer)
Rule fired: USPEC plusTwoRec @Int (T15445a)
Rule fired: USPEC $fShowList @Int (GHC.Internal.Show)
Rule fired: Class op >> (BUILTIN)
+Rule fired: Class op show (BUILTIN)
Rule fired: USPEC plusTwoRec @Int (T15445a)
Rule fired: Class op enumFromTo (BUILTIN)
Rule fired: eftInt (GHC.Internal.Enum)
@@ -11,7 +12,6 @@ Rule fired: Class op show (BUILTIN)
Rule fired: USPEC plusTwoRec @Int (T15445a)
Rule fired: Class op enumFromTo (BUILTIN)
Rule fired: eftInt (GHC.Internal.Enum)
-Rule fired: Class op show (BUILTIN)
Rule fired: eftIntList (GHC.Internal.Enum)
Rule fired: ># (BUILTIN)
Rule fired: ==# (BUILTIN)
=====================================
testsuite/tests/simplCore/should_compile/T27962.hs
=====================================
@@ -0,0 +1,16 @@
+{-# LANGUAGE MagicHash #-}
+
+module T27962 where
+
+import GHC.Exts
+
+newtype N = MkN Int
+
+-- After inlining myRun, we get (runRW# @LiftedRep @Int |> co) (\_ -> ...)
+-- and the simplifier pushes co into the argument of runRW#.
+-- #27962: the simplifier dropped that cast, yielding ill-typed Core
+myRun :: (State# RealWorld -> N) -> N
+myRun = coerce (runRW# :: (State# RealWorld -> Int) -> Int)
+{-# INLINE myRun #-}
+
+foo x = myRun (\_ -> MkN (x+1))
=====================================
testsuite/tests/simplCore/should_compile/T27963.hs
=====================================
@@ -0,0 +1,23 @@
+{-# OPTIONS_GHC -Wno-inline-rule-shadowing #-}
+
+module T27963 where
+
+-- This test is pretty delicate because it relies on a function
+-- that has BOTH a rule AND an INLINE pragma
+--
+-- See #27963 for some discussion
+--
+-- Still, it should (just) work
+
+import GHC.Exts
+
+newtype N = MkN Int
+
+f x = x
+{-# INLINE f #-}
+{-# RULES "f/42" forall x. f x = 42::Int #-}
+
+-- The rule "f/42" should fire in both h1 and h2
+h1,h2 :: N -> Int
+h1 n = f (coerce n) + 0
+h2 n = (coerce f) n + 0 -- #27963: the rule did not fire here
=====================================
testsuite/tests/simplCore/should_compile/T27963.stderr
=====================================
@@ -0,0 +1,2 @@
+Rule fired: f/42 (T27963)
+Rule fired: f/42 (T27963)
=====================================
testsuite/tests/simplCore/should_compile/all.T
=====================================
@@ -633,4 +633,3 @@ test('T27962', normal, compile, [''])
test('T27962a', [extra_files(['T27962.hs'])], multimod_compile, ['T27962', '-v0 -O'])
test('T27963', [grep_errmsg(r'f/42')], compile, ['-O -ddump-rule-firings'])
test('T27965', normal, compile, [''])
-
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/24d1ab3bb660abb34da658f76098323…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/24d1ab3bb660abb34da658f76098323…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/romes/async-rpc] 2 commits: interp: cancel requests if async exceptions while waiting for reply.
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
by Andrea Vezzosi (@trac-Saizan) 27 Oct '26
27 Oct '26
Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC
Commits:
7df65417 by Andrea Vezzosi at 2026-10-27T18:24:03+01:00
interp: cancel requests if async exceptions while waiting for reply.
- - - - -
9ee50b9f by Andrea Vezzosi at 2026-10-27T18:24:03+01:00
interp: do not terminate ext. interp. process on exception
`sendMessage` throwing an exception does not warrant terminating the external
interpreter process anymore, because communication can keep going no matter what.
`stopInterp` now resets the whole `Interp` state, so that a new
ExtInterpInstance can be later created and work correctly.
`withExtInterp` now calls `stopInterp` on a `PipeClosedException` and rethrows.
Fixes #18852
- - - - -
7 changed files:
- compiler/GHC/Runtime/Interpreter.hs
- compiler/GHC/Runtime/Interpreter/Process.hs
- compiler/GHC/Tc/Gen/Splice.hs
- libraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/Message/Async.hs
- libraries/ghci/GHCi/Server.hs
- libraries/ghci/GHCi/Server/Threads.hs
Changes:
=====================================
compiler/GHC/Runtime/Interpreter.hs
=====================================
@@ -105,7 +105,7 @@ import GHCi.Run
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
-import Control.Monad.Catch as MC (mask)
+import Control.Monad.Catch as MC (mask, handle, MonadThrow (..))
import Data.Binary
import Data.ByteString (ByteString)
import qualified Data.ByteString.Short as SBS
@@ -196,14 +196,16 @@ interpCmd interp msg = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> run msg -- Just run it directly
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst ->
+ ExternalInterp ext -> withExtInterp interp ext $ \inst ->
sendMessage inst msg
-withExtInterp :: ExceptionMonad m => ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
-withExtInterp ext action = case ext of
+withExtInterp :: ExceptionMonad m => Interp -> ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a
+withExtInterp interp ext action = stopInterpOnPipeClosed $ case ext of
ExtJS i -> withJSInterp i action
ExtWasm i -> withWasmInterp i action
ExtIServ i -> withIServ i action
+ where
+ stopInterpOnPipeClosed = MC.handle $ \ (e :: PipeClosedException) -> liftIO (stopInterp interp) >> MC.throwM e
withExtInterpStatus :: ExtInterp -> (forall d. ExtInterpStatusVar d -> m a) -> m a
withExtInterpStatus ext action = case ext of
@@ -642,6 +644,9 @@ stopInterp interp = case interpInstance interp of
else sendMessage i Shutdown
-- We asked the server to shutdown, so we will also shutdown any communication.
closePipe (interpPipe (instProcess i))
+ modifyMVar_ (loader_state (interpLoader interp)) (const $ pure Nothing)
+ purgeInterpSymbolCache (interpSymbolCache interp)
+ modifyMVar_ (interpStringCache interp) (const $ pure emptyFsEnv)
pure InterpPending
-- -----------------------------------------------------------------------------
=====================================
compiler/GHC/Runtime/Interpreter/Process.hs
=====================================
@@ -12,13 +12,10 @@ import GHC.Prelude
import GHC.Runtime.Interpreter.Types
import GHCi.Message
-import GHC.IO (catchException)
import GHC.Utils.Panic
import GHC.Utils.Exception as Ex
import Data.Binary
-import System.Exit
-import System.Process
import Control.Concurrent
import Data.IORef
import Control.Concurrent.STM
@@ -34,7 +31,6 @@ import Control.Concurrent.STM
sendMessage :: Binary a => ExtInterpInstance d -> Message a -> IO a
sendMessage i msg =
remoteCall (interpPipe proc) msg
- `catchException` \(e :: SomeException) -> handleInterpProcessFailure proc e
where
proc = instProcess i
@@ -64,7 +60,7 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
where
proc = instProcess i
handleOne mbox doneRef = do
- handleRemoteTHCall (interpPipe proc) mbox $ \req -> do
+ handleRemoteTHCall (interpPipe proc) mbox $ \_setCancel req -> do
(stop, res) <- reqHandler req
writeIORef doneRef stop
return res
@@ -77,18 +73,6 @@ sendTHComputation i msg reqHandler = mask $ \restore -> do
else loop
in loop
-handleInterpProcessFailure :: InterpProcess -> SomeException -> IO a
-handleInterpProcessFailure i e = do
- let hdl = interpHandle i
- ex <- getProcessExitCode hdl
- case ex of
- Just (ExitFailure n) ->
- throwIO (InstallationError ("External interpreter terminated (" ++ show n ++ ")"))
- _ -> do
- terminateProcess hdl
- _ <- waitForProcess hdl
- throw e
-
data CancelTHHandler = CancelTHHandler deriving Show
instance Exception CancelTHHandler where
fromException = asyncExceptionFromException
=====================================
compiler/GHC/Tc/Gen/Splice.hs
=====================================
@@ -1160,7 +1160,7 @@ runRemoteModFinalizers (ThModFinalizers finRefs) = do
runQinTcM $ sequence_ qs
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
tcg <- getGblEnv
th_state <- readTcRef (tcg_th_remote_state tcg)
case th_state of
@@ -1817,7 +1817,7 @@ runTH ty fhv = do
return r
#endif
- ExternalInterp ext -> withExtInterp ext $ \inst -> do
+ ExternalInterp ext -> withExtInterp interp ext $ \inst -> do
-- Run it on the server. For an overview of how TH works with
-- Remote GHCi, see Note [Remote Template Haskell] in
-- libraries/ghci/GHCi/TH.hs.
=====================================
libraries/ghci/GHCi/Message.hs
=====================================
@@ -27,7 +27,7 @@ module GHCi.Message
, Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe
, remoteCall, remoteTHCall
, handleRemoteCall, handleRemoteTHCall, writeReply
- , RemoteProcessException(..), PipeReadClosedException(..)
+ , RemoteProcessException(..), PipeClosedException(..)
, InboxId(..)
, newMailbox
@@ -666,12 +666,12 @@ remoteCall pipe msg = either throwIO (pure . runGet get)
-- | Handle a single 'Message' request on the interpreter and reply to the host
handleRemoteCall :: Pipe
-> (Msg -> IO Msg) {-^ Msg hook -}
- -> (forall a. (Show a, Binary a) => WriteReply a %1 -> Message a -> IO r)
+ -> (forall a. (Show a, Binary a) => WriteReply a %1 -> (IO () -> IO ()) -> Message a -> IO r)
-> IO r
handleRemoteCall pipe hook k =
- handleAsyncReq pipe getMessage Nothing \(WriteReply writer) msg0 -> IO \s0 ->
+ handleAsyncReq pipe getMessage Nothing \(WriteReply writer) sa msg0 -> IO \s0 ->
case hook msg0 |#> s0 of -- unIO for linearity checking
- (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) msg |#> s1
+ (# s1, Msg msg #) -> k (WriteReply \x -> writer (fmap put x)) sa msg |#> s1
-- | Send a 'THMessage' request to the host and wait for a reply from the host
-- to the interpreter.
@@ -687,9 +687,9 @@ remoteTHCall pipe ii msg = either throwIO (pure . runGet get)
-- Symmterically to 'remoteTHCall', we always want to specify the 'InboxId' for
-- RunTH "reverse requests"
handleRemoteTHCall
- :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => THMessage a -> IO a) -> IO ()
+ :: Pipe -> InboxId -> (forall a. (Show a, Binary a) => (IO () -> IO ()) -> THMessage a -> IO a) -> IO ()
handleRemoteTHCall pipe ii k =
- handleAsyncReq pipe getTHMessage (Just ii) \writer (THMsg msg) -> IO \s0 ->
- case k msg |#> s0 of -- unIO for linearity checking
+ handleAsyncReq pipe getTHMessage (Just ii) \writer sa (THMsg msg) -> IO \s0 ->
+ case k sa msg |#> s0 of -- unIO for linearity checking
(# s1, a #) -> writeReply writer (put a) |#> s1
=====================================
libraries/ghci/GHCi/Message/Async.hs
=====================================
@@ -7,7 +7,7 @@ module GHCi.Message.Async
(
-- * Pipes
Pipe(..), mkPipeFromHandles, mkPipeFromContinuations
- , closePipe, PipeReadClosedException(..)
+ , closePipe, PipeClosedException(..)
-- * Interface for async bidirectional RPC
, remoteAsyncReq, handleAsyncReq
@@ -145,13 +145,13 @@ closePipe p@Pipe{..} = do
unless writeAlreadyClosed
(throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
--- | We've closed the pipe read end on this process
-data PipeReadClosedException = PipeReadClosedException deriving Show
-instance Exception PipeReadClosedException
+data PipeClosedException
+ = PipeReadClosedException -- ^ We've closed the pipe read end on this process
+
+ | PipeWriteClosedException -- ^ We've closed the pipe write end on this process
+ deriving Show
+instance Exception PipeClosedException
--- | We've closed the pipe write end on this process
-data PipeWriteClosedException = PipeWriteClosedException deriving Show
-instance Exception PipeWriteClosedException
--------------------------------------------------------------------------------
-- ** Pipe thread-safe read/write
@@ -160,14 +160,19 @@ instance Exception PipeWriteClosedException
-- and identify whether they are requests or replies.
writePipe :: Pipe -> IddMsg -> IO ()
-writePipe Pipe{..} msg = do
- var <- newEmptyTMVarIO
- atomically $ do
+writePipe p msg = do
+ var <- atomically $ writePipeAsync p msg
+ either throwIO pure =<< atomically (takeTMVar var)
+
+writePipeAsync :: Pipe -> IddMsg -> STM (TMVar (Either SomeException ()))
+writePipeAsync Pipe{..} msg = do
+ var <- newEmptyTMVar
readTVar pipeWriteClosed >>= \case
True -> throwSTM PipeWriteClosedException
False -> pure ()
writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
- either throwIO pure =<< atomically (takeTMVar var)
+ pure var
+
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
-- functions should wait for the matching reply to be delivered asynchronously.
@@ -239,14 +244,18 @@ remoteAsyncReq pipe msg ii = do
-- Write request
writePipe pipe (Request uq ii (runPut msg))
`onException`
- -- the req wasn't done, or no one will be reading the reply:
- -- in either case, drop it from the pending map.
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
+ -- the req wasn't done, or no one will be reading the reply:
+ -- in either case, drop it from the pending map.
+ atomically (modifyTVar pendingReplies (IntMap.delete uq))
-- Block waiting for reply
atomically (takeTMVar wv)
`onException`
- atomically (modifyTVar pendingReplies (IntMap.delete uq))
+ atomically (do
+ modifyTVar pendingReplies (IntMap.delete uq)
+ -- we don't wait for Cancel to be written on the pipe,
+ -- and won't get a reply: nowhere to report it anyway.
+ writePipeAsync pipe (Cancel uq))
-- | Receive a single asynchronous request and send a matching reply
-- asynchronously (the handler may fork a computation that eventually replies
@@ -258,7 +267,7 @@ remoteAsyncReq pipe msg ii = do
-- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
--
-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
-handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
+handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> (IO () -> IO ()) -> a -> IO r) -> IO r
handleAsyncReq pipe getit mii k = mask $ \restore -> do
let am = pipeAsyncMngr pipe
ack <- newEmptyMVar
@@ -274,7 +283,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
Nothing -> tryReadTQueue $ requestsQueue am
Just ii -> tryReadTQueue =<< getMailbox am ii
case taken of
- Just req -> pure (Just req)
+ Just req@(uq,_) -> fetchedRequest am uq >> pure (Just req)
Nothing | isClosed -> pure Nothing -- empty and closed
| otherwise -> retry -- wait for msg
@@ -282,9 +291,9 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
Nothing -> throwIO PipeReadClosedException -- can't expect reqs
Just (uq, bs) -> do
let reply p = do
- writePipe pipe (Reply uq (bimap toSerializableException runPut p))
+ b <- atomically $ doneHandling am uq
+ when b $ writePipe pipe (Reply uq (bimap toSerializableException runPut p))
putMVar ack ()
-
-- Run the continuation which must reply exactly once with 'writeReply'
case runGetOrFail getit bs of
Left (_, _, err) -> do
@@ -292,7 +301,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
reply (Left (toException exc))
throwIO exc
Right (_, _, it) ->
- restore (k (WriteReply reply) it)
+ restore (k (WriteReply reply) (setActive am uq) it)
-- If there's an exception before the reply is sent, reply with an error instead.
`catch` \(e :: SomeException) -> do
@@ -306,6 +315,7 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do
reply (Left e)
throwIO e
+
-- | 'handleSyncReq' failed to decode an incoming request payload using the given @'Get' a@
newtype DecodeAsyncReqException = DecodeAsyncReqException String deriving Show
instance Exception DecodeAsyncReqException
@@ -347,6 +357,28 @@ getMailbox am (InboxId ii) = do
-- ** Async matching "internals" -----------------------------------------------
+-- | Tracks handling status of a received 'Request'. The values are states of a
+-- tiny state machine used to coordinate cancelations.
+--
+-- 'handleAsyncReq' will pop a request from the appropriate queue, setting the
+-- status to `Fetched`. Then one of two things can happen first:
+-- - we receive a 'Cancel', then status is set to `Canceling` so the cancel
+-- action will run when set.
+-- - handler sets a cancel action, then reception of a `Cancel` runs that
+-- action and deletes the RequestStatus.
+--
+-- If RequestStatus is `Canceling` or absent, then a `Reply` won't be sent.
+--
+-- The two possible transition paths:
+-- requestsQueue -> Fetched -> Active cancel -> gone
+-- requestsQueue -> Fetched -> Canceling -> gone
+
+data RequestStatus
+ = Fetched -- ^ 'Fetched' from a request queue, to pass to handler.
+ | Active (IO ()) -- ^ Handler set a cancel action, and has not sent a reply
+ -- yet. The cancel action must not block.
+ | Canceling -- ^ `Cancel` received while status is still `Fetched`.
+
data AsyncManager = AsyncManager
{ pendingReply :: !(TVar (IntMap (TMVar (Either SomeException LB.ByteString))))
-- ^ When a request is sent, keep track that we're expecting a reply. The
@@ -357,6 +389,9 @@ data AsyncManager = AsyncManager
-- /without/ a specific 'InboxId', queue it here. A request handler will
-- pop the next request from it. (Nothing except the 'drainPipe' thread
-- should read from the pipe). Read with @'handleAsyncReq' p get Nothing@.
+ , cancelStatus :: !(TVar (IntMap RequestStatus))
+ -- ^ When a `Cancel uq` is received, we lookup `uq` in this map and call the
+ -- callback if present.
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
-- ^ Requests for a specific 'InboxId' that can be read using
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
@@ -389,15 +424,21 @@ type CorrelationId = Int
-- exclusively.
data IddMsg = Request !CorrelationId !(Maybe InboxId) LB.ByteString
| Reply !CorrelationId (Either SerializableException LB.ByteString)
+ | Cancel !CorrelationId -- ^ Technically a Request, but no Reply
+ -- is expected, the CorrelationId is the
+ -- one of the Request being canceled.
instance Binary IddMsg where
put (Request i ii bs) = putWord8 0 >> put i >> put ii >> put bs
put (Reply i bs) = putWord8 1 >> put i >> put bs
+ put (Cancel i) = putWord8 2 >> put i
+
get = do
b <- getWord8
case b of
0 -> Request <$> get <*> get <*> get
1 -> Reply <$> get <*> get
+ 2 -> Cancel <$> get
_ -> fail $ "impossible: Binary IddMsg - " ++ show b
newAsyncManager :: Pipe -> IO AsyncManager
@@ -405,6 +446,7 @@ newAsyncManager pipe = do
asyncFreshId <- newIORef 0
pendingReply <- newTVarIO IntMap.empty
requestsQueue <- newTQueueIO
+ cancelStatus <- newTVarIO IntMap.empty
mailboxQueues <- newTVarIO IntMap.empty
writeQueue <- newTQueueIO
@@ -430,41 +472,85 @@ drainPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPendings where
loop = do
readPipe p >>= \case
- -- Receive a reply to a pending notification
- Just (Reply uq bs) -> do
- atomically $ do
- m <- readTVar (pendingReply am)
- case IntMap.lookup uq m of
- Nothing ->
- -- the req was dropped from pending bc there was an exception, and no
- -- one's listening any more. the reply is ignored
- pure ()
- Just wv -> do
- writeTVar (pendingReply am) (IntMap.delete uq m)
- -- signal matching request
- putTMVar wv (bimap (toException . RemoteProcessException) id bs)
- loop
- -- Receive a request and queue it
- Just (Request uq mbi bs) -> do
- atomically $ case mbi of
- Nothing ->
- -- "Global" request
- writeTQueue (requestsQueue am) (uq,bs)
- Just ii -> do
- -- "Specific mailbox" request
- q <- getMailbox am ii
- writeTQueue q (uq,bs)
+ Just msg -> do
+ handleMsg msg
loop
-- EOF on read handle
Nothing -> do
-- clean up pipe then exit loop
replyToPendings (toException PipeReadClosedException)
pure ()
+ handleMsg = \case
+ -- Receive a reply to a pending notification
+ Reply uq bs -> do
+ atomically $ do
+ m <- readTVar (pendingReply am)
+ case IntMap.lookup uq m of
+ Nothing ->
+ -- the req was dropped from pending bc there was an exception, and no
+ -- one's listening any more. the reply is ignored
+ pure ()
+ Just wv -> do
+ writeTVar (pendingReply am) (IntMap.delete uq m)
+ -- signal matching request
+ putTMVar wv (bimap (toException . RemoteProcessException) id bs)
+ -- Receive a request and queue it
+ Request uq mbi bs -> do
+ atomically $ case mbi of
+ Nothing ->
+ -- "Global" request
+ writeTQueue (requestsQueue am) (uq,bs)
+ Just ii -> do
+ -- "Specific mailbox" request
+ q <- getMailbox am ii
+ writeTQueue q (uq,bs)
+ Cancel uq -> cancelRequest am uq
replyToPendings e = atomically $ failPendingReplies p e
am = pipeAsyncMngr p
+cancelRequest :: AsyncManager -> CorrelationId -> IO ()
+cancelRequest am uq = join . atomically $ do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure (pure ())
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq Canceling cs) >> pure (pure ())
+ Just (Active cancel) -> writeTVar (cancelStatus am) (IntMap.delete uq cs) >> pure cancel
+ Just Canceling -> pure (pure ())
+
+-- | Returns @True@ when the handler should write its 'Reply'.
+doneHandling :: AsyncManager -> CorrelationId -> STM Bool
+doneHandling am uq = do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure False
+ Just s -> do
+ writeTVar (cancelStatus am) $ IntMap.delete uq cs
+ case s of
+ Canceling -> pure False
+ _ -> pure True
+
+setActive :: AsyncManager -> CorrelationId -> IO () -> IO ()
+setActive am uq cancel = join . atomically $ do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> pure (pure ()) -- reply already received.
+ Just Fetched -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
+ Just (Active _) -> writeTVar (cancelStatus am) (IntMap.insert uq (Active cancel) cs) >> pure (pure ())
+ Just Canceling -> pure cancel
+
+fetchedRequest :: AsyncManager -> CorrelationId -> STM ()
+fetchedRequest am uq = do
+ cs <- readTVar (cancelStatus am)
+ case IntMap.lookup uq cs of
+ Nothing -> writeTVar (cancelStatus am) (IntMap.insert uq Fetched cs)
+ Just Fetched -> error "impossible"
+ Just (Active _) -> error "impossible"
+ Just Canceling -> error "impossible"
+
+
+
{-
Note [Exceptions during serialization]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -553,7 +639,7 @@ runWriteReply (WriteReply r) = r
(|#>) (IO fs) s = fs s
(%>>=) :: (IO a) -> (a -> IO b) %1 -> IO b
-(%>>=) = \ m f -> IO (\s0 ->
+(%>>=) = \ m -> \ f -> IO (\s0 ->
case m |#> s0 of -- unIO for linearity checking
(# s1, x #) -> f x |#> s1)
infixl 1 %>>=
=====================================
libraries/ghci/GHCi/Server.hs
=====================================
@@ -66,13 +66,13 @@ servWithCustom
-> IO ()
servWithCustom verbose hook pipe restore customHandler = do
ref <- newIORef False
- withThreadManager $ \mngr ->
+ withThreadManager $ \mngr -> do
loop ref mngr `finally` closePipe pipe
where
loop doneRef mngr = do
when verbose $ trace "reading pipe..."
- handleRemoteCall pipe hook $ \wr (msg :: Message a) ->
+ handleRemoteCall pipe hook $ \wr setCancel (msg :: Message a) ->
discardCtrlC %>>= \_ ->
when verbose (trace ("msg: " ++ (show msg))) %>>= \_ ->
@@ -81,10 +81,14 @@ servWithCustom verbose hook pipe restore customHandler = do
CustomMessage tag payload -> handleCustom tag payload %>>= writeReply wr
Shutdown -> writeIORef doneRef True >>
reply () %>>= writeReply wr
- RunTH ii st q ty loc -> spawnFinally mngr (runWriteReply wr)
+ RunTH ii st q ty loc -> unsafe_linear restore
+ (spawnFinally mngr (runWriteReply wr)
(wrapRunTH ii (runTH pipe ii st q ty loc))
- RunModFinalizers ii st qs -> spawnFinally mngr (runWriteReply wr)
+ (setCancel . void . forkIO . killThread))
+ RunModFinalizers ii st qs -> unsafe_linear restore
+ (spawnFinally mngr (runWriteReply wr)
(wrapRunTH ii (runModFinalizerRefs pipe ii st qs))
+ (setCancel . void . forkIO . killThread))
_other -> run msg >>= reply %>>= writeReply wr
isDone <- readIORef doneRef
=====================================
libraries/ghci/GHCi/Server/Threads.hs
=====================================
@@ -41,8 +41,8 @@ withThreadManager k = do
-- | Linear and resource-safe because 'ThreadManager' is guaranteed to wait or
-- cancel all threads.
-spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> IO ()
-spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
+spawnFinally :: ThreadManager -> (Either SomeException a -> IO ()) %1 -> IO a -> (ThreadId -> IO b) -> IO b
+spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> \k ->
forkFinally
( mask $ \unmask -> do
tid <- myThreadId
@@ -57,6 +57,7 @@ spawnFinally ThreadManager{..} = unsafe_linear \fin -> \action -> void $
else throwIO ThreadKilled
)
( \ea -> fin ea )
+ >>= k
unsafe_linear :: (a -> b) %1 -> (a %1 -> b)
unsafe_linear = Unsafe.unsafeCoerce (Unsafe.unsafeCoerce :: (a -> b) -> (a %1 -> b))
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/fc2470dd36448ef3e8a12683fe9e6f…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/fc2470dd36448ef3e8a12683fe9e6f…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/dcoutts/signals-cleanup] 4 commits: Redo the scheme for special fds in the poll and selectbis I/O managers
by Duncan Coutts (@dcoutts) 27 Oct '26
by Duncan Coutts (@dcoutts) 27 Oct '26
27 Oct '26
Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC
Commits:
5b0be3ef by Duncan Coutts at 2026-10-27T17:11:50+00:00
Redo the scheme for special fds in the poll and selectbis I/O managers
At the moment we only have one special fd for these I/O managers: the
pipe (or eventfd) for interrupting them when they're sleeping. We're
about to gain a second for signal handling.
The existing scheme was rather ad-hoc and hard to extend. It was
already complicated by the fact that the interrupt fd is optional. The
signal fd will also be optional in the sense that cap 0 will have it but
cap >0 will not.
So the new scheme is that we add special aiop entries into the
aiop_table, and for the most part we can treat these uniformly with
other aiops. But the action we take for them if/when they are ready is
handled specially.
This seems much cleaner and will be easy to extend for signals.
- - - - -
3f545d73 by Duncan Coutts at 2026-10-27T17:11:50+00:00
Use the signal self-pipe trick for the poll and selectbis I/O managers
So now the low level signal handler delivers signals into a pipe that is
monitored by the I/O manager (on cap 0). The I/O manager then dispatches
the handler (starting a Haskell thread).
There are two important reasons for this change:
1. This method does not have any windows of time where signals can be
missed for arbitrary periods of time. The previous method has a race
where a signal delivered just before the I/O manager calls
poll/select will not notice the signal until some unrelated event
causes poll/select to return. See issue #27540. The new method
ensures that a fd is ready and so poll/select will notice
immediately (if the signal was delivered prior to the call to
poll/select) or later.
2. The existing method relies on temporarily masking signals for
concurrency safety which cannot work reliably in a multi-threaded
process. Fixing this is a necessary pre-requisite to enable these
in-RTS I/O managers to work in the threaded RTS. The issue is that
startPendingSignalHandlers and the low level signal handler share the
pending_handler_buf data structure, and so startPendingSignalHandlers
has to mask delivery of signals to avoid the shared data being
mutated concurrently. But masking signals in a multi-threaded process
is not generally effective because the signal can still be delivered
in any thread
Document the self-pipe trick in a Note.
This fixes issue #27540.
- - - - -
ddf14de2 by Duncan Coutts at 2026-10-27T17:11:50+00:00
Move the legacy user signal handler into legacy select I/O manager
We do not convert the legacy select I/O manager to use the self-pipe
trick. It keeps the existing code, but since it is now the only user of
it, then that code can move from being a shared utility to live with the
select I/O manager.
There are some minor modifications to the code moved so that it is not
entangled with other remaining shared signal utils.
- - - - -
d1925b83 by Duncan Coutts at 2026-10-27T17:11:50+00:00
Deprecate (un)blockUserSignals for external RTS users
Encourage external users to stop using these actions.
- - - - -
12 changed files:
- rts/IOManager.c
- rts/IOManagerInternals.h
- rts/RtsSymbols.c
- rts/include/rts/BlockSignals.h
- rts/include/rts/storage/Closures.h
- rts/posix/IOManSignals.c
- rts/posix/IOManSignals.h
- rts/posix/Poll.c
- rts/posix/Poll.h
- rts/posix/Select.c
- rts/posix/SelectBis.c
- rts/posix/SelectBis.h
Changes:
=====================================
rts/IOManager.c
=====================================
@@ -462,6 +462,20 @@ void startIOManager(void)
switch (iomgr_type) {
+#if defined(IOMGR_ENABLED_SELECTBIS)
+ case IO_MANAGER_SELECTBIS:
+ for (unsigned int i = 0; i < n_capabilities; i++) {
+ startCapabilityIOManagerSelectBis(getCapability(i)->iomgr);
+ }
+ break;
+#endif
+#if defined(IOMGR_ENABLED_POLL)
+ case IO_MANAGER_POLL:
+ for (unsigned int i = 0; i < n_capabilities; i++) {
+ startCapabilityIOManagerPoll(getCapability(i)->iomgr);
+ }
+ break;
+#endif
#if defined(IOMGR_ENABLED_MIO_POSIX)
case IO_MANAGER_MIO_POSIX:
/* Posix implementation in posix/Signals.c
@@ -514,6 +528,18 @@ restartIOManager(CapIOManager *iomgr, Capability **pcap)
{
switch (iomgr_type) {
+#if defined(IOMGR_ENABLED_SELECTBIS)
+ case IO_MANAGER_SELECTBIS:
+ startCapabilityIOManagerSelectBis(iomgr);
+ break;
+#endif
+
+#if defined(IOMGR_ENABLED_POLL)
+ case IO_MANAGER_POLL:
+ startCapabilityIOManagerPoll(iomgr);
+ break;
+#endif
+
#if defined(IOMGR_ENABLED_MIO_POSIX)
case IO_MANAGER_MIO_POSIX:
/* Posix implementation in posix/Signals.c
@@ -530,8 +556,6 @@ restartIOManager(CapIOManager *iomgr, Capability **pcap)
break;
#endif
/* The IO_MANAGER_SELECT needs no initialisation */
- /* The IO_MANAGER_SELECTBIS needs no initialisation */
- /* The IO_MANAGER_POLL needs no initialisation */
/* No impl for any of the Windows I/O managers, since no forking. */
default:
=====================================
rts/IOManagerInternals.h
=====================================
@@ -76,6 +76,9 @@ struct _CapIOManager {
/* AIOP and timeout collections shared by several I/O manager impls */
ClosureTable aiop_table;
StgTimeoutQueue *timeout_queue;
+
+ /* The number of special fds at the start of the aiop table. */
+ int n_special_fds;
#endif
#if defined(IOMGR_ENABLED_SELECTBIS)
@@ -83,11 +86,8 @@ struct _CapIOManager {
#endif
#if defined(IOMGR_ENABLED_POLL)
- /* Auxiliary table with size and indexes matching the aiop_table. This is
- * aliased to the tail of the full poll table, which has a head entry for
- * the wakeup_fd_r above, so we can also poll that fd.
- */
- struct pollfd *aiop_poll_table, *full_poll_table;
+ /* Auxiliary table with size and indexes matching the aiop_table. */
+ struct pollfd *aiop_poll_table;
#endif
#if defined(IOMGR_ENABLED_WIN32_LEGACY)
@@ -113,6 +113,17 @@ struct _CapIOManager {
*/
struct _CapIOManagerShared {
+#if defined(IOMGR_ENABLED_POLL) \
+ || defined(IOMGR_ENABLED_SELECTBIS)
+#if defined(RTS_USER_SIGNALS)
+ /* Pipe FDs for delivering posix signals to the I/O manager.
+ * The signal number and siginfo_t are written to the write end by the
+ * low-level signal handler, and read by the I/O manager on capability 0.
+ */
+ int signal_fd_r, signal_fd_w;
+#endif
+#endif
+
#if defined(IOMGR_ENABLED_MIO_POSIX)
/* The timer manager control pipe (write end). This is the pipe into which
* we will send our signals.
=====================================
rts/RtsSymbols.c
=====================================
@@ -1233,6 +1233,9 @@ RTS_SYMBOLS_PRIM
(void*)(&(xxx)), strength, ty },
+#pragma GCC diagnostic push
+/* Don't warn about exporting deprecated RTS functions. */
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/* Initialize (if not already initialized) and return an array of symbols with stuff from the RTS. */
void initLinkerRtsSyms (StrHashTable *symhash) {
@@ -1270,6 +1273,7 @@ void initLinkerRtsSyms (StrHashTable *symhash) {
IF_DEBUG(linker, debugBelch("done with built-in RTS symbols\n"));
}
+#pragma GCC diagnostic pop
// Note [Extra RTS symbols]
=====================================
rts/include/rts/BlockSignals.h
=====================================
@@ -33,13 +33,17 @@
* This is exported from the RTS but should no longer be used. It is likely to
* be removed in future.
*/
-void blockUserSignals(void);
+void blockUserSignals(void)
+ __attribute__((deprecated("blockUserSignals should no longer be used, "
+ "use pthread_sigmask or similar.")));
/*
* Function: unblockUserSignals()
*
* The inverse of blockUserSignals(); re-enable the deliver of console events.
*/
-void unblockUserSignals(void);
+void unblockUserSignals(void)
+ __attribute__((deprecated("unblockUserSignals should no longer be "
+ "used, use pthread_sigmask or similar.")));
#endif
=====================================
rts/include/rts/storage/Closures.h
=====================================
@@ -722,14 +722,16 @@ typedef struct {
------------------------------------------------------------------------- */
union NotifyCompletion {
- StgTSO *tso;
- StgMVar *mvar;
- StgTVar *tvar;
+ StgTSO *tso;
+ StgMVar *mvar;
+ StgTVar *tvar;
+ StgClosure *unused;
};
enum NotifyCompletionType {
- NotifyTSO = 0, // thread-synchronous I/O
- NotifyMVar = 1, // async I/O with MVar notification
- NotifyTVar = 2 // async I/O with TVar notification
+ NotifyTSO = 0, // thread-synchronous I/O
+ NotifyMVar = 1, // async I/O with MVar notification
+ NotifyTVar = 2, // async I/O with TVar notification
+ NotifyInternal = 3 // used for special I/O manager-internal notification
};
/* A node in the leftist heap. */
@@ -873,6 +875,10 @@ typedef struct {
// For failed outcomes, this is the error code.
uint32_t error;
+
+ // For notify_type NotifyInternal, this is available for internal
+ // use by individual I/O managers.
+ uint32_t internal;
};
// Note that because we use fixed size Ctypes here then the size in words
=====================================
rts/posix/IOManSignals.c
=====================================
@@ -92,76 +92,6 @@ more_handlers(int sig)
nHandlers = sig + 1;
}
-#if !defined(THREADED_RTS)
-
-#define N_PENDING_HANDLERS 16
-
-siginfo_t pending_handler_buf[N_PENDING_HANDLERS];
-siginfo_t *next_pending_handler = pending_handler_buf;
-
-static inline bool signals_pending(void) {
- return (next_pending_handler != pending_handler_buf);
-}
-
-#endif /* THREADED_RTS */
-
-/* -----------------------------------------------------------------------------
- * Low-level signal handler
- *
- * Places the requested handler on a stack of pending handlers to be
- * started up at the next context switch.
- * -------------------------------------------------------------------------- */
-
-#if !defined(THREADED_RTS)
-void
-generic_handler(int sig USED_IF_THREADS,
- siginfo_t *info,
- void *p STG_UNUSED)
-{
-
- /* Can't call allocate from here. Probably can't call malloc
- either. However, we have to schedule a new thread somehow.
-
- It's probably ok to request a context switch and allow the
- scheduler to start the handler thread, but how do we
- communicate this to the scheduler?
-
- We need some kind of locking, but with low overhead (i.e. no
- blocking signals every time around the scheduler).
-
- Signal Handlers are atomic (i.e. they can't be interrupted), and
- we can make use of this. We just need to make sure the
- critical section of the scheduler can't be interrupted - the
- only way to do this is to block signals. However, we can lower
- the overhead by only blocking signals when there are any
- handlers to run, i.e. the set of pending handlers is
- non-empty.
- */
-
- /* We use a stack to store the pending signals. We can't
- dynamically grow this since we can't allocate any memory from
- within a signal handler.
-
- Hence unfortunately we have to bomb out if the buffer
- overflows. It might be acceptable to carry on in certain
- circumstances, depending on the signal.
- */
-
- memcpy(next_pending_handler, info, sizeof(siginfo_t));
-
- next_pending_handler++;
-
- // stack full?
- if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
- errorBelch("too many pending signals");
- stg_exit(EXIT_FAILURE);
- }
-
- interruptCapability(&MainCapability);
-
-}
-#endif /* THREADED_RTS */
-
/* -----------------------------------------------------------------------------
* Blocking/Unblocking of the user signals
* -------------------------------------------------------------------------- */
@@ -267,34 +197,6 @@ installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
* Creating new threads for signal handlers.
* -------------------------------------------------------------------------- */
-#if !defined(THREADED_RTS)
-void
-startPendingSignalHandlers(Capability *cap)
-{
- siginfo_t *info;
- int sig;
-
- if (!signals_pending()) return;
-
- blockUserSignals();
-
- while (next_pending_handler != pending_handler_buf) {
-
- next_pending_handler--;
-
- sig = next_pending_handler->si_signo;
- if (signal_handlers[sig] == STG_SIG_DFL) {
- continue; // handler has been changed.
- }
-
- info = next_pending_handler;
- startSignalHandler(cap, sig, info);
- }
-
- unblockUserSignals();
-}
-#endif
-
void startSignalHandler(Capability *cap, int sig, siginfo_t *info)
{
/* Allocate a copy. This is freed by runHandler. */
@@ -321,6 +223,43 @@ void startSignalHandler(Capability *cap, int sig, siginfo_t *info)
setThreadLabel(cap, t, "signal handler thread");
}
+/* Note [Non-racy signal handling using the self-pipe trick]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ *
+ * Reliably picking up unix/posix signals promptly is surprisingly tricky!
+ * The standard problem is that there is a race between signal delivery and
+ * waiting/blocking in something like select/poll/epoll_wait. The problem is
+ * that if one checks for a signal having been delivered (e.g. by looking at a
+ * shared variable set by the signal handler) just before calling select/poll
+ * then there's a time window between that point and calling select/poll when
+ * a signal could be delivered and then not noticed until select/poll returns.
+ * Once we've called select/poll then it can return EINTR upon signal arrival,
+ * but the window before remains a problem.
+ *
+ * One solution is to use pselect/ppoll: mask signals, then do a final check
+ * for signals having been delivered, then call pselect/ppoll to unmask
+ * signals (atomically) for the duration of the wait. This is fine in a single
+ * threaded process. In a multi-threaded process, masking signals doesn't work.
+ * See Note [pthread_sigmask and the ineffectiveness of masking signals].
+ *
+ * The standard solution is the "self-pipe trick" where the low level signal
+ * handler writes the signal info into a pipe, and poll/select monitors that
+ * pipe. This neatly eliminates the window where signals could be missed. If
+ * the signal arrives before calling poll/select then the pipe is readable and
+ * poll/select will return. If the signal arrives during the poll/select call
+ * then they will return with EINTR.
+ *
+ * In the RTS context, this is done by the I/O managers: the in-RTS ones use
+ * newNonBlockingPipe on startup, have their low level signal handler use
+ * writeSignalInfoToPipe, and when the I/O manager finds the other end of the
+ * pipe is readable then it calls readSignalInfoFromPipe, and can then use
+ * startSignalHandler.
+ *
+ * The in-Haskell MIO I/O manager does essentially the same.
+ *
+ * See issue #27540.
+ */
+
void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w)
{
int pipefd[2];
=====================================
rts/posix/IOManSignals.h
=====================================
@@ -25,16 +25,6 @@ void initUserSignals(void);
*/
void finiUserSignals(void);
-#if !defined(THREADED_RTS)
-/*
- * Function: startPendingSignalHandlers()
- *
- * If there are any queued up posix signals, run the handlers
- * associated with them.
- */
-void startPendingSignalHandlers(Capability *cap);
-#endif
-
void startSignalHandler(Capability *cap, int sig, siginfo_t *info);
typedef void (*SignalHandler)(int sig, siginfo_t *info, void *p);
@@ -47,11 +37,6 @@ void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w);
void writeSignalInfoToPipe (int sig, siginfo_t *info, int fd);
bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd);
-/* Temporary */
-void generic_handler(int sig USED_IF_THREADS,
- siginfo_t *info,
- void *p STG_UNUSED);
-
#endif
#include "EndPrivate.h"
=====================================
rts/posix/Poll.c
=====================================
@@ -1,6 +1,6 @@
/* -----------------------------------------------------------------------------
*
- * (c) The GHC Team 2020-2023
+ * (c) The GHC Team 2020-2026
*
* An I/O manager based on the classic Unix poll() system call.
*
@@ -107,9 +107,10 @@ timeout (if any) as the poll() timeout parameter.
The CapIOManager structure for this I/O manager contains:
ClosureTable aiop_table;
- struct pollfd *aiop_poll_table, *full_poll_table;
+ struct pollfd *aiop_poll_table;
StgTimeoutQueue *timeout_queue;
int interrupt_fd_r, interrupt_fd_w;
+ int n_special_fds;
We also support the Linux-specific ppoll API which supports higher resolution
time delays -- nanoseconds rather than milliseconds as in classic poll(). It
@@ -118,14 +119,15 @@ also allows the signal mask to be adjusted, but we do not make use of this.
int ppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *tmo_p, const sigset_t *sigmask);
-We have both aiop_poll_table and full_poll_table. This is to cope with needing
-to wait on the special extra file descriptor interrupt_fd_r. This fd is used to
-support waking the I/O manager when we are blocked in a poll call. This
-requires waiting on an extra fd that has no corresponding entry in the
-aiop_table. To manage this quirk, we alias the aiop_poll_table to be the tail
-of the full_poll_table and have the first entry of the full_poll_table be the
-interrupt_fd_r. This means the aiop_poll_table indicies match up exactly with
-the aiop_table, but still allows the full_poll_table to have an extra entry.
+In addition to aiops, there are some special internal fds we have to monitor
+and respond to. In particular there is the interrupt_fd_r used to implement
+interruptIOManager. Whether this fd is used or not depends on configuration
+(use of interrupt_fd_r depends on HAVE_PREEMPTION). To enable multiple such
+fds and allow them to be optional we use the following scheme: we create
+special internal AsyncIOOps and put them into the aiop_table. These are
+treated much like other IOOpCodeWaitRead operations but with special handling
+for notification. The variable n_special_fds tracks how many such entries we
+have so that we can handle them specially but in a reasonably uniform manner.
******************************************************************************/
@@ -135,7 +137,20 @@ static void notifyIOCompletion(CapIOManager *iomgr, StgAsyncIOOp *aiop);
static void removeFromTables(CapIOManager *iomgr, int i);
static void reportPollError(int res, nfds_t nfds) STG_NORETURN;
+enum SpecialFd {
+ InterruptFd, /* iomgr->interrupt_fd_r for interruptIOManager */
+ SignalFd /* iomgr->shared->signal_fd_r */
+};
+static void insertSpecialFd(CapIOManager *iomgr,
+ enum SpecialFd special_fd_kind,
+ int special_fd);
+static void processSpecialFd(CapIOManager *iomgr,
+ StgAsyncIOOp *aiop, bool *interrupt);
+
+/* Called early in RTS startup _before_ the storage manager is initialised,
+ * so this is not allowed to allocate on the GC heap.
+ */
void initCapabilityIOManagerPoll(CapIOManager *iomgr)
{
initClosureTable(&iomgr->aiop_table, ClosureTableCompact);
@@ -145,26 +160,80 @@ void initCapabilityIOManagerPoll(CapIOManager *iomgr)
newFdWakeup(&iomgr->interrupt_fd_r, &iomgr->interrupt_fd_w);
#endif
- iomgr->full_poll_table = stgMallocBytes(sizeof(struct pollfd) /* size 1 */,
- "initCapabilityIOManagerPoll");
- iomgr->full_poll_table[0] = (struct pollfd) {
+ iomgr->n_special_fds = 0
#if defined(HAVE_PREEMPTION)
- .fd = iomgr->interrupt_fd_r,
- .events = POLLIN,
-#else
- .fd = -1, // unused
- .events = 0, // unused
+ + 1
+#endif
+#if defined(RTS_USER_SIGNALS)
+ + (iomgr->cap->no == 0 ? 1 : 0)
#endif
- .revents = 0
- };
- iomgr->aiop_poll_table = iomgr->full_poll_table+1; /* hence empty */
+ ;
#if defined(RTS_USER_SIGNALS)
/* Signal handlers are shared across all caps. Only init once. */
if (iomgr->cap->no == 0) {
initUserSignals();
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ newNonBlockingPipe(&iomgr->shared->signal_fd_r,
+ &iomgr->shared->signal_fd_w);
+ }
+#endif
+}
+
+
+/* Called late in RtsStartup, once heap allocation is ready. */
+void startCapabilityIOManagerPoll(CapIOManager *iomgr)
+{
+ /* ClosureTable allocates on the heap, so we can do it here not in init */
+ if (iomgr->n_special_fds > 0) {
+ bool ok = enlargeClosureTable(iomgr->cap, &iomgr->aiop_table,
+ iomgr->n_special_fds);
+ CHECK(ok);
}
+ iomgr->aiop_poll_table =
+ stgMallocBytes(sizeof(struct pollfd) * iomgr->n_special_fds,
+ "initCapabilityIOManagerPoll");
+
+#if defined(HAVE_PREEMPTION)
+ insertSpecialFd(iomgr, InterruptFd, iomgr->interrupt_fd_r);
#endif
+#if defined(RTS_USER_SIGNALS)
+ if (iomgr->cap->no == 0) {
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ insertSpecialFd(iomgr, SignalFd, iomgr->shared->signal_fd_r);
+ }
+#endif
+ ASSERT(iomgr->n_special_fds == sizeClosureTable(&iomgr->aiop_table));
+}
+
+
+/* For special fds, we add a dummy entry to the aiop_table and a
+ * corresponding real entry to the aiop_poll_table.
+ */
+static void insertSpecialFd(CapIOManager *iomgr,
+ enum SpecialFd special_fd_kind,
+ int special_fd)
+{
+ StgAsyncIOOp *aiop = (StgAsyncIOOp *)allocate(iomgr->cap,
+ sizeofW(StgAsyncIOOp));
+ SET_HDR(aiop, &stg_ASYNCIOOP_info, CCS_SYSTEM);
+ aiop->notify.unused = &stg_ASYNCIO_LIVE0_closure;
+ aiop->live = &stg_ASYNCIO_LIVE0_closure;
+ aiop->capno = iomgr->cap->no;
+ aiop->notify_type = NotifyInternal;
+ aiop->outcome = IOOpOutcomeInFlight;
+ aiop->operation = IOOpCodeWaitRead;
+ aiop->fd = special_fd;
+ aiop->internal = special_fd_kind;
+
+ int ix = insertClosureTable(iomgr->cap, &iomgr->aiop_table, aiop);
+ aiop->index = ix;
+
+ iomgr->aiop_poll_table[ix] = (struct pollfd) {
+ .fd = special_fd,
+ .events = POLLIN,
+ .revents = 0
+ };
}
@@ -173,9 +242,11 @@ void freeCapabilityIOManagerPoll(CapIOManager *iomgr)
#if defined(RTS_USER_SIGNALS)
if (iomgr->cap->no == 0) {
finiUserSignals();
+ close(iomgr->shared->signal_fd_r);
+ close(iomgr->shared->signal_fd_w);
}
#endif
- stgFree(iomgr->full_poll_table);
+ stgFree(iomgr->aiop_poll_table);
#if defined(HAVE_PREEMPTION)
closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
#endif
@@ -314,22 +385,10 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
ASSERT(ncompletions > 0);
bool interrupt = false;
-#if defined(HAVE_PREEMPTION)
- /* If the interrupt_fd_r is ready, collect it */
- if (iomgr->full_poll_table[0].revents) {
- ASSERT(iomgr->full_poll_table[0].fd == iomgr->interrupt_fd_r);
- collectFdWakeup(iomgr->interrupt_fd_r);
- ncompletions--;
- interrupt = true;
- debugTrace(DEBUG_iomanager, "Received interrupt in poll I/O manager");
- }
-#endif
struct pollfd *aiop_poll_table = iomgr->aiop_poll_table;
int i = 0;
- while (ncompletions > 0) {
- ASSERT(i < sizeClosureTable(&iomgr->aiop_table));
-
+ while (ncompletions > 0 && i < sizeClosureTable(&iomgr->aiop_table)) {
/* Since each aiop_table entry is for a single (fd, rw) pair, we
* just need to check if the aiop_poll_table[n].revents contains
* anything. We don't need to distinguish read vs write.
@@ -337,26 +396,34 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
if (aiop_poll_table[i].revents) {
StgAsyncIOOp *aiop = indexClosureTable(&iomgr->aiop_table, i);
+ ASSERT(aiop->operation == IOOpCodeWaitRead ||
+ aiop->operation == IOOpCodeWaitWrite);
ASSERT(aiop->outcome == IOOpOutcomeInFlight);
- /* We do need to handle POLLNVAL, but we do not need to do anything
- * special for POLLERR or POLLHUP. (See man poll for details).
- * The calling thread will typically try to do I/O after waiting
- * for I/O readiness. Thus for POLLERR they'll discover the error
- * when doing the I/O. And for POLLHUP, there is typically no
- * action required anyway: it's a notification and not an error.
- */
- if (aiop_poll_table[i].revents & POLLNVAL) {
- aiop->outcome = IOOpOutcomeFailed;
- aiop->error = EBADF;
+ if (aiop->notify_type == NotifyInternal) {
+ processSpecialFd(iomgr, aiop, &interrupt);
+ ncompletions--;
+ i++;
} else {
- aiop->outcome = IOOpOutcomeSuccess;
- aiop->result = 0;
+ /* We do need to handle POLLNVAL, but we do not need to do anything
+ * special for POLLERR or POLLHUP. (See man poll for details).
+ * The calling thread will typically try to do I/O after waiting
+ * for I/O readiness. Thus for POLLERR they'll discover the error
+ * when doing the I/O. And for POLLHUP, there is typically no
+ * action required anyway: it's a notification and not an error.
+ */
+ if (aiop_poll_table[i].revents & POLLNVAL) {
+ aiop->outcome = IOOpOutcomeFailed;
+ aiop->error = EBADF;
+ } else {
+ aiop->outcome = IOOpOutcomeSuccess;
+ aiop->result = 0;
+ }
+
+ removeFromTables(iomgr, i);
+ notifyIOCompletion(iomgr, aiop);
+ ncompletions--;
}
-
- removeFromTables(iomgr, i);
- notifyIOCompletion(iomgr, aiop);
- ncompletions--;
} else {
/* You'd expect incrementing the table index to be unconditional,
* but we don't increment the index if we did process the entry,
@@ -366,14 +433,44 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
i++;
}
}
+ ASSERT(ncompletions == 0);
return interrupt;
}
-void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
+static void processSpecialFd(CapIOManager *iomgr,
+ StgAsyncIOOp *aiop, bool *interrupt)
{
- ASSERT(iomgr->aiop_poll_table == iomgr->full_poll_table+1);
+ ASSERT(aiop->notify_type == NotifyInternal);
+ enum SpecialFd special_fd_kind = aiop->internal;
+ int fd = aiop->fd;
+
+ switch (special_fd_kind) {
+#if defined(HAVE_PREEMPTION)
+ case InterruptFd:
+ ASSERT(fd == iomgr->interrupt_fd_r);
+ collectFdWakeup(fd);
+ *interrupt = true;
+ debugTrace(DEBUG_iomanager, "Received interrupt in poll I/O manager");
+ break;
+#endif
+#if defined(RTS_USER_SIGNALS)
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ case SignalFd:
+ ASSERT(fd == iomgr->shared->signal_fd_r);
+ int sig; siginfo_t info;
+ readSignalInfoFromPipe(&sig, &info, fd);
+ startSignalHandler(iomgr->cap, sig, &info);
+ debugTrace(DEBUG_iomanager,
+ "Dispatched signal %d in I/O manager", sig);
+ break;
+#endif
+ }
+}
+
+void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
+{
if (!isEmptyTimeoutQueue(iomgr->timeout_queue)) {
Time now = getProcessElapsedTime();
processTimeoutCompletions(iomgr, now);
@@ -381,29 +478,20 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
if (!isEmptyClosureTable(&iomgr->aiop_table)) {
- int n_aiops = sizeClosureTable(&iomgr->aiop_table);
-#if defined(HAVE_PREEMPTION)
- /* the full_poll_table includes interrupt_fd_r */
- nfds_t nfds = n_aiops + 1;
- struct pollfd *poll_table = iomgr->full_poll_table;
-#else
- /* the aiop_poll_table does not include interrupt_fd_r */
- nfds_t nfds = n_aiops + 0;
- struct pollfd *poll_table = iomgr->aiop_poll_table;
-#endif
+ nfds_t nfds = sizeClosureTable(&iomgr->aiop_table);
/* Poll for I/O readiness, without waiting. */
#if defined(HAVE_DECL_PPOLL) && HAVE_DECL_PPOLL == 1
/* We could use poll here, since we use no timeout, but for
consistency we use the same syscall as at the other call site. */
struct timespec tv = (struct timespec) { .tv_sec = 0, .tv_nsec = 0 };
- int res = ppoll(poll_table, nfds, &tv, NULL);
+ int res = ppoll(iomgr->aiop_poll_table, nfds, &tv, NULL);
debugTrace(DEBUG_iomanager,
"ppoll(nfds = %lu, timeout = 0) = %d",
(unsigned long) nfds, res);
#else
- int res = poll(poll_table, nfds, 0);
+ int res = poll(iomgr->aiop_poll_table, nfds, 0);
debugTrace(DEBUG_iomanager,
"poll(nfds = %lu, timeout = 0) = %d",
@@ -426,10 +514,6 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
reportPollError(res, nfds);
}
}
-
-#if defined(RTS_USER_SIGNALS)
- startPendingSignalHandlers(iomgr->cap);
-#endif
}
@@ -437,8 +521,6 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
{
bool interrupt = false; /* got woken up via interruptIOManager */
- ASSERT(iomgr->aiop_poll_table == iomgr->full_poll_table+1);
-
/* Loop until we've woken up some threads. This loop is needed because the
* poll() timing isn't accurate, we sometimes sleep for a while but not
* long enough to wake up a thread in a threadDelay. Or we may need to
@@ -461,22 +543,14 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
*/
bool wait = emptyRunQueue(iomgr->cap);
- int n_aiops = sizeClosureTable(&iomgr->aiop_table);
-#if defined(HAVE_PREEMPTION)
- /* the full_poll_table includes interrupt_fd_r */
- nfds_t nfds = n_aiops + 1;
- struct pollfd *poll_table = iomgr->full_poll_table;
-#else
- /* the aiop_poll_table does not include interrupt_fd_r */
- nfds_t nfds = n_aiops + 0;
- struct pollfd *poll_table = iomgr->aiop_poll_table;
-#endif
-
/* Decide if we are going to wait if no I/O is ready, either:
* poll only, wait indefinitely, or wait until a timeout.
*/
Time timeout = timeoutWaitTime(iomgr, wait, now);
+ nfds_t nfds = sizeClosureTable(&iomgr->aiop_table);
+ int n_aiops = nfds - iomgr->n_special_fds;
+
if (RTS_UNLIKELY(timeout == -1 && n_aiops == 0)) {
/* See Note [Deadlock detection without idle GC] */
interrupt = notifyIdleGcDeadlock();
@@ -489,7 +563,7 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
* time, and if that timeout is reached then we schedule an idle GC
* (see handleIdleGcTimeout below).
*/
- bool any_pending_io = nfds > 0;
+ bool any_pending_io = n_aiops > 0;
bool timeout_idlegc_delay;
adjustTimeoutForIdleGc(any_pending_io, &timeout, &timeout_idlegc_delay);
#endif
@@ -497,14 +571,14 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
/* Check for I/O readiness, possibly waiting. */
#if defined(HAVE_DECL_PPOLL) && HAVE_DECL_PPOLL == 1
struct timespec ts, *timeout_ns = timeoutAsTimespec(timeout, &ts);
- int res = ppoll(poll_table, nfds, timeout_ns, NULL);
+ int res = ppoll(iomgr->aiop_poll_table, nfds, timeout_ns, NULL);
debugTrace(DEBUG_iomanager,
"ppoll(nfds = %lu, timeout = %" FMT_Int64 " ns) = %d",
(unsigned long) nfds, timeout, res);
#else
int timeout_ms = timeoutAsPollTimeout(timeout);
- int res = poll(poll_table, nfds, timeout_ms);
+ int res = poll(iomgr->aiop_poll_table, nfds, timeout_ms);
debugTrace(DEBUG_iomanager,
"poll(nfds = %lu, timeout = %d ms) = %d",
@@ -534,20 +608,23 @@ bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr)
*/
} else if (errno == EINTR) {
- /* We got interrupted by a signal. */
-
-#if defined(RTS_USER_SIGNALS)
- /* Start any corresponding user signal handlers. If any, the run
- * queue will become non-empty and we will drop out of the loop.
- */
- startPendingSignalHandlers(iomgr->cap);
-#endif
-
- /* We can also be interrupted by the shutdown signal handler, which
- * will set sched_state and so cause us to drop out of the loop.
+ /* We got interrupted by a signal.
+ *
+ * Signals of note:
+ * + signals with a user handler: these will be delivered to us
+ * via the signal_fd_r pipe, which we handle by starting new
+ * threads, which will terminate the loop (non-empty run queue).
+ * + The backup ctl-c signal handler sets sched_state, and to avoid
+ * races, it also calls interruptIOManagerPoll (which causes
+ * interrupt_fd_r to become ready). We're in the EINTR case here
+ * so we know the signal arrived while we were waiting, so it is
+ * the getSchedState condition that will will terminate the loop.
+ * (If the signal arrives before the call to poll then we will
+ * notice interrupt_fd_r is ready and set interrupt=true, which
+ * will terminate the loop).
*
- * For any other interruption (e.g. timer) we will go round the
- * do-while loop again.
+ * For any other interruption we will go round the do-while loop
+ * again.
*/
} else {
@@ -585,11 +662,20 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+#if defined(RTS_USER_SIGNALS)
+static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
+{
+ int signal_fd_w = getCapability(0)->iomgr->shared->signal_fd_w;
+ writeSignalInfoToPipe(sig, info, signal_fd_w);
+}
+
+
int installUserSignalHandlerPoll(int sig, int spi, sigset_t *mask,
bool nocldstop)
{
- return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+ return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
}
+#endif
/* Helper function to double the size of the aiop_table and aiop_poll_table.
@@ -602,14 +688,11 @@ static bool enlargeTables(CapIOManager *iomgr)
bool ok = enlargeClosureTable(iomgr->cap, &iomgr->aiop_table, newcapacity);
if (RTS_UNLIKELY(!ok)) return false;
- /* Update the auxiliary aiop_poll_table to match. The full_poll_table is
- * one bigger than the aiop_poll_table, since it has an extra entry at the
- * front for interrupt_fd_r, with no corresponding aiop. */
- iomgr->full_poll_table =
- stgReallocBytes(iomgr->full_poll_table,
- sizeof(struct pollfd) * (newcapacity+1),
+ /* Update the auxiliary aiop_poll_table to match. */
+ iomgr->aiop_poll_table =
+ stgReallocBytes(iomgr->aiop_poll_table,
+ sizeof(struct pollfd) * newcapacity,
"Poll.c: enlargeTables");
- iomgr->aiop_poll_table = iomgr->full_poll_table+1;
/* Initialise the new part of the aiop_poll_table */
struct pollfd *aiop_poll_table = iomgr->aiop_poll_table;
=====================================
rts/posix/Poll.h
=====================================
@@ -18,6 +18,7 @@
void initCapabilityIOManagerPoll(CapIOManager *iomgr);
void freeCapabilityIOManagerPoll(CapIOManager *iomgr);
+void startCapabilityIOManagerPoll(CapIOManager *iomgr);
/* Synchronous I/O and timer operations */
IOSubmitResult syncIOWaitReadyPoll(CapIOManager *iomgr, StgTSO *tso,
=====================================
rts/posix/Select.c
=====================================
@@ -55,6 +55,8 @@
#define TimeToLowResTimeRoundUp(t) (t)
#endif
+static void startPendingSignalHandlers(Capability *cap);
+
void initCapabilityIOManagerSelect(CapIOManager *iomgr)
{
iomgr->blocked_queue_hd = END_TSO_QUEUE;
@@ -524,10 +526,97 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait)
return !interrupt;
}
+
+/* -----------------------------------------------------------------------------
+ * Low-level signal handler
+ *
+ * Places the requested handler on a stack of pending handlers to be
+ * started up at the next context switch.
+ * -------------------------------------------------------------------------- */
+
+#define N_PENDING_HANDLERS 16
+
+siginfo_t pending_handler_buf[N_PENDING_HANDLERS];
+siginfo_t *next_pending_handler = pending_handler_buf;
+
+static void
+signal_handler(int sig STG_UNUSED, siginfo_t *info, void *p STG_UNUSED)
+{
+
+ /* Can't call allocate from here. Probably can't call malloc
+ either. However, we have to schedule a new thread somehow.
+
+ It's probably ok to request a context switch and allow the
+ scheduler to start the handler thread, but how do we
+ communicate this to the scheduler?
+
+ We need some kind of locking, but with low overhead (i.e. no
+ blocking signals every time around the scheduler).
+
+ Signal Handlers are atomic (i.e. they can't be interrupted), and
+ we can make use of this. We just need to make sure the
+ critical section of the scheduler can't be interrupted - the
+ only way to do this is to block signals. However, we can lower
+ the overhead by only blocking signals when there are any
+ handlers to run, i.e. the set of pending handlers is
+ non-empty.
+ */
+
+ /* We use a stack to store the pending signals. We can't
+ dynamically grow this since we can't allocate any memory from
+ within a signal handler.
+
+ Hence unfortunately we have to bomb out if the buffer
+ overflows. It might be acceptable to carry on in certain
+ circumstances, depending on the signal.
+ */
+
+ memcpy(next_pending_handler, info, sizeof(siginfo_t));
+
+ next_pending_handler++;
+
+ // stack full?
+ if (next_pending_handler == &pending_handler_buf[N_PENDING_HANDLERS]) {
+ errorBelch("too many pending signals");
+ stg_exit(EXIT_FAILURE);
+ }
+
+ interruptCapability(&MainCapability);
+
+}
+
int installUserSignalHandlerSelect(int sig, int spi, sigset_t *mask,
bool nocldstop)
{
- return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+ return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
+}
+
+static void startPendingSignalHandlers(Capability *cap)
+{
+ if (next_pending_handler == pending_handler_buf) return;
+
+ // Temporarily mask all signals to stop the signal_handler from mutating
+ // next_pending_handler and pending_handler_buf while we are looking at
+ // and modifying them.
+ static sigset_t maskSignals, savedSignals;
+ sigfillset(&maskSignals);
+ pthread_sigmask(SIG_SETMASK, &maskSignals, &savedSignals);
+
+ while (next_pending_handler != pending_handler_buf) {
+
+ next_pending_handler--;
+
+ siginfo_t *info = next_pending_handler;
+ int sig = info->si_signo;
+ startSignalHandler(cap, sig, info);
+
+ // It's possible by the time the signal handler runs that the handler
+ // has changed: runHandlers will run whatever handler is set at the
+ // time (or none).
+ }
+
+ // Restore the signal mask.
+ pthread_sigmask(SIG_SETMASK, &savedSignals, NULL);
}
#endif /* IOMGR_ENABLED_SELECT */
=====================================
rts/posix/SelectBis.c
=====================================
@@ -121,6 +121,17 @@ The CapIOManager structure for this I/O manager contains:
ClosureTable aiop_table;
StgTimeoutQueue *timeout_queue;
int interrupt_fd_r, interrupt_fd_w;
+ int n_special_fds;
+
+In addition to aiops, there are some special internal fds we have to monitor
+and respond to. In particular there is the interrupt_fd_r used to implement
+interruptIOManager. Whether this fd is used or not depends on configuration
+(use of interrupt_fd_r depends on HAVE_PREEMPTION). To enable multiple such
+fds and allow them to be optional we use the following scheme: we create
+special internal AsyncIOOps and put them into the aiop_table. These are
+treated much like other IOOpCodeWaitRead operations but with special handling
+for notification. The variable n_special_fds tracks how many such entries we
+have so that we can handle them specially but in a reasonably uniform manner.
******************************************************************************/
@@ -133,7 +144,20 @@ static int collectFdSets(CapIOManager *iomgr);
static void processBadFds(CapIOManager *iomgr);
static void reportSelectError(void) STG_NORETURN;
+enum SpecialFd {
+ InterruptFd, /* iomgr->interrupt_fd_r for interruptIOManager */
+ SignalFd /* iomgr->shared->signal_fd_r */
+};
+static void insertSpecialFd(CapIOManager *iomgr,
+ enum SpecialFd special_fd_kind,
+ int special_fd);
+static void processSpecialFd(CapIOManager *iomgr,
+ StgAsyncIOOp *aiop, bool *interrupt);
+
+/* Called early in RTS startup _before_ the storage manager is initialised,
+ * so this is not allowed to allocate on the GC heap.
+ */
void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
{
initClosureTable(&iomgr->aiop_table, ClosureTableCompact);
@@ -145,12 +169,21 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
/* Would never happen in a standalone process, but could plausibly happen
* if the RTS is used within another process that already has many open fds.
*/
- if (iomgr->interrupt_fd_r < 0 || iomgr->interrupt_fd_r >= (int)FD_SETSIZE ||
- iomgr->interrupt_fd_w < 0 || iomgr->interrupt_fd_w >= (int)FD_SETSIZE) {
+ if (iomgr->interrupt_fd_r >= (int)FD_SETSIZE ||
+ iomgr->interrupt_fd_w >= (int)FD_SETSIZE) {
barf("initCapabilityIOManagerSelectBis: fds out of select range");
}
#endif
+ iomgr->n_special_fds = 0
+#if defined(HAVE_PREEMPTION)
+ + 1
+#endif
+#if defined(RTS_USER_SIGNALS)
+ + (iomgr->cap->no == 0 ? 1 : 0)
+#endif
+ ;
+
iomgr->rfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
iomgr->wfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
@@ -158,8 +191,62 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
/* Signal handlers are shared across all caps. Only init once. */
if (iomgr->cap->no == 0) {
initUserSignals();
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ newNonBlockingPipe(&iomgr->shared->signal_fd_r,
+ &iomgr->shared->signal_fd_w);
+ if (iomgr->shared->signal_fd_r >= (int)FD_SETSIZE ||
+ iomgr->shared->signal_fd_w >= (int)FD_SETSIZE) {
+ barf("initCapabilityIOManagerSelectBis: fds out of select range");
+ }
+ }
+#endif
+}
+
+
+/* Called late in RtsStartup, once heap allocation is ready. */
+void startCapabilityIOManagerSelectBis(CapIOManager *iomgr)
+{
+ /* ClosureTable allocates on the heap, so we can do it here not in init */
+ if (iomgr->n_special_fds > 0) {
+ bool ok = enlargeClosureTable(iomgr->cap, &iomgr->aiop_table,
+ iomgr->n_special_fds);
+ CHECK(ok);
+ }
+
+#if defined(HAVE_PREEMPTION)
+ insertSpecialFd(iomgr, InterruptFd, iomgr->interrupt_fd_r);
+#endif
+#if defined(RTS_USER_SIGNALS)
+ if (iomgr->cap->no == 0) {
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ insertSpecialFd(iomgr, SignalFd, iomgr->shared->signal_fd_r);
}
#endif
+ ASSERT(iomgr->n_special_fds == sizeClosureTable(&iomgr->aiop_table));
+}
+
+
+/* For special fds, we add a dummy entry to the aiop_table and a
+ * corresponding real entry to the aiop_poll_table.
+ */
+static void insertSpecialFd(CapIOManager *iomgr,
+ enum SpecialFd special_fd_kind,
+ int special_fd)
+{
+ StgAsyncIOOp *aiop = (StgAsyncIOOp *)allocate(iomgr->cap,
+ sizeofW(StgAsyncIOOp));
+ SET_HDR(aiop, &stg_ASYNCIOOP_info, CCS_SYSTEM);
+ aiop->notify.unused = &stg_ASYNCIO_LIVE0_closure;
+ aiop->live = &stg_ASYNCIO_LIVE0_closure;
+ aiop->capno = iomgr->cap->no;
+ aiop->notify_type = NotifyInternal;
+ aiop->outcome = IOOpOutcomeInFlight;
+ aiop->operation = IOOpCodeWaitRead;
+ aiop->fd = special_fd;
+ aiop->internal = special_fd_kind;
+
+ int ix = insertClosureTable(iomgr->cap, &iomgr->aiop_table, aiop);
+ aiop->index = ix;
}
@@ -168,6 +255,8 @@ void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr)
#if defined(RTS_USER_SIGNALS)
if (iomgr->cap->no == 0) {
finiUserSignals();
+ close(iomgr->shared->signal_fd_r);
+ close(iomgr->shared->signal_fd_w);
}
#endif
stgFree(iomgr->rfds);
@@ -316,20 +405,9 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
ASSERT(ncompletions > 0);
bool interrupt = false;
-#if defined(HAVE_PREEMPTION)
- /* If the interrupt_fd_r is ready, collect it */
- if (FD_ISSET(iomgr->interrupt_fd_r, iomgr->rfds)) {
- collectFdWakeup(iomgr->interrupt_fd_r);
- ncompletions--;
- interrupt = true;
- debugTrace(DEBUG_iomanager, "Received interrupt in poll I/O manager");
- }
-#endif
int i = 0;
- while (ncompletions > 0) {
- ASSERT(i < sizeClosureTable(&iomgr->aiop_table));
-
+ while (ncompletions > 0 && i < sizeClosureTable(&iomgr->aiop_table)) {
StgAsyncIOOp *aiop = indexClosureTable(&iomgr->aiop_table, i);
int fd = aiop->fd;
enum IOOpCode op = aiop->operation;
@@ -339,11 +417,17 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
if (op == IOOpCodeWaitRead ? FD_ISSET(fd, iomgr->rfds)
: FD_ISSET(fd, iomgr->wfds)) {
- aiop->outcome = IOOpOutcomeSuccess;
- aiop->result = 0;
- removeFromTables(iomgr, i);
- notifyIOCompletion(iomgr, aiop);
- ncompletions--;
+ if (aiop->notify_type == NotifyInternal) {
+ processSpecialFd(iomgr, aiop, &interrupt);
+ ncompletions--;
+ i++;
+ } else {
+ aiop->outcome = IOOpOutcomeSuccess;
+ aiop->result = 0;
+ removeFromTables(iomgr, i);
+ notifyIOCompletion(iomgr, aiop);
+ ncompletions--;
+ }
} else {
/* You'd expect incrementing the table index to be unconditional,
* but we don't increment the index if we did process the entry,
@@ -353,10 +437,42 @@ static bool processIOCompletions(CapIOManager *iomgr, int ncompletions)
i++;
}
}
+ ASSERT(ncompletions == 0);
return interrupt;
}
+static void processSpecialFd(CapIOManager *iomgr,
+ StgAsyncIOOp *aiop, bool *interrupt)
+{
+ ASSERT(aiop->notify_type == NotifyInternal);
+ enum SpecialFd special_fd_kind = aiop->internal;
+ int fd = aiop->fd;
+
+ switch (special_fd_kind) {
+#if defined(HAVE_PREEMPTION)
+ case InterruptFd:
+ ASSERT(fd == iomgr->interrupt_fd_r);
+ collectFdWakeup(fd);
+ *interrupt = true;
+ debugTrace(DEBUG_iomanager, "Received interrupt in poll I/O manager");
+ break;
+#endif
+#if defined(RTS_USER_SIGNALS)
+ /* See Note [Non-racy signal handling using the self-pipe trick] */
+ case SignalFd:
+ ASSERT(fd == iomgr->shared->signal_fd_r);
+ int sig; siginfo_t info;
+ readSignalInfoFromPipe(&sig, &info, fd);
+ startSignalHandler(iomgr->cap, sig, &info);
+ debugTrace(DEBUG_iomanager,
+ "Dispatched signal %d in I/O manager", sig);
+ break;
+#endif
+ }
+}
+
+
void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
{
if (!isEmptyTimeoutQueue(iomgr->timeout_queue)) {
@@ -397,10 +513,6 @@ void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
reportSelectError();
}
}
-
-#if defined(RTS_USER_SIGNALS)
- startPendingSignalHandlers(iomgr->cap);
-#endif
}
@@ -440,8 +552,10 @@ bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
*/
Time timeout = timeoutWaitTime(iomgr, wait, now);
- if (RTS_UNLIKELY(timeout == -1 &&
- isEmptyClosureTable(&iomgr->aiop_table))) {
+ int nfds = sizeClosureTable(&iomgr->aiop_table);
+ int n_aiops = nfds - iomgr->n_special_fds;
+
+ if (RTS_UNLIKELY(timeout == -1 && n_aiops == 0)) {
/* See Note [Deadlock detection without idle GC] */
interrupt = notifyIdleGcDeadlock();
if (interrupt) break;
@@ -453,7 +567,7 @@ bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
* time, and if that timeout is reached then we schedule an idle GC
* (see handleIdleGcTimeout below).
*/
- bool any_pending_io = maxfd >= 0;
+ bool any_pending_io = n_aiops > 0;
bool timeout_idlegc_delay;
adjustTimeoutForIdleGc(any_pending_io, &timeout, &timeout_idlegc_delay);
#endif
@@ -479,7 +593,7 @@ bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
} else if (res > 0) {
int ncompletions = res;
- ASSERT(ncompletions <= sizeClosureTable(&iomgr->aiop_table)+1);
+ ASSERT(ncompletions <= nfds);
interrupt = processIOCompletions(iomgr, ncompletions);
/* We do _not_ need to check for timeouts again here. We know that
* select() returned due to I/O being ready before the timeout. If
@@ -489,20 +603,23 @@ bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr)
*/
} else if (errno == EINTR) {
- /* We got interrupted by a signal. */
-
-#if defined(RTS_USER_SIGNALS)
- /* Start any corresponding user signal handlers. If any, the run
- * queue will become non-empty and we will drop out of the loop.
- */
- startPendingSignalHandlers(iomgr->cap);
-#endif
-
- /* We can also be interrupted by the shutdown signal handler, which
- * will set sched_state and so cause us to drop out of the loop.
+ /* We got interrupted by a signal.
*
- * For any other interruption (e.g. timer) we will go round the
- * do-while loop again.
+ * Signals of note:
+ * + signals with a user handler: these will be delivered to us
+ * via the signal_fd_r pipe, which we handle by starting new
+ * threads, which will terminate the loop (non-empty run queue).
+ * + The backup ctl-c signal handler sets sched_state, and to avoid
+ * races, it also calls interruptIOManagerPoll (which causes
+ * interrupt_fd_r to become ready). We're in the EINTR case here
+ * so we know the signal arrived while we were waiting, so it is
+ * the getSchedState condition that will will terminate the loop.
+ * (If the signal arrives before the call to poll then we will
+ * notice interrupt_fd_r is ready and set interrupt=true, which
+ * will terminate the loop).
+ *
+ * For any other interruption we will go round the do-while loop
+ * again.
*/
} else if (errno == EBADF) {
@@ -595,11 +712,20 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+#if defined(RTS_USER_SIGNALS)
+static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
+{
+ int signal_fd_w = getCapability(0)->iomgr->shared->signal_fd_w;
+ writeSignalInfoToPipe(sig, info, signal_fd_w);
+}
+
+
int installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *mask,
bool nocldstop)
{
- return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+ return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
}
+#endif
/* Helper function to double the size of the aiop_table.
@@ -647,15 +773,6 @@ static int collectFdSets(CapIOManager *iomgr)
FD_ZERO(iomgr->rfds);
FD_ZERO(iomgr->wfds);
-#if defined(HAVE_PREEMPTION)
- /* We're always interested in our interrupt fd */
- {
- int fd = iomgr->interrupt_fd_r;
- maxfd = (fd > maxfd) ? fd : maxfd;
- FD_SET(fd, iomgr->rfds);
- }
-#endif
-
for (int ix = 0; ix < nentries; ix++) {
StgAsyncIOOp *aiop = indexClosureTable(&iomgr->aiop_table, ix);
int fd = aiop->fd;
=====================================
rts/posix/SelectBis.h
=====================================
@@ -38,6 +38,7 @@
void initCapabilityIOManagerSelectBis(CapIOManager *iomgr);
void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr);
+void startCapabilityIOManagerSelectBis(CapIOManager *iomgr);
/* Synchronous I/O and timer operations */
IOSubmitResult syncIOWaitReadySelectBis(CapIOManager *iomgr, StgTSO *tso,
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0267a1656213f118407577e4cb4611…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0267a1656213f118407577e4cb4611…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/marge_bot_batch_merge_job] 21 commits: Rework LogQueue to be a proper queue
by Marge Bot (@marge-bot) 27 Oct '26
by Marge Bot (@marge-bot) 27 Oct '26
27 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
953e4c7f by sheaf at 2026-10-26T18:19:10-04:00
Rework LogQueue to be a proper queue
Now 'LogQueue' uses a 'TQueue' with a separate 'TVar' to indicate
whether the log queue is finished logging, instead of the previous weird
data representation using a reverse-order '[Maybe Msg]' with 'Nothing'
denoting the end of the queue.
- - - - -
ec29c5fa by Rodrigo Mesquita at 2026-10-26T18:19:10-04:00
Refactor GHC.Driver.MakeAction
Pull out of 'runParPipelines' the logic for creating a 'MakeEnv' ready
to be used by multiple threads, as that will be useful for downsweep as
well (which doesn't fit the 'runPipelines' flow), rather than being just
for upsweep.
The code is moved and re-structured to match the export list, simplify
the sequentiality checks previously both in 'runPipelines' and
'runAllPipelines', which were weirdly similar and confusing; into the
two part step where we need these checks: (1) to construct the MakeEnv,
(2) to run the MakeActions in parallel. These two steps are separate and
used to be too mixed up.
Some additional little simplifications or clean ups here and there.
- - - - -
3fb22073 by Rodrigo Mesquita at 2026-10-26T18:19:11-04:00
Refactor GHC.Driver.Downsweep in preparation for parallel downsweep
Pure refactor to improve the code to facilitate implementing parallel
downsweep in the next commit.
This commit puts a MakeEnv into the DownsweepEnv, gives the fields
proper names and uses RecordWildcards to simplify, rather than passing
around all diagnostic wrappers, driver-message-things and using 10s of
positional fields.
No behavior changes here!
- - - - -
52f385a7 by sheaf at 2026-10-26T18:19:11-04:00
Driver: enableCodeGenForTH in load', not downsweep
enableCodeGenForTH modifies the module graph for planning, creating
fresh temporary paths. So it should only be run when we are about to
execute a build plan (e.g. in load'), so that consumers that want to
know facts about the module graph don't go looking for files that don't
exist yet (in particular 'haddock --no-compilation').
- - - - -
82402d14 by sheaf at 2026-10-26T18:19:11-04:00
2-phase Cache/Search Finder monad
This commit restructures the finder abstraction by introducing the
'FinderM' monad, which splits module lookup operations into two phases:
- a cache-only phase, performing no filesystem access,
- from the first cache miss onwards, a search action which may access
the filesystem.
This allows consumers to distinguish between quick cached results versus
more expensive filesystem search operations.
- - - - -
5c302b40 by sheaf at 2026-10-26T18:19:11-04:00
Separate Home/External finder caches
The caches for home modules and modules from external units have
different lifetimes, needing to be invalidated at different points.
This commit moves the cache for modules from external units into the
'UnitState' type, which keeps the cache correct whenever package flags
or package databases change (setSessionDynFlags, :set -package ...,
Backpack's addUnit).
This also makes it easier to clear the cache of home modules, as they
are now stored separately (obviating the 'isUnitEnvInstalledModule'
check we used to perform).
- - - - -
605fbb96 by sheaf at 2026-10-26T18:19:11-04:00
Add known home modules to the finder cache
The finder looks in the file system for the on-disk source that
corresponds to a Haskell module name. There are two kinds of modules
where we need to bypass this search:
- a file passed directly in the command line, which may not be in any
search path and whose module name may differ from the file name;
- Backpack signatures and modules, which do not exist as files at all.
This commit handles these two situations by adding an immutable
'KnownHomeModules' in the finder cache, which is always consulted first
before doing any search.
This removes the ad-hoc imperative logic which relied on mutating the
finder cache at careful points.
'setModuleGraph' now also takes the known home modules, so that the
module graph and the known home modules are always set consistently.
See Note [Known home modules] in GHC.Unit.Finder.Types.
- - - - -
82718f44 by sheaf at 2026-10-26T18:19:11-04:00
Make 'UnresolvedImport' into a suitable cache key
The goal of this commit is to make 'UnresolvedImport' suitable for use
as a cache key. Specifically, during downsweep we want to be able to
cache module resolution queries. We want to do this using some kind of
map, e.g. (roughly speaking):
Map UnresolvedImport ResolvedImport
For this to work, 'UnresolvedImport' must contain **precisely** the
input data to each resolution computation. In other words:
1. If something changes that materially affects resolution, it must
be stored in the cache key.
2. If changing some input does not materially affect the outcome,
then it must not be stored in the cache key.
To satisfy (1), we change 'UnitNode' to also keep track of the home
unit in whose unit state the unit's dependencies were looked up.
For (2), 'ImportResolution' now stores a 'ModuleLookupScope' instead of an
'UnresolvedImportOrigin'. Plugin imports are expressed with a new
'LookupPlugin' scope. The source location of an import, which is
immaterial to the resolution, is stored out of band outside
'UnresolvedImport', in 'ModSummary'.
- - - - -
d4635c7c by sheaf at 2026-10-26T18:19:11-04:00
Add GHC.Data.Dependent
This commit adds a minimal implementation of dependent sums
('Some', 'DSum') and dependent maps ('DMap').
The implementation is not entirely performance optimal, as there are a
few extraneous allocations compared to the full-blown implementation in
the 'dependent-map' package. For our use case (caching downsweep
computations), this does not matter much.
See GHC.Data.Dependent.
- - - - -
6759533e by sheaf at 2026-10-26T18:19:11-04:00
Driver: structured concurrent worker abstraction
This commit introduces a structured concurrency framework in the style
of the 'ki' library: a collection of threads within a scope.
We implement two kinds of concurrent workers on top of this framework:
- Independent workers cannot wait for one another at all. The only
scheduling operation is to wait for quiescence.
- Coordinating workers declare an STM readiness condition (waiting on
other workers to complete) which gates their start.
See Note [Deterministic concurrent workers] in GHC.Driver.Concurrency.
This commit ports upsweep to this new framework, with downsweep being
left as subsequent work.
Further changes along the way:
- Refactoring of how concurrency is acquired to avoid the footgun of
trying to use a no-op 'AbstractSem' as a lock in the serial case.
- The "re-run with -j1" logic for semaphore opening errors no longer
triggers on late semaphore failures (part-way through a lengthy
computation).
- Logger threads are properly cleaned up on exception, with each
concurrent worker's log queue and local TmpFs properly bracketed.
- The 'GhcMessage -> AnyGhcDiagnostic' and 'Maybe Messager'
arguments of 'depanalE', 'depanalPartial' and 'downsweep', which
were all dead in practice, have been dropped.
- Concurrent logging has been completely overhauled. Each worker has
its own log queue. Workers whose logs are printed as we go are
spawned in the order in which their logs must be printed, now using
a TQueue instead of the old complicated LogQueueQueue mechanism.
'logThread' is replaced by 'withLogPrinter', which runs the thread
that prints the log queues within a concurrent worker scope.
These changes allow us to fix issues with the exception handling:
we now always flush remaining log queues before exiting (even when
the action throws an exception), and exceptions that arise while
printing are properly rethrown. This fixes #28009.
- - - - -
38a99006 by sheaf at 2026-10-26T18:19:11-04:00
Rule-based deterministic concurrent downsweep
This commit rewrites downsweep as a single query-answering rule
(see 'DownsweepRule') that can be executed by concurrent worker threads.
The design allows every expensive operation (preprocessing files with CPP,
parsing headers, reading interfaces) to be performed concurrently
according to the -j<N>/-jsem flags.
See Note [Rules-based downsweep] in GHC.Driver.Downsweep.
The rules are run by "GHC.Driver.Concurrency.runRules" in a demand-driven
way: each worker can demand other workers to run, but never wait on
another worker. Every piece of work is done at most once.
Fixes #27514
Memory usage can increase due to doing more work concurrently:
-------------------------
Metric Increase:
MultiComponentModulesRecomp
MultiComponentModulesRecomp100
mhu-perf
-------------------------
- - - - -
9fa7b7be by sheaf at 2026-10-26T18:19:11-04:00
Acquire/release semaphore tokens more readily
This commit reworks GHC.Driver.MakeSem to use the scoped worker
abstraction of GHC.Utils.Concurrent.Scope. This allows us to get rid of
a lot of the tricky logic in GHC.Driver.MakeSem involving manual
exception handling, thread spawning and lifetime management, etc.
The main user-visible change is that token acquisition is no longer
rate limited (due to a bug, there previously was a 1s rate limit on
acquisition of semaphore tokens), with the release debounce period
shortened from 1s to 10ms.
Other changes:
- With -jsem, the number of capabilities is now capped at the number
of processors, bringing it in line with the -jN behaviour.
- Pending jobs whose wait for a token is interrupted by an exception
are now withdrawn from the jobserver, instead of acquiring a token
that it hung onto without doing useful work.
Fixes #27763
- - - - -
c83c6733 by Alan Zimmerman at 2026-10-26T18:19:55-04:00
EPA: Update AnnClsInstDecl to contain AnnList
It was already separately tracking the '{', '}' and leading ';' values.
This aligns it with layout-introducing parser productions ('vocurly'),
so the layout processing can be handled uniformly
- - - - -
732712dd by Simon Jakobi at 2026-10-27T12:57:31-04:00
X86 NCG: use 32-bit POPCNT for sub-word popcounts
A 32-bit write zero-extends the result to the full register, so we
don't need to emit additional code for this.
We also skip emitting zero-extension instructions for the argument
when it is already visibly zero-extended.
For the byte-wise popcount loop from #24813, this shrinks the popcount
sequence from 5 instructions to 3.
Assisted-by: Claude Fable 5.1
- - - - -
7e0e1332 by Zubin Duggal at 2026-10-27T12:57:32-04:00
ghci: Drop GHCi's exception context when rethrowing in ghc -e
Fixes #27847.
- - - - -
4e06453a by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context in modifyIOError
Fixes #27851.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/446
- - - - -
4454bc5d by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context in mapException
Fixes #27851.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/446
- - - - -
8636cc1a by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context when rethrowing exceptions in Handle reads
Fixes #27875.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/447
- - - - -
a5432183 by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context when rethrowing exceptions in hClose
Fixes #27875.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/447
- - - - -
ac718df1 by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context when rethrowing exceptions in Windows openFile
Fixes #27875.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/447
- - - - -
bd5348af by Zubin Duggal at 2026-10-27T12:57:33-04:00
base: Preserve exception context when rethrowing exceptions in executablePath
Fixes #27875.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/447
- - - - -
98 changed files:
- + changelog.d/T27763
- + changelog.d/T27847
- + changelog.d/clc-446
- + changelog.d/clc-447
- + changelog.d/ncg-x86-subword-popcnt
- + changelog.d/parallel-downsweep
- compiler/GHC.hs
- compiler/GHC/Builtin.hs
- compiler/GHC/CmmToAsm/X86/CodeGen.hs
- + compiler/GHC/Data/Dependent.hs
- compiler/GHC/Driver/Backpack.hs
- + compiler/GHC/Driver/Concurrency.hs
- + compiler/GHC/Driver/Config/Concurrency.hs
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Env.hs
- compiler/GHC/Driver/Errors/Ppr.hs
- compiler/GHC/Driver/Errors/Types.hs
- compiler/GHC/Driver/Main/Passes.hs
- compiler/GHC/Driver/Make.hs
- compiler/GHC/Driver/MakeAction.hs
- compiler/GHC/Driver/MakeFile.hs
- compiler/GHC/Driver/MakeSem.hs
- compiler/GHC/Driver/Pipeline/Execute.hs
- compiler/GHC/Driver/Pipeline/LogQueue.hs
- compiler/GHC/Hs/Decls.hs
- compiler/GHC/Hs/Dump.hs
- compiler/GHC/Iface/Load.hs
- compiler/GHC/Iface/Recomp.hs
- compiler/GHC/Linker/Deps.hs
- compiler/GHC/Parser.y
- compiler/GHC/Parser/Header.hs
- compiler/GHC/Runtime/Interpreter/JS.hs
- compiler/GHC/Runtime/Loader.hs
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/Tc/Gen/Splice.hs
- compiler/GHC/Tc/Module.hs
- compiler/GHC/Tc/Plugin.hs
- compiler/GHC/Tc/Utils/Backpack.hs
- compiler/GHC/Types/Error/Codes.hs
- compiler/GHC/Types/UnresolvedImport.hs
- compiler/GHC/Unit/Finder.hs
- + compiler/GHC/Unit/Finder/Cache.hs
- compiler/GHC/Unit/Finder/Types.hs
- compiler/GHC/Unit/Module/Graph.hs
- compiler/GHC/Unit/Module/ModSummary.hs
- compiler/GHC/Unit/Module/WholeCoreBindings.hs
- compiler/GHC/Unit/State.hs
- compiler/GHC/Unit/Types.hs
- + compiler/GHC/Utils/Concurrent/Scope.hs
- compiler/GHC/Utils/TmpFs.hs
- compiler/ghc.cabal.in
- ghc/GHCi/UI.hs
- ghc/GHCi/UI/Monad.hs
- ghc/Main.hs
- libraries/base/changelog.md
- libraries/ghc-internal/src/GHC/Internal/Control/Exception/Base.hs
- libraries/ghc-internal/src/GHC/Internal/IO/Handle/Internals.hs
- libraries/ghc-internal/src/GHC/Internal/IO/Handle/Text.hs
- libraries/ghc-internal/src/GHC/Internal/IO/Handle/Windows.hs
- libraries/ghc-internal/src/GHC/Internal/System/Environment/ExecutablePath.hsc
- libraries/ghc-internal/src/GHC/Internal/System/IO/Error.hs
- linters/lint-codes/LintCodes/Static.hs
- + testsuite/tests/codeGen/should_gen_asm/T24813.asm
- + testsuite/tests/codeGen/should_gen_asm/T24813.cmm
- testsuite/tests/codeGen/should_gen_asm/all.T
- testsuite/tests/count-deps/CountDepsParser.stdout
- testsuite/tests/diagnostic-codes/codes.stdout
- testsuite/tests/driver/Makefile
- testsuite/tests/driver/T27461/Makefile
- + testsuite/tests/driver/T27461/T27461c.stderr
- testsuite/tests/driver/T27461/all.T
- + testsuite/tests/driver/T27461/src/Wrong.hs
- + testsuite/tests/driver/T28009.hs
- testsuite/tests/driver/all.T
- + testsuite/tests/exceptions/T27851.hs
- + testsuite/tests/exceptions/T27851.stderr
- + testsuite/tests/exceptions/T27851b.hs
- + testsuite/tests/exceptions/T27851b.stderr
- + testsuite/tests/exceptions/T27851c.hs
- + testsuite/tests/exceptions/T27851c.stdout
- + testsuite/tests/exceptions/T27851d.hs
- + testsuite/tests/exceptions/T27851d.stdout
- testsuite/tests/exceptions/all.T
- testsuite/tests/ghc-api/downsweep/IncrementalDownsweep.hs
- testsuite/tests/ghc-api/downsweep/OldModLocation.hs
- testsuite/tests/ghc-api/downsweep/PartialDownsweep.hs
- testsuite/tests/ghc-api/fixed-nodes/FixedNodes.hs
- testsuite/tests/ghc-api/fixed-nodes/InterfaceModuleGraph.hs
- testsuite/tests/ghc-api/fixed-nodes/ModuleGraphInvariants.hs
- testsuite/tests/ghc-e/should_fail/T18441fail7.stderr
- testsuite/tests/ghc-e/should_run/ghc-e005.stderr
- testsuite/tests/haddock/should_compile_flag_haddock/T17544.stderr
- testsuite/tests/plugins/defaulting-plugin/DefaultLifted.hs
- testsuite/tests/printer/Test24533.stdout
- testsuite/tests/splice-imports/SI35.hs
- utils/check-exact/ExactPrint.hs
- utils/check-ppr/Main.hs
- utils/haddock/haddock-api/src/Haddock/Interface.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f0a49bd0705e6434ead21e2ce1456a…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f0a49bd0705e6434ead21e2ce1456a…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/andreask/9.10.4-batch1] 18 commits: Fix for alex-3.5.2.0 (#25623)
by Andreas Klebinger (@AndreasK) 27 Oct '26
by Andreas Klebinger (@AndreasK) 27 Oct '26
27 Oct '26
Andreas Klebinger pushed to branch wip/andreask/9.10.4-batch1 at Glasgow Haskell Compiler / GHC
Commits:
2373884c by Brandon Chinn at 2026-10-23T17:53:01+02:00
Fix for alex-3.5.2.0 (#25623)
This INLINE pragma for alexScanUser was added in 9.12, but then I
ported the change to alex in 3.5.2.0
(https://github.com/haskell/alex/pull/262)
I didn't realize that GHC errors on duplicate INLINE pragmas, so
this ended up being a breaking change.
This change should be backported into 9.12
(cherry picked from commit a1d923786baed5b001c523fd2a76f133be510b04)
- - - - -
1a86cc78 by Teo Camarasu at 2026-10-23T18:01:25+02:00
rts/nonmovingGC: remove n_free
We remove the nonmovingHeap.n_free variable.
We wanted this to track the length of nonmovingHeap.free.
But this isn't possible to do atomically.
When this isn't accurate we can get a segfault by going past the end of
the list.
Instead, we just count the length of the list when we grab it in
nonmovingPruneFreeSegment.
Resolves #26186
(cherry picked from commit 45efaf71d97355f76fe0db5af2fc5b4b67fddf47)
- - - - -
1c417885 by Ben Gamari at 2026-10-23T18:04:59+02:00
Bump win32-tarballs to v0.9
(cherry picked from commit 7308276916232706c8c196f929ef05fdffca4ddd)
- - - - -
5d72c333 by Andreas Klebinger at 2026-10-23T18:05:47+02:00
Disable -fprof-late-overloaded-calls for join points.
Currently GHC considers cost centres as destructive to
join contexts. Or in other words this is not considered valid:
join f x = ...
in
... -> scc<tick> jmp
This makes the functionality of `-fprof-late-overloaded-calls` not feasible
for join points in general. We used to try to work around this by putting the
ticks on the rhs of the join point rather than around the jump. However beyond
the loss of accuracy this was broken for recursive join points as we ended up
with something like:
rec-join f x = scc<tick> ... jmp f x
Which similarly is not valid as the tick once again destroys the tail call.
One might think we could limit ourselves to non-recursive tail calls and do
something clever like:
join f x = scc<tick> ...
in ... jmp f x
And sometimes this works! But sometimes the full rhs would look something like:
join g x = ....
join f x = scc<tick> ... -> jmp g x
Which, would again no longer be valid. I believe in the long run we can make
cost centre ticks non-destructive to join points. Or we could keep track of
where we are/are not allowed to insert a cost centre. But in the short term I will
simply disable the annotation of join calls under this flag.
(cherry picked from commit 7da86e165612721c4e09f772a3fdaffc733e9293)
- - - - -
361eb284 by Sebastian Graf at 2026-10-23T18:08:36+02:00
CprAnal: Detect recursive newtypes (#25944)
While `cprTransformDataConWork` handles recursive data con workers, it
did not detect the case when a newtype is responsible for the recursion.
This is now detected in the `Cast` case of `cprAnal`.
The same reproducer made it clear that `isRecDataCon` lacked congruent
handling for `AppTy` and `CastTy`, now fixed.
Furthermore, the new repro case T25944 triggered this bug via an
infinite loop in `cprFix`, caused by the infelicity in `isRecDataCon`.
While it should be much less likely to trigger such an infinite loop now
that `isRecDataCon` has been fixed, I made sure to abort the loop after
10 iterations and emitting a warning instead.
Fixes #25944.
(cherry picked from commit 4bc78496406f7469640faaa46e2f311c05760124)
- - - - -
15067627 by Ben Gamari at 2026-10-23T18:09:00+02:00
configure: Allow override of CrossCompiling
As noted in #26236, the current inference logic is a bit simplistic. In
particular, there are many cases (e.g. building for a new libc) where
the target and host triples may differ yet we are still able to run the
produced artifacts as native code.
Closes #26236.
(cherry picked from commit 81577fe7c1913c53608bf03e48f84507be904620)
- - - - -
ae506c05 by Ben Gamari at 2026-10-23T18:09:25+02:00
rts/linker/LoadArchive: Use bool
Improve type precision by using `bool` instead of `int` and `StgBool`.
(cherry picked from commit 580a33536e6180ce1d623d0922f7de5c975b6b3e)
- - - - -
a408ab4f by Ben Gamari at 2026-10-23T18:09:25+02:00
rts/linker/LoadArchive: Don't rely on file extensions for identification
Previously archive members would be identified via their file extension,
as described in #13103. We now instead use a more principled approach,
relying on the magic number in the member's header.
As well, we refactor treatment of archive format detection to improve
code clarity and error handling.
Closes #13103.
(cherry picked from commit 76d1041dfa4b96108cfdd22b07f2b3feb424dcbe)
- - - - -
96d8ff81 by Julian Ospald at 2026-10-23T18:10:37+02:00
rts: Fix object file format detection in loadArchive
Commit 76d1041dfa4b96108cfdd22b07f2b3feb424dcbe seems to
have introduced this bug, ultimately leading to failure of
test T11788. I can only theorize that this test isn't run
in upstream's CI, because they don't build a static GHC.
The culprit is that we go through the thin archive, trying
to follow the members on the filesystem, but don't
re-identify the new object format of the member. This pins
`object_fmt` to `NotObject` from the thin archive.
Thanks to @angerman for spotting this.
(cherry picked from commit fc958fc9eb6f6f4db473cdda23c381da8f32163d)
- - - - -
c57ce901 by Andreas Klebinger at 2026-10-23T18:13:21+02:00
rts: Support COFF BigObj files in archives.
(cherry picked from commit 01136779b1d4c1576c925ef673f55c81c45d8551)
- - - - -
a2cc97f3 by Sylvain Henry at 2026-10-23T18:21:32+02:00
Bump stack resolver to use GHC 9.6.7
Cf #26139
(cherry picked from commit e358e47756b455e167165950fb305b7413366ec4)
- - - - -
03541292 by Simon Peyton Jones at 2026-10-23T18:26:08+02:00
Take more care in zonkEqTypes on AppTy/AppTy
This patch fixes #26256.
See Note [zonkEqTypes and the PKTI] in GHC.Tc.Solver.Equality
(cherry picked from commit 18036d5205ac648bb245217519fed2fd931a9982)
- - - - -
699c9780 by Cheng Shao at 2026-10-27T12:53:16+01:00
libffi: update to 3.5.2
Bumps libffi submodule.
(cherry picked from commit 45dbfa23f508f221b6aeb667783a928511a7654e)
- - - - -
35c38e9a by sheaf at 2026-10-27T14:07:58+01:00
Use tcMkScaledFunTys in matchExpectedFunTys
We should use tcMkScaledFunTys rather than mkScaledFunTys in
GHC.Tc.Utils.Unify.matchExpectedFunTys, as the latter crashes
when the kind of the result type is a bare metavariable.
We know the result is always Type-like, so we don't need scaledFunTys
to try to rediscover that from the kind.
Fixes #26277
(cherry picked from commit 624afa4a65caa8ec23f85e70574dfb606f90c173)
- - - - -
2b1b7796 by Andreas Klebinger at 2026-10-27T14:29:31+01:00
Bump hadrian directory bound to 1.3.9 for windows.
While it's annoying because older releases don't ship it, it seems
borderling impossible to build the branch without it on windows. So we
use the tighther bound to give builds on windows a chance to finish.
- - - - -
664301e9 by Ben Gamari at 2026-10-27T14:46:35+01:00
StgToByteCode: Don't assume that data con workers are nullary
Previously StgToByteCode assumed that all data-con workers were of a
nullary representation. This is not a valid assumption, as seen
in #23210, where an unsaturated application of a unary data
constructor's worker resulted in invalid bytecode. Sadly, I have not yet
been able to reduce a minimal testcase for this.
Fixes #23210.
(cherry picked from commit d1d9e39ec293cd1d2b539b8246a349b539c6a61a)
- - - - -
09b292df by Ben Gamari at 2026-10-27T14:46:51+01:00
testsuite: Mark T23146* as unbroken
(cherry picked from commit 3eeecd508ef80812bc8ee84ab83f496f5030d59b)
- - - - -
e03f58bd by sheaf at 2026-10-27T14:46:51+01:00
Add test for #26216
(cherry picked from commit 2e73f3426ab6e3cf1938b53831005593f3fd351c)
- - - - -
35 changed files:
- compiler/GHC/Core/LateCC/OverloadedCalls.hs
- compiler/GHC/Core/Opt/CprAnal.hs
- compiler/GHC/Core/Opt/WorkWrap/Utils.hs
- compiler/GHC/Parser/Lexer.x
- compiler/GHC/StgToByteCode.hs
- compiler/GHC/Tc/Solver/Equality.hs
- compiler/GHC/Tc/Utils/Unify.hs
- configure.ac
- docs/users_guide/profiling.rst
- hadrian/hadrian.cabal
- hadrian/stack.yaml
- hadrian/stack.yaml.lock
- libffi-tarballs
- mk/get-win32-tarballs.py
- rts/Interpreter.c
- rts/linker/LoadArchive.c
- rts/sm/NonMoving.c
- rts/sm/NonMoving.h
- rts/sm/NonMovingAllocate.c
- rts/sm/Sanity.c
- + testsuite/tests/bytecode/T26216.hs
- + testsuite/tests/bytecode/T26216.script
- + testsuite/tests/bytecode/T26216.stdout
- + testsuite/tests/bytecode/T26216_aux.hs
- testsuite/tests/bytecode/all.T
- testsuite/tests/codeGen/should_run/T23146/all.T
- + testsuite/tests/cpranal/sigs/T25944.hs
- + testsuite/tests/cpranal/sigs/T25944.stderr
- testsuite/tests/cpranal/sigs/all.T
- + testsuite/tests/partial-sigs/should_compile/T26256.hs
- + testsuite/tests/partial-sigs/should_compile/T26256.stderr
- testsuite/tests/partial-sigs/should_compile/all.T
- + testsuite/tests/typecheck/should_compile/T26256a.hs
- + testsuite/tests/typecheck/should_compile/T26277.hs
- testsuite/tests/typecheck/should_compile/all.T
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/146fc9b7282394c0feaec30b190a8b…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/146fc9b7282394c0feaec30b190a8b…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/romes/multi-threaded-debugger] 7 commits: add break033 checks continue after unsetting break on exception works
by Rodrigo Mesquita (@alt-romes) 27 Oct '26
by Rodrigo Mesquita (@alt-romes) 27 Oct '26
27 Oct '26
Rodrigo Mesquita pushed to branch wip/romes/multi-threaded-debugger at Glasgow Haskell Compiler / GHC
Commits:
19e11d17 by Rodrigo Mesquita at 2026-10-27T11:14:36+00:00
add break033 checks continue after unsetting break on exception works
- - - - -
97fc0948 by Rodrigo Mesquita at 2026-10-27T11:18:08+00:00
Add test checking we don't stop at breakpoints in TH
- - - - -
c6de4b33 by Rodrigo Mesquita at 2026-10-27T11:22:38+00:00
Test that :abandon doesn't hang when -fbreak-on-error
- - - - -
09768c87 by Rodrigo Mesquita at 2026-10-27T11:33:40+00:00
run conc033 in ghci too
- - - - -
275f5fe7 by Rodrigo Mesquita at 2026-10-27T11:42:40+00:00
Add test checking a ^C interrupt of a :force interrupts the expression
- - - - -
061d37f4 by Rodrigo Mesquita at 2026-10-27T11:44:38+00:00
fixup! TSO_STOP_ON_EXCEPTION -- per-thread break-on-exception
- - - - -
bfd992fe by Rodrigo Mesquita at 2026-10-27T12:10:06+00:00
setBreakAction should also unset if flag is off.
(e.g. we don't want a :continue to keep flags if the :continue resume
request has unset things)
- - - - -
18 changed files:
- libraries/ghci/GHCi/Run.hs
- rts/Interpreter.h
- testsuite/tests/concurrent/should_run/all.T
- testsuite/tests/ghci.debugger/scripts/all.T
- + testsuite/tests/ghci.debugger/scripts/break032.hs
- + testsuite/tests/ghci.debugger/scripts/break032.script
- + testsuite/tests/ghci.debugger/scripts/break032.stdout
- + testsuite/tests/ghci.debugger/scripts/break033.hs
- + testsuite/tests/ghci.debugger/scripts/break033.script
- + testsuite/tests/ghci.debugger/scripts/break033.stdout
- + testsuite/tests/ghci.debugger/scripts/break034.hs
- + testsuite/tests/ghci.debugger/scripts/break034.script
- + testsuite/tests/ghci.debugger/scripts/break034.stdout
- + testsuite/tests/ghci/should_run/ForceInterrupt.script
- + testsuite/tests/ghci/should_run/ForceInterrupt.stdout
- + testsuite/tests/ghci/should_run/ForceInterruptExt.stdout
- testsuite/tests/ghci/should_run/Makefile
- testsuite/tests/ghci/should_run/all.T
Changes:
=====================================
libraries/ghci/GHCi/Run.hs
=====================================
@@ -367,7 +367,7 @@ sandboxIO opts io
-- tracing, etc.) need the expression to be running in a
-- separate thread, so debugging is only enabled when
-- using the sandbox.
- tid <- forkIO (pure ())
+ tid <- forkIO (pure ()) -- TODO doesn't work
runIt id >>= writeThreadEvalStatus tid
mkRemoteRef tid
-- We `runIt` on the main thread, but fork a new thread to store to the
@@ -419,16 +419,16 @@ setBreakAction opts tid@(ThreadId tid#) = do
where
setBreakAction' ctx = do
- runIf# singleStep rts_enableStopNextBreakpoint
- runIf# stepOut rts_enableStopAfterReturn
+ runIf# singleStep rts_enableStopNextBreakpoint rts_disableStopNextBreakpoint
+ runIf# stepOut rts_enableStopAfterReturn rts_disableStopAfterReturn
-- Breaking on exceptions is not enabled by default, since it might be a
-- bit surprising. The exception flag is turned off as soon as it is hit.
- runIf# breakOnException rts_enableStopOnException
+ runIf# breakOnException rts_enableStopOnException rts_disableStopOnException
if isolateThreadBreaks opts
then setIsolatedCtx ctx
else unsetIsolatedCtx ctx
- runIf# pred what = when (pred opts) (what tid#)
+ runIf# pred what what_off = if pred opts then what tid# else what_off tid#
=====================================
rts/Interpreter.h
=====================================
@@ -21,3 +21,9 @@ void rts_disableStopNextBreakpoint ( StgTSO* );
// Debugger step out
void rts_enableStopAfterReturn ( StgTSO* );
void rts_disableStopAfterReturn ( StgTSO* );
+
+// Debugger stop on exception
+void rts_enableStopOnException ( StgTSO* );
+void rts_disableStopOnException ( StgTSO* );
+void rts_enableStopOnExceptionAll ( void );
+void rts_disableStopOnExceptionAll ( void );
=====================================
testsuite/tests/concurrent/should_run/all.T
=====================================
@@ -168,7 +168,10 @@ test('conc030', extra_run_opts('+RTS -K4M -RTS'), compile_and_run, [''])
test('conc031', exit_code(1), compile_and_run, [''])
test('conc032', normal, compile_and_run, [''])
-test('conc033', normal, compile_and_run, [''])
+# Also run in GHCi: the sandbox thread must still be detected as deadlocked
+test('conc033',
+ [extra_ways(['ghci'] + (['ghci-ext'] if config.have_ext_interp else []))],
+ compile_and_run, [''])
# Omit for GHCi, because it just sits there waiting for you to press ^C
test('conc034', [
=====================================
testsuite/tests/ghci.debugger/scripts/all.T
=====================================
@@ -155,3 +155,7 @@ test('T26042f2', [extra_hc_opts('-O -fno-unoptimized-core-for-interpreter'), ext
test('T26042g', extra_files(['T26042g.hs']), ghci_script, ['T26042g.script'])
test('T27633c', extra_files(['T27633c_obj.hs']), ghci_script, ['T27633c.script'])
+
+test('break032', extra_files(['break032.hs']), ghci_script, ['break032.script'])
+test('break033', extra_files(['break033.hs']), ghci_script, ['break033.script'])
+test('break034', extra_files(['break034.hs']), ghci_script, ['break034.script'])
=====================================
testsuite/tests/ghci.debugger/scripts/break032.hs
=====================================
@@ -0,0 +1,4 @@
+module Break032 where
+
+f :: Int -> Int
+f x = x + 1
=====================================
testsuite/tests/ghci.debugger/scripts/break032.script
=====================================
@@ -0,0 +1,7 @@
+:set -XTemplateHaskell
+:l break032
+:break f
+-- Running a statement installs the debugger's breakpoint action (TODO: not rely on this.)
+print (1 :: Int)
+-- A breakpoint hit while running a splice must be ignored, not block forever
+y = $(Language.Haskell.TH.Syntax.lift (f 41))
=====================================
testsuite/tests/ghci.debugger/scripts/break032.stdout
=====================================
@@ -0,0 +1,3 @@
+Breakpoint 0 activated at break032.hs:4:7-11
+1
+*** Ignoring breakpoint
=====================================
testsuite/tests/ghci.debugger/scripts/break033.hs
=====================================
@@ -0,0 +1,11 @@
+module Break033 where
+
+import Control.Exception
+
+f :: Int -> IO Int
+f x = do
+ r <- try (evaluate (x `div` 0))
+ case r of
+ Left DivideByZero -> pure x
+ Left e -> throwIO e
+ Right y -> pure y
=====================================
testsuite/tests/ghci.debugger/scripts/break033.script
=====================================
@@ -0,0 +1,8 @@
+:l break033
+:break f
+:set -fbreak-on-exception
+f 1
+-- stopped on entry, hasn't hit exception yet
+:unset -fbreak-on-exception
+-- The DivideByZero must not stop us after break-on-exception was unset
+:continue
=====================================
testsuite/tests/ghci.debugger/scripts/break033.stdout
=====================================
@@ -0,0 +1,5 @@
+Breakpoint 0 activated at break033.hs:(6,7)-(11,31)
+Stopped in Break033.f, break033.hs:(6,7)-(11,31)
+_result :: IO Int = _
+x :: Int = 1
+1
=====================================
testsuite/tests/ghci.debugger/scripts/break034.hs
=====================================
@@ -0,0 +1,4 @@
+module Break034 where
+
+f :: Int -> Int
+f x = x + 1
=====================================
testsuite/tests/ghci.debugger/scripts/break034.script
=====================================
@@ -0,0 +1,11 @@
+:l break034
+:set -fbreak-on-error
+:break f
+f 1
+-- Killing the thread stopped at the breakpoint may break again, on the
+-- ThreadKilled exception; :abandon must still clean up without errors and
+-- without blocking there.
+:abandon
+f 2
+:abandon
+putStrLn "done"
=====================================
testsuite/tests/ghci.debugger/scripts/break034.stdout
=====================================
@@ -0,0 +1,8 @@
+Breakpoint 0 activated at break034.hs:4:7-11
+Stopped in Break034.f, break034.hs:4:7-11
+_result :: Int = _
+x :: Int = 1
+Stopped in Break034.f, break034.hs:4:7-11
+_result :: Int = _
+x :: Int = 2
+done
=====================================
testsuite/tests/ghci/should_run/ForceInterrupt.script
=====================================
@@ -0,0 +1,11 @@
+import Control.Concurrent
+import Control.Monad
+import Data.IORef
+import System.IO.Unsafe
+ticks <- newIORef (0 :: Int)
+let x = unsafePerformIO (writeFile "force.started" "" >> forever (modifyIORef' ticks (+1) >> threadDelay 20000)) :: ()
+-- blocks the interpreter waiting for the expression being forced to terminate.
+-- Test that a SIGINT (from the Makefile) interrupts the :force and kills the
+-- expression which is running in a (possibly remote) thread.
+:force x
+do { a <- readIORef ticks; threadDelay 500000; b <- readIORef ticks; putStrLn (if a == b then "stopped" else "still running") }
=====================================
testsuite/tests/ghci/should_run/ForceInterrupt.stdout
=====================================
@@ -0,0 +1,2 @@
+Interrupted.
+stopped
=====================================
testsuite/tests/ghci/should_run/ForceInterruptExt.stdout
=====================================
@@ -0,0 +1,2 @@
+Interrupted.
+stopped
=====================================
testsuite/tests/ghci/should_run/Makefile
=====================================
@@ -18,6 +18,15 @@ T3171 T3171ext:
while [ ! -e $@.started ] && kill -0 $$! 2>/dev/null; do sleep 0.1; done; \
kill -INT $$! $$(pgrep -P $$!); wait
+# Like T3171, but ^C interrupts a :force rather than an expression: the
+# computation being forced must be interrupted too, not left running.
+ForceInterruptExt: ForceInterrupt_OPTS = -fexternal-interpreter
+ForceInterrupt ForceInterruptExt:
+ rm -f force.started
+ "$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) $(ForceInterrupt_OPTS) < ForceInterrupt.script & \
+ while [ ! -e force.started ] && kill -0 $$! 2>/dev/null; do sleep 0.1; done; \
+ kill -INT $$! $$(pgrep -P $$!); wait
+
TopEnvIface:
"$(TEST_HC)" $(TEST_HC_OPTS_INTERACTIVE) TopEnvIface -v1 -fno-hide-source-paths -e "a" -e ":q" -fwrite-if-simplified-core -fwrite-interface
# Second compilation starts from interface files, but still can print "a"
=====================================
testsuite/tests/ghci/should_run/all.T
=====================================
@@ -23,6 +23,21 @@ test('T3171ext',
combined_output],
makefile_test, [])
+test('ForceInterrupt',
+ [when(opsys('mingw32'),skip),
+ req_interp,
+ extra_files(['ForceInterrupt.script']),
+ combined_output],
+ makefile_test, [])
+
+test('ForceInterruptExt',
+ [when(opsys('mingw32'),skip),
+ req_interp,
+ unless(config.have_ext_interp, skip),
+ extra_files(['ForceInterrupt.script']),
+ combined_output],
+ makefile_test, [])
+
test('ghcirun004', just_ghci, compile_and_run, [''])
test('T8377', just_ghci, compile_and_run, [''])
test('T9914', just_ghci, ghci_script, ['T9914.script'])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/51dfa0c5b5b6d5295aa3ed8430ab3c…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/51dfa0c5b5b6d5295aa3ed8430ab3c…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0