Skip to content

Commit 981db03

Browse files
committed
Fix handling of known tag in case with default in simplifier (#564)
This was already mostly fixed on new-pipeline, except for the scenario where the tag matches none of the alternatives and no default is given (which should not occur?). This commit backports that fix, and handles the case where no default is present. It also renames 'x' to 'tag' to not suggest that it has the same type and role as 'xs'.
1 parent 4583e76 commit 981db03

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/Data/Array/Accelerate/AST.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module Data.Array.Accelerate.AST (
8282
ALeftHandSide, ArrayVar, ArrayVars,
8383

8484
-- ** Scalar expressions
85-
ELeftHandSide, ExpVar, ExpVars, expVars,
85+
ELeftHandSide, ExpVar, ExpVars, expVars, undefs,
8686
Fun, OpenFun(..),
8787
Exp, OpenExp(..),
8888
Boundary(..),
@@ -513,6 +513,10 @@ expVars TupRunit = Nil
513513
expVars (TupRsingle var) = Evar var
514514
expVars (TupRpair v1 v2) = expVars v1 `Pair` expVars v2
515515

516+
undefs :: TypeR t -> OpenExp env aenv t
517+
undefs (TupRsingle tp) = Undef tp
518+
undefs (TupRpair t1 t2) = undefs t1 `Pair` undefs t2
519+
undefs TupRunit = Nil
516520

517521
-- | Vanilla open expressions using de Bruijn indices for variables ranging
518522
-- over tuples of scalars and arrays of tuples. All code, except Cond, is

src/Data/Array/Accelerate/Trafo/Simplify.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,26 +337,34 @@ simplifyOpenExp env = first getAny . cvtE
337337
-> (Any, [(TAG, OpenExp env aenv b)])
338338
-> (Any, Maybe (OpenExp env aenv b))
339339
-> (Any, OpenExp env aenv b)
340-
caseof x@(_,x') xs@(_,xs') md@(_,md')
341-
| Const _ t <- x'
342-
= Stats.caseElim "known" (yes (fromJust $ lookup t xs'))
340+
caseof tag@(_,tag') xs@(_,xs') md@(_,md')
341+
| Const _ t <- tag'
342+
= Stats.caseElim "known" $ yes $ fromMaybe
343+
-- If the tag matches no alternative, then return the default (if present)
344+
-- or undef.
345+
(fromMaybe (undefs tp) md')
346+
$ lookup t xs'
343347
| Just d <- md'
344348
, [] <- xs'
345349
= Stats.caseElim "redundant" (yes d)
346350
| Just d <- md'
347351
, [(_,(_,u))] <- us
348352
, Just Refl <- matchOpenExp d u
349-
= Stats.caseDefault "merge" $ yes (Case x' (map snd vs) (Just u))
353+
= Stats.caseDefault "merge" $ yes (Case tag' (map snd vs) (Just u))
350354
| Nothing <- md'
351355
, [] <- vs
352356
, [(_,(_,u))] <- us
353357
= Stats.caseElim "overlap" (yes u)
354358
| Nothing <- md'
355359
, [(_,(_,u))] <- us
356-
= Stats.caseDefault "introduction" $ yes (Case x' (map snd vs) (Just u))
360+
= Stats.caseDefault "introduction" $ yes (Case tag' (map snd vs) (Just u))
357361
| otherwise
358-
= Case <$> x <*> xs <*> md
362+
= Case <$> tag <*> xs <*> md
359363
where
364+
tp
365+
| Just d <- md' = expType d
366+
| (_, x) : _ <- xs' = expType x
367+
| otherwise = internalError "Empty case statement should not occur"
360368
(us,vs) = partition (\(n,_) -> n > 1)
361369
$ Map.elems
362370
. Map.fromListWith merge

0 commit comments

Comments
 (0)