Skip to content

Commit c741e3b

Browse files
committed
Moved conversion output classes to CodeConversionResults trait
1 parent 02ecc5b commit c741e3b

File tree

4 files changed

+147
-119
lines changed

4 files changed

+147
-119
lines changed

src/main/scala/scalacl/impl/CodeConversion.scala

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,26 @@
3131
package scalacl.impl
3232
import scalaxy.components.FlatCode
3333
import scalaxy.components.FlatCodes._
34+
import scalaxy.components.StreamTransformers
3435

3536
import scalacl.CLArray
3637
import scalacl.CLFilteredArray
3738

3839
import scala.reflect.api.Universe
3940
import scala.util.matching.Regex
4041

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 {
4447

48+
val global: Universe
4549
import global._
4650
import definitions._
4751

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
6154
/*
6255
ParamDesc(i, ParamKindRangeIndex, Some(0))
6356
-> get_global_id(0)
@@ -68,13 +61,45 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
6861
ParamDesc(x, ParamKindRead, Some(0))
6962
-> x[get_global_id(0)]
7063
*/
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)
76102

77-
def convertCode(code: Tree, explicitParamDescs: Seq[ParamDesc]): CodeConversionResult = {
78103
val externalSymbols =
79104
getExternalSymbols(
80105
code,
@@ -88,6 +113,7 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
88113
ParamDesc(
89114
sym.asInstanceOf[Symbol],
90115
tpe.asInstanceOf[Type],
116+
output = false,
91117
mode = ParamKind.Normal,
92118
usage = usage)
93119
}
@@ -102,11 +128,13 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
102128
ParamDesc(
103129
symbol = d.rangeOffset.get,
104130
tpe = IntTpe,
131+
output = false,
105132
mode = ParamKind.Normal,
106133
usage = UsageKind.Input),
107134
ParamDesc(
108135
symbol = d.rangeStep.get,
109136
tpe = IntTpe,
137+
output = false,
110138
mode = ParamKind.Normal,
111139
usage = UsageKind.Input)
112140
)
@@ -137,10 +165,10 @@ trait CodeConversion extends OpenCLConverter with UniverseCasts {
137165
val r = ("\\b(" + Regex.quoteReplacement(paramDesc.symbol.name.toString) + ")\\b").r
138166
// TODO handle composite types, with replacements of all possible fibers (x._1, x._2._1, x._2._2)
139167
paramDesc match {
140-
case ParamDesc(_, _, ParamKind.ImplicitArrayElement, _, Some(i), None, None) =>
168+
case ParamDesc(_, _, _, ParamKind.ImplicitArrayElement, _, Some(i), None, None) =>
141169
(s: String) =>
142170
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)) =>
144172
(s: String) =>
145173
r.replaceAllIn(s, Regex.quoteReplacement("(" + from.name + " + " + globalIDValNames(i) + " * " + by.name + ")"))
146174
case _ =>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* ScalaCL - putting Scala on the GPU with JavaCL / OpenCL
3+
* http://scalacl.googlecode.com/
4+
*
5+
* Copyright (c) 2009-2013, Olivier Chafik (http://ochafik.com/)
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* * Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
* * Neither the name of Olivier Chafik nor the
17+
* names of its contributors may be used to endorse or promote products
18+
* derived from this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY OLIVIER CHAFIK AND CONTRIBUTORS ``AS IS'' AND ANY
21+
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package scalacl.impl
32+
import scalaxy.components.FlatCode
33+
import scalaxy.components.FlatCodes._
34+
35+
import scalacl.CLArray
36+
import scalacl.CLFilteredArray
37+
38+
import scala.reflect.api.Universe
39+
import scala.util.matching.Regex
40+
41+
trait CodeConversionResults {
42+
val global: Universe
43+
def fresh(s: String): String
44+
45+
import global._
46+
import definitions._
47+
48+
sealed trait ParamKind
49+
object ParamKind {
50+
case object ImplicitArrayElement extends ParamKind
51+
case object RangeIndex extends ParamKind
52+
case object Normal extends ParamKind
53+
}
54+
55+
sealed abstract class UsageKind(val isInput: Boolean, val isOutput: Boolean) {
56+
def merge(usage: UsageKind): UsageKind
57+
}
58+
object UsageKind {
59+
case object Input extends UsageKind(true, false) {
60+
override def merge(usage: UsageKind) =
61+
if (usage == Input) Input
62+
else InputOutput
63+
}
64+
case object Output extends UsageKind(false, true) {
65+
override def merge(usage: UsageKind) =
66+
if (usage == Output) Output
67+
else InputOutput
68+
}
69+
case object InputOutput extends UsageKind(true, true) {
70+
override def merge(usage: UsageKind) = this
71+
}
72+
}
73+
74+
case class ParamDesc(
75+
symbol: Symbol,
76+
tpe: Type,
77+
output: Boolean,
78+
mode: ParamKind,
79+
usage: UsageKind,
80+
implicitIndexDimension: Option[Int] = None,
81+
rangeOffset: Option[Symbol] = None,
82+
rangeStep: Option[Symbol] = None) {
83+
assert((mode == ParamKind.ImplicitArrayElement || mode == ParamKind.RangeIndex) == (implicitIndexDimension != None))
84+
def isArray =
85+
mode == ParamKind.ImplicitArrayElement || mode == ParamKind.Normal && tpe <:< typeOf[CLArray[_]]
86+
87+
def name = symbol.name.asInstanceOf[TermName]
88+
}
89+
90+
case class CodeConversionResult(
91+
code: String,
92+
capturedInputs: Seq[ParamDesc],
93+
capturedOutputs: Seq[ParamDesc],
94+
capturedConstants: Seq[ParamDesc])
95+
}

src/main/scala/scalacl/impl/ParamKind.scala

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/main/scala/scalacl/impl/UsageKind.scala

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)