Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify intermediate code/value format to be less Haskell-runtime specific #4311

Merged
merged 25 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
360e829
Introduce some new runtime functionality for cases/lits
dolio Sep 1, 2023
bd51afe
stdHandle didn't match its calling convention
dolio Sep 1, 2023
4245897
Add more literals to value serialization format.
dolio Sep 5, 2023
f450e97
Add a test for int/nat hash equivalence
dolio Sep 5, 2023
9c9654d
Alignment for new code constructors
dolio Sep 7, 2023
9da57e9
Store Char in interchange literals
dolio Sep 7, 2023
d91de0e
Add fake builtins for sequence split patterns
dolio Sep 7, 2023
0f52e01
Fix up tests w/r/t new ANF constructors
dolio Sep 7, 2023
ccb25dc
Implement racket portion of new intermediate format
dolio Sep 7, 2023
f9a8b6c
Expected transcript changes: hashes
dolio Sep 7, 2023
dfc3814
Increment code serialization version
dolio Sep 7, 2023
d9e0806
Ormolu changes
dolio Sep 7, 2023
3139432
Increment @unison/internal version
dolio Sep 7, 2023
0ec34c4
Try running base tests from CI
dolio Sep 7, 2023
f9e8c78
Remove base tests from interpreter builtin tests
dolio Sep 8, 2023
1384d8a
Add a note in comments on new serialization format
dolio Sep 8, 2023
d6c0ae2
Add to random-deserial tests
dolio Sep 8, 2023
699b9d6
Add serialization for boxed arrays
dolio Sep 9, 2023
3c042b8
Increment share compiler version
dolio Sep 9, 2023
16c5b7b
Merge remote-tracking branch 'origin/trunk' into topic/interchange-fo…
dolio Sep 11, 2023
95c9eb3
Eliminate dead reqArgs code
dolio Sep 11, 2023
a9065ec
More informative names for serialized test cases
dolio Sep 11, 2023
9d52145
Change transcripts for new serialized file names
dolio Sep 11, 2023
2f47a85
Tweak serial case generator code
dolio Sep 11, 2023
9fb3496
Fix JIT/interpreter builtin serial test names
dolio Sep 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 51 additions & 23 deletions parser-typechecker/src/Unison/Runtime/ANF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Unison.Runtime.ANF
( minimizeCyclesOrCrash,
pattern TVar,
pattern TLit,
pattern TBLit,
pattern TApp,
pattern TApv,
pattern TCom,
Expand Down Expand Up @@ -43,6 +44,7 @@ module Unison.Runtime.ANF
floatGroup,
lamLift,
lamLiftGroup,
litRef,
inlineAlias,
addDefaultCases,
ANormalF (.., AApv, ACom, ACon, AKon, AReq, APrm, AFOp),
Expand Down Expand Up @@ -668,6 +670,7 @@ data ANormalF v e
= ALet (Direction Word16) [Mem] e e
| AName (Either Reference v) [v] e
| ALit Lit
| ABLit Lit -- direct boxed literal
| AMatch v (Branched e)
| AShift Reference e
| AHnd [Reference] v e
Expand Down Expand Up @@ -745,6 +748,7 @@ instance Num CTag where
instance Functor (ANormalF v) where
fmap _ (AVar v) = AVar v
fmap _ (ALit l) = ALit l
fmap _ (ABLit l) = ABLit l
fmap f (ALet d m bn bo) = ALet d m (f bn) (f bo)
fmap f (AName n as bo) = AName n as $ f bo
fmap f (AMatch v br) = AMatch v $ f <$> br
Expand All @@ -756,6 +760,7 @@ instance Functor (ANormalF v) where
instance Bifunctor ANormalF where
bimap f _ (AVar v) = AVar (f v)
bimap _ _ (ALit l) = ALit l
bimap _ _ (ABLit l) = ABLit l
bimap _ g (ALet d m bn bo) = ALet d m (g bn) (g bo)
bimap f g (AName n as bo) = AName (f <$> n) (f <$> as) $ g bo
bimap f g (AMatch v br) = AMatch (f v) $ fmap g br
Expand All @@ -767,6 +772,7 @@ instance Bifunctor ANormalF where
instance Bifoldable ANormalF where
bifoldMap f _ (AVar v) = f v
bifoldMap _ _ (ALit _) = mempty
bifoldMap _ _ (ABLit _) = mempty
bifoldMap _ g (ALet _ _ b e) = g b <> g e
bifoldMap f g (AName n as e) = foldMap f n <> foldMap f as <> g e
bifoldMap f g (AMatch v br) = f v <> foldMap g br
Expand All @@ -779,6 +785,8 @@ instance ABTN.Align ANormalF where
align f _ (AVar u) (AVar v) = Just $ AVar <$> f u v
align _ _ (ALit l) (ALit r)
| l == r = Just $ pure (ALit l)
align _ _ (ABLit l) (ABLit r)
| l == r = Just $ pure (ABLit l)
align _ g (ALet dl ccl bl el) (ALet dr ccr br er)
| dl == dr,
ccl == ccr =
Expand Down Expand Up @@ -886,6 +894,14 @@ alignBranch f (MatchSum bl) (MatchSum br)
| keysSet bl == keysSet br,
all (\w -> fst (bl ! w) == fst (br ! w)) (keys bl) =
Just $ MatchSum <$> interverse (alignCCs f) bl br
alignBranch f (MatchNumeric rl bl dl) (MatchNumeric rr br dr)
| rl == rr,
keysSet bl == keysSet br,
Just ds <- alignMaybe f dl dr =
Just $
MatchNumeric rl
<$> interverse f bl br
<*> ds
alignBranch _ _ _ = Nothing

alignCCs :: (Functor f) => (l -> r -> f s) -> (a, l) -> (a, r) -> f (a, s)
Expand Down Expand Up @@ -946,6 +962,12 @@ pattern TLit ::
ABTN.Term ANormalF v
pattern TLit l = ABTN.TTm (ALit l)

pattern TBLit ::
(ABT.Var v) =>
Lit ->
ABTN.Term ANormalF v
pattern TBLit l = ABTN.TTm (ABLit l)

pattern TApp ::
(ABT.Var v) =>
Func v ->
Expand Down Expand Up @@ -1110,13 +1132,18 @@ pattern TBinds ctx bd <-
data SeqEnd = SLeft | SRight
deriving (Eq, Ord, Enum, Show)

-- Note: MatchNumeric is a new form for matching directly on boxed
-- numeric data. This leaves MatchIntegral around so that builtins can
-- continue to use it. But interchanged code can be free of unboxed
-- details.
data Branched e
= MatchIntegral (EnumMap Word64 e) (Maybe e)
| MatchText (Map.Map Util.Text.Text e) (Maybe e)
| MatchRequest (Map Reference (EnumMap CTag ([Mem], e))) e
| MatchEmpty
| MatchData Reference (EnumMap CTag ([Mem], e)) (Maybe e)
| MatchSum (EnumMap Word64 ([Mem], e))
| MatchNumeric Reference (EnumMap Word64 e) (Maybe e)
deriving (Show, Eq, Functor, Foldable, Traversable)

-- Data cases expected to cover all constructors
Expand Down Expand Up @@ -1496,6 +1523,11 @@ data BLit
| Quote Value
| Code (SuperGroup Symbol)
| BArr PA.ByteArray
| Pos Word64
| Neg Word64
| Char Char
| Float Double
| Arr (PA.Array Value)
deriving (Show)

groupVars :: ANFM v (Set v)
Expand Down Expand Up @@ -1711,14 +1743,8 @@ anfBlock (Match' scrut cas) = do
)
AccumText df cs ->
pure (sctx <> cx, pure . TMatch v $ MatchText cs df)
AccumIntegral r df cs -> do
i <- fresh
let dcs =
MatchDataCover
r
(EC.mapSingleton 0 ([UN], ABTN.TAbss [i] ics))
ics = TMatch i $ MatchIntegral cs df
pure (sctx <> cx, pure $ TMatch v dcs)
AccumIntegral r df cs ->
pure (sctx <> cx, pure $ TMatch v $ MatchNumeric r cs df)
AccumData r df cs ->
pure (sctx <> cx, pure . TMatch v $ MatchData r cs df)
AccumSeqEmpty _ ->
Expand Down Expand Up @@ -1747,21 +1773,22 @@ anfBlock (Match' scrut cas) = do
AccumSeqSplit en n mdf bd -> do
i <- fresh
r <- fresh
n <- fresh
s <- fresh
b <- binder
let split = ST1 (Indirect b) r BX (TCom op [i, v])
pure
( sctx <> cx <> directed [lit i, split i r],
pure . TMatch r . MatchSum $
( sctx <> cx <> directed [lit i, split],
pure . TMatch r . MatchDataCover Ty.seqViewRef $
mapFromList
[ (0, ([], df n)),
(1, ([BX, BX], bd))
[ (fromIntegral Ty.seqViewEmpty, ([], df s)),
(fromIntegral Ty.seqViewElem, ([BX, BX], bd))
]
)
where
op
| SLeft <- en = SPLL
| otherwise = SPLR
lit i = ST1 Direct i UN (TLit . N $ fromIntegral n)
split i r = ST1 Direct r UN (TPrm op [i, v])
| SLeft <- en = Builtin "List.splitLeft"
| otherwise = Builtin "List.splitRight"
lit i = ST1 Direct i BX (TBLit . N $ fromIntegral n)
df n =
fromMaybe
( TLet Direct n BX (TLit (T "pattern match failure")) $
Expand Down Expand Up @@ -1802,12 +1829,8 @@ anfBlock (Boolean' b) =
pure (mempty, pure $ TCon Ty.booleanRef (if b then 1 else 0) [])
anfBlock (Lit' l@(T _)) =
pure (mempty, pure $ TLit l)
anfBlock (Lit' l) = do
lv <- fresh
pure
( directed [ST1 Direct lv UN $ TLit l],
pure $ TCon (litRef l) 0 [lv]
)
anfBlock (Lit' l) =
pure (mempty, pure $ TBLit l)
anfBlock (Ref' r) = pure (mempty, (Indirect (), TCom r []))
anfBlock (Blank' b) = do
nm <- fresh
Expand Down Expand Up @@ -2033,6 +2056,8 @@ branchLinks _ g (MatchText m e) =
MatchText <$> traverse g m <*> traverse g e
branchLinks _ g (MatchIntegral m e) =
MatchIntegral <$> traverse g m <*> traverse g e
branchLinks _ g (MatchNumeric r m e) =
MatchNumeric r <$> traverse g m <*> traverse g e
branchLinks _ g (MatchSum m) =
MatchSum <$> (traverse . traverse) g m
branchLinks _ _ MatchEmpty = pure MatchEmpty
Expand Down Expand Up @@ -2252,6 +2277,9 @@ prettyBranches ind bs = case bs of
(uncurry $ prettyCase ind . shows)
id
(mapToList $ snd <$> bs)
MatchNumeric _ bs df ->
maybe id (\e -> prettyCase ind (showString "_") e id) df
. foldr (uncurry $ prettyCase ind . shows) id (mapToList bs)
-- _ -> error "prettyBranches: todo"
where
-- prettyReq :: Reference -> CTag -> ShowS
Expand Down
Loading
Loading