Commit 4b438d05
4b438d05e7d2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 9 files · +8 added · ~1 modified
Description
Merge pull request #670 from fc-anjos/codex/fix-value-type-gc
Fix value-type GC tracing
Details
- Commit
- 4b438d05e7d2
- Parent
- 0fbdf33a3bd3
- Merge parent
- 2238b26d2f8f
spinel_codegen.rbmodifiedView file
@@ -3625,13 +3625,10 @@
36253625
# element types must be marked, otherwise the GC frees the inner object36263626
# while the tuple keeps a dangling pointer.36273627
def tuple_field_needs_mark(et)3628-
if et == "poly"3628+
if gc_trace_kind(et) != "none"36293629
return 136303630
end3631-
if et == "int" || et == "float" || et == "bool" || et == "symbol" || et == "void" || et == "nil"3632-
return 03633-
end3634-
type_is_pointer(et)3631+
036353632
end36363633
36373634
# Returns the scan function name for the tuple, or "NULL" if no field@@ -5026,8 +5023,12 @@
50265023
emit(" SP_GC_SAVE();")50275024
@in_gc_scope = 150285025
end5029-
if @needs_gc == 1 && @cls_is_value_type[ci] == 05030-
emit(" SP_GC_ROOT(self);")5026+
if @needs_gc == 15027+
if @cls_is_value_type[ci] == 05028+
emit(" SP_GC_ROOT(self);")5029+
else5030+
emit_gc_root_for_expr("self", "obj_" + cname)5031+
end50315032
end50325033
compile_body_return(bid, ret_type)50335034
end@@ -7295,6 +7296,7 @@
72957296
# forward declaration.72967297
emit_class_runtime72977298
emit_class_structs7299+
emit_gc_scan_function_prototypes72987300
emit_raw("/*TUPLE_INSERT_POINT*/")72997301
emit_gc_scan_functions73007302
emit_toplevel_ivar_decls@@ -7380,10 +7382,9 @@
73807382
while fi < parts.length73817383
if tuple_field_needs_mark(parts[fi]) == 173827384
field = "_t->_" + fi.to_s7383-
if parts[fi] == "poly"7384-
body = body + " sp_mark_rbval(" + field + ");"7385-
else7386-
body = body + " sp_gc_mark((void *)" + field + ");"7385+
stmt = gc_mark_stmt_for_expr(field, parts[fi])7386+
if stmt != ""7387+
body = body + " " + stmt73877388
end73887389
end73897390
fi = fi + 1@@ -8671,13 +8672,133 @@
86718672
086728673
end86738674
8675+
def ivar_needs_gc_mark(t)8676+
if gc_trace_kind(t) != "none"8677+
return 18678+
end8679+
08680+
end8681+
8682+
def value_type_scan_class_name(t)8683+
bt = base_type(t)8684+
if is_value_type_obj(bt) == 18685+
return bt[4, bt.length - 4]8686+
end8687+
""8688+
end8689+
8690+
def value_type_needs_gc_scan(t)8691+
cname = value_type_scan_class_name(t)8692+
if cname == ""8693+
return 08694+
end8695+
ci = find_class_idx(cname)8696+
if ci >= 0 && class_has_ptr_ivars(ci) == 18697+
return 18698+
end8699+
08700+
end8701+
8702+
def gc_trace_kind(t)8703+
bt = base_type(t)8704+
if bt == "poly"8705+
return "poly"8706+
end8707+
if value_type_needs_gc_scan(bt) == 18708+
return "value_type"8709+
end8710+
if type_is_pointer(bt) == 18711+
return "ptr"8712+
end8713+
"none"8714+
end8715+
8716+
def emit_gc_root_line(stmt, raw)8717+
if raw == 18718+
emit_raw(" " + stmt)8719+
else8720+
emit(" " + stmt)8721+
end8722+
08723+
end8724+
8725+
def emit_value_type_field_roots(expr, t, raw, global)8726+
cname = value_type_scan_class_name(t)8727+
if cname == ""8728+
return8729+
end8730+
ci = find_class_idx(cname)8731+
if ci < 08732+
return8733+
end8734+
names = @cls_ivar_names[ci].split(";")8735+
types = @cls_ivar_types[ci].split(";")8736+
j = 08737+
while j < names.length8738+
if j < types.length8739+
emit_gc_root_for_expr_impl(expr + "." + sanitize_ivar(names[j]), types[j], raw, global)8740+
end8741+
j = j + 18742+
end8743+
08744+
end8745+
8746+
def emit_gc_root_for_expr_impl(expr, t, raw, global)8747+
kind = gc_trace_kind(t)8748+
if kind == "ptr"8749+
if global == 18750+
emit_gc_root_line("_sp_gc_root_push((void**)&" + expr + ");", raw)8751+
else8752+
emit_gc_root_line("SP_GC_ROOT(" + expr + ");", raw)8753+
end8754+
elsif kind == "value_type"8755+
emit_value_type_field_roots(expr, t, raw, global)8756+
end8757+
08758+
end8759+
8760+
def gc_mark_stmt_for_expr(expr, t)8761+
kind = gc_trace_kind(t)8762+
if kind == "poly"8763+
return "sp_mark_rbval(" + expr + ");"8764+
end8765+
if kind == "value_type"8766+
cname = value_type_scan_class_name(t)8767+
return "sp_" + cname + "_gc_scan(&" + expr + ");"8768+
end8769+
if kind == "ptr"8770+
return "if (" + expr + ") sp_gc_mark((void *)" + expr + ");"8771+
end8772+
""8773+
end8774+
8775+
def emit_gc_root_for_expr(expr, t)8776+
emit_gc_root_for_expr_impl(expr, t, 0, 0)8777+
end8778+
8779+
def emit_raw_gc_root_for_expr(expr, t)8780+
emit_gc_root_for_expr_impl(expr, t, 1, 0)8781+
end8782+
8783+
def emit_global_gc_roots_for_expr(expr, t)8784+
emit_gc_root_for_expr_impl(expr, t, 0, 1)8785+
end8786+
8787+
def type_needs_gc_root(t)8788+
kind = gc_trace_kind(t)8789+
if kind == "ptr" || kind == "value_type"8790+
return 18791+
end8792+
08793+
end8794+
86748795
def class_has_ptr_ivars(ci)86758796
names = @cls_ivar_names[ci].split(";")86768797
types = @cls_ivar_types[ci].split(";")86778798
j = 086788799
while j < names.length86798800
if j < types.length8680-
if ivar_is_gc_ptr(types[j]) == 18801+
if ivar_needs_gc_mark(types[j]) == 186818802
return 186828803
end86838804
end@@ -8692,8 +8813,7 @@
86928813
086938814
end86948815
8695-
# Emit `self->iv_<name> = NULL;` / `sp_box_nil();` for each8696-
# GC-traced ivar on the class chain. Called right after8816+
# Clear each GC-traced field on the class chain. Called right after86978817
# SP_POOL_NEW + SP_GC_ROOT(self) to defuse the recycled-slot86988818
# stale-pointer hazard: if a sub-allocation in the initialize86998819
# body triggers GC before the body writes every pointer ivar,@@ -8715,13 +8835,14 @@
87158835
j = 087168836
while j < names.length87178837
if j < types.length8718-
if ivar_is_gc_ptr(types[j]) == 18719-
field = "self->" + sanitize_ivar(names[j])8720-
if types[j] == "poly"8721-
emit_raw(" " + field + " = sp_box_nil();")8722-
else8723-
emit_raw(" " + field + " = NULL;")8724-
end8838+
kind = gc_trace_kind(types[j])8839+
field = "self->" + sanitize_ivar(names[j])8840+
if kind == "poly"8841+
emit_raw(" " + field + " = sp_box_nil();")8842+
elsif kind == "ptr"8843+
emit_raw(" " + field + " = NULL;")8844+
elsif kind == "value_type"8845+
emit_raw(" " + field + " = (" + c_type(types[j]) + "){0};")87258846
end87268847
end87278848
j = j + 1@@ -8729,10 +8850,24 @@
87298850
end87308851
87318852
def emit_gc_mark_ivar(field, t)8732-
if t == "poly"8733-
emit_raw(" sp_mark_rbval(" + field + ");")8734-
else8735-
emit_raw(" if (" + field + ") sp_gc_mark((void *)" + field + ");")8853+
stmt = gc_mark_stmt_for_expr(field, t)8854+
if stmt != ""8855+
emit_raw(" " + stmt)8856+
end8857+
end8858+
8859+
def emit_gc_scan_function_prototypes8860+
emitted_proto = 08861+
i = 08862+
while i < @cls_names.length8863+
if cls_emit_skipped(i) == 0 && class_has_ptr_ivars(i) == 18864+
emit_raw("static void sp_" + @cls_names[i] + "_gc_scan(void *p);")8865+
emitted_proto = 18866+
end8867+
i = i + 18868+
end8869+
if emitted_proto == 18870+
emit_raw("")87368871
end87378872
end87388873
@@ -8768,7 +8903,7 @@
87688903
end87698904
end87708905
end8771-
if skip_inherited == 0 && ivar_is_gc_ptr(types[j]) == 18906+
if skip_inherited == 0 && ivar_needs_gc_mark(types[j]) == 187728907
emit_gc_mark_ivar("self->" + sanitize_ivar(names[j]), types[j])87738908
end87748909
end@@ -8783,7 +8918,7 @@
87838918
pj = 087848919
while pj < pnames.length87858920
if pj < ptypes.length8786-
if ivar_is_gc_ptr(ptypes[pj]) == 18921+
if ivar_needs_gc_mark(ptypes[pj]) == 187878922
emit_gc_mark_ivar("self->" + sanitize_ivar(pnames[pj]), ptypes[pj])87888923
end87898924
end@@ -9724,6 +9859,29 @@
97249859
# to that DefNode's own class context, neither of which we want97259860
# to attribute to the outer cls method).97269861
9862+
def emit_constructor_param_roots(ci, init_idx)9863+
if init_idx < 09864+
return9865+
end9866+
all_params_str = @cls_meth_params[ci].split("|")9867+
all_ptypes_str = @cls_meth_ptypes[ci].split("|")9868+
if init_idx >= all_params_str.length9869+
return9870+
end9871+
cp_names = all_params_str[init_idx].split(",")9872+
cp_types = "".split(",")9873+
if init_idx < all_ptypes_str.length9874+
cp_types = all_ptypes_str[init_idx].split(",")9875+
end9876+
cpi = 09877+
while cpi < cp_names.length9878+
if cpi < cp_types.length9879+
emit_raw_gc_root_for_expr("lv_" + cp_names[cpi], cp_types[cpi])9880+
end9881+
cpi = cpi + 19882+
end9883+
end9884+
97279885
def emit_constructor(ci)97289886
saved_gc_scope = @in_gc_scope97299887
cname = @cls_names[ci]@@ -9732,6 +9890,7 @@
97329890
emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")97339891
emit_raw(" sp_" + cname + " self = {0};")97349892
@in_gc_scope = 09893+
emit_constructor_param_roots(ci, init_idx)97359894
else97369895
# Per-class free-list pool. Unmarked instances are pushed onto a97379896
# bounded pool by sp_gc_collect (via the recycle hook in sp_gc_hdr)@@ -9742,6 +9901,7 @@
97429901
emit_raw("static inline sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")97439902
emit_raw(" SP_GC_SAVE();")97449903
@in_gc_scope = 19904+
emit_constructor_param_roots(ci, init_idx)97459905
scan_fn = "NULL"97469906
if class_has_ptr_ivars(ci) == 197479907
scan_fn = "sp_" + cname + "_gc_scan"@@ -9765,28 +9925,6 @@
97659925
# a stale .v.p, so reset it to a tagged nil that sp_mark_rbval97669926
# treats as inert.97679927
emit_clear_ptr_ivars_for_new(ci)9768-
end9769-
9770-
# Root pointer-type constructor parameters9771-
if init_idx >= 09772-
all_params_str = @cls_meth_params[ci].split("|")9773-
all_ptypes_str = @cls_meth_ptypes[ci].split("|")9774-
if init_idx < all_params_str.length9775-
cp_names = all_params_str[init_idx].split(",")9776-
cp_types = "".split(",")9777-
if init_idx < all_ptypes_str.length9778-
cp_types = all_ptypes_str[init_idx].split(",")9779-
end9780-
cpi = 09781-
while cpi < cp_names.length9782-
if cpi < cp_types.length9783-
if type_is_pointer(cp_types[cpi]) == 19784-
emit_raw(" SP_GC_ROOT(lv_" + cp_names[cpi] + ");")9785-
end9786-
end9787-
cpi = cpi + 19788-
end9789-
end97909928
end97919929
97929930
init_ci = find_init_class(ci)@@ -10296,13 +10434,13 @@
1029610434
if @in_gc_scope == 11029710435
if @cls_is_value_type[ci] == 01029810436
emit(" SP_GC_ROOT(self);")10437+
else10438+
emit_gc_root_for_expr("self", "obj_" + cname)1029910439
end1030010440
j = 01030110441
while j < pnames.length1030210442
if j < ptypes.length10303-
if type_is_pointer(ptypes[j]) == 110304-
emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")10305-
end10443+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])1030610444
end1030710445
j = j + 11030810446
end@@ -10355,6 +10493,21 @@
1035510493
else1035610494
declare_method_locals(bid, pnames)1035710495
end10496+
if @in_gc_scope == 010497+
if @needs_gc == 110498+
emit(" SP_GC_SAVE();")10499+
@in_gc_scope = 110500+
end10501+
end10502+
if @in_gc_scope == 110503+
j = 010504+
while j < pnames.length10505+
if j < ptypes.length10506+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])10507+
end10508+
j = j + 110509+
end10510+
end1035810511
compile_body_return(bid, rt)1035910512
end1036010513
@@ -10387,7 +10540,7 @@
1038710540
has_gc_locals = 01038810541
j = 01038910542
while j < lnames.length10390-
if type_is_pointer(ltypes[j]) == 110543+
if type_needs_gc_root(ltypes[j]) == 11039110544
has_gc_locals = 11039210545
end1039310546
j = j + 1@@ -10604,9 +10757,7 @@
1060410757
j = 01060510758
while j < pnames.length1060610759
if j < ptypes.length10607-
if type_is_pointer(ptypes[j]) == 110608-
emit(" SP_GC_ROOT(lv_" + pnames[j] + ");")10609-
end10760+
emit_gc_root_for_expr("lv_" + pnames[j], ptypes[j])1061010761
end1061110762
j = j + 11061210763
end@@ -10657,7 +10808,7 @@
1065710808
has_gc_locals = 01065810809
j = 01065910810
while j < lnames.length10660-
if type_is_pointer(ltypes[j]) == 110811+
if type_needs_gc_root(ltypes[j]) == 11066110812
has_gc_locals = 11066210813
end1066310814
j = j + 1@@ -10750,9 +10901,7 @@
1075010901
def emit_gc_roots(lnames, ltypes)1075110902
j = 01075210903
while j < lnames.length10753-
if type_is_pointer(ltypes[j]) == 110754-
emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")10755-
end10904+
emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])1075610905
j = j + 11075710906
end1075810907
end@@ -11304,9 +11453,9 @@
1130411453
# for the program's lifetime.1130511454
ti = 01130611455
while ti < @toplevel_ivar_names.length11307-
if type_is_pointer(@toplevel_ivar_types[ti]) == 111308-
emit(" _sp_gc_root_push((void**)&" + sanitize_ivar(@toplevel_ivar_names[ti]) + ");")11309-
end11456+
tivar = sanitize_ivar(@toplevel_ivar_names[ti])11457+
ttype = @toplevel_ivar_types[ti]11458+
emit_global_gc_roots_for_expr(tivar, ttype)1131011459
ti = ti + 11131111460
end1131211461
if @needs_gc == 1@@ -11342,9 +11491,9 @@
1134211491
# runtime helper signatures (sp_str_length, sp_IntArray_length,1134311492
# etc.) without affecting correctness. Mirrors the per-method1134411493
# logic in declare_method_locals (#548).11345-
if type_is_pointer(ltypes[j]) == 111346-
emit(" " + ctp + "lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")11347-
emit(" SP_GC_ROOT(lv_" + lnames[j] + ");")11494+
if type_needs_gc_root(ltypes[j]) == 111495+
emit(" " + ctp + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")11496+
emit_gc_root_for_expr("lv_" + lnames[j], ltypes[j])1134811497
else1134911498
vol = ""1135011499
if @needs_setjmp == 1@@ -11462,10 +11611,8 @@
1146211611
emit(" cst_" + @const_names[i] + " = " + val + ";")1146311612
if guarded_6461146411613
emit(" sp_init_in_progress_" + @const_names[i] + " = 0;")11465-
end11466-
if type_is_pointer(@const_types[i]) == 111467-
emit(" SP_GC_ROOT(cst_" + @const_names[i] + ");")1146811614
end11615+
emit_gc_root_for_expr("cst_" + @const_names[i], @const_types[i])1146911616
end1147011617
i = i + 11147111618
end@@ -11615,9 +11762,7 @@
1161511762
val_b = "sp_bigint_to_int((sp_Bigint *)" + val_b + ")"1161611763
end1161711764
emit(" cst_" + cwn_name + " = " + val_b + ";")11618-
if type_is_pointer(@const_types[cwn_idx]) == 111619-
emit(" SP_GC_ROOT(cst_" + cwn_name + ");")11620-
end11765+
emit_gc_root_for_expr("cst_" + cwn_name, @const_types[cwn_idx])1162111766
end1162211767
end1162311768
end@@ -13235,6 +13380,9 @@
1323513380
if is_nullable_type(t) == 11323613381
t = base_type(t)1323713382
end13383+
if value_type_needs_gc_scan(t) == 113384+
return 113385+
end1323813386
if t == "string" || t == "mutable_str"1323913387
return 11324013388
end@@ -13251,7 +13399,7 @@
1325113399
return 11325213400
end1325313401
if is_obj_type(t) == 113254-
return 113402+
return type_is_pointer(t)1325513403
end1325613404
if is_tuple_type(t) == 11325713405
return 1@@ -13282,7 +13430,7 @@
1328213430
@needs_gc = 11328313431
tmp = new_temp1328413432
emit(" " + c_type(t) + " " + tmp + " = " + val + ";")13285-
emit(" SP_GC_ROOT(" + tmp + ");")13433+
emit_gc_root_for_expr(tmp, t)1328613434
tmp1328713435
end1328813436
@@ -14774,7 +14922,7 @@
1477414922
end1477514923
end1477614924
# Root receiver if it may be collected during argument evaluation14777-
if expr_may_gc(recv) == 1 && type_is_pointer(recv_type) == 114925+
if expr_may_gc(recv) == 1 && type_needs_gc_root(recv_type) == 11477814926
args_id = @nd_arguments[nid]1477914927
if args_id >= 01478014928
aargs = get_args(args_id)@@ -14789,7 +14937,7 @@
1478914937
if has_gc_arg == 11479014938
tmp = new_temp1479114939
emit(" " + c_type(recv_type) + " " + tmp + " = " + rc + ";")14792-
emit(" SP_GC_ROOT(" + tmp + ");")14940+
emit_gc_root_for_expr(tmp, recv_type)1479314941
rc = tmp1479414942
end1479514943
end@@ -24824,9 +24972,7 @@
2482424972
@needs_gc = 12482524973
src_tmp = new_temp2482624974
emit(" " + c_type(src_t) + " " + src_tmp + " = " + compile_expr(splat_src_id) + ";")24827-
if type_is_pointer(src_t) == 124828-
emit(" SP_GC_ROOT(" + src_tmp + ");")24829-
end24975+
emit_gc_root_for_expr(src_tmp, src_t)2483024976
src_v = src_tmp2483124977
end2483224978
src_len_expr = length_c_expr(src_t, src_v)@@ -25676,9 +25822,7 @@
2567625822
at = infer_type(arg_ids[k])2567725823
ct = c_type(at)2567825824
emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";")25679-
if type_is_pointer(at) == 125680-
emit(" SP_GC_ROOT(" + tmp + ");")25681-
end25825+
emit_gc_root_for_expr(tmp, at)2568225826
temps.push(tmp)2568325827
else2568425828
temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])))@@ -25699,13 +25843,7 @@
2569925843
2570025844
def constructor_arg_needs_root(arg_type)2570125845
bt = base_type(arg_type)25702-
if is_obj_type(bt) == 125703-
return 125704-
end25705-
if is_array_type(bt) == 1 || is_ptr_array_type(bt) == 1 || is_tuple_type(bt) == 125706-
return 125707-
end25708-
if bt == "fiber" || bt == "bigint" || bt == "lambda" || bt == "poly_array"25846+
if type_needs_gc_root(bt) == 12570925847
return 12571025848
end2571125849
0@@ -25734,7 +25872,7 @@
2573425872
@needs_gc = 12573525873
tmp = new_temp2573625874
emit(" " + c_type(arg_type) + " " + tmp + " = " + expr + ";")25737-
emit(" SP_GC_ROOT(" + tmp + ");")25875+
emit_gc_root_for_expr(tmp, arg_type)2573825876
tmp2573925877
end2574025878
test/value_type_constructor_arg_gc_root.rbaddedView file
Loading diff…
test/value_type_constructor_arg_gc_root.rb.expectedaddedView file
Loading diff…
test/value_type_ivar_gc_scan.rbaddedView file
Loading diff…
test/value_type_ivar_gc_scan.rb.expectedaddedView 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…