Commit bd4f1a5f
bd4f1a5f830aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
prepare: detect SRA-eligible classes
Add @cls_is_sra[] flag and detection pass. A class is SRA-eligible (scalar-replacement candidate) when:
- only scalar ivars (int/float/bool), up to 8
- no inheritance
- only initialize + attr_accessor-derived methods (no custom code)
Distinct from @cls_is_value_type: SRA allows attr_writer since the per-field mutation is rewritten at use sites (to be implemented in follow-up Phase 2b-2c).
Detection-only commit; no codegen change yet. 75/75 tests pass, bootstrap stable.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- bd4f1a5f830a
- Parent
- d21a55f09b37
spinel_codegen.rbmodifiedView file
@@ -119,6 +119,11 @@
119119
@cls_cmeth_returns = "".split(",")120120
@cls_cmeth_bodies = "".split(",")121121
@cls_is_value_type = []122+
# SRA (scalar replacement of aggregates) eligibility flag per class.123+
# Classes marked here can have their non-escaping instances replaced124+
# with individual scalar locals. Distinct from value_type: SRA allows125+
# attr_writer (mutation is rewritten to per-field assignment).126+
@cls_is_sra = []122127
123128
# ---- Constants (parallel arrays) ----124129
@const_names = "".split(",")@@ -3290,6 +3295,7 @@
32903295
ci = @cls_names.length32913296
@cls_names.push(cname)32923297
@cls_is_value_type.push(0)3298+
@cls_is_sra.push(0)32933299
@cls_parents.push(parent)32943300
# Initialize struct fields as ivars32953301
ivar_names = ""@@ -4185,6 +4191,7 @@
41854191
ci = @cls_names.length41864192
@cls_names.push(cname)41874193
@cls_is_value_type.push(0)4194+
@cls_is_sra.push(0)41884195
@cls_parents.push("")41894196
@cls_ivar_names.push("")41904197
@cls_ivar_types.push("")@@ -8693,6 +8700,62 @@
86938700
i = i + 186948701
end86958702
end8703+
# SRA eligibility (Phase 2a): like value-type but allows attr_writer.8704+
# The per-instance escape check happens separately at use sites.8705+
i = 08706+
while i < @cls_names.length8707+
if @cls_is_value_type[i] == 18708+
# Already handled as value-type; SRA redundant for these.8709+
i = i + 18710+
next8711+
end8712+
names = @cls_ivar_names[i].split(";")8713+
types = @cls_ivar_types[i].split(";")8714+
eligible = 18715+
if names.length == 0 || names.length > 88716+
eligible = 08717+
end8718+
j = 08719+
while eligible == 1 && j < types.length8720+
t = types[j]8721+
if t != "int" && t != "float" && t != "bool"8722+
eligible = 08723+
end8724+
j = j + 18725+
end8726+
# No inheritance8727+
if eligible == 1 && @cls_parents[i] != ""8728+
eligible = 08729+
end8730+
if eligible == 18731+
si = 08732+
while si < @cls_names.length8733+
if @cls_parents[si] == @cls_names[i]8734+
eligible = 08735+
end8736+
si = si + 18737+
end8738+
end8739+
# Only initialize + attr_* methods (no custom methods).8740+
if eligible == 18741+
mnames = @cls_meth_names[i].split(";")8742+
readers = @cls_attr_readers[i].split(";")8743+
writers = @cls_attr_writers[i].split(";")8744+
mk = 08745+
while mk < mnames.length8746+
mn = mnames[mk]8747+
# allowed: initialize, any attr_reader/writer name8748+
if mn != "initialize" && not_in(mn, readers) == 1 && not_in(mn, writers) == 18749+
eligible = 08750+
end8751+
mk = mk + 18752+
end8753+
end8754+
if eligible == 18755+
@cls_is_sra[i] = 18756+
end8757+
i = i + 18758+
end86968759
end86978760
86988761
def emit_tuple_structs