Skip to content

feat: extract Metal (.metal) kernels into the graph#1450

Closed
GoodOlClint wants to merge 1 commit into
Graphify-Labs:v8from
GoodOlClint:feat/metal-extension
Closed

feat: extract Metal (.metal) kernels into the graph#1450
GoodOlClint wants to merge 1 commit into
Graphify-Labs:v8from
GoodOlClint:feat/metal-extension

Conversation

@GoodOlClint

Copy link
Copy Markdown

What

Adds Metal Shading Language (.metal) support to graphify by routing it through
the existing C++ extractor — the exact reuse pattern CUDA's .cu/.cuh
already follow. MSL is C++14, so extract_cpp (tree-sitter-cpp) handles it with
no new grammar. ERROR nodes around Metal-specific qualifiers (kernel,
[[buffer(0)]]) are expected and harmless, just as they are for CUDA's
__global__/__device__.

Why

Before this change graphify classified .metal as non-code and skipped it
entirely, so MLX/Metal compute kernels never appeared in the graph. This lets
them join it like any other source.

Approach

Mirror CUDA. No dedicated Metal grammar — deferred. ~3-line change across the
same three touchpoints CUDA uses.

Edits (3 touchpoints)

  1. graphify/detect.py'.metal' added to CODE_EXTENSIONS (next to .cu/.cuh).
  2. graphify/extract.py".metal": extract_cpp in the extension→extractor dict.
  3. graphify/build.py".metal": "cpp" in _LANG_FAMILY, so Metal↔C++ count
    as one family for cross-language seam / import resolution.

Plus a contract test + tests/fixtures/sample.metal (a real MSL snippet: a
struct + a dot3 function + a saxpy kernel).

Evidence (before → after)

Contract test (tests/test_languages.py, mirrors the cpp/CUDA tests):
extract_cpp(FIXTURES / "sample.metal")error absent, Vec3 struct and
dot3 function both appear in nodes. Symbols graphify produced nothing for before.

E2eGRAPHIFY_OUT=<tmp> graphify update <dir-with-.metal> on a dir
containing one kernel.metal:

graph.json metal-source nodes
before (origin/v8) not created — No code files found - nothing to rebuild 0 (skipped)
after (this branch) Rebuilt: 11 nodes, 10 edges, 4 communities 7

After-state metal nodes: kernel.metal, Vec3, x, y, z, dot3(), saxpy().

Test gateuv run pytest (full suite): 2351 passed, 28 skipped
(includes 3 new test_metal_*). ruff check on changed files: clean.

Notes

  • cpp-reuse, not a new extractor — CUDA precedent; extract_cpp is the point.
  • Dedicated Metal grammar deferred.
  • Branched off v8; pushed to fork GoodOlClint. PR not opened.

Metal Shading Language is C++14, so .metal routes through the existing
cpp extractor — the same reuse pattern CUDA's .cu/.cuh already follow.
No dedicated Metal grammar; ERROR nodes around Metal qualifiers are
expected and harmless. Lets MLX/Metal kernels join the code graph
instead of being skipped as non-code.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
safishamsi pushed a commit that referenced this pull request Jun 27, 2026
Metal Shading Language is C++14, so .metal files are classified as code and routed
through the existing C++ extractor, mirroring the CUDA .cu/.cuh reuse. Also adds
.cu/.cuh/.metal to build.py's _LANG_FAMILY map (they were all missing), so the
cross-language phantom-`calls`-edge filter treats them as C++. README language
table updated.

Ported from PR #1480 by @jiangyq9. This supersedes the earlier #1450 by
@GoodOlClint (same .metal -> C++ approach and fixture) — credit to @GoodOlClint
for the original; #1480 additionally closes the CUDA family-map gap and updates
the docs, and merges clean against current v8.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@safishamsi

Copy link
Copy Markdown
Collaborator

Thanks @GoodOlClint, and apologies for the overlap. Metal support has landed on v8 (0b33511) — same .metal→C++ approach and fixture as yours, with the additional _LANG_FAMILY map fix and README update from #1480. Your PR had drifted behind v8 (detect.py conflict). Closing as superseded, with credit to you noted in the commit for the original approach. Thank you for the contribution.

@safishamsi safishamsi closed this Jun 27, 2026
safishamsi pushed a commit that referenced this pull request Jun 27, 2026
Metal Shading Language is C++14, so .metal files are classified as code and routed
through the existing C++ extractor, mirroring the CUDA .cu/.cuh reuse. Also adds
.cu/.cuh/.metal to build.py's _LANG_FAMILY map (they were all missing), so the
cross-language phantom-`calls`-edge filter treats them as C++. README language
table updated.

Ported from PR #1480 by @jiangyq9. This supersedes the earlier #1450 by
@GoodOlClint (same .metal -> C++ approach and fixture) — credit to @GoodOlClint
for the original; #1480 additionally closes the CUDA family-map gap and updates
the docs, and merges clean against current v8.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants