31
31
package scalacl .impl
32
32
import scalaxy .components .FlatCode
33
33
import scalaxy .components .FlatCodes ._
34
+ import scalaxy .components .StreamTransformers
34
35
35
36
import scalacl .CLArray
36
37
import scalacl .CLFilteredArray
37
38
38
39
import scala .reflect .api .Universe
39
40
import scala .util .matching .Regex
40
41
41
- trait CodeConversion extends OpenCLConverter with UniverseCasts {
42
- val global : Universe
43
- def fresh (s : String ): String
42
+ trait CodeConversion
43
+ extends OpenCLConverter
44
+ with StreamTransformers
45
+ with CodeConversionResults
46
+ with UniverseCasts {
44
47
48
+ val global : Universe
45
49
import global ._
46
50
import definitions ._
47
51
48
- case class ParamDesc (
49
- symbol : Symbol ,
50
- tpe : Type ,
51
- mode : ParamKind ,
52
- usage : UsageKind ,
53
- implicitIndexDimension : Option [Int ] = None ,
54
- rangeOffset : Option [Symbol ] = None ,
55
- rangeStep : Option [Symbol ] = None ) {
56
- assert((mode == ParamKind .ImplicitArrayElement || mode == ParamKind .RangeIndex ) == (implicitIndexDimension != None ))
57
- def isArray =
58
- mode == ParamKind .ImplicitArrayElement || mode == ParamKind .Normal && tpe <:< typeOf[CLArray [_]]
59
- }
60
-
52
+ def fresh (s : String ): String
53
+ def cleanTypeCheck (tree : Tree ): Tree
61
54
/*
62
55
ParamDesc(i, ParamKindRangeIndex, Some(0))
63
56
-> get_global_id(0)
@@ -68,13 +61,45 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
68
61
ParamDesc(x, ParamKindRead, Some(0))
69
62
-> x[get_global_id(0)]
70
63
*/
71
- case class CodeConversionResult (
72
- code : String ,
73
- capturedInputs : Seq [ParamDesc ],
74
- capturedOutputs : Seq [ParamDesc ],
75
- capturedConstants : Seq [ParamDesc ])
64
+ def transformStreams (tree : Tree , paramDescs : Seq [ParamDesc ]): (Tree , Seq [ParamDesc ]) = {
65
+ val typableBlock =
66
+ Block (
67
+ for (param <- paramDescs.toList) yield {
68
+ ValDef (
69
+ if (param.output)
70
+ Modifiers (Flag .MUTABLE )
71
+ else
72
+ NoMods ,
73
+ param.name,
74
+ TypeTree (param.tpe),
75
+ Literal (Constant (defaultValue(param.tpe)))
76
+ )
77
+ },
78
+ tree.duplicate)
79
+
80
+ println(s """
81
+ Generating CL function for:
82
+ tree = $tree
83
+ paramDescs = $paramDescs
84
+ typableBlock = $typableBlock
85
+ """ )
86
+
87
+ val Block (valdefs, transformedBody) =
88
+ newStreamTransformer(false ).transform(cleanTypeCheck(typableBlock))
89
+
90
+ println(s """
91
+ transformedBody = $transformedBody
92
+ """ )
93
+
94
+ (
95
+ transformedBody,
96
+ for ((paramDesc, vd) <- paramDescs.zip(valdefs)) yield paramDesc.copy(symbol = vd.symbol)
97
+ )
98
+ }
99
+
100
+ def convertCode (tree : Tree , initialParamDescs : Seq [ParamDesc ]): CodeConversionResult = {
101
+ val (code, explicitParamDescs) = transformStreams(tree, initialParamDescs)
76
102
77
- def convertCode (code : Tree , explicitParamDescs : Seq [ParamDesc ]): CodeConversionResult = {
78
103
val externalSymbols =
79
104
getExternalSymbols(
80
105
code,
@@ -88,6 +113,7 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
88
113
ParamDesc (
89
114
sym.asInstanceOf [Symbol ],
90
115
tpe.asInstanceOf [Type ],
116
+ output = false ,
91
117
mode = ParamKind .Normal ,
92
118
usage = usage)
93
119
}
@@ -102,11 +128,13 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
102
128
ParamDesc (
103
129
symbol = d.rangeOffset.get,
104
130
tpe = IntTpe ,
131
+ output = false ,
105
132
mode = ParamKind .Normal ,
106
133
usage = UsageKind .Input ),
107
134
ParamDesc (
108
135
symbol = d.rangeStep.get,
109
136
tpe = IntTpe ,
137
+ output = false ,
110
138
mode = ParamKind .Normal ,
111
139
usage = UsageKind .Input )
112
140
)
@@ -137,10 +165,10 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
137
165
val r = (" \\ b(" + Regex .quoteReplacement(paramDesc.symbol.name.toString) + " )\\ b" ).r
138
166
// TODO handle composite types, with replacements of all possible fibers (x._1, x._2._1, x._2._2)
139
167
paramDesc match {
140
- case ParamDesc (_, _, ParamKind .ImplicitArrayElement , _, Some (i), None , None ) =>
168
+ case ParamDesc (_, _, _, ParamKind .ImplicitArrayElement , _, Some (i), None , None ) =>
141
169
(s : String ) =>
142
170
r.replaceAllIn(s, " $1" + Regex .quoteReplacement(" [" + globalIDValNames(i) + " ]" ))
143
- case ParamDesc (_, _, ParamKind .RangeIndex , _, Some (i), Some (from), Some (by)) =>
171
+ case ParamDesc (_, _, _, ParamKind .RangeIndex , _, Some (i), Some (from), Some (by)) =>
144
172
(s : String ) =>
145
173
r.replaceAllIn(s, Regex .quoteReplacement(" (" + from.name + " + " + globalIDValNames(i) + " * " + by.name + " )" ))
146
174
case _ =>
0 commit comments