-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathK3-Reference.tex
More file actions
308 lines (261 loc) · 12.5 KB
/
K3-Reference.tex
File metadata and controls
308 lines (261 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
\documentclass[10pt]{article}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{chngpage}
\usepackage{hyperref}
\usepackage{mathpartir}
\usepackage{stmaryrd}
\usepackage{todonotes}
\usepackage{K3-Reference}
\hypersetup{colorlinks,linkcolor=magenta}
\newcommand{\tinysection}[1]{\noindent{\bf #1.}}
\title{K3: System and Language Design Reference}
\author{}
\date{}
\newcommand{\K}{\ensuremath{\texttt{K3}}}
\begin{document}
\maketitle
\tableofcontents
\todototoc
\listoftodos
\section{Introduction}
\subsection{Target applications}
\subsection{Related work}
\section{System overview}
\subsection{Framework Architecture}
\section{Computation model}
\subsection{Triggers and message passing}
\subsection{Scheduling}
\subsection{Language grammar}
\subsection{Operational semantics}
\section{Data model}
\subsection{Nested collections}
\subsection{Indirections}
\subsection{Literals}
\subsection{Mutability}
\subsection{Memory model}
\section{Extensibility model}
\subsection{Data annotations}
\subsection{Control annotations}
\subsection{Properties}
\section{Type System}
\tinysection{Zach's document contents.}
\section{Program transformations and Optimization}
\subsection{Program normalization}
\subsubsection{Effect lifting}
\subsection{Standard optimizations}
\subsubsection{Constant folding}
\subsubsection{Dead code elimination}
\subsubsection{Common subexpression elimination}
\subsection{Fusion}
\begin{itemize}
\item We want to avoid excessive effort to get our optimizations to industrial strength
\begin{itemize}
\item Define explicit optimizations supported, and those elided
\item We focus on high-level optimizations in our framework, e.g., fusion/deforestation
\end{itemize}
\end{itemize}
\subsection{Annotation inference}
\subsubsection{Data structure selection}
\subsubsection{Execution strategy selection}
\subsection{Co-design and iterative inference}
\subsubsection{Workload partitioning}
\section{Code Generation and Execution}
\subsection{Interpretation}
\subsection{Imperative Targets}
Code generation to imperative target languages is done in two stages. The first is a pure $\K
\rightarrow \K$ transformation which produces an imperative embedding, more amenable to
translation into an imperative language. The second stage consists of the actual translation to
the backing language.
\subsubsection{Declaration Rules}
The declaration rules for the imperative transformation perform the following tasks:
\begin{itemize}
\item Rewrite triggers into functions of the corresponding type.
\end{itemize}
%
\begin{adjustwidth}{-0.5in}{-0.5in}\begin{align*}
\eqI{\gtDeclare x \gtColon \tau \gtIs e}
{\gtDeclare x \gtColon \tau \gtIs \intI{e}}
{if $\tau$ is not a collection type.} \\
\eqI{\gtDeclare x \gtColon \tau \gtIs e}
{\gtDeclare x \gtColon \gtNamed \ConstructComposite{ids} \gtIs \intI{e}}
{if $\tau \sim \gtCollection \gtAt \{ids\}$.} \\
\eqI{\gtTrigger x \gtColon \tau \gtIs e}
{\gtDeclare x \gtColon \tau \gtArrow \gtLP \gtRP \gtIs e}
{} \\
\eqI{\gtAnnotation A \gtGiven \gtLP t_1 \gtComma \ldots \gtComma t_n \gtRP \gtLB \bar{D} \gtRB}
{\gtAnnotation A \gtGiven \gtLP t_1 \gtComma \ldots \gtComma t_n \gtRP \gtLB \intI{\bar{D}} \gtRB}{} \\
\eqI{\gtProvides \gtLifted x \gtColon \tau \gtIs e}
{\gtProvides \gtLifted x \gtColon \tau \gtIs \intI{e}}{} \\
\eqI{\gtRequires \gtLifted x \gtColon \tau}
{\gtRequires \gtLifted x \gtColon \tau}{} \\
\eqI{\gtProvides x \gtColon \tau \gtIs e}
{\gtProvides x \gtColon \tau \gtIs \intI{e}}{} \\
\eqI{\gtRequires x \gtColon \tau}
{\gtRequires x \gtColon \tau}{} \\
\eqI{\gtProvides \gtAnnotation a}
{\gtProvides \gtAnnotation a}{} \\
\eqI{\gtRequires \gtAnnotation a}
{\gtRequires \gtAnnotation a}{}
\end{align*}\end{adjustwidth}
\subsubsection{Expression Rules}
The expression rules for the imperative transformation perform the following tasks:
\begin{itemize}
\item Inline anonymous function applications.
\end{itemize}
%
\begin{adjustwidth}{-0.5in}{-0.5in}\begin{align*}
\eqI{e_1 \oplus e_2}{\intI{e_1} \oplus \intI{e_2}}{where $\oplus \neq \epsilon$ or $e_1$ is a variable.} \\
\eqI{\gtLP \lambda x \gtArrow e_1 \gtRP e_2}{\gtLet x \gtAssign \gtImmut \intI{e_2} \gtIn \intI{e_1}}{} \\
\eqI{\gtSome e}{\gtSome \intI{e}}{} \\
\eqI{\gtInd e}{\gtInd \intI{e}}{} \\
\eqI{\gtLP e_1, e_2, \ldots, e_n \gtRP}{\gtLP \intI{e_1}, \intI{e_2}, \ldots, \intI{e_n} \gtRP}{} \\
\eqI{\gtLP x_1 \gtColon e_1, x_2 \gtColon e_2, \ldots, x_n \gtColon e_n \gtRP}
{\gtLP x_1 \gtColon \intI{e_1}, x_2 \gtColon \intI{e_2}, \ldots, x_n \gtColon \intI{e_n} \gtRP}{} \\
\eqI{\gtIf e_1 \gtThen e_2 \gtElse e_3}{\gtIf \intI{e_1} \gtThen \intI{e_2} \gtElse \intI{e_3}}{} \\
\eqI{e.x}{\intI{e}.x}{} \\
\eqI{\gtLet x \gtAssign e_1 \gtIn e_2}{\gtLet x \gtAssign \intI{e_1} \gtIn \intI{e_2}}{} \\
\eqI{x \gtAssign e}{x \gtAssign \intI{e}}{} \\
\eqI{\gtCase e_1 \gtOf \gtLB \gtSome x \gtArrow e_2 \gtRB
\gtLB \gtNone \gtArrow e_3 \gtRB}
{\gtCase \intI{e_1} \gtOf \gtLB \gtSome x \gtArrow \intI{e_2} \gtRB
\gtLB \gtNone \gtArrow \intI{e_3} \gtRB}
{} \\
\eqI{\gtBind e_1 \gtAs b \gtIn e_2}{\gtBind \intI{e_1} \gtAs b \gtIn \intI{e_2}}{} \\
\eqI{e}{e}{otherwise.}
\end{align*}\end{adjustwidth}
\subsection{\texttt{C++} Generation}
Following the imperative transformation, \texttt{C++} code generation takes place through
stringification.
\subsubsection{The Reification Problem}
When generating \texttt{C++} code for \K expressions, we must address the problem of
how to make use of intermediate storage for results. While most expressions can be inlined, some
(e.g.~\texttt{if-then-else}) require that their results be stored in an intermediate location
while others (e.g.~the left operand of a sequence operator) do not care about their results
whatsoever.
In this scenario, we can define four different ways in which the result of an expression might
be handled.
\begin{description}
\item[By Inlining:] The most common method is to inline the result of the sub-expression
into its position in the calling context. Most expressions follow this form.
\item[By Name:] Some expressions cannot provide their result inline, as the syntactic form
of the result might not be known until runtime (e.g.~\texttt{if-then-else}). In this
case, the expression must reify its result in a named variable, which it can pass up to
its calling context. The name of this variable can either be passed in from the calling
context, or generated in-situ.
\item[By Return:] If the result of an expression is the return value of a generated
function, its result need not be stored in an intermediate. Instead, the value can be
returned directly.
\sn{Return Reification}{Unless of course the expression requires that the result be
stored in an intermediate. (e.g.~$\gtSome$, $\gtInd$), in which case, how does
allocation work in the return context?}
\item[By Forgetting:] If the result is not used, it need not be stored at all (e.g.~the left
operand of a sequence operator).
\end{description}
The last three forms of reification share a similar structure in \texttt{C++}. Each form can be
expressed as an effect which reifies the result appropriately. To simplify the treatment of the
various reification forms in the code generation rules, we define the reification statement
$R_\delta$ as follows:
%
\begin{align*}
R_\delta(v) = \begin{cases}
\gtReturn v & \delta = \textsc{Return} \\
k \gtIs v & \delta = \textsc{Named$(k)$} \\
\phi & \delta = \textsc{Forget}
\end{cases}
\end{align*}
\subsubsection{Expression Rules}
For the purposes of generating code, we define the family of derivation relations $\rwD$, where
$\delta \in \{\textsc{Inline}, \textsc{Named$(k)$}, \textsc{Return}, \textsc{Forget}\}$. Some
expressions lend themselves naturally to the inline form, and for such rules the reified form
can be derived mechanically. Alternately, other expressions lend themselves naturally to the
reified form, where the reverse is possible. These two canonical rules are as follows:
%
\begin{align*}
\intCD{e} &= \intEI{e} \gtSemi R_\delta(\intVI{e}) \with \phi \\
\intCI{e} &= \Fresh{k} \gtSemi \intE{N(k)}{e} \with k \\
\end{align*}
Therefore it is generally only necessary to specify one of the rules (\textsc{Inline} or
otherwise) for each expression. These rules are as follows:
\begin{adjustwidth}{-0.5in}{-0.5in}\begin{align*}
\intC{*}{e} &= \intE{*}{e} \with \intV{*}{e} \\
\intCI{c} &= \phi \with c \\
\intCI{v} &= \phi \with v \\
\intCI{\gtSome e} &= \intEI{e} \with \ccall{shared\_ptr}{\tau_e}{\gtNew \tau_e\gtLP\intVI{e}\gtRP} \\
\intCI{\gtInd e} &= \intEI{e} \with \ccall{shared\_ptr}{\tau_e}{\gtNew \tau_e\gtLP\intVI{e}\gtRP} \\
\intCI{\gtLP e_1 \gtComma \ldots e_n \gtRP}
&= \intEI{e_1} \gtSemi \ldots \gtSemi \intEI{e_n}
\with \gtLP \intVI{e_1} \gtComma \ldots \gtComma \intVI{e_n} \gtRP \\
\intCI{\gtLB x_1 \gtColon e_1 \gtComma \ldots x_n \gtColon e_n \gtRB}
&= \intEI{e_1} \gtSemi \ldots \gtSemi \intEI{e_n}
\with \gtLB x_1 \gtColon \intVI{e_1} \gtComma \ldots \gtComma x_n \gtColon \intVI{e_n} \gtRP \\
\intCI{e_1 \gtSemi e_2} &= \intE{F}{e_1} \gtSemi \intEI{e_2} \with \intVI{e_2} \\
\intCI{\oplus e} &= \intEI{e} \with \oplus \intVI{e} \\
\intCI{e_1 \oplus e_2} &= \intEI{e_1} \gtSemi \intEI{e_2} \with \intVI{e_1} \oplus \intVI{e_2} \\
\intCI{x \gtAssign e}
&= \intE{N(x)}{e} \with \gtNull \\
% \intCD{\gtLet x \gtAssign a \gtIn b}
% &= \intEI{a} \gtSemi \gtLB x \gtAssign \intVI{a} \gtSemi \intED{b} \gtRB \\
\intCD{\gtLet x \gtAssign a \gtIn b}
&= \gtLB \intE{N(x)}{a} \gtSemi \intED{b} \gtRB \\
\intCD{\gtCase e \gtOf \gtLB \gtSome x \gtArrow a \gtRB \gtLB \gtNone \gtArrow b \gtRB}
&= \intEI{e} \gtSemi \gtIf \gtLP \intVI{e} \gtNeq \gtNull \gtRP
\gtLB x \gtAssign \gtStar \intVI{e} \gtSemi \intED{a} \gtRB \gtElse
\gtLB \intED{b} \gtRB \\
\intCD{\gtBind e \gtAs \gtInd x \gtIn b}
&= \intEI{e} \gtSemi \gtLB x \gtAssign \gtStar \intVI{e} \gtSemi \intED{b} \gtRB \\
\intCD{\gtBind e \gtAs \gtLP x_1 \gtComma \ldots \gtComma x_n \gtRP \gtIn b}
&= \intEI{e} \gtSemi \gtLB
x_1 \gtAssign \ccall{get}{0}{\intVI{e}} \gtSemi
\ldots \gtSemi
x_n \gtAssign \ccall{get}{\texttt{$n$-$1$}}{\intVI{e}} \gtSemi
\intED{b} \gtRB \\
\intCD{\gtBind e \gtAs \gtLB x_1 \gtColon y_1 \gtComma \ldots \gtComma x_n \gtColon y_n \gtRB \gtIn b}
&= \intEI{e} \gtSemi \gtLB
y_1 \gtAssign \intVI{e}.x_1 \gtSemi
\ldots \gtSemi
y_n \gtAssign \intVI{e}.x_n \gtSemi
\intED{b} \gtRB \\
\intCD{\gtIf p \gtThen t \gtElse e}
&= \intEI{p} \gtSemi \gtIf \gtLP \intVI{p} \gtRP
\gtLB \intED{t} \gtRB \gtElse
\gtLB \intED{e} \gtRB
\end{align*}\end{adjustwidth}
\subsubsection{Declaration Rules}
\begin{align*}
\intC{*}{\gtDeclare x \gtColon \tau \gtAssign e}
&= \Decl{\tau}{x}{} \gtSemi
\\& \text{if $\tau$ is not a function type.} \\
\intC{*}{\gtDeclare x \gtColon \tau_1 \gtArrow \tau_2 = e}
&= \Omega(\tau_2) x \gtLP \Omega(\tau_1) \gtRP \gtLB \intE{R}{e} \gtRB
\\& \text{otherwise.}
\end{align*}
\subsection{Haskell}
\tinysection{Effect embedding}
\section{Adaptive execution}
\subsection{Adaptive engine design:}
\begin{itemize}
\item need a common IR data representation accessible by all adaptive backends
\begin{itemize}
\item can we synthesize this from a K3 representation using a standard code generator?
\item that is, we metacompile our IR data as a tree with tagged variants?
\end{itemize}
\item code generation embeds initial K3 IR
\item each adaptive backend provides its own:
\begin{itemize}
\item JIT compiler from the IR to native code
\item compiled library loading facilities
\item adaptation algorithms on the IR
\end{itemize}
\end{itemize}
\subsection{Adaptation algorithm design}
\section{Deployment}
\subsection{Bootstrap}
\subsection{Docker}
\subsection{Mesos}
\section{Benchmarks}
\subsection{SQL Queries}
\subsection{Machine Learning Algorithms}
\subsection{Indexing Data Structures}
\end{document}