1*9880d681SAndroid Build Coastguard Worker=================================== 2*9880d681SAndroid Build Coastguard WorkerStack maps and patch points in LLVM 3*9880d681SAndroid Build Coastguard Worker=================================== 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Worker.. contents:: 6*9880d681SAndroid Build Coastguard Worker :local: 7*9880d681SAndroid Build Coastguard Worker :depth: 2 8*9880d681SAndroid Build Coastguard Worker 9*9880d681SAndroid Build Coastguard WorkerDefinitions 10*9880d681SAndroid Build Coastguard Worker=========== 11*9880d681SAndroid Build Coastguard Worker 12*9880d681SAndroid Build Coastguard WorkerIn this document we refer to the "runtime" collectively as all 13*9880d681SAndroid Build Coastguard Workercomponents that serve as the LLVM client, including the LLVM IR 14*9880d681SAndroid Build Coastguard Workergenerator, object code consumer, and code patcher. 15*9880d681SAndroid Build Coastguard Worker 16*9880d681SAndroid Build Coastguard WorkerA stack map records the location of ``live values`` at a particular 17*9880d681SAndroid Build Coastguard Workerinstruction address. These ``live values`` do not refer to all the 18*9880d681SAndroid Build Coastguard WorkerLLVM values live across the stack map. Instead, they are only the 19*9880d681SAndroid Build Coastguard Workervalues that the runtime requires to be live at this point. For 20*9880d681SAndroid Build Coastguard Workerexample, they may be the values the runtime will need to resume 21*9880d681SAndroid Build Coastguard Workerprogram execution at that point independent of the compiled function 22*9880d681SAndroid Build Coastguard Workercontaining the stack map. 23*9880d681SAndroid Build Coastguard Worker 24*9880d681SAndroid Build Coastguard WorkerLLVM emits stack map data into the object code within a designated 25*9880d681SAndroid Build Coastguard Worker:ref:`stackmap-section`. This stack map data contains a record for 26*9880d681SAndroid Build Coastguard Workereach stack map. The record stores the stack map's instruction address 27*9880d681SAndroid Build Coastguard Workerand contains a entry for each mapped value. Each entry encodes a 28*9880d681SAndroid Build Coastguard Workervalue's location as a register, stack offset, or constant. 29*9880d681SAndroid Build Coastguard Worker 30*9880d681SAndroid Build Coastguard WorkerA patch point is an instruction address at which space is reserved for 31*9880d681SAndroid Build Coastguard Workerpatching a new instruction sequence at run time. Patch points look 32*9880d681SAndroid Build Coastguard Workermuch like calls to LLVM. They take arguments that follow a calling 33*9880d681SAndroid Build Coastguard Workerconvention and may return a value. They also imply stack map 34*9880d681SAndroid Build Coastguard Workergeneration, which allows the runtime to locate the patchpoint and 35*9880d681SAndroid Build Coastguard Workerfind the location of ``live values`` at that point. 36*9880d681SAndroid Build Coastguard Worker 37*9880d681SAndroid Build Coastguard WorkerMotivation 38*9880d681SAndroid Build Coastguard Worker========== 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard WorkerThis functionality is currently experimental but is potentially useful 41*9880d681SAndroid Build Coastguard Workerin a variety of settings, the most obvious being a runtime (JIT) 42*9880d681SAndroid Build Coastguard Workercompiler. Example applications of the patchpoint intrinsics are 43*9880d681SAndroid Build Coastguard Workerimplementing an inline call cache for polymorphic method dispatch or 44*9880d681SAndroid Build Coastguard Workeroptimizing the retrieval of properties in dynamically typed languages 45*9880d681SAndroid Build Coastguard Workersuch as JavaScript. 46*9880d681SAndroid Build Coastguard Worker 47*9880d681SAndroid Build Coastguard WorkerThe intrinsics documented here are currently used by the JavaScript 48*9880d681SAndroid Build Coastguard Workercompiler within the open source WebKit project, see the `FTL JIT 49*9880d681SAndroid Build Coastguard Worker<https://trac.webkit.org/wiki/FTLJIT>`_, but they are designed to be 50*9880d681SAndroid Build Coastguard Workerused whenever stack maps or code patching are needed. Because the 51*9880d681SAndroid Build Coastguard Workerintrinsics have experimental status, compatibility across LLVM 52*9880d681SAndroid Build Coastguard Workerreleases is not guaranteed. 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard WorkerThe stack map functionality described in this document is separate 55*9880d681SAndroid Build Coastguard Workerfrom the functionality described in 56*9880d681SAndroid Build Coastguard Worker:ref:`stack-map`. `GCFunctionMetadata` provides the location of 57*9880d681SAndroid Build Coastguard Workerpointers into a collected heap captured by the `GCRoot` intrinsic, 58*9880d681SAndroid Build Coastguard Workerwhich can also be considered a "stack map". Unlike the stack maps 59*9880d681SAndroid Build Coastguard Workerdefined above, the `GCFunctionMetadata` stack map interface does not 60*9880d681SAndroid Build Coastguard Workerprovide a way to associate live register values of arbitrary type with 61*9880d681SAndroid Build Coastguard Workeran instruction address, nor does it specify a format for the resulting 62*9880d681SAndroid Build Coastguard Workerstack map. The stack maps described here could potentially provide 63*9880d681SAndroid Build Coastguard Workerricher information to a garbage collecting runtime, but that usage 64*9880d681SAndroid Build Coastguard Workerwill not be discussed in this document. 65*9880d681SAndroid Build Coastguard Worker 66*9880d681SAndroid Build Coastguard WorkerIntrinsics 67*9880d681SAndroid Build Coastguard Worker========== 68*9880d681SAndroid Build Coastguard Worker 69*9880d681SAndroid Build Coastguard WorkerThe following two kinds of intrinsics can be used to implement stack 70*9880d681SAndroid Build Coastguard Workermaps and patch points: ``llvm.experimental.stackmap`` and 71*9880d681SAndroid Build Coastguard Worker``llvm.experimental.patchpoint``. Both kinds of intrinsics generate a 72*9880d681SAndroid Build Coastguard Workerstack map record, and they both allow some form of code patching. They 73*9880d681SAndroid Build Coastguard Workercan be used independently (i.e. ``llvm.experimental.patchpoint`` 74*9880d681SAndroid Build Coastguard Workerimplicitly generates a stack map without the need for an additional 75*9880d681SAndroid Build Coastguard Workercall to ``llvm.experimental.stackmap``). The choice of which to use 76*9880d681SAndroid Build Coastguard Workerdepends on whether it is necessary to reserve space for code patching 77*9880d681SAndroid Build Coastguard Workerand whether any of the intrinsic arguments should be lowered according 78*9880d681SAndroid Build Coastguard Workerto calling conventions. ``llvm.experimental.stackmap`` does not 79*9880d681SAndroid Build Coastguard Workerreserve any space, nor does it expect any call arguments. If the 80*9880d681SAndroid Build Coastguard Workerruntime patches code at the stack map's address, it will destructively 81*9880d681SAndroid Build Coastguard Workeroverwrite the program text. This is unlike 82*9880d681SAndroid Build Coastguard Worker``llvm.experimental.patchpoint``, which reserves space for in-place 83*9880d681SAndroid Build Coastguard Workerpatching without overwriting surrounding code. The 84*9880d681SAndroid Build Coastguard Worker``llvm.experimental.patchpoint`` intrinsic also lowers a specified 85*9880d681SAndroid Build Coastguard Workernumber of arguments according to its calling convention. This allows 86*9880d681SAndroid Build Coastguard Workerpatched code to make in-place function calls without marshaling. 87*9880d681SAndroid Build Coastguard Worker 88*9880d681SAndroid Build Coastguard WorkerEach instance of one of these intrinsics generates a stack map record 89*9880d681SAndroid Build Coastguard Workerin the :ref:`stackmap-section`. The record includes an ID, allowing 90*9880d681SAndroid Build Coastguard Workerthe runtime to uniquely identify the stack map, and the offset within 91*9880d681SAndroid Build Coastguard Workerthe code from the beginning of the enclosing function. 92*9880d681SAndroid Build Coastguard Worker 93*9880d681SAndroid Build Coastguard Worker'``llvm.experimental.stackmap``' Intrinsic 94*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 95*9880d681SAndroid Build Coastguard Worker 96*9880d681SAndroid Build Coastguard WorkerSyntax: 97*9880d681SAndroid Build Coastguard Worker""""""" 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker:: 100*9880d681SAndroid Build Coastguard Worker 101*9880d681SAndroid Build Coastguard Worker declare void 102*9880d681SAndroid Build Coastguard Worker @llvm.experimental.stackmap(i64 <id>, i32 <numShadowBytes>, ...) 103*9880d681SAndroid Build Coastguard Worker 104*9880d681SAndroid Build Coastguard WorkerOverview: 105*9880d681SAndroid Build Coastguard Worker""""""""" 106*9880d681SAndroid Build Coastguard Worker 107*9880d681SAndroid Build Coastguard WorkerThe '``llvm.experimental.stackmap``' intrinsic records the location of 108*9880d681SAndroid Build Coastguard Workerspecified values in the stack map without generating any code. 109*9880d681SAndroid Build Coastguard Worker 110*9880d681SAndroid Build Coastguard WorkerOperands: 111*9880d681SAndroid Build Coastguard Worker""""""""" 112*9880d681SAndroid Build Coastguard Worker 113*9880d681SAndroid Build Coastguard WorkerThe first operand is an ID to be encoded within the stack map. The 114*9880d681SAndroid Build Coastguard Workersecond operand is the number of shadow bytes following the 115*9880d681SAndroid Build Coastguard Workerintrinsic. The variable number of operands that follow are the ``live 116*9880d681SAndroid Build Coastguard Workervalues`` for which locations will be recorded in the stack map. 117*9880d681SAndroid Build Coastguard Worker 118*9880d681SAndroid Build Coastguard WorkerTo use this intrinsic as a bare-bones stack map, with no code patching 119*9880d681SAndroid Build Coastguard Workersupport, the number of shadow bytes can be set to zero. 120*9880d681SAndroid Build Coastguard Worker 121*9880d681SAndroid Build Coastguard WorkerSemantics: 122*9880d681SAndroid Build Coastguard Worker"""""""""" 123*9880d681SAndroid Build Coastguard Worker 124*9880d681SAndroid Build Coastguard WorkerThe stack map intrinsic generates no code in place, unless nops are 125*9880d681SAndroid Build Coastguard Workerneeded to cover its shadow (see below). However, its offset from 126*9880d681SAndroid Build Coastguard Workerfunction entry is stored in the stack map. This is the relative 127*9880d681SAndroid Build Coastguard Workerinstruction address immediately following the instructions that 128*9880d681SAndroid Build Coastguard Workerprecede the stack map. 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard WorkerThe stack map ID allows a runtime to locate the desired stack map 131*9880d681SAndroid Build Coastguard Workerrecord. LLVM passes this ID through directly to the stack map 132*9880d681SAndroid Build Coastguard Workerrecord without checking uniqueness. 133*9880d681SAndroid Build Coastguard Worker 134*9880d681SAndroid Build Coastguard WorkerLLVM guarantees a shadow of instructions following the stack map's 135*9880d681SAndroid Build Coastguard Workerinstruction offset during which neither the end of the basic block nor 136*9880d681SAndroid Build Coastguard Workeranother call to ``llvm.experimental.stackmap`` or 137*9880d681SAndroid Build Coastguard Worker``llvm.experimental.patchpoint`` may occur. This allows the runtime to 138*9880d681SAndroid Build Coastguard Workerpatch the code at this point in response to an event triggered from 139*9880d681SAndroid Build Coastguard Workeroutside the code. The code for instructions following the stack map 140*9880d681SAndroid Build Coastguard Workermay be emitted in the stack map's shadow, and these instructions may 141*9880d681SAndroid Build Coastguard Workerbe overwritten by destructive patching. Without shadow bytes, this 142*9880d681SAndroid Build Coastguard Workerdestructive patching could overwrite program text or data outside the 143*9880d681SAndroid Build Coastguard Workercurrent function. We disallow overlapping stack map shadows so that 144*9880d681SAndroid Build Coastguard Workerthe runtime does not need to consider this corner case. 145*9880d681SAndroid Build Coastguard Worker 146*9880d681SAndroid Build Coastguard WorkerFor example, a stack map with 8 byte shadow: 147*9880d681SAndroid Build Coastguard Worker 148*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 149*9880d681SAndroid Build Coastguard Worker 150*9880d681SAndroid Build Coastguard Worker call void @runtime() 151*9880d681SAndroid Build Coastguard Worker call void (i64, i32, ...)* @llvm.experimental.stackmap(i64 77, i32 8, 152*9880d681SAndroid Build Coastguard Worker i64* %ptr) 153*9880d681SAndroid Build Coastguard Worker %val = load i64* %ptr 154*9880d681SAndroid Build Coastguard Worker %add = add i64 %val, 3 155*9880d681SAndroid Build Coastguard Worker ret i64 %add 156*9880d681SAndroid Build Coastguard Worker 157*9880d681SAndroid Build Coastguard WorkerMay require one byte of nop-padding: 158*9880d681SAndroid Build Coastguard Worker 159*9880d681SAndroid Build Coastguard Worker.. code-block:: none 160*9880d681SAndroid Build Coastguard Worker 161*9880d681SAndroid Build Coastguard Worker 0x00 callq _runtime 162*9880d681SAndroid Build Coastguard Worker 0x05 nop <--- stack map address 163*9880d681SAndroid Build Coastguard Worker 0x06 movq (%rdi), %rax 164*9880d681SAndroid Build Coastguard Worker 0x07 addq $3, %rax 165*9880d681SAndroid Build Coastguard Worker 0x0a popq %rdx 166*9880d681SAndroid Build Coastguard Worker 0x0b ret <---- end of 8-byte shadow 167*9880d681SAndroid Build Coastguard Worker 168*9880d681SAndroid Build Coastguard WorkerNow, if the runtime needs to invalidate the compiled code, it may 169*9880d681SAndroid Build Coastguard Workerpatch 8 bytes of code at the stack map's address at follows: 170*9880d681SAndroid Build Coastguard Worker 171*9880d681SAndroid Build Coastguard Worker.. code-block:: none 172*9880d681SAndroid Build Coastguard Worker 173*9880d681SAndroid Build Coastguard Worker 0x00 callq _runtime 174*9880d681SAndroid Build Coastguard Worker 0x05 movl $0xffff, %rax <--- patched code at stack map address 175*9880d681SAndroid Build Coastguard Worker 0x0a callq *%rax <---- end of 8-byte shadow 176*9880d681SAndroid Build Coastguard Worker 177*9880d681SAndroid Build Coastguard WorkerThis way, after the normal call to the runtime returns, the code will 178*9880d681SAndroid Build Coastguard Workerexecute a patched call to a special entry point that can rebuild a 179*9880d681SAndroid Build Coastguard Workerstack frame from the values located by the stack map. 180*9880d681SAndroid Build Coastguard Worker 181*9880d681SAndroid Build Coastguard Worker'``llvm.experimental.patchpoint.*``' Intrinsic 182*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 183*9880d681SAndroid Build Coastguard Worker 184*9880d681SAndroid Build Coastguard WorkerSyntax: 185*9880d681SAndroid Build Coastguard Worker""""""" 186*9880d681SAndroid Build Coastguard Worker 187*9880d681SAndroid Build Coastguard Worker:: 188*9880d681SAndroid Build Coastguard Worker 189*9880d681SAndroid Build Coastguard Worker declare void 190*9880d681SAndroid Build Coastguard Worker @llvm.experimental.patchpoint.void(i64 <id>, i32 <numBytes>, 191*9880d681SAndroid Build Coastguard Worker i8* <target>, i32 <numArgs>, ...) 192*9880d681SAndroid Build Coastguard Worker declare i64 193*9880d681SAndroid Build Coastguard Worker @llvm.experimental.patchpoint.i64(i64 <id>, i32 <numBytes>, 194*9880d681SAndroid Build Coastguard Worker i8* <target>, i32 <numArgs>, ...) 195*9880d681SAndroid Build Coastguard Worker 196*9880d681SAndroid Build Coastguard WorkerOverview: 197*9880d681SAndroid Build Coastguard Worker""""""""" 198*9880d681SAndroid Build Coastguard Worker 199*9880d681SAndroid Build Coastguard WorkerThe '``llvm.experimental.patchpoint.*``' intrinsics creates a function 200*9880d681SAndroid Build Coastguard Workercall to the specified ``<target>`` and records the location of specified 201*9880d681SAndroid Build Coastguard Workervalues in the stack map. 202*9880d681SAndroid Build Coastguard Worker 203*9880d681SAndroid Build Coastguard WorkerOperands: 204*9880d681SAndroid Build Coastguard Worker""""""""" 205*9880d681SAndroid Build Coastguard Worker 206*9880d681SAndroid Build Coastguard WorkerThe first operand is an ID, the second operand is the number of bytes 207*9880d681SAndroid Build Coastguard Workerreserved for the patchable region, the third operand is the target 208*9880d681SAndroid Build Coastguard Workeraddress of a function (optionally null), and the fourth operand 209*9880d681SAndroid Build Coastguard Workerspecifies how many of the following variable operands are considered 210*9880d681SAndroid Build Coastguard Workerfunction call arguments. The remaining variable number of operands are 211*9880d681SAndroid Build Coastguard Workerthe ``live values`` for which locations will be recorded in the stack 212*9880d681SAndroid Build Coastguard Workermap. 213*9880d681SAndroid Build Coastguard Worker 214*9880d681SAndroid Build Coastguard WorkerSemantics: 215*9880d681SAndroid Build Coastguard Worker"""""""""" 216*9880d681SAndroid Build Coastguard Worker 217*9880d681SAndroid Build Coastguard WorkerThe patch point intrinsic generates a stack map. It also emits a 218*9880d681SAndroid Build Coastguard Workerfunction call to the address specified by ``<target>`` if the address 219*9880d681SAndroid Build Coastguard Workeris not a constant null. The function call and its arguments are 220*9880d681SAndroid Build Coastguard Workerlowered according to the calling convention specified at the 221*9880d681SAndroid Build Coastguard Workerintrinsic's callsite. Variants of the intrinsic with non-void return 222*9880d681SAndroid Build Coastguard Workertype also return a value according to calling convention. 223*9880d681SAndroid Build Coastguard Worker 224*9880d681SAndroid Build Coastguard WorkerOn PowerPC, note that ``<target>`` must be the ABI function pointer for the 225*9880d681SAndroid Build Coastguard Workerintended target of the indirect call. Specifically, when compiling for the 226*9880d681SAndroid Build Coastguard WorkerELF V1 ABI, ``<target>`` is the function-descriptor address normally used as 227*9880d681SAndroid Build Coastguard Workerthe C/C++ function-pointer representation. 228*9880d681SAndroid Build Coastguard Worker 229*9880d681SAndroid Build Coastguard WorkerRequesting zero patch point arguments is valid. In this case, all 230*9880d681SAndroid Build Coastguard Workervariable operands are handled just like 231*9880d681SAndroid Build Coastguard Worker``llvm.experimental.stackmap.*``. The difference is that space will 232*9880d681SAndroid Build Coastguard Workerstill be reserved for patching, a call will be emitted, and a return 233*9880d681SAndroid Build Coastguard Workervalue is allowed. 234*9880d681SAndroid Build Coastguard Worker 235*9880d681SAndroid Build Coastguard WorkerThe location of the arguments are not normally recorded in the stack 236*9880d681SAndroid Build Coastguard Workermap because they are already fixed by the calling convention. The 237*9880d681SAndroid Build Coastguard Workerremaining ``live values`` will have their location recorded, which 238*9880d681SAndroid Build Coastguard Workercould be a register, stack location, or constant. A special calling 239*9880d681SAndroid Build Coastguard Workerconvention has been introduced for use with stack maps, anyregcc, 240*9880d681SAndroid Build Coastguard Workerwhich forces the arguments to be loaded into registers but allows 241*9880d681SAndroid Build Coastguard Workerthose register to be dynamically allocated. These argument registers 242*9880d681SAndroid Build Coastguard Workerwill have their register locations recorded in the stack map in 243*9880d681SAndroid Build Coastguard Workeraddition to the remaining ``live values``. 244*9880d681SAndroid Build Coastguard Worker 245*9880d681SAndroid Build Coastguard WorkerThe patch point also emits nops to cover at least ``<numBytes>`` of 246*9880d681SAndroid Build Coastguard Workerinstruction encoding space. Hence, the client must ensure that 247*9880d681SAndroid Build Coastguard Worker``<numBytes>`` is enough to encode a call to the target address on the 248*9880d681SAndroid Build Coastguard Workersupported targets. If the call target is constant null, then there is 249*9880d681SAndroid Build Coastguard Workerno minimum requirement. A zero-byte null target patchpoint is 250*9880d681SAndroid Build Coastguard Workervalid. 251*9880d681SAndroid Build Coastguard Worker 252*9880d681SAndroid Build Coastguard WorkerThe runtime may patch the code emitted for the patch point, including 253*9880d681SAndroid Build Coastguard Workerthe call sequence and nops. However, the runtime may not assume 254*9880d681SAndroid Build Coastguard Workeranything about the code LLVM emits within the reserved space. Partial 255*9880d681SAndroid Build Coastguard Workerpatching is not allowed. The runtime must patch all reserved bytes, 256*9880d681SAndroid Build Coastguard Workerpadding with nops if necessary. 257*9880d681SAndroid Build Coastguard Worker 258*9880d681SAndroid Build Coastguard WorkerThis example shows a patch point reserving 15 bytes, with one argument 259*9880d681SAndroid Build Coastguard Workerin $rdi, and a return value in $rax per native calling convention: 260*9880d681SAndroid Build Coastguard Worker 261*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 262*9880d681SAndroid Build Coastguard Worker 263*9880d681SAndroid Build Coastguard Worker %target = inttoptr i64 -281474976710654 to i8* 264*9880d681SAndroid Build Coastguard Worker %val = call i64 (i64, i32, ...)* 265*9880d681SAndroid Build Coastguard Worker @llvm.experimental.patchpoint.i64(i64 78, i32 15, 266*9880d681SAndroid Build Coastguard Worker i8* %target, i32 1, i64* %ptr) 267*9880d681SAndroid Build Coastguard Worker %add = add i64 %val, 3 268*9880d681SAndroid Build Coastguard Worker ret i64 %add 269*9880d681SAndroid Build Coastguard Worker 270*9880d681SAndroid Build Coastguard WorkerMay generate: 271*9880d681SAndroid Build Coastguard Worker 272*9880d681SAndroid Build Coastguard Worker.. code-block:: none 273*9880d681SAndroid Build Coastguard Worker 274*9880d681SAndroid Build Coastguard Worker 0x00 movabsq $0xffff000000000002, %r11 <--- patch point address 275*9880d681SAndroid Build Coastguard Worker 0x0a callq *%r11 276*9880d681SAndroid Build Coastguard Worker 0x0d nop 277*9880d681SAndroid Build Coastguard Worker 0x0e nop <--- end of reserved 15-bytes 278*9880d681SAndroid Build Coastguard Worker 0x0f addq $0x3, %rax 279*9880d681SAndroid Build Coastguard Worker 0x10 movl %rax, 8(%rsp) 280*9880d681SAndroid Build Coastguard Worker 281*9880d681SAndroid Build Coastguard WorkerNote that no stack map locations will be recorded. If the patched code 282*9880d681SAndroid Build Coastguard Workersequence does not need arguments fixed to specific calling convention 283*9880d681SAndroid Build Coastguard Workerregisters, then the ``anyregcc`` convention may be used: 284*9880d681SAndroid Build Coastguard Worker 285*9880d681SAndroid Build Coastguard Worker.. code-block:: none 286*9880d681SAndroid Build Coastguard Worker 287*9880d681SAndroid Build Coastguard Worker %val = call anyregcc @llvm.experimental.patchpoint(i64 78, i32 15, 288*9880d681SAndroid Build Coastguard Worker i8* %target, i32 1, 289*9880d681SAndroid Build Coastguard Worker i64* %ptr) 290*9880d681SAndroid Build Coastguard Worker 291*9880d681SAndroid Build Coastguard WorkerThe stack map now indicates the location of the %ptr argument and 292*9880d681SAndroid Build Coastguard Workerreturn value: 293*9880d681SAndroid Build Coastguard Worker 294*9880d681SAndroid Build Coastguard Worker.. code-block:: none 295*9880d681SAndroid Build Coastguard Worker 296*9880d681SAndroid Build Coastguard Worker Stack Map: ID=78, Loc0=%r9 Loc1=%r8 297*9880d681SAndroid Build Coastguard Worker 298*9880d681SAndroid Build Coastguard WorkerThe patch code sequence may now use the argument that happened to be 299*9880d681SAndroid Build Coastguard Workerallocated in %r8 and return a value allocated in %r9: 300*9880d681SAndroid Build Coastguard Worker 301*9880d681SAndroid Build Coastguard Worker.. code-block:: none 302*9880d681SAndroid Build Coastguard Worker 303*9880d681SAndroid Build Coastguard Worker 0x00 movslq 4(%r8) %r9 <--- patched code at patch point address 304*9880d681SAndroid Build Coastguard Worker 0x03 nop 305*9880d681SAndroid Build Coastguard Worker ... 306*9880d681SAndroid Build Coastguard Worker 0x0e nop <--- end of reserved 15-bytes 307*9880d681SAndroid Build Coastguard Worker 0x0f addq $0x3, %r9 308*9880d681SAndroid Build Coastguard Worker 0x10 movl %r9, 8(%rsp) 309*9880d681SAndroid Build Coastguard Worker 310*9880d681SAndroid Build Coastguard Worker.. _stackmap-format: 311*9880d681SAndroid Build Coastguard Worker 312*9880d681SAndroid Build Coastguard WorkerStack Map Format 313*9880d681SAndroid Build Coastguard Worker================ 314*9880d681SAndroid Build Coastguard Worker 315*9880d681SAndroid Build Coastguard WorkerThe existence of a stack map or patch point intrinsic within an LLVM 316*9880d681SAndroid Build Coastguard WorkerModule forces code emission to create a :ref:`stackmap-section`. The 317*9880d681SAndroid Build Coastguard Workerformat of this section follows: 318*9880d681SAndroid Build Coastguard Worker 319*9880d681SAndroid Build Coastguard Worker.. code-block:: none 320*9880d681SAndroid Build Coastguard Worker 321*9880d681SAndroid Build Coastguard Worker Header { 322*9880d681SAndroid Build Coastguard Worker uint8 : Stack Map Version (current version is 1) 323*9880d681SAndroid Build Coastguard Worker uint8 : Reserved (expected to be 0) 324*9880d681SAndroid Build Coastguard Worker uint16 : Reserved (expected to be 0) 325*9880d681SAndroid Build Coastguard Worker } 326*9880d681SAndroid Build Coastguard Worker uint32 : NumFunctions 327*9880d681SAndroid Build Coastguard Worker uint32 : NumConstants 328*9880d681SAndroid Build Coastguard Worker uint32 : NumRecords 329*9880d681SAndroid Build Coastguard Worker StkSizeRecord[NumFunctions] { 330*9880d681SAndroid Build Coastguard Worker uint64 : Function Address 331*9880d681SAndroid Build Coastguard Worker uint64 : Stack Size 332*9880d681SAndroid Build Coastguard Worker } 333*9880d681SAndroid Build Coastguard Worker Constants[NumConstants] { 334*9880d681SAndroid Build Coastguard Worker uint64 : LargeConstant 335*9880d681SAndroid Build Coastguard Worker } 336*9880d681SAndroid Build Coastguard Worker StkMapRecord[NumRecords] { 337*9880d681SAndroid Build Coastguard Worker uint64 : PatchPoint ID 338*9880d681SAndroid Build Coastguard Worker uint32 : Instruction Offset 339*9880d681SAndroid Build Coastguard Worker uint16 : Reserved (record flags) 340*9880d681SAndroid Build Coastguard Worker uint16 : NumLocations 341*9880d681SAndroid Build Coastguard Worker Location[NumLocations] { 342*9880d681SAndroid Build Coastguard Worker uint8 : Register | Direct | Indirect | Constant | ConstantIndex 343*9880d681SAndroid Build Coastguard Worker uint8 : Reserved (location flags) 344*9880d681SAndroid Build Coastguard Worker uint16 : Dwarf RegNum 345*9880d681SAndroid Build Coastguard Worker int32 : Offset or SmallConstant 346*9880d681SAndroid Build Coastguard Worker } 347*9880d681SAndroid Build Coastguard Worker uint16 : Padding 348*9880d681SAndroid Build Coastguard Worker uint16 : NumLiveOuts 349*9880d681SAndroid Build Coastguard Worker LiveOuts[NumLiveOuts] 350*9880d681SAndroid Build Coastguard Worker uint16 : Dwarf RegNum 351*9880d681SAndroid Build Coastguard Worker uint8 : Reserved 352*9880d681SAndroid Build Coastguard Worker uint8 : Size in Bytes 353*9880d681SAndroid Build Coastguard Worker } 354*9880d681SAndroid Build Coastguard Worker uint32 : Padding (only if required to align to 8 byte) 355*9880d681SAndroid Build Coastguard Worker } 356*9880d681SAndroid Build Coastguard Worker 357*9880d681SAndroid Build Coastguard WorkerThe first byte of each location encodes a type that indicates how to 358*9880d681SAndroid Build Coastguard Workerinterpret the ``RegNum`` and ``Offset`` fields as follows: 359*9880d681SAndroid Build Coastguard Worker 360*9880d681SAndroid Build Coastguard Worker======== ========== =================== =========================== 361*9880d681SAndroid Build Coastguard WorkerEncoding Type Value Description 362*9880d681SAndroid Build Coastguard Worker-------- ---------- ------------------- --------------------------- 363*9880d681SAndroid Build Coastguard Worker0x1 Register Reg Value in a register 364*9880d681SAndroid Build Coastguard Worker0x2 Direct Reg + Offset Frame index value 365*9880d681SAndroid Build Coastguard Worker0x3 Indirect [Reg + Offset] Spilled value 366*9880d681SAndroid Build Coastguard Worker0x4 Constant Offset Small constant 367*9880d681SAndroid Build Coastguard Worker0x5 ConstIndex Constants[Offset] Large constant 368*9880d681SAndroid Build Coastguard Worker======== ========== =================== =========================== 369*9880d681SAndroid Build Coastguard Worker 370*9880d681SAndroid Build Coastguard WorkerIn the common case, a value is available in a register, and the 371*9880d681SAndroid Build Coastguard Worker``Offset`` field will be zero. Values spilled to the stack are encoded 372*9880d681SAndroid Build Coastguard Workeras ``Indirect`` locations. The runtime must load those values from a 373*9880d681SAndroid Build Coastguard Workerstack address, typically in the form ``[BP + Offset]``. If an 374*9880d681SAndroid Build Coastguard Worker``alloca`` value is passed directly to a stack map intrinsic, then 375*9880d681SAndroid Build Coastguard WorkerLLVM may fold the frame index into the stack map as an optimization to 376*9880d681SAndroid Build Coastguard Workeravoid allocating a register or stack slot. These frame indices will be 377*9880d681SAndroid Build Coastguard Workerencoded as ``Direct`` locations in the form ``BP + Offset``. LLVM may 378*9880d681SAndroid Build Coastguard Workeralso optimize constants by emitting them directly in the stack map, 379*9880d681SAndroid Build Coastguard Workereither in the ``Offset`` of a ``Constant`` location or in the constant 380*9880d681SAndroid Build Coastguard Workerpool, referred to by ``ConstantIndex`` locations. 381*9880d681SAndroid Build Coastguard Worker 382*9880d681SAndroid Build Coastguard WorkerAt each callsite, a "liveout" register list is also recorded. These 383*9880d681SAndroid Build Coastguard Workerare the registers that are live across the stackmap and therefore must 384*9880d681SAndroid Build Coastguard Workerbe saved by the runtime. This is an important optimization when the 385*9880d681SAndroid Build Coastguard Workerpatchpoint intrinsic is used with a calling convention that by default 386*9880d681SAndroid Build Coastguard Workerpreserves most registers as callee-save. 387*9880d681SAndroid Build Coastguard Worker 388*9880d681SAndroid Build Coastguard WorkerEach entry in the liveout register list contains a DWARF register 389*9880d681SAndroid Build Coastguard Workernumber and size in bytes. The stackmap format deliberately omits 390*9880d681SAndroid Build Coastguard Workerspecific subregister information. Instead the runtime must interpret 391*9880d681SAndroid Build Coastguard Workerthis information conservatively. For example, if the stackmap reports 392*9880d681SAndroid Build Coastguard Workerone byte at ``%rax``, then the value may be in either ``%al`` or 393*9880d681SAndroid Build Coastguard Worker``%ah``. It doesn't matter in practice, because the runtime will 394*9880d681SAndroid Build Coastguard Workersimply save ``%rax``. However, if the stackmap reports 16 bytes at 395*9880d681SAndroid Build Coastguard Worker``%ymm0``, then the runtime can safely optimize by saving only 396*9880d681SAndroid Build Coastguard Worker``%xmm0``. 397*9880d681SAndroid Build Coastguard Worker 398*9880d681SAndroid Build Coastguard WorkerThe stack map format is a contract between an LLVM SVN revision and 399*9880d681SAndroid Build Coastguard Workerthe runtime. It is currently experimental and may change in the short 400*9880d681SAndroid Build Coastguard Workerterm, but minimizing the need to update the runtime is 401*9880d681SAndroid Build Coastguard Workerimportant. Consequently, the stack map design is motivated by 402*9880d681SAndroid Build Coastguard Workersimplicity and extensibility. Compactness of the representation is 403*9880d681SAndroid Build Coastguard Workersecondary because the runtime is expected to parse the data 404*9880d681SAndroid Build Coastguard Workerimmediately after compiling a module and encode the information in its 405*9880d681SAndroid Build Coastguard Workerown format. Since the runtime controls the allocation of sections, it 406*9880d681SAndroid Build Coastguard Workercan reuse the same stack map space for multiple modules. 407*9880d681SAndroid Build Coastguard Worker 408*9880d681SAndroid Build Coastguard WorkerStackmap support is currently only implemented for 64-bit 409*9880d681SAndroid Build Coastguard Workerplatforms. However, a 32-bit implementation should be able to use the 410*9880d681SAndroid Build Coastguard Workersame format with an insignificant amount of wasted space. 411*9880d681SAndroid Build Coastguard Worker 412*9880d681SAndroid Build Coastguard Worker.. _stackmap-section: 413*9880d681SAndroid Build Coastguard Worker 414*9880d681SAndroid Build Coastguard WorkerStack Map Section 415*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^ 416*9880d681SAndroid Build Coastguard Worker 417*9880d681SAndroid Build Coastguard WorkerA JIT compiler can easily access this section by providing its own 418*9880d681SAndroid Build Coastguard Workermemory manager via the LLVM C API 419*9880d681SAndroid Build Coastguard Worker``LLVMCreateSimpleMCJITMemoryManager()``. When creating the memory 420*9880d681SAndroid Build Coastguard Workermanager, the JIT provides a callback: 421*9880d681SAndroid Build Coastguard Worker``LLVMMemoryManagerAllocateDataSectionCallback()``. When LLVM creates 422*9880d681SAndroid Build Coastguard Workerthis section, it invokes the callback and passes the section name. The 423*9880d681SAndroid Build Coastguard WorkerJIT can record the in-memory address of the section at this time and 424*9880d681SAndroid Build Coastguard Workerlater parse it to recover the stack map data. 425*9880d681SAndroid Build Coastguard Worker 426*9880d681SAndroid Build Coastguard WorkerOn Darwin, the stack map section name is "__llvm_stackmaps". The 427*9880d681SAndroid Build Coastguard Workersegment name is "__LLVM_STACKMAPS". 428*9880d681SAndroid Build Coastguard Worker 429*9880d681SAndroid Build Coastguard WorkerStack Map Usage 430*9880d681SAndroid Build Coastguard Worker=============== 431*9880d681SAndroid Build Coastguard Worker 432*9880d681SAndroid Build Coastguard WorkerThe stack map support described in this document can be used to 433*9880d681SAndroid Build Coastguard Workerprecisely determine the location of values at a specific position in 434*9880d681SAndroid Build Coastguard Workerthe code. LLVM does not maintain any mapping between those values and 435*9880d681SAndroid Build Coastguard Workerany higher-level entity. The runtime must be able to interpret the 436*9880d681SAndroid Build Coastguard Workerstack map record given only the ID, offset, and the order of the 437*9880d681SAndroid Build Coastguard Workerlocations, which LLVM preserves. 438*9880d681SAndroid Build Coastguard Worker 439*9880d681SAndroid Build Coastguard WorkerNote that this is quite different from the goal of debug information, 440*9880d681SAndroid Build Coastguard Workerwhich is a best-effort attempt to track the location of named 441*9880d681SAndroid Build Coastguard Workervariables at every instruction. 442*9880d681SAndroid Build Coastguard Worker 443*9880d681SAndroid Build Coastguard WorkerAn important motivation for this design is to allow a runtime to 444*9880d681SAndroid Build Coastguard Workercommandeer a stack frame when execution reaches an instruction address 445*9880d681SAndroid Build Coastguard Workerassociated with a stack map. The runtime must be able to rebuild a 446*9880d681SAndroid Build Coastguard Workerstack frame and resume program execution using the information 447*9880d681SAndroid Build Coastguard Workerprovided by the stack map. For example, execution may resume in an 448*9880d681SAndroid Build Coastguard Workerinterpreter or a recompiled version of the same function. 449*9880d681SAndroid Build Coastguard Worker 450*9880d681SAndroid Build Coastguard WorkerThis usage restricts LLVM optimization. Clearly, LLVM must not move 451*9880d681SAndroid Build Coastguard Workerstores across a stack map. However, loads must also be handled 452*9880d681SAndroid Build Coastguard Workerconservatively. If the load may trigger an exception, hoisting it 453*9880d681SAndroid Build Coastguard Workerabove a stack map could be invalid. For example, the runtime may 454*9880d681SAndroid Build Coastguard Workerdetermine that a load is safe to execute without a type check given 455*9880d681SAndroid Build Coastguard Workerthe current state of the type system. If the type system changes while 456*9880d681SAndroid Build Coastguard Workersome activation of the load's function exists on the stack, the load 457*9880d681SAndroid Build Coastguard Workerbecomes unsafe. The runtime can prevent subsequent execution of that 458*9880d681SAndroid Build Coastguard Workerload by immediately patching any stack map location that lies between 459*9880d681SAndroid Build Coastguard Workerthe current call site and the load (typically, the runtime would 460*9880d681SAndroid Build Coastguard Workersimply patch all stack map locations to invalidate the function). If 461*9880d681SAndroid Build Coastguard Workerthe compiler had hoisted the load above the stack map, then the 462*9880d681SAndroid Build Coastguard Workerprogram could crash before the runtime could take back control. 463*9880d681SAndroid Build Coastguard Worker 464*9880d681SAndroid Build Coastguard WorkerTo enforce these semantics, stackmap and patchpoint intrinsics are 465*9880d681SAndroid Build Coastguard Workerconsidered to potentially read and write all memory. This may limit 466*9880d681SAndroid Build Coastguard Workeroptimization more than some clients desire. This limitation may be 467*9880d681SAndroid Build Coastguard Workeravoided by marking the call site as "readonly". In the future we may 468*9880d681SAndroid Build Coastguard Workeralso allow meta-data to be added to the intrinsic call to express 469*9880d681SAndroid Build Coastguard Workeraliasing, thereby allowing optimizations to hoist certain loads above 470*9880d681SAndroid Build Coastguard Workerstack maps. 471*9880d681SAndroid Build Coastguard Worker 472*9880d681SAndroid Build Coastguard WorkerDirect Stack Map Entries 473*9880d681SAndroid Build Coastguard Worker^^^^^^^^^^^^^^^^^^^^^^^^ 474*9880d681SAndroid Build Coastguard Worker 475*9880d681SAndroid Build Coastguard WorkerAs shown in :ref:`stackmap-section`, a Direct stack map location 476*9880d681SAndroid Build Coastguard Workerrecords the address of frame index. This address is itself the value 477*9880d681SAndroid Build Coastguard Workerthat the runtime requested. This differs from Indirect locations, 478*9880d681SAndroid Build Coastguard Workerwhich refer to a stack locations from which the requested values must 479*9880d681SAndroid Build Coastguard Workerbe loaded. Direct locations can communicate the address if an alloca, 480*9880d681SAndroid Build Coastguard Workerwhile Indirect locations handle register spills. 481*9880d681SAndroid Build Coastguard Worker 482*9880d681SAndroid Build Coastguard WorkerFor example: 483*9880d681SAndroid Build Coastguard Worker 484*9880d681SAndroid Build Coastguard Worker.. code-block:: none 485*9880d681SAndroid Build Coastguard Worker 486*9880d681SAndroid Build Coastguard Worker entry: 487*9880d681SAndroid Build Coastguard Worker %a = alloca i64... 488*9880d681SAndroid Build Coastguard Worker llvm.experimental.stackmap(i64 <ID>, i32 <shadowBytes>, i64* %a) 489*9880d681SAndroid Build Coastguard Worker 490*9880d681SAndroid Build Coastguard WorkerThe runtime can determine this alloca's relative location on the 491*9880d681SAndroid Build Coastguard Workerstack immediately after compilation, or at any time thereafter. This 492*9880d681SAndroid Build Coastguard Workerdiffers from Register and Indirect locations, because the runtime can 493*9880d681SAndroid Build Coastguard Workeronly read the values in those locations when execution reaches the 494*9880d681SAndroid Build Coastguard Workerinstruction address of the stack map. 495*9880d681SAndroid Build Coastguard Worker 496*9880d681SAndroid Build Coastguard WorkerThis functionality requires LLVM to treat entry-block allocas 497*9880d681SAndroid Build Coastguard Workerspecially when they are directly consumed by an intrinsics. (This is 498*9880d681SAndroid Build Coastguard Workerthe same requirement imposed by the llvm.gcroot intrinsic.) LLVM 499*9880d681SAndroid Build Coastguard Workertransformations must not substitute the alloca with any intervening 500*9880d681SAndroid Build Coastguard Workervalue. This can be verified by the runtime simply by checking that the 501*9880d681SAndroid Build Coastguard Workerstack map's location is a Direct location type. 502*9880d681SAndroid Build Coastguard Worker 503*9880d681SAndroid Build Coastguard Worker 504*9880d681SAndroid Build Coastguard WorkerSupported Architectures 505*9880d681SAndroid Build Coastguard Worker======================= 506*9880d681SAndroid Build Coastguard Worker 507*9880d681SAndroid Build Coastguard WorkerSupport for StackMap generation and the related intrinsics requires 508*9880d681SAndroid Build Coastguard Workersome code for each backend. Today, only a subset of LLVM's backends 509*9880d681SAndroid Build Coastguard Workerare supported. The currently supported architectures are X86_64, 510*9880d681SAndroid Build Coastguard WorkerPowerPC, and Aarch64. 511*9880d681SAndroid Build Coastguard Worker 512