ffi 1.5: intern Obj-C selectors — one static SEL slot per unique name
101/101 regression tests pass; the IR snapshot for the selector-
sharing test diff flips from four per-call `sel_registerName` calls
to two (one per unique selector) routed through a module-init
constructor — matching what clang emits for `@selector(...)`.
Hot-path cost collapses from a libobjc hashtable lookup per call to
a single load of a static `SEL*` slot:
Before (Phase 1.3):
%sel = call ptr @sel_registerName(<"init">)
call ptr @objc_msgSend(<recv>, %sel)
After (Phase 1.5):
%sel = load ptr, ptr @OBJC_SELECTOR_REFERENCES_init
call ptr @objc_msgSend(<recv>, %sel)
+ @OBJC_SELECTOR_REFERENCES_init = internal global ptr null
+ @OBJC_SELECTOR_REFERENCES_release = internal global ptr null
+ define internal void @__sx_objc_selector_init() {
+ %sel = call ptr @sel_registerName(ptr @OBJC_METH_VAR_NAME_)
+ store ptr %sel, ptr @OBJC_SELECTOR_REFERENCES_init
+ %sel1 = call ptr @sel_registerName(ptr @OBJC_METH_VAR_NAME_.2)
+ store ptr %sel1, ptr @OBJC_SELECTOR_REFERENCES_release
+ ret void
+ }
+ @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }]
+ [{ ..., ptr @__sx_objc_selector_init, ptr null }]
Implementation:
module.zig | new `objc_selector_cache: ArrayList(ObjcSelectorEntry)`
with `lookupObjcSelector` / `appendObjcSelector`. List
(not hashmap) keeps emit order stable across builds so
the IR snapshot doesn't flicker on rehash.
lower.zig | `internObjcSelector(sel)` creates the slot on first
use, returns the same `GlobalId` on every subsequent
call to the same selector. lowerFfiIntrinsicCall now
emits `global_addr + load` for literal selectors.
Non-literal selectors keep the `sel_registerName`
fallback. Declaring `sel_registerName` lazily on
first intern so emit_llvm finds it for the
constructor body.
emit_llvm.zig | new `emitObjcSelectorInit` pass synthesizes a void
constructor that loops over the cache, calls
`sel_registerName` for each unique selector string,
stores the result in the slot. Constructor is
registered in `@llvm.global_ctors` with default
priority (65535) so dyld runs it before main.
The `@OBJC_METH_VAR_NAME_` private string globals and unnamed-addr
flag match clang's exact emission shape — picked up by the system
linker into the right Mach-O sections on macOS / iOS. Chess
Android + iOS-sim still build clean (no `#objc_call` in chess yet —
phase-3 migration will start exercising this).
This commit is contained in:
@@ -3734,6 +3734,43 @@ pub const Lowering = struct {
|
||||
|
||||
// ── FFI intrinsics (#objc_call / #jni_call / #jni_static_call) ─
|
||||
|
||||
/// Intern an Obj-C selector string into a module-scoped `SEL*` slot.
|
||||
/// First call creates the global; subsequent calls return the same
|
||||
/// `GlobalId`. emit_llvm.zig walks `module.objc_selector_cache` and
|
||||
/// synthesizes a constructor that populates each slot via
|
||||
/// `sel_registerName` exactly once at module load.
|
||||
///
|
||||
/// Slot name matches clang's convention: `OBJC_SELECTOR_REFERENCES_<sel>`
|
||||
/// with `:` replaced by `_` to keep the symbol name valid.
|
||||
fn internObjcSelector(self: *Lowering, sel_str: []const u8) inst_mod.GlobalId {
|
||||
if (self.module.lookupObjcSelector(sel_str)) |gid| return gid;
|
||||
|
||||
// First interned selector → ensure `sel_registerName` is declared
|
||||
// so emit_llvm.zig's constructor pass can find it and populate
|
||||
// every cached SEL slot at module load.
|
||||
_ = self.getSelRegisterNameFid();
|
||||
|
||||
// Mangle selector: replace colons with underscores. Apple's
|
||||
// toolchain does the same (foo:bar: → foo_bar_).
|
||||
var mangled = std.ArrayList(u8).empty;
|
||||
defer mangled.deinit(self.alloc);
|
||||
mangled.appendSlice(self.alloc, "OBJC_SELECTOR_REFERENCES_") catch unreachable;
|
||||
for (sel_str) |ch| {
|
||||
mangled.append(self.alloc, if (ch == ':') '_' else ch) catch unreachable;
|
||||
}
|
||||
const slot_name = self.module.types.internString(mangled.items);
|
||||
const vptr_ty = self.module.types.ptrTo(.void);
|
||||
const gid = self.module.addGlobal(.{
|
||||
.name = slot_name,
|
||||
.ty = vptr_ty,
|
||||
.init_val = .null_val,
|
||||
.is_extern = false,
|
||||
.is_const = false,
|
||||
});
|
||||
self.module.appendObjcSelector(sel_str, gid);
|
||||
return gid;
|
||||
}
|
||||
|
||||
/// Lazily declare `sel_registerName(name: *u8) -> *void` as an extern.
|
||||
/// Cached per Lowering instance so multiple `#objc_call` sites share
|
||||
/// one declaration.
|
||||
@@ -3810,25 +3847,30 @@ pub const Lowering = struct {
|
||||
// Receiver expression.
|
||||
const recv = self.lowerExpr(fic.args[0]);
|
||||
|
||||
// Selector must be a literal string at parse time so we can
|
||||
// intern it (Phase 1.5 will cache the SEL too). For Phase 1.3
|
||||
// we accept any expression that lowers to a string Ref.
|
||||
// Selector. If it's a literal at parse time, intern into a
|
||||
// module-scoped `SEL*` slot that emit_llvm.zig populates once
|
||||
// at module init (Phase 1.5). Per call site collapses to a
|
||||
// single load — matches clang's `@selector(...)` lowering.
|
||||
// Non-literal selectors keep the per-call sel_registerName
|
||||
// fallback for now.
|
||||
const sel_arg_node = fic.args[1];
|
||||
const sel_ref = blk: {
|
||||
const vptr_ty = self.module.types.ptrTo(.void);
|
||||
const sel = blk: {
|
||||
if (sel_arg_node.data == .string_literal) {
|
||||
const raw = sel_arg_node.data.string_literal.raw;
|
||||
break :blk self.builder.constString(self.module.types.internString(raw));
|
||||
const slot_gid = self.internObjcSelector(raw);
|
||||
const slot_ptr = self.builder.emit(.{ .global_addr = slot_gid }, self.module.types.ptrTo(vptr_ty));
|
||||
break :blk self.builder.emit(.{ .load = .{ .operand = slot_ptr } }, vptr_ty);
|
||||
}
|
||||
break :blk self.lowerExpr(sel_arg_node);
|
||||
// Fallback: non-literal selector → runtime lookup per call.
|
||||
const sel_ref = self.lowerExpr(sel_arg_node);
|
||||
const sel_fid = self.getSelRegisterNameFid();
|
||||
var sel_args = std.ArrayList(Ref).empty;
|
||||
sel_args.append(self.alloc, sel_ref) catch unreachable;
|
||||
const sel_owned = sel_args.toOwnedSlice(self.alloc) catch unreachable;
|
||||
break :blk self.builder.emit(.{ .call = .{ .callee = sel_fid, .args = sel_owned } }, vptr_ty);
|
||||
};
|
||||
|
||||
// Resolve selector via the runtime — per call site for now.
|
||||
const sel_fid = self.getSelRegisterNameFid();
|
||||
var sel_args = std.ArrayList(Ref).empty;
|
||||
sel_args.append(self.alloc, sel_ref) catch unreachable;
|
||||
const sel_owned = sel_args.toOwnedSlice(self.alloc) catch unreachable;
|
||||
const sel = self.builder.emit(.{ .call = .{ .callee = sel_fid, .args = sel_owned } }, self.module.types.ptrTo(.void));
|
||||
|
||||
// Dispatch through objc_msgSend.
|
||||
const msg_fid = self.getObjcMsgSendFid();
|
||||
var call_args = std.ArrayList(Ref).empty;
|
||||
|
||||
Reference in New Issue
Block a user