[Git][ghc/ghc][master] add options for resizing bytearrays (#27244)
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 13dff5d0 by L0neGamer at 2026-10-22T21:37:31-04:00 add options for resizing bytearrays (#27244) add five mutable bytearray resizing operations for various kinds of pinnedness and aligning. - prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default) - prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``) - prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``) - prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``) - ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``) - - - - - 16 changed files: - + changelog.d/resizeMutableByteArray-pinnedness - compiler/GHC/Builtin/primops.txt.pp - compiler/GHC/StgToCmm/Prim.hs - compiler/GHC/StgToJS/Prim.hs - libraries/base/src/GHC/Base.hs - libraries/base/src/GHC/Exts.hs - rts/PrimOps.cmm - rts/RtsSymbols.c - rts/include/stg/MiscClosures.h - testsuite/tests/interface-stability/ghc-experimental-exports.stdout - testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32 - testsuite/tests/interface-stability/ghc-prim-exports.stdout - testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32 - + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs - + testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout - testsuite/tests/primops/should_run/all.T Changes: ===================================== changelog.d/resizeMutableByteArray-pinnedness ===================================== @@ -0,0 +1,15 @@ +section: ghc-internal +synopsis: Add five more primops for resizing byte arrays, relating to their pinnedness. +description: { + Add more primops for resizing mutable byte arrays, giving five options: + + - prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default) + - prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``) + - prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``) + - prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``) + - ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``) + + They can be found in ``GHC.Prim`` and ``GHC.PrimOps``. +} +issues: #27244 +mrs: !16010 ===================================== compiler/GHC/Builtin/primops.txt.pp ===================================== @@ -2033,14 +2033,22 @@ primop NewByteArrayOp_Char "newByteArray#" GenPrimOp primop NewPinnedByteArrayOp_Char "newPinnedByteArray#" GenPrimOp Int# -> State# s -> (# State# s, MutableByteArray# s #) - {Like 'newByteArray#' but GC guarantees not to move it.} + {Like 'newByteArray#' but GC guarantees not to move it. + + The alignment of the resulting byte array is 16 bytes as per 'newAlignedPinnedByteArray#'. + } with out_of_line = True effect = ReadWriteEffect primop NewAlignedPinnedByteArrayOp_Char "newAlignedPinnedByteArray#" GenPrimOp Int# -> Int# -> State# s -> (# State# s, MutableByteArray# s #) {Like 'newPinnedByteArray#' but allow specifying an arbitrary - alignment, which must be a power of two.} + alignment, which must be a power of two. Alignment will be the maximum of the + system word size or given alignment. + + The first parameter is the size of the new array and the second parameter is + the alignment. + } with out_of_line = True effect = ReadWriteEffect can_fail_warning = YesWarnCanFail @@ -2117,6 +2125,73 @@ primop ResizeMutableByteArrayOp_Char "resizeMutableByteArray#" GenPrimOp with out_of_line = True effect = ReadWriteEffect +primop ResizeMutableByteArrayKeepPinnednessOp_Char "resizeMutableByteArrayKeepPinnedness#" GenPrimOp + MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) + {Like 'resizeMutableByteArray#', but ensures the pinnedness of the output array + is the same as the pinnedness of the input array. + + If the input array is pinned, so is the output array, even if a new array + was allocated. + If the input array is unpinned, so is the output array, even if a new array + was allocated. + + @since X.X} + with out_of_line = True + effect = ReadWriteEffect + +primop ResizeMutableByteArrayPreferUnpinnedOp_Char "resizeMutableByteArrayPreferUnpinned#" GenPrimOp + MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) + {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same + pinnedness as the original if it succeeds), but allocating an + unpinned 'MutableByteArray#' if that is not possible. + + This is the default behaviour of 'resizeMutableByteArray#'. + + @since X.X} + with out_of_line = True + effect = ReadWriteEffect + +primop ResizeMutableByteArrayPreferPinnedOp_Char "resizeMutableByteArrayPreferPinned#" GenPrimOp + MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) + {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same + pinnedness as the original if it succeeds), but allocating a + pinned 'MutableByteArray#' if that is not possible. + + @since X.X} + with out_of_line = True + effect = ReadWriteEffect + +primop ResizeMutableByteArrayPreferAlignedPinnedOp_Char "resizeMutableByteArrayPreferAlignedPinned#" GenPrimOp + MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #) + {Like 'resizeMutableByteArrayPreferPinned#', trying to resize in place (keeping the same + pinnedness as the original if it succeeds), but allocating an aligned, + pinned 'MutableByteArray#' if that is not possible. + + The first 'Int#' parameter is the size of the new array, and the second 'Int#' + parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#' + for restrictions on alignment. + + @since X.X} + with out_of_line = True + effect = ReadWriteEffect + can_fail_warning = YesWarnCanFail + -- can fail warning for the "power of two" requirement + +primop ResizeMutableByteArrayMakeAlignedPinnedOp_Char "resizeMutableByteArrayMakeAlignedPinned#" GenPrimOp + MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #) + {Like 'resizeMutableByteArrayPreferAlignedPinned#', but if the input array is + not aligned and pinned to the given alignment, a new array is always allocated. + + The first 'Int#' parameter is the size of the new array, and the second 'Int#' + parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#' + for restrictions on alignment. + + @since X.X} + with out_of_line = True + effect = ReadWriteEffect + can_fail_warning = YesWarnCanFail + -- can fail warning for the "power of two" requirement + primop UnsafeFreezeByteArrayOp "unsafeFreezeByteArray#" GenPrimOp MutableByteArray# s -> State# s -> (# State# s, ByteArray# #) {Make a mutable byte array immutable, without copying.} ===================================== compiler/GHC/StgToCmm/Prim.hs ===================================== @@ -1767,6 +1767,11 @@ emitPrimOp cfg primop = ByteArrayIsWeaklyPinnedOp -> alwaysExternal ShrinkMutableByteArrayOp_Char -> alwaysExternal ResizeMutableByteArrayOp_Char -> alwaysExternal + ResizeMutableByteArrayKeepPinnednessOp_Char -> alwaysExternal + ResizeMutableByteArrayPreferUnpinnedOp_Char -> alwaysExternal + ResizeMutableByteArrayPreferPinnedOp_Char -> alwaysExternal + ResizeMutableByteArrayPreferAlignedPinnedOp_Char -> alwaysExternal + ResizeMutableByteArrayMakeAlignedPinnedOp_Char -> alwaysExternal ShrinkSmallMutableArrayOp_Char -> alwaysExternal NewMutVarOp -> alwaysExternal AtomicModifyMutVar2Op -> alwaysExternal ===================================== compiler/GHC/StgToJS/Prim.hs ===================================== @@ -680,6 +680,11 @@ genPrim prof bound ty op = case op of MutableByteArrayContents_Char -> \[a,o] [b] -> pure $ PrimInline $ mconcat [a |= b, o |= zero_] ShrinkMutableByteArrayOp_Char -> \[] [a,n] -> pure $ PrimInline $ appS hdShrinkMutableByteArrayStr [a,n] ResizeMutableByteArrayOp_Char -> \[r] [a,n] -> pure $ PrimInline $ r |= app hdResizeMutableByteArrayStr [a,n] + ResizeMutableByteArrayKeepPinnednessOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char + ResizeMutableByteArrayPreferUnpinnedOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char + ResizeMutableByteArrayPreferPinnedOp_Char -> genPrim prof bound ty ResizeMutableByteArrayOp_Char + ResizeMutableByteArrayPreferAlignedPinnedOp_Char -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n] + ResizeMutableByteArrayMakeAlignedPinnedOp_Char -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n] UnsafeFreezeByteArrayOp -> \[a] [b] -> pure $ PrimInline $ a |= b UnsafeThawByteArrayOp -> \[a] [b] -> pure $ PrimInline $ a |= b SizeofByteArrayOp -> \[r] [a] -> pure $ PrimInline $ r |= a .^ len ===================================== libraries/base/src/GHC/Base.hs ===================================== @@ -396,6 +396,12 @@ import GHC.Prim hiding , sqrtDoubleX4# , sqrtFloatX16# , sqrtDoubleX8# + -- Don't export mutable byte array primops before stabilisation + , resizeMutableByteArrayKeepPinnedness# + , resizeMutableByteArrayPreferPinned# + , resizeMutableByteArrayPreferAlignedPinned# + , resizeMutableByteArrayMakeAlignedPinned# + , resizeMutableByteArrayPreferUnpinned# #endif #if __GLASGOW_HASKELL__ >= 1001 ===================================== libraries/base/src/GHC/Exts.hs ===================================== @@ -364,6 +364,12 @@ import GHC.Prim hiding , sqrtDoubleX4# , sqrtFloatX16# , sqrtDoubleX8# + -- Don't export mutable byte array primops before stabilisation + , resizeMutableByteArrayKeepPinnedness# + , resizeMutableByteArrayPreferPinned# + , resizeMutableByteArrayPreferAlignedPinned# + , resizeMutableByteArrayMakeAlignedPinned# + , resizeMutableByteArrayPreferUnpinned# #endif #if __GLASGOW_HASKELL__ >= 1001 ===================================== rts/PrimOps.cmm ===================================== @@ -152,22 +152,18 @@ stg_newAlignedPinnedByteArrayzh ( W_ n, W_ alignment ) return (p); } +// See Note [Pinned ByteArray# variants] stg_isByteArrayPinnedzh ( gcptr ba ) // ByteArray# s -> Int# { W_ bd, flags; bd = Bdescr(ba); - // Pinned byte arrays live in blocks with the BF_PINNED flag set. - // We also consider BF_LARGE objects to be immovable. See #13894. - // See the comment in Storage.c:allocatePinned. - // We also consider BF_COMPACT objects to be immovable. See #14900. flags = TO_W_(bdescr_flags(bd)); - // We used to also consider BF_LARGE pinned, but stopped doing so - // because it interacted badly with compact regions. See #22255 return (flags & BF_PINNED != 0); } +// See Note [Pinned ByteArray# variants] stg_isByteArrayWeaklyPinnedzh ( gcptr ba ) // ByteArray# s -> Int# { @@ -243,6 +239,35 @@ stg_isMutableByteArrayWeaklyPinnedzh ( gcptr mba ) * in ClosureMacros.h. */ +/* Note [Pinned ByteArray# variants] + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * We distinguish different kinds of pinning for byte arrays. + * + * Pinned: + * * Always safe for ffi. + * * Never moves. + * * Can't be compacted. + * + * Weakly pinned: + * * Safe for FFI *unless* the byte array is moved into a compact region. + * * Can be compacted, which can invalidate Addr# references to the contents. + * + * Not pinned: + * * Never safe for FFI. + * * Can be compacted. + * + * Distinguishing three cases rather than just pinned/unpinned was needed to avoid + * edge cases with compact regions. See #22255, #14900 and #13894 for the motivation and more details. + * + * We determine the status of a byte array via the block it's allocated + * in. Checking the blocks BF_LARGE, BF_PINNED, BF_COMPACT flags. + * + * Those relate such that: + * * Weakly pinned <-> BF_PINNED | BF_COMPACT | BF_LARGE + * * Pinned <-> BF_PINNED + * +*/ + // shrink size of MutableByteArray in-place stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size ) // MutableByteArray# s -> Int# -> State# s -> State# s @@ -283,17 +308,63 @@ stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size ) return (); } +// allocate unpinned if resizing fails +#define RMB_RESIZED_OR_UNPINNED 0 +// allocate pinned if resizing fails +#define RMB_RESIZED_OR_PINNED 1 +// allocate same pinnedness if resizing fails +#define RMB_RESIZED_OR_KEEP_PINNEDNESS 2 +// allocate aligned and pinned if resizing fails +#define RMB_RESIZED_OR_ALIGNED_PINNED 3 + // resize MutableByteArray // // The returned MutableByteArray is either the original // MutableByteArray resized in-place or, if not possible, a newly -// allocated (unpinned) MutableByteArray (with the original content +// allocated MutableByteArray (with the original content // copied over) stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size ) -// MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +// resizeMutableByteArray# +// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED); +} +stg_resizzeMutableByteArrayPreferUnpinnedzh ( gcptr mba, W_ new_size ) +// resizeMutableByteArrayPreferUnpinned# +// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED); +} +stg_resizzeMutableByteArrayPreferPinnedzh ( gcptr mba, W_ new_size ) +// resizeMutableByteArrayPreferPinned# +// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_PINNED); +} +stg_resizzeMutableByteArrayKeepPinnednesszh ( gcptr mba, W_ new_size ) +// resizeMutableByteArrayKeepPinnedness# +// :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_KEEP_PINNEDNESS); +} +stg_resizzeMutableByteArrayPreferAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment ) +// resizeMutableByteArrayPreferAlignedPinned# +// :: MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED); +} + +// base function to share implementation between the four resize operations: +// resize or unpin (mode == RMB_RESIZED_OR_UNPINNED) +// resize or pin (mode == RMB_RESIZED_OR_PINNED) +// resize with pinnedness (mode == RMB_RESIZED_OR_KEEP_PINNEDNESS) +// resize or pin with alignment (mode == RMB_RESIZED_OR_ALIGNED_PINNED) +stg_resizzeMutableByteArrayChoosezh ( gcptr mba, W_ new_size, W_ alignment, W_ mode ) +// MutableByteArray# s -> Int# -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #) { W_ old_size, old_wds, new_wds, new_free; W_ bd; + P_ new_mba; ASSERT(new_size `ge` 0); @@ -317,9 +388,45 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size ) StgArrBytes_bytes(mba) = new_size; return (mba); } else { - (P_ new_mba) = call stg_newByteArrayzh(new_size); + // Resizing is not possible, create a new array object according to the mode. + switch [RMB_RESIZED_OR_UNPINNED .. RMB_RESIZED_OR_ALIGNED_PINNED] (mode) { + case RMB_RESIZED_OR_UNPINNED: { + (P_ new_mba_unpinned) = call stg_newByteArrayzh(new_size); + new_mba = new_mba_unpinned; + goto after; + } + case RMB_RESIZED_OR_PINNED: { + (P_ new_mba_pinned) = call stg_newPinnedByteArrayzh(new_size); + new_mba = new_mba_pinned; + goto after; + } + case RMB_RESIZED_OR_KEEP_PINNEDNESS: { + (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba); + if (is_pinned != 0) { + (P_ new_mba_pinned_two) = call stg_newPinnedByteArrayzh(new_size); + new_mba = new_mba_pinned_two; + } else { + (P_ new_mba_unpinned_two) = call stg_newByteArrayzh(new_size); + new_mba = new_mba_unpinned_two; + } + goto after; + } + case RMB_RESIZED_OR_ALIGNED_PINNED: { + (P_ new_mba_aligned_pinned) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment); + new_mba = new_mba_aligned_pinned; + goto after; + } + default: { + // shouldn't be possible; this function is only called with one of + // the four above enumerated values + ccall sbarf("stg_resizzeMutableByteArrayChoosezh: bad mode, something went wrong.") never returns; + } + } + after: // copy over old content + // at this point we know that we are making the array bigger, so we copy + // over ALL of the old array prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba), StgArrBytes_bytes(mba), SIZEOF_W); @@ -327,6 +434,34 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size ) } } +// resizeMutableByteArrayMakeAlignedPinned# +stg_resizzeMutableByteArrayMakeAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment ) +// MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #) +{ + (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba); + + // if the array is already pinned and aligned, we can call in and try and + // resize in place + if (is_pinned != 0 && ((BYTE_ARR_CTS(mba) % alignment) == 0)) { + jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED); + } + + // else we just create an aligned array of the correct size + (P_ new_mba) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment); + + // copy over old content + // take the minimum of the old size and the new size + W_ copy_size; + if (new_size < StgArrBytes_bytes(mba)) { + copy_size = new_size; + } else { + copy_size = StgArrBytes_bytes(mba); + } + prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba), copy_size, SIZEOF_W); + + return (new_mba); +} + // shrink size of SmallMutableArray in-place stg_shrinkSmallMutableArrayzh ( gcptr mba, W_ new_size ) // SmallMutableArray# s -> Int# -> State# s -> State# s ===================================== rts/RtsSymbols.c ===================================== @@ -693,6 +693,12 @@ extern char **environ; SymI_HasDataProto(stg_isMutableByteArrayWeaklyPinnedzh) \ SymI_HasDataProto(stg_shrinkMutableByteArrayzh) \ SymI_HasDataProto(stg_resizzeMutableByteArrayzh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayKeepPinnednesszh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayPreferUnpinnedzh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayPreferPinnedzh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayPreferAlignedPinnedzh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayChoosezh) \ + SymI_HasDataProto(stg_resizzeMutableByteArrayMakeAlignedPinnedzh) \ SymI_HasDataProto(stg_shrinkSmallMutableArrayzh) \ SymI_HasProto(newSpark) \ SymI_HasProto(updateRemembSetPushThunk) \ ===================================== rts/include/stg/MiscClosures.h ===================================== @@ -428,6 +428,12 @@ RTS_FUN_DECL(stg_isByteArrayWeaklyPinnedzh); RTS_FUN_DECL(stg_isMutableByteArrayWeaklyPinnedzh); RTS_FUN_DECL(stg_shrinkMutableByteArrayzh); RTS_FUN_DECL(stg_resizzeMutableByteArrayzh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayKeepPinnednesszh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferUnpinnedzh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferPinnedzh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferAlignedPinnedzh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayChoosezh); +RTS_FUN_DECL(stg_resizzeMutableByteArrayMakeAlignedPinnedzh); RTS_FUN_DECL(stg_shrinkSmallMutableArrayzh); RTS_FUN_DECL(stg_casIntArrayzh); RTS_FUN_DECL(stg_casInt8Arrayzh); ===================================== testsuite/tests/interface-stability/ghc-experimental-exports.stdout ===================================== @@ -7326,6 +7326,11 @@ module GHC.PrimOps where remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32# remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64# resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) resizeSmallMutableArray# :: forall s a. SmallMutableArray# s a -> Int# -> a -> State# s -> (# State# s, SmallMutableArray# s a #) retry# :: forall {l :: Levity} (a :: TYPE (BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #) rightSection :: forall {q :: RuntimeRep} {r :: RuntimeRep} {s :: RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c ===================================== testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32 ===================================== @@ -7329,6 +7329,11 @@ module GHC.PrimOps where remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32# remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64# resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) resizeSmallMutableArray# :: forall s a. SmallMutableArray# s a -> Int# -> a -> State# s -> (# State# s, SmallMutableArray# s a #) retry# :: forall {l :: Levity} (a :: TYPE (BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #) rightSection :: forall {q :: RuntimeRep} {r :: RuntimeRep} {s :: RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c ===================================== testsuite/tests/interface-stability/ghc-prim-exports.stdout ===================================== @@ -2578,6 +2578,11 @@ module GHC.Prim where remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32# remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64# resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) retry# :: forall {l :: GHC.Internal.Types.Levity} (a :: TYPE (GHC.Internal.Types.BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #) rightSection :: forall {q :: GHC.Internal.Types.RuntimeRep} {r :: GHC.Internal.Types.RuntimeRep} {s :: GHC.Internal.Types.RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c seq :: forall {r :: GHC.Internal.Types.RuntimeRep} a (b :: TYPE r). a -> b -> b @@ -3600,6 +3605,11 @@ module GHC.PrimopWrappers where remWord64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# remWord8# :: GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# resizeMutableByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayKeepPinnedness# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayMakeAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferUnpinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) retry# :: forall a_levpoly. GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld, a_levpoly #) setAddrRange# :: GHC.Internal.Prim.Addr# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld setByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> GHC.Internal.Prim.State# s ===================================== testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32 ===================================== @@ -2578,6 +2578,11 @@ module GHC.Prim where remWord8X32# :: Word8X32# -> Word8X32# -> Word8X32# remWord8X64# :: Word8X64# -> Word8X64# -> Word8X64# resizeMutableByteArray# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayKeepPinnedness# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayMakeAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferAlignedPinned# :: forall d. MutableByteArray# d -> Int# -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferPinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) + resizeMutableByteArrayPreferUnpinned# :: forall d. MutableByteArray# d -> Int# -> State# d -> (# State# d, MutableByteArray# d #) retry# :: forall {l :: GHC.Internal.Types.Levity} (a :: TYPE (GHC.Internal.Types.BoxedRep l)). State# RealWorld -> (# State# RealWorld, a #) rightSection :: forall {q :: GHC.Internal.Types.RuntimeRep} {r :: GHC.Internal.Types.RuntimeRep} {s :: GHC.Internal.Types.RuntimeRep} (a :: TYPE q) (b :: TYPE r) (c :: TYPE s). (a -> b -> c) -> b -> a -> c seq :: forall {r :: GHC.Internal.Types.RuntimeRep} a (b :: TYPE r). a -> b -> b @@ -3603,6 +3608,11 @@ module GHC.PrimopWrappers where remWord64# :: GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# -> GHC.Internal.Prim.Word64# remWord8# :: GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# -> GHC.Internal.Prim.Word8# resizeMutableByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayKeepPinnedness# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayMakeAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferAlignedPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferPinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) + resizeMutableByteArrayPreferUnpinned# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> (# GHC.Internal.Prim.State# s, GHC.Internal.Prim.MutableByteArray# s #) retry# :: forall a_levpoly. GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> (# GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld, a_levpoly #) setAddrRange# :: GHC.Internal.Prim.Addr# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld -> GHC.Internal.Prim.State# GHC.Internal.Prim.RealWorld setByteArray# :: forall s. GHC.Internal.Prim.MutableByteArray# s -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.Int# -> GHC.Internal.Prim.State# s -> GHC.Internal.Prim.State# s ===================================== testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs ===================================== @@ -0,0 +1,85 @@ +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE UnboxedTuples #-} + +import GHC.Exts +import GHC.Prim +import GHC.IO + +import Data.List (intercalate) + +import System.Mem (performMinorGC) + +type Resizer s = + MutableByteArray# s -> Int# -> State# s -> (# State# s, MutableByteArray# s #) + +{-# NOINLINE testResize #-} +testResize :: + Int# + -> Resizer RealWorld + -> IO () +testResize initSize# resizer# = + testCaseUnpinned initSize# resizer# + >> testCasePinned initSize# resizer# + >> testCaseAlignedPinned initSize# resizer# + where + testCaseUnpinned initSize# = + testCase "unpinned" (newByteArray# initSize#) + testCasePinned initSize# = + testCase "pinned" (newPinnedByteArray# initSize#) + testCaseAlignedPinned initSize# = + testCase "aligned (8) & pinned" (newAlignedPinnedByteArray# initSize# 8#) + +{-# NOINLINE testCase #-} +testCase :: + String + -> (State# RealWorld -> (# State# RealWorld, MutableByteArray# RealWorld #)) + -> Resizer RealWorld + -> IO () +testCase pinnedMessage alloc# resizer# = do + performMinorGC + testWithAlloc >>= putStrLn + where + -- by allocating these all in one go hopefully we can force the resize + {-# NOINLINE testWithAlloc #-} + testWithAlloc :: IO String + testWithAlloc = IO $ \s0 -> case alloc# s0 of + (# s1, mba1 #) -> case alloc# s1 of + (# s2, mba2 #) -> case resizer# mba1 10# s2 of + (# s3, mba1' #) -> case resizer# mba2 10# s3 of + (# s4, mba2' #) -> + (# s4 + , unlines + [ "starts " <> pinnedMessage <> ":" + , "first " <> checkArrayPair mba1 mba1' + , "second " <> checkArrayPair mba2 mba2' + ] + #) + checkArrayPair mba mba' = + "array ends " <> isPinned mba' <> ", is " <> isSame mba mba' + isPinned mba = if I# (isMutableByteArrayPinned# mba) == 1 + then "pinned" + else "unpinned" + isSame mba mba' = if I# (sameMutableByteArray# mba mba') == 1 + then "the same array" + else "a different array" + +main :: IO () +main = do + putStrLn "testing resizeMutableByteArray#" + -- prefer to go to unpinned + testResize 5# resizeMutableByteArray# + putStrLn "===========\ntesting resizeMutableByteArrayKeepPinnedness#" + -- keep pinnedness, both arrays stay the same + testResize 5# resizeMutableByteArrayKeepPinnedness# + putStrLn "===========\ntesting resizeMutableByteArrayPreferUnpinned#" + -- prefer unpinned arrays when creating new ones, this is the same as resizeMutableByteArray# + testResize 5# resizeMutableByteArrayPreferUnpinned# + putStrLn "===========\ntesting resizeMutableByteArrayPreferPinned#" + -- prefer pinned arrays when creating new ones + testResize 5# resizeMutableByteArrayPreferPinned# + putStrLn "===========\ntesting resizeMutableByteArrayPreferAlignedPinned#" + -- prefer pinned arrays when creating new ones. + testResize 5# (\mba size -> resizeMutableByteArrayPreferAlignedPinned# mba size 32#) + putStrLn "===========\ntesting resizeMutableByteArrayMakeAlignedPinned#" + -- guarantee pinned arrays. + testResize 5# (\mba size -> resizeMutableByteArrayMakeAlignedPinned# mba size 32#) ===================================== testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout ===================================== @@ -0,0 +1,82 @@ +testing resizeMutableByteArray# +starts unpinned: +first array ends unpinned, is a different array +second array ends unpinned, is a different array + +starts pinned: +first array ends unpinned, is a different array +second array ends pinned, is the same array + +starts aligned (8) & pinned: +first array ends unpinned, is a different array +second array ends pinned, is the same array + +=========== +testing resizeMutableByteArrayKeepPinnedness# +starts unpinned: +first array ends unpinned, is a different array +second array ends unpinned, is a different array + +starts pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +starts aligned (8) & pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +=========== +testing resizeMutableByteArrayPreferUnpinned# +starts unpinned: +first array ends unpinned, is a different array +second array ends unpinned, is a different array + +starts pinned: +first array ends unpinned, is a different array +second array ends pinned, is the same array + +starts aligned (8) & pinned: +first array ends unpinned, is a different array +second array ends pinned, is the same array + +=========== +testing resizeMutableByteArrayPreferPinned# +starts unpinned: +first array ends pinned, is a different array +second array ends unpinned, is the same array + +starts pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +starts aligned (8) & pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +=========== +testing resizeMutableByteArrayPreferAlignedPinned# +starts unpinned: +first array ends pinned, is a different array +second array ends unpinned, is the same array + +starts pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +starts aligned (8) & pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +=========== +testing resizeMutableByteArrayMakeAlignedPinned# +starts unpinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +starts pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array + +starts aligned (8) & pinned: +first array ends pinned, is a different array +second array ends pinned, is a different array ===================================== testsuite/tests/primops/should_run/all.T ===================================== @@ -34,6 +34,9 @@ test('CmpWord32', normal, compile_and_run, ['']) test('ShrinkSmallMutableArrayA', normal, compile_and_run, ['']) test('ShrinkSmallMutableArrayB', normal, compile_and_run, ['']) +test('ResizeByteArrayPinnedness', + js_skip, # js does not implement performMinorGc + compile_and_run, ['']) test('T14664', normal, compile_and_run, ['']) test('CStringLength', normal, compile_and_run, ['-O2']) test('NonNativeSwitch', normal, compile_and_run, ['-O2']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/13dff5d0629a1da60b4c8b0fc36ae2dc... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/13dff5d0629a1da60b4c8b0fc36ae2dc... 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
participants (1)
-
Marge Bot (@marge-bot)