Language environments for building program generators · 2020-07-06 · DSLs Translation between...
Transcript of Language environments for building program generators · 2020-07-06 · DSLs Translation between...
![Page 1: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/1.jpg)
Department of Computer ScienceETH Zürich, Switzerland
Language environments for building program generatorsGeorg Ofenbeck
http://xkcd.com/1319/
![Page 2: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/2.jpg)
Spiral
DFT(n)
2
![Page 3: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/3.jpg)
Spiral
DFT(n)
2
![Page 4: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/4.jpg)
Spiral
decompose
DFT(n) SPL
2
![Page 5: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/5.jpg)
Spiral
decompose
DFT(n) SPL
2
![Page 6: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/6.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPLtranslate
2
![Page 7: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/7.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPLtranslate
2
![Page 8: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/8.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IRtranslate translate
2
![Page 9: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/9.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IRtranslate translate
2
![Page 10: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/10.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IR C-Codetranslate translate translate
2
![Page 11: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/11.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IR C-Code
… for (int i=0;i < 2;i++) {
y[i] = x[i] + x[i*2+1];y[i*2+1] = x[i] - x[i*2+1];
}for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}…
translate translate translate
2
![Page 12: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/12.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IR C-Code
… for (int i=0;i < 2;i++) {
y[i] = x[i] + x[i*2+1];y[i*2+1] = x[i] - x[i*2+1];
}for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}…
translate translate translate measure
2
![Page 13: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/13.jpg)
Spiral
decompose
DFT(n) SPL Σ-SPL C-IR C-Code
… for (int i=0;i < 2;i++) {
y[i] = x[i] + x[i*2+1];y[i*2+1] = x[i] - x[i*2+1];
}for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}…
translate translate translate measure
search
2
![Page 14: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/14.jpg)
DFT on Intel Multicore
3
![Page 15: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/15.jpg)
DFT on Intel Multicore
5MB vectorized, threaded, general-size, adaptive librarySpiral 3
![Page 16: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/16.jpg)
My Research
decompose
DFT(n) SPL Σ-SPL C-IR C-Codetranslate translate translate measure
search
Can we utilize modern program language features for:
DSLsTranslation between DSLsRewrites on DSLs
Abstraction overLow level transformationsData layouts
4
![Page 17: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/17.jpg)
My Research
decompose
DFT(n) SPL Σ-SPL C-IR C-Codetranslate translate translate measure
search
Can we utilize modern program language features for:
DSLsTranslation between DSLsRewrites on DSLs
Abstraction overLow level transformationsData layouts
4
![Page 18: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/18.jpg)
My Research
decompose
DFT(n) SPL Σ-SPL C-IR C-Codetranslate translate translate measure
search
Can we utilize modern program language features for:
DSLsTranslation between DSLsRewrites on DSLs
Abstraction overLow level transformationsData layouts
4
![Page 19: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/19.jpg)
My Research
decompose
DFT(n) SPL Σ-SPL C-IR C-Codetranslate translate translate measure
search
Can we utilize modern program language features for:
DSLsTranslation between DSLsRewrites on DSLs
Abstraction overLow level transformationsData layouts
Spiral in Scala: Towards the Systematic Construction of Generators for Performance LibrariesGeorg Ofenbeck, Tiark Rompf, Alen Stojanov, Martin Odersky and Markus Püschel (Proc. International Conference on Generative Programming: Concepts & Experiences (GPCE), pp. 125-134, 2013)
4
![Page 20: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/20.jpg)
Content of this LectureDSLs
• Use Case• External vs. Internal (embedded) DSL
• Shallow vs. Deep embedding
Staging• What is Staging?• Staging in Scala• Deep DSL Embedding through Staging
Abstracting Meta Programming• Overview and Motivation• Abstracting Code Style• Abstracting Data Layout
5
![Page 21: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/21.jpg)
DSLsA language that captures a domain (not general purpose)
• More concise
• Quicker to write
• Easier to maintain
• Easier to reason about
6
![Page 22: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/22.jpg)
DSLsImplementation can be roughly categorized into
• External DSLs
• Internal (Embedded) DSLs
• Shallow Embedding
• Deep Embedding
7
![Page 23: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/23.jpg)
External DSLs
8
External DSLs
Most famous
Older DSLs tend to be external DSLs
Require to design syntax, semantics and tools for the language
Reinventing the wheel for your own language (IR optimizations etc.)
Usually only pays off for “large” efforts
![Page 24: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/24.jpg)
Internal (Embedded DSLs)• Use (abuse) the parsing infrastructure of the host language
• Limited by it – easier in modern languages
9
![Page 25: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/25.jpg)
Internal (Embedded DSLs)• Use (abuse) the parsing infrastructure of the host language
• Limited by it – easier in modern languages
9
![Page 26: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/26.jpg)
Internal (Embedded DSLs)• Use (abuse) the parsing infrastructure of the host language
• Limited by it – easier in modern languages
9
![Page 27: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/27.jpg)
Internal (Embedded DSLs)• Use (abuse) the parsing infrastructure of the host language
• Limited by it – easier in modern languages
9
![Page 28: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/28.jpg)
Shallow Embedded DSLs
10
Shallow Embedded DSLs
Syntactic Sugar for a library
Uses (abuses) the syntax of the host language
No external tools requiredeasy to deploywhole program optimization very harddebugging potentially painful (e.g. templates)
![Page 29: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/29.jpg)
Deep Embedded DSLs
11
Deeply Embedded DSLs
Creates an IR instead of calling a libraryexpose IR manipulation to user
Uses (abuses) the syntax of the host language
Infrastructure for handling the IR requiredno broad adaptation yet
Delite
Feldspar
![Page 30: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/30.jpg)
DSLs
12
External DSLs Embedded DSLs
Σ-SPLDelite
Feldspar
![Page 31: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/31.jpg)
Content of this LectureDSLs
• Use Case• External vs. Internal (embedded) DSL
• Shallow vs. Deep embedding
Staging• What is Staging?• Staging in Scala• Deep DSL Embedding through Staging
Abstracting Meta Programming• Overview and Motivation• Abstracting Code Style• Abstracting Data Layout
13
![Page 32: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/32.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
14
![Page 33: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/33.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
14
![Page 34: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/34.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
def specialized_power4(b: Double): Double = { b * b * b * b }
14
![Page 35: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/35.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
def specialized_power4(b: Double): Double = { b * b * b * b }
def specialized_power5(b: Double): Double = { b * b * b * b * b }
14
![Page 36: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/36.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
def specialized_power4(b: Double): Double = { b * b * b * b }
def specialized_power5(b: Double): Double = { b * b * b * b * b }
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
14
![Page 37: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/37.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
def specialized_power4(b: Double): Double = { b * b * b * b }
def specialized_power5(b: Double): Double = { b * b * b * b * b }
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
println(specialized_power_generator(6))
14
![Page 38: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/38.jpg)
Stagingdef generic_power(b: Double, n: Int): Double = {if(n == 0) 1.0 else b * generic_power(b,n -1) }
def specialized_power3(b: Double): Double = { b * b * b }
def specialized_power4(b: Double): Double = { b * b * b * b }
def specialized_power5(b: Double): Double = { b * b * b * b * b }
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
println(specialized_power_generator(6))
def specialized_power6(b: Double):Double ={ b * b * b * b * b * b } 14
![Page 39: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/39.jpg)
Staging
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
15
![Page 40: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/40.jpg)
Staging
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
15
Target Code (next Stage)
![Page 41: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/41.jpg)
Staging
def specialized_power_generator(n: Int): String ={
def recurse(n: Int): String = if(n == 1) " b" else "b * " + recurse(n-1)
"def specialized_power"+n+"(b: Double):Double ={ " + recurse(n) + " }"
}
15
Meta Code (current Stage)
Meta Code (current Stage)
![Page 42: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/42.jpg)
Staging
16
#ifdef OPTIONy[0] *= OPTION
#elsey[0] *= 0
#endif
![Page 43: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/43.jpg)
Staging
16
#ifdef OPTIONy[0] *= OPTION
#elsey[0] *= 0
#endif
Target Code (next Stage)
![Page 44: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/44.jpg)
Staging
16
#ifdef OPTIONy[0] *= OPTION
#elsey[0] *= 0
#endif
Meta Code (current Stage)
![Page 45: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/45.jpg)
Compiler Assisted Stagingcase class Rep[T](i: T){
def * (lhs: Rep[T]): Rep[T] = createGraphNode(this,lhs)
def createGraphNode(x: Rep[T], y: Rep[T]): Rep[T] = ???
}
def staged_power(b: Rep[Double], n: Int): Rep[Double] = {
if(n == 1) b else b * staged_power(b,n -1)
}
17
![Page 46: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/46.jpg)
Compiler Assisted Stagingcase class Rep[T](i: T){
def * (lhs: Rep[T]): Rep[T] = createGraphNode(this,lhs)
def createGraphNode(x: Rep[T], y: Rep[T]): Rep[T] = ???
}
def staged_power(b: Rep[Double], n: Int): Rep[Double] = {
if(n == 1) b else b * staged_power(b,n -1)
}
17
![Page 47: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/47.jpg)
Compiler Assisted Stagingcase class Rep[T](i: T){
def * (lhs: Rep[T]): Rep[T] = createGraphNode(this,lhs)
def createGraphNode(x: Rep[T], y: Rep[T]): Rep[T] = ???
}
def staged_power(b: Rep[Double], n: Int): Rep[Double] = {
if(n == 1) b else b * staged_power(b,n -1)
}
17
Target Code (next Stage)
![Page 48: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/48.jpg)
Compiler Assisted Stagingcase class Rep[T](i: T){
def * (lhs: Rep[T]): Rep[T] = createGraphNode(this,lhs)
def createGraphNode(x: Rep[T], y: Rep[T]): Rep[T] = ???
}
def staged_power(b: Rep[Double], n: Int): Rep[Double] = {
if(n == 1) b else b * staged_power(b,n -1)
}
17
Meta Code (current Stage)
![Page 49: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/49.jpg)
Deep DSL Embedding through Staging
abstract class SPL
case class I(n: Int) extends SPL
case class F_2() extends SPL
case class Const(c: SPL) extends Rep[SPL](c)
implicit def SPLtoRep (i: SPL): Rep[SPL] = Const(i)
def tensor(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
def compose(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
18
![Page 50: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/50.jpg)
Deep DSL Embedding through Staging
abstract class SPL
case class I(n: Int) extends SPL
case class F_2() extends SPL
case class Const(c: SPL) extends Rep[SPL](c)
implicit def SPLtoRep (i: SPL): Rep[SPL] = Const(i)
def tensor(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
def compose(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
18
![Page 51: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/51.jpg)
Deep DSL Embedding through Staging
abstract class SPL
case class I(n: Int) extends SPL
case class F_2() extends SPL
case class Const(c: SPL) extends Rep[SPL](c)
implicit def SPLtoRep (i: SPL): Rep[SPL] = Const(i)
def tensor(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
def compose(x: Rep[SPL], y: Rep[SPL]): Rep[SPL] = x.createGraphNode(x, y)
18
![Page 52: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/52.jpg)
Content of this LectureDSLs
• Use Case• External vs. Internal (embedded) DSL
• Shallow vs. Deep embedding
Staging• What is Staging?• Staging in Scala• Deep DSL Embedding through Staging
Abstracting Meta Programming• Overview and Motivation• Abstracting Code Style• Abstracting Data Layout
19
![Page 53: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/53.jpg)
Σ-SPL
Code Style and Data Layouts
20
![Page 54: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/54.jpg)
Σ-SPL
Code Style and Data Layouts
Looped Code
Unrolled Code
Code with Precomputation
Code Style
20
![Page 55: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/55.jpg)
Data LayoutΣ-SPL
Code Style and Data Layouts
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
Code Style
20
![Page 56: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/56.jpg)
Data LayoutΣ-SPL
Code Style and Data Layouts
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
ASTCode Style
rewrite
20
![Page 57: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/57.jpg)
Data LayoutΣ-SPL
Code Style and Data Layouts
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
rewrite
20
![Page 58: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/58.jpg)
Code Style
Sin
Sin
Sin
Sin
xy
21
![Page 59: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/59.jpg)
Code Style
Sin
Sin
Sin
Sin
xy
Loopedfor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}
21
![Page 60: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/60.jpg)
Code Style
Sin
Sin
Sin
Sin
xy
Loopedfor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}
Unrolledy[0] = x[0] + x[1];y[1] = x[0] - x[1];y[2] = x[2] + x[3];y[3] = x[2] - x[3];
y[0] = y[0] * sin(PI/4*(2));y[1] = y[1] * sin(PI/4*(3));y[2] = y[2] * sin(PI/4*(4));y[3] = y[3] * sin(PI/4*(5)); 21
![Page 61: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/61.jpg)
Code Style
Sin
Sin
Sin
Sin
xy
Loopedfor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} for (int i=0;i < 4;i++) {
y[i] = y[i] * sin(PI/4*(i+2));}
Unrolledy[0] = x[0] + x[1];y[1] = x[0] - x[1];y[2] = x[2] + x[3];y[3] = x[2] - x[3];
y[0] = y[0] * sin(PI/4*(2));y[1] = y[1] * sin(PI/4*(3));y[2] = y[2] * sin(PI/4*(4));y[3] = y[3] * sin(PI/4*(5));
Scalarized and Precomputedt0 = x[0] + x[1];t1 = x[0] - x[1];t2 = x[2] + x[3];t3 = x[2] - x[3];
y[0] = t0 * 1;y[1] = t1 * 0.707;y[2] = t2 * 0;y[3] = t3 * -0.707; 21
![Page 62: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/62.jpg)
Data Layout
Sin
Sin
Sin
Sin
xy
Array of Doublefor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} …
22
![Page 63: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/63.jpg)
Data Layout
Sin
Sin
Sin
Sin
xy
Array of Doublefor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} …
Interleaved Complexfor (int i=0;i < 2;i++) {
y[i*4] = x[i*4] + x[i*4+2]; //realy[i*4+1] = x[i*4+1] + x[i*4+3]; //complex
y[i*4+2] = x[i*4] - x[i*4+2]; //realy[i*4+3] = x[i*4+1] - x[i*4+3]; //complex
} …
22
![Page 64: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/64.jpg)
Data Layout
Sin
Sin
Sin
Sin
xy
Array of Doublefor (int i=0;i < 2;i++) {
y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
} …
Interleaved Complexfor (int i=0;i < 2;i++) {
y[i*4] = x[i*4] + x[i*4+2]; //realy[i*4+1] = x[i*4+1] + x[i*4+3]; //complex
y[i*4+2] = x[i*4] - x[i*4+2]; //realy[i*4+3] = x[i*4+1] - x[i*4+3]; //complex
} …
Split Complexfor (int i=0;i < 2;i++) {
ry[i*2] = rx[i*2] + rx[i*2+1];ry[i*2+1] = rx[i*2] - rx[i*2+1];
iy[i*2] = ix[i*2] + ix[i*2+1];iy[i*2+1] = ix[i*2] - ix[i*2+1];
} …
22
![Page 65: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/65.jpg)
Code Style and Data LayoutsData LayoutΣ-SPL
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
rewrite
23
![Page 66: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/66.jpg)
Code Style and Data LayoutsData LayoutΣ-SPL
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
How to build this?Option 1: “Do it yourself”Option 2: C++ Meta programmingOption 3: Staging in Scala…
rewrite
23
![Page 67: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/67.jpg)
Data LayoutΣ-SPL
Option 1: “Do it yourself”
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
rewritesrewrites
rewrite
24
![Page 68: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/68.jpg)
Data LayoutΣ-SPL
Option 1: “Do it yourself”
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
rewritesrewrites
rewrite
24
Rewrite Engine from ScratchLarge Implementation and Maintenance Effort Little Reuse between “Backends”
![Page 69: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/69.jpg)
Data LayoutΣ-SPL
Option 2: C++ Meta Programming
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
C ++ Meta Program
Template Instantiation
25
![Page 70: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/70.jpg)
C++ Template Meta Programmingtemplate <int T>struct Fibonacci{ enum { value = (Fibonacci<T - 1>::value + Fibonacci<T - 2>::value) }; };
template <>struct Fibonacci<0>{ enum { value = 0 }; };
template <>struct Fibonacci<1>{ enum { value = 1 }; };
template <>struct Fibonacci<2>{ enum { value = 1 }; };
int main(){
int x = Fibonacci<45>::value;cout << x << endl;
}
int main(){
int x = 701408733;cout << x << endl;
}
Template Instantiation
26
![Page 71: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/71.jpg)
C++ Template Meta Programmingauto body1 = [&](const int& i) {y[i*2] = x[i*2] + x[i*2+1];y[i*2+1] = x[i*2] - x[i*2+1];
};for(int i = 0; i < 2; i+= UnrollFact) {unroller( body1, i, uint_<UnrollFact - 1 >());
}auto body2 = [&](const int& i) {#ifdef PRECOMPUTEy[0] *= sin_ct(M_PI/4*(i+2));
#elsey[0] *= sin(M_PI/4*(i+2));
#endif};for(int i = 0; i < 4; i+= UnrollFact) {unroller( body2, i, uint_<UnrollFact - 1>() );
}
Looped Code
C ++ Meta Program
Unrolled CodeCode with Precomputation
<Templates not shown>27
![Page 72: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/72.jpg)
C++ Template Meta ProgrammingSin
Sin
Sin
Sin
xy
28
![Page 73: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/73.jpg)
C++ Template Meta ProgrammingFFT
FFT
FFT
FFT
xy
28
![Page 74: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/74.jpg)
C++ Template Meta Programming
https://github.com/pkeir/ctfft
FFT
FFT
FFT
FFT
xy
28
![Page 75: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/75.jpg)
C++ Template Meta Programming
cfft.cpp:25:60: in constexpr expansion of ‘tupconst::map(F, std::tuple<_Elements ...>) [with F = Comp<cxns::cx<double> (*)(const cxns::cx<double>&), Comp<Init<cxns::cx<double> >, StaticCast<cxns::cx<double>, long unsigned int> > >; Ts = {long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int}; decltype (tupconst::map_helper(f, t, mk_index_range<0ul, (sizeof (Ts ...) - 1)>())) = std::tuple<cxns::cx<double>, cxns::cx<double>, cxns::cx<double>, cxns::cx<double>, cxns::cx<double>, cxns::cx<double>, cxns::cx<double>, cxns::cx<double> >; mk_index_range<0ul, (sizeof (Ts ...) - 1)> = indicesT<long unsigned int, 0ul, 1ul, 2ul, 3ul, 4ul, 5ul, 6ul, 7ul>](tupconst::iota() [with long unsigned int N = 8ul; decltype (tupconst::iota_helper(mk_index_range<0ul, (N - 1)>())) = std::tuple<long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int, long unsigned int>; mk_index_range<0ul, (N - 1)> = indicesT<long unsigned int, 0ul, 1ul, 2ul, 3ul, 4ul, 5ul, 6ul, 7ul>]())’
https://github.com/pkeir/ctfft
FFT
FFT
FFT
FFT
xy
28
![Page 76: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/76.jpg)
Data LayoutΣ-SPL
Option 2: C++ Meta Programming
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
C ++ Meta Program
Template Instantiation
29
![Page 77: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/77.jpg)
Data LayoutΣ-SPL
Option 2: C++ Meta Programming
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
C ++ Meta Program
Template Instantiation
29
C++ Meta ProgrammingOne meta program for all targetsDebugging very difficultErrors only appear when instantiatedBy default no explicit AST
![Page 78: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/78.jpg)
Data LayoutΣ-SPL
Option 3: Staging in Scala
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
Scala Staging Program
Staging decisions rewrite
30
![Page 79: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/79.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
x
y
31
xy
![Page 80: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/80.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
x
y
31
![Page 81: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/81.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
x
y
i = 0
31
![Page 82: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/82.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
x
y
i = 0
31
![Page 83: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/83.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Array.apply(i: Int): T
x
y
i = 0
31
![Page 84: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/84.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Array.apply(i: Int): T
Rep[Double] = Rep[Double] + Rep[Double]
x
y
i = 0
31
![Page 85: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/85.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
x
y
i = 0
31
![Page 86: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/86.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
x
y
i = 0
31
![Page 87: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/87.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
How to add two “Reps”?
x
y
i = 0
31
![Page 88: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/88.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
How to add two “Reps”?Implementation in Lightweight Modular Staging Library
x
y
i = 0
31
![Page 89: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/89.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
How to add two “Reps”?
x
y
i = 0
31
![Page 90: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/90.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
How to add two “Reps”?
s1 s2 s3 s4x
y
i = 0
31
![Page 91: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/91.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}} Rep[Double] = Rep[Double] + Rep[Double]
How to add two “Reps”?
s1
t0 = Plus(s0,s1)
s2 s3 s4x
y
i = 0
31
![Page 92: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/92.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1)
s2 s3 s4x
y
i = 0
31
![Page 93: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/93.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1)
s2 s3 s4x
y
i = 0
31
![Page 94: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/94.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1)
s2 s3 s4x
y
i = 1
31
![Page 95: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/95.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3)
s2 s3 s4x
y
i = 1
31
![Page 96: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/96.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3)
s2 s3 s4x
y
i = 1
31
![Page 97: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/97.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3) t3 = Minus(s2,s3)
s2 s3 s4x
y
i = 1
31
![Page 98: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/98.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3) t3 = Minus(s2,s3)
s2 s3 s4x
y
i = 1
31
![Page 99: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/99.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3) t3 = Minus(s2,s3)
s2 s3 s4
t0 = s0 + s1;t1 = s0 - s1;t2 = s2 + s3;t2 = s2 - s3;
x
y
i = 1
31
![Page 100: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/100.jpg)
Staging Applied to the Running Example
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3) t3 = Minus(s2,s3)
s2 s3 s4
t0 = s0 + s1;t1 = s0 - s1;t2 = s2 + s3;t2 = s2 - s3;
x
y
i = 1 Scalars onlyUnrolled
31
![Page 101: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/101.jpg)
trait FFT { this: Arith with Trig => def omega(k: Int, N: Int): Complex = { val kth = -2.0 * k * math.Pi / N Complex(cos(kth), sin(kth)) }
case class Complex(re: Double, im: Double) {def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)def -(that: Complex) = Complex(this.re - that.re, this.im - that.im) def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
this.re * that.im + this.im * that.re) } def splitEvenOdd[T](xs: List[T]): (List[T], List[T]) = (xs: @unchecked)match { case e :: o :: xt => val (es, os) = splitEvenOdd(xt) ((e :: es), (o :: os))
case Nil => (Nil, Nil) }def mergeEvenOdd[T](even: List[T], odd: List[T]): List[T] = ((even, odd): @unchecked) match {case (Nil, Nil) => Nil case ((e :: es), (o :: os)) => e :: (o :: mergeEvenOdd(es, os)) }
def fft(xs: List[Complex]): List[Complex] = xs match {case (x :: Nil) => xs case _ => val N = xs.lengthval (even0, odd0) = splitEvenOdd(xs) val (even1, odd1) = (fft(even0), fft(odd0))val (even2, odd2) = (even1 zip odd1 zipWithIndex) map {case ((x, y), k) => val z = omega(k, N) * y (x + z, x - z) } unzip; even2 ::: odd2 }}32
![Page 102: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/102.jpg)
trait FFT { this: Arith with Trig => def omega(k: Int, N: Int): Complex = { val kth = -2.0 * k * math.Pi / N Complex(cos(kth), sin(kth)) }
case class Complex(re: Rep[Double], im: Rep[Double]) {def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)def -(that: Complex) = Complex(this.re - that.re, this.im - that.im) def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
this.re * that.im + this.im * that.re) } def splitEvenOdd[T](xs: List[T]): (List[T], List[T]) = (xs: @unchecked)match { case e :: o :: xt => val (es, os) = splitEvenOdd(xt) ((e :: es), (o :: os))
case Nil => (Nil, Nil) }def mergeEvenOdd[T](even: List[T], odd: List[T]): List[T] = ((even, odd): @unchecked) match {case (Nil, Nil) => Nil case ((e :: es), (o :: os)) => e :: (o :: mergeEvenOdd(es, os)) }
def fft(xs: List[Complex]): List[Complex] = xs match {case (x :: Nil) => xs case _ => val N = xs.lengthval (even0, odd0) = splitEvenOdd(xs) val (even1, odd1) = (fft(even0), fft(odd0))val (even2, odd2) = (even1 zip odd1 zipWithIndex) map {case ((x, y), k) => val z = omega(k, N) * y (x + z, x - z) } unzip; even2 ::: odd2 }}33
![Page 103: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/103.jpg)
trait FFT { this: Arith with Trig => def omega(k: Int, N: Int): Complex = { val kth = -2.0 * k * math.Pi / N Complex(cos(kth), sin(kth)) }
case class Complex(re: Rep[Double], im: Rep[Double]) {def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)def -(that: Complex) = Complex(this.re - that.re, this.im - that.im) def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
this.re * that.im + this.im * that.re) } def splitEvenOdd[T](xs: List[T]): (List[T], List[T]) = (xs: @unchecked)match { case e :: o :: xt => val (es, os) = splitEvenOdd(xt) ((e :: es), (o :: os))
case Nil => (Nil, Nil) }def mergeEvenOdd[T](even: List[T], odd: List[T]): List[T] = ((even, odd): @unchecked) match {case (Nil, Nil) => Nil case ((e :: es), (o :: os)) => e :: (o :: mergeEvenOdd(es, os)) }
def fft(xs: List[Complex]): List[Complex] = xs match {case (x :: Nil) => xs case _ => val N = xs.lengthval (even0, odd0) = splitEvenOdd(xs) val (even1, odd1) = (fft(even0), fft(odd0))val (even2, odd2) = (even1 zip odd1 zipWithIndex) map {case ((x, y), k) => val z = omega(k, N) * y (x + z, x - z) } unzip; even2 ::: odd2 }}34
![Page 104: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/104.jpg)
35
![Page 105: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/105.jpg)
mults
addssubs
35
![Page 106: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/106.jpg)
typedef struct {double* input;double* output;
} spiral_t;const double x708[] = { 1.0, 0.9238795325112867, 0.7071067811865476, 0.38268 const double x709[] = { -0.0, 0.3826834323650898, 0.7071067811865476, 0.9238 void staged(spiral_t* x0) {double* x2 = x0->output;double* x1 = x0->input;double x6 = x1[0];double x22 = x1[16];double x38 = x6 + x22;double x14 = x1[8];double x30 = x1[24];double x46 = x14 + x30;double x343 = x38 + x46;double x10 = x1[4];double x26 = x1[20];double x42 = x10 + x26;double x18 = x1[12];double x34 = x1[28];double x50 = x18 + x34;double x344 = x42 + x50;double x345 = x343 + x344;double x8 = x1[2];double x24 = x1[18];double x115 = x8 + x24;double x16 = x1[10];double x32 = x1[26];double x123 = x16 + x32;double x346 = x115 + x123;double x12 = x1[6];double x28 = x1[22];double x119 = x12 + x28;double x20 = x1[14];double x36 = x1[30];double x127 = x20 + x36;double x347 = x119 + x127;double x348 = x346 + x347;double x349 = x345 + x348;x2[0] = x349;double x7 = x1[1];double x23 = x1[17];double x39 = x7 + x23;double x15 = x1[9];double x31 = x1[25];double x47 = x15 + x31;double x76 = x39 + x47;double x11 = x1[5];double x27 = x1[21];double x43 = x11 + x27;double x19 = x1[13];double x35 = x1[29];double x51 = x19 + x35;
mults
addssubs
35
![Page 107: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/107.jpg)
Selective Staging
trait FFT {...case class Complex(re: Double, im: Double)def fft(…)
...
trait FFT {...case class Complex(re: Rep[Double], im: Rep[Double])def fft(…)
...
Scala FFT Function
36
![Page 108: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/108.jpg)
Selective Staging
trait FFT {...case class Complex(re: Double, im: Double)def fft(…)
...
trait FFT {...case class Complex(re: Rep[Double], im: Rep[Double])def fft(…)
...
Scala FFT Function
36
![Page 109: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/109.jpg)
Selective Staging
trait FFT {...case class Complex(re: Double, im: Double)def fft(…)
...
trait FFT {...case class Complex(re: Rep[Double], im: Rep[Double])def fft(…)
...
Scala FFT Function
trait FFT[T] {...case class Complex(re: T, im: T)def fft(…)
...
36
![Page 110: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/110.jpg)
Selective Staging
trait FFT {...case class Complex(re: Double, im: Double)def fft(…)
...
trait FFT {...case class Complex(re: Rep[Double], im: Rep[Double])def fft(…)
...
Scala FFT Function
trait FFT[T] {...case class Complex(re: T, im: T)def fft(…)
...
36
![Page 111: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/111.jpg)
Data LayoutΣ-SPL
Option 3: Staging in Scala
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
Scala Staging Program
Staging decisions rewrite
37
![Page 112: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/112.jpg)
Code Style: Unrolled and Scalarized
def f(x: Array[Rep[Double]], y: Array[Rep[Double]]) ={for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
s1
t0 = Plus(s0,s1) t1 = Minus(s0,s1) t2 = Plus(s2,s3) t3 = Minus(s2,s3)
s2 s3 s4
t0 = s0 + s1;t1 = s0 - s1;t2 = s2 + s3;t2 = s2 - s3;
x
y
Scalars onlyUnrolled
38
![Page 113: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/113.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
39
![Page 114: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/114.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
i = 0
39
![Page 115: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/115.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): T
i = 0
39
![Page 116: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/116.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
i = 0
39
![Page 117: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/117.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
i = 0
Implementation in Lightweight Modular Staging Library
39
![Page 118: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/118.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
i = 0
39
![Page 119: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/119.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
x
i = 0
39
![Page 120: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/120.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
x
t0 = apply(x,0) t1 = apply(x,1)
i = 0
39
![Page 121: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/121.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
t2 = Plus(t0,t1)
x
t0 = apply(x,0) t1 = apply(x,1)
i = 0
39
![Page 122: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/122.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Rep[…].apply(i: Int): THow to apply() on a Rep?
t2 = Plus(t0,t1)
x
t0 = apply(x,0) t1 = apply(x,1)
update(y,0)
y
i = 0
39
![Page 123: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/123.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
t2 = Plus(t0,t1)
x
t0 = apply(x,0) t1 = apply(x,1)
update(y,0)
y
i = 0
39
![Page 124: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/124.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
t2 = Plus(t0,t1) t3 = Minus(t0,t1)
x
t0 = apply(x,0) t1 = apply(x,1)
update(y,0) update(y,1)
y
i = 0
39
![Page 125: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/125.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
t2 = Plus(t0,t1) t3 = Minus(t0,t1) t6 = Plus(t4,t5)
x
t0 = apply(x,0) t1 = apply(x,1) t4 = apply(x,2) t5 = apply(x,3)
update(y,0) update(y,1) update(y,2)
y
i = 1
39
![Page 126: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/126.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
t2 = Plus(t0,t1) t3 = Minus(t0,t1) t6 = Plus(t4,t5) t7 = Minus(t4,t5)
x
t0 = apply(x,0) t1 = apply(x,1) t4 = apply(x,2) t5 = apply(x,3)
update(y,0) update(y,1) update(y,2) update(y,3)
y
i = 1
39
![Page 127: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/127.jpg)
Code Style: Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
t2 = Plus(t0,t1) t3 = Minus(t0,t1) t6 = Plus(t4,t5) t7 = Minus(t4,t5)
t0 = x[0];t1 = x[1];t2 = t0 + t1;y[0] = t2;t3 = t0 - t1;y[1] = t3;t4 = x[0];t5 = x[1];t6 = t4 + x5;y[0] = t6;t7 = t4 – x5;y[3] = t7;x
t0 = apply(x,0) t1 = apply(x,1) t4 = apply(x,2) t5 = apply(x,3)
update(y,0) update(y,1) update(y,2) update(y,3)
y
i = 1
39
![Page 128: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/128.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
40
![Page 129: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/129.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
40
![Page 130: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/130.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
How to apply on a Rep?
40
![Page 131: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/131.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
How to apply on a Rep?
Loop(i, 0, 2)
40
![Page 132: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/132.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
How to apply on a Rep?
Loop(i, 0, 2) i
40
![Page 133: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/133.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Loop(i, 0, 2) i
40
![Page 134: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/134.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Loop(i, 0, 2) i
Loop Body
40
![Page 135: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/135.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
Loop(i, 0, 2) i
Loop Body
40
![Page 136: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/136.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0x
t0 = apply(x,i) t1 = apply(x,i+1)
Loop(i, 0, 2) i
Loop Body
40
![Page 137: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/137.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
t2 = Plus(t0,t1)
x
t0 = apply(x,i) t1 = apply(x,i+1)
Loop(i, 0, 2) i
Loop Body
40
![Page 138: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/138.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
t2 = Plus(t0,t1)
x
t0 = apply(x,i) t1 = apply(x,i+1)
update(y,i,t2)
y
Loop(i, 0, 2) i
Loop Body
40
![Page 139: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/139.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
t2 = Plus(t0,t1) t3 = Minus(t0,t1)
x
t0 = apply(x,i) t1 = apply(x,i+1)
update(y,i,t2) update(y,i+1,t3)
y
Loop(i, 0, 2) i
Loop Body
40
![Page 140: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/140.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
t2 = Plus(t0,t1) t3 = Minus(t0,t1)
x
t0 = apply(x,i) t1 = apply(x,i+1)
update(y,i,t2) update(y,i+1,t3)
y
Loop(i, 0, 2) i
Loop Body
40
![Page 141: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/141.jpg)
Code Style: Loops & Arraysdef f(x: Rep[Array[Double]], y: Rep[Array[Double]]) ={
for (i <- 0 until 2: Rep[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
Block b0
t2 = Plus(t0,t1) t3 = Minus(t0,t1)
for (int i=0;i < 2;i++){
t0 = x[i];t1 = x[i+1];t2 = t0 + t1;y[i] = t2;t3 = t0 - t1;y[i+1] = t3;
}
x
t0 = apply(x,i) t1 = apply(x,i+1)
update(y,i,t2) update(y,i+1,t3)
y
Loop(i, 0, 2) i
Loop Body
40
![Page 142: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/142.jpg)
Abstraction over Code Types
41
![Page 143: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/143.jpg)
Abstraction over Code TypesUnrolled, scalarized
41
![Page 144: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/144.jpg)
Abstraction over Code TypesUnrolled, scalarized Unrolled, Arrays
41
![Page 145: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/145.jpg)
Abstraction over Code TypesUnrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 146: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/146.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 147: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/147.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
f₃(Rep, Rep[Array[Double]], Rep[Array[Double]])
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 148: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/148.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
type NoRep[T] = T
f₃(Rep, Rep[Array[Double]], Rep[Array[Double]])
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 149: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/149.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
type NoRep[T] = T
f₃(Rep, Rep[Array[Double]], Rep[Array[Double]])
f₃(NoRep, Rep[Array[Double]], Rep[Array[Double]])
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 150: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/150.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
type NoRep[T] = T
f₃(Rep, Rep[Array[Double]], Rep[Array[Double]])
f₃(NoRep, NoRep[Array[Rep[Double]]], NoRep[Array[Rep[Double]]])
f₃(NoRep, Rep[Array[Double]], Rep[Array[Double]])
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41
![Page 151: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/151.jpg)
Abstraction over Code Types
def f[L[_],A[_],T](looptype: L, x: A[Array[T]], y: A[Array[T]]) ={for (i <- 0 until 2: L[Range]) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1)= x(i*2) - x(i*2+1)
}}
type NoRep[T] = T
f₃(Rep, Rep[Array[Double]], Rep[Array[Double]])
f₃(NoRep, NoRep[Array[Rep[Double]]], NoRep[Array[Rep[Double]]])
f₃(NoRep, Rep[Array[Double]], Rep[Array[Double]])
Unrolled, scalarized Unrolled, ArraysLooped, Arrays
41*Typeclasses omitted for simplicity
![Page 152: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/152.jpg)
Data LayoutΣ-SPL
Option 3: Staging in Scala
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
Scala Staging Program
Staging decisions rewrite
42
![Page 153: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/153.jpg)
Abstraction over Data Layout
class DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
43
![Page 154: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/154.jpg)
Abstraction over Data Layout
class DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class Array[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
extends
43
![Page 155: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/155.jpg)
Abstraction over Data Layout
class DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class Array[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class List[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
extends extends
43
![Page 156: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/156.jpg)
Abstraction over Data Layout
class DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class Array[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class List[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
extends extends
def f[T](x: DataAbstraction, y: DataAbstraction) {x(0) = y(0)}
43
![Page 157: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/157.jpg)
Abstraction over Data Layout
class DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class Array[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
class List[T] extends DataAbstraction[T] {def apply (i: Int): Tdef update (i: Int, y: T)}
extends extends
val a = Array[Double](3)val b = List[Double](3)
f(a,b)
def f[T](x: DataAbstraction, y: DataAbstraction) {x(0) = y(0)}
43
![Page 158: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/158.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
44
![Page 159: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/159.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
extends
44
![Page 160: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/160.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
extends
extends
44
![Page 161: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/161.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
extends
extends
44
![Page 162: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/162.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
x
extends
extends
44
![Page 163: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/163.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
x
y
extends
extends
44
![Page 164: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/164.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
x
t0 = apply(x,0) t1 = apply(x,1)
y
extends
extends
44
![Page 165: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/165.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
t2 = Plus(t0,t1)
x
t0 = apply(x,0) t1 = apply(x,1)
y
extends
extends
44
![Page 166: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/166.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
val x = new LoopedArrayval y = new UnrolledScalarf(x,y)
t2 = Plus(t0,t1) t3 = Minus(t0,t1) t6 = Plus(t4,t5) t7 = Minus(t4,t5)
x
t0 = apply(x,0) t1 = apply(x,1) t4 = apply(x,2) t5 = apply(x,3)
y
extends
extends
44
![Page 167: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/167.jpg)
Abstraction over Data Layout
class Vector[A[_], T, L]{type looptype = Ldef apply (i: A[Int]): Tdef update (i: A[Int], y: T)}
class LoopedArray extends Vector[Rep,Rep[Double], Rep] {val data: Rep[Array[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
class UnrolledScalar extends Vector [NoRep, Rep[Double], NoRep] {val data: Array[Rep[Double]]def apply (i: Int): Rep[Double] def update (i: Int, y: Rep[Double])}
extends
extends
class StagedTree extends Vector [NoRep, Rep[Double], NoRep] {val data: Rep[Tree[Double]]def apply (i: Rep[Int]): Rep[Double] = data(i)def update (i: Rep[Int], y: Rep[Double]) = { data(i) = y }}
extends
45
![Page 168: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/168.jpg)
Data LayoutΣ-SPL
Option 3: Staging in Scala
Looped Code
Unrolled Code
Code with Precomputation
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
C99 ComplexInterleaved ComplexSplit Complex
AST CodeCode Style
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) …
Staging decisions rewrite
46
![Page 169: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/169.jpg)
BenefitsSingle Decision Point for Data Layout and Code Style
Single Maintenance Point for Translations
val sigmaspl = …val f = sigmaspl.translate
val input = new LoopedArrayInterleavedComplexval output = new LoopedArraySplitComplexval unrolled_code = new UnrolledScalarizedC99Complexval twiddles = TwiddleComputation[Rep]
val ast = f(input,output,unrolled_code,twiddles)
Staging decisions
Instantiation
Creating the Meta Function
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
47
![Page 170: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/170.jpg)
BenefitsSingle Decision Point for Data Layout and Code Style
Single Maintenance Point for Translations
val sigmaspl = …val f = sigmaspl.translate
val input = new LoopedArrayInterleavedComplexval output = new LoopedArraySplitComplexval unrolled_code = new UnrolledScalarizedC99Complexval twiddles = TwiddleComputation[Rep]
val ast = f(input,output,unrolled_code,twiddles)
Staging decisions
Instantiation
Creating the Meta Function
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
47
![Page 171: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/171.jpg)
BenefitsSingle Decision Point for Data Layout and Code Style
Single Maintenance Point for Translations
val sigmaspl = …val f = sigmaspl.translate
val input = new LoopedArrayInterleavedComplexval output = new LoopedArraySplitComplexval unrolled_code = new UnrolledScalarizedC99Complexval twiddles = TwiddleComputation[Rep]
val ast = f(input,output,unrolled_code,twiddles)
Staging decisions
Instantiation
Creating the Meta Function
def f[A1[_], A2[_], L1, L2, T] (x: Vector[A1[_], T, L1], y: Vector[A1[_], T, L2]) ={for (i <- 0 until 2: y.looptype) {y(2*i) = x(i*2) + x(i*2+1)y(2*i+1) = x(i*2) - x(i*2+1)
}}
47
xy
![Page 172: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/172.jpg)
BenefitsCertain Errors Manifest at Compile Time
DFT(47) has ~16.000 Decomposition
48
![Page 173: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/173.jpg)
49
![Page 174: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/174.jpg)
http://spiral.net/
http://spiral.net/software/spiral-scala.html
• Georg Ofenbeck, Tiark Rompf, Alen Stojanov, Martin Odersky and Markus Püschel. Spiral in Scala: Towards the Systematic Construction of Generators for Performance Libraries, GPCE 2013
• Tiark Rompf, Martin Odersky. Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs. Commun. ACM 55(6): 121-130 (2012)
• Markus Püschel, Franz Franchetti and Yevgen VoronenkoSpiralin Encyclopedia of Parallel Computing, Eds. David Padua, Springer 2011
References
50
![Page 175: Language environments for building program generators · 2020-07-06 · DSLs Translation between DSLs Rewrites on DSLs Abstraction over Low level transformations Data layouts Spiral](https://reader033.fdocuments.net/reader033/viewer/2022050315/5f77ce0d383b64461255673b/html5/thumbnails/175.jpg)
51