Bind an intrinsic's immediates from the call sites of the function passing them on

Metal's compiler leaves OpenCL C's barrier(flags) and mem_fence(flags) out of line: an internal
function passes its parameter on as air.wg.barrier's flags, and mem_fence computes
air.atomic.fence's flags from it through llvm.bitreverse.i2. Every call site passes a constant.
Decode refused the call (Intrinsic(ImmediateNotConstant)), and so every OpenCL kernel that
synchronizes was refused.

Decode now keeps such a call as Callee::Unbound, with the family its declaration names
(intrinsics::Template). Once inlining has made the parameter the call site's constant, SCCP's
edit binds it in the same rebuild, with no pass of its own. SCCP also folds the integer llvm.*
operations (min/max, ctpop, bitreverse, bswap) over constants. LlvmIntrinsic::NarrowInt decodes
those operations on widths no SPIR-V type has (i2), so the fence's flags fold. An immediate still
not constant after canonicalization is refused at the call, in post_check, with the inlined
entry's function location. A NarrowInt that survives is refused as Lowering(Arith).

Tests:
- canon::tests::unbound_immediates: each call site binds its own flags; binding meters no more
  work than the constant twin (264 vs 265 units) and prints the same module; the i2 fence binds
  after folding; flags read from a buffer are refused at the call.
- intrinsics::tests: the narrow-width decode.
- runtime tests::opencl_kernels: a barrier and a fence out of line compute what the M3 and the
  Intel UHD 630 compute.

Verified on the same changes at base c956a4236c: metal2vulkan-air 516/516 (m3-max-4223), the
runtime test on lavapipe (m3-max-4223), and the consumer's glcheck b, b0, f and f0 passing with
the 5080 and 780M profiles (m3-max-4227).

(cherry picked from commit f49960f427b2f1371468b97a215788dea8018d45)
This commit is contained in:
Anees Iqbal committed 2026-11-07 09:53:17 -08:00
1 parent fa599c471a
commit 89d8e46d26
37 files changed
+758 -76

No files matched your search

+11
View File
@@ -596,6 +596,17 @@ layout stride of `None` is `MTLBufferLayoutStrideDynamic`: the stride comes from
does.
- **Dynamic libraries.** `DynamicLibrary::new(library, &budget)` checks a dynamic metallib as
Metal's `newDynamicLibrary:` does; pipelines link its exported functions.
- **OpenCL C kernels.** Metal compiles OpenCL C (`newLibraryWithSource` with the compile options'
`sourceLanguage` 1, which the GL renderer's compute path uses), naming each kernel
`__entry_<name>`, and its AIR has shapes MSL's does not:
- A helper left out of line that passes its parameter on as an intrinsic's immediate operand:
OpenCL C's `barrier(flags)` passes its flags to `air.wg.barrier`, and `mem_fence(flags)`
computes `air.atomic.fence`'s flags from them with `llvm.bitreverse.i2`. Each inlined copy
reads its own call site's constants, folded through the integer arithmetic between them. An
immediate that is still not a constant after that is
`InvalidInput(Intrinsic(ImmediateNotConstant))` at the call. An integer operation on a width
no SPIR-V type has (`i2`) is folded over constants; one that is not folded is refused
(`Lowering(Arith)`).
## 4. A render pipeline