authorgravatar for seda18@rolmail.netDavid Senoner <seda18@rolmail.net> 2026-09-03 00:45:19+02:00
committergravatar for justus@klausecker.deJustus Klausecker <justus@klausecker.de> 2026-09-13 13:02:47+02:00
log23e69f7c5766c46c2b699762642f7134d916e515
treea6d19c6ea2f93c7eedf8228bbc0d3ebd63d4ad83
parentd08989840e245c76261cb00930db2369f468392e

compiler: add `expand_ptr_elem_val` legalization

creates a block in place of the old ptr_elem_val air instruction and in the block adds ptr_elem_ptr and load air instructions

1 files changed, 45 insertions(+), 1 deletions(-)

src/Air/Legalize.zig+45-1
......@@ -204,6 +204,8 @@ pub const Feature = enum {
204204 expand_array_splat,
205205 /// Replace `array_to_vector` with an `array_elem_val` per element followed by an `aggregate_init`.
206206 expand_array_to_vector,
207 /// Replace `ptr_elem_val` with an `ptr_elem_ptr` followed by a `load`.
208 expand_ptr_elem_val,
207209
208210 /// Replace all arithmetic operations on 16-bit floating-point types with calls to soft-float
209211 /// routines in compiler_rt, including `fptrunc`/`fpext`/`float_from_int`/`int_from_float`
......@@ -863,10 +865,12 @@ fn legalizeBody(l: *Legalize, body_start: usize, body_len: usize) Error!void {
863865 .array_elem_val,
864866 .slice_elem_val,
865867 .slice_elem_ptr,
866 .ptr_elem_val,
867868 .ptr_elem_ptr,
868869 .array_to_slice,
869870 => {},
871 .ptr_elem_val => if (l.features.has(.expand_ptr_elem_val)) {
872 continue :inst l.replaceInst(inst, .block, try l.ptrElemValBlockPayload(inst));
873 },
870874 .array_to_vector => if (l.features.has(.expand_array_to_vector)) {
871875 continue :inst l.replaceInst(inst, .block, try l.arrayToVectorBlockPayload(inst));
872876 },
......@@ -2973,6 +2977,46 @@ fn packedAggregateInitBlockPayload(l: *Legalize, orig_inst: Air.Inst.Index) Erro
29732977 } };
29742978}
29752979
2980fn ptrElemValBlockPayload(l: *Legalize, orig_inst: Air.Inst.Index) Error!Air.Inst.Data {
2981 const pt = l.pt;
2982 const zcu = pt.zcu;
2983 const gpa = zcu.gpa;
2984
2985 const orig_bin_op = l.air_instructions.items(.data)[@backingInt(orig_inst)].bin_op;
2986
2987 const ptr_val = orig_bin_op.lhs;
2988 const ptr_ty = try l.typeOf(ptr_val).elemPtrType(null, pt);
2989 const val_ty = ptr_ty.childType(zcu);
2990
2991 var inst_buf: [3]Air.Inst.Index = undefined;
2992 var main_block: Block = .init(&inst_buf);
2993 try l.air_instructions.ensureUnusedCapacity(gpa, inst_buf.len);
2994
2995 const ptr = main_block.add(l, .{
2996 .tag = .ptr_elem_ptr,
2997 .data = .{
2998 .ty_pl = .{
2999 .ty = ptr_ty,
3000 .payload = try l.addExtra(Air.Bin, .{
3001 .lhs = ptr_val,
3002 .rhs = orig_bin_op.rhs,
3003 }),
3004 },
3005 },
3006 }).toRef();
3007
3008 const val = main_block.addTyOp(l, .load, val_ty, ptr).toRef();
3009
3010 main_block.addBr(l, orig_inst, val);
3011
3012 return .{
3013 .ty_pl = .{
3014 .ty = val_ty,
3015 .payload = try l.addBlockBody(main_block.body()),
3016 },
3017 };
3018}
3019
29763020fn arrayToVectorBlockPayload(l: *Legalize, orig_inst: Air.Inst.Index) Error!Air.Inst.Data {
29773021 const pt = l.pt;
29783022 const zcu = pt.zcu;