[Git][ghc/ghc][master] JS: remove archive padding after wasm objects (#25665)
by Marge Bot (@marge-bot) 22 Oct '26
by Marge Bot (@marge-bot) 22 Oct '26
22 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
593f5f5b by Sylvain Henry at 2026-10-22T04:51:16-04:00
JS: remove archive padding after wasm objects (#25665)
On Darwin hosts, emar (llvm-ar) writes archives in the Darwin format when
the first member is not a known object file, which is the case for the
archives of the JS backend. This format pads each member to 8 bytes with
'\n' and includes the padding in the member size. Wasm objects do not
accept trailing bytes: wasm-ld failed with "section too large".
The JS linker now removes this padding when it extracts a wasm object from
an archive. In addition, Hadrian passes --format=gnu to emar for the JS
target. See Note [Wasm objects in Darwin archives].
Also document the format of Cc (wasm) objects in Note [Cc object format].
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
12 changed files:
- + changelog.d/25665
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- hadrian/src/Settings/Builders/Ar.hs
- testsuite/tests/javascript/Makefile
- + testsuite/tests/javascript/T25665.hs
- + testsuite/tests/javascript/T25665.pkg
- + testsuite/tests/javascript/T25665.stdout
- + testsuite/tests/javascript/T25665Lib.hs
- + testsuite/tests/javascript/T25665_c.c
- + testsuite/tests/javascript/T25665_js.js
- testsuite/tests/javascript/all.T
Changes:
=====================================
changelog.d/25665
=====================================
@@ -0,0 +1,6 @@
+section: js-backend
+synopsis: Fix linking of C sources in packages on Darwin (``wasm-ld: section too large``).
+ The padding added by ``llvm-ar`` after wasm objects in Darwin format archives is now removed.
+ Hadrian now also builds the archives of the JS boot libraries in the GNU format.
+mrs: !16809
+issues: #25665
=====================================
compiler/GHC/StgToJS/Linker/Linker.hs
=====================================
@@ -269,9 +269,10 @@ jsLink lc_cfg cfg logger tmpfs ar_cache out link_plan = do
go_entries emcc_opts cc_objs es
Just ObjCc -> do
-- extract the object file from the archive in a temporary
- -- file and return its path
+ -- file and return its path.
+ -- See Note [Wasm objects in Darwin archives] in GHC.StgToJS.Object
cc_obj_fn <- newTempName logger tmpfs tmp_dir TFL_CurrentModule "o"
- B.writeFile cc_obj_fn (Ar.filedata e)
+ B.writeFile cc_obj_fn (trimWasmObjectBS (Ar.filedata e))
let cc_objs' = cc_obj_fn:cc_objs
go_entries emcc_opts cc_objs' es
Just ObjJs -> do
=====================================
compiler/GHC/StgToJS/Object.hs
=====================================
@@ -26,6 +26,8 @@ module GHC.StgToJS.Object
( ObjectKind(..)
, getObjectKind
, getObjectKindBS
+ -- * Cc object
+ , trimWasmObjectBS
-- * JS object
, JSOptions(..)
, defaultJSOptions
@@ -109,7 +111,8 @@ import qualified Control.Exception as Exception
data ObjectKind
= ObjJs -- ^ JavaScript source embedded in a .o
| ObjHs -- ^ JS backend object for Haskell code
- | ObjCc -- ^ Wasm module object as produced by emcc
+ | ObjCc -- ^ Wasm module object as produced by emcc.
+ -- See Note [Cc object format]
deriving (Show,Eq,Ord)
-- | Get the kind of a file object, if any
@@ -141,6 +144,120 @@ hsHeader = unsafePerformIO $ B.unsafePackAddressLen 8 "GHCJS_HS"#
wasmHeader :: B.ByteString
wasmHeader = unsafePerformIO $ B.unsafePackAddressLen 4 "\0asm"#
+-- Note [Cc object format]
+-- ~~~~~~~~~~~~~~~~~~~~~~~
+-- Cc objects are wasm object files produced by emcc (clang). A wasm object
+-- file is a wasm module binary with additional custom sections ("linking",
+-- "reloc.*") for the linker. Layout:
+--
+-- magic : 4 bytes "\0asm"
+-- version : 4 bytes, u32 little-endian (1)
+-- sections : until the end of the file. Each section is:
+-- id : 1 byte
+-- size : u32 as unsigned LEB128 (1 to 5 bytes): size of the contents
+-- contents : `size` bytes
+--
+-- There is no section count and no total size: a reader reads sections until
+-- the end of the file. Thus a reader reads trailing bytes as a section (see
+-- Note [Wasm objects in Darwin archives]).
+--
+-- Unsigned LEB128: each byte gives 7 bits of the value, least significant
+-- group first. Bit 7 is set in all the bytes except the last one. E.g. 300
+-- (0b10_0101100) is encoded as [0xac, 0x02].
+--
+-- We only use the magic (to detect Cc objects, see getObjectKindBS) and the
+-- section headers (to find the end of the last section, see
+-- trimWasmObjectBS). We do not read the contents of the sections.
+--
+-- References:
+-- - WebAssembly spec, binary format of modules and sections:
+-- https://webassembly.github.io/spec/core/binary/modules.html
+-- - WebAssembly spec, LEB128 integers:
+-- https://webassembly.github.io/spec/core/binary/values.html
+-- - Wasm object files (tool conventions, not in the spec):
+-- https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md
+-- - LLVM implementation (used by wasm-ld): readSection in
+-- llvm/lib/Object/WasmObjectFile.cpp
+
+-- | Remove the archive padding at the end of a wasm object, if any.
+--
+-- See Note [Wasm objects in Darwin archives] and Note [Cc object format]
+trimWasmObjectBS :: B.ByteString -> B.ByteString
+trimWasmObjectBS bs = go 8 -- skip magic and version
+ where
+ len = B.length bs
+
+ go !off
+ | off >= len
+ = bs
+ | Just sz <- full_section_size off
+ , off + sz <= len
+ = go (off + sz)
+ | is_padding (B.drop off bs)
+ = B.take off bs
+ | otherwise
+ = bs -- not padding: keep the object and let wasm-ld report the error
+
+ is_padding rest = B.length rest < 8 && B.all (== 0x0a) rest
+
+ -- full size of the section at the given offset: id, payload size, payload
+ full_section_size off = do
+ (payload_sz, n) <- get_uleb128 (off + 1) 0 0
+ pure (1 + n + payload_sz)
+
+ -- read a LEB128 u32 (5 bytes max). Return the value and its size in bytes.
+ get_uleb128 :: Int -> Int -> Int -> Maybe (Int, Int)
+ get_uleb128 start i acc
+ | i >= 5 || start + i >= len
+ = Nothing
+ | otherwise
+ = let b = B.index bs (start + i)
+ !acc' = acc .|. (fromIntegral (b .&. 0x7f) `shiftL` (7 * i))
+ in if testBit b 7
+ then get_uleb128 start (i + 1) acc'
+ else Just (acc', i + 1)
+
+-- Note [Wasm objects in Darwin archives]
+-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+-- llvm-ar (used as emar) supports several archive formats: gnu, bsd, darwin,
+-- etc. (see the --format option). On Darwin hosts, it uses the Darwin format
+-- by default when the first member is not a known object file, which is the
+-- case for the JS and HS objects of the JS backend.
+--
+-- The Darwin format is a variant of the BSD format: member names are stored
+-- in the member data (#1/N names) and the data of each member is aligned to 8
+-- bytes with '\n' (0x0a) bytes. This alignment is what ld64 expects (see
+-- computeMemberData in llvm/lib/Object/ArchiveWriter.cpp). The size in the
+-- member header includes the name and the padding. The archive parser
+-- (GHC.SysTools.Ar) removes the name but not the padding: it returns the
+-- padding as part of the member data. The JS linker removes it with
+-- trimWasmObjectBS (see below).
+--
+-- Only the Darwin format has this padding: the gnu and bsd formats do not.
+-- "llvm-ar --format=darwin" reproduces the issue on any host.
+--
+-- JS and HS objects accept trailing bytes. A wasm object does not: it is a
+-- sequence of sections until the end of the file.
+-- See Note [Cc object format]. wasm-ld reads "\n\n" as a section with id 10
+-- and payload size 10 and fails with "section too large" (#25665).
+--
+-- Thus, before we give a wasm object from an archive to emcc, the JS linker
+-- calls trimWasmObjectBS: we read the sections of the object and we remove the
+-- trailing bytes if they are padding (less than 8 '\n' bytes).
+-- Padding never contains a full section: a section that starts
+-- with 0x0a has at least 12 bytes. If the trailing bytes are not padding, we
+-- keep the object unchanged and wasm-ld reports the error.
+--
+-- In addition, Hadrian passes --format=gnu to emar, thus the archives of the
+-- boot libraries have no padding. Cabal does not do this, thus we still need
+-- the code above for other libraries and for older installations.
+--
+-- References:
+-- - llvm-ar formats: https://llvm.org/docs/CommandGuide/llvm-ar.html
+-- - Darwin padding and format detection: computeMemberData and
+-- NewArchiveMember::detectKindFromObject in
+-- llvm/lib/Object/ArchiveWriter.cpp
+
------------------------------------------------
=====================================
hadrian/src/Settings/Builders/Ar.hs
=====================================
@@ -10,7 +10,11 @@ arBuilderArgs = do
stage <- getStage
mconcat
[ builder (Ar Pack) ? mconcat
- [ -- When building on platforms which don't support object merging
+ [ -- JS target: emar (llvm-ar) uses the Darwin archive format on Darwin
+ -- hosts. Force the GNU format to avoid padding after wasm objects.
+ -- See Note [Wasm objects in Darwin archives] in GHC.StgToJS.Object
+ expr (isJsTarget stage) ? arg "--format=gnu"
+ , -- When building on platforms which don't support object merging
-- we must use the -L flag supported by llvm-ar, which ensures that
-- .a files added to the archive are merged into the resulting archive,
-- not added as a single file. This requires that we are using llvm-ar
=====================================
testsuite/tests/javascript/Makefile
=====================================
@@ -31,3 +31,16 @@ T24744:
# check that the optimization occurred
grep -c "h\$$r1 = h\$$decodeUtf8z" T24744.dump-js
grep -c "h\$$stc(h\$$mainZCMainzitestzuvalzu80zulocal,h\$$mainZCMainzitestzuvalzu80zulocal_e,\[h\$$ghczminternalZCGHCziInternalziTypesziunpackCStringzh,h\$$mainZCMainzimain7_1,h\$$mainZCMainzimain7_2\])" T24744.jsexe/out.js
+
+T25665:
+ mkdir T25665pkg
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 -this-unit-id t25665-lib -odir T25665pkg -hidir T25665pkg -c T25665Lib.hs
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 -c T25665_js.js -o T25665pkg/T25665_js.o
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 -c T25665_c.c -o T25665_c.o
+ # add archive padding as llvm-ar does in the Darwin format (--format=darwin)
+ (cat T25665_c.o; printf '\n\n\n') > T25665pkg/T25665_c.o
+ '$(AR)' qcS T25665pkg/libHSt25665-lib.a T25665pkg/T25665Lib.o T25665pkg/T25665_js.o T25665pkg/T25665_c.o
+ '$(GHC_PKG)' init T25665.package.conf
+ '$(GHC_PKG)' --no-user-package-db -f T25665.package.conf register --force -v0 T25665.pkg
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 -package-db T25665.package.conf -package t25665-lib T25665.hs
+ ./T25665
=====================================
testsuite/tests/javascript/T25665.hs
=====================================
@@ -0,0 +1,11 @@
+-- #25665: link a package whose archive contains a wasm object (built from a C
+-- source) followed by archive padding. In the Darwin format (the default of
+-- llvm-ar on Darwin hosts), llvm-ar adds '\n' bytes after each member and
+-- includes them in the member size. The Makefile adds this padding by hand,
+-- thus the test does not depend on llvm-ar.
+--
+-- See Note [Wasm objects in Darwin archives] in GHC.StgToJS.Object
+import T25665Lib
+
+main :: IO ()
+main = print =<< t25665 41
=====================================
testsuite/tests/javascript/T25665.pkg
=====================================
@@ -0,0 +1,9 @@
+name: t25665-lib
+version: 1.0
+id: t25665-lib
+key: t25665-lib
+exposed: True
+exposed-modules: T25665Lib
+import-dirs: ${pkgroot}/T25665pkg
+library-dirs: ${pkgroot}/T25665pkg
+hs-libraries: HSt25665-lib
=====================================
testsuite/tests/javascript/T25665.stdout
=====================================
@@ -0,0 +1 @@
+42
=====================================
testsuite/tests/javascript/T25665Lib.hs
=====================================
@@ -0,0 +1,3 @@
+module T25665Lib (t25665) where
+
+foreign import javascript "t25665_wrapper" t25665 :: Int -> IO Int
=====================================
testsuite/tests/javascript/T25665_c.c
=====================================
@@ -0,0 +1,3 @@
+int t25665_c(int a) {
+ return a+1;
+}
=====================================
testsuite/tests/javascript/T25665_js.js
=====================================
@@ -0,0 +1,6 @@
+//#OPTIONS:CPP
+//#OPTIONS:EMCC:EXPORTED_FUNCTIONS=_t25665_c
+
+function t25665_wrapper(a) {
+ return _t25665_c(a);
+}
=====================================
testsuite/tests/javascript/all.T
=====================================
@@ -30,3 +30,4 @@ test('T25633', normal, compile_and_run, [''])
test('T24886', normal, compile, [''])
test('T27788', normal, compile_and_run, ['-O0'])
+test('T25665', extra_files(['T25665Lib.hs','T25665_js.js','T25665_c.c','T25665.pkg']), makefile_test, ['T25665'])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/593f5f5bbe0ab73bbb1cd86e1f8be8f…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/593f5f5bbe0ab73bbb1cd86e1f8be8f…
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] 2 commits: Add GHC.Internal.Unboxed.Do: do-notation for unboxed values (#27944)
by Marge Bot (@marge-bot) 22 Oct '26
by Marge Bot (@marge-bot) 22 Oct '26
22 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
71808e29 by Sylvain Henry at 2026-10-22T04:50:24-04:00
Add GHC.Internal.Unboxed.Do: do-notation for unboxed values (#27944)
QualifiedDo operators can be representation-polymorphic: a do-block is
expanded into ordinary applications before typechecking, and the only
check on a bound variable is the deferred fixed-runtime-rep check. A
representation-polymorphic (>>=) cannot be defined directly, though,
because its body would bind a variable of unknown representation.
The new module provides such operators for State#-passing code as
methods of a class indexed by RuntimeRep, with one instance per
representation. With optimisation the dictionaries disappear and a
block compiles to the usual nested case expressions:
Ubx.do { x <- readWordArray# mba 0# -- x :: Word#
; writeWordArray# mba 1# x -- unit actions are statements
; Ubx.pure x }
Also add users guide documentation and tests.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
33247780 by Sylvain Henry at 2026-10-22T04:50:24-04:00
Bignum: use Ubx.do for State#-passing code (#27944)
Rewrite the State#-passing code of the GHC.Internal.Bignum modules
(Primitives, WordArray, BigNat, Natural, Integer, Backend.Native,
Backend.GMP) with the qualified do-notation of GHC.Internal.Unboxed.Do.
Nested case chains such as
case newWordArray# sz s of { (# s, mwa #) ->
case act mwa s of { s ->
unsafeFreezeByteArray# mwa s }}
become
Ubx.do
mwa <- newWordArray# sz
act mwa
unsafeFreezeByteArray# mwa
The optimised Core is unchanged (compared per top-level binding).
BigNat now enables MonoLocalBinds: local loops written with Ubx.do
must stay monomorphic in the state type, otherwise GHC does not turn
them into join points.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
21 changed files:
- + changelog.d/unboxed-do
- docs/users_guide/exts/qualified_do.rst
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/GMP.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/Native.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/BigNat.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Integer.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Natural.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Primitives.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/WordArray.hs
- + libraries/ghc-internal/src/GHC/Internal/Unboxed/Do.hs
- testsuite/tests/qualifieddo/should_compile/all.T
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.hs
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.stderr
- testsuite/tests/qualifieddo/should_fail/all.T
- + testsuite/tests/qualifieddo/should_fail/qdofail006.hs
- + testsuite/tests/qualifieddo/should_fail/qdofail006.stderr
- testsuite/tests/qualifieddo/should_run/all.T
- + testsuite/tests/qualifieddo/should_run/qdorun008.hs
- + testsuite/tests/qualifieddo/should_run/qdorun008.stdout
Changes:
=====================================
changelog.d/unboxed-do
=====================================
@@ -0,0 +1,11 @@
+section: ghc-internal
+issues: #27944
+mrs: !16808
+synopsis:
+ Add ``GHC.Internal.Unboxed.Do`` for do-notation over unboxed values
+description:
+ The new module ``GHC.Internal.Unboxed.Do`` provides ``QualifiedDo``
+ operations for ``State#``-passing code. Bound variables can have any
+ representation (``Word#``, ``MutableByteArray# s``, ``(# Word#, Word# #)``,
+ ...) and unit actions of type ``State# s -> State# s`` can be used as
+ statements. The ``GHC.Internal.Bignum`` modules now use it.
=====================================
docs/users_guide/exts/qualified_do.rst
=====================================
@@ -118,6 +118,17 @@ If no qualifier is specified with ``-XQualifiedDo`` enabled, it defaults to the
Note that the operations to be qualified must be in scope for QualifiedDo to work. I.e. ``import MAC (label)`` in the
example above would result in an error, since ``MAC.>>=`` and ``MAC.>>`` would not be in scope.
+.. _qualified-do-rep-poly:
+
+Representation-polymorphic operations
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The operations can be representation-polymorphic, so that the variables bound
+by ``pat <- u`` can have unboxed types such as ``Word#``. The representation of
+each bound variable must be fixed by the type of ``u``
+(see :ref:`representation-polymorphism-restrictions`). This does not work with
+:extension:`ApplicativeDo`.
+
Examples
~~~~~~~~
=====================================
libraries/ghc-internal/CHANGELOG.md
=====================================
@@ -4,6 +4,7 @@
* Introduce `dataToCodeQ` and `liftDataTyped`, typed variants of `dataToExpQ` and `liftData` respectively.
* Add new `gc_sync_elapsed_ns` counter to GHC.Internal.Stats
+* Add `GHC.Internal.Unboxed.Do`: `QualifiedDo` operations for `State#`-passing code that binds unboxed values.
## 9.1001.0 -- 2024-05-01
=====================================
libraries/ghc-internal/ghc-internal.cabal.in
=====================================
@@ -94,6 +94,7 @@ Library
OverloadedStrings
ParallelListComp
PolyKinds
+ QualifiedDo
RankNTypes
RecordWildCards
RoleAnnotations
@@ -328,6 +329,7 @@ Library
GHC.Internal.Text.Show
GHC.Internal.Type.Reflection
GHC.Internal.Type.Reflection.Unsafe
+ GHC.Internal.Unboxed.Do
GHC.Internal.Unsafe.Coerce
GHC.Internal.CString
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/GMP.hs
=====================================
@@ -9,6 +9,7 @@
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE QualifiedDo #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
@@ -26,6 +27,7 @@ import GHC.Internal.Bignum.Primitives
import GHC.Internal.Prim
import GHC.Internal.Types
import GHC.Internal.Magic (runRW#)
+import qualified GHC.Internal.Unboxed.Do as Ubx
import {-# SOURCE #-} GHC.Internal.Bignum.Integer
import {-# SOURCE #-} GHC.Internal.Bignum.BigNat
import {-# SOURCE #-} GHC.Internal.Bignum.Natural
@@ -75,15 +77,17 @@ bignat_add
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_add #-}
-bignat_add mwa wa wb s
+bignat_add mwa wa wb
-- weird GMP requirement: the biggest comes first
| isTrue# (wordArraySize# wb ># wordArraySize# wa)
- = case ioWord# (c_mpn_add mwa wb (wordArraySize# wb) wa (wordArraySize# wa)) s of
- (# s', c #) -> mwaWriteMostSignificant mwa c s'
+ = Ubx.do
+ c <- ioWord# (c_mpn_add mwa wb (wordArraySize# wb) wa (wordArraySize# wa))
+ mwaWriteMostSignificant mwa c
| True
- = case ioWord# (c_mpn_add mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
- (# s', c #) -> mwaWriteMostSignificant mwa c s'
+ = Ubx.do
+ c <- ioWord# (c_mpn_add mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
+ mwaWriteMostSignificant mwa c
bignat_add_word
:: MutableWordArray# RealWorld
@@ -92,9 +96,9 @@ bignat_add_word
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_add_word #-}
-bignat_add_word mwa wa b s = do
- case ioWord# (c_mpn_add_1 mwa wa (wordArraySize# wa) b) s of
- (# s', c #) -> mwaWriteMostSignificant mwa c s'
+bignat_add_word mwa wa b = Ubx.do
+ c <- ioWord# (c_mpn_add_1 mwa wa (wordArraySize# wa) b)
+ mwaWriteMostSignificant mwa c
bignat_sub
:: MutableWordArray# RealWorld
@@ -103,10 +107,11 @@ bignat_sub
-> State# RealWorld
-> (# State# RealWorld, Bool# #)
{-# INLINE bignat_sub #-}
-bignat_sub mwa wa wb s =
- case ioWord# (c_mpn_sub mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
- (# s', 1## #) -> (# s', 0# #) -- underflow
- (# s', _ #) -> (# s', 1# #) -- no underflow
+bignat_sub mwa wa wb = Ubx.do
+ c <- ioWord# (c_mpn_sub mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
+ case c of
+ 1## -> Ubx.pure 0# -- underflow
+ _ -> Ubx.pure 1# -- no underflow
bignat_sub_word
:: MutableWordArray# RealWorld
@@ -115,10 +120,11 @@ bignat_sub_word
-> State# RealWorld
-> (# State# RealWorld, Bool# #)
{-# INLINE bignat_sub_word #-}
-bignat_sub_word mwa wa b s =
- case ioWord# (c_mpn_sub_1 mwa wa (wordArraySize# wa) b) s of
- (# s', 1## #) -> (# s', 0# #) -- underflow
- (# s', _ #) -> (# s', 1# #) -- no underflow
+bignat_sub_word mwa wa b = Ubx.do
+ c <- ioWord# (c_mpn_sub_1 mwa wa (wordArraySize# wa) b)
+ case c of
+ 1## -> Ubx.pure 0# -- underflow
+ _ -> Ubx.pure 1# -- no underflow
bignat_mul
:: MutableWordArray# RealWorld
@@ -127,11 +133,12 @@ bignat_mul
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_mul #-}
-bignat_mul mwa wa wb s = do
- case ioWord# (c_mpn_mul mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
- (# s', _msl #) -> s' -- we don't care about the most-significant
- -- limb. The caller shrink the mwa if
- -- necessary anyway.
+bignat_mul mwa wa wb = Ubx.do
+ _msl <- ioWord# (c_mpn_mul mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
+ -- _msl is the most-significant limb of the result. It tells if the result
+ -- has a leading zero limb. We ignore it: the caller removes leading zero
+ -- limbs anyway (see withNewWordArrayTrimmed#).
+ Ubx.nop
bignat_mul_word
:: MutableWordArray# RealWorld -- ^ Result
@@ -140,9 +147,9 @@ bignat_mul_word
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_mul_word #-}
-bignat_mul_word mwa wa b s =
- case ioWord# (c_mpn_mul_1 mwa wa (wordArraySize# wa) b) s of
- (# s', c #) -> mwaWriteMostSignificant mwa c s'
+bignat_mul_word mwa wa b = Ubx.do
+ c <- ioWord# (c_mpn_mul_1 mwa wa (wordArraySize# wa) b)
+ mwaWriteMostSignificant mwa c
bignat_popcount :: WordArray# -> Word#
{-# INLINE bignat_popcount #-}
@@ -156,11 +163,12 @@ bignat_shiftl
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_shiftl #-}
-bignat_shiftl mwa wa n s =
- case ioWord# (c_mpn_lshift mwa wa (wordArraySize# wa) n) s of
- (# s', _msl #) -> s' -- we don't care about the most-significant
- -- limb. The caller shrink the mwa if
- -- necessary anyway.
+bignat_shiftl mwa wa n = Ubx.do
+ _msl <- ioWord# (c_mpn_lshift mwa wa (wordArraySize# wa) n)
+ -- _msl is the most-significant limb of the result. It tells if the result
+ -- has a leading zero limb. We ignore it: the caller removes leading zero
+ -- limbs anyway (see withNewWordArrayTrimmed#).
+ Ubx.nop
bignat_shiftr
:: MutableWordArray# RealWorld
@@ -169,11 +177,12 @@ bignat_shiftr
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_shiftr #-}
-bignat_shiftr mwa wa n s =
- case ioWord# (c_mpn_rshift mwa wa (wordArraySize# wa) n) s of
- (# s', _msl #) -> s' -- we don't care about the most-significant
- -- limb. The caller shrink the mwa if
- -- necessary anyway.
+bignat_shiftr mwa wa n = Ubx.do
+ _msl <- ioWord# (c_mpn_rshift mwa wa (wordArraySize# wa) n)
+ -- _msl is the most-significant limb of the result. It tells if the result
+ -- has a leading zero limb. We ignore it: the caller removes leading zero
+ -- limbs anyway (see withNewWordArrayTrimmed#).
+ Ubx.nop
bignat_or
:: MutableWordArray# RealWorld
@@ -182,15 +191,16 @@ bignat_or
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_or #-}
-bignat_or mwa wa wb s1
- | isTrue# (szA >=# szB) = go wa szA wb szB s1
- | True = go wb szB wa szA s1
+bignat_or mwa wa wb
+ | isTrue# (szA >=# szB) = go wa szA wb szB
+ | True = go wb szB wa szA
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
-- nx >= ny
- go wx nx wy ny s = case ioVoid (c_mpn_ior_n mwa wx wy ny) s of
- s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
+ go wx nx wy ny = Ubx.do
+ ioVoid (c_mpn_ior_n mwa wx wy ny)
+ mwaArrayCopy# mwa ny wx ny (nx -# ny)
bignat_xor
:: MutableWordArray# RealWorld
@@ -199,15 +209,16 @@ bignat_xor
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_xor #-}
-bignat_xor mwa wa wb s1
- | isTrue# (szA >=# szB) = go wa szA wb szB s1
- | True = go wb szB wa szA s1
+bignat_xor mwa wa wb
+ | isTrue# (szA >=# szB) = go wa szA wb szB
+ | True = go wb szB wa szA
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
-- nx >= ny
- go wx nx wy ny s = case ioVoid (c_mpn_xor_n mwa wx wy ny) s of
- s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
+ go wx nx wy ny = Ubx.do
+ ioVoid (c_mpn_xor_n mwa wx wy ny)
+ mwaArrayCopy# mwa ny wx ny (nx -# ny)
bignat_and
:: MutableWordArray# RealWorld -- ^ Result
@@ -229,9 +240,9 @@ bignat_and_not
-> State# RealWorld
-> State# RealWorld
{-# INLINE bignat_and_not #-}
-bignat_and_not mwa wa wb s =
- case ioVoid (c_mpn_andn_n mwa wa wb n) s of
- s' -> mwaArrayCopy# mwa szB wa szB (szA -# szB) s'
+bignat_and_not mwa wa wb = Ubx.do
+ ioVoid (c_mpn_andn_n mwa wa wb n)
+ mwaArrayCopy# mwa szB wa szB (szA -# szB)
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
@@ -291,9 +302,7 @@ bignat_quot_word
-> Word#
-> State# RealWorld
-> State# RealWorld
-bignat_quot_word mwq wa b s =
- case bignat_quotrem_word mwq wa b s of
- (# s', _ #) -> s'
+bignat_quot_word mwq wa b = Ubx.void (bignat_quotrem_word mwq wa b)
bignat_rem_word
:: WordArray#
@@ -309,10 +318,10 @@ bignat_gcd
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_gcd mwr wa wb s =
+bignat_gcd mwr wa wb = Ubx.do
-- wa > wb
- case ioInt# (c_mpn_gcd# mwr wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
- (# s', sz #) -> mwaSetSize# mwr (narrowGmpSize# sz) s'
+ sz <- ioInt# (c_mpn_gcd# mwr wa (wordArraySize# wa) wb (wordArraySize# wb))
+ mwaSetSize# mwr (narrowGmpSize# sz)
bignat_gcd_word
:: WordArray#
@@ -379,7 +388,7 @@ integer_powmod b e m = naturalFromBigNat# (withNewWordArray# szm io)
0# -> szb
_ -> negateInt# szb
- io r s = sbignat_powmod r ssb bb be bm s
+ io r = sbignat_powmod r ssb bb be bm
sbignat_powmod
@@ -390,9 +399,9 @@ sbignat_powmod
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-sbignat_powmod r b_signed_size b e m s =
- case ioInt# (integer_gmp_powm# r b b_signed_size e (wordArraySize# e) m (wordArraySize# m)) s of
- (# s', n #) -> mwaSetSize# r (narrowGmpSize# n) s'
+sbignat_powmod r b_signed_size b e m = Ubx.do
+ n <- ioInt# (integer_gmp_powm# r b b_signed_size e (wordArraySize# e) m (wordArraySize# m))
+ mwaSetSize# r (narrowGmpSize# n)
integer_gcde
:: Integer
@@ -422,40 +431,39 @@ integer_gcde a b = case runRW# io of (# _, a #) -> a
!x_init_sz = szb
!y_init_sz = sza
- io s =
+ io = Ubx.do
-- allocate output arrays
- case newWordArray# g_init_sz s of { (# s, mbg #) ->
- case newWordArray# x_init_sz s of { (# s, mbx #) ->
- case newWordArray# y_init_sz s of { (# s, mby #) ->
+ mbg <- newWordArray# g_init_sz
+ mbx <- newWordArray# x_init_sz
+ mby <- newWordArray# y_init_sz
-- allocate space to return sizes (3x4 = 12)
- case newPinnedByteArray# 12# s of { (# s, mszs #) ->
- case unsafeFreezeByteArray# mszs s of { (# s, szs #) ->
- let !ssx_ptr = byteArrayContents# szs in
- let !ssy_ptr = ssx_ptr `plusAddr#` 4# in
- let !sg_ptr = ssy_ptr `plusAddr#` 4# in
+ mszs <- newPinnedByteArray# 12#
+ szs <- unsafeFreezeByteArray# mszs
+ let !ssx_ptr = byteArrayContents# szs
+ !ssy_ptr = ssx_ptr `plusAddr#` 4#
+ !sg_ptr = ssy_ptr `plusAddr#` 4#
-- call GMP
- case ioVoid (integer_gmp_gcdext# mbx ssx_ptr mby ssy_ptr mbg sg_ptr ba ssa bb ssb) s of { s ->
+ ioVoid (integer_gmp_gcdext# mbx ssx_ptr mby ssy_ptr mbg sg_ptr ba ssa bb ssb)
-- read sizes
- case readInt32OffAddr# ssx_ptr 0# s of { (# s, ssx #) ->
- case readInt32OffAddr# ssy_ptr 0# s of { (# s, ssy #) ->
- case readInt32OffAddr# sg_ptr 0# s of { (# s, sg #) ->
- case touch# szs s of { s ->
+ ssx <- readInt32OffAddr# ssx_ptr 0#
+ ssy <- readInt32OffAddr# ssy_ptr 0#
+ sg <- readInt32OffAddr# sg_ptr 0#
+ touch# szs
-- shrink x, y and g to their actual sizes and freeze them
- let !sx = absI# (int32ToInt# ssx) in
- let !sy = absI# (int32ToInt# ssy) in
- case mwaSetSize# mbx sx s of { s ->
- case mwaSetSize# mby sy s of { s ->
- case mwaSetSize# mbg (int32ToInt# sg) s of { s ->
+ let !sx = absI# (int32ToInt# ssx)
+ !sy = absI# (int32ToInt# ssy)
+ mwaSetSize# mbx sx
+ mwaSetSize# mby sy
+ mwaSetSize# mbg (int32ToInt# sg)
-- return x, y and g as Integer
- case unsafeFreezeByteArray# mbx s of { (# s, bx #) ->
- case unsafeFreezeByteArray# mby s of { (# s, by #) ->
- case unsafeFreezeByteArray# mbg s of { (# s, bg #) ->
+ bx <- unsafeFreezeByteArray# mbx
+ by <- unsafeFreezeByteArray# mby
+ bg <- unsafeFreezeByteArray# mbg
- (# s, (# integerFromBigNat# bg
- , integerFromBigNatSign# (int32ToInt# ssx <# 0#) bx
- , integerFromBigNatSign# (int32ToInt# ssy <# 0#) by #) #)
- }}}}}}}}}}}}}}}}
+ Ubx.pure (# integerFromBigNat# bg
+ , integerFromBigNatSign# (int32ToInt# ssx <# 0#) bx
+ , integerFromBigNatSign# (int32ToInt# ssy <# 0#) by #)
@@ -477,8 +485,9 @@ integer_recip_mod x m =
sbignat_recip_mod :: Int# -> BigNat# -> BigNat# -> BigNat#
sbignat_recip_mod sign_x x m = withNewWordArray# szm io
where
- io r s = case ioInt# (integer_gmp_invert# r x ssx m szm) s of
- (# s, rn #) -> mwaSetSize# r (narrowGmpSize# rn) s
+ io r = Ubx.do
+ rn <- ioInt# (integer_gmp_invert# r x ssx m szm)
+ mwaSetSize# r (narrowGmpSize# rn)
!szx = bigNatSize# x
!szm = bigNatSize# m
!ssx = case sign_x of -- signed size of x
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/Native.hs
=====================================
@@ -7,6 +7,7 @@
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE BinaryLiterals #-}
+{-# LANGUAGE QualifiedDo #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
module GHC.Internal.Bignum.Backend.Native where
@@ -27,6 +28,7 @@ import GHC.Internal.Bignum.WordArray
import GHC.Internal.Bignum.Primitives
import GHC.Internal.Prim
import GHC.Internal.Types
+import qualified GHC.Internal.Unboxed.Do as Ubx
-- See W1 of Note [Tracking dependencies on primitives] in GHC.Internal.Base
-- (This module uses the empty tuple () and string literals.)
@@ -96,39 +98,41 @@ bignat_add mwa wa wb = addABc 0# 0##
-- 4) We only have a potential carry
-- => write the carry or shrink the array
- addABc i carry s
- | isTrue# (i <# szMin) =
+ addABc i carry
+ | isTrue# (i <# szMin) = Ubx.do
let
!(# carry', r #) = plusWord3#
(indexWordArray# wa i)
(indexWordArray# wb i)
carry
- in case mwaWrite# mwa i r s of
- s' -> addABc (i +# 1#) carry' s'
+ mwaWrite# mwa i r
+ addABc (i +# 1#) carry'
| isTrue# ((i ==# szA) &&# (i ==# szB))
- = mwaWriteOrShrink mwa carry i s
+ = mwaWriteOrShrink mwa carry i
| isTrue# (i ==# szA)
- = addAoBc wb i carry s
+ = addAoBc wb i carry
| True
- = addAoBc wa i carry s
+ = addAoBc wa i carry
- addAoBc wab i carry s
+ addAoBc wab i carry
| isTrue# (i ==# wordArraySize# wab)
- = mwaWriteOrShrink mwa carry i s
+ = mwaWriteOrShrink mwa carry i
| 0## <- carry
- = -- copy the remaining words and remove the word allocated for the
- -- potential carry
- case mwaArrayCopy# mwa i wab i (wordArraySize# wab -# i) s of
- s' -> mwaShrink# mwa 1# s'
+ = Ubx.do
+ -- copy the remaining words and remove the word allocated for the
+ -- potential carry
+ mwaArrayCopy# mwa i wab i (wordArraySize# wab -# i)
+ mwaShrink# mwa 1#
| True
- = let !(# carry', r #) = plusWord2# (indexWordArray# wab i) carry
- in case mwaWrite# mwa i r s of
- s' -> addAoBc wab (i +# 1#) carry' s'
+ = Ubx.do
+ let !(# carry', r #) = plusWord2# (indexWordArray# wab i) carry
+ mwaWrite# mwa i r
+ addAoBc wab (i +# 1#) carry'
-- | Add a non-zero BigNat and a non-zero Word#
--
@@ -163,22 +167,25 @@ bignat_sub_word
bignat_sub_word mwa wa b = go b 0#
where
!sz = wordArraySize# wa
- go carry i s
+ go carry i
| isTrue# (i >=# sz)
- = (# s, carry `eqWord#` 0## #)
+ = Ubx.pure (carry `eqWord#` 0##)
| 0## <- carry
- = case mwaArrayCopy# mwa i wa i (sz -# i) s of
- s' -> (# s', 1# #) -- no underflow
+ = Ubx.do
+ mwaArrayCopy# mwa i wa i (sz -# i)
+ Ubx.pure 1# -- no underflow
| True
= case subWordC# (indexWordArray# wa i) carry of
(# 0##, 0# #)
- | isTrue# (i ==# sz) -> case mwaShrink# mwa 1# s of
- s' -> (# s', 1# #) -- no underflow
+ | isTrue# (i ==# sz) -> Ubx.do
+ mwaShrink# mwa 1#
+ Ubx.pure 1# -- no underflow
- (# l , c #) -> case mwaWrite# mwa i l s of
- s1 -> go (int2Word# c) (i +# 1#) s1
+ (# l , c #) -> Ubx.do
+ mwaWrite# mwa i l
+ go (int2Word# c) (i +# 1#)
-- | Multiply a non-zero BigNat and a non-zero Word#
--
@@ -196,14 +203,14 @@ bignat_mul_word
bignat_mul_word mwa wa b = go 0# 0##
where
!szA = wordArraySize# wa
- go i carry s
- | isTrue# (i ==# szA) = mwaWriteOrShrink mwa carry i s
- | True =
+ go i carry
+ | isTrue# (i ==# szA) = mwaWriteOrShrink mwa carry i
+ | True = Ubx.do
let
ai = indexWordArray# wa i
!(# carry', r #) = plusWord12# carry (timesWord2# ai b)
- in case mwaWrite# mwa i r s of
- s' -> go (i +# 1#) carry' s'
+ mwaWrite# mwa i r
+ go (i +# 1#) carry'
-- | Multiply two non-zero BigNat
@@ -219,10 +226,10 @@ bignat_mul
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_mul mwa wa wb s1 =
+bignat_mul mwa wa wb = Ubx.do
-- initialize the resulting WordArray
- case mwaFill# mwa 0## 0## (int2Word# sz) s1 of
- s' -> mulEachB ctzB s' -- loop on b Words
+ mwaFill# mwa 0## 0## (int2Word# sz)
+ mulEachB ctzB -- loop on b Words
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
@@ -232,27 +239,29 @@ bignat_mul mwa wa wb s1 =
!ctzB = word2Int# (bigNatCtzWord# wb)
-- multiply a single bj Word# to the whole wa WordArray
- mul mwa wa bj j i carry s
+ mul mwa wa bj j i carry
| isTrue# (i ==# wordArraySize# wa)
-- write the carry
- = mwaAddInplaceWord# mwa (i +# j) carry s
+ = mwaAddInplaceWord# mwa (i +# j) carry
- | True = let
- ai = indexWordArray# wa i
- !(# c',r' #) = timesWord2# ai bj
- !(# c'',r #) = plusWord2# r' carry
- carry' = plusWord# c' c''
- in case mwaAddInplaceWord# mwa (i +# j) r s of
- s' -> mul mwa wa bj j (i +# 1#) carry' s'
+ | True = Ubx.do
+ let
+ ai = indexWordArray# wa i
+ !(# c',r' #) = timesWord2# ai bj
+ !(# c'',r #) = plusWord2# r' carry
+ carry' = plusWord# c' c''
+ mwaAddInplaceWord# mwa (i +# j) r
+ mul mwa wa bj j (i +# 1#) carry'
-- for each bj in wb, call `mul bj wa`
- mulEachB i s
- | isTrue# (i ==# szB) = s
+ mulEachB i
+ | isTrue# (i ==# szB) = Ubx.nop
| True = case indexWordArray# wb i of
-- detect bj == 0## and skip the loop
- 0## -> mulEachB (i +# 1#) s
- bi -> case mul mwa wa bi i ctzA 0## s of
- s' -> mulEachB (i +# 1#) s'
+ 0## -> mulEachB (i +# 1#)
+ bi -> Ubx.do
+ mul mwa wa bi i ctzA 0##
+ mulEachB (i +# 1#)
-- | Sub two non-zero BigNat
--
@@ -269,12 +278,12 @@ bignat_sub
-> WordArray#
-> State# RealWorld
-> (# State# RealWorld, Bool# #)
-bignat_sub mwa wa wb s =
+bignat_sub mwa wa wb = Ubx.do
-- initialize the resulting WordArray
-- Note: we could avoid the copy by subtracting the first non-zero
-- less-significant word of b...
- case mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa) s of
- s' -> mwaSubInplaceArray mwa 0# wb s'
+ mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa)
+ mwaSubInplaceArray mwa 0# wb
-- | PopCount of a non-zero BigNat
bignat_popcount :: WordArray# -> Word#
@@ -298,30 +307,29 @@ bignat_shiftl
-> Word#
-> State# s
-> State# s
-bignat_shiftl mwa wa n s1 =
+bignat_shiftl mwa wa n = Ubx.do
-- set the lower words to 0
- case mwaFill# mwa 0## 0## (int2Word# nw) s1 of
- s2 -> if
- | 0# <- nb -> mwaArrayCopy# mwa nw wa 0# szA s2
- | True -> mwaBitShift 0# 0## s2
+ mwaFill# mwa 0## 0## (int2Word# nw)
+ if | 0# <- nb -> mwaArrayCopy# mwa nw wa 0# szA
+ | True -> mwaBitShift 0# 0##
where
!szA = wordArraySize# wa
!(# nw, nb #) = count_words_bits_int n
!sh = WORD_SIZE_IN_BITS# -# nb
-- Bit granularity (c is the carry from the previous shift)
- mwaBitShift i c s
+ mwaBitShift i c
-- write the carry
| isTrue# (i ==# szA)
- = mwaWriteOrShrink mwa c (i +# nw) s
+ = mwaWriteOrShrink mwa c (i +# nw)
- | True =
+ | True = Ubx.do
let
!ai = indexWordArray# wa i
!v = c `or#` (ai `uncheckedShiftL#` nb)
!c' = ai `uncheckedShiftRL#` sh
- in case mwaWrite# mwa (i +# nw) v s of
- s' -> mwaBitShift (i +# 1#) c' s'
+ mwaWrite# mwa (i +# nw) v
+ mwaBitShift (i +# 1#) c'
-- | Right-shift a non-zero BigNat by a non-zero amount of bits
@@ -337,9 +345,9 @@ bignat_shiftr
-> Word#
-> State# s
-> State# s
-bignat_shiftr mwa wa n s1
- | isTrue# (nb ==# 0#) = mwaArrayCopy# mwa 0# wa nw sz s1
- | True = mwaBitShift (sz -# 1#) 0## s1
+bignat_shiftr mwa wa n
+ | isTrue# (nb ==# 0#) = mwaArrayCopy# mwa 0# wa nw sz
+ | True = mwaBitShift (sz -# 1#) 0##
where
!szA = wordArraySize# wa
!(# nw, nb #) = count_words_bits_int n
@@ -347,15 +355,15 @@ bignat_shiftr mwa wa n s1
!sh = WORD_SIZE_IN_BITS# -# nb
-- Bit granularity (c is the carry from the previous shift)
- mwaBitShift i c s
- | isTrue# (i <# 0#) = s
- | True =
+ mwaBitShift i c
+ | isTrue# (i <# 0#) = Ubx.nop
+ | True = Ubx.do
let
!ai = indexWordArray# wa (i +# nw)
!v = c `or#` (ai `uncheckedShiftRL#` nb)
!c' = ai `uncheckedShiftL#` sh
- in case mwaWrite# mwa i v s of
- s' -> mwaBitShift (i -# 1#) c' s'
+ mwaWrite# mwa i v
+ mwaBitShift (i -# 1#) c'
-- | Right-shift a non-zero BigNat by a non-zero amount of bits by first
-- converting it into its two's complement representation and then again after
@@ -372,15 +380,15 @@ bignat_shiftr_neg
-> Word#
-> State# s
-> State# s
-bignat_shiftr_neg mwa wa n s1
+bignat_shiftr_neg mwa wa n = Ubx.do
-- initialize higher limb of mwa
- = case mwaSize# mwa s1 of
- (# s2, sz_mwa #) -> case mwaWrite# mwa (sz_mwa -# 1#) 0## s2 of
- s3 -> case bignat_shiftr mwa wa n s3 of
- s4 -> if nz_shifted_out
- -- round if non-zero bits were shifted out
- then mwaAddInplaceWord# mwa 0# 1## s4
- else s4
+ sz_mwa <- mwaSize# mwa
+ mwaWrite# mwa (sz_mwa -# 1#) 0##
+ bignat_shiftr mwa wa n
+ if nz_shifted_out
+ -- round if non-zero bits were shifted out
+ then mwaAddInplaceWord# mwa 0# 1##
+ else Ubx.nop
where
!(# nw, nb #) = count_words_bits_int n
@@ -412,16 +420,16 @@ bignat_or
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_or mwa wa wb s1
- | isTrue# (szA >=# szB) = go wa szA wb szB s1
- | True = go wb szB wa szA s1
+bignat_or mwa wa wb
+ | isTrue# (szA >=# szB) = go wa szA wb szB
+ | True = go wb szB wa szA
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
-- nx >= ny
- go wx nx wy ny s =
- case mwaInitArrayBinOp mwa wx wy or# s of
- s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
+ go wx nx wy ny = Ubx.do
+ mwaInitArrayBinOp mwa wx wy or#
+ mwaArrayCopy# mwa ny wx ny (nx -# ny)
-- | XOR two non-zero BigNat
--
@@ -436,16 +444,16 @@ bignat_xor
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_xor mwa wa wb s1
- | isTrue# (szA >=# szB) = go wa szA wb szB s1
- | True = go wb szB wa szA s1
+bignat_xor mwa wa wb
+ | isTrue# (szA >=# szB) = go wa szA wb szB
+ | True = go wb szB wa szA
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
-- nx >= ny
- go wx nx wy ny s =
- case mwaInitArrayBinOp mwa wx wy xor# s of
- s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
+ go wx nx wy ny = Ubx.do
+ mwaInitArrayBinOp mwa wx wy xor#
+ mwaArrayCopy# mwa ny wx ny (nx -# ny)
-- | AND two non-zero BigNat
--
@@ -475,9 +483,9 @@ bignat_and_not
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_and_not mwa wa wb s =
- case mwaInitArrayBinOp mwa wa wb (\x y -> x `and#` not# y) s of
- s' -> mwaArrayCopy# mwa szB wa szB (szA -# szB) s'
+bignat_and_not mwa wa wb = Ubx.do
+ mwaInitArrayBinOp mwa wa wb (\x y -> x `and#` not# y)
+ mwaArrayCopy# mwa szB wa szB (szA -# szB)
where
!szA = wordArraySize# wa
!szB = wordArraySize# wb
@@ -497,7 +505,7 @@ bignat_quotrem
-> WordArray#
-> State# s
-> State# s
-bignat_quotrem mwq mwr uwa uwb s0 =
+bignat_quotrem mwq mwr uwa uwb = Ubx.do
-- Normalization consists in left-shifting bits in B and A so that the
-- most-significant bit of the most-significant word of B is 1. It makes
-- quotient prediction much more efficient as we only use the two most
@@ -513,30 +521,27 @@ bignat_quotrem mwq mwr uwa uwb s0 =
--
-- We allocate it here with an additionnal Word compared to A because
-- normalizing can left shift at most (N-1) bits (on N-bit arch).
- in case newWordArray# (wordArraySize# uwa +# 1#) s0 of { (# s1, mnwa #) ->
+ mnwa <- newWordArray# (wordArraySize# uwa +# 1#)
-- normalized A in mnwa
- let normalizeA s = case mwaWrite# mnwa (wordArraySize# uwa) 0## s of -- init potential carry
- s -> case bignat_shiftl mnwa uwa clzb s of -- left shift
- s -> mwaTrimZeroes# mnwa s -- remove null carry if any
- in case normalizeA s1 of { s2 ->
+ mwaWrite# mnwa (wordArraySize# uwa) 0## -- init potential carry
+ bignat_shiftl mnwa uwa clzb -- left shift
+ mwaTrimZeroes# mnwa -- remove null carry if any
-- normalize B. We don't do it in a MutableWordArray because it will remain
-- constant during the whole computation.
- let !nwb = bigNatShiftL# uwb clzb in
+ let !nwb = bigNatShiftL# uwb clzb
-- perform quotrem on normalized inputs
- case bignat_quotrem_normalized mwq mnwa nwb s2 of { s3 ->
+ bignat_quotrem_normalized mwq mnwa nwb
-- denormalize the remainder now stored in mnwa. We just have to right shift
-- of "clzb" bits. We copy the result into "mwr" array.
- let denormalizeR s = case mwaTrimZeroes# mnwa s of
- s -> case unsafeFreezeByteArray# mnwa s of
- (# s, wr #) -> case mwaSetSize# mwr (wordArraySize# wr) s of
- s -> case bignat_shiftr mwr wr clzb s of
- s -> mwaTrimZeroes# mwr s
- in denormalizeR s3
- }}}
+ mwaTrimZeroes# mnwa
+ wr <- unsafeFreezeByteArray# mnwa
+ mwaSetSize# mwr (wordArraySize# wr)
+ bignat_shiftr mwr wr clzb
+ mwaTrimZeroes# mwr
@@ -553,10 +558,10 @@ bignat_quot
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_quot mwq wa wb s =
+bignat_quot mwq wa wb = Ubx.do
-- allocate a temporary array for the remainder and call quotrem
- case newWordArray# (wordArraySize# wb) s of
- (# s, mwr #) -> bignat_quotrem mwq mwr wa wb s
+ mwr <- newWordArray# (wordArraySize# wb)
+ bignat_quotrem mwq mwr wa wb
-- | Remainder of two non-zero BigNat
--
@@ -571,12 +576,12 @@ bignat_rem
-> WordArray#
-> State# RealWorld
-> State# RealWorld
-bignat_rem mwr wa wb s =
+bignat_rem mwr wa wb = Ubx.do
-- allocate a temporary array for the quotient and call quotrem
-- (we could avoid allocating it as it is not used to compute the result but
-- it would require non trivial modification of bignat_quotrem)
- case newWordArray# szQ s of
- (# s, mwq #) -> bignat_quotrem mwq mwr wa wb s
+ mwq <- newWordArray# szQ
+ bignat_quotrem mwq mwr wa wb
where
szA = wordArraySize# wa
szB = wordArraySize# wb
@@ -593,14 +598,14 @@ bignat_quotrem_normalized
-> WordArray#
-> State# s
-> State# s
-bignat_quotrem_normalized mwq mwa b s0 =
+bignat_quotrem_normalized mwq mwa b = Ubx.do
-- n is the size of B
let !n = wordArraySize# b
-- m+n is the size of A (m >= 0)
- in case mwaSize# mwa s0 of { (# s1, szA #) ->
- let !m = szA -# n in
+ szA <- mwaSize# mwa
+ let !m = szA -# n
-- Definitions:
-- MSW(x) is the most-significant word of x
@@ -624,18 +629,22 @@ bignat_quotrem_normalized mwq mwa b s0 =
-- A := A - (B << m words)
-- else Qm = 0
-- A unchanged
- let computeQm s = case mwaTrimCompare m mwa b s of
- (# s, LT #) -> (# s, 0## #)
- (# s, _ #) -> (# s, 1## #)
-
- updateQj j qj qjb s = case mwaWrite# mwq j qj s of -- write Qj
- s | 0## <- qj -> s
- | True -> case mwaSubInplaceArray mwa j qjb s of -- subtract (qjB << j words)
- (# s, _ #) -> s
+ let computeQm = Ubx.do
+ c <- mwaTrimCompare m mwa b
+ case c of
+ LT -> Ubx.pure 0##
+ _ -> Ubx.pure 1##
+
+ updateQj j qj qjb = Ubx.do
+ mwaWrite# mwq j qj -- write Qj
+ case qj of
+ 0## -> Ubx.nop
+ _ -> Ubx.void (mwaSubInplaceArray mwa j qjb) -- subtract (qjB << j words)
-- update the highest word of Q
- updateQm s = case computeQm s of
- (# s, qm #) -> updateQj m qm b s
+ updateQm = Ubx.do
+ qm <- computeQm
+ updateQj m qm b
-- the size of Q is szA+szB+1 BEFORE normalization. Normalization may add
-- an additional higher word to A.
@@ -649,11 +658,13 @@ bignat_quotrem_normalized mwq mwa b s0 =
-- accordingly
--
-- We detect if A has an additional limb by comparing the size of Q with m
- updateQmMaybe s = case mwaSize# mwq s of
- (# s, szQ #) | isTrue# (m <# szQ) -> updateQm s
- | True -> s
+ updateQmMaybe = Ubx.do
+ szQ <- mwaSize# mwq
+ if isTrue# (m <# szQ)
+ then updateQm
+ else Ubx.nop
- in case updateQmMaybe s1 of { s2 ->
+ updateQmMaybe
-- main loop: for j from (m-1) downto 0
@@ -669,33 +680,37 @@ bignat_quotrem_normalized mwq mwa b s0 =
let bmsw = wordArrayLast# b -- most significant word of B
- estimateQj j s =
- case mwaRead# mwa (n +# j) s of
- (# s, a1 #) -> case mwaRead# mwa (n +# j -# 1#) s of
- (# s, a0 #) -> case quotRemWord3# (# a1, a0 #) bmsw of
- (# (# 0##, qj #), _ #) -> (# s, qj #)
- (# (# _, _ #), _ #) -> (# s, WORD_MAXBOUND## #)
+ estimateQj j = Ubx.do
+ a1 <- mwaRead# mwa (n +# j)
+ a0 <- mwaRead# mwa (n +# j -# 1#)
+ case quotRemWord3# (# a1, a0 #) bmsw of
+ (# (# 0##, qj #), _ #) -> Ubx.pure qj
+ (# (# _, _ #), _ #) -> Ubx.pure WORD_MAXBOUND##
-- we perform the qj*B multiplication once and then we subtract B from
-- qj*B as much as needed until (qj'*B << j words) <= A
- findRealQj j qj s = findRealQj' j qj (bigNatMulWord# b qj) s
+ findRealQj j qj = findRealQj' j qj (bigNatMulWord# b qj)
- findRealQj' j qj qjB s = case mwaTrimCompare j mwa qjB s of
- (# s, LT #) -> findRealQj' j (qj `minusWord#` 1##) (bigNatSubUnsafe qjB b) s
+ findRealQj' j qj qjB = Ubx.do
+ c <- mwaTrimCompare j mwa qjB
+ case c of
+ LT -> findRealQj' j (qj `minusWord#` 1##) (bigNatSubUnsafe qjB b)
-- TODO: we could do the sub inplace to
-- reduce allocations
- (# s, _ #) -> (# s, qj, qjB #)
+ _ -> Ubx.pure (# qj, qjB #)
- loop j s = case estimateQj j s of
- (# s, qj #) -> case findRealQj j qj s of
- (# s, qj, qjB #) -> case updateQj j qj qjB s of
- s | 0# <- j -> s
- | True -> loop (j -# 1#) s
+ loop j = Ubx.do
+ qj <- estimateQj j
+ (# qj', qjB #) <- findRealQj j qj
+ updateQj j qj' qjB
+ case j of
+ 0# -> Ubx.nop
+ _ -> loop (j -# 1#)
- in if | 0# <- m -> s2
- | True -> loop (m -# 1#) s2
- }}
+ case m of
+ 0# -> Ubx.nop
+ _ -> loop (m -# 1#)
-- | QuotRem of a non-zero BigNat and a non-zero Word
--
@@ -712,17 +727,17 @@ bignat_quotrem_word
-> Word#
-> State# s
-> (# State# s, Word# #)
-bignat_quotrem_word mwq wa b s = go (sz -# 1#) 0## s
+bignat_quotrem_word mwq wa b = go (sz -# 1#) 0##
where
sz = wordArraySize# wa
- go i r s
- | isTrue# (i <# 0#) = (# s, r #)
- | True =
+ go i r
+ | isTrue# (i <# 0#) = Ubx.pure r
+ | True = Ubx.do
let
ai = indexWordArray# wa i
!(# q,r' #) = quotRemWord2# r ai b
- in case mwaWrite# mwq i q s of
- s' -> go (i -# 1#) r' s'
+ mwaWrite# mwq i q
+ go (i -# 1#) r'
-- | Quot of a non-zero BigNat and a non-zero Word
--
@@ -737,17 +752,17 @@ bignat_quot_word
-> Word#
-> State# s
-> State# s
-bignat_quot_word mwq wa b s = go (sz -# 1#) 0## s
+bignat_quot_word mwq wa b = go (sz -# 1#) 0##
where
sz = wordArraySize# wa
- go i r s
- | isTrue# (i <# 0#) = s
- | True =
+ go i r
+ | isTrue# (i <# 0#) = Ubx.nop
+ | True = Ubx.do
let
ai = indexWordArray# wa i
!(# q,r' #) = quotRemWord2# r ai b
- in case mwaWrite# mwq i q s of
- s' -> go (i -# 1#) r' s'
+ mwaWrite# mwq i q
+ go (i -# 1#) r'
-- | Remainder of a non-zero BigNat and a non-zero Word
--
@@ -784,15 +799,15 @@ bignat_gcd
-> State# s
bignat_gcd mwr = go
where
- go wmax wmin s
+ go wmax wmin
| isTrue# (wordArraySize# wmin ==# 0#)
- = mwaInitCopyShrink# mwr wmax s
+ = mwaInitCopyShrink# mwr wmax
| True
= let
wmax' = wmin
!wmin' = bigNatRem wmax wmin
- in go wmax' wmin' s
+ in go wmax' wmin'
-- | Greatest common divisor (GCD) of a non-zero/non-one BigNat and a
-- non-zero/non-one Word#
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/BigNat.hs
=====================================
@@ -9,6 +9,11 @@
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE BinaryLiterals #-}
+{-# LANGUAGE QualifiedDo #-}
+{-# LANGUAGE MonoLocalBinds #-}
+ -- Local State#-passing loops (e.g. in bigNatFromAddrLE#) must stay
+ -- monomorphic in the state type, otherwise GHC does not turn them into
+ -- join points (see #14620).
{-# OPTIONS_GHC -fdefines-known-key-names #-}
-- Defines lots of functions that have BuiltinRules
@@ -28,6 +33,7 @@ import GHC.Internal.Magic
import GHC.Internal.Bignum.Primitives
import GHC.Internal.Bignum.WordArray
import GHC.Internal.Bignum.Backend
+import qualified GHC.Internal.Unboxed.Do as Ubx
-- | A BigNat
--
@@ -198,10 +204,10 @@ bigNatFromWordListUnsafe xs =
length i [] = i
length i (_:ys) = length (i +# 1#) ys
!lxs = length 0# xs
- writeWordList _mwa _i [] s = s
- writeWordList mwa i (W# w:ws) s =
- case mwaWrite# mwa i w s of
- s1 -> writeWordList mwa (i -# 1#) ws s1
+ writeWordList _mwa _i [] = Ubx.nop
+ writeWordList mwa i (W# w:ws) = Ubx.do
+ mwaWrite# mwa i w
+ writeWordList mwa (i -# 1#) ws
in withNewWordArray# lxs \mwa ->
writeWordList mwa (lxs -# 1#) xs
@@ -503,17 +509,18 @@ bigNatSubWordUnsafe# x y
where
!sz = wordArraySize# x
- go mwa carry i s
+ go mwa carry i
| isTrue# (i >=# sz)
- = s
+ = Ubx.nop
| 0## <- carry
- = mwaArrayCopy# mwa i x i (sz -# i) s
+ = mwaArrayCopy# mwa i x i (sz -# i)
| True
- = case subWordC# (indexWordArray# x i) carry of
- (# l, c #) -> case mwaWrite# mwa i l s of
- s1 -> go mwa (int2Word# c) (i +# 1#) s1
+ = Ubx.do
+ let !(# l, c #) = subWordC# (indexWordArray# x i) carry
+ mwaWrite# mwa i l
+ go mwa (int2Word# c) (i +# 1#)
-- | Subtract a Word# from a BigNat
--
@@ -537,13 +544,14 @@ bigNatSubUnsafe a b
| bigNatIsZero b = a
| True =
let szA = wordArraySize# a
- in withNewWordArrayTrimmed# szA \mwa s->
- case inline bignat_sub mwa a b s of
- (# s', 1# #) -> s'
- (# s', _ #) -> case raiseUnderflow of
- !_ -> s'
- -- see Note [ghc-bignum exceptions] in
- -- GHC.Internal.Bignum.Primitives
+ in withNewWordArrayTrimmed# szA \mwa -> Ubx.do
+ ok <- inline bignat_sub mwa a b
+ case ok of
+ 1# -> Ubx.nop
+ _ -> case raiseUnderflow of
+ !_ -> Ubx.nop
+ -- see Note [ghc-bignum exceptions] in
+ -- GHC.Internal.Bignum.Primitives
-- | Subtract two BigNat
bigNatSub :: BigNat# -> BigNat# -> (# (# #) | BigNat# #)
@@ -620,13 +628,12 @@ bigNatQuotRemWord# a b
| True =
let
sz = wordArraySize# a
- io s =
- case newWordArray# sz s of { (# s1, mwq #) ->
- case inline bignat_quotrem_word mwq a b s1 of { (# s2, r #) ->
- case mwaTrimZeroes# mwq s2 of { s3 ->
- case unsafeFreezeByteArray# mwq s3 of { (# s4, wq #) ->
- (# s4, (# wq, r #) #)
- }}}}
+ io = Ubx.do
+ mwq <- newWordArray# sz
+ r <- inline bignat_quotrem_word mwq a b
+ mwaTrimZeroes# mwq
+ wq <- unsafeFreezeByteArray# mwq
+ Ubx.pure (# wq, r #)
in case runRW# io of
(# _, (# wq,r #) #) -> (# wq, r #)
@@ -815,9 +822,9 @@ bigNatOrWord# a b
| 0## <- b = a
| True =
let sz = wordArraySize# a
- in withNewWordArray# sz \mwa s ->
- case mwaArrayCopy# mwa 1# a 1# (sz -# 1#) s of
- s' -> mwaWrite# mwa 0# (indexWordArray# a 0# `or#` b) s'
+ in withNewWordArray# sz \mwa -> Ubx.do
+ mwaArrayCopy# mwa 1# a 1# (sz -# 1#)
+ mwaWrite# mwa 0# (indexWordArray# a 0# `or#` b)
-- | Bitwise AND
bigNatAnd :: BigNat# -> BigNat# -> BigNat#
@@ -852,11 +859,10 @@ bigNatAndNotWord# :: BigNat# -> Word# -> BigNat#
bigNatAndNotWord# a b
| bigNatIsZero a = a
| szA <- bigNatSize# a
- = withNewWordArray# szA \mwa s ->
+ = withNewWordArray# szA \mwa -> Ubx.do
-- duplicate higher limbs
- case mwaArrayCopy# mwa 1# a 1# (szA -# 1#) s of
- s' -> writeWordArray# mwa 0#
- (indexWordArray# a 0# `and#` not# b) s'
+ mwaArrayCopy# mwa 1# a 1# (szA -# 1#)
+ writeWordArray# mwa 0# (indexWordArray# a 0# `and#` not# b)
-- | Bitwise AND with Int#
bigNatAndInt# :: BigNat# -> Int# -> BigNat#
@@ -864,11 +870,10 @@ bigNatAndInt# a b
| bigNatIsZero a = a
| isTrue# (b >=# 0#) = bigNatAndWord# a (int2Word# b)
| szA <- bigNatSize# a
- = withNewWordArray# szA \mwa s ->
+ = withNewWordArray# szA \mwa -> Ubx.do
-- duplicate higher limbs (because of sign-extension of b)
- case mwaArrayCopy# mwa 1# a 1# (szA -# 1#) s of
- s' -> writeWordArray# mwa 0#
- (indexWordArray# a 0# `and#` int2Word# b) s'
+ mwaArrayCopy# mwa 1# a 1# (szA -# 1#)
+ writeWordArray# mwa 0# (indexWordArray# a 0# `and#` int2Word# b)
-- | Bitwise XOR
@@ -891,9 +896,9 @@ bigNatXorWord# a b
| True =
let
sz = wordArraySize# a
- in withNewWordArray# sz \mwa s ->
- case mwaArrayCopy# mwa 1# a 1# (sz -# 1#) s of
- s' -> mwaWrite# mwa 0# (indexWordArray# a 0# `xor#` b) s'
+ in withNewWordArray# sz \mwa -> Ubx.do
+ mwaArrayCopy# mwa 1# a 1# (sz -# 1#)
+ mwaWrite# mwa 0# (indexWordArray# a 0# `xor#` b)
-- | PopCount for BigNat
bigNatPopCount :: BigNat# -> Word
@@ -1005,11 +1010,11 @@ bigNatBit# i
!nb = word2Int# (i `and#` WORD_SIZE_BITS_MASK##)
!sz = nw +# 1#
!v = 1## `uncheckedShiftL#` nb
- in withNewWordArray# sz \mwa s ->
+ in withNewWordArray# sz \mwa -> Ubx.do
-- clear the array
- case mwaFill# mwa 0## 0## (int2Word# sz) s of
- -- set the bit in the most-significant word
- s2 -> mwaWrite# mwa (sz -# 1#) v s2
+ mwaFill# mwa 0## 0## (int2Word# sz)
+ -- set the bit in the most-significant word
+ mwaWrite# mwa (sz -# 1#) v
-- | Return a BigNat whose bit `i` is the only one set.
--
@@ -1042,9 +1047,9 @@ bigNatClearBit# a n
mwaArrayCopy# mwa 0# a 0# nsz s
| True ->
- withNewWordArray# sz \mwa s ->
- case mwaArrayCopy# mwa 0# a 0# sz s of
- s' -> writeWordArray# mwa nw nv s'
+ withNewWordArray# sz \mwa -> Ubx.do
+ mwaArrayCopy# mwa 0# a 0# sz
+ writeWordArray# mwa nw nv
-- | BigNat set bit
bigNatSetBit# :: BigNat# -> Word# -> BigNat#
@@ -1061,15 +1066,15 @@ bigNatSetBit# a n
in if
-- result BigNat will have more limbs
| isTrue# (d ># 0#)
- -> withNewWordArray# (nw +# 1#) \mwa s ->
- case mwaArrayCopy# mwa 0# a 0# sz s of
- s' -> case mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#)) s' of
- s'' -> writeWordArray# mwa nw (bitW# nb) s''
+ -> withNewWordArray# (nw +# 1#) \mwa -> Ubx.do
+ mwaArrayCopy# mwa 0# a 0# sz
+ mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#))
+ writeWordArray# mwa nw (bitW# nb)
| nv <- bigNatIndex# a nw `or#` bitW# nb
- -> withNewWordArray# sz \mwa s ->
- case mwaArrayCopy# mwa 0# a 0# sz s of
- s' -> writeWordArray# mwa nw nv s'
+ -> withNewWordArray# sz \mwa -> Ubx.do
+ mwaArrayCopy# mwa 0# a 0# sz
+ writeWordArray# mwa nw nv
-- | Reverse the given bit
bigNatComplementBit# :: BigNat# -> Word# -> BigNat#
@@ -1082,15 +1087,15 @@ bigNatComplementBit# a n =
in if
-- result BigNat will have more limbs
| isTrue# (d ># 0#)
- -> withNewWordArray# (nw +# 1#) \mwa s ->
- case mwaArrayCopy# mwa 0# a 0# sz s of
- s' -> case mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#)) s' of
- s'' -> writeWordArray# mwa nw (bitW# nb) s''
+ -> withNewWordArray# (nw +# 1#) \mwa -> Ubx.do
+ mwaArrayCopy# mwa 0# a 0# sz
+ mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#))
+ writeWordArray# mwa nw (bitW# nb)
| nv <- bigNatIndex# a nw `xor#` bitW# nb
- -> withNewWordArrayTrimmed# sz \mwa s ->
- case mwaArrayCopy# mwa 0# a 0# sz s of
- s' -> writeWordArray# mwa nw nv s'
+ -> withNewWordArrayTrimmed# sz \mwa -> Ubx.do
+ mwaArrayCopy# mwa 0# a 0# sz
+ writeWordArray# mwa nw nv
-------------------------------------------------
-- Log operations
@@ -1259,11 +1264,12 @@ bigNatCtzWord a = W# (bigNatCtzWord# a)
-- zero bytes written.
bigNatToAddrLE# :: BigNat# -> Addr# -> State# s -> (# State# s, Word# #)
{-# NOINLINE bigNatToAddrLE# #-}
-bigNatToAddrLE# a addr s0
- | isTrue# (sz ==# 0#) = (# s0, 0## #)
- | True = case writeMSB s0 of
- (# s1, k #) -> case go 0# s1 of
- s2 -> (# s2, k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
+bigNatToAddrLE# a addr
+ | isTrue# (sz ==# 0#) = Ubx.pure 0##
+ | True = Ubx.do
+ k <- writeMSB
+ go 0#
+ Ubx.pure (k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
where
!sz = wordArraySize# a
!li = sz -# 1#
@@ -1271,15 +1277,16 @@ bigNatToAddrLE# a addr s0
writeMSB = wordToAddrLE# (indexWordArray# a li)
(addr `plusAddr#` (li `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#))
- go i s
+ go i
| isTrue# (i <# li)
, off <- i `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#
, w <- indexWordArray# a i
- = case wordWriteAddrLE# w (addr `plusAddr#` off) s of
- s -> go (i +# 1#) s
+ = Ubx.do
+ wordWriteAddrLE# w (addr `plusAddr#` off)
+ go (i +# 1#)
| True
- = s
+ = Ubx.nop
-- | Write a BigNat in base-256 big-endian representation and return the
-- number of bytes written.
@@ -1289,23 +1296,24 @@ bigNatToAddrLE# a addr s0
-- zero bytes written.
bigNatToAddrBE# :: BigNat# -> Addr# -> State# s -> (# State# s, Word# #)
{-# NOINLINE bigNatToAddrBE# #-}
-bigNatToAddrBE# a addr s0
- | isTrue# (sz ==# 0#) = (# s0, 0## #)
+bigNatToAddrBE# a addr
+ | isTrue# (sz ==# 0#) = Ubx.pure 0##
| msw <- indexWordArray# a (sz -# 1#)
- = case wordToAddrBE# msw addr s0 of
- (# s1, k #) -> case go (sz -# 1#) (addr `plusAddr#` word2Int# k) s1 of
- s2 -> (# s2, k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
+ = Ubx.do
+ k <- wordToAddrBE# msw addr
+ go (sz -# 1#) (addr `plusAddr#` word2Int# k)
+ Ubx.pure (k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
where
sz = wordArraySize# a
- go i adr s
+ go i adr
| 0# <- i
- = s
+ = Ubx.nop
| w <- indexWordArray# a (i -# 1#)
- = case wordWriteAddrBE# w adr s of
- s' -> go (i -# 1#)
- (adr `plusAddr#` WORD_SIZE_IN_BYTES# ) s'
+ = Ubx.do
+ wordWriteAddrBE# w adr
+ go (i -# 1#) (adr `plusAddr#` WORD_SIZE_IN_BYTES# )
-- | Write a BigNat in base-256 representation and return the
@@ -1333,8 +1341,9 @@ bigNatToAddr# a addr _ s = bigNatToAddrBE# a addr s
-- written in advance. In case of @/i/ == 0@, the function will write and report
-- zero bytes written.
bigNatToAddr :: BigNat# -> Addr# -> Bool# -> IO Word
-bigNatToAddr a addr e = IO \s -> case bigNatToAddr# a addr e s of
- (# s', w #) -> (# s', W# w #)
+bigNatToAddr a addr e = IO Ubx.do
+ w <- bigNatToAddr# a addr e
+ Ubx.pure (W# w)
@@ -1349,34 +1358,37 @@ bigNatToAddr a addr e = IO \s -> case bigNatToAddr# a addr e s of
-- Higher limbs equal to 0 are automatically trimmed.
bigNatFromAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, BigNat# #)
{-# NOINLINE bigNatFromAddrLE# #-}
-bigNatFromAddrLE# 0## _ s = (# s, bigNatZero# (# #) #)
-bigNatFromAddrLE# sz addr s =
+bigNatFromAddrLE# 0## _ = Ubx.pure (bigNatZero# (# #))
+bigNatFromAddrLE# sz addr =
let
!nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
!nb = sz `and#` WORD_SIZE_BYTES_MASK##
- readMSB mwa s
+ readMSB mwa
| 0## <- nb
- = s
+ = Ubx.nop
| off <- word2Int# (nw `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#)
- = case wordFromAddrLE# nb (addr `plusAddr#` off) s of
- (# s, w #) -> mwaWrite# mwa (word2Int# nw) w s
+ = Ubx.do
+ w <- wordFromAddrLE# nb (addr `plusAddr#` off)
+ mwaWrite# mwa (word2Int# nw) w
- go mwa i s
+ go mwa i
| isTrue# (i ==# word2Int# nw)
- = s
+ = Ubx.nop
| off <- i `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#
- = case wordFromAddrLE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off) s of
- (# s, w #) -> case mwaWrite# mwa i w s of
- s -> go mwa (i +# 1#) s
-
- in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
- (# s, mwa #) -> case readMSB mwa s of
- s -> case go mwa 0# s of
- s -> case mwaTrimZeroes# mwa s of
- s -> unsafeFreezeByteArray# mwa s
+ = Ubx.do
+ w <- wordFromAddrLE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off)
+ mwaWrite# mwa i w
+ go mwa (i +# 1#)
+
+ in Ubx.do
+ mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
+ readMSB mwa
+ go mwa 0#
+ mwaTrimZeroes# mwa
+ unsafeFreezeByteArray# mwa
-- | Read a BigNat in base-256 big-endian representation from an Addr#.
--
@@ -1385,35 +1397,38 @@ bigNatFromAddrLE# sz addr s =
-- Null higher limbs are automatically trimmed.
bigNatFromAddrBE# :: Word# -> Addr# -> State# s -> (# State# s, BigNat# #)
{-# NOINLINE bigNatFromAddrBE# #-}
-bigNatFromAddrBE# 0## _ s = (# s, bigNatZero# (# #) #)
-bigNatFromAddrBE# sz addr s =
+bigNatFromAddrBE# 0## _ = Ubx.pure (bigNatZero# (# #))
+bigNatFromAddrBE# sz addr =
let
!nw = word2Int# (sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#)
!nb = sz `and#` WORD_SIZE_BYTES_MASK##
- goMSB mwa s
+ goMSB mwa
| 0## <- nb
- = s
+ = Ubx.nop
| True
- = case wordFromAddrBE# nb addr s of
- (# s, w #) -> mwaWrite# mwa nw w s
+ = Ubx.do
+ w <- wordFromAddrBE# nb addr
+ mwaWrite# mwa nw w
- go mwa i s
+ go mwa i
| isTrue# (i ==# nw)
- = s
+ = Ubx.nop
| k <- nw -# 1# -# i
, off <- (k `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#) +# word2Int# nb
- = case wordFromAddrBE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off) s of
- (# s, w #) -> case mwaWrite# mwa i w s of
- s -> go mwa (i +# 1#) s
-
- in case newWordArray# (nw +# (word2Int# nb /=# 0#)) s of
- (# s, mwa #) -> case goMSB mwa s of
- s -> case go mwa 0# s of
- s -> case mwaTrimZeroes# mwa s of
- s -> unsafeFreezeByteArray# mwa s
+ = Ubx.do
+ w <- wordFromAddrBE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off)
+ mwaWrite# mwa i w
+ go mwa (i +# 1#)
+
+ in Ubx.do
+ mwa <- newWordArray# (nw +# (word2Int# nb /=# 0#))
+ goMSB mwa
+ go mwa 0#
+ mwaTrimZeroes# mwa
+ unsafeFreezeByteArray# mwa
-- | Read a BigNat in base-256 representation from an Addr#.
--
@@ -1440,11 +1455,12 @@ bigNatFromAddr# sz addr _ s = bigNatFromAddrBE# sz addr s
-- zero bytes written.
bigNatToMutableByteArrayLE# :: BigNat# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
{-# NOINLINE bigNatToMutableByteArrayLE# #-}
-bigNatToMutableByteArrayLE# a mba moff s0
- | isTrue# (sz ==# 0#) = (# s0, 0## #)
- | True = case writeMSB s0 of
- (# s1, k #) -> case go 0# s1 of
- s2 -> (# s2, k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
+bigNatToMutableByteArrayLE# a mba moff
+ | isTrue# (sz ==# 0#) = Ubx.pure 0##
+ | True = Ubx.do
+ k <- writeMSB
+ go 0#
+ Ubx.pure (k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
where
!sz = wordArraySize# a
!li = sz -# 1#
@@ -1452,15 +1468,16 @@ bigNatToMutableByteArrayLE# a mba moff s0
writeMSB = wordToMutableByteArrayLE# (indexWordArray# a li)
mba (moff `plusWord#` int2Word# (li `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#))
- go i s
+ go i
| isTrue# (i <# li)
, off <- int2Word# i `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
, w <- indexWordArray# a i
- = case wordWriteMutableByteArrayLE# w mba (moff `plusWord#` off) s of
- s -> go (i +# 1#) s
+ = Ubx.do
+ wordWriteMutableByteArrayLE# w mba (moff `plusWord#` off)
+ go (i +# 1#)
| True
- = s
+ = Ubx.nop
-- | Write a BigNat in base-256 big-endian representation and return the
-- number of bytes written.
@@ -1470,23 +1487,24 @@ bigNatToMutableByteArrayLE# a mba moff s0
-- zero bytes written.
bigNatToMutableByteArrayBE# :: BigNat# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
{-# NOINLINE bigNatToMutableByteArrayBE# #-}
-bigNatToMutableByteArrayBE# a mba moff s0
- | isTrue# (sz ==# 0#) = (# s0, 0## #)
+bigNatToMutableByteArrayBE# a mba moff
+ | isTrue# (sz ==# 0#) = Ubx.pure 0##
| msw <- indexWordArray# a (sz -# 1#)
- = case wordToMutableByteArrayBE# msw mba moff s0 of
- (# s1, k #) -> case go (sz -# 1#) k s1 of
- s2 -> (# s2, k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
+ = Ubx.do
+ k <- wordToMutableByteArrayBE# msw mba moff
+ go (sz -# 1#) k
+ Ubx.pure (k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
where
sz = wordArraySize# a
- go i c s
+ go i c
| 0# <- i
- = s
+ = Ubx.nop
| w <- indexWordArray# a (i -# 1#)
- = case wordWriteMutableByteArrayBE# w mba (moff `plusWord#` c) s of
- s' -> go (i -# 1#)
- (c `plusWord#` WORD_SIZE_IN_BYTES## ) s'
+ = Ubx.do
+ wordWriteMutableByteArrayBE# w mba (moff `plusWord#` c)
+ go (i -# 1#) (c `plusWord#` WORD_SIZE_IN_BYTES## )
-- | Write a BigNat in base-256 representation and return the
@@ -1514,34 +1532,36 @@ bigNatToMutableByteArray# a mba off _ s = bigNatToMutableByteArrayBE# a mba off
-- Null higher limbs are automatically trimmed.
bigNatFromByteArrayLE# :: Word# -> ByteArray# -> Word# -> State# s -> (# State# s, BigNat# #)
{-# NOINLINE bigNatFromByteArrayLE# #-}
-bigNatFromByteArrayLE# 0## _ _ s = (# s, bigNatZero# (# #) #)
-bigNatFromByteArrayLE# sz ba moff s =
+bigNatFromByteArrayLE# 0## _ _ = Ubx.pure (bigNatZero# (# #))
+bigNatFromByteArrayLE# sz ba moff =
let
!nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
!nb = sz `and#` WORD_SIZE_BYTES_MASK##
- readMSB mwa s
+ readMSB mwa
| 0## <- nb
- = s
+ = Ubx.nop
| off <- nw `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
= case wordFromByteArrayLE# nb ba (moff `plusWord#` off) of
- w -> mwaWrite# mwa (word2Int# nw) w s
+ w -> mwaWrite# mwa (word2Int# nw) w
- go mwa i s
+ go mwa i
| isTrue# (i `eqWord#` nw)
- = s
+ = Ubx.nop
| off <- i `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
= case wordFromByteArrayLE# WORD_SIZE_IN_BYTES## ba (moff `plusWord#` off) of
- w -> case mwaWrite# mwa (word2Int# i) w s of
- s -> go mwa (i `plusWord#` 1##) s
+ w -> Ubx.do
+ mwaWrite# mwa (word2Int# i) w
+ go mwa (i `plusWord#` 1##)
- in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
- (# s, mwa #) -> case readMSB mwa s of
- s -> case go mwa 0## s of
- s -> case mwaTrimZeroes# mwa s of
- s -> unsafeFreezeByteArray# mwa s
+ in Ubx.do
+ mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
+ readMSB mwa
+ go mwa 0##
+ mwaTrimZeroes# mwa
+ unsafeFreezeByteArray# mwa
-- | Read a BigNat in base-256 big-endian representation from a ByteArray#.
--
@@ -1550,35 +1570,37 @@ bigNatFromByteArrayLE# sz ba moff s =
-- Null higher limbs are automatically trimmed.
bigNatFromByteArrayBE# :: Word# -> ByteArray# -> Word# -> State# s -> (# State# s, BigNat# #)
{-# NOINLINE bigNatFromByteArrayBE# #-}
-bigNatFromByteArrayBE# 0## _ _ s = (# s, bigNatZero# (# #) #)
-bigNatFromByteArrayBE# sz ba moff s =
+bigNatFromByteArrayBE# 0## _ _ = Ubx.pure (bigNatZero# (# #))
+bigNatFromByteArrayBE# sz ba moff =
let
!nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
!nb = sz `and#` WORD_SIZE_BYTES_MASK##
- goMSB mwa s
+ goMSB mwa
| 0## <- nb
- = s
+ = Ubx.nop
| True
= case wordFromByteArrayBE# nb ba moff of
- w -> mwaWrite# mwa (word2Int# nw) w s
+ w -> mwaWrite# mwa (word2Int# nw) w
- go mwa i s
+ go mwa i
| isTrue# (i `eqWord#` nw)
- = s
+ = Ubx.nop
| k <- nw `minusWord#` 1## `minusWord#` i
, off <- (k `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) `plusWord#` nb
= case wordFromByteArrayBE# WORD_SIZE_IN_BYTES## ba (moff `plusWord#` off) of
- w -> case mwaWrite# mwa (word2Int# i) w s of
- s -> go mwa (i `plusWord#` 1##) s
-
- in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
- (# s, mwa #) -> case goMSB mwa s of
- s -> case go mwa 0## s of
- s -> case mwaTrimZeroes# mwa s of
- s -> unsafeFreezeByteArray# mwa s
+ w -> Ubx.do
+ mwaWrite# mwa (word2Int# i) w
+ go mwa (i `plusWord#` 1##)
+
+ in Ubx.do
+ mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
+ goMSB mwa
+ go mwa 0##
+ mwaTrimZeroes# mwa
+ unsafeFreezeByteArray# mwa
-- | Read a BigNat in base-256 representation from a ByteArray#.
--
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/Integer.hs
=====================================
@@ -7,6 +7,7 @@
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE QualifiedDo #-}
{-# OPTIONS_GHC -fdefines-known-key-names #-}
-- Defines lots of functions that have BuiltinRules
@@ -166,6 +167,7 @@ import GHC.Internal.Bignum.Primitives
import GHC.Internal.Bignum.BigNat
import GHC.Internal.Bignum.Natural
import qualified GHC.Internal.Bignum.Backend as Backend
+import qualified GHC.Internal.Unboxed.Do as Ubx
-- | Arbitrary precision integers. In contrast with fixed-size integral types
-- such as 'Int', the 'Integer' type represents the entire infinite range of
@@ -1334,8 +1336,9 @@ integerToAddr# (IN n) = bigNatToAddr# n
-- byte first (big-endian) if @1#@ or least significant byte first
-- (little-endian) if @0#@.
integerToAddr :: Integer -> Addr# -> Bool# -> IO Word
-integerToAddr a addr e = IO \s -> case integerToAddr# a addr e s of
- (# s', w #) -> (# s', W# w #)
+integerToAddr a addr e = IO Ubx.do
+ w <- integerToAddr# a addr e
+ Ubx.pure (W# w)
-- | Read an 'Integer' (without sign) in base-256 representation from an Addr#.
--
@@ -1347,9 +1350,9 @@ integerToAddr a addr e = IO \s -> case integerToAddr# a addr e s of
--
-- Null higher limbs are automatically trimed.
integerFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Integer #)
-integerFromAddr# sz addr e s =
- case bigNatFromAddr# sz addr e s of
- (# s', n #) -> (# s', integerFromBigNat# n #)
+integerFromAddr# sz addr e = Ubx.do
+ n <- bigNatFromAddr# sz addr e
+ Ubx.pure (integerFromBigNat# n)
-- | Read an 'Integer' (without sign) in base-256 representation from an Addr#.
--
@@ -1383,8 +1386,9 @@ integerToMutableByteArray# (IN a) = bigNatToMutableByteArray# a
-- byte first (big-endian) if @1#@ or least significant byte first
-- (little-endian) if @0#@.
integerToMutableByteArray :: Integer -> MutableByteArray# RealWorld -> Word# -> Bool# -> IO Word
-integerToMutableByteArray i mba w e = IO \s -> case integerToMutableByteArray# i mba w e s of
- (# s', r #) -> (# s', W# r #)
+integerToMutableByteArray i mba w e = IO Ubx.do
+ r <- integerToMutableByteArray# i mba w e
+ Ubx.pure (W# r)
-- | Read an 'Integer' (without sign) in base-256 representation from a ByteArray#.
--
@@ -1396,8 +1400,9 @@ integerToMutableByteArray i mba w e = IO \s -> case integerToMutableByteArray# i
--
-- Null higher limbs are automatically trimed.
integerFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Integer #)
-integerFromByteArray# sz ba off e s = case bigNatFromByteArray# sz ba off e s of
- (# s', a #) -> (# s', integerFromBigNat# a #)
+integerFromByteArray# sz ba off e = Ubx.do
+ a <- bigNatFromByteArray# sz ba off e
+ Ubx.pure (integerFromBigNat# a)
-- | Read an 'Integer' (without sign) in base-256 representation from a ByteArray#.
--
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/Natural.hs
=====================================
@@ -4,6 +4,7 @@
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
+{-# LANGUAGE QualifiedDo #-}
{-# OPTIONS_GHC -fdefines-known-key-names #-}
-- Defines lots of functions that have BuiltinRules
@@ -121,6 +122,7 @@ import GHC.Internal.Classes
import GHC.Internal.Bignum.BigNat
import GHC.Internal.Bignum.Primitives
+import qualified GHC.Internal.Unboxed.Do as Ubx
-- | Natural number
--
@@ -656,8 +658,9 @@ naturalToAddr# (NB n) = bigNatToAddr# n
-- byte first (big-endian) if @1#@ or least significant byte first
-- (little-endian) if @0#@.
naturalToAddr :: Natural -> Addr# -> Bool# -> IO Word
-naturalToAddr a addr e = IO \s -> case naturalToAddr# a addr e s of
- (# s', w #) -> (# s', W# w #)
+naturalToAddr a addr e = IO Ubx.do
+ w <- naturalToAddr# a addr e
+ Ubx.pure (W# w)
-- | Read a Natural in base-256 representation from an Addr#.
@@ -670,9 +673,9 @@ naturalToAddr a addr e = IO \s -> case naturalToAddr# a addr e s of
--
-- Null higher limbs are automatically trimed.
naturalFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Natural #)
-naturalFromAddr# sz addr e s =
- case bigNatFromAddr# sz addr e s of
- (# s', n #) -> (# s', naturalFromBigNat# n #)
+naturalFromAddr# sz addr e = Ubx.do
+ n <- bigNatFromAddr# sz addr e
+ Ubx.pure (naturalFromBigNat# n)
-- | Read a Natural in base-256 representation from an Addr#.
--
@@ -707,8 +710,9 @@ naturalToMutableByteArray# (NB a) = bigNatToMutableByteArray# a
--
-- Null higher limbs are automatically trimed.
naturalFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Natural #)
-naturalFromByteArray# sz ba off e s = case bigNatFromByteArray# sz ba off e s of
- (# s', a #) -> (# s', naturalFromBigNat# a #)
+naturalFromByteArray# sz ba off e = Ubx.do
+ a <- bigNatFromByteArray# sz ba off e
+ Ubx.pure (naturalFromBigNat# a)
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/Primitives.hs
=====================================
@@ -11,6 +11,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE BinaryLiterals #-}
+{-# LANGUAGE QualifiedDo #-}
{-# OPTIONS_GHC -fexpose-all-unfoldings #-}
module GHC.Internal.Bignum.Primitives
@@ -93,6 +94,7 @@ import GHC.Internal.Prim.Exception
import GHC.Internal.Prim
import GHC.Internal.Types
+import qualified GHC.Internal.Unboxed.Do as Ubx
----------------------------------
-- Bool#
@@ -322,13 +324,14 @@ wordReverseBits32# x0 = wordReverseBits# x0
wordToAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
wordToAddrLE# x addr = go x 0#
where
- go w c s
+ go w c
| 0## <- w
- = (# s, int2Word# c #)
+ = Ubx.pure (int2Word# c)
| True
- = case writeWord8OffAddr# addr c (wordToWord8# w) s of
- s' -> go (w `uncheckedShiftRL#` 8#) (c +# 1#) s'
+ = Ubx.do
+ writeWord8OffAddr# addr c (wordToWord8# w)
+ go (w `uncheckedShiftRL#` 8#) (c +# 1#)
-- | Write a Word to @/addr/@ in base-256 big-endian representation and
-- return the number of bytes written.
@@ -337,14 +340,15 @@ wordToAddrBE# w addr = go 0# (WORD_SIZE_IN_BITS# -# clz)
where
!clz = word2Int# (clz# w `and#` (not# 0b0111##)) -- keep complete bytes
- go c sh s
+ go c sh
| 0# <- sh
- = (# s, int2Word# c #)
+ = Ubx.pure (int2Word# c)
| True
, w' <- wordToWord8# (w `uncheckedShiftRL#` (sh -# 8#))
- = case writeWord8OffAddr# addr c w' s of
- s' -> go (c +# 1#) (sh -# 8#) s'
+ = Ubx.do
+ writeWord8OffAddr# addr c w'
+ go (c +# 1#) (sh -# 8#)
-- | Write a Word to @/addr/@ in base-256 representation and
-- return the number of bytes written.
@@ -361,53 +365,55 @@ wordToAddr# a addr _ s = wordToAddrBE# a addr s
--
-- @'n' is the number of bytes to read.
wordFromAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
-wordFromAddrLE# n addr s
+wordFromAddrLE# n addr
-- Optimize when we read a full word
| WORD_SIZE_IN_BYTES## <- n
- = case readWordOffAddr# addr 0# s of
+ = Ubx.do
+ w <- readWordOffAddr# addr 0#
#if defined(WORDS_BIGENDIAN)
- (# s', w #) -> (# s', wordReverseBytes# w #)
+ Ubx.pure (wordReverseBytes# w)
#else
- (# s', w #) -> (# s', w #)
+ Ubx.pure w
#endif
-wordFromAddrLE# n addr s0 = go 0## 0# s0
+wordFromAddrLE# n addr = go 0## 0#
where
- go w c s
+ go w c
| isTrue# (c ==# word2Int# n)
- = (# s, w #)
+ = Ubx.pure w
| True
- = case readWord8OffAddr# addr c s of
- (# s', b #) -> go (w `or#` (word8ToWord# b `uncheckedShiftL#` (c `uncheckedIShiftL#` 3#)))
- (c +# 1#)
- s'
+ = Ubx.do
+ b <- readWord8OffAddr# addr c
+ go (w `or#` (word8ToWord# b `uncheckedShiftL#` (c `uncheckedIShiftL#` 3#)))
+ (c +# 1#)
-- | Read a Word from @/addr/@ in base-256 big-endian representation.
--
-- @'n' is the number of bytes to read.
wordFromAddrBE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
-wordFromAddrBE# n addr s
+wordFromAddrBE# n addr
-- Optimize when we read a full word
| WORD_SIZE_IN_BYTES## <- n
- = case readWordOffAddr# addr 0# s of
+ = Ubx.do
+ w <- readWordOffAddr# addr 0#
#if defined(WORDS_BIGENDIAN)
- (# s', w #) -> (# s', w #)
+ Ubx.pure w
#else
- (# s', w #) -> (# s', wordReverseBytes# w #)
+ Ubx.pure (wordReverseBytes# w)
#endif
-wordFromAddrBE# n addr s0 = go 0## 0# s0
+wordFromAddrBE# n addr = go 0## 0#
where
- go w c s
+ go w c
| isTrue# (c ==# word2Int# n)
- = (# s, w #)
+ = Ubx.pure w
| True
- = case readWord8OffAddr# addr c s of
- (# s', b #) -> go ((w `uncheckedShiftL#` 8#) `or#` word8ToWord# b)
- (c +# 1#)
- s'
+ = Ubx.do
+ b <- readWord8OffAddr# addr c
+ go ((w `uncheckedShiftL#` 8#) `or#` word8ToWord# b)
+ (c +# 1#)
-- | Read a Word from @/addr/@ in base-256 representation.
--
@@ -447,13 +453,14 @@ wordWriteAddrBE# w addr = writeWordOffAddr# addr 0#
wordToMutableByteArrayLE# :: Word# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
wordToMutableByteArrayLE# x mba off = go x 0#
where
- go w c s
+ go w c
| 0## <- w
- = (# s, int2Word# c #)
+ = Ubx.pure (int2Word# c)
| True
- = case writeWord8Array# mba (word2Int# off +# c) (wordToWord8# w) s of
- s' -> go (w `uncheckedShiftRL#` 8#) (c +# 1#) s'
+ = Ubx.do
+ writeWord8Array# mba (word2Int# off +# c) (wordToWord8# w)
+ go (w `uncheckedShiftRL#` 8#) (c +# 1#)
-- | Write a Word to @/MutableByteArray/@ in base-256 big-endian representation and
-- return the number of bytes written.
@@ -464,14 +471,15 @@ wordToMutableByteArrayBE# w mba off = go 0# (WORD_SIZE_IN_BITS# -# clz)
where
!clz = word2Int# (clz# w `and#` (not# 0b0111##)) -- keep complete bytes
- go c sh s
+ go c sh
| 0# <- sh
- = (# s, int2Word# c #)
+ = Ubx.pure (int2Word# c)
| True
, w' <- wordToWord8# (w `uncheckedShiftRL#` (sh -# 8#))
- = case writeWord8Array# mba (word2Int# off +# c) w' s of
- s' -> go (c +# 1#) (sh -# 8#) s'
+ = Ubx.do
+ writeWord8Array# mba (word2Int# off +# c) w'
+ go (c +# 1#) (sh -# 8#)
-- | Write a Word to @/MutableByteArray/@ in base-256 representation and
-- return the number of bytes written.
@@ -568,21 +576,24 @@ wordFromByteArray# a ba off _ = wordFromByteArrayBE# a ba off
----------------------------------
ioVoid :: IO a -> State# RealWorld -> State# RealWorld
-ioVoid (IO io) s = case io s of
- (# s', _ #) -> s'
+ioVoid (IO io) = Ubx.void io
ioWord# :: IO Word -> State# RealWorld -> (# State# RealWorld, Word# #)
-ioWord# (IO io) s = case io s of
- (# s', W# w #) -> (# s', w #)
+ioWord# (IO io) = Ubx.do
+ W# w <- io
+ Ubx.pure w
ioInt# :: IO Int -> State# RealWorld -> (# State# RealWorld, Int# #)
-ioInt# (IO io) s = case io s of
- (# s', I# i #) -> (# s', i #)
+ioInt# (IO io) = Ubx.do
+ I# i <- io
+ Ubx.pure i
ioBool :: IO Bool -> State# RealWorld -> (# State# RealWorld, Bool# #)
-ioBool (IO io) s = case io s of
- (# s', False #) -> (# s', 0# #)
- (# s', True #) -> (# s', 1# #)
+ioBool (IO io) = Ubx.do
+ b <- io
+ case b of
+ False -> Ubx.pure 0#
+ True -> Ubx.pure 1#
----------------------------------
=====================================
libraries/ghc-internal/src/GHC/Internal/Bignum/WordArray.hs
=====================================
@@ -8,7 +8,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE KindSignatures #-}
-{-# OPTIONS_GHC -Wno-name-shadowing #-}
+{-# LANGUAGE QualifiedDo #-}
module GHC.Internal.Bignum.WordArray where
@@ -16,6 +16,7 @@ import GHC.Internal.Prim
import GHC.Internal.Magic
import GHC.Internal.Types
import GHC.Internal.Bignum.Primitives
+import qualified GHC.Internal.Unboxed.Do as Ubx
#include "MachDeps.h"
#include "WordSize.h"
@@ -44,11 +45,10 @@ withNewWordArray#
-> WordArray#
withNewWordArray# sz act = case runRW# io of (# _, a #) -> a
where
- io s =
- case newWordArray# sz s of { (# s, mwa #) ->
- case act mwa s of { s ->
- unsafeFreezeByteArray# mwa s
- }}
+ io = Ubx.do
+ mwa <- newWordArray# sz
+ act mwa
+ unsafeFreezeByteArray# mwa
-- | Create two new WordArray# of the given sizes (*in Word#*) and apply the
-- action to them before returning them frozen
@@ -62,14 +62,13 @@ withNewWordArray2#
-> (# WordArray#, WordArray# #)
withNewWordArray2# sz1 sz2 act = case runRW# io of (# _, a #) -> a
where
- io s =
- case newWordArray# sz1 s of { (# s, mwa1 #) ->
- case newWordArray# sz2 s of { (# s, mwa2 #) ->
- case act mwa1 mwa2 s of { s ->
- case unsafeFreezeByteArray# mwa1 s of { (# s, wa1 #) ->
- case unsafeFreezeByteArray# mwa2 s of { (# s, wa2 #) ->
- (# s, (# wa1, wa2 #) #)
- }}}}}
+ io = Ubx.do
+ mwa1 <- newWordArray# sz1
+ mwa2 <- newWordArray# sz2
+ act mwa1 mwa2
+ wa1 <- unsafeFreezeByteArray# mwa1
+ wa2 <- unsafeFreezeByteArray# mwa2
+ Ubx.pure (# wa1, wa2 #)
-- | Create a new WordArray#
newWordArray# :: Int# -> State# s -> (# State# s, MutableWordArray# s #)
@@ -81,9 +80,9 @@ withNewWordArrayTrimmed#
:: Int# -- ^ Size in Word
-> (MutableWordArray# RealWorld -> State# RealWorld -> State# RealWorld)
-> WordArray#
-withNewWordArrayTrimmed# sz act = withNewWordArray# sz \mwa s ->
- case act mwa s of
- s' -> mwaTrimZeroes# mwa s'
+withNewWordArrayTrimmed# sz act = withNewWordArray# sz \mwa -> Ubx.do
+ act mwa
+ mwaTrimZeroes# mwa
-- | Create two new WordArray# of the given sizes (*in Word#*), apply the action
-- to them, trim their most significant zeroes, then return them frozen
@@ -95,10 +94,10 @@ withNewWordArray2Trimmed#
-> State# RealWorld
-> State# RealWorld)
-> (# WordArray#, WordArray# #)
-withNewWordArray2Trimmed# sz1 sz2 act = withNewWordArray2# sz1 sz2 \mwa1 mwa2 s ->
- case act mwa1 mwa2 s of
- s' -> case mwaTrimZeroes# mwa1 s' of
- s'' -> mwaTrimZeroes# mwa2 s''
+withNewWordArray2Trimmed# sz1 sz2 act = withNewWordArray2# sz1 sz2 \mwa1 mwa2 -> Ubx.do
+ act mwa1 mwa2
+ mwaTrimZeroes# mwa1
+ mwaTrimZeroes# mwa2
-- | Create a new WordArray# of the given size (*in Word#*), apply the action to
-- it. If the action returns true#, trim its most significant zeroes, then
@@ -109,13 +108,15 @@ withNewWordArrayTrimmedMaybe#
-> (# (# #) | WordArray# #)
withNewWordArrayTrimmedMaybe# sz act = case runRW# io of (# _, a #) -> a
where
- io s =
- case newWordArray# sz s of
- (# s, mwa #) -> case act mwa s of
- (# s, 0# #) -> (# s, (# (# #) | #) #)
- (# s, _ #) -> case mwaTrimZeroes# mwa s of
- s -> case unsafeFreezeByteArray# mwa s of
- (# s, ba #) -> (# s, (# | ba #) #)
+ io = Ubx.do
+ mwa <- newWordArray# sz
+ b <- act mwa
+ case b of
+ 0# -> Ubx.pure (# (# #) | #)
+ _ -> Ubx.do
+ mwaTrimZeroes# mwa
+ ba <- unsafeFreezeByteArray# mwa
+ Ubx.pure (# | ba #)
-- | Create a WordArray# from two Word#
--
@@ -124,9 +125,9 @@ withNewWordArrayTrimmedMaybe# sz act = case runRW# io of (# _, a #) -> a
-- l is the least significant word
wordArrayFromWord2# :: Word# -> Word# -> WordArray#
wordArrayFromWord2# h l =
- withNewWordArray# 2# \mwa s ->
- case mwaWrite# mwa 0# l s of
- s -> mwaWrite# mwa 1# h s
+ withNewWordArray# 2# \mwa -> Ubx.do
+ mwaWrite# mwa 0# l
+ mwaWrite# mwa 1# h
-- | Create a WordArray# from one Word#
wordArrayFromWord# :: Word# -> WordArray#
@@ -143,8 +144,9 @@ wordArraySize# ba = bytesToWords# (sizeofByteArray# ba)
-- | Get size in Words
mwaSize# :: MutableWordArray# s-> State# s -> (# State# s, Int# #)
-mwaSize# mba s = case getSizeofMutableByteArray# mba s of
- (# s2, sz #) -> (# s2, bytesToWords# sz #)
+mwaSize# mba = Ubx.do
+ sz <- getSizeofMutableByteArray# mba
+ Ubx.pure (bytesToWords# sz)
-- | Get the last Word (must be non empty!)
wordArrayLast# :: WordArray# -> Word#
@@ -163,10 +165,10 @@ mwaArrayCopy# dst dstIdx src srcIdx n s
-- | Shrink last words of a WordArray
mwaShrink# :: MutableByteArray# s -> Int# -> State# s -> State# s
-mwaShrink# _mwa 0# s = s
-mwaShrink# mwa i s =
- case mwaSize# mwa s of
- (# s, n #) -> shrinkMutableByteArray# mwa (wordsToBytes# (n -# i)) s
+mwaShrink# _mwa 0# = Ubx.nop
+mwaShrink# mwa i = Ubx.do
+ n <- mwaSize# mwa
+ shrinkMutableByteArray# mwa (wordsToBytes# (n -# i))
-- | Set size
mwaSetSize# :: MutableByteArray# s -> Int# -> State# s -> State# s
@@ -175,31 +177,35 @@ mwaSetSize# mwa n s = shrinkMutableByteArray# mwa (wordsToBytes# n) s
-- | Copy the WordArray into the MWA and shrink the size of MWA to the one of
-- the WordArray
mwaInitCopyShrink# :: MutableByteArray# s -> WordArray# -> State# s -> State# s
-mwaInitCopyShrink# mwa wa s =
- case mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa) s of
- s -> mwaSetSize# mwa (wordArraySize# wa) s
+mwaInitCopyShrink# mwa wa = Ubx.do
+ mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa)
+ mwaSetSize# mwa (wordArraySize# wa)
-- | Trim ending zeroes
mwaTrimZeroes# :: MutableByteArray# s -> State# s -> State# s
-mwaTrimZeroes# mwa s1 =
- case mwaClz mwa s1 of
- (# s2, 0# #) -> s2
- (# s2, c #) -> mwaShrink# mwa c s2
+mwaTrimZeroes# mwa = Ubx.do
+ c <- mwaClz mwa
+ case c of
+ 0# -> Ubx.nop
+ _ -> mwaShrink# mwa c
-- | Count leading zero Words
mwaClz :: MutableWordArray# s -> State# s -> (# State# s, Int# #)
-mwaClz mwa s1 = case mwaSize# mwa s1 of
- (# s2,sz #) -> mwaClzAt mwa (sz -# 1#) s2
+mwaClz mwa = Ubx.do
+ sz <- mwaSize# mwa
+ mwaClzAt mwa (sz -# 1#)
-- | Count leading zero Words starting at given position
mwaClzAt :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Int# #)
mwaClzAt mwa = go 0#
where
- go c i s
- | isTrue# (i <# 0#) = (# s, c #)
- | True = case readWordArray# mwa i s of
- (# s', 0## #) -> go (c +# 1#) (i -# 1#) s'
- (# s', _ #) -> (# s', c #)
+ go c i
+ | isTrue# (i <# 0#) = Ubx.pure c
+ | True = Ubx.do
+ w <- readWordArray# mwa i
+ case w of
+ 0## -> go (c +# 1#) (i -# 1#)
+ _ -> Ubx.pure c
-- | Count leading zero Words starting at given position
waClzAt :: WordArray# -> Int# -> Int#
@@ -258,15 +264,16 @@ mwaInitArrayPlusWord :: MutableWordArray# s -> WordArray# -> Word# -> State# s -
mwaInitArrayPlusWord mwa wa = go 0#
where
sz = wordArraySize# wa
- go i carry s
- | isTrue# (i ># sz) = s
- | isTrue# (i ==# sz) = mwaWriteOrShrink mwa carry i s
- | 0## <- carry = -- copy higher remaining words and shrink the mwa
- case mwaArrayCopy# mwa i wa i (sz -# i) s of
- s2 -> mwaShrink# mwa 1# s2
- | True = let !(# l,c #) = addWordC# (indexWordArray# wa i) carry
- in case mwaWrite# mwa i l s of
- s2 -> go (i +# 1#) (int2Word# c) s2
+ go i carry
+ | isTrue# (i ># sz) = Ubx.nop
+ | isTrue# (i ==# sz) = mwaWriteOrShrink mwa carry i
+ | 0## <- carry = Ubx.do -- copy higher remaining words and shrink the mwa
+ mwaArrayCopy# mwa i wa i (sz -# i)
+ mwaShrink# mwa 1#
+ | True = Ubx.do
+ let !(# l, c #) = addWordC# (indexWordArray# wa i) carry
+ mwaWrite# mwa i l
+ go (i +# 1#) (int2Word# c)
-- | Write the most-significant Word:
-- * if it is 0: shrink the array of 1 Word
@@ -277,23 +284,24 @@ mwaWriteOrShrink mwa w i s = mwaWrite# mwa i w s
-- | Compute the index of the most-significant Word and write it.
mwaWriteMostSignificant :: MutableWordArray# s -> Word# -> State# s -> State# s
-mwaWriteMostSignificant mwa w s =
- case mwaSize# mwa s of
- (# s', sz #) -> mwaWriteOrShrink mwa w (sz -# 1#) s'
+mwaWriteMostSignificant mwa w = Ubx.do
+ sz <- mwaSize# mwa
+ mwaWriteOrShrink mwa w (sz -# 1#)
-- | MutableWordArray <- zipWith op wa1 wa2
--
-- Required output: Size(MutableWordArray) = min Size(wa1) Size(wa2)
mwaInitArrayBinOp :: MutableWordArray# s -> WordArray# -> WordArray# -> (Word# -> Word# -> Word#) -> State# s -> State#s
-mwaInitArrayBinOp mwa wa wb op s = go 0# s
+mwaInitArrayBinOp mwa wa wb op = go 0#
where
!sz = minI# (wordArraySize# wa) (wordArraySize# wb)
- go i s'
- | isTrue# (i ==# sz) = s'
+ go i
+ | isTrue# (i ==# sz) = Ubx.nop
| True =
case indexWordArray# wa i `op` indexWordArray# wb i of
- v -> case mwaWrite# mwa i v s' of
- s'' -> go (i +# 1#) s''
+ v -> Ubx.do
+ mwaWrite# mwa i v
+ go (i +# 1#)
-- | Write an element of the MutableWordArray
mwaWrite# :: MutableWordArray# s -> Int# -> Word# -> State# s -> State# s
@@ -301,17 +309,19 @@ mwaWrite# = writeWordArray#
-- | Fill some part of a MutableWordArray with the given Word#
mwaFill# :: MutableWordArray# s -> Word# -> Word# -> Word# -> State# s -> State# s
-mwaFill# _ _ _ 0## s = s
-mwaFill# mwa v off n s = case mwaWrite# mwa (word2Int# off) v s of
- s' -> mwaFill# mwa v (off `plusWord#` 1##) (n `minusWord#` 1##) s'
+mwaFill# _ _ _ 0## = Ubx.nop
+mwaFill# mwa v off n = Ubx.do
+ mwaWrite# mwa (word2Int# off) v
+ mwaFill# mwa v (off `plusWord#` 1##) (n `minusWord#` 1##)
-- | Add Word# inplace (a the specified offset) in the mwa with carry propagation.
mwaAddInplaceWord# :: MutableWordArray# d -> Int# -> Word# -> State# d -> State# d
-mwaAddInplaceWord# _ _ 0## s = s
-mwaAddInplaceWord# mwa i y s = case readWordArray# mwa i s of
- (# s1, x #) -> let !(# h,l #) = plusWord2# x y
- in case mwaWrite# mwa i l s1 of
- s2 -> mwaAddInplaceWord# mwa (i +# 1#) h s2
+mwaAddInplaceWord# _ _ 0## = Ubx.nop
+mwaAddInplaceWord# mwa i y = Ubx.do
+ x <- readWordArray# mwa i
+ let !(# h, l #) = plusWord2# x y
+ mwaWrite# mwa i l
+ mwaAddInplaceWord# mwa (i +# 1#) h
-- | Sub Word# inplace (at the specified offset) in the mwa with carry
-- propagation.
@@ -323,43 +333,44 @@ mwaSubInplaceWord#
-> Word#
-> State# d
-> (# State# d, Bool# #)
-mwaSubInplaceWord# mwa ii iw s1 = case mwaSize# mwa s1 of
- (# is, sz #) ->
- let
- go _ 0## s = (# s, 1# #) -- no underflow
- go i y s
- | isTrue# (i >=# sz) = (# s, 0# #) -- underflow
- | True = case readWordArray# mwa i s of
- (# s1, x #) -> let !(# l,h #) = subWordC# x y
- in case mwaWrite# mwa i l s1 of
- s2 -> go (i +# 1#) (int2Word# h) s2
- in go ii iw is
+mwaSubInplaceWord# mwa ii iw = Ubx.do
+ sz <- mwaSize# mwa
+ let
+ go _ 0## = Ubx.pure 1# -- no underflow
+ go i y
+ | isTrue# (i >=# sz) = Ubx.pure 0# -- underflow
+ | True = Ubx.do
+ x <- readWordArray# mwa i
+ let !(# l, h #) = subWordC# x y
+ mwaWrite# mwa i l
+ go (i +# 1#) (int2Word# h)
+ go ii iw
-- | Trim `a` of `k` less significant limbs and then compare the result with `b`
--
-- "mwa" doesn't need to be trimmed
mwaTrimCompare :: Int# -> MutableWordArray# s -> WordArray# -> State# s -> (# State# s, Ordering #)
-mwaTrimCompare k mwa wb s1
- | (# s, szA #) <- mwaSize# mwa s1
- , szB <- wordArraySize# wb
- =
- let
- go i s
- | isTrue# (i <# 0#) = (# s, EQ #)
- | True = case readWordArray# mwa (i +# k) s of
- (# s2, ai #) ->
- let bi = if isTrue# (i >=# szB)
- then 0##
- else indexWordArray# wb i
- in if | isTrue# (ai `gtWord#` bi) -> (# s2, GT #)
- | isTrue# (bi `gtWord#` ai) -> (# s2, LT #)
- | True -> go (i -# 1#) s2
+mwaTrimCompare k mwa wb = Ubx.do
+ szA <- mwaSize# mwa
+ let
+ szB = wordArraySize# wb
+
+ go i
+ | isTrue# (i <# 0#) = Ubx.pure EQ
+ | True = Ubx.do
+ ai <- readWordArray# mwa (i +# k)
+ let bi = if isTrue# (i >=# szB)
+ then 0##
+ else indexWordArray# wb i
+ if | isTrue# (ai `gtWord#` bi) -> Ubx.pure GT
+ | isTrue# (bi `gtWord#` ai) -> Ubx.pure LT
+ | True -> go (i -# 1#)
szTrimA = szA -# k
- in if | isTrue# (szTrimA <# szB) -> (# s, LT #)
- | True -> go (szA -# k -# 1#) s
+ if | isTrue# (szTrimA <# szB) -> Ubx.pure LT
+ | True -> go (szA -# k -# 1#)
-- | Sub array inplace (at the specified offset) in the mwa with carry propagation.
@@ -370,12 +381,13 @@ mwaTrimCompare k mwa wb s1
mwaSubInplaceArray :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> (# State# d, Bool# #)
mwaSubInplaceArray mwa off wb = go (wordArraySize# wb -# 1#)
where
- go i s
- | isTrue# (i <# 0#) = (# s, 1# #) -- no underflow
- | True
- = case mwaSubInplaceWord# mwa (off +# i) (indexWordArray# wb i) s of
- (# s2, 1# #) -> go (i -# 1#) s2
- (# s2, _ #) -> (# s2, 0# #) -- underflow
+ go i
+ | isTrue# (i <# 0#) = Ubx.pure 1# -- no underflow
+ | True = Ubx.do
+ b <- mwaSubInplaceWord# mwa (off +# i) (indexWordArray# wb i)
+ case b of
+ 1# -> go (i -# 1#)
+ _ -> Ubx.pure 0# -- underflow
-- | Add array inplace (a the specified offset) in the mwa with carry propagation.
--
@@ -384,13 +396,13 @@ mwaAddInplaceArray :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> S
mwaAddInplaceArray mwa off wb = go 0# 0##
where
!maxi = wordArraySize# wb
- go i c s
- | isTrue# (i ==# maxi) = mwaAddInplaceWord# mwa (i +# off) c s
- | True
- = case readWordArray# mwa (i +# off) s of
- (# s, v #) -> case plusWord3# v (indexWordArray# wb i) c of
- (# c', v' #) -> case writeWordArray# mwa (i +# off) v' s of
- s -> go (i +# 1#) c' s
+ go i c
+ | isTrue# (i ==# maxi) = mwaAddInplaceWord# mwa (i +# off) c
+ | True = Ubx.do
+ v <- readWordArray# mwa (i +# off)
+ let !(# c', v' #) = plusWord3# v (indexWordArray# wb i) c
+ writeWordArray# mwa (i +# off) v'
+ go (i +# 1#) c'
-- | Sub array inplace (at the specified offset) in the mwa with carry propagation.
--
@@ -398,35 +410,36 @@ mwaAddInplaceArray mwa off wb = go 0# 0##
--
-- Return False# on underflow.
mwaSubInplaceMutableArray :: MutableWordArray# d -> Int# -> MutableWordArray# d -> State# d -> (# State# d, Bool# #)
-mwaSubInplaceMutableArray mwa off mwb s0 =
- case mwaSize# mwb s0 of
- (# s1, szB #) -> go (szB -# 1#) s1
+mwaSubInplaceMutableArray mwa off mwb = Ubx.do
+ szB <- mwaSize# mwb
+ go (szB -# 1#)
where
- go i s
- | isTrue# (i <# 0#) = (# s, 1# #) -- no underflow
- | True
- = case readWordArray# mwb i s of
- (# s1, bi #) -> case mwaSubInplaceWord# mwa (off +# i) bi s1 of
- (# s2, 1# #) -> go (i -# 1#) s2
- (# s2, _ #) -> (# s2, 0# #) -- underflow
+ go i
+ | isTrue# (i <# 0#) = Ubx.pure 1# -- no underflow
+ | True = Ubx.do
+ bi <- readWordArray# mwb i
+ b <- mwaSubInplaceWord# mwa (off +# i) bi
+ case b of
+ 1# -> go (i -# 1#)
+ _ -> Ubx.pure 0# -- underflow
-- | Sub an array inplace and then trim zeroes
--
-- Don't check overflow. The caller must ensure that a>=b
mwaSubInplaceArrayTrim :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> State# d
-mwaSubInplaceArrayTrim mwa off wb s =
- case mwaSubInplaceArray mwa off wb s of
- (# s', _ #) -> mwaTrimZeroes# mwa s'
+mwaSubInplaceArrayTrim mwa off wb = Ubx.do
+ _ <- mwaSubInplaceArray mwa off wb
+ mwaTrimZeroes# mwa
-- | Read an indexed Word in the MutableWordArray. If the index is out-of-bound,
-- return zero.
mwaReadOrZero :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Word# #)
-mwaReadOrZero mwa i s = case mwaSize# mwa s of
- (# s2, sz #)
- | isTrue# (i >=# sz) -> (# s2, 0## #)
- | isTrue# (i <# 0#) -> (# s2, 0## #)
- | True -> readWordArray# mwa i s2
+mwaReadOrZero mwa i = Ubx.do
+ sz <- mwaSize# mwa
+ if | isTrue# (i >=# sz) -> Ubx.pure 0##
+ | isTrue# (i <# 0#) -> Ubx.pure 0##
+ | True -> readWordArray# mwa i
mwaRead# :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Word# #)
mwaRead# = readWordArray#
=====================================
libraries/ghc-internal/src/GHC/Internal/Unboxed/Do.hs
=====================================
@@ -0,0 +1,152 @@
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnboxedTuples #-}
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE PolyKinds #-}
+{-# LANGUAGE KindSignatures #-}
+{-# LANGUAGE RankNTypes #-}
+{-# LANGUAGE FlexibleInstances #-}
+{-# LANGUAGE MultiParamTypeClasses #-}
+{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE TypeOperators #-}
+{-# LANGUAGE StandaloneKindSignatures #-}
+
+-- |
+-- Module : GHC.Internal.Unboxed.Do
+-- Copyright : (c) The University of Glasgow 2026
+-- License : see libraries/ghc-internal/LICENSE
+--
+-- Maintainer : ghc-devs(a)haskell.org
+-- Stability : internal
+-- Portability : non-portable (GHC Extensions)
+--
+-- Do-notation for @State#@-passing code that binds unboxed values.
+--
+-- Import this module qualified and use it with @QualifiedDo@:
+--
+-- > {-# LANGUAGE QualifiedDo, MagicHash, UnboxedTuples #-}
+-- > import qualified GHC.Internal.Unboxed.Do as Ubx
+-- >
+-- > sumTwo :: MutableByteArray# s -> State# s -> (# State# s, Word# #)
+-- > sumTwo mba = Ubx.do
+-- > a <- readWordArray# mba 0# -- a :: Word#
+-- > b <- readWordArray# mba 1#
+-- > writeWordArray# mba 2# (plusWord# a b) -- a unit action is a statement too
+-- > Ubx.pure (plusWord# a b)
+--
+-- A block has type @State# s -> o@ for any @o@: it can end with a unit
+-- action (@State# s -> State# s@) or with an action that returns a value
+-- (@State# s -> (# State# s, a #)@). Bound variables can have any
+-- representation for which there is a 'Bind#' instance.
+--
+-- See #27944.
+
+module GHC.Internal.Unboxed.Do
+ ( ST#
+ , Bind# (..)
+ , Then# (..)
+ , nop
+ , void
+ )
+where
+
+import GHC.Internal.Prim
+import GHC.Internal.Types
+
+-- | A @State#@-passing action that returns a value of any representation.
+type ST# :: forall (r :: RuntimeRep). Type -> TYPE r -> Type
+type ST# s a = State# s -> (# State# s, a #)
+
+-- | Operations of 'ST#' actions whose result has representation @r@.
+-- One instance per representation, so that the bound variable has a fixed one.
+--
+-- The continuation result @o@ is arbitrary, so that a block can end with a
+-- unit action (@o = State# s@) or with an 'ST#' action.
+class Bind# (r :: RuntimeRep) where
+ (>>=) :: forall s (a :: TYPE r) {r'} (o :: TYPE r').
+ ST# s a -> (a -> State# s -> o) -> State# s -> o
+
+ -- | Sequence two actions, discard the result of the first one
+ then# :: forall s (a :: TYPE r) {r'} (o :: TYPE r').
+ ST# s a -> (State# s -> o) -> State# s -> o
+
+ pure :: forall s (a :: TYPE r). a -> ST# s a
+
+-- | Sequencing. The discarded action @m@ is a unit action
+-- (@State# s -> State# s@) or an 'ST#' action.
+class Then# (m :: Type) (n :: Type) where
+ (>>) :: m -> n -> n
+
+infixl 1 >>=, >>
+
+-- The `s ~ s'` contexts give instance improvement: the state type of the
+-- continuation is inferred from the discarded action.
+instance forall s s' r' (o :: TYPE r'). s ~ s'
+ => Then# (State# s -> State# s) (State# s' -> o) where
+ {-# INLINE (>>) #-}
+ m >> k = \s -> case m s of s2 -> k s2
+
+instance forall s s' r (a :: TYPE r) r' (o :: TYPE r'). (s ~ s', Bind# r)
+ => Then# (State# s -> (# State# s, a #)) (State# s' -> o) where
+ {-# INLINE (>>) #-}
+ m >> k = m `then#` k
+
+-- | Unit action that does nothing.
+nop :: State# s -> State# s
+nop s = s
+{-# INLINE nop #-}
+
+-- | Run an action and discard its result.
+void :: forall s r (a :: TYPE r). Bind# r => ST# s a -> State# s -> State# s
+void m = m `then#` nop
+{-# INLINE void #-}
+
+#define BIND_INSTANCE(rep) \
+instance Bind# (rep) where { \
+ {-# INLINE (>>=) #-} ; \
+ m >>= k = \s -> case m s of { (# s2, a #) -> k a s2 } ; \
+ {-# INLINE then# #-} ; \
+ m `then#` k = \s -> case m s of { (# s2, _ #) -> k s2 } ; \
+ {-# INLINE pure #-} ; \
+ pure a = \s -> (# s, a #) }
+
+-- Traditional CPP: no commas in macro arguments, hence the type synonyms.
+type TupleRep_WW = TupleRep '[WordRep, WordRep] -- (# Word#, Word# #)
+type TupleRep_WI = TupleRep '[WordRep, IntRep] -- (# Word#, Int# #)
+type TupleRep_IW = TupleRep '[IntRep, WordRep] -- (# Int#, Word# #)
+type TupleRep_II = TupleRep '[IntRep, IntRep] -- (# Int#, Int# #)
+type TupleRep_UU = TupleRep '[UnliftedRep, UnliftedRep] -- (# ByteArray#, ByteArray# #)
+type TupleRep_UW = TupleRep '[UnliftedRep, WordRep] -- (# ByteArray#, Word# #)
+type TupleRep_WU = TupleRep '[WordRep, UnliftedRep] -- (# Word#, ByteArray# #)
+type TupleRep_LL = TupleRep '[LiftedRep, LiftedRep] -- (# a, b #)
+type TupleRep_LLL = TupleRep '[LiftedRep, LiftedRep, LiftedRep] -- (# a, b, c #)
+type SumRep_0U = SumRep '[ZeroBitRep, UnliftedRep] -- (# (# #) | ByteArray# #)
+
+-- Only the representations used in ghc-internal. Extend as needed (#27944).
+BIND_INSTANCE(IntRep)
+BIND_INSTANCE(Int8Rep)
+BIND_INSTANCE(Int16Rep)
+BIND_INSTANCE(Int32Rep)
+BIND_INSTANCE(Int64Rep)
+BIND_INSTANCE(WordRep)
+BIND_INSTANCE(Word8Rep)
+BIND_INSTANCE(Word16Rep)
+BIND_INSTANCE(Word32Rep)
+BIND_INSTANCE(Word64Rep)
+BIND_INSTANCE(AddrRep)
+BIND_INSTANCE(FloatRep)
+BIND_INSTANCE(DoubleRep)
+BIND_INSTANCE(LiftedRep)
+BIND_INSTANCE(UnliftedRep)
+BIND_INSTANCE(ZeroBitRep)
+BIND_INSTANCE(TupleRep_WW)
+BIND_INSTANCE(TupleRep_WI)
+BIND_INSTANCE(TupleRep_IW)
+BIND_INSTANCE(TupleRep_II)
+BIND_INSTANCE(TupleRep_UU)
+BIND_INSTANCE(TupleRep_UW)
+BIND_INSTANCE(TupleRep_WU)
+BIND_INSTANCE(TupleRep_LL)
+BIND_INSTANCE(TupleRep_LLL)
+BIND_INSTANCE(SumRep_0U)
=====================================
testsuite/tests/qualifieddo/should_compile/all.T
=====================================
@@ -2,3 +2,4 @@ setTestOpts(only_ways(['normal']));
test('qdocompile001', normal, compile, ['-v0 -ddump-rn -dsuppress-uniques'])
test('qdocompile002', normal, compile, ['-v0'])
+test('qdocompile003', normal, compile, ['-O -ddump-simpl -dsuppress-all -dsuppress-uniques -dno-typeable-binds'])
=====================================
testsuite/tests/qualifieddo/should_compile/qdocompile003.hs
=====================================
@@ -0,0 +1,16 @@
+{-# LANGUAGE QualifiedDo #-}
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnboxedTuples #-}
+
+-- With -O, the block must compile to nested case expressions.
+module QDoCompile003 where
+
+import GHC.Exts
+import qualified GHC.Internal.Unboxed.Do as Ubx
+
+sumTwo :: MutableByteArray# s -> State# s -> (# State# s, Word# #)
+sumTwo mba = Ubx.do
+ a <- readWordArray# mba 0#
+ b <- readWordArray# mba 1#
+ writeWordArray# mba 2# (plusWord# a b)
+ Ubx.pure (plusWord# a b)
=====================================
testsuite/tests/qualifieddo/should_compile/qdocompile003.stderr
=====================================
@@ -0,0 +1,18 @@
+
+==================== Tidy Core ====================
+Result size of Tidy Core
+ = {terms: 30, types: 44, coercions: 0, joins: 0/0}
+
+sumTwo
+ = \ @s mba s1 ->
+ case readWordArray# mba 0# s1 of { (# ipv, ipv1 #) ->
+ case readWordArray# mba 1# ipv of { (# ipv2, ipv3 #) ->
+ case writeWordArray# mba 2# (plusWord# ipv1 ipv3) ipv2 of s2
+ { __DEFAULT ->
+ (# s2, plusWord# ipv1 ipv3 #)
+ }
+ }
+ }
+
+
+
=====================================
testsuite/tests/qualifieddo/should_fail/all.T
=====================================
@@ -5,3 +5,4 @@ test('qdofail002', normal, compile_fail, ['-v0'])
test('qdofail003', normal, compile_fail, ['-v0'])
test('qdofail004', normal, compile_fail, ['-v0'])
test('qdofail005', normal, compile_fail, ['-v0'])
+test('qdofail006', normal, compile_fail, ['-v0'])
=====================================
testsuite/tests/qualifieddo/should_fail/qdofail006.hs
=====================================
@@ -0,0 +1,18 @@
+{-# LANGUAGE QualifiedDo #-}
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnboxedTuples #-}
+{-# LANGUAGE PolyKinds #-}
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE RankNTypes #-}
+
+-- A bound variable must have a fixed runtime representation.
+module QDoFail006 where
+
+import GHC.Exts
+import qualified GHC.Internal.Unboxed.Do as Ubx
+import GHC.Internal.Unboxed.Do (ST#)
+
+bad :: forall s r (a :: TYPE r). ST# s a -> ST# s a
+bad m = Ubx.do
+ x <- m
+ Ubx.pure x
=====================================
testsuite/tests/qualifieddo/should_fail/qdofail006.stderr
=====================================
@@ -0,0 +1,14 @@
+qdofail006.hs:17:3: error: [GHC-55287]
+ • The binder of the lambda expression
+ does not have a fixed runtime representation.
+ Its type is:
+ a :: TYPE r
+ • In a stmt of a qualified 'do' block: x <- m
+ In the expression:
+ Ubx.do x <- m
+ Ubx.pure x
+ In an equation for ‘bad’:
+ bad m
+ = Ubx.do x <- m
+ Ubx.pure x
+
=====================================
testsuite/tests/qualifieddo/should_run/all.T
=====================================
@@ -9,3 +9,4 @@ test('qdorun004', normal, compile_and_run, [''])
test('qdorun005', [qextra_files], multimod_compile_and_run, ['qdorun005', ''])
test('qdorun006', [req_th,qextra_files], multimod_compile_and_run, ['qdorun006', ''])
test('qdorun007', [qextra_files], multimod_compile_and_run, ['qdorun007', ''])
+test('qdorun008', normal, compile_and_run, [''])
=====================================
testsuite/tests/qualifieddo/should_run/qdorun008.hs
=====================================
@@ -0,0 +1,43 @@
+{-# LANGUAGE QualifiedDo #-}
+{-# LANGUAGE MagicHash #-}
+{-# LANGUAGE UnboxedTuples #-}
+{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE MultiWayIf #-}
+
+module Main where
+
+import GHC.Exts
+import qualified GHC.Internal.Unboxed.Do as Ubx
+import GHC.Internal.Unboxed.Do (ST#)
+
+fill :: MutableByteArray# s -> Word# -> State# s -> State# s -- unit result
+fill mba x = Ubx.do
+ writeWordArray# mba 0# x -- unit action as a statement
+ n <- getSizeofMutableByteArray# mba -- binds an Int#
+ writeWordArray# mba 1# (int2Word# n)
+
+sumAll :: Int# -> ST# s Word#
+sumAll n = Ubx.do
+ mba <- newByteArray# n -- binds an unlifted pointer
+ fill mba 40##
+ a <- readWordArray# mba 0# -- binds a Word#
+ b <- readWordArray# mba 1#
+ (# c, ov #) <- Ubx.pure (addWordC# a b) -- binds an unboxed tuple
+ let !(# hi, lo #) = timesWord2# a 2##
+ Ubx.pure (plusWord# (plusWord# c (int2Word# ov)) (plusWord# hi lo))
+
+ordering :: MutableByteArray# s -> ST# s Ordering -- lifted result
+ordering mba = Ubx.do
+ a <- readWordArray# mba 0#
+ b <- readWordArray# mba 1#
+ if | isTrue# (ltWord# a b) -> Ubx.pure LT
+ | isTrue# (gtWord# a b) -> Ubx.pure GT
+ | True -> Ubx.pure EQ
+
+main :: IO ()
+main = do
+ print (W# (case runRW# (sumAll 16#) of (# _, w #) -> w))
+ print (case runRW# (\s -> case newByteArray# 16# s of
+ (# s1, mba #) -> case fill mba 5## s1 of
+ s2 -> ordering mba s2) of
+ (# _, o #) -> o)
=====================================
testsuite/tests/qualifieddo/should_run/qdorun008.stdout
=====================================
@@ -0,0 +1,2 @@
+136
+LT
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/6bbca56943ef68ba32520bcf8712a8…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/6bbca56943ef68ba32520bcf8712a8…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
22 Oct '26
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
6bbca569 by Andreas Klebinger at 2026-10-21T10:01:10+00:00
Fix a few tag inference bugs.
* Treat bottoming functions and nullary join points as functions.
* When generating bytecode don't rely on imported tag sigs.
Fixes #27831, #27841 and #27842.
- - - - -
24 changed files:
- + changelog.d/T27831-bottoming-strict-field
- + changelog.d/T27842-bytecode-imported-tags
- compiler/GHC/Stg/EnforceEpt.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/EnforceEpt/Types.hs
- + testsuite/tests/bytecode/T27842.hs
- + testsuite/tests/bytecode/T27842.stdout
- + testsuite/tests/bytecode/T27842A.hs
- + testsuite/tests/bytecode/T27842N.hs
- testsuite/tests/bytecode/all.T
- testsuite/tests/simplStg/should_compile/all.T
- + testsuite/tests/simplStg/should_compile/inferTags005.hs
- + testsuite/tests/simplStg/should_compile/inferTags005.stderr
- + testsuite/tests/simplStg/should_compile/inferTags006.hs
- + testsuite/tests/simplStg/should_compile/inferTags006.stderr
- + testsuite/tests/simplStg/should_run/InferTags007_A.hs
- + testsuite/tests/simplStg/should_run/T27831.hs
- + testsuite/tests/simplStg/should_run/T27831.stdout
- + testsuite/tests/simplStg/should_run/T27857.hs
- + testsuite/tests/simplStg/should_run/T27857.stdout
- testsuite/tests/simplStg/should_run/all.T
- + testsuite/tests/simplStg/should_run/inferTags007.hs
- + testsuite/tests/simplStg/should_run/inferTags007.stdout
Changes:
=====================================
changelog.d/T27831-bottoming-strict-field
=====================================
@@ -0,0 +1,8 @@
+section: compiler
+issues: #27831
+mrs: !16719
+synopsis:
+ Fix a segfault that could occur when a bottoming function was stored in a strict field.
+description:
+ This was happening when a function that always diverges (e.g. ``\_ -> error "..."``) was stored in a strict field, and the constructor was pattern matched on
+ in "nearby" code where the structure of the constructor application was still visible to GHC.
=====================================
changelog.d/T27842-bytecode-imported-tags
=====================================
@@ -0,0 +1,6 @@
+section: bytecode
+issues: #27842
+mrs: !16719
+synopsis:
+ Fix a segfault caused by reusing interface files generated with
+ ``-fbyte-code-and-object-code`` for interpreter sessions using ``-fprefer-byte-code``.
=====================================
compiler/GHC/Stg/EnforceEpt.hs
=====================================
@@ -331,12 +331,19 @@ a) Tags for let-bindings:
We make this differentiation in `mkLetSig` where we simply never assume
lets are tagged when targeting bytecode.
-b) When referencing ids from other modules the Cmm backend will try to put a
- proper tag on these references through various means. When doing analysis we
- usually predict these cases to improve precision of the analysis.
- But to my knowledge the bytecode generator makes no such attempts so we must
- not infer imported bindings as tagged.
- This is handled in GHC.Stg.EnforceEpt.Types.lookupInfo
+b) When referencing ids from other modules the Cmm based backends ensure top
+ level binders are properly tagged if they are constructors.
+
+ This is not true for the bytecode generator, which gives fewer guarantees about
+ what it tags. So we assume the safe, but slightly less efficient default of
+ no imported binding being tagged when generating bytecode.
+
+ This is handled in GHC.Stg.EnforceEpt.Types.argTagInfo
+
+ Eventually we will want to update the Analysis and bco code gen to uphold and
+ take advantage of tag inference at least to some degree. But for now it's
+ not a perf benefit for interpreted code, and essentially only run to uphold
+ the "strict field invariant"/EPT Invariant described in EPT enforcement.
-}
@@ -359,7 +366,7 @@ enforceEpt ppr_opts !for_bytecode logger this_mod stg_binds = do
-- Rewrite STG to uphold the strict field invariant
us_t <- mkSplitUniqSupply StgTag
- let rewritten_binds = {-# SCC "StgEptRewrite" #-} rewriteTopBinds this_mod us_t stg_binds_w_tags :: [TgStgTopBinding]
+ let rewritten_binds = {-# SCC "StgEptRewrite" #-} rewriteTopBinds for_bytecode this_mod us_t stg_binds_w_tags :: [TgStgTopBinding]
return (rewritten_binds,export_tag_info)
@@ -414,12 +421,14 @@ inferTagExpr env (StgApp fun args)
| isDeadEndAppSig (idDmdSig fun) (length args)
= TagBottoming
- | fun_arity == 0 -- Unknown arity => Thunk or unknown call
+ | not (isJoinId fun) -- zero arity join ids behave like functions NOT thunks.
+ , fun_arity == 0 -- Unknown arity => Thunk or unknown call
= TagDunno
-- Imported function with known return tag
| Just (TagFun res_info) <- tagSigInfo (idInfo fun)
, fun_arity == length args -- Saturated
+ , not (te_bytecode env) -- #27842
= res_info
-- Local function with known return tag
@@ -593,7 +602,7 @@ initSig (bndr, StgRhsClosure _ _ _ bndrs _ _)
{- Note [Bottom functions are TagBottoming]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-If we have a function with two branches with one
+If we have a function with two branches with one branch
being bottom, and the other returning a tagged
unboxed tuple what is the result? We give it TagBottoming!
To answer why consider this function:
@@ -603,20 +612,42 @@ foo x = case x of
True -> (# True,True #)
False -> undefined
-The true branch is obviously tagged. The other branch isn't.
-We want to treat the *result* of foo as tagged as well so that
-the combination of the branches also is tagged if all non-bottom
-branches are tagged.
-This is safe because the function is still always called/entered as long
-as it's applied to arguments. Since the function will never return we can give
-it safely any tag sig we like.
-So we give it TagBottoming, as it allows the combined tag sig of the case expression
-to be the combination of all non-bottoming branches.
+The true branch obviously returns tagged components. The other branch is bottom.
+We want to treat the components in the *result* returned by foo as tagged.
+Because at a call site like:
+
+ case foo e of
+ (# x, y #) ->
+ case x of
+ ...
+
+Either `foo` will have evaluated undefined or blown up. Or we will bind x and y
+to tagged pointers. A fact we take advantage of to optimize `case x of`.
+
+The rule is then simple. A combination of the alternatives of the same case has
+tagged components if all non-bottom branches have tagged components.
+
+To represent the bottoming alternatives we use TagBottoming. Which says all its
+components are tagged. Now the same is true if we have:
+
+foo :: Bool -> (# Bool, Bool #)
+foo x = case x of
+ True -> (# True,True #)
+ False -> willCrash x
+
+We want to check:
+
+* Is `willCrash x` bottom?
+* If the whole case expression *does* return are the tuple components tagged?
+
+The first check is done by inferTagExpr, which needs to check if willCrash is
+applied to enough arguments to crash. If so we get TagBottoming for the case
+alternative. Which when combined with TagTuple [TagEPT, TagEPT] gives the
+desired TagTuple [TagEPT, TagEPT] for the overall case expression.
-NB: After the analysis is done we go back to treating bottoming functions as
-untagged to ensure they are evaluated as expected in code like:
+NB: If there is just one rhs it's handled by inferTagRhs which will give
+SigFun TagBottoming for `foo x= willCrash x` in the same fashion as inferTagExpr does.
- case bottom_id of { ...}
-}
=====================================
compiler/GHC/Stg/EnforceEpt/Rewrite.hs
=====================================
@@ -3,6 +3,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE UnboxedTuples #-}
module GHC.Stg.EnforceEpt.Rewrite (rewriteTopBinds, rewriteOpApp)
where
@@ -13,7 +14,7 @@ import GHC.Builtin.PrimOps ( PrimOp(..) )
import GHC.Types.Basic ( CbvMark (..), isMarkedCbv
, TopLevelFlag(..), isTopLevel )
import GHC.Types.Id
-import GHC.Types.Name
+import GHC.Types.Name ( nameIsLocalOrFrom )
import GHC.Types.Unique.Supply
import GHC.Types.Unique.FM
import GHC.Types.RepType
@@ -22,10 +23,7 @@ import GHC.Unit.Types
import GHC.Core.DataCon
import GHC.Core ( AltCon(..) )
-import GHC.Core.Type
-
-import GHC.StgToCmm.Types
-import GHC.StgToCmm.Closure (importedIdLFInfo)
+import GHC.Core.Type ( definitelyLiftedType )
import GHC.Stg.Utils
import GHC.Stg.Syntax as StgSyn
@@ -34,15 +32,54 @@ import GHC.Data.Maybe
import GHC.Utils.Panic
import GHC.Utils.Outputable
-import GHC.Utils.Monad.StrictState
import GHC.Utils.Misc
import GHC.Stg.EnforceEpt.Types
import Control.Monad
-
-newtype RM a = RM { unRM :: (State (UniqFM Id TagSig, UniqSupply, Module, IdSet) a) }
- deriving (Functor, Monad, Applicative)
+import GHC.Exts (oneShot)
+import Data.Traversable (mapAccumM)
+
+-- | Constant for whole module
+data RmModInfo = RmModInfo { rm_mod :: !Module, rm_for_interpreter :: !Bool }
+
+type SigEnv = UniqFM Id TagSig
+data RmEnv = RmEnv { rm_fvs :: IdSet, rm_sig_env :: !(SigEnv) }
+
+newtype RM a = RM { unRM :: RmModInfo -> RmEnv -> UniqSupply -> (# UniqSupply, a #) }
+
+instance Functor RM where
+ fmap f (RM x) = RM $ oneShot $ \info -> oneShot $ \env -> oneShot $ \us ->
+ case x info env us of
+ (# !us1, !x' #) ->
+ let !r = f x'
+ in (# us1, r #)
+
+instance Applicative RM where
+ (RM f) <*> (RM x) = RM $ oneShot $ \info -> oneShot $ \env -> oneShot $ \us ->
+ case f info env us of
+ (# !us1, !f' #) ->
+ case x info env us1 of
+ (# !us2, !x' #) ->
+ let !r = f' x'
+ in (# us2, r #)
+ pure !x = RM $ \_ !_env !us -> (# us, x #)
+
+-- See Note [The one-shot state monad trick] in GHC.Utils.Monad
+instance Monad RM where
+ RM x >>= f = RM $ oneShot $ \info -> oneShot $ \env -> oneShot $ \us ->
+ case x info env us of
+ (# !us1, !x' #) -> case (f x') of RM r -> r info env us1
+
+-- | runRM this_mod for_bytecode us
+runRM :: Module -> Bool -> UniqSupply -> RM a -> a
+runRM mod for_bytecode us act =
+ case (unRM act)
+ (RmModInfo { rm_mod = mod, rm_for_interpreter = for_bytecode })
+ (RmEnv { rm_fvs = emptyVarSet, rm_sig_env = emptyUFM })
+ us
+ of
+ (# _, r #) -> r
------------------------------------------------------------
-- Add cases around strict fields where required.
@@ -107,30 +144,31 @@ when building the free variable list.
--------------------------------
instance MonadUnique RM where
- getUniqueSupplyM = RM $ do
- (m, us, mod,lcls) <- get
- let (us1, us2) = splitUniqSupply us
- (put) (m,us2,mod,lcls)
- return us1
+ getUniqueSupplyM = RM $ \_ _env us ->
+ case splitUniqSupply us of
+ (us1, us2) -> (# us2, us1 #)
-getMap :: RM (UniqFM Id TagSig)
-getMap = RM $ ((\(fst,_,_,_) -> fst) <$> get)
+getSigEnv :: RM SigEnv
+getSigEnv = RM $ \_ env us -> (# us, rm_sig_env env #)
-setMap :: (UniqFM Id TagSig) -> RM ()
-setMap !m = RM $ do
- (_,us,mod,lcls) <- get
- put (m, us,mod,lcls)
-
-getMod :: RM Module
-getMod = RM $ ( (\(_,_,thrd,_) -> thrd) <$> get)
+{-# INLINE withSigEnv #-}
+withSigEnv :: SigEnv-> RM a -> RM a
+withSigEnv !m (RM f)= RM $ \mi env us -> f mi env { rm_sig_env = m} us
getFVs :: RM IdSet
-getFVs = RM $ ((\(_,_,_,lcls) -> lcls) <$> get)
+getFVs = RM $ \_ env us -> (# us, rm_fvs env #)
-setFVs :: IdSet -> RM ()
-setFVs !fvs = RM $ do
- (tag_map,us,mod,_lcls) <- get
- put (tag_map, us,mod,fvs)
+{-# INLINE withFVs #-}
+withFVs :: IdSet -> RM a -> RM a
+withFVs !fvs (RM f)= RM $ \mi env us -> f mi env { rm_fvs = fvs } us
+
+getMod :: RM Module
+getMod = RM $ \info _env us -> (# us, rm_mod info #)
+
+-- | When generating bytecode certain bindings might be untagged. So we have to
+-- have a way to check for that.
+getForBytecode :: RM Bool
+getForBytecode = RM $ \info _env us -> (# us, rm_for_interpreter info #)
-- Rewrite the RHS(s) while making the id and it's sig available
-- to determine if things are tagged/need to be captured as FV.
@@ -140,66 +178,50 @@ withBind top_flag (StgRec binds) cont = do
let (bnds,_rhss) = unzip binds :: ([(Id, TagSig)], [GenStgRhs 'InferTaggedBinders])
withBinders top_flag bnds cont
-addTopBind :: GenStgBinding 'InferTaggedBinders -> RM ()
+addTopBind :: GenStgBinding 'InferTaggedBinders -> RM SigEnv
addTopBind (StgNonRec (id, tag) _) = do
- s <- getMap
+ s <- getSigEnv
-- pprTraceM "AddBind" (ppr id)
- setMap $ addToUFM s id tag
- return ()
+ pure $! addToUFM s id tag
addTopBind (StgRec binds) = do
let (bnds,_rhss) = unzip binds
- !s <- getMap
+ !s <- getSigEnv
-- pprTraceM "AddBinds" (ppr $ map fst bnds)
- setMap $! addListToUFM s bnds
+ pure $! addListToUFM s bnds
withBinder :: TopLevelFlag -> (Id, TagSig) -> RM a -> RM a
withBinder top_flag (id,sig) cont = do
- oldMap <- getMap
- setMap $ addToUFM oldMap id sig
- a <- if isTopLevel top_flag
+ sig_env <- getSigEnv
+ withSigEnv (addToUFM sig_env id sig) $ do
+ if isTopLevel top_flag
then cont
else withLcl id cont
- setMap oldMap
- return a
withBinders :: TopLevelFlag -> [(Id, TagSig)] -> RM a -> RM a
withBinders TopLevel sigs cont = do
- oldMap <- getMap
- setMap $ addListToUFM oldMap sigs
- a <- cont
- setMap oldMap
- return a
+ sig_env <- getSigEnv
+ withSigEnv (addListToUFM sig_env sigs) cont
withBinders NotTopLevel sigs cont = do
- oldMap <- getMap
- oldFvs <- getFVs
- setMap $ addListToUFM oldMap sigs
- setFVs $ extendVarSetList oldFvs (map fst sigs)
- a <- cont
- setMap oldMap
- setFVs oldFvs
- return a
+ sig_env <- getSigEnv
+ fvs <- getFVs
+ withSigEnv (addListToUFM sig_env sigs) $
+ withFVs (extendVarSetList fvs (map fst sigs)) cont
-- | Compute the argument with the given set of ids treated as requiring capture
-- as free variables.
withClosureLcls :: DIdSet -> RM a -> RM a
withClosureLcls fvs act = do
old_fvs <- getFVs
- let !fvs' = nonDetStrictFoldDVarSet (flip extendVarSet) old_fvs fvs
- setFVs fvs'
- !r <- act
- setFVs old_fvs
- return r
+ withFVs
+ (nonDetStrictFoldDVarSet (flip extendVarSet) old_fvs fvs)
+ act
-- | Compute the argument with the given id treated as requiring capture
-- as free variables in closures.
withLcl :: Id -> RM a -> RM a
withLcl fv act = do
old_fvs <- getFVs
- let !fvs' = extendVarSet old_fvs fv
- setFVs fvs'
- !r <- act
- setFVs old_fvs
- return r
+ withFVs (extendVarSet old_fvs fv) act
{- Note [Tag inference for interactive contexts]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -238,51 +260,26 @@ indicates a bug in the tag inference implementation.
For this reason we assert that we are running in interactive mode if a lookup fails.
-}
isTagged :: Id -> RM Bool
-isTagged v
- -- See Note [Bottom functions are TagBottoming]
- | isDeadEndId v = pure False
- | otherwise = do
+isTagged v = do
+ for_bytecode <- getForBytecode
this_mod <- getMod
- -- See Note [Tag inference for interactive contexts]
- let lookupDefault v = assertPpr (isInteractiveModule this_mod)
- (text "unknown Id:" <> ppr this_mod <+> ppr v)
- (TagVal TagDunno)
- case nameIsLocalOrFrom this_mod (idName v) of
- True
- | definitelyUnliftedType (idType v)
- -- NB: v might be the Id of a representation-polymorphic join point,
- -- so we shouldn't use isUnliftedType here. See T22212.
- -> return True
- | otherwise -> do -- Local binding
- !s <- getMap
- let !sig = lookupWithDefaultUFM s (lookupDefault v) v
- return $ case sig of
- TagFun _ -> True -- function closure is tagged
- TagVal TagDunno -> False
- TagVal TagEPT -> True
- TagVal TagBottoming -> True
- TagVal (TagTuple _) -> True -- Consider unboxed tuples tagged.
- -- Imported
- False -> return $!
- -- Determine whether it is tagged from the LFInfo of the imported id.
- -- See Note [The LFInfo of Imported Ids]
- case importedIdLFInfo v of
- -- Function, applied not entered.
- LFReEntrant {}
- -> True
- -- Thunks need to be entered.
- LFThunk {}
- -> False
- -- LFCon means we already know the tag, and it's tagged.
- LFCon {}
- -> True
- LFUnknown {}
- -> False
- LFUnlifted {} -- Unboxed, tagging irrelevant
- -> True
- LFLetNoEscape {}
- -- Shouldn't be possible. I don't think we can export letNoEscapes
- -> True
+ s <- getSigEnv
+ let get_env_sig = lookupUFM s
+ massertPpr
+ -- Assert the env is complete for local lifted binders, which means either
+ -- - The binder is present in the env
+ -- - The binder is not a local lifted binder *or* comes from a ghci context
+ (isJust (get_env_sig v)
+ -- See Note [Tag inference for interactive contexts]
+ || isInteractiveModule this_mod
+ || not (nameIsLocalOrFrom this_mod (idName v))
+ || not (definitelyLiftedType (idType v)))
+ (text "unknown Id:" <> ppr this_mod <+> ppr v)
+ -- TODO: We could refactor the whole pass such that we also set id_tagSig
+ -- for every imported id. But this likely comes with a heavy allocation
+ -- overhead so for now we just ensure to use the same logic across analysis
+ -- and rewriter.
+ pure $! isTaggedInfo $ argTagInfo for_bytecode (get_env_sig v) (StgVarArg v)
isArgTagged :: StgArg -> RM Bool
@@ -299,18 +296,22 @@ mkLocalArgId id = do
---------------------------
-rewriteTopBinds :: Module -> UniqSupply -> [GenStgTopBinding 'InferTaggedBinders] -> [TgStgTopBinding]
-rewriteTopBinds mod us binds =
- let doBinds = mapM rewriteTop binds
-
- in evalState (unRM doBinds) (mempty, us, mod, mempty)
+rewriteTopBinds :: Bool -- ^ Generating bytecode?
+ -> Module -> UniqSupply -> [GenStgTopBinding 'InferTaggedBinders] -> [TgStgTopBinding]
+rewriteTopBinds for_bytecode mod us binds =
+ runRM mod for_bytecode us $ do
+ snd <$> (mapAccumM rewriteTop mempty binds)
-rewriteTop :: InferStgTopBinding -> RM TgStgTopBinding
-rewriteTop (StgTopStringLit v s) = return $! (StgTopStringLit v s)
-rewriteTop (StgTopLifted bind) = do
+rewriteTop :: SigEnv -> InferStgTopBinding -> RM (SigEnv, TgStgTopBinding)
+rewriteTop sig_env (StgTopStringLit v s) = return $! (sig_env, StgTopStringLit v s)
+rewriteTop sig_env (StgTopLifted bind) = do
-- Top level bindings can, and must remain in scope
- addTopBind bind
- (StgTopLifted) <$!> (rewriteBinds TopLevel bind)
+ -- We use withSigEnv inside rewriteTop per binding to avoid
+ -- allocating one withEnv continuation per top binder.
+ withSigEnv sig_env $ do
+ sig_env <- addTopBind bind
+ !binds <- withSigEnv sig_env (rewriteBinds TopLevel bind)
+ pure (sig_env, StgTopLifted binds)
-- For top level binds, the wrapper is guaranteed to be `id`
rewriteBinds :: TopLevelFlag -> InferStgBinding -> RM (TgStgBinding)
=====================================
compiler/GHC/Stg/EnforceEpt/TagSig.hs
=====================================
@@ -119,7 +119,7 @@ isTaggedSig (TagVal ti) = isTaggedInfo ti
-- | Is the given value-level tag known to be properly tagged?
-- NB: unboxed tuples are *not* treated as tagged here; they are handled
--- specially by the rewriter (which considers them already evaluated).
+-- specially by the rewriter. See Note [Constructor TagSigs]
isTaggedInfo :: TagInfo -> Bool
isTaggedInfo TagEPT = True
isTaggedInfo TagBottoming = True
=====================================
compiler/GHC/Stg/EnforceEpt/Types.hs
=====================================
@@ -12,7 +12,7 @@ where
import GHC.Prelude
import GHC.Core.DataCon
-import GHC.Core.Type (isUnliftedType)
+import GHC.Core.Type (isUnliftedType, definitelyUnliftedType)
import GHC.Types.Id
import GHC.Stg.Syntax
import GHC.Stg.EnforceEpt.TagSig as TagSig
@@ -22,6 +22,7 @@ import GHC.Utils.Misc( zipWithEqual )
import GHC.Utils.Panic
import GHC.StgToCmm.Types
+import GHC.StgToCmm.Closure (importedIdLFInfo)
{- *********************************************************************
* *
@@ -97,31 +98,40 @@ lookupReturnInfo env fun = case lookupVarEnv (te_env env) fun of
Just (TagVal _) -> Nothing
Nothing -> Nothing
--- | Look up a value-level tag for an argument: either from the env (where
--- a function-typed argument flattens to TagEPT — its closure is tagged)
--- or derived from information on the variable itself.
-lookupInfo :: TagEnv p -> StgArg -> TagInfo
-lookupInfo env (StgVarArg var)
+-- | Look up a value-level tag.
+--
+-- If the tag info is completely determined by Id info we use that tag.
+-- If the tag info is dependent on the analysis/call site use the passed TagSig.
+-- If the passed TagSig is Nothing we fall back to safe tag info based on id properties.
+argTagInfo :: Bool -> (Maybe TagSig) -> StgArg -> TagInfo
+{-# INLINE argTagInfo #-} -- Specialize for the call site.
+argTagInfo _for_bytecode _env_sig (StgLitArg{}) = TagEPT
+argTagInfo for_bytecode env_sig (StgVarArg var)
-- Nullary data constructors like True, False
| Just dc <- isDataConWorkId_maybe var
, isNullaryRepDataCon dc
, not for_bytecode
= TagEPT
- | isUnliftedType (idType var)
+ -- NB: v might be the Id of a representation-polymorphic join point,
+ -- so we shouldn't use isUnliftedType here. See T22212.
+ | definitelyUnliftedType (idType var)
= TagEPT
-- Variables in the environment. A function binding flattens to TagEPT
-- since a function closure pointer is properly tagged; its return info
-- is not relevant when the function is used as a value.
- | Just sig <- lookupVarEnv (te_env env) var
+ | Just sig <- env_sig
= case sig of
TagVal info -> info
TagFun _ -> TagEPT
- | Just lf_info <- idLFInfo_maybe var
- , not for_bytecode
- = case lf_info of
+-- Always save defaults.
+ | lf_info <- importedIdLFInfo var -- will make up LFInfo if not present.
+ = if for_bytecode
+ then TagDunno -- See Note [EPT enforcement for interpreted code] in GHC.Stg.EnforceEpt, point b)
+ else
+ case lf_info of
-- Function, tagged (with arity)
LFReEntrant {}
-> TagEPT
@@ -138,13 +148,12 @@ lookupInfo env (StgVarArg var)
-- Shouldn't be possible. I don't think we can export letNoEscapes
LFLetNoEscape {} -> panic "LFLetNoEscape exported"
- | otherwise
- = TagDunno
- where
- for_bytecode = te_bytecode env
-
-lookupInfo _ (StgLitArg {})
- = TagEPT
+-- | Get tag info using the environment.
+lookupInfo :: TagEnv p -> StgArg -> TagInfo
+lookupInfo env arg@(StgVarArg var) =
+ let env_sig = (lookupVarEnv (te_env env) var)
+ in argTagInfo (te_bytecode env) env_sig arg
+lookupInfo _env (StgLitArg{}) = TagEPT
isDunnoSig :: TagSig -> Bool
isDunnoSig (TagVal TagDunno) = True
=====================================
testsuite/tests/bytecode/T27842.hs
=====================================
@@ -0,0 +1,42 @@
+{-# LANGUAGE UnboxedTuples #-}
+{-# OPTIONS_GHC -fno-worker-wrapper -fno-cpr-anal #-}
+-- Regression test for #27842.
+--
+-- The basic rundown is this:
+-- We produce one interface file, however we store information dependent on the
+-- code generation method in the interface file.
+--
+-- We happen to pick the native code information to store in the interface file.
+--
+-- When we compile a module M this way, and then generate byte code for code
+-- depending on M based on M.hi we will assume certain exported definitions are EPT
+-- tagged, despite the interpreter doing no such thing.
+--
+-- This then led to crashes in the interpreter.
+--
+-- We test three kinds of imported definitions:
+-- * mk uses fun, a function whose TagSig says its result is tagged.
+-- * bar uses just_con, a value whose LFInfo says it is a constructor.
+-- * baz uses gt_con, the same for a nullary constructor.
+module Main where
+
+import T27842A (fun, just_con, gt_con)
+import T27842N (S(..), use_S, M(..), use_M, N(..), use_N)
+
+mk :: Int -> S
+mk x = case fun x of (# xs, _ #) -> S xs
+{-# NOINLINE mk #-}
+
+bar :: M
+bar = M just_con
+{-# NOINLINE bar #-}
+
+baz :: N
+baz = N gt_con
+{-# NOINLINE baz #-}
+
+main :: IO ()
+main = do
+ print (use_S (mk 42))
+ print (use_M bar)
+ print (use_N baz)
=====================================
testsuite/tests/bytecode/T27842.stdout
=====================================
@@ -0,0 +1,3 @@
+42
+42
+3
=====================================
testsuite/tests/bytecode/T27842A.hs
=====================================
@@ -0,0 +1,19 @@
+{-# LANGUAGE UnboxedTuples #-}
+{-# OPTIONS_GHC -fno-worker-wrapper -fno-cpr-anal #-}
+module T27842A (fun, just_con, gt_con) where
+
+-- The native EPT run gives fun the TagSig TagFun[TagTuple[TagEPT, TagDunno]]:
+fun :: Int -> (# [Int], Int #)
+fun x = let xs = x : xs in (# xs, x #)
+{-# NOINLINE fun #-}
+
+-- In native code a static constructor, in bytecode an unevaluated closure.
+just_con :: Maybe Int
+just_con = Just 42
+{-# NOINLINE just_con #-}
+
+-- The same for a nullary constructor. Without its pointer tag, the object code
+-- in T27842N would take the LT alternative, so GT shows the difference.
+gt_con :: Ordering
+gt_con = GT
+{-# NOINLINE gt_con #-}
=====================================
testsuite/tests/bytecode/T27842N.hs
=====================================
@@ -0,0 +1,26 @@
+{-# OPTIONS_GHC -fno-byte-code-and-object-code #-}
+-- We compile this module as native code, depending on A's interface file.
+-- A itself is loaded into GHCi, despite it's interface file being generated based
+-- on native code generation.
+module T27842N (S(..), use_S, M(..), use_M, N(..), use_N) where
+
+data S = S ![Int]
+
+-- We take apart the strict constructor value and branch on the value in its field.
+-- If the field content is not EPT this causes undefined behaviour.
+
+use_S :: S -> Int
+use_S (S xs) = case xs of { (y:_) -> y; [] -> -1 }
+{-# NOINLINE use_S #-}
+
+data M = M !(Maybe Int)
+
+use_M :: M -> Int
+use_M (M m) = case m of { Just x -> x; Nothing -> -1 }
+{-# NOINLINE use_M #-}
+
+data N = N !Ordering
+
+use_N :: N -> Int
+use_N (N o) = case o of { LT -> 1; EQ -> 2; GT -> 3 }
+{-# NOINLINE use_N #-}
=====================================
testsuite/tests/bytecode/all.T
=====================================
@@ -17,3 +17,12 @@ test('T26216', extra_files(["T26216_aux.hs"]), ghci_script, ['T26216.script'])
test('T27001', [extra_files(['T27001.hs']), req_interp],
run_command,
['{compiler} -e main -O -fno-unoptimized-core-for-interpreter T27001.hs'])
+
+# Bytecode must not trust tag information from imported interfaces, which
+# comes from native code (#27842): neither the TagSig of an imported function
+# nor the LFInfo of an imported value (with and without constructor fields).
+test('T27842', [extra_files(['T27842A.hs', 'T27842N.hs']), req_interp],
+ run_command,
+ ['{compiler} -e main -O -fno-unoptimized-core-for-interpreter '
+ '-fobject-code -fbyte-code-and-object-code -fwrite-byte-code -fprefer-byte-code '
+ 'T27842.hs'])
=====================================
testsuite/tests/simplStg/should_compile/all.T
=====================================
@@ -25,5 +25,17 @@ test('inferTags003', [ only_ways(['optasm']),
], compile, ['-ddump-cmm -dno-typeable-binds -O'])
test('inferTags004', normal, compile, ['-O -ddump-stg-tags -dno-typeable-binds -dsuppress-uniques'])
test('T27005b', normal, compile, ['-O -ddump-stg-tags -dno-typeable-binds -dsuppress-uniques'])
+# Both f and the join point j should have a signature TagTuple[TagEPT, TagEPT]
+test('inferTags005',
+ grep_errmsg(r'(TagTuple\[TagEPT, TagEPT\])', [1]),
+ compile,
+ ['-O -fno-worker-wrapper -fno-cpr-anal -ddump-stg-tags '
+ '-dno-typeable-binds -dsuppress-uniques'])
+# The function needs to have a signature TagTuple[TagEPT, TagEPT]
+test('inferTags006',
+ grep_errmsg(r'(TagTuple\[TagEPT, TagEPT\])', [1]),
+ compile,
+ ['-O -fno-worker-wrapper -fno-cpr-anal -fno-full-laziness -ddump-stg-tags '
+ '-dno-typeable-binds -dsuppress-uniques'])
test('T24806', grep_errmsg('^\\(T24806\\.\\$wgo'), compile, ['-O -ddump-stg-tags -dno-typeable-binds -dsuppress-uniques'])
=====================================
testsuite/tests/simplStg/should_compile/inferTags005.hs
=====================================
@@ -0,0 +1,39 @@
+{-# LANGUAGE UnboxedTuples #-}
+module EnforceEpt005 where
+
+-- If we have
+-- f x y =
+-- join j =
+-- <rhs> -- <TagTuple[TagEPT, TagEPT]]>
+-- in case x of
+-- 1 -> j
+-- 2 -> j
+-- _ ->
+-- <something> -- <TagTuple[TagEPT, TagEPT]]>
+
+-- Consider the case where rhs and something both represent a tag value of <TagTuple[TagEPT, TagEPT]]>
+-- In this case we want `f` to also give rise to TagFun [TagTuple[TagEPT, TagEPT]]]
+
+-- To achieve this we have to recognize j as a zero-arity function equivalent
+-- which is what this test checks.
+
+-- The payoff is in `g` `r` is then known to be tagged, so `S r` needs no eval.
+-- Only an unboxed tuple result exposes this.
+
+-- We disable worker/wrapper and CPR so `f` matches more closely the tests source.
+
+data T = A Int Int Int Int Int Int | B
+
+f :: Int -> Int -> (# T, T #)
+f x y = case x of
+ 0 -> j
+ 1 -> j
+ _ -> (# B, B #)
+ where
+ j = (# A (y+1) (y*2) (y-3) (y*y) (y+x) (y*x), B #)
+{-# NOINLINE f #-}
+
+data S = S !T
+
+g :: Int -> Int -> S
+g x y = case f x y of (# r, _ #) -> S r
=====================================
testsuite/tests/simplStg/should_compile/inferTags005.stderr
=====================================
@@ -0,0 +1,2 @@
+TagTuple[TagEPT, TagEPT]
+TagTuple[TagEPT, TagEPT]
=====================================
testsuite/tests/simplStg/should_compile/inferTags006.hs
=====================================
@@ -0,0 +1,31 @@
+{-# LANGUAGE UnboxedTuples #-}
+module EnforceEpt006 where
+
+-- If we have
+-- f x y =
+-- join j =
+-- <rhs> -- TagBottoming
+-- in case x of
+-- 1 -> j
+-- 2 -> j
+-- _ ->
+-- <something> -- <TagTuple[TagEPT, TagEPT]]>
+
+-- Similar to inferTage005 we want to give `f` the signature: TagFun <TagTuple[TagEPT, TagEPT]]>
+-- This requires us to give `j` a `TagFun[TagBottoming]` signature, which is safe because
+-- either its execution will diverge, or the resulting tuple *will* have two
+-- tagged values.
+
+data T = A Int Int Int Int Int Int | B
+
+f :: Int -> Int -> (# T, T #)
+-- This case-of-case is just here to be optimized into a nullary join point.
+f x y = case (case x of { 0 -> False; 1 -> False; _ -> True }) of
+ False -> error ("f: bad input " ++ show x ++ " " ++ show y ++ " " ++ show (x*y))
+ True -> (# A (y+1) (y*2) (y-3) (y*y) (y+x) (y*x), B #)
+{-# NOINLINE f #-}
+
+data S = S !T
+
+g :: Int -> Int -> S
+g x y = case f x y of (# r, _ #) -> S r
=====================================
testsuite/tests/simplStg/should_compile/inferTags006.stderr
=====================================
@@ -0,0 +1 @@
+TagTuple[TagEPT, TagEPT]
=====================================
testsuite/tests/simplStg/should_run/InferTags007_A.hs
=====================================
@@ -0,0 +1,20 @@
+-- We suppress arity and LfInfo with the option.
+{-# OPTIONS_GHC -fomit-interface-pragmas #-}
+module InferTags007_A (fun, caf, bot, con) where
+
+fun :: Int -> Int
+fun x = x + 1
+{-# NOINLINE fun #-}
+
+-- A CAF/thunk
+caf :: [Int]
+caf = map (* 2) [1 .. 10]
+{-# NOINLINE caf #-}
+
+bot :: Int -> Int
+bot _ = error "bot called"
+{-# NOINLINE bot #-}
+
+con :: Maybe Int
+con = Just 42
+{-# NOINLINE con #-}
=====================================
testsuite/tests/simplStg/should_run/T27831.hs
=====================================
@@ -0,0 +1,35 @@
+-- Regression test for #27831: a bottoming function stored in a strict
+-- constructor field.
+--
+-- Different parts of the tag inference machinery didn't agree on weither or not
+-- a bottoming function should be treated as a EPT value, leading to a segfault.
+module Main (main) where
+
+import Control.Exception
+import GHC.Exts.Heap (GenClosure(..), getClosureData)
+import System.Exit
+
+data T = MkT Int !(Int -> Int)
+
+t :: T
+t = MkT 1 (\_ -> error "T27831")
+{-# NOINLINE t #-}
+
+f :: () -> T
+f _ = t
+{-# OPAQUE f #-}
+
+main :: IO ()
+main = do
+ c <- getClosureData t
+ case c of
+ ThunkClosure{} -> putStrLn "FAIL: ThunkClosure" >> exitFailure
+ APClosure{} -> putStrLn "FAIL: APClosure" >> exitFailure
+ _ -> putStrLn "OK: not a thunk"
+
+ case f () of MkT n _ -> print n
+
+ r <- try (evaluate (case t of MkT _ g -> g 1))
+ case r of
+ Left (ErrorCall msg) -> putStrLn ("OK: " ++ msg)
+ Right v -> putStrLn ("FAIL: got " ++ show v) >> exitFailure
=====================================
testsuite/tests/simplStg/should_run/T27831.stdout
=====================================
@@ -0,0 +1,3 @@
+OK: not a thunk
+1
+OK: T27831
=====================================
testsuite/tests/simplStg/should_run/T27857.hs
=====================================
@@ -0,0 +1,45 @@
+{-# LANGUAGE StrictData #-}
+-- Regression test from #27857: We store a function that loops indefinitely
+-- in a strict field.
+module Main (main) where
+import Control.Monad
+
+data Body
+ = BNone
+ | BStream ((Int -> IO ()) -> IO () -> IO ())
+ | BIface (Int -> IO ())
+ | BBuilder String
+ | BFile Int
+
+data Obj = Obj [String] Body Int
+
+newtype Resp = Resp Obj
+
+stream :: Int -> [String] -> ((Int -> IO ()) -> IO () -> IO ()) -> Resp
+stream st hdr b = Resp $ Obj (show st : hdr) (BStream b) 0
+
+noBody :: Int -> [String] -> Resp
+noBody st hdr = Resp $ Obj (show st : hdr) BNone 0
+
+infinite :: Resp
+infinite = stream 200 ["text/plain"] body
+ where
+ body :: (Int -> IO ()) -> IO () -> IO ()
+ body write flush = do
+ let go n = write n *> flush *> go (succ n)
+ go (0 :: Int)
+
+other :: Resp
+other = noBody 404 []
+
+name :: Body -> String
+name b = case b of
+ BNone -> "None"
+ BStream _ -> "Stream"
+ BIface _ -> "Iface"
+ BBuilder _ -> "Builder"
+ BFile _ -> "File"
+
+main :: IO ()
+main = forM_ [("infinite", infinite), ("other", other)] $ \(n, Resp (Obj _ b _)) ->
+ putStrLn $ n ++ "=" ++ name b
=====================================
testsuite/tests/simplStg/should_run/T27857.stdout
=====================================
@@ -0,0 +1,2 @@
+infinite=Stream
+other=None
=====================================
testsuite/tests/simplStg/should_run/all.T
=====================================
@@ -3,13 +3,15 @@
# extra run flags
# expected process return value, if not zero
-# Only compile with optimisation
+# Only run optimised code, native and interpreted:
+# We now generate bytecode from STG so we should test native and bytecode.
def f( name, opts ):
- opts.only_ways = ['optasm']
+ opts.only_ways = ['optasm', 'ghci-opt']
setTestOpts(f)
-test('T9291', normal, compile_and_run, [''])
+# Breakpoint ticks would break CSE which this tests.
+test('T9291', [omit_ways(['ghci']), extra_hc_opts('-fno-break-points')], compile_and_run, [''])
test('T13536', normal, compile_and_run, [''])
test('T13536a',
@@ -20,6 +22,16 @@ test('T13536a',
test('inferTags001', normal, multimod_compile_and_run, ['inferTags001', 'inferTags001_a'])
test('T22042', [extra_files(['T22042a.hs']),only_ways('normal'),unless(have_dynamic(), skip)], makefile_test, ['T22042'])
-test('T23783', normal, multimod_compile_and_run, ['T23783', '-O -v0'])
-test('unpack_enum', normal, compile_and_run, [''])
-test('T27005a', normal, compile_and_run, [''])
+
+# Depends on tag inference being similarly effective for bytecode which it is not yet.
+test('T23783', omit_ghci, multimod_compile_and_run, ['T23783', '-O -v0'])
+
+# Requires UNPACK which needs -O
+test('unpack_enum', omit_ways(['ghci']), compile_and_run, [''])
+
+# Tests that tag inference optimizes well, which is not the case for bytecode or
+# when we insert profiling ticks.
+test('T27005a', omit_ways(ghci_ways + prof_ways), compile_and_run, [''])
+test('T27831', omit_ways(ghci_ways + prof_ways), compile_and_run, [''])
+test('inferTags007', [extra_files(['InferTags007_A.hs'])], multimod_compile_and_run, ['inferTags007', '-O -v0'])
+test('T27857', normal, compile_and_run, [''])
=====================================
testsuite/tests/simplStg/should_run/inferTags007.hs
=====================================
@@ -0,0 +1,109 @@
+{-# LANGUAGE MagicHash #-}
+-- This test sanity checks the tag info we generate for imported binders which
+-- have no real info in the .hi file.
+--
+-- This is important to avoid running into cases where we optimistically
+-- generated better information based on things like type than the
+-- compiled code actually provided. For example GHCi doesn't ensure tagging
+-- of constructors.
+module Main (main) where
+
+import Control.Exception
+import Control.Monad
+import GHC.Exts (Addr#)
+import GHC.Exts.Heap (GenClosure(..), getClosureData)
+import GHC.Internal.Control.Exception.Base (patError)
+import System.Exit
+
+import qualified InferTags007_A as A
+
+data S a = S !a
+
+data P = MkP !(Addr# -> Int)
+
+-- Put the imported values into a strict field of a top level Con
+con_top_fun :: S (Int -> Int)
+con_top_fun = S A.fun
+{-# NOINLINE con_top_fun #-}
+
+con_top_caf :: S [Int]
+con_top_caf = S A.caf
+{-# NOINLINE con_top_caf #-}
+
+con_top_bot :: S (Int -> Int)
+con_top_bot = S A.bot
+{-# NOINLINE con_top_bot #-}
+
+con_top_con :: S (Maybe Int)
+con_top_con = S A.con
+{-# NOINLINE con_top_con #-}
+
+con_top_pat :: P
+con_top_pat = MkP patError
+{-# NOINLINE con_top_pat #-}
+
+-- Put the imported values into a strict field of a Con inside a rhs
+expr_fun :: () -> S (Int -> Int)
+expr_fun _ = S A.fun
+{-# OPAQUE expr_fun #-}
+
+expr_caf :: () -> S [Int]
+expr_caf _ = S A.caf
+{-# OPAQUE expr_caf #-}
+
+expr_bot :: () -> S (Int -> Int)
+expr_bot _ = S A.bot
+{-# OPAQUE expr_bot #-}
+
+expr_con :: () -> S (Maybe Int)
+expr_con _ = S A.con
+{-# OPAQUE expr_con #-}
+
+-- Actually use the field.
+use_S_head :: S [Int] -> Int
+use_S_head (S xs) = case xs of { (y:_) -> y; [] -> -1 }
+{-# NOINLINE use_S_head #-}
+
+use_S_fromJust :: S (Maybe Int) -> Int
+use_S_fromJust (S m) = case m of { Just x -> x; Nothing -> -1 }
+{-# NOINLINE use_S_fromJust #-}
+
+use_S_apply :: S (Int -> Int) -> Int
+use_S_apply (S f) = f 1
+{-# NOINLINE use_S_apply #-}
+
+checkNotThunk :: String -> a -> IO ()
+checkNotThunk name x = do
+ c <- getClosureData x
+ case c of
+ ThunkClosure{} -> putStrLn ("FAIL: " ++ name ++ " is a thunk") >> exitFailure
+ APClosure{} -> putStrLn ("FAIL: " ++ name ++ " is an AP") >> exitFailure
+ _ -> pure ()
+
+-- Pattern matching binds the field itself, so no selector thunk is involved.
+checkField :: String -> S a -> IO ()
+checkField name (S x) = checkNotThunk name x
+{-# NOINLINE checkField #-}
+
+main :: IO ()
+main = do
+ -- Check if the pointer in the strict field is a thunk.
+ checkField "con_top_caf" con_top_caf
+ checkField "con_top_fun" con_top_fun
+ checkField "con_top_bot" con_top_bot
+ checkField "con_top_con" con_top_con
+ case con_top_pat of MkP f -> checkNotThunk "con_top_pat" f
+ checkField "expr_caf" (expr_caf ())
+ checkField "expr_fun" (expr_fun ())
+ checkField "expr_bot" (expr_bot ())
+ checkField "expr_con" (expr_con ())
+
+ -- Actually execute a case on the contents of the field.
+ print (use_S_head con_top_caf, use_S_head (expr_caf ()))
+ print (use_S_fromJust con_top_con, use_S_fromJust (expr_con ()))
+ print (use_S_apply con_top_fun, use_S_apply (expr_fun ()))
+ forM_ [con_top_bot, expr_bot ()] $ \s -> do
+ r <- try (evaluate (use_S_apply s))
+ case r of
+ Left (ErrorCall msg) -> putStrLn ("OK: " ++ msg)
+ Right v -> putStrLn ("FAIL: got " ++ show v) >> exitFailure
=====================================
testsuite/tests/simplStg/should_run/inferTags007.stdout
=====================================
@@ -0,0 +1,5 @@
+(2,2)
+(42,42)
+(2,2)
+OK: bot called
+OK: bot called
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6bbca56943ef68ba32520bcf8712a8f…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6bbca56943ef68ba32520bcf8712a8f…
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-json-link-nodes] `ghc -M`: Add support for link nodes to json output
by Hannes Siebenhandl (@fendor) 22 Oct '26
by Hannes Siebenhandl (@fendor) 22 Oct '26
22 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC
Commits:
8789f654 by fendor at 2026-10-22T10:33:59+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)
+ , ("deps", 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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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/8789f6547bea1e7afc8764e055b1a47…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/8789f6547bea1e7afc8764e055b1a47…
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-json-link-nodes] `ghc -M`: Add support for link nodes to json output
by Hannes Siebenhandl (@fendor) 22 Oct '26
by Hannes Siebenhandl (@fendor) 22 Oct '26
22 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC
Commits:
2ea992c8 by fendor at 2026-10-22T10:01:04+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)
+ , ("deps", 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 ""
+ 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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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/2ea992c8d9acb44eea5ab3f25cd9032…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/2ea992c8d9acb44eea5ab3f25cd9032…
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-json-link-nodes] `ghc -M`: Add support for link nodes to json output
by Hannes Siebenhandl (@fendor) 22 Oct '26
by Hannes Siebenhandl (@fendor) 22 Oct '26
22 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC
Commits:
175beaef by fendor at 2026-10-22T09:55:26+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,12 @@
+section: compiler
+synopsis: Expose link nodes in `ghc -M -dep-json` output
+issues: #24384
+mrs: !16717
+description: {
+ Adds the option to output the dependency information of `ghc -M` in
+ JSON Lines format via the new flag `-dep-json <FILE>`.
+
+ The output is richer than the `Makefile`, since it can encode additional information
+ such as `quote` and `splice` imports (see `-XExplicitLevelImports`).
+ By using JSON Lines, the output of `-dep-json` can be streamed efficiently.
+}
=====================================
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)
+ , ("deps", 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 ""
+ 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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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/175beaefb7d913049e63f2ff9962040…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/175beaefb7d913049e63f2ff9962040…
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-json-link-nodes] `ghc -M`: Add support for link nodes to json output
by Hannes Siebenhandl (@fendor) 22 Oct '26
by Hannes Siebenhandl (@fendor) 22 Oct '26
22 Oct '26
Hannes Siebenhandl pushed to branch wip/fendor/dep-json-link-nodes at Glasgow Haskell Compiler / GHC
Commits:
44ec118a by fendor at 2026-10-22T09:37:26+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.
- - - - -
23 changed files:
- 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:
=====================================
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)
+ , ("deps", 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 ""
+ 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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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","deps":[{"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/44ec118a8112c7762df17c074c84dcb…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/44ec118a8112c7762df17c074c84dcb…
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: Fix a few tag inference bugs.
by Marge Bot (@marge-bot) 21 Oct '26
by Marge Bot (@marge-bot) 21 Oct '26
21 Oct '26
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC
Commits:
6bbca569 by Andreas Klebinger at 2026-10-21T10:01:10+00:00
Fix a few tag inference bugs.
* Treat bottoming functions and nullary join points as functions.
* When generating bytecode don't rely on imported tag sigs.
Fixes #27831, #27841 and #27842.
- - - - -
4ee31b77 by Sylvain Henry at 2026-10-21T18:58:11-04:00
Add GHC.Internal.Unboxed.Do: do-notation for unboxed values (#27944)
QualifiedDo operators can be representation-polymorphic: a do-block is
expanded into ordinary applications before typechecking, and the only
check on a bound variable is the deferred fixed-runtime-rep check. A
representation-polymorphic (>>=) cannot be defined directly, though,
because its body would bind a variable of unknown representation.
The new module provides such operators for State#-passing code as
methods of a class indexed by RuntimeRep, with one instance per
representation. With optimisation the dictionaries disappear and a
block compiles to the usual nested case expressions:
Ubx.do { x <- readWordArray# mba 0# -- x :: Word#
; writeWordArray# mba 1# x -- unit actions are statements
; Ubx.pure x }
Also add users guide documentation and tests.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
99131272 by Sylvain Henry at 2026-10-21T18:58:11-04:00
Bignum: use Ubx.do for State#-passing code (#27944)
Rewrite the State#-passing code of the GHC.Internal.Bignum modules
(Primitives, WordArray, BigNat, Natural, Integer, Backend.Native,
Backend.GMP) with the qualified do-notation of GHC.Internal.Unboxed.Do.
Nested case chains such as
case newWordArray# sz s of { (# s, mwa #) ->
case act mwa s of { s ->
unsafeFreezeByteArray# mwa s }}
become
Ubx.do
mwa <- newWordArray# sz
act mwa
unsafeFreezeByteArray# mwa
The optimised Core is unchanged (compared per top-level binding).
BigNat now enables MonoLocalBinds: local loops written with Ubx.do
must stay monomorphic in the state type, otherwise GHC does not turn
them into join points.
Co-Authored-By: Claude Fable 5.1 <noreply(a)anthropic.com>
- - - - -
2122dc00 by Sylvain Henry at 2026-10-21T18:58:19-04:00
JS: remove archive padding after wasm objects (#25665)
On Darwin hosts, emar (llvm-ar) writes archives in the Darwin format when
the first member is not a known object file, which is the case for the
archives of the JS backend. This format pads each member to 8 bytes with
'\n' and includes the padding in the member size. Wasm objects do not
accept trailing bytes: wasm-ld failed with "section too large".
The JS linker now removes this padding when it extracts a wasm object from
an archive. In addition, Hadrian passes --format=gnu to emar for the JS
target. See Note [Wasm objects in Darwin archives].
Also document the format of Cc (wasm) objects in Note [Cc object format].
Co-Authored-By: Claude Opus 5.5 <noreply(a)anthropic.com>
- - - - -
57 changed files:
- + changelog.d/25665
- + changelog.d/T27831-bottoming-strict-field
- + changelog.d/T27842-bytecode-imported-tags
- + changelog.d/unboxed-do
- compiler/GHC/Stg/EnforceEpt.hs
- compiler/GHC/Stg/EnforceEpt/Rewrite.hs
- compiler/GHC/Stg/EnforceEpt/TagSig.hs
- compiler/GHC/Stg/EnforceEpt/Types.hs
- compiler/GHC/StgToJS/Linker/Linker.hs
- compiler/GHC/StgToJS/Object.hs
- docs/users_guide/exts/qualified_do.rst
- hadrian/src/Settings/Builders/Ar.hs
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/GMP.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/Native.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/BigNat.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Integer.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Natural.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Primitives.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/WordArray.hs
- + libraries/ghc-internal/src/GHC/Internal/Unboxed/Do.hs
- + testsuite/tests/bytecode/T27842.hs
- + testsuite/tests/bytecode/T27842.stdout
- + testsuite/tests/bytecode/T27842A.hs
- + testsuite/tests/bytecode/T27842N.hs
- testsuite/tests/bytecode/all.T
- testsuite/tests/javascript/Makefile
- + testsuite/tests/javascript/T25665.hs
- + testsuite/tests/javascript/T25665.pkg
- + testsuite/tests/javascript/T25665.stdout
- + testsuite/tests/javascript/T25665Lib.hs
- + testsuite/tests/javascript/T25665_c.c
- + testsuite/tests/javascript/T25665_js.js
- testsuite/tests/javascript/all.T
- testsuite/tests/qualifieddo/should_compile/all.T
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.hs
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.stderr
- testsuite/tests/qualifieddo/should_fail/all.T
- + testsuite/tests/qualifieddo/should_fail/qdofail006.hs
- + testsuite/tests/qualifieddo/should_fail/qdofail006.stderr
- testsuite/tests/qualifieddo/should_run/all.T
- + testsuite/tests/qualifieddo/should_run/qdorun008.hs
- + testsuite/tests/qualifieddo/should_run/qdorun008.stdout
- testsuite/tests/simplStg/should_compile/all.T
- + testsuite/tests/simplStg/should_compile/inferTags005.hs
- + testsuite/tests/simplStg/should_compile/inferTags005.stderr
- + testsuite/tests/simplStg/should_compile/inferTags006.hs
- + testsuite/tests/simplStg/should_compile/inferTags006.stderr
- + testsuite/tests/simplStg/should_run/InferTags007_A.hs
- + testsuite/tests/simplStg/should_run/T27831.hs
- + testsuite/tests/simplStg/should_run/T27831.stdout
- + testsuite/tests/simplStg/should_run/T27857.hs
- + testsuite/tests/simplStg/should_run/T27857.stdout
- testsuite/tests/simplStg/should_run/all.T
- + testsuite/tests/simplStg/should_run/inferTags007.hs
- + testsuite/tests/simplStg/should_run/inferTags007.stdout
The diff was not included because it is too large.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2fc01862c6805eec9ef1bca4d1f2ba…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/2fc01862c6805eec9ef1bca4d1f2ba…
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] 3 commits: Make Haddock avoid the PIT
by Wolfgang Jeltsch (@jeltsch) 21 Oct '26
by Wolfgang Jeltsch (@jeltsch) 21 Oct '26
21 Oct '26
Wolfgang Jeltsch pushed to branch wip/jeltsch/smaller-pit at Glasgow Haskell Compiler / GHC
Commits:
9db804f7 by Wolfgang Jeltsch at 2026-10-22T00:51:26+03:00
Make Haddock avoid the PIT
To this end, Haddock has been using `loadSysInterface` to fetch module
interfaces. The `loadSysInterface` operation is very complex, containing
many bits that do not seem to be needed by Haddock. In particular,
storing interface data in and potentially retrieving it from the PIT
seems to be superfluous. This interaction with the PIT limits how much
we can statically shrink the amount of information that is stored in the
PIT. Therefore, we drop the use of `loadSysInterface` and implement the
necessary bits of interface fetching directly in `loadHiFile`, where
`loadSysInterface` use to be invoked.
- - - - -
2c716577 by Wolfgang Jeltsch at 2026-10-22T00:52:07+03:00
Statically enforce module interface reduction for the PIT
`GHC.Iface.Load.loadInterface` used to replace certain fields of each
fetched module interface by `panic` applications before storing it in
the PIT. With the present change, the absence of those fields in the PIT
is guaranteed statically.
See #27044 for the bigger picture.
- - - - -
fbafcc41 by Wolfgang Jeltsch at 2026-10-22T00:52:07+03:00
Make `IfaceBinHandle` a type synonym family
This brings the handling of `IfaceBinHandle` somewhat in line with how
other phase-dependent data is handled. Note that `mi_hi_bytes_` now
includes the `Strict.Maybe` layer itself, independently of the phase.
This is necessary, because with a type family instead of a GADT
`clear_mi_hi_bytes` cannot determine the phase at runtime anymore and
thus has to set `mi_hi_bytes_` to `Strict.Nothing` for every phase.
- - - - -
25 changed files:
- compiler/GHC/Driver/Downsweep.hs
- compiler/GHC/Driver/Env.hs
- compiler/GHC/Driver/Main/Interactive.hs
- compiler/GHC/Driver/Main/Passes.hs
- compiler/GHC/Driver/Plugins.hs
- compiler/GHC/Driver/Session/Inspect.hs
- compiler/GHC/HsToCore/Usage.hs
- compiler/GHC/Iface/Binary.hs
- compiler/GHC/Iface/Load.hs
- compiler/GHC/Iface/Load.hs-boot
- compiler/GHC/Iface/Make.hs
- compiler/GHC/Iface/Recomp.hs
- compiler/GHC/Rename/Names.hs
- compiler/GHC/Rename/Utils.hs
- compiler/GHC/Runtime/Loader.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Gen/Splice.hs
- compiler/GHC/Tc/Instance/Family.hs
- compiler/GHC/Tc/Module.hs
- compiler/GHC/Unit/External.hs
- compiler/GHC/Unit/Module/ModIface.hs
- ghc/Main.hs
- utils/count-deps/Main.hs
- utils/haddock/haddock-api/src/Haddock/Interface.hs
Changes:
=====================================
compiler/GHC/Driver/Downsweep.hs
=====================================
@@ -688,7 +688,8 @@ expandFixedModuleNode key loc = do
-- part of the compiler.
lookupIfaceByModuleHsc hsc_env (mnkToModule key) >>= \case
Just iface -> return (M.Succeeded iface)
- Nothing -> readIface (hsc_hooks hsc_env) (hsc_logger hsc_env) (hsc_dflags hsc_env) (hsc_NC hsc_env) (mnkToModule key) (ml_hi_file loc)
+ Nothing -> fmap shrinkModIface <$>
+ readIface (hsc_hooks hsc_env) (hsc_logger hsc_env) (hsc_dflags hsc_env) (hsc_NC hsc_env) (mnkToModule key) (ml_hi_file loc)
case read_result of
M.Succeeded iface -> do
-- Computer information about this node
=====================================
compiler/GHC/Driver/Env.hs
=====================================
@@ -372,10 +372,10 @@ lookupIfaceByModule
:: HomeUnitGraph
-> PackageIfaceTable
-> Module
- -> IO (Maybe ModIface)
+ -> IO (Maybe RetainedModIface)
lookupIfaceByModule hug pit mod
= HUG.lookupHugByModule mod hug >>= pure . \case
- Just hm -> Just (hm_iface hm)
+ Just hm -> Just (shrinkModIface (hm_iface hm))
Nothing -> lookupModuleEnv pit mod
-- If the module does come from the home package, why do we look in the PIT as well?
-- (a) In OneShot mode, even home-package modules accumulate in the PIT
@@ -384,7 +384,7 @@ lookupIfaceByModule hug pit mod
-- We could eliminate (b) if we wanted, by making GHC.Prim belong to a package
-- of its own, but it doesn't seem worth the bother.
-lookupIfaceByModuleHsc :: HscEnv -> Module -> IO (Maybe ModIface)
+lookupIfaceByModuleHsc :: HscEnv -> Module -> IO (Maybe RetainedModIface)
lookupIfaceByModuleHsc hsc_env mod = do
eps <- hscEPS hsc_env
lookupIfaceByModule (hsc_HUG hsc_env) (eps_PIT eps) mod
=====================================
compiler/GHC/Driver/Main/Interactive.hs
=====================================
@@ -148,7 +148,7 @@ hscIsGHCiMonad :: HscEnv -> String -> IO Name
hscIsGHCiMonad hsc_env name
= runHsc hsc_env $ ioMsgMaybe $ hoistTcRnMessage $ isGHCiMonad hsc_env name
-hscGetModuleInterface :: HscEnv -> Module -> IO ModIface
+hscGetModuleInterface :: HscEnv -> Module -> IO RetainedModIface
hscGetModuleInterface hsc_env0 mod = runInteractiveHsc hsc_env0 $ do
hsc_env <- getHscEnv
ioMsgMaybe $ hoistTcRnMessage $ getModuleInterface hsc_env mod
@@ -270,7 +270,7 @@ hscCheckSafe' m l = do
_ | isHomeModule home_unit mod -> True
_ -> isUnitTrusted unit_state (moduleUnit m)
- lookup' :: Module -> Hsc (Maybe ModIface)
+ lookup' :: Module -> Hsc (Maybe RetainedModIface)
lookup' m = do
hsc_env <- getHscEnv
iface <- liftIO $ lookupIfaceByModuleHsc hsc_env m
=====================================
compiler/GHC/Driver/Main/Passes.hs
=====================================
@@ -1335,7 +1335,7 @@ hscCheckSafe' m l = do
_ | isHomeModule home_unit mod -> True
_ -> isUnitTrusted unit_state (moduleUnit m)
- lookup' :: Module -> Hsc (Maybe ModIface)
+ lookup' :: Module -> Hsc (Maybe RetainedModIface)
lookup' m = do
hsc_env <- getHscEnv
iface <- liftIO $ lookupIfaceByModuleHsc hsc_env m
=====================================
compiler/GHC/Driver/Plugins.hs
=====================================
@@ -224,7 +224,7 @@ data PluginWithArgs = PluginWithArgs
data LoadedPlugin = LoadedPlugin
{ lpPlugin :: PluginWithArgs
-- ^ the actual plugin together with its commandline arguments
- , lpModule :: ModIface
+ , lpModule :: RetainedModIface
-- ^ the module containing the plugin
}
=====================================
compiler/GHC/Driver/Session/Inspect.hs
=====================================
@@ -87,7 +87,7 @@ data ModuleInfo = ModuleInfo {
minf_type_env :: TypeEnv,
minf_exports :: [AvailInfo],
minf_instances :: [ClsInst],
- minf_iface :: Maybe ModIface,
+ minf_iface :: Maybe RetainedModIface,
minf_safe :: SafeHaskellMode,
minf_modBreaks :: Maybe InternalModBreaks
}
@@ -140,7 +140,7 @@ getHomeModuleInfo hsc_env mdl =
Nothing -> return Nothing
Just hmi -> do
let details = hm_details hmi
- iface = hm_iface hmi
+ iface = shrinkModIface (hm_iface hmi)
return (Just (ModuleInfo {
minf_type_env = md_types details,
minf_exports = md_exports details,
@@ -187,7 +187,7 @@ modInfoLookupName minf name = withSession $ \hsc_env -> do
Just tyThing -> return (Just tyThing)
Nothing -> liftIO (lookupType hsc_env name)
-modInfoIface :: ModuleInfo -> Maybe ModIface
+modInfoIface :: ModuleInfo -> Maybe RetainedModIface
modInfoIface = minf_iface
-- | Retrieve module safe haskell mode
=====================================
compiler/GHC/HsToCore/Usage.hs
=====================================
@@ -276,7 +276,7 @@ mk_mod_usage_info uc home_unit home_unit_ids this_mod direct_imports imp_decls u
-- a) we used something from it; has something in used_names
-- b) we imported it, even if we used nothing from it
-- (need to recompile if its export list changes: export_fprint)
- mkUsage :: Module -> ModIface -> Maybe Usage
+ mkUsage :: Module -> RetainedModIface -> Maybe Usage
mkUsage mod iface
| toUnitId (moduleUnit mod) `Set.notMember` home_unit_ids
= Just $ UsagePackageModule{ usg_mod = mod,
@@ -302,9 +302,9 @@ mk_mod_usage_info uc home_unit home_unit_ids this_mod direct_imports imp_decls u
usg_entities = Map.toList ent_hashs,
usg_safe = imp_safe }
where
- finsts_mod = mi_finsts iface
+ finsts_mod = mi_abi_finsts (mi_abi_hashes iface)
hash_env = mi_hash_fn iface
- mod_hash = mi_mod_hash iface
+ mod_hash = mi_abi_mod_hash (mi_abi_hashes iface)
imported_exports
= if not depend_on_exports
then Nothing
@@ -312,9 +312,9 @@ mk_mod_usage_info uc home_unit home_unit_ids this_mod direct_imports imp_decls u
Just $
HomeModImport
{ hmiu_orphanLikeHash
- = mi_orphan_like_hash iface
+ = mi_abi_orphan_like_hash (mi_abi_hashes iface)
, hmiu_importedAvails
- = moduleImportedAvails mod (mi_export_avails_hash iface) imp_decls
+ = moduleImportedAvails mod (mi_abi_export_avails_hash (mi_abi_hashes iface)) imp_decls
}
by_is_safe (ImportedByUser imv) = imv_is_safe imv
=====================================
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/Load.hs
=====================================
@@ -308,7 +308,7 @@ loadKnownKeyOccMaps
known_key_maps :: Module -> IfM lcl KnownKeyNameMaps
known_key_maps mod
= do { iface <- loadInterfaceWithException doc mod ImportBySystem
- ; let !abi_hash = mi_mod_hash iface
+ ; let !abi_hash = mi_abi_mod_hash (mi_abi_hashes iface)
; eps <- getEps
; case lookupModuleEnv (eps_known_keys eps) mod of
Just (cached_hash, kk_maps)
@@ -321,7 +321,7 @@ loadKnownKeyOccMaps
(abi_hash, kk_maps) }
; return kk_maps } }
- build_maps :: ModIface -> KnownKeyNameMaps
+ build_maps :: RetainedModIface -> KnownKeyNameMaps
build_maps iface = (kk_map, occ_map)
where
kk_map :: UniqFM KnownKey Name
@@ -628,7 +628,7 @@ loadSrcInterface :: SDoc
-> ModuleName
-> IsBootInterface -- {-# SOURCE #-} ?
-> PkgQual -- "package", if any
- -> RnM ModIface
+ -> RnM RetainedModIface
loadSrcInterface doc scope mod want_boot maybe_pkg
= do { res <- loadSrcInterface_maybe doc scope mod want_boot maybe_pkg
@@ -648,7 +648,7 @@ loadSrcInterface_maybe :: SDoc
-> ModuleName
-> IsBootInterface -- {-# SOURCE #-} ?
-> PkgQual -- "package", if any
- -> RnM (MaybeErr MissingInterfaceError ModIface)
+ -> RnM (MaybeErr MissingInterfaceError RetainedModIface)
loadSrcInterface_maybe doc scope mod want_boot maybe_pkg
-- We must first find which Module this import refers to. This involves
@@ -667,7 +667,7 @@ loadSrcInterface_maybe doc scope mod want_boot maybe_pkg
-- rare operation, but in particular it is used to load orphan modules
-- in order to pull their instances into the global package table and to
-- handle some operations in GHCi).
-loadModuleInterface :: SDoc -> Module -> TcM ModIface
+loadModuleInterface :: SDoc -> Module -> TcM RetainedModIface
loadModuleInterface doc mod = initIfaceTcRn (loadSysInterface doc mod)
-- | Load interfaces for a collection of modules.
@@ -681,7 +681,7 @@ loadModuleInterfaces doc mods
-- | Loads the interface for a given Name.
-- Should only be called for an imported name;
-- otherwise loadSysInterface may not find the interface
-loadInterfaceForName :: SDoc -> Name -> TcRn ModIface
+loadInterfaceForName :: SDoc -> Name -> TcRn RetainedModIface
loadInterfaceForName doc name
= do { when debugIsOn $ -- Check pre-condition
do { this_mod <- getModule
@@ -690,7 +690,7 @@ loadInterfaceForName doc name
initIfaceTcRn $ loadSysInterface doc (nameModule name) }
-- | Loads the interface for a given Module.
-loadInterfaceForModule :: SDoc -> Module -> TcRn ModIface
+loadInterfaceForModule :: SDoc -> Module -> TcRn RetainedModIface
loadInterfaceForModule doc m
= do
-- Should not be called with this module
@@ -724,23 +724,23 @@ loadWiredInHomeIface name
------------------
-- | Loads a system interface and throws an exception if it fails
-loadSysInterface :: SDoc -> Module -> IfM lcl ModIface
+loadSysInterface :: SDoc -> Module -> IfM lcl RetainedModIface
loadSysInterface doc mod_name = loadInterfaceWithException doc mod_name ImportBySystem
------------------
-- | Loads a user interface and throws an exception if it fails. The first parameter indicates
-- whether we should import the boot variant of the module
-loadUserInterface :: IsBootInterface -> SDoc -> Module -> IfM lcl ModIface
+loadUserInterface :: IsBootInterface -> SDoc -> Module -> IfM lcl RetainedModIface
loadUserInterface is_boot doc mod_name
= loadInterfaceWithException doc mod_name (ImportByUser is_boot)
-loadPluginInterface :: SDoc -> Module -> IfM lcl ModIface
+loadPluginInterface :: SDoc -> Module -> IfM lcl RetainedModIface
loadPluginInterface doc mod_name
= loadInterfaceWithException doc mod_name ImportByPlugin
------------------
-- | A wrapper for 'loadInterface' that throws an exception if it fails
-loadInterfaceWithException :: SDoc -> Module -> WhereFrom -> IfM lcl ModIface
+loadInterfaceWithException :: SDoc -> Module -> WhereFrom -> IfM lcl RetainedModIface
loadInterfaceWithException doc mod_name where_from
= do
dflags <- getDynFlags
@@ -749,7 +749,7 @@ loadInterfaceWithException doc mod_name where_from
------------------
loadInterface :: SDoc -> Module -> WhereFrom
- -> IfM lcl (MaybeErr MissingInterfaceError ModIface)
+ -> IfM lcl (MaybeErr MissingInterfaceError RetainedModIface)
-- loadInterface looks in both the HPT and PIT for the required interface
-- If not found, it loads it, and puts it in the PIT (always).
@@ -796,7 +796,7 @@ loadInterface doc_str mod from
liftIO $ computeInterface hsc_env doc_str hi_boot_file mod
; case read_result of {
Failed err -> do
- { let fake_iface = emptyFullModIface mod
+ { let fake_iface = shrinkModIface (emptyFullModIface mod)
; updateEps_ $ \eps ->
eps { eps_PIT = extendModuleEnv (eps_PIT eps) (mi_module fake_iface) fake_iface }
@@ -856,16 +856,7 @@ loadInterface doc_str mod from
; new_eps_complete_matches <- tcIfaceCompleteMatches (mi_complete_matches iface)
; purged_hsc_env <- getTopEnv
- ; let final_iface = iface
- & set_mi_decls (panic "No mi_decls in PIT")
- & set_mi_insts (panic "No mi_insts in PIT")
- & set_mi_defaults (panic "No mi_defaults in PIT")
- & set_mi_fam_insts (panic "No mi_fam_insts in PIT")
- & set_mi_rules (panic "No mi_rules in PIT")
- & set_mi_anns (panic "No mi_anns in PIT")
- & set_mi_simplified_core (panic "No mi_simplified_core in PIT")
-
- bad_boot = mi_boot iface == IsBoot
+ ; let bad_boot = mi_boot iface == IsBoot
&& isJust (lookupKnotVars (if_rec_types gbl_env) mod)
-- Warn against an EPS-updating import
-- of one's own boot file! (one-shot only)
@@ -892,7 +883,7 @@ loadInterface doc_str mod from
then eps { eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls }
else
eps {
- eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface,
+ eps_PIT = extendModuleEnv (eps_PIT eps) mod (shrinkModIface iface),
eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls,
eps_iface_bytecode = add_bytecode (eps_iface_bytecode eps),
eps_rule_base = extendRuleBaseList (eps_rule_base eps)
@@ -921,10 +912,8 @@ loadInterface doc_str mod from
eps_defaults = extendModuleEnv (eps_defaults eps) mod new_eps_defaults
}
- ; -- invoke plugins with *full* interface, not final_iface, to ensure
- -- that plugins have access to declarations, etc.
- res <- withPlugins (hsc_plugins hsc_env) (\p -> interfaceLoadAction p) iface
- ; return (Succeeded res)
+ ; res <- withPlugins (hsc_plugins hsc_env) (\p -> interfaceLoadAction p) iface
+ ; return (Succeeded (shrinkModIface res))
}}}}
{- Note [Loading your own hi-boot file]
=====================================
compiler/GHC/Iface/Load.hs-boot
=====================================
@@ -2,7 +2,7 @@ module GHC.Iface.Load where
import GHC.Unit.Module (Module)
import GHC.Tc.Utils.Monad (IfM)
-import GHC.Unit.Module.ModIface (ModIface)
+import GHC.Unit.Module.ModIface (RetainedModIface)
import GHC.Utils.Outputable (SDoc)
-loadSysInterface :: SDoc -> Module -> IfM lcl ModIface
+loadSysInterface :: SDoc -> Module -> IfM lcl RetainedModIface
=====================================
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/Iface/Recomp.hs
=====================================
@@ -707,7 +707,7 @@ checkDependencies hsc_env summary iface
return $ needsRecompileBecause $ ModulePackageChanged new_name
-needInterface :: Module -> (ModIface -> IO RecompileRequired)
+needInterface :: Module -> (RetainedModIface -> IO RecompileRequired)
-> IfG RecompileRequired
needInterface mod continue
= do
@@ -727,7 +727,7 @@ needBytecode mod continue
Nothing -> return $ NeedsRecompile MustCompile
Just mbc -> liftIO $ continue mbc
-tryGetModIface :: String -> Module -> IfG (Maybe ModIface)
+tryGetModIface :: String -> Module -> IfG (Maybe RetainedModIface)
tryGetModIface doc_msg mod
= do -- Load the imported interface if possible
logger <- getLogger
@@ -779,7 +779,7 @@ checkModUsage _ UsagePackageModule{
logger <- getLogger
needInterface mod $ \iface -> do
let reason = ModuleChanged (moduleName mod)
- checkModuleFingerprint logger reason old_mod_hash (mi_mod_hash iface)
+ checkModuleFingerprint logger reason old_mod_hash (mi_abi_mod_hash (mi_abi_hashes iface))
-- We only track the ABI hash of package modules, rather than
-- individual entity usages, so if the ABI hash changes we must
-- recompile. This is safe but may entail more recompilation when
@@ -789,7 +789,7 @@ checkModUsage _ UsageMergedRequirement{ usg_mod = mod, usg_mod_hash = old_mod_ha
logger <- getLogger
needInterface mod $ \iface -> do
let reason = ModuleChangedRaw (moduleName mod)
- checkModuleFingerprint logger reason old_mod_hash (mi_mod_hash iface)
+ checkModuleFingerprint logger reason old_mod_hash (mi_abi_mod_hash (mi_abi_hashes iface))
checkModUsage _ UsageHomeModuleBytecode{ usg_mod_name = mod_name
, usg_unit_id = uid
, usg_bytecode_hash = old_bytecode_hash } = do
@@ -810,7 +810,7 @@ checkModUsage _ UsageHomeModule{
logger <- getLogger
needInterface mod $ \iface -> do
let
- new_mod_hash = mi_mod_hash iface
+ new_mod_hash = mi_abi_mod_hash (mi_abi_hashes iface)
new_decl_hash = mi_hash_fn iface
reason = ModuleChanged (moduleName mod)
@@ -864,7 +864,7 @@ checkModUsage fc UsageDirectory{ usg_dir_path = dir,
-- Does this require recompilation?
--
-- See Note [When to recompile when export lists change?]
-checkHomeModImport :: Logger -> RecompReason -> Maybe HomeModImport -> ModIface -> IO RecompileRequired
+checkHomeModImport :: Logger -> RecompReason -> Maybe HomeModImport -> RetainedModIface -> IO RecompileRequired
checkHomeModImport _ _ Nothing _ = return UpToDate
checkHomeModImport logger reason
(Just (HomeModImport old_orphan_like_hash old_avails))
@@ -901,8 +901,8 @@ checkHomeModImport logger reason
2 (ppr changes)
return $ needsRecompileBecause reason
where
- new_orphan_like_hash = mi_orphan_like_hash iface
- new_avails_hash = mi_export_avails_hash iface
+ new_orphan_like_hash = mi_abi_orphan_like_hash (mi_abi_hashes iface)
+ new_avails_hash = mi_abi_export_avails_hash (mi_abi_hashes iface)
new_exports = mi_exports iface
-- | The exported avails of a module have changed. Should this cause recompilation
@@ -1617,7 +1617,7 @@ getOrphanHashes hsc_env mods = do
get_orph_hash mod = do
iface <- initIfaceLoad hsc_env . withIfaceErr ctx
$ loadInterface (text "getOrphanHashes") mod ImportBySystem
- return (mi_orphan_hash iface)
+ return (mi_abi_orphan_hash (mi_abi_hashes iface))
mapM get_orph_hash mods
=====================================
compiler/GHC/Rename/Names.hs
=====================================
@@ -517,7 +517,7 @@ renamePkgQual unit_env mn mb_pkg = case mb_pkg of
-- interface, but without 'imp_mods'.
calculateAvails :: HomeUnit
-> S.Set UnitId
- -> ModIface
+ -> RetainedModIface
-> IsSafeImport
-> IsBootInterface
-> ImportedBy
@@ -525,8 +525,8 @@ calculateAvails :: HomeUnit
calculateAvails home_unit other_home_units iface mod_safe' want_boot imported_by =
let imp_mod = mi_module iface
imp_sem_mod= mi_semantic_module iface
- orph_iface = mi_orphan iface
- has_finsts = mi_finsts iface
+ orph_iface = mi_abi_orphan (mi_abi_hashes iface)
+ has_finsts = mi_abi_finsts (mi_abi_hashes iface)
deps = mi_deps iface
trust = getSafeMode $ mi_trust iface
trust_pkg = mi_trust_pkg iface
@@ -1209,7 +1209,7 @@ gresFromAvail hsc_env prov avail =
gresFromAvails :: HscEnv -> Maybe ImportSpec -> [AvailInfo] -> [GlobalRdrElt]
gresFromAvails hsc_env prov = concatMap (gresFromAvail hsc_env prov)
-importsFromIface :: HscEnv -> ModIface -> ImpDeclSpec -> Maybe NameSet -> GlobalRdrEnv
+importsFromIface :: HscEnv -> RetainedModIface -> ImpDeclSpec -> Maybe NameSet -> GlobalRdrEnv
importsFromIface hsc_env iface decl_spec hidden = mkGlobalRdrEnv $ case hidden of
Nothing -> all_gres
Just hidden_names -> filter (not . (`elemNameSet` hidden_names) . greName) all_gres
@@ -1225,7 +1225,7 @@ rnNamespaceSpecifier (DataNamespaceSpecifier x) = DataNamespaceSpecifier x
filterImports
:: HasDebugCallStack
=> HscEnv
- -> ModIface
+ -> RetainedModIface
-> ImpDeclSpec
-- ^ Import spec
-> Maybe (ImportListInterpretation, [LIE GhcPs])
@@ -2327,7 +2327,7 @@ getMinimalImports ie_decls
where
doc = text "Compute minimal imports for" <+> ppr decl
- to_ie :: GlobalRdrEnv -> ModIface -> AvailInfo -> RnM [IE GhcRn]
+ to_ie :: GlobalRdrEnv -> RetainedModIface -> AvailInfo -> RnM [IE GhcRn]
-- The main trick here is that if we're importing all the constructors
-- we want to say "T(..)", but if we're importing only a subset we want
-- to say "T(A,B,C)". So we have to find out what the module exports.
@@ -2529,7 +2529,7 @@ DRFPatSynExport for a test of this.
-}
badImportItemErr
- :: ModIface -> ImpDeclSpec -> IE GhcPs
+ :: RetainedModIface -> ImpDeclSpec -> IE GhcPs
-> IsSubordinateError
-> [AvailInfo]
-> TcRn (NonEmpty ImportLookupReason)
=====================================
compiler/GHC/Rename/Utils.hs
=====================================
@@ -534,7 +534,7 @@ warnIfDeclDeprecated gre@(GRE { gre_imp = iss })
definedMod = moduleName $ assertPpr (isExternalName name) (ppr name) (nameModule name)
doc = text "The name" <+> quotes (ppr occ) <+> text "is mentioned explicitly"
-lookupImpDeclDeprec :: ModIface -> GlobalRdrElt -> Maybe (WarningTxt GhcRn)
+lookupImpDeclDeprec :: RetainedModIface -> GlobalRdrElt -> Maybe (WarningTxt GhcRn)
lookupImpDeclDeprec iface gre
-- Bleat if the thing, or its parent, is warn'd
= mi_decl_warn_fn iface (greOccName gre) `mplus`
=====================================
compiler/GHC/Runtime/Loader.hs
=====================================
@@ -190,7 +190,7 @@ checkExternalInterpreter hsc_env = case interpInstance <$> hsc_interp hsc_env of
-> throwIO (InstallationError "Plugins require -fno-external-interpreter")
_ -> pure ()
-loadPlugin' :: OccName -> Name -> HscEnv -> ModuleName -> IO (a, ModIface, [LinkableUsage], PkgsLoaded)
+loadPlugin' :: OccName -> Name -> HscEnv -> ModuleName -> IO (a, RetainedModIface, [LinkableUsage], PkgsLoaded)
loadPlugin' occ_name plugin_name hsc_env mod_name
= do { let plugin_rdr_name = mkRdrQual mod_name occ_name
dflags = hsc_dflags hsc_env
@@ -341,7 +341,7 @@ lessUnsafeCoerce logger context what = do
-- Need the module as well to record information in the interface file
lookupRdrNameInModuleForPlugins :: HasDebugCallStack
=> HscEnv -> ModuleName -> RdrName
- -> IO (Maybe (Name, ModIface))
+ -> IO (Maybe (Name, RetainedModIface))
lookupRdrNameInModuleForPlugins hsc_env mod_name rdr_name = do
let dflags = hsc_dflags hsc_env
-- First find the unit the module resides in by searching exposed units and home modules
=====================================
compiler/GHC/Tc/Errors/Ppr.hs
=====================================
@@ -6386,7 +6386,7 @@ pprImportLookup :: ImportLookupReason -> SDoc
pprImportLookup = \case
ImportLookupBad k iface decl_spec ie _exts ->
let
- pprImpDeclSpec :: ModIface -> ImpDeclSpec -> SDoc
+ pprImpDeclSpec :: RetainedModIface -> ImpDeclSpec -> SDoc
pprImpDeclSpec iface decl_spec =
quotes (ppr (moduleName $ is_mod decl_spec)) <+> case mi_boot iface of
IsBoot -> text "(hi-boot interface)"
=====================================
compiler/GHC/Tc/Errors/Types.hs
=====================================
@@ -215,7 +215,7 @@ import GHC.Types.DefaultEnv (ClassDefaults)
import GHC.Unit.Types (Module, ModuleName)
import GHC.Unit.State (UnitState)
-import GHC.Unit.Module.ModIface (ModIface)
+import GHC.Unit.Module.ModIface (RetainedModIface)
import GHC.Utils.Outputable
@@ -6634,7 +6634,7 @@ data ImportLookupReason where
T9006, T11071, T9905fail2, T5385, T10668
-}
ImportLookupBad :: BadImportKind
- -> ModIface
+ -> RetainedModIface
-> ImpDeclSpec
-> IE GhcPs
-> ImportLookupExtensions
=====================================
compiler/GHC/Tc/Gen/Splice.hs
=====================================
@@ -1693,7 +1693,7 @@ lookupArgDoc i nm = do
_ -> pure Nothing
-- | Returns the module a Name belongs to, if it is isn't local.
-getExternalModIface :: Name -> TcM (Maybe ModIface)
+getExternalModIface :: Name -> TcM (Maybe RetainedModIface)
getExternalModIface nm = do
isLocal <- nameIsLocalOrFrom <$> getModule <*> pure nm
if isLocal
=====================================
compiler/GHC/Tc/Instance/Family.hs
=====================================
@@ -308,7 +308,7 @@ checkFamInstConsistency directlyImpMods
ifc <- modIface mod
deps <- dep_finsts . mi_deps <$> modIface mod
pure $
- if mi_finsts ifc
+ if mi_abi_finsts (mi_abi_hashes ifc)
then mod:deps
else deps
=====================================
compiler/GHC/Tc/Module.hs
=====================================
@@ -2966,7 +2966,7 @@ externaliseAndTidyId this_mod id
-- a package module with an interface on disk. If neither of these is
-- true, then the result will be an error indicating the interface
-- could not be found.
-getModuleInterface :: HscEnv -> Module -> IO (Messages TcRnMessage, Maybe ModIface)
+getModuleInterface :: HscEnv -> Module -> IO (Messages TcRnMessage, Maybe RetainedModIface)
getModuleInterface hsc_env mod
= runTcInteractive NoTcMPlugins hsc_env $
loadModuleInterface (text "getModuleInterface") mod
=====================================
compiler/GHC/Unit/External.hs
=====================================
@@ -49,7 +49,7 @@ type PackageAnnEnv = AnnEnv
type PackageCompleteMatches = CompleteMatches
-- | Helps us find information about modules in the imported packages
-type PackageIfaceTable = ModuleEnv ModIface
+type PackageIfaceTable = ModuleEnv RetainedModIface
-- Domain = modules in the imported packages
-- | Constructs an empty PackageIfaceTable
=====================================
compiler/GHC/Unit/Module/ModIface.hs
=====================================
@@ -2,6 +2,7 @@
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE DeriveAnyClass #-}
+{-# LANGUAGE RecordWildCards #-}
module GHC.Unit.Module.ModIface
( ModIface
@@ -68,19 +69,31 @@ module GHC.Unit.Module.ModIface
, set_mi_fix_fn
, set_mi_hash_fn
, completePartialModIface
- , IfaceBinHandle(..)
+ , shrinkModIface
+ , IfaceBinHandle
, PartialModIface
+ , RetainedModIface
, IfaceAbiHashes (..)
, IfaceSelfRecomp (..)
, IfaceCache (..)
, IfaceSimplifiedCore (..)
, withSelfRecomp
- , IfaceDeclExts
- , IfaceAbiHashesExts
+ , IfaceSelfRecompData
+ , IfaceSimplifiedCoreData
+ , ExtensibleFieldsData
+ , IfaceAnnotationsData
+ , IfaceDeclsData
+ , IfaceDefaultsData
+ , IfaceClsInstsData
+ , IfaceFamInstsData
+ , IfaceRulesData
+ , IfaceCompleteMatchesData
+ , IfaceAbiHashesData
, IfaceExport
, IfacePublic_(..)
, IfacePublic
, PartialIfacePublic
+ , RetainedIfacePublic
, IfaceModInfo(..)
, WhetherHasOrphans
, WhetherHasFamInst
@@ -147,6 +160,7 @@ import GHC.Utils.Binary
import Control.DeepSeq
import Control.Exception
+import Data.Void (Void)
{- Note [Interface file stages]
@@ -167,9 +181,11 @@ We can build a full interface file two ways:
type PartialModIface = ModIface_ 'ModIfaceCore
type ModIface = ModIface_ 'ModIfaceFinal
+type RetainedModIface = ModIface_ 'ModIfaceRetained
type PartialIfacePublic = IfacePublic_ 'ModIfaceCore
type IfacePublic = IfacePublic_ 'ModIfaceFinal
+type RetainedIfacePublic = IfacePublic_ 'ModIfaceRetained
-- | Extends a PartialModIface with hashes of the ABI.
--
@@ -212,34 +228,78 @@ data IfaceCache = IfaceCache
data ModIfacePhase
= ModIfaceCore
- -- ^ Partial interface built based on output of core pipeline.
+ -- ^ Partial interface built from the output of the core pipeline
| ModIfaceFinal
-
--- | Selects a IfaceDecl representation.
--- For fully instantiated interfaces we also maintain
--- a fingerprint, which is used for recompilation checks.
-type family IfaceDeclExts (phase :: ModIfacePhase) = decl | decl -> phase where
- IfaceDeclExts 'ModIfaceCore = IfaceDecl
- IfaceDeclExts 'ModIfaceFinal = (Fingerprint, IfaceDecl)
-
-type family IfaceAbiHashesExts (phase :: ModIfacePhase) = bk | bk -> phase where
- IfaceAbiHashesExts 'ModIfaceCore = ()
- IfaceAbiHashesExts 'ModIfaceFinal = IfaceAbiHashes
+ -- ^ Complete interface
+ | ModIfaceRetained
+ -- ^ Interface shrunk to the parts that may be retained
-- | 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
-
-
-withSelfRecomp :: ModIface_ phase -> r -> (IfaceSelfRecomp -> r) -> r
+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
+ IfaceSelfRecompData 'ModIfaceFinal = Maybe IfaceSelfRecomp
+ IfaceSelfRecompData 'ModIfaceRetained = ()
+
+type family IfaceSimplifiedCoreData (phase :: ModIfacePhase) where
+ IfaceSimplifiedCoreData 'ModIfaceCore = Maybe IfaceSimplifiedCore
+ IfaceSimplifiedCoreData 'ModIfaceFinal = Maybe IfaceSimplifiedCore
+ IfaceSimplifiedCoreData 'ModIfaceRetained = ()
+
+type family ExtensibleFieldsData (phase :: ModIfacePhase) where
+ ExtensibleFieldsData 'ModIfaceCore = ExtensibleFields
+ ExtensibleFieldsData 'ModIfaceFinal = ExtensibleFields
+ ExtensibleFieldsData 'ModIfaceRetained = ()
+
+type family IfaceAnnotationsData (phase :: ModIfacePhase) where
+ IfaceAnnotationsData 'ModIfaceCore = [IfaceAnnotation]
+ IfaceAnnotationsData 'ModIfaceFinal = [IfaceAnnotation]
+ IfaceAnnotationsData 'ModIfaceRetained = ()
+
+-- | The fingerprint in the 'ModIfaceFinal' case is used for recompilation
+-- checks.
+type family IfaceDeclsData (phase :: ModIfacePhase) where
+ IfaceDeclsData 'ModIfaceCore = [IfaceDecl]
+ IfaceDeclsData 'ModIfaceFinal = [(Fingerprint, IfaceDecl)]
+ IfaceDeclsData 'ModIfaceRetained = ()
+
+type family IfaceDefaultsData (phase :: ModIfacePhase) where
+ IfaceDefaultsData 'ModIfaceCore = [IfaceDefault]
+ IfaceDefaultsData 'ModIfaceFinal = [IfaceDefault]
+ IfaceDefaultsData 'ModIfaceRetained = ()
+
+type family IfaceClsInstsData (phase :: ModIfacePhase) where
+ IfaceClsInstsData 'ModIfaceCore = [IfaceClsInst]
+ IfaceClsInstsData 'ModIfaceFinal = [IfaceClsInst]
+ IfaceClsInstsData 'ModIfaceRetained = ()
+
+type family IfaceFamInstsData (phase :: ModIfacePhase) where
+ IfaceFamInstsData 'ModIfaceCore = [IfaceFamInst]
+ IfaceFamInstsData 'ModIfaceFinal = [IfaceFamInst]
+ IfaceFamInstsData 'ModIfaceRetained = ()
+
+type family IfaceRulesData (phase :: ModIfacePhase) where
+ IfaceRulesData 'ModIfaceCore = [IfaceRule]
+ IfaceRulesData 'ModIfaceFinal = [IfaceRule]
+ IfaceRulesData 'ModIfaceRetained = ()
+
+type family IfaceCompleteMatchesData (phase :: ModIfacePhase) where
+ IfaceCompleteMatchesData 'ModIfaceCore = [IfaceCompleteMatch]
+ IfaceCompleteMatchesData 'ModIfaceFinal = [IfaceCompleteMatch]
+ IfaceCompleteMatchesData 'ModIfaceRetained = ()
+
+type family IfaceAbiHashesData (phase :: ModIfacePhase) where
+ IfaceAbiHashesData 'ModIfaceCore = ()
+ IfaceAbiHashesData 'ModIfaceFinal = IfaceAbiHashes
+ IfaceAbiHashesData 'ModIfaceRetained = IfaceAbiHashes
+
+withSelfRecomp :: ModIface -> r -> (IfaceSelfRecomp -> r) -> r
withSelfRecomp iface nk jk =
case mi_self_recomp_info iface of
Nothing -> nk
@@ -258,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,
@@ -279,11 +350,11 @@ data ModIface_ (phase :: ModIfacePhase)
-- importing this module. The main, original part of an interface.
- mi_self_recomp_ :: Maybe IfaceSelfRecomp,
+ mi_self_recomp_ :: IfaceSelfRecompData phase,
-- ^ Information needed for checking self-recompilation.
-- See Note [Self recompilation information in interface files]
- mi_simplified_core_ :: Maybe IfaceSimplifiedCore,
+ mi_simplified_core_ :: IfaceSimplifiedCoreData phase,
-- ^ The part of the interface written when `-fwrite-if-simplified-core` is enabled.
-- These parts are used to restart bytecode generation.
@@ -301,7 +372,7 @@ data ModIface_ (phase :: ModIfacePhase)
-- defined by the user). Used for GHCi and for inspecting
-- the contents of modules via the GHC API only.
- mi_ext_fields_ :: ExtensibleFields
+ mi_ext_fields_ :: ExtensibleFieldsData phase
-- ^ Additional optional fields, where the Map key represents
-- the field name, resulting in a (size, serialized data) pair.
-- Because the data is intended to be serialized through the
@@ -334,12 +405,12 @@ data IfacePublic_ phase = IfacePublic {
-- ^ Warnings
-- NOT STRICT! we read this field lazily from the interface file
- mi_anns_ :: [IfaceAnnotation],
+ mi_anns_ :: IfaceAnnotationsData phase,
-- ^ Annotations
-- NOT STRICT! we read this field lazily from the interface file
- mi_decls_ :: [IfaceDeclExts phase],
+ mi_decls_ :: IfaceDeclsData phase,
-- ^ Type, class and variable declarations
-- The hash of an Id changes if its fixity or deprecations change
-- (as well as its type of course)
@@ -347,14 +418,14 @@ data IfacePublic_ phase = IfacePublic {
-- the hash of the parent class/tycon changes
- mi_defaults_ :: [IfaceDefault],
+ mi_defaults_ :: IfaceDefaultsData phase,
-- ^ default declarations exported by the module
-- Instance declarations and rules
- mi_insts_ :: [IfaceClsInst], -- ^ Sorted class instance
- mi_fam_insts_ :: [IfaceFamInst], -- ^ Sorted family instances
- mi_rules_ :: [IfaceRule], -- ^ Sorted rules
+ mi_insts_ :: IfaceClsInstsData phase, -- ^ Sorted class instance
+ mi_fam_insts_ :: IfaceFamInstsData phase, -- ^ Sorted family instances
+ mi_rules_ :: IfaceRulesData phase, -- ^ Sorted rules
mi_trust_ :: IfaceTrustInfo,
@@ -367,20 +438,20 @@ data IfacePublic_ phase = IfacePublic {
-- itself) but imports some trustworthy modules from its own
-- package (which does require its own package be trusted).
-- See Note [Trust Own Package] in GHC.Rename.Names
- mi_complete_matches_ :: [IfaceCompleteMatch],
+ mi_complete_matches_ :: IfaceCompleteMatchesData phase,
-- ^ {-# COMPLETE #-} declarations
mi_caches_ :: IfaceCache,
-- ^ Cached lookups of some parts of mi_public
- mi_abi_hashes_ :: (IfaceAbiHashesExts phase)
+ mi_abi_hashes_ :: IfaceAbiHashesData phase
-- ^ Either `()` or `IfaceAbiHashes` for
-- a fully instantiated interface.
-- These fields are hashes of different parts of the public interface.
}
mkIfacePublic :: [IfaceExport]
- -> [IfaceDeclExts 'ModIfaceFinal]
+ -> [(Fingerprint, IfaceDecl)]
-> [(OccName, Fixity)]
-> IfaceWarnings
-> [IfaceAnnotation]
@@ -501,22 +572,22 @@ That's why in GHC.Driver.Main.hscMaybeWriteIface there is the call to
forceModIface.
-}
-mi_flag_hash :: ModIface_ phase -> Maybe (FingerprintWithValue IfaceDynFlags)
+mi_flag_hash :: ModIface -> Maybe (FingerprintWithValue IfaceDynFlags)
mi_flag_hash = fmap mi_sr_flag_hash . mi_self_recomp_
-mi_opt_hash :: ModIface_ phase -> Maybe Fingerprint
+mi_opt_hash :: ModIface -> Maybe Fingerprint
mi_opt_hash = fmap mi_sr_opt_hash . mi_self_recomp_
-mi_hpc_hash :: ModIface_ phase -> Maybe Fingerprint
+mi_hpc_hash :: ModIface -> Maybe Fingerprint
mi_hpc_hash = fmap mi_sr_hpc_hash . mi_self_recomp_
-mi_src_hash :: ModIface_ phase -> Maybe Fingerprint
+mi_src_hash :: ModIface -> Maybe Fingerprint
mi_src_hash = fmap mi_sr_src_hash . mi_self_recomp_
-mi_usages :: ModIface_ phase -> Maybe [Usage]
+mi_usages :: ModIface -> Maybe [Usage]
mi_usages = fmap mi_sr_usages . mi_self_recomp_
-mi_plugin_hash :: ModIface_ phase -> Maybe Fingerprint
+mi_plugin_hash :: ModIface -> Maybe Fingerprint
mi_plugin_hash = fmap mi_sr_plugin_hash . mi_self_recomp_
-- | Accessor for the module hash of the ABI from a ModIface.
@@ -547,7 +618,7 @@ mi_orphan_hash iface = mi_abi_orphan_hash (mi_abi_hashes iface)
-- | Old-style accessor for whether or not the ModIface came from an hs-boot
-- file.
-mi_boot :: ModIface -> IsBootInterface
+mi_boot :: ModIface_ phase -> IsBootInterface
mi_boot iface = if mi_hsc_src iface == HsBootFile
then IsBoot
else NotBoot
@@ -557,7 +628,7 @@ mi_mnwib iface = GWIB (moduleName $ mi_module iface) (mi_boot iface)
-- | Lookups up a (possibly cached) fixity from a 'ModIface'. If one cannot be
-- found, 'defaultFixity' is returned instead.
-mi_fix :: ModIface -> OccName -> Fixity
+mi_fix :: ModIface_ phase -> OccName -> Fixity
mi_fix iface name = mi_fix_fn iface name `orElse` defaultFixity
-- | The semantic module for this interface; e.g., if it's a interface
@@ -573,7 +644,7 @@ mi_semantic_module iface = mi_mod_info_semantic_module (mi_mod_info iface)
-- | The "precise" free holes, e.g., the signatures that this
-- 'ModIface' depends on.
-mi_free_holes :: ModIface -> UniqDSet ModuleName
+mi_free_holes :: ModIface_ phase -> UniqDSet ModuleName
mi_free_holes iface =
case getModuleInstantiation (mi_module iface) of
(_, Just indef)
@@ -654,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
@@ -769,9 +840,9 @@ 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_public_ = emptyPublicModIface [] [] [] [] [] [] [] (),
mi_simplified_core_ = Nothing,
mi_top_env_ = IfaceTopEnv emptyDetOrdAvails [] ,
mi_docs_ = Nothing,
@@ -787,24 +858,39 @@ emptyIfaceModInfo mod = IfaceModInfo
, mi_mod_info_hsc_src = HsSrcFile
}
-
-emptyPublicModIface :: IfaceAbiHashesExts phase -> IfacePublic_ phase
-emptyPublicModIface abi_hashes = IfacePublic
- { mi_exports_ = []
- , mi_decls_ = []
- , mi_fixities_ = []
- , mi_warns_ = IfWarnSome [] []
- , mi_anns_ = []
- , mi_defaults_ = []
- , mi_insts_ = []
- , mi_fam_insts_ = []
- , mi_rules_ = []
- , mi_abi_hashes_ = abi_hashes
- , mi_trust_ = noIfaceTrustInfo
- , mi_trust_pkg_ = False
- , mi_caches_ = emptyModIfaceCache
- , mi_complete_matches_ = []
- }
+emptyPublicModIface :: IfaceAnnotationsData phase
+ -> IfaceDeclsData phase
+ -> IfaceDefaultsData phase
+ -> IfaceClsInstsData phase
+ -> IfaceFamInstsData phase
+ -> IfaceRulesData phase
+ -> IfaceCompleteMatchesData phase
+ -> IfaceAbiHashesData phase
+ -> IfacePublic_ phase
+emptyPublicModIface anns
+ decls
+ defaults
+ insts
+ fam_insts
+ rules
+ complete_matches
+ abi_hashes
+ = IfacePublic
+ { mi_exports_ = []
+ , mi_fixities_ = []
+ , mi_warns_ = IfWarnSome [] []
+ , mi_anns_ = anns
+ , mi_decls_ = decls
+ , mi_defaults_ = defaults
+ , mi_insts_ = insts
+ , mi_fam_insts_ = fam_insts
+ , mi_rules_ = rules
+ , mi_trust_ = noIfaceTrustInfo
+ , mi_trust_pkg_ = False
+ , mi_complete_matches_ = complete_matches
+ , mi_caches_ = emptyModIfaceCache
+ , mi_abi_hashes_ = abi_hashes
+ }
emptyModIfaceCache :: IfaceCache
emptyModIfaceCache = IfaceCache {
@@ -827,8 +913,8 @@ emptyIfaceBackend = IfaceAbiHashes
emptyFullModIface :: Module -> ModIface
emptyFullModIface mod =
(emptyPartialModIface mod)
- { mi_public_ = emptyPublicModIface emptyIfaceBackend
- , mi_hi_bytes_ = FullIfaceBinHandle Strict.Nothing
+ { mi_public_ = emptyPublicModIface [] [] [] [] [] [] [] emptyIfaceBackend
+ , mi_hi_bytes_ = Strict.Nothing
}
@@ -848,11 +934,20 @@ emptyIfaceHashCache _occ = Nothing
-- ModIface is completely forced since it will live in memory for a long time.
-- If forcing it uses a lot of memory, then store less things in ModIface.
-instance ( NFData (IfaceAbiHashesExts (phase :: ModIfacePhase))
- , NFData (IfaceDeclExts (phase :: ModIfacePhase))
+instance ( NFData (IfaceSelfRecompData phase)
+ , NFData (IfaceSimplifiedCoreData phase)
+ , NFData (ExtensibleFieldsData phase)
+ , NFData (IfaceAnnotationsData phase)
+ , NFData (IfaceDeclsData phase)
+ , NFData (IfaceDefaultsData phase)
+ , NFData (IfaceClsInstsData phase)
+ , NFData (IfaceFamInstsData phase)
+ , NFData (IfaceRulesData phase)
+ , NFData (IfaceCompleteMatchesData 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
@@ -882,7 +977,15 @@ instance NFData IfaceAbiHashes where
`seq` rnf a5
`seq` rnf a6
-instance (NFData (IfaceAbiHashesExts phase), NFData (IfaceDeclExts phase)) => NFData (IfacePublic_ phase) where
+instance ( NFData (IfaceAnnotationsData phase)
+ , NFData (IfaceDeclsData phase)
+ , NFData (IfaceDefaultsData phase)
+ , NFData (IfaceClsInstsData phase)
+ , NFData (IfaceFamInstsData phase)
+ , NFData (IfaceRulesData phase)
+ , NFData (IfaceCompleteMatchesData phase)
+ , NFData (IfaceAbiHashesData phase)
+ ) => NFData (IfacePublic_ phase) where
rnf (IfacePublic a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14)
= rnf a1
`seq` rnf a2
@@ -955,10 +1058,9 @@ 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
}
- where
-- | Given a 'PartialIfacePublic', turn it into an 'IfacePublic' by completing
-- missing fields.
@@ -973,13 +1075,39 @@ completePublicModIface decls abi_hashes cache partial = partial
, mi_caches_ = cache
}
+shrinkModIface :: ModIface -> RetainedModIface
+shrinkModIface PrivateModIface {..}
+ = PrivateModIface
+ {
+ mi_hi_bytes_ = Strict.Nothing,
+ mi_public_ = shrinkIfacePublic mi_public_,
+ mi_self_recomp_ = (),
+ mi_simplified_core_ = (),
+ mi_ext_fields_ = (),
+ ..
+ }
+
+shrinkIfacePublic :: IfacePublic -> RetainedIfacePublic
+shrinkIfacePublic IfacePublic {..}
+ = IfacePublic
+ {
+ mi_anns_ = (),
+ mi_decls_ = (),
+ mi_defaults_ = (),
+ mi_insts_ = (),
+ mi_fam_insts_ = (),
+ mi_rules_ = (),
+ mi_complete_matches_ = (),
+ ..
+ }
+
set_mi_mod_info :: IfaceModInfo -> ModIface_ phase -> ModIface_ phase
set_mi_mod_info val iface = clear_mi_hi_bytes $ iface { mi_mod_info_ = val }
-set_mi_self_recomp :: Maybe IfaceSelfRecomp-> ModIface_ phase -> ModIface_ phase
+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
@@ -988,7 +1116,7 @@ set_mi_deps val iface = clear_mi_hi_bytes $ iface { mi_deps_ = val }
set_mi_public :: (IfacePublic_ phase -> IfacePublic_ phase) -> ModIface_ phase -> ModIface_ phase
set_mi_public f iface = clear_mi_hi_bytes $ iface { mi_public_ = f (mi_public_ iface) }
-set_mi_simplified_core :: Maybe IfaceSimplifiedCore -> ModIface_ phase -> ModIface_ phase
+set_mi_simplified_core :: IfaceSimplifiedCoreData phase -> ModIface_ phase -> ModIface_ phase
set_mi_simplified_core val iface = clear_mi_hi_bytes $ iface { mi_simplified_core_ = val }
set_mi_top_env :: IfaceTopEnv -> ModIface_ phase -> ModIface_ phase
@@ -997,7 +1125,7 @@ set_mi_top_env val iface = clear_mi_hi_bytes $ iface { mi_top_env_ = val }
set_mi_docs :: Maybe Docs -> ModIface_ phase -> ModIface_ phase
set_mi_docs val iface = clear_mi_hi_bytes $ iface { mi_docs_ = val }
-set_mi_ext_fields :: ExtensibleFields -> ModIface_ phase -> ModIface_ phase
+set_mi_ext_fields :: ExtensibleFieldsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_ext_fields val iface = clear_mi_hi_bytes $ iface { mi_ext_fields_ = val }
{- Settings for mi_public interface fields -}
@@ -1011,22 +1139,22 @@ set_mi_fixities val = set_mi_public (\iface -> iface { mi_fixities_ = val })
set_mi_warns :: IfaceWarnings -> ModIface_ phase -> ModIface_ phase
set_mi_warns val = set_mi_public (\iface -> iface { mi_warns_ = val })
-set_mi_anns :: [IfaceAnnotation] -> ModIface_ phase -> ModIface_ phase
+set_mi_anns :: IfaceAnnotationsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_anns val = set_mi_public (\iface -> iface { mi_anns_ = val })
-set_mi_insts :: [IfaceClsInst] -> ModIface_ phase -> ModIface_ phase
+set_mi_insts :: IfaceClsInstsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_insts val = set_mi_public (\iface -> iface { mi_insts_ = val })
-set_mi_fam_insts :: [IfaceFamInst] -> ModIface_ phase -> ModIface_ phase
+set_mi_fam_insts :: IfaceFamInstsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_fam_insts val = set_mi_public (\iface -> iface { mi_fam_insts_ = val })
-set_mi_rules :: [IfaceRule] -> ModIface_ phase -> ModIface_ phase
+set_mi_rules :: IfaceRulesData phase -> ModIface_ phase -> ModIface_ phase
set_mi_rules val = set_mi_public (\iface -> iface { mi_rules_ = val })
-set_mi_decls :: [IfaceDeclExts phase] -> ModIface_ phase -> ModIface_ phase
+set_mi_decls :: IfaceDeclsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_decls val = set_mi_public (\iface -> iface { mi_decls_ = val })
-set_mi_defaults :: [IfaceDefault] -> ModIface_ phase -> ModIface_ phase
+set_mi_defaults :: IfaceDefaultsData phase -> ModIface_ phase -> ModIface_ phase
set_mi_defaults val = set_mi_public (\iface -> iface { mi_defaults_ = val })
set_mi_trust :: IfaceTrustInfo -> ModIface_ phase -> ModIface_ phase
@@ -1035,10 +1163,10 @@ set_mi_trust val = set_mi_public (\iface -> iface { mi_trust_ = val })
set_mi_trust_pkg :: Bool -> ModIface_ phase -> ModIface_ phase
set_mi_trust_pkg val = set_mi_public (\iface -> iface { mi_trust_pkg_ = val })
-set_mi_complete_matches :: [IfaceCompleteMatch] -> ModIface_ phase -> ModIface_ phase
+set_mi_complete_matches :: IfaceCompleteMatchesData phase -> ModIface_ phase -> ModIface_ phase
set_mi_complete_matches val = set_mi_public (\iface -> iface { mi_complete_matches_ = val })
-set_mi_abi_hashes :: IfaceAbiHashesExts phase -> ModIface_ phase -> ModIface_ phase
+set_mi_abi_hashes :: IfaceAbiHashesData phase -> ModIface_ phase -> ModIface_ phase
set_mi_abi_hashes val = set_mi_public (\iface -> iface { mi_abi_hashes_ = val })
{- Setters for mi_caches interface fields -}
@@ -1082,11 +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
- }
+clear_mi_hi_bytes iface = iface { mi_hi_bytes_ = Strict.Nothing }
-- ----------------------------------------------------------------------------
-- 'ModIface' pattern synonyms to keep breakage low.
@@ -1183,22 +1307,22 @@ pattern ModIface ::
-> [IfaceExport]
-> [(OccName, Fixity)]
-> IfaceWarnings
- -> [IfaceAnnotation]
- -> [IfaceDeclExts phase]
- -> Maybe IfaceSimplifiedCore
- -> [IfaceDefault]
+ -> IfaceAnnotationsData phase
+ -> IfaceDeclsData phase
+ -> IfaceSimplifiedCoreData phase
+ -> IfaceDefaultsData phase
-> IfaceTopEnv
- -> [IfaceClsInst]
- -> [IfaceFamInst]
- -> [IfaceRule]
+ -> IfaceClsInstsData phase
+ -> IfaceFamInstsData phase
+ -> IfaceRulesData phase
-> IfaceTrustInfo
-> Bool
- -> [IfaceCompleteMatch]
+ -> IfaceCompleteMatchesData phase
-> Maybe Docs
- -> IfaceAbiHashesExts phase
- -> ExtensibleFields
- -> IfaceBinHandle phase
- -> Maybe IfaceSelfRecomp
+ -> IfaceAbiHashesData phase
+ -> ExtensibleFieldsData phase
+ -> Strict.Maybe (IfaceBinHandle phase)
+ -> IfaceSelfRecompData phase
-> (OccName -> Maybe Fixity)
-> (OccName -> Maybe (OccName, Fingerprint))
-> (OccName -> Maybe (WarningTxt GhcRn))
=====================================
ghc/Main.hs
=====================================
@@ -515,7 +515,7 @@ abiHash strs = do
put_ bh hiVersion
-- package hashes change when the compiler version changes (for now)
-- see #5328
- mapM_ (put_ bh . mi_mod_hash) ifaces
+ mapM_ (put_ bh . mi_abi_mod_hash . mi_abi_hashes) ifaces
f <- fingerprintBinMem bh
putStrLn (showPpr dflags f)
=====================================
utils/count-deps/Main.hs
=====================================
@@ -79,5 +79,5 @@ calcDeps modName libdir =
mkModule :: String -> ModuleName -> Module
mkModule ghcUnitId = Module (stringToUnit ghcUnitId)
- modDeps :: ModIface -> [ModuleName]
+ modDeps :: ModIface_ phase -> [ModuleName]
modDeps mi = map (gwib_mod . (\(_, _, mn) -> mn)) $ Set.toList $ dep_direct_mods (mi_deps mi)
=====================================
utils/haddock/haddock-api/src/Haddock/Interface.hs
=====================================
@@ -57,21 +57,25 @@ import GHC.Driver.Monad
import GHC.Driver.Make
import GHC.Driver.Main
import GHC.Core.InstEnv
+import GHC.Core.FamInstEnv
import qualified GHC.Driver.DynFlags as DynFlags
import qualified GHC.Utils.Outputable as Outputable
import GHC.Driver.Session hiding (verbosity)
import GHC.Driver.Phases
import GHC.Driver.Pipeline (compileFile)
import GHC.HsToCore.Docs (getMainDeclBinder)
-import GHC.Iface.Load (loadSysInterface)
-import GHC.IfaceToCore (tcIfaceInst, tcIfaceFamInst)
-import GHC.Tc.Utils.Monad (initIfaceLoad, initIfaceLcl)
+import GHC.Iface.Load (findAndReadIface)
+import GHC.IfaceToCore (tcIfaceDecls, tcIfaceInst, tcIfaceFamInst)
+import GHC.Tc.Utils.Monad (goptM, updateEps_, initIfaceLoad, initIfaceLcl)
import GHC.Tc.Utils.Env (lookupGlobal_maybe)
import GHC.Types.Error (mkUnknownDiagnostic)
import GHC.Types.Name.Occurrence (emptyOccEnv)
+import GHC.Types.Name.Env (extendNameEnvList)
import GHC.Unit.Finder (findImportedModule, ModuleLookupScope(..), FindResult(Found))
import GHC.Unit.Home.ModInfo
import GHC.Unit.Home.PackageTable
+import GHC.Unit.External (eps_PTE, eps_inst_env, eps_fam_inst_env)
+import GHC.Unit.Module (getModuleInstantiation)
import GHC.Unit.Module.Graph (ModuleGraphNode (..), ModuleNodeInfo(..))
import GHC.Unit.Module.ModDetails
import GHC.Unit.Module.ModIface (mi_semantic_module, mi_boot)
@@ -254,19 +258,29 @@ dropErr :: MaybeErr e a -> Maybe a
dropErr (Succeeded a) = Just a
dropErr (Failed _) = Nothing
-loadHiFile :: HscEnv -> Outputable.SDoc -> Module -> IO (ModIface, ([ClsInst], [FamInst]))
-loadHiFile hsc_env doc theModule = initIfaceLoad hsc_env $ do
-
- mod_iface <- loadSysInterface doc theModule
-
- insts <- initIfaceLcl (mi_semantic_module mod_iface) doc (mi_boot mod_iface) $ do
-
- new_eps_insts <- mapM tcIfaceInst (mi_insts mod_iface)
- new_eps_fam_insts <- mapM tcIfaceFamInst (mi_fam_insts mod_iface)
-
- pure (new_eps_insts, new_eps_fam_insts)
-
- pure (mod_iface, insts)
+loadHiFile :: Verbosity -> HscEnv -> Outputable.SDoc -> Module -> IO (ModIface, ([ClsInst], [FamInst]))
+loadHiFile verbosity hsc_env doc requested = initIfaceLoad hsc_env $ do
+ let installed = fst (getModuleInstantiation requested)
+ iface_fetch_result <- liftIO $ findAndReadIface hsc_env doc installed requested NotBoot
+ case iface_fetch_result of
+ Failed _ -> do
+ out verbosity normal $ "Loading interface for " ++
+ moduleNameString (moduleName requested) ++
+ " failed"
+ liftIO exitFailure
+ Succeeded (mod_iface, _) -> do
+ all_insts <- initIfaceLcl (mi_semantic_module mod_iface) doc (mi_boot mod_iface) $ do
+ ignore_iface_pragmas <- goptM Opt_IgnoreInterfacePragmas
+ decls <- tcIfaceDecls ignore_iface_pragmas (mi_decls mod_iface)
+ insts <- mapM tcIfaceInst (mi_insts mod_iface)
+ fam_insts <- mapM tcIfaceFamInst (mi_fam_insts mod_iface)
+ updateEps_ $ \ eps -> eps {
+ eps_PTE = extendNameEnvList (eps_PTE eps) decls,
+ eps_inst_env = extendInstEnvList (eps_inst_env eps) insts,
+ eps_fam_inst_env = extendFamInstEnvList (eps_fam_inst_env eps) fam_insts
+ }
+ pure (insts, fam_insts)
+ pure (mod_iface, all_insts)
processModule :: Verbosity -> ModSummary -> [Flag] -> IfaceMap -> InstIfaceMap -> WarningMap -> Ghc (Maybe Interface)
processModule verbosity modSummary flags ifaceMap instIfaceMap warningMap = do
@@ -279,7 +293,7 @@ processModule verbosity modSummary flags ifaceMap instIfaceMap warningMap = do
unit_state = hsc_units hsc_env
(mod_iface, insts) <- if Flag_NoCompilation `elem` flags
- then liftIO $ loadHiFile hsc_env doc $ ms_mod modSummary
+ then liftIO $ loadHiFile verbosity hsc_env doc $ ms_mod modSummary
else do
hmi <- liftIO $ lookupHpt (hsc_HPT hsc_env) (moduleName $ ms_mod modSummary) >>= \case
Nothing -> error "processModule: All modules should be loaded into the HPT by this point"
@@ -374,7 +388,7 @@ createOneShotIface verbosity flags instIfaceMap moduleNameStr = do
modifySession $ hscSetFlags dflags
hsc_env <- getSession
- (iface, insts) <- liftIO $ loadHiFile hsc_env doc $ mkMainModule_ moduleNm
+ (iface, insts) <- liftIO $ loadHiFile verbosity hsc_env doc $ mkMainModule_ moduleNm
-- Update the DynFlags with the extensions from the source file (as stored in the interface file)
-- This is instead of ms_hspp_opts from ModSummary, which is not available in one-shot mode.
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/c8dfce01dcdde741b9f34d65c572bd…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/c8dfce01dcdde741b9f34d65c572bd…
You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
1
0
Simon Peyton Jones pushed to branch wip/T27994 at Glasgow Haskell Compiler / GHC
Commits:
dd2534cb by Simon Peyton Jones at 2026-10-21T17:35:49+01:00
Wibble tests
- - - - -
1 changed file:
- testsuite/tests/typecheck/should_compile/all.T
Changes:
=====================================
testsuite/tests/typecheck/should_compile/all.T
=====================================
@@ -972,6 +972,6 @@ test('T27557', normal, compile, [''])
test('T27664', normal, compile, [''])
test('T27916', normal, compile, [''])
test('T26543_orig', normal, compile, [''])
-test('T7994', normal, compile, [''])
-test('T7994a', normal, compile, [''])
+test('T27994', normal, compile, [''])
+test('T27994a', normal, compile, [''])
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/dd2534cbd98f38348805a01c87adf62…
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/dd2534cbd98f38348805a01c87adf62…
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