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 replaced
124+ # with individual scalar locals. Distinct from value_type: SRA allows
125+ # 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.length
32913296 @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 ivars
32953301 ivar_names = ""
@@ -4185,6 +4191,7 @@
41854191 ci = @cls_names.length
41864192 @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 + 1
86948701 end
86958702 end
8703+ # SRA eligibility (Phase 2a): like value-type but allows attr_writer.
8704+ # The per-instance escape check happens separately at use sites.
8705+ i = 0
8706+ while i < @cls_names.length
8707+ if @cls_is_value_type[i] == 1
8708+ # Already handled as value-type; SRA redundant for these.
8709+ i = i + 1
8710+ next
8711+ end
8712+ names = @cls_ivar_names[i].split(";")
8713+ types = @cls_ivar_types[i].split(";")
8714+ eligible = 1
8715+ if names.length == 0 || names.length > 8
8716+ eligible = 0
8717+ end
8718+ j = 0
8719+ while eligible == 1 && j < types.length
8720+ t = types[j]
8721+ if t != "int" && t != "float" && t != "bool"
8722+ eligible = 0
8723+ end
8724+ j = j + 1
8725+ end
8726+ # No inheritance
8727+ if eligible == 1 && @cls_parents[i] != ""
8728+ eligible = 0
8729+ end
8730+ if eligible == 1
8731+ si = 0
8732+ while si < @cls_names.length
8733+ if @cls_parents[si] == @cls_names[i]
8734+ eligible = 0
8735+ end
8736+ si = si + 1
8737+ end
8738+ end
8739+ # Only initialize + attr_* methods (no custom methods).
8740+ if eligible == 1
8741+ mnames = @cls_meth_names[i].split(";")
8742+ readers = @cls_attr_readers[i].split(";")
8743+ writers = @cls_attr_writers[i].split(";")
8744+ mk = 0
8745+ while mk < mnames.length
8746+ mn = mnames[mk]
8747+ # allowed: initialize, any attr_reader/writer name
8748+ if mn != "initialize" && not_in(mn, readers) == 1 && not_in(mn, writers) == 1
8749+ eligible = 0
8750+ end
8751+ mk = mk + 1
8752+ end
8753+ end
8754+ if eligible == 1
8755+ @cls_is_sra[i] = 1
8756+ end
8757+ i = i + 1
8758+ end
86968759 end
86978760
86988761 def emit_tuple_structs