Commit 2238b26d
2238b26d2f8fMerged
fc-anjos <[email protected]> · 6 months ago · 9 files · +4 added · ~5 modified
Description
root value-type fields in codegen
Co-Authored-By: OpenAI Codex 5.5
Details
- Commit
- 2238b26d2f8f
- Parent
- 45fae0a0846a
spinel_codegen.rbmodifiedView file
@@ -3494,13 +3494,10 @@
34943494
# element types must be marked, otherwise the GC frees the inner object34953495
# while the tuple keeps a dangling pointer.34963496
def tuple_field_needs_mark(et)3497-
if et == "poly"3497+
if gc_trace_kind(et) != "none"34983498
return 134993499
end3500-
if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"3501-
return 03502-
end3503-
type_is_pointer(et)3500+
035043501
end35053502
35063503
# Returns the scan function name for the tuple, or "NULL" if no field@@ -4867,8 +4864,12 @@
48674864
emit(" SP_GC_SAVE();")48684865
@in_gc_scope = 148694866
end4870-
if @needs_gc == 1 && @cls_is_value_type[ci] == 04871-
emit(" SP_GC_ROOT(self);")4867+
if @needs_gc == 14868+
if @cls_is_value_type[ci] == 04869+
emit(" SP_GC_ROOT(self);")4870+
else4871+
emit_gc_root_for_expr("self", "obj_" + cname)4872+
end48724873
end48734874
compile_body_return(bid, ret_type)48744875
end@@ -7136,6 +7137,7 @@
71367137
# forward declaration.71377138
emit_class_runtime71387139
emit_class_structs7140+
emit_gc_scan_function_prototypes71397141
emit_raw("/*TUPLE_INSERT_POINT*/")71407142
emit_gc_scan_functions71417143
emit_toplevel_ivar_decls@@ -7221,10 +7223,9 @@
72217223
while fi < parts.length72227224
if tuple_field_needs_mark(parts[fi]) == 172237225
field = "_t->_" + fi.to_s7224-
if parts[fi] == "poly"7225-
body = body + " sp_mark_rbval(" + field + ");"7226-
else7227-
body = body + " sp_gc_mark((void *)" + field + ");"7226+
stmt = gc_mark_stmt_for_expr(field, parts[fi])7227+
if stmt != ""7228+
body = body + " " + stmt72287229
end72297230
end72307231
fi = fi + 1@@ -8509,19 +8510,125 @@
85098510
end85108511
85118512
def ivar_needs_gc_mark(t)8512-
if ivar_is_gc_ptr(t) == 18513+
if gc_trace_kind(t) != "none"8514+
return 18515+
end8516+
08517+
end8518+
8519+
def value_type_scan_class_name(t)8520+
bt = base_type(t)8521+
if is_value_type_obj(bt) == 18522+
return bt[4, bt.length - 4]8523+
end8524+
""8525+
end8526+
8527+
def value_type_needs_gc_scan(t)8528+
cname = value_type_scan_class_name(t)8529+
if cname == ""8530+
return 08531+
end8532+
ci = find_class_idx(cname)8533+
if ci >= 0 && class_has_ptr_ivars(ci) == 185138534
return 185148535
end8515-
if is_value_type_obj(t) == 18516-
cname = t[4, t.length - 4]8517-
ci = find_class_idx(cname)8518-
if ci >= 0 && class_has_ptr_ivars(ci) == 18519-
return 18536+
08537+
end8538+
8539+
def gc_trace_kind(t)8540+
bt = base_type(t)8541+
if bt == "poly"8542+
return "poly"8543+
end8544+
if value_type_needs_gc_scan(bt) == 18545+
return "value_type"8546+
end8547+
if type_is_pointer(bt) == 18548+
return "ptr"8549+
end8550+
"none"8551+
end8552+
8553+
def emit_gc_root_line(stmt, raw)8554+
if raw == 18555+
emit_raw(" " + stmt)8556+
else8557+
emit(" " + stmt)8558+
end8559+
08560+
end8561+
8562+
def emit_value_type_field_roots(expr, t, raw, global)8563+
cname = value_type_scan_class_name(t)8564+
if cname == ""8565+
return8566+
end8567+
ci = find_class_idx(cname)8568+
if ci < 08569+
return8570+
end8571+
names = @cls_ivar_names[ci].split(";")8572+
types = @cls_ivar_types[ci].split(";")8573+
j = 08574+
while j < names.length8575+
if j < types.length8576+
emit_gc_root_for_expr_impl(expr + "." + sanitize_ivar(names[j]), types[j], raw, global)8577+
end8578+
j = j + 18579+
end8580+
08581+
end8582+
8583+
def emit_gc_root_for_expr_impl(expr, t, raw, global)8584+
kind = gc_trace_kind(t)8585+
if kind == "ptr"8586+
if global == 18587+
emit_gc_root_line("_sp_gc_root_push((void**)&" + expr + ");", raw)8588+
else8589+
emit_gc_root_line("SP_GC_ROOT(" + expr + ");", raw)85208590
end8591+
elsif kind == "value_type"8592+
emit_value_type_field_roots(expr, t, raw, global)85218593
end85228594
085238595
end85248596
8597+
def gc_mark_stmt_for_expr(expr, t)8598+
kind = gc_trace_kind(t)8599+
if kind == "poly"8600+
return "sp_mark_rbval(" + expr + ");"8601+
end8602+
if kind == "value_type"8603+
cname = value_type_scan_class_name(t)8604+
return "sp_" + cname + "_gc_scan(&" + expr + ");"8605+
end8606+
if kind == "ptr"8607+
return "if (" + expr + ") sp_gc_mark((void *)" + expr + ");"8608+
end8609+
""8610+
end8611+
8612+
def emit_gc_root_for_expr(expr, t)8613+
emit_gc_root_for_expr_impl(expr, t, 0, 0)8614+
end8615+
8616+
def emit_raw_gc_root_for_expr(expr, t)8617+
emit_gc_root_for_expr_impl(expr, t, 1, 0)8618+
end8619+
8620+
def emit_global_gc_roots_for_expr(expr, t)8621+
emit_gc_root_for_expr_impl(expr, t, 0, 1)8622+
end8623+
8624+
def type_needs_gc_root(t)8625+
kind = gc_trace_kind(t)8626+
if kind == "ptr" || kind == "value_type"8627+
return 18628+
end8629+
08630+
end8631+
85258632
def class_has_ptr_ivars(ci)85268633
names = @cls_ivar_names[ci].split(";")85278634
types = @cls_ivar_types[ci].split(";")@@ -8543,8 +8650,7 @@
85438650
085448651
end85458652
8546-
# Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each8547-
# GC-traced ivar on the class chain. Called right after8653+
# Clear each GC-traced field on the class chain. Called right after85488654
# SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot85498655
# stale-pointer hazard: if a sub-allocation in the initialize85508656
# body triggers GC before the body writes every pointer ivar,@@ -8566,13 +8672,14 @@
85668672
j = 085678673
while j < names.length85688674
if j < types.length8569-
if ivar_is_gc_ptr(types[j]) == 18570-
field = "self->" + sanitize_ivar(names[j])8571-
if types[j] == "poly"8572-
emit_raw(" " + field + " = sp_box_nil();")8573-
else8574-
emit_raw(" " + field + " = NULL;")8575-
end8675+
kind = gc_trace_kind(types[j])8676+
field = "self->" + sanitize_ivar(names[j])8677+
if kind == "poly"8678+
emit_raw(" " + field + " = sp_box_nil();")8679+
elsif kind == "ptr"8680+
emit_raw(" " + field + " = NULL;")8681+
elsif kind == "value_type"8682+
emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")85768683
end85778684
end85788685
j = j + 1@@ -8580,17 +8687,13 @@
85808687
end85818688
85828689
def emit_gc_mark_ivar(field, t)8583-
if t == "poly"8584-
emit_raw(" sp_mark_rbval(" + field + ");")8585-
elsif is_value_type_obj(t) == 18586-
cname = t[4, t.length - 4]8587-
emit_raw(" sp_" + cname + "_gc_scan(&" + field + ");")8588-
else8589-
emit_raw(" if (" + field + ") sp_gc_mark((void *)" + field + ");")8690+
stmt = gc_mark_stmt_for_expr(field, t)8691+
if stmt != ""8692+
emit_raw(" " + stmt)85908693
end85918694
end85928695
8593-
def emit_gc_scan_functions8696+
def emit_gc_scan_function_prototypes85948697
emitted_proto = 085958698
i = 085968699
while i < @cls_names.length@@ -8603,7 +8706,9 @@
86038706
if emitted_proto == 186048707
emit_raw("")86058708
end8709+
end86068710
8711+
def emit_gc_scan_functions86078712
i = 086088713
while i < @cls_names.length86098714
if cls_emit_skipped(i) == 1@@ -9591,6 +9696,29 @@
95919696
# to that DefNode's own class context, neither of which we want95929697
# to attribute to the outer cls method).95939698
9699+
def emit_constructor_param_roots(ci, init_idx)9700+
if init_idx < 09701+
return9702+
end9703+
all_params_str = @cls_meth_params[ci].split("|")9704+
all_ptypes_str = @cls_meth_ptypes[ci].split("|")9705+
if init_idx >= all_params_str.length9706+
return9707+
end9708+
cp_names = all_params_str[init_idx].split(",")9709+
cp_types = "".split(",")9710+
if init_idx < all_ptypes_str.length9711+
cp_types = all_ptypes_str[init_idx].split(",")9712+
end9713+
cpi = 09714+
while cpi < cp_names.length9715+
if cpi < cp_types.length9716+
emit_raw_gc_root_for_expr("lv_" + cp_names[cpi], cp_types[cpi])9717+
end9718+
cpi = cpi + 19719+
end9720+
end9721+
95949722
def emit_constructor(ci)95959723
saved_gc_scope = @in_gc_scope95969724
cname = @cls_names[ci]@@ -9599,6 +9727,7 @@
95999727
emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")96009728
emit_raw(" sp_" + cname + " self = {0};")96019729
@in_gc_scope = 09730+
emit_constructor_param_roots(ci, init_idx)96029731
else96039732
# Per-class free-list pool. Unmarked instances are pushed onto a96049733
# bounded pool by sp_gc_collect (via the recycle hook in sp_gc_hdr)@@ -9609,6 +9738,7 @@
96099738
emit_raw("static inline sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")96109739
emit_raw(" SP_GC_SAVE();")96119740
@in_gc_scope = 19741+
emit_constructor_param_roots(ci, init_idx)96129742
scan_fn = "NULL"96139743
if class_has_ptr_ivars(ci) == 196149744
scan_fn = "sp_" + cname + "_gc_scan"@@ -9634,28 +9764,6 @@
96349764
emit_clear_ptr_ivars_for_new(ci)96359765
end96369766
9637-
# Root pointer-type constructor parameters9638-
if init_idx >= 09639-
all_params_str = @cls_meth_params[ci].split("|")9640-
all_ptypes_str = @cls_meth_ptypes[ci].split("|")9641-
if init_idx < all_params_str.length9642-
cp_names = all_params_str[init_idx].split(",")9643-
cp_types = "".split(",")9644-
if init_idx < all_ptypes_str.length9645-
cp_types = all_ptypes_str[init_idx].split(",")9646-
end9647-
cpi = 09648-
while cpi < cp_names.length9649-
if cpi < cp_types.length9650-
if type_is_pointer(cp_types[cpi]) == 19651-
emit_raw(" SP_GC_ROOT(lv_" + cp_names[cpi] + ");")9652-
end9653-
end9654-
cpi = cpi + 19655-
end9656-
end9657-
end9658-
96599767
init_ci = find_init_class(ci)96609768
96619769
if init_idx >= 0@@ -10150,13 +10258,13 @@
1015010258
if @in_gc_scope == 11015110259
if @cls_is_value_type[ci] == 01015210260
emit(" SP_GC_ROOT(self);")10261+
else10262+
emit_gc_root_for_expr("self", "obj_" + cname)1015310263
end1015410264
j = 01015510265
while j < pnames.length1015610266
if j < ptypes.length10157-
if type_is_pointer(ptypes[j]) == 110158-
emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")10159-
end10267+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])1016010268
end1016110269
j = j + 11016210270
end@@ -10209,6 +10317,21 @@
1020910317
else1021010318
declare_method_locals(bid, pnames)1021110319
end10320+
if @in_gc_scope == 010321+
if @needs_gc == 110322+
emit(" SP_GC_SAVE();")10323+
@in_gc_scope = 110324+
end10325+
end10326+
if @in_gc_scope == 110327+
j = 010328+
while j < pnames.length10329+
if j < ptypes.length10330+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])10331+
end10332+
j = j + 110333+
end10334+
end1021210335
compile_body_return(bid, rt)1021310336
end1021410337
@@ -10241,7 +10364,7 @@
1024110364
has_gc_locals = 01024210365
j = 01024310366
while j < lnames.length10244-
if type_is_pointer(ltypes[j]) == 110367+
if type_needs_gc_root(ltypes[j]) == 11024510368
has_gc_locals = 11024610369
end1024710370
j = j + 1@@ -10458,9 +10581,7 @@
1045810581
j = 01045910582
while j < pnames.length1046010583
if j < ptypes.length10461-
if type_is_pointer(ptypes[j]) == 110462-
emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")10463-
end10584+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])1046410585
end1046510586
j = j + 11046610587
end@@ -10511,7 +10632,7 @@
1051110632
has_gc_locals = 01051210633
j = 01051310634
while j < lnames.length10514-
if type_is_pointer(ltypes[j]) == 110635+
if type_needs_gc_root(ltypes[j]) == 11051510636
has_gc_locals = 11051610637
end1051710638
j = j + 1@@ -10604,9 +10725,7 @@
1060410725
def emit_gc_roots(lnames, ltypes)1060510726
j = 01060610727
while j < lnames.length10607-
if type_is_pointer(ltypes[j]) == 110608-
emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")10609-
end10728+
emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])1061010729
j = j + 11061110730
end1061210731
end@@ -11158,9 +11277,9 @@
1115811277
# for the program's lifetime.1115911278
ti = 01116011279
while ti < @toplevel_ivar_names.length11161-
if type_is_pointer(@toplevel_ivar_types[ti]) == 111162-
emit(" _sp_gc_root_push((void**)&" + sanitize_ivar(@toplevel_ivar_names[ti]) + ");")11163-
end11280+
tivar = sanitize_ivar(@toplevel_ivar_names[ti])11281+
ttype = @toplevel_ivar_types[ti]11282+
emit_global_gc_roots_for_expr(tivar, ttype)1116411283
ti = ti + 11116511284
end1116611285
if @needs_gc == 1@@ -11196,9 +11315,9 @@
1119611315
# runtime helper signatures (sp_str_length, sp_IntArray_length,1119711316
# etc.) without affecting correctness. Mirrors the per-method1119811317
# logic in declare_method_locals (#548).11199-
if type_is_pointer(ltypes[j]) == 111200-
emit(" " + ctp + "lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")11201-
emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")11318+
if type_needs_gc_root(ltypes[j]) == 111319+
emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")11320+
emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])1120211321
else1120311322
vol = ""1120411323
if @needs_setjmp == 1@@ -11316,10 +11435,8 @@
1131611435
emit(" cst_" + @const_names[i] + " = " + val + ";")1131711436
if guarded_6461131811437
emit(" sp_init_in_progress_" + @const_names[i] + " = 0;")11319-
end11320-
if type_is_pointer(@const_types[i]) == 111321-
emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")1132211438
end11439+
emit_gc_root_for_expr("cst_" + @const_names[i], @const_types[i])1132311440
end1132411441
i = i + 11132511442
end@@ -11469,9 +11586,7 @@
1146911586
val_b = "sp_bigint_to_int((sp_Bigint *)" + val_b + ")"1147011587
end1147111588
emit(" cst_" + cwn_name + " = " + val_b + ";")11472-
if type_is_pointer(@const_types[cwn_idx]) == 111473-
emit(" SP_GC_ROOT(cst_" + cwn_name + ");")11474-
end11589+
emit_gc_root_for_expr("cst_" + cwn_name, @const_types[cwn_idx])1147511590
end1147611591
end1147711592
end@@ -12840,6 +12955,9 @@
1284012955
if is_nullable_type(t) == 11284112956
t = base_type(t)1284212957
end12958+
if value_type_needs_gc_scan(t) == 112959+
return 112960+
end1284312961
if t == "string" || t == "mutable_str"1284412962
return 11284512963
end@@ -12856,7 +12974,7 @@
1285612974
return 11285712975
end1285812976
if is_obj_type(t) == 112859-
return 112977+
return type_is_pointer(t)1286012978
end1286112979
if is_tuple_type(t) == 11286212980
return 1@@ -12887,7 +13005,7 @@
1288713005
@needs_gc = 11288813006
tmp = new_temp1288913007
emit(" " + c_type(t) + " " + tmp + " = " + val + ";")12890-
emit(" SP_GC_ROOT(" + tmp + ");")13008+
emit_gc_root_for_expr(tmp, t)1289113009
tmp1289213010
end1289313011
@@ -14367,7 +14485,7 @@
1436714485
end1436814486
end1436914487
# Root receiver if it may be collected during argument evaluation14370-
if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 114488+
if expr_may_gc(recv) == 1 && type_needs_gc_root(recv_type) == 11437114489
args_id = @nd_arguments[nid]1437214490
if args_id >= 01437314491
aargs = get_args(args_id)@@ -14382,7 +14500,7 @@
1438214500
if has_gc_arg == 11438314501
tmp = new_temp1438414502
emit(" " + c_type(recv_type) + " " + tmp + " = " + rc + ";")14385-
emit(" SP_GC_ROOT(" + tmp + ");")14503+
emit_gc_root_for_expr(tmp, recv_type)1438614504
rc = tmp1438714505
end1438814506
end@@ -24114,9 +24232,7 @@
2411424232
@needs_gc = 12411524233
src_tmp = new_temp2411624234
emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")24117-
if type_is_pointer(src_t) == 124118-
emit(" SP_GC_ROOT(" + src_tmp + ");")24119-
end24235+
emit_gc_root_for_expr(src_tmp, src_t)2412024236
src_v = src_tmp2412124237
end2412224238
src_len_expr = length_c_expr(src_t, src_v)@@ -24914,9 +25030,7 @@
2491425030
at = infer_type(arg_ids[k])2491525031
ct = c_type(at)2491625032
emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";")24917-
if type_is_pointer(at) == 124918-
emit(" SP_GC_ROOT(" + tmp + ");")24919-
end25033+
emit_gc_root_for_expr(tmp, at)2492025034
temps.push(tmp)2492125035
else2492225036
temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])))@@ -24937,13 +25051,7 @@
2493725051
2493825052
def constructor_arg_needs_root(arg_type)2493925053
bt = base_type(arg_type)24940-
if is_obj_type(bt) == 124941-
return type_is_pointer(bt)24942-
end24943-
if is_array_type(bt) == 1 || is_ptr_array_type(bt) == 1 || is_tuple_type(bt) == 124944-
return 124945-
end24946-
if bt == "fiber" || bt == "bigint" || bt == "lambda" || bt == "poly_array"25054+
if type_needs_gc_root(bt) == 12494725055
return 12494825056
end2494925057
0@@ -24963,7 +25071,7 @@
2496325071
@needs_gc = 12496425072
tmp = new_temp2496525073
emit(" " + c_type(arg_type) + " " + tmp + " = " + expr + ";")24966-
emit(" SP_GC_ROOT(" + tmp + ");")25074+
emit_gc_root_for_expr(tmp, arg_type)2496725075
tmp2496825076
end2496925077
test/value_type_constructor_arg_gc_root.rbmodifiedView file
Loading diff…
test/value_type_constructor_arg_gc_root.rb.expectedmodifiedView file
Loading diff…
test/value_type_ivar_gc_scan.rbmodifiedView file
Loading diff…
test/value_type_ivar_gc_scan.rb.expectedmodifiedView file
Loading diff…
test/value_type_recycled_field_clear.rbaddedView file
Loading diff…
test/value_type_recycled_field_clear.rb.expectedaddedView file
Loading diff…
test/value_type_tuple_gc_scan.rbaddedView file
Loading diff…
test/value_type_tuple_gc_scan.rb.expectedaddedView file
Loading diff…