Camil Demetrescu edited osr-llvm.tex  over 8 years ago

Commit id: 93472ed1da67dd8969b7df711d2e17258d8d04e4

deletions | additions      

       

\section{OSR in LLVM}  \label{se:osr-llvm}  In this section we discuss an LLVM implementation of the approach described in \mysection\ref{se:overview}. Our discussion is based on a set of abstractions to support OSR instrumentation of IR code, which we organized in an LLVM-based library for VM builders called \osrkit. A simplified overview of the main building blocks is:  \begin{itemize}  \item {\em StateMapper}: a class that [...]  \item {\em InsertResolvedOSR(f1, b1, f2, b2, cond, m)}: function that inserts a resolved OSR point in function {\em f1} at basic block {\em b1} to function {\em f2} at basic block {\em b2}, using as OSR condition a sequence of IR instructions {\em cond} and a state mapper {\em m}.  \item {\em InsertOpenOSR()}: [...]  \item {\em GenerateOSRContFun(...)}: generates the continuation function [...]  \end{itemize}  % ====> The following text goes in the artifact  % In this section we discuss our implementation of the approach described in \mysection\ref{se:overview} in \tinyvm, a proof-of-concept virtual machine we developed as a playground to exercise our OSR techniques. TinyVM is based on LLVM's MCJIT compiler and supports interactive invocation of LLVM IR functions either generated at run-time or loaded from disk. The main design goal behind TinyVM is the creation of an interactive environment for IR manipulation and JIT-compilation of functions: for instance, it allows the user to insert OSR points in loaded functions, run optimization passes on them or display their CFGs, repeatedly invoke a function for a specified amount of times and so on. TinyVM supports dynamic library loading and linking, and comes with a helper component for MCJIT that simplifies tasks such as handling multiple IR modules, symbol resolution in presence of multiple versions of a function, and tracking native code and other machine-level generated object such as Stackmaps. 

%The final step to be performed before execution is native code generation.   \myfigure\ref{fig:isordx86-64} shows the x86-64 code generated by LLVM for {\tt isordfrom} and {\tt isordto}. For the sake of comparison with the native code that would be generated for the original non-OSR versions, additions resulting from the IR instrumentation are in grey, while removals are struck-through. Notice that the OSR intrusiveness in {\tt isordfrom} is minimal, consisting of just two assembly instructions with register and immediate operands. As a result of induction variable canonicalization in the LLVM back-end, loop index {\tt i} and hotness counter {\tt p.osr} are fused in register {\tt\%r12}. We also note that tail call optimization is applied in the OSR-firing block, resulting in no stack growth during an OSR. The continuation function {\tt isordto} is identical to the optimized version of {\tt isord}, except that the loop index is passed as a parameter in {\tt \%rdx} and no loop pre-header is needed since OSR jumps directly in the loop body.  \subsection{\osrkit\ %\subsection{\osrkit\  Instrumentation API} \label{se:instrum-api}  To %\label{se:instrum-api}  %To  support the IR instrumentation tasks of \mysection\ref{se:overview}, \osrkit\ provides a number of abstractions for VM builders.A simplified overview of the main building blocks is:  \begin{itemize}  \item {\em StateMapper}: a class that [...]  \item {\em InsertResolvedOSR(f1, b1, f2, b2, cond, m)}: function that inserts a resolved OSR point in function {\em f1} at basic block {\em b1} to function {\em f2} at basic block {\em b2}, using as OSR condition a sequence of IR instructions {\em cond} and a state mapper {\em m}.  \item {\em InsertOpenOSR()}: [...]  \item {\em GenerateOSRContFun(...)}: generates the continuation function [...]  \end{itemize}    %To place an open OSR, \tinyvm\ requires the following pieces of information: 1) a pointer to the  %LLVMContext& Context, OSRLibrary::OpenOSRInfo& info,  % OSRLibrary::OSRCond& cond, Value* profDataVal, OSRLibrary::DestFunGenerator destFunGenerator,  % std::vector *valuesToTransfer, OSRLibrary::OSRPointConfig &config  %Function* src, BasicBlock* src_bb, std::string* F1NewName, OSRLibrary::OSRCond &cond, int branchTakenProb  %OSR instrumentation produces function {\tt isordfrom} obtained from {\tt isord} by including   %includes extra code (in grey) that increments a counter ,   %The generated stub calls a function inliner that generates a version of {\tt isord} where the comparator's body, pointed to by live variable {\tt c_osr}, is inlined in the loop body. The optimized version {\tt isordto} is shown in \myfigure\ref{fig:isordascto}.  %\subsection{Resolved OSR Points}  %\subsection{Open OSR Points}