Skip to content

Commit db49996

Browse files
authored
Merge pull request #59 from grin-compiler/32-conversion-fix
Extended syntax: syntax conversion fix
2 parents f095825 + f123a71 commit db49996

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

grin/src/Transformations/ExtendedSyntax/Conversion.hs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,40 @@ instance Convertible Exp New.Exp where
125125
convert exp = fst $ evalNameM exp $ flip anaM exp $ \case
126126
(Program exts defs) -> pure $ New.ProgramF (map convert exts) defs
127127
(Def name args body) -> pure $ New.DefF (convert name) (map convert args) body
128-
{- NOTE: we assume Binding Pattern Simplification has been run
128+
129+
{- NOTE: We assume Binding Pattern Simplification has been run,
130+
and the value has been given the name v.0. This is a special
131+
case of the next program patterns. This one transforms the result
132+
of Binding Pattern Simplification to a more concise form.
133+
129134
v.0 <- pure <value>
130135
<non-var pat> <- pure v.0
131136
<rhs2>
137+
138+
<non-var pat> @ v.0 <- pure <value>
139+
<rhs2>
132140
-}
133141
(EBind lhs1 (Var var) rhs1)
134142
| EBind (SReturn (Var var')) pat rhs2 <- rhs1
135143
, isn't _Var pat
136144
, var == var'
137145
-> pure $ New.EBindF lhs1 (New.AsPat (convert var) (convert pat)) rhs2
146+
{- NOTE: In this case, v.0 has been defined earlier in the program.
147+
This is a more general case that covers the one before as well.
148+
149+
v.0 <- pure <value>
150+
<...>
151+
<non-var pat> <- pure v.0
152+
<rhs>
153+
154+
v.0 <- pure <value>
155+
<...>
156+
<non-var pat> @ a.0 <- pure v.0
157+
<rhs>
158+
-}
159+
(EBind lhs pat rhs) | isn't _Var pat -> do
160+
asPatName <- deriveNewName "a"
161+
pure $ New.EBindF lhs (New.AsPat (convert asPatName) (convert pat)) rhs
138162
(EBind lhs (Var var) rhs)
139163
-> pure $ New.EBindF lhs (New.VarPat $ convert var) rhs
140164
(ECase scrut alts)
@@ -158,6 +182,7 @@ instance Convertible Exp New.Exp where
158182
(Alt cpat exp) -> do
159183
altName <- deriveNewName "alt"
160184
pure $ New.NAltF (convert cpat) (convert altName) exp
185+
_ -> error "Conversion from Old to New has failed: unexpected AST pattern"
161186

162187
instance Convertible New.TagType TagType where
163188
convert = \case
@@ -257,10 +282,10 @@ instance Convertible New.Exp Exp where
257282
convertToNew :: Exp -> New.Exp
258283
convertToNew = convert . nameEverything
259284

260-
-- TODO: modify CopyPropagation such that it removes resulting dead bindings (see CopyPropagation.hs)
261285
nameEverything :: Exp -> Exp
262286
nameEverything
263-
= bindNormalisation
287+
= copyPropagation
288+
. bindNormalisation
264289
. nodeArgumentNaming
265290
. bindNormalisation
266291
. appArgumentNaming

grin/src/Transformations/Optimising/CopyPropagation.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ import Transformations.Util
1515
NOTE:
1616
Do not propagate literal values because literals are not used for optimisations. (GRIN is not a supercompiler)
1717
Only propagates variables. It does not cause performance penalty, LLVM will optimise the code further.
18-
19-
TODO:
20-
CUrrently, copy propagation does not remove the resulting dead bindings, SDVE does. However, SDVE needs interprocedural
21-
information such as the type env (this can be removed) and the effect map. Maybe copy propagation should remove
22-
the binding for which it already substituted the variable.
23-
2418
-}
2519

2620
type Env = (Map Val Val, Map Name Name)
@@ -69,5 +63,15 @@ copyPropagation e = hylo folder builder (mempty, e) where
6963
| val == lpat
7064
, isConstant val
7165
-> rightExp
66+
-- left unit law ; cleanup x <- pure y copies
67+
{- NOTE: This case could be handled by SDVE as well, however
68+
performing it locally saves us an effect tracking analysis.
69+
This is because here, we have more information about variable
70+
bidnings. We know for sure that such copying bindings are not needed
71+
since all the occurences of the left-hand side have been replaced with
72+
the variable on the right-hand side.
73+
-}
74+
EBindF (SReturn Var{}) Var{} rightExp
75+
-> rightExp
7276

7377
exp -> embed exp

grin/src/Transformations/Simplifying/BindingPatternSimplification.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ bindingPatternSimplification e = evalNameM e . cataM alg $ e where
2222
alg :: ExpF Exp -> NameM Exp
2323
alg = \case
2424

25+
-- NOTE: <pat> <- pure <var>
26+
-- The above pattern does not need to be simplified.
27+
EBindF lhs@(SReturn Var{}) pat rhs ->
28+
pure $ EBind lhs pat rhs
29+
2530
-- NOTE: binding to Unit?
2631
EBindF lhs pat rhs | isn't _ValVar pat -> do
2732
newVar <- fmap Var newNodeName

grin/test/Transformations/Optimising/CopyPropagationSpec.hs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ spec = do
2525
|]
2626
let after = [expr|
2727
a1 <- pure 1
28-
a2 <- pure a1
29-
a3 <- pure a1
3028
pure a1
3129
|]
3230
copyPropagation (ctx before) `sameAs` (ctx after)
@@ -41,8 +39,6 @@ spec = do
4139
|]
4240
let after = [expr|
4341
a1 <- pure 1
44-
a2 <- pure a1
45-
a3 <- pure a1
4642
case a1 of
4743
#default -> pure a1
4844
|]
@@ -61,11 +57,7 @@ spec = do
6157
let after = [expr|
6258
a1 <- pure 1
6359
n1 <- pure (CNode a1 0)
64-
n2 <- pure n1
65-
a2 <- pure a1
6660
b1 <- pure 0
67-
b2 <- pure b1
68-
a3 <- pure a1
6961
pure (CNode a1 b1)
7062
|]
7163
copyPropagation (ctx before) `sameAs` (ctx after)
@@ -84,7 +76,6 @@ spec = do
8476
a1 <- pure 1
8577
b1 <- pure 0
8678
n1 <- pure (CNode a1 b1)
87-
a2 <- pure a1
8879
n2 <- pure (CNode a1 b1)
8980
case n2 of
9081
#default -> pure n2
@@ -119,7 +110,6 @@ spec = do
119110
|]
120111
let after = [expr|
121112
a1 <- pure 1
122-
a2 <- pure a1
123113
0 <- pure 1
124114
pure a1
125115
|]

0 commit comments

Comments
 (0)