git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
STFn inlining (#31)

* Initial work on st functions * Adds tests * Fix mkSTFn inlining error Co-authored-by: Mike Solomon <mike120982@gmail.com> Co-authored-by: Nathan Faubion <nathan@awakesecurity.com>

commit910c34f57d90581ad706e0f914817b05022f8b72 authorMike Solomon <mike@meeshkan.com> dateFri 28 Oct 2022, 20:47 (4 years ago) parent869a6ca
7 files changed+106−7
backend-es/test/snapshots-out/Snapshot.UncurriedSTFns01.js+27 backend-es/test/snapshots-out/Snapshot.UncurriedSTFns02.js+12 backend-es/test/snapshots/Snapshot.UncurriedSTFns01.purs+25 backend-es/test/snapshots/Snapshot.UncurriedSTFns02.purs+21 backend-es/test/snapshots/spago.dhall+1 packages.dhall+2−3 src/PureScript/Backend/Optimizer/Semantics/Foreign.purs+18−4
backend-es/test/snapshots-out/Snapshot.UncurriedSTFns01.jsnew file+27
@@ -0,0 +1,27 @@
1+import * as $runtime from "../runtime.js";
2+import * as Control$dMonad$dST$dUncurried from "../Control.Monad.ST.Uncurried/index.js";
3+import {f, g} from "./foreign.js";
4+const test6 = /* #__PURE__ */ (() => {
5+ const $0 = g(1);
6+ return () => {
7+ f($0, 2, 3);
8+ f(g(1), 2, 3);
9+ return f(g(1), 2, 3);
10+ };
11+})();
12+const test5 = /* #__PURE__ */ (() => {
13+ const $0 = g(1);
14+ return () => {
15+ f($0, 2, 3);
16+ return f(g(1), 2, 3);
17+ };
18+})();
19+const test4 = /* #__PURE__ */ Control$dMonad$dST$dUncurried.runSTFn3(f);
20+const test3 = /* #__PURE__ */ Control$dMonad$dST$dUncurried.runSTFn3(f)(/* #__PURE__ */ g(1));
21+const test2 = /* #__PURE__ */ Control$dMonad$dST$dUncurried.runSTFn3(f)(/* #__PURE__ */ g(1))(2);
22+const test1 = /* #__PURE__ */ (() => {
23+ const $0 = g(1);
24+ return () => f($0, 2, 3);
25+})();
26+export {test1, test2, test3, test4, test5, test6};
27+export * from "./foreign.js";
backend-es/test/snapshots-out/Snapshot.UncurriedSTFns02.jsnew file+12
@@ -0,0 +1,12 @@
1+import * as $runtime from "../runtime.js";
2+import * as Control$dMonad$dST$dInternal from "../Control.Monad.ST.Internal/index.js";
3+import * as Data$dShow from "../Data.Show/index.js";
4+import * as Effect$dConsole from "../Effect.Console/index.js";
5+import {random} from "./foreign.js";
6+const test2 = () => {
7+ const n = random();
8+ return Effect$dConsole.log(Data$dShow.showIntImpl(Control$dMonad$dST$dInternal.run(() => n)))();
9+};
10+const test1 = () => 12;
11+export {test1, test2};
12+export * from "./foreign.js";
backend-es/test/snapshots/Snapshot.UncurriedSTFns01.pursnew file+25
@@ -0,0 +1,25 @@
1+module Snapshot.UncurriedSTFns01 where
2+
3+import Prelude
4+
5+import Control.Monad.ST.Uncurried (STFn3, runSTFn3)
6+import Control.Monad.ST.Internal (ST)
7+
8+foreign import f :: forall a b c r. STFn3 a b c r Unit
9+
10+foreign import g :: forall a. a -> a
11+
12+test1 = runSTFn3 f (g 1) 2 3
13+test2 = runSTFn3 f (g 1) 2
14+test3 = runSTFn3 f (g 1)
15+test4 = runSTFn3 f
16+
17+test5 = do
18+ runSTFn3 f (g 1) 2 3
19+ runSTFn3 f (g 1) 2 3
20+
21+test6 = do
22+ runSTFn3 f (g 1) 2 3
23+ runSTFn3 f (g 1) 2 3
24+ runSTFn3 f (g 1) 2 3
25+
backend-es/test/snapshots/Snapshot.UncurriedSTFns02.pursnew file+21
@@ -0,0 +1,21 @@
1+module Snapshot.UncurriedSTFns02 where
2+
3+import Prelude
4+
5+import Control.Monad.ST.Internal (ST, run)
6+import Control.Monad.ST.Uncurried (STFn1, mkSTFn1, runSTFn1)
7+import Effect (Effect)
8+import Effect.Class.Console (logShow)
9+
10+foreign import random :: Effect Int
11+
12+test1 :: forall r. ST r Int
13+test1 = runSTFn1 (mkSTFn1 \m -> pure m) 12
14+
15+test2 :: Effect Unit
16+test2 = do
17+ let
18+ f :: forall r. STFn1 Int r Int
19+ f = mkSTFn1 \n -> pure n
20+ n <- random
21+ logShow (run (runSTFn1 f n))
backend-es/test/snapshots/spago.dhall+1
@@ -19,6 +19,7 @@
1919 , "record"
2020 , "refs"
2121 , "safe-coerce"
22+ , "st"
2223 , "strings"
2324 , "tuples"
2425 , "type-equality"
packages.dhall+2−3
@@ -1,7 +1,6 @@
11let upstream =
2− https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20220822/packages.dhall
3− sha256:908b4ffbfba37a0a4edf806513a555d0dbcdd0cde7abd621f8d018d2e8ecf828
4−
2+ https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221018/packages.dhall
3+ sha256:b1db2e4a17260ace8d17858602f8c56f460982d6e404818d7f6cb9f053324bb1
54in upstream
65 with dodo-printer =
76 { dependencies =
src/PureScript/Backend/Optimizer/Semantics/Foreign.purs+18−4
@@ -92,6 +92,8 @@ coreForeignSemantics = Map.fromFoldable semantics
9292 <> map data_function_uncurried_runFn oneToTen
9393 <> map effect_uncurried_mkEffectFn oneToTen
9494 <> map effect_uncurried_runEffectFn oneToTen
95+ <> map control_monad_st_uncurried_mkSTFn oneToTen
96+ <> map control_monad_st_uncurried_runSTFn oneToTen
9597
9698 oneToTen =
9799 Array.range 1 10
@@ -239,8 +241,8 @@ data_function_uncurried_runFn n = Tuple (qualified "Data.Function.Uncurried" ("r
239241 SemLam Nothing \val ->
240242 goRunFn env (n' - 1) head (Array.snoc tail val)
241243
242−effect_uncurried_mkEffectFn :: Int -> ForeignSemantics
243−effect_uncurried_mkEffectFn n = Tuple (qualified "Effect.Uncurried" ("mkEffectFn" <> show n)) go
244+mkEffectFn :: String -> String -> Int -> ForeignSemantics
245+mkEffectFn mod name n = Tuple (qualified mod (name <> show n)) go
244246 where
245247 go env _ = case _ of
246248 [ ExternApp [ sem ] ] ->
@@ -248,8 +250,14 @@ effect_uncurried_mkEffectFn n = Tuple (qualified "Effect.Uncurried" ("mkEffectFn
248250 _ ->
249251 Nothing
250252
251−effect_uncurried_runEffectFn :: Int -> ForeignSemantics
252−effect_uncurried_runEffectFn n = Tuple (qualified "Effect.Uncurried" ("runEffectFn" <> show n)) go
253+effect_uncurried_mkEffectFn :: Int -> ForeignSemantics
254+effect_uncurried_mkEffectFn = mkEffectFn "Effect.Uncurried" "mkEffectFn"
255+
256+control_monad_st_uncurried_mkSTFn :: Int -> ForeignSemantics
257+control_monad_st_uncurried_mkSTFn = mkEffectFn "Control.Monad.ST.Uncurried" "mkSTFn"
258+
259+runEffectFn :: String -> String -> Int -> ForeignSemantics
260+runEffectFn mod name n = Tuple (qualified mod (name <> show n)) go
253261 where
254262 go env _ = case _ of
255263 [ ExternApp items ]
@@ -266,6 +274,12 @@ effect_uncurried_runEffectFn n = Tuple (qualified "Effect.Uncurried" ("runEffect
266274 SemLet Nothing arg \nextArg ->
267275 goRunEffectFn env (Array.snoc acc nextArg) head args
268276
277+effect_uncurried_runEffectFn :: Int -> ForeignSemantics
278+effect_uncurried_runEffectFn = runEffectFn "Effect.Uncurried" "runEffectFn"
279+
280+control_monad_st_uncurried_runSTFn :: Int -> ForeignSemantics
281+control_monad_st_uncurried_runSTFn = runEffectFn "Control.Monad.ST.Uncurried" "runSTFn"
282+
269283data_heytingAlgebra_boolConj :: ForeignSemantics
270284data_heytingAlgebra_boolConj = Tuple (qualified "Data.HeytingAlgebra" "boolConj") $ primBinaryOperator OpBooleanAnd
271285