diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-05-26 22:59:01 +0100 |
|---|---|---|
| committer | Matthew Lugg <mlugg@mlugg.co.uk> | 2025-05-27 19:23:11 +0100 |
| commit | 92c63126e8a58235625cceaf0ea9e3d4eacfe513 (patch) | |
| tree | ab36da0c91e39622bdd8a9c49e2f636ee341878d /src/codegen/c.zig | |
| parent | 3ed9155f10b725e6d24c01f5ecbebf359f3599a7 (diff) | |
| download | zig-92c63126e8a58235625cceaf0ea9e3d4eacfe513.tar.gz zig-92c63126e8a58235625cceaf0ea9e3d4eacfe513.zip | |
compiler: tlv pointers are not comptime-known
Pointers to thread-local variables do not have their addresses known
until runtime, so it is nonsensical for them to be comptime-known. There
was logic in the compiler which was essentially attempting to treat them
as not being comptime-known despite the pointer being an interned value.
This was a bit of a mess, the check was frequent enough to actually show
up in compiler profiles, and it was very awkward for backends to deal
with, because they had to grapple with the fact that a "constant" they
were lowering might actually require runtime operations.
So, instead, do not consider these pointers to be comptime-known in
*any* way. Never intern such a pointer; instead, when the address of a
threadlocal is taken, emit an AIR instruction which computes the pointer
at runtime. This avoids lots of special handling for TLVs across
basically all codegen backends; of all somewhat-functional backends, the
only one which wasn't improved by this change was the LLVM backend,
because LLVM pretends this complexity around threadlocals doesn't exist.
This change simplifies Sema and codegen, avoids a potential source of
bugs, and potentially improves Sema performance very slightly by
avoiding a non-trivial check on a hot path.
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 891b84457e..075d9ba303 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -3453,6 +3453,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, .error_set_has_value => return f.fail("TODO: C backend: implement error_set_has_value", .{}), .vector_store_elem => return f.fail("TODO: C backend: implement vector_store_elem", .{}), + .tlv_dllimport_ptr => try airTlvDllimportPtr(f, inst), + .c_va_start => try airCVaStart(f, inst), .c_va_arg => try airCVaArg(f, inst), .c_va_end => try airCVaEnd(f, inst), @@ -7617,6 +7619,17 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { return local; } +fn airTlvDllimportPtr(f: *Function, inst: Air.Inst.Index) !CValue { + const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; + const writer = f.object.writer(); + const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); + try f.writeCValue(writer, local, .Other); + try writer.writeAll(" = "); + try f.object.dg.renderNav(writer, ty_nav.nav, .Other); + try writer.writeAll(";\n"); + return local; +} + fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { const pt = f.object.dg.pt; const zcu = pt.zcu; |
