Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

16 changed files:

Changes:

  • changelog.d/resizeMutableByteArray-pinnedness
    1
    +section: ghc-internal
    
    2
    +synopsis: Add five more primops for resizing byte arrays, relating to their pinnedness.
    
    3
    +description: {
    
    4
    +  Add more primops for resizing mutable byte arrays, giving five options:
    
    5
    +
    
    6
    +  - prefer unpinned when allocating new array (``resizeMutableByteArrayPreferUnpinned#``; the ``resizeMutableByteArray#`` default)
    
    7
    +  - prefer pinned when allocating new array (``resizeMutableByteArrayPreferPinned#``)
    
    8
    +  - prefer aligned and pinned when allocating new array (``resizeMutableByteArrayPreferAlignedPinned#``)
    
    9
    +  - prefer same as input bytearray when allocating new array (``resizeMutableByteArrayKeepPinnedness#``)
    
    10
    +  - ensure the output array is aligned and pinned after operation (``resizeMutableByteArrayMakeAlignedPinned#``)
    
    11
    +
    
    12
    +  They can be found in ``GHC.Prim`` and ``GHC.PrimOps``.
    
    13
    +}
    
    14
    +issues: #27244
    
    15
    +mrs: !16010

  • compiler/GHC/Builtin/primops.txt.pp
    ... ... @@ -2033,14 +2033,22 @@ primop NewByteArrayOp_Char "newByteArray#" GenPrimOp
    2033 2033
     
    
    2034 2034
     primop  NewPinnedByteArrayOp_Char "newPinnedByteArray#" GenPrimOp
    
    2035 2035
        Int# -> State# s -> (# State# s, MutableByteArray# s #)
    
    2036
    -   {Like 'newByteArray#' but GC guarantees not to move it.}
    
    2036
    +   {Like 'newByteArray#' but GC guarantees not to move it.
    
    2037
    +   
    
    2038
    +    The alignment of the resulting byte array is 16 bytes as per 'newAlignedPinnedByteArray#'.
    
    2039
    +   }
    
    2037 2040
        with out_of_line = True
    
    2038 2041
             effect = ReadWriteEffect
    
    2039 2042
     
    
    2040 2043
     primop  NewAlignedPinnedByteArrayOp_Char "newAlignedPinnedByteArray#" GenPrimOp
    
    2041 2044
        Int# -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
    
    2042 2045
        {Like 'newPinnedByteArray#' but allow specifying an arbitrary
    
    2043
    -    alignment, which must be a power of two.}
    
    2046
    +    alignment, which must be a power of two. Alignment will be the maximum of the
    
    2047
    +    system word size or given alignment.
    
    2048
    +
    
    2049
    +    The first parameter is the size of the new array and the second parameter is
    
    2050
    +    the alignment.
    
    2051
    +    }
    
    2044 2052
        with out_of_line = True
    
    2045 2053
             effect = ReadWriteEffect
    
    2046 2054
             can_fail_warning = YesWarnCanFail
    
    ... ... @@ -2117,6 +2125,73 @@ primop ResizeMutableByteArrayOp_Char "resizeMutableByteArray#" GenPrimOp
    2117 2125
        with out_of_line = True
    
    2118 2126
             effect = ReadWriteEffect
    
    2119 2127
     
    
    2128
    +primop  ResizeMutableByteArrayKeepPinnednessOp_Char "resizeMutableByteArrayKeepPinnedness#" GenPrimOp
    
    2129
    +   MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    2130
    +   {Like 'resizeMutableByteArray#', but ensures the pinnedness of the output array
    
    2131
    +    is the same as the pinnedness of the input array.
    
    2132
    +
    
    2133
    +    If the input array is pinned, so is the output array, even if a new array
    
    2134
    +    was allocated.
    
    2135
    +    If the input array is unpinned, so is the output array, even if a new array
    
    2136
    +    was allocated.
    
    2137
    +
    
    2138
    +    @since X.X}
    
    2139
    +   with out_of_line = True
    
    2140
    +        effect = ReadWriteEffect
    
    2141
    +
    
    2142
    +primop  ResizeMutableByteArrayPreferUnpinnedOp_Char "resizeMutableByteArrayPreferUnpinned#" GenPrimOp
    
    2143
    +   MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    2144
    +   {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same
    
    2145
    +    pinnedness as the original if it succeeds), but allocating an
    
    2146
    +    unpinned 'MutableByteArray#' if that is not possible.
    
    2147
    +
    
    2148
    +    This is the default behaviour of 'resizeMutableByteArray#'.
    
    2149
    +
    
    2150
    +    @since X.X}
    
    2151
    +   with out_of_line = True
    
    2152
    +        effect = ReadWriteEffect
    
    2153
    +
    
    2154
    +primop  ResizeMutableByteArrayPreferPinnedOp_Char "resizeMutableByteArrayPreferPinned#" GenPrimOp
    
    2155
    +   MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    2156
    +   {Like 'resizeMutableByteArray#', trying to resize in place (keeping the same
    
    2157
    +    pinnedness as the original if it succeeds), but allocating a
    
    2158
    +    pinned 'MutableByteArray#' if that is not possible.
    
    2159
    +
    
    2160
    +    @since X.X}
    
    2161
    +   with out_of_line = True
    
    2162
    +        effect = ReadWriteEffect
    
    2163
    +
    
    2164
    +primop  ResizeMutableByteArrayPreferAlignedPinnedOp_Char "resizeMutableByteArrayPreferAlignedPinned#" GenPrimOp
    
    2165
    +   MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    2166
    +   {Like 'resizeMutableByteArrayPreferPinned#', trying to resize in place (keeping the same
    
    2167
    +    pinnedness as the original if it succeeds), but allocating an aligned,
    
    2168
    +    pinned 'MutableByteArray#' if that is not possible.
    
    2169
    +
    
    2170
    +    The first 'Int#' parameter is the size of the new array, and the second 'Int#'
    
    2171
    +    parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#'
    
    2172
    +    for restrictions on alignment.
    
    2173
    +
    
    2174
    +    @since X.X}
    
    2175
    +   with out_of_line = True
    
    2176
    +        effect = ReadWriteEffect
    
    2177
    +        can_fail_warning = YesWarnCanFail
    
    2178
    +        -- can fail warning for the "power of two" requirement
    
    2179
    +
    
    2180
    +primop  ResizeMutableByteArrayMakeAlignedPinnedOp_Char "resizeMutableByteArrayMakeAlignedPinned#" GenPrimOp
    
    2181
    +   MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    2182
    +   {Like 'resizeMutableByteArrayPreferAlignedPinned#', but if the input array is
    
    2183
    +    not aligned and pinned to the given alignment, a new array is always allocated.
    
    2184
    +
    
    2185
    +    The first 'Int#' parameter is the size of the new array, and the second 'Int#'
    
    2186
    +    parameter is the alignment of the re-allocated array. See 'newAlignedPinnedByteArray#'
    
    2187
    +    for restrictions on alignment.
    
    2188
    +
    
    2189
    +    @since X.X}
    
    2190
    +   with out_of_line = True
    
    2191
    +        effect = ReadWriteEffect
    
    2192
    +        can_fail_warning = YesWarnCanFail
    
    2193
    +        -- can fail warning for the "power of two" requirement
    
    2194
    +
    
    2120 2195
     primop  UnsafeFreezeByteArrayOp "unsafeFreezeByteArray#" GenPrimOp
    
    2121 2196
        MutableByteArray# s -> State# s -> (# State# s, ByteArray# #)
    
    2122 2197
        {Make a mutable byte array immutable, without copying.}
    

  • compiler/GHC/StgToCmm/Prim.hs
    ... ... @@ -1767,6 +1767,11 @@ emitPrimOp cfg primop =
    1767 1767
       ByteArrayIsWeaklyPinnedOp -> alwaysExternal
    
    1768 1768
       ShrinkMutableByteArrayOp_Char -> alwaysExternal
    
    1769 1769
       ResizeMutableByteArrayOp_Char -> alwaysExternal
    
    1770
    +  ResizeMutableByteArrayKeepPinnednessOp_Char -> alwaysExternal
    
    1771
    +  ResizeMutableByteArrayPreferUnpinnedOp_Char -> alwaysExternal
    
    1772
    +  ResizeMutableByteArrayPreferPinnedOp_Char -> alwaysExternal
    
    1773
    +  ResizeMutableByteArrayPreferAlignedPinnedOp_Char -> alwaysExternal
    
    1774
    +  ResizeMutableByteArrayMakeAlignedPinnedOp_Char -> alwaysExternal
    
    1770 1775
       ShrinkSmallMutableArrayOp_Char -> alwaysExternal
    
    1771 1776
       NewMutVarOp -> alwaysExternal
    
    1772 1777
       AtomicModifyMutVar2Op -> alwaysExternal
    

  • compiler/GHC/StgToJS/Prim.hs
    ... ... @@ -680,6 +680,11 @@ genPrim prof bound ty op = case op of
    680 680
       MutableByteArrayContents_Char     -> \[a,o] [b]        -> pure $ PrimInline $ mconcat [a |= b, o |= zero_]
    
    681 681
       ShrinkMutableByteArrayOp_Char     -> \[]    [a,n]      -> pure $ PrimInline $ appS hdShrinkMutableByteArrayStr [a,n]
    
    682 682
       ResizeMutableByteArrayOp_Char     -> \[r]   [a,n]      -> pure $ PrimInline $ r |= app hdResizeMutableByteArrayStr [a,n]
    
    683
    +  ResizeMutableByteArrayKeepPinnednessOp_Char            -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
    
    684
    +  ResizeMutableByteArrayPreferUnpinnedOp_Char            -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
    
    685
    +  ResizeMutableByteArrayPreferPinnedOp_Char              -> genPrim prof bound ty ResizeMutableByteArrayOp_Char
    
    686
    +  ResizeMutableByteArrayPreferAlignedPinnedOp_Char       -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n]
    
    687
    +  ResizeMutableByteArrayMakeAlignedPinnedOp_Char         -> \rs (a:n:_align:_) -> genPrim prof bound ty ResizeMutableByteArrayOp_Char rs [a,n]
    
    683 688
       UnsafeFreezeByteArrayOp           -> \[a]   [b]        -> pure $ PrimInline $ a |= b
    
    684 689
       UnsafeThawByteArrayOp             -> \[a]   [b]        -> pure $ PrimInline $ a |= b
    
    685 690
       SizeofByteArrayOp                 -> \[r]   [a]        -> pure $ PrimInline $ r |= a .^ len
    

  • libraries/base/src/GHC/Base.hs
    ... ... @@ -396,6 +396,12 @@ import GHC.Prim hiding
    396 396
       , sqrtDoubleX4#
    
    397 397
       , sqrtFloatX16#
    
    398 398
       , sqrtDoubleX8#
    
    399
    +  -- Don't export mutable byte array primops before stabilisation
    
    400
    +  , resizeMutableByteArrayKeepPinnedness#
    
    401
    +  , resizeMutableByteArrayPreferPinned#
    
    402
    +  , resizeMutableByteArrayPreferAlignedPinned#
    
    403
    +  , resizeMutableByteArrayMakeAlignedPinned#
    
    404
    +  , resizeMutableByteArrayPreferUnpinned#
    
    399 405
     #endif
    
    400 406
     
    
    401 407
     #if __GLASGOW_HASKELL__ >= 1001
    

  • libraries/base/src/GHC/Exts.hs
    ... ... @@ -364,6 +364,12 @@ import GHC.Prim hiding
    364 364
       , sqrtDoubleX4#
    
    365 365
       , sqrtFloatX16#
    
    366 366
       , sqrtDoubleX8#
    
    367
    +  -- Don't export mutable byte array primops before stabilisation
    
    368
    +  , resizeMutableByteArrayKeepPinnedness#
    
    369
    +  , resizeMutableByteArrayPreferPinned#
    
    370
    +  , resizeMutableByteArrayPreferAlignedPinned#
    
    371
    +  , resizeMutableByteArrayMakeAlignedPinned#
    
    372
    +  , resizeMutableByteArrayPreferUnpinned#
    
    367 373
     #endif
    
    368 374
     
    
    369 375
     #if __GLASGOW_HASKELL__ >= 1001
    

  • rts/PrimOps.cmm
    ... ... @@ -152,22 +152,18 @@ stg_newAlignedPinnedByteArrayzh ( W_ n, W_ alignment )
    152 152
         return (p);
    
    153 153
     }
    
    154 154
     
    
    155
    +// See Note [Pinned ByteArray# variants]
    
    155 156
     stg_isByteArrayPinnedzh ( gcptr ba )
    
    156 157
     // ByteArray# s -> Int#
    
    157 158
     {
    
    158 159
         W_ bd, flags;
    
    159 160
         bd = Bdescr(ba);
    
    160
    -    // Pinned byte arrays live in blocks with the BF_PINNED flag set.
    
    161
    -    // We also consider BF_LARGE objects to be immovable. See #13894.
    
    162
    -    // See the comment in Storage.c:allocatePinned.
    
    163
    -    // We also consider BF_COMPACT objects to be immovable. See #14900.
    
    164 161
         flags = TO_W_(bdescr_flags(bd));
    
    165 162
     
    
    166
    -    // We used to also consider BF_LARGE pinned, but stopped doing so
    
    167
    -    // because it interacted badly with compact regions. See #22255
    
    168 163
         return (flags & BF_PINNED != 0);
    
    169 164
     }
    
    170 165
     
    
    166
    +// See Note [Pinned ByteArray# variants]
    
    171 167
     stg_isByteArrayWeaklyPinnedzh ( gcptr ba )
    
    172 168
     // ByteArray# s -> Int#
    
    173 169
     {
    
    ... ... @@ -243,6 +239,35 @@ stg_isMutableByteArrayWeaklyPinnedzh ( gcptr mba )
    243 239
      * in ClosureMacros.h.
    
    244 240
      */
    
    245 241
     
    
    242
    +/* Note [Pinned ByteArray# variants]
    
    243
    + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    244
    + * We distinguish different kinds of pinning for byte arrays.
    
    245
    + *
    
    246
    + * Pinned:
    
    247
    + *   * Always safe for ffi.
    
    248
    + *   * Never moves.
    
    249
    + *   * Can't be compacted.
    
    250
    + *
    
    251
    + * Weakly pinned:
    
    252
    + *   * Safe for FFI *unless* the byte array is moved into a compact region.
    
    253
    + *   * Can be compacted, which can invalidate Addr# references to the contents.
    
    254
    + *
    
    255
    + * Not pinned:
    
    256
    + *   * Never safe for FFI.
    
    257
    + *   * Can be compacted.
    
    258
    + *
    
    259
    + * Distinguishing three cases rather than just pinned/unpinned was needed to avoid
    
    260
    + * edge cases with compact regions. See #22255, #14900 and #13894 for the motivation and more details.
    
    261
    + * 
    
    262
    + * We determine the status of a byte array via the block it's allocated
    
    263
    + * in. Checking the blocks BF_LARGE, BF_PINNED, BF_COMPACT flags.
    
    264
    + * 
    
    265
    + * Those relate such that:
    
    266
    + * * Weakly pinned <-> BF_PINNED | BF_COMPACT | BF_LARGE
    
    267
    + * * Pinned <-> BF_PINNED
    
    268
    + *
    
    269
    +*/
    
    270
    +
    
    246 271
     // shrink size of MutableByteArray in-place
    
    247 272
     stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size )
    
    248 273
     // MutableByteArray# s -> Int# -> State# s -> State# s
    
    ... ... @@ -283,17 +308,63 @@ stg_shrinkMutableByteArrayzh ( gcptr mba, W_ new_size )
    283 308
        return ();
    
    284 309
     }
    
    285 310
     
    
    311
    +// allocate unpinned if resizing fails
    
    312
    +#define RMB_RESIZED_OR_UNPINNED 0
    
    313
    +// allocate pinned if resizing fails
    
    314
    +#define RMB_RESIZED_OR_PINNED 1
    
    315
    +// allocate same pinnedness if resizing fails
    
    316
    +#define RMB_RESIZED_OR_KEEP_PINNEDNESS 2
    
    317
    +// allocate aligned and pinned if resizing fails
    
    318
    +#define RMB_RESIZED_OR_ALIGNED_PINNED 3
    
    319
    +
    
    286 320
     // resize MutableByteArray
    
    287 321
     //
    
    288 322
     // The returned MutableByteArray is either the original
    
    289 323
     // MutableByteArray resized in-place or, if not possible, a newly
    
    290
    -// allocated (unpinned) MutableByteArray (with the original content
    
    324
    +// allocated MutableByteArray (with the original content
    
    291 325
     // copied over)
    
    292 326
     stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
    
    293
    -// MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    327
    +// resizeMutableByteArray#
    
    328
    +//  :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    329
    +{
    
    330
    +   jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED);
    
    331
    +}
    
    332
    +stg_resizzeMutableByteArrayPreferUnpinnedzh ( gcptr mba, W_ new_size )
    
    333
    +// resizeMutableByteArrayPreferUnpinned#
    
    334
    +//  :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    335
    +{
    
    336
    +   jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_UNPINNED);
    
    337
    +}
    
    338
    +stg_resizzeMutableByteArrayPreferPinnedzh ( gcptr mba, W_ new_size )
    
    339
    +// resizeMutableByteArrayPreferPinned#
    
    340
    +//  :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    341
    +{
    
    342
    +   jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_PINNED);
    
    343
    +}
    
    344
    +stg_resizzeMutableByteArrayKeepPinnednesszh ( gcptr mba, W_ new_size )
    
    345
    +// resizeMutableByteArrayKeepPinnedness#
    
    346
    +//  :: MutableByteArray# s -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    347
    +{
    
    348
    +   jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, 0, RMB_RESIZED_OR_KEEP_PINNEDNESS);
    
    349
    +}
    
    350
    +stg_resizzeMutableByteArrayPreferAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment )
    
    351
    +// resizeMutableByteArrayPreferAlignedPinned#
    
    352
    +//  :: MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    353
    +{
    
    354
    +   jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED);
    
    355
    +}
    
    356
    +
    
    357
    +// base function to share implementation between the four resize operations:
    
    358
    +// resize or unpin (mode == RMB_RESIZED_OR_UNPINNED)
    
    359
    +// resize or pin (mode == RMB_RESIZED_OR_PINNED)
    
    360
    +// resize with pinnedness (mode == RMB_RESIZED_OR_KEEP_PINNEDNESS)
    
    361
    +// resize or pin with alignment (mode == RMB_RESIZED_OR_ALIGNED_PINNED)
    
    362
    +stg_resizzeMutableByteArrayChoosezh ( gcptr mba, W_ new_size, W_ alignment, W_ mode )
    
    363
    +// MutableByteArray# s -> Int# -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    294 364
     {
    
    295 365
        W_ old_size, old_wds, new_wds, new_free;
    
    296 366
        W_ bd;
    
    367
    +   P_ new_mba;
    
    297 368
     
    
    298 369
        ASSERT(new_size `ge` 0);
    
    299 370
     
    
    ... ... @@ -317,9 +388,45 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
    317 388
           StgArrBytes_bytes(mba) = new_size;
    
    318 389
           return (mba);
    
    319 390
        } else {
    
    320
    -      (P_ new_mba) = call stg_newByteArrayzh(new_size);
    
    391
    +      // Resizing is not possible, create a new array object according to the mode.
    
    392
    +      switch [RMB_RESIZED_OR_UNPINNED .. RMB_RESIZED_OR_ALIGNED_PINNED] (mode) {
    
    393
    +         case RMB_RESIZED_OR_UNPINNED: {
    
    394
    +            (P_ new_mba_unpinned) = call stg_newByteArrayzh(new_size);
    
    395
    +            new_mba = new_mba_unpinned;
    
    396
    +            goto after;
    
    397
    +         }
    
    398
    +         case RMB_RESIZED_OR_PINNED: {
    
    399
    +            (P_ new_mba_pinned) = call stg_newPinnedByteArrayzh(new_size);
    
    400
    +            new_mba = new_mba_pinned;
    
    401
    +            goto after;
    
    402
    +         }
    
    403
    +         case RMB_RESIZED_OR_KEEP_PINNEDNESS: {
    
    404
    +            (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba);
    
    405
    +            if (is_pinned != 0) {
    
    406
    +               (P_ new_mba_pinned_two) = call stg_newPinnedByteArrayzh(new_size);
    
    407
    +               new_mba = new_mba_pinned_two;
    
    408
    +            } else {
    
    409
    +               (P_ new_mba_unpinned_two) = call stg_newByteArrayzh(new_size);
    
    410
    +               new_mba = new_mba_unpinned_two;
    
    411
    +            }
    
    412
    +            goto after;
    
    413
    +         }
    
    414
    +         case RMB_RESIZED_OR_ALIGNED_PINNED: {
    
    415
    +            (P_ new_mba_aligned_pinned) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment);
    
    416
    +            new_mba = new_mba_aligned_pinned;
    
    417
    +            goto after;
    
    418
    +         }
    
    419
    +         default: {
    
    420
    +            // shouldn't be possible; this function is only called with one of
    
    421
    +            // the four above enumerated values
    
    422
    +            ccall sbarf("stg_resizzeMutableByteArrayChoosezh: bad mode, something went wrong.") never returns;
    
    423
    +         }
    
    424
    +      }
    
    425
    +      after:
    
    321 426
     
    
    322 427
           // copy over old content
    
    428
    +      // at this point we know that we are making the array bigger, so we copy
    
    429
    +      // over ALL of the old array
    
    323 430
           prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba),
    
    324 431
                        StgArrBytes_bytes(mba), SIZEOF_W);
    
    325 432
     
    
    ... ... @@ -327,6 +434,34 @@ stg_resizzeMutableByteArrayzh ( gcptr mba, W_ new_size )
    327 434
        }
    
    328 435
     }
    
    329 436
     
    
    437
    +// resizeMutableByteArrayMakeAlignedPinned#
    
    438
    +stg_resizzeMutableByteArrayMakeAlignedPinnedzh ( gcptr mba, W_ new_size, W_ alignment )
    
    439
    +// MutableByteArray# s -> Int# -> Int# -> State# s -> (# State# s,MutableByteArray# s #)
    
    440
    +{
    
    441
    +   (W_ is_pinned) = call stg_isMutableByteArrayPinnedzh(mba);
    
    442
    +
    
    443
    +   // if the array is already pinned and aligned, we can call in and try and
    
    444
    +   // resize in place
    
    445
    +   if (is_pinned != 0 && ((BYTE_ARR_CTS(mba) % alignment) == 0)) {
    
    446
    +      jump stg_resizzeMutableByteArrayChoosezh(mba, new_size, alignment, RMB_RESIZED_OR_ALIGNED_PINNED);
    
    447
    +   }
    
    448
    +
    
    449
    +   // else we just create an aligned array of the correct size
    
    450
    +   (P_ new_mba) = call stg_newAlignedPinnedByteArrayzh(new_size, alignment);
    
    451
    +
    
    452
    +   // copy over old content
    
    453
    +   // take the minimum of the old size and the new size
    
    454
    +   W_ copy_size;
    
    455
    +   if (new_size < StgArrBytes_bytes(mba)) {
    
    456
    +      copy_size = new_size;
    
    457
    +   } else {
    
    458
    +      copy_size = StgArrBytes_bytes(mba);
    
    459
    +   }
    
    460
    +   prim %memcpy(BYTE_ARR_CTS(new_mba), BYTE_ARR_CTS(mba), copy_size, SIZEOF_W);
    
    461
    +
    
    462
    +   return (new_mba);
    
    463
    +}
    
    464
    +
    
    330 465
     // shrink size of SmallMutableArray in-place
    
    331 466
     stg_shrinkSmallMutableArrayzh ( gcptr mba, W_ new_size )
    
    332 467
     // SmallMutableArray# s -> Int# -> State# s -> State# s
    

  • rts/RtsSymbols.c
    ... ... @@ -693,6 +693,12 @@ extern char **environ;
    693 693
           SymI_HasDataProto(stg_isMutableByteArrayWeaklyPinnedzh)               \
    
    694 694
           SymI_HasDataProto(stg_shrinkMutableByteArrayzh)                       \
    
    695 695
           SymI_HasDataProto(stg_resizzeMutableByteArrayzh)                      \
    
    696
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayKeepPinnednesszh)        \
    
    697
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayPreferUnpinnedzh)        \
    
    698
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayPreferPinnedzh)          \
    
    699
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayPreferAlignedPinnedzh)   \
    
    700
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayChoosezh)                \
    
    701
    +      SymI_HasDataProto(stg_resizzeMutableByteArrayMakeAlignedPinnedzh)     \
    
    696 702
           SymI_HasDataProto(stg_shrinkSmallMutableArrayzh)                      \
    
    697 703
           SymI_HasProto(newSpark)                                           \
    
    698 704
           SymI_HasProto(updateRemembSetPushThunk)                             \
    

  • rts/include/stg/MiscClosures.h
    ... ... @@ -428,6 +428,12 @@ RTS_FUN_DECL(stg_isByteArrayWeaklyPinnedzh);
    428 428
     RTS_FUN_DECL(stg_isMutableByteArrayWeaklyPinnedzh);
    
    429 429
     RTS_FUN_DECL(stg_shrinkMutableByteArrayzh);
    
    430 430
     RTS_FUN_DECL(stg_resizzeMutableByteArrayzh);
    
    431
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayKeepPinnednesszh);
    
    432
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferUnpinnedzh);
    
    433
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferPinnedzh);
    
    434
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayPreferAlignedPinnedzh);
    
    435
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayChoosezh);
    
    436
    +RTS_FUN_DECL(stg_resizzeMutableByteArrayMakeAlignedPinnedzh);
    
    431 437
     RTS_FUN_DECL(stg_shrinkSmallMutableArrayzh);
    
    432 438
     RTS_FUN_DECL(stg_casIntArrayzh);
    
    433 439
     RTS_FUN_DECL(stg_casInt8Arrayzh);
    

  • testsuite/tests/interface-stability/ghc-experimental-exports.stdout
    No preview for this file type
  • testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32
    No preview for this file type
  • testsuite/tests/interface-stability/ghc-prim-exports.stdout
    No preview for this file type
  • testsuite/tests/interface-stability/ghc-prim-exports.stdout-mingw32
    No preview for this file type
  • testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +{-# LANGUAGE UnboxedTuples #-}
    
    3
    +
    
    4
    +import GHC.Exts
    
    5
    +import GHC.Prim
    
    6
    +import GHC.IO
    
    7
    +
    
    8
    +import Data.List (intercalate)
    
    9
    +
    
    10
    +import System.Mem (performMinorGC)
    
    11
    +
    
    12
    +type Resizer s =
    
    13
    +  MutableByteArray# s -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
    
    14
    +
    
    15
    +{-# NOINLINE testResize #-}
    
    16
    +testResize ::
    
    17
    +  Int#
    
    18
    +  -> Resizer RealWorld
    
    19
    +  -> IO ()
    
    20
    +testResize initSize# resizer# =
    
    21
    +  testCaseUnpinned initSize# resizer#
    
    22
    +  >> testCasePinned initSize# resizer#
    
    23
    +  >> testCaseAlignedPinned initSize# resizer#
    
    24
    +  where
    
    25
    +  testCaseUnpinned initSize# =
    
    26
    +    testCase "unpinned" (newByteArray# initSize#)
    
    27
    +  testCasePinned initSize# =
    
    28
    +    testCase "pinned" (newPinnedByteArray# initSize#)
    
    29
    +  testCaseAlignedPinned initSize# =
    
    30
    +    testCase "aligned (8) & pinned" (newAlignedPinnedByteArray# initSize# 8#)
    
    31
    +
    
    32
    +{-# NOINLINE testCase #-}
    
    33
    +testCase ::
    
    34
    +  String
    
    35
    +  -> (State# RealWorld -> (# State# RealWorld, MutableByteArray# RealWorld #))
    
    36
    +  -> Resizer RealWorld
    
    37
    +  -> IO ()
    
    38
    +testCase pinnedMessage alloc# resizer# = do
    
    39
    +  performMinorGC
    
    40
    +  testWithAlloc >>= putStrLn
    
    41
    +  where
    
    42
    +  -- by allocating these all in one go hopefully we can force the resize
    
    43
    +  {-# NOINLINE testWithAlloc #-}
    
    44
    +  testWithAlloc :: IO String
    
    45
    +  testWithAlloc = IO $ \s0 -> case alloc# s0 of
    
    46
    +    (# s1, mba1 #) -> case alloc# s1 of
    
    47
    +      (# s2, mba2 #) -> case resizer# mba1 10# s2 of
    
    48
    +        (# s3, mba1' #) -> case resizer# mba2 10# s3 of
    
    49
    +          (# s4, mba2' #) ->
    
    50
    +            (# s4
    
    51
    +            , unlines
    
    52
    +              [ "starts " <> pinnedMessage <> ":"
    
    53
    +              , "first " <> checkArrayPair mba1 mba1'
    
    54
    +              , "second " <> checkArrayPair mba2 mba2'
    
    55
    +              ]
    
    56
    +            #)
    
    57
    +  checkArrayPair mba mba' =
    
    58
    +    "array ends " <> isPinned mba' <> ", is " <> isSame mba mba'
    
    59
    +  isPinned mba = if I# (isMutableByteArrayPinned# mba) == 1
    
    60
    +    then "pinned"
    
    61
    +    else "unpinned"
    
    62
    +  isSame mba mba' = if I# (sameMutableByteArray# mba mba') == 1
    
    63
    +    then "the same array"
    
    64
    +    else "a different array"
    
    65
    +
    
    66
    +main :: IO ()
    
    67
    +main = do
    
    68
    +  putStrLn "testing resizeMutableByteArray#"
    
    69
    +  -- prefer to go to unpinned
    
    70
    +  testResize 5# resizeMutableByteArray#
    
    71
    +  putStrLn "===========\ntesting resizeMutableByteArrayKeepPinnedness#"
    
    72
    +  -- keep pinnedness, both arrays stay the same
    
    73
    +  testResize 5# resizeMutableByteArrayKeepPinnedness#
    
    74
    +  putStrLn "===========\ntesting resizeMutableByteArrayPreferUnpinned#"
    
    75
    +  -- prefer unpinned arrays when creating new ones, this is the same as resizeMutableByteArray#
    
    76
    +  testResize 5# resizeMutableByteArrayPreferUnpinned#
    
    77
    +  putStrLn "===========\ntesting resizeMutableByteArrayPreferPinned#"
    
    78
    +  -- prefer pinned arrays when creating new ones
    
    79
    +  testResize 5# resizeMutableByteArrayPreferPinned#
    
    80
    +  putStrLn "===========\ntesting resizeMutableByteArrayPreferAlignedPinned#"
    
    81
    +  -- prefer pinned arrays when creating new ones.
    
    82
    +  testResize 5# (\mba size -> resizeMutableByteArrayPreferAlignedPinned# mba size 32#)
    
    83
    +  putStrLn "===========\ntesting resizeMutableByteArrayMakeAlignedPinned#"
    
    84
    +  -- guarantee pinned arrays.
    
    85
    +  testResize 5# (\mba size -> resizeMutableByteArrayMakeAlignedPinned# mba size 32#)

  • testsuite/tests/primops/should_run/ResizeByteArrayPinnedness.stdout
    1
    +testing resizeMutableByteArray#
    
    2
    +starts unpinned:
    
    3
    +first array ends unpinned, is a different array
    
    4
    +second array ends unpinned, is a different array
    
    5
    +
    
    6
    +starts pinned:
    
    7
    +first array ends unpinned, is a different array
    
    8
    +second array ends pinned, is the same array
    
    9
    +
    
    10
    +starts aligned (8) & pinned:
    
    11
    +first array ends unpinned, is a different array
    
    12
    +second array ends pinned, is the same array
    
    13
    +
    
    14
    +===========
    
    15
    +testing resizeMutableByteArrayKeepPinnedness#
    
    16
    +starts unpinned:
    
    17
    +first array ends unpinned, is a different array
    
    18
    +second array ends unpinned, is a different array
    
    19
    +
    
    20
    +starts pinned:
    
    21
    +first array ends pinned, is a different array
    
    22
    +second array ends pinned, is a different array
    
    23
    +
    
    24
    +starts aligned (8) & pinned:
    
    25
    +first array ends pinned, is a different array
    
    26
    +second array ends pinned, is a different array
    
    27
    +
    
    28
    +===========
    
    29
    +testing resizeMutableByteArrayPreferUnpinned#
    
    30
    +starts unpinned:
    
    31
    +first array ends unpinned, is a different array
    
    32
    +second array ends unpinned, is a different array
    
    33
    +
    
    34
    +starts pinned:
    
    35
    +first array ends unpinned, is a different array
    
    36
    +second array ends pinned, is the same array
    
    37
    +
    
    38
    +starts aligned (8) & pinned:
    
    39
    +first array ends unpinned, is a different array
    
    40
    +second array ends pinned, is the same array
    
    41
    +
    
    42
    +===========
    
    43
    +testing resizeMutableByteArrayPreferPinned#
    
    44
    +starts unpinned:
    
    45
    +first array ends pinned, is a different array
    
    46
    +second array ends unpinned, is the same array
    
    47
    +
    
    48
    +starts pinned:
    
    49
    +first array ends pinned, is a different array
    
    50
    +second array ends pinned, is a different array
    
    51
    +
    
    52
    +starts aligned (8) & pinned:
    
    53
    +first array ends pinned, is a different array
    
    54
    +second array ends pinned, is a different array
    
    55
    +
    
    56
    +===========
    
    57
    +testing resizeMutableByteArrayPreferAlignedPinned#
    
    58
    +starts unpinned:
    
    59
    +first array ends pinned, is a different array
    
    60
    +second array ends unpinned, is the same array
    
    61
    +
    
    62
    +starts pinned:
    
    63
    +first array ends pinned, is a different array
    
    64
    +second array ends pinned, is a different array
    
    65
    +
    
    66
    +starts aligned (8) & pinned:
    
    67
    +first array ends pinned, is a different array
    
    68
    +second array ends pinned, is a different array
    
    69
    +
    
    70
    +===========
    
    71
    +testing resizeMutableByteArrayMakeAlignedPinned#
    
    72
    +starts unpinned:
    
    73
    +first array ends pinned, is a different array
    
    74
    +second array ends pinned, is a different array
    
    75
    +
    
    76
    +starts pinned:
    
    77
    +first array ends pinned, is a different array
    
    78
    +second array ends pinned, is a different array
    
    79
    +
    
    80
    +starts aligned (8) & pinned:
    
    81
    +first array ends pinned, is a different array
    
    82
    +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, [''])
    34 34
     
    
    35 35
     test('ShrinkSmallMutableArrayA', normal, compile_and_run, [''])
    
    36 36
     test('ShrinkSmallMutableArrayB', normal, compile_and_run, [''])
    
    37
    +test('ResizeByteArrayPinnedness', 
    
    38
    +  js_skip, # js does not implement performMinorGc
    
    39
    +  compile_and_run, [''])
    
    37 40
     test('T14664', normal, compile_and_run, [''])
    
    38 41
     test('CStringLength', normal, compile_and_run, ['-O2'])
    
    39 42
     test('NonNativeSwitch', normal, compile_and_run, ['-O2'])