Compiling and Optimizing Scripting Languages

Document Sample
Compiling and Optimizing Scripting Languages Powered By Docstoc
					                                                                                    Introduction to phc
                                                                                  Current state of phc
                                                              Next for phc - Analysis and Optimization
                                                                                Experiences with PHP




Compiling and Optimizing Scripting Languages

            Paul Biggar and David Gregg

         Department of Computer Science and Statistics
                    Trinity College Dublin
                                                         Compiling and Optimizing Scripting Languages
              Google, 18th March, 2009

                                                                              Paul Biggar and David Gregg

                                                                          Department of Computer Science and Statistics
                                                                                     Trinity College Dublin


                                                                                 Google, 18th March, 2009




                                                                                                          Trinity College Dublin   1
                                                                                            Introduction to phc
                                                                                          Current state of phc
                                                                      Next for phc - Analysis and Optimization
                    Motivation                                                          Experiences with PHP


                                                              Motivation
                         User needs web page in 0.5 seconds
                             Execution time
1. dont have to              DB access
                             Network latency
   obduscate your            Browser rendering
   code for                                                        User needs web page in 0.5 seconds
                         Easier maintainance
   performance                                                         Execution time
                         What if execution was:                        DB access
                             2x as fast?
                             10x as fast?
                                                                       Network latency
                                                                       Browser rendering

                                                                   Easier maintainance

                                                                   What if execution was:
                                                                       2x as fast?
                                                                       10x as fast?



                                                                                                                  Trinity College Dublin   2
                                                                                 Introduction to phc
                                                                               Current state of phc
                                                           Next for phc - Analysis and Optimization
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   3
                                                                                 Introduction to phc
                                                                               Current state of phc
                                                           Next for phc - Analysis and Optimization
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   4
                                                                                                         Introduction to phc
                                                                                                       Current state of phc
                                                                                   Next for phc - Analysis and Optimization
                        phc                                                                          Experiences with PHP


                                                                          phc
1. BSD licence useful
                              http://phpcompiler.org
   since its easy to
                              Ahead-of-time compiler for PHP
   extend                     Edsko de Vries, John Gilbert, Paul Biggar
2. Well engineered -          BSD license
   turns out you dont         Latest release: 0.2.0.3 - compiles non-OO
                                                                                http://phpcompiler.org
   get a phd for that         svn trunk: compiles most OO
                                                                                Ahead-of-time compiler for PHP
                                                                                Edsko de Vries, John Gilbert, Paul Biggar
                                                                                BSD license
                                                                                Latest release: 0.2.0.3 - compiles non-OO
                                                                                svn trunk: compiles most OO




                                                                                                                               Trinity College Dublin   5
                                                                      Introduction to phc
                                                                    Current state of phc
                                                Next for phc - Analysis and Optimization
                     Structure of phc                             Experiences with PHP


                                        Structure of phc

1. All can be
   unparsed to PHP




                                                                                            Trinity College Dublin   6
                                                             Introduction to phc
                                                           Current state of phc
                                       Next for phc - Analysis and Optimization
PHP                                                      Experiences with PHP


                                 PHP

      <?php

       echo "hello", "world!";

      ?>

                                                   <?php

                                                        echo "hello", "world!";

                                                   ?>




                                                                                   Trinity College Dublin   7
                                                                                                                                                              Introduction to phc
                                                                                                                                                            Current state of phc
                                                                                                                                        Next for phc - Analysis and Optimization
                         AST                                                                                                                              Experiences with PHP


                                                                                                                                  AST
                                                           PHP_script




                                                         List<Statement>


                                                                                                                                                                                     PHP_script
                                              Eval_expr (3)             Nop (5)

1. better than parse
   (“concrete syntax”)                    Method_invocation (3)

                                                                                                                                                                                   List<Statement>
   tree                         NULL
                               (Target)
                                          METHOD_NAME (3)                         List<Actual_parameter>




                                                  echo                     Actual_parameter (3)            Actual_parameter (3)                                         Eval_expr (3)             Nop (5)


                                                                              STRING (3)                      STRING (3)


                                                                                                                                                                    Method_invocation (3)
                                                                                    hello                        world!




                                                                                                                                                       NULL
                                                                                                                                                                    METHOD_NAME (3)                         List<Actual_parameter>
                                                                                                                                                      (Target)



                                                                                                                                                                            echo                     Actual_parameter (3)            Actual_parameter (3)




                                                                                                                                                                                                        STRING (3)                      STRING (3)




                                                                                                                                                                                                              hello                        world!


                                                                                                                                                                                             Trinity College Dublin                                         8
                                                                             Introduction to phc
                                                                           Current state of phc
                                                       Next for phc - Analysis and Optimization
               HIR                                                       Experiences with PHP


                                                 HIR
                     <?php
                       $x = $a + $b + $c + $d;
                     ?>
1. 3AC
2. Still PHP                                                       <?php
                     <?php
                       $TLE0 = ($a + $b);
                                                                     $x = $a + $b + $c + $d;
                       $TLE1 = ($TLE0 + $c);                       ?>
                       $x = ($TLE1 + $d);
                     ?>




                                                                   <?php
                                                                     $TLE0 = ($a + $b);
                                                                     $TLE1 = ($TLE0 + $c);
                                                                     $x = ($TLE1 + $d);
                                                                   ?>


                                                                                                   Trinity College Dublin   9
                                                                                    Introduction to phc
                                                                                  Current state of phc
                                                              Next for phc - Analysis and Optimization
             MIR                                                                Experiences with PHP

                   <?php
                     while ($cond)
                                                        MIR
                       echo "hello", "world!";
                   ?>
                                                                          <?php
1. Not PHP         <?php
                                                                            while ($cond)
                   L7:
2. Gotos             $TLE0 = !$cond;                                          echo "hello", "world!";
                     if ($TLE0) goto L3 else goto L6;
                   L6:                                                    ?>
                     print(’hello’);
                     print(’world!’);
                     goto L7;                                             <?php
                   L3:
                   ?>
                                                                          L7:
                                                                            $TLE0 = !$cond;
                                                                            if ($TLE0) goto L3 else goto L6;
                                                                          L6:
                                                                            print(’hello’);
                                                                            print(’world!’);
                                                                            goto L7;
                                                                          L3:
                                                                          ?>
                                                                                                          Trinity College Dublin   10
                                                                                                                   Introduction to phc
                                                                                                                 Current state of phc
                                                                                             Next for phc - Analysis and Optimization
                      Plugins                                                                                  Experiences with PHP


                                                                                   Plugins
1. Visitor pattern
2. We use it for                http://phpcompiler.org/doc/latest/devmanual.html
   testing a lot
3. Manual documents
   it well
4.                                                                                                          http://phpcompiler.org/doc/latest/devmanual.html




                                                                                                                                         Trinity College Dublin   11
                                                                                                    Introduction to phc
                                                                                                  Current state of phc
                                                                              Next for phc - Analysis and Optimization
