@@ -165,46 +165,52 @@ whenChanged f act = do
165
165
166
166
descend ::
167
167
(Memo m , Var v ) =>
168
- (Bool -> ANormal v -> m (ANormal v )) ->
168
+ (Bool -> Set v -> ANormal v -> m (ANormal v )) ->
169
169
Bool ->
170
+ Set v ->
170
171
ANormal v ->
171
172
m (ANormal v )
172
- descend rec tail tm = memo tm $ case tm of
173
+ descend rec tail bound tm = memo tm $ case tm of
173
174
TLets d vs ccs bn bd ->
174
- TLets d vs ccs <$> rec False bn <*> rec tail bd
175
+ TLets d vs ccs <$> rec False bound bn <*> rec tail bnd bd
176
+ where
177
+ bnd = Set. union (Set. fromList vs) bound
175
178
TName v f vs bd ->
176
- TName v f vs <$> rec tail bd
179
+ TName v f vs <$> rec tail ( Set. insert v bound) bd
177
180
TMatch v bs ->
178
- TMatch v <$> traverse (rec tail ) bs
181
+ TMatch v <$> traverse (rec tail bound ) bs
179
182
TShift r v bd ->
180
- TShift r v <$> rec tail bd
183
+ TShift r v <$> rec tail ( Set. insert v bound) bd
181
184
THnd rs hn ha bd ->
182
- THnd rs hn ha <$> rec tail bd
185
+ THnd rs hn ha <$> rec tail bound bd
183
186
TLocal v bd ->
184
- TLocal v <$> rec tail bd
187
+ TLocal v <$> rec tail bound bd
185
188
ABTN. TAbs v (ABTN. TAbss vs bd) ->
186
- ABTN. TAbss (v : vs) <$> rec tail bd
189
+ ABTN. TAbss (v : vs) <$> rec tail bnd bd
190
+ where
191
+ bnd = Set. union (Set. fromList $ v : vs) bound
187
192
_ -> pure tm
188
193
189
194
-- Rewrites a term from the top down, first applying the step
190
195
-- transform given, then descending to children.
191
196
rewriteDown ::
192
197
(Memo m , Var v ) =>
193
- (Bool -> ANormal v -> m (ANormal v )) ->
198
+ (Bool -> Set v -> ANormal v -> m (ANormal v )) ->
194
199
ANormal v ->
195
200
m (ANormal v )
196
- rewriteDown step = go True
201
+ rewriteDown step = go True Set. empty
197
202
where
198
- go tail tm = step tail tm >>= descend go tail
203
+ go tail bound tm = step tail bound tm >>= descend go tail bound
199
204
200
205
rewriteUp ::
201
206
(Memo m , Var v ) =>
202
- (Bool -> ANormal v -> m (ANormal v )) ->
207
+ (Bool -> Set v -> ANormal v -> m (ANormal v )) ->
203
208
ANormal v ->
204
209
m (ANormal v )
205
- rewriteUp step = go True
210
+ rewriteUp step = go True Set. empty
206
211
where
207
- go tail tm = memo tm (descend go tail tm) >>= step tail
212
+ go tail bound tm =
213
+ memo tm (descend go tail bound tm) >>= step tail bound
208
214
209
215
-- Performs inlining on a `SuperGroup` using the inlining information
210
216
-- in the map. The map can be created from typical `SuperGroup` data
@@ -225,15 +231,15 @@ inline avoid (arities, inls) n0 = memo n0 $ go (30 :: Int) n0
225
231
| n <= 0 = pure tm
226
232
| otherwise = rewriteUp (step n) tm
227
233
228
- step n tail (TApp (FComb r) args)
229
- | Just new <- findInline tail r args =
234
+ step n tail bound (TApp (FComb r) args)
235
+ | Just new <- findInline tail bound r args =
230
236
dirty *> go (n - 1 ) new
231
- step _ _tail tm = pure tm
237
+ step _ _tail _bound tm = pure tm
232
238
233
- findInline tail r args = do
239
+ findInline tail bound r args = do
234
240
info <- Map. lookup r inls
235
241
arity <- Map. lookup r arities
236
- tweak tail args arity info
242
+ tweak tail bound args arity info
237
243
238
244
don'tInline Don'tInl _ = True
239
245
don'tInline TailInl isTail = not isTail
@@ -245,17 +251,17 @@ inline avoid (arities, inls) n0 = memo n0 $ go (30 :: Int) n0
245
251
-- multiple inlining steps, so we freshen anything else we inline
246
252
-- to not be capable of capturing the variables from the entry
247
253
-- code.
248
- tweak isTail args arity (InlInfo clazz (ABTN. TAbss vs body))
254
+ tweak isTail bound args arity (InlInfo clazz (ABTN. TAbss vs body))
249
255
| don'tInline clazz isTail = Nothing
250
256
-- exactly saturated
251
257
| length args == arity,
252
258
rn <- Map. fromList (zip vs args) =
253
- Just $ ABTN. renamesAvoiding avoid rn body
259
+ Just $ ABTN. renamesAvoiding ( avoid `Set.union` bound) rn body
254
260
-- oversaturated, only makes sense if body is a call
255
261
| length args > arity,
256
262
(pre, post) <- splitAt arity args,
257
263
rn <- Map. fromList (zip vs pre),
258
- TApp f pre <- ABTN. renamesAvoiding avoid rn body =
264
+ TApp f pre <- ABTN. renamesAvoiding ( avoid `Set.union` bound) rn body =
259
265
Just $ TApp f (pre ++ post)
260
266
| otherwise = Nothing
261
267
@@ -276,7 +282,7 @@ peephole arities affine n0 = memo n0 $ go (30 :: Int) n0
276
282
where
277
283
go 0 = pure
278
284
go n =
279
- whenChanged (go $ n - 1 ) . rewriteDown \ tail -> \ case
285
+ whenChanged (go $ n - 1 ) . rewriteDown \ tail _bound -> \ case
280
286
-- eliminate `v = u` bindings in affine contexts
281
287
TLet _ v _ (TVar u) bd
282
288
| affine -> ABTN. rename v u bd <$ dirty
@@ -338,9 +344,11 @@ optSuper ::
338
344
Bool ->
339
345
SuperNormal v ->
340
346
m (SuperNormal v )
341
- optSuper opts avoid affine sn@ (Lambda ccs (ABTN. TAbss vs bd)) =
347
+ optSuper opts avoid0 affine sn@ (Lambda ccs (ABTN. TAbss vs bd)) =
342
348
memo sn $
343
349
Lambda ccs . ABTN. TAbss vs <$> optNormal opts avoid affine bd
350
+ where
351
+ avoid = Set. union (Set. fromList vs) avoid0
344
352
345
353
-- Optimizes a single group
346
354
optGroup ::
@@ -716,6 +724,7 @@ translateHandlerMatch ::
716
724
(Var v ) => OptInfos v -> v -> v -> SuperNormal v -> Maybe (SuperNormal v )
717
725
translateHandlerMatch opts self ah (Lambda ccs (ABTN. TAbss args body))
718
726
| v : vs <- shiftArgs args,
727
+ bound <- Set. fromList (self : args),
719
728
TMatch u branches <- body,
720
729
u == v,
721
730
MatchRequest cs df <- branches,
@@ -725,7 +734,7 @@ translateHandlerMatch opts self ah (Lambda ccs (ABTN.TAbss args body))
725
734
. ABTN. TAbss args
726
735
. TMatch u
727
736
. flip MatchRequest df
728
- <$> traverse3 (affineHandlerCase opts self vs ah) cs
737
+ <$> traverse3 (affineHandlerCase opts self bound vs ah) cs
729
738
| otherwise = Nothing
730
739
where
731
740
ar = freshAff 2
@@ -753,12 +762,12 @@ augmentHandlerEntry thunk0 mv0 ah body
753
762
-- Recognizes an affine handler case, yielding a translated efficient
754
763
-- version if it is one.
755
764
affineHandlerCase ::
756
- (Var v ) => OptInfos v -> v -> [v ] -> v -> ANormal v -> Maybe (ANormal v )
757
- affineHandlerCase opts self vs rec br
765
+ (Var v ) => OptInfos v -> v -> Set v -> [v ] -> v -> ANormal v -> Maybe (ANormal v )
766
+ affineHandlerCase opts self bound vs rec br
758
767
| ABTN. TAbss us body <- br,
759
768
TShift _ kf0 body <- body,
760
769
TName kf (Left (Builtin " jumpCont" )) [kf1] body <- body,
761
- bound <- Set. fromList (kf0 : kf : us),
770
+ bound <- Set. union bound ( Set. fromList (kf0 : kf : us) ),
762
771
kf0 == kf1 =
763
772
ABTN. TAbss us
764
773
<$> affinePreBranch opts self bound vs rec ar kf body
0 commit comments