[Git][ghc/ghc][wip/jeltsch/smaller-pit] Make `IfaceBinHandle` a type synonym family
by Wolfgang Jeltsch (@jeltsch) 22 Oct '26
by Wolfgang Jeltsch (@jeltsch) 22 Oct '26
22 Oct '26
Wolfgang Jeltsch pushed to branch wip/jeltsch/smaller-pit at Glasgow Haskell Compiler / GHC
Commits:
fbafcc41 by Wolfgang Jeltsch at 2026-10-22T00:52:07+03:00
Make `IfaceBinHandle` a type synonym family
This brings the handling of `IfaceBinHandle` somewhat in line with how
other phase-dependent data is handled. Note that `mi_hi_bytes_` now
includes the `Strict.Maybe` layer itself, independently of the phase.
This is necessary, because with a type family instead of a GADT
`clear_mi_hi_bytes` cannot determine the phase at runtime anymore and
thus has to set `mi_hi_bytes_` to `Strict.Nothing` for every phase.
- - - - -
3 changed files:
- compiler/GHC/Iface/Binary.hs
- compiler/GHC/Iface/Make.hs
- compiler/GHC/Unit/Module/ModIface.hs
Changes:
=====================================
compiler/GHC/Iface/Binary.hs
=====================================
@@ -197,7 +197,7 @@ getIfaceWithExtFields name_cache bh = do
modIfaceBinData <- freezeBinHandle bh start
pure $ mod_iface
& set_mi_ext_fields extFields
- & set_mi_hi_bytes (FullIfaceBinHandle $ Strict.Just modIfaceBinData)
+ & set_mi_hi_bytes (Strict.Just modIfaceBinData)
-- | This performs a get action after reading the dictionary and symbol
-- table. It is necessary to run this before trying to deserialise any
@@ -276,10 +276,10 @@ writeBinIface profile traceBinIface compressionLevel hi_path mod_iface = do
putIfaceWithExtFields :: TraceBinIFace -> CompressionIFace -> WriteBinHandle -> ModIface -> IO ()
putIfaceWithExtFields traceBinIface compressionLevel bh mod_iface =
case mi_hi_bytes mod_iface of
- FullIfaceBinHandle Strict.Nothing -> do
+ Strict.Nothing -> do
forwardPutRel_ bh (\_ -> put_ bh (mi_ext_fields mod_iface)) $ do
putWithUserData traceBinIface compressionLevel bh mod_iface
- FullIfaceBinHandle (Strict.Just binData) -> putFullBinData bh binData
+ Strict.Just binData -> putFullBinData bh binData
-- | Put a piece of data with an initialised `UserData` field. This
-- is necessary if you want to serialise Names or FastStrings.
=====================================
compiler/GHC/Iface/Make.hs
=====================================
@@ -77,6 +77,7 @@ import GHC.Iface.Binary
import GHC.Data.FastString
import GHC.Data.Maybe
+import qualified GHC.Data.Strict as Strict
import GHC.HsToCore.Docs
import GHC.HsToCore.Usage
@@ -394,7 +395,7 @@ mkIface_ hsc_env
& set_mi_docs docs
& set_mi_abi_hashes ()
& set_mi_ext_fields emptyExtensibleFields
- & set_mi_hi_bytes PartialIfaceBinHandle
+ & set_mi_hi_bytes Strict.Nothing
where
cmp_rule = lexicalCompareFS `on` ifRuleName
=====================================
compiler/GHC/Unit/Module/ModIface.hs
=====================================
@@ -70,7 +70,7 @@ module GHC.Unit.Module.ModIface
, set_mi_hash_fn
, completePartialModIface
, shrinkModIface
- , IfaceBinHandle(..)
+ , IfaceBinHandle
, PartialModIface
, RetainedModIface
, IfaceAbiHashes (..)
@@ -160,6 +160,7 @@ import GHC.Utils.Binary
import Control.DeepSeq
import Control.Exception
+import Data.Void (Void)
{- Note [Interface file stages]
@@ -236,15 +237,10 @@ data ModIfacePhase
-- | In-memory byte array representation of a 'ModIface'.
--
-- See Note [Sharing of ModIface] for why we need this.
-data IfaceBinHandle (phase :: ModIfacePhase) where
- -- | A partial 'ModIface' cannot be serialised to disk.
- PartialIfaceBinHandle :: IfaceBinHandle 'ModIfaceCore
- -- | Optional 'FullBinData' that can be serialised to disk directly.
- --
- -- See Note [Private fields in ModIface] for when this fields needs to be cleared
- -- (e.g., set to 'Nothing').
- FullIfaceBinHandle :: !(Strict.Maybe FullBinData) -> IfaceBinHandle 'ModIfaceFinal
- RetainedIfaceBinHandle :: IfaceBinHandle 'ModIfaceRetained
+type family IfaceBinHandle (phase :: ModIfacePhase) where
+ IfaceBinHandle 'ModIfaceCore = Void
+ IfaceBinHandle 'ModIfaceFinal = FullBinData
+ IfaceBinHandle 'ModIfaceRetained = Void
type family IfaceSelfRecompData (phase :: ModIfacePhase) where
IfaceSelfRecompData 'ModIfaceCore = Maybe IfaceSelfRecomp
@@ -322,11 +318,22 @@ withSelfRecomp iface nk jk =
-- fields.
data ModIface_ (phase :: ModIfacePhase)
= PrivateModIface {
- mi_hi_bytes_ :: !(IfaceBinHandle phase),
- -- ^ A serialised in-memory buffer of this 'ModIface'.
- -- If this handle is given, we can avoid serialising the 'ModIface'
- -- when writing this 'ModIface' to disk, and write this buffer to disk instead.
- -- See Note [Sharing of ModIface].
+ mi_hi_bytes_ :: !(Strict.Maybe (IfaceBinHandle phase)),
+ -- ^ Optionally an in-memory buffer that contains the
+ -- serialization of this 'ModIface'. If such a buffer is
+ -- present, we can avoid serialising the 'ModIface' when
+ -- writing it to disk and write this buffer instead. See
+ -- Note [Sharing of ModIface] and
+ -- Note [Private fields in ModIface].
+ --
+ -- Only in the 'ModIfaceFinal' phase, there can be a buffer
+ -- with the serialization. Both @IfaceBinHandle 'ModIfaceCore@
+ -- and @IfaceBinHandle 'ModIfaceRetained@ are defined to be
+ -- 'Void', so that the 'mi_hi_bytes' field gets the type
+ -- @Strict.Maybe Void@. This type is isomorphic to '()', even
+ -- when taking partially defined values into account, because
+ -- @Strict.Just undefined@ is @undefined@ and so @Nothing@ and
+ -- @undefined@ are the only inhabitants of that type.
mi_iface_hash_ :: Fingerprint, -- A hash of the whole interface
mi_mod_info_ :: IfaceModInfo,
@@ -718,7 +725,7 @@ instance Binary ModIface where
-- We can't populate this field here, as we are
-- missing the 'mi_ext_fields_' field, which is
-- handled in 'getIfaceWithExtFields'.
- mi_hi_bytes_ = FullIfaceBinHandle Strict.Nothing
+ mi_hi_bytes_ = Strict.Nothing
})
instance Binary IfaceModInfo where
@@ -833,7 +840,7 @@ emptyPartialModIface mod
= PrivateModIface
{ mi_mod_info_ = emptyIfaceModInfo mod,
mi_iface_hash_ = fingerprint0,
- mi_hi_bytes_ = PartialIfaceBinHandle,
+ mi_hi_bytes_ = Strict.Nothing,
mi_deps_ = noDependencies,
mi_public_ = emptyPublicModIface [] [] [] [] [] [] [] (),
mi_simplified_core_ = Nothing,
@@ -907,7 +914,7 @@ emptyFullModIface :: Module -> ModIface
emptyFullModIface mod =
(emptyPartialModIface mod)
{ mi_public_ = emptyPublicModIface [] [] [] [] [] [] [] emptyIfaceBackend
- , mi_hi_bytes_ = FullIfaceBinHandle Strict.Nothing
+ , mi_hi_bytes_ = Strict.Nothing
}
@@ -940,7 +947,7 @@ instance ( NFData (IfaceSelfRecompData phase)
, NFData (IfaceAbiHashesData phase)
) => NFData (ModIface_ phase) where
rnf (PrivateModIface a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
- = (a1 :: IfaceBinHandle phase)
+ = a1
`seq` rnf a2
`seq` rnf a3
`seq` rnf a4
@@ -1051,7 +1058,7 @@ completePartialModIface :: PartialModIface
completePartialModIface partial iface_hash decls extra_decls final_exts cache = partial
{ mi_public_ = completePublicModIface decls final_exts cache (mi_public_ partial)
, mi_simplified_core_ = extra_decls
- , mi_hi_bytes_ = FullIfaceBinHandle Strict.Nothing
+ , mi_hi_bytes_ = Strict.Nothing
, mi_iface_hash_ = iface_hash
}
@@ -1072,7 +1079,7 @@ shrinkModIface :: ModIface -> RetainedModIface
shrinkModIface PrivateModIface {..}
= PrivateModIface
{
- mi_hi_bytes_ = RetainedIfaceBinHandle,
+ mi_hi_bytes_ = Strict.Nothing,
mi_public_ = shrinkIfacePublic mi_public_,
mi_self_recomp_ = (),
mi_simplified_core_ = (),
@@ -1100,7 +1107,7 @@ set_mi_mod_info val iface = clear_mi_hi_bytes $ iface { mi_mod_info_ = val }
set_mi_self_recomp :: IfaceSelfRecompData phase -> ModIface_ phase -> ModIface_ phase
set_mi_self_recomp val iface = clear_mi_hi_bytes $ iface { mi_self_recomp_ = val }
-set_mi_hi_bytes :: IfaceBinHandle phase -> ModIface_ phase -> ModIface_ phase
+set_mi_hi_bytes :: Strict.Maybe (IfaceBinHandle phase) -> ModIface_ phase -> ModIface_ phase
set_mi_hi_bytes val iface = iface { mi_hi_bytes_ = val }
set_mi_deps :: Dependencies -> ModIface_ phase -> ModIface_ phase
@@ -1203,12 +1210,7 @@ set_mi_mod_info_field f iface = clear_mi_hi_bytes $ iface { mi_mod_info_ = f (mi
-- | Invalidate any byte array buffer we might have.
clear_mi_hi_bytes :: ModIface_ phase -> ModIface_ phase
-clear_mi_hi_bytes iface = iface
- { mi_hi_bytes_ = case mi_hi_bytes iface of
- PartialIfaceBinHandle -> PartialIfaceBinHandle
- FullIfaceBinHandle _ -> FullIfaceBinHandle Strict.Nothing
- RetainedIfaceBinHandle -> RetainedIfaceBinHandle
- }
+clear_mi_hi_bytes iface = iface { mi_hi_bytes_ = Strict.Nothing }
-- ----------------------------------------------------------------------------
-- 'ModIface' pattern synonyms to keep breakage low.
@@ -1319,7 +1321,7 @@ pattern ModIface ::
-> Maybe Docs
-> IfaceAbiHashesData phase
-> ExtensibleFieldsData phase
- -> IfaceBinHandle phase
+ -> Strict.Maybe (IfaceBinHandle phase)
-> IfaceSelfRecompData phase
-> (OccName -> Maybe Fixity)
-> (OccName -> Maybe (OccName, Fingerprint))
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/fbafcc41f52de9c62058752b3ca1a29…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/fbafcc41f52de9c62058752b3ca1a29…
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] 5 commits: Add GHC.Internal.Unboxed.Do: do-notation for unboxed values (#27944)
by Marge Bot (@marge-bot) 22 Oct '26
by Marge Bot (@marge-bot) 22 Oct '26
22 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
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>
- - - - -
7a4c2305 by L0neGamer at 2026-10-22T13:06:56-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#``)
- - - - -
c0df89a6 by Sylvain Henry at 2026-10-22T13:07:01-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>
- - - - -
54 changed files:
- + changelog.d/25665
- + changelog.d/28012
- + changelog.d/resizeMutableByteArray-pinnedness
- + changelog.d/unboxed-do
- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/StgToCmm/Prim.hs
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- compiler/GHC/StgToJS/Prim.hs
- docs/users_guide/exts/qualified_do.rst
- hadrian/src/Settings/Builders/Ar.hs
- libraries/base/src/GHC/Base.hs
- libraries/base/src/GHC/Exts.hs
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- 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/Unboxed/Do.hs
- rts/PrimOps.cmm
- rts/RtsSymbols.c
- rts/include/stg/MiscClosures.h
- rts/js/environment.js
- 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/primops/should_run/ResizeByteArrayPinnedness.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
- testsuite/tests/primops/should_run/all.T
- 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/rts/T28012.hs
- + testsuite/tests/rts/T28012.stdout
- testsuite/tests/rts/all.T
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2122dc00c8967d010c4c9387353514…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2122dc00c8967d010c4c9387353514…
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
Simon Peyton Jones pushed to branch wip/T27994 at Glasgow Haskell Compiler / GHC
Commits:
f9fcb585 by Simon Peyton Jones at 2026-10-22T17:48:02+01:00
More wibbles
- - - - -
5 changed files:
- compiler/GHC/Core/Coercion/Axiom.hs
- compiler/GHC/Core/Predicate.hs
- compiler/GHC/Core/Unify.hs
- compiler/GHC/Tc/Instance/Family.hs
- compiler/GHC/Tc/Solver/FunDeps.hs
Changes:
=====================================
compiler/GHC/Core/Coercion/Axiom.hs
=====================================
@@ -277,8 +277,8 @@ data CoAxBranch
-- See Note [CoAxBranch type variables]
, cab_eta_tvs :: [TyVar]
-- ^ Eta-reduced tyvars
- -- cab_tvs and cab_lhs may be eta-reduced; see
- -- Note [Eta reduction for data families]
+ -- For a data family (only) cab_tvs and cab_lhs may be eta-reduced
+ -- See Note [Eta reduction for data families]
, cab_cvs :: [CoVar]
-- ^ Bound coercion variables
-- Always empty, for now.
@@ -286,8 +286,14 @@ data CoAxBranch
-- in GHC.Tc.TyCl
, cab_roles :: [Role]
-- ^ See Note [CoAxBranch roles]
+
, cab_lhs :: [Type]
-- ^ Type patterns to match against
+ -- For a data family `cab_lhs` may be shorter than the arity
+ -- of the data type
+ -- For a type family `cab_lhs` is exactly the family arity
+ -- See Note [Eta reduction for data families]
+
, cab_rhs :: Type
-- ^ Right-hand side of the equality
-- See Note [CoAxioms are homogeneous]
=====================================
compiler/GHC/Core/Predicate.hs
=====================================
@@ -36,7 +36,7 @@ module GHC.Core.Predicate (
-- Equality left-hand sides
CanEqLHS(..), canEqLHS_maybe, canTyFamEqLHS_maybe,
- canEqLHSKind, canEqLHSType, eqCanEqLHS
+ canEqLHSKind, canEqLHSType, eqCanEqLHS, saturatedFamApp_maybe
) where
@@ -61,6 +61,8 @@ import GHC.Utils.Misc
import GHC.Utils.Panic
import GHC.Data.FastString
+import GHC.Exts( inline )
+
{- *********************************************************************
* *
@@ -783,13 +785,19 @@ canEqLHS_maybe xi
| otherwise
= canTyFamEqLHS_maybe xi
-canTyFamEqLHS_maybe :: Type -> Maybe CanEqLHS
-canTyFamEqLHS_maybe xi
+saturatedFamApp_maybe :: Type -> Maybe (TyCon,[Type])
+saturatedFamApp_maybe xi
| Just (tc, args) <- tcSplitTyConApp_maybe xi
, isTypeFamilyTyCon tc
, args `lengthIs` tyConArity tc
- = Just $ TyFamLHS tc args
+ = Just (tc,args)
+ | otherwise
+ = Nothing
+canTyFamEqLHS_maybe :: Type -> Maybe CanEqLHS
+canTyFamEqLHS_maybe xi
+ | Just (tc, args) <- inline saturatedFamApp_maybe xi
+ = Just $ TyFamLHS tc args
| otherwise
= Nothing
=====================================
compiler/GHC/Core/Unify.hs
=====================================
@@ -17,6 +17,9 @@ module GHC.Core.Unify (
UnifyResult, UnifyResultM(..), MaybeApartReason(..),
typesCantMatch, typesAreApart, typeListsAreApart,
+ -- Finding the fixpoint of a substitution
+ niFixSubst,
+
-- Matching a type against a lifted type (coercion)
liftCoMatch
) where
=====================================
compiler/GHC/Tc/Instance/Family.hs
=====================================
@@ -16,6 +16,7 @@ import GHC.Prelude
import GHC.Driver.DynFlags
import GHC.Driver.Env
+import GHC.Core.Predicate( saturatedFamApp_maybe )
import GHC.Core.FamInstEnv
import GHC.Core.Coercion
import GHC.Core.TyCon
@@ -715,15 +716,9 @@ reportInjectivityErrors dflags fi_ax axiom inj
when wrong_bare_rhs $ reportBareVariableInRHSErr fam_tc bare_variables axiom
-- | Is type headed by a type family application?
-isTFHeaded :: Type -> Bool
-- See Note [Verifying injectivity annotation], case 3.
-isTFHeaded ty | Just ty' <- coreView ty
- = isTFHeaded ty'
-isTFHeaded ty | (TyConApp tc args) <- ty
- , isTypeFamilyTyCon tc
- = args `lengthIs` tyConArity tc
-isTFHeaded _ = False
-
+isTFHeaded :: Type -> Bool
+isTFHeaded ty = isJust (saturatedFamApp_maybe ty)
-- | If a RHS is a bare type variable return a set of LHS patterns that are not
-- bare type variables.
=====================================
compiler/GHC/Tc/Solver/FunDeps.hs
=====================================
@@ -24,7 +24,7 @@ import GHC.Tc.Types.Constraint
import GHC.Core.FamInstEnv
import GHC.Core.Coercion
import GHC.Core.TyCo.Rep( Type(..) )
-import GHC.Core.Predicate( EqRel(..) )
+import GHC.Core.Predicate( EqRel(..), saturatedFamApp_maybe )
import GHC.Core.TyCon
import GHC.Core.Unify( AmIUnifying(..)
, tcUnifyTysForInjectivity, typeListsAreApart )
@@ -32,7 +32,9 @@ import GHC.Core.Coercion.Axiom
import GHC.Builtin.WiredIn.TypeLits( tryInteractTopFam, tryInteractInertFam )
+import GHC.Types.Var( tyVarKind )
import GHC.Types.Var.Set
+import GHC.Types.Var.Env( mkInScopeSet )
import GHC.Utils.Outputable
import GHC.Utils.Panic
@@ -609,6 +611,7 @@ tryFamEqFunDeps mode eqs_for_me fam_tc work_args
mkTopFamEqFDs :: TyCon -> [TcType] -> EqCt -> SolverStage [FunDepEqns]
mkTopFamEqFDs fam_tc work_args work_item
+ -- NB: work_args exactly saturate fam_tc
| isOpenTypeFamilyTyCon fam_tc
, Injective inj_flags <- tyConInjectivityInfo fam_tc
= -- Open, injective type families
@@ -721,6 +724,7 @@ getRelevantBranches ax work_args work_rhs
mkTopOpenFamEqFDs :: TyCon -> [Bool] -> [TcType] -> EqCt -> TcS [FunDepEqns]
-- Implements (INJFAM:Wanted/top-open)
-- See Note [Exploiting open type families]
+-- NB: work_args exactly saturate fam_tc
mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs })
= do { fam_envs <- getFamInstEnvs
; let branches :: [CoAxBranch]
@@ -735,6 +739,7 @@ mkTopOpenFamEqFDs fam_tc inj_flags work_args (EqCt { eq_rhs = work_rhs })
| Just {} <- tcUnifyTysForInjectivity Matching [branch_rhs] [work_rhs]
-- No need for an apartness check here;
-- for open type families there are no "preceding" equations
+ -- Both branch_lhs_tys and work_args exactly saturate fam_tc
= Just (mkInjectivityFDEqn branch_qtvs (True: inj_flags)
(branch_rhs: branch_lhs_tys)
(work_rhs: work_args))
@@ -750,9 +755,9 @@ mkLocalFamEqFDs eqs_for_me fam_tc inj_flags work_args work_rhs
= do { let -- eqns_from_inerts: see (INJFAM:Wanted/other)
eqns_from_inerts = mapMaybe do_one eqs_for_me
-- eqns_from_self: see (INJFAM:Wanted/Self)
- eqns_from_self = case tcSplitTyConApp_maybe work_rhs of
- Just (tc,rhs_tys) | tc==fam_tc -> [mk_eqn rhs_tys]
- _ -> []
+ eqns_from_self = case saturatedFamApp_maybe work_rhs of
+ Just (tc, rhs_tys) | fam_tc == tc -> [mk_eqn rhs_tys]
+ _ -> []
; return (eqns_from_inerts ++ eqns_from_self) }
where
@@ -1541,12 +1546,10 @@ instantiateFunDepEqns (FDEqns { fd_qtvs = qtvs, fd_eqs = eqs })
rev_eqs = reverse eqs
-- (reverse eqs): See Note [Reverse order of fundep equations]
- empty_subst = emptySubst
- -- If you re-instate `trim_qtvs`, take care here:
- -- The kinds of `qtvs` can have free variables, because of `trim_qtvs`
- -- Example: T27828b
- -- empty_subst = mkEmptySubst $ mkInScopeSet $
- -- tyCoVarsOfTypes (map tyVarKind qtvs)
+ -- The kinds of `qtvs` can have free variables;
+ -- in the FDEqns produced by GHC.Tc.Instance.improveFromInstEnv
+ empty_subst = mkEmptySubst $ mkInScopeSet $
+ tyCoVarsOfTypes (map tyVarKind qtvs)
subst_pair subst (Pair ty1 ty2)
= Pair (substTy subst' ty1) ty2
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f9fcb58563cd24ca3c898cd7f65c088…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f9fcb58563cd24ca3c898cd7f65c088…
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] 15 commits: Fix a few tag inference bugs.
by Simon Peyton Jones (@simonpj) 22 Oct '26
by Simon Peyton Jones (@simonpj) 22 Oct '26
22 Oct '26
Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC
Commits:
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>
- - - - -
ba8e329b by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Make pushCast pure, and move it to Simplify.Utils
pushCast and pushCastMCo were in the SimplM monad, but no longer do
anything monadic. (Historically, pushing a coercion into an argument
used to simplify that argument; but now the coercion is recorded in
the sc_cast field of ApplyToVal.)
This patch makes them pure functions, and moves them, together with
addCastMCo, optOutCoercion and Note [The sc_cast field of ApplyToVal],
from GHC.Core.Opt.Simplify.Iteration to GHC.Core.Opt.Simplify.Utils,
alongside the SimplCont type they manipulate.
No change in behaviour.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
4519927e by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Lambda and case binders have vanilla IdInfo (#27965)
Lambda binders, case binders, and case-alternative binders should never
have RULES, a (non-default) inline pragma, or a stable unfolding. See the
new Note [Lambda and case binders have vanilla IdInfo] in GHC.Core.
This patch
* Makes Core Lint check the invariant, using the new predicate
GHC.Types.Id.lamBndrInfoOK.
* Fixes the desugarer, which violated it. For a strict binding
let !x = rhs in body
we generate
let x = rhs in case x of x { DEFAULT -> body }
re-using x (shadowed) as the case binder; see Note [Desugar Strict binds].
But the case binder inherited x's IdInfo, including, say, a NOINLINE
pragma (e.g. `res1` in GHC.Internal.Control.Monad.ST.Lazy.Imp). Now
GHC.HsToCore.Utils.seqVar gives the case binder vanilla IdInfo.
This was not just a matter of taste. For example
h fm s = let {-# NOINLINE res1 #-}
!res1 = fm s
!(f, s') = res1
in (f+1, s')
The NOINLINE case binder becomes a NOINLINE alias `let res3 = res2`,
which is never inlined, and survives (as a silly join point) into the
final code.
* Fixes the Simplifier, which could violate it when duplicating a
StrictBind continuation, which makes a join point
let x = <> in b ==> join j x = b in j <>
If x has such IdInfo, we now use a fresh binder for the join point
let x = <> in b ==> join j x' = let x = x' in b in j <>
See (DSB1) in Note [Duplicating StrictBind].
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
d61f1a7f by Simon Peyton Jones at 2026-10-22T17:46:09+01: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].
* 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>
- - - - -
66e95655 by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Fix the runRW# case of simplOutId to respect a pending cast
In the runRW# case of simplOutId, if the argument was a literal lambda
we took a short cut, simplifying the lambda body directly. But we
ignored any pending cast on the argument, arising from pushing a cast
into it; e.g. after inlining
myRun :: (State# RealWorld -> N) -> N
myRun = coerce (runRW# :: (State# RealWorld -> Int) -> Int)
That yielded ill-typed Core (#27962).
Fix: only take the short cut if there is no pending cast. Otherwise use
the general case, which deals with the cast properly.
Test: T27962, T27962a
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
d0960e5b by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Get the DupFlag right in the runRW# case of simplOutId
In the runRW# case of simplOutId we built an ApplyToVal continuation
with sc_dup = OkDup, regardless of whether its tail, inner_cont, was
itself dupable. That violates invariant (AV2) in
Note [The argument of ApplyToVal], and could lead mkDupableCont to
duplicate a large continuation.
Fix: use OkDup only if (contIsDupable inner_cont).
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
c503ddb7 by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Add regression test for #27963
The bug itself was fixed by "Use SimplClo for the argument of ApplyToVal".
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
fda04119 by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Add varIsHNF, and use it in move_transit_vars
`varIsHNF v` is the same as `exprIsHNF (Var v)`, but avoids building the
Var only to take it apart. The (Var v) case of exprIsHNFlike now uses
the shared varIsHNFlike, so the two cannot drift apart.
In GHC.Core.Opt.WorkWrap.Utils.move_transit_vars we had the guard
isUnliftedType var_ty || exprIsHNF (Var var)
but exprIsHNF is already True of variables of (definitely) unlifted type,
so the isUnliftedType test was redundant. Now it is just (varIsHNF var).
No change in behaviour.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
518ca15e by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Document evaluated-ness on case binders; tighten lamBndrInfoOK
Case binders and strict-field case-alternative binders carry an OtherCon
(evaluated-ness) unfolding in the syntax tree. This patch adds
Note [Evaluated-ness on case binders] in GHC.Core, explaining which passes
use that information and why it must be on the binder (many passes rebuild
occurrences from binders, notably CoreTidy just before CorePrep).
It also tightens GHC.Types.Id.lamBndrInfoOK, and hence the Core Lint check
on lambda, case, and case-alternative binders: previously it rejected only
stable unfoldings; now it allows only NoUnfolding or an OtherCon unfolding.
(Lambda binders are still separately required to have no unfolding at all.)
The tighter check exposed another source of bad case binders:
GHC.Core.Utils.bindNonRec (used by mkCoreLet, and hence the Simplifier's
wrapFloats) turns let x = rhs in body into a case when rhs is unlifted
and not ok-for-speculation, re-using the let-binder x, complete with its
let-unfolding (e.g. from makeTrivial), as the case binder. Now both
bindNonRec and the desugarer's seqVar use the new
GHC.Types.Id.mkCaseBndrFromLetBndr, which resets the binder's IdInfo
(except to record that it is evaluated).
See #27965.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
793bc0ba by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Wibble comments
- - - - -
04d6888e by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
SpecConstr: give the case binder in mkSeqs suitable IdInfo
mkSeqs (see Note [SpecConstr and strict fields]) builds
case arg of arg' { DEFAULT -> rhs }
where arg' is arg, localised. But arg may have IdInfo that a case binder
must not have; e.g. in test T24944 it was an imported global with a
top-level unfolding, which tripped the new Core Lint check. Use
mkCaseBndrFromLetBndr. See Note [Lambda and case binders have vanilla
IdInfo] in GHC.Core, and #27965.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
4fd3480f by Simon Peyton Jones at 2026-10-22T17:46:09+01:00
Wibbles
- - - - -
71 changed files:
- + changelog.d/25665
- + changelog.d/T27831-bottoming-strict-field
- + changelog.d/T27842-bytecode-imported-tags
- + changelog.d/unboxed-do
- compiler/GHC/Core.hs
- compiler/GHC/Core/Lint.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/Opt/SpecConstr.hs
- compiler/GHC/Core/Opt/WorkWrap/Utils.hs
- compiler/GHC/Core/Utils.hs
- compiler/GHC/HsToCore/Utils.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/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- compiler/GHC/Types/Id.hs
- docs/users_guide/exts/qualified_do.rst
- hadrian/src/Settings/Builders/Ar.hs
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- 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/Unboxed/Do.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/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/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/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
- 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
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f48ae9853ff76e84ecda2b5f8de1ac…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/f48ae9853ff76e84ecda2b5f8de1ac…
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] 2 commits: SpecConstr: give the case binder in mkSeqs suitable IdInfo
by Simon Peyton Jones (@simonpj) 22 Oct '26
by Simon Peyton Jones (@simonpj) 22 Oct '26
22 Oct '26
Simon Peyton Jones pushed to branch wip/case-bndr-unf at Glasgow Haskell Compiler / GHC
Commits:
2adf59e3 by Simon Peyton Jones at 2026-10-19T18:03:39+01:00
SpecConstr: give the case binder in mkSeqs suitable IdInfo
mkSeqs (see Note [SpecConstr and strict fields]) builds
case arg of arg' { DEFAULT -> rhs }
where arg' is arg, localised. But arg may have IdInfo that a case binder
must not have; e.g. in test T24944 it was an imported global with a
top-level unfolding, which tripped the new Core Lint check. Use
mkCaseBndrFromLetBndr. See Note [Lambda and case binders have vanilla
IdInfo] in GHC.Core, and #27965.
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
f48ae985 by Simon Peyton Jones at 2026-10-22T17:45:00+01:00
Wibbles
- - - - -
4 changed files:
- compiler/GHC/Core/Opt/Simplify/Env.hs
- compiler/GHC/Core/Opt/Simplify/Iteration.hs
- compiler/GHC/Core/Opt/Simplify/Utils.hs
- compiler/GHC/Core/Opt/SpecConstr.hs
Changes:
=====================================
compiler/GHC/Core/Opt/Simplify/Env.hs
=====================================
@@ -479,6 +479,10 @@ type SimplIdSubst = IdEnv SimplClo -- IdId |--> OutExpr
-- 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
@@ -486,7 +490,6 @@ data SimplClo
-- 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
=====================================
compiler/GHC/Core/Opt/Simplify/Iteration.hs
=====================================
@@ -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
@@ -1652,8 +1643,7 @@ completeBindX env from_what bndr rhs body cont
| FromBeta arg_levity <- from_what
, needsCaseBindingL arg_levity rhs -- Enforcing the let-can-float-invariant
= do { (env1, bndr1) <- simplNonRecBndr env bndr -- Lambda binders don't have rules
- -- See Note [Lambda and case binders have
- -- vanilla IdInfo] in GHC.Core
+ -- See Note [Lambda and case binders have vanilla IdInfo] in GHC.Core
; (floats, expr') <- simplNonRecBody env1 from_what body cont
-- Do not float floats past the Case binder below
; let expr'' = wrapFloats floats expr'
@@ -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 MOutCoercion field of ContEx] in GHC.Core.Opt.Simplify.Utils.
--}
+********************************************************************* -}
simplArg :: SimplEnvIS -- ^ Used only for its InScopeSet
-> Maybe ArgInfo -- ^ Just <=> This arg `ai` occurs in an app
@@ -1771,13 +1704,12 @@ simplArg env mb_arg_info fun_ty (ContEx arg_se arg mco)
cont = addCastMCo mco stop
; simplExprC arg_env' arg cont }
-{-
-************************************************************************
+
+{- *********************************************************************
* *
-\subsection{Lambdas}
+ Lambdas
* *
-************************************************************************
--}
+********************************************************************* -}
simplNonRecBody :: SimplEnv -> FromWhat
-> InExpr -> SimplCont
@@ -2295,8 +2227,8 @@ simplOutId env fun cont
env' = arg_env `addNewInScopeIds` [s']
hole_ty = mkVisFunTy m realWorldStatePrimTy new_runrw_res_ty
cont' = pushCastMCo env' arg_mco $
- ApplyToVal { sc_arg = DoneEx (Var s') NotJoinPoint
- , sc_dup = dup
+ ApplyToVal { sc_arg = DoneId s'
+ , sc_dup = dup
, sc_cont = inner_cont
, sc_hole_ty = hole_ty }
-- cont' applies to s', then K
=====================================
compiler/GHC/Core/Opt/Simplify/Utils.hs
=====================================
@@ -175,7 +175,6 @@ 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 ]
@@ -886,10 +885,65 @@ But we /also/ sometimes want to substitute that argument for a lambda-binder, in
`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
@@ -909,7 +963,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 ...))
@@ -918,7 +973,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
=====================================
compiler/GHC/Core/Opt/SpecConstr.hs
=====================================
@@ -2068,7 +2068,11 @@ mkSeqs seqees res_ty rhs =
-- Argument representing strict field and it's worth passing via cbv
| wantCbvForId arg_id
= Case (Var arg_id)
- (localiseId arg_id) -- See (SCF1) in Note [SpecConstr and strict fields]
+ (mkCaseBndrFromLetBndr (localiseId arg_id))
+ -- localiseId: see (SCF1) in Note [SpecConstr and strict fields]
+ -- mkCaseBndrFromLetBndr: arg_id may have IdInfo (e.g. an unfolding,
+ -- if it is a global) that a case binder must not have; see
+ -- Note [Lambda and case binders have vanilla IdInfo] in GHC.Core
res_ty
([Alt DEFAULT [] rhs])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/7cf966272fca8a0665a5bcb8850ef2…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/7cf966272fca8a0665a5bcb8850ef2…
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/davide/windows-dlls_sysm-test_master] RtsSymbols stores redirect labels
by David Eichmann (@DavidEichmann) 22 Oct '26
by David Eichmann (@DavidEichmann) 22 Oct '26
22 Oct '26
David Eichmann pushed to branch wip/davide/windows-dlls_sysm-test_master at Glasgow Haskell Compiler / GHC
Commits:
ba364142 by David Eichmann at 2026-10-22T16:58:49+01:00
RtsSymbols stores redirect labels
Redirect labels are used by the rts-check-symbols test
- - - - -
8 changed files:
- rts/RtsSymbols.c
- rts/include/rts/RtsSymbols.h
- testsuite/tests/ghci/linking/T25155_iserv_main.c
- testsuite/tests/rts/symbols/Main.hs
- testsuite/tests/rts/symbols/all.T
- testsuite/tests/rts/symbols/dumpDeclaredSymbols.c
- testsuite/tests/rts/symbols/rts-check-symbols.ignore
- testsuite/tests/rts/symbols/rts-check-symbols.stdout
Changes:
=====================================
rts/RtsSymbols.c
=====================================
@@ -1220,13 +1220,13 @@ RTS_SYMBOLS_PRIM
/* See Note [Naming Scheme for Symbol Macros] */
#define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE },
+ (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE, NULL },
#define SymI_HasDataProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_DATA },
+ (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_DATA, NULL },
#define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE },
+ (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE, NULL },
#define SymE_HasDataProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_DATA },
+ (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_DATA, NULL },
#define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
#define SymI_NeedsDataProto(vvv) SymI_HasDataProto(vvv)
@@ -1237,7 +1237,7 @@ RTS_SYMBOLS_PRIM
// another symbol. See newCAF/newRetainedCAF/newGCdCAF for an example.
#define SymI_HasProto_redirect(vvv,xxx,strength,ty) \
{ MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(xxx)), strength, ty },
+ (void*)(&(xxx)), strength, ty, MAYBE_LEADING_UNDERSCORE_STR(#xxx) },
static RtsSymbolVal * rtsSyms = NULL;
@@ -1264,7 +1264,7 @@ RtsSymbolVal const * getRtsSymbols(void) {
RTS_ARM_OUTLINE_ATOMIC_SYMBOLS
RTS_SYMBOLS_PRIM
SymI_HasDataProto(nonmoving_write_barrier_enabled)
- { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE } /* sentinel */
+ { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE, NULL } /* sentinel */
};
uint32_t n = 1;
@@ -1362,7 +1362,7 @@ RtsSymbolVal const * getRtsSymbols(void) {
// CODE_SYM(getauxval)
// CODE_SYM(posix_spawn_file_actions_init)
// ...
-// { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE } /* sentinel */
+// { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE, NULL } /* sentinel */
// };
//
// RtsSymbolVal* rtsExtraSyms() {
@@ -1375,7 +1375,7 @@ RtsSymbolVal const * getRtsSymbols(void) {
//
static RtsSymbolVal default_extra_syms[] = {
- { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE } /* sentinel */
+ { 0, 0, STRENGTH_NORMAL, SYM_TYPE_CODE, NULL } /* sentinel */
};
/* Default empty extra RTS symbols. See Note[Extra RTS symbols] */
=====================================
rts/include/rts/RtsSymbols.h
=====================================
@@ -8,6 +8,7 @@
#pragma once
+#include <stdbool.h>
typedef void SymbolAddr;
typedef char SymbolName;
@@ -36,6 +37,9 @@ typedef struct _RtsSymbolVal {
SymbolAddr* addr;
SymStrength strength;
SymType type;
+ // If this is a redirection, then this is the lable of the
+ // target symbol. Otherwise NULL.
+ const SymbolName* lbl_redirect_target;
} RtsSymbolVal;
RtsSymbolVal const * getRtsSymbols(void);
=====================================
testsuite/tests/ghci/linking/T25155_iserv_main.c
=====================================
@@ -23,7 +23,7 @@ int main (int argc, char *argv[])
// can use the FFI call to foobar in a TH splice.
#define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
- (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE },
+ (void*)(&(vvv)), STRENGTH_NORMAL, SYM_TYPE_CODE, NULL },
void foobar(int x) {
=====================================
testsuite/tests/rts/symbols/Main.hs
=====================================
@@ -19,18 +19,26 @@ main = do
-- want to avoid copying that logic again in the test. So we just ignore some
-- symbols so that CI can pass and in the future we aim to fix RtsSymbols.c and
-- reduce the size of ignoredSymbols.
- ignoreSymbols <- S.fromList
+ ignoreSymbols' <- S.fromList
. filter (not . null)
. lines
<$> readFile ignoreSymbolsFile
+ let symbolFilter :: String -> CodeOrData -> Bool
+ symbolFilter name cd =
+ -- Not in ingore list
+ not (S.member name ignoreSymbols')
+ -- Doesn't look like a entry code symbol
+ -- (These can appear in unregisterd builds wihere tables next to code is disabled)
+ && not ("_entry" `isSuffixOf` name && cd == Code)
+
declaredSymbolsStr <- readFile declaredSymbolsFile
observedSymbolsStr <- readFile observedSymbolsFile
- let declaredSymbols = readCodeOrData <$> linePairs declaredSymbolsStr
- let observedSymbols = parseReadelf observedSymbolsStr
+ let declaredSymbols = M.filterWithKey symbolFilter $ readCodeOrData <$> linePairs declaredSymbolsStr
+ let observedSymbols = M.filterWithKey symbolFilter $ parseReadelf observedSymbolsStr
- let diffStr = diff ignoreSymbols declaredSymbols observedSymbols
+ let diffStr = diff declaredSymbols observedSymbols
-- writeFile "<ghc root>/testsuite/tests/rts/symbols/rts-check-symbols.stdout"
putStrLn diffStr
@@ -44,8 +52,8 @@ data CodeOrData = Code | Data
-- Compare observedSymbols (symbols observed in the DSO) to declaredSymbols
-- (symbols declared in RtsSymbols.c)
-diff :: S.Set String -> M.Map String CodeOrData -> M.Map String CodeOrData -> String
-diff ignoreSymbols declaredSymbolsAll observedSymbolsAll = unlines $
+diff :: M.Map String CodeOrData -> M.Map String CodeOrData -> String
+diff declaredSymbols observedSymbols = unlines $
[ "Symbols declared in RtsSymbols.c but not observed in the DSO:" ]
++ [ "Declared> " ++ k ++ " " ++ show v | (k, v) <- M.toList diffInDeclaredSymbols ]
++ [ "" ]
@@ -56,10 +64,6 @@ diff ignoreSymbols declaredSymbolsAll observedSymbolsAll = unlines $
++ [ "WrongType> " ++ k ++ " (declared, observed): (" ++ show declared ++ ", " ++ show observed ++ ")"
| (k, (declared, observed)) <- M.toList diffOnType ]
where
- -- Ignore some symbols
- declaredSymbols = declaredSymbolsAll `M.withoutKeys` ignoreSymbols
- observedSymbols = observedSymbolsAll `M.withoutKeys` ignoreSymbols
-
-- Undeclared observed symbols
diffInObservedSymbols :: M.Map String CodeOrData
diffInObservedSymbols = observedSymbols M.\\ declaredSymbols
=====================================
testsuite/tests/rts/symbols/all.T
=====================================
@@ -1,5 +1,5 @@
test('rts-check-symbols', [
- unless(have_dynamic() and have_readelf() and have_ldd(), skip),
+ unless(opsys('linux') and have_dynamic() and have_readelf() and have_ldd(), skip),
windows_skip,
extra_files([
'dumpDeclaredSymbols.c',
=====================================
testsuite/tests/rts/symbols/dumpDeclaredSymbols.c
=====================================
@@ -19,8 +19,16 @@ void dumpDeclaredSymbols(void) {
type = "data";
}
+ const char * lbl = NULL;
+ if (sym->lbl_redirect_target) {
+ // In the case of redirects, the true symbol name is lbl_redirect_target
+ lbl = sym->lbl_redirect_target;
+ } else {
+ lbl = sym->lbl;
+ }
+
if (type) {
- printf("%s %s\n", sym->lbl, type);
+ printf("%s %s\n", lbl, type);
}
}
}
=====================================
testsuite/tests/rts/symbols/rts-check-symbols.ignore
=====================================
@@ -1,3 +1,16 @@
+__ashldi3
+__ashrdi3
+__divdi3
+__fixunsdfdi
+__lshrdi3
+__moddi3
+__muldi3
+__udivdi3
+__udivti3
+__umoddi3
+__umodti3
+_fini
+_init
complex_ccall_adjustor_template
decodeAddendAarch64
encodeAddendAarch64
@@ -30,11 +43,13 @@ isLoadStore
isVectorOp
makeStub
makeStubAarch64
-needStubForRelAarch64
+mblock_address_space
+mblock_map
needStubForRelaAarch64
+needStubForRelAarch64
numberOfStubsForSection
relocateObjectCode
relocateObjectCodeAarch64
simple_ccall_adjustor_template
StgReturn
-stubSizeAarch64
+stubSizeAarch64
\ No newline at end of file
=====================================
testsuite/tests/rts/symbols/rts-check-symbols.stdout
=====================================
@@ -1,6 +1,4 @@
Symbols declared in RtsSymbols.c but not observed in the DSO:
-Declared> __udivti3 Code
-Declared> __umodti3 Code
Declared> atomic_dec Code
Declared> dirty_TVAR Code
Declared> ffi_call Code
@@ -172,7 +170,6 @@ Observed> lookupIPEId Code
Observed> m32_free_page_pool Data
Observed> m32_free_page_pool_size Data
Observed> makeGot Code
-Observed> mblock_address_space Data
Observed> mblocks_allocated Data
Observed> modules Data
Observed> mpc_misses Data
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ba364142b29b702c3b89d131f1f4195…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ba364142b29b702c3b89d131f1f4195…
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/T26599] 10 commits: Improve exprOkForSpeculation
by Simon Peyton Jones (@simonpj) 22 Oct '26
by Simon Peyton Jones (@simonpj) 22 Oct '26
22 Oct '26
Simon Peyton Jones pushed to branch wip/T26599 at Glasgow Haskell Compiler / GHC
Commits:
8c947573 by Simon Peyton Jones at 2026-10-16T13:11:53+01:00
Improve exprOkForSpeculation
Fixes #27910, which exposed an infelicity in exprOkForSpeculation
The new story is carefully described in new Note [exprOkForSpeculation],
esp (a-c) and (EOKS1).
- - - - -
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>
- - - - -
1abedd1a by Simon Peyton Jones at 2026-10-22T15:19:40+01: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.
- - - - -
155 changed files:
- + changelog.d/25665
- + changelog.d/27624
- + changelog.d/T27412
- + changelog.d/T27831-bottoming-strict-field
- + changelog.d/T27842-bytecode-imported-tags
- + changelog.d/unboxed-do
- + changelog.d/zap-coercions
- 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/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/Type.hs
- compiler/GHC/Core/Utils.hs
- compiler/GHC/CoreToIface.hs
- compiler/GHC/Driver/Config.hs
- compiler/GHC/Driver/Config/Core/Lint.hs
- compiler/GHC/Driver/DynFlags.hs
- compiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Session.hs
- compiler/GHC/Hs/Decls.hs
- compiler/GHC/Hs/Utils.hs
- compiler/GHC/HsToCore/Quote.hs
- compiler/GHC/Iface/Ext/Ast.hs
- compiler/GHC/Iface/Rename.hs
- compiler/GHC/Iface/Syntax.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Parser.y
- compiler/GHC/Parser/PostProcess.hs
- compiler/GHC/Rename/Module.hs
- compiler/GHC/Runtime/Eval/Types.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/StgToJS/Apply.hs
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- compiler/GHC/Tc/Gen/App.hs
- compiler/GHC/Tc/Instance/Family.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/Utils/TcMType.hs
- compiler/GHC/ThToHs.hs
- compiler/GHC/Types/Unique/DSet.hs
- compiler/GHC/Types/Var/Set.hs
- compiler/Language/Haskell/Syntax/Decls.hs
- compiler/Language/Haskell/Syntax/Extension.hs
- docs/users_guide/extending_ghc.rst
- docs/users_guide/exts/qualified_do.rst
- docs/users_guide/exts/type_families.rst
- ghc/GHCi/UI/Info.hs
- hadrian/src/Settings/Builders/Ar.hs
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- 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/Unboxed/Do.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_run/T27624.hs
- + testsuite/tests/codeGen/should_run/T27624.stdout
- testsuite/tests/codeGen/should_run/all.T
- testsuite/tests/corelint/T27374.hs
- testsuite/tests/count-deps/CountDepsParser.stdout
- testsuite/tests/dependent/should_compile/T14729.stderr
- 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/should_compile_flag_haddock/T17544.stderr
- + testsuite/tests/indexed-types/should_compile/ZapDeps.hs
- testsuite/tests/indexed-types/should_compile/all.T
- 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/parser/should_compile/DumpParsedAst.stderr
- testsuite/tests/parser/should_compile/DumpRenamedAst.stderr
- testsuite/tests/parser/should_compile/KindSigs.stderr
- 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/rep-poly/RepPolyRule3.stderr
- testsuite/tests/simplCore/should_compile/T26051.stderr
- + testsuite/tests/simplCore/should_compile/T27910.hs
- + testsuite/tests/simplCore/should_compile/T27910.stderr
- testsuite/tests/simplCore/should_compile/all.T
- 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/typecheck/should_fail/T22707.stderr
- testsuite/tests/typecheck/should_fail/T25950.stderr
- testsuite/tests/typecheck/should_fail/T8450.stderr
- utils/check-exact/ExactPrint.hs
- 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/Rename.hs
- utils/haddock/haddock-api/src/Haddock/Types.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/8f35faed2b459fc34d161300e7ea57…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/8f35faed2b459fc34d161300e7ea57…
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] 18 commits: Change sigprocmask to pthread_sigmask in a couple places in the rts
by Duncan Coutts (@dcoutts) 22 Oct '26
by Duncan Coutts (@dcoutts) 22 Oct '26
22 Oct '26
Duncan Coutts pushed to branch wip/dcoutts/signals-cleanup at Glasgow Haskell Compiler / GHC
Commits:
f926f179 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Change sigprocmask to pthread_sigmask in a couple places in the rts
This is part of a series of changes to fix or remove uses of
sigprocmask. See issue #27968.
According to Posix:
> The use of the sigprocmask() function is unspecified in a
> multi-threaded process.
and indeed it does different things on different platforms. In
particular on macOS the behaviour is to change the signal mask for every
thread in the process (there is no per-process mask, it changes the
per-thread mask for every thread). This obviously does not mix well with
other concurrent changes to signal masks in other threads.
The use of sigprocmask in exitBySignal just needs to unmask a signal in
the current OS thread so that it can send a signal to itself. This
purpose only requires changing the signal mask of the calling thread,
which is exactly what pthread_sigmask is specified to do.
In resetTerminalSettings, sigprocmask (now pthread_sigmask) is used in
an attempt to mask a signal. It is unclear if this is effective. It
depends on whether there are any remaining threads running by the time
the function is called during shutdown.
Add a Note [pthread_sigmask and the ineffectiveness of masking signals]
and refer to it.
- - - - -
4647eb92 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Change (un)blockUserSignals to use pthread_sigmask, not sigprocmask
These functions are exported from the RTS (on posix platforms). They are
used in the process library to temporarily mask signals during a fork().
This is only needed to ensure the new process has the signals masked
initially. So in particular for this use case we want it to only affect
the signal mask of the calling thread. Thus pthread_sigmask is
appropriate.
- - - - -
2c282519 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Make (un)blockUserSignals local functions in win32 console handling
These functions were defined for all platforms in the RTS public
headers but only exported from the RTS on non-win32 platforms. They are
only used externally (by the process library) on posix platforms.
So for win32, break the link with the exported names and use local only
C static functions. They do serve a local purpose, but are now unrelated
to the posix ones.
- - - - -
ae933af6 by Duncan Coutts at 2026-10-22T09:42:05+01:00
In System.Posix.Internals, change import of sigprocmask to pthread_sigmask
and refer to the Note. It's still pretty dubious!
- - - - -
c8736342 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Drop use of sigprocmask in stg_sig_install
It is somewhat unclear what the signal masking was trying to protect in
stg_sig_install. The comment said
// Block the signal until we figure out what to do
There are no shared data structured between stg_sig_install and the
generic_handler that it installs.
In any case, masking signals does not in general work, so whatever it
was doing was a false sense of security.
- - - - -
ff39bcb0 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Make startPendingSignalHandlers a per-platform, not common, API
The direction of travel is that user-signal handling should be managed
by the I/O manager(s). This is because signals (& win32 console events)
are (a funny kind of) I/O, and also because they have to be involved in
noticing signals and starting signal handlers.
And how we communicate between the low level signal handler and starting
the Haskell signal handler threads is also speific to the I/O manager
implementation.
There is no need for a common API here. These are just utilities shared
between some I/O manager implementations.
- - - - -
8d91f749 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Unify freeSignalHandlers and finiUserSignals in RTS shutdown
They were called at different stages of shutdown. Historically this
might have made sense when the handler mechanism used stable pointers.
Now there appears to be no good reason.
On win32, freeSignalHandlers did nothing. On posix, finiUserSignals did
nothing. There appears to be no dependency on the stage of shutdown. It
is all local resources.
- - - - -
6ef09cae by Duncan Coutts at 2026-10-22T09:42:05+01:00
Push checks on install_signal_handlers flag into signal functions
Also, change the logic slightly. The docs for --install-signal-handlers
say that it installs handlers for ctl-c etc. It does _not_ say that it
disables the API for Haskell code to install signal handerls. Previously
however the RTS startup code would guard initUserSignals behind the flag
install_signal_handlers. All that code does is initalise variables
properly. This meant that when user code installs a handler, it would be
working with ill-defined userSignals value and an un-initialised mutex!
Similarly, in the win32 console handling, it would skip initialising the
hConsoleEvent, which certainly cannot lead to good outcomes.
So now we only guard initDefaultHandlers and resetDefaultHandlers behind
the install_signal_handlers flag.
- - - - -
30eb1db7 by Duncan Coutts at 2026-10-22T09:42:05+01:00
Fix user guide reference to --install-signal-handlers
While reviewing --install-signal-handlers docs, noticed a typo in the
--generate-crash-dumps docs that referred to --install-signal-handlers
when it means --install-seh-handlers. Yes I double-checked the code.
- - - - -
44a3e261 by Duncan Coutts at 2026-10-22T14:43:43+01:00
Split user signal functions out of posix/Signals into new file
The direction of travel is that user-signal handling should be managed
by the I/O manager(s).
The user signal functions (i.e. to allow Haskell code to set signal
handlers) should stop being first class functions called by the rest of
the RTS and become utilities used by the I/O managers.
Start here by splitting them out.
- - - - -
b99b82fb by Duncan Coutts at 2026-10-22T14:49:18+01:00
Make stg_sig_install part of the IOManager API
This is the external entry point for user code to install signal
handlers. So it becomes part of the I/O manager API. Eventually it will
dispatch to the I/O manager implementations (which will do different
things), but for now it dispatches to the same old implementation, under
a new name.
- - - - -
103a6e93 by Duncan Coutts at 2026-10-22T14:49:20+01:00
Document that nocldstop is a totally mad implicit parameter
and it's part of our public API!
- - - - -
6a4e9de3 by Duncan Coutts at 2026-10-22T14:49:20+01:00
Move user signal init and fini into the I/O manager
Initially just moved into start/stopIOManager, but a later step will be
to make that be part of the initialisation of each I/O manager.
Why? Because they cannot have a common init if they have different data
structures that need initialisation. In fact they already do have
different code and data (threaded/non-threaded, windows/other), but
this is fudged using CPP.
- - - - -
ebe75c89 by Duncan Coutts at 2026-10-22T14:49:20+01:00
Push {init,fini}{UserSignals,ConsoleEvents} down into I/O managers
So they become part of individual I/O managers, which will allow us to
start specialising them more sanely.
Rename the Win32 ones to {init,fini}ConsoleEvents.
- - - - -
7cc77334 by Duncan Coutts at 2026-10-22T15:07:19+01:00
Push installUserSignalHandler down into individual I/O managers
This will allow us to specialise them.
Initally they all use the same (existing) generic_handler.
- - - - -
d8e070f7 by Duncan Coutts at 2026-10-22T15:07:19+01:00
Specialise MIO's installing of signals
It already used a separate implementation, but using cpp via the
signal "generic_handler". Now it is much more direct and local.
MIO writes signals into a pipe and reads and dispatches them at the
other end (in Haskell).
- - - - -
ce8ccebf by Duncan Coutts at 2026-10-22T15:15:41+01:00
Introduce a I/O manager data structure shared between all caps
Accessed via cap->iomgr_shared, but it points to the same struct for all
capabilities.
We initialise and free this with cap 0.
Use this initially for the MIO I/O manager's timer manager control fd.
This fd is used for signals. Soon we will use the same mechanism for
the same purpose of signal handling, but for the other posix I/O
managers.
This is a fairly general mechanism, so other I/O managers may be able to
make use of it, once they are adapted to work in the threaded RTS with
multiple capabilities.
- - - - -
544e5e6b by Duncan Coutts at 2026-10-22T15:15:41+01:00
Extract and add more utility functions for the signal self-pipe trick
Extract a utility function writeSignalInfoToPipe from the MIO I/O
manager so we can use it in other I/O managers too.
Also add related utility functions readSignalInfoFromPipe,
newNonBlockingPipe and startSignalHandler.
We will use these in the poll and selectbis I/O managers.
- - - - -
26 changed files:
- libraries/ghc-internal/src/GHC/Internal/System/Posix/Internals.hs
- rts/IOManager.c
- rts/IOManagerInternals.h
- rts/RtsFlags.c
- rts/RtsSignals.h
- rts/RtsStartup.c
- rts/configure.ac
- rts/include/Rts.h
- rts/include/rts/BlockSignals.h
- rts/include/rts/IOInterface.h
- + rts/posix/IOManSignals.c
- + rts/posix/IOManSignals.h
- rts/posix/MIO.c
- rts/posix/MIO.h
- rts/posix/Poll.c
- rts/posix/Poll.h
- rts/posix/Select.c
- rts/posix/Select.h
- rts/posix/SelectBis.c
- rts/posix/SelectBis.h
- rts/posix/Signals.c
- rts/posix/TTY.c
- rts/posix/Ticker.c
- rts/rts.cabal
- rts/win32/ConsoleHandler.c
- rts/win32/ConsoleHandler.h
Changes:
=====================================
libraries/ghc-internal/src/GHC/Internal/System/Posix/Internals.hs
=====================================
@@ -878,7 +878,12 @@ foreign import capi unsafe "signal.h sigemptyset"
foreign import capi unsafe "signal.h sigaddset"
c_sigaddset :: Ptr CSigset -> CInt -> IO CInt
-foreign import capi unsafe "signal.h sigprocmask"
+-- Actually uses @pthread_sigmask@, which is the more modern version
+-- specified by Posix. pthread_sigmask() is specified to be per-thread.
+--
+-- Use of this is still very dubious however!
+-- See Note [pthread_sigmask and the ineffectiveness of masking signals]
+foreign import capi unsafe "signal.h pthread_sigmask"
c_sigprocmask :: CInt -> Ptr CSigset -> Ptr CSigset -> IO CInt
#endif
=====================================
rts/IOManager.c
=====================================
@@ -27,6 +27,11 @@
#include "IOManagerInternals.h"
+#if !defined(mingw32_HOST_OS)
+// TODO: make this per-I/O manager, not called from here directly.
+#include "posix/IOManSignals.h"
+#endif
+
#if defined(IOMGR_ENABLED_SELECT)
#include "Threads.h"
#include "posix/Select.h"
@@ -50,6 +55,7 @@
#if defined(IOMGR_ENABLED_MIO_WIN32)
#include "win32/ThrIOManager.h"
#include "win32/AsyncMIO.h"
+#include "win32/ConsoleHandler.h"
#endif
#if defined(IOMGR_ENABLED_WIN32_LEGACY)
@@ -57,6 +63,7 @@
#include "win32/AsyncMIO.h"
#include "win32/AwaitEvent.h"
#include "win32/MIOManager.h"
+#include "win32/ConsoleHandler.h"
#endif
#if defined(IOMGR_ENABLED_WINIO)
@@ -65,6 +72,7 @@
#if !defined(THREADED_RTS)
#include "win32/AwaitEvent.h"
#endif
+#include "win32/ConsoleHandler.h"
#endif
#include <string.h>
@@ -348,6 +356,15 @@ CapIOManager *allocCapabilityIOManager(Capability *cap)
CapIOManager *iomgr = stgMallocBytes(sizeof(CapIOManager),
"allocCapabilityIOManager");
iomgr->cap = cap; /* link back */
+
+ /* Allocate and/or link the shared I/O manager data structure */
+ if (cap->no == 0) {
+ iomgr->shared = stgMallocBytes(sizeof(CapIOManagerShared),
+ "allocCapabilityIOManager");
+ } else {
+ iomgr->shared = MainCapability.iomgr->shared;
+ }
+
return iomgr;
}
@@ -364,6 +381,8 @@ void initCapabilityIOManager(CapIOManager *iomgr)
debugTrace(DEBUG_iomanager, "initialising I/O manager %s for cap %d",
showIOManager(), iomgr->cap->no);
+ ASSERT(iomgr->cap && iomgr->shared); /* These are initialised already */
+
switch (iomgr_type) {
#if defined(IOMGR_ENABLED_SELECT)
case IO_MANAGER_SELECT:
@@ -392,7 +411,7 @@ void initCapabilityIOManager(CapIOManager *iomgr)
#if defined(IOMGR_ENABLED_MIO_POSIX)
case IO_MANAGER_MIO_POSIX:
- iomgr->control_fd = -1;
+ initCapabilityIOManagerMIO(iomgr);
break;
#endif
default:
@@ -424,6 +443,13 @@ void freeCapabilityIOManager(CapIOManager *iomgr)
default:
break;
}
+
+ /* We expect the per-impl free function (for cap 0) to free anything within
+ * the shared structure.
+ */
+ if (iomgr->cap->no == 0) {
+ stgFree(iomgr->shared);
+ }
}
@@ -449,6 +475,7 @@ void startIOManager(void)
* TODO: rename ioManagerStart to be less generic, impl specific
*/
ioManagerStart();
+ initConsoleEvents(); //TODO: fully integrate
break;
#endif
#if defined(IOMGR_ENABLED_WINIO)
@@ -465,11 +492,13 @@ void startIOManager(void)
#else
startupAsyncWinIO();
#endif
+ initConsoleEvents(); //TODO: fully integrate
break;
#endif
#if defined(IOMGR_ENABLED_WIN32_LEGACY)
case IO_MANAGER_WIN32_LEGACY:
startupAsyncIO();
+ initConsoleEvents(); //TODO: fully integrate
break;
#endif
default:
@@ -566,14 +595,21 @@ void
exitIOManager(bool wait_threads)
{
switch (iomgr_type) {
+#if defined(IOMGR_ENABLED_MIO_WIN32)
+ case IO_MANAGER_MIO_WIN32:
+ finiConsoleEvents(); //TODO: fully integrate
+ break;
+#endif
#if defined(IOMGR_ENABLED_WINIO)
case IO_MANAGER_WINIO:
shutdownAsyncWinIO(wait_threads);
+ finiConsoleEvents(); //TODO: fully integrate
break;
#endif
#if defined(IOMGR_ENABLED_WIN32_LEGACY)
case IO_MANAGER_WIN32_LEGACY:
shutdownAsyncIO(wait_threads);
+ finiConsoleEvents(); //TODO: fully integrate
break;
#endif
default:
@@ -634,6 +670,67 @@ setIOManagerControlFd(uint32_t cap_no, int fd) {
#endif
+/* This is a plausible candidate for a newer public API, but it isn't yet.
+ * It is currently the worker for stg_sig_install below.
+ */
+static int installUserSignalHandler(int sig, int spi, sigset_t *mask,
+ bool nocldstop)
+{
+#if !defined(RTS_USER_SIGNALS)
+ return STG_SIG_DFL;
+#endif
+
+ debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
+
+ switch (iomgr_type) {
+#if defined(IOMGR_ENABLED_SELECT)
+ case IO_MANAGER_SELECT:
+ return installUserSignalHandlerSelect(sig, spi, mask, nocldstop);
+#endif
+#if defined(IOMGR_ENABLED_SELECTBIS)
+ case IO_MANAGER_SELECTBIS:
+ return installUserSignalHandlerSelectBis(sig, spi, mask, nocldstop);
+#endif
+#if defined(IOMGR_ENABLED_POLL)
+ case IO_MANAGER_POLL:
+ return installUserSignalHandlerPoll(sig, spi, mask, nocldstop);
+#endif
+#if defined(IOMGR_ENABLED_MIO)
+ case IO_MANAGER_MIO:
+ return installUserSignalHandlerMIO(sig, spi, mask, nocldstop);
+#endif
+ default:
+ barf("installUserSignalHandler not implemented");
+ }
+}
+
+
+
+#if !defined(mingw32_HOST_OS)
+/* This flag is a public API, used by the unix package. It is an _implicit_
+ * argument to stg_sig_install to control whether or not to include
+ * SA_NOCLDSTOP when installing a SIGCHLD handler.
+ *
+ * Yes this is completely mad, and it should really be a normal explicit
+ * parameter. Note also the total lack of protection against data races.
+ */
+HsInt nocldstop = 0;
+
+
+/* Declared in rts/IOInterface.h. This is a public API, used by the unix
+ * package for installHandler.
+ *
+ * This is used as an FFI ccall import, but it would be better as a primop, for
+ * consistency and so we can always have a Capability to hand.
+ */
+int stg_sig_install(int sig, int spi, void *mask)
+{
+ /* Grab the current value of the nocldstop argument. Yes this is madness. */
+ return installUserSignalHandler(sig, spi, mask, nocldstop);
+}
+#endif
+
+
void pollCompletedTimeoutsOrIO(CapIOManager *iomgr)
{
debugTrace(DEBUG_iomanager, "polling for completed IO or timeouts");
=====================================
rts/IOManagerInternals.h
=====================================
@@ -30,6 +30,11 @@
#include "BeginPrivate.h"
+/* Forward reference */
+struct _CapIOManagerShared;
+typedef struct _CapIOManagerShared CapIOManagerShared;
+
+
/* The per-capability data structures belonging to the I/O manager.
*
* It can be accessed as cap->iomgr. Or given just the iomgr, you can access
@@ -42,9 +47,12 @@
*/
struct _CapIOManager {
- /* Back reference to the containing capability */
+ /* Back reference to the containing capability */
Capability *cap;
+ /* Data shared between the I/O manager instances across all capabilities */
+ CapIOManagerShared *shared;
+
#if defined(IOMGR_ENABLED_SELECT)
/* Thread queue for threads blocked on I/O completion. */
StgTSO *blocked_queue_hd;
@@ -96,6 +104,24 @@ struct _CapIOManager {
};
+
+/* Much like above, but for I/O manager data that is shared between the
+ * I/O manager instances for all the capabilities.
+ *
+ * This gets allocated and linked by the shared IOManager.c code, and should be
+ * initialised by the individual I/O manager's init function but only for cap 0.
+ */
+struct _CapIOManagerShared {
+
+#if defined(IOMGR_ENABLED_MIO_POSIX)
+ /* The timer manager control pipe (write end). This is the pipe into which
+ * we will send our signals.
+ */
+ int timer_manager_control_wr_fd;
+#endif
+};
+
+
/* Fill in the outcome and result/error on the TSO's stack frame.
* The TSO must be blocked using one of the stg_block_io_* blocking actions.
*
=====================================
rts/RtsFlags.c
=====================================
@@ -535,9 +535,8 @@ usage_text[] = {
" --install-seh-handlers=<yes|no>",
" Install exception handlers (default: yes)",
" --generate-crash-dumps",
-" Generate Windows crash dumps, requires exception handlers",
-" to be installed. Implies --install-signal-handlers=yes.",
-" (default: no)",
+" Generate Windows crash dumps (default: no).",
+" Implies --install-seh-handlers=yes",
" --generate-stack-traces=<yes|no>",
" Generate a stack trace when your application encounters a",
" fatal error. When symbols are available an attempt will be",
=====================================
rts/RtsSignals.h
=====================================
@@ -15,13 +15,6 @@
#include "BeginPrivate.h"
-/*
- * Function: initUserSignals()
- *
- * Initialize the console handling substrate.
- */
-void initUserSignals(void);
-
/*
* Function: initDefaultHandlers()
*
@@ -31,24 +24,6 @@ void initUserSignals(void);
void initDefaultHandlers(void);
void resetDefaultHandlers(void);
-void freeSignalHandlers(void);
-
-/* Tear down and shut down user signal processing.
- * This is called *after* freeSignalHandlers, but unconditionally!
- * TODO: unify this and freeSignalHandlers together, and make them make sense!
- */
-void finiUserSignals(void);
-
-/*
- * Function: startPendingSignalHandlers()
- *
- * If there are any queued up posix signals or win32 console events, run the
- * handlers associated with them. This is used by some in-RTS I/O managers.
- */
-#if !defined(THREADED_RTS)
-void startPendingSignalHandlers(Capability *cap);
-#endif
-
#include "EndPrivate.h"
#endif /* RTS_USER_SIGNALS */
=====================================
rts/RtsStartup.c
=====================================
@@ -423,12 +423,8 @@ hs_init_ghc(int *argc, char **argv[], RtsConfig rts_config)
unpauseTimer();
#if defined(RTS_USER_SIGNALS)
- if (RtsFlags.MiscFlags.install_signal_handlers) {
- /* Initialise the user signal handler set */
- initUserSignals();
- /* Set up handler to run on SIGINT, etc. */
- initDefaultHandlers();
- }
+ /* Set up handler to run on SIGINT, etc. */
+ initDefaultHandlers();
#endif
startIOManager();
@@ -510,12 +506,6 @@ hs_exit_(bool wait_foreign)
}
runAllCFinalizers(nonmoving_weak_ptr_list);
-#if defined(RTS_USER_SIGNALS)
- if (RtsFlags.MiscFlags.install_signal_handlers) {
- freeSignalHandlers();
- }
-#endif
-
/* We rely on the guarantee that exitTimer stops the timer synchronously,
* which ensures the timer handler does not get run again after this point.
* We are about to start freeing resources used by the timer handler (like
@@ -540,10 +530,8 @@ hs_exit_(bool wait_foreign)
#endif
#if defined(RTS_USER_SIGNALS)
- if (RtsFlags.MiscFlags.install_signal_handlers) {
- // uninstall signal handlers
- resetDefaultHandlers();
- }
+ // uninstall signal handlers
+ resetDefaultHandlers();
#endif
/* stop timing the shutdown, we're about to print stats */
@@ -623,10 +611,6 @@ hs_exit_(bool wait_foreign)
hs_restoreConsoleCP();
#endif
-#if defined(RTS_USER_SIGNALS)
- finiUserSignals ();
-#endif
-
/* tear down statistics subsystem */
stat_exit();
@@ -725,10 +709,12 @@ exitBySignal(int sig)
dfl.sa_handler = SIG_DFL;
(void)sigaction(sig, &dfl, NULL);
- // Then we unblock the signal so we can deliver it to ourselves
+ // Then we unmask the signal so we can deliver it to ourselves.
+ // This is a (rare) valid use of signal mask manipulation.
+ // See Note [pthread_sigmask and the ineffectiveness of masking signals].
sigemptyset(&sigset);
sigaddset(&sigset, sig);
- sigprocmask(SIG_UNBLOCK, &sigset, NULL);
+ pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
switch (sig) {
case SIGSTOP: case SIGTSTP: case SIGTTIN: case SIGTTOU: case SIGCONT:
=====================================
rts/configure.ac
=====================================
@@ -221,10 +221,6 @@ AC_LINK_IFELSE([AC_LANG_CALL([], [printf\$LDBLStub])],
FP_CHECK_PTHREAD_FUNCS
-dnl ** check for eventfd which is needed by the I/O manager
-AC_CHECK_HEADERS([sys/eventfd.h])
-AC_CHECK_FUNCS([eventfd])
-
AC_CHECK_FUNCS([getpid getuid raise])
dnl large address space support (see rts/include/rts/storage/MBlock.h)
@@ -359,6 +355,10 @@ dnl -------------------------------------------------------------------
dnl Here we check for platform constraints. The result is the CPP flag
dnl IOMGR_BUILD_<name> and a EnableIOManager<Name> var for use here later.
+dnl Check for functions (optionally) used by several I/O managers
+AC_CHECK_HEADERS([sys/eventfd.h])
+AC_CHECK_FUNCS([eventfd pipe2])
+
GHC_IOMANAGER_ENABLE([select], [EnableIOManagerSelect], [IOMGR_BUILD_SELECT],
[if test "$HostOS" = "mingw32"; then
EnableIOManagerSelect=NO
=====================================
rts/include/Rts.h
=====================================
@@ -283,10 +283,6 @@ void exitHeapOverflow(void) STG_NORETURN;;
void stg_exit(int n) STG_NORETURN;
-#if !defined(mingw32_HOST_OS)
-int stg_sig_install (int, int, void *);
-#endif
-
/* -----------------------------------------------------------------------------
Ways
-------------------------------------------------------------------------- */
=====================================
rts/include/rts/BlockSignals.h
=====================================
@@ -13,16 +13,25 @@
#pragma once
-/* Used by runProcess() in the process package
+#if !defined(mingw32_HOST_OS)
+
+/* Used by runProcess() in the process package, but this is almost certainly
+ * a bad idea.
+ *
+ * See also:
+ * + https://github.com/haskell/process/issues/384
+ * + https://github.com/haskell/process/issues/385
+ * + GHC issue #27968
+ * + See Note [pthread_sigmask and the ineffectiveness of masking signals]
*/
/*
* Function: blockUserSignals()
*
- * Temporarily block the delivery of further console events. Needed to
- * avoid race conditions when GCing the queue of outstanding handlers or
- * when emptying the queue by running the handlers.
+ * Temporarily block the delivery of further user-registered posix signals.
*
+ * This is exported from the RTS but should no longer be used. It is likely to
+ * be removed in future.
*/
void blockUserSignals(void);
@@ -32,3 +41,5 @@ void blockUserSignals(void);
* The inverse of blockUserSignals(); re-enable the deliver of console events.
*/
void unblockUserSignals(void);
+
+#endif
=====================================
rts/include/rts/IOInterface.h
=====================================
@@ -33,6 +33,7 @@ void ioManagerFinished (void);
void setIOManagerControlFd (uint32_t cap_no, int fd);
void setTimerManagerControlFd(int fd);
+int stg_sig_install(int sig, int spi, void *mask);
#endif
=====================================
rts/posix/IOManSignals.c
=====================================
@@ -0,0 +1,389 @@
+/* -----------------------------------------------------------------------------
+ *
+ * (c) The GHC Team, 1998-2005
+ *
+ * Signal processing / handling.
+ *
+ * ---------------------------------------------------------------------------*/
+
+#include "rts/PosixSource.h"
+#include "Rts.h"
+
+#if defined(RTS_USER_SIGNALS)
+
+#include "RtsSignals.h"
+#include "IOManSignals.h"
+#include "RtsUtils.h"
+#include "Schedule.h"
+#include "Prelude.h"
+#include "ThreadLabels.h"
+#include "Libdw.h"
+#include "MIO.h"
+
+#include <unistd.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <string.h>
+
+/* -----------------------------------------------------------------------------
+ * The table of signal handlers
+ * -------------------------------------------------------------------------- */
+
+/* SUP: The type of handlers is a little bit, well, doubtful... */
+StgInt *signal_handlers = NULL; /* Dynamically grown array of signal handlers */
+static StgInt nHandlers = 0; /* Size of handlers array */
+
+static sigset_t userSignals;
+static sigset_t savedSignals;
+
+#if defined(THREADED_RTS)
+static Mutex sig_mutex; // protects signal_handlers, nHandlers
+#endif
+
+/* -----------------------------------------------------------------------------
+ * Initialisation / deinitialisation
+ * -------------------------------------------------------------------------- */
+
+void
+initUserSignals(void)
+{
+ sigemptyset(&userSignals);
+#if defined(THREADED_RTS)
+ initMutex(&sig_mutex);
+#endif
+}
+
+void finiUserSignals(void)
+{
+ if (signal_handlers != NULL) {
+ stgFree(signal_handlers);
+ signal_handlers = NULL;
+ nHandlers = 0;
+ }
+#if defined(THREADED_RTS)
+ closeMutex(&sig_mutex);
+#endif
+};
+
+/* -----------------------------------------------------------------------------
+ * Allocate/resize the table of signal handlers.
+ * -------------------------------------------------------------------------- */
+
+static void
+more_handlers(int sig)
+{
+ StgInt i;
+
+ if (sig < nHandlers)
+ return;
+
+ if (signal_handlers == NULL)
+ signal_handlers = (StgInt *)stgMallocBytes((sig + 1) * sizeof(StgInt),
+ "more_handlers");
+ else
+ signal_handlers = (StgInt *)stgReallocBytes(signal_handlers,
+ (sig + 1) * sizeof(StgInt),
+ "more_handlers");
+
+ for(i = nHandlers; i <= sig; i++)
+ // Fill in the new slots with default actions
+ signal_handlers[i] = STG_SIG_DFL;
+
+ 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
+ * -------------------------------------------------------------------------- */
+
+void
+blockUserSignals(void)
+{
+ pthread_sigmask(SIG_BLOCK, &userSignals, &savedSignals);
+}
+
+void
+unblockUserSignals(void)
+{
+ pthread_sigmask(SIG_SETMASK, &savedSignals, NULL);
+}
+
+/* -----------------------------------------------------------------------------
+ * Install a Haskell signal handler.
+ *
+ * We should really do this in Haskell in GHC.Conc, and share the
+ * signal_handlers array with the one there.
+ *
+ * -------------------------------------------------------------------------- */
+
+int
+installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
+ SignalHandler signal_handler)
+{
+ ACQUIRE_LOCK(&sig_mutex);
+
+ // Use sigaddset as a way to validate the signal number.
+ // We do not use the resulting signal set.
+ {
+ sigset_t signals;
+ sigemptyset(&signals);
+ if (sig < 0 || sigaddset(&signals, sig)) {
+ RELEASE_LOCK(&sig_mutex);
+ return STG_SIG_ERR;
+ }
+ }
+
+ more_handlers(sig);
+
+ StgInt previous_spi = signal_handlers[sig];
+
+ struct sigaction action;
+ memset(&action, 0, sizeof(struct sigaction));
+ action.sa_flags = 0;
+
+ switch(spi) {
+ case STG_SIG_IGN:
+ action.sa_handler = SIG_IGN;
+ break;
+
+ case STG_SIG_DFL:
+ action.sa_handler = SIG_DFL;
+ break;
+
+ case STG_SIG_RST:
+ action.sa_flags |= SA_RESETHAND;
+ /* fall through */
+ case STG_SIG_HAN:
+ action.sa_sigaction = signal_handler;
+ action.sa_flags |= SA_SIGINFO;
+ break;
+
+ default:
+ barf("stg_sig_install: bad spi");
+ }
+
+ if (mask != NULL) {
+ action.sa_mask = *mask;
+ } else {
+ sigemptyset(&action.sa_mask);
+ }
+
+ action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
+
+ if (sigaction(sig, &action, NULL)) {
+ errorBelch("sigaction");
+ RELEASE_LOCK(&sig_mutex);
+ return STG_SIG_ERR;
+ }
+
+ signal_handlers[sig] = spi;
+
+ switch(spi) {
+ case STG_SIG_RST:
+ case STG_SIG_HAN:
+ sigaddset(&userSignals, sig);
+ break;
+
+ default:
+ sigdelset(&userSignals, sig);
+ break;
+ }
+
+ RELEASE_LOCK(&sig_mutex);
+ return previous_spi;
+}
+
+/* -----------------------------------------------------------------------------
+ * 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. */
+ // TODO: this could be improved to use a GC'd pinned byte array, but this
+ // would need coordination with the unix library.
+ siginfo_t *info_copy = stgMallocBytes(sizeof(siginfo_t), "startSignalHandler");
+ *info_copy = *info;
+
+ /* The info structure can be zero on solaris. So at least set the signo. */
+ info_copy->si_signo = sig;
+
+ debugTrace(DEBUG_iomanager,
+ "Handling pending signal, no %d", info_copy->si_signo);
+
+ StgTSO *t =
+ createIOThread(cap,
+ RtsFlags.GcFlags.initialStkSize,
+ rts_apply(cap,
+ rts_apply(cap,
+ runHandlersPtr_closure,
+ rts_mkPtr(cap, info_copy)),
+ rts_mkInt(cap, info_copy->si_signo)));
+ scheduleThread(cap, t);
+ setThreadLabel(cap, t, "signal handler thread");
+}
+
+void newNonBlockingPipe(int *pipe_fd_r, int *pipe_fd_w)
+{
+ int pipefd[2];
+#if defined(HAVE_PIPE2)
+ int res = pipe2(pipefd, O_CLOEXEC | O_NONBLOCK);
+#else
+ int res = pipe(pipefd);
+#endif
+ if (RTS_UNLIKELY(res < 0)) {
+ sysErrorBelch("newNonBlockingPipe");
+ stg_exit(EXIT_FAILURE);
+ }
+#if !defined(HAVE_PIPE2)
+ for (int i = 0; i < 2; i++) {
+ int res1 = fcntl(pipefd[i], F_SETFD, FD_CLOEXEC);
+ int res2 = fcntl(pipefd[i], F_SETFL, O_NONBLOCK);
+ if (RTS_UNLIKELY(res1 < 0 || res2 < 0)) {
+ sysErrorBelch("newFdWakeup fcntl()");
+ stg_exit(EXIT_FAILURE);
+ }
+ }
+#endif
+ *pipe_fd_r = pipefd[0]; /* read end */
+ *pipe_fd_w = pipefd[1]; /* write end */
+}
+
+/* Write the signal number and siginfo into the given fd (which is
+ * typically a pipe in non-blocking mode).
+ */
+void writeSignalInfoToPipe(int sig, siginfo_t *info, int fd)
+{
+ StgWord8 buf[sizeof(siginfo_t) + 1];
+
+ buf[0] = sig;
+ if (info) {
+ memcpy(buf+1, info, sizeof(siginfo_t));
+ } else {
+ // info may be NULL on Solaris (see #3790)
+ memset(buf+1, 0, sizeof(siginfo_t));
+ }
+
+ int r = write(fd, buf, sizeof(siginfo_t)+1);
+ if (r == -1 && errno == EAGAIN) {
+ errorBelch("lost signal due to full pipe: %d\n", sig);
+ }
+}
+
+bool readSignalInfoFromPipe(int *sig, siginfo_t *info, int fd)
+{
+ StgWord8 buf[sizeof(siginfo_t) + 1];
+
+ int r = read(fd, buf, sizeof(siginfo_t)+1);
+
+ if (r == sizeof(siginfo_t)+1) {
+ *sig = (int) buf[0];
+ memcpy(info, buf+1, sizeof(siginfo_t));
+ return true;
+ } else {
+ // TODO: if r is >0 and < expected then we're kind of screwed
+ // it means we're all out of sync with the protocol.
+ // it _should_ never happen of course!
+ return false;
+ }
+}
+
+#endif /* RTS_USER_SIGNALS */
=====================================
rts/posix/IOManSignals.h
=====================================
@@ -0,0 +1,58 @@
+/* -----------------------------------------------------------------------------
+ *
+ * (c) The GHC Team, 1998-2005, 2026
+ *
+ * Shared utilities for I/O managers: signal handling functionality.
+ *
+ * ---------------------------------------------------------------------------*/
+
+#pragma once
+
+#if defined(RTS_USER_SIGNALS)
+
+#include <signal.h>
+
+#include "BeginPrivate.h"
+
+/*
+ * Function: initUserSignals()
+ *
+ * Initialize the console handling substrate.
+ */
+void initUserSignals(void);
+
+/* Finalise signal and console events handling.
+ */
+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);
+
+int installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
+ SignalHandler handler);
+
+/* Utilities to help with the self-pipe trick for signal handling. */
+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/MIO.c
=====================================
@@ -13,15 +13,19 @@
#include "rts/PosixSource.h"
#include "Rts.h"
+#include "IOManager.h"
+
+#if defined(IOMGR_ENABLED_MIO_POSIX)
+
+#include "MIO.h"
+#include "IOManagerInternals.h"
+#include "IOManSignals.h"
+
#include "Schedule.h"
#include "RtsUtils.h"
#include "Prelude.h"
#include "ThreadLabels.h"
-#include "MIO.h"
-#include "IOManager.h"
-#include "IOManagerInternals.h"
-
#if defined(HAVE_ERRNO_H)
# include <errno.h>
#endif
@@ -29,62 +33,65 @@
#include <stdlib.h>
#include <unistd.h>
-// Here's the pipe into which we will send our signals
-static int timer_manager_control_wr_fd = -1;
-// TODO: Eliminate these globals. Put then into the CapIOManager, but the
-// problem is these are shared across all caps, not per cap.
-
#define IO_MANAGER_DIE 0xfe
+void initCapabilityIOManagerMIO(CapIOManager *iomgr)
+{
+ iomgr->control_fd = -1;
+ if (iomgr->cap == 0) {
+ iomgr->shared->timer_manager_control_wr_fd = -1;
+ }
+}
+
void setTimerManagerControlFd(int fd) {
- RELAXED_STORE(&timer_manager_control_wr_fd, fd);
+ int *timer_manager_control_wr_fd =
+ &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
+ RELAXED_STORE(timer_manager_control_wr_fd, fd);
}
-#if defined(THREADED_RTS)
-void timerManagerNotifySignal(int sig, siginfo_t *info)
+static void signal_handler(int sig, siginfo_t *info, void *p STG_UNUSED)
{
- StgWord8 buf[sizeof(siginfo_t) + 1];
- int r;
-
- buf[0] = sig;
- if (info == NULL) {
- // info may be NULL on Solaris (see #3790)
- memset(buf+1, 0, sizeof(siginfo_t));
- } else {
- memcpy(buf+1, info, sizeof(siginfo_t));
- }
-
- int timer_control_fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
+ /* In the signal handler we do not have Capability context, so access the
+ * control fd via MainCapability.
+ */
+ int *timer_manager_control_wr_fd =
+ &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
+ int timer_control_fd = RELAXED_LOAD(timer_manager_control_wr_fd);
if (0 <= timer_control_fd)
{
- r = write(timer_control_fd, buf, sizeof(siginfo_t)+1);
- if (r == -1 && errno == EAGAIN) {
- errorBelch("lost signal due to full pipe: %d\n", sig);
- }
+ writeSignalInfoToPipe(sig, info, timer_control_fd);
}
// If the IO manager hasn't told us what the FD of the write end
// of its pipe is, there's not much we can do here, so just ignore
// the signal..
}
-#endif
+int installUserSignalHandlerMIO(int sig, int spi, void *mask,
+ bool nocldstop)
+{
+ return installSignalHandler(sig, spi, mask, nocldstop, signal_handler);
+}
-#if defined(THREADED_RTS)
void
ioManagerDie (void)
{
+ finiUserSignals();
+
+ int *timer_manager_control_wr_fd =
+ &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
+
StgWord8 byte = (StgWord8)IO_MANAGER_DIE;
uint32_t i;
int r;
{
// Shut down timer manager
- const int fd = RELAXED_LOAD(&timer_manager_control_wr_fd);
+ const int fd = RELAXED_LOAD(timer_manager_control_wr_fd);
if (0 <= fd) {
r = write(fd, &byte, 1);
if (r == -1) { sysErrorBelch("ioManagerDie: write"); }
- RELAXED_STORE(&timer_manager_control_wr_fd, -1);
+ RELAXED_STORE(timer_manager_control_wr_fd, -1);
}
}
@@ -110,13 +117,23 @@ ioManagerStartCap (Capability **cap)
void
ioManagerStart (void)
{
+ initUserSignals();
+
+ int *timer_manager_control_wr_fd =
+ &MainCapability.iomgr->shared->timer_manager_control_wr_fd;
+
// Make sure the IO manager thread is running
Capability *cap;
- if (SEQ_CST_LOAD(&timer_manager_control_wr_fd) < 0) {
+ if (SEQ_CST_LOAD(timer_manager_control_wr_fd) < 0) {
cap = rts_lock();
ioManagerStartCap(&cap);
rts_unlock(cap);
}
}
-#endif
+#else // !IOMGR_ENABLED_MIO_POSIX
+
+/* This is exported from the RTS unconditionally, but only used for MIO. */
+void setTimerManagerControlFd(int fd) { /* do nothing */ }
+
+#endif // IOMGR_ENABLED_MIO_POSIX
=====================================
rts/posix/MIO.h
=====================================
@@ -19,11 +19,14 @@
/* Communicating with the IO manager thread (see GHC.Conc).
*/
#if defined(THREADED_RTS)
+void initCapabilityIOManagerMIO(CapIOManager *iomgr);
+
void ioManagerDie (void);
void ioManagerStart (void);
void ioManagerStartCap (/* inout */ Capability **cap);
-void timerManagerNotifySignal(int sig, siginfo_t *info);
+int installUserSignalHandlerMIO(int sig, int spi, void *mask,
+ bool nocldstop);
#endif
#include "EndPrivate.h"
=====================================
rts/posix/Poll.c
=====================================
@@ -23,7 +23,6 @@
#include "Trace.h"
#include "Poll.h"
-#include "RtsSignals.h"
#include <limits.h>
#include <errno.h>
@@ -39,6 +38,7 @@
#endif
#include "IOManagerInternals.h"
+#include "IOManSignals.h"
#include "Timeout.h"
#include "FdWakeup.h"
@@ -158,11 +158,23 @@ void initCapabilityIOManagerPoll(CapIOManager *iomgr)
.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();
+ }
+#endif
}
void freeCapabilityIOManagerPoll(CapIOManager *iomgr)
{
+#if defined(RTS_USER_SIGNALS)
+ if (iomgr->cap->no == 0) {
+ finiUserSignals();
+ }
+#endif
stgFree(iomgr->full_poll_table);
#if defined(HAVE_PREEMPTION)
closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
@@ -573,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+int installUserSignalHandlerPoll(int sig, int spi, void *mask,
+ bool nocldstop)
+{
+ return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+}
+
+
/* Helper function to double the size of the aiop_table and aiop_poll_table.
*/
static bool enlargeTables(CapIOManager *iomgr)
=====================================
rts/posix/Poll.h
=====================================
@@ -34,6 +34,9 @@ void pollCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
bool awaitCompletedTimeoutsOrIOPoll(CapIOManager *iomgr);
void interruptIOManagerPoll(CapIOManager *iomgr);
+/* Signals */
+int installUserSignalHandlerPoll(int sig, int spi, void *mask, bool nocldstop);
+
#endif /* IOMGR_ENABLED_POLL */
#include "EndPrivate.h"
=====================================
rts/posix/Select.c
=====================================
@@ -12,13 +12,13 @@
#include "rts/PosixSource.h"
#include "Rts.h"
-#include "RtsSignals.h"
#include "Schedule.h"
#include "Prelude.h"
#include "RtsUtils.h"
#include "Capability.h"
#include "Select.h"
#include "IOManagerInternals.h"
+#include "IOManSignals.h"
#include "Stats.h"
#include "GetTime.h"
#include "FdWakeup.h"
@@ -72,10 +72,16 @@ void initCapabilityIOManagerSelect(CapIOManager *iomgr)
barf("initCapabilityIOManagerSelect: fds out of select range");
}
#endif
+#if defined(RTS_USER_SIGNALS)
+ initUserSignals();
+#endif
}
void freeCapabilityIOManagerSelect(CapIOManager *iomgr USED_IF_PREEMPTION)
{
+#if defined(RTS_USER_SIGNALS)
+ finiUserSignals();
+#endif
#if defined(HAVE_PREEMPTION)
closeFdWakeup(iomgr->interrupt_fd_r, iomgr->interrupt_fd_w);
#endif
@@ -518,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait)
return !interrupt;
}
+int installUserSignalHandlerSelect(int sig, int spi, void *mask,
+ bool nocldstop)
+{
+ return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+}
+
#endif /* IOMGR_ENABLED_SELECT */
=====================================
rts/posix/Select.h
=====================================
@@ -21,6 +21,8 @@ void wakeupIOManagerSelect(CapIOManager *iomgr);
bool awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait);
void interruptIOManagerSelect(CapIOManager *iomgr);
+int installUserSignalHandlerSelect(int sig, int spi, void *mask,
+ bool nocldstop);
#include "EndPrivate.h"
=====================================
rts/posix/SelectBis.c
=====================================
@@ -25,12 +25,12 @@
#include "Trace.h"
#include "SelectBis.h"
-#include "RtsSignals.h"
#include <errno.h>
#include <sys/select.h>
#include "IOManagerInternals.h"
+#include "IOManSignals.h"
#include "Timeout.h"
#include "FdWakeup.h"
@@ -153,11 +153,23 @@ void initCapabilityIOManagerSelectBis(CapIOManager *iomgr)
iomgr->rfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
iomgr->wfds = stgMallocBytes(sizeof (fd_set), "IOManagerSelectBis");
+
+#if defined(RTS_USER_SIGNALS)
+ /* Signal handlers are shared across all caps. Only init once. */
+ if (iomgr->cap->no == 0) {
+ initUserSignals();
+ }
+#endif
}
void freeCapabilityIOManagerSelectBis(CapIOManager *iomgr)
{
+#if defined(RTS_USER_SIGNALS)
+ if (iomgr->cap->no == 0) {
+ finiUserSignals();
+ }
+#endif
stgFree(iomgr->rfds);
stgFree(iomgr->wfds);
#if defined(HAVE_PREEMPTION)
@@ -583,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+int installUserSignalHandlerSelectBis(int sig, int spi, void *mask,
+ bool nocldstop)
+{
+ return installSignalHandler(sig, spi, mask, nocldstop, generic_handler);
+}
+
+
/* Helper function to double the size of the aiop_table.
*/
static bool enlargeTables(CapIOManager *iomgr)
=====================================
rts/posix/SelectBis.h
=====================================
@@ -55,6 +55,10 @@ void pollCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
bool awaitCompletedTimeoutsOrIOSelectBis(CapIOManager *iomgr);
void interruptIOManagerSelectBis(CapIOManager *iomgr);
+/* Signals */
+int installUserSignalHandlerSelectBis(int sig, int spi, void *mask,
+ bool nocldstop);
+
#endif /* IOMGR_ENABLED_SELECTBIS */
#include "EndPrivate.h"
=====================================
rts/posix/Signals.c
=====================================
@@ -9,6 +9,8 @@
#include "rts/PosixSource.h"
#include "Rts.h"
+#if defined(RTS_USER_SIGNALS)
+
#include "RtsSignals.h"
#include "RtsUtils.h"
#include "Schedule.h"
@@ -25,344 +27,46 @@
# endif
#endif
-#if defined(HAVE_UNISTD_H)
-# include <unistd.h>
-#endif
-
-#if defined(HAVE_SIGNAL_H)
-# include <signal.h>
-#endif
-
-#if defined(HAVE_TERMIOS_H)
+#include <unistd.h>
+#include <signal.h>
#include <termios.h>
-#endif
-
#include <string.h>
-/* This curious flag is provided for the benefit of the Haskell binding
- * to POSIX.1 to control whether or not to include SA_NOCLDSTOP when
- * installing a SIGCHLD handler.
- */
-HsInt nocldstop = 0;
-
-/* -----------------------------------------------------------------------------
- * The table of signal handlers
- * -------------------------------------------------------------------------- */
-
-#if defined(RTS_USER_SIGNALS)
-
-/* SUP: The type of handlers is a little bit, well, doubtful... */
-StgInt *signal_handlers = NULL; /* Dynamically grown array of signal handlers */
-static StgInt nHandlers = 0; /* Size of handlers array */
-
-static sigset_t userSignals;
-static sigset_t savedSignals;
-
-#if defined(THREADED_RTS)
-static Mutex sig_mutex; // protects signal_handlers, nHandlers
-#endif
-
-/* -----------------------------------------------------------------------------
- * Initialisation / deinitialisation
- * -------------------------------------------------------------------------- */
-
-void
-initUserSignals(void)
-{
- sigemptyset(&userSignals);
-#if defined(THREADED_RTS)
- initMutex(&sig_mutex);
-#endif
-}
-
-void
-freeSignalHandlers(void) {
- if (signal_handlers != NULL) {
- stgFree(signal_handlers);
- signal_handlers = NULL;
- nHandlers = 0;
- }
-#if defined(THREADED_RTS)
- closeMutex(&sig_mutex);
-#endif
-}
-
-void finiUserSignals(void)
-{
- /* nothing */
-};
-
-/* -----------------------------------------------------------------------------
- * Allocate/resize the table of signal handlers.
- * -------------------------------------------------------------------------- */
-
-static void
-more_handlers(int sig)
-{
- StgInt i;
-
- if (sig < nHandlers)
- return;
-
- if (signal_handlers == NULL)
- signal_handlers = (StgInt *)stgMallocBytes((sig + 1) * sizeof(StgInt),
- "more_handlers");
- else
- signal_handlers = (StgInt *)stgReallocBytes(signal_handlers,
- (sig + 1) * sizeof(StgInt),
- "more_handlers");
-
- for(i = nHandlers; i <= sig; i++)
- // Fill in the new slots with default actions
- signal_handlers[i] = STG_SIG_DFL;
-
- 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
+/* Note [pthread_sigmask and the ineffectiveness of masking signals]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
- * Places the requested handler on a stack of pending handlers to be
- * started up at the next context switch.
- * -------------------------------------------------------------------------- */
-
-static void
-generic_handler(int sig USED_IF_THREADS,
- siginfo_t *info,
- void *p STG_UNUSED)
-{
-#if defined(THREADED_RTS)
- //TODO: This calls MIO directly. We should go via IOManager API.
- // The IOManager API should be extended to cover signals.
- timerManagerNotifySignal(sig, info);
-#else /* not THREADED_RTS */
-
- /* 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
- * -------------------------------------------------------------------------- */
-
-void
-blockUserSignals(void)
-{
- sigprocmask(SIG_BLOCK, &userSignals, &savedSignals);
-}
-
-void
-unblockUserSignals(void)
-{
- sigprocmask(SIG_SETMASK, &savedSignals, NULL);
-}
-
-/* -----------------------------------------------------------------------------
- * Install a Haskell signal handler.
+ * Relevant posix signals facts:
*
- * We should really do this in Haskell in GHC.Conc, and share the
- * signal_handlers array with the one there.
+ * + Every (OS) thread has a signal mask, which can be manipulated by that
+ * thread itself (though sigprocmask on macOS breaks this).
+ * + Signal masks are inherited when new threads or processes are created (or
+ * can be set as an attribute in posix spawn on some platforms).
+ * + Most signals are processes directed, while some are thread-directed.
+ * + Process-directed signals will be delivered to _any_ thread that does not
+ * have the signal masked.
*
- * -------------------------------------------------------------------------- */
-
-int
-stg_sig_install(int sig, int spi, void *mask)
-{
- sigset_t signals, osignals;
- StgInt previous_spi;
-
- struct sigaction action;
- memset(&action, 0, sizeof(struct sigaction));
-
- ACQUIRE_LOCK(&sig_mutex);
-
- // Block the signal until we figure out what to do
- // Count on this to fail if the signal number is invalid
- if (sig < 0 ||
- sigemptyset(&signals) ||
- sigaddset(&signals, sig) ||
- sigprocmask(SIG_BLOCK, &signals, &osignals)) {
- RELEASE_LOCK(&sig_mutex);
- return STG_SIG_ERR;
- }
-
- more_handlers(sig);
-
- previous_spi = signal_handlers[sig];
-
- action.sa_flags = 0;
-
- switch(spi) {
- case STG_SIG_IGN:
- action.sa_handler = SIG_IGN;
- break;
-
- case STG_SIG_DFL:
- action.sa_handler = SIG_DFL;
- break;
-
- case STG_SIG_RST:
- action.sa_flags |= SA_RESETHAND;
- /* fall through */
- case STG_SIG_HAN:
- action.sa_sigaction = generic_handler;
- action.sa_flags |= SA_SIGINFO;
- break;
-
- default:
- barf("stg_sig_install: bad spi");
- }
-
- if (mask != NULL)
- action.sa_mask = *(sigset_t *)mask;
- else
- sigemptyset(&action.sa_mask);
-
- action.sa_flags |= sig == SIGCHLD && nocldstop ? SA_NOCLDSTOP : 0;
-
- if (sigaction(sig, &action, NULL))
- {
- errorBelch("sigaction");
- RELEASE_LOCK(&sig_mutex);
- return STG_SIG_ERR;
- }
-
- signal_handlers[sig] = spi;
-
- switch(spi) {
- case STG_SIG_RST:
- case STG_SIG_HAN:
- sigaddset(&userSignals, sig);
- break;
-
- default:
- sigdelset(&userSignals, sig);
- break;
- }
-
- if (sigprocmask(SIG_SETMASK, &osignals, NULL))
- {
- errorBelch("sigprocmask");
- RELEASE_LOCK(&sig_mutex);
- return STG_SIG_ERR;
- }
-
- RELEASE_LOCK(&sig_mutex);
- return previous_spi;
-}
-
-/* -----------------------------------------------------------------------------
- * 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 = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers");
- // freed by runHandler
- memcpy(info, next_pending_handler, sizeof(siginfo_t));
-
- debugTrace(DEBUG_iomanager,
- "Handling pending signal, no %d", info->si_signo);
-
- StgTSO *t =
- createIOThread(cap,
- RtsFlags.GcFlags.initialStkSize,
- rts_apply(cap,
- rts_apply(cap,
- runHandlersPtr_closure,
- rts_mkPtr(cap, info)),
- rts_mkInt(cap, info->si_signo)));
- scheduleThread(cap, t);
- setThreadLabel(cap, t, "signal handler thread");
- }
-
- unblockUserSignals();
-}
-#endif
-
-#else /* !RTS_USER_SIGNALS */
-StgInt
-stg_sig_install(StgInt sig STG_UNUSED,
- StgInt spi STG_UNUSED,
- void* mask STG_UNUSED)
-{
- //barf("User signals not supported");
- return STG_SIG_DFL;
-}
-
-#endif
+ * The important consequence of the "any thread will do" property is that:
+ *
+ * + it is generally _effective_ to unmask a signal in a thread, because at
+ * least the calling thread has the signal unmasked; however
+ * + it is generally _ineffective_ to mask a signal in a thread, because
+ * the signal will likely still be unmasked in some other thread and thus
+ * be delivered there.
+ *
+ * Thus we should avoid use the masking signals as a mechanism that we rely on,
+ * since it will not in general be effective. In best-effort cases where there
+ * is a reasonable likelyhood that the calling thread is the only thread with
+ * signals unmasked then it is ok.
+ *
+ * Closely related: always avoid sigprocmask, and prefer pthread_sigmask.
+ * The older function is sigprocmask but it's meaning in multi-threaded
+ * programs is explicitly unspecified by Posix. On Linux it modifies the
+ * calling thread's signal mask, but on macOS it modifies the signal mask of
+ * every thread in the process, leading to chaos. The new function is
+ * pthread_sigmask which is well defined and operates only on the signal mask
+ * of the calling thread.
+ */
-#if defined(RTS_USER_SIGNALS)
/* -----------------------------------------------------------------------------
* SIGINT handler.
*
@@ -504,8 +208,8 @@ set_sigtstp_action (bool handle)
void
initDefaultHandlers(void)
{
- // N.B. We can't use initializers here as CentOS's ancient toolchain throws
- // spurious warnings. See #23577.
+ if (!RtsFlags.MiscFlags.install_signal_handlers) return;
+
struct sigaction action, oact;
memset(&oact, 0, sizeof(struct sigaction));
memset(&action, 0, sizeof(struct sigaction));
@@ -569,6 +273,8 @@ initDefaultHandlers(void)
void
resetDefaultHandlers(void)
{
+ if (!RtsFlags.MiscFlags.install_signal_handlers) return;
+
struct sigaction action;
action.sa_handler = SIG_DFL;
=====================================
rts/posix/TTY.c
=====================================
@@ -46,14 +46,21 @@ resetTerminalSettings (void)
#if HAVE_TERMIOS_H
// Reset the terminal settings on the standard file descriptors,
// if we changed them. See System.Posix.Internals.tcSetAttr for
- // more details, including the reason we temporarily disable
+ // more details, including the reason we temporarily mask
// SIGTTOU here.
+ //
+ // This masking best effort only: it will only be effective if the
+ // calling thread is the only thread left in the process with
+ // signals unmasked. This should typically be true when this is
+ // called during shutdown, but there are many ways to make it false.
+ //
+ // See Note [pthread_sigmask and the ineffectiveness of masking signals].
{
int fd;
sigset_t sigset, old_sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGTTOU);
- sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
+ pthread_sigmask(SIG_BLOCK, &sigset, &old_sigset);
for (fd = 0; fd <= 2; fd++) {
struct termios* ts =
(struct termios*)__hscore_get_saved_termios(fd);
@@ -61,7 +68,7 @@ resetTerminalSettings (void)
tcsetattr(fd,TCSANOW,ts);
}
}
- sigprocmask(SIG_SETMASK, &old_sigset, NULL);
+ pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
}
#endif
}
=====================================
rts/posix/Ticker.c
=====================================
@@ -242,7 +242,7 @@ initTicker (Time interval, TickProc handle_tick)
* Create the thread with all blockable signals blocked, leaving signal
* handling to the main and/or other threads. This is especially useful in
* the non-threaded runtime, where applications might expect sigprocmask(2)
- * to effectively block signals.
+ * or pthread_sigmask(3) to effectively block signals.
*/
#if defined(HAVE_SIGNAL_H)
sigfillset(&mask);
=====================================
rts/rts.cabal
=====================================
@@ -579,6 +579,7 @@ library
c-sources: posix/GetEnv.c
posix/GetTime.c
posix/Ticker.c
+ posix/IOManSignals.c
posix/OSMem.c
posix/OSThreads.c
posix/FdWakeup.c
=====================================
rts/win32/ConsoleHandler.c
=====================================
@@ -54,11 +54,6 @@ initUserSignals(void)
return;
}
-void
-freeSignalHandlers(void) {
- /* Do nothing */
-}
-
/* Called in hs_exit to clean up resources. */
void
finiUserSignals(void)
@@ -115,6 +110,8 @@ static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
*/
void initDefaultHandlers(void)
{
+ if (!RtsFlags.MiscFlags.install_signal_handlers) return;
+
if ( !SetConsoleCtrlHandler(shutdown_handler, true) ) {
errorBelch("warning: failed to install default console handler");
}
@@ -122,33 +119,25 @@ void initDefaultHandlers(void)
void resetDefaultHandlers(void)
{
+ if (!RtsFlags.MiscFlags.install_signal_handlers) return;
+
if ( !SetConsoleCtrlHandler(shutdown_handler, false) ) {
errorBelch("warning: failed to uninstall default console handler");
}
}
-/*
- * Function: blockUserSignals()
- *
- * Temporarily block the delivery of further console events. Needed to
- * avoid race conditions when GCing the stack of outstanding handlers or
- * when emptying the stack by running the handlers.
- *
+/* Temporarily block the delivery of further console events. Needed to
+ * avoid race conditions when emptying the stack by running the handlers.
*/
-void
-blockUserSignals(void)
+static void blockUserSignals(void)
{
deliver_event = false;
}
-/*
- * Function: unblockUserSignals()
- *
- * The inverse of blockUserSignals(); re-enable the deliver of console events.
+/* The inverse of blockUserSignals(); re-enable the deliver of console events.
*/
-void
-unblockUserSignals(void)
+static void unblockUserSignals(void)
{
deliver_event = true;
}
=====================================
rts/win32/ConsoleHandler.h
=====================================
@@ -19,12 +19,31 @@
* handling code also.
*/
+/*
+ * Function: initConsoleEvents()
+ *
+ * Initialize the console handling substrate.
+ */
+void initConsoleEvents(void);
+
+/* Finalise signal and console events handling.
+ */
+void finiConsoleEvents(void);
+
#if !defined(THREADED_RTS)
/*
* under THREADED_RTS, console events are passed to the IO manager
* thread, which starts up the handler. See ThrIOManager.c.
*/
+/*
+ * Function: startPendingSignalHandlers()
+ *
+ * If there are any queued up win32 console events, run the handlers
+ * associated with them.
+ */
+void startPendingSignalHandlers(Capability *cap);
+
/*
* Function: rts_waitConsoleHandlerCompletion()
*
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0d32dcdfed5cc8318c58fb5cf7b19a…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/0d32dcdfed5cc8318c58fb5cf7b19a…
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
22 Oct '26
Simon Peyton Jones pushed to branch wip/T27994 at Glasgow Haskell Compiler / GHC
Commits:
501fad96 by Simon Peyton Jones at 2026-10-22T14:48:02+01:00
Fix niFixSubst again
- - - - -
1 changed file:
- compiler/GHC/Core/Unify.hs
Changes:
=====================================
compiler/GHC/Core/Unify.hs
=====================================
@@ -33,9 +33,8 @@ import GHC.Core.TyCon
import GHC.Core.Predicate( CanEqLHS(..), canEqLHS_maybe )
import GHC.Core.TyCon.Env
import GHC.Core.TyCo.Rep
-import GHC.Core.TyCo.FVs
+import GHC.Core.TyCo.FVs( anyShallowFreeVarsOfType )
import GHC.Core.TyCo.Compare ( eqType, tcEqType, tcEqTyConAppArgs )
-import qualified GHC.Core.TyCo.Subst as S
import GHC.Core.TyCo.Subst ( mkTvSubst )
import GHC.Core.Map.Type
import GHC.Core.Multiplicity
@@ -1071,15 +1070,16 @@ unification is much trickier than it looks, because of kinds. Consider
If we unify, we get the substitution
[ k -> *
, g -> H k (f:k) ]
+This isn't idempotent: it mentions `k` in the range.
+
To make it idempotent we don't want to get just
[ k -> *
, g -> H * (f:k) ]
-We also want to substitute inside f's kind, to get
- [ k -> *
- , g -> H k (f:*) ]
-If we don't do this, we may apply the substitution to something,
-and get an ill-formed type, i.e. one where typeKind will fail.
+because (H * (f:k)) is itself ill-kinded, and `typeKind` will fail.
This happened, for example, in #9106.
+So (yuckily) we must also substitute inside f's kind, to get
+ [ k -> *
+ , g -> H * (f:*) ]
It gets worse. In #14164 we wanted to take the fixpoint of
this substitution
@@ -1094,42 +1094,41 @@ probably rightly so).
Ugh! This whole thing is pretty horrible. We work as follows:
- 1. Start with the current substitution (which we are
- trying to fixpoint
- [ xs :-> F a (z :: a) (rest :: G a (z :: a))
- , a :-> b ]
-
- 2. Find its fixed point. Remember, substitution is shallow. That gives:
- [ xs :-> F b (z :: a) (rest :: G a (z :: a))
- , a :-> b ]
+ 1. Start with the current substitution S, which we are fixpointing
+ S = [ xs :-> F a (z :: a) (rest :: G a (z :: a))
+ , a :-> (b::*) ]
Note the 'a' lurking inside (z::a)
- 3. Take all the deep free vars of the range of the substitution:
- {a, (z::a), (rest::G a z), b}
+ 2. Find `range_tvs`, the deep free vars of the range of the substitution:
+ range_tvs = {a, (z::a), (rest::G a z), b}
- 4. Remove the variables that are bound by the substitution, leaving
- {z, rest, b}
+ 3. If none of the `range_tvs` are in the domain of the substitution, stop.
- 5. Do a `scopedSort` to put them in dependency order:
- [ b :: *, z :: a, rest :: G a z ]
+ 4. Find `free_tvs`, the `range_tvs` that are NOT in the substitution domain
+ and used `scopedSort` to put them in dependency order:
+ free_tvs = [z::a, rest::G a z, b]
- 6. Make a "re-kinding substitution" by applying the fixpoint from (2)
- to the kind of each tyvar in this list -- and then applying the
- re-kinding from earlier variables.
- [ b :-> b :: *
- , z :-> z :: b
- , rest :-> rest :: G b (z :: b) ]
- Note that rest now has the right kind
+ 5. Extend the substitution with "re-kinding bindings", mapping each free_tv
+ to the self-same variable, but with the original substitution applied
+ to its kind.
+ S_plus_rekinding = S + [ z :-> z::b
+ , rest :-> rest::G b (z::b)
+ , b :-> b]
+ Here we see the reason for putting `free_tvs` in dependency order:
+ we want the new kind of `rest` to be well-kinded, not (G b (z:a)).
+ S_plus_rekinding = S + [ z :-> z::b
+ , rest :-> rest::G b (z::b) ]
- 7. Apply this extended substitution (once) to the range of the
- fixpoint substitution from (2).
+ Optimisation: if the kind of a free_tvs is a fixpoint of the substitution,
+ no need to add it to the substitution. So we get
-In Step 6 we use the free vars from Step 2 as the initial
-in-scope set, because all of those variables appear in the
-range of the substitution, so they must all be in the in-scope
-set. But NB that the type substitution engine does not look up
-variables in the in-scope set; it is used only to ensure no
-shadowing.
+ 6. Apply S_plus_rekinding to the range of S
+ S_new = [ xs :-> F b (z::b) (rest::G b (z::b))
+ , a :-> b ]
+
+ 7. Loop back to (2)
+
+In our example, the substitution is now a fixpoint.
-}
niFixSubst, niFixSubst' :: InScopeSet -> TvSubstEnv -> Subst
@@ -1147,88 +1146,38 @@ niFixSubst in_scope tenv
where
subst = niFixSubst' in_scope tenv
-niFixSubst' in_scope orig_tenv
- = mkTvSubst in_scope (mapVarEnv (subst_tenv rekinding_tenv) fix_tenv)
- where
- in_dom tv = tv `elemVarEnv` orig_tenv
-
- fix_tenv :: TvSubstEnv
- fix_tenv = go_fix orig_tenv -- Repeatedly apply tenv until we get a fixpoint
-
- go_fix tenv_so_far
- | anyVarEnv (anyShallowFreeVarsOfType in_dom) tenv_so_far
- = go_fix (mapVarEnv (subst_tenv tenv_so_far) tenv_so_far)
- | otherwise
- = tenv_so_far
-
- free_tvs :: [TyVar]
- free_tvs = scopedSort $
- filterOut in_dom $
- nonDetVarSetElems $
- tyCoVarsOfTypes $
- nonDetEltsUFM fix_tenv
- -- It's OK to use nonDetEltsUFM (twice) here because
- -- the exact order of free_tvs is immaterial
-
- rekinding_tenv = foldl' add_free_tv emptyTvSubstEnv free_tvs
-
- add_free_tv :: TvSubstEnv -> TyVar -> TvSubstEnv
- add_free_tv rke_so_far tv
- | isTyVar tv
- , let k' = subst_tenv rke_so_far $
- subst_tenv fix_tenv $
- tyVarKind tv
- = extendVarEnv rke_so_far tv (mkTyVarTy (setTyVarKind tv k'))
- | otherwise
- = rke_so_far
-
- subst_tenv :: TvSubstEnv -> Type -> Type
- subst_tenv tenv = substTy (mkTvSubst in_scope tenv)
-
-{-
--- | not_fixpoint = niFixSubst' in_scope (mapVarEnv (substTy subst) tenv)
- | not_fixpoint = niFixSubst' in_scope (mapVarEnv (substTy subst) tenv)
- | otherwise = tenv_subst
+niFixSubst' in_scope tenv_orig
+ | not (any in_dom range_tvs) = mkTvSubst in_scope tenv_orig
+ | otherwise = niFixSubst' in_scope tenv_new
where
- tenv_subst = mkTvSubst in_scope tenv -- This is our starting point
+ in_dom tv = tv `elemVarEnv` tenv_orig
range_tvs :: [TyVar]
range_tvs = nonDetVarSetElems $
tyCoVarsOfTypes $
- nonDetEltsUFM tenv
+ nonDetEltsUFM tenv_orig
-- It's OK to use nonDetEltsUFM (twice) here because
-- the order of range_tvs is immaterial
- not_fixpoint = any in_domain range_tvs
- in_domain tv = tv `elemVarEnv` tenv
- free_tvs = scopedSort (filterOut in_domain range_tvs)
+ free_tvs = scopedSort (filterOut in_dom range_tvs)
+
+ tenv_plus_rekinding = foldl' add_free_tv tenv_orig free_tvs
+ tenv_new = mapVarEnv (subst_tenv tenv_plus_rekinding) tenv_orig
- -- See Note [Finding the substitution fixpoint], Step 6
- rekinding_tenv = foldl' add_free_tv emptyTvSubstEnv free_tvs
add_free_tv :: TvSubstEnv -> TyVar -> TvSubstEnv
- add_free_tv rke tv
- | isTyVar tv, anyFreeVarsOfType in_domain (varType tv)
- , let tv' = updateTyVarKind (substTy tenv_subst) tv
- = extendVarEnv rke tv (mkTyVarTy tv')
+ add_free_tv so_far tv
+ | isTyVar tv
+ , let kind = tyVarKind tv
+ , anyShallowFreeVarsOfType (`elemVarEnv` so_far) kind
+ -- Shallow free vars because substitution is shallow
+ = extendVarEnv so_far tv $ mkTyVarTy $
+ setTyVarKind tv $ subst_tenv so_far kind
| otherwise
- = rke
-
- rekinding_subst = mkTvSubst in_scope rekinding_tenv
+ = so_far
- subst = mkTvSubst in_scope (rekinding_tenv `plusVarEnv`
- mapVarEnv (substTy rekinding_subst) tenv)
-
-{-
- subst = foldl' add_free_tv tenv_subst free_tvs
+ subst_tenv :: TvSubstEnv -> Type -> Type
+ subst_tenv tenv = substTy (mkTvSubst in_scope tenv)
- add_free_tv :: Subst -> TyVar -> Subst
- add_free_tv subst tv
- | isTyVar tv = extendTvSubst subst tv (mkTyVarTy tv')
- | otherwise = subst -- Ignore free coercion variables
- where
- tv' = updateTyVarKind (substTy subst) tv
--}
--}
{-
************************************************************************
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/501fad960f4993ddddb3e9047b8571f…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/501fad960f4993ddddb3e9047b8571f…
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/davide/io_manager_tests] Add test modifier to test over all IO Managers
by David Eichmann (@DavidEichmann) 22 Oct '26
by David Eichmann (@DavidEichmann) 22 Oct '26
22 Oct '26
David Eichmann pushed to branch wip/davide/io_manager_tests at Glasgow Haskell Compiler / GHC
Commits:
6fe4ea9f by David Eichmann at 2026-10-22T15:28:47+01:00
Add test modifier to test over all IO Managers
- - - - -
7 changed files:
- rts/RtsUtils.c
- testsuite/config/ghc
- testsuite/driver/runtests.py
- testsuite/driver/testglobals.py
- testsuite/driver/testlib.py
- testsuite/driver/testutil.py
- testsuite/tests/rts/all.T
Changes:
=====================================
rts/RtsUtils.c
=====================================
@@ -397,6 +397,7 @@ void printRtsInfo(const RtsConfig rts_config) {
rts_config.rts_opts != NULL ? rts_config.rts_opts : "");
selectIOManager(); /* resolve the io-manager, accounting for flags */
mkRtsInfoPair("I/O manager default", showIOManager());
+ mkRtsInfoPair("I/O managers", &IOMGRS_ENABLED_STR[1]); // skips the initial ' ' character in IOMGRS_ENABLED_STR
printf(" ]\n");
}
=====================================
testsuite/config/ghc
=====================================
@@ -257,7 +257,9 @@ def get_compiler_info():
)
except Exception as err:
- print("Exception thrown in testsuite/config/ghc.get_compiler_info: %s", err)
+ # We don't throw so that we can still discover tests before we've built
+ # GHC (i.e. config.compiler doesn't yet exist).
+ print("Exception thrown in testsuite/config/ghc.get_compiler_info: ", err)
else:
res = p.returncode == 0
@@ -315,6 +317,58 @@ def get_compiler_info():
config.ghc_th_way = "normal"
config.ghc_th_hi_ext = "hi"
+ # I/O manager discovery.
+ #
+ # Which I/O managers (GHC's `--io-manager` RTS flag) a given RTS build
+ # supports depends on the target platform and on whether the RTS is the
+ # threaded or non-threaded flavour (see rts/IOManager.h's
+ # IOMGR_ENABLED_* macros). Rather than duplicate that platform/way
+ # dependent logic here, we ask the RTS itself using `+RTS --info -RTS`
+ # and extract the "I/O managers" value from stdout.
+ def discover_io_managers_generic(extra_flags):
+ import tempfile
+
+ test_src = 'main :: IO ()\nmain = return ()\n'
+
+ res = [None]
+
+ with tempfile.TemporaryDirectory() as d:
+ src = Path(d) / 'IOMgrProbe.hs'
+ src.write_text(test_src)
+ exe = Path(d) / ('IoMgrProbe' + config.exeext)
+
+ # -rtsopts is needed so that the resulting binary will even
+ # consider an --io-manager= flag (rather than immediately
+ # rejecting all "unsafe" RTS options).
+ cmd = '{} -v0 -rtsopts -o {} {} {}'.format(
+ config.compiler,
+ exe,
+ src,
+ ' '.join(extra_flags))
+ try:
+ cp = subprocess.run(
+ cmd,
+ shell=True,
+ cwd=d,
+ stdout=subprocess.DEVNULL,
+ stderr=None if config.verbose >= 3 else subprocess.DEVNULL)
+ if cp.returncode != 0 or not exe.exists():
+ raise Exception("Error in testsuite/config/ghc.discover_io_managers: failed to compile with: {}".format(cmd))
+ res = list(discover_io_managers(exe, d))
+
+ except Exception as err:
+ # We don't throw so that we can still discover tests before we've built
+ # GHC (i.e. config.compiler doesn't yet exist).
+ print("Exception thrown in testsuite/config/ghc.discover_io_managers_generic: ", err)
+
+ return res
+
+ config.io_managers_nonthreaded = discover_io_managers_generic([])
+ config.io_managers_threaded = (
+ discover_io_managers_generic(['-threaded']) if config.ghc_with_threaded_rts
+ else [])
+ config.io_managers_all = list(set(config.io_managers_nonthreaded).union(set(config.io_managers_threaded)))
+
config.cmm_cpp_is_gcc = test_compile(
['-c'],
test_filename='test.cmm',
=====================================
testsuite/driver/runtests.py
=====================================
@@ -28,7 +28,7 @@ import subprocess
import asyncio
-from testutil import getStdout, str_warn, str_info, print_table, shorten_metric_name, str_removeprefix
+from testutil import discover_io_managers, getStdout, str_warn, str_info, print_table, shorten_metric_name, str_removeprefix
from testglobals import getConfig, ghc_env, TestConfig, t, \
TestOptions, brokens, PerfMetric
from my_typing import TestName
=====================================
testsuite/driver/testglobals.py
=====================================
@@ -126,6 +126,15 @@ class TestConfig:
self.way_flags = {} # type: Dict[WayName, List[str]]
self.way_rts_flags = {} # type: Dict[WayName, List[str]]
+ # I/O managers (GHC's `--io-manager` RTS flag) supported by the threaded runtime.
+ self.io_managers_threaded = [] # type: List[str]
+
+ # I/O managers (GHC's `--io-manager` RTS flag) supported by the non-threaded runtime.
+ self.io_managers_nonthreaded = [] # type: List[str]
+
+ # All I/O managers (GHC's `--io-manager` RTS flag)
+ self.io_managers_all = [] # type: List[str]
+
# Do we have a functional LLVM toolchain?
self.have_llvm = False
@@ -367,6 +376,14 @@ class TestOptions:
# add these ways to the default set
self.extra_ways = [] # type: List[WayName]
+ # IO Managers to test (passed as the --io-manager RTS option)
+ # * [None] - only default IO Manager
+ # * [...] - list of io managers e.g. "mio" or "poll"
+ self.io_managers = [None] # type: List[Optional[str]]
+
+ # IO Manager used in the current test case. Set before each test case. None means default IO Manager
+ self.io_manager_current = None # type: Optional[str]
+
# the result we normally expect for this test
self.expect = 'pass'
=====================================
testsuite/driver/testlib.py
=====================================
@@ -23,7 +23,7 @@ import subprocess
from testglobals import config, ghc_env, default_testopts, brokens, t, \
TestRun, TestResult, TestOptions, PerfMetric
-from testutil import strip_quotes, lndir, link_or_copy_file, passed, \
+from testutil import discover_io_managers, strip_quotes, lndir, link_or_copy_file, passed, \
failBecause, residency_testing_metrics, \
stable_perf_counters, \
PassFail, badResult, str_warn, str_removeprefix
@@ -569,6 +569,34 @@ def extra_ways( ways: List[WayName] ):
return helper
+# -----
+
+def _io_managers( name: TestName, opts, io_mgrs: List[str] ) -> None:
+ opts.io_managers = io_mgrs
+
+def io_managers( io_mgrs: List[str] ):
+ """
+ Further split test cases into the given IO Managers if available.
+ Available IO Managers are determined by the test case's way (threaded/non-threads) and OS.
+ You'll likely want to use `extra_ways(['threaded2'])` if you want to test threaded IO Managers (e.g. mio).
+ """
+ return lambda name, opts: _io_managers(name, opts, io_mgrs)
+
+def all_available_io_managers( name: TestName, opts ):
+ """
+ Further split test cases into all available IO Managers.
+ Available IO Managers are determined by the test case's way (threaded/non-threads) and OS.
+ You'll likely want to use `extra_ways(['threaded2'])` to ensure threaded IO Managers are tested (e.g. mio).
+ """
+ _io_managers(name, opts, config.io_managers_all)
+
+def all_available_io_managers_except(exclude: List[str]):
+ """
+ Further split test cases into all available (except the given excluded) IO Managers.
+ Available IO Managers are determined by the test case's way (threaded/non-threads) and OS.
+ You'll likely want to use `extra_ways(['threaded2'])` to ensure threaded IO Managers are tested (e.g. mio).
+ """
+ return lambda name, opts: _io_managers(name, opts, list(set(config.io_managers_all) - set(exclude)))
# -----
@@ -1571,6 +1599,15 @@ do_not_copy = ('.hi', '.o', '.dyn_hi'
, '.dyn_o', '.out'
,'.hi-boot', '.o-boot') # 12112
+def way_supports_io_manager(way: WayName, io_manager: Optional[str]) -> bool:
+ # All ways support a default IO Manager
+ if io_manager is None:
+ return True
+
+ way_is_threaded = '-threaded' in config.way_flags[way]
+ available_io_managers = config.io_managers_threaded if way_is_threaded else config.io_managers_nonthreaded
+ return io_manager in available_io_managers
+
async def test_common_work(name: TestName, opts,
func, args) -> None:
try:
@@ -1619,7 +1656,22 @@ async def test_common_work(name: TestName, opts,
# A test itself can request extra ways by setting opts.extra_ways
all_ways = list(OrderedDict.fromkeys(all_ways + [way for way in opts.extra_ways if way not in all_ways]))
- t.total_test_cases += len(all_ways)
+ # We split each way over available io managers
+ if len(opts.io_managers) == 0:
+ framework_fail(name, None, 'opts.io_managers is empty')
+ all_ways_and_io_managers = dict(
+ (
+ way,
+ [io_manager
+ for io_manager in opts.io_managers
+ if way_supports_io_manager(way, io_manager)
+ ]
+ )
+ for way in all_ways
+ )
+
+ for io_managers in all_ways_and_io_managers.values():
+ t.total_test_cases += len(io_managers)
only_ways = getTestOpts().only_ways
ok_way = lambda way: \
@@ -1702,19 +1754,23 @@ async def test_common_work(name: TestName, opts,
# Run the required tests...
for way in do_ways:
- if stopping():
- break
- start = time.monotonic()
- try:
- await do_test(name, way, func, args, files)
- except KeyboardInterrupt:
- stopNow()
- except Exception as e:
- traceback.print_exc()
- framework_fail(name, way, traceback.format_exc(),
- runtime=time.monotonic() - start)
+ for io_manager in all_ways_and_io_managers[way]:
+ if stopping():
+ break
+ start = time.monotonic()
+ try:
+ getTestOpts().io_manager_current = io_manager
+ await do_test(name ,way ,func ,args ,files)
+ except KeyboardInterrupt:
+ stopNow()
+ except Exception as e:
+ traceback.print_exc()
+ framework_fail(name, way, traceback.format_exc(),
+ runtime=time.monotonic() - start)
+
+ for skipped_way in set(all_ways) - set(do_ways):
+ t.n_tests_skipped += len(all_ways_and_io_managers[skipped_way])
- t.n_tests_skipped += len(set(all_ways) - set(do_ways))
if getTestOpts().expect == 'missing-lib': t.n_missing_libs += 1
if config.cleanup and do_ways:
@@ -1741,6 +1797,8 @@ async def do_test(name: TestName,
start = time.monotonic()
full_name = name + '(' + way + ')'
+ if opts.io_manager_current is not None:
+ full_name += ' (IO Manager: {})'.format(opts.io_manager_current)
test_n = len(allTestNames)
progress_args = [ full_name, t.total_tests, test_n,
[len(t.unexpected_passes),
@@ -2140,7 +2198,7 @@ async def compile_grep_core(name: TestName,
async def compile_and_run__(name: TestName,
way: WayName,
- top_mod: Path | None,
+ top_mod: Optional[Path],
extra_mods: List[str],
extra_hc_opts: str,
backpack: bool=False,
@@ -2423,6 +2481,9 @@ async def simple_run(name: TestName, way: WayName, prog: str, extra_run_opts: st
my_rts_flags = rts_flags(way)
+ if opts.io_manager_current is not None:
+ my_rts_flags += ' +RTS --io-manager={0} -RTS '.format(opts.io_manager_current)
+
# Collect runtime stats if necessary:
# isStatsTest and not isCompilerStatsTest():
# assume we are running a ghc compiled program. Collect stats.
@@ -2448,6 +2509,12 @@ async def simple_run(name: TestName, way: WayName, prog: str, extra_run_opts: st
output_file = perfStatsFile(False, name)
+ # Check that IO Manager is available
+ if opts.io_manager_current is not None:
+ available_io_managers = discover_io_managers(prog, opts.testdir)
+ if opts.io_manager_current not in available_io_managers:
+ framework_fail(name, way, 'IO Manager {} unexpectedly unavailable (available IO Managers: {}). `-non-threaded`/`-threaded` is only supported through way RTS flags (not manually added via test modifiers/args) when testing over IO Managers.'.format(opts.io_manager_current, available_io_managers))
+
# run the command
exit_code = await runCmdPerf(opts.compiler_perf_counters, cmd, output_file,
working_dir=opts.testdir,
@@ -2509,7 +2576,7 @@ def rts_flags(way: WayName) -> str:
async def interpreter_run(name: TestName,
way: WayName,
extra_hc_opts: str,
- top_mod: Path | None
+ top_mod: Optional[Path]
) -> PassFail:
opts = getTestOpts()
=====================================
testsuite/driver/testutil.py
=====================================
@@ -1,4 +1,5 @@
import os
+import re
import subprocess
import shutil
import tempfile
@@ -179,3 +180,19 @@ def shorten_metric_name(name: str) -> str:
"binary_size/if_compression(3)": "size/3",
}
return dic.get(name, name)
+
+# I/O manager discovery for a haskell binary. Returns the set of IO Manager
+# strings supported by the binary
+def discover_io_managers(hs_exe: Union[str, Path], cwd: Union[str, Path]) -> Set[str]:
+ rp = subprocess.run(
+ [str(hs_exe), '+RTS', '--info', '-RTS'],
+ cwd=cwd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.DEVNULL)
+ stdout = rp.stdout.decode('utf-8', errors='replace')
+
+ m = re.search(r'\("I/O managers", "(.*)"\)', stdout)
+ if not m:
+ raise Exception('Expected stdout output of form \'... ("I/O managers", "select selectbis poll") ...\' but got:\n{}'.format(stdout))
+
+ return set(m.group(1).split())
=====================================
testsuite/tests/rts/all.T
=====================================
@@ -674,8 +674,9 @@ test('T23142', [unless(debug_rts(), skip), req_interp], makefile_test, ['T23142'
test('T23400', [], compile_and_run, ['-with-rtsopts -A8k'])
test('IOManager', [js_skip, when(arch('wasm32'), skip), when(opsys('mingw32'), skip),
- pre_cmd('$MAKE -s --no-print-directory IOManager.hs')],
- compile_and_run, [''])
+ pre_cmd('$MAKE -s --no-print-directory IOManager.hs'),
+ extra_ways(['threaded2']), all_available_io_managers_except('winio')],
+ compile_and_run, [''])
test('T24142', [req_target_smp], compile_and_run, ['-threaded -with-rtsopts "-N2"'])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6fe4ea9ff143ae0826864f098879162…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6fe4ea9ff143ae0826864f098879162…
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