[Git][ghc/ghc][wip/fendor/dep-json-link-nodes] `ghc -M`: Add support for link nodes to json output
by Hannes Siebenhandl (@fendor) 23 Oct '26
by Hannes Siebenhandl (@fendor) 23 Oct '26
23 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC
Commits:
0d14d60c by fendor at 2026-10-23T10:11:36+02:00
`ghc -M`: Add support for link nodes to json output
`LinkNode`s are part of the module graph and indicate that a certain
linking artefact is to be produced from other module graph nodes.
We expose `LinkNode`s in the output of `ghc -M -dep-json`, allowing
consumers to see when a linking artefact is to be produced.
Only the JSON output format of `ghc -M` supports `LinkNode` as it is
unclear how the Makefile output should be retrofitted to express the
dependencies, and whether it would require a feature guard.
Closes #28007
- - - - -
24 changed files:
- + changelog.d/make-depends-link-nodes
- compiler/GHC/Driver/MakeFile.hs
- docs/users_guide/make-depends-json-schema-1_0.json
- testsuite/tests/driver/mkdepend/all.T
- + testsuite/tests/driver/mkdepend/mhu04/app/Main.hs
- + testsuite/tests/driver/mkdepend/mhu04/app/Utils.hs
- + testsuite/tests/driver/mkdepend/mhu04/lib/Lib.hs
- + testsuite/tests/driver/mkdepend/mhu04/lib/Lib/Internal.hs
- + testsuite/tests/driver/mkdepend/mhu04/unitApp
- + testsuite/tests/driver/mkdepend/mhu04/unitLib
- + testsuite/tests/driver/mkdepend/mhu05/app/Main.hs
- + testsuite/tests/driver/mkdepend/mhu05/lib/Lib.hs
- + testsuite/tests/driver/mkdepend/mhu05/lib/Lib/Internal.hs
- + testsuite/tests/driver/mkdepend/mhu05/test/Spec.hs
- + testsuite/tests/driver/mkdepend/mhu05/unitApp
- + testsuite/tests/driver/mkdepend/mhu05/unitLib
- + testsuite/tests/driver/mkdepend/mhu05/unitTest
- + testsuite/tests/driver/mkdepend/mkdepend02.dep-json
- + testsuite/tests/driver/mkdepend/mkdepend02/Main.hs
- + testsuite/tests/driver/mkdepend/mkdepend_bkp_01/bkp01.bkp
- + testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-json
- + testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-makefile
- + testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-json
- + testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-makefile
Changes:
=====================================
changelog.d/make-depends-link-nodes
=====================================
@@ -0,0 +1,4 @@
+section: compiler
+synopsis: Expose link nodes in `ghc -M -dep-json` output
+issues: #28007
+mrs: !16848
=====================================
compiler/GHC/Driver/MakeFile.hs
=====================================
@@ -77,6 +77,7 @@ import System.IO
import System.IO.Error (isEOFError)
import System.OsPath as OsPath
import System.OsString qualified as OsString
+import qualified GHC.Unit.Env as UnitEnv
-----------------------------------------------------------------
--
@@ -216,6 +217,19 @@ data Dep
{ dep_path :: OsPath
}
+data DepLink
+ = DepLink
+ { link_output :: !UnitId
+ , link_nodes :: ![DepLinkNode]
+ , link_type :: GhcLink
+ }
+
+data DepLinkNode
+ = DepLink_Unit !UnitId
+ | DepLink_Module !ModuleNameWithIsBoot !UnitId
+ | DepLink_HomeUnit {-# UNPACK #-} !UnitId
+ | DepLink_ExtUnit {-# UNPACK #-} !UnitId
+
data DependencyWriter = DependencyWriter
{ dw_beginWriter :: IO DepSink
, dw_endWriter :: IO ()
@@ -223,6 +237,7 @@ data DependencyWriter = DependencyWriter
data DepSink = DepSink
{ ds_writeDependency :: DepNode -> [Dep] -> IO ()
+ , ds_writeLinkDependency :: DepLink -> IO ()
}
-----------------------------------------------------------------
@@ -261,7 +276,22 @@ processDeps hsc_env _ _ (AcyclicSCC (InstantiationNode _uid node))
mkPlainErrorMsgEnvelope noSrcSpan $
GhcDriverMessage $ DriverInstantiationNodeInDependencyGeneration node
-processDeps _ _ _ (AcyclicSCC (LinkNode {})) = return ()
+processDeps hsc_env _ sinks (AcyclicSCC (LinkNode keys unitId)) = do
+ let
+ toLinkNodeDep = \ case
+ NodeKey_Unit iu -> DepLink_Unit $ virtualUnitId iu
+ NodeKey_Module node -> DepLink_Module (mnkModuleName node) (mnkUnitId node)
+ NodeKey_Link uid -> DepLink_HomeUnit uid
+ NodeKey_ExternalUnit uid -> DepLink_ExtUnit uid
+
+ linkNode = DepLink
+ { link_nodes = map toLinkNodeDep keys
+ , link_output = unitId
+ , link_type = ghcLink $ UnitEnv.ue_unitFlags unitId (hsc_unit_env hsc_env)
+ }
+
+ traverse_ (\ sink -> ds_writeLinkDependency sink linkNode) sinks
+
processDeps _ _ _ (AcyclicSCC (UnitNode {})) = return ()
processDeps _ _ _ (AcyclicSCC (ModuleNode _ (ModuleNodeFixed {})))
-- No dependencies needed for fixed modules (already compiled)
@@ -385,8 +415,13 @@ initFileDepWriter logger root tmpfs dflags makefile = do
files <- beginMkDependHS logger tmpfs dflags makefile
pure DependencyWriter
{ dw_beginWriter = do
- pure $ DepSink $ \ node deps -> do
- writeDependencies root (mkd_tmp_hdl files) suffixes node deps
+ pure DepSink
+ { ds_writeDependency = \ node deps -> do
+ writeDependencies root (mkd_tmp_hdl files) suffixes node deps
+ , ds_writeLinkDependency = \ _linkNode -> do
+ -- Link nodes are not supported
+ pure ()
+ }
, dw_endWriter = endMkDependHS logger files
}
where
@@ -559,8 +594,13 @@ initJsonDepWriter logger tmpfs dflags output = do
json_var <- mkJsonOutput logger tmpfs dflags output initDepJson
pure DependencyWriter
{ dw_beginWriter =
- pure $ DepSink $ \ node deps ->
- writeHomeUnitDeps json_var node deps
+ pure DepSink
+ { ds_writeDependency = \ node deps ->
+ writeHomeUnitDeps json_var node deps
+ , ds_writeLinkDependency = \ link ->
+ -- Link nodes are not supported
+ writeLinkNode json_var link
+ }
, dw_endWriter = do
writeJsonOutput logger json_var
}
@@ -629,8 +669,12 @@ writeJsonOutput logger JsonOutput {json_path, json_tmp_file, json_tmp_hdl} = do
-- Output helpers
writeJsonl :: ToJson a => JsonOutput -> a -> IO ()
-writeJsonl JsonOutput {json_tmp_hdl} output = do
- hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON $ json output)
+writeJsonl deps_json output = do
+ writeJsonl' deps_json (json output)
+
+writeJsonl' :: JsonOutput -> JsonDoc -> IO ()
+writeJsonl' JsonOutput {json_tmp_hdl} output = do
+ hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON output)
--------------------------------------------------------------------------------
-- Payload for -dep-json
@@ -849,6 +893,65 @@ writeHomeUnitDeps deps_json DepNode {..} deps = do
DepCpp {dep_path} ->
(extDeps, addCpp dep_path node_data)
+writeLinkNode :: JsonOutput -> DepLink -> IO ()
+writeLinkNode dep_json (DepLink uid deps linkType) = do
+ writeJsonl' dep_json $
+ JSObject
+ [ ("unit-id", JSString $ unitIdString uid)
+ , ("link-type", linkTypeJson linkType)
+ , ("links", JSArray $ concat
+ [ map renderMods (Map.toList allMods)
+ , map (JSString . unitIdString) (Set.toList units)
+ , map (JSString . unitIdString) (Set.toList homeUids)
+ , map (JSString . unitIdString) (Set.toList extUids)
+ ])
+ ]
+ where
+ (extUids, homeUids, units, allMods) = foldl' go (Set.empty, Set.empty, Set.empty, Map.empty) deps
+
+ renderMods (u, ms) =
+ JSObject
+ [ ("unit-id", JSString $ unitIdString u)
+ , ("modules", JSArray $ map moduleKey (Set.toList ms))
+ ]
+
+
+ go (ext, homeUid, unit, mods) = \ case
+ DepLink_Module modName u ->
+ let
+ newMods = Map.insertWith (Set.union) u (Set.singleton modName) mods
+ in
+ (ext, homeUid, unit, newMods)
+ DepLink_Unit uid ->
+ (ext, homeUid, Set.insert uid unit, mods)
+ DepLink_HomeUnit uid ->
+ (ext, Set.insert uid homeUid, unit, mods)
+ DepLink_ExtUnit uid ->
+ (Set.insert uid ext, homeUid, unit, mods)
+
+ moduleNameJson m = JSString $ moduleNameString m
+
+ moduleKey :: ModuleNameWithIsBoot -> JsonDoc
+ moduleKey t =
+ case gwib_isBoot t of
+ NotBoot -> moduleNameJson (gwib_mod t)
+ IsBoot -> JSObject
+ [ ("module", moduleNameJson (gwib_mod t))
+ -- Redundant check, but the compiler will fix it and
+ -- this feels like it might avoid bugs in the future.
+ , ("boot", JSBool $ gwib_isBoot t == IsBoot)
+ ]
+
+ linkTypeJson :: GhcLink -> JsonDoc
+ linkTypeJson = JSString . \ case
+ NoLink{} -> panic "Received -no-link, but a link node must not exist with -no-link."
+ LinkExecutable{} -> "executable"
+ LinkInMemory{} -> "in-memory"
+ LinkDynLib{} -> "dynamic-library"
+ LinkBytecodeLib{} -> "bytecode-library"
+ LinkStaticLib{} -> "static-library"
+ LinkMergedObj{} -> "merged-object"
+
-----------------------------------------------------------------
-- Module cycles
-----------------------------------------------------------------
=====================================
docs/users_guide/make-depends-json-schema-1_0.json
=====================================
@@ -3,14 +3,15 @@
"title": "GHC dependencies",
"description": "The dependency graph of a Haskell program.",
"oneOf": [
- { "$ref": "#/definitions/home-module" },
- { "$ref": "#/definitions/external-unit" }
+ { "$ref": "#/definitions/external-unit" },
+ { "$ref": "#/definitions/module" },
+ { "$ref": "#/definitions/link" }
],
"definitions": {
"external-unit": {
"type": "object",
"properties": {
- "unit-id": { "$ref": "#/definitions/unit-name" },
+ "unit-id": { "$ref": "#/definitions/unit-id" },
"package-name": { "$ref": "#/definitions/package-name" },
"package-id": { "$ref": "#/definitions/package-id" }
},
@@ -29,17 +30,17 @@
"module-name": {
"type": "string"
},
- "home-module": {
+ "module": {
"type": "object",
"properties": {
"module": { "$ref": "#/definitions/module-name" },
"unit-id": { "$ref": "#/definitions/unit-id" },
"package-name": { "$ref": "#/definitions/package-name" },
- "source": { "$ref": "#/definitions/home-module-source" },
- "boot": { "type": "boolean" },
+ "source": { "$ref": "#/definitions/module-source" },
+ "boot": { "$ref": "#/definitions/boot" },
"includes": {
"type": "array",
- "items": { "$ref": "#/definitions/home-module-include" }
+ "items": { "$ref": "#/definitions/module-include" }
},
"imports": {
"type": "object",
@@ -48,18 +49,18 @@
"items": { "$ref": "#/definitions/import" }
}
},
- "preprocessor": { "$ref": "#/definitions/home-module-preprocessor" }
+ "preprocessor": { "$ref": "#/definitions/module-preprocessor" }
},
"required": ["module", "unit-id", "source"],
"unevaluatedProperties": false
},
- "home-module-source": {
+ "module-source": {
"type": "string"
},
- "home-module-include": {
+ "module-include": {
"type": "string"
},
- "home-module-preprocessor": {
+ "module-preprocessor": {
"type": "string"
},
"import": {
@@ -70,17 +71,66 @@
"properties": {
"module": { "$ref": "#/definitions/module-name" },
"level": { "$ref": "#/definitions/import-level" },
- "boot": { "type": "boolean" }
+ "boot": { "$ref": "#/definitions/boot" }
},
"required": ["module"],
"unevaluatedProperties": false
}
]
},
+ "boot": {
+ "type": "boolean",
+ "default": false
+ },
"import-level": {
"type": "string",
"enum": ["normal", "quote", "splice"],
"default": "normal"
+ },
+ "link": {
+ "type": "object",
+ "link-type": { "$ref": "#/definitions/link-type" },
+ "properties": {
+ "links": {
+ "type": "array",
+ "items": { "$ref": "#/definitions/link-unit" }
+ }
+ }
+ },
+ "link-type": {
+ "type": "string",
+ "enum": [
+ "executable",
+ "in-memory",
+ "dynamic-library",
+ "bytecode-library",
+ "static-library",
+ "merged-object"
+ ]
+ },
+ "link-unit": {
+ "type": "object",
+ "properties": {
+ "unit-id": { "$ref": "#/definitions/unit-id" },
+ "modules": {
+ "type": "array",
+ "items": { "$ref": "#/definitions/link-module" }
+ }
+ },
+ "required": ["unit-id"]
+ },
+ "link-module": {
+ "oneOf": [
+ { "$ref": "#/definitions/module-name" },
+ {
+ "type": "object",
+ "properties": {
+ "module": { "$ref": "#/definitions/module-name" },
+ "boot": { "$ref": "#/definitions/boot" }
+ },
+ "required": ["module"]
+ }
+ ]
}
}
}
=====================================
testsuite/tests/driver/mkdepend/all.T
=====================================
@@ -30,9 +30,23 @@ test('mkdepend_mhu03',
, when(opsys('mingw32'), skip) # shell script preprocessor
],
make_depend_multiunit, [['mhu03/unitA'], ''])
+# Link nodes
+test('mkdepend_mhu04',
+ [ extra_files(['mhu04/']), dep_makefile, dep_json ],
+ make_depend_multiunit, [['mhu04/unitLib', 'mhu04/unitApp'], ''])
+test('mkdepend_mhu05',
+ [ extra_files(['mhu05/']), dep_makefile, dep_json ],
+ make_depend_multiunit, [['mhu05/unitLib', 'mhu05/unitApp', 'mhu05/unitTest'], ''])
# Honour -F
test('mkdepend01',
[ extra_files(['mkdepend01/']), dep_makefile, dep_json ],
make_depend, [['mkdepend01/A.hs'], '-pgmF pp'])
-
+# Link nodes
+test('mkdepend02',
+ [ extra_files(['mkdepend02/']), dep_json ],
+ make_depend, [['mkdepend02/Main.hs'], ''])
+# Backpack
+test('mkdepend_bkp_01',
+ [ extra_files(['mkdepend_bkp_01/']), dep_json, expect_fail ],
+ make_depend, [['mkdepend_bkp_01/bkp01.bkp'], ''])
=====================================
testsuite/tests/driver/mkdepend/mhu04/app/Main.hs
=====================================
@@ -0,0 +1,7 @@
+module Main (main) where
+
+import Lib (greeting)
+import Utils (shout)
+
+main :: IO ()
+main = putStrLn (shout greeting)
=====================================
testsuite/tests/driver/mkdepend/mhu04/app/Utils.hs
=====================================
@@ -0,0 +1,6 @@
+module Utils (shout) where
+
+import Data.Char (toUpper)
+
+shout :: String -> String
+shout = map toUpper
=====================================
testsuite/tests/driver/mkdepend/mhu04/lib/Lib.hs
=====================================
@@ -0,0 +1,6 @@
+module Lib (greeting) where
+
+import Lib.Internal (name)
+
+greeting :: String
+greeting = "Hello, " ++ name
=====================================
testsuite/tests/driver/mkdepend/mhu04/lib/Lib/Internal.hs
=====================================
@@ -0,0 +1,4 @@
+module Lib.Internal (name) where
+
+name :: String
+name = "world"
=====================================
testsuite/tests/driver/mkdepend/mhu04/unitApp
=====================================
@@ -0,0 +1 @@
+-i -i. -working-dir=mhu04/app -this-unit-id app -this-package-name app -package-id lib -o app Main
=====================================
testsuite/tests/driver/mkdepend/mhu04/unitLib
=====================================
@@ -0,0 +1 @@
+-i -i. -working-dir=mhu04/lib -this-unit-id lib -this-package-name lib Lib Lib.Internal
=====================================
testsuite/tests/driver/mkdepend/mhu05/app/Main.hs
=====================================
@@ -0,0 +1,6 @@
+module Main (main) where
+
+import Lib (greeting)
+
+main :: IO ()
+main = putStrLn greeting
=====================================
testsuite/tests/driver/mkdepend/mhu05/lib/Lib.hs
=====================================
@@ -0,0 +1,6 @@
+module Lib (greeting) where
+
+import Lib.Internal (name)
+
+greeting :: String
+greeting = "Hello, " ++ name
=====================================
testsuite/tests/driver/mkdepend/mhu05/lib/Lib/Internal.hs
=====================================
@@ -0,0 +1,4 @@
+module Lib.Internal (name) where
+
+name :: String
+name = "world"
=====================================
testsuite/tests/driver/mkdepend/mhu05/test/Spec.hs
=====================================
@@ -0,0 +1,9 @@
+module Spec (main) where
+
+import Lib (greeting)
+import Lib.Internal (name)
+
+main :: IO ()
+main
+ | greeting == "Hello, " ++ name = putStrLn "OK"
+ | otherwise = error "unexpected greeting"
=====================================
testsuite/tests/driver/mkdepend/mhu05/unitApp
=====================================
@@ -0,0 +1 @@
+-i -i. -working-dir=mhu05/app -this-unit-id app -this-package-name app -package-id lib -o app Main
=====================================
testsuite/tests/driver/mkdepend/mhu05/unitLib
=====================================
@@ -0,0 +1 @@
+-i -i. -working-dir=mhu05/lib -this-unit-id lib -this-package-name lib -staticlib Lib Lib.Internal
=====================================
testsuite/tests/driver/mkdepend/mhu05/unitTest
=====================================
@@ -0,0 +1 @@
+-i -i. -working-dir=mhu05/test -this-unit-id test -this-package-name test -package-id lib -main-is Spec -o test Spec
=====================================
testsuite/tests/driver/mkdepend/mkdepend02.dep-json
=====================================
@@ -0,0 +1,2 @@
+{"module":"Main","unit-id":"main","imports":{},"source":"mkdepend02/Main.hs"}
+{"unit-id":"main","link-type":"executable","links":[{"unit-id":"main","modules":["Main"]}]}
=====================================
testsuite/tests/driver/mkdepend/mkdepend02/Main.hs
=====================================
@@ -0,0 +1,3 @@
+module Main where
+
+main = putStrLn "Hello, World"
=====================================
testsuite/tests/driver/mkdepend/mkdepend_bkp_01/bkp01.bkp
=====================================
@@ -0,0 +1,20 @@
+unit p where
+ signature H where
+ data T
+ x :: Bool
+ module A where
+ import H
+ data A = MkA T
+ y = x
+
+unit q where
+ dependency p[H=<H>]
+
+unit h where
+ module H where
+ data T = T
+ x = True
+
+unit r where
+ dependency h
+ dependency q[H=h:H]
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-json
=====================================
@@ -0,0 +1,5 @@
+{"module":"Lib.Internal","unit-id":"lib","imports":{},"package-name":"lib","source":"mhu04/lib/Lib/Internal.hs"}
+{"module":"Lib","unit-id":"lib","imports":{"lib":["Lib.Internal"]},"package-name":"lib","source":"mhu04/lib/Lib.hs"}
+{"module":"Utils","unit-id":"app","imports":{},"package-name":"app","source":"mhu04/app/Utils.hs"}
+{"module":"Main","unit-id":"app","imports":{"app":["Utils"],"lib":["Lib"]},"package-name":"app","source":"mhu04/app/Main.hs"}
+{"unit-id":"app","link-type":"executable","links":[{"unit-id":"app","modules":["Main","Utils"]}]}
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu04.dep-makefile
=====================================
@@ -0,0 +1,9 @@
+# DO NOT DELETE: Beginning of Haskell dependencies
+mhu04/lib/Lib/Internal.o : mhu04/lib/Lib/Internal.hs
+mhu04/lib/Lib.o : mhu04/lib/Lib.hs
+mhu04/lib/Lib.o : mhu04/lib/Lib/Internal.hi
+mhu04/app/Utils.o : mhu04/app/Utils.hs
+mhu04/app/Main.o : mhu04/app/Main.hs
+mhu04/app/Main.o : mhu04/app/Utils.hi
+mhu04/app/Main.o : mhu04/lib/Lib.hi
+# DO NOT DELETE: End of Haskell dependencies
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-json
=====================================
@@ -0,0 +1,7 @@
+{"module":"Lib.Internal","unit-id":"lib","imports":{},"package-name":"lib","source":"mhu05/lib/Lib/Internal.hs"}
+{"module":"Lib","unit-id":"lib","imports":{"lib":["Lib.Internal"]},"package-name":"lib","source":"mhu05/lib/Lib.hs"}
+{"unit-id":"lib","link-type":"static-library","links":[{"unit-id":"lib","modules":["Lib","Lib.Internal"]}]}
+{"module":"Main","unit-id":"app","imports":{"lib":["Lib"]},"package-name":"app","source":"mhu05/app/Main.hs"}
+{"unit-id":"app","link-type":"executable","links":[{"unit-id":"app","modules":["Main"]}]}
+{"module":"Spec","unit-id":"test","imports":{"lib":["Lib","Lib.Internal"]},"package-name":"test","source":"mhu05/test/Spec.hs"}
+{"unit-id":"test","link-type":"executable","links":[{"unit-id":"test","modules":["Spec"]}]}
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu05.dep-makefile
=====================================
@@ -0,0 +1,10 @@
+# DO NOT DELETE: Beginning of Haskell dependencies
+mhu05/lib/Lib/Internal.o : mhu05/lib/Lib/Internal.hs
+mhu05/lib/Lib.o : mhu05/lib/Lib.hs
+mhu05/lib/Lib.o : mhu05/lib/Lib/Internal.hi
+mhu05/app/Main.o : mhu05/app/Main.hs
+mhu05/app/Main.o : mhu05/lib/Lib.hi
+mhu05/test/Spec.o : mhu05/test/Spec.hs
+mhu05/test/Spec.o : mhu05/lib/Lib/Internal.hi
+mhu05/test/Spec.o : mhu05/lib/Lib.hi
+# DO NOT DELETE: End of Haskell dependencies
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0d14d60ccb89b4959acf42ed65a0c1b…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0d14d60ccb89b4959acf42ed65a0c1b…
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/fendor/dep-stdout] Add support for streaming `ghc -M` to stdout
by Hannes Siebenhandl (@fendor) 23 Oct '26
by Hannes Siebenhandl (@fendor) 23 Oct '26
23 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-stdout at Glasgow Haskell Compiler / GHC
Commits:
53e36084 by fendor at 2026-10-23T10:04:03+02:00
Add support for streaming `ghc -M` to stdout
Adds the flags `-dep-json-stdout` and `-dep-makefile-stdout` which
streams dependency information line-by-line to stdout.
Each dependency generation can be redirected to separate outputs.
Providing any extra `-dep-{json,makefile}[-stdout]` disables the default
generation of the `Makefile` file.
Allows consumers to start processing the dependency information as it
comes in and not waiting for the dependency generation to finish.
`DepStream` abstracts over the possible output handles, which is either
a file or the stdout handle.
Writing to a file requires temporary file paths, thus `DepStream` must
provide a way to perform clean up when dependency processing finished or
when an error is thrown for any reason.
We further decouple the rendering and writing of the result from the
dependency generation, localising side effects and making the code
slightly more readable.
The testsuite now also handles `ghc -M` writing to stdout.
We add a test for each combination of writing to stdout and file,
except for writing both makefile and json.
While it probably works, is really undefined behaviour.
Closes #27937
- - - - -
15 changed files:
- + changelog.d/make-depends-streaming-output
- compiler/GHC/Driver/DynFlags.hs
- compiler/GHC/Driver/MakeFile.hs
- compiler/GHC/Driver/Session.hs
- docs/users_guide/separate_compilation.rst
- testsuite/driver/my_typing.py
- testsuite/driver/testglobals.py
- testsuite/driver/testlib.py
- testsuite/tests/driver/mkdepend/all.T
- testsuite/tests/driver/mkdepend/mkdepend_mhu01.dep-json → testsuite/tests/driver/mkdepend/mkdepend_mhu01a.dep-json
- testsuite/tests/driver/mkdepend/mkdepend_mhu01.dep-makefile → testsuite/tests/driver/mkdepend/mkdepend_mhu01a.dep-makefile
- + testsuite/tests/driver/mkdepend/mkdepend_mhu01b.stderr
- + testsuite/tests/driver/mkdepend/mkdepend_mhu01c.stderr
- + testsuite/tests/driver/mkdepend/mkdepend_mhu01d.dep-makefile
- + testsuite/tests/driver/mkdepend/mkdepend_mhu01d.stderr
Changes:
=====================================
changelog.d/make-depends-streaming-output
=====================================
@@ -0,0 +1,16 @@
+section: compiler
+synopsis: Allow streaming `ghc -M` output to stdout
+issues: #27937
+mrs: !16830
+description: {
+ Add options `-dep-makefile-stdout` and `-dep-json-stdout` which stream the dependency generation
+ output to stdout.
+
+ The Makefile and json dependency generation can stream to separate targets, e.g. `-dep-makefile target.Makefile -dep-json-stdout`
+ streams the JSON Lines output to stdout and the Makefile to `target.Makefile`.
+
+ If `-dep-makefile-stdout`, `-dep-json` or `-dep-json-stdout` is given, we no longer generate a Makefile by default, but only
+ if the user explicitly requests the Makefile dependency generation.
+
+ The output from a particular backend can be disabled via `-no-dep-json` and `-no-dep-makefile` respectively.
+}
=====================================
compiler/GHC/Driver/DynFlags.hs
=====================================
@@ -73,6 +73,9 @@ module GHC.Driver.DynFlags (
IncludeSpecs(..), addGlobalInclude, addQuoteInclude, flattenIncludes,
addImplicitQuoteInclude,
+ -- * `ghc -M` specification
+ MkDependOutput(..),
+
-- * SDoc
initSDocContext, initDefaultSDocContext,
initPromotionTickContext,
@@ -350,8 +353,8 @@ data DynFlags = DynFlags {
-- ^ External plugins loaded from shared libraries
-- For ghc -M
- depMakefile :: Maybe FilePath,
- depJson :: Maybe FilePath,
+ depMakefile :: MkDependOutput,
+ depJson :: MkDependOutput,
depIncludePkgDeps :: Bool,
depIncludeCppDeps :: Bool,
depExcludeMods :: [ModuleName],
@@ -694,8 +697,8 @@ defaultDynFlags mySettings =
llvmOptLevel = 0,
-- ghc -M values
- depMakefile = Just "Makefile",
- depJson = Nothing,
+ depMakefile = MkDepend_Default "Makefile",
+ depJson = MkDepend_None,
depIncludePkgDeps = False,
depIncludeCppDeps = False,
depExcludeMods = [],
@@ -801,6 +804,15 @@ flattenExtensionFlags ml = foldr g defaultExtensionFlags
g (Off f) flags = EnumSet.delete f flags
defaultExtensionFlags = EnumSet.fromList (languageExtensions ml)
+-- -----------------------------------------------------------------------------
+-- -M -dep-*
+
+data MkDependOutput
+ = MkDepend_None
+ | MkDepend_Default !FilePath
+ | MkDepend_Explicit !FilePath
+ | MkDepend_StdOut
+
-- -----------------------------------------------------------------------------
-- -jN
=====================================
compiler/GHC/Driver/MakeFile.hs
=====================================
@@ -132,7 +132,7 @@ doMkDepend = do
module_graph <- GHC.depanal excl_mods True {- Allow dup roots -}
doMkDependModuleGraph dflags module_graph
-doMkDependModuleGraph :: GhcMonad m => DynFlags -> ModuleGraph -> m ()
+doMkDependModuleGraph :: GhcMonad m => DynFlags -> ModuleGraph -> m ()
doMkDependModuleGraph dflags module_graph = do
hsc_env <- getSession
logger <- getLogger
@@ -143,12 +143,14 @@ doMkDependModuleGraph dflags module_graph = do
root <- liftIO Directory.getCurrentDirectory
let
+ suffixes = fmap unsafeEncodeUtf (depSuffixes dflags)
+ tmpdir = tmpDir dflags
backends = concat
- [ [ initFileDepWriter logger root tmpfs dflags makefile_output
- | Just makefile_output <- [depMakefile dflags]
+ [ [ initFileDepWriter logger root tmpfs tmpdir suffixes makefile_output
+ | Just makefile_output <- [initDepOutput (depMakefile dflags)]
]
- , [ initJsonDepWriter logger tmpfs dflags json_output
- | Just json_output <- [depJson dflags]
+ , [ initJsonDepWriter logger tmpfs tmpdir json_output
+ | Just json_output <- [initDepOutput (depJson dflags)]
]
]
@@ -158,24 +160,14 @@ doMkDependModuleGraph dflags module_graph = do
liftIO $ debugTraceMsg logger 2 (text "Module dependencies" $$ ppr sorted)
liftIO $ do
-
- -- Setup the writers
- writers <- sequenceA backends
- sinks <- traverse dw_beginWriter writers
-
- -- Do the actual work
- do
+ withWriters backends $ \ depSink -> do
+ -- Do the actual work
-- Process them one by one, dumping results and complaining about cycles
- mapM_ (processDeps hsc_env (UniqSet.mkUniqSet excl_mods) sinks) sorted
+ mapM_ (processDeps hsc_env (UniqSet.mkUniqSet excl_mods) depSink) sorted
-- If -ddump-mod-cycles, show cycles in the module graph
liftIO $ dumpModCycles logger module_graph
- -- Tidy up
- do
- traverse_ dw_endWriter writers
-
-
-- Unconditional exiting is a bad idea. If an error occurs we'll get an
--exception; if that is not caught it's fine, but at least we have a
--chance to find out exactly what went wrong. Uncomment the following
@@ -216,15 +208,119 @@ data Dep
{ dep_path :: OsPath
}
+-- | Consumer of the dependency information generated from the 'ModuleGraph'.
+--
+-- Use with 'withWriter'/'withWriters' to initialise the consumers of the dependency information
+-- generation.
data DependencyWriter = DependencyWriter
- { dw_beginWriter :: IO DepSink
+ { dw_sink :: DepSink
+ -- ^ Sink for dependency information
, dw_endWriter :: IO ()
+ -- ^ We finished consuming the dependency information, perform final
+ -- tidy up operations. This may be a no-op, but could also
+ -- copy temporary files, close open handlers, etc...
+ --
+ -- If 'dw_endWriter' was called, calling 'dw_onError' is unnecessary,
+ -- but it must be safe to call 'dw_onError' any way.
+ , dw_onError :: IO ()
+ -- ^ Something crashed! Clean up any resources opened by this 'DependencyWriter'.
}
-data DepSink = DepSink
+-- | Initialise multiple backends and combine them into one output function.
+--
+-- Combines the 'DepSink' of each 'DependencyWriter' into one single consumer.
+withWriters :: [IO DependencyWriter] -> (DepSink -> IO a) -> IO a
+withWriters [] k = k noopSink
+withWriters (dw : dws) k =
+ withWriter
+ dw
+ $ \s -> withWriters dws (\s' -> k (s `mappendSink` s'))
+
+-- | Initialise a single 'DependencyWriter', making sure any allocated resource
+-- get's tidied up again.
+--
+-- Accepts 'IO' 'DependencyWriter' since the initialisation may perform setup operations,
+-- such as opening file handles, or intermediate data types.
+withWriter :: IO DependencyWriter -> (DepSink -> IO r) -> IO r
+withWriter dwIO k =
+ bracketOnError dwIO dw_onError $ \ dw -> do
+ r <- k (dw_sink dw)
+ -- Clean up open resources
+ dw_endWriter dw
+ pure r
+
+-- | Process the dependency information for one node in the 'ModuleGraph'.
+newtype DepSink = DepSink
{ ds_writeDependency :: DepNode -> [Dep] -> IO ()
}
+noopSink :: DepSink
+noopSink = DepSink $ \ _ _ -> pure ()
+
+mappendSink :: DepSink -> DepSink -> DepSink
+mappendSink (DepSink s1) (DepSink s2) = DepSink (\ node deps -> s1 node deps >> s2 node deps)
+
+-- | Where should the dependency output end up?
+data DepOutput
+ = DepStreamHandle Handle
+ -- ^ The dependency information should be written to this 'Handle'.
+ | DepFileOutput FilePath
+ -- ^ The dependency information should be written to this 'FilePath'.
+
+-- | A 'DepStream' is an abstraction over how to output single elements
+-- of the dependency generation, e.g., a json object or one makefile rule.
+data DepStream = DepStream
+ { depStream_write :: String -> IO ()
+ , depStream_close :: IO ()
+ , depStream_onError :: IO ()
+ }
+
+initDepOutput :: MkDependOutput -> Maybe DepOutput
+initDepOutput = \ case
+ MkDepend_None -> Nothing
+ MkDepend_Default fp -> Just $ DepFileOutput fp
+ MkDepend_Explicit fp -> Just $ DepFileOutput fp
+ MkDepend_StdOut -> Just $ DepStreamHandle stdout
+
+initMakefileDepStream :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DepStream
+initMakefileDepStream _ _ _ (DepStreamHandle h) =
+ pure $ depStreamHandle h
+initMakefileDepStream logger tmpfs tmpdir (DepFileOutput fp) = do
+ files <- beginMkDependHS logger tmpfs tmpdir fp
+ pure $
+ DepStream
+ { depStream_write = hPutStrLn (mkd_tmp_hdl files)
+ , depStream_onError = do
+ hClose $ mkd_tmp_hdl files
+ case mkd_make_hdl files of
+ Nothing -> return ()
+ Just hdl -> hClose hdl
+ , depStream_close = endMkDependHS logger files
+ }
+
+initJsonDepStream :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DepStream
+initJsonDepStream _ _ _ (DepStreamHandle h) =
+ pure $ depStreamHandle h
+initJsonDepStream logger tmpfs tmpdir (DepFileOutput fp) = do
+ files <- initJsonDepFiles logger tmpfs tmpdir fp
+ pure $
+ DepStream
+ { depStream_write = hPutStrLn (json_tmp_hdl files)
+ , depStream_onError = hClose (json_tmp_hdl files)
+ , depStream_close = endJsonDepFiles logger files
+ }
+
+depStreamHandle :: Handle -> DepStream
+depStreamHandle h =
+ DepStream
+ { depStream_write =
+ \output -> do
+ hPutStrLn h output
+ hFlush h -- We flush to ensure the output is streamed per line.
+ , depStream_close = pure ()
+ , depStream_onError = pure ()
+ }
+
-----------------------------------------------------------------
--
-- processDeps
@@ -233,7 +329,7 @@ data DepSink = DepSink
processDeps :: HscEnv
-> UniqSet ModuleName -- ^ Excludes
- -> [DepSink]
+ -> DepSink
-> SCC ModuleGraphNode
-> IO ()
-- Write suitable dependencies to handle
@@ -267,7 +363,7 @@ processDeps _ _ _ (AcyclicSCC (ModuleNode _ (ModuleNodeFixed {})))
-- No dependencies needed for fixed modules (already compiled)
= return ()
-processDeps hsc_env0 excl_mods sinks (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node))) = do
+processDeps hsc_env0 excl_mods sink (AcyclicSCC (ModuleNode _ (ModuleNodeCompile node))) = do
pp <- preprocessor
let
dep_node = mkDepNode pp
@@ -279,7 +375,7 @@ processDeps hsc_env0 excl_mods sinks (AcyclicSCC (ModuleNode _ (ModuleNodeCompil
if null missing_dep_errs
then do
- traverse_ (\ sink -> ds_writeDependency sink dep_node deps) sinks
+ ds_writeDependency sink dep_node deps
else do
let sec = initSourceErrorContext (hsc_dflags local_hsc_env)
throwErrors sec (mkMessages (listToBag missing_dep_errs))
@@ -380,17 +476,16 @@ find_preprocessor d = do
--
-----------------------------------------------------------------
-initFileDepWriter :: Logger -> OsPath -> TmpFs -> DynFlags -> FilePath -> IO DependencyWriter
-initFileDepWriter logger root tmpfs dflags makefile = do
- files <- beginMkDependHS logger tmpfs dflags makefile
+initFileDepWriter :: Logger -> OsPath -> TmpFs -> TempDir -> [OsPath] -> DepOutput -> IO DependencyWriter
+initFileDepWriter logger root tmpfs tmpdir suffixes output = do
+ stream <- initMakefileDepStream logger tmpfs tmpdir output
pure DependencyWriter
- { dw_beginWriter = do
- pure $ DepSink $ \ node deps -> do
- writeDependencies root (mkd_tmp_hdl files) suffixes node deps
- , dw_endWriter = endMkDependHS logger files
+ { dw_sink =
+ DepSink $ \ node deps -> do
+ traverse_ (depStream_write stream) (dependencyRules root suffixes node deps)
+ , dw_endWriter = depStream_close stream
+ , dw_onError = depStream_onError stream
}
- where
- suffixes = map unsafeEncodeUtf (depSuffixes dflags)
data MkDepFiles
= MkDep { mkd_make_file :: FilePath, -- Name of the makefile
@@ -398,11 +493,11 @@ data MkDepFiles
mkd_tmp_file :: FilePath, -- Name of the temporary file
mkd_tmp_hdl :: Handle } -- Handle of the open temporary file
-beginMkDependHS :: Logger -> TmpFs -> DynFlags -> FilePath -> IO MkDepFiles
-beginMkDependHS logger tmpfs dflags makefile = do
+beginMkDependHS :: Logger -> TmpFs -> TempDir -> FilePath -> IO MkDepFiles
+beginMkDependHS logger tmpfs tmpdir makefile = do
-- open a new temp file in which to stuff the dependency info
-- as we go along.
- tmp_file <- newTempName logger tmpfs (tmpDir dflags) TFL_CurrentModule "dep"
+ tmp_file <- newTempName logger tmpfs tmpdir TFL_CurrentModule "dep"
tmp_hdl <- openFile tmp_file WriteMode
-- open the makefile
@@ -443,16 +538,15 @@ beginMkDependHS logger tmpfs dflags makefile = do
return (MkDep { mkd_make_file = makefile, mkd_make_hdl = mb_make_hdl,
mkd_tmp_file = tmp_file, mkd_tmp_hdl = tmp_hdl})
-writeDependencies ::
+dependencyRules ::
OsPath ->
- Handle ->
[OsString] ->
-- ^ Suffixes
DepNode ->
[Dep] ->
- IO ()
-writeDependencies root hdl suffixes node deps =
- traverse_ write tasks
+ [String]
+dependencyRules root suffixes node deps =
+ map rule tasks
where
tasks = source_dep : boot_dep ++ concatMap import_dep deps
@@ -479,7 +573,7 @@ writeDependencies root hdl suffixes node deps =
DepCpp {dep_path} -> [(obj_files, dep_path)]
- write (from, to) = writeDependency root hdl from to
+ rule (from, to) = dependencyRule root from to
obj_files = suffixed dn_obj
@@ -488,18 +582,18 @@ writeDependencies root hdl suffixes node deps =
DepNode {dn_src, dn_obj, dn_hi, dn_mod} = node
-----------------------------
-writeDependency :: OsPath -> Handle -> [OsPath] -> OsPath -> IO ()
--- (writeDependency r h [t1,t2] dep) writes to handle h the dependency
+dependencyRule :: OsPath -> [OsPath] -> OsPath -> String
+-- (dependencyRule r [t1,t2] dep) renders the dependency
-- t1 t2 : dep
-writeDependency root hdl targets dep
- = do let -- We need to avoid making deps on
- -- c:/foo/...
- -- on Windows as make gets confused by the :
- -- Making relative deps avoids some instances of this.
- dep' = OsPath.makeRelative root dep
- forOutput = escapeSpaces . reslash Forwards . unsafeDecodeUtf . OsPath.normalise
- output = unwords (map forOutput targets) ++ " : " ++ forOutput dep'
- hPutStrLn hdl output
+dependencyRule root targets dep
+ = let -- We need to avoid making deps on
+ -- c:/foo/...
+ -- on Windows as make gets confused by the :
+ -- Making relative deps avoids some instances of this.
+ dep' = OsPath.makeRelative root dep
+ forOutput = escapeSpaces . reslash Forwards . unsafeDecodeUtf . OsPath.normalise
+ in
+ unwords (map forOutput targets) ++ " : " ++ forOutput dep'
-----------------------------
insertSuffixes
@@ -554,16 +648,27 @@ endMkDependHS logger
--
-----------------------------------------------------------------
-initJsonDepWriter :: Logger -> TmpFs -> DynFlags -> FilePath -> IO DependencyWriter
-initJsonDepWriter logger tmpfs dflags output = do
- json_var <- mkJsonOutput logger tmpfs dflags output initDepJson
- pure DependencyWriter
- { dw_beginWriter =
- pure $ DepSink $ \ node deps ->
- writeHomeUnitDeps json_var node deps
- , dw_endWriter = do
- writeJsonOutput logger json_var
- }
+initJsonDepWriter :: Logger -> TmpFs -> TempDir -> DepOutput -> IO DependencyWriter
+initJsonDepWriter logger tmpfs tmpdir output = do
+ stream <- initJsonDepStream logger tmpfs tmpdir output
+ json_var <- mkJsonOutput
+ pure
+ DependencyWriter
+ { dw_sink =
+ DepSink $ \node deps -> do
+ extUnits <- readJson json_var
+ let
+ (extUnitsDeps, payload) = homeUnitDeps node deps
+ newExtUnits = Set.difference extUnitsDeps (externalDeps extUnits)
+ -- Write new external unit deps before the module that uses them
+ for_ newExtUnits $ \extUnit -> do
+ depStream_write stream (renderJsonl extUnit)
+ depStream_write stream (renderJsonl payload)
+ -- Cache external deps we already reported in the past
+ updateJson json_var $ cacheExternalDepsJson newExtUnits
+ , dw_endWriter = depStream_close stream
+ , dw_onError = depStream_onError stream
+ }
--------------------------------------------------------------------------------
-- Output interface for json dumps
@@ -572,65 +677,60 @@ initJsonDepWriter logger tmpfs dflags output = do
-- The flag @-dep-json@ add an additional output target for dependency
-- diagnostics.
--
--- Contains temporary file locations to stream output to.
-data JsonOutput =
- JsonOutput {
- -- | This ref is updated in @processDeps@ incrementally, using a
- -- flag-specific type.
- json_ref :: IORef DepJson,
-
- -- | The output file path specified as argument to the flag.
- json_path :: FilePath,
- -- | Name of the temporary file
- json_tmp_file :: FilePath,
- -- | Handle of the open temporary file
- json_tmp_hdl :: Handle
+data JsonOutput
+ = JsonOutput
+ { json_ref :: IORef DepJson
+ -- ^ This ref is updated in @processDeps@ incrementally, using a
+ -- flag-specific type.
}
--- | Set up json generation for `ghc -M`.
---
--- Creates a temporary file that we write the dependency information to.
--- The temporary file is written to the final output file path in 'writeJsonOutput'.
-mkJsonOutput ::
- Logger ->
- TmpFs ->
- DynFlags ->
- FilePath ->
- IO (IORef DepJson) ->
- IO JsonOutput
-mkJsonOutput logger tmpfs dflags json_path mk_ref = do
- json_ref <- mk_ref
- -- open a new temp file in which to stuff the dependency info
+data JsonDepFiles = JsonDepFiles
+ { json_path :: FilePath
+ -- ^ The output file path specified as argument to the flag.
+ , json_tmp_file :: FilePath
+ -- ^ Name of the temporary file
+ , json_tmp_hdl :: Handle
+ -- ^ Handle of the open temporary file
+ }
+
+initJsonDepFiles :: Logger -> TmpFs -> TempDir -> FilePath -> IO JsonDepFiles
+initJsonDepFiles logger tmpfs tmpdir path = do
+ -- open a new temp file in which to stuff the dependency info into
-- as we go along.
- json_tmp_file <- newTempName logger tmpfs (tmpDir dflags) TFL_CurrentModule "dep"
+ json_tmp_file <- newTempName logger tmpfs tmpdir TFL_CurrentModule "dep-json"
json_tmp_hdl <- openFile json_tmp_file WriteMode
-
- pure JsonOutput {json_ref, json_path, json_tmp_file, json_tmp_hdl}
-
-updateJson :: JsonOutput -> (DepJson -> DepJson) -> IO ()
-updateJson JsonOutput {json_ref} f = modifyIORef' json_ref f
-
-readJson :: JsonOutput -> IO DepJson
-readJson JsonOutput {json_ref} = readIORef json_ref
+ pure $ JsonDepFiles
+ { json_path = path
+ , json_tmp_file = json_tmp_file
+ , json_tmp_hdl = json_tmp_hdl
+ }
-- | Finalise the json generation by copying the temporary file to the final destination.
-- Avoids partial writes in the case of an error.
-writeJsonOutput ::
- Logger ->
- JsonOutput ->
- IO ()
-writeJsonOutput logger JsonOutput {json_path, json_tmp_file, json_tmp_hdl} = do
+endJsonDepFiles :: Logger -> JsonDepFiles -> IO ()
+endJsonDepFiles logger JsonDepFiles {json_path, json_tmp_file, json_tmp_hdl} = do
hClose json_tmp_hdl
-- Copy the new jsonl in place
showPass logger "Installing new json output"
SysTools.copyFile json_tmp_file json_path
+-- | Set up json generation for `ghc -M`.
+mkJsonOutput :: IO JsonOutput
+mkJsonOutput =
+ JsonOutput <$> newIORef (DepJson Set.empty)
+
+updateJson :: JsonOutput -> (DepJson -> DepJson) -> IO ()
+updateJson JsonOutput {json_ref} f = modifyIORef' json_ref f
+
+readJson :: JsonOutput -> IO DepJson
+readJson JsonOutput {json_ref} = readIORef json_ref
+
--------------------------------------------------------------------------------
-- Output helpers
-writeJsonl :: ToJson a => JsonOutput -> a -> IO ()
-writeJsonl JsonOutput {json_tmp_hdl} output = do
- hPutStrLn json_tmp_hdl (showSDocUnsafe $ renderJSON $ json output)
+renderJsonl :: ToJson a => a -> String
+renderJsonl output =
+ showSDocUnsafe $ renderJSON $ json output
--------------------------------------------------------------------------------
-- Payload for -dep-json
@@ -788,9 +888,6 @@ instance ToJson ExtUnitDep where
where
showPackageId (DPackageId (PackageId pid)) = unpackFS pid
-initDepJson :: IO (IORef DepJson)
-initDepJson = newIORef $ DepJson Set.empty
-
cacheExternalDepsJson :: Set.Set ExtUnitDep -> DepJson -> DepJson
cacheExternalDepsJson extUnits (DepJson e0) =
DepJson
@@ -798,19 +895,10 @@ cacheExternalDepsJson extUnits (DepJson e0) =
Set.union e0 extUnits
}
-writeHomeUnitDeps :: JsonOutput -> DepNode -> [Dep] -> IO ()
-writeHomeUnitDeps deps_json DepNode {..} deps = do
- extUnits <- readJson deps_json
- let newExtUnits = Set.difference extUnitsDeps (externalDeps extUnits)
- -- Write new external unit deps before the module that uses them
- for_ newExtUnits $ \ extUnit -> do
- writeJsonl deps_json extUnit
- writeJsonl deps_json payload
- -- Cache external deps we already reported in the past
- updateJson deps_json $ cacheExternalDepsJson newExtUnits
+homeUnitDeps :: Foldable t => DepNode -> t Dep -> (Set.Set ExtUnitDep, HomeUnitDep)
+homeUnitDeps DepNode {..} deps =
+ foldl' go (Set.empty, initial_node_data) deps
where
- (extUnitsDeps, payload) = foldl' go (Set.empty, initial_node_data) deps
-
initial_node_data =
HomeUnitDep
{ unitId = dn_unitId
=====================================
compiler/GHC/Driver/Session.hs
=====================================
@@ -790,16 +790,44 @@ addOptCmmP f = alterToolSettings $ \s -> s
}
setDepMakefile :: FilePath -> DynFlags -> DynFlags
-setDepMakefile f d = d { depMakefile = Just f }
+setDepMakefile f d = d { depMakefile = MkDepend_Explicit f
+ -- depJson has no default value, we add this for symmetry with `setDepJson`
+ -- and if anything changes in the future, then the code is already here :)
+ , depJson = unsetMkDependOutputIfDefault (depJson d)
+ }
+
+setDepMakefileStdout :: DynFlags -> DynFlags
+setDepMakefileStdout d = d { depMakefile = MkDepend_StdOut
+ -- depJson has no default value, we add this for symmetry with `setDepJson`
+ -- and if anything changes in the future, then the code is already here :)
+ , depJson = unsetMkDependOutputIfDefault (depJson d)
+ }
unsetDepMakefile :: DynFlags -> DynFlags
-unsetDepMakefile d = d { depMakefile = Nothing }
+unsetDepMakefile d = d { depMakefile = MkDepend_None }
setDepJson :: FilePath -> DynFlags -> DynFlags
-setDepJson f d = d { depJson = Just f }
+setDepJson f d = d { depJson = MkDepend_Explicit f
+ -- We generate a Makefile by default, but if a user requests json
+ -- output, then they most likely don't need a Makefile.
+ -- Thus, we only generate it if the user requests it.
+ , depMakefile = unsetMkDependOutputIfDefault (depMakefile d)
+ }
+
+setDepJsonStdout :: DynFlags -> DynFlags
+setDepJsonStdout d = d { depJson = MkDepend_StdOut
+ -- We generate a Makefile by default, but if a user requests json
+ -- output, then they most likely don't need a Makefile.
+ -- Thus, we only generate it if the user requests it.
+ , depMakefile = unsetMkDependOutputIfDefault (depMakefile d)
+ }
unsetDepJson :: DynFlags -> DynFlags
-unsetDepJson d = d { depJson = Nothing }
+unsetDepJson d = d { depJson = MkDepend_None }
+
+unsetMkDependOutputIfDefault :: MkDependOutput -> MkDependOutput
+unsetMkDependOutputIfDefault (MkDepend_Default _) = MkDepend_None
+unsetMkDependOutputIfDefault mkDepend_output = mkDepend_output
setDepIncludeCppDeps :: Bool -> DynFlags -> DynFlags
setDepIncludeCppDeps b d = d { depIncludeCppDeps = b }
@@ -1270,8 +1298,10 @@ dynamic_flags_deps = [
-------- ghc -M -----------------------------------------------------
, make_ord_flag defGhcFlag "dep-suffix" (hasArg addDepSuffix)
+ , make_ord_flag defGhcFlag "dep-makefile-stdout" (noArg setDepMakefileStdout)
, make_ord_flag defGhcFlag "dep-makefile" (hasArg setDepMakefile)
, make_ord_flag defGhcFlag "no-dep-makefile" (noArg unsetDepMakefile)
+ , make_ord_flag defGhcFlag "dep-json-stdout" (noArg setDepJsonStdout)
, make_ord_flag defGhcFlag "dep-json" (hasArg setDepJson)
, make_ord_flag defGhcFlag "no-dep-json" (noArg unsetDepJson)
, make_ord_flag defGhcFlag "include-cpp-deps"
=====================================
docs/users_guide/separate_compilation.rst
=====================================
@@ -1511,6 +1511,23 @@ generation are:
Introduced in GHC 10.1, ``-no-dep-makefile`` disables the generation of a Makefile altogether.
+.. ghc-flag:: -dep-makefile-stdout
+ :shortdesc: Write dependency generation to stdout using makefile format
+ :type: dynamic
+ :reverse: -no-dep-makefile
+ :category: redirect-output
+
+ :since: 10.1
+
+ Stream the dependency generation of :ghc-flag:`-dep-makefile ⟨file⟩` to stdout instead of ⟨file⟩.
+
+ Contrary to :ghc-flag:`-dep-makefile ⟨file⟩`, the markers
+
+ * "``# DO NOT DELETE: Beginning of Haskell dependencies``" and
+ * "``# DO NOT DELETE: End of Haskell dependencies``"
+
+ are not emitted.
+
.. ghc-flag:: -dep-json ⟨file⟩
:shortdesc: Write dependencies to ⟨file⟩ as a JSON Lines format
:type: dynamic
@@ -1540,8 +1557,19 @@ generation are:
``-no-dep-json`` disables the generation of the JSON file.
- By default, :ghc-flag:`-dep-makefile ⟨file⟩` is enabled even if :ghc-flag:`-dep-json ⟨file⟩` is given.
- Disable the generation of any Makefile via ``-no-dep-makefile``.
+ :ghc-flag:`-dep-json ⟨file⟩` and :ghc-flag:`-dep-json-stdout` disable the default generation of a Makefile.
+ To generate a Makefile for the dependency information, explicitly use :ghc-flag:`-dep-makefile ⟨file⟩` or
+ :ghc-flag:`-dep-makefile-stdout`.
+
+.. ghc-flag:: -dep-json-stdout
+ :shortdesc: Write dependency generation to stdout using JSON Lines format
+ :type: dynamic
+ :reverse: -no-dep-json
+ :category: redirect-output
+
+ :since: 10.1
+
+ Stream the dependency generation of :ghc-flag:`-dep-json ⟨file⟩` to stdout instead of ⟨file⟩.
.. ghc-flag:: -dep-suffix ⟨suffix⟩
:shortdesc: Make dependencies that declare that files with suffix
=====================================
testsuite/driver/my_typing.py
=====================================
@@ -10,7 +10,7 @@ The testsuite driver can be typechecked using mypy [1].
from typing import *
import typing
-
+import enum
####################################################
# Backwards compatibility shims
@@ -34,3 +34,7 @@ GitHash = NewType("GitHash", str) # a Git commit hash
GitRef = NewType("GitRef", str)
TestEnv = NewType("TestEnv", str)
MetricName = NewType("MetricName", str)
+
+# Used by `ghc -M` test mode
+DepMode = enum.Enum('DepMode', [('NONE', 0), ('FILE', 1), ('STDOUT', 2)])
+
=====================================
testsuite/driver/testglobals.py
=====================================
@@ -390,8 +390,8 @@ class TestOptions:
self.ignore_extension = False
# make_depend tests: which outputs to write and check
- self.dep_makefile = False
- self.dep_json = False
+ self.dep_makefile: DepMode = DepMode.NONE
+ self.dep_json: DepMode = DepMode.NONE
self.ignore_dep_outputs = False
# Backpack test
=====================================
testsuite/driver/testlib.py
=====================================
@@ -438,14 +438,26 @@ def dep_makefile( name, opts ):
For make_depend tests: write the -dep-makefile output and
compare it against <name>.dep-makefile.
"""
- opts.dep_makefile = True
+ opts.dep_makefile = DepMode.FILE
def dep_json( name, opts ):
"""
For make_depend tests: write the -dep-json output and
compare it against <name>.dep-json.
"""
- opts.dep_json = True
+ opts.dep_json = DepMode.FILE
+
+def dep_makefile_stdout( name, opts ):
+ """
+ For make_depend tests: add -dep-makefile-stdout flag.
+ """
+ opts.dep_makefile = DepMode.STDOUT
+
+def dep_json_stdout( name, opts ):
+ """
+ For make_depend tests: add -dep-json-stdout flag.
+ """
+ opts.dep_json = DepMode.STDOUT
def ignore_dep_outputs( name, opts ):
"""
@@ -2052,6 +2064,10 @@ async def do_make_depend(name: TestName,
against <name>.dep-makefile. Otherwise, -no-dep-makefile is passed.
* 'dep_json': write the -dep-json output and compare it
against <name>.dep-json.
+ * 'dep_json_stdout': write the -dep-json-stdout and compare it with
+ <name>.stderr
+ * 'dep_makefile_stdout': write the -dep-makefile-stdout and compare it with
+ <name>.stderr
"""
opts = getTestOpts()
@@ -2060,14 +2076,29 @@ async def do_make_depend(name: TestName,
outputs = []
output_flags = []
- if opts.dep_makefile:
+ if opts.dep_makefile == DepMode.FILE:
outputs.append(('dep-makefile', normalise_dep_makefile))
output_flags.append('-dep-makefile %s' % actual_file('dep-makefile'))
- else:
- output_flags.append('-no-dep-makefile')
- if opts.dep_json:
+ elif opts.dep_makefile == DepMode.STDOUT:
+ outputs.append(('stdout', normalise_dep_makefile))
+ output_flags.append('-dep-makefile-stdout')
+
+ if opts.dep_json == DepMode.FILE:
outputs.append(('dep-json', normalise_dep_json))
output_flags.append('-dep-json %s' % actual_file('dep-json'))
+ elif opts.dep_json == DepMode.STDOUT:
+ outputs.append(('stdout', normalise_dep_json))
+ output_flags.append('-dep-json-stdout')
+
+ # Somewhat non-sensical here, but `do_compile` outputs stdout and stderr into the same.
+ # Then, it only applies the `normalise_errmsg_fun` to the output, and compares it against
+ # <name>.stderr as the golden file.
+ #
+ # This is not really what we want in this situation, but it seems this requires more refactoring
+ # of the `do_compile` function than what fendor is ready to commit to.
+ # Thus, as a workaround, we simply add the normalisers for the stderr...
+ stdout_normalisers = [ n for (k, n) in outputs if k == 'stdout' ]
+ opts.extra_errmsg_normaliser = join_normalisers(opts.extra_errmsg_normaliser, normalise_slashes_, *stdout_normalisers)
result = await do_compile( name, way, False, None, [], units,
' '.join(output_flags + [extra_hc_opts]),
@@ -2077,9 +2108,9 @@ async def do_make_depend(name: TestName,
for (kind, normaliser) in outputs:
output_match = await compare_outputs(way, kind,
- join_normalisers(opts.extra_normaliser,
- normalise_slashes_, normaliser),
- find_expected_file(name, kind, way), actual_file(kind))
+ join_normalisers(opts.extra_normaliser, normalise_slashes_, normaliser),
+ find_expected_file(name, kind, way),
+ actual_file(kind))
if not output_match:
return failBecause(kind + ' mismatch', diff=output_match.diff)
=====================================
testsuite/tests/driver/mkdepend/all.T
=====================================
@@ -15,9 +15,20 @@ test('T24384b', [ *T24384_extras, when(opsys('mingw32'), skip), *json_normaliser
test('T24384c', [ *T24384_extras, when(opsys('mingw32'), skip), *json_normalisers ], makefile_test, [])
# Multiple home units with explicit level imports.
-test('mkdepend_mhu01',
+test('mkdepend_mhu01a',
[ extra_files(['mhu01/']), dep_makefile, dep_json ],
make_depend_multiunit, [['mhu01/unitA', 'mhu01/unitB', 'mhu01/unitC'], ''])
+# test the output streaming and writing to files
+test('mkdepend_mhu01b',
+ [ extra_files(['mhu01/']), dep_json_stdout ],
+ make_depend_multiunit, [['mhu01/unitA', 'mhu01/unitB', 'mhu01/unitC'], ''])
+test('mkdepend_mhu01c',
+ [ extra_files(['mhu01/']), dep_makefile_stdout ],
+ make_depend_multiunit, [['mhu01/unitA', 'mhu01/unitB', 'mhu01/unitC'], ''])
+test('mkdepend_mhu01d',
+ [ extra_files(['mhu01/']), dep_json_stdout, dep_makefile ],
+ make_depend_multiunit, [['mhu01/unitA', 'mhu01/unitB', 'mhu01/unitC'], ''])
+
# Preprocessor must not be inherited by default active home unit
test('mkdepend_mhu02',
[ extra_files(['mhu02/']), dep_json
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01.dep-json → testsuite/tests/driver/mkdepend/mkdepend_mhu01a.dep-json
=====================================
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01.dep-makefile → testsuite/tests/driver/mkdepend/mkdepend_mhu01a.dep-makefile
=====================================
@@ -1,23 +1,23 @@
# DO NOT DELETE: Beginning of Haskell dependencies
+mhu01/a/A/Base.o-boot : mhu01/a/A/Base.hs-boot
mhu01/a/A/Base.o : mhu01/a/A/Base.hi-boot
+mhu01/a/Util.o : mhu01/a/Util.hs
+mhu01/a/Util.o : mhu01/a/A/Base.hi-boot
mhu01/a/A/Base.o : mhu01/a/A/Base.hs
mhu01/a/A/Base.o : mhu01/a/Util.hi
-mhu01/a/A/Base.o-boot : mhu01/a/A/Base.hs-boot
-mhu01/a/A/TH.o : mhu01/a/A/Base.hi
mhu01/a/A/TH.o : mhu01/a/A/TH.hs
-mhu01/a/Util.o : mhu01/a/A/Base.hi-boot
-mhu01/a/Util.o : mhu01/a/Util.hs
-mhu01/b/B/Normal.o : mhu01/a/A/Base.hi
-mhu01/b/B/Normal.o : mhu01/b/B/Normal.hs
-mhu01/b/B/Normal.o : mhu01/b/B/Splice.hi
-mhu01/b/B/Splice.o : mhu01/a/A/Base.hi
-mhu01/b/B/Splice.o : mhu01/a/A/TH.hi
+mhu01/a/A/TH.o : mhu01/a/A/Base.hi
+mhu01/b/Util.o : mhu01/b/Util.hs
mhu01/b/B/Splice.o : mhu01/b/B/Splice.hs
mhu01/b/B/Splice.o : mhu01/b/Util.hi
-mhu01/b/Util.o : mhu01/b/Util.hs
-mhu01/c/C.o : mhu01/a/Util.hi
-mhu01/c/C.o : mhu01/b/B/Normal.hi
+mhu01/b/B/Splice.o : mhu01/a/A/Base.hi
+mhu01/b/B/Splice.o : mhu01/a/A/TH.hi
+mhu01/b/B/Normal.o : mhu01/b/B/Normal.hs
+mhu01/b/B/Normal.o : mhu01/b/B/Splice.hi
+mhu01/b/B/Normal.o : mhu01/a/A/Base.hi
+mhu01/c/C.o : mhu01/c/C.hs
mhu01/c/C.o : mhu01/b/B/Splice.hi
+mhu01/c/C.o : mhu01/b/B/Normal.hi
mhu01/c/C.o : mhu01/b/Util.hi
-mhu01/c/C.o : mhu01/c/C.hs
+mhu01/c/C.o : mhu01/a/Util.hi
# DO NOT DELETE: End of Haskell dependencies
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01b.stderr
=====================================
@@ -0,0 +1,8 @@
+{"module":"A.Base","unit-id":"a","boot":true,"imports":{},"package-name":"a","source":"mhu01/a/A/Base.hs-boot"}
+{"module":"Util","unit-id":"a","imports":{"a":[{"module":"A.Base","boot":true}]},"package-name":"a","source":"mhu01/a/Util.hs"}
+{"module":"A.Base","unit-id":"a","imports":{"a":["Util"]},"package-name":"a","source":"mhu01/a/A/Base.hs"}
+{"module":"A.TH","unit-id":"a","imports":{"a":["A.Base"]},"package-name":"a","source":"mhu01/a/A/TH.hs"}
+{"module":"Util","unit-id":"b","imports":{},"package-name":"b","source":"mhu01/b/Util.hs"}
+{"module":"B.Splice","unit-id":"b","imports":{"a":[{"module":"A.Base","level":"quote"},{"module":"A.TH","level":"splice"}],"b":["Util"]},"package-name":"b","source":"mhu01/b/B/Splice.hs"}
+{"module":"B.Normal","unit-id":"b","imports":{"a":["A.Base"],"b":["B.Splice"]},"package-name":"b","source":"mhu01/b/B/Normal.hs"}
+{"module":"C","unit-id":"c","imports":{"a":[{"module":"Util","level":"splice"}],"b":["B.Normal",{"module":"B.Splice","level":"splice"},{"module":"Util","level":"quote"}]},"package-name":"c","source":"mhu01/c/C.hs"}
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01c.stderr
=====================================
@@ -0,0 +1,21 @@
+mhu01/a/A/Base.o-boot : mhu01/a/A/Base.hs-boot
+mhu01/a/A/Base.o : mhu01/a/A/Base.hi-boot
+mhu01/a/Util.o : mhu01/a/Util.hs
+mhu01/a/Util.o : mhu01/a/A/Base.hi-boot
+mhu01/a/A/Base.o : mhu01/a/A/Base.hs
+mhu01/a/A/Base.o : mhu01/a/Util.hi
+mhu01/a/A/TH.o : mhu01/a/A/TH.hs
+mhu01/a/A/TH.o : mhu01/a/A/Base.hi
+mhu01/b/Util.o : mhu01/b/Util.hs
+mhu01/b/B/Splice.o : mhu01/b/B/Splice.hs
+mhu01/b/B/Splice.o : mhu01/b/Util.hi
+mhu01/b/B/Splice.o : mhu01/a/A/Base.hi
+mhu01/b/B/Splice.o : mhu01/a/A/TH.hi
+mhu01/b/B/Normal.o : mhu01/b/B/Normal.hs
+mhu01/b/B/Normal.o : mhu01/b/B/Splice.hi
+mhu01/b/B/Normal.o : mhu01/a/A/Base.hi
+mhu01/c/C.o : mhu01/c/C.hs
+mhu01/c/C.o : mhu01/b/B/Splice.hi
+mhu01/c/C.o : mhu01/b/B/Normal.hi
+mhu01/c/C.o : mhu01/b/Util.hi
+mhu01/c/C.o : mhu01/a/Util.hi
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01d.dep-makefile
=====================================
@@ -0,0 +1,23 @@
+# DO NOT DELETE: Beginning of Haskell dependencies
+mhu01/a/A/Base.o-boot : mhu01/a/A/Base.hs-boot
+mhu01/a/A/Base.o : mhu01/a/A/Base.hi-boot
+mhu01/a/Util.o : mhu01/a/Util.hs
+mhu01/a/Util.o : mhu01/a/A/Base.hi-boot
+mhu01/a/A/Base.o : mhu01/a/A/Base.hs
+mhu01/a/A/Base.o : mhu01/a/Util.hi
+mhu01/a/A/TH.o : mhu01/a/A/TH.hs
+mhu01/a/A/TH.o : mhu01/a/A/Base.hi
+mhu01/b/Util.o : mhu01/b/Util.hs
+mhu01/b/B/Splice.o : mhu01/b/B/Splice.hs
+mhu01/b/B/Splice.o : mhu01/b/Util.hi
+mhu01/b/B/Splice.o : mhu01/a/A/Base.hi
+mhu01/b/B/Splice.o : mhu01/a/A/TH.hi
+mhu01/b/B/Normal.o : mhu01/b/B/Normal.hs
+mhu01/b/B/Normal.o : mhu01/b/B/Splice.hi
+mhu01/b/B/Normal.o : mhu01/a/A/Base.hi
+mhu01/c/C.o : mhu01/c/C.hs
+mhu01/c/C.o : mhu01/b/B/Splice.hi
+mhu01/c/C.o : mhu01/b/B/Normal.hi
+mhu01/c/C.o : mhu01/b/Util.hi
+mhu01/c/C.o : mhu01/a/Util.hi
+# DO NOT DELETE: End of Haskell dependencies
=====================================
testsuite/tests/driver/mkdepend/mkdepend_mhu01d.stderr
=====================================
@@ -0,0 +1,8 @@
+{"module":"A.Base","unit-id":"a","boot":true,"imports":{},"package-name":"a","source":"mhu01/a/A/Base.hs-boot"}
+{"module":"Util","unit-id":"a","imports":{"a":[{"module":"A.Base","boot":true}]},"package-name":"a","source":"mhu01/a/Util.hs"}
+{"module":"A.Base","unit-id":"a","imports":{"a":["Util"]},"package-name":"a","source":"mhu01/a/A/Base.hs"}
+{"module":"A.TH","unit-id":"a","imports":{"a":["A.Base"]},"package-name":"a","source":"mhu01/a/A/TH.hs"}
+{"module":"Util","unit-id":"b","imports":{},"package-name":"b","source":"mhu01/b/Util.hs"}
+{"module":"B.Splice","unit-id":"b","imports":{"a":[{"module":"A.Base","level":"quote"},{"module":"A.TH","level":"splice"}],"b":["Util"]},"package-name":"b","source":"mhu01/b/B/Splice.hs"}
+{"module":"B.Normal","unit-id":"b","imports":{"a":["A.Base"],"b":["B.Splice"]},"package-name":"b","source":"mhu01/b/B/Normal.hs"}
+{"module":"C","unit-id":"c","imports":{"a":[{"module":"Util","level":"splice"}],"b":["B.Normal",{"module":"B.Splice","level":"splice"},{"module":"Util","level":"quote"}]},"package-name":"c","source":"mhu01/c/C.hs"}
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/53e360844be69a5da0a395f790eb206…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/53e360844be69a5da0a395f790eb206…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/andreask/9.10.4-batch1] 3 commits: rts/posix: Enforce iteration limit on heap reservation logic
by Andreas Klebinger (@AndreasK) 23 Oct '26
by Andreas Klebinger (@AndreasK) 23 Oct '26
23 Oct '26
Andreas Klebinger pushed to branch wip/andreask/9.10.4-batch1 at Glasgow Haskell Compiler / GHC
Commits:
f6a2555b by Ben Gamari at 2026-10-23T09:38:10+02:00
rts/posix: Enforce iteration limit on heap reservation logic
Previously we could loop indefinitely when attempting to get an address
space reservation for our heap. Limit the logic to 8 iterations to
ensure we instead issue a reasonable error message.
Addresses #26151.
(cherry picked from commit a48db5706800ed2691d219323a6fdb5d7a73c638)
- - - - -
e81d64e1 by Ben Gamari at 2026-10-23T09:38:25+02:00
rts/posix: Hold on to low reservations when reserving heap
Previously when the OS gave us an address space reservation in low
memory we would immediately release it and try again. However, on some
platforms this meant that we would get the same allocation again in the
next iteration (since mmap's `hint` argument is just that, a hint).
Instead we now hold on to low reservations until we have found a
suitable heap reservation.
Fixes #26151.
(cherry picked from commit 3ab77586e6daae5c7de481d2b9532bf499b587ff)
- - - - -
44b8c8f0 by Ben Gamari at 2026-10-23T09:38:37+02:00
rts: Eliminate uses of implicit constant arrays
Folding of `const`-sized variable-length arrays to a constant-length
array is a gnu extension which clang complains about.
Closes #26502.
(cherry picked from commit 064107a57b90a2981049e3158b5904f8f3fb506f)
- - - - -
2 changed files:
- rts/Printer.c
- rts/posix/OSMem.c
Changes:
=====================================
rts/Printer.c
=====================================
@@ -994,8 +994,8 @@ findPtr(P_ p, int follow)
{
uint32_t g, n;
bdescr *bd;
- const int arr_size = 1024;
- StgPtr arr[arr_size];
+#define ARR_SIZE 1024
+ StgPtr arr[ARR_SIZE];
int i = 0;
searched = 0;
@@ -1005,24 +1005,24 @@ findPtr(P_ p, int follow)
// just before a block is used.
for (n = 0; n < getNumCapabilities(); n++) {
bd = nurseries[i].blocks;
- i = findPtrBlocks(p,bd,arr,arr_size,i);
- if (i >= arr_size) return;
+ i = findPtrBlocks(p,bd,arr,ARR_SIZE,i);
+ if (i >= ARR_SIZE) return;
}
#endif
for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
bd = generations[g].blocks;
- i = findPtrBlocks(p,bd,arr,arr_size,i);
+ i = findPtrBlocks(p,bd,arr,ARR_SIZE,i);
bd = generations[g].large_objects;
- i = findPtrBlocks(p,bd,arr,arr_size,i);
- if (i >= arr_size) return;
+ i = findPtrBlocks(p,bd,arr,ARR_SIZE,i);
+ if (i >= ARR_SIZE) return;
for (n = 0; n < getNumCapabilities(); n++) {
i = findPtrBlocks(p, gc_threads[n]->gens[g].part_list,
- arr, arr_size, i);
+ arr, ARR_SIZE, i);
i = findPtrBlocks(p, gc_threads[n]->gens[g].todo_bd,
- arr, arr_size, i);
+ arr, ARR_SIZE, i);
}
- if (i >= arr_size) return;
+ if (i >= ARR_SIZE) return;
}
if (follow && i == 1) {
debugBelch("-->\n");
=====================================
rts/posix/OSMem.c
=====================================
@@ -585,8 +585,14 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len)
}
#endif
+#define MAX_ATTEMPTS 256
+ void *bad_allocs[MAX_ATTEMPTS];
+ size_t bad_alloc_lens[MAX_ATTEMPTS];
+ memset(bad_allocs, 0, sizeof(void*) * MAX_ATTEMPTS);
+ memset(bad_alloc_lens, 0, sizeof(size_t) * MAX_ATTEMPTS);
+
attempt = 0;
- while (1) {
+ while (attempt < MAX_ATTEMPTS) {
*len &= ~MBLOCK_MASK;
if (*len < MBLOCK_SIZE) {
@@ -611,18 +617,35 @@ void *osReserveHeapMemory(void *startAddressPtr, W_ *len)
} else if ((W_)at >= minimumAddress) {
// Success! We were given a block of memory starting above the 8 GB
// mark, which is what we were looking for.
-
break;
} else {
// We got addressing space but it wasn't above the 8GB mark.
- // Try again.
- if (munmap(at, *len) < 0) {
- sysErrorBelch("unable to release reserved heap");
+ // Free any portion *above* 8GB and hang on to the rest to increase
+ // the likelihood that we get a suitable allocation next iteration.
+ uintptr_t end = (W_) at + *len;
+ bad_allocs[attempt] = at;
+ if (end > minimumAddress) {
+ if (munmap((void *) minimumAddress, end - minimumAddress) < 0) {
+ sysErrorBelch("unable to release high portion of low memory reservation");
+ }
+ bad_alloc_lens[attempt] = minimumAddress - (W_) at;
+ } else {
+ bad_alloc_lens[attempt] = *len;
}
}
attempt++;
}
+ for (int i=0; i < MAX_ATTEMPTS; i++) {
+ if (bad_allocs[i] != NULL && munmap(bad_allocs[i], bad_alloc_lens[i]) < 0) {
+ sysErrorBelch("unable to release reserved heap");
+ }
+ }
+
+ if (at == NULL) {
+ sysErrorBelch("failed to reserve heap memory");
+ }
+
return at;
}
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/93a33fc3f15c421f248ae36bc3e67b…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/93a33fc3f15c421f248ae36bc3e67b…
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] 4 commits: add options for resizing bytearrays (#27244)
by Marge Bot (@marge-bot) 23 Oct '26
by Marge Bot (@marge-bot) 23 Oct '26
23 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
13dff5d0 by L0neGamer at 2026-10-22T21:37:31-04:00
add options for resizing bytearrays (#27244)
add five mutable bytearray resizing operations
for various kinds of pinnedness and aligning.
- prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default)
- prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``)
- prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``)
- prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``)
- ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``)
- - - - -
0c17201c by Sylvain Henry at 2026-10-22T21:38:23-04:00
JS: implement performMinorGC and performBlockingMajorGC (#28012)
GHC.Internal.System.Mem imports three C entry points: performMajorGC,
performGC (for performMinorGC) and performBlockingMajorGC. The JS RTS
only defined h$performMajorGC, so calling performMinorGC or
performBlockingMajorGC threw "ReferenceError: h$performGC is not
defined" at runtime.
The JS RTS has a single non-generational collector, so both missing
entry points are now aliases of h$performMajorGC.
Add a test that calls every GC entry point of System.Mem.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
3d07c181 by L0neGamer at 2026-10-22T22:10:59-04:00
Mon(a|oi)d of No (Return|Mappend) progress (!15795)
Make non-canonical definitions of return and mappend
non-suppressible errors, and make canonical definitions
of those methods warnings.
CLC proposal: https://github.com/haskell/core-libraries-committee/issues/328
- - - - -
29851eb7 by Simon Jakobi at 2026-10-22T22:11:01-04:00
Cmm: keep the CBE substitution idempotent
When blocks are merged over multiple rounds during common block
elimination, a block that won a merge in an earlier round can still
lose a merge in a later round. The substitution map (Subst) used to
record this as a chain: the earlier loser mapped to a block that was
itself eliminated. In PLT terminology, the substitution became
*non-idempotent*. Applying such a substitution with a single lookup
left edges to unreachable blocks in the Cmm graph, eventually leading
to a setInfoTableStackMap panic (#27368).
With this patch, each round's fresh substitutions are applied to the old
ones as they are added (extendSubst), so every eliminated block maps
directly to its final replacement and the substitution stays idempotent.
Fixes #27368.
Assisted-by: Claude Fable 5.1
- - - - -
121 changed files:
- + changelog.d/27368
- + changelog.d/28012
- + changelog.d/monad-of-no-return-canonical
- + changelog.d/resizeMutableByteArray-pinnedness
- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/ByteCode/Asm.hs
- compiler/GHC/Cmm/CommonBlockElim.hs
- compiler/GHC/CmmToAsm/CFG/Dominators.hs
- compiler/GHC/CmmToLlvm/CodeGen.hs
- compiler/GHC/Data/FastString.hs
- compiler/GHC/Data/OrdList.hs
- compiler/GHC/Data/Pair.hs
- compiler/GHC/Data/Word64Map/Internal.hs
- compiler/GHC/Data/Word64Set/Internal.hs
- compiler/GHC/Driver/CmdLine.hs
- compiler/GHC/Driver/Flags.hs
- compiler/GHC/Driver/Session.hs
- compiler/GHC/HsToCore/Pmc/Types.hs
- compiler/GHC/Iface/Type.hs
- compiler/GHC/JS/Opt/Simple.hs
- compiler/GHC/Rename/Env.hs
- compiler/GHC/Rename/Module.hs
- compiler/GHC/StgToCmm/Prim.hs
- compiler/GHC/StgToJS/Prim.hs
- compiler/GHC/Tc/Deriv/Generate.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Solver/Monad.hs
- compiler/GHC/Tc/Types/Constraint.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Types/Basic.hs
- compiler/GHC/Types/Error/Codes.hs
- compiler/GHC/Types/Hint.hs
- compiler/GHC/Types/Hint/Ppr.hs
- compiler/GHC/Types/Unique/FM.hs
- compiler/GHC/Types/Unique/Map.hs
- compiler/GHC/Unit/External/ModuleOrigin.hs
- compiler/GHC/Unit/External/Visibility.hs
- compiler/GHC/Utils/Monad/Codensity.hs
- compiler/GHC/Utils/Ppr/Colour.hs
- compiler/ghc.cabal.in
- docs/users_guide/using-warnings.rst
- ghc/ghc-bin.cabal.in
- hadrian/src/Settings/Warnings.hs
- libraries/base/src/GHC/Base.hs
- libraries/base/src/GHC/Exts.hs
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Base.hs
- libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc
- rts/PrimOps.cmm
- rts/RtsSymbols.c
- rts/include/stg/MiscClosures.h
- rts/js/environment.js
- testsuite/tests/ado/ado-optimal.hs
- testsuite/tests/ado/ado001.hs
- + testsuite/tests/codeGen/should_compile/T27368.hs
- testsuite/tests/codeGen/should_compile/all.T
- testsuite/tests/concurrent/T13615/Parallel.hs
- testsuite/tests/concurrent/should_run/T21651.hs
- testsuite/tests/concurrent/should_run/setnumcapabilities001.hs
- testsuite/tests/cpranal/should_run/T20598a.hs
- testsuite/tests/deriving/should_compile/T13758.hs
- testsuite/tests/deriving/should_run/all.T
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.hs
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.stderr
- + testsuite/tests/deriving/should_run/drvrun-canonical-methods.stdout
- testsuite/tests/determinism/determ017/A.hs
- testsuite/tests/dmdanal/should_compile/T9208.hs
- testsuite/tests/generics/T27245.hs
- testsuite/tests/generics/T27245.stdout
- testsuite/tests/haddock/perf/Fold.hs
- testsuite/tests/hiefile/should_compile/hie002.hs
- testsuite/tests/hpc/raytrace/Eval.hs
- testsuite/tests/indexed-types/should_compile/T3787.hs
- testsuite/tests/indexed-types/should_fail/T7729.hs
- testsuite/tests/indexed-types/should_fail/T7729a.hs
- testsuite/tests/interface-stability/ghc-experimental-exports.stdout
- testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32
- testsuite/tests/interface-stability/ghc-prim-exports.stdout
- testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32
- testsuite/tests/module/mod133.hs
- testsuite/tests/perf/compiler/T13253.hs
- testsuite/tests/perf/compiler/T14683.hs
- testsuite/tests/perf/compiler/T16473.hs
- testsuite/tests/perf/compiler/T18730_A.hs
- testsuite/tests/perf/compiler/T25723.hs
- testsuite/tests/pmcheck/should_compile/T11276.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
- testsuite/tests/primops/should_run/all.T
- testsuite/tests/profiling/should_compile/T19894/StreamK.hs
- testsuite/tests/profiling/should_run/T3001-2.hs
- testsuite/tests/programs/galois_raytrace/Eval.hs
- testsuite/tests/rebindable/rebindable2.hs
- + testsuite/tests/rename/should_fail/Noncanonical.hs
- + testsuite/tests/rename/should_fail/Noncanonical.stderr
- testsuite/tests/rename/should_fail/all.T
- testsuite/tests/rename/should_fail/rnfail026.hs
- + testsuite/tests/rts/T28012.hs
- + testsuite/tests/rts/T28012.stdout
- testsuite/tests/rts/all.T
- testsuite/tests/rts/ipe/IpeStats/Fold.hs
- testsuite/tests/simplCore/should_compile/T14908_Deps.hs
- testsuite/tests/simplCore/should_compile/simpl017.hs
- testsuite/tests/typecheck/should_compile/DeepSubsumption02.hs
- testsuite/tests/typecheck/should_compile/T4524.hs
- testsuite/tests/typecheck/should_compile/T4969.hs
- testsuite/tests/typecheck/should_fail/T22707.hs
- testsuite/tests/typecheck/should_fail/T7019.hs
- testsuite/tests/typecheck/should_fail/T8603.hs
- + testsuite/tests/warnings/should_compile/RedundantCanonicalMethods.hs
- + testsuite/tests/warnings/should_compile/RedundantCanonicalMethods.stderr
- testsuite/tests/warnings/should_compile/WarnNoncanonical.hs
- testsuite/tests/warnings/should_compile/WarnNoncanonical.stderr
- testsuite/tests/warnings/should_compile/all.T
- utils/haddock/haddock-api/haddock-api.cabal
- utils/haddock/haddock-api/src/Haddock/Interface/ParseModuleHeader.hs
- utils/haddock/haddock-api/src/Haddock/Utils/Json.hs
- utils/haddock/haddock-library/haddock-library.cabal
- utils/haddock/haddock.cabal
- utils/runghc/Main.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/c0df89a6b92bef88674c6c003d1346…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/c0df89a6b92bef88674c6c003d1346…
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][master] JS: implement performMinorGC and performBlockingMajorGC (#28012)
by Marge Bot (@marge-bot) 23 Oct '26
by Marge Bot (@marge-bot) 23 Oct '26
23 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
0c17201c by Sylvain Henry at 2026-10-22T21:38:23-04:00
JS: implement performMinorGC and performBlockingMajorGC (#28012)
GHC.Internal.System.Mem imports three C entry points: performMajorGC,
performGC (for performMinorGC) and performBlockingMajorGC. The JS RTS
only defined h$performMajorGC, so calling performMinorGC or
performBlockingMajorGC threw "ReferenceError: h$performGC is not
defined" at runtime.
The JS RTS has a single non-generational collector, so both missing
entry points are now aliases of h$performMajorGC.
Add a test that calls every GC entry point of System.Mem.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
5 changed files:
- + changelog.d/28012
- rts/js/environment.js
- + testsuite/tests/rts/T28012.hs
- + testsuite/tests/rts/T28012.stdout
- testsuite/tests/rts/all.T
Changes:
=====================================
changelog.d/28012
=====================================
@@ -0,0 +1,4 @@
+section: js-backend
+synopsis: The JavaScript RTS now implements ``performMinorGC`` and ``performBlockingMajorGC``; calling them used to throw a JavaScript ``ReferenceError``.
+issues: #28012
+mrs: !16847
=====================================
rts/js/environment.js
=====================================
@@ -389,6 +389,13 @@ function h$performMajorGC() {
if(err) throw err;
}
+function h$performGC() {
+ h$performMajorGC();
+}
+
+function h$performBlockingMajorGC() {
+ h$performMajorGC();
+}
function h$ghczminternalZCSystemziCPUTimeZCgetrusage() {
return 0;
=====================================
testsuite/tests/rts/T28012.hs
=====================================
@@ -0,0 +1,19 @@
+-- Test for #28012.
+-- Check that every GC entry point exported by System.Mem can be called.
+-- The JS RTS used to define only performMajorGC: performMinorGC and
+-- performBlockingMajorGC were foreign imports of C symbols that had no
+-- JS implementation, so calling them threw a ReferenceError at runtime.
+module Main where
+
+import System.Mem
+
+main :: IO ()
+main = do
+ performGC
+ putStrLn "performGC ok"
+ performMinorGC
+ putStrLn "performMinorGC ok"
+ performMajorGC
+ putStrLn "performMajorGC ok"
+ performBlockingMajorGC
+ putStrLn "performBlockingMajorGC ok"
=====================================
testsuite/tests/rts/T28012.stdout
=====================================
@@ -0,0 +1,4 @@
+performGC ok
+performMinorGC ok
+performMajorGC ok
+performBlockingMajorGC ok
=====================================
testsuite/tests/rts/all.T
=====================================
@@ -757,3 +757,5 @@ test('T27618',
[req_target_smp, omit_ghci],
compile_and_run, ['-O2 -threaded -with-rtsopts "-N4 -A32k"'])
+
+test('T28012', normal, compile_and_run, [''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0c17201cd3a4aab2a22d83e1630c59d…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/0c17201cd3a4aab2a22d83e1630c59d…
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][master] add options for resizing bytearrays (#27244)
by Marge Bot (@marge-bot) 23 Oct '26
by Marge Bot (@marge-bot) 23 Oct '26
23 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
13dff5d0 by L0neGamer at 2026-10-22T21:37:31-04:00
add options for resizing bytearrays (#27244)
add five mutable bytearray resizing operations
for various kinds of pinnedness and aligning.
- prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default)
- prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``)
- prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``)
- prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``)
- ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``)
- - - - -
16 changed files:
- + changelog.d/resizeMutableByteArray-pinnedness
- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/StgToCmm/Prim.hs
- compiler/GHC/StgToJS/Prim.hs
- libraries/base/src/GHC/Base.hs
- libraries/base/src/GHC/Exts.hs
- rts/PrimOps.cmm
- rts/RtsSymbols.c
- rts/include/stg/MiscClosures.h
- 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/primops/should_run/ResizeByteArrayPinnedness.hs
- + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
- testsuite/tests/primops/should_run/all.T
Changes:
=====================================
changelog.d/resizeMutableByteArray-pinnedness
=====================================
@@ -0,0 +1,15 @@
+section: ghc-internal
+synopsis: Add five more primops for resizing byte arrays, relating to their pinnedness.
+description: {
+ Add more primops for resizing mutable byte arrays, giving five options:
+
+ - 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#``)
+
+ They can be found in ``GHC.Prim`` and ``GHC.PrimOps``.
+}
+issues: #27244
+mrs: !16010
=====================================
compiler/GHC/Builtin/primops.txt.pp
=====================================
@@ -2033,14 +2033,22 @@ primop NewByteArrayOp_Char "newByteArray#" GenPrimOp
primop NewPinnedByteArrayOp_Char "newPinnedByteArray#" GenPrimOp
Int# -> State# s -> (# State# s, MutableByteArray# s #)
- {Like 'newByteArray#' but GC guarantees not to move it.}
+ {Like 'newByteArray#' but GC guarantees not to move it.
+
+ The alignment of the resulting byte array is 16 bytes as per 'newAlignedPinnedByteArray#'.
+ }
with out_of_line = True
effect = ReadWriteEffect
primop NewAlignedPinnedByteArrayOp_Char "newAlignedPinnedByteArray#" GenPrimOp
Int# -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
{Like 'newPinnedByteArray#' but allow specifying an arbitrary
- alignment, which must be a power of two.}
+ alignment, which must be a power of two. Alignment will be the maximum of the
+ system word size or given alignment.
+
+ The first parameter is the size of the new array and the second parameter is
+ the alignment.
+ }
with out_of_line = True
effect = ReadWriteEffect
can_fail_warning = YesWarnCanFail
@@ -2117,6 +2125,73 @@ primop ResizeMutableByteArrayOp_Char "resizeMutableByteArray#" GenPrimOp
with out_of_line = True
effect = ReadWriteEffect
+primop ResizeMutableByteArrayKeepPinnednessOp_Char "resizeMutableByteArrayKeepPinnedness#" GenPrimOp
+ MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+ {Like 'resizeMutableByteArray#', but ensures the pinnedness of the output array
+ is the same as the pinnedness of the input array.
+
+ If the input array is pinned, so is the output array, even if a new array
+ was allocated.
+ If the input array is unpinned, so is the output array, even if a new array
+ was allocated.
+
+ @since X.X}
+ with out_of_line = True
+ effect = ReadWriteEffect
+
+primop ResizeMutableByteArrayPreferUnpinnedOp_Char "resizeMutableByteArrayPreferUnpinned#" GenPrimOp
+ MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+ {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same
+ pinnedness as the original if it succeeds), but allocating an
+ unpinned 'MutableByteArray#' if that is not possible.
+
+ This is the default behaviour of 'resizeMutableByteArray#'.
+
+ @since X.X}
+ with out_of_line = True
+ effect = ReadWriteEffect
+
+primop ResizeMutableByteArrayPreferPinnedOp_Char "resizeMutableByteArrayPreferPinned#" GenPrimOp
+ MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+ {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same
+ pinnedness as the original if it succeeds), but allocating a
+ pinned 'MutableByteArray#' if that is not possible.
+
+ @since X.X}
+ with out_of_line = True
+ effect = ReadWriteEffect
+
+primop ResizeMutableByteArrayPreferAlignedPinnedOp_Char "resizeMutableByteArrayPreferAlignedPinned#" GenPrimOp
+ MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+ {Like 'resizeMutableByteArrayPreferPinned#', trying to resize in place (keeping the same
+ pinnedness as the original if it succeeds), but allocating an aligned,
+ pinned 'MutableByteArray#' if that is not possible.
+
+ The first 'Int#' parameter is the size of the new array, and the second 'Int#'
+ parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#'
+ for restrictions on alignment.
+
+ @since X.X}
+ with out_of_line = True
+ effect = ReadWriteEffect
+ can_fail_warning = YesWarnCanFail
+ -- can fail warning for the "power of two" requirement
+
+primop ResizeMutableByteArrayMakeAlignedPinnedOp_Char "resizeMutableByteArrayMakeAlignedPinned#" GenPrimOp
+ MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+ {Like 'resizeMutableByteArrayPreferAlignedPinned#', but if the input array is
+ not aligned and pinned to the given alignment, a new array is always allocated.
+
+ The first 'Int#' parameter is the size of the new array, and the second 'Int#'
+ parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#'
+ for restrictions on alignment.
+
+ @since X.X}
+ with out_of_line = True
+ effect = ReadWriteEffect
+ can_fail_warning = YesWarnCanFail
+ -- can fail warning for the "power of two" requirement
+
primop UnsafeFreezeByteArrayOp "unsafeFreezeByteArray#" GenPrimOp
MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
{Make a mutable byte array immutable, without copying.}
=====================================
compiler/GHC/StgToCmm/Prim.hs
=====================================
@@ -1767,6 +1767,11 @@ emitPrimOp cfg primop =
ByteArrayIsWeaklyPinnedOp -> alwaysExternal
ShrinkMutableByteArrayOp_Char -> alwaysExternal
ResizeMutableByteArrayOp_Char -> alwaysExternal
+ ResizeMutableByteArrayKeepPinnednessOp_Char -> alwaysExternal
+ ResizeMutableByteArrayPreferUnpinnedOp_Char -> alwaysExternal
+ ResizeMutableByteArrayPreferPinnedOp_Char -> alwaysExternal
+ ResizeMutableByteArrayPreferAlignedPinnedOp_Char -> alwaysExternal
+ ResizeMutableByteArrayMakeAlignedPinnedOp_Char -> alwaysExternal
ShrinkSmallMutableArrayOp_Char -> alwaysExternal
NewMutVarOp -> alwaysExternal
AtomicModifyMutVar2Op -> alwaysExternal
=====================================
compiler/GHC/StgToJS/Prim.hs
=====================================
@@ -680,6 +680,11 @@ genPrim prof bound ty op = case op of
MutableByteArrayContents_Char -> \[a,o] [b] -> pure $ PrimInline $ mconcat [a |= b, o |= zero_]
ShrinkMutableByteArrayOp_Char -> \[] [a,n] -> pure $ PrimInline $ appS hdShrinkMutableByteArrayStr [a,n]
ResizeMutableByteArrayOp_Char -> \[r] [a,n] -> pure $ PrimInline $ r |= app hdResizeMutableByteArrayStr [a,n]
+ ResizeMutableByteArrayKeepPinnednessOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
+ ResizeMutableByteArrayPreferUnpinnedOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
+ ResizeMutableByteArrayPreferPinnedOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
+ ResizeMutableByteArrayPreferAlignedPinnedOp_Char -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n]
+ ResizeMutableByteArrayMakeAlignedPinnedOp_Char -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n]
UnsafeFreezeByteArrayOp -> \[a] [b] -> pure $ PrimInline $ a |= b
UnsafeThawByteArrayOp -> \[a] [b] -> pure $ PrimInline $ a |= b
SizeofByteArrayOp -> \[r] [a] -> pure $ PrimInline $ r |= a .^ len
=====================================
libraries/base/src/GHC/Base.hs
=====================================
@@ -396,6 +396,12 @@ import GHC.Prim hiding
, sqrtDoubleX4#
, sqrtFloatX16#
, sqrtDoubleX8#
+ -- Don't export mutable byte array primops before stabilisation
+ , resizeMutableByteArrayKeepPinnedness#
+ , resizeMutableByteArrayPreferPinned#
+ , resizeMutableByteArrayPreferAlignedPinned#
+ , resizeMutableByteArrayMakeAlignedPinned#
+ , resizeMutableByteArrayPreferUnpinned#
#endif
#if __GLASGOW_HASKELL__ >= 1001
=====================================
libraries/base/src/GHC/Exts.hs
=====================================
@@ -364,6 +364,12 @@ import GHC.Prim hiding
, sqrtDoubleX4#
, sqrtFloatX16#
, sqrtDoubleX8#
+ -- Don't export mutable byte array primops before stabilisation
+ , resizeMutableByteArrayKeepPinnedness#
+ , resizeMutableByteArrayPreferPinned#
+ , resizeMutableByteArrayPreferAlignedPinned#
+ , resizeMutableByteArrayMakeAlignedPinned#
+ , resizeMutableByteArrayPreferUnpinned#
#endif
#if __GLASGOW_HASKELL__ >= 1001
=====================================
rts/PrimOps.cmm
=====================================
@@ -152,22 +152,18 @@ stg_newAlignedPinnedByteArrayzh ( W_ n, W_ alignment )
return (p);
}
+// See Note [Pinned ByteArray# variants]
stg_isByteArrayPinnedzh ( gcptr ba )
// ByteArray# s -> Int#
{
W_ bd, flags;
bd = Bdescr(ba);
- // Pinned byte arrays live in blocks with the BF_PINNED flag set.
- // We also consider BF_LARGE objects to be immovable. See #13894.
- // See the comment in Storage.c:allocatePinned.
- // We also consider BF_COMPACT objects to be immovable. See #14900.
flags = TO_W_(bdescr_flags(bd));
- // We used to also consider BF_LARGE pinned, but stopped doing so
- // because it interacted badly with compact regions. See #22255
return (flags & BF_PINNED != 0);
}
+// See Note [Pinned ByteArray# variants]
stg_isByteArrayWeaklyPinnedzh ( gcptr ba )
// ByteArray# s -> Int#
{
@@ -243,6 +239,35 @@ stg_isMutableByteArrayWeaklyPinnedzh ( gcptr mba )
* in ClosureMacros.h.
*/
+/* Note [Pinned ByteArray# variants]
+ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ * We distinguish different kinds of pinning for byte arrays.
+ *
+ * Pinned:
+ * * Always safe for ffi.
+ * * Never moves.
+ * * Can't be compacted.
+ *
+ * Weakly pinned:
+ * * Safe for FFI *unless* the byte array is moved into a compact region.
+ * * Can be compacted, which can invalidate Addr# references to the contents.
+ *
+ * Not pinned:
+ * * Never safe for FFI.
+ * * Can be compacted.
+ *
+ * Distinguishing three cases rather than just pinned/unpinned was needed to avoid
+ * edge cases with compact regions. See #22255, #14900 and #13894 for the motivation and more details.
+ *
+ * We determine the status of a byte array via the block it's allocated
+ * in. Checking the blocks BF_LARGE, BF_PINNED, BF_COMPACT flags.
+ *
+ * Those relate such that:
+ * * Weakly pinned <-> BF_PINNED | BF_COMPACT | BF_LARGE
+ * * Pinned <-> BF_PINNED
+ *
+*/
+
// shrink size of MutableByteArray in-place
stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size )
// MutableByteArray# s -> Int# -> State# s -> State# s
@@ -283,17 +308,63 @@ stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size )
return ();
}
+// allocate unpinned if resizing fails
+#define RMB_RESIZED_OR_UNPINNED 0
+// allocate pinned if resizing fails
+#define RMB_RESIZED_OR_PINNED 1
+// allocate same pinnedness if resizing fails
+#define RMB_RESIZED_OR_KEEP_PINNEDNESS 2
+// allocate aligned and pinned if resizing fails
+#define RMB_RESIZED_OR_ALIGNED_PINNED 3
+
// resize MutableByteArray
//
// The returned MutableByteArray is either the original
// MutableByteArray resized in-place or, if not possible, a newly
-// allocated (unpinned) MutableByteArray (with the original content
+// allocated MutableByteArray (with the original content
// copied over)
stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
-// MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+// resizeMutableByteArray#
+// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED);
+}
+stg_resizzeMutableByteArrayPreferUnpinnedzh ( gcptr mba, W_ new_size )
+// resizeMutableByteArrayPreferUnpinned#
+// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED);
+}
+stg_resizzeMutableByteArrayPreferPinnedzh ( gcptr mba, W_ new_size )
+// resizeMutableByteArrayPreferPinned#
+// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_PINNED);
+}
+stg_resizzeMutableByteArrayKeepPinnednesszh ( gcptr mba, W_ new_size )
+// resizeMutableByteArrayKeepPinnedness#
+// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_KEEP_PINNEDNESS);
+}
+stg_resizzeMutableByteArrayPreferAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment )
+// resizeMutableByteArrayPreferAlignedPinned#
+// :: MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED);
+}
+
+// base function to share implementation between the four resize operations:
+// resize or unpin (mode == RMB_RESIZED_OR_UNPINNED)
+// resize or pin (mode == RMB_RESIZED_OR_PINNED)
+// resize with pinnedness (mode == RMB_RESIZED_OR_KEEP_PINNEDNESS)
+// resize or pin with alignment (mode == RMB_RESIZED_OR_ALIGNED_PINNED)
+stg_resizzeMutableByteArrayChoosezh ( gcptr mba, W_ new_size, W_ alignment, W_ mode )
+// MutableByteArray# s -> Int# -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
{
W_ old_size, old_wds, new_wds, new_free;
W_ bd;
+ P_ new_mba;
ASSERT(new_size `ge` 0);
@@ -317,9 +388,45 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
StgArrBytes_bytes(mba) = new_size;
return (mba);
} else {
- (P_ new_mba) = call stg_newByteArrayzh(new_size);
+ // Resizing is not possible, create a new array object according to the mode.
+ switch [RMB_RESIZED_OR_UNPINNED .. RMB_RESIZED_OR_ALIGNED_PINNED] (mode) {
+ case RMB_RESIZED_OR_UNPINNED: {
+ (P_ new_mba_unpinned) = call stg_newByteArrayzh(new_size);
+ new_mba = new_mba_unpinned;
+ goto after;
+ }
+ case RMB_RESIZED_OR_PINNED: {
+ (P_ new_mba_pinned) = call stg_newPinnedByteArrayzh(new_size);
+ new_mba = new_mba_pinned;
+ goto after;
+ }
+ case RMB_RESIZED_OR_KEEP_PINNEDNESS: {
+ (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba);
+ if (is_pinned != 0) {
+ (P_ new_mba_pinned_two) = call stg_newPinnedByteArrayzh(new_size);
+ new_mba = new_mba_pinned_two;
+ } else {
+ (P_ new_mba_unpinned_two) = call stg_newByteArrayzh(new_size);
+ new_mba = new_mba_unpinned_two;
+ }
+ goto after;
+ }
+ case RMB_RESIZED_OR_ALIGNED_PINNED: {
+ (P_ new_mba_aligned_pinned) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment);
+ new_mba = new_mba_aligned_pinned;
+ goto after;
+ }
+ default: {
+ // shouldn't be possible; this function is only called with one of
+ // the four above enumerated values
+ ccall sbarf("stg_resizzeMutableByteArrayChoosezh: bad mode, something went wrong.") never returns;
+ }
+ }
+ after:
// copy over old content
+ // at this point we know that we are making the array bigger, so we copy
+ // over ALL of the old array
prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba),
StgArrBytes_bytes(mba), SIZEOF_W);
@@ -327,6 +434,34 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
}
}
+// resizeMutableByteArrayMakeAlignedPinned#
+stg_resizzeMutableByteArrayMakeAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment )
+// MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
+{
+ (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba);
+
+ // if the array is already pinned and aligned, we can call in and try and
+ // resize in place
+ if (is_pinned != 0 && ((BYTE_ARR_CTS(mba) % alignment) == 0)) {
+ jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED);
+ }
+
+ // else we just create an aligned array of the correct size
+ (P_ new_mba) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment);
+
+ // copy over old content
+ // take the minimum of the old size and the new size
+ W_ copy_size;
+ if (new_size < StgArrBytes_bytes(mba)) {
+ copy_size = new_size;
+ } else {
+ copy_size = StgArrBytes_bytes(mba);
+ }
+ prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba), copy_size, SIZEOF_W);
+
+ return (new_mba);
+}
+
// shrink size of SmallMutableArray in-place
stg_shrinkSmallMutableArrayzh ( gcptr mba, W_ new_size )
// SmallMutableArray# s -> Int# -> State# s -> State# s
=====================================
rts/RtsSymbols.c
=====================================
@@ -693,6 +693,12 @@ extern char **environ;
SymI_HasDataProto(stg_isMutableByteArrayWeaklyPinnedzh) \
SymI_HasDataProto(stg_shrinkMutableByteArrayzh) \
SymI_HasDataProto(stg_resizzeMutableByteArrayzh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayKeepPinnednesszh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayPreferUnpinnedzh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayPreferPinnedzh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayPreferAlignedPinnedzh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayChoosezh) \
+ SymI_HasDataProto(stg_resizzeMutableByteArrayMakeAlignedPinnedzh) \
SymI_HasDataProto(stg_shrinkSmallMutableArrayzh) \
SymI_HasProto(newSpark) \
SymI_HasProto(updateRemembSetPushThunk) \
=====================================
rts/include/stg/MiscClosures.h
=====================================
@@ -428,6 +428,12 @@ RTS_FUN_DECL(stg_isByteArrayWeaklyPinnedzh);
RTS_FUN_DECL(stg_isMutableByteArrayWeaklyPinnedzh);
RTS_FUN_DECL(stg_shrinkMutableByteArrayzh);
RTS_FUN_DECL(stg_resizzeMutableByteArrayzh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayKeepPinnednesszh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferUnpinnedzh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferPinnedzh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferAlignedPinnedzh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayChoosezh);
+RTS_FUN_DECL(stg_resizzeMutableByteArrayMakeAlignedPinnedzh);
RTS_FUN_DECL(stg_shrinkSmallMutableArrayzh);
RTS_FUN_DECL(stg_casIntArrayzh);
RTS_FUN_DECL(stg_casInt8Arrayzh);
=====================================
testsuite/tests/interface-stability/ghc-experimental-exports.stdout
=====================================
@@ -7326,6 +7326,11 @@ module GHC.PrimOps where
remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32#
remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64#
resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
resizeSmallMutableArray# :: forall s a. SmallMutableArray# s a -> Int# -> a -> State# s -> (# State# s, SmallMutableArray# s a #)
retry# :: forall {l :: Levity} (a :: TYPE (BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #)
rightSection :: forall {q :: RuntimeRep} {r :: RuntimeRep} {s :: RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c
=====================================
testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32
=====================================
@@ -7329,6 +7329,11 @@ module GHC.PrimOps where
remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32#
remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64#
resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
resizeSmallMutableArray# :: forall s a. SmallMutableArray# s a -> Int# -> a -> State# s -> (# State# s, SmallMutableArray# s a #)
retry# :: forall {l :: Levity} (a :: TYPE (BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #)
rightSection :: forall {q :: RuntimeRep} {r :: RuntimeRep} {s :: RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c
=====================================
testsuite/tests/interface-stability/ghc-prim-exports.stdout
=====================================
@@ -2578,6 +2578,11 @@ module GHC.Prim where
remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32#
remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64#
resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
retry# :: forall {l :: GHC.Internal.Types.Levity} (a :: TYPE (GHC.Internal.Types.BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #)
rightSection :: forall {q :: GHC.Internal.Types.RuntimeRep} {r :: GHC.Internal.Types.RuntimeRep} {s :: GHC.Internal.Types.RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c
seq :: forall {r :: GHC.Internal.Types.RuntimeRep} a (b :: TYPE r). a -> b -> b
@@ -3600,6 +3605,11 @@ module GHC.PrimopWrappers where
remWord64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64#
remWord8# :: GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8#
resizeMutableByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayKeepPinnedness# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferUnpinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
retry# :: forall a_levpoly. GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld, a_levpoly #)
setAddrRange# :: GHC.Internal.Prim.Addr# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld
setByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> GHC.Internal.Prim.State# s
=====================================
testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32
=====================================
@@ -2578,6 +2578,11 @@ module GHC.Prim where
remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32#
remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64#
resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
+ resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #)
retry# :: forall {l :: GHC.Internal.Types.Levity} (a :: TYPE (GHC.Internal.Types.BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #)
rightSection :: forall {q :: GHC.Internal.Types.RuntimeRep} {r :: GHC.Internal.Types.RuntimeRep} {s :: GHC.Internal.Types.RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c
seq :: forall {r :: GHC.Internal.Types.RuntimeRep} a (b :: TYPE r). a -> b -> b
@@ -3603,6 +3608,11 @@ module GHC.PrimopWrappers where
remWord64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64#
remWord8# :: GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8#
resizeMutableByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayKeepPinnedness# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayMakeAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
+ resizeMutableByteArrayPreferUnpinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #)
retry# :: forall a_levpoly. GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld, a_levpoly #)
setAddrRange# :: GHC.Internal.Prim.Addr# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld
setByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> GHC.Internal.Prim.State# s
=====================================
testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs
=====================================
@@ -0,0 +1,85 @@
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnboxedTuples #-}
+
+import GHC.Exts
+import GHC.Prim
+import GHC.IO
+
+import Data.List (intercalate)
+
+import System.Mem (performMinorGC)
+
+type Resizer s =
+ MutableByteArray# s -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
+
+{-# NOINLINE testResize #-}
+testResize ::
+ Int#
+ -> Resizer RealWorld
+ -> IO ()
+testResize initSize# resizer# =
+ testCaseUnpinned initSize# resizer#
+ >> testCasePinned initSize# resizer#
+ >> testCaseAlignedPinned initSize# resizer#
+ where
+ testCaseUnpinned initSize# =
+ testCase "unpinned" (newByteArray# initSize#)
+ testCasePinned initSize# =
+ testCase "pinned" (newPinnedByteArray# initSize#)
+ testCaseAlignedPinned initSize# =
+ testCase "aligned (8) & pinned" (newAlignedPinnedByteArray# initSize# 8#)
+
+{-# NOINLINE testCase #-}
+testCase ::
+ String
+ -> (State# RealWorld -> (# State# RealWorld, MutableByteArray# RealWorld #))
+ -> Resizer RealWorld
+ -> IO ()
+testCase pinnedMessage alloc# resizer# = do
+ performMinorGC
+ testWithAlloc >>= putStrLn
+ where
+ -- by allocating these all in one go hopefully we can force the resize
+ {-# NOINLINE testWithAlloc #-}
+ testWithAlloc :: IO String
+ testWithAlloc = IO $ \s0 -> case alloc# s0 of
+ (# s1, mba1 #) -> case alloc# s1 of
+ (# s2, mba2 #) -> case resizer# mba1 10# s2 of
+ (# s3, mba1' #) -> case resizer# mba2 10# s3 of
+ (# s4, mba2' #) ->
+ (# s4
+ , unlines
+ [ "starts " <> pinnedMessage <> ":"
+ , "first " <> checkArrayPair mba1 mba1'
+ , "second " <> checkArrayPair mba2 mba2'
+ ]
+ #)
+ checkArrayPair mba mba' =
+ "array ends " <> isPinned mba' <> ", is " <> isSame mba mba'
+ isPinned mba = if I# (isMutableByteArrayPinned# mba) == 1
+ then "pinned"
+ else "unpinned"
+ isSame mba mba' = if I# (sameMutableByteArray# mba mba') == 1
+ then "the same array"
+ else "a different array"
+
+main :: IO ()
+main = do
+ putStrLn "testing resizeMutableByteArray#"
+ -- prefer to go to unpinned
+ testResize 5# resizeMutableByteArray#
+ putStrLn "===========\ntesting resizeMutableByteArrayKeepPinnedness#"
+ -- keep pinnedness, both arrays stay the same
+ testResize 5# resizeMutableByteArrayKeepPinnedness#
+ putStrLn "===========\ntesting resizeMutableByteArrayPreferUnpinned#"
+ -- prefer unpinned arrays when creating new ones, this is the same as resizeMutableByteArray#
+ testResize 5# resizeMutableByteArrayPreferUnpinned#
+ putStrLn "===========\ntesting resizeMutableByteArrayPreferPinned#"
+ -- prefer pinned arrays when creating new ones
+ testResize 5# resizeMutableByteArrayPreferPinned#
+ putStrLn "===========\ntesting resizeMutableByteArrayPreferAlignedPinned#"
+ -- prefer pinned arrays when creating new ones.
+ testResize 5# (\mba size -> resizeMutableByteArrayPreferAlignedPinned# mba size 32#)
+ putStrLn "===========\ntesting resizeMutableByteArrayMakeAlignedPinned#"
+ -- guarantee pinned arrays.
+ testResize 5# (\mba size -> resizeMutableByteArrayMakeAlignedPinned# mba size 32#)
=====================================
testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
=====================================
@@ -0,0 +1,82 @@
+testing resizeMutableByteArray#
+starts unpinned:
+first array ends unpinned, is a different array
+second array ends unpinned, is a different array
+
+starts pinned:
+first array ends unpinned, is a different array
+second array ends pinned, is the same array
+
+starts aligned (8) & pinned:
+first array ends unpinned, is a different array
+second array ends pinned, is the same array
+
+===========
+testing resizeMutableByteArrayKeepPinnedness#
+starts unpinned:
+first array ends unpinned, is a different array
+second array ends unpinned, is a different array
+
+starts pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+starts aligned (8) & pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+===========
+testing resizeMutableByteArrayPreferUnpinned#
+starts unpinned:
+first array ends unpinned, is a different array
+second array ends unpinned, is a different array
+
+starts pinned:
+first array ends unpinned, is a different array
+second array ends pinned, is the same array
+
+starts aligned (8) & pinned:
+first array ends unpinned, is a different array
+second array ends pinned, is the same array
+
+===========
+testing resizeMutableByteArrayPreferPinned#
+starts unpinned:
+first array ends pinned, is a different array
+second array ends unpinned, is the same array
+
+starts pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+starts aligned (8) & pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+===========
+testing resizeMutableByteArrayPreferAlignedPinned#
+starts unpinned:
+first array ends pinned, is a different array
+second array ends unpinned, is the same array
+
+starts pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+starts aligned (8) & pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+===========
+testing resizeMutableByteArrayMakeAlignedPinned#
+starts unpinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+starts pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
+
+starts aligned (8) & pinned:
+first array ends pinned, is a different array
+second array ends pinned, is a different array
=====================================
testsuite/tests/primops/should_run/all.T
=====================================
@@ -34,6 +34,9 @@ test('CmpWord32', normal, compile_and_run, [''])
test('ShrinkSmallMutableArrayA', normal, compile_and_run, [''])
test('ShrinkSmallMutableArrayB', normal, compile_and_run, [''])
+test('ResizeByteArrayPinnedness',
+ js_skip, # js does not implement performMinorGc
+ compile_and_run, [''])
test('T14664', normal, compile_and_run, [''])
test('CStringLength', normal, compile_and_run, ['-O2'])
test('NonNativeSwitch', normal, compile_and_run, ['-O2'])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/13dff5d0629a1da60b4c8b0fc36ae2d…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/13dff5d0629a1da60b4c8b0fc36ae2d…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc][wip/dcoutts/signals-cleanup] 4 commits: Push installUserSignalHandler down into individual I/O managers
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:
84627130 by Duncan Coutts at 2026-10-22T21:42:28+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.
- - - - -
a03ec47f by Duncan Coutts at 2026-10-22T21:42:28+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).
- - - - -
b1f2d9ed by Duncan Coutts at 2026-10-22T21:42:28+01:00
Introduce an 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.
- - - - -
f809312b by Duncan Coutts at 2026-10-22T21:42:37+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.
- - - - -
13 changed files:
- rts/IOManager.c
- rts/IOManagerInternals.h
- rts/configure.ac
- 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
Changes:
=====================================
rts/IOManager.c
=====================================
@@ -356,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;
}
@@ -372,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:
@@ -400,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:
@@ -432,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);
+ }
}
@@ -653,6 +671,41 @@ setIOManagerControlFd(uint32_t cap_no, int fd) {
#if !defined(mingw32_HOST_OS)
+/* 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_POSIX)
+ case IO_MANAGER_MIO_POSIX:
+ return installUserSignalHandlerMIO(sig, spi, mask, nocldstop);
+#endif
+ default:
+ barf("installUserSignalHandler not implemented");
+ }
+}
+
+
/* 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.
@@ -665,20 +718,14 @@ 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)
{
- debugTrace(DEBUG_iomanager, "Installing a signal handler for sig %d", sig);
-
/* Grab the current value of the nocldstop argument. Yes this is madness. */
- bool nocldstop_arg = nocldstop;
-
-#if defined(RTS_USER_SIGNALS)
- return installUserSignalHandler(sig, spi, mask, nocldstop_arg);
-
-#else /* !RTS_USER_SIGNALS */
- return STG_SIG_DFL;
-#endif
+ return installUserSignalHandler(sig, spi, mask, nocldstop);
}
#endif
=====================================
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/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/posix/IOManSignals.c
=====================================
@@ -22,6 +22,7 @@
#include <unistd.h>
#include <signal.h>
+#include <fcntl.h>
#include <string.h>
/* -----------------------------------------------------------------------------
@@ -111,16 +112,12 @@ static inline bool signals_pending(void) {
* started up at the next context switch.
* -------------------------------------------------------------------------- */
-static void
+#if !defined(THREADED_RTS)
+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.
@@ -162,8 +159,8 @@ generic_handler(int sig USED_IF_THREADS,
interruptCapability(&MainCapability);
-#endif /* THREADED_RTS */
}
+#endif /* THREADED_RTS */
/* -----------------------------------------------------------------------------
* Blocking/Unblocking of the user signals
@@ -190,7 +187,8 @@ unblockUserSignals(void)
* -------------------------------------------------------------------------- */
int
-installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
+installSignalHandler(int sig, int spi, sigset_t *mask, bool nocldstop,
+ SignalHandler signal_handler)
{
ACQUIRE_LOCK(&sig_mutex);
@@ -226,7 +224,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
action.sa_flags |= SA_RESETHAND;
/* fall through */
case STG_SIG_HAN:
- action.sa_sigaction = generic_handler;
+ action.sa_sigaction = signal_handler;
action.sa_flags |= SA_SIGINFO;
break;
@@ -235,7 +233,7 @@ installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop)
}
if (mask != NULL) {
- action.sa_mask = *(sigset_t *)mask;
+ action.sa_mask = *mask;
} else {
sigemptyset(&action.sa_mask);
}
@@ -289,12 +287,27 @@ startPendingSignalHandlers(Capability *cap)
continue; // handler has been changed.
}
- info = stgMallocBytes(sizeof(siginfo_t), "startPendingSignalHandlers");
- // freed by runHandler
- memcpy(info, next_pending_handler, sizeof(siginfo_t));
+ 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->si_signo);
+ "Handling pending signal, no %d", info_copy->si_signo);
StgTSO *t =
createIOThread(cap,
@@ -302,14 +315,75 @@ startPendingSignalHandlers(Capability *cap)
rts_apply(cap,
rts_apply(cap,
runHandlersPtr_closure,
- rts_mkPtr(cap, info)),
- rts_mkInt(cap, info->si_signo)));
+ rts_mkPtr(cap, info_copy)),
+ rts_mkInt(cap, info_copy->si_signo)));
scheduleThread(cap, t);
setThreadLabel(cap, t, "signal handler thread");
- }
-
- unblockUserSignals();
}
+
+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
=====================================
@@ -8,6 +8,10 @@
#pragma once
+#if defined(RTS_USER_SIGNALS)
+
+#include <signal.h>
+
#include "BeginPrivate.h"
/*
@@ -31,7 +35,24 @@ void finiUserSignals(void);
void startPendingSignalHandlers(Capability *cap);
#endif
-int installUserSignalHandler(int sig, int spi, void *mask, bool nocldstop);
+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,16 +13,19 @@
#include "rts/PosixSource.h"
#include "Rts.h"
-#include "Schedule.h"
-#include "RtsUtils.h"
-#include "Prelude.h"
-#include "ThreadLabels.h"
+#include "IOManager.h"
+
+#if defined(IOMGR_ENABLED_MIO_POSIX)
#include "MIO.h"
-#include "IOManager.h"
#include "IOManagerInternals.h"
#include "IOManSignals.h"
+#include "Schedule.h"
+#include "RtsUtils.h"
+#include "Prelude.h"
+#include "ThreadLabels.h"
+
#if defined(HAVE_ERRNO_H)
# include <errno.h>
#endif
@@ -30,64 +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, sigset_t *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);
}
}
@@ -115,13 +119,21 @@ 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 STG_UNUSED) { /* 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, sigset_t *mask,
+ bool nocldstop);
#endif
#include "EndPrivate.h"
=====================================
rts/posix/Poll.c
=====================================
@@ -585,6 +585,13 @@ void interruptIOManagerPoll(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+int installUserSignalHandlerPoll(int sig, int spi, sigset_t *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, sigset_t *mask, bool nocldstop);
+
#endif /* IOMGR_ENABLED_POLL */
#include "EndPrivate.h"
=====================================
rts/posix/Select.c
=====================================
@@ -524,4 +524,10 @@ awaitCompletedTimeoutsOrIOSelect(CapIOManager *iomgr, bool wait)
return !interrupt;
}
+int installUserSignalHandlerSelect(int sig, int spi, sigset_t *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, sigset_t *mask,
+ bool nocldstop);
#include "EndPrivate.h"
=====================================
rts/posix/SelectBis.c
=====================================
@@ -595,6 +595,13 @@ void interruptIOManagerSelectBis(CapIOManager *iomgr USED_IF_PREEMPTION)
}
+int installUserSignalHandlerSelectBis(int sig, int spi, sigset_t *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, sigset_t *mask,
+ bool nocldstop);
+
#endif /* IOMGR_ENABLED_SELECTBIS */
#include "EndPrivate.h"
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/544e5e6b234a359025121f804c9dad…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/544e5e6b234a359025121f804c9dad…
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/int-index/curried-ctxts] Curried contexts in prefix GADT constructors (#27424)
by Vladislav Zavialov (@int-index) 22 Oct '26
by Vladislav Zavialov (@int-index) 22 Oct '26
22 Oct '26
Vladislav Zavialov pushed to branch wip/int-index/curried-ctxts at Glasgow Haskell Compiler / GHC
Commits:
cf5bb0b6 by Vladislav Zavialov at 2026-10-22T22:33:44+02:00
Curried contexts in prefix GADT constructors (#27424)
Prefix GADT constructor declarations now accept multiple consecutive
contexts, equivalent to a single conjoined context:
data G a where
MkCurried :: C a => (D a, E a) => G a
MkConjoined :: (C a, D a, E a) => G a
This is a step towards GHC Proposal #402 "Stable GADT constructor syntax".
To represent multiple contexts, the `con_mb_cxt` field of `ConDeclGADT` has
been replaced by `con_cxts`, which stores a list instead of a Maybe:
- con_mb_cxt :: Maybe (LHsContext pass)
+ con_cxts :: [LHsContext pass]
The rest of this patch follows from this change.
Note that in ConDeclH98, the optional context is still stored as a Maybe.
To accommodate this difference, a few helper functions were generalised over
a Foldable container `t`:
hsConDeclTheta :: Foldable t => t (LHsContext ...) -> [LHsType ...]
pprLHsContext :: (Foldable t, ...) => t (LHsContext ...) -> SDoc
repContexts :: Foldable t => t (LHsContext GhcRn) -> MetaM (Core (M TH.Cxt))
tcHsContext :: Foldable t => t (LHsContext GhcRn) -> TcM [PredType]
In practice, the instantiations of `t` are either `Maybe` or `[]`.
Record-style GADT constructors continue to accept at most one context.
`splitLHsGadtTy` accounts for this by splitting off only the first context
if it detects record syntax, leaving the offending part of the type in the
body for the renamer to reject later on.
Test cases: T12087a T27424a T27424b T27424c T27424d GadtConSigs_th_dump1
- - - - -
42 changed files:
- changelog.d/allow-gadt-prefix-con-parens
- + changelog.d/gadt-curried-contexts-27424
- compiler/GHC/Hs/Decls.hs
- compiler/GHC/Hs/Type.hs
- compiler/GHC/HsToCore/Quote.hs
- compiler/GHC/Iface/Ext/Ast.hs
- compiler/GHC/Parser/PostProcess.hs
- compiler/GHC/Parser/PostProcess/Haddock.hs
- compiler/GHC/Rename/Module.hs
- compiler/GHC/Tc/Gen/HsType.hs
- compiler/GHC/Tc/TyCl.hs
- compiler/GHC/ThToHs.hs
- compiler/Language/Haskell/Syntax/Decls.hs
- compiler/Language/Haskell/Syntax/Type.hs
- docs/users_guide/exts/gadt_syntax.rst
- − testsuite/tests/gadt/T12087.stderr
- + testsuite/tests/gadt/T12087a.hs
- testsuite/tests/gadt/T12087.hs → testsuite/tests/gadt/T12087b.hs
- + testsuite/tests/gadt/T12087b.stderr
- + testsuite/tests/gadt/T27424a.hs
- + testsuite/tests/gadt/T27424b.hs
- + testsuite/tests/gadt/T27424b.stderr
- testsuite/tests/gadt/all.T
- testsuite/tests/haddock/should_compile_flag_haddock/T17544.stderr
- testsuite/tests/haddock/should_compile_flag_haddock/T17544_kw.stderr
- testsuite/tests/parser/should_compile/DumpParsedAst.stderr
- testsuite/tests/parser/should_compile/DumpRenamedAst.stderr
- testsuite/tests/parser/should_compile/T15323.stderr
- testsuite/tests/printer/Makefile
- testsuite/tests/printer/T18791.stderr
- + testsuite/tests/printer/T27424c.hs
- testsuite/tests/printer/all.T
- testsuite/tests/th/GadtConSigs_th_dump1.hs
- testsuite/tests/th/GadtConSigs_th_dump1.stderr
- + testsuite/tests/th/T27424d.hs
- + testsuite/tests/th/T27424d.stderr
- testsuite/tests/th/all.T
- utils/check-exact/ExactPrint.hs
- utils/haddock/haddock-api/src/Haddock/Backends/Hoogle.hs
- utils/haddock/haddock-api/src/Haddock/Convert.hs
- utils/haddock/haddock-api/src/Haddock/GhcUtils.hs
- utils/haddock/haddock-api/src/Haddock/Interface/Rename.hs
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cf5bb0b6620649cf6d2709f4a14c807…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cf5bb0b6620649cf6d2709f4a14c807…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
[Git][ghc/ghc] Pushed new branch wip/jeltsch/unit-related-doc-fixes
by Wolfgang Jeltsch (@jeltsch) 22 Oct '26
by Wolfgang Jeltsch (@jeltsch) 22 Oct '26
22 Oct '26
Wolfgang Jeltsch pushed new branch wip/jeltsch/unit-related-doc-fixes at Glasgow Haskell Compiler / GHC
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/tree/wip/jeltsch/unit-related-doc-fixes
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/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:
359e6448 by Wolfgang Jeltsch at 2026-10-22T20:57:01+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.
Metric Decrease:
T15164
T19695
T21839c
WWRec
- - - - -
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/359e64480aff450121da1a75d7167c9…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/359e64480aff450121da1a75d7167c9…
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