view cfopm.tex @ 15:bbbeecda034d

fic
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Sat, 04 Jul 2015 23:10:39 +0900
parents 117760bfcae9
children e13520c327f1
line wrap: on
line source

\documentclass[conference]{IEEEtran}

\usepackage[cmex10]{amsmath}
\usepackage{url}
\usepackage{listings}
\usepackage[dvipdfmx]{graphicx}

\lstset{
  frame=single,
  keepspaces=true,
  stringstyle={\ttfamily},
  commentstyle={\ttfamily},
  identifierstyle={\ttfamily},
  keywordstyle={\ttfamily},
  basicstyle={\ttfamily},
  breaklines=true,
  xleftmargin=0zw,
  xrightmargin=0zw,
  framerule=.2pt,
  columns=[l]{fullflexible},
  numbers=left,
  stepnumber=1,
  numberstyle={\scriptsize},
  numbersep=1em,
  language=c,
  tabsize=4,
  lineskip=-0.5zw,
  escapechar={@},
}

\ifCLASSINFOpdf
  % \usepackage[pdftex]{graphicx}
  % declare the path(s) where your graphic files are
  % \graphicspath{{../pdf/}{../jpeg/}}
  % and their extensions so you won't have to specify these with
  % every instance of \includegraphics
  % \DeclareGraphicsExtensions{.pdf,.jpeg,.png}
\else
  % or other class option (dvipsone, dvipdf, if not using dvips). graphicx
  % will default to the driver specified in the system graphics.cfg if no
  % driver is specified.
  % \usepackage[dvips]{graphicx}
  % declare the path(s) where your graphic files are
  % \graphicspath{{../eps/}}
  % and their extensions so you won't have to specify these with
  % every instance of \includegraphics
  % \DeclareGraphicsExtensions{.eps}
\fi


% correct bad hyphenation here
\hyphenation{op-tical net-works semi-conduc-tor}


\begin{document}
%
% paper title
% Titles are generally capitalized except for words such as a, an, and, as,
% at, but, by, for, in, nor, of, on, or, the, to and up, which are usually
% not capitalized unless they are the first or last word of the title.
% Linebreaks \\ can be used within to get better formatting as desired.
% Do not put math or special symbols in the title.
\title{Implementing Continuation based language in LLVM and Clang}


% author names and affiliations
% use a multiple column layout for up to three different
% affiliations
\author{
\IEEEauthorblockN{Kaito TOKUMORI}
\IEEEauthorblockA{University of the Ryukyus \\ Email: kaito@cr.ie.u-ryukyu.ac.jp}
\and
\IEEEauthorblockN{Shinji KONO}
\IEEEauthorblockA{University of the Ryukyus \\ Email: kono@ie.u-ryukyu.ac.jp}
}

% make the title area
\maketitle

% As a general rule, do not put math, special symbols or citations
% in the abstract
\begin{abstract}
The programming paradigm which use data segments and code segments
are proposed. 
This paradigm uses Continuation based C (CbC), 
which a slight modified C language.
Code segments are units of calculation and
Data segments are sets of typed data.
We use these segments as units of computation and meta computation.
In this paper we show the implementation of CbC on LLVM and Clang 3.7.
\end{abstract}

% no keywords




% For peer review papers, you can put extra information on the cover
% page as needed:
% \ifCLASSOPTIONpeerreview
% \begin{center} \bfseries EDICS Category: 3-BBND \end{center}
% \fi
%
% For peerreview papers, this IEEEtran command inserts a page break and
% creates the second title. It will be ignored for other modes.
\IEEEpeerreviewmaketitle

\section{A Practical Continuation based Language}
The proposed units of programming are named code segments and data segments. 
Code segments are units of calculation which have no state. 
Data segments are sets of typed data.
Code segments are connected to data segments by a meta data segment called a context. 
After the execution of a code segment and its context, the next code segment (continuation) is executed.

Continuation based C (CbC) \cite{DBLP:journals/corr/abs-1109-4048}, hereafter referred to as CbC, is a slight modified C which supports code segments. It is compatible with C and has continuation as a goto statement. 

Code segments and data segments are low level enough to represent computational details,
and are architecture independent. They can be used as architecture independent assemblers. 

