Skip to content

Commit

Permalink
fix: immix allocation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronostasys committed Dec 29, 2024
1 parent 1272999 commit 6d5edf8
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 383 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,17 @@
"cwd": "${workspaceFolder}",
"env": {
// "GC_LOG": "info"
// "PL_IMMIX_HEAP_SIZE": "80000000"
}
// "stopOnEntry": true
},
{
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "out",
"MIMode": "lldb"
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
Expand Down
55 changes: 12 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ members = ["internal_macro", "vm", "pl_linker", "immix", "kagari"]
[profile.release]
lto = "fat"
opt-level = 3
# debug = "line-tables-only"
debug = "line-tables-only"

[profile.bench]
opt-level = 3
Expand Down
144 changes: 63 additions & 81 deletions alloc-aarch64.ll
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ define double @sqrt_64(double %Val) {
; define new DioGC__malloc
define ptr addrspace(1) @DioGC__malloc(i64 %size, i8 %obj_type, i64 %rsp) noinline optnone allockind("alloc") {
entry:
; if size > 7936, call slowpath
%size_gt_7936 = icmp ugt i64 %size, 7936
br i1 %size_gt_7936, label %call_slowpath, label %check_collector
; if size > 128, call slowpath
; call void @printi64ln(i64 2222)
%size_gt_128 = icmp ugt i64 %size, 128
br i1 %size_gt_128, label %call_slowpath, label %check_collector
check_collector:
; Load collector from gc_handle
%collector_ptr = load ptr, ptr @gc_handle, align 8, !invariant.load !0
Expand All @@ -62,108 +63,89 @@ call_slowpath:
%innerrsp = tail call ptr asm alignstack "mov $0, sp", "=r"() #0
%rspi = ptrtoint ptr %innerrsp to i64
%slowpath_result = call ptr addrspace(1) @DioGC__malloc_slowpath(i64 %size, i8 %obj_type, i64 %rspi, ptr @gc_handle)
; call void @printi64ln(i64 999)
; %slowpath_result_i = ptrtoint ptr addrspace(1) %slowpath_result to i64
; call void @printi64ln(i64 %slowpath_result_i)
ret ptr addrspace(1) %slowpath_result
fastpath_start:
; call void @printi64ln(i64 1)
%thread_local_allocator_ptr = load ptr, ptr %collector_ptr, align 8

; get second field of collector, which is bytes_allocated_since_last_gc
%bytes_allocated_since_last_gc_ptr = getelementptr i64, ptr %collector_ptr, i32 1
; ; get second field of collector, which is bytes_allocated_since_last_gc
; %bytes_allocated_since_last_gc_ptr = getelementptr i64, ptr %collector_ptr, i32 1

; Get thread_local_allocator (first field)
%block = load ptr addrspace(1), ptr %thread_local_allocator_ptr, align 8

; Load block fields
%cursor_ptr = getelementptr i64, ptr addrspace(1) %block, i32 0
%cursor = load i64, ptr addrspace(1) %cursor_ptr, align 8

%cursor = load ptr addrspace(1), ptr addrspace(1) %cursor_ptr, align 8
; call void @printi64ln(i64 2)
; ; if cursor > 256, call slowpath
; %cursor_gt_256 = icmp ugt i64 %cursor, 255
; br i1 %cursor_gt_256, label %call_slowpath, label %load_block_fields_2

; load_block_fields_2:

%next_hole_size_ptr = getelementptr i64, ptr addrspace(1) %block, i32 1
%next_hole_size = load i64, ptr addrspace(1) %next_hole_size_ptr, align 8

%available_line_num_ptr = getelementptr i64, ptr addrspace(1) %block, i32 2
%available_line_num = load i64, ptr addrspace(1) %available_line_num_ptr, align 8
%hole_end_ptr = getelementptr i64, ptr addrspace(1) %block, i32 1
%hole_end = load ptr addrspace(1), ptr addrspace(1) %hole_end_ptr, align 8
; call void @printi64ln(i64 3)

%hole_num_ptr = getelementptr i64, ptr addrspace(1) %block, i32 3
%hole_num = load i64, ptr addrspace(1) %hole_num_ptr, align 8



; Calculate line_size = (size - 1) / LINE_SIZE + 1
; Calculate alloc size = (size + 7) / 8 * 8
; LINE_SIZE is 128
%size_minus_1 = sub i64 %size, 1
%div = udiv i64 %size_minus_1, 128
%line_size = add i64 %div, 1

; Check if fast path is possible (next_hole_size >= line_size)
%fast_path_possible = icmp uge i64 %next_hole_size, %line_size
br i1 %fast_path_possible, label %fast_path, label %call_slowpath
%size_plus_7 = add i64 %size, 7
%size_div_8 = lshr i64 %size_plus_7, 3
%alloc_size = shl i64 %size_div_8, 3

fast_path:
; Update available_line_num
%new_available = sub i64 %available_line_num, %line_size
store i64 %new_available, ptr addrspace(1) %available_line_num_ptr, align 8

; Get line map pointer (after the first three fields)
%line_map_ptr = getelementptr i64, ptr addrspace(1) %block, i32 4

; Mark lines as used and set object type for first line
%first_line_ptr = getelementptr i8, ptr addrspace(1) %line_map_ptr, i64 %cursor
; Set object type and mark as used (obj_type << 2 | 0b10000001)
%shifted_type = shl i8 %obj_type, 2
%header_val = or i8 %shifted_type, 129 ; 129 = 0b10000001
store i8 %header_val, ptr addrspace(1) %first_line_ptr, align 1


%cursor_i64 = ptrtoint ptr addrspace(1) %cursor to i64
%hole_end_i64 = ptrtoint ptr addrspace(1) %hole_end to i64
%hole_end_minus_cursor = sub i64 %hole_end_i64, %cursor_i64
; check if hole_end - cursor >= alloc_size
%hole_end_minus_cursor_ge_alloc_size = icmp sge i64 %hole_end_minus_cursor, %alloc_size
br i1 %hole_end_minus_cursor_ge_alloc_size, label %check_current_line, label %call_slowpath

; Check if line_size is 1
%is_one_line = icmp eq i64 %line_size, 1
br i1 %is_one_line, label %finish_fast_path, label %mark_lines_start

mark_lines_start:
; Mark remaining lines as used (0b00000001)
%next_cursor = add i64 %cursor, 1
%end_cursor = add i64 %cursor, %line_size
br label %mark_lines

mark_lines:
%current = phi i64 [ %next, %mark_lines ], [ %next_cursor, %mark_lines_start ]
%line_ptr = getelementptr i8, ptr addrspace(1) %line_map_ptr, i64 %current
store i8 1, ptr addrspace(1) %line_ptr, align 1
%next = add i64 %current, 1
%continue = icmp ult i64 %next, %end_cursor
br i1 %continue, label %mark_lines, label %finish_fast_path

finish_fast_path:
; Calculate return address (block + cursor * LINE_SIZE)
%base_offset = mul i64 %cursor, 128
%result_addr = getelementptr i8, ptr addrspace(1) %block, i64 %base_offset
check_current_line:
; Check if alloc in current line is possible
; let current_line_remains = self.cursor.align_offset(LINE_SIZE);
%current_line_occupied = and i64 %cursor_i64, 127
%current_line_remains = sub i64 128, %current_line_occupied


; call void @printi64ln(i64 %current_line_remains)
; call void @printi64ln(i64 %alloc_size)
; check if alloc_size <= current_line_remains && current_line_remains != 0
%alloc_size_le_remains = icmp ule i64 %alloc_size, %current_line_remains
%current_line_remains_ne_0 = icmp ne i64 %current_line_remains, 0
%alloc_size_le_remains_and_ne_0 = and i1 %alloc_size_le_remains, %current_line_remains_ne_0
br i1 %alloc_size_le_remains_and_ne_0, label %fast_path, label %check_remaining

check_remaining:
; Check if 128 <= hole_end - cursor
%hole_end_minus_cursor_ge_128 = icmp uge i64 %hole_end_minus_cursor, 128

; self.cursor = self.cursor.add(current_line_remains);
%new_cursor_i = add i64 %cursor_i64, %current_line_remains
%new_cursor = inttoptr i64 %new_cursor_i to ptr addrspace(1)
br i1 %hole_end_minus_cursor_ge_128, label %fast_path, label %call_slowpath


fast_path:
; phi get cursor
%cursor_phi = phi ptr addrspace(1) [ %cursor, %check_current_line ], [ %new_cursor, %check_remaining ]

%cursor_phi_i = ptrtoint ptr addrspace(1) %cursor_phi to i64
; Update cursor
%new_cursor = add i64 %cursor, %line_size
store i64 %new_cursor, ptr addrspace(1) %cursor_ptr, align 8

; Update next_hole_size
%new_hole_size = sub i64 %next_hole_size, %line_size
store i64 %new_hole_size, ptr addrspace(1) %next_hole_size_ptr, align 8

; if new_hole_size == 0, update hole_num to hole_num - 1
%hole_size_eq_0 = icmp eq i64 %new_hole_size, 0
br i1 %hole_size_eq_0, label %update_hole_num, label %finish_fast_path_2

update_hole_num:
%new_hole_num = sub i64 %hole_num, 1
store i64 %new_hole_num, ptr addrspace(1) %hole_num_ptr, align 8
br label %finish_fast_path_2

finish_fast_path_2:
; Update bytes_allocated_since_last_gc (the size should be line_size * 128)
%bytes_allocated_since_last_gc = load i64, ptr %bytes_allocated_since_last_gc_ptr, align 8
%size_128 = mul i64 %line_size, 128
%new_bytes_allocated = add i64 %size_128, %bytes_allocated_since_last_gc
store i64 %new_bytes_allocated, ptr %bytes_allocated_since_last_gc_ptr, align 8
ret ptr addrspace(1) %result_addr
%new_cursor_after_alloc_i = add i64 %cursor_phi_i, %alloc_size
%new_cursor_after_alloc = inttoptr i64 %new_cursor_after_alloc_i to ptr addrspace(1)
store ptr addrspace(1) %new_cursor_after_alloc, ptr addrspace(1) %cursor_ptr, align 8
; call void @printi64ln(i64 4)
; call void @printi64ln(i64 %cursor_phi_i)
ret ptr addrspace(1) %cursor_phi
}


Expand Down
Loading

0 comments on commit 6d5edf8

Please sign in to comment.