A Nanopass Framework for Commercial Compiler Development Andrew W. - - PowerPoint PPT Presentation

a nanopass framework for commercial compiler development
SMART_READER_LITE
LIVE PREVIEW

A Nanopass Framework for Commercial Compiler Development Andrew W. - - PowerPoint PPT Presentation

A Nanopass Framework for Commercial Compiler Development Andrew W. Keep and R. Kent Dybvig Monday, October 7, 13 Traditional Compilers Composed of a few large passes Each pass performs several tasks Difficult to maintain


slide-1
SLIDE 1

A Nanopass Framework for Commercial Compiler Development

Andrew W. Keep and R. Kent Dybvig

Monday, October 7, 13

slide-2
SLIDE 2

Traditional Compilers

  • Composed of a few large passes
  • Each pass performs several tasks
  • Difficult to maintain
  • Difficult to add new optimizations

Monday, October 7, 13

slide-3
SLIDE 3

Nanopass Compilers

  • Composed of many small passes
  • Each pass performs a single task
  • Easier to maintain
  • Easier to add new optimizations

Monday, October 7, 13

slide-4
SLIDE 4

Potential Dangers

  • Large languages can lead to large passes

(lots of boilerplate)

  • Tracking language changes can be difficult
  • The nanopass framework helps address this

Monday, October 7, 13

slide-5
SLIDE 5

Nanopass Framework

  • Embedded DSL for writing compilers
  • Languages are formally defined
  • Passes operate over languages
  • Sarkar, Waddell, and Dybvig ICFP ’04

Monday, October 7, 13

slide-6
SLIDE 6

Defining a language

(define-language Lsrc (terminals (datum (d)) (primitive (pr)) (uvar (x))) (Expr (e body) x (quote d) (if e0 e1 e2) (begin e* ... e) (lambda (x* ...) body) (let ([x* e*] ...) body) (letrec ([x* e*] ...) body) (set! x e) (pr e* ...) (call e e* ...) => (e e* ...)))

Monday, October 7, 13

slide-7
SLIDE 7

Extending a language

(define-language L1 (extends Lsrc) (terminals (- (datum (d))) (+ (constant (c)))) (Expr (e body) (- (quote d)) (+ (quote c))))

Monday, October 7, 13

slide-8
SLIDE 8

Defining a pass

(define-pass convert-complex-datum : Lsrc (e) -> L1 () (definitions (define const-x* '()) (define const-e* '()) (define datum->expr ---)) (Expr : Expr (e) -> Expr () [,x x] [(quote ,d) (if (constant? d) `(quote ,d) (let ([t (unique-name 't)]) (set! const-x* (cons t const-x*)) (set! const-e* (cons (datum->expr d) const-e*)) t))] [(if ,e0 ,e1 ,e2) `(if ,(Expr e0) ,(Expr e1) ,(Expr e2))] [(begin ,e* ... ,e) `(begin ,(map Expr e*) ... ,(Expr e))] [(lambda (,x* ...) ,body) `(lambda (,x* ...) ,(Expr body))] [(let ([,x* ,e*] ...) ,body) `(let ([,x* ,(map Expr e*)] ...) ,(Expr body))] [(letrec ([,x* ,e*] ...) ,body) `(letrec ([,x* ,(map Expr e*)] ...) ,(Expr body))] [(set! ,x ,e) `(set! ,x ,(Expr e))] [(,pr ,e* ...) `(,pr ,(map Expr e*) ...)] [(call ,e ,e* ...) `(call ,(Expr e) ,(map Expr e*) ...)] [else (errorf who "invalid Expr form ~s" e)]) (let ([e (Expr e)]) (if (null? const-x*) e `(let ([,const-x* ,const-e*] ...) ,e))))

Monday, October 7, 13

slide-9
SLIDE 9

Defining a pass

(define-pass convert-complex-datum : Lsrc (e) -> L1 () (definitions (define const-x* '()) (define const-e* '()) (define datum->expr ---)) (Expr : Expr (e) -> Expr () [(quote ,d) (guard (not (constant? d))) (let ([t (unique-name 't)]) (set! const-x* (cons t const-x*)) (set! const-e* (cons (datum->expr d) const-e*)) t))]) (let ([e (Expr e)]) (if (null? const-x*) e `(let ([,const-x* ,const-e*] ...) ,e))))

Monday, October 7, 13

slide-10
SLIDE 10

Defining a pass

(define-pass convert-complex-datum : Lsrc (e) -> L1 () (definitions (define const-x* '()) (define const-e* '()) (define datum->expr ---)) (Expr : Expr (e) -> Expr () [(quote ,d) (guard (not (constant? d))) (let ([t (unique-name 't)]) (set! const-x* (cons t const-x*)) (set! const-e* (cons (datum->expr d) const-e*)) t))]) (let ([e (Expr e)]) (if (null? const-x*) e `(let ([,const-x* ,const-e*] ...) ,e))))

Monday, October 7, 13

slide-11
SLIDE 11

Is the nanopass methodology suitable for use in commercial compiler development?

Question

Monday, October 7, 13

slide-12
SLIDE 12

Approach

  • Replace the compiler for Chez Scheme
  • Support identical feature set
  • Improve the nanopass framework

(as needed)

  • Graph coloring register allocator
  • Improve and add optimizations

Monday, October 7, 13

slide-13
SLIDE 13

Comparing compilers

Front end: Back end:

Monday, October 7, 13

slide-14
SLIDE 14

Comparing compilers

Back end: Front end:

Monday, October 7, 13

slide-15
SLIDE 15

Evaluation

  • Three sets of benchmarks
  • R6RS Benchmarks
  • Chez Scheme benchmarks
  • Source optimizer benchmarks

Monday, October 7, 13

slide-16
SLIDE 16

Compile-time factors

Optimize level x86 x86_64 2 1.38 1.44 3 1.26 1.38

Average factor of compile-time slow down

Monday, October 7, 13

slide-17
SLIDE 17

Run-time Performance

Optimize level x86 x86_64 2 0.72 0.76 3 0.76 0.83

Average improvement of benchmark run times

Monday, October 7, 13

slide-18
SLIDE 18

Improved Framework

  • Error checking
  • Robustness
  • Functionality
  • Performance

Monday, October 7, 13

slide-19
SLIDE 19

Conclusion

  • Commercial nanopass compiler
  • Supports identical feature set
  • Modest compile-time penalty
  • 17%-28% average run-time speed-up
  • Has proved easier to extend

Monday, October 7, 13

slide-20
SLIDE 20

Nanopass Framework

  • github.com/akeep/nanopass-framework
  • Projects using the nanopass framework
  • Chez Scheme
  • Harlan - github.com/eholk/harlan

Monday, October 7, 13