authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-03 15:35:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-04 15:57:40-07:00
log6c0a1417c6edb40cfc86e546c5c853a2d19c22f0
treef3137ac1aa8b6b9494b35350faa926ba04f61e38
parentda73410e7ff41fe1f196236ca405f225650a0726

CBE: fix static allocs being double allocated


1 files changed, 9 insertions(+), 4 deletions(-)

src/codegen/c.zig+9-4
...@@ -271,7 +271,8 @@ pub const Function = struct {...@@ -271,7 +271,8 @@ pub const Function = struct {
271 /// the locals within so that it can be used to render the block of271 /// the locals within so that it can be used to render the block of
272 /// variable declarations at the top of a function, sorted descending by272 /// variable declarations at the top of a function, sorted descending by
273 /// type alignment.273 /// type alignment.
274 allocs: std.AutoArrayHashMapUnmanaged(LocalIndex, void) = .{},274 /// The value is whether the alloc is static or not.
275 allocs: std.AutoArrayHashMapUnmanaged(LocalIndex, bool) = .{},
275 /// Needed for memory used by Type objects used as keys in free_locals.276 /// Needed for memory used by Type objects used as keys in free_locals.
276 arena: std.heap.ArenaAllocator,277 arena: std.heap.ArenaAllocator,
277278
...@@ -290,6 +291,8 @@ pub const Function = struct {...@@ -290,6 +291,8 @@ pub const Function = struct {
290 const writer = f.object.code_header.writer();291 const writer = f.object.code_header.writer();
291 const alignment = 0;292 const alignment = 0;
292 const decl_c_value = try f.allocLocalValue(ty, alignment);293 const decl_c_value = try f.allocLocalValue(ty, alignment);
294 const gpa = f.object.dg.gpa;
295 try f.allocs.put(gpa, decl_c_value.local, true);
293 try writer.writeAll("static ");296 try writer.writeAll("static ");
294 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, .Const, alignment, .Complete);297 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, .Const, alignment, .Complete);
295 try writer.writeAll(" = ");298 try writer.writeAll(" = ");
...@@ -2487,7 +2490,9 @@ pub fn genFunc(f: *Function) !void {...@@ -2487,7 +2490,9 @@ pub fn genFunc(f: *Function) !void {
2487 // Liveness analysis, however, locals from alloc instructions will be2490 // Liveness analysis, however, locals from alloc instructions will be
2488 // missing. These are added now to complete the map. Then we can sort by2491 // missing. These are added now to complete the map. Then we can sort by
2489 // alignment, descending.2492 // alignment, descending.
2490 for (f.allocs.keys()) |local_index| {2493 const values = f.allocs.values();
2494 for (f.allocs.keys()) |local_index, i| {
2495 if (values[i]) continue; // static
2491 const local = f.locals.items[local_index];2496 const local = f.locals.items[local_index];
2492 log.debug("inserting local {d} into free_locals", .{local_index});2497 log.debug("inserting local {d} into free_locals", .{local_index});
2493 const gop = try f.free_locals.getOrPutContext(gpa, local.ty, f.tyHashCtx());2498 const gop = try f.free_locals.getOrPutContext(gpa, local.ty, f.tyHashCtx());
...@@ -3110,7 +3115,7 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3110,7 +3115,7 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
3110 const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target));3115 const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target));
3111 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });3116 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });
3112 const gpa = f.object.dg.module.gpa;3117 const gpa = f.object.dg.module.gpa;
3113 try f.allocs.put(gpa, local.local, {});3118 try f.allocs.put(gpa, local.local, false);
3114 return CValue{ .local_ref = local.local };3119 return CValue{ .local_ref = local.local };
3115}3120}
31163121
...@@ -3127,7 +3132,7 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3127,7 +3132,7 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue {
3127 const local = try f.allocAlignedLocal(elem_ty, mutability, inst_ty.ptrAlignment(target));3132 const local = try f.allocAlignedLocal(elem_ty, mutability, inst_ty.ptrAlignment(target));
3128 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });3133 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });
3129 const gpa = f.object.dg.module.gpa;3134 const gpa = f.object.dg.module.gpa;
3130 try f.allocs.put(gpa, local.local, {});3135 try f.allocs.put(gpa, local.local, false);
3131 return CValue{ .local_ref = local.local };3136 return CValue{ .local_ref = local.local };
3132}3137}
31333138