View on GitHub

Lightweight Modular Staging

Program Generation and Embedded Compilers in Scala

Lightweight Modular Staging (LMS) is a runtime code generation approach. The framework provides a library of core components for building high performance code generators and embedded compilers in Scala.

Two closely related projects are Delite, a framework for domain specific languages (DSLs) that generates parallel code for heterogeneous devices, and Scala Virtualized, a set of minimal extensions to the Scala compiler that make embedding DSLs more seamless.

News Flash

We’ve been holding tutorials on LMS and Delite at recent conferences:

Slides and additional materials are available from the links above.

Abstraction Without Regret

Turn nice high-level programs into fast low-level code. Strip abstraction overhead from generic programs. Add domain-specific optimizations.

class Vector[T:Numeric:Manifest](val data: Rep[Array[T]]) {
  def foreach(f: Rep[T] => Rep[Unit]): Rep[Unit] =
    for (i <- 0 until data.length) f(data(i))
  def sumIf(f: Rep[T] => Rep[Unit]) = { 
    var n = zero[T]; foreach(x => if (f(x) n += x)); n }

val v: Vector[Double] = ...
println(v.sumIf(_ > 0))
var n: Double = 0.0
var i: Int = 0
val end = data.length
while (i < end) {
  val x = data(i)
  val c = x > 0
  if (c) n += x
Running the snippet above will generate the program on the left: All functions are inlined, all generics specialized, all type class instances removed. All of this is guaranteed by the type system: Expressions of type `Rep\[T\]` become part of the generated program, everything else is evaluated when the original snippet is run.

Actually this is only half of the story: The output on the left is not produced directly, but going through an extensible intermediate representation (IR) that can be further transformed and optimized. Read more

Run Scala Anywhere

Generate code for heterogenenous targets. Compile Scala to JavaScript, SQL, CUDA, C

Getting Started

Clone the GitHub repo. It contains an extensive test suite with examples.

git clone git://

Be sure to check out the docs below!

Background / Documentation

Overview presentations:

Talks/videos from ScalaDays:

Tutorial write-ups:

Comprehensive, but many many pages:

Training courses, full-day tutorials:

Conference and Journal publications:

Related projects: