1*03ce13f7SAndroid Build Coastguard WorkerSubzero - Fast code generator for PNaCl bitcode 2*03ce13f7SAndroid Build Coastguard Worker=============================================== 3*03ce13f7SAndroid Build Coastguard Worker 4*03ce13f7SAndroid Build Coastguard WorkerDesign 5*03ce13f7SAndroid Build Coastguard Worker------ 6*03ce13f7SAndroid Build Coastguard Worker 7*03ce13f7SAndroid Build Coastguard WorkerSee the accompanying DESIGN.rst file for a more detailed technical overview of 8*03ce13f7SAndroid Build Coastguard WorkerSubzero. 9*03ce13f7SAndroid Build Coastguard Worker 10*03ce13f7SAndroid Build Coastguard WorkerBuilding 11*03ce13f7SAndroid Build Coastguard Worker-------- 12*03ce13f7SAndroid Build Coastguard Worker 13*03ce13f7SAndroid Build Coastguard WorkerSubzero is set up to be built within the Native Client tree. Follow the 14*03ce13f7SAndroid Build Coastguard Worker`Developing PNaCl 15*03ce13f7SAndroid Build Coastguard Worker<https://sites.google.com/a/chromium.org/dev/nativeclient/pnacl/developing-pnacl>`_ 16*03ce13f7SAndroid Build Coastguard Workerinstructions, in particular the section on building PNaCl sources. This will 17*03ce13f7SAndroid Build Coastguard Workerprepare the necessary external headers and libraries that Subzero needs. 18*03ce13f7SAndroid Build Coastguard WorkerChecking out the Native Client project also gets the pre-built clang and LLVM 19*03ce13f7SAndroid Build Coastguard Workertools in ``native_client/../third_party/llvm-build/Release+Asserts/bin`` which 20*03ce13f7SAndroid Build Coastguard Workerare used for building Subzero. 21*03ce13f7SAndroid Build Coastguard Worker 22*03ce13f7SAndroid Build Coastguard WorkerThe Subzero source is in ``native_client/toolchain_build/src/subzero``. From 23*03ce13f7SAndroid Build Coastguard Workerwithin that directory, ``git checkout master && git pull`` to get the latest 24*03ce13f7SAndroid Build Coastguard Workerversion of Subzero source code. 25*03ce13f7SAndroid Build Coastguard Worker 26*03ce13f7SAndroid Build Coastguard WorkerThe Makefile is designed to be used as part of the higher level LLVM build 27*03ce13f7SAndroid Build Coastguard Workersystem. To build manually, use the ``Makefile.standalone``. There are several 28*03ce13f7SAndroid Build Coastguard Workerbuild configurations from the command line:: 29*03ce13f7SAndroid Build Coastguard Worker 30*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone 31*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone DEBUG=1 32*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone NOASSERT=1 33*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone DEBUG=1 NOASSERT=1 34*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone MINIMAL=1 35*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone ASAN=1 36*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone TSAN=1 37*03ce13f7SAndroid Build Coastguard Worker 38*03ce13f7SAndroid Build Coastguard Worker``DEBUG=1`` builds without optimizations and is good when running the translator 39*03ce13f7SAndroid Build Coastguard Workerinside a debugger. ``NOASSERT=1`` disables assertions and is the preferred 40*03ce13f7SAndroid Build Coastguard Workerconfiguration for performance testing the translator. ``MINIMAL=1`` attempts to 41*03ce13f7SAndroid Build Coastguard Workerminimize the size of the translator by compiling out everything unnecessary. 42*03ce13f7SAndroid Build Coastguard Worker``ASAN=1`` enables AddressSanitizer, and ``TSAN=1`` enables ThreadSanitizer. 43*03ce13f7SAndroid Build Coastguard Worker 44*03ce13f7SAndroid Build Coastguard WorkerThe result of the ``make`` command is the target ``pnacl-sz`` in the current 45*03ce13f7SAndroid Build Coastguard Workerdirectory. 46*03ce13f7SAndroid Build Coastguard Worker 47*03ce13f7SAndroid Build Coastguard WorkerBuilding within LLVM trunk 48*03ce13f7SAndroid Build Coastguard Worker-------------------------- 49*03ce13f7SAndroid Build Coastguard Worker 50*03ce13f7SAndroid Build Coastguard WorkerSubzero can also be built from within a standard LLVM trunk checkout. Here is 51*03ce13f7SAndroid Build Coastguard Workeran example of how it can be checked out and built:: 52*03ce13f7SAndroid Build Coastguard Worker 53*03ce13f7SAndroid Build Coastguard Worker mkdir llvm-git 54*03ce13f7SAndroid Build Coastguard Worker cd llvm-git 55*03ce13f7SAndroid Build Coastguard Worker git clone http://llvm.org/git/llvm.git 56*03ce13f7SAndroid Build Coastguard Worker cd llvm/projects/ 57*03ce13f7SAndroid Build Coastguard Worker git clone https://chromium.googlesource.com/native_client/pnacl-subzero 58*03ce13f7SAndroid Build Coastguard Worker cd ../.. 59*03ce13f7SAndroid Build Coastguard Worker mkdir build 60*03ce13f7SAndroid Build Coastguard Worker cd build 61*03ce13f7SAndroid Build Coastguard Worker cmake -G Ninja ../llvm/ 62*03ce13f7SAndroid Build Coastguard Worker ninja 63*03ce13f7SAndroid Build Coastguard Worker ./bin/pnacl-sz -version 64*03ce13f7SAndroid Build Coastguard Worker 65*03ce13f7SAndroid Build Coastguard WorkerThis creates a default build of ``pnacl-sz``; currently any options such as 66*03ce13f7SAndroid Build Coastguard Worker``DEBUG=1`` or ``MINIMAL=1`` have to be added manually. 67*03ce13f7SAndroid Build Coastguard Worker 68*03ce13f7SAndroid Build Coastguard Worker``pnacl-sz`` 69*03ce13f7SAndroid Build Coastguard Worker------------ 70*03ce13f7SAndroid Build Coastguard Worker 71*03ce13f7SAndroid Build Coastguard WorkerThe ``pnacl-sz`` program parses a pexe or an LLVM bitcode file and translates it 72*03ce13f7SAndroid Build Coastguard Workerinto ICE (Subzero's intermediate representation). It then invokes the ICE 73*03ce13f7SAndroid Build Coastguard Workertranslate method to lower it to target-specific machine code, optionally dumping 74*03ce13f7SAndroid Build Coastguard Workerthe intermediate representation at various stages of the translation. 75*03ce13f7SAndroid Build Coastguard Worker 76*03ce13f7SAndroid Build Coastguard WorkerThe program can be run as follows:: 77*03ce13f7SAndroid Build Coastguard Worker 78*03ce13f7SAndroid Build Coastguard Worker ../pnacl-sz ./path/to/<file>.pexe 79*03ce13f7SAndroid Build Coastguard Worker ../pnacl-sz ./tests_lit/pnacl-sz_tests/<file>.ll 80*03ce13f7SAndroid Build Coastguard Worker 81*03ce13f7SAndroid Build Coastguard WorkerAt this time, ``pnacl-sz`` accepts a number of arguments, including the 82*03ce13f7SAndroid Build Coastguard Workerfollowing: 83*03ce13f7SAndroid Build Coastguard Worker 84*03ce13f7SAndroid Build Coastguard Worker ``-help`` -- Show available arguments and possible values. (Note: this 85*03ce13f7SAndroid Build Coastguard Worker unfortunately also pulls in some LLVM-specific options that are reported but 86*03ce13f7SAndroid Build Coastguard Worker that Subzero doesn't use.) 87*03ce13f7SAndroid Build Coastguard Worker 88*03ce13f7SAndroid Build Coastguard Worker ``-notranslate`` -- Suppress the ICE translation phase, which is useful if 89*03ce13f7SAndroid Build Coastguard Worker ICE is missing some support. 90*03ce13f7SAndroid Build Coastguard Worker 91*03ce13f7SAndroid Build Coastguard Worker ``-target=<TARGET>`` -- Set the target architecture. The default is x8632. 92*03ce13f7SAndroid Build Coastguard Worker Future targets include x8664, arm32, and arm64. 93*03ce13f7SAndroid Build Coastguard Worker 94*03ce13f7SAndroid Build Coastguard Worker ``-filetype=obj|asm|iasm`` -- Select the output file type. ``obj`` is a 95*03ce13f7SAndroid Build Coastguard Worker native ELF file, ``asm`` is a textual assembly file, and ``iasm`` is a 96*03ce13f7SAndroid Build Coastguard Worker low-level textual assembly file demonstrating the integrated assembler. 97*03ce13f7SAndroid Build Coastguard Worker 98*03ce13f7SAndroid Build Coastguard Worker ``-O<LEVEL>`` -- Set the optimization level. Valid levels are ``2``, ``1``, 99*03ce13f7SAndroid Build Coastguard Worker ``0``, ``-1``, and ``m1``. Levels ``-1`` and ``m1`` are synonyms, and 100*03ce13f7SAndroid Build Coastguard Worker represent the minimum optimization and worst code quality, but fastest code 101*03ce13f7SAndroid Build Coastguard Worker generation. 102*03ce13f7SAndroid Build Coastguard Worker 103*03ce13f7SAndroid Build Coastguard Worker ``-verbose=<list>`` -- Set verbosity flags. This argument allows a 104*03ce13f7SAndroid Build Coastguard Worker comma-separated list of values. The default is ``none``, and the value 105*03ce13f7SAndroid Build Coastguard Worker ``inst,pred`` will roughly match the .ll bitcode file. Of particular use 106*03ce13f7SAndroid Build Coastguard Worker are ``all``, ``most``, and ``none``. 107*03ce13f7SAndroid Build Coastguard Worker 108*03ce13f7SAndroid Build Coastguard Worker ``-o <FILE>`` -- Set the assembly output file name. Default is stdout. 109*03ce13f7SAndroid Build Coastguard Worker 110*03ce13f7SAndroid Build Coastguard Worker ``-log <FILE>`` -- Set the file name for diagnostic output (whose level is 111*03ce13f7SAndroid Build Coastguard Worker controlled by ``-verbose``). Default is stdout. 112*03ce13f7SAndroid Build Coastguard Worker 113*03ce13f7SAndroid Build Coastguard Worker ``-timing`` -- Dump some pass timing information after translating the input 114*03ce13f7SAndroid Build Coastguard Worker file. 115*03ce13f7SAndroid Build Coastguard Worker 116*03ce13f7SAndroid Build Coastguard WorkerRunning the test suite 117*03ce13f7SAndroid Build Coastguard Worker---------------------- 118*03ce13f7SAndroid Build Coastguard Worker 119*03ce13f7SAndroid Build Coastguard WorkerSubzero uses the LLVM ``lit`` testing tool for part of its test suite, which 120*03ce13f7SAndroid Build Coastguard Workerlives in ``tests_lit``. To execute the test suite, first build Subzero, and then 121*03ce13f7SAndroid Build Coastguard Workerrun:: 122*03ce13f7SAndroid Build Coastguard Worker 123*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone check-lit 124*03ce13f7SAndroid Build Coastguard Worker 125*03ce13f7SAndroid Build Coastguard WorkerThere is also a suite of cross tests in the ``crosstest`` directory. A cross 126*03ce13f7SAndroid Build Coastguard Workertest takes a test bitcode file implementing some unit tests, and translates it 127*03ce13f7SAndroid Build Coastguard Workertwice, once with Subzero and once with LLVM's known-good ``llc`` translator. 128*03ce13f7SAndroid Build Coastguard WorkerThe Subzero-translated symbols are specially mangled to avoid multiple 129*03ce13f7SAndroid Build Coastguard Workerdefinition errors from the linker. Both translated versions are linked together 130*03ce13f7SAndroid Build Coastguard Workerwith a driver program that calls each version of each unit test with a variety 131*03ce13f7SAndroid Build Coastguard Workerof interesting inputs and compares the results for equality. The cross tests 132*03ce13f7SAndroid Build Coastguard Workerare currently invoked by running:: 133*03ce13f7SAndroid Build Coastguard Worker 134*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone check-xtest 135*03ce13f7SAndroid Build Coastguard Worker 136*03ce13f7SAndroid Build Coastguard WorkerSimilar, there is a suite of unit tests:: 137*03ce13f7SAndroid Build Coastguard Worker 138*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone check-unit 139*03ce13f7SAndroid Build Coastguard Worker 140*03ce13f7SAndroid Build Coastguard WorkerA convenient way to run the lit, cross, and unit tests is:: 141*03ce13f7SAndroid Build Coastguard Worker 142*03ce13f7SAndroid Build Coastguard Worker make -f Makefile.standalone check 143*03ce13f7SAndroid Build Coastguard Worker 144*03ce13f7SAndroid Build Coastguard WorkerAssembling ``pnacl-sz`` output as needed 145*03ce13f7SAndroid Build Coastguard Worker---------------------------------------- 146*03ce13f7SAndroid Build Coastguard Worker 147*03ce13f7SAndroid Build Coastguard Worker``pnacl-sz`` can now produce a native ELF binary using ``-filetype=obj``. 148*03ce13f7SAndroid Build Coastguard Worker 149*03ce13f7SAndroid Build Coastguard Worker``pnacl-sz`` can also produce textual assembly code in a structure suitable for 150*03ce13f7SAndroid Build Coastguard Workerinput to ``llvm-mc``, using ``-filetype=asm`` or ``-filetype=iasm``. An object 151*03ce13f7SAndroid Build Coastguard Workerfile can then be produced using the command:: 152*03ce13f7SAndroid Build Coastguard Worker 153*03ce13f7SAndroid Build Coastguard Worker llvm-mc -triple=i686 -filetype=obj -o=MyObj.o 154*03ce13f7SAndroid Build Coastguard Worker 155*03ce13f7SAndroid Build Coastguard WorkerBuilding a translated binary 156*03ce13f7SAndroid Build Coastguard Worker---------------------------- 157*03ce13f7SAndroid Build Coastguard Worker 158*03ce13f7SAndroid Build Coastguard WorkerThere is a helper script, ``pydir/szbuild.py``, that translates a finalized pexe 159*03ce13f7SAndroid Build Coastguard Workerinto a fully linked executable. Run it with ``-help`` for extensive 160*03ce13f7SAndroid Build Coastguard Workerdocumentation. 161*03ce13f7SAndroid Build Coastguard Worker 162*03ce13f7SAndroid Build Coastguard WorkerBy default, ``szbuild.py`` builds an executable using only Subzero translation, 163*03ce13f7SAndroid Build Coastguard Workerbut it can also be used to produce hybrid Subzero/``llc`` binaries (``llc`` is 164*03ce13f7SAndroid Build Coastguard Workerthe name of the LLVM translator) for bisection-based debugging. In bisection 165*03ce13f7SAndroid Build Coastguard Workerdebugging mode, the pexe is translated using both Subzero and ``llc``, and the 166*03ce13f7SAndroid Build Coastguard Workerresulting object files are combined into a single executable using symbol 167*03ce13f7SAndroid Build Coastguard Workerweakening and other linker tricks to control which Subzero symbols and which 168*03ce13f7SAndroid Build Coastguard Worker``llc`` symbols take precedence. This is controlled by the ``-include`` and 169*03ce13f7SAndroid Build Coastguard Worker``-exclude`` arguments. These can be used to rapidly find a single function 170*03ce13f7SAndroid Build Coastguard Workerthat Subzero translates incorrectly leading to incorrect output. 171*03ce13f7SAndroid Build Coastguard Worker 172*03ce13f7SAndroid Build Coastguard WorkerThere is another helper script, ``pydir/szbuild_spec2k.py``, that runs 173*03ce13f7SAndroid Build Coastguard Worker``szbuild.py`` on one or more components of the Spec2K suite. This assumes that 174*03ce13f7SAndroid Build Coastguard WorkerSpec2K is set up in the usual place in the Native Client tree, and the finalized 175*03ce13f7SAndroid Build Coastguard Workerpexe files have been built. (Note: for working with Spec2K and other pexes, 176*03ce13f7SAndroid Build Coastguard Workerit's helpful to finalize the pexe using ``--no-strip-syms``, to preserve the 177*03ce13f7SAndroid Build Coastguard Workeroriginal function and global variable names.) 178*03ce13f7SAndroid Build Coastguard Worker 179*03ce13f7SAndroid Build Coastguard WorkerStatus 180*03ce13f7SAndroid Build Coastguard Worker------ 181*03ce13f7SAndroid Build Coastguard Worker 182*03ce13f7SAndroid Build Coastguard WorkerSubzero currently fully supports the x86-32 architecture, for both native and 183*03ce13f7SAndroid Build Coastguard WorkerNative Client sandboxing modes. The x86-64 architecture is also supported in 184*03ce13f7SAndroid Build Coastguard Workernative mode only, and only for the x32 flavor due to the fact that pointers and 185*03ce13f7SAndroid Build Coastguard Worker32-bit integers are indistinguishable in PNaCl bitcode. Sandboxing support for 186*03ce13f7SAndroid Build Coastguard Workerx86-64 is in progress. ARM and MIPS support is in progress. Two optimization 187*03ce13f7SAndroid Build Coastguard Workerlevels, ``-Om1`` and ``-O2``, are implemented. 188*03ce13f7SAndroid Build Coastguard Worker 189*03ce13f7SAndroid Build Coastguard WorkerThe ``-Om1`` configuration is designed to be the simplest and fastest possible, 190*03ce13f7SAndroid Build Coastguard Workerwith a minimal set of passes and transformations. 191*03ce13f7SAndroid Build Coastguard Worker 192*03ce13f7SAndroid Build Coastguard Worker* Simple Phi lowering before target lowering, by generating temporaries and 193*03ce13f7SAndroid Build Coastguard Worker adding assignments to the end of predecessor blocks. 194*03ce13f7SAndroid Build Coastguard Worker 195*03ce13f7SAndroid Build Coastguard Worker* Simple register allocation limited to pre-colored or infinite-weight 196*03ce13f7SAndroid Build Coastguard Worker Variables. 197*03ce13f7SAndroid Build Coastguard Worker 198*03ce13f7SAndroid Build Coastguard WorkerThe ``-O2`` configuration is designed to use all optimizations available and 199*03ce13f7SAndroid Build Coastguard Workerproduce the best code. 200*03ce13f7SAndroid Build Coastguard Worker 201*03ce13f7SAndroid Build Coastguard Worker* Address mode inference to leverage the complex x86 addressing modes. 202*03ce13f7SAndroid Build Coastguard Worker 203*03ce13f7SAndroid Build Coastguard Worker* Compare/branch fusing based on liveness/last-use analysis. 204*03ce13f7SAndroid Build Coastguard Worker 205*03ce13f7SAndroid Build Coastguard Worker* Global, linear-scan register allocation. 206*03ce13f7SAndroid Build Coastguard Worker 207*03ce13f7SAndroid Build Coastguard Worker* Advanced phi lowering after target lowering and global register allocation, 208*03ce13f7SAndroid Build Coastguard Worker via edge splitting, topological sorting of the parallel moves, and final local 209*03ce13f7SAndroid Build Coastguard Worker register allocation. 210*03ce13f7SAndroid Build Coastguard Worker 211*03ce13f7SAndroid Build Coastguard Worker* Stack slot coalescing to reduce frame size. 212*03ce13f7SAndroid Build Coastguard Worker 213*03ce13f7SAndroid Build Coastguard Worker* Branch optimization to reduce the number of branches to the following block. 214