Fixture pooled_cells: thread pointers held in an array of structs, and Metal's answer

An owned fixture of a renderer's closure array: closure_alloc hands out the next element or
null, closure_alloc_extra hands out a thread pointer to room at the array's end, which the
element keeps among its data. Four kernels:

- pooled_closures: elements and extra room handed out at run-time indices, read back and
  summed.
- pooled_bytes: a pointer into another array copied into an element as bytes, over a length the
  run reads, then read and written through.
- pooled_spill: the same pointer's bits stored into device memory (volatile) and read back into
  the element as an integer.
- pooled_context: the closures beside a context struct holding a constant pointer to an
  argument buffer whose pointer leads to two textures' resource IDs, read by a noinline
  function through the context.

The .ll and .air.ll companions and the TOOLCHAIN line are regenerated on the M3 Max.
probes/pooled_cells.m prints Metal's words on the M3 Max (macOS 27.0.1). pooled_bytes and
pooled_spill answer alike there.

Verified: the probe ran on the M3 Max and its words are the tests' constants.
This commit is contained in:
Anees Iqbal committed 2026-11-12 10:57:17 -08:00
1 parent be2e4d5c26
commit 941e1105ce
5 files changed
+1669

No files matched your search

@@ -0,0 +1,295 @@
// What Metal answers for thread pointers held in a thread's array of structs at indices only the
// run knows, among the structs' data: the owned fixture `engine/pooled_cells.metal`'s source (its
// header gives each word). Runs `pooled_closures`, `pooled_bytes` (buffer 2: `len[0]` = 8),
// `pooled_spill` (buffer 2: eight 8-byte words) and `pooled_context` (buffer 2: the address of a
// buffer holding the resource IDs of two 4 by 1 `R32Float` textures, 1 2 3 4 and 10 20 30 40) over
// 8 threads in one threadgroup with `in[i] = i + 0.5` (16 words), and prints each thread's 8 words.
//
// Build (no cargo target needed):
// clang -fobjc-arc -O1 -fmodules -o pooled_cells pooled_cells.m \
// -framework Foundation -framework Metal
// Run: ./pooled_cells, MTL_DEBUG_LAYER unset.
//
// WHAT IT MEASURED (Apple M3 Max, macOS 27.0.1): see the runtime lane's
// `runtime_lane::pooled_cells` module docs.
#import <Foundation/Foundation.h>
#import <Metal/Metal.h>
#include <stdio.h>
#include <string.h>
static const char *SOURCE =
"#include <metal_stdlib>\n"
"using namespace metal;\n"
"\n"
"struct Closure {\n"
" float w;\n"
" int type;\n"
" thread float *extra;\n"
" float data[2];\n"
"};\n"
"\n"
"struct Shader {\n"
" Closure closure[6];\n"
" int num;\n"
" int left;\n"
"};\n"
"\n"
"static thread Closure *closure_alloc(thread Shader &sd, int type, float w) {\n"
" if (sd.left == 0)\n"
" return nullptr;\n"
" thread Closure *sc = &sd.closure[sd.num];\n"
" sc->type = type;\n"
" sc->w = w;\n"
" sc->extra = nullptr;\n"
" sd.num++;\n"
" sd.left--;\n"
" return sc;\n"
"}\n"
"\n"
"static thread float *closure_alloc_extra(thread Shader &sd) {\n"
" if (sd.left == 0)\n"
" return nullptr;\n"
" sd.left--;\n"
" return &sd.closure[sd.num + sd.left].data[0];\n"
"}\n"
"\n"
"// Three elements, each with extra room where some is left.\n"
"static void fill(thread Shader &sd, float x) {\n"
" for (int j = 0; j < 3; j++) {\n"
" thread Closure *c = closure_alloc(sd, j + 1, x + float(j));\n"
" if (c == nullptr)\n"
" continue;\n"
" thread float *e = closure_alloc_extra(sd);\n"
" if (e != nullptr) {\n"
" e[0] = float(10 * (j + 1));\n"
" e[1] = x;\n"
" c->extra = e;\n"
" }\n"
" }\n"
"}\n"
"\n"
"// Words [first..8) of `pooled_closures`.\n"
"static void summarize(thread Shader &sd, device float *o, int first) {\n"
" int types = 0, extras = 0;\n"
" float seconds = 0.0f;\n"
" for (int i = 0; i < 3; i++) {\n"
" float v = -1.0f;\n"
" if (i < sd.num) {\n"
" thread float *e = sd.closure[i].extra;\n"
" v = sd.closure[i].w + (e != nullptr ? e[0] : 0.0f);\n"
" types += sd.closure[i].type;\n"
" if (e != nullptr) {\n"
" seconds += e[1];\n"
" extras++;\n"
" }\n"
" }\n"
" if (i >= first)\n"
" o[i] = v;\n"
" }\n"
" o[3] = float(sd.num);\n"
" o[4] = float(sd.left);\n"
" o[5] = float(types);\n"
" o[6] = seconds;\n"
" o[7] = float(extras);\n"
"}\n"
"\n"
"kernel void pooled_closures(device float *out [[buffer(0)]],\n"
" device const float *in [[buffer(1)]],\n"
" uint t [[thread_position_in_grid]]) {\n"
" switch (t >> 31) {\n"
" Shader sd;\n"
" default:\n"
" sd.num = 0;\n"
" sd.left = 2 + int(t & 3);\n"
" fill(sd, in[t]);\n"
" summarize(sd, out + 8 * t, 0);\n"
" }\n"
"}\n"
"\n"
"struct Holder {\n"
" thread float *p;\n"
" int n;\n"
"};\n"
"\n"
"kernel void pooled_bytes(device float *out [[buffer(0)]],\n"
" device const float *in [[buffer(1)]],\n"
" device const int *len [[buffer(2)]],\n"
" uint t [[thread_position_in_grid]]) {\n"
" switch (t >> 31) {\n"
" Shader sd;\n"
" float vals[4];\n"
" Holder h;\n"
" default:\n"
" for (int k = 0; k < 4; k++)\n"
" vals[k] = in[t] + float(k);\n"
" h.p = &vals[t & 3];\n"
" h.n = len[0];\n"
" sd.num = int(t & 1);\n"
" sd.left = 2;\n"
" thread Closure *c = closure_alloc(sd, 1, 1.0f);\n"
" thread uchar *d = (thread uchar *)&c->extra;\n"
" thread const uchar *s = (thread const uchar *)&h.p;\n"
" for (int k = 0; k < h.n; k++)\n"
" d[k] = s[k];\n"
" device float *o = out + 8 * t;\n"
" thread float *e = sd.closure[sd.num - 1].extra;\n"
" o[0] = *e;\n"
" *e += 100.0f;\n"
" o[1] = vals[t & 3];\n"
" o[2] = e == &vals[t & 3] ? 1.0f : 0.0f;\n"
" o[3] = float(sd.num);\n"
" }\n"
"}\n"
"\n"
"kernel void pooled_spill(device float *out [[buffer(0)]],\n"
" device const float *in [[buffer(1)]],\n"
" volatile device ulong *spill [[buffer(2)]],\n"
" uint t [[thread_position_in_grid]]) {\n"
" switch (t >> 31) {\n"
" Shader sd;\n"
" float vals[4];\n"
" default:\n"
" for (int k = 0; k < 4; k++)\n"
" vals[k] = in[t] + float(k);\n"
" thread float *p = &vals[t & 3];\n"
" ulong bits;\n"
" __builtin_memcpy(&bits, &p, sizeof(p));\n"
" spill[t] = bits;\n"
" sd.num = int(t & 1);\n"
" sd.left = 2;\n"
" thread Closure *c = closure_alloc(sd, 1, 1.0f);\n"
" ulong back = spill[t];\n"
" __builtin_memcpy(&c->extra, &back, sizeof(back));\n"
" device float *o = out + 8 * t;\n"
" thread float *e = sd.closure[sd.num - 1].extra;\n"
" o[0] = *e;\n"
" *e += 100.0f;\n"
" o[1] = vals[t & 3];\n"
" o[2] = e == &vals[t & 3] ? 1.0f : 0.0f;\n"
" o[3] = float(sd.num);\n"
" }\n"
"}\n"
"\n"
"struct Held {\n"
" texture2d<float> t;\n"
"};\n"
"\n"
"struct Anc {\n"
" device const Held *held;\n"
"};\n"
"\n"
"struct Context {\n"
" constant Anc *anc;\n"
" int k;\n"
"};\n"
"\n"
"__attribute__((noinline)) float shade(thread Context &ctx, thread Shader &sd, float x, uint t) {\n"
" fill(sd, x);\n"
" return ctx.anc->held[ctx.k].t.read(uint2(t & 3, 0)).x;\n"
"}\n"
"\n"
"kernel void pooled_context(device float *out [[buffer(0)]],\n"
" device const float *in [[buffer(1)]],\n"
" constant Anc *anc [[buffer(2)]],\n"
" uint t [[thread_position_in_grid]]) {\n"
" switch (t >> 31) {\n"
" Shader sd;\n"
" Context ctx;\n"
" default:\n"
" ctx.anc = anc;\n"
" ctx.k = int(t & 1);\n"
" sd.num = 0;\n"
" sd.left = 2 + int(t & 3);\n"
" device float *o = out + 8 * t;\n"
" o[0] = shade(ctx, sd, in[t], t);\n"
" summarize(sd, o, 1);\n"
" }\n"
"}\n";
enum { THREADS = 8, WORDS = 8 };
static id<MTLBuffer> buffer(id<MTLDevice> D, const void *bytes, size_t n) {
id<MTLBuffer> b = [D newBufferWithLength:n options:MTLResourceStorageModeShared];
memcpy(b.contents, bytes, n);
return b;
}
// One run of `name` over THREADS threads in one threadgroup: buffer 0 the words, buffer 1
// `in[i] = i + 0.5`, buffer 2 `extra` when given; `used` made resident.
static void run(id<MTLDevice> D, id<MTLCommandQueue> Q, id<MTLLibrary> lib, NSString *name,
id<MTLBuffer> extra, NSArray<id<MTLResource>> *used) {
NSError *e = nil;
id<MTLFunction> f = [lib newFunctionWithName:name];
id<MTLComputePipelineState> ps = [D newComputePipelineStateWithFunction:f error:&e];
if (!ps) {
printf("RESULT %s pipeline error %s\n", name.UTF8String, e.localizedDescription.UTF8String);
return;
}
float ins[16];
for (int i = 0; i < 16; i++) ins[i] = (float)i + 0.5f;
id<MTLBuffer> in = buffer(D, ins, sizeof ins);
id<MTLBuffer> out = [D newBufferWithLength:THREADS * WORDS * 4 options:MTLResourceStorageModeShared];
memset(out.contents, 0, THREADS * WORDS * 4);
id<MTLCommandBuffer> cb = [Q commandBuffer];
id<MTLComputeCommandEncoder> ce = [cb computeCommandEncoder];
[ce setComputePipelineState:ps];
[ce setBuffer:out offset:0 atIndex:0];
[ce setBuffer:in offset:0 atIndex:1];
if (extra) [ce setBuffer:extra offset:0 atIndex:2];
for (id<MTLResource> r in used) [ce useResource:r usage:MTLResourceUsageRead];
[ce dispatchThreads:MTLSizeMake(THREADS, 1, 1) threadsPerThreadgroup:MTLSizeMake(THREADS, 1, 1)];
[ce endEncoding];
[cb commit];
[cb waitUntilCompleted];
printf("RESULT %s status=%ld\n", name.UTF8String, (long)cb.status);
const float *w = (const float *)out.contents;
for (int t = 0; t < THREADS; t++) {
printf("RESULT %s thread %d:", name.UTF8String, t);
for (int i = 0; i < WORDS; i++) printf(" %g", w[t * WORDS + i]);
printf("\n");
}
}
// A 4 by 1 `R32Float` texture holding `texels`.
static id<MTLTexture> texture(id<MTLDevice> D, const float *texels) {
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
width:4
height:1
mipmapped:NO];
d.usage = MTLTextureUsageShaderRead;
d.storageMode = MTLStorageModeShared;
id<MTLTexture> t = [D newTextureWithDescriptor:d];
[t replaceRegion:MTLRegionMake2D(0, 0, 4, 1) mipmapLevel:0 withBytes:texels bytesPerRow:16];
return t;
}
int main(void) {
@autoreleasepool {
printf("RESULT debug_layer=%s\n", getenv("MTL_DEBUG_LAYER") ? getenv("MTL_DEBUG_LAYER") : "unset");
id<MTLDevice> D = MTLCreateSystemDefaultDevice();
printf("RESULT device=%s\n", D.name.UTF8String);
id<MTLCommandQueue> Q = [D newCommandQueue];
NSError *e = nil;
id<MTLLibrary> lib = [D newLibraryWithSource:[NSString stringWithUTF8String:SOURCE]
options:[MTLCompileOptions new]
error:&e];
if (!lib) {
printf("RESULT library error %s\n", e.localizedDescription.UTF8String);
return 1;
}
run(D, Q, lib, @"pooled_closures", nil, @[]);
int len = 8;
run(D, Q, lib, @"pooled_bytes", buffer(D, &len, sizeof len), @[]);
id<MTLBuffer> spill = [D newBufferWithLength:THREADS * 8 options:MTLResourceStorageModeShared];
run(D, Q, lib, @"pooled_spill", spill, @[]);
// held[0] and held[1]: two textures; anc: the address of held.
const float a[4] = {1, 2, 3, 4}, b[4] = {10, 20, 30, 40};
id<MTLTexture> t0 = texture(D, a), t1 = texture(D, b);
MTLResourceID ids[2] = {t0.gpuResourceID, t1.gpuResourceID};
id<MTLBuffer> held = buffer(D, ids, sizeof ids);
uint64_t address = held.gpuAddress;
id<MTLBuffer> anc = buffer(D, &address, sizeof address);
run(D, Q, lib, @"pooled_context", anc, @[ t0, t1, held ]);
}
return 0;
}
@@ -265,6 +265,7 @@ fixture ob3_vertex_input: metal: Apple metal version 32023.921 (metalfe-32023.92
fixture pdc_compare_clamp: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture pdc_post_depth_coverage: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture pfx_fetch: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture pooled_cells: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture primitive_ids: metal: Apple metal version 32023.921 (metalfe-32023.921.6); macosx-sdk: 27.0 (26A425)
fixture private_reach_functions: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
fixture private_reach_tiles: metal: Apple metal version 32023.922 (metalfe-32023.922.4); macosx-sdk: 27.2 (26B118)
@@ -0,0 +1,580 @@
; Generated by scripts/regen-air-fixtures from pooled_cells.metal (typed pointers; xcrun metal-as reassembles it). Do not edit.
; ModuleID = '-'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
%struct.Shader = type { [6 x %struct.Closure], i32, i32 }
%struct.Closure = type { float, i32, float*, [2 x float] }
%struct.Holder = type { float*, i32 }
%struct.Context = type { %struct.Anc addrspace(2)*, i32 }
%struct.Anc = type { %struct.Held addrspace(1)* }
%struct.Held = type { %"struct.metal::texture2d" }
%"struct.metal::texture2d" = type { %struct._texture_2d_t addrspace(1)* }
%struct._texture_2d_t = type opaque
%struct._sampler_t = type opaque
; Function Attrs: convergent mustprogress nofree nosync nounwind
define void @pooled_closures(float addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0, float addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %1, i32 noundef %2) local_unnamed_addr #0 {
%4 = alloca %struct.Shader, align 8
%5 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 1
store i32 0, i32* %5, align 8, !tbaa !35
%6 = and i32 %2, 3
%7 = add nuw nsw i32 %6, 2
%8 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 2
store i32 %7, i32* %8, align 4, !tbaa !40
%9 = zext i32 %2 to i64
%10 = getelementptr inbounds float, float addrspace(1)* %1, i64 %9
%11 = load float, float addrspace(1)* %10, align 4, !tbaa !41, !alias.scope !43, !noalias !46
br label %12
12: ; preds = %49, %3
%13 = phi i32 [ 0, %3 ], [ %29, %49 ]
%14 = phi i32 [ 0, %3 ], [ %16, %49 ]
%15 = phi i32 [ %7, %3 ], [ %50, %49 ]
%16 = add nuw nsw i32 %14, 1
%17 = icmp eq i32 %15, 0
br i1 %17, label %28, label %18
18: ; preds = %12
%19 = call fast float @air.convert.f.f32.s.i32(i32 %14) #8
%20 = fadd fast float %19, %11
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 0, i64 %21
%23 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 0, i64 %21, i32 1
store i32 %16, i32* %23, align 4, !tbaa !48
%24 = getelementptr inbounds %struct.Closure, %struct.Closure* %22, i64 0, i32 0
store float %20, float* %24, align 8, !tbaa !51
%25 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 0, i64 %21, i32 2
store float* null, float** %25, align 8, !tbaa !52
%26 = add nsw i32 %13, 1
store i32 %26, i32* %5, align 8, !tbaa !35
%27 = add nsw i32 %15, -1
store i32 %27, i32* %8, align 4, !tbaa !40
br label %28
28: ; preds = %18, %12
%29 = phi i32 [ %26, %18 ], [ %13, %12 ]
%30 = phi i32 [ %27, %18 ], [ 0, %12 ]
%31 = phi %struct.Closure* [ %22, %18 ], [ null, %12 ]
%32 = icmp eq %struct.Closure* %31, null
br i1 %32, label %49, label %33
33: ; preds = %28
%34 = icmp eq i32 %30, 0
br i1 %34, label %40, label %35
35: ; preds = %33
%36 = add nsw i32 %30, -1
%37 = add nsw i32 %36, %29
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.Shader, %struct.Shader* %4, i64 0, i32 0, i64 %38, i32 3, i64 0
store i32 %36, i32* %8, align 4, !tbaa !40
br label %40
40: ; preds = %35, %33
%41 = phi i32 [ %36, %35 ], [ 0, %33 ]
%42 = phi float* [ %39, %35 ], [ null, %33 ]
%43 = icmp eq float* %42, null
br i1 %43, label %49, label %44
44: ; preds = %40
%45 = mul nuw nsw i32 %16, 10
%46 = getelementptr inbounds float, float* %42, i64 1
%47 = getelementptr inbounds %struct.Closure, %struct.Closure* %31, i64 0, i32 2
%48 = call fast float @air.convert.f.f32.s.i32(i32 %45) #8
store float %48, float* %42, align 4, !tbaa !41
store float %11, float* %46, align 4, !tbaa !41
store float* %42, float** %47, align 8, !tbaa !52
br label %49
49: ; preds = %44, %40, %28
%50 = phi i32 [ %41, %40 ], [ %41, %44 ], [ %30, %28 ]
%51 = icmp eq i32 %16, 3
br i1 %51, label %52, label %12, !llvm.loop !53
52: ; preds = %49
%53 = shl i32 %2, 3
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds float, float addrspace(1)* %0, i64 %54
call fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(%struct.Shader* noundef nonnull align 8 dereferenceable(152) %4, float addrspace(1)* noundef %55, i32 noundef 0) #9
ret void
}
; Function Attrs: mustprogress nofree nosync nounwind
define internal fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(%struct.Shader* nocapture noundef nonnull readonly align 8 dereferenceable(152) %0, float addrspace(1)* nocapture noundef writeonly %1, i32 noundef %2) unnamed_addr #1 {
%4 = getelementptr inbounds %struct.Shader, %struct.Shader* %0, i64 0, i32 1
%5 = load i32, i32* %4, align 8, !tbaa !35
br label %18
6: ; preds = %53
%7 = tail call fast float @air.convert.f.f32.s.i32(i32 %5) #8
%8 = getelementptr inbounds float, float addrspace(1)* %1, i64 3
store float %7, float addrspace(1)* %8, align 4, !tbaa !41
%9 = getelementptr inbounds %struct.Shader, %struct.Shader* %0, i64 0, i32 2
%10 = load i32, i32* %9, align 4, !tbaa !40
%11 = tail call fast float @air.convert.f.f32.s.i32(i32 %10) #8
%12 = getelementptr inbounds float, float addrspace(1)* %1, i64 4
store float %11, float addrspace(1)* %12, align 4, !tbaa !41
%13 = tail call fast float @air.convert.f.f32.s.i32(i32 %48) #8
%14 = getelementptr inbounds float, float addrspace(1)* %1, i64 5
store float %13, float addrspace(1)* %14, align 4, !tbaa !41
%15 = getelementptr inbounds float, float addrspace(1)* %1, i64 6
store float %46, float addrspace(1)* %15, align 4, !tbaa !41
%16 = tail call fast float @air.convert.f.f32.s.i32(i32 %45) #8
%17 = getelementptr inbounds float, float addrspace(1)* %1, i64 7
store float %16, float addrspace(1)* %17, align 4, !tbaa !41
ret void
18: ; preds = %3, %53
%19 = phi i32 [ 0, %3 ], [ %48, %53 ]
%20 = phi i32 [ 0, %3 ], [ %54, %53 ]
%21 = phi float [ 0.000000e+00, %3 ], [ %46, %53 ]
%22 = phi i32 [ 0, %3 ], [ %45, %53 ]
%23 = icmp slt i32 %20, %5
br i1 %23, label %24, label %44
24: ; preds = %18
%25 = zext i32 %20 to i64
%26 = getelementptr inbounds %struct.Shader, %struct.Shader* %0, i64 0, i32 0, i64 %25, i32 2
%27 = load float*, float** %26, align 8, !tbaa !52
%28 = getelementptr inbounds %struct.Shader, %struct.Shader* %0, i64 0, i32 0, i64 %25, i32 0
%29 = load float, float* %28, align 8, !tbaa !51
%30 = icmp ne float* %27, null
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = load float, float* %27, align 4, !tbaa !41
br label %33
33: ; preds = %24, %31
%34 = phi fast float [ %32, %31 ], [ 0.000000e+00, %24 ]
%35 = fadd fast float %34, %29
%36 = getelementptr inbounds %struct.Shader, %struct.Shader* %0, i64 0, i32 0, i64 %25, i32 1
%37 = load i32, i32* %36, align 4, !tbaa !48
%38 = add nsw i32 %37, %19
br i1 %30, label %39, label %44
39: ; preds = %33
%40 = add nsw i32 %22, 1
%41 = getelementptr inbounds float, float* %27, i64 1
%42 = load float, float* %41, align 4, !tbaa !41
%43 = fadd fast float %42, %21
br label %44
44: ; preds = %33, %39, %18
%45 = phi i32 [ %22, %18 ], [ %40, %39 ], [ %22, %33 ]
%46 = phi float [ %21, %18 ], [ %43, %39 ], [ %21, %33 ]
%47 = phi float [ -1.000000e+00, %18 ], [ %35, %39 ], [ %35, %33 ]
%48 = phi i32 [ %19, %18 ], [ %38, %39 ], [ %38, %33 ]
%49 = icmp slt i32 %20, %2
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = zext i32 %20 to i64
%52 = getelementptr inbounds float, float addrspace(1)* %1, i64 %51
store float %47, float addrspace(1)* %52, align 4, !tbaa !41
br label %53
53: ; preds = %50, %44
%54 = add nuw nsw i32 %20, 1
%55 = icmp eq i32 %54, 3
br i1 %55, label %6, label %18, !llvm.loop !55
}
; Function Attrs: mustprogress nofree nosync nounwind
define void @pooled_bytes(float addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0, float addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %1, i32 addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #1 {
%5 = alloca %struct.Shader, align 8
%6 = alloca [4 x float], align 4
%7 = alloca %struct.Holder, align 8
%8 = zext i32 %3 to i64
%9 = getelementptr inbounds float, float addrspace(1)* %1, i64 %8
%10 = load float, float addrspace(1)* %9, align 4, !tbaa !41, !alias.scope !56, !noalias !59
br label %29
11: ; preds = %29
%12 = and i32 %3, 3
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [4 x float], [4 x float]* %6, i64 0, i64 %13
%15 = getelementptr inbounds %struct.Holder, %struct.Holder* %7, i64 0, i32 0
store float* %14, float** %15, align 8, !tbaa !62
%16 = load i32, i32 addrspace(1)* %2, align 4, !tbaa !64, !alias.scope !65, !noalias !66
%17 = getelementptr inbounds %struct.Holder, %struct.Holder* %7, i64 0, i32 1
store i32 %16, i32* %17, align 8, !tbaa !67
%18 = and i32 %3, 1
%19 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 1
%20 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 2
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %21, i32 1
store i32 1, i32* %22, align 4, !tbaa !48
%23 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %21, i32 0
store float 1.000000e+00, float* %23, align 8, !tbaa !51
%24 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %21, i32 2
store float* null, float** %24, align 8, !tbaa !52
%25 = add nuw nsw i32 %18, 1
store i32 %25, i32* %19, align 8, !tbaa !35
store i32 1, i32* %20, align 4, !tbaa !40
%26 = bitcast float** %24 to i8*
%27 = bitcast %struct.Holder* %7 to i8*
%28 = icmp sgt i32 %16, 0
br i1 %28, label %57, label %39
29: ; preds = %4, %29
%30 = phi i32 [ 0, %4 ], [ %35, %29 ]
%31 = tail call fast float @air.convert.f.f32.s.i32(i32 %30) #8
%32 = fadd fast float %31, %10
%33 = zext i32 %30 to i64
%34 = getelementptr inbounds [4 x float], [4 x float]* %6, i64 0, i64 %33
store float %32, float* %34, align 4, !tbaa !41
%35 = add nuw nsw i32 %30, 1
%36 = icmp eq i32 %35, 4
br i1 %36, label %11, label %29, !llvm.loop !68
37: ; preds = %57
%38 = load i32, i32* %19, align 8, !tbaa !35
br label %39
39: ; preds = %37, %11
%40 = phi i32 [ %38, %37 ], [ %25, %11 ]
%41 = shl i32 %3, 3
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds float, float addrspace(1)* %0, i64 %42
%44 = add nsw i32 %40, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %45, i32 2
%47 = load float*, float** %46, align 8, !tbaa !52
%48 = load float, float* %47, align 4, !tbaa !41
store float %48, float addrspace(1)* %43, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%49 = fadd fast float %48, 1.000000e+02
store float %49, float* %47, align 4, !tbaa !41
%50 = load float, float* %14, align 4, !tbaa !41
%51 = getelementptr inbounds float, float addrspace(1)* %43, i64 1
store float %50, float addrspace(1)* %51, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%52 = icmp eq float* %47, %14
%53 = select fast i1 %52, float 1.000000e+00, float 0.000000e+00
%54 = getelementptr inbounds float, float addrspace(1)* %43, i64 2
store float %53, float addrspace(1)* %54, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%55 = call fast float @air.convert.f.f32.s.i32(i32 %40) #8
%56 = getelementptr inbounds float, float addrspace(1)* %43, i64 3
store float %55, float addrspace(1)* %56, align 4, !tbaa !41, !alias.scope !69, !noalias !70
ret void
57: ; preds = %11, %57
%58 = phi i32 [ %63, %57 ], [ 0, %11 ]
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %27, i64 %59
%61 = load i8, i8* %60, align 1, !tbaa !71
%62 = getelementptr inbounds i8, i8* %26, i64 %59
store i8 %61, i8* %62, align 1, !tbaa !71
%63 = add nuw nsw i32 %58, 1
%64 = icmp eq i32 %63, %16
br i1 %64, label %37, label %57, !llvm.loop !72
}
; Function Attrs: mustprogress nofree nosync nounwind readnone willreturn
declare float @air.convert.f.f32.s.i32(i32) local_unnamed_addr #2
; Function Attrs: mustprogress nofree nounwind
define void @pooled_spill(float addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0, float addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %1, i64 addrspace(1)* noundef "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #3 {
%5 = alloca %struct.Shader, align 8
%6 = alloca [4 x float], align 4
%7 = zext i32 %3 to i64
%8 = getelementptr inbounds float, float addrspace(1)* %1, i64 %7
%9 = load float, float addrspace(1)* %8, align 4, !tbaa !41, !alias.scope !73, !noalias !76
br label %37
10: ; preds = %37
%11 = and i32 %3, 3
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [4 x float], [4 x float]* %6, i64 0, i64 %12
%14 = ptrtoint float* %13 to i64
%15 = getelementptr inbounds i64, i64 addrspace(1)* %2, i64 %7
store volatile i64 %14, i64 addrspace(1)* %15, align 8, !tbaa !79, !alias.scope !81, !noalias !82
%16 = and i32 %3, 1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %17, i32 2
%19 = add nuw nsw i32 %16, 1
%20 = load volatile i64, i64 addrspace(1)* %15, align 8, !tbaa !79, !alias.scope !81, !noalias !82
%21 = bitcast float** %18 to i64*
store i64 %20, i64* %21, align 8
%22 = shl i32 %3, 3
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds float, float addrspace(1)* %0, i64 %23
%25 = zext i32 %16 to i64
%26 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 0, i64 %25, i32 2
%27 = load float*, float** %26, align 8, !tbaa !52
%28 = load float, float* %27, align 4, !tbaa !41
store float %28, float addrspace(1)* %24, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%29 = fadd fast float %28, 1.000000e+02
store float %29, float* %27, align 4, !tbaa !41
%30 = load float, float* %13, align 4, !tbaa !41
%31 = getelementptr inbounds float, float addrspace(1)* %24, i64 1
store float %30, float addrspace(1)* %31, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%32 = icmp eq float* %27, %13
%33 = select fast i1 %32, float 1.000000e+00, float 0.000000e+00
%34 = getelementptr inbounds float, float addrspace(1)* %24, i64 2
store float %33, float addrspace(1)* %34, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%35 = call fast float @air.convert.f.f32.s.i32(i32 %19) #8
%36 = getelementptr inbounds float, float addrspace(1)* %24, i64 3
store float %35, float addrspace(1)* %36, align 4, !tbaa !41, !alias.scope !83, !noalias !84
ret void
37: ; preds = %4, %37
%38 = phi i32 [ 0, %4 ], [ %43, %37 ]
%39 = tail call fast float @air.convert.f.f32.s.i32(i32 %38) #8
%40 = fadd fast float %39, %9
%41 = zext i32 %38 to i64
%42 = getelementptr inbounds [4 x float], [4 x float]* %6, i64 0, i64 %41
store float %40, float* %42, align 4, !tbaa !41
%43 = add nuw nsw i32 %38, 1
%44 = icmp eq i32 %43, 4
br i1 %44, label %10, label %37, !llvm.loop !85
}
; Function Attrs: mustprogress nofree noinline nounwind
define float @_Z5shadeR7ContextR6Shaderfj(%struct.Context* nocapture noundef nonnull readonly align 8 dereferenceable(16) %0, %struct.Shader* noundef nonnull align 8 dereferenceable(152) %1, float noundef %2, i32 noundef %3) local_unnamed_addr #4 {
%5 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 2
%6 = load i32, i32* %5, align 4, !tbaa !40
%7 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 1
%8 = load i32, i32* %7, align 8, !tbaa !35
br label %9
9: ; preds = %46, %4
%10 = phi i32 [ %8, %4 ], [ %26, %46 ]
%11 = phi i32 [ 0, %4 ], [ %13, %46 ]
%12 = phi i32 [ %6, %4 ], [ %47, %46 ]
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %12, 0
br i1 %14, label %25, label %15
15: ; preds = %9
%16 = tail call fast float @air.convert.f.f32.s.i32(i32 %11) #8
%17 = fadd fast float %16, %2
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 0, i64 %18
%20 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 0, i64 %18, i32 1
store i32 %13, i32* %20, align 4, !tbaa !48
%21 = getelementptr inbounds %struct.Closure, %struct.Closure* %19, i64 0, i32 0
store float %17, float* %21, align 8, !tbaa !51
%22 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 0, i64 %18, i32 2
store float* null, float** %22, align 8, !tbaa !52
%23 = add nsw i32 %10, 1
store i32 %23, i32* %7, align 8, !tbaa !35
%24 = add nsw i32 %12, -1
store i32 %24, i32* %5, align 4, !tbaa !40
br label %25
25: ; preds = %15, %9
%26 = phi i32 [ %23, %15 ], [ %10, %9 ]
%27 = phi i32 [ %24, %15 ], [ 0, %9 ]
%28 = phi %struct.Closure* [ %19, %15 ], [ null, %9 ]
%29 = icmp eq %struct.Closure* %28, null
br i1 %29, label %46, label %30
30: ; preds = %25
%31 = icmp eq i32 %27, 0
br i1 %31, label %37, label %32
32: ; preds = %30
%33 = add nsw i32 %27, -1
%34 = add nsw i32 %33, %26
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.Shader, %struct.Shader* %1, i64 0, i32 0, i64 %35, i32 3, i64 0
store i32 %33, i32* %5, align 4, !tbaa !40
br label %37
37: ; preds = %32, %30
%38 = phi i32 [ %33, %32 ], [ 0, %30 ]
%39 = phi float* [ %36, %32 ], [ null, %30 ]
%40 = icmp eq float* %39, null
br i1 %40, label %46, label %41
41: ; preds = %37
%42 = mul nuw nsw i32 %13, 10
%43 = getelementptr inbounds float, float* %39, i64 1
%44 = getelementptr inbounds %struct.Closure, %struct.Closure* %28, i64 0, i32 2
%45 = tail call fast float @air.convert.f.f32.s.i32(i32 %42) #8
store float %45, float* %39, align 4, !tbaa !41
store float %2, float* %43, align 4, !tbaa !41
store float* %39, float** %44, align 8, !tbaa !52
br label %46
46: ; preds = %41, %37, %25
%47 = phi i32 [ %38, %37 ], [ %38, %41 ], [ %27, %25 ]
%48 = icmp eq i32 %13, 3
br i1 %48, label %49, label %9, !llvm.loop !53
49: ; preds = %46
%50 = getelementptr inbounds %struct.Context, %struct.Context* %0, i64 0, i32 0
%51 = load %struct.Anc addrspace(2)*, %struct.Anc addrspace(2)** %50, align 8, !tbaa !86
%52 = getelementptr inbounds %struct.Anc, %struct.Anc addrspace(2)* %51, i64 0, i32 0
%53 = load %struct.Held addrspace(1)*, %struct.Held addrspace(1)* addrspace(2)* %52, align 8, !tbaa !88
%54 = getelementptr inbounds %struct.Context, %struct.Context* %0, i64 0, i32 1
%55 = load i32, i32* %54, align 8, !tbaa !90
%56 = sext i32 %55 to i64
%57 = and i32 %3, 3
%58 = insertelement <2 x i32> <i32 poison, i32 0>, i32 %57, i64 0
%59 = getelementptr inbounds %struct.Held, %struct.Held addrspace(1)* %53, i64 %56, i32 0, i32 0
%60 = load %struct._texture_2d_t addrspace(1)*, %struct._texture_2d_t addrspace(1)* addrspace(1)* %59, align 8, !tbaa !91
%61 = tail call %struct._sampler_t addrspace(2)* @air.get_read_sampler() #10
%62 = tail call { <4 x float>, i8 } @air.read_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly %60, %struct._sampler_t addrspace(2)* %61, <2 x i32> %58, <2 x i32> zeroinitializer, i32 0, i32 0) #11
%63 = extractvalue { <4 x float>, i8 } %62, 0
%64 = extractelement <4 x float> %63, i64 0
ret float %64
}
; Function Attrs: convergent mustprogress nofree nounwind
define void @pooled_context(float addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0, float addrspace(1)* nocapture noundef readonly "air-buffer-no-alias" %1, %struct.Anc addrspace(2)* noundef "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #5 {
%5 = alloca %struct.Shader, align 8
%6 = alloca %struct.Context, align 8
%7 = getelementptr inbounds %struct.Context, %struct.Context* %6, i64 0, i32 0
store %struct.Anc addrspace(2)* %2, %struct.Anc addrspace(2)** %7, align 8, !tbaa !86
%8 = and i32 %3, 1
%9 = getelementptr inbounds %struct.Context, %struct.Context* %6, i64 0, i32 1
store i32 %8, i32* %9, align 8, !tbaa !90
%10 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 1
store i32 0, i32* %10, align 8, !tbaa !35
%11 = and i32 %3, 3
%12 = add nuw nsw i32 %11, 2
%13 = getelementptr inbounds %struct.Shader, %struct.Shader* %5, i64 0, i32 2
store i32 %12, i32* %13, align 4, !tbaa !40
%14 = shl i32 %3, 3
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds float, float addrspace(1)* %0, i64 %15
%17 = zext i32 %3 to i64
%18 = getelementptr inbounds float, float addrspace(1)* %1, i64 %17
%19 = load float, float addrspace(1)* %18, align 4, !tbaa !41, !alias.scope !94, !noalias !97
%20 = call fast float @_Z5shadeR7ContextR6Shaderfj(%struct.Context* noundef nonnull align 8 dereferenceable(16) %6, %struct.Shader* noundef nonnull align 8 dereferenceable(152) %5, float noundef %19, i32 noundef %3) #9
store float %20, float addrspace(1)* %16, align 4, !tbaa !41, !alias.scope !100, !noalias !101
call fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(%struct.Shader* noundef nonnull align 8 dereferenceable(152) %5, float addrspace(1)* noundef %16, i32 noundef 1) #9
ret void
}
; Function Attrs: inaccessiblememonly mustprogress nofree nounwind readonly willreturn
declare %struct._sampler_t addrspace(2)* @air.get_read_sampler() local_unnamed_addr #6
; Function Attrs: argmemonly mustprogress nofree nounwind readonly willreturn
declare { <4 x float>, i8 } @air.read_texture_2d.v4f32(%struct._texture_2d_t addrspace(1)* nocapture readonly, %struct._sampler_t addrspace(2)*, <2 x i32>, <2 x i32>, i32, i32) local_unnamed_addr #7
attributes #0 = { convergent mustprogress nofree nosync nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { mustprogress nofree nosync nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #2 = { mustprogress nofree nosync nounwind readnone willreturn }
attributes #3 = { mustprogress nofree nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #4 = { mustprogress nofree noinline nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #5 = { convergent mustprogress nofree nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #6 = { inaccessiblememonly mustprogress nofree nounwind readonly willreturn }
attributes #7 = { argmemonly mustprogress nofree nounwind readonly willreturn }
attributes #8 = { nounwind readnone willreturn }
attributes #9 = { nobuiltin "no-builtins" }
attributes #10 = { inaccessiblememonly nounwind readonly willreturn }
attributes #11 = { argmemonly nounwind readonly willreturn }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9, !15, !19, !22}
!air.compile_options = !{!29, !30, !31}
!llvm.ident = !{!32}
!air.version = !{!33}
!air.language_version = !{!34}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{void (float addrspace(1)*, float addrspace(1)*, i32)* @pooled_closures, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14}
!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"out"}
!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"in"}
!14 = !{i32 2, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!15 = !{void (float addrspace(1)*, float addrspace(1)*, i32 addrspace(1)*, i32)* @pooled_bytes, !10, !16}
!16 = !{!12, !13, !17, !18}
!17 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"int", !"air.arg_name", !"len"}
!18 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!19 = !{void (float addrspace(1)*, float addrspace(1)*, i64 addrspace(1)*, i32)* @pooled_spill, !10, !20}
!20 = !{!12, !13, !21, !18}
!21 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"ulong", !"air.arg_name", !"spill"}
!22 = !{void (float addrspace(1)*, float addrspace(1)*, %struct.Anc addrspace(2)*, i32)* @pooled_context, !10, !23}
!23 = !{!12, !13, !24, !18}
!24 = !{i32 2, !"air.indirect_buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !25, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Anc", !"air.arg_name", !"anc"}
!25 = !{i32 0, i32 8, i32 0, !"Held", !"held", !"air.indirect_argument", !26}
!26 = !{i32 0, !"air.indirect_buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !27, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Held", !"air.arg_name", !"held"}
!27 = !{i32 0, i32 8, i32 0, !"texture2d<float, sample>", !"t", !"air.indirect_argument", !28}
!28 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<float, sample>", !"air.arg_name", !"t"}
!29 = !{!"air.compile.denorms_disable"}
!30 = !{!"air.compile.fast_math_enable"}
!31 = !{!"air.compile.framebuffer_fetch_enable"}
!32 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!33 = !{i32 2, i32 8, i32 0}
!34 = !{!"Metal", i32 4, i32 0, i32 0}
!35 = !{!36, !39, i64 144}
!36 = !{!"_ZTS6Shader", !37, i64 0, !39, i64 144, !39, i64 148}
!37 = !{!"omnipotent char", !38, i64 0}
!38 = !{!"Simple C++ TBAA"}
!39 = !{!"int", !37, i64 0}
!40 = !{!36, !39, i64 148}
!41 = !{!42, !42, i64 0}
!42 = !{!"float", !37, i64 0}
!43 = !{!44}
!44 = distinct !{!44, !45, !"air-alias-scope-arg(1)"}
!45 = distinct !{!45, !"air-alias-scopes(pooled_closures)"}
!46 = !{!47}
!47 = distinct !{!47, !45, !"air-alias-scope-arg(0)"}
!48 = !{!49, !39, i64 4}
!49 = !{!"_ZTS7Closure", !42, i64 0, !39, i64 4, !50, i64 8, !37, i64 16}
!50 = !{!"any pointer", !37, i64 0}
!51 = !{!49, !42, i64 0}
!52 = !{!49, !50, i64 8}
!53 = distinct !{!53, !54}
!54 = !{!"llvm.loop.mustprogress"}
!55 = distinct !{!55, !54}
!56 = !{!57}
!57 = distinct !{!57, !58, !"air-alias-scope-arg(1)"}
!58 = distinct !{!58, !"air-alias-scopes(pooled_bytes)"}
!59 = !{!60, !61}
!60 = distinct !{!60, !58, !"air-alias-scope-arg(0)"}
!61 = distinct !{!61, !58, !"air-alias-scope-arg(2)"}
!62 = !{!63, !50, i64 0}
!63 = !{!"_ZTS6Holder", !50, i64 0, !39, i64 8}
!64 = !{!39, !39, i64 0}
!65 = !{!61}
!66 = !{!60, !57}
!67 = !{!63, !39, i64 8}
!68 = distinct !{!68, !54}
!69 = !{!60}
!70 = !{!57, !61}
!71 = !{!37, !37, i64 0}
!72 = distinct !{!72, !54}
!73 = !{!74}
!74 = distinct !{!74, !75, !"air-alias-scope-arg(1)"}
!75 = distinct !{!75, !"air-alias-scopes(pooled_spill)"}
!76 = !{!77, !78}
!77 = distinct !{!77, !75, !"air-alias-scope-arg(0)"}
!78 = distinct !{!78, !75, !"air-alias-scope-arg(2)"}
!79 = !{!80, !80, i64 0}
!80 = !{!"long", !37, i64 0}
!81 = !{!78}
!82 = !{!77, !74}
!83 = !{!77}
!84 = !{!74, !78}
!85 = distinct !{!85, !54}
!86 = !{!87, !50, i64 0}
!87 = !{!"_ZTS7Context", !50, i64 0, !39, i64 8}
!88 = !{!89, !50, i64 0}
!89 = !{!"_ZTS3Anc", !50, i64 0}
!90 = !{!87, !39, i64 8}
!91 = !{!92, !93, i64 0}
!92 = !{!"_ZTSN5metal9texture2dIfLNS_6accessE0ELNS_16memory_coherenceE0EvEE", !93, i64 0}
!93 = !{!"__metal_texture_2d_t", !37, i64 0}
!94 = !{!95}
!95 = distinct !{!95, !96, !"air-alias-scope-arg(1)"}
!96 = distinct !{!96, !"air-alias-scopes(pooled_context)"}
!97 = !{!98, !99}
!98 = distinct !{!98, !96, !"air-alias-scope-arg(0)"}
!99 = distinct !{!99, !96, !"air-alias-scope-arg(2)"}
!100 = !{!98}
!101 = !{!95, !99}
@@ -0,0 +1,578 @@
; Generated by scripts/regen-air-fixtures from pooled_cells.metal (llvm-dis of pooled_cells.air). Do not edit.
; ModuleID = '<stdin>'
source_filename = "-"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
target triple = "air64_v28-apple-macosx26.0.0"
%struct.Shader = type { [6 x %struct.Closure], i32, i32 }
%struct.Closure = type { float, i32, ptr, [2 x float] }
%struct.Holder = type { ptr, i32 }
%struct.Context = type { ptr addrspace(2), i32 }
%struct.Anc = type { ptr addrspace(1) }
%struct.Held = type { %"struct.metal::texture2d" }
%"struct.metal::texture2d" = type { ptr addrspace(1) }
; Function Attrs: convergent mustprogress nofree nosync nounwind
define void @pooled_closures(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %1, i32 noundef %2) local_unnamed_addr #0 {
%4 = alloca %struct.Shader, align 8
%5 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 1
store i32 0, ptr %5, align 8, !tbaa !35
%6 = and i32 %2, 3
%7 = add nuw nsw i32 %6, 2
%8 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 2
store i32 %7, ptr %8, align 4, !tbaa !40
%9 = zext i32 %2 to i64
%10 = getelementptr inbounds float, ptr addrspace(1) %1, i64 %9
%11 = load float, ptr addrspace(1) %10, align 4, !tbaa !41, !alias.scope !43, !noalias !46
br label %12
12: ; preds = %49, %3
%13 = phi i32 [ 0, %3 ], [ %29, %49 ]
%14 = phi i32 [ 0, %3 ], [ %16, %49 ]
%15 = phi i32 [ %7, %3 ], [ %50, %49 ]
%16 = add nuw nsw i32 %14, 1
%17 = icmp eq i32 %15, 0
br i1 %17, label %28, label %18
18: ; preds = %12
%19 = call fast float @air.convert.f.f32.s.i32(i32 %14) #8
%20 = fadd fast float %19, %11
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 0, i64 %21
%23 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 0, i64 %21, i32 1
store i32 %16, ptr %23, align 4, !tbaa !48
%24 = getelementptr inbounds %struct.Closure, ptr %22, i64 0, i32 0
store float %20, ptr %24, align 8, !tbaa !51
%25 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 0, i64 %21, i32 2
store ptr null, ptr %25, align 8, !tbaa !52
%26 = add nsw i32 %13, 1
store i32 %26, ptr %5, align 8, !tbaa !35
%27 = add nsw i32 %15, -1
store i32 %27, ptr %8, align 4, !tbaa !40
br label %28
28: ; preds = %18, %12
%29 = phi i32 [ %26, %18 ], [ %13, %12 ]
%30 = phi i32 [ %27, %18 ], [ 0, %12 ]
%31 = phi ptr [ %22, %18 ], [ null, %12 ]
%32 = icmp eq ptr %31, null
br i1 %32, label %49, label %33
33: ; preds = %28
%34 = icmp eq i32 %30, 0
br i1 %34, label %40, label %35
35: ; preds = %33
%36 = add nsw i32 %30, -1
%37 = add nsw i32 %36, %29
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds %struct.Shader, ptr %4, i64 0, i32 0, i64 %38, i32 3, i64 0
store i32 %36, ptr %8, align 4, !tbaa !40
br label %40
40: ; preds = %35, %33
%41 = phi i32 [ %36, %35 ], [ 0, %33 ]
%42 = phi ptr [ %39, %35 ], [ null, %33 ]
%43 = icmp eq ptr %42, null
br i1 %43, label %49, label %44
44: ; preds = %40
%45 = mul nuw nsw i32 %16, 10
%46 = getelementptr inbounds float, ptr %42, i64 1
%47 = getelementptr inbounds %struct.Closure, ptr %31, i64 0, i32 2
%48 = call fast float @air.convert.f.f32.s.i32(i32 %45) #8
store float %48, ptr %42, align 4, !tbaa !41
store float %11, ptr %46, align 4, !tbaa !41
store ptr %42, ptr %47, align 8, !tbaa !52
br label %49
49: ; preds = %44, %40, %28
%50 = phi i32 [ %41, %40 ], [ %41, %44 ], [ %30, %28 ]
%51 = icmp eq i32 %16, 3
br i1 %51, label %52, label %12, !llvm.loop !53
52: ; preds = %49
%53 = shl i32 %2, 3
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds float, ptr addrspace(1) %0, i64 %54
call fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(ptr noundef nonnull align 8 dereferenceable(152) %4, ptr addrspace(1) noundef %55, i32 noundef 0) #9
ret void
}
; Function Attrs: mustprogress nofree nosync nounwind
define internal fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(ptr noundef nonnull readonly align 8 captures(none) dereferenceable(152) %0, ptr addrspace(1) noundef writeonly captures(none) %1, i32 noundef %2) unnamed_addr #1 {
%4 = getelementptr inbounds %struct.Shader, ptr %0, i64 0, i32 1
%5 = load i32, ptr %4, align 8, !tbaa !35
br label %18
6: ; preds = %53
%7 = tail call fast float @air.convert.f.f32.s.i32(i32 %5) #8
%8 = getelementptr inbounds float, ptr addrspace(1) %1, i64 3
store float %7, ptr addrspace(1) %8, align 4, !tbaa !41
%9 = getelementptr inbounds %struct.Shader, ptr %0, i64 0, i32 2
%10 = load i32, ptr %9, align 4, !tbaa !40
%11 = tail call fast float @air.convert.f.f32.s.i32(i32 %10) #8
%12 = getelementptr inbounds float, ptr addrspace(1) %1, i64 4
store float %11, ptr addrspace(1) %12, align 4, !tbaa !41
%13 = tail call fast float @air.convert.f.f32.s.i32(i32 %48) #8
%14 = getelementptr inbounds float, ptr addrspace(1) %1, i64 5
store float %13, ptr addrspace(1) %14, align 4, !tbaa !41
%15 = getelementptr inbounds float, ptr addrspace(1) %1, i64 6
store float %46, ptr addrspace(1) %15, align 4, !tbaa !41
%16 = tail call fast float @air.convert.f.f32.s.i32(i32 %45) #8
%17 = getelementptr inbounds float, ptr addrspace(1) %1, i64 7
store float %16, ptr addrspace(1) %17, align 4, !tbaa !41
ret void
18: ; preds = %53, %3
%19 = phi i32 [ 0, %3 ], [ %48, %53 ]
%20 = phi i32 [ 0, %3 ], [ %54, %53 ]
%21 = phi float [ 0.000000e+00, %3 ], [ %46, %53 ]
%22 = phi i32 [ 0, %3 ], [ %45, %53 ]
%23 = icmp slt i32 %20, %5
br i1 %23, label %24, label %44
24: ; preds = %18
%25 = zext i32 %20 to i64
%26 = getelementptr inbounds %struct.Shader, ptr %0, i64 0, i32 0, i64 %25, i32 2
%27 = load ptr, ptr %26, align 8, !tbaa !52
%28 = getelementptr inbounds %struct.Shader, ptr %0, i64 0, i32 0, i64 %25, i32 0
%29 = load float, ptr %28, align 8, !tbaa !51
%30 = icmp ne ptr %27, null
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = load float, ptr %27, align 4, !tbaa !41
br label %33
33: ; preds = %31, %24
%34 = phi fast float [ %32, %31 ], [ 0.000000e+00, %24 ]
%35 = fadd fast float %34, %29
%36 = getelementptr inbounds %struct.Shader, ptr %0, i64 0, i32 0, i64 %25, i32 1
%37 = load i32, ptr %36, align 4, !tbaa !48
%38 = add nsw i32 %37, %19
br i1 %30, label %39, label %44
39: ; preds = %33
%40 = add nsw i32 %22, 1
%41 = getelementptr inbounds float, ptr %27, i64 1
%42 = load float, ptr %41, align 4, !tbaa !41
%43 = fadd fast float %42, %21
br label %44
44: ; preds = %39, %33, %18
%45 = phi i32 [ %22, %18 ], [ %40, %39 ], [ %22, %33 ]
%46 = phi float [ %21, %18 ], [ %43, %39 ], [ %21, %33 ]
%47 = phi float [ -1.000000e+00, %18 ], [ %35, %39 ], [ %35, %33 ]
%48 = phi i32 [ %19, %18 ], [ %38, %39 ], [ %38, %33 ]
%49 = icmp slt i32 %20, %2
br i1 %49, label %53, label %50
50: ; preds = %44
%51 = zext i32 %20 to i64
%52 = getelementptr inbounds float, ptr addrspace(1) %1, i64 %51
store float %47, ptr addrspace(1) %52, align 4, !tbaa !41
br label %53
53: ; preds = %50, %44
%54 = add nuw nsw i32 %20, 1
%55 = icmp eq i32 %54, 3
br i1 %55, label %6, label %18, !llvm.loop !55
}
; Function Attrs: mustprogress nofree nosync nounwind
define void @pooled_bytes(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %1, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #1 {
%5 = alloca %struct.Shader, align 8
%6 = alloca [4 x float], align 4
%7 = alloca %struct.Holder, align 8
%8 = zext i32 %3 to i64
%9 = getelementptr inbounds float, ptr addrspace(1) %1, i64 %8
%10 = load float, ptr addrspace(1) %9, align 4, !tbaa !41, !alias.scope !56, !noalias !59
br label %29
11: ; preds = %29
%12 = and i32 %3, 3
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds [4 x float], ptr %6, i64 0, i64 %13
%15 = getelementptr inbounds %struct.Holder, ptr %7, i64 0, i32 0
store ptr %14, ptr %15, align 8, !tbaa !62
%16 = load i32, ptr addrspace(1) %2, align 4, !tbaa !64, !alias.scope !65, !noalias !66
%17 = getelementptr inbounds %struct.Holder, ptr %7, i64 0, i32 1
store i32 %16, ptr %17, align 8, !tbaa !67
%18 = and i32 %3, 1
%19 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 1
%20 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 2
%21 = zext i32 %18 to i64
%22 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %21, i32 1
store i32 1, ptr %22, align 4, !tbaa !48
%23 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %21, i32 0
store float 1.000000e+00, ptr %23, align 8, !tbaa !51
%24 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %21, i32 2
store ptr null, ptr %24, align 8, !tbaa !52
%25 = add nuw nsw i32 %18, 1
store i32 %25, ptr %19, align 8, !tbaa !35
store i32 1, ptr %20, align 4, !tbaa !40
%26 = bitcast ptr %24 to ptr
%27 = bitcast ptr %7 to ptr
%28 = icmp sgt i32 %16, 0
br i1 %28, label %57, label %39
29: ; preds = %29, %4
%30 = phi i32 [ 0, %4 ], [ %35, %29 ]
%31 = tail call fast float @air.convert.f.f32.s.i32(i32 %30) #8
%32 = fadd fast float %31, %10
%33 = zext i32 %30 to i64
%34 = getelementptr inbounds [4 x float], ptr %6, i64 0, i64 %33
store float %32, ptr %34, align 4, !tbaa !41
%35 = add nuw nsw i32 %30, 1
%36 = icmp eq i32 %35, 4
br i1 %36, label %11, label %29, !llvm.loop !68
37: ; preds = %57
%38 = load i32, ptr %19, align 8, !tbaa !35
br label %39
39: ; preds = %37, %11
%40 = phi i32 [ %38, %37 ], [ %25, %11 ]
%41 = shl i32 %3, 3
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds float, ptr addrspace(1) %0, i64 %42
%44 = add nsw i32 %40, -1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %45, i32 2
%47 = load ptr, ptr %46, align 8, !tbaa !52
%48 = load float, ptr %47, align 4, !tbaa !41
store float %48, ptr addrspace(1) %43, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%49 = fadd fast float %48, 1.000000e+02
store float %49, ptr %47, align 4, !tbaa !41
%50 = load float, ptr %14, align 4, !tbaa !41
%51 = getelementptr inbounds float, ptr addrspace(1) %43, i64 1
store float %50, ptr addrspace(1) %51, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%52 = icmp eq ptr %47, %14
%53 = select fast i1 %52, float 1.000000e+00, float 0.000000e+00
%54 = getelementptr inbounds float, ptr addrspace(1) %43, i64 2
store float %53, ptr addrspace(1) %54, align 4, !tbaa !41, !alias.scope !69, !noalias !70
%55 = call fast float @air.convert.f.f32.s.i32(i32 %40) #8
%56 = getelementptr inbounds float, ptr addrspace(1) %43, i64 3
store float %55, ptr addrspace(1) %56, align 4, !tbaa !41, !alias.scope !69, !noalias !70
ret void
57: ; preds = %57, %11
%58 = phi i32 [ %63, %57 ], [ 0, %11 ]
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds i8, ptr %27, i64 %59
%61 = load i8, ptr %60, align 1, !tbaa !71
%62 = getelementptr inbounds i8, ptr %26, i64 %59
store i8 %61, ptr %62, align 1, !tbaa !71
%63 = add nuw nsw i32 %58, 1
%64 = icmp eq i32 %63, %16
br i1 %64, label %37, label %57, !llvm.loop !72
}
; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare float @air.convert.f.f32.s.i32(i32) local_unnamed_addr #2
; Function Attrs: mustprogress nofree nounwind
define void @pooled_spill(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %1, ptr addrspace(1) noundef "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #3 {
%5 = alloca %struct.Shader, align 8
%6 = alloca [4 x float], align 4
%7 = zext i32 %3 to i64
%8 = getelementptr inbounds float, ptr addrspace(1) %1, i64 %7
%9 = load float, ptr addrspace(1) %8, align 4, !tbaa !41, !alias.scope !73, !noalias !76
br label %37
10: ; preds = %37
%11 = and i32 %3, 3
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [4 x float], ptr %6, i64 0, i64 %12
%14 = ptrtoint ptr %13 to i64
%15 = getelementptr inbounds i64, ptr addrspace(1) %2, i64 %7
store volatile i64 %14, ptr addrspace(1) %15, align 8, !tbaa !79, !alias.scope !81, !noalias !82
%16 = and i32 %3, 1
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %17, i32 2
%19 = add nuw nsw i32 %16, 1
%20 = load volatile i64, ptr addrspace(1) %15, align 8, !tbaa !79, !alias.scope !81, !noalias !82
%21 = bitcast ptr %18 to ptr
store i64 %20, ptr %21, align 8
%22 = shl i32 %3, 3
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds float, ptr addrspace(1) %0, i64 %23
%25 = zext i32 %16 to i64
%26 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 0, i64 %25, i32 2
%27 = load ptr, ptr %26, align 8, !tbaa !52
%28 = load float, ptr %27, align 4, !tbaa !41
store float %28, ptr addrspace(1) %24, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%29 = fadd fast float %28, 1.000000e+02
store float %29, ptr %27, align 4, !tbaa !41
%30 = load float, ptr %13, align 4, !tbaa !41
%31 = getelementptr inbounds float, ptr addrspace(1) %24, i64 1
store float %30, ptr addrspace(1) %31, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%32 = icmp eq ptr %27, %13
%33 = select fast i1 %32, float 1.000000e+00, float 0.000000e+00
%34 = getelementptr inbounds float, ptr addrspace(1) %24, i64 2
store float %33, ptr addrspace(1) %34, align 4, !tbaa !41, !alias.scope !83, !noalias !84
%35 = call fast float @air.convert.f.f32.s.i32(i32 %19) #8
%36 = getelementptr inbounds float, ptr addrspace(1) %24, i64 3
store float %35, ptr addrspace(1) %36, align 4, !tbaa !41, !alias.scope !83, !noalias !84
ret void
37: ; preds = %37, %4
%38 = phi i32 [ 0, %4 ], [ %43, %37 ]
%39 = tail call fast float @air.convert.f.f32.s.i32(i32 %38) #8
%40 = fadd fast float %39, %9
%41 = zext i32 %38 to i64
%42 = getelementptr inbounds [4 x float], ptr %6, i64 0, i64 %41
store float %40, ptr %42, align 4, !tbaa !41
%43 = add nuw nsw i32 %38, 1
%44 = icmp eq i32 %43, 4
br i1 %44, label %10, label %37, !llvm.loop !85
}
; Function Attrs: mustprogress nofree noinline nounwind
define float @_Z5shadeR7ContextR6Shaderfj(ptr noundef nonnull readonly align 8 captures(none) dereferenceable(16) %0, ptr noundef nonnull align 8 dereferenceable(152) %1, float noundef %2, i32 noundef %3) local_unnamed_addr #4 {
%5 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 2
%6 = load i32, ptr %5, align 4, !tbaa !40
%7 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 1
%8 = load i32, ptr %7, align 8, !tbaa !35
br label %9
9: ; preds = %46, %4
%10 = phi i32 [ %8, %4 ], [ %26, %46 ]
%11 = phi i32 [ 0, %4 ], [ %13, %46 ]
%12 = phi i32 [ %6, %4 ], [ %47, %46 ]
%13 = add nuw nsw i32 %11, 1
%14 = icmp eq i32 %12, 0
br i1 %14, label %25, label %15
15: ; preds = %9
%16 = tail call fast float @air.convert.f.f32.s.i32(i32 %11) #8
%17 = fadd fast float %16, %2
%18 = sext i32 %10 to i64
%19 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 0, i64 %18
%20 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 0, i64 %18, i32 1
store i32 %13, ptr %20, align 4, !tbaa !48
%21 = getelementptr inbounds %struct.Closure, ptr %19, i64 0, i32 0
store float %17, ptr %21, align 8, !tbaa !51
%22 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 0, i64 %18, i32 2
store ptr null, ptr %22, align 8, !tbaa !52
%23 = add nsw i32 %10, 1
store i32 %23, ptr %7, align 8, !tbaa !35
%24 = add nsw i32 %12, -1
store i32 %24, ptr %5, align 4, !tbaa !40
br label %25
25: ; preds = %15, %9
%26 = phi i32 [ %23, %15 ], [ %10, %9 ]
%27 = phi i32 [ %24, %15 ], [ 0, %9 ]
%28 = phi ptr [ %19, %15 ], [ null, %9 ]
%29 = icmp eq ptr %28, null
br i1 %29, label %46, label %30
30: ; preds = %25
%31 = icmp eq i32 %27, 0
br i1 %31, label %37, label %32
32: ; preds = %30
%33 = add nsw i32 %27, -1
%34 = add nsw i32 %33, %26
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds %struct.Shader, ptr %1, i64 0, i32 0, i64 %35, i32 3, i64 0
store i32 %33, ptr %5, align 4, !tbaa !40
br label %37
37: ; preds = %32, %30
%38 = phi i32 [ %33, %32 ], [ 0, %30 ]
%39 = phi ptr [ %36, %32 ], [ null, %30 ]
%40 = icmp eq ptr %39, null
br i1 %40, label %46, label %41
41: ; preds = %37
%42 = mul nuw nsw i32 %13, 10
%43 = getelementptr inbounds float, ptr %39, i64 1
%44 = getelementptr inbounds %struct.Closure, ptr %28, i64 0, i32 2
%45 = tail call fast float @air.convert.f.f32.s.i32(i32 %42) #8
store float %45, ptr %39, align 4, !tbaa !41
store float %2, ptr %43, align 4, !tbaa !41
store ptr %39, ptr %44, align 8, !tbaa !52
br label %46
46: ; preds = %41, %37, %25
%47 = phi i32 [ %38, %37 ], [ %38, %41 ], [ %27, %25 ]
%48 = icmp eq i32 %13, 3
br i1 %48, label %49, label %9, !llvm.loop !53
49: ; preds = %46
%50 = getelementptr inbounds %struct.Context, ptr %0, i64 0, i32 0
%51 = load ptr addrspace(2), ptr %50, align 8, !tbaa !86
%52 = getelementptr inbounds %struct.Anc, ptr addrspace(2) %51, i64 0, i32 0
%53 = load ptr addrspace(1), ptr addrspace(2) %52, align 8, !tbaa !88
%54 = getelementptr inbounds %struct.Context, ptr %0, i64 0, i32 1
%55 = load i32, ptr %54, align 8, !tbaa !90
%56 = sext i32 %55 to i64
%57 = and i32 %3, 3
%58 = insertelement <2 x i32> <i32 poison, i32 0>, i32 %57, i64 0
%59 = getelementptr inbounds %struct.Held, ptr addrspace(1) %53, i64 %56, i32 0, i32 0
%60 = load ptr addrspace(1), ptr addrspace(1) %59, align 8, !tbaa !91
%61 = tail call ptr addrspace(2) @air.get_read_sampler() #10
%62 = tail call { <4 x float>, i8 } @air.read_texture_2d.v4f32(ptr addrspace(1) readonly captures(none) %60, ptr addrspace(2) %61, <2 x i32> %58, <2 x i32> zeroinitializer, i32 0, i32 0) #11
%63 = extractvalue { <4 x float>, i8 } %62, 0
%64 = extractelement <4 x float> %63, i64 0
ret float %64
}
; Function Attrs: convergent mustprogress nofree nounwind
define void @pooled_context(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0, ptr addrspace(1) noundef readonly captures(none) "air-buffer-no-alias" %1, ptr addrspace(2) noundef "air-buffer-no-alias" %2, i32 noundef %3) local_unnamed_addr #5 {
%5 = alloca %struct.Shader, align 8
%6 = alloca %struct.Context, align 8
%7 = getelementptr inbounds %struct.Context, ptr %6, i64 0, i32 0
store ptr addrspace(2) %2, ptr %7, align 8, !tbaa !86
%8 = and i32 %3, 1
%9 = getelementptr inbounds %struct.Context, ptr %6, i64 0, i32 1
store i32 %8, ptr %9, align 8, !tbaa !90
%10 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 1
store i32 0, ptr %10, align 8, !tbaa !35
%11 = and i32 %3, 3
%12 = add nuw nsw i32 %11, 2
%13 = getelementptr inbounds %struct.Shader, ptr %5, i64 0, i32 2
store i32 %12, ptr %13, align 4, !tbaa !40
%14 = shl i32 %3, 3
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds float, ptr addrspace(1) %0, i64 %15
%17 = zext i32 %3 to i64
%18 = getelementptr inbounds float, ptr addrspace(1) %1, i64 %17
%19 = load float, ptr addrspace(1) %18, align 4, !tbaa !41, !alias.scope !94, !noalias !97
%20 = call fast float @_Z5shadeR7ContextR6Shaderfj(ptr noundef nonnull align 8 dereferenceable(16) %6, ptr noundef nonnull align 8 dereferenceable(152) %5, float noundef %19, i32 noundef %3) #9
store float %20, ptr addrspace(1) %16, align 4, !tbaa !41, !alias.scope !100, !noalias !101
call fastcc void @_ZL9summarizeR6ShaderPU9MTLdevicefi(ptr noundef nonnull align 8 dereferenceable(152) %5, ptr addrspace(1) noundef %16, i32 noundef 1) #9
ret void
}
; Function Attrs: mustprogress nofree nounwind willreturn memory(inaccessiblemem: read)
declare ptr addrspace(2) @air.get_read_sampler() local_unnamed_addr #6
; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare { <4 x float>, i8 } @air.read_texture_2d.v4f32(ptr addrspace(1) readonly captures(none), ptr addrspace(2), <2 x i32>, <2 x i32>, i32, i32) local_unnamed_addr #7
attributes #0 = { convergent mustprogress nofree nosync nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #1 = { mustprogress nofree nosync nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #2 = { mustprogress nofree nosync nounwind willreturn memory(none) }
attributes #3 = { mustprogress nofree nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #4 = { mustprogress nofree noinline nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="128" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #5 = { convergent mustprogress nofree nounwind "approx-func-fp-math"="true" "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="true" }
attributes #6 = { mustprogress nofree nounwind willreturn memory(inaccessiblemem: read) }
attributes #7 = { mustprogress nofree nounwind willreturn memory(argmem: read) }
attributes #8 = { nounwind willreturn memory(none) }
attributes #9 = { nobuiltin "no-builtins" }
attributes #10 = { nounwind willreturn memory(inaccessiblemem: read) }
attributes #11 = { nounwind willreturn memory(argmem: read) }
!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8}
!air.kernel = !{!9, !15, !19, !22}
!air.compile_options = !{!29, !30, !31}
!llvm.ident = !{!32}
!air.version = !{!33}
!air.language_version = !{!34}
!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]}
!1 = !{i32 1, !"wchar_size", i32 4}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 7, !"air.max_device_buffers", i32 31}
!4 = !{i32 7, !"air.max_constant_buffers", i32 31}
!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31}
!6 = !{i32 7, !"air.max_textures", i32 128}
!7 = !{i32 7, !"air.max_read_write_textures", i32 8}
!8 = !{i32 7, !"air.max_samplers", i32 16}
!9 = !{ptr @pooled_closures, !10, !11}
!10 = !{}
!11 = !{!12, !13, !14}
!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"out"}
!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"in"}
!14 = !{i32 2, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!15 = !{ptr @pooled_bytes, !10, !16}
!16 = !{!12, !13, !17, !18}
!17 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"int", !"air.arg_name", !"len"}
!18 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"t"}
!19 = !{ptr @pooled_spill, !10, !20}
!20 = !{!12, !13, !21, !18}
!21 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"ulong", !"air.arg_name", !"spill"}
!22 = !{ptr @pooled_context, !10, !23}
!23 = !{!12, !13, !24, !18}
!24 = !{i32 2, !"air.indirect_buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !25, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Anc", !"air.arg_name", !"anc"}
!25 = !{i32 0, i32 8, i32 0, !"Held", !"held", !"air.indirect_argument", !26}
!26 = !{i32 0, !"air.indirect_buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.struct_type_info", !27, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"Held", !"air.arg_name", !"held"}
!27 = !{i32 0, i32 8, i32 0, !"texture2d<float, sample>", !"t", !"air.indirect_argument", !28}
!28 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d<float, sample>", !"air.arg_name", !"t"}
!29 = !{!"air.compile.denorms_disable"}
!30 = !{!"air.compile.fast_math_enable"}
!31 = !{!"air.compile.framebuffer_fetch_enable"}
!32 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"}
!33 = !{i32 2, i32 8, i32 0}
!34 = !{!"Metal", i32 4, i32 0, i32 0}
!35 = !{!36, !39, i64 144}
!36 = !{!"_ZTS6Shader", !37, i64 0, !39, i64 144, !39, i64 148}
!37 = !{!"omnipotent char", !38, i64 0}
!38 = !{!"Simple C++ TBAA"}
!39 = !{!"int", !37, i64 0}
!40 = !{!36, !39, i64 148}
!41 = !{!42, !42, i64 0}
!42 = !{!"float", !37, i64 0}
!43 = !{!44}
!44 = distinct !{!44, !45, !"air-alias-scope-arg(1)"}
!45 = distinct !{!45, !"air-alias-scopes(pooled_closures)"}
!46 = !{!47}
!47 = distinct !{!47, !45, !"air-alias-scope-arg(0)"}
!48 = !{!49, !39, i64 4}
!49 = !{!"_ZTS7Closure", !42, i64 0, !39, i64 4, !50, i64 8, !37, i64 16}
!50 = !{!"any pointer", !37, i64 0}
!51 = !{!49, !42, i64 0}
!52 = !{!49, !50, i64 8}
!53 = distinct !{!53, !54}
!54 = !{!"llvm.loop.mustprogress"}
!55 = distinct !{!55, !54}
!56 = !{!57}
!57 = distinct !{!57, !58, !"air-alias-scope-arg(1)"}
!58 = distinct !{!58, !"air-alias-scopes(pooled_bytes)"}
!59 = !{!60, !61}
!60 = distinct !{!60, !58, !"air-alias-scope-arg(0)"}
!61 = distinct !{!61, !58, !"air-alias-scope-arg(2)"}
!62 = !{!63, !50, i64 0}
!63 = !{!"_ZTS6Holder", !50, i64 0, !39, i64 8}
!64 = !{!39, !39, i64 0}
!65 = !{!61}
!66 = !{!60, !57}
!67 = !{!63, !39, i64 8}
!68 = distinct !{!68, !54}
!69 = !{!60}
!70 = !{!57, !61}
!71 = !{!37, !37, i64 0}
!72 = distinct !{!72, !54}
!73 = !{!74}
!74 = distinct !{!74, !75, !"air-alias-scope-arg(1)"}
!75 = distinct !{!75, !"air-alias-scopes(pooled_spill)"}
!76 = !{!77, !78}
!77 = distinct !{!77, !75, !"air-alias-scope-arg(0)"}
!78 = distinct !{!78, !75, !"air-alias-scope-arg(2)"}
!79 = !{!80, !80, i64 0}
!80 = !{!"long", !37, i64 0}
!81 = !{!78}
!82 = !{!77, !74}
!83 = !{!77}
!84 = !{!74, !78}
!85 = distinct !{!85, !54}
!86 = !{!87, !50, i64 0}
!87 = !{!"_ZTS7Context", !50, i64 0, !39, i64 8}
!88 = !{!89, !50, i64 0}
!89 = !{!"_ZTS3Anc", !50, i64 0}
!90 = !{!87, !39, i64 8}
!91 = !{!92, !93, i64 0}
!92 = !{!"_ZTSN5metal9texture2dIfLNS_6accessE0ELNS_16memory_coherenceE0EvEE", !93, i64 0}
!93 = !{!"__metal_texture_2d_t", !37, i64 0}
!94 = !{!95}
!95 = distinct !{!95, !96, !"air-alias-scope-arg(1)"}
!96 = distinct !{!96, !"air-alias-scopes(pooled_context)"}
!97 = !{!98, !99}
!98 = distinct !{!98, !96, !"air-alias-scope-arg(0)"}
!99 = distinct !{!99, !96, !"air-alias-scope-arg(2)"}
!100 = !{!98}
!101 = !{!95, !99}
@@ -0,0 +1,215 @@
// Owned fixture: thread pointers held in a thread's array of structs at indices only the run knows,
// among the structs' data. `closure_alloc` hands out the next element of the array, or null once
// its room is used up; `closure_alloc_extra` takes room from the array's end and hands out a
// thread pointer to it, which the element keeps in its `extra` field. Each kernel runs eight
// threads in one threadgroup over `in[i] = i + 0.5`.
//
// `pooled_closures`: thread t's array has room for 2 + (t & 3) elements. It asks three times for
// an element (type j + 1, weight in[t] + j), each followed by extra room (10 (j + 1), in[t]) where
// some is left, and writes out[8t + j]: [0..3] element j's weight plus its extra's first float, its
// weight alone without extra room, -1 past the elements handed out; [3] the elements handed out;
// [4] the room left; [5] the sum of their types; [6] the sum of their extras' second floats; [7]
// the elements with extra room.
//
// `pooled_bytes` and `pooled_spill`: a thread pointer to vals[t & 3] (vals[k] = in[t] + k, another
// array) is copied into an element's `extra` as data: as bytes, a byte loop over a length the run
// reads (`len[0]` = 8) from a struct that holds the pointer; and as an integer of its bits, stored
// into device memory (`spill[t]`, volatile) and read back. Words [0] the float the copied pointer
// reads; [1] vals[t & 3] after a write of +100 through it; [2] 1 where the copied pointer equals
// &vals[t & 3]; [3] the elements handed out; the rest 0.
//
// `pooled_context`: the closures of `pooled_closures` behind a context struct that holds a
// constant pointer to an argument buffer (`Anc`, whose `held` points at device structs of
// textures), as a renderer's kernels keep theirs; `shade` (noinline) reads texture `held[t & 1]`
// at x = t & 3 through the context. Words [0] the texel's red, [1..7] as `pooled_closures`' [1..7].
//
// Each array is declared ahead of its switch's only label (on t >> 31, zero for every thread), so
// every path into the switch bypasses its declaration and Clang emits no lifetime markers for it
// (as in generic_pointers.metal).
#include <metal_stdlib>
using namespace metal;
struct Closure {
float w;
int type;
thread float *extra;
float data[2];
};
struct Shader {
Closure closure[6];
int num;
int left;
};
static thread Closure *closure_alloc(thread Shader &sd, int type, float w) {
if (sd.left == 0)
return nullptr;
thread Closure *sc = &sd.closure[sd.num];
sc->type = type;
sc->w = w;
sc->extra = nullptr;
sd.num++;
sd.left--;
return sc;
}
static thread float *closure_alloc_extra(thread Shader &sd) {
if (sd.left == 0)
return nullptr;
sd.left--;
return &sd.closure[sd.num + sd.left].data[0];
}
// Three elements, each with extra room where some is left.
static void fill(thread Shader &sd, float x) {
for (int j = 0; j < 3; j++) {
thread Closure *c = closure_alloc(sd, j + 1, x + float(j));
if (c == nullptr)
continue;
thread float *e = closure_alloc_extra(sd);
if (e != nullptr) {
e[0] = float(10 * (j + 1));
e[1] = x;
c->extra = e;
}
}
}
// Words [first..8) of `pooled_closures`.
static void summarize(thread Shader &sd, device float *o, int first) {
int types = 0, extras = 0;
float seconds = 0.0f;
for (int i = 0; i < 3; i++) {
float v = -1.0f;
if (i < sd.num) {
thread float *e = sd.closure[i].extra;
v = sd.closure[i].w + (e != nullptr ? e[0] : 0.0f);
types += sd.closure[i].type;
if (e != nullptr) {
seconds += e[1];
extras++;
}
}
if (i >= first)
o[i] = v;
}
o[3] = float(sd.num);
o[4] = float(sd.left);
o[5] = float(types);
o[6] = seconds;
o[7] = float(extras);
}
kernel void pooled_closures(device float *out [[buffer(0)]],
device const float *in [[buffer(1)]],
uint t [[thread_position_in_grid]]) {
switch (t >> 31) {
Shader sd;
default:
sd.num = 0;
sd.left = 2 + int(t & 3);
fill(sd, in[t]);
summarize(sd, out + 8 * t, 0);
}
}
struct Holder {
thread float *p;
int n;
};
kernel void pooled_bytes(device float *out [[buffer(0)]],
device const float *in [[buffer(1)]],
device const int *len [[buffer(2)]],
uint t [[thread_position_in_grid]]) {
switch (t >> 31) {
Shader sd;
float vals[4];
Holder h;
default:
for (int k = 0; k < 4; k++)
vals[k] = in[t] + float(k);
h.p = &vals[t & 3];
h.n = len[0];
sd.num = int(t & 1);
sd.left = 2;
thread Closure *c = closure_alloc(sd, 1, 1.0f);
thread uchar *d = (thread uchar *)&c->extra;
thread const uchar *s = (thread const uchar *)&h.p;
for (int k = 0; k < h.n; k++)
d[k] = s[k];
device float *o = out + 8 * t;
thread float *e = sd.closure[sd.num - 1].extra;
o[0] = *e;
*e += 100.0f;
o[1] = vals[t & 3];
o[2] = e == &vals[t & 3] ? 1.0f : 0.0f;
o[3] = float(sd.num);
}
}
kernel void pooled_spill(device float *out [[buffer(0)]],
device const float *in [[buffer(1)]],
volatile device ulong *spill [[buffer(2)]],
uint t [[thread_position_in_grid]]) {
switch (t >> 31) {
Shader sd;
float vals[4];
default:
for (int k = 0; k < 4; k++)
vals[k] = in[t] + float(k);
thread float *p = &vals[t & 3];
ulong bits;
__builtin_memcpy(&bits, &p, sizeof(p));
spill[t] = bits;
sd.num = int(t & 1);
sd.left = 2;
thread Closure *c = closure_alloc(sd, 1, 1.0f);
ulong back = spill[t];
__builtin_memcpy(&c->extra, &back, sizeof(back));
device float *o = out + 8 * t;
thread float *e = sd.closure[sd.num - 1].extra;
o[0] = *e;
*e += 100.0f;
o[1] = vals[t & 3];
o[2] = e == &vals[t & 3] ? 1.0f : 0.0f;
o[3] = float(sd.num);
}
}
struct Held {
texture2d<float> t;
};
struct Anc {
device const Held *held;
};
struct Context {
constant Anc *anc;
int k;
};
__attribute__((noinline)) float shade(thread Context &ctx, thread Shader &sd, float x, uint t) {
fill(sd, x);
return ctx.anc->held[ctx.k].t.read(uint2(t & 3, 0)).x;
}
kernel void pooled_context(device float *out [[buffer(0)]],
device const float *in [[buffer(1)]],
constant Anc *anc [[buffer(2)]],
uint t [[thread_position_in_grid]]) {
switch (t >> 31) {
Shader sd;
Context ctx;
default:
ctx.anc = anc;
ctx.k = int(t & 1);
sd.num = 0;
sd.left = 2 + int(t & 3);
device float *o = out + 8 * t;
o[0] = shade(ctx, sd, in[t], t);
summarize(sd, o, 1);
}
}