XML                                                                                             Experiences with PHP
      <?xml version="1.0"?>
      <AST:PHP_script xmlns:AST="http://www.phpcompiler.org/phc-1.1">
        <AST:Statement_list>
          <AST:Eval_expr>
                                                                        XML
            <AST:Method_invocation>
              <AST:Target xsi:nil="true" />
              <AST:METHOD_NAME>                                                                 <?xml version="1.0"?>
                <value>echo</value>
              </AST:METHOD_NAME>                                                                <AST:PHP_script xmlns:AST="http://www.phpcompiler.org/phc-1.1">
              <AST:Actual_parameter_list>
                <AST:Actual_parameter>
                                                                                                  <AST:Statement_list>
                  <bool><!-- is_ref -->false</bool>                                                 <AST:Eval_expr>
                  <AST:STRING>
                    <value>hello</value>                                                              <AST:Method_invocation>
                  </AST:STRING>
                </AST:Actual_parameter>
                                                                                                        <AST:Target xsi:nil="true" />
                <AST:Actual_parameter>                                                                  <AST:METHOD_NAME>
                  <bool><!-- is_ref -->false</bool>
                  <AST:STRING>                                                                            <value>echo</value>
                    <value>world!</value>
                  </AST:STRING>
                                                                                                        </AST:METHOD_NAME>
                </AST:Actual_parameter>                                                                 <AST:Actual_parameter_list>
              </AST:Actual_parameter_list>
            </AST:Method_invocation>                                                                      <AST:Actual_parameter>
          </AST:Eval_expr>
          <AST:Nop>
                                                                                                            <bool><!-- is_ref -->false</bool>
          </AST:Nop>                                                                                        <AST:STRING>
        </AST:Statement_list>
      </AST:PHP_script>                                                                                       <value>hello</value>
                                                                                                            </AST:STRING>
                                                                                                          </AST:Actual_parameter>
                                                                                                          <AST:Actual_parameter>
                                                                                                            <bool><!-- is_ref -->false</bool>
                                                                                                            <AST:STRING>
                                                                                                              <value>world!</value>
                                                                                                            </AST:STRING>
                                                                                                          </AST:Actual_parameter>
                                                                                                        </AST:Actual_parameter_list>
                                                                                                      </AST:Method_invocation>
                                                                                                    </AST:Eval_expr>
                                                                                                    <AST:Nop>
                                                                                                    </AST:Nop>
                                                                                                  </AST:Statement_list>
                                                                                                </AST:PHP_script>
                                                                                                                      Trinity College Dublin                      12
                                                                                 Introduction to phc
                                                                                                       Challenges to compilation?
                                                                               Current state of phc
                                                                                                       phc solution: use the C API
                                                           Next for phc - Analysis and Optimization
Outline                                                                                                Speedup
                                                                             Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin        13
                                                                                                                 Introduction to phc
                                                                                                                                       Challenges to compilation?
                                                                                                               Current state of phc
                                                                                                                                       phc solution: use the C API
                                                                                           Next for phc - Analysis and Optimization
                       SAC 2009                                                                                                        Speedup
                                                                                                             Experiences with PHP


                                                                                    SAC 2009
                           A Practical Solution for Scripting Language
1.   Correctness                            Compilers
2.   Large libraries
3.   Odd features
4.   No spec
                               Paul Biggar, Edsko de Vries and David Gregg
                                                                                        A Practical Solution for Scripting Language
                                    Department of Computer Science and Statistics
                                               Trinity College Dublin
                                                                                                         Compilers
                             ACM Symposium on Applied Computing - PL track
                                         12th March, 2009

                                                                                            Paul Biggar, Edsko de Vries and David Gregg

                                                                                                       Department of Computer Science and Statistics
                                                                                                                  Trinity College Dublin


                                                                                          ACM Symposium on Applied Computing - PL track
                                                                                                      12th March, 2009


                                                                                                                                       Trinity College Dublin        14
                                                                                          Introduction to phc
                                                                                                                Challenges to compilation?
                                                                                        Current state of phc
                                                                                                                phc solution: use the C API
                                                                    Next for phc - Analysis and Optimization
Sneak peak                                                                                                      Speedup
                                                                                      Experiences with PHP


                                                             Sneak peak
    Problem: Scripting languages present “unique” problems
    (in practice)


    Solution: Re-use as much of the Canonical Reference
    Implementation as possible.
                                                                 Problem: Scripting languages present “unique” problems
                                                                 (in practice)


                                                                 Solution: Re-use as much of the Canonical Reference
                                                                 Implementation as possible.




                                                                                                                Trinity College Dublin        15
                                                                                 Introduction to phc
                                                                                                       Challenges to compilation?
                                                                               Current state of phc
                                                                                                       phc solution: use the C API
                                                           Next for phc - Analysis and Optimization
Outline                                                                                                Speedup
                                                                             Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin        16
                                                                                             Introduction to phc
                                                                                                                   Challenges to compilation?
                                                                                           Current state of phc
                                                                                                                   phc solution: use the C API
                                                                       Next for phc - Analysis and Optimization
Undefined                                                                                                           Speedup
                                                                                         Experiences with PHP


                                                               Undefined
     The PHP group claim that they have the final say in
     the specification of PHP. This group’s specification is
     an implementation, and there is no prose specification
     or agreed validation suite. There are alternate
     implementations [...] that claim to be compatible (they
     don’t say what this means) with some version of PHP.
                                                                    The PHP group claim that they have the final say in
                                                                    the specification of PHP. This group’s specification is
 D. M. Jones. Forms of language specification: Examples from
 commonly used computer languages. ISO/IEC                          an implementation, and there is no prose specification
 JTC1/SC22/OWG/N0121, February 2008.                                or agreed validation suite. There are alternate
                                                                    implementations [...] that claim to be compatible (they
                                                                    don’t say what this means) with some version of PHP.

                                                                D. M. Jones. Forms of language specification: Examples from
                                                                commonly used computer languages. ISO/IEC
                                                                JTC1/SC22/OWG/N0121, February 2008.



                                                                                                                   Trinity College Dublin        17
                                                                                                                    Introduction to phc
                                                                                                                                          Challenges to compilation?
                                                                                                                  Current state of phc
                                                                                                                                          phc solution: use the C API
                                                                                              Next for phc - Analysis and Optimization
                           Batteries included                                                                                             Speedup
                                                                                                                Experiences with PHP


1. all written in C, not                                                             Batteries included
   PHP
2. Mike Furr earlier:
   1000
   methods/classes in
   C
3. 4870 functions,
   1000 methods
                             Jeff Atwood, Coding Horror, May 20th, 2008
                             http://www.codinghorror.com/blog/archives/001119.html




                                                                                       Jeff Atwood, Coding Horror, May 20th, 2008
                                                                                       http://www.codinghorror.com/blog/archives/001119.html
                                                                                                                                          Trinity College Dublin        18
                                                            Introduction to phc
                                                                                  Challenges to compilation?
                                                          Current state of phc
                                                                                  phc solution: use the C API
                                      Next for phc - Analysis and Optimization
Change between releases                                                           Speedup
                                                        Experiences with PHP


                             Change between releases
  <?php
    var_dump (0x9fa0ff0b);
  ?>


  PHP 5.2.1 (32-bit)           <?php
  int(2147483647)
                                 var_dump (0x9fa0ff0b);
  PHP 5.2.3 (32-bit)           ?>
  float(2678128395)



                               PHP 5.2.1 (32-bit)
                               int(2147483647)

                               PHP 5.2.3 (32-bit)
                               float(2678128395)


                                                                                  Trinity College Dublin        19
                                                                                    Introduction to phc
                                                                                                          Challenges to compilation?
                                                                                  Current state of phc
                                                                                                          phc solution: use the C API
                                                              Next for phc - Analysis and Optimization
                         Run-time code generation                                                         Speedup
                                                                                Experiences with PHP

1. scripting langs are
                           <?php
                                                       Run-time code generation
   typically made for        eval ($argv[1]);
   interpreters            ?>
2. can do source
   inclusion at                                          <?php
   compile time
                           <?php                           eval ($argv[1]);
                             include ("mylib.php");
3. same mechanism            ...                         ?>
                             include ("plugin.php");
   for plugins               ...
                           ?>



                                                         <?php
                                                           include ("mylib.php");
                                                           ...
                                                           include ("plugin.php");
                                                           ...
                                                         ?>

                                                                                                          Trinity College Dublin        20
                                                                                 Introduction to phc
                                                                                                       Challenges to compilation?
                                                                               Current state of phc
                                                                                                       phc solution: use the C API
                                                           Next for phc - Analysis and Optimization
Outline                                                                                                Speedup
                                                                             Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin        21
                                                                 Introduction to phc
                                                                                       Challenges to compilation?
                                                               Current state of phc
                                                                                       phc solution: use the C API
                                           Next for phc - Analysis and Optimization
                        Use C API                                                      Speedup
                                                             Experiences with PHP


                                    Use C API
