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