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 {
271271 /// the locals within so that it can be used to render the block of
272272 /// variable declarations at the top of a function, sorted descending by
273273 /// 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) = .{},
275276 /// Needed for memory used by Type objects used as keys in free_locals.
276277 arena: std.heap.ArenaAllocator,
277278
......@@ -290,6 +291,8 @@ pub const Function = struct {
290291 const writer = f.object.code_header.writer();
291292 const alignment = 0;
292293 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);
293296 try writer.writeAll("static ");
294297 try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, .Const, alignment, .Complete);
295298 try writer.writeAll(" = ");
......@@ -2487,7 +2490,9 @@ pub fn genFunc(f: *Function) !void {
24872490 // Liveness analysis, however, locals from alloc instructions will be
24882491 // missing. These are added now to complete the map. Then we can sort by
24892492 // 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
24912496 const local = f.locals.items[local_index];
24922497 log.debug("inserting local {d} into free_locals", .{local_index});
24932498 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 {
31103115 const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target));
31113116 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });
31123117 const gpa = f.object.dg.module.gpa;
3113 try f.allocs.put(gpa, local.local, {});
3118 try f.allocs.put(gpa, local.local, false);
31143119 return CValue{ .local_ref = local.local };
31153120}
31163121
......@@ -3127,7 +3132,7 @@ fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue {
31273132 const local = try f.allocAlignedLocal(elem_ty, mutability, inst_ty.ptrAlignment(target));
31283133 log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.local });
31293134 const gpa = f.object.dg.module.gpa;
3130 try f.allocs.put(gpa, local.local, {});
3135 try f.allocs.put(gpa, local.local, false);
31313136 return CValue{ .local_ref = local.local };
31323137}
31333138