1. RTCG
2. Functions
3. Changes between
   releases: also use
   C API at
   compile-time




                                                                                       Trinity College Dublin        22
                                                                                                                          Introduction to phc
                                                                                                                                                 Challenges to compilation?
                                                                                                                        Current state of phc
                                                                                                                                                 phc solution: use the C API
                                                                                                    Next for phc - Analysis and Optimization
                          More detail                                                                                                            Speedup
                                                                                                                      Experiences with PHP


                                                                                           More detail
                                         PHP             zval
1. C API is just zval +
   macros and
                                         Python          PyObject
                                         Ruby            VALUE
   functions
2. Use (target) PHP’s                    Lua             TValue
                                                                                                                PHP                             zval
   C API at run-time
                            H. Muhammad and R. Ierusalimschy. C APIs in extension and
                            extensible languages. Journal of Universal Computer Science,
                                                                                                                Python                          PyObject
                            13(6):839–853, 2007.
                                                                                                                Ruby                            VALUE
                                                                                                                Lua                             TValue
                                                                                             H. Muhammad and R. Ierusalimschy. C APIs in extension and
                                                                                             extensible languages. Journal of Universal Computer Science,
                                                                                             13(6):839–853, 2007.


                                                                                                                                                 Trinity College Dublin        23
                                                                                  Introduction to phc
                                                                                                        Challenges to compilation?
                                                                                Current state of phc
                                                                                                        phc solution: use the C API
                                                            Next for phc - Analysis and Optimization
Simple listings: $i = 0                                                                                 Speedup
                                                                              Experiences with PHP


                                                   Simple listings: $i = 0
  // $i = 0;
  {
    zval* p_i;
    php_hash_find (LOCAL_ST, "i", 5863374, p_i);
    php_destruct (p_i);
    php_allocate (p_i);
    ZVAL_LONG (*p_i, 0);
                                                     // $i = 0;
  }                                                  {
                                                       zval* p_i;
                                                       php_hash_find (LOCAL_ST, "i", 5863374, p_i);
                                                       php_destruct (p_i);
                                                       php_allocate (p_i);
                                                       ZVAL_LONG (*p_i, 0);
                                                     }




                                                                                                        Trinity College Dublin        24
                                                                                   Introduction to phc
                                                                                                         Challenges to compilation?
                                                                                 Current state of phc
                                                                                                         phc solution: use the C API
                                                             Next for phc - Analysis and Optimization
Example: $i = 0                                                                                          Speedup
                                                                               Experiences with PHP


                                                Example: $i = 0
  // $i = 0;
  {
    if (local_i == NULL)
    {
      local_i = EG (uninitialized_zval_ptr);
      local_i->refcount++;
    }
                                                  // $i = 0;
    zval **p_lhs = &local_i;                      {
      zval *value;                                  if (local_i == NULL)
      if ((*p_lhs)->is_ref)
      {
                                                    {
        // Always overwrite the current value         local_i = EG (uninitialized_zval_ptr);
        value = *p_lhs;
        zval_dtor (value);                            local_i->refcount++;
      }
      else
                                                    }
      {                                             zval **p_lhs = &local_i;
        ALLOC_INIT_ZVAL (value);
        zval_ptr_dtor (p_lhs);
        *p_lhs = value;                               zval *value;
      }
                                                      if ((*p_lhs)->is_ref)
      ZVAL_LONG (value, 0);
  }                                                   {
                                                        // Always overwrite the current value
                                                        value = *p_lhs;
                                                        zval_dtor (value);
                                                      }
                                                      else
                                                      {
                                                        ALLOC_INIT_ZVAL (value);
                                                        zval_ptr_dtor (p_lhs);
                                                        *p_lhs = value;
                                                      }

                                                      ZVAL_LONG (value, 0);
                                                  }
                                                                                                         Trinity College Dublin        25
                                                                                                                  Introduction to phc
                                                                                                                                                Challenges to compilation?
                                                                                                                Current state of phc
                                                                                                                                                phc solution: use the C API
                                                                                            Next for phc - Analysis and Optimization
