CS252r: Advanced Functional Language Compilation
Fall 2012: Maxwell Dworkin 323, Mon-Wed-Fri 3-4pm
Homework 3: Due Friday Oct 19
There are two tasks for this homework:
- Translate closure-converted CPS code into LLVM code (ignoring GC issues)
- Write a one page proposal regarding what advanced transformation, optimization, or analysis you wish to pursue towards a final project.
Translation to LLVM
You will want to download and install LLVM if you haven't already. I'm working with the current release version (3.1) of the tools. You can find the documentation here.
I have provided some preliminary definitions for LLVM abstract syntax in the repository, though undoubtedly, there are bugs and problems with it. Feel free to fix such bugs and push them back.
You can assume that the input to your code generator is a version of CPS where all of the function definitions have been lifted to the top-level. Your code generator should produce an LLVM function for each of these functions, plus a function coq_main that kicks off evaluation.
For each function, you should have two arguments corresponding to the allocation pointer and the limit pointer. These extra arguments will be passed to each function or continuation when it is invoked. You can assume that coq_main will be passed appropriate initial values for these two variables. To allocate something of size n bytes, you simply need to make sure that the difference between the limit and alloc pointers is at least n and then increment the allocptr by n.
For now, we will ignore the issue of garbage collection and simply die when we run out of space. You can die by simply calling the exit function.
Throughout the translation, you'll have to be careful to convert to/from a universal type (typically i8* or char* in C-speak) as discussed in class.
For the rest of the class, we're going to break up into groups and work on more advanced optimizations, analyses, or transformations. There are many potential topics to work on, and I suggest that you team up with at least one other person (but larger teams are encouraged.)
In no particular order, here are some things that you might consider:
- Implement a fancier garbage collector (e.g., generational)
- Modify the compiler to stack-allocate continuations instead of heap-allocating them. This will also require work interfacing to the GC.
- Implement a call-by-need (i.e., lazy) version of the compiler and do strictness analysis to try to get rid of the overhead of the laziness.
- Implement aggressive, inter-procedural dead value analysis. Ideally, this should eliminate fields of data structures that are never used.
- Implement lightweight and/or selective closure conversion.
- Implement a generic flow analysis and use it to improve the basic optimizations.
- Break functions into strongly connected components.
- Implement better environment sharing for closures without compromising space safety.
- Implement uncurrying and/or unboxing for functions.
- Implement transformations for key data types (e.g., nat, ascii, string, etc.) so that they are efficiently implemented in the machine.
- Implement deforestation or other forms of shortcut fusion.
- Implement a more sophisticated translation to LLVM that integrates mutually recursive functions into a single function of loops.
- Implement optimiztions for monadic computations, particularly for state, so that we avoid the functional overheads.
- Augment the language with support for (native) computational effects and extend the basic optimizations to handle them.
Moving forward, you will be held responsible for finding the key background material for your topic, presenting the major ideas in class, and coordinating with the other groups.