Skip to content

native: share one resource slot registry across partitions - #633

Merged
jackalcooper merged 3 commits into
mainfrom
agent/kind-registry
Aug 12, 2026
Merged

native: share one resource slot registry across partitions#633
jackalcooper merged 3 commits into
mainfrom
agent/kind-registry

Conversation

@jackalcooper

Copy link
Copy Markdown
Contributor

背景

beaver 的 NIF 加载入口(nif_load)把 MLIR C API 分片成多个 DSO(core / conversion / callback_bridge / rewrite_pattern / cuda)。每个 kind 有 3 个 ERTS resource slot(value / Ptr / Array),跨 partition 共享 resource type handle 时不能重复 enif_open_resource_type(会创建不同 resource type),所以 core 通过 core_resource_type_by_name 按名字发布 handle,leaf partition 通过 syncResourceTypes 拉取。

现状是三份手写遍历:

  • core.zigcore_resource_type_by_name:逐个 allKinds + Internal 类型 + ReplyToken 手写字符串比较;
  • resource_sync.zigsyncResourceTypes:手写同一批 kind 的同步;
  • mlir_capi.zigopen_all:5 处手写 aliasKind 调用。

改动

依赖 kinda 5d34fb2ResourceKind 暴露统一的 slots 数组,open/alias 都遍历槽位)。

  • mlir_capi.zig:新增 resourceSlots comptime 注册表(所有 kind 的 3 槽位 + Internal 4 个 kind + callback ReplyToken),alias 收敛为 kindAliases 表;
  • core.zigcore_resource_type_by_name 遍历注册表;
  • resource_sync.zigsyncResourceTypes 遍历同一注册表;
  • build.zig.zon / kinda.ref:kinda pin 更新到 5d34fb2。

验证

  • zig build(beaver_native 全部分片编译通过);
  • mix test:431 passed(含 resource 密集的 op/memref/pattern/transform 模块),5 skipped,21 excluded(triton/cuda 相关)。

后续

注册表目前是线性 comptime 遍历(~200 槽位),加载时一次性执行;如果未来槽位数进一步膨胀,可以换排序+二分,但当前量级没必要。

Collect every ERTS resource slot (value/Ptr/Array per kind, plus the
internal kinds and callback reply token) into a single comptime
registry in mlir_capi.zig. The core partition publishes handles by
slot name and the leaf partitions sync from it, replacing the hand
written per-kind lookups in core.zig and resource_sync.zig. Kind
aliases (OpaquePtr/OpaqueArray/USize/DiagnosticHandlerID/
SparseTensorLevelType) are declared as one table instead of five
aliasKind calls.

Requires kinda 5d34fb2 exposing ResourceKind.slots.
Kinda 0.11.0 on Hex predates the ResourceSlot registry API that the
partition resource sync now depends on, so the paired checkout can no
longer be Windows-only. Pin kinda.ref and build.zig.zon to the merged
aa43cd0 (beaver-lodge/kinda#87).
Kinda 0.11.0-dev (aa43cd0) requires Elixir ~> 1.20, which is not
available on OTP 25; the dependency chain no longer supports that
lane. Keep the Elixir 1.18 coverage on OTP 27, which passes.

The docker_build job previously resolved kinda from Hex (which lacks
the ResourceSlot API), so it also needs the paired checkout and
BEAVER_KINDA_PATH when publishing.
@jackalcooper
jackalcooper merged commit 9aca0c2 into main Aug 12, 2026
12 checks passed
@jackalcooper
jackalcooper deleted the agent/kind-registry branch August 12, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant