Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating structs #3

Open
emilaxelsson opened this issue Oct 19, 2012 · 2 comments
Open

Avoid creating structs #3

emilaxelsson opened this issue Oct 19, 2012 · 2 comments

Comments

@emilaxelsson
Copy link
Member

In many cases it's possible to compile Feldspar tuples into a set of variables instead of creating a struct. For example:

f a b = c+d
  where
    (c,d) = (a==b) ? ((a,2), (b,3))

There's no need to create a struct in this case.

See this proposal.

See also the issue for feldspar-language.

@pjonsson
Copy link
Member

pjonsson commented Nov 1, 2012

Would you consider a patch that pushes the select "down" to the leaves of the tree to enable optimizations at the Feldspar-language level instead? Something along these lines:

+++ b/src/Feldspar/Core/Constructs/Tuple.hs
@@ -42,6 +42,7 @@ import Data.Tuple.Select

 import Language.Syntactic
 import Language.Syntactic.Constructs.Binding
+import Language.Syntactic.Constructs.Condition
 import Language.Syntactic.Constructs.Tuple

 import Feldspar.Core.Types
@@ -269,8 +270,10 @@ instance

 instance
     ( (Select :|| Type) :<: dom
+    , (Condition :|| Type) :<: dom
     , (Tuple  :|| Type) :<: dom
     , Optimize dom dom
+    , Optimize (Condition :|| Type) dom
     ) =>
       Optimize (Select :|| Type) dom
   where
@@ -281,6 +284,10 @@ instance
         | ((prjF -> Just (C' Tup5)) :$ a :$ _ :$ _ :$ _ :$ _) <- t           = return a
         | ((prjF -> Just (C' Tup6)) :$ a :$ _ :$ _ :$ _ :$ _ :$ _) <- t      = return a
         | ((prjF -> Just (C' Tup7)) :$ a :$ _ :$ _ :$ _ :$ _ :$ _ :$ _) <- t = return a
+        | ((prjF -> Just (C' Condition)) :$ c :$ t1 :$ f1) <- t = do
+            t' <- constructFeat (c' Sel1) (t1 :* Nil)
+            f' <- constructFeat (c' Sel1) (f1 :* Nil)
+            constructFeat (c' Condition) (c :* t' :* f' :* Nil)

     constructFeatOpt (C' Sel2) (t :* Nil)
         | ((prjF -> Just (C' Tup2)) :$ _ :$ a) <- t                          = return a

Test expression:

cexp10 :: Data WordN -> Data WordN -> Data WordN
cexp10 a b = c
  where
    (c,d) = (a==b) ? ((a,2::Data WordN), (b,3::Data WordN))

Result:

(\var0 -> (\var1 -> (condition (var0 == var1) var0 var1)))

@emwap
Copy link
Member

emwap commented Nov 2, 2012

Yes, please make a pull request that we can test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants