From 2b45afa2abd15fff62ad2d8aed8c577c8f99584a Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 3 Dec 2026 04:46:12 -0800 Subject: [PATCH] Fixture: kernel_handle_word_joins's sampler entries load the element at a run-time index The sampler entries read samplers[slot[gid] & 3] again, the structure under test: a word loaded from a sampler-array element at a run-time index (a GEP whose index is a value, not element 0) meets the constexpr sampler's bits at a join. The array stays an array: the authored case checker binds only the first slot of a sampler array (the case schema has no sampler-array resource), so the cases' slot values make the index 0 and Metal reads the one element. engine_handle_words compiles the same three sampler entries over an array, a text edit of the fixture's byval, GEP and argument metadata types, on every committed profile under both policies, and checks the element's load is the clamped load over its run-time index range. Verified: nothing yet at this commit; the next job runs the tests and llvm-dis of the twin. --- .../public/kernel_handle_word_joins.air.ll | 39 ++++++---- .../public/kernel_handle_word_joins.ll | 56 +++++++++----- .../src/tests/engine_handle_words.rs | 75 +++++++++++++++++-- 3 files changed, 131 insertions(+), 39 deletions(-) diff --git a/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.air.ll b/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.air.ll index de951771b..b93b8bea0 100644 --- a/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.air.ll +++ b/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.air.ll @@ -11,7 +11,7 @@ target triple = "air64_v23-apple-macosx11.0.0" @__air_sampler_state = internal addrspace(2) constant i64 -9188470239253757879, align 8 -define void @sampler_word_join(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_word_join(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, i8 addrspace(2)* nocapture readonly "air-buffer-no-alias" %slot, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 @@ -19,8 +19,12 @@ entry: br i1 %pick, label %element, label %join element: + %slotp = getelementptr inbounds i8, i8 addrspace(2)* %slot, i64 %i + %slotv = load i8, i8 addrspace(2)* %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 %arr = bitcast [1 x %struct._sampler_t addrspace(2)*]* %samplers to [1 x %"struct.metal::sampler"]* - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 0 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 %k %wordp = bitcast %"struct.metal::sampler"* %elemp to i64* %word = load i64, i64* %wordp, align 8 br label %join @@ -37,13 +41,17 @@ join: ret void } -define void @sampler_word_select(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_word_select(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, i8 addrspace(2)* nocapture readonly "air-buffer-no-alias" %slot, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 %pick = icmp ne i32 %odd, 0 + %slotp = getelementptr inbounds i8, i8 addrspace(2)* %slot, i64 %i + %slotv = load i8, i8 addrspace(2)* %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 %arr = bitcast [1 x %struct._sampler_t addrspace(2)*]* %samplers to [1 x %"struct.metal::sampler"]* - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 0 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 %k %wordp = bitcast %"struct.metal::sampler"* %elemp to i64* %word = load i64, i64* %wordp, align 8 %bits = select i1 %pick, i64 %word, i64 ptrtoint (i64 addrspace(2)* @__air_sampler_state to i64) @@ -91,7 +99,7 @@ join: ret void } -define void @sampler_pointer_join(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_pointer_join(%struct._texture_2d_t addrspace(1)* %t, [1 x %struct._sampler_t addrspace(2)*]* nocapture readonly byval([1 x %struct._sampler_t addrspace(2)*]) %samplers, i8 addrspace(2)* nocapture readonly "air-buffer-no-alias" %slot, <2 x float> addrspace(2)* nocapture readonly "air-buffer-no-alias" %uv, <4 x float> addrspace(1)* nocapture writeonly "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 @@ -99,8 +107,12 @@ entry: br i1 %pick, label %element, label %join element: + %slotp = getelementptr inbounds i8, i8 addrspace(2)* %slot, i64 %i + %slotv = load i8, i8 addrspace(2)* %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 %arr = bitcast [1 x %struct._sampler_t addrspace(2)*]* %samplers to [1 x %"struct.metal::sampler"]* - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 0, i32 0 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], [1 x %"struct.metal::sampler"]* %arr, i64 0, i64 %k, i32 0 %elem = load %struct._sampler_t addrspace(2)*, %struct._sampler_t addrspace(2)** %elemp, align 8 br label %join @@ -140,19 +152,20 @@ attributes #5 = { argmemonly mustprogress nofree nounwind readonly willreturn } !4 = !{i32 7, !"air.max_read_write_textures", i32 8} !5 = !{i32 7, !"air.max_samplers", i32 16} !6 = !{i32 1, !"wchar_size", i32 4} -!7 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_word_join, !8, !9} +!7 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, i8 addrspace(2)*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_word_join, !8, !9} !8 = !{} -!9 = !{!10, !11, !13, !14, !15} +!9 = !{!10, !11, !12, !13, !14, !15} !10 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} !11 = !{i32 1, !"air.sampler", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"array", !"air.arg_name", !"samplers"} -!13 = !{i32 2, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} -!14 = !{i32 3, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} -!15 = !{i32 4, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"gid"} -!16 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_word_select, !8, !9} +!12 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_size", i32 1, !"air.arg_type_align_size", i32 1, !"air.arg_type_name", !"uchar", !"air.arg_name", !"slot"} +!13 = !{i32 3, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} +!14 = !{i32 4, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} +!15 = !{i32 5, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"gid"} +!16 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, i8 addrspace(2)*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_word_select, !8, !9} !17 = !{void ([4 x %struct._texture_2d_t addrspace(1)*]*, %struct._texture_2d_t addrspace(1)*, i8 addrspace(2)*, <4 x float> addrspace(1)*, i32)* @texture_word_join, !8, !18} !18 = !{!19, !21, !22, !23, !24} !19 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 4, !"air.sample", !"air.arg_type_name", !"array, 4>", !"air.arg_name", !"ts"} -!20 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_pointer_join, !8, !9} +!20 = !{void (%struct._texture_2d_t addrspace(1)*, [1 x %struct._sampler_t addrspace(2)*]*, i8 addrspace(2)*, <2 x float> addrspace(2)*, <4 x float> addrspace(1)*, i32)* @sampler_pointer_join, !8, !9} !21 = !{i32 1, !"air.texture", !"air.location_index", i32 4, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"other"} !22 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_size", i32 1, !"air.arg_type_align_size", i32 1, !"air.arg_type_name", !"uchar", !"air.arg_name", !"slot"} !23 = !{i32 3, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} diff --git a/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.ll b/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.ll index 7984dedba..42306483b 100644 --- a/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.ll +++ b/crates/metal2vulkan-validation/fixtures/public/kernel_handle_word_joins.ll @@ -2,18 +2,21 @@ ; Samplers and textures copied as their 64-bit integer bits and merged at a join, the form an older ; Metal frontend writes for ; -; sampler s = cond ? samplers[0] : fallback; // fallback: a constexpr sampler +; sampler s = cond ? samplers[slot[gid] & 3] : fallback; // fallback: a constexpr sampler ; -; Each branch's handle is copied as an integer: the element of an `array` parameter as a -; plain i64 load through its address, the constexpr sampler as the `ptrtoint` of its state. The -; integers are merged in a `phi` (or a `select`), and the merged integer is turned back into a -; sampler with `inttoptr` at its use. Today's frontend loads and merges the pointers instead; no MSL -; it compiles spells this, so the text is written by hand, after the module today's compiler writes -; for that source (`kernel_handle_word_joins.air.ll` is its typed-pointer twin, which Metal runs). +; Each branch's handle is copied as an integer: an element of a sampler array, read at a run-time +; index, as a plain i64 load through its address, the constexpr sampler as the `ptrtoint` of its +; state. The integers are merged in a `phi` (or a `select`), and the merged integer is turned back +; into a sampler with `inttoptr` at its use. Today's frontend loads and merges the pointers +; instead; no MSL it compiles spells this, so the text is written by hand, after the module today's +; compiler writes for that source (`kernel_handle_word_joins.air.ll` is its typed-pointer twin, +; which Metal runs). ; -; - `sampler_word_join`: odd threads sample through `samplers[0]`, even threads through the -; constexpr sampler (nearest, clamp to edge), at the coordinate `uv[gid]`: a branch and a `phi` -; of the two integers. +; - `sampler_word_join`: odd threads sample through `samplers[slot[gid] & 3]`, even threads +; through the constexpr sampler (nearest, clamp to edge), at the coordinate `uv[gid]`: a branch +; and a `phi` of the two integers. The array is an `array`, since the authored cases +; bind a sampler array at its first slot only, and their slot values make the index 0; +; `engine_handle_words` compiles the same entries over an `array`. ; - `sampler_word_select`: the same choice as a `select` of the two integers. ; - `texture_word_join`: odd threads read `ts[slot[gid] & 3]`, even threads `other`: a `phi` of a ; texture-array element's integer and the `ptrtoint` of a texture parameter. @@ -27,7 +30,7 @@ target triple = "air64_v23-apple-macosx11.0.0" @__air_sampler_state = internal addrspace(2) constant i64 -9188470239253757879, align 8 -define void @sampler_word_join(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_word_join(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %slot, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 @@ -35,7 +38,11 @@ entry: br i1 %pick, label %element, label %join element: - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 0 + %slotp = getelementptr inbounds i8, ptr addrspace(2) %slot, i64 %i + %slotv = load i8, ptr addrspace(2) %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 %k %word = load i64, ptr %elemp, align 8 br label %join @@ -51,12 +58,16 @@ join: ret void } -define void @sampler_word_select(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_word_select(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %slot, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 %pick = icmp ne i32 %odd, 0 - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 0 + %slotp = getelementptr inbounds i8, ptr addrspace(2) %slot, i64 %i + %slotv = load i8, ptr addrspace(2) %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 %k %word = load i64, ptr %elemp, align 8 %bits = select i1 %pick, i64 %word, i64 ptrtoint (ptr addrspace(2) @__air_sampler_state to i64) %s = inttoptr i64 %bits to ptr addrspace(2) @@ -101,7 +112,7 @@ join: ret void } -define void @sampler_pointer_join(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { +define void @sampler_pointer_join(ptr addrspace(1) %t, ptr readonly byval([1 x ptr addrspace(2)]) captures(none) %samplers, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %slot, ptr addrspace(2) readonly captures(none) "air-buffer-no-alias" %uv, ptr addrspace(1) writeonly captures(none) "air-buffer-no-alias" %out, i32 %gid) local_unnamed_addr #0 { entry: %i = zext i32 %gid to i64 %odd = and i32 %gid, 1 @@ -109,7 +120,11 @@ entry: br i1 %pick, label %element, label %join element: - %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 0, i32 0 + %slotp = getelementptr inbounds i8, ptr addrspace(2) %slot, i64 %i + %slotv = load i8, ptr addrspace(2) %slotp, align 1 + %k8 = and i8 %slotv, 3 + %k = zext i8 %k8 to i64 + %elemp = getelementptr inbounds [1 x %"struct.metal::sampler"], ptr %samplers, i64 0, i64 %k, i32 0 %elem = load ptr addrspace(2), ptr %elemp, align 8 br label %join @@ -151,12 +166,13 @@ attributes #5 = { mustprogress nofree nounwind willreturn memory(argmem: read) } !6 = !{i32 1, !"wchar_size", i32 4} !7 = !{ptr @sampler_word_join, !8, !9} !8 = !{} -!9 = !{!10, !11, !13, !14, !15} +!9 = !{!10, !11, !12, !13, !14, !15} !10 = !{i32 0, !"air.texture", !"air.location_index", i32 0, i32 1, !"air.sample", !"air.arg_type_name", !"texture2d", !"air.arg_name", !"t"} !11 = !{i32 1, !"air.sampler", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"array", !"air.arg_name", !"samplers"} -!13 = !{i32 2, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} -!14 = !{i32 3, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} -!15 = !{i32 4, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"gid"} +!12 = !{i32 2, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.arg_type_size", i32 1, !"air.arg_type_align_size", i32 1, !"air.arg_type_name", !"uchar", !"air.arg_name", !"slot"} +!13 = !{i32 3, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read", !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"float2", !"air.arg_name", !"uv"} +!14 = !{i32 4, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read_write", !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"float4", !"air.arg_name", !"out"} +!15 = !{i32 5, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"gid"} !16 = !{ptr @sampler_word_select, !8, !9} !17 = !{ptr @texture_word_join, !8, !18} !18 = !{!19, !21, !22, !23, !24} diff --git a/crates/metal2vulkan/src/tests/engine_handle_words.rs b/crates/metal2vulkan/src/tests/engine_handle_words.rs index c040167b2..49d794751 100644 --- a/crates/metal2vulkan/src/tests/engine_handle_words.rs +++ b/crates/metal2vulkan/src/tests/engine_handle_words.rs @@ -69,9 +69,7 @@ fn handles_merged_at_a_join_compile_on_every_profile() { /// The fixture's text with entry `entry`'s body edited (each `(from, to)` must occur once in it), /// assembled; `None` (skipped visibly) without `llvm-as`. fn variant(entry: &str, edits: &[(&str, &str)]) -> Option> { - static NEXT: AtomicU64 = AtomicU64::new(0); - let path = fixtures_dir().join("kernel_handle_word_joins.ll"); - let text = std::fs::read_to_string(&path).expect("fixture"); + let text = fixture_text(); let start = text.find(&format!("define void @{entry}(")).expect("entry"); let end = text[start..] .find("\n}\n") @@ -82,7 +80,17 @@ fn variant(entry: &str, edits: &[(&str, &str)]) -> Option> { assert_eq!(body.matches(from).count(), 1, "{entry}: {from}"); body = body.replacen(from, to, 1); } - let edited = format!("{}{body}{}", &text[..start], &text[end..]); + assemble_text(entry, &format!("{}{body}{}", &text[..start], &text[end..])) +} + +/// The fixture's text. +fn fixture_text() -> String { + std::fs::read_to_string(fixtures_dir().join("kernel_handle_word_joins.ll")).expect("fixture") +} + +/// `text` assembled in a scratch directory of its own; `None` (skipped visibly) without `llvm-as`. +fn assemble_text(name: &str, text: &str) -> Option> { + static NEXT: AtomicU64 = AtomicU64::new(0); let dir = std::env::temp_dir().join(format!( "m2v-handle-words-{}-{}", std::process::id(), @@ -90,7 +98,7 @@ fn variant(entry: &str, edits: &[(&str, &str)]) -> Option> { )); std::fs::create_dir_all(&dir).expect("scratch"); let ll = dir.join("k.ll"); - std::fs::write(&ll, &edited).expect("write"); + std::fs::write(&ll, text).expect("write"); let r = llvm_assemble(&ll); let _ = std::fs::remove_dir_all(&dir); match r { @@ -99,10 +107,65 @@ fn variant(entry: &str, edits: &[(&str, &str)]) -> Option> { eprintln!("SKIPPED: {why}"); None } - Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected {entry}: {why}"), + Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected {name}: {why}"), } } +/// The sampler entries over an `array` (the fixture's is of one, the most the authored +/// cases bind): the element word is read at a run-time index over four of its elements, clamped, +/// and merged with the constexpr sampler's bits, on every profile under either policy. +#[test] +fn a_sampler_word_at_a_run_time_index_of_eight_compiles_on_every_profile() { + let mut text = fixture_text(); + for (from, to, n) in [ + ( + "byval([1 x ptr addrspace(2)])", + "byval([8 x ptr addrspace(2)])", + 3, + ), + ( + "[1 x %\"struct.metal::sampler\"]", + "[8 x %\"struct.metal::sampler\"]", + 3, + ), + ( + "i32 0, i32 1, !\"air.arg_type_name\", !\"array\"", + "i32 0, i32 8, !\"air.arg_type_name\", !\"array\"", + 1, + ), + ] { + assert_eq!(text.matches(from).count(), n, "{from}"); + text = text.replace(from, to); + } + let Some(bc) = assemble_text("eight_samplers", &text) else { + return; + }; + let mut failed = Vec::new(); + for policy in [DevicePolicy::RESIDENT_TABLES, DevicePolicy::default()] { + for (stem, p) in committed_profiles_with(policy).expect("profiles") { + for entry in [ + "sampler_word_join", + "sampler_word_select", + "sampler_pointer_join", + ] { + let checked = compile(&bc, entry, &p) + .map_err(|e| e.to_string()) + .and_then(|a| { + link_and_validate(&a, &p).map_err(|e| format!("L3: {e}"))?; + match uses(&a, "access.handle.array_load.clamped") { + 1 => Ok(()), + n => Err(format!("{n} clamped element loads")), + } + }); + if let Err(e) = checked { + failed.push(format!("{stem} {policy:?} {entry}: {e}")); + } + } + } + } + assert!(failed.is_empty(), "{}", failed.join("\n")); +} + /// The refusal compiling `entry` of `bc` meets on the M3's profile. fn refusal(bc: &[u8], entry: &str) -> Unsupported { let p = profile("moltenvk-m3", DevicePolicy::RESIDENT_TABLES).expect("profile");