From 16dfb7b5f594c86ed83718dedb2bba12f349d5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20G=C3=A9lineau?= Date: Mon, 18 Dec 2023 23:35:01 -0500 Subject: [PATCH] lang: Add more test cases related to future graph optimization We are planning to implement an optimization in which some function calls are compiled to a single static graph rather than to a CallFunc which dynamically creates a sub-graph at runtime. These test cases exercise corner cases for which it would be theoretically possible to use a static graph, but which we might not be able to optimize. --- .../TestAstFunc2/constant-sub-graph.txtar | 17 +++++++++++++++++ .../TestAstFunc2/speculative-call1.txtar | 11 +++++++++++ .../TestAstFunc2/speculative-call2.txtar | 12 ++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 lang/interpret_test/TestAstFunc2/constant-sub-graph.txtar create mode 100644 lang/interpret_test/TestAstFunc2/speculative-call1.txtar create mode 100644 lang/interpret_test/TestAstFunc2/speculative-call2.txtar diff --git a/lang/interpret_test/TestAstFunc2/constant-sub-graph.txtar b/lang/interpret_test/TestAstFunc2/constant-sub-graph.txtar new file mode 100644 index 00000000..6358b392 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/constant-sub-graph.txtar @@ -0,0 +1,17 @@ +-- main.mcl -- +import "os" +$apply1 = func($f, $x) { + $f($x) +} +$apply2 = func($f, $x) { + $f($f($x)) +} +$apply = if (os.system("echo 0; echo 1") == "0") {$apply1} else {$apply2} +$id = func($y) { $y } + +# since $apply changes over time, this call needs a dynamic sub-graph. In +# theory, the $f calls above do not need a sub-graph, but does our optimization +# support this corner case yet? +test $apply($id, "foo") {} +-- OUTPUT -- +Vertex: test[foo] diff --git a/lang/interpret_test/TestAstFunc2/speculative-call1.txtar b/lang/interpret_test/TestAstFunc2/speculative-call1.txtar new file mode 100644 index 00000000..67136ca0 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/speculative-call1.txtar @@ -0,0 +1,11 @@ +-- main.mcl -- +$f1 = func($x) { $x } +$f2 = func($x) { $x + $x } +$id = func($y) { $y } +$f = if ($id(true)) {$f1} else {$f2} + +# $f is always $f1, so in theory, this call does not need a sub-graph. +# But does our optimization support this corner case yet? +test $f("foo") {} +-- OUTPUT -- +Vertex: test[foo] diff --git a/lang/interpret_test/TestAstFunc2/speculative-call2.txtar b/lang/interpret_test/TestAstFunc2/speculative-call2.txtar new file mode 100644 index 00000000..75b2b745 --- /dev/null +++ b/lang/interpret_test/TestAstFunc2/speculative-call2.txtar @@ -0,0 +1,12 @@ +-- main.mcl -- +$x = "not this x" +$add = func($x) { + func($y) {$x + $y} +} +$addfoo = $add("foo") + +# making sure that $x correctly refers to the lambda parameter rather than the +# top-level "not this x", even in the Value() codepath. +test $addfoo("bar") {} +-- OUTPUT -- +Vertex: test[foobar]