Skip to content

Commit

Permalink
Check whether original is consumed before simplifying.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas authored and CKuke committed Nov 8, 2023
1 parent 1bb9b00 commit 4575c8f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Futhark/Optimise/Simplify/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ emptyArrayToScratch _ _ = Skip

simplifyIndex :: (BuilderOps rep) => BottomUpRuleBasicOp rep
simplifyIndex (vtable, used) pat@(Pat [pe]) (StmAux cs attrs _) (Index idd inds)
| Just m <- simplifyIndexing vtable seType idd inds consumed =
| Just m <- simplifyIndexing vtable seType idd inds consumed consuming =
Simplify $ certifying cs $ do
res <- m
attributing attrs $ case res of
Expand All @@ -134,7 +134,8 @@ simplifyIndex (vtable, used) pat@(Pat [pe]) (StmAux cs attrs _) (Index idd inds)
IndexResult extra_cs idd' inds' ->
certifying extra_cs $ letBindNames (patNames pat) $ BasicOp $ Index idd' inds'
where
consumed = patElemName pe `UT.isConsumed` used
consuming = (`UT.isConsumed` used)
consumed = consuming $ patElemName pe
seType (Var v) = ST.lookupType v vtable
seType (Constant v) = Just $ Prim $ primValueType v
simplifyIndex _ _ _ _ = Skip
Expand Down
6 changes: 4 additions & 2 deletions src/Futhark/Optimise/Simplify/Rules/Index.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ simplifyIndexing ::
VName ->
Slice SubExp ->
Bool ->
(VName -> Bool) ->
Maybe (m IndexResult)
simplifyIndexing vtable seType idd (Slice inds) consuming =
simplifyIndexing vtable seType idd (Slice inds) consuming consumed =
case defOf idd of
_
| Just t <- seType (Var idd),
Expand Down Expand Up @@ -100,11 +101,11 @@ simplifyIndexing vtable seType idd (Slice inds) consuming =
<$> subExpSlice (sliceSlice (primExpSlice ais) (primExpSlice (Slice inds)))
Just (Replicate (Shape [_]) (Var vv), cs)
| [DimFix {}] <- inds,
not consuming,
ST.available vv vtable ->
Just $ pure $ SubExpResult cs $ Var vv
| DimFix {} : is' <- inds,
not consuming,
not $ consumed vv,
ST.available vv vtable ->
Just $ pure $ IndexResult cs vv $ Slice is'
Just (Replicate (Shape [_]) val@(Constant _), cs)
Expand All @@ -130,6 +131,7 @@ simplifyIndexing vtable seType idd (Slice inds) consuming =
Just (Replicate (Shape []) (Var src), cs)
| Just dims <- arrayDims <$> seType (Var src),
length inds == length dims,
not $ consumed src,
-- It is generally not safe to simplify a slice of a copy,
-- because the result may be used in an in-place update of the
-- original. But we know this can only happen if the original
Expand Down

0 comments on commit 4575c8f

Please sign in to comment.