Repository navigation
Conversation
edsko
left a comment
There was a problem hiding this comment.
I think the code as (mostly) good as-is, but I think the comment for withUnique should change.
Also, perhaps we should document somewhere that updates to nodes should be disallowed; something like n { preSmartAcc = p' } would result in different nodes with the same unique ID. I don't think this happens in the current codebase, but it might be worth a line somewhere.
| @@ -0,0 +1,31 @@ | |||
| {-# OPTIONS_GHC -fno-full-laziness -fno-cse #-} | |||
| -- | ||
| withUnique :: (Unique -> a) -> a | ||
| withUnique f = unsafePerformIO $ f <$> newUnique | ||
| {-# NOINLINE withUnique #-} |
There was a problem hiding this comment.
The NOINLINE (and the OPTIONS_GHC above) have no effect here. They are (potentially) necessary at call-sites. So I would remove them; the comment ("Keeping the builder ... safe") is I think just wrong. Proposed replacement:
-- | Build a value with a fresh unique identity.
--
-- Note that the \"identity\" will only be as unique as the term in which it
-- appears, which will depend on GHC's transformation rules. A few examples
-- are listed below; note that in all cases, preventing these transformations
-- needs to happen /at use sites/, and can be very difficult in general.
--
-- == Common subexpression elimination
--
-- This might transform
--
-- > .... withUnique f .... withUnique f ....
--
-- into
--
-- > let shared = withUnique f in .... shared .... shared ....
--
-- To prevent that, you'd have to disable CSE in all modules, transitively,
-- that use 'withUnique'.
--
-- == Inlining
--
-- It might also go the other way, and apply inlining, turning
--
-- > let shared = withUnique f in .... shared .... shared ....
--
-- into
--
-- > .... withUnique f .... withUnique f ....
--
-- unless @shared@ is marked @NOINLINE@.
--
-- == Full laziness
--
-- This would turn
--
-- > \x y -> .... withUnique (f x) ....
--
-- into
--
-- > \x -> let shared = withUnique (f x) in \y -> .... shared ....
--
-- unless you disable full laziness in all modules, transitively.
--
-- == State hack
--
-- This goes the other way, turning
--
-- > \x -> let shared = withUnique (f x) in \(y :: State# s) -> .... shared ....
--
-- into
--
-- > \x (y :: State# s) -> .... withUnique (f x) ....
--
-- unless you disable the state hack in all modules, transitively. This would
-- matter when this is used in IO or ST code, for example the sharing in
--
-- > let shared = .... withUnique ....
-- > replicateM n $
-- > .... shared ....
--
-- might be lost.
withUnique :: (Unique -> a) -> a
withUnique f = unsafePerformIO $ f <$> newUniqueI realize that these concerns aren't really specific to withUnique per se, but in a way withSharing now becomes the central point where this "sharing detection" happens, so being specific here I think might be helpful (see also AccelerateHS/accelerate-llvm#116 for example).
| -- There is one caveat: We currently rely on the 'Atag' and 'Tag' leaves representing free | ||
| -- variables to be shared if any of them is used more than once. If one is duplicated, the | ||
| -- environment for de Bruijn conversion will have a duplicate entry, and hence, be of the wrong | ||
| -- size, which is fatal. (The 'buildInitialEnv*' functions will already bail out.) |
There was a problem hiding this comment.
I think this caveat should not have been removed. The "NB" part about stable pointers no longer applies, but this caveat still very much does.
In particular, the inlining proviso I suggest adding to the docs of withUnique means a tag can still be duplicated, with exactly the consequence the caveat describes.
| traceLine :: Builder -> Builder -> IO () | ||
| traceLine = Debug.traceM Debug.dump_sharing (builder % ": " % builder) | ||
| traceLine :: Builder -> Builder -> ST s () | ||
| traceLine header msg = return $! tracePure header msg () |
There was a problem hiding this comment.
Minor, but:
| traceLine header msg = return $! tracePure header msg () | |
| traceLine header msg = unsafeIOToST $ Debug.traceM Debug.dump_sharing (builder % ": " % builder) header msg |
As written, the tracePure may be floated out; that is, the same caveats would apply as for traceM from base, see https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Debug-Trace.html#v:traceM . Since we are in ST, however, we can avoid that.
(Same would apply to traceChunk also.)
2a0e0f2 to
f409dc9
Compare
f409dc9 to
1088723
Compare
|
Pushed pr fixes |
|
LGTM! |
Description
Store a
Data.Unique.Uniquein eachSmartAccandSmartExpnode and use it throughout sharing recovery. Copies of a node made by the garbage collector retain the same identity, including theAtagandTagleaves used to build variable environments.Motivation and context
Fixes #587. With parallel GC, physically shared tag nodes can be copied and receive different
StableNames. Sharing recovery then rejects them as duplicate tags. Storing identity in the node avoids relying on physical sharing surviving GC.How has this been tested?
Linux x86_64, GHC 9.8.4:
-O2: all 630 tests passed, including the existing expected-failure case.-O2and+RTS -N8 -A64k -n32k: all 32 tests passed. The regression cases perform 1,000 array-function conversions and 1,000 scalar-function conversions. This GC stress check is probabilistic.Types of changes
This is a breaking change to the exposed
Data.Array.Accelerate.SmartAPI:SmartAccandSmartExpchange fromnewtypewrappers todatatypes.Checklist