Transcript
Page 1: Torben Amtoft Kansas State University

The Semantic Soundness of a Type System for

Interprocedural Register Allocation and Constructor

Registration

Torben Amtoft Kansas State University

joint work with Robert Muller, Boston College

Semantics Seminar, Northeastern University, May 28, 2003

Page 2: Torben Amtoft Kansas State University

Typed Compilation of ML-like Languages

let val a : int = 3 val b : int = 4 fun f(x : int) = x + a * b fun g(x : int) = x + bin (if test then f else g) @ 343end

Page 3: Torben Amtoft Kansas State University

Defunctionalization let type t1 = {a : int, b : int} type t2 = {b : int} type t3 = (t1 + t2) fun apply1(h : t3, x : int) : t3 * int int = case h of r : t1 => let val a : int = #a(r) val b : int = #b(r) val c : int = a * b val d : int = x + c in d end r : t2 => let val b : int = #b(r) val c : int = x + b in c end

. . .

Page 4: Torben Amtoft Kansas State University

Defunctionalization

…val a : int = 3val b : int = 4val c : t1 = {a = a, b = b}val d : t2 = {b = b}val e: int = 343val h : t3 = if test then inj(1,c)t3 else inj(2,d)t3

in apply1(h, e)end

Page 5: Torben Amtoft Kansas State University

Types with Storage Annotations

let type t1 = {a : int H, b : int H} r1 type t2 = {a : int H, b : int H} H type t3 = {b : int H} r2 type t4 = {b : int H} H type t5 = (t2 +H t4) r3 fun apply1(h : t5, x : int r4) : int r5 = case h of r : t1 => let val a : int r6 = #a(r) val b : int r7 = #b(r) val c : int r8 = a * b val d : int r5 = x + c in d end r : t3 => let val b : int r9 = #b(r) val c : int r5 = x + b in c end

. . .

Page 6: Torben Amtoft Kansas State University

Types with Storage Annotations

…val a : int r1 = 3val b : int r2 = 4val c : t1 = {a = a, b = b}val d : t3 = {b = b}val e: int r4 = 343val h : t5 = if test then inj(1,c)t5 else inj(2,d)t5

in apply1(h, e)end

Page 7: Torben Amtoft Kansas State University

type t = {a : int H, b : {c : int H, d : int H} H} r1val v : t = {a = 3, b = {c = 4, d = 3}}type t = {a : int H, b : {c : int H, d : int H} H} r1val v : t = {a = 3, b = {c = 4, d = 3}}

3HeapRegister

4

3

r1

r2

r3

r4

Standard Record Representation

Page 8: Torben Amtoft Kansas State University

type t = {a : int r1, b : {c : int H, d : int H} r3} oval v : t = {a = 3, b = {c = 4, d = 3}}type t = {a : int r1, b : {c : int H, d : int H} r3} oval v : t = {a = 3, b = {c = 4, d = 3}}

3HeapRegister

4

3

r1

r2

r3

r4

Record Registration

Page 9: Torben Amtoft Kansas State University

type t = {a : int r1, b : {c : int r4, d : int r3} o} oval v : t = {a = 3, b = {c = 4, d = 3}}type t = {a : int r1, b : {c : int r4, d : int r3} o} oval v : t = {a = 3, b = {c = 4, d = 3}}

3HeapRegister

3

4

r1

r2

r3

r4

Complete Registration

Page 10: Torben Amtoft Kansas State University

type t1 = (int H +H int H) Htype t2 = (int H +H t1) r1val v : t2 = inj(2,inj(1,25)t1)t2

type t1 = (int H +H int H) Htype t2 = (int H +H t1) r1val v : t2 = inj(2,inj(1,25)t1)t2

2HeapRegister

1

25

r1

r2

r3

r4

Standard Variant Representation

Page 11: Torben Amtoft Kansas State University

type t1 = (int H +H int H) r3type t2 = (int r2 +r1 t1) oval v : t2 = inj(2,inj(1,25)t1)t2

type t1 = (int H +H int H) r3type t2 = (int r2 +r1 t1) oval v : t2 = inj(2,inj(1,25)t1)t2

2HeapRegister

1

25

r1

r2

r3

r4

Variant Registration

Page 12: Torben Amtoft Kansas State University

type t1 = (int r2 +r4 int r1) otype t2 = (int r3 +r1 t1) oval v : t2 = inj(2,inj(1,25)t1)t2

type t1 = (int r2 +r4 int r1) otype t2 = (int r3 +r1 t1) oval v : t2 = inj(2,inj(1,25)t1)t2

2HeapRegister

25

1

r1

r2

r3

r4

Complete Variant Registration

Page 13: Torben Amtoft Kansas State University