Example: $i = $j                                                                                                                                Speedup
                                                                                                              Experiences with PHP
  // $i = $j;
  {
    if (local_i == NULL)
    {
      local_i = EG (uninitialized_zval_ptr);
      local_i->refcount++;
                                                                        Example: $i = $j
    }
    zval **p_lhs = &local_i;

      zval *rhs;                                                          // $i = $j;
      if (local_j == NULL)
        rhs = EG (uninitialized_zval_ptr);                                {
      else                                                                  if (local_i == NULL)
        rhs = local_j;
                                                                            {
      if (*p_lhs != rhs)
      {                                                                       local_i = EG (uninitialized_zval_ptr);
        if ((*p_lhs)->is_ref)                                                 local_i->refcount++;
        {
          // First, call the destructor to remove any data structures       }
          // associated with lhs that will now be overwritten
          zval_dtor (*p_lhs);                                               zval **p_lhs = &local_i;
          // Overwrite LHS
          (*p_lhs)->value = rhs->value;
          (*p_lhs)->type = rhs->type;                                         zval *rhs;
          zval_copy_ctor (*p_lhs);
        }                                                                     if (local_j == NULL)
        else                                                                    rhs = EG (uninitialized_zval_ptr);
        {
          zval_ptr_dtor (p_lhs);                                              else
          if (rhs->is_ref)
          {                                                                     rhs = local_j;
            // Take a copy of RHS for LHS
            *p_lhs = zvp_clone_ex (rhs);
          }                                                                   if (*p_lhs != rhs)
          else
          {                                                                   {
            // Share a copy                                                     if ((*p_lhs)->is_ref)
            rhs->refcount++;
            *p_lhs = rhs;                                                       {
          }
        }                                                                         // First, call the destructor to remove any data structures
      }                                                                           // associated with lhs that will now be overwritten
  }
                                                                                  zval_dtor (*p_lhs);
                                                                                  // Overwrite LHS
                                                                                  (*p_lhs)->value = rhs->value;
                                                                                  (*p_lhs)->type = rhs->type;
                                                                                  zval_copy_ctor (*p_lhs);
                                                                                }
                                                                                else
                                                                                {
                                                                                  zval_ptr_dtor (p_lhs);
                                                                                  if (rhs->is_ref)
                                                                                  {
                                                                                    // Take a copy of RHS for LHS
                                                                                    *p_lhs = zvp_clone_ex (rhs);
                                                                                  }
                                                                                  else
                                                                                  {
                                                                                    // Share a copy
                                                                                    rhs->refcount++;
                                                                                    *p_lhs = rhs;
                                                                                  }
                                                                                }
                                                                              }
                                                                          }                                                                     Trinity College Dublin        26
                                                                                                                                                                                    Introduction to phc
                                                                                                                                                                                                          Challenges to compilation?
                                                                                                                                                                                  Current state of phc
                                                                                                                                                                                                          phc solution: use the C API
                                                                                                                                                              Next for phc - Analysis and Optimization
Example: printf ($f)                                                                                                                                                                                      Speedup
                                                                                                                                                                                Experiences with PHP
  static zend_fcall_info printf_fci;
  static zend_fcall_info_cache printf_fcic = { 0, NULL, NULL, NULL };




                                                                                      Example: printf ($f)
  // printf($f);
  {
    if (!printf_fcic->initialized)
    {
      zval fn;
      INIT_PZVAL (&fn);
      ZVAL_STRING (&fn, "printf", 0);
      int result = zend_fcall_info_init (&fn, &printf_fci, &printf_fcic TSRMLS_CC);
      if (result != SUCCESS)
      {
        phc_setup_error (1, "listings_source.php", 8, NULL TSRMLS_CC);
        php_error_docref (NULL TSRMLS_CC, E_ERROR,
            "Call to undefined function %s()", function_name);
      }
    }

      zend_function *signature = printf_fcic.function_handler;
      zend_arg_info *arg_info = signature->common.arg_info; // optional

      int by_ref[1];
      int abr_index = 0;
      // TODO: find names to replace index
      if (arg_info)
      {
        by_ref[abr_index] = arg_info->pass_by_reference;
        arg_info++;
      }
      else
        by_ref[abr_index] = signature->common.pass_rest_by_reference;

      abr_index++;                                                                      static zend_fcall_info printf_fci;
                                                                                        static zend_fcall_info_cache printf_fcic = { 0, NULL, NULL, NULL };
      // Setup array of arguments
      // TODO: i think arrays of size 0 is an error
      int destruct[1];                                                                  // printf($f);
      zval *args[1];
      zval **args_ind[1];                                                               {
                                                                                          if (!printf_fcic->initialized)
      int af_index = 0;
      destruct[af_index] = 0;                                                             {
      if (by_ref[af_index])
      {                                                                                     zval fn;
        if (local_f == NULL)                                                                INIT_PZVAL (&fn);
        {
          local_f = EG (uninitialized_zval_ptr);                                            ZVAL_STRING (&fn, "printf", 0);
          local_f->refcount++;
        }                                                                                   int result = zend_fcall_info_init (&fn, &printf_fci, &printf_fcic TSRMLS_CC);
        zval **p_arg = &local_f;                                                            if (result != SUCCESS)
          // We don’t need to restore ->is_ref afterwards,                                  {
          // because the called function will reduce the
          // refcount of arg on return, and will reset is_ref to                              phc_setup_error (1, "listings_source.php", 8, NULL TSRMLS_CC);
          // 0 when refcount drops to 1. If the refcount does                                 php_error_docref (NULL TSRMLS_CC, E_ERROR,
          // not drop to 1 when the function returns, but we did
          // set is_ref to 1 here, that means that is_ref must                                    "Call to undefined function %s()", function_name);
          // already have been 1 to start with (since if it had
          // not, that means that the variable would have been                              }
          // in a copy-on-write set, and would have been                                  }
          // seperated above).
          (*p_arg)->is_ref = 1;

          args_ind[af_index] = p_arg;                                                       zend_function *signature = printf_fcic.function_handler;
                                                                                            zend_arg_info *arg_info = signature->common.arg_info; // optional
        assert (!in_copy_on_write (*args_ind[af_index]));
        args[af_index] = *args_ind[af_index];
      }
      else                                                                                  int by_ref[1];
      {                                                                                     int abr_index = 0;
        zval *arg;
        if (local_f == NULL)                                                                // TODO: find names to replace index
          arg = EG (uninitialized_zval_ptr);
        else                                                                                if (arg_info)
          arg = local_f;                                                                    {
          args[af_index] = fetch_var_arg (arg, &destruct[af_index]);                          by_ref[abr_index] = arg_info->pass_by_reference;
          if (arg->is_ref)
          {                                                                                   arg_info++;
            // We dont separate since we don’t own one of ARG’s references.                 }
            arg = zvp_clone_ex (arg);
            destruct[af_index] = 1;                                                         else
           // It seems we get incorrect refcounts without this.                               by_ref[abr_index] = signature->common.pass_rest_by_reference;
           // TODO This decreases the refcount to zero, which seems wrong,
           // but gives the right answer. We should look at how zend does
           // this.                                                                         abr_index++;
            arg->refcount--;
          }
          args[af_index] = arg;
                                                                                            // Setup array of arguments
        args_ind[af_index] = &args[af_index];
      }                                                                                     // TODO: i think arrays of size 0 is an error
      af_index++;                                                                           int destruct[1];
                                                                                            zval *args[1];
      phc_setup_error (1, "listings_source.php", 8, NULL TSRMLS_CC);
                                                                                            zval **args_ind[1];
      // save existing parameters, in case of recursion
      int param_count_save = printf_fci.param_count;
      zval ***params_save = printf_fci.params;                                              int af_index = 0;
      zval **retval_save = printf_fci.retval_ptr_ptr;
                                                                                            destruct[af_index] = 0;
      zval *rhs = NULL;                                                                     if (by_ref[af_index])
      // set up params                                                                      {
      printf_fci.params = args_ind;
      printf_fci.param_count = 1;                                                             if (local_f == NULL)
      printf_fci.retval_ptr_ptr = &rhs;                                                       {
      // call the function                                                                      local_f = EG (uninitialized_zval_ptr);
      int success = zend_call_function (&printf_fci, &printf_fcic TSRMLS_CC);
      assert (success == SUCCESS);                                                              local_f->refcount++;
                                                                                              }
      // restore params
      printf_fci.params = params_save;                                                        zval **p_arg = &local_f;
      printf_fci.param_count = param_count_save;
      printf_fci.retval_ptr_ptr = retval_save;
                                                                                                // We don’t need to restore ->is_ref afterwards,
      // unset the errors
      phc_setup_error (0, NULL, 0, NULL TSRMLS_CC);                                             // because the called function will reduce the
      int i;                                                                                    // refcount of arg on return, and will reset is_ref to
      for (i = 0; i < 1; i++)                                                                   // 0 when refcount drops to 1. If the refcount does
      {
        if (destruct[i])                                                                        // not drop to 1 when the function returns, but we did
        {
          assert (destruct[i]);                                                                 // set is_ref to 1 here, that means that is_ref must
          zval_ptr_dtor (args_ind[i]);                                                          // already have been 1 to start with (since if it had
        }
      }                                                                                         // not, that means that the variable would have been
                                                                                                // in a copy-on-write set, and would have been
      // When the Zend engine returns by reference, it allocates a zval into                    // seperated above).
      // retval_ptr_ptr. To return by reference, the callee writes into the
      // retval_ptr_ptr, freeing the allocated value as it does. (Note, it may                  (*p_arg)->is_ref = 1;
      // not actually return anything). So the zval returned - whether we return
      // it, or it is the allocated zval - has a refcount of 1.
                                                                                                args_ind[af_index] = p_arg;
      // The caller is responsible for cleaning that up (note, this is unaffected
      // by whether it is added to some COW set).

      // For reasons unknown, the Zend API resets the refcount and is_ref fields              assert (!in_copy_on_write (*args_ind[af_index]));
      // of the return value after the function returns (unless the callee is                 args[af_index] = *args_ind[af_index];
      // interpreted). If the function is supposed to return by reference, this
      // loses the refcount. This only happens when non-interpreted code is                 }
      // called. We work around it, when compiled code is called, by saving the
      // refcount into SAVED_REFCOUNT, in the return statement. The downside is             else
      // that we may create an error if our code is called by a callback, and               {
      // returns by reference, and the callback returns by reference. At least
      // this is an obscure case.                                                             zval *arg;
      if (signature->common.return_reference
          && signature->type != ZEND_USER_FUNCTION)                                           if (local_f == NULL)
      {                                                                                         arg = EG (uninitialized_zval_ptr);
        assert (rhs != EG (uninitialized_zval_ptr));
        rhs->is_ref = 1;                                                                      else
        if (saved_refcount != 0)
        {                                                                                       arg = local_f;
          rhs->refcount = saved_refcount;
        }
        rhs->refcount++;                                                                        args[af_index] = fetch_var_arg (arg, &destruct[af_index]);
      }
      saved_refcount = 0;   // for ’obscure cases’                                              if (arg->is_ref)
                                                                                                {
      zval_ptr_dtor (&rhs);
      if (signature->common.return_reference                                                      // We dont separate since we don’t own one of ARG’s references.
          && signature->type != ZEND_USER_FUNCTION)
        zval_ptr_dtor (&rhs);                                                                     arg = zvp_clone_ex (arg);
  }                                                                                               destruct[af_index] = 1;

                                                                                                  // It seems we get incorrect refcounts without this.
                                                                                                  // TODO This decreases the refcount to zero, which seems wrong,
                                                                                                  // but gives the right answer. We should look at how zend does
                                                                                                  // this.

                                                                                                  arg->refcount--;
                                                                                                }
                                                                                                args[af_index] = arg;

                                                                                                args_ind[af_index] = &args[af_index];
                                                                                            }
                                                                                            af_index++;


                                                                                            phc_setup_error (1, "listings_source.php", 8, NULL TSRMLS_CC);

                                                                                            // save existing parameters, in case of recursion
                                                                                            int param_count_save = printf_fci.param_count;
                                                                                            zval ***params_save = printf_fci.params;
                                                                                            zval **retval_save = printf_fci.retval_ptr_ptr;

                                                                                            zval *rhs = NULL;

                                                                                            // set up params
                                                                                            printf_fci.params = args_ind;
                                                                                            printf_fci.param_count = 1;
                                                                                            printf_fci.retval_ptr_ptr = &rhs;

                                                                                            // call the function
                                                                                            int success = zend_call_function (&printf_fci, &printf_fcic TSRMLS_CC);
                                                                                            assert (success == SUCCESS);

                                                                                            // restore params
                                                                                            printf_fci.params = params_save;
                                                                                            printf_fci.param_count = param_count_save;
                                                                                            printf_fci.retval_ptr_ptr = retval_save;

                                                                                            // unset the errors
                                                                                            phc_setup_error (0, NULL, 0, NULL TSRMLS_CC);

                                                                                            int i;
                                                                                            for (i = 0; i < 1; i++)
                                                                                            {
                                                                                              if (destruct[i])
                                                                                              {
                                                                                                assert (destruct[i]);
                                                                                                zval_ptr_dtor (args_ind[i]);
                                                                                              }
                                                                                            }


                                                                                            // When the Zend engine returns by reference, it allocates a zval into
                                                                                            // retval_ptr_ptr. To return by reference, the callee writes into the
                                                                                            // retval_ptr_ptr, freeing the allocated value as it does. (Note, it may
                                                                                            // not actually return anything). So the zval returned - whether we return
                                                                                            // it, or it is the allocated zval - has a refcount of 1.

                                                                                            // The caller is responsible for cleaning that up (note, this is unaffected
                                                                                            // by whether it is added to some COW set).

                                                                                            //   For reasons unknown, the Zend API resets the refcount and is_ref fields
                                                                                            //   of the return value after the function returns (unless the callee is
                                                                                            //   interpreted). If the function is supposed to return by reference, this
                                                                                            //   loses the refcount. This only happens when non-interpreted code is
                                                                                            //   called. We work around it, when compiled code is called, by saving the
                                                                                            //   refcount into SAVED_REFCOUNT, in the return statement. The downside is
                                                                                            //   that we may create an error if our code is called by a callback, and
                                                                                            //   returns by reference, and the callback returns by reference. At least
                                                                                            //   this is an obscure case.
                                                                                            if   (signature->common.return_reference
                                                                                                  && signature->type != ZEND_USER_FUNCTION)
                                                                                            {
                                                                                                assert (rhs != EG (uninitialized_zval_ptr));
                                                                                                rhs->is_ref = 1;
                                                                                                if (saved_refcount != 0)
                                                                                                {
                                                                                                  rhs->refcount = saved_refcount;
                                                                                                }
                                                                                                rhs->refcount++;
                                                                                            }
                                                                                            saved_refcount = 0;     // for ’obscure cases’

                                                                                            zval_ptr_dtor (&rhs);



                                                                                                                                                                                                          Trinity College Dublin        27
                                                                                            if (signature->common.return_reference
                                                                                                && signature->type != ZEND_USER_FUNCTION)
                                                                                              zval_ptr_dtor (&rhs);
                                                                                        }
                                                         Introduction to phc
                                                                               Challenges to compilation?
                                                       Current state of phc
                                                                               phc solution: use the C API
                                   Next for phc - Analysis and Optimization
Applicability                                                                  Speedup
                                                     Experiences with PHP


      Everything          Applicability
          Perl
          PHP
          Ruby
          Tcl – I think
                                Everything
                                    Perl
                                    PHP
                                    Ruby
                                    Tcl – I think




                                                                               Trinity College Dublin        28
                                                                                   Introduction to phc
                                                                                                         Challenges to compilation?
                                                                                 Current state of phc
                                                                                                         phc solution: use the C API
                                                             Next for phc - Analysis and Optimization
                        Applicability                                                                    Speedup
                                                                               Experiences with PHP


                              Everything            Applicability
                                  Perl
                                  PHP
                                  Ruby
1. Python used to be              Tcl – I think
                                                          Everything
   bad - aycock quote         Except specification             Perl
                                  Lua
                                  Python
                                                              PHP
                                                              Ruby
                                                              Tcl – I think


                                                          Except specification
                                                              Lua
                                                              Python




                                                                                                         Trinity College Dublin        28
                                                           Introduction to phc
                                                                                 Challenges to compilation?
                                                         Current state of phc
                                                                                 phc solution: use the C API
                                     Next for phc - Analysis and Optimization
Applicability                                                                    Speedup
                                                       Experiences with PHP


      Everything            Applicability
           Perl
           PHP
           Ruby
           Tcl – I think
                                  Everything
      Except specification              Perl
           Lua
           Python
                                       PHP
                                       Ruby
      Not at all                       Tcl – I think
           Javascript


                                  Except specification
                                       Lua
                                       Python


                                  Not at all
                                       Javascript


                                                                                 Trinity College Dublin        28
                                                                                 Introduction to phc
                                                                                                       Challenges to compilation?
                                                                               Current state of phc
                                                                                                       phc solution: use the C API
                                                           Next for phc - Analysis and Optimization
Outline                                                                                                Speedup
                                                                             Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin        29
                                                                                                                  Introduction to phc
                                                                                                                                        Challenges to compilation?
                                                                                                                Current state of phc
1. Why is                                                                                   Next for phc - Analysis and Optimization
                                                                                                                                        phc solution: use the C API
                                                                                                                                        Speedup
   experiemental          Original Speed-up                                                                   Experiences with PHP

   evaluation a
   speedup?
                                                                                    Original Speed-up
2. That’s an
   interesting result.
   Shouldnt compilers                                0.1x
                                       (10 times slower than the PHP interpreter)
   always be faster!!!
3. PHP’s interpreter
   isnt slowed by
   interpreter loop.
   Rather its the level                                                                                                        0.1x
   of dynamicism.                                                                                          (10 times slower than the PHP interpreter)




                                                                                                                                        Trinity College Dublin        30
                                                                                       Introduction to phc
                                                                                                             Challenges to compilation?
                                                                                     Current state of phc
                                                                                                             phc solution: use the C API
                                                                 Next for phc - Analysis and Optimization
                       The problem with copies                                                               Speedup
                                                                                   Experiences with PHP


                         <?php
                                                         The problem with copies
                           for ($i = 0; $i < $n; $i++)
                             $str = $str . "hello";
                         ?>
1. each statement is                                       <?php
   pretty high level
                         <?php
                                                             for ($i = 0; $i < $n; $i++)
                           for ($i = 0; $i < $n; $i++)         $str = $str . "hello";
                           {
                             $T = $str . "hello";          ?>
                             $str = $T;
                           }
                         ?>


                                                           <?php
                                                             for ($i = 0; $i < $n; $i++)
                                                             {
                                                               $T = $str . "hello";
                                                               $str = $T;
                                                             }
                                                           ?>
                                                                                                             Trinity College Dublin        31
                                                                                            Introduction to phc
                                                                                                                  Challenges to compilation?
                                                                                          Current state of phc
                                                                                                                  phc solution: use the C API
                                                                      Next for phc - Analysis and Optimization
                          Optimization                                                                            Speedup
                                                                                        Experiences with PHP

                               Constant folding               Optimization
1. We dont need to
   know how to fold                                                Constant folding
   constants - we just    <?php                   <?php
   pass it off to PHP’s     ...                     ...
                            $T = "5" + true;        $T = 6;
   eval                     ...                     ...
                          ?>                      ?>



                                                              <?php                                               <?php
                                                                 ...                                                ...
                                                                 $T = "5" + true;                                   $T = 6;
                                                                 ...                                                ...
                                                              ?>                                                  ?>




                                                                                                                  Trinity College Dublin        32
                                                                   Introduction to phc
                                                                                         Challenges to compilation?
                                                                 Current state of phc
                                                                                         phc solution: use the C API
                                             Next for phc - Analysis and Optimization
Optimization                                                                             Speedup
                                                               Experiences with PHP

     Constant folding                Optimization
     Constant pooling


                                          Constant folding
  <?php
    $sum = 0;
                                          Constant pooling
    for ($i = 0; $i < 10; $i=$i+1)
    {
      $sum .= "hello";
    }
  ?>
                                       <?php
                                         $sum = 0;
                                         for ($i = 0; $i < 10; $i=$i+1)
                                         {
                                           $sum .= "hello";
                                         }
                                       ?>


                                                                                         Trinity College Dublin        32
                                                                                                                  Introduction to phc
                                                                                                                                        Challenges to compilation?
                                                                                                                Current state of phc
                                                                                                                                        phc solution: use the C API
                                                                                            Next for phc - Analysis and Optimization
                        Optimization                                                                                                    Speedup
                                                                                                              Experiences with PHP

1. PHP implements
   this
                                Constant folding                                Optimization
                                Constant pooling
2. function cant                Function caching
   change afte first                                                                     Constant folding
   invocation - dont                                                                    Constant pooling
                          // printf ($f);
   need lookup-cache      static php_fcall_info printf_info;

   of inline cache or
                          {
                            php_fcall_info_init ("printf", &printf_info);
                                                                                        Function caching
   polymorphic inline         php_hash_find (
                                LOCAL_ST, "f", 5863275, &printf_info.params);
   cache                      php_call_function (&printf_info);
                          }

                                                                                  // printf ($f);
                                                                                  static php_fcall_info printf_info;
                                                                                  {
                                                                                    php_fcall_info_init ("printf", &printf_info);

                                                                                      php_hash_find (
                                                                                        LOCAL_ST, "f", 5863275, &printf_info.params);

                                                                                      php_call_function (&printf_info);
                                                                                  }

                                                                                                                                        Trinity College Dublin        32
                                                                                 Introduction to phc
                                                                                                       Challenges to compilation?
                                                                               Current state of phc
                                                                                                       phc solution: use the C API
                                                           Next for phc - Analysis and Optimization
Optimization                                                                                           Speedup
                                                                             Experiences with PHP

     Constant folding                              Optimization
     Constant pooling
     Function caching
     Pre-hashing                                        Constant folding
  // $i = 0;                                            Constant pooling
  {
    zval* p_i;                                          Function caching
    php_hash_find (LOCAL_ST, "i", 5863374, p_i);
    php_destruct (p_i);                                 Pre-hashing
    php_allocate (p_i);
    ZVAL_LONG (*p_i, 0);
  }
                                                     // $i = 0;
                                                     {
                                                       zval* p_i;
                                                       php_hash_find (LOCAL_ST, "i", 5863374, p_i);
                                                       php_destruct (p_i);
                                                       php_allocate (p_i);
                                                       ZVAL_LONG (*p_i, 0);
                                                     }

                                                                                                       Trinity College Dublin        32
                                                             Introduction to phc
                                                                                   Challenges to compilation?
                                                           Current state of phc
                                                                                   phc solution: use the C API
                                       Next for phc - Analysis and Optimization
Optimization                                                                       Speedup
                                                         Experiences with PHP

     Constant folding          Optimization
     Constant pooling
     Function caching
     Pre-hashing                    Constant folding
     Symbol-table removal
  // $i = 0;                        Constant pooling
  {
    php_destruct (local_i);         Function caching
    php_allocate (local_i);
    ZVAL_LONG (*local_i, 0);        Pre-hashing
  }
                                    Symbol-table removal
                                 // $i = 0;
                                 {
                                   php_destruct (local_i);
                                   php_allocate (local_i);
                                   ZVAL_LONG (*local_i, 0);
                                 }



                                                                                   Trinity College Dublin        32
                                                                                                                                                                                                                                                                                                                   Introduction to phc
                                                                                                                                                                                                                                                                                                                                                                           Challenges to compilation?
                                                                                                                                                                                                                                                                                                                 Current state of phc
                                                                                                                                                                                                                                                                                                                                                                           phc solution: use the C API
                                                                                                                                                                                                                                                                                             Next for phc - Analysis and Optimization
                       Current speed-up                                                                                                                                                                                                                                                                                                                                    Speedup
                                                                                                                                                                                                                                                                                                               Experiences with PHP


1. Explain how to
                                                          3
                                                                                                                                                                                                                                                     Current speed-up




                         Speedup of compiled benchmark
   read graph                                            2.5


2. Much better than                                       2
                                                                                                                                                                                                                                                                                        3
   0.1x                                                  1.5




                                                                                                                                                                                                                                                       Speedup of compiled benchmark
3. C compiler: be 5x                                      1                                                                                                                                                                                                                            2.5
   faster                                                0.5

4. PHP 40x-70x                                                                                                                                                                                                                                                                          2
                                                          0




                                                               ackermann
                                                                           ary
                                                                                 ary2
                                                                                        ary3
                                                                                               fibo
                                                                                                      hash1
                                                                                                              hash2
                                                                                                                      heapsort
                                                                                                                                 mandel
                                                                                                                                          mandel2
                                                                                                                                                    matrix
                                                                                                                                                             nestedloop
                                                                                                                                                                          sieve
                                                                                                                                                                                  simple
                                                                                                                                                                                           simplecall
                                                                                                                                                                                                        simpleucall
                                                                                                                                                                                                                      simpleudcall
                                                                                                                                                                                                                                     strcat
                                                                                                                                                                                                                                              mean
   slower
                                                                                                                                                                                                                                                                                       1.5

                                                                                                                                                                                                                                                                                        1

                                                                                                                                                                                                                                                                                       0.5

                                                                                                                                                                                                                                                                                        0




                                                                                                                                                                                                                                                                                                ackermann
                                                                                                                                                                                                                                                                                                            ary
                                                                                                                                                                                                                                                                                                                  ary2
                                                                                                                                                                                                                                                                                                                         ary3
                                                                                                                                                                                                                                                                                                                                fibo
                                                                                                                                                                                                                                                                                                                                       hash1
                                                                                                                                                                                                                                                                                                                                               hash2
                                                                                                                                                                                                                                                                                                                                                       heapsort
                                                                                                                                                                                                                                                                                                                                                                  mandel
                                                                                                                                                                                                                                                                                                                                                                            mandel2
                                                                                                                                                                                                                                                                                                                                                                                      matrix
                                                                                                                                                                                                                                                                                                                                                                                               nestedloop
                                                                                                                                                                                                                                                                                                                                                                                                            sieve
                                                                                                                                                                                                                                                                                                                                                                                                                    simple
                                                                                                                                                                                                                                                                                                                                                                                                                             simplecall
                                                                                                                                                                                                                                                                                                                                                                                                                                          simpleucall
                                                                                                                                                                                                                                                                                                                                                                                                                                                        simpleudcall
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       strcat
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                mean
                                                                                                                                                                                                                                                                                                                                                                           Trinity College Dublin                                                                                      33
                                                                                 Introduction to phc
                                                                               Current state of phc    Simple Optimizations
                                                           Next for phc - Analysis and Optimization    Advanced Optimizations
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   34
                                                                                 Introduction to phc
                                                                               Current state of phc    Simple Optimizations
                                                           Next for phc - Analysis and Optimization    Advanced Optimizations
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   35
                                                                                                       Introduction to phc
                                                                                                     Current state of phc    Simple Optimizations
                                                                                 Next for phc - Analysis and Optimization    Advanced Optimizations
                Intra-procedural optimizations                                                     Experiences with PHP


                                                                         Intra-procedural optimizations

1. 2x speedup                                    Dead-code elimination
                                                 Sparse-conditional
                                                 constant propagation




                                                                                                                                     Dead-code elimination
                                                                                                                                     Sparse-conditional
                                                                                                                                     constant propagation




                                                                                                                             Trinity College Dublin          36
                                                          Introduction to phc
                                                        Current state of phc    Simple Optimizations
                                    Next for phc - Analysis and Optimization    Advanced Optimizations
Type-inference                                        Experiences with PHP


                          Type-inference
  <?php

    function a ($x, $y)
    {
      $str = $x . $y;
      ...                   <?php
      return $str;
    }
                              function a ($x, $y)
  ?>
                              {
                                $str = $x . $y;
                                ...
                                return $str;
                              }

                            ?>



                                                                                Trinity College Dublin   37
                                                       Introduction to phc
                                                     Current state of phc    Simple Optimizations
                                 Next for phc - Analysis and Optimization    Advanced Optimizations
User-space handlers                                Experiences with PHP


     __toString
                      User-space handlers
     __get
     __set
     __isset
     __unset
                           __toString
     __sleep               __get
     __wake
     __call                __set
     __callStatic
     ...
                           __isset
                           __unset
                           __sleep
                           __wake
                           __call
                           __callStatic
                           ...

                                                                             Trinity College Dublin   38
                                                                              Introduction to phc
                                                                            Current state of phc    Simple Optimizations
                                                        Next for phc - Analysis and Optimization    Advanced Optimizations
                       C API handlers                                     Experiences with PHP



1. So previous SSA
                                             C API handlers
                            read_property
   opts were illegal        read_dimension
2. Complete access          get
   to interpreter           set
   internals                cast_object           read_property
                            has_property
3. Need accurate                                  read_dimension
                            unset_property
   use-defs                 ...
                                                  get
                                                  set
                                                  cast_object
                                                  has_property
                                                  unset_property
                                                  ...



                                                                                                    Trinity College Dublin   39
                                                          Introduction to phc
                                                        Current state of phc    Simple Optimizations
                                    Next for phc - Analysis and Optimization    Advanced Optimizations
Unknown types propagate                               Experiences with PHP


                            Unknown types propagate
     local symbol table
     global symbol table
     return values
     reference parameters
     callee parameters
                                 local symbol table
                                 global symbol table
                                 return values
                                 reference parameters
                                 callee parameters




                                                                                Trinity College Dublin   40
                                                                                 Introduction to phc
                                                                               Current state of phc    Simple Optimizations
                                                           Next for phc - Analysis and Optimization    Advanced Optimizations
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   41
                                                                                                Introduction to phc
                                                                                              Current state of phc    Simple Optimizations
                                                                          Next for phc - Analysis and Optimization    Advanced Optimizations
Analysis design                                                                             Experiences with PHP


                                                                  Analysis design
     Must model types precisely
         (Possibly unnamed) fields, arrays, variables and method
         calls



                                                                       Must model types precisely
                                                                           (Possibly unnamed) fields, arrays, variables and method
                                                                           calls




                                                                                                                      Trinity College Dublin   42
                                                                                                                       Introduction to phc
                                                                                                                     Current state of phc    Simple Optimizations
                                                                                                 Next for phc - Analysis and Optimization    Advanced Optimizations
                       Analysis design                                                                             Experiences with PHP


                                                                                         Analysis design
                            Must model types precisely
                                (Possibly unnamed) fields, arrays, variables and method
1. Uses and                     calls
   defintions
                            Uses and definitions incomplete
   incomplete - this            Can’t use def-use chains                                      Must model types precisely
   doesnt use them              Can’t use SSA
                                                                                                  (Possibly unnamed) fields, arrays, variables and method
                                                                                                  calls

                                                                                              Uses and definitions incomplete
                                                                                                  Can’t use def-use chains
                                                                                                  Can’t use SSA




                                                                                                                                             Trinity College Dublin   42
                                                                                                                       Introduction to phc
                                                                                                                     Current state of phc    Simple Optimizations
                                                                                                 Next for phc - Analysis and Optimization    Advanced Optimizations
                       Analysis design                                                                             Experiences with PHP


                                                                                         Analysis design
                            Must model types precisely
                                (Possibly unnamed) fields, arrays, variables and method
1. Imprecise                    calls

   callgraph - do it        Uses and definitions incomplete
   lazily                       Can’t use def-use chains                                      Must model types precisely
                                Can’t use SSA
                                                                                                  (Possibly unnamed) fields, arrays, variables and method
                            Imprecise callgraph                                                   calls

                                                                                              Uses and definitions incomplete
                                                                                                  Can’t use def-use chains
                                                                                                  Can’t use SSA

                                                                                              Imprecise callgraph




                                                                                                                                             Trinity College Dublin   42
                                                                         Introduction to phc
                                                                       Current state of phc    Simple Optimizations
                                                   Next for phc - Analysis and Optimization    Advanced Optimizations
Algorithm                                                            Experiences with PHP


     Abstract Execution / Interpretation   Algorithm

                                                Abstract Execution / Interpretation




                                                                                               Trinity College Dublin   43
                                                                                            Introduction to phc
                                                                                          Current state of phc    Simple Optimizations
                                                                      Next for phc - Analysis and Optimization    Advanced Optimizations
                   Algorithm                                                            Experiences with PHP


                        Abstract Execution / Interpretation   Algorithm
                        Points-to analysis
                            *-sensitive
1. flow, context,                                                   Abstract Execution / Interpretation
   object, field
                                                                   Points-to analysis
                                                                       *-sensitive




                                                                                                                  Trinity College Dublin   43
                                                                                                     Introduction to phc
                                                                                                   Current state of phc    Simple Optimizations
                                                                               Next for phc - Analysis and Optimization    Advanced Optimizations
Algorithm                                                                                        Experiences with PHP


      Abstract Execution / Interpretation                            Algorithm
      Points-to analysis
           *-sensitive
                                                                           Abstract Execution / Interpretation
      Constant-propagation
           Precision
           Array-indices/field names
           Implicit conversions
                                                                           Points-to analysis
                                                                                *-sensitive
  A. Pioli. Conditional pointer aliasing and constant propagation.
  Master’s thesis, SUNY at New Paltz, 1999.
                                                                           Constant-propagation
                                                                                Precision
                                                                                Array-indices/field names
                                                                                Implicit conversions


                                                                       A. Pioli. Conditional pointer aliasing and constant propagation.
                                                                       Master’s thesis, SUNY at New Paltz, 1999.

                                                                                                                           Trinity College Dublin   43
                                                                                                  Introduction to phc
                                                                                                Current state of phc    Simple Optimizations
1. Make polymorphic                                                         Next for phc - Analysis and Optimization    Advanced Optimizations
   calls monomorphic     Algorithm                                                            Experiences with PHP

2. Go through each of
   the problems on            Abstract Execution / Interpretation
                                                                    Algorithm
   the previous slide         Points-to analysis
3. model types                    *-sensitive
                                                                         Abstract Execution / Interpretation
   precisely
                              Constant-propagation
4. need to model                  Precision
   many functions - in            Array-indices/field names               Points-to analysis
                                  Implicit conversions
   contrast to SAC                                                           *-sensitive
   stuff                      Type-inference
                                  Virtual calls
5. much easier than               Function annotations                   Constant-propagation
   reimplementing,                                                           Precision
   however                                                                   Array-indices/field names
                                                                             Implicit conversions

                                                                         Type-inference
                                                                             Virtual calls
                                                                             Function annotations

                                                                                                                        Trinity College Dublin   43
                                                                                  Introduction to phc
                                                                                Current state of phc    Simple Optimizations
                                                            Next for phc - Analysis and Optimization    Advanced Optimizations
                        Complex cases                                         Experiences with PHP


                                                    Complex cases
1. Static-includes           Hashtables
   optimization needs        Implicit conversions
   to be                     Variable-variables
   deployment-time           $GLOBALS

2. hashtables - SAC          Static includes             Hashtables
                             $SESSION
   javascript talk           Compiler temporaries        Implicit conversions
                                                         Variable-variables
                                                         $GLOBALS
                                                         Static includes
                                                         $SESSION
                                                         Compiler temporaries




                                                                                                        Trinity College Dublin   44
                                                                                          Introduction to phc
                                                                                        Current state of phc    Simple Optimizations
                                                                    Next for phc - Analysis and Optimization    Advanced Optimizations
                       Interesting thoughts                                           Experiences with PHP



                            Strip off first loop iteration
                                                            Interesting thoughts

1. just like hotspot                                             Strip off first loop iteration




                                                                                                                Trinity College Dublin   45
                                                                                         Introduction to phc
                                                                                       Current state of phc    Simple Optimizations
                                                                   Next for phc - Analysis and Optimization    Advanced Optimizations
                      Interesting thoughts                                           Experiences with PHP



                           Strip off first loop iteration
                                                           Interesting thoughts
                           JITs or Gal/Franz Tracing?
1. Would it go well
   with Gal/Franz                                               Strip off first loop iteration
   tracing?
                                                                JITs or Gal/Franz Tracing?




                                                                                                               Trinity College Dublin   45
                                                                                           Introduction to phc
                                                                                         Current state of phc    Simple Optimizations
                                                                     Next for phc - Analysis and Optimization    Advanced Optimizations
Interesting thoughts                                                                   Experiences with PHP



      Strip off first loop iteration
                                                            Interesting thoughts
      JITs or Gal/Franz Tracing?
      Use string transducer analysis

  Sound and Precise Analysis of Web Applications                  Strip off first loop iteration
  for Injection Vulnerabilities
  Gary Wassermann, Zhendong Su, PLDI’07.                          JITs or Gal/Franz Tracing?
  Static approximation of dynamically generated Web pages         Use string transducer analysis
  Yasuhiko Minamide, WWW 2005

                                                              Sound and Precise Analysis of Web Applications
                                                              for Injection Vulnerabilities
                                                              Gary Wassermann, Zhendong Su, PLDI’07.

                                                              Static approximation of dynamically generated Web pages
                                                              Yasuhiko Minamide, WWW 2005


                                                                                                                 Trinity College Dublin   45
                                                                                 Introduction to phc
                                                                               Current state of phc
                                                           Next for phc - Analysis and Optimization
Outline                                                                      Experiences with PHP


  1   Introduction to phc
                                                 Outline
  2   Current state of phc
        Challenges to compilation?
        phc solution: use the C API
        Speedup
                                                   1   Introduction to phc
  3   Next for phc - Analysis and Optimization
        Simple Optimizations
                                                   2   Current state of phc
        Advanced Optimizations                           Challenges to compilation?
  4   Experiences with PHP                               phc solution: use the C API
                                                         Speedup

                                                   3   Next for phc - Analysis and Optimization
                                                         Simple Optimizations
                                                         Advanced Optimizations

                                                   4   Experiences with PHP


                                                                                                       Trinity College Dublin   46
                                                                                 Introduction to phc
                                                                               Current state of phc
                                                           Next for phc - Analysis and Optimization
                       Opinions and conjecture                               Experiences with PHP


                                                   Opinions and conjecture
1. hope you like PhD     Opinions and conjecture
   work as much as I
   hope my externs
   will
2. working with php                                  Opinions and conjecture




                                                                                                       Trinity College Dublin   47
                                                                                   Introduction to phc
                                                                                 Current state of phc
                                                             Next for phc - Analysis and Optimization
                         Opinions and conjecture                               Experiences with PHP

1. difficult to get a
   good mental
                                                     Opinions and conjecture
   model, without          Opinions and conjecture
   looking at source -
                              Language Problems
   edge cases
2. Then:
3. dont go into it                                     Opinions and conjecture
4. documented all
   over web                                               Language Problems




                                                                                                         Trinity College Dublin   47
                                                                                      Introduction to phc
                                                                                    Current state of phc
1. implementation                                               Next for phc - Analysis and Optimization
   slow                                                                           Experiences with PHP

2. C API prevents fast   Opinions and conjecture
   implementation -
                                                        Opinions and conjecture
   tight coupling -
   unchangable             Opinions and conjecture
3. Instead use libs +
                              Language Problems
   good FFI +
   user-code                  Implementation problems     Opinions and conjecture
4. Allows analysis
5. Other                                                     Language Problems
   implementations
6. Allow
   experimentation                                           Implementation problems
   within Zend engine




                                                                                                            Trinity College Dublin   47
                                                                                       Introduction to phc
                                                                                     Current state of phc
1. reference -                                                   Next for phc - Analysis and Optimization
   spending time in                                                                Experiences with PHP

   GCC world
2. never seen so
                                                         Opinions and conjecture
   much bikeshedding
3. needs more code
   review - possibly
   even commit-after-
   review                                                  Opinions and conjecture
4. few working on
                          Opinions and conjecture
   making php                                                 Language Problems
   “better” - except in
   APC
5. Alternate imple-         Opinions and conjecture           Implementation problems
   mentations/tools            Language Problems
   not done by                                                Community Problems
                               Implementation problems
   community -
   instead by IBM,             Community Problems

   roadsend davis,
   tuwien, stanford,
   utrecht, trinity.
   However: YARV,                                                                                            Trinity College Dublin   47
                                                                                                                   Introduction to phc
                                                                                                                 Current state of phc
                                                                                             Next for phc - Analysis and Optimization
                         Opinions and conjecture                                                               Experiences with PHP


                                                                                   Opinions and conjecture
1.   php7k - like py3k     Fixes

2.   break BC                  Remove coupling between libraries and interpreter
                               Better community interactions:
3.   remove legacy                 Pre-commit reviews
4.   internally                    Mailing list moderation

     consistent
                                   Per-area maintainers                              Fixes
                               Love of the language leads to more tools
                                                                                         Remove coupling between libraries and interpreter
                                                                                         Better community interactions:
                                                                                              Pre-commit reviews
                                                                                              Mailing list moderation
                                                                                              Per-area maintainers
                                                                                         Love of the language leads to more tools




                                                                                                                                         Trinity College Dublin   48
                                                                                                Introduction to phc
                                                                                              Current state of phc
                                                                          Next for phc - Analysis and Optimization
Summary                                                                                     Experiences with PHP


                                                                   Summary
    Re-use existing run-time for language
    Better yet: standardize libraries (and language?), including
    FFI

    Analysis needs to be precise, and whole-program
                                                                       Re-use existing run-time for language
    Pessimistic assumptions spread

    Language, implementation and community need to be
                                                                       Better yet: standardize libraries (and language?), including
    fixed                                                               FFI
        All related?


                                                                       Analysis needs to be precise, and whole-program
                                                                       Pessimistic assumptions spread

                                                                       Language, implementation and community need to be
                                                                       fixed
                                                                           All related?



                                                                                                                      Trinity College Dublin   49
                                                                               Introduction to phc
                                                                             Current state of phc
                                                         Next for phc - Analysis and Optimization
Thanks                                                                     Experiences with PHP


                                                Thanks
  phc needs contributors
    contribute:
    http://phpcompiler.org/contribute.html
    mailing list: phc-general@phpcompiler.org     phc needs contributors
    slides: http://www.cs.tcd.ie/~pbiggar/
    contact: paul.biggar@gmail.com
                                                    contribute:
                                                    http://phpcompiler.org/contribute.html
                                                    mailing list: phc-general@phpcompiler.org

                                                    slides: http://www.cs.tcd.ie/~pbiggar/
                                                    contact: paul.biggar@gmail.com




                                                                                                     Trinity College Dublin   50
                                                                                  Introduction to phc
                                                                                Current state of phc
                                                            Next for phc - Analysis and Optimization
                        Complex cases                                         Experiences with PHP


                                                    Complex cases
1. Static-includes           Hashtables
   optimization needs        Implicit conversions
   to be                     Variable-variables
   deployment-time           $GLOBALS

2. hashtables - SAC          Static includes             Hashtables
                             $SESSION
   javascript talk           Compiler temporaries        Implicit conversions
                                                         Variable-variables
                                                         $GLOBALS
                                                         Static includes
                                                         $SESSION
                                                         Compiler temporaries




                                                                                                        Trinity College Dublin   51

				
DOCUMENT INFO
Description: Compiling and Optimizing Scripting Languages