Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
324 commits
Select commit Hold shift + click to select a range
1495b25
minor fix on free vars
ychenfo Feb 21, 2025
fc67528
wip: a select can be in multiple match arms (nested)
ychenfo Feb 21, 2025
b11d654
also keep track of which matching stmts a selection belongs to
ychenfo Feb 22, 2025
4e221bc
correct selections in match stmts
ychenfo Feb 25, 2025
ab886a1
fix the filtering of strategies
ychenfo Feb 25, 2025
935b5ff
does not generate tmp vars for function arguments
ychenfo Feb 26, 2025
1b5901b
improve previous commit on avoiding tmp vars for function args
ychenfo Feb 27, 2025
58c4cef
selections in nested matching arms are not considered
ychenfo Feb 27, 2025
210d96a
update tests
ychenfo Feb 27, 2025
414e23f
better rewritten of rest
ychenfo Feb 27, 2025
6ed7e76
minor
ychenfo Feb 28, 2025
479bcd1
cleanup
ychenfo Feb 28, 2025
210fa1d
wip: functions for match arms
ychenfo Mar 1, 2025
264983e
function for match arms
ychenfo Mar 3, 2025
31376e5
update tests
ychenfo Mar 3, 2025
8e64cd6
clean up
ychenfo Mar 3, 2025
d1f99fe
fix parameter order and cleanup
ychenfo Mar 3, 2025
b248653
use set to record dtor sources
ychenfo Mar 4, 2025
a81ca85
wip: try to replace select when rewriting
ychenfo Mar 4, 2025
a187b4f
wip: restore changes to keep track of selections in nested matches; u…
ychenfo Mar 4, 2025
84ff209
Merge branch 'hkmc2' into hkmc2-deforest
ychenfo Mar 5, 2025
57df882
wip: update after merge, no need to flatten and merge arms in defores…
ychenfo Mar 5, 2025
f27b72c
toplevel symbols are not freevars
ychenfo Mar 5, 2025
270495a
Merge remote-tracking branch 'anto/hkmc2-deforest' into hkmc2-deforest
ychenfo Mar 5, 2025
f3833d6
fix error by updating free var for fun defns
ychenfo Mar 5, 2025
d7c7e42
use blocktraverser
ychenfo Mar 5, 2025
32130a5
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 6, 2025
da297ea
update tests
ychenfo Mar 6, 2025
8e3eebd
wip: restore my changes on function `Block.freeVars`
ychenfo Mar 6, 2025
aaac95e
use my own free var function
ychenfo Mar 6, 2025
0a02e4f
restore my changes to the definition of Block.Match
ychenfo Mar 6, 2025
f4a80b7
update tests
ychenfo Mar 6, 2025
7f07ab8
revert my changes toBblockTransformer and properly implement ReplaceL…
ychenfo Mar 6, 2025
64994f4
minor
ychenfo Mar 6, 2025
c6550cf
better scopeExtrusionInfo; more tests
ychenfo Mar 6, 2025
ca4cdd5
add test
ychenfo Mar 6, 2025
dec504d
wip: proper handling of sels in nested matches
ychenfo Mar 6, 2025
bf504b6
wip: proper handling of sels in nested matches
ychenfo Mar 7, 2025
291b868
wip
ychenfo Mar 7, 2025
f021437
wip: use pre-computed symbols for rewriting, also prevent passing unu…
ychenfo Mar 7, 2025
d144789
seems to work; need cleanup
ychenfo Mar 7, 2025
b6ee001
wip: update tests
ychenfo Mar 7, 2025
d626ecf
ctordest and ctorfinaldests are already branch-specific
ychenfo Mar 7, 2025
8eca442
some cleanup and update tests
ychenfo Mar 7, 2025
832db64
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 7, 2025
eb0a955
fix non determinism
ychenfo Mar 7, 2025
06a4ddb
further cleanup
ychenfo Mar 8, 2025
4c472fd
fix: there should be one CtorFinalDest per arm for each pat mat expr
ychenfo Mar 8, 2025
8a0fcc6
wip: add problematic tests
ychenfo Mar 9, 2025
42977e6
wip
ychenfo Mar 10, 2025
8b2496e
wip: move DeforestTransformer out
ychenfo Mar 10, 2025
c39b54d
further fixes to free vars of matches; found the new problem of missi…
ychenfo Mar 10, 2025
2365471
update test
ychenfo Mar 11, 2025
44239ab
keep track of how matches are nested
ychenfo Mar 11, 2025
75cf3af
wip: seems to fix the missing `rest`s
ychenfo Mar 11, 2025
43f738e
update tests
ychenfo Mar 12, 2025
79c489f
minor fixes for selections
ychenfo Mar 12, 2025
bf718c8
update todos with more test cases and comments
ychenfo Mar 12, 2025
3f3fe05
cleanup and more tests
ychenfo Mar 12, 2025
5d96ecb
better usage of blocktransformer
ychenfo Mar 13, 2025
2268f3c
further fix to take care of the `rest`s from non-fusing parent match …
ychenfo Mar 13, 2025
41fd30c
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 14, 2025
7f85ac1
fix after merge: add val to public class fields; remove subst for blo…
ychenfo Mar 14, 2025
f117a68
another problematic test
ychenfo Mar 17, 2025
8991cd3
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 17, 2025
640cc59
more comments on tests
ychenfo Mar 18, 2025
87781c3
check and compare outputs from deforestated programs
ychenfo Mar 18, 2025
c5afea7
update test
ychenfo Mar 18, 2025
921665d
remove a useless field from CtorFinalDest.Match
ychenfo Mar 19, 2025
f901a80
add another test
ychenfo Mar 19, 2025
120d7be
wip: try to take care of matches from multiple levels up
ychenfo Mar 20, 2025
5615ba8
update test; need to clean undefined vars in dead code...
ychenfo Mar 21, 2025
6a664fc
better name; wip: tidy up computation of free vars
ychenfo Mar 24, 2025
7d9ce98
remove wrong fixmes; add more comments
ychenfo Mar 24, 2025
4d337fd
don't rewrite already rewritten blocks
ychenfo Mar 24, 2025
b258545
revisit free var computation and update comments...
ychenfo Mar 24, 2025
2373e06
improve `matchRest` impl
ychenfo Mar 25, 2025
58cc209
minor fix and a new test
ychenfo Mar 25, 2025
35679fe
include runtimeSymbol as always in scope
ychenfo Mar 25, 2025
d65224e
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Mar 25, 2025
4614a0d
better handling for dflt arms
ychenfo Mar 27, 2025
d029daf
better names for fields of classes
ychenfo Mar 27, 2025
4ba1ea9
update fixme tests
ychenfo Mar 28, 2025
a855c0f
tests output program despite of undefined var
ychenfo Mar 29, 2025
a666b60
avoid creating needless `match_rest` functions
ychenfo Mar 29, 2025
8d8a83a
use extension method for resultId
ychenfo Mar 29, 2025
b61ea2e
update test: found code duplication; some problems are masked by chan…
ychenfo Mar 30, 2025
7a3cb55
another duplication
ychenfo Mar 31, 2025
f4c693f
fix for avoiding the previous duplication
ychenfo Mar 31, 2025
f86dc2c
minor
ychenfo Mar 31, 2025
f8c2fc5
use flatten to remove some dead code which contains free vars
ychenfo Mar 31, 2025
b648936
remove dead code which contains f.v.s in match `rest`
ychenfo Apr 1, 2025
5194522
return block unchanged instead of panic for unsupported cases
ychenfo Apr 1, 2025
4b27f05
wip: no need for two repl hosts
ychenfo Apr 2, 2025
0739bf6
better handling for noprod
ychenfo Apr 2, 2025
8bcb753
update test
ychenfo Apr 2, 2025
a16a2b7
minor fix
ychenfo Apr 2, 2025
31bc090
fusion stats
ychenfo Apr 2, 2025
0e68eec
improve jsbackenddiffmaker
ychenfo Apr 2, 2025
2486b4d
lessen output
ychenfo Apr 2, 2025
f75d2be
use locally; use `data class`
ychenfo Apr 2, 2025
b92abee
cleanup jsbackenddiffmaker
ychenfo Apr 3, 2025
3a47e3b
better code: avoid local `object`s
ychenfo Apr 3, 2025
33f4703
tests from meeting
ychenfo Apr 3, 2025
1553cc8
more comments
ychenfo Apr 3, 2025
ca9a63f
wip: more needs to be done for things like `id` to block fusion
ychenfo Apr 7, 2025
a3b4308
use of functions without defn now really blocks fusion
ychenfo Apr 7, 2025
ce79cb5
more helpful deforestation debug output from `tl.log`
ychenfo Apr 7, 2025
f0c022c
fresh type var for `throw` to avoid blocking fusion
ychenfo Apr 8, 2025
0e96f9e
more recursive tests
ychenfo Apr 8, 2025
418ee33
zip unzip tests
ychenfo Apr 9, 2025
c91409c
use infix `::`; more tests
ychenfo Apr 9, 2025
987e7bd
more tests
ychenfo Apr 9, 2025
2efe8c6
fix: match block now correctly contain the scrut as a f.v. (if it is …
ychenfo Apr 10, 2025
6ade5c3
update fix
ychenfo Apr 10, 2025
8f7639d
fix nested definitions using `definedVar`; more tests
ychenfo Apr 10, 2025
aafc673
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 10, 2025
2519a80
style: use `locally:`
ychenfo Apr 10, 2025
7e835e1
trailing whitespaces
ychenfo Apr 10, 2025
dbd9173
remove useless `locally`; add reified tests
ychenfo Apr 11, 2025
3125840
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 11, 2025
15d1524
minor
ychenfo Apr 15, 2025
56e2373
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 16, 2025
04bdcfb
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 22, 2025
5e19b31
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 22, 2025
51485b3
fix `Deforetation.scala`
ychenfo Apr 22, 2025
1048529
deduplicate logic in `JSBackendDiffMaker.scala`
ychenfo Apr 23, 2025
d7ce214
use identity for ResultId and remove global state
ychenfo Apr 24, 2025
48467a7
remove awkward type projection
ychenfo Apr 24, 2025
cadb80b
better documentation on the todo about ctor as function; properly blo…
ychenfo Apr 24, 2025
79e38c7
just use `ResultId`
ychenfo Apr 24, 2025
10c9d22
better handling of throw and instantiate
ychenfo Apr 24, 2025
daf8749
update inappropriate uses of `???` in `Deforestation.scala`
ychenfo Apr 25, 2025
793c57e
minor update on comment
ychenfo Apr 25, 2025
fd306ac
minor
ychenfo Apr 25, 2025
7edb825
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo Apr 26, 2025
e382b8e
wip: use linkedhashmap for determinism; mutate map instead of generat…
ychenfo Apr 27, 2025
8b2b77a
fix another nondeterministic behavior; add some tests
ychenfo Apr 27, 2025
f5da463
remove the useless ordering and the unnecessary lazy val which only s…
ychenfo Apr 27, 2025
e92a50f
cleanup
ychenfo Apr 27, 2025
a529545
do not silently discard extra parameter lists
ychenfo Apr 27, 2025
f4f7f95
clarify comment
ychenfo Apr 28, 2025
0e662c3
track where a ctor expr come from
ychenfo Apr 28, 2025
5d3c3ff
minor fix
ychenfo Apr 28, 2025
245a011
wip
ychenfo Apr 29, 2025
e56612e
remove unnecessary default arg; better type var name
ychenfo Apr 29, 2025
ef9c008
Merge branch 'hkmc2' into hkmc2-deforest
LPTK Apr 30, 2025
028f5f4
improve fusion clash resolving impl
ychenfo May 1, 2025
0546800
improve stratvar uid impl
ychenfo May 1, 2025
0da2822
further improve clash resolving impl
ychenfo May 1, 2025
817d408
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-deforest
ychenfo May 1, 2025
53bccba
also track if a type var represent a call result
ychenfo May 2, 2025
c8455b9
wip
ychenfo May 2, 2025
b0b9c8d
wip: keep track of intermediate vars as ctor dests
ychenfo May 2, 2025
0bbce59
only track vars that represent call res
ychenfo May 5, 2025
06484e7
wip: find def dup chances
ychenfo May 5, 2025
fc63b1a
wip: still need to compute all upperBounds
ychenfo May 6, 2025
ee34529
minor cleanup
ychenfo May 6, 2025
e26a234
find def dup chance
ychenfo May 7, 2025
c7fe134
need a toMap to dedup
ychenfo May 7, 2025
3759331
improve later: flag for deforest def dup
ychenfo May 7, 2025
5951318
try to fix nondeterminism
ychenfo May 7, 2025
f9c216b
minor; update test
ychenfo May 7, 2025
b59f46c
ignore obvious recursive callres
ychenfo May 8, 2025
c457613
minor
ychenfo May 8, 2025
fe02fa7
cache some result
ychenfo May 8, 2025
3a4f269
improve where to check obvious recursive calls
ychenfo May 9, 2025
7c49c57
minor fix
ychenfo May 9, 2025
ee963ca
fix the passing of `inDef`
ychenfo May 9, 2025
85cc27f
another duplication chance
ychenfo May 9, 2025
d9d9ace
update comment to include an example for dead code using never assign…
ychenfo May 9, 2025
90841f9
Merge branch 'hkmc2-deforest' into hkmc2-def-duplicate
ychenfo May 9, 2025
d693b26
fix tests
ychenfo May 10, 2025
f5065fa
add stub implementation for `notOnlyOneCallSite` filtering of duplica…
ychenfo May 10, 2025
a07b201
more todos and tothinks...
ychenfo May 10, 2025
4fef072
rewrite find def dup chance
ychenfo May 11, 2025
5fa7a0d
fix nondeterminism
ychenfo May 11, 2025
82f7846
remove old impl
ychenfo May 11, 2025
d59d322
update tests
ychenfo May 11, 2025
f3b05d8
more updates to tests
ychenfo May 11, 2025
32696c5
wip: basic def dup impl with very rough driver code
ychenfo May 11, 2025
d0b60e8
also dup those inner calls whose result flows into the call results t…
ychenfo May 12, 2025
bb10714
update tests
ychenfo May 12, 2025
31a56ae
typo
ychenfo May 12, 2025
771c0c9
pipeline: deforest the duplicated program; update tests
ychenfo May 12, 2025
22415f9
to fix: fix dup of recursive calls
ychenfo May 12, 2025
f41f9b4
local defined vars should also be renewed when duplicating defns
ychenfo May 12, 2025
dc03d25
wip: cleanup; update tests
ychenfo May 12, 2025
702cdd7
update tests
ychenfo May 13, 2025
db1e49a
previous method wip
ychenfo May 13, 2025
83b5759
save some tests
ychenfo May 15, 2025
f491f7c
wip: polymorphism
ychenfo May 27, 2025
14d8a64
move instantiate function
ychenfo May 30, 2025
d489137
ctor keep track of instantiate id
ychenfo Jun 2, 2025
abc0333
wip: preparation traversal to store function bodies
ychenfo Jun 2, 2025
de255e8
allow multiple creation of Dtor strat since now strats can be duplicated
ychenfo Jun 2, 2025
5a73860
update instantiate
ychenfo Jun 3, 2025
1dfb527
do not add unnecessary constraints to the work list
ychenfo Jun 4, 2025
2f281f4
also include constraints from instantiating other functions into the …
ychenfo Jun 4, 2025
ecb2842
wip: instantiate accumulate call path for ctor
ychenfo Jun 4, 2025
e4a4d78
wip: ctor dests differentiate between instantiations
ychenfo Jun 4, 2025
588c155
correctly update indef
ychenfo Jun 5, 2025
080c25e
only solve constraints from top-level
ychenfo Jun 5, 2025
1561322
refactoring...
ychenfo Jun 8, 2025
ec3deae
wip: collector
ychenfo Jun 9, 2025
84f9590
wip: resolve clashes
ychenfo Jun 10, 2025
ac4ef44
fix
ychenfo Jun 10, 2025
0cc725d
fix: no check when collecting constraints, and inst duplicate things …
ychenfo Jun 10, 2025
1988f32
some new tests
ychenfo Jun 10, 2025
852cdc5
wip
ychenfo Jun 10, 2025
b48f3d4
fix
ychenfo Jun 10, 2025
208af14
small improvements
ychenfo Jun 10, 2025
5155e91
fix selection filter
ychenfo Jun 10, 2025
6a7392b
stable resultid when needed
ychenfo Jun 10, 2025
7548db4
tests
ychenfo Jun 10, 2025
677b9f0
distinguish instantiation id: Some(Nil) vs None
ychenfo Jun 11, 2025
7ae110f
wip
ychenfo Jun 11, 2025
6210ef2
more on cycle detection
ychenfo Jun 11, 2025
c1acf12
wip
ychenfo Jun 12, 2025
8c424eb
selections also have instantiation ids now
ychenfo Jun 13, 2025
f8ecafc
free vars traversers
ychenfo Jun 13, 2025
7d78d05
more lastWords instead of ???
ychenfo Jun 13, 2025
4702a5d
also precompute tmp vars for the fields of fusing ctors
ychenfo Jun 13, 2025
341730c
match arms and tests
ychenfo Jun 14, 2025
f15be70
transform
ychenfo Jun 14, 2025
96ba053
make new functions
ychenfo Jun 14, 2025
26e8ecb
some fixes to free vars
ychenfo Jun 15, 2025
705b0a3
better instantiation id suffix
ychenfo Jun 15, 2025
7409fe7
better logic
ychenfo Jun 15, 2025
53d915c
correct instantiatino id in rewrite
ychenfo Jun 15, 2025
41de79b
fix instantiation id for recursive calls; more tests
ychenfo Jun 15, 2025
cec7eed
minor
ychenfo Jun 15, 2025
74afb68
minor
ychenfo Jun 16, 2025
b67ff20
vars for outside symbols used in function bodies shouldn't be duplicated
ychenfo Jun 16, 2025
39cfebc
more fix to sym to strat vars; pipeline
ychenfo Jun 16, 2025
0f46035
fix the checking when handling match rests
ychenfo Jun 16, 2025
477d19b
fix fusion of selection
ychenfo Jun 16, 2025
9854d01
remove old deforestation
ychenfo Jun 17, 2025
a0c314c
new deforest runner
ychenfo Jun 17, 2025
6715757
handle functions nested in functions
ychenfo Jun 17, 2025
c26bfab
slightly improve sym to strat var
ychenfo Jun 18, 2025
168e1e1
more fixes for lambdas and nested functions; update tests
ychenfo Jun 19, 2025
9f85855
make it possible to know whether a symbol is for a function...?
ychenfo Jun 19, 2025
364298d
cleanup
ychenfo Jun 19, 2025
a72843f
wip
ychenfo Jun 19, 2025
555b7d1
Merge remote-tracking branch 'origin/hkmc2' into hkmc2-def-duplicate-new
ychenfo Jun 19, 2025
dc083b4
new todos
ychenfo Jun 19, 2025
1e2509d
wip: noprod for arrays for now
ychenfo Jun 20, 2025
cc9eb03
minor
ychenfo Jun 20, 2025
917aafa
update new todos
ychenfo Jun 21, 2025
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
1 change: 1 addition & 0 deletions hkmc2/shared/src/main/scala/hkmc2/Uid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object Uid:
curUid
def reset = curUid = -1
object Symbol extends Handler[semantics.Symbol]
object StratVar extends Handler[codegen.deforest.StratVarState]

extension [T] (x: Uid[T])
def <=(rhs: Uid[T]) = x <= rhs
Expand Down
12 changes: 12 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ enum Case:

sealed trait TrivialResult extends Result

object Result:
opaque type ResultId = Int
private def ResultId(v: Int): ResultId = v


sealed abstract class Result extends AutoLocated:

protected def children: List[Located] = this match
Expand Down Expand Up @@ -465,6 +470,13 @@ sealed abstract class Result extends AutoLocated:
case DynSelect(qual, fld, arrayIdx) => qual.freeVarsLLIR ++ fld.freeVarsLLIR
case Value.Rcd(args) => args.flatMap(arg => arg.idx.fold(Set.empty)(_.freeVarsLLIR) ++ arg.value.freeVarsLLIR).toSet

def uid =
import Result.*
val uidValue = ResultId(System.identityHashCode(this))
uidValue



// type Local = LocalSymbol
type Local = Symbol

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ object LambdaRewriter:
val lambdaRewriter = new BlockDataTransformer(SymbolSubst()):
override def applyValue(v: Value): Value = v match
case lam: Value.Lam =>
val sym = BlockMemberSymbol("lambda", Nil, nameIsMeaningful = false)
val sym = BlockMemberSymbol(
"lambda",
syntax.Tree.TermDef(syntax.Fun, syntax.Tree.Empty(), N) :: Nil,
nameIsMeaningful = false)
lambdasList ::= (sym -> super.applyLam(lam))
Value.Ref(sym)
case _ => super.applyValue(v)
Expand Down
681 changes: 681 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Analyze.scala

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Deforest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hkmc2
package codegen
package deforest

import semantics.*
import syntax.Tree
import utils.*
import mlscript.utils.*, shorthands.*
import scala.collection.mutable
import Result.ResultId


object Deforest:
class State:
val topLevelFunSymToFunDefn = collection.mutable.Map.empty[BlockMemberSymbol, FunDefn]

def apply(p: Program)(using State, Elaborator.State): Either[Program -> String -> String, String] =
try
val pre = new DeforestPreAnalyzer(p.main)
val col = new DeforestConstraintsCollector(pre)
val ana = new DeforestConstrainSolver(col)
val rwp = new DeforestRewritePrepare(ana)
val rw = new DeforestRewriter(rwp)
val detail = col.constraints
.map: (p, c) =>
(s"$p --> $c")
.mkString("\n")
val summary = rwp.ctorIdToFinalDest
.map: (ctorid, dest) =>
pre.getResult(ctorid._1).toString() +
"@" +
pre.getStableResultId(ctorid._1) +
"@" +
ctorid._2.makeSuffix(pre) +
" --> " +
dest.toString(pre)
.mkString("\n")
val deforestRes = rw()
L:
Program(p.imports, deforestRes) -> summary -> detail
catch
case NotDeforestableException(msg) => R(msg)
Loading
Loading