Skip to content

Fix sharing recovery under parallel GC using stored node identities - #590

Open
JktuJQ wants to merge 4 commits into
AccelerateHS:masterfrom
JktuJQ:fix/sharing-unique-ids
Open

JktuJQ wants to merge 4 commits into
AccelerateHS:masterfrom
JktuJQ:fix/sharing-unique-ids

Conversation

@JktuJQ

@JktuJQ JktuJQ commented Nov 9, 2026 •

Copy link
Copy Markdown

Description

Store a Data.Unique.Unique in each SmartAcc and SmartExp node and use it throughout sharing recovery. Copies of a node made by the garbage collector retain the same identity, including the Atag and Tag leaves 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:

  • Full interpreter suite with -O2: all 630 tests passed, including the existing expected-failure case.
  • Doctest: all 235 examples passed.
  • Sharing suite with -O2 and +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.
  • Tests cover distinct identities, identity preservation across GC, lazy pattern matching and payloads, and the recovered AST for shared and distinct nodes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

This is a breaking change to the exposed Data.Array.Accelerate.Smart API: SmartAcc and SmartExp change from newtype wrappers to data types.

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

@edsko edsko left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 #-}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have no effect in this module.

--
withUnique :: (Unique -> a) -> a
withUnique f = unsafePerformIO $ f <$> newUnique
{-# NOINLINE withUnique #-}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 <$> newUnique

I 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.)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but:

Suggested change
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.)

@JktuJQ
JktuJQ force-pushed the fix/sharing-unique-ids branch from 2a0e0f2 to f409dc9 Compare November 13, 2026 13:08
@JktuJQ
JktuJQ force-pushed the fix/sharing-unique-ids branch from f409dc9 to 1088723 Compare November 13, 2026 14:55
@JktuJQ

JktuJQ commented Nov 13, 2026

Copy link
Copy Markdown
Author

Pushed pr fixes

@edsko

edsko commented Nov 13, 2026

Copy link
Copy Markdown

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Duplicate ATag error during sharing recovery with parallel GC

2 participants