瀏覽代碼

progress on graph coloring

Jeremy Siek 9 年之前
父節點
當前提交
8f69ad1e7e
共有 2 個文件被更改,包括 194 次插入41 次删除
  1. 55 3
      all.bib
  2. 139 38
      book.tex

+ 55 - 3
all.bib

@@ -2,13 +2,65 @@
 %% http://bibdesk.sourceforge.net/
 
 
-%% Created for Jeremy Siek at 2015-11-06 15:53:10 -0500 
+%% Created for Jeremy Siek at 2015-11-11 23:20:46 -0500 
 
 
 %% Saved with string encoding Unicode (UTF-8) 
 
 
 
+@mastersthesis{Bharadwaj:2012aa,
+	Author = {Shashank Bharadwaj},
+	Date-Added = {2015-11-10 20:28:23 +0000},
+	Date-Modified = {2015-11-10 20:29:37 +0000},
+	School = {University of Colorado at Boulder},
+	Title = {Optimizing Jython using invokedynamic and Gradual Typing},
+	Year = {2012},
+	Bdsk-File-1 = {YnBsaXN0MDDUAQIDBAUGJCVYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoKgHCBMUFRYaIVUkbnVsbNMJCgsMDxJXTlMua2V5c1pOUy5vYmplY3RzViRjbGFzc6INDoACgAOiEBGABIAFgAdccmVsYXRpdmVQYXRoWWFsaWFzRGF0YVxzaGFzaGFuay5wZGbSFwsYGVdOUy5kYXRhTxEBYgAAAAABYgACAAAMTWFjaW50b3NoIEhEAAAAAAAAAAAAAAAAAAAAzcjA6kgrAAAAv23xDHNoYXNoYW5rLnBkZgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEgtafSZ73ZUERGIAAAAAAAAQACAAAJIAAAAAAAAAAAAAAAAAAAAANiaWIAABAACAAAzcj5KgAAABEACAAA0mgEKQAAAAEADAC/bfEABcByAAISKQACACxNYWNpbnRvc2ggSEQ6VXNlcnM6AGpzaWVrOgBiaWI6AHNoYXNoYW5rLnBkZgAOABoADABzAGgAYQBzAGgAYQBuAGsALgBwAGQAZgAPABoADABNAGEAYwBpAG4AdABvAHMAaAAgAEgARAASABxVc2Vycy9qc2llay9iaWIvc2hhc2hhbmsucGRmABMAAS8AABUAAgAM//8AAIAG0hscHR5aJGNsYXNzbmFtZVgkY2xhc3Nlc11OU011dGFibGVEYXRhox0fIFZOU0RhdGFYTlNPYmplY3TSGxwiI1xOU0RpY3Rpb25hcnmiIiBfEA9OU0tleWVkQXJjaGl2ZXLRJidUcm9vdIABAAgAEQAaACMALQAyADcAQABGAE0AVQBgAGcAagBsAG4AcQBzAHUAdwCEAI4AmwCgAKgCDgIQAhUCIAIpAjcCOwJCAksCUAJdAmACcgJ1AnoAAAAAAAACAQAAAAAAAAAoAAAAAAAAAAAAAAAAAAACfA==}}
+
+@inproceedings{Grimmer:2015aa,
+	Acmid = {2816714},
+	Address = {New York, NY, USA},
+	Author = {Grimmer, Matthias and Seaton, Chris and Schatz, Roland and W\"{u}rthinger, Thomas and M\"{o}ssenb\"{o}ck, Hanspeter},
+	Booktitle = {Proceedings of the 11th Symposium on Dynamic Languages},
+	Date-Added = {2015-11-10 19:53:58 +0000},
+	Date-Modified = {2015-11-10 19:54:02 +0000},
+	Doi = {10.1145/2816707.2816714},
+	Isbn = {978-1-4503-3690-1},
+	Keywords = {cross-language, language implementation, language interoperability, optimization, virtual machine},
+	Location = {Pittsburgh, PA, USA},
+	Numpages = {13},
+	Pages = {78--90},
+	Publisher = {ACM},
+	Series = {DLS 2015},
+	Title = {High-performance Cross-language Interoperability in a Multi-language Runtime},
+	Url = {http://doi.acm.org/10.1145/2816707.2816714},
+	Year = {2015},
+	Bdsk-Url-1 = {http://doi.acm.org/10.1145/2816707.2816714},
+	Bdsk-Url-2 = {http://dx.doi.org/10.1145/2816707.2816714}}
+
+@inproceedings{Richards:2015aa,
+	Author = {Gregor Richards and Francesco Zappa Nardelli and Jan Vitek },
+	Booktitle = {European {C}onference on {O}bject-{O}riented {P}rogramming},
+	Date-Added = {2015-11-10 19:26:58 +0000},
+	Date-Modified = {2015-11-10 19:28:45 +0000},
+	Series = {ECOOP},
+	Title = {Concrete Types for TypeScript},
+	Year = {2015},
+	Bdsk-File-1 = {YnBsaXN0MDDUAQIDBAUGJCVYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoKgHCBMUFRYaIVUkbnVsbNMJCgsMDxJXTlMua2V5c1pOUy5vYmplY3RzViRjbGFzc6INDoACgAOiEBGABIAFgAdccmVsYXRpdmVQYXRoWWFsaWFzRGF0YVxlY29vcDE1YS5wZGbSFwsYGVdOUy5kYXRhTxEBYgAAAAABYgACAAAMTWFjaW50b3NoIEhEAAAAAAAAAAAAAAAAAAAAzcjA6kgrAAAAv23xDGVjb29wMTVhLnBkZgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEgsJLSZ6+QUERGIAAAAAAAAQACAAAJIAAAAAAAAAAAAAAAAAAAAANiaWIAABAACAAAzcj5KgAAABEACAAA0mf14AAAAAEADAC/bfEABcByAAISKQACACxNYWNpbnRvc2ggSEQ6VXNlcnM6AGpzaWVrOgBiaWI6AGVjb29wMTVhLnBkZgAOABoADABlAGMAbwBvAHAAMQA1AGEALgBwAGQAZgAPABoADABNAGEAYwBpAG4AdABvAHMAaAAgAEgARAASABxVc2Vycy9qc2llay9iaWIvZWNvb3AxNWEucGRmABMAAS8AABUAAgAM//8AAIAG0hscHR5aJGNsYXNzbmFtZVgkY2xhc3Nlc11OU011dGFibGVEYXRhox0fIFZOU0RhdGFYTlNPYmplY3TSGxwiI1xOU0RpY3Rpb25hcnmiIiBfEA9OU0tleWVkQXJjaGl2ZXLRJidUcm9vdIABAAgAEQAaACMALQAyADcAQABGAE0AVQBgAGcAagBsAG4AcQBzAHUAdwCEAI4AmwCgAKgCDgIQAhUCIAIpAjcCOwJCAksCUAJdAmACcgJ1AnoAAAAAAAACAQAAAAAAAAAoAAAAAAAAAAAAAAAAAAACfA==}}
+
+@inproceedings{Takikawa:2016aa,
+	Author = {Asumu Takikawa and Daniel Feltey and Ben Greenman and Max New and Jan Vitek and Matthias Felleisen},
+	Booktitle = {Principles of Programming Languages},
+	Date-Added = {2015-11-10 19:09:27 +0000},
+	Date-Modified = {2015-11-10 19:10:32 +0000},
+	Month = {January},
+	Publisher = {ACM},
+	Series = {POPL},
+	Title = {Is Sound Gradual Typing Dead?},
+	Year = {2016},
+	Bdsk-File-1 = {YnBsaXN0MDDUAQIDBAUGJCVYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoKgHCBMUFRYaIVUkbnVsbNMJCgsMDxJXTlMua2V5c1pOUy5vYmplY3RzViRjbGFzc6INDoACgAOiEBGABIAFgAdccmVsYXRpdmVQYXRoWWFsaWFzRGF0YV8QEXBvcGwxNi10ZmdudmYucGRm0hcLGBlXTlMuZGF0YU8RAXgAAAAAAXgAAgAADE1hY2ludG9zaCBIRAAAAAAAAAAAAAAAAAAAAM3IwOpIKwAAAL9t8RFwb3BsMTYtdGZnbnZmLnBkZgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABIK4h0merp1BERiAAAAAAAAEAAgAACSAAAAAAAAAAAAAAAAAAAAADYmliAAAQAAgAAM3I+SoAAAARAAgAANJn8fcAAAABAAwAv23xAAXAcgACEikAAgAxTWFjaW50b3NoIEhEOlVzZXJzOgBqc2llazoAYmliOgBwb3BsMTYtdGZnbnZmLnBkZgAADgAkABEAcABvAHAAbAAxADYALQB0AGYAZwBuAHYAZgAuAHAAZABmAA8AGgAMAE0AYQBjAGkAbgB0AG8AcwBoACAASABEABIAIVVzZXJzL2pzaWVrL2JpYi9wb3BsMTYtdGZnbnZmLnBkZgAAEwABLwAAFQACAAz//wAAgAbSGxwdHlokY2xhc3NuYW1lWCRjbGFzc2VzXU5TTXV0YWJsZURhdGGjHR8gVk5TRGF0YVhOU09iamVjdNIbHCIjXE5TRGljdGlvbmFyeaIiIF8QD05TS2V5ZWRBcmNoaXZlctEmJ1Ryb290gAEACAARABoAIwAtADIANwBAAEYATQBVAGAAZwBqAGwAbgBxAHMAdQB3AIQAjgCiAKcArwIrAi0CMgI9AkYCVAJYAl8CaAJtAnoCfQKPApIClwAAAAAAAAIBAAAAAAAAACgAAAAAAAAAAAAAAAAAAAKZ}}
+
 @inproceedings{Takikawa:2015aa,
 	Author = {Asumu Takikawa and Daniel Feltey and Earl Dean and Matthew Flatt and Robert Bruce Findler and Sam Tobin-Hochstadt and Matthias Felleisen},
 	Booktitle = {European Conference on Object-Oriented Programming},
@@ -11143,9 +11195,9 @@ semantics was validated.},
 	Bdsk-File-1 = {YnBsaXN0MDDUAQIDBAUGJCVYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3ASAAGGoKgHCBMUFRYaIVUkbnVsbNMJCgsMDxJXTlMua2V5c1pOUy5vYmplY3RzViRjbGFzc6INDoACgAOiEBGABIAFgAdccmVsYXRpdmVQYXRoWWFsaWFzRGF0YV8QEGZ1bGx0ZXh0KDEwKS5wZGbSFwsYGVdOUy5kYXRhTxEBcgAAAAABcgACAAAMTWFjaW50b3NoIEhEAAAAAAAAAAAAAAAAAAAAzcjA6kgrAAAAv23xEGZ1bGx0ZXh0KDEwKS5wZGYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC/c3vRDzb+AAAAAAAAAAAAAQACAAAJIAAAAAAAAAAAAAAAAAAAAANiaWIAABAACAAAzcj5KgAAABEACAAA0Q99TgAAAAEADAC/bfEABcByAAISKQACADBNYWNpbnRvc2ggSEQ6VXNlcnM6AGpzaWVrOgBiaWI6AGZ1bGx0ZXh0KDEwKS5wZGYADgAiABAAZgB1AGwAbAB0AGUAeAB0ACgAMQAwACkALgBwAGQAZgAPABoADABNAGEAYwBpAG4AdABvAHMAaAAgAEgARAASACBVc2Vycy9qc2llay9iaWIvZnVsbHRleHQoMTApLnBkZgATAAEvAAAVAAIADP//AACABtIbHB0eWiRjbGFzc25hbWVYJGNsYXNzZXNdTlNNdXRhYmxlRGF0YaMdHyBWTlNEYXRhWE5TT2JqZWN00hscIiNcTlNEaWN0aW9uYXJ5oiIgXxAPTlNLZXllZEFyY2hpdmVy0SYnVHJvb3SAAQAIABEAGgAjAC0AMgA3AEAARgBNAFUAYABnAGoAbABuAHEAcwB1AHcAhACOAKEApgCuAiQCJgIrAjYCPwJNAlECWAJhAmYCcwJ2AogCiwKQAAAAAAAAAgEAAAAAAAAAKAAAAAAAAAAAAAAAAAAAApI=}}
 
 @article{Omari:2006uq,
-	Author = {Hussein A. Omari and Khair E.& nbsp;Sabri Hussein A. Omari and Khair E.& nbsp;Sabri Hussein A. Omari and Khair E.& nbsp;Sabri Hussein A. Omari and Khair E. Sabri},
+	Author = {Hussein Al-Omari and Khair Eddin Sabri},
 	Date-Added = {2010-12-14 12:52:52 -0700},
-	Date-Modified = {2010-12-14 12:54:46 -0700},
+	Date-Modified = {2015-11-12 04:20:43 +0000},
 	Journal = {Journal of Mathematics and Statistics},
 	Number = {4},
 	Read = {1},

+ 139 - 38
book.tex

@@ -768,10 +768,10 @@ variables instead of stack locations or registers.
 \begin{minipage}{0.45\textwidth}
 Source program:
 \begin{lstlisting}
-(let ([x 30])
-  (let ([z (+ x 4)])
+(let ([x 25])
+  (let ([z (+ 9 x)])
     (let ([y 2])
-      (let ([w (+ z 10)])
+      (let ([w (+ 10 z)])
         (- w y)))))
 \end{lstlisting}
 \end{minipage}
@@ -779,12 +779,12 @@ Source program:
 After instruction selection:
 \begin{lstlisting}
 (program (x z y w)
-  (mov (int 30) (var x))
-  (mov (var x) (var z))
-  (add (int 4) (var z))
+  (mov (int 25) (var x))
+  (mov (int 9) (var z))
+  (add (var x) (var z))
   (mov (int 2) (var y))
-  (mov (var z) (var w))
-  (add (int 10) (var w))
+  (mov (int 10) (var w))
+  (add (var z) (var w))
   (mov (var w) (reg rax))
   (sub (var y) (reg rax)))
 \end{lstlisting}
@@ -793,7 +793,6 @@ After instruction selection:
 \label{fig:reg-eg}
 \end{figure}
 
-
 The goal of register allocation is to fit as many variables into
 registers as possible. It is often the case that we have more
 variables than registers, so we can't naively map each variable to a
@@ -857,17 +856,17 @@ $L_{\mathtt{after}}$ set.
 
 \begin{figure}[tbp]
 \begin{lstlisting}
-(program (x z y w)           ; @$\{ \}$@
-  (mov (int 30) (var x))     ; @$\{ x \}$@
-  (mov (var x) (var z))      ; @$\{ z \}$@
-  (add (int 4) (var z))      ; @$\{ z \}$@
-  (mov (int 2) (var y))      ; @$\{ y, z \}$@
-  (mov (var z) (var w))      ; @$\{ w, y \}$@
-  (add (int 10) (var w))     ; @$\{ w, y \}$@
-  (mov (var w) (reg rax))    ; @$\{ y, \itm{rax} \}$@
-  (sub (var y) (reg rax)))   ; @$\{ \}$@
+(program (x z y w)
+  (mov (int 25) (var x))      @$\{x\}$@
+  (mov (int 9) (var z))       @$\{x,z\}$@
+  (add (var x) (var z))       @$\{z\}$@
+  (mov (int 2) (var y))       @$\{y,z\}$@
+  (mov (int 10) (var w))      @$\{w,y,z\}$@
+  (add (var z) (var w))       @$\{w,y\}$@
+  (mov (var w) (reg rax))     @$\{y\}$@
+  (sub (var y) (reg rax)))    @$\{\}$@
 \end{lstlisting}
-\caption{Running example program annotated with live variables.}
+\caption{Running example program annotated with live-after sets.}
 \label{fig:live-eg}
 \end{figure}
 
@@ -911,17 +910,16 @@ by the following rules.
   register $r$ and every variable $v \in L_{\mathsf{after}}(k)$.
 \end{itemize}
 
-Working from the top to bottom of Figure~\ref{fig:live-eg}, $y$
-interferes with $z$, $w$ interfers with $y$, 
-[?? w should not conflict with z! ??]
-The resulting interference graph is shown in
+Working from the top to bottom of Figure~\ref{fig:live-eg}, $z$
+interferes with $x$, $y$ interferes with $z$, and $w$ interferes with
+$y$ and $z$.  The resulting interference graph is shown in
 Figure~\ref{fig:interfere}.
 
 \begin{figure}[tbp]
 \large
 \[
 \xymatrix@=40pt{
-  w \ar@{-}[d] \ar@{-}[dr] &  x \ar@{-}[d] \\
+  w \ar@{-}[d]\ar@{-}[dr] &  x \ar@{-}[d]\\
   y \ar@{-}[r] & z
 }
 \]
@@ -930,36 +928,139 @@ Figure~\ref{fig:interfere}.
 \end{figure}
 
 
+\section{Graph Coloring via Sudoku}
+
+We now come to the main event, mapping variables to registers (or to
+stack locations in the event that we run out of registers).  We need
+to make sure not to map two variables to the same register if the two
+variables interfere with each other.  In terms of the interference
+graph, this means we cannot map adjacent nodes to the same register.
+If we think of registers as colors, the register allocation problem
+becomes the widely-studied graph coloring
+problem~\citep{Balakrishnan:1996ve,Rosen:2002bh}.  
+
+The reader may be more familar with the graph coloring problem then he
+or she realizes; the popular game of Sudoku is an instance of the
+graph coloring problem. The following describes how to build a graph
+out of a Sudoku board.
+\begin{itemize}
+\item There is one node in the graph for each Sudoku square.
+\item There is an edge between two nodes if the corresponding squares
+  are in the same row or column, or if the squares are in the same
+  $3\times 3$ region.
+\item Choose nine colors to correspond to the numbers $1$ to $9$.
+\item Based on the initial assignment of numbers to squares in the
+  Sudoku board, assign the corresponding colors to the corresponding
+  nodes in the graph.
+\end{itemize}
+If you can color the remaining nodes in the graph with the nine
+colors, then you've also solved the corresponding game of Sudoku.
+
+Given that Sudoku is graph coloring, one can use Sudoku strategies to
+come up with an algorithm for allocating registers. For example, one
+of the basic techniques for Sudoku is Pencil Marks. The idea is that
+you use a process of elimination to determine what numbers still make
+sense for a square, and write down those numbers in the square
+(writing very small). At first, each number might be a
+possibility, but as the board fills up, more and more of the
+possibilities are crossed off (or erased). For example, if the number
+$1$ is assigned to a square, then by process of elimination, you can
+cross off the $1$ pencil mark from all the squares in the same row,
+column, and region. Many Sudoku computer games provide automatic
+support for Pencil Marks. This heuristic also reduces the degree of
+branching in the search tree.
+
+The Pencil Marks technique corresponds to the notion of color
+\emph{saturation} due to \cite{Brelaz:1979eu}.  The
+saturation of a node, in Sudoku terms, is the number of possibilities
+that have been crossed off using the process of elimination mentioned
+above. In graph terminology, we have the following definition:
+\begin{equation*}
+  \mathrm{saturation}(u) = |\{ c \;|\; \exists v. v \in \mathrm{Adj}(u) 
+     \text{ and } \mathrm{color}(v) = c \}|
+\end{equation*}
+where $\mathrm{Adj}(u)$ is the set of nodes adjacent to $u$ and
+the notation $|S|$ stands for the size of the set $S$.
+
+Using the Pencil Marks technique leads to a simple strategy for
+filling in numbers: if there is a square with only one possible number
+left, then write down that number! But what if there aren't any
+squares with only one possibility left? One brute-force approach is to
+just make a guess. If that guess ultimately leads to a solution,
+great.  If not, backtrack to the guess and make a different guess.  Of
+course, this is horribly time consuming. One standard way to reduce
+the amount of backtracking is to use the most-constrained-first
+heuristic. That is, when making a guess, always choose a square with
+the fewest possibilities left (the node with the highest saturation).
+The idea is that choosing highly constrained squares earlier rather
+than later is better because later there may not be any possibilities
+left.
+
+In some sense, register allocation is easier than Sudoku because we
+can always cheat and add more numbers by spilling variables to the
+stack. Also, we'd like to minimize the time needed to color the graph,
+and backtracking is expensive. Thus, it makes sense to keep the
+most-constrained-first heuristic but drop the backtracking in favor of
+greedy search (guess and just keep going).
+Figure~\ref{fig:satur-algo} gives the pseudo-code for this simple
+greedy algorithm for register allocation based on saturation and the
+most-constrained-first heuristic, which is roughly equivalent to the
+DSATUR algorithm of \cite{Brelaz:1979eu} (also known as
+saturation degree ordering
+(SDO)~\citep{Gebremedhin:1999fk,Omari:2006uq}).  Just as in Sudoku,
+the algorithm represents colors with integers, with the first $k$
+colors corresponding to the $k$ registers in a given machine and the
+rest of the integers corresponding to stack locations.
+
+\begin{figure}[btp]
+  \centering
+\begin{lstlisting}[basicstyle=\rmfamily,deletekeywords={for,from,with,is,not,in,find},morekeywords={while},columns=fullflexible]
+Algorithm: DSATUR
+Input: the inference graph @$G$@
+Output: an assignment @$\mathrm{color}(v)$@ for each node @$v \in G$@
+
+@$W \gets \mathit{vertices}(G)$@
+while @$W \neq \emptyset$@ do
+    pick a node @$u$@ from @$W$@ with the highest saturation,
+        breaking ties randomly
+    find the lowest color @$c$@ that is not in @$\{ \mathrm{color}(v) \;|\; v \in \mathrm{Adj}(v)\}$@
+    @$\mathrm{color}(u) = c$@
+    @$W \gets W - \{u\}$@
+\end{lstlisting}
+  \caption{Saturation-based greedy graph coloring algorithm.}
+  \label{fig:satur-algo}
+\end{figure}
+
 
+UNDER CONSTRUCTION
 
-\section{Graph Coloring via Sudoku}
 
 Suppose only \key{rbx} is available for use by the register allocator.
 
 \[
-\xymatrix{
-  w:\key{-8(\%rbp)}  \ar@{-}[d] \ar@{-}[dr] &  x:\itm{rbx} \ar@{-}[d] \\
-  y:\itm{rbx} \ar@{-}[r] & z:\key{-16(\%rbp)}
+\xymatrix@=40pt{
+  w:\key{-16(\%rbp)} \ar@{-}[d] \ar@{-}[dr] &  x:\key{\%rbx} \ar@{-}[d] \\
+  y:\key{\%rbx} \ar@{-}[r] & z:\key{-8(\%rbp)}
 }
 \]
 
 \begin{lstlisting}
-	movq	$30, %rbx
-	movq	%rbx, -16(%rbp)
-	addq	$4, -16(%rbp)
+	movq	$25, %rbx
+	movq	$9, -8(%rbp)
+	addq	%rbx, -8(%rbp)
 	movq	$2, %rbx
-	movq	-16(%rbp), -8(%rbp)
-	addq	$10, -8(%rbp)
-	movq	-8(%rbp), %rax
+	movq	$10, -16(%rbp)
+	addq	-8(%rbp), -16(%rbp)
+	movq	-16(%rbp), %rax
 	subq	%rbx, %rax
 \end{lstlisting}
 
-patch instructions fixes the move from 
-\key{-16(\%rbp)} to  \key{-8(\%rbp)}.
+patch instructions fixes the addition of
+\key{-8(\%rbp)} to  \key{-16(\%rbp)}.
 
 \begin{lstlisting}
-	movq	-16(%rbp), %rax
-	movq	%rax, -8(%rbp)
+	movq	-8(%rbp), %rax
+	addq	%rax, -16(%rbp)
 \end{lstlisting}
 
 % three new passes between instruction selection and spill code