You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
5.5 KiB

  1. # Adding or extending a family of adaptive instructions.
  2. ## Families of instructions
  3. The core part of PEP 659 (specializing adaptive interpreter) is the families
  4. of instructions that perform the adaptive specialization.
  5. A family of instructions has the following fundamental properties:
  6. * It corresponds to a single instruction in the code
  7. generated by the bytecode compiler.
  8. * It has a single adaptive instruction that records an execution count and,
  9. at regular intervals, attempts to specialize itself. If not specializing,
  10. it executes the non-adaptive instruction.
  11. * It has at least one specialized form of the instruction that is tailored
  12. for a particular value or set of values at runtime.
  13. * All members of the family have access to the same number of cache entries.
  14. Individual family members do not need to use all of the entries.
  15. The current implementation also requires the following,
  16. although these are not fundamental and may change:
  17. * If a family uses one or more entries, then the first entry must be a
  18. `_PyAdaptiveEntry` entry.
  19. * If a family uses no cache entries, then the `oparg` is used as the
  20. counter for the adaptive instruction.
  21. * All instruction names should start with the name of the non-adaptive
  22. instruction.
  23. * The adaptive instruction should end in `_ADAPTIVE`.
  24. * Specialized forms should have names describing their specialization.
  25. ## Example family
  26. The `LOAD_GLOBAL` instruction (in Python/ceval.c) already has an adaptive
  27. family that serves as a relatively simple example.
  28. The `LOAD_GLOBAL_ADAPTIVE` instruction performs adaptive specialization,
  29. calling `_Py_Specialize_LoadGlobal()` when the counter reaches zero.
  30. There are two specialized instructions in the family, `LOAD_GLOBAL_MODULE`
  31. which is specialized for global variables in the module, and
  32. `LOAD_GLOBAL_BUILTIN` which is specialized for builtin variables.
  33. ## Performance analysis
  34. The benefit of a specialization can be assessed with the following formula:
  35. `Tbase/Tadaptive`.
  36. Where `Tbase` is the mean time to execute the base instruction,
  37. and `Tadaptive` is the mean time to execute the specialized and adaptive forms.
  38. `Tadaptive = (sum(Ti*Ni) + Tmiss*Nmiss)/(sum(Ni)+Nmiss)`
  39. `Ti` is the time to execute the `i`th instruction in the family and `Ni` is
  40. the number of times that instruction is executed.
  41. `Tmiss` is the time to process a miss, including de-optimzation
  42. and the time to execute the base instruction.
  43. The ideal situation is where misses are rare and the specialized
  44. forms are much faster than the base instruction.
  45. `LOAD_GLOBAL` is near ideal, `Nmiss/sum(Ni) ≈ 0`.
  46. In which case we have `Tadaptive ≈ sum(Ti*Ni)`.
  47. Since we can expect the specialized forms `LOAD_GLOBAL_MODULE` and
  48. `LOAD_GLOBAL_BUILTIN` to be much faster than the adaptive base instruction,
  49. we would expect the specialization of `LOAD_GLOBAL` to be profitable.
  50. ## Design considerations
  51. While `LOAD_GLOBAL` may be ideal, instructions like `LOAD_ATTR` and
  52. `CALL_FUNCTION` are not. For maximum performance we want to keep `Ti`
  53. low for all specialized instructions and `Nmiss` as low as possible.
  54. Keeping `Nmiss` low means that there should be specializations for almost
  55. all values seen by the base instruction. Keeping `sum(Ti*Ni)` low means
  56. keeping `Ti` low which means minimizing branches and dependent memory
  57. accesses (pointer chasing). These two objectives may be in conflict,
  58. requiring judgement and experimentation to design the family of instructions.
  59. ### Gathering data
  60. Before choosing how to specialize an instruction, it is important to gather
  61. some data. What are the patterns of usage of the base instruction?
  62. Data can best be gathered by instrumenting the interpreter. Since a
  63. specialization function and adaptive instruction are going to be required,
  64. instrumentation can most easily be added in the specialization function.
  65. ### Choice of specializations
  66. The performance of the specializing adaptive interpreter relies on the
  67. quality of specialization and keeping the overhead of specialization low.
  68. Specialized instructions must be fast. In order to be fast,
  69. specialized instructions should be tailored for a particular
  70. set of values that allows them to:
  71. 1. Verify that incoming value is part of that set with low overhead.
  72. 2. Perform the operation quickly.
  73. This requires that the set of values is chosen such that membership can be
  74. tested quickly and that membership is sufficient to allow the operation to
  75. performed quickly.
  76. For example, `LOAD_GLOBAL_MODULE` is specialized for `globals()`
  77. dictionaries that have a keys with the expected version.
  78. This can be tested quickly:
  79. * `globals->keys->dk_version == expected_version`
  80. and the operation can be performed quickly:
  81. * `value = globals->keys->entries[index].value`.
  82. Because it is impossible to measure the performance of an instruction without
  83. also measuring unrelated factors, the assessment of the quality of a
  84. specialization will require some judgement.
  85. As a general rule, specialized instructions should be much faster than the
  86. base instruction.
  87. ### Implementation of specialized instructions
  88. In general, specialized instructions should be implemented in two parts:
  89. 1. A sequence of guards, each of the form
  90. `DEOPT_IF(guard-condition-is-false, BASE_NAME)`,
  91. followed by a `record_cache_hit()`.
  92. 2. The operation, which should ideally have no branches and
  93. a minimum number of dependent memory accesses.
  94. In practice, the parts may overlap, as data required for guards
  95. can be re-used in the operation.
  96. If there are branches in the operation, then consider further specialization
  97. to eliminate the branches.