[commit: ghc] ghc-8.4: base: Make Foreign.Marshal.Alloc.allocBytes[Aligned] NOINLINE (575caf5)

git at git.haskell.org git at git.haskell.org
Mon Jul 30 15:37:25 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : ghc-8.4
Link       : http://ghc.haskell.org/trac/ghc/changeset/575caf56518f8d54a1cb54398890618bcf1e9bfd/ghc

>---------------------------------------------------------------

commit 575caf56518f8d54a1cb54398890618bcf1e9bfd
Author: Ben Gamari <ben at smart-cactus.org>
Date:   Tue Oct 24 12:19:08 2017 -0400

    base: Make Foreign.Marshal.Alloc.allocBytes[Aligned] NOINLINE
    
    As noted in #14346, touch# may be optimized away when the simplifier can see
    that the continuation passed to allocaBytes will not return. Marking CPS-style
    functions with NOINLINE ensures that the simplier can't draw any unsound
    conclusions.
    
    Ultimately the right solution here will be to do away with touch# and instead
    introduce a scoped primitive as is suggested in #14375.
    
    (cherry picked from commit 404bf05ed3193e918875cd2f6c95ae0da5989be2)
    (cherry picked from commit 7409e28c513054b735b6a86dfab4fdab212aaf8f)


>---------------------------------------------------------------

575caf56518f8d54a1cb54398890618bcf1e9bfd
 libraries/base/Foreign/Marshal/Alloc.hs | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/libraries/base/Foreign/Marshal/Alloc.hs b/libraries/base/Foreign/Marshal/Alloc.hs
index 2a3c756..10b6d36 100644
--- a/libraries/base/Foreign/Marshal/Alloc.hs
+++ b/libraries/base/Foreign/Marshal/Alloc.hs
@@ -123,6 +123,19 @@ alloca  = doAlloca undefined
     doAlloca       :: Storable a' => a' -> (Ptr a' -> IO b') -> IO b'
     doAlloca dummy  = allocaBytesAligned (sizeOf dummy) (alignment dummy)
 
+-- Note [NOINLINE for touch#]
+-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
+-- Both allocaBytes and allocaBytesAligned use the touch#, which is notoriously
+-- fragile in the presence of simplification (see #14346). In particular, the
+-- simplifier may drop the continuation containing the touch# if it can prove
+-- that the action passed to allocaBytes will not return. The hack introduced to
+-- fix this for 8.2.2 is to mark allocaBytes as NOINLINE, ensuring that the
+-- simplifier can't see the divergence.
+--
+-- These can be removed once #14375 is fixed, which suggests that we instead do
+-- away with touch# in favor of a primitive that will capture the scoping left
+-- implicit in the case of touch#.
+
 -- |@'allocaBytes' n f@ executes the computation @f@, passing as argument
 -- a pointer to a temporarily allocated block of memory of @n@ bytes.
 -- The block of memory is sufficiently aligned for any of the basic
@@ -141,6 +154,8 @@ allocaBytes (I# size) action = IO $ \ s0 ->
      case touch# barr# s3 of { s4 ->
      (# s4, r #)
   }}}}}
+-- See Note [NOINLINE for touch#]
+{-# NOINLINE allocaBytes #-}
 
 allocaBytesAligned :: Int -> Int -> (Ptr a -> IO b) -> IO b
 allocaBytesAligned (I# size) (I# align) action = IO $ \ s0 ->
@@ -152,6 +167,8 @@ allocaBytesAligned (I# size) (I# align) action = IO $ \ s0 ->
      case touch# barr# s3 of { s4 ->
      (# s4, r #)
   }}}}}
+-- See Note [NOINLINE for touch#]
+{-# NOINLINE allocaBytesAligned #-}
 
 -- |Resize a memory area that was allocated with 'malloc' or 'mallocBytes'
 -- to the size needed to store values of type @b at .  The returned pointer



More information about the ghc-commits mailing list