Closure Registrationlet type t1 = {a : int r1, b : int r2} o type t2 = {b : int r2} o type t3 = (t1 +r3 t2) o fun apply1(h : t3, x : int r4) : int r5 = case h of r : t1 => let val a : int r1 = #a(r) // no-op val b : int r2 = #b(r) // no-op val c : int r6 = a * b val d : int r5 = x + c in d end r : t2 => let val b : int r2 = #b(r) // no-op val c : int r5 = x + b in c end

. . .

Page 14: Torben Amtoft Kansas State University

Closure Registration

…val a : int r1 = 3val b : int r2 = 4val c : t1 = {a = a, b = b} // no-opval d : t3 = {b = b} // no-opval e: int r4 = 343val h : t3 = if test then inj(1,c)t3 else inj(2,d)t3

in apply1(h, e)end

Page 15: Torben Amtoft Kansas State University

What we have Developed

1. A storage annotation type system

2. A typed SECD-like abstract machine

3. An annotation inference algorithmOur system works on monomorphized

whole-programs in defunctionalized A-nf.

Page 16: Torben Amtoft Kansas State University

Type System

Syntax:a ::= ri | H | o ;

annotations ::= t a

t ::= unit | int | * | +a ft ::= * A

e ::= …

Judgments: e

Kill Set

Page 17: Torben Amtoft Kansas State University

Example: Product Types

E |- (x1,x2) : (t1 a’1 * t2 a’2) a ! {a}

WFat((t1 a’1 * t2 a’2) a)E |- x1 : t1 a1; E |- x2 : t2 a2; a’i in {ai,H}

E |- i(x) : ti a’i ! {a’i} - {ai}

WFat(ti a’i)E |- x : (t1 a1 * t2 a2) a; ai in {a’i,H}

Page 18: Torben Amtoft Kansas State University

RecursionInterference

and theStack

Page 19: Torben Amtoft Kansas State University

let fun apply1(fact : unit, n : int) : int = let val a : int = 1 in ifzero n then a else let val b : int = n – a val c : int = apply1(fact,b) in let val d : int = c * n in d end end end val e : int = 4 val fact : unit = {}in apply1(fact,e) end

Defunctionalized Factorial

Page 20: Torben Amtoft Kansas State University

let fun apply1(fact : unit o, n : int r1) : int r2 = let val a : int r2 = 1 in ifzero n then a else let val b : int r3 = n – a in letcall{r1} val c : int r2 = apply1(fact,br3) in let val d : int r2 = c * nr1 in d end end end end val e : int r1 = 6 val fact : unit o = {}in apply1(fact,e) end

Defunctionalized Factorial

move r1,r3

Page 21: Torben Amtoft Kansas State University

Abstract Machine

Continuations:

K ::= stop | <x,a,{vi

ri},e,E,W,K>

Value Environment

Type Environment

Savedvalues

Page 22: Torben Amtoft Kansas State University

Abstract Machine

Machine States:

Z ::= <e, R, H, E, W, K>

Register File

Heap

Page 23: Torben Amtoft Kansas State University

Abstract Machine

Machine States:

Z ::= <e, R, H, E, W, K>

Transition Relation:

Z Z’

Page 24: Torben Amtoft Kansas State University

Properties of the System

1. Well-Typedness Preservation: WT(Z) and Z Z’ implies WT(Z’).

2. Progress: If WT(Z) then either Z is final or there exists Z’ such that Z Z’.

Page 25: Torben Amtoft Kansas State University

Annotation Inference Algorithm

Annotate(E, ue) = (e, , q, C)

Set expression

Typed but unannotated expression

Constraint Set

Expression with annotation variables

Annotated type scheme

Page 26: Torben Amtoft Kansas State University

What our System Does1. Interprocedural Virtual Register

Allocation

2. Record and Variant Registration• Function Argument Registration• Multiple-value Return

3. Customized Calling Conventions

4. Space-Efficient Tail-Calls

5. Type Safety

Page 27: Torben Amtoft Kansas State University

What we don’t Do1. Solve the Constraints

2. Physical Register Allocation

3. Heap Management

4. Empirical Analysis of Allocation Heuristics

5. Separate Compilation

Page 28: Torben Amtoft Kansas State University

Related Work

1. J. Agat 1997, 1998.

2. Morrisett et. al., 1998, 1999,…

3. Crary 2003

4. Petersen et. al., 2003

Page 29: Torben Amtoft Kansas State University

Conclusion

1. We have developed a type system that may be useful in performing reliable and efficient register allocation.

2. We intend to integrate it with a physical register allocation system.

3. We intend to study its performance.

Page 30: Torben Amtoft Kansas State University

Implementation StatusDone:1. Translation, annotation and constraint

generation (Brendan Connell)

2. Abstract Machine (Dan Allen)

Not Done:1. Constraint solving (Dan Allen)

2. Physical Register Allocation


Recommended