% CbC has stand alone compiler and GCC version. Here we report new partial implementation of CbC compiler based on LLVM and Clang 3.7.
CbC was first implemented on micro-C one path compiler.
GCC based CbC compiler is developed in 2008\cite{DBLP:journals/corr/abs-1109-4048}.
GCC is GNU Compiler Collection \cite{gcc}.
In GCC version,  nested function is used to implement goto with environment in 2011\cite{CbC2011}. 
In this study, we report a latest CbC compiler which is implemented
in LLVM and Clang 3.7. 

\verb+C--+  \cite{cminusminus} is also known as a lower level language of C. It has precise type specification and goto statement with parameters.
CbC introduces \verb+__code++ type for code segment which makes clear separation of functions and code segments.

\section{Continuation based C}
CbC's basic programming unit is the code segment. These are not subroutines, but they look like functions because they take input and produce output. Both input and output should be data segments. Table \ref{src:example} details the definition of the data segment. 

\begin{table}[html]
\begin{lstlisting}
  __code f(Allocate allocate){
    allocate.size = 0;
    goto g(allocate);
  }

  // data segment definition
  // (generated automatically)
  union Data {
    struct Allocate {
      long size;
    } allocate;
  };
\end{lstlisting}
\caption{CbC Example}
\label{src:example}
\end{table}

In this example, the code segment {\bf f} takes the input data segment {\bf allocate} (allocate is the data segments identifier) and sends f's output to the code segment {\bf g}. The CbC compiler generates the data segment definition automatically, so writing it is unnecessary. There is no return from code segment {\bf g}. {\bf G} should call another continuation using {\bf goto}. Code segments have input data segments and output data segments. Data segments have two kind of dependency with code segments.
First, Code segments access the contents of data segments using field names. So data segments should have the named fields.
The second dependency is a data dependency, that is all input data segments should be ready before their execution.

\begin{figure}[htp]
  \begin{center}
    \scalebox{0.5}{\includegraphics{fig/csds.pdf}}
  \end{center}
  \caption{Code Segments and Data Segments on CbC}
  \label{fig:csds}
\end{figure}

Shifting completely, from C to CbC is  unnecessary as in CbC we can go to code segments from C functions and call C functions in code segments   The latter is straightforward but the former needs further extensions.

\begin{table}[html]
\begin{lstlisting}
  int main() {
    goto hello("Hello World\n", __return, __environment);
  }

  __code hello(char *s, __code(*ret)(int, void*), void *env) {
    printf(s);
    goto (*ret)(123);
  }
\end{lstlisting}
\caption{Call C Functions in a Code Segment}
\label{src:example}
\end{table}

In this hello world example, the environment of {\bf main}() and its continuation is kept in the variable {\bf \_\_environment}. The environment and the continuation can be accessed using {\bf \_\_environment} and {\bf \_\_return}.The arbitrary mixing of code segments and functions is allowed. The continuation of a {\bf goto} statement never returns to the original function, but goes to the caller or the original function. In that case, it returns the result 123 to the operating system. This continuation is called {\bf goto with environment}. 

\section{LLVM and Clang}
The LLVM Project is a collection of modular and reusable compilers and tool chain technologies, and the LLVM Core libraries provide a modern source and target independent optimizer, along with code generation support for many popular CPUs. Clang is an LLVM native C/C++/Objective-C compiler. Figure \ref{fig:structure} shows Clang and LLVM's compilation flow.

\begin{figure}[htp]
  \begin{center}
    \scalebox{0.25}{\includegraphics{fig/clang_llvm_structure.pdf}}
  \end{center}
  \caption{LLVM and Clang structure}
  \label{fig:structure}
\end{figure}

LLVM has an intermediate representation which is called LLVM IR\cite{LLVMIR}. This part remains unmodified so that the optimization part does not need to be modified.

\section{Implementation in LLVM and Clang}
The CbC compiler is implemented in LLVM and Clang using the following ideas. 

\begin{itemize}
\item Code segments are implemented by C functions.
\item Transition is implemented by forced tail call.
\item Goto with environment is implemented by setjmp and longjmp.
\end{itemize}

{\bf \_\_code} is implemented as a new type keyword in LLVM and Clang. {\bf \_\_code} is similar to an attribute of a function, which means that the function can only be called in tail call elimination. Because of this implementation, code segments can actually be called as C function calls.

Forcing a tail call requires many conditions be met. For example, there should not be a statement after a tail call, the caller and callee's calling conventions must be the same and their types should be cc10, cc11 or fastcc and  the callee's return value type has to be the same as the caller's.

All code segments have the void return type and writing statements after continuation is not allowed. As a result, type problems and after statement problems are solved.

Tail call elimination passes are enabled in {\bf BackendUtil.cpp}. In Clang, when the optimization level is two or more, tail call elimination passing is enable. Here it has been modified to be enabled anytime, however if the optimization level is one or less, tail call elimination passes only work for code segments. 
A calling convention problem was also solved. fastcc was selected for a code segment's calling convention. In Clang, calling conventions are managed by the CGFunctionInfo class and its information is set in {\bf CGCall.ccp} ( a part of CodeGen ), which is where code segments calling conventions were set to fastcc. 

Goto with environment is implemented by code rearranging. If the {\bf \_\_environment} or {\bf \_\_return} is declared, the CbC compiler rearranges the code for goto with environment. Setjmp and longjmp are used to do this. setjmp to save the environment before continuation and longjmp to restore it. 

\section{Result}
Table \ref{src:example} shows the benchmark program.

\begin{table}[html]
\begin{lstlisting}
int f0(int i) {
  int k,j;
  k = 3+i;
  j = g0(i+3);
  return k+4+j;
}

int g0(int i) {
  return h0(i+4)+i;
}

int h0(int i) {
  return i+4;
}
\end{lstlisting}
\caption{benchmark program in C}
\label{src:example}
\end{table}

Fig.\ref{src:example} is a normal C program. We can rewrite this program into CbC in several way, \verb+conv1,conv2,conv3+.
Basicaly function call is emulated by goto statement with explicit stack. \verb+conv2,conv3+ uses extra
argument to eliminate these stacks.  
The CbC \verb+conv3+ source is shown in fig.\ref{src:cbc}
Using this benchmark, function call overhead become visible.
In order to see the overhead, inline function expansion is prohibited.
The benchmark results are shown in TABLE \ref{result}.

\begin{table}[html]
\begin{lstlisting}

struct cont_interface { // General Return Continuation
    __code (*ret)();
};

__code f2_1(int i,char *sp) {
    int k,j;
    k = 3+i;
    goto g2_1(k,i+3,sp);
}

__code g2_1(int k,int i,char *sp) {
    goto h2_11(k,i+4,sp);
}

__code h2_1_1(int i,int k,int j,char *sp) {
    goto f2_0_1(k,i+j,sp);
}

__code h2_11(int i,int k,char *sp) {
    goto h2_1_1(i,k,i+4,sp);
}

__code f2_0_1(int k,int j,char *sp) {
    goto (( (struct cont_interface *)sp)->ret)(k+4+j,sp);
}

\end{lstlisting}
\caption{benchmark program conv3 in CbC}
\label{src:cbc}
\end{table}

\begin{table}[htpb]
  \centering
  \begin{tabular}{|l|r|r|r|} \hline
    & conv1 & conv2 & conv3 \\ \hline
    Micro-C  & 6.875 & 2.4562 & 3.105 \\ \hline
    GCC -O2 & 2.9438 & 0.955 & 1.265  \\ \hline
    LLVM/clang -O0 & 5.835 & 4.1887 & 5.0625 \\ \hline
    LLVM/clang -O2 & 3.3875 & 2.29 & 2.5087 \\ \hline
  \end{tabular}
  \caption{Execution time(s)}
  \label{result}
\end{table} 

LLVM and Clang compilers are faster than Micro-C when optimization is enabled. This means CbC get benefits from LLVM optimizations. The LLVM and Clang complier is slower than GCC, but GCC cannot compile safely without optimization. This means LLVM can compile more reliably than GCC.

\section{Conclusion}
This Continuation based language has been designed and implemented for practical use. CbC has been partially implemented using LLVM and Clang 3.7.
CbC can use LLVM's optimization. LLVM IR was not modified to implement CbC's compiler.

In the future, data segments, meta code segments and meta data segments for meta computation will be designed and implemented. 
\nocite{opac-b1092711, LLVMIR, LLVM, clang}
\bibliographystyle{IEEEtran}
\bibliography{IEEEabrv,reference}



% that's all folks
\end{document}