diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-07-03 22:09:30 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-07-18 19:02:05 -0700 |
| commit | db33ee45b7261c9ec62a1087cfc9377bc4e7aa8f (patch) | |
| tree | 81e1f629c296a8a9c9573879382586dac3784737 /src/codegen/c | |
| parent | 70c71935c7c9f20353dc2a50b497b752d70d3452 (diff) | |
| download | zig-db33ee45b7261c9ec62a1087cfc9377bc4e7aa8f.tar.gz zig-db33ee45b7261c9ec62a1087cfc9377bc4e7aa8f.zip | |
rework generic function calls
Abridged summary:
* Move `Module.Fn` into `InternPool`.
* Delete a lot of confusing and problematic `Sema` logic related to
generic function calls.
This commit removes `Module.Fn` and replaces it with two new
`InternPool.Tag` values:
* `func_decl` - corresponding to a function declared in the source
code. This one contains line/column numbers, zir_body_inst, etc.
* `func_instance` - one for each monomorphization of a generic
function. Contains a reference to the `func_decl` from whence the
instantiation came, along with the `comptime` parameter values (or
types in the case of `anytype`)
Since `InternPool` provides deduplication on these values, these fields
are now deleted from `Module`:
* `monomorphed_func_keys`
* `monomorphed_funcs`
* `align_stack_fns`
Instead of these, Sema logic for generic function instantiation now
unconditionally evaluates the function prototype expression for every
generic callsite. This is technically required in order for type
coercions to work. The previous code had some dubious, probably wrong
hacks to make things work, such as `hashUncoerced`. I'm not 100% sure
how we were able to eliminate that function and still pass all the
behavior tests, but I'm pretty sure things were still broken without
doing type coercion for every generic function call argument.
After the function prototype is evaluated, it produces a deduplicated
`func_instance` `InternPool.Index` which can then be used for the
generic function call.
Some other nice things made by this simplification are the removal of
`comptime_args_fn_inst` and `preallocated_new_func` from `Sema`, and the
messy logic associated with them.
I have not yet been able to measure the perf of this against master
branch. On one hand, it reduces memory usage and pointer chasing of the
most heavily used `InternPool` Tag - function bodies - but on the other
hand, it does evaluate function prototype expressions more than before.
We will soon find out.
Diffstat (limited to 'src/codegen/c')
| -rw-r--r-- | src/codegen/c/type.zig | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/codegen/c/type.zig b/src/codegen/c/type.zig index dbdb65ac29..01540a9f2d 100644 --- a/src/codegen/c/type.zig +++ b/src/codegen/c/type.zig @@ -1722,6 +1722,7 @@ pub const CType = extern union { .Fn => { const info = mod.typeToFunc(ty).?; + const ip = &mod.intern_pool; if (!info.is_generic) { if (lookup.isMutable()) { const param_kind: Kind = switch (kind) { @@ -1730,7 +1731,7 @@ pub const CType = extern union { .payload => unreachable, }; _ = try lookup.typeToIndex(info.return_type.toType(), param_kind); - for (info.param_types) |param_type| { + for (info.param_types.get(ip)) |param_type| { if (!param_type.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; _ = try lookup.typeToIndex(param_type.toType(), param_kind); } @@ -2014,6 +2015,7 @@ pub const CType = extern union { .function, .varargs_function, => { + const ip = &mod.intern_pool; const info = mod.typeToFunc(ty).?; assert(!info.is_generic); const param_kind: Kind = switch (kind) { @@ -2023,14 +2025,14 @@ pub const CType = extern union { }; var c_params_len: usize = 0; - for (info.param_types) |param_type| { + for (info.param_types.get(ip)) |param_type| { if (!param_type.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; c_params_len += 1; } const params_pl = try arena.alloc(Index, c_params_len); var c_param_i: usize = 0; - for (info.param_types) |param_type| { + for (info.param_types.get(ip)) |param_type| { if (!param_type.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; params_pl[c_param_i] = store.set.typeToIndex(param_type.toType(), mod, param_kind).?; c_param_i += 1; @@ -2147,6 +2149,7 @@ pub const CType = extern union { => { if (ty.zigTypeTag(mod) != .Fn) return false; + const ip = &mod.intern_pool; const info = mod.typeToFunc(ty).?; assert(!info.is_generic); const data = cty.cast(Payload.Function).?.data; @@ -2160,7 +2163,7 @@ pub const CType = extern union { return false; var c_param_i: usize = 0; - for (info.param_types) |param_type| { + for (info.param_types.get(ip)) |param_type| { if (!param_type.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; if (c_param_i >= data.param_types.len) return false; @@ -2202,6 +2205,7 @@ pub const CType = extern union { autoHash(hasher, t); const mod = self.lookup.getModule(); + const ip = &mod.intern_pool; switch (t) { .fwd_anon_struct, .fwd_anon_union, @@ -2270,7 +2274,7 @@ pub const CType = extern union { }; self.updateHasherRecurse(hasher, info.return_type.toType(), param_kind); - for (info.param_types) |param_type| { + for (info.param_types.get(ip)) |param_type| { if (!param_type.toType().hasRuntimeBitsIgnoreComptime(mod)) continue; self.updateHasherRecurse(hasher, param_type.toType(), param_kind); } |
