
in GHC.Prim, there is a primitive 'touch#'. It seems that it is used to keep pointers alive, and takes something of any runtime representation, and a State# RealWorld, but there's really no documentation on it, so my idea of what it does is just a guess. Can anyone explain what exactly it does?

On 19 Sep 2018, at 1:07 am, Daniel Cartwright
wrote: in GHC.Prim, there is a primitive 'touch#'. It seems that it is used to keep pointers alive, and takes something of any runtime representation, and a State# RealWorld, but there's really no documentation on it, so my idea of what it does is just a guess. Can anyone explain what exactly it does?
It’s used to place a dependency on a computed value, to prevent the GHC simplifier from moving the binding that computes it around. This is sometimes helpful when writing array fusion systems. I mentioned it briefly in http://benl.ouroborus.net/papers/2011-stencil/stencil-haskell2011.pdf http://benl.ouroborus.net/papers/2011-stencil/stencil-haskell2011.pdf Cheers, Ben.

Daniel Cartwright
in GHC.Prim, there is a primitive 'touch#'. It seems that it is used to keep pointers alive, and takes something of any runtime representation, and a State# RealWorld, but there's really no documentation on it, so my idea of what it does is just a guess. Can anyone explain what exactly it does?
Technically I think touch# is only useful when applied to something with pointer runtime representation. However, you otherwise have nailed it. Note, however, that touch# isn't entirely water-tight since the simplifier can (at least as of GHC 8.2) transform your program in ways that break the liveness guarantee provided by touch#; see #14346 for an example. We have proposed a new primop, with#, to fix this (see #14375). Cheers, - Ben
participants (3)
-
Ben Gamari
-
Ben Lippmeier
-
Daniel Cartwright