Skip to content

Apple Target IR fusion descriptors — emit + consume (Decision #19) - #63

Merged
gstoner merged 3 commits into
mainfrom
feat/apple-fusion-descriptor
Jun 11, 2026
Merged

gstoner merged 3 commits into
mainfrom
feat/apple-fusion-descriptor

Conversation

@gstoner

@gstoner gstoner commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Closes the COMPILER_AUDIT "fusion intent is too late" item for the Apple backend: the Target IR fusion passes re-discovered the same matmul→softmax[→matmul] / matmul→gelu / matmul→rmsnorm / swiglu chains the canonical compile already computes (and the runtime already consumes via fusion_groups). This makes the fusion decision first-class in Target IR (Decision #19, "emit backend descriptors").

C++ — all 7 Apple fusion passes emit a descriptor

Each fused call now carries tessera.fusion.kernel + tessera.fusion.source:

  • 4 chain passes (matmul→softmax→matmul, →softmax, →gelu, →rmsnorm) also consume an upstream tessera.fusion.intentsource="descriptor"; absent it, structural re-discovery → source="rediscovered", with a Decision-Apple GPU Tier-3: conv2d via MPSGraph convolution2D #21 warning on descriptor/IR disagreement.
  • 3 composite passes (swiglu / mla_decode / native_sparse_attn) emit source="composite_op" — they lower a pre-fused op (the op is the descriptor).

Python — the emit-half

canonical_compile.stamp_fusion_intents(module) stamps tessera.fusion.intent on each recognized chain's terminal op (from the canonical _KNOWN_FUSION_CHAINS), so the frontend produces descriptor-annotated IR. Idempotent. An emit↔consume contract test ties the Python intents to the C++ consumers so the halves can't drift.

Verification

Rebuilt clean; full tessera-ir lit 119 pass / 0 fail; new lit fixture (descriptor + rediscovered across softmax/gelu/rmsnorm) + test_apple_fusion_descriptor.py + test_fusion_intent_emitter.py green; mypy clean; drift gate in sync.

🤖 Generated with Claude Code

gstoner and others added 3 commits June 11, 2026 14:23
… slice 1)

The Apple Target IR fusion passes re-discover the matmul→softmax[→matmul] /
matmul→gelu / ... chains the canonical compile already recognized (COMPILER_AUDIT
"fusion intent is too late"). This lands the flagship slice that makes the
fusion decision first-class in Target IR.

MatmulSoftmaxMatmulFusionToAppleGPU.cpp now:
- EMITS the descriptor on the fused call — tessera.fusion.kernel =
  "matmul_softmax_matmul" + tessera.fusion.source = "descriptor" | "rediscovered"
  — so the fusion decision (which kernel, and whether the compiler's intent drove
  it vs. structural re-discovery) is auditable in the IR (Decision #19).
- CONSUMES an upstream tessera.fusion.intent on the tail op: when present the
  fusion is descriptor-driven (source="descriptor"); absent it, the structural
  walk re-discovers it (source="rediscovered", back-compat). Both fuse to the
  same kernel.
- On a descriptor/IR disagreement (intent set but structure doesn't match) emits
  a Decision-#21 warning naming the op instead of silently falling back.

Built clean; full tessera-ir lit 119 pass / 0 fail (existing fusion fixtures
unaffected by the new call attrs). Tests:
tests/tessera-ir/phase8/apple_gpu_fusion_descriptor.mlir (lit) +
tests/unit/test_apple_fusion_descriptor.py (2). test_coverage regenerated;
COMPILER_AUDIT updated.

Follow-on: same template for the other 6 Apple fusion passes + a Python emitter
stamping tessera.fusion.intent from the canonical _KNOWN_FUSION_CHAINS.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Completes the Decision #19 follow-on started in the flagship slice.

C++ (Tile→Apple fusion passes):
- 3 more chain passes (matmul→softmax, matmul→gelu, matmul→rmsnorm) now consume
  an upstream tessera.fusion.intent (source="descriptor") and fall back to
  structural re-discovery (source="rediscovered"), with a Decision-#21 warning on
  descriptor/IR disagreement — same template as matmul→softmax→matmul.
- 3 composite passes (swiglu / mla_decode / native_sparse_attn) emit the
  descriptor with source="composite_op" (they lower a pre-fused op, which is
  itself the descriptor — no chain re-discovery).
All 7 fused calls now carry tessera.fusion.kernel + tessera.fusion.source, so the
fusion decision is first-class/auditable in Target IR.

Python (emit-half):
- canonical_compile.stamp_fusion_intents(module) tags the terminal op of each
  recognized linear chain with tessera.fusion.intent (from the canonical
  _KNOWN_FUSION_CHAINS), so the frontend produces descriptor-annotated IR that
  the C++ passes consume. Idempotent.

Built clean; full tessera-ir lit 119 pass / 0 fail. Tests:
tests/tessera-ir/phase8/apple_gpu_fusion_descriptor.mlir (extended to gelu +
rmsnorm) and tests/unit/test_fusion_intent_emitter.py (6, incl. an emit↔consume
contract guard tying the Python intents to the C++ consumers). mypy clean.
COMPILER_AUDIT updated.

Remaining: auto-wire stamp_fusion_intents into a Target-IR lowering path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Closes the Decision #19 loop end-to-end: the frontend now auto-produces
descriptor-annotated Graph IR for Apple targets, which the Target IR fusion
passes consume (source="descriptor").

- driver.compile_graph_module calls stamp_fusion_intents(module) before rendering
  the Graph IR, gated to apple_gpu / apple_cpu (the only Target IR consumers
  today; the descriptor is backend-agnostic, extends when others consume it).
  Lazy import avoids the canonical_compile ↔ driver cycle.
- Fix: stamp the intent into the op's MLIR `attrs` field, NOT `kwargs`. kwargs
  are forwarded as the op's real call arguments in the reference/runtime
  execution path, so a descriptor there leaked into the numpy op call
  (gelu(**kwargs) got an unexpected 'tessera.fusion.intent'). attrs renders only
  into the MLIR text the C++ passes read.

Verified: apple_gpu/apple_cpu compiles carry tessera.fusion.intent on chain
terminals; cpu is gated (unstamped); the previously-broken apple_cpu transformer
execution test passes; 116 apple/canonical/strict-dispatch tests green; mypy
clean; drift in sync. emitter tests updated to assert via attrs + that the
intent never leaks into kwargs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@gstoner
gstoner merged commit 27f91c8 into main Jun 11, 2026
22 checks passed
@gstoner
gstoner deleted the feat/apple-fusion-descriptor branch June 15, 2026 00:10
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