Pack-fns (`isPackFn(fd) == true` — last param `is_variadic AND
is_comptime`, no other comptime params) now emit ONE
monomorphised function per unique call-site signature. Repeat
calls with the same arg-type tuple share the mono; distinct
shapes get distinct symbols. Pre-2b each call inlined a fresh
body copy into the caller's basic block; IR size grew linearly
in call sites.
Plumbing in `src/ir/lower.zig`:
- `isPackFn(fd)` — true when the only comptime param is a
trailing pack. Mixed `($fmt, ..$args)` shapes stay on the
inline `lowerComptimeCall` path (different substitution
mechanism for the comptime non-pack param; deferred).
- `lowerPackFnCall(fd, call_node)`:
- Builds a mangled name `<fn_name>__pack__<arg_types>` from
call-site `inferExprType` results. Distinct shapes get
distinct symbols.
- Cache-checks `lowered_functions`; calls
`monomorphizePackFn` on miss.
- Lowers call args, then re-fetches the func pointer (the
fetch BEFORE arg lowering would invalidate after any
transitively-triggered module.functions.items realloc),
prepends ctx if needed, coerces, emits direct call.
- `monomorphizePackFn(fd, mangled, arg_types)`:
- Mirrors `monomorphizeFunction` for the standard fn build:
save state, build param list (ctx + fixed prefix + N pack
params with synthesised names `__pack_<name>_<i>`),
`beginFunction`, entry block, bind params to scope.
- Installs `pack_arg_nodes[<name>]` with synthesised AST
identifier nodes pointing at the pack-param slots so the
body's `args[<int_literal>]` substitutes through the
existing 2a.B mechanism — substitution resolves to the
mono's own param slot loads.
- Installs `pack_param_count[<name>] = N` so the body's
`args.len` resolves to a compile-time constant via a new
intercept in `lowerFieldAccess` (and the parallel arm in
`inferExprType`).
- Lowers the body with `inline_return_target = null` so
`return X;` emits a real `ret X` instead of the inline-slot
routing — the mono is a real fn now.
- Routed at three call sites: each `if (hasComptimeParams(fd))
{ return self.lowerComptimeCall(...); }` now first checks
`isPackFn(fd)` and routes to `lowerPackFnCall` when true.
Lifetime gotcha caught and fixed: `params.items` is stored by
reference in `Function.init` (no copy), so the local
`ArrayList(Function.Param)` must NOT be deinit'd in
`monomorphizePackFn` — matches the leak convention already used
by `monomorphizeFunction`.
`examples/158-pack-mono-dedup.sx` confirms the dedup
end-to-end: `count(), count(1), count(2), count(1,2,3),
count("x", true)` produces `0 1 1 3 2` at runtime AND emits
exactly 4 monos in IR (`count__pack`, `count__pack_s64`,
`count__pack_s64_s64_s64`, `count__pack_string_bool`) — the
two s64 calls share. `args.len` resolves to the comptime
constant N inside each mono.
`examples/156-pack-typed-index.sx` and
`examples/157-pack-if-return.sx` continue to pass unchanged.
Out of scope:
- Mixed `$fmt + ..$args` shapes (stays on inline path).
- Generic `$R` return types (concrete returns only).
- Bare `args` reference (passing the slice as a whole).
- `args[<runtime_int>]` (non-literal index).
197/197 example tests + `zig build test` green.
31 lines
1.0 KiB
Plaintext
31 lines
1.0 KiB
Plaintext
// Variadic heterogeneous type packs — step 2b: per-call-shape
|
|
// monomorphisation. Each unique call signature gets ONE mono fn;
|
|
// repeat calls with the same signature share it. The runtime output
|
|
// confirms correct semantics; the IR (visible via `sx ir`) shows
|
|
// the distinct mono symbols:
|
|
//
|
|
// call @count__pack(ctx)
|
|
// call @count__pack_s64(ctx, 1)
|
|
// call @count__pack_s64(ctx, 2) ← shares with the 1-arg s64 call
|
|
// call @count__pack_s64_s64_s64(ctx, 1, 2, 3)
|
|
// call @count__pack_string_bool(ctx, ..)
|
|
//
|
|
// Before step 2b, each call inlined a fresh copy of the body into
|
|
// main's basic block — no shared symbols, IR size grew linearly in
|
|
// call sites. After 2b, distinct shapes get distinct functions,
|
|
// repeats share, IR scales with unique shapes.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
count :: (..$args) -> s64 => args.len;
|
|
|
|
main :: () -> s32 {
|
|
a := count();
|
|
b := count(1);
|
|
c := count(2);
|
|
d := count(1, 2, 3);
|
|
e := count("x", true);
|
|
print("{} {} {} {} {}\n", a, b, c, d, e);
|
|
return 0;
|
|
}
|