Skip to content

exprIsWorkFree is dubious

I'm trying to get a sense of when exprIsWorkFree should return False. From Note [exprIsWorkFree]:

Note [exprIsWorkFree]
~~~~~~~~~~~~~~~~~~~~~
exprIsWorkFree is used when deciding whether to inline something; we
don't inline it if doing so might duplicate work, by peeling off a
complete copy of the expression.  Here we do not want even to
duplicate a primop (#5623):
   eg   let x = a #+ b in x +# x
   we do not want to inline/duplicate x
...

But if I look at the Core output of the following program:

{-# LANGUAGE MagicHash #-}

module Lib where

import GHC.Exts

data T = MkT !Double

f :: Double -> Double
f n = n / 13

g :: Double# -> Double#
g n = n /## 13.0##

I indeed see that both f and g are marked work-free despite there occuring a hefty division primop inside of them (same for integer division, I tried).

Moreover, isWorkFreeApp does not look at unlifted arguments (which are ok-for-spec but not always work-free!), so even if we said that g was not work-free, we would still say that e.g., f is work-free, because there the work occurs in the argument to I# which is automatically work-free.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information