FaultMaps.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ==============================
  2. FaultMaps and implicit checks
  3. ==============================
  4. .. contents::
  5. :local:
  6. :depth: 2
  7. Motivation
  8. ==========
  9. Code generated by managed language runtimes tend to have checks that
  10. are required for safety but never fail in practice. In such cases, it
  11. is profitable to make the non-failing case cheaper even if it makes
  12. the failing case significantly more expensive. This asymmetry can be
  13. exploited by folding such safety checks into operations that can be
  14. made to fault reliably if the check would have failed, and recovering
  15. from such a fault by using a signal handler.
  16. For example, Java requires null checks on objects before they are read
  17. from or written to. If the object is ``null`` then a
  18. ``NullPointerException`` has to be thrown, interrupting normal
  19. execution. In practice, however, dereferencing a ``null`` pointer is
  20. extremely rare in well-behaved Java programs, and typically the null
  21. check can be folded into a nearby memory operation that operates on
  22. the same memory location.
  23. The Fault Map Section
  24. =====================
  25. Information about implicit checks generated by LLVM are put in a
  26. special "fault map" section. On Darwin this section is named
  27. ``__llvm_faultmaps``.
  28. The format of this section is
  29. .. code-block:: none
  30. Header {
  31. uint8 : Fault Map Version (current version is 1)
  32. uint8 : Reserved (expected to be 0)
  33. uint16 : Reserved (expected to be 0)
  34. }
  35. uint32 : NumFunctions
  36. FunctionInfo[NumFunctions] {
  37. uint64 : FunctionAddress
  38. uint32 : NumFaultingPCs
  39. uint32 : Reserved (expected to be 0)
  40. FunctionFaultInfo[NumFaultingPCs] {
  41. uint32 : FaultKind = FaultMaps::FaultingLoad (only legal value currently)
  42. uint32 : FaultingPCOffset
  43. uint32 : HandlerPCOffset
  44. }
  45. }
  46. The ``ImplicitNullChecks`` pass
  47. ===============================
  48. The ``ImplicitNullChecks`` pass transforms explicit control flow for
  49. checking if a pointer is ``null``, like:
  50. .. code-block:: llvm
  51. %ptr = call i32* @get_ptr()
  52. %ptr_is_null = icmp i32* %ptr, null
  53. br i1 %ptr_is_null, label %is_null, label %not_null, !make.implicit !0
  54. not_null:
  55. %t = load i32, i32* %ptr
  56. br label %do_something_with_t
  57. is_null:
  58. call void @HFC()
  59. unreachable
  60. !0 = !{}
  61. to control flow implicit in the instruction loading or storing through
  62. the pointer being null checked:
  63. .. code-block:: llvm
  64. %ptr = call i32* @get_ptr()
  65. %t = load i32, i32* %ptr ;; handler-pc = label %is_null
  66. br label %do_something_with_t
  67. is_null:
  68. call void @HFC()
  69. unreachable
  70. This transform happens at the ``MachineInstr`` level, not the LLVM IR
  71. level (so the above example is only representative, not literal). The
  72. ``ImplicitNullChecks`` pass runs during codegen, if
  73. ``-enable-implicit-null-checks`` is passed to ``llc``.
  74. The ``ImplicitNullChecks`` pass adds entries to the
  75. ``__llvm_faultmaps`` section described above as needed.
  76. ``make.implicit`` metadata
  77. --------------------------
  78. Making null checks implicit is an aggressive optimization, and it can
  79. be a net performance pessimization if too many memory operations end
  80. up faulting because of it. A language runtime typically needs to
  81. ensure that only a negligible number of implicit null checks actually
  82. fault once the application has reached a steady state. A standard way
  83. of doing this is by healing failed implicit null checks into explicit
  84. null checks via code patching or recompilation. It follows that there
  85. are two requirements an explicit null check needs to satisfy for it to
  86. be profitable to convert it to an implicit null check:
  87. 1. The case where the pointer is actually null (i.e. the "failing"
  88. case) is extremely rare.
  89. 2. The failing path heals the implicit null check into an explicit
  90. null check so that the application does not repeatedly page
  91. fault.
  92. The frontend is expected to mark branches that satisfy (1) and (2)
  93. using a ``!make.implicit`` metadata node (the actual content of the
  94. metadata node is ignored). Only branches that are marked with
  95. ``!make.implicit`` metadata are considered as candidates for
  96. conversion into implicit null checks.
  97. (Note that while we could deal with (1) using profiling data, dealing
  98. with (2) requires some information not present in branch profiles.)