Skip to content

Commit 43c7d0f

Browse files
flambda-backend: Enable locals for arm64 (#2442)
* Implement local allocation for arm64 This patch implements local allocation by adding code emission for local allocation, regions and assembly routine for calling stack relocation. (cherry picked from commit 0694909) * Minor fixes to backend/arm64/emit.mlp * Copy runtime5 implementation of caml_call_local_realloc to runtime4 * Revert ocaml/asmcomp changes * Code size estimate for local allocs * Line lengths * Tweak boundary condition for allocation to match amd64 emitter * New runtime5 implementation for caml_call_local_realloc --------- Co-authored-by: Anmol Sahoo <[email protected]>
1 parent b0a712c commit 43c7d0f

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

runtime/arm64.S

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,29 @@ FUNCTION(caml_allocN)
418418
CFI_ENDPROC
419419
END_FUNCTION(caml_allocN)
420420

421+
/* Reallocate the locals stack. This is like caml_call_gc, above. */
422+
FUNCTION(caml_call_local_realloc)
423+
CFI_STARTPROC
424+
L(caml_call_local_realloc):
425+
/* Save return address and frame pointer */
426+
CFI_OFFSET(29, -16)
427+
CFI_OFFSET(30, -8)
428+
stp x29, x30, [sp, -16]!
429+
CFI_ADJUST(16)
430+
add x29, sp, #0
431+
/* Store all registers (including ALLOC_PTR & TRAP_PTR) */
432+
SAVE_ALL_REGS
433+
SWITCH_OCAML_TO_C
434+
/* Call the runtime to reallocate the local stack */
435+
bl G(caml_local_realloc)
436+
SWITCH_C_TO_OCAML
437+
RESTORE_ALL_REGS
438+
/* Free stack space and return to caller */
439+
ldp x29, x30, [sp], 16
440+
ret
441+
CFI_ENDPROC
442+
END_FUNCTION(caml_call_gc)
443+
421444
/* Call a C function from OCaml */
422445
/* Function to call is in ADDITIONAL_ARG */
423446

runtime4/arm64.S

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,91 @@ FUNCTION(caml_allocN)
280280
CFI_ENDPROC
281281
END_FUNCTION(caml_allocN)
282282

283+
FUNCTION(caml_call_local_realloc)
284+
L(caml_call_local_realloc):
285+
CFI_STARTPROC
286+
/* Set up stack space, saving return address and frame pointer */
287+
/* Store return address and frame pointer */
288+
/* (2 RA/GP, 24 allocatable int regs, 24 caller-saved float regs) * 8 */
289+
CFI_OFFSET(29,-400)
290+
CFI_OFFSET(30,-392)
291+
stp x29, x30, [sp,-400]! /* pre-indexing stp */
292+
CFI_ADJUST(400)
293+
add x29, sp, #0
294+
295+
/* Save allocatable integer registers on the stack, using order in proc.ml */
296+
stp x0, x1, [sp, 16]
297+
stp x2, x3, [sp, 32]
298+
stp x4, x5, [sp, 48]
299+
stp x6, x7, [sp, 64]
300+
stp x8, x9, [sp, 80]
301+
stp x10, x11, [sp, 96]
302+
stp x12, x13, [sp, 112]
303+
stp x14, x15, [sp, 128]
304+
stp x19, x20, [sp, 144]
305+
stp x21, x22, [sp, 160]
306+
stp x23, x24, [sp, 176]
307+
str x25, [sp, 192]
308+
309+
/* Save caller saved floating-point registers on the stack */
310+
stp d0, d1, [sp, 208]
311+
stp d2, d3, [sp, 224]
312+
stp d4, d5, [sp, 240]
313+
stp d6, d7, [sp, 256]
314+
stp d16, d17, [sp, 272]
315+
stp d18, d19, [sp, 288]
316+
stp d20, d21, [sp, 304]
317+
stp d22, d23, [sp, 320]
318+
stp d24, d25, [sp, 336]
319+
stp d26, d27, [sp, 352]
320+
stp d28, d29, [sp, 368]
321+
stp d30, d31, [sp, 384]
322+
323+
/* Store pointer to saved integer registers in Caml_state->gc_regs */
324+
add TMP, sp, #16
325+
str TMP, Caml_state(gc_regs)
326+
327+
/* Save current allocation pointer for debugging purposes */
328+
str ALLOC_PTR, Caml_state(young_ptr)
329+
330+
/* Call the realloc function */
331+
bl G(caml_local_realloc)
332+
333+
/* Restore registers */
334+
ldp x0, x1, [sp, 16]
335+
ldp x2, x3, [sp, 32]
336+
ldp x4, x5, [sp, 48]
337+
ldp x6, x7, [sp, 64]
338+
ldp x8, x9, [sp, 80]
339+
ldp x10, x11, [sp, 96]
340+
ldp x12, x13, [sp, 112]
341+
ldp x14, x15, [sp, 128]
342+
ldp x19, x20, [sp, 144]
343+
ldp x21, x22, [sp, 160]
344+
ldp x23, x24, [sp, 176]
345+
ldr x25, [sp, 192]
346+
ldp d0, d1, [sp, 208]
347+
ldp d2, d3, [sp, 224]
348+
ldp d4, d5, [sp, 240]
349+
ldp d6, d7, [sp, 256]
350+
ldp d16, d17, [sp, 272]
351+
ldp d18, d19, [sp, 288]
352+
ldp d20, d21, [sp, 304]
353+
ldp d22, d23, [sp, 320]
354+
ldp d24, d25, [sp, 336]
355+
ldp d26, d27, [sp, 352]
356+
ldp d28, d29, [sp, 368]
357+
ldp d30, d31, [sp, 384]
358+
359+
/* Reload new allocation pointer */
360+
ldr ALLOC_PTR, Caml_state(young_ptr)
361+
362+
/* Free stack space and return to caller */
363+
ldp x29, x30, [sp], 400
364+
ret
365+
CFI_ENDPROC
366+
END_FUNCTION(caml_call_local_realloc)
367+
283368
/* Call a C function from OCaml */
284369
/* Function to call is in ADDITIONAL_ARG */
285370

0 commit comments

Comments
 (0)