From 82fd1b61088663166d3b1923b49ab5233b1e477d Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 09:19:27 -0800 Subject: [PATCH] depth_executions: the narrow-gate frame keeps its host steps through per-command strides Fixes runtime_lane::depth_executions::a_frame_s_executions_wait_for_the_culling_alone. It has failed since 5766d29051 (SEMANTICS_VERSION 2051, RUNTIME_VERSION 2124), with steps 0, gates narrowed 0 and correct depths. Cause: the test lost its premise; no behaviour regressed. - Its state fetches one float3 stage_in attribute from a fixed 12-byte layout. - Before RV 2124, plan_replay refused every vertex fetch under replay bindings. So each execution took the host step, and the test counted those steps' narrowed gates. - RV 2124 lets a replay pull fixed-function layouts (draw_replay_vertex_layouts). These executions now run on the GPU alone, with no host step and no gate. Fix: - The fixture icb_depth_indexed_encode gains dio_encode_strided. It is dio_encode with the positions bound at a stride of 12 bytes of its own (set_vertex_buffer(positions, 12, 0)), for a state whose layout takes its stride per bind. Its companions are regenerated with scripts/regen-air-fixtures. - Workload::with_strides(device, per_bind) makes that state (layout stride None), the buffer with supportDynamicAttributeStride, and the strided kernel. - A layout whose stride is set per bind keeps the host step (DrawReplayRule::VertexAttributes: a command's stride lives in its record alone). So the narrow-gate frame takes two host steps again, and each gate leaves out the unrelated fill and, for the second step, the first pass's batch: (2, 2). - Visibility results were tried first and rejected. They keep the host step too, but they unbind the steps' reads (DeferredPass::bound is None for an execution that counts), so no gate narrows: (2, 0). - The fixed-stride shape stays as a_frame_s_executions_at_a_fixed_stride_run_on_the_gpu, which checks for no host step and no gate, (0, 0), with correct depths. - The frame's body moved into frame(device, per_bind). It checks the statuses, the refusal and the depths, and returns (steps, narrowed). The new kernel adds rows to the translator's fixture tables (public_fixture_expectations_engine.tsv, golden_hashes.tsv, tests/air_input/icb_depth_indexed_encode.txt). They are left for the bless on development. Verified on lavapipe in debug, m3-max-21648, one run each without and with METAL2VULKAN_LANE_VALIDATION=sync: - the strided frame printed steps 2, gates narrowed 2; - the fixed-stride frame printed steps 0, gates narrowed 0; - the depths were correct in both, and all three depth_executions tests passed each time; - clippy of metal2vulkan-validation (every feature; plain, --cfg m2v_gpu, --cfg m2v_cpu_icd) is clean. --- .../engine/icb_depth_indexed_encode.air.ll | 144 +++++++++++------ .../public/engine/icb_depth_indexed_encode.ll | 144 +++++++++++------ .../engine/icb_depth_indexed_encode.metal | 22 ++- .../src/runtime_lane/depth_executions.rs | 152 +++++++++++------- 4 files changed, 300 insertions(+), 162 deletions(-) diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.air.ll b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.air.ll index 634286e00..7dbd605b0 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.air.ll +++ b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.air.ll @@ -12,15 +12,15 @@ target triple = "air64_v28-apple-macosx26.0.0" ; Function Attrs: argmemonly mustprogress nofree norecurse nosync nounwind willreturn writeonly define void @dio_reset(i32 addrspace(1)* nocapture noundef writeonly "air-buffer-no-alias" %0) local_unnamed_addr #0 { - store i32 0, i32 addrspace(1)* %0, align 4, !tbaa !38, !alias.scope !42 + store i32 0, i32 addrspace(1)* %0, align 4, !tbaa !39, !alias.scope !43 %2 = getelementptr inbounds i32, i32 addrspace(1)* %0, i64 1 - store i32 0, i32 addrspace(1)* %2, align 4, !tbaa !38, !alias.scope !42 + store i32 0, i32 addrspace(1)* %2, align 4, !tbaa !39, !alias.scope !43 ret void } ; Function Attrs: mustprogress nounwind willreturn define void @dio_encode(%struct.Encoder addrspace(1)* nocapture noundef readonly align 8 dereferenceable(8) "air-buffer-no-alias" %0, float addrspace(1)* noundef "air-buffer-no-alias" %1, i16 addrspace(1)* noundef "air-buffer-no-alias" %2, %struct.Camera addrspace(2)* noundef align 4 dereferenceable(8) "air-buffer-no-alias" %3, %"struct.metal::_atomic" addrspace(1)* nocapture noundef "air-buffer-no-alias" %4, i32 addrspace(2)* nocapture noundef readonly align 4 dereferenceable(4) "air-buffer-no-alias" %5, i32 noundef %6) local_unnamed_addr #1 { - %8 = load i32, i32 addrspace(2)* %5, align 4, !tbaa !38, !alias.scope !45, !noalias !48 + %8 = load i32, i32 addrspace(2)* %5, align 4, !tbaa !39, !alias.scope !46, !noalias !49 %9 = and i32 %6, 31 %10 = shl nuw i32 1, %9 %11 = and i32 %8, %10 @@ -31,7 +31,7 @@ define void @dio_encode(%struct.Encoder addrspace(1)* nocapture noundef readonly %14 = getelementptr inbounds %"struct.metal::_atomic", %"struct.metal::_atomic" addrspace(1)* %4, i64 0, i32 0 %15 = tail call i32 @air.atomic.global.add.u.i32(i32 addrspace(1)* nocapture %14, i32 1, i32 0, i32 2, i1 true) #4 %16 = getelementptr inbounds %struct.Encoder, %struct.Encoder addrspace(1)* %0, i64 0, i32 0, i32 0 - %17 = load %struct._command_buffer_t addrspace(1)*, %struct._command_buffer_t addrspace(1)* addrspace(1)* %16, align 8, !tbaa.struct !54, !alias.scope !57, !noalias !58 + %17 = load %struct._command_buffer_t addrspace(1)*, %struct._command_buffer_t addrspace(1)* addrspace(1)* %16, align 8, !tbaa.struct !55, !alias.scope !58, !noalias !59 %18 = bitcast float addrspace(1)* %1 to i8 addrspace(1)* tail call void @air.set_vertex_buffer_render_command.p1i8(%struct._command_buffer_t addrspace(1)* nocapture %17, i32 %15, i8 addrspace(1)* %18, i64 -1, i32 0) #4 %19 = bitcast %struct.Camera addrspace(2)* %3 to i8 addrspace(2)* @@ -46,15 +46,43 @@ define void @dio_encode(%struct.Encoder addrspace(1)* nocapture noundef readonly ret void } +; Function Attrs: mustprogress nounwind willreturn +define void @dio_encode_strided(%struct.Encoder addrspace(1)* nocapture noundef readonly align 8 dereferenceable(8) "air-buffer-no-alias" %0, float addrspace(1)* noundef "air-buffer-no-alias" %1, i16 addrspace(1)* noundef "air-buffer-no-alias" %2, %struct.Camera addrspace(2)* noundef align 4 dereferenceable(8) "air-buffer-no-alias" %3, %"struct.metal::_atomic" addrspace(1)* nocapture noundef "air-buffer-no-alias" %4, i32 addrspace(2)* nocapture noundef readonly align 4 dereferenceable(4) "air-buffer-no-alias" %5, i32 noundef %6) local_unnamed_addr #1 { + %8 = load i32, i32 addrspace(2)* %5, align 4, !tbaa !39, !alias.scope !60, !noalias !63 + %9 = and i32 %6, 31 + %10 = shl nuw i32 1, %9 + %11 = and i32 %8, %10 + %12 = icmp eq i32 %11, 0 + br i1 %12, label %23, label %13 + +13: ; preds = %7 + %14 = getelementptr inbounds %"struct.metal::_atomic", %"struct.metal::_atomic" addrspace(1)* %4, i64 0, i32 0 + %15 = tail call i32 @air.atomic.global.add.u.i32(i32 addrspace(1)* nocapture %14, i32 1, i32 0, i32 2, i1 true) #4 + %16 = getelementptr inbounds %struct.Encoder, %struct.Encoder addrspace(1)* %0, i64 0, i32 0, i32 0 + %17 = load %struct._command_buffer_t addrspace(1)*, %struct._command_buffer_t addrspace(1)* addrspace(1)* %16, align 8, !tbaa.struct !55, !alias.scope !69, !noalias !70 + %18 = bitcast float addrspace(1)* %1 to i8 addrspace(1)* + tail call void @air.set_vertex_buffer_render_command.p1i8(%struct._command_buffer_t addrspace(1)* nocapture %17, i32 %15, i8 addrspace(1)* %18, i64 12, i32 0) #4 + %19 = bitcast %struct.Camera addrspace(2)* %3 to i8 addrspace(2)* + tail call void @air.set_vertex_buffer_render_command.p2i8(%struct._command_buffer_t addrspace(1)* nocapture %17, i32 %15, i8 addrspace(2)* %19, i64 -1, i32 1) #4 + %20 = mul i32 %6, 6 + %21 = zext i32 %20 to i64 + %22 = getelementptr inbounds i16, i16 addrspace(1)* %2, i64 %21 + tail call void @air.draw_indexed_primitives_render_command.p1i16(%struct._command_buffer_t addrspace(1)* nocapture %17, i32 %15, i32 3, i32 6, i16 addrspace(1)* %22, i32 1, i32 0, i32 0) #4 + br label %23 + +23: ; preds = %7, %13 + ret void +} + ; Function Attrs: mustprogress nofree norecurse nosync nounwind readnone willreturn define <4 x float> @dio_vertex(<3 x float> %0, %struct.Camera addrspace(2)* nocapture noundef readonly align 4 dereferenceable(8) "air-buffer-no-alias" %1) local_unnamed_addr #2 { %3 = shufflevector <3 x float> %0, <3 x float> poison, <4 x i32> %4 = extractelement <3 x float> %0, i64 2 %5 = getelementptr inbounds %struct.Camera, %struct.Camera addrspace(2)* %1, i64 0, i32 0 - %6 = load float, float addrspace(2)* %5, align 4, !tbaa !59, !alias.scope !62 + %6 = load float, float addrspace(2)* %5, align 4, !tbaa !71, !alias.scope !74 %7 = fmul fast float %6, %4 %8 = getelementptr inbounds %struct.Camera, %struct.Camera addrspace(2)* %1, i64 0, i32 1 - %9 = load float, float addrspace(2)* %8, align 4, !tbaa !65, !alias.scope !62 + %9 = load float, float addrspace(2)* %8, align 4, !tbaa !77, !alias.scope !74 %10 = fadd fast float %7, %9 %11 = insertelement <4 x float> %3, float 1.000000e+00, i64 3 %12 = insertelement <4 x float> %11, float %10, i64 2 @@ -80,12 +108,12 @@ attributes #3 = { mustprogress nounwind willreturn } attributes #4 = { nounwind willreturn } !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} -!air.kernel = !{!9, !13} -!air.vertex = !{!26} -!air.compile_options = !{!32, !33, !34} -!llvm.ident = !{!35} -!air.version = !{!36} -!air.language_version = !{!37} +!air.kernel = !{!9, !13, !26} +!air.vertex = !{!27} +!air.compile_options = !{!33, !34, !35} +!llvm.ident = !{!36} +!air.version = !{!37} +!air.language_version = !{!38} !0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} !1 = !{i32 1, !"wchar_size", i32 4} @@ -113,43 +141,55 @@ attributes #4 = { nounwind willreturn } !23 = !{i32 0, i32 4, i32 0, !"uint", !"__s"} !24 = !{i32 5, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 5, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"visible"} !25 = !{i32 6, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"tid"} -!26 = !{<4 x float> (<3 x float>, %struct.Camera addrspace(2)*)* @dio_vertex, !27, !29} -!27 = !{!28} -!28 = !{!"air.position", !"air.arg_type_name", !"float4"} -!29 = !{!30, !31} -!30 = !{i32 0, !"air.vertex_input", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"float3", !"air.arg_name", !"position"} -!31 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !21, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"Camera", !"air.arg_name", !"camera"} -!32 = !{!"air.compile.denorms_disable"} -!33 = !{!"air.compile.fast_math_enable"} -!34 = !{!"air.compile.framebuffer_fetch_enable"} -!35 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} -!36 = !{i32 2, i32 8, i32 0} -!37 = !{!"Metal", i32 4, i32 0, i32 0} -!38 = !{!39, !39, i64 0} -!39 = !{!"int", !40, i64 0} -!40 = !{!"omnipotent char", !41, i64 0} -!41 = !{!"Simple C++ TBAA"} -!42 = !{!43} -!43 = distinct !{!43, !44, !"air-alias-scope-arg(0)"} -!44 = distinct !{!44, !"air-alias-scopes(dio_reset)"} -!45 = !{!46} -!46 = distinct !{!46, !47, !"air-alias-scope-arg(5)"} -!47 = distinct !{!47, !"air-alias-scopes(dio_encode)"} -!48 = !{!49, !50, !51, !52, !53} -!49 = distinct !{!49, !47, !"air-alias-scope-arg(0)"} -!50 = distinct !{!50, !47, !"air-alias-scope-arg(1)"} -!51 = distinct !{!51, !47, !"air-alias-scope-arg(2)"} -!52 = distinct !{!52, !47, !"air-alias-scope-arg(3)"} -!53 = distinct !{!53, !47, !"air-alias-scope-arg(4)"} -!54 = !{i64 0, i64 8, !55} -!55 = !{!56, !56, i64 0} -!56 = !{!"__metal_command_buffer_t", !40, i64 0} -!57 = !{!49} -!58 = !{!50, !51, !52, !53, !46} -!59 = !{!60, !61, i64 0} -!60 = !{!"_ZTS6Camera", !61, i64 0, !61, i64 4} -!61 = !{!"float", !40, i64 0} -!62 = !{!63} -!63 = distinct !{!63, !64, !"air-alias-scope-arg(1)"} -!64 = distinct !{!64, !"air-alias-scopes(dio_vertex)"} -!65 = !{!60, !61, i64 4} +!26 = !{void (%struct.Encoder addrspace(1)*, float addrspace(1)*, i16 addrspace(1)*, %struct.Camera addrspace(2)*, %"struct.metal::_atomic" addrspace(1)*, i32 addrspace(2)*, i32)* @dio_encode_strided, !10, !14} +!27 = !{<4 x float> (<3 x float>, %struct.Camera addrspace(2)*)* @dio_vertex, !28, !30} +!28 = !{!29} +!29 = !{!"air.position", !"air.arg_type_name", !"float4"} +!30 = !{!31, !32} +!31 = !{i32 0, !"air.vertex_input", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"float3", !"air.arg_name", !"position"} +!32 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !21, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"Camera", !"air.arg_name", !"camera"} +!33 = !{!"air.compile.denorms_disable"} +!34 = !{!"air.compile.fast_math_enable"} +!35 = !{!"air.compile.framebuffer_fetch_enable"} +!36 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!37 = !{i32 2, i32 8, i32 0} +!38 = !{!"Metal", i32 4, i32 0, i32 0} +!39 = !{!40, !40, i64 0} +!40 = !{!"int", !41, i64 0} +!41 = !{!"omnipotent char", !42, i64 0} +!42 = !{!"Simple C++ TBAA"} +!43 = !{!44} +!44 = distinct !{!44, !45, !"air-alias-scope-arg(0)"} +!45 = distinct !{!45, !"air-alias-scopes(dio_reset)"} +!46 = !{!47} +!47 = distinct !{!47, !48, !"air-alias-scope-arg(5)"} +!48 = distinct !{!48, !"air-alias-scopes(dio_encode)"} +!49 = !{!50, !51, !52, !53, !54} +!50 = distinct !{!50, !48, !"air-alias-scope-arg(0)"} +!51 = distinct !{!51, !48, !"air-alias-scope-arg(1)"} +!52 = distinct !{!52, !48, !"air-alias-scope-arg(2)"} +!53 = distinct !{!53, !48, !"air-alias-scope-arg(3)"} +!54 = distinct !{!54, !48, !"air-alias-scope-arg(4)"} +!55 = !{i64 0, i64 8, !56} +!56 = !{!57, !57, i64 0} +!57 = !{!"__metal_command_buffer_t", !41, i64 0} +!58 = !{!50} +!59 = !{!51, !52, !53, !54, !47} +!60 = !{!61} +!61 = distinct !{!61, !62, !"air-alias-scope-arg(5)"} +!62 = distinct !{!62, !"air-alias-scopes(dio_encode_strided)"} +!63 = !{!64, !65, !66, !67, !68} +!64 = distinct !{!64, !62, !"air-alias-scope-arg(0)"} +!65 = distinct !{!65, !62, !"air-alias-scope-arg(1)"} +!66 = distinct !{!66, !62, !"air-alias-scope-arg(2)"} +!67 = distinct !{!67, !62, !"air-alias-scope-arg(3)"} +!68 = distinct !{!68, !62, !"air-alias-scope-arg(4)"} +!69 = !{!64} +!70 = !{!65, !66, !67, !68, !61} +!71 = !{!72, !73, i64 0} +!72 = !{!"_ZTS6Camera", !73, i64 0, !73, i64 4} +!73 = !{!"float", !41, i64 0} +!74 = !{!75} +!75 = distinct !{!75, !76, !"air-alias-scope-arg(1)"} +!76 = distinct !{!76, !"air-alias-scopes(dio_vertex)"} +!77 = !{!72, !73, i64 4} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.ll b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.ll index f1951d5bc..bd5e129a7 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.ll +++ b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.ll @@ -11,15 +11,15 @@ target triple = "air64_v28-apple-macosx26.0.0" ; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) define void @dio_reset(ptr addrspace(1) noundef writeonly captures(none) "air-buffer-no-alias" %0) local_unnamed_addr #0 { - store i32 0, ptr addrspace(1) %0, align 4, !tbaa !38, !alias.scope !42 + store i32 0, ptr addrspace(1) %0, align 4, !tbaa !39, !alias.scope !43 %2 = getelementptr inbounds i32, ptr addrspace(1) %0, i64 1 - store i32 0, ptr addrspace(1) %2, align 4, !tbaa !38, !alias.scope !42 + store i32 0, ptr addrspace(1) %2, align 4, !tbaa !39, !alias.scope !43 ret void } ; Function Attrs: mustprogress nounwind willreturn define void @dio_encode(ptr addrspace(1) noundef readonly align 8 captures(none) dereferenceable(8) "air-buffer-no-alias" %0, ptr addrspace(1) noundef "air-buffer-no-alias" %1, ptr addrspace(1) noundef "air-buffer-no-alias" %2, ptr addrspace(2) noundef align 4 dereferenceable(8) "air-buffer-no-alias" %3, ptr addrspace(1) noundef captures(none) "air-buffer-no-alias" %4, ptr addrspace(2) noundef readonly align 4 captures(none) dereferenceable(4) "air-buffer-no-alias" %5, i32 noundef %6) local_unnamed_addr #1 { - %8 = load i32, ptr addrspace(2) %5, align 4, !tbaa !38, !alias.scope !45, !noalias !48 + %8 = load i32, ptr addrspace(2) %5, align 4, !tbaa !39, !alias.scope !46, !noalias !49 %9 = and i32 %6, 31 %10 = shl nuw i32 1, %9 %11 = and i32 %8, %10 @@ -30,7 +30,7 @@ define void @dio_encode(ptr addrspace(1) noundef readonly align 8 captures(none) %14 = getelementptr inbounds %"struct.metal::_atomic", ptr addrspace(1) %4, i64 0, i32 0 %15 = tail call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) captures(none) %14, i32 1, i32 0, i32 2, i1 true) #4 %16 = getelementptr inbounds %struct.Encoder, ptr addrspace(1) %0, i64 0, i32 0, i32 0 - %17 = load ptr addrspace(1), ptr addrspace(1) %16, align 8, !tbaa.struct !54, !alias.scope !57, !noalias !58 + %17 = load ptr addrspace(1), ptr addrspace(1) %16, align 8, !tbaa.struct !55, !alias.scope !58, !noalias !59 %18 = bitcast ptr addrspace(1) %1 to ptr addrspace(1) tail call void @air.set_vertex_buffer_render_command.p1i8(ptr addrspace(1) captures(none) %17, i32 %15, ptr addrspace(1) %18, i64 -1, i32 0) #4 %19 = bitcast ptr addrspace(2) %3 to ptr addrspace(2) @@ -45,15 +45,43 @@ define void @dio_encode(ptr addrspace(1) noundef readonly align 8 captures(none) ret void } +; Function Attrs: mustprogress nounwind willreturn +define void @dio_encode_strided(ptr addrspace(1) noundef readonly align 8 captures(none) dereferenceable(8) "air-buffer-no-alias" %0, ptr addrspace(1) noundef "air-buffer-no-alias" %1, ptr addrspace(1) noundef "air-buffer-no-alias" %2, ptr addrspace(2) noundef align 4 dereferenceable(8) "air-buffer-no-alias" %3, ptr addrspace(1) noundef captures(none) "air-buffer-no-alias" %4, ptr addrspace(2) noundef readonly align 4 captures(none) dereferenceable(4) "air-buffer-no-alias" %5, i32 noundef %6) local_unnamed_addr #1 { + %8 = load i32, ptr addrspace(2) %5, align 4, !tbaa !39, !alias.scope !60, !noalias !63 + %9 = and i32 %6, 31 + %10 = shl nuw i32 1, %9 + %11 = and i32 %8, %10 + %12 = icmp eq i32 %11, 0 + br i1 %12, label %23, label %13 + +13: ; preds = %7 + %14 = getelementptr inbounds %"struct.metal::_atomic", ptr addrspace(1) %4, i64 0, i32 0 + %15 = tail call i32 @air.atomic.global.add.u.i32(ptr addrspace(1) captures(none) %14, i32 1, i32 0, i32 2, i1 true) #4 + %16 = getelementptr inbounds %struct.Encoder, ptr addrspace(1) %0, i64 0, i32 0, i32 0 + %17 = load ptr addrspace(1), ptr addrspace(1) %16, align 8, !tbaa.struct !55, !alias.scope !69, !noalias !70 + %18 = bitcast ptr addrspace(1) %1 to ptr addrspace(1) + tail call void @air.set_vertex_buffer_render_command.p1i8(ptr addrspace(1) captures(none) %17, i32 %15, ptr addrspace(1) %18, i64 12, i32 0) #4 + %19 = bitcast ptr addrspace(2) %3 to ptr addrspace(2) + tail call void @air.set_vertex_buffer_render_command.p2i8(ptr addrspace(1) captures(none) %17, i32 %15, ptr addrspace(2) %19, i64 -1, i32 1) #4 + %20 = mul i32 %6, 6 + %21 = zext i32 %20 to i64 + %22 = getelementptr inbounds i16, ptr addrspace(1) %2, i64 %21 + tail call void @air.draw_indexed_primitives_render_command.p1i16(ptr addrspace(1) captures(none) %17, i32 %15, i32 3, i32 6, ptr addrspace(1) %22, i32 1, i32 0, i32 0) #4 + br label %23 + +23: ; preds = %13, %7 + ret void +} + ; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none) define <4 x float> @dio_vertex(<3 x float> %0, ptr addrspace(2) noundef readonly align 4 captures(none) dereferenceable(8) "air-buffer-no-alias" %1) local_unnamed_addr #2 { %3 = shufflevector <3 x float> %0, <3 x float> poison, <4 x i32> %4 = extractelement <3 x float> %0, i64 2 %5 = getelementptr inbounds %struct.Camera, ptr addrspace(2) %1, i64 0, i32 0 - %6 = load float, ptr addrspace(2) %5, align 4, !tbaa !59, !alias.scope !62 + %6 = load float, ptr addrspace(2) %5, align 4, !tbaa !71, !alias.scope !74 %7 = fmul fast float %6, %4 %8 = getelementptr inbounds %struct.Camera, ptr addrspace(2) %1, i64 0, i32 1 - %9 = load float, ptr addrspace(2) %8, align 4, !tbaa !65, !alias.scope !62 + %9 = load float, ptr addrspace(2) %8, align 4, !tbaa !77, !alias.scope !74 %10 = fadd fast float %7, %9 %11 = insertelement <4 x float> %3, float 1.000000e+00, i64 3 %12 = insertelement <4 x float> %11, float %10, i64 2 @@ -79,12 +107,12 @@ attributes #3 = { mustprogress nounwind willreturn } attributes #4 = { nounwind willreturn } !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} -!air.kernel = !{!9, !13} -!air.vertex = !{!26} -!air.compile_options = !{!32, !33, !34} -!llvm.ident = !{!35} -!air.version = !{!36} -!air.language_version = !{!37} +!air.kernel = !{!9, !13, !26} +!air.vertex = !{!27} +!air.compile_options = !{!33, !34, !35} +!llvm.ident = !{!36} +!air.version = !{!37} +!air.language_version = !{!38} !0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 2]} !1 = !{i32 1, !"wchar_size", i32 4} @@ -112,43 +140,55 @@ attributes #4 = { nounwind willreturn } !23 = !{i32 0, i32 4, i32 0, !"uint", !"__s"} !24 = !{i32 5, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 5, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"visible"} !25 = !{i32 6, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"tid"} -!26 = !{ptr @dio_vertex, !27, !29} -!27 = !{!28} -!28 = !{!"air.position", !"air.arg_type_name", !"float4"} -!29 = !{!30, !31} -!30 = !{i32 0, !"air.vertex_input", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"float3", !"air.arg_name", !"position"} -!31 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !21, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"Camera", !"air.arg_name", !"camera"} -!32 = !{!"air.compile.denorms_disable"} -!33 = !{!"air.compile.fast_math_enable"} -!34 = !{!"air.compile.framebuffer_fetch_enable"} -!35 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} -!36 = !{i32 2, i32 8, i32 0} -!37 = !{!"Metal", i32 4, i32 0, i32 0} -!38 = !{!39, !39, i64 0} -!39 = !{!"int", !40, i64 0} -!40 = !{!"omnipotent char", !41, i64 0} -!41 = !{!"Simple C++ TBAA"} -!42 = !{!43} -!43 = distinct !{!43, !44, !"air-alias-scope-arg(0)"} -!44 = distinct !{!44, !"air-alias-scopes(dio_reset)"} -!45 = !{!46} -!46 = distinct !{!46, !47, !"air-alias-scope-arg(5)"} -!47 = distinct !{!47, !"air-alias-scopes(dio_encode)"} -!48 = !{!49, !50, !51, !52, !53} -!49 = distinct !{!49, !47, !"air-alias-scope-arg(0)"} -!50 = distinct !{!50, !47, !"air-alias-scope-arg(1)"} -!51 = distinct !{!51, !47, !"air-alias-scope-arg(2)"} -!52 = distinct !{!52, !47, !"air-alias-scope-arg(3)"} -!53 = distinct !{!53, !47, !"air-alias-scope-arg(4)"} -!54 = !{i64 0, i64 8, !55} -!55 = !{!56, !56, i64 0} -!56 = !{!"__metal_command_buffer_t", !40, i64 0} -!57 = !{!49} -!58 = !{!50, !51, !52, !53, !46} -!59 = !{!60, !61, i64 0} -!60 = !{!"_ZTS6Camera", !61, i64 0, !61, i64 4} -!61 = !{!"float", !40, i64 0} -!62 = !{!63} -!63 = distinct !{!63, !64, !"air-alias-scope-arg(1)"} -!64 = distinct !{!64, !"air-alias-scopes(dio_vertex)"} -!65 = !{!60, !61, i64 4} +!26 = !{ptr @dio_encode_strided, !10, !14} +!27 = !{ptr @dio_vertex, !28, !30} +!28 = !{!29} +!29 = !{!"air.position", !"air.arg_type_name", !"float4"} +!30 = !{!31, !32} +!31 = !{i32 0, !"air.vertex_input", !"air.location_index", i32 0, i32 1, !"air.arg_type_name", !"float3", !"air.arg_name", !"position"} +!32 = !{i32 1, !"air.buffer", !"air.buffer_size", i32 8, !"air.location_index", i32 1, i32 1, !"air.read", !"air.address_space", i32 2, !"air.struct_type_info", !21, !"air.arg_type_size", i32 8, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"Camera", !"air.arg_name", !"camera"} +!33 = !{!"air.compile.denorms_disable"} +!34 = !{!"air.compile.fast_math_enable"} +!35 = !{!"air.compile.framebuffer_fetch_enable"} +!36 = !{!"Apple metal version 32023.922 (metalfe-32023.922.4)"} +!37 = !{i32 2, i32 8, i32 0} +!38 = !{!"Metal", i32 4, i32 0, i32 0} +!39 = !{!40, !40, i64 0} +!40 = !{!"int", !41, i64 0} +!41 = !{!"omnipotent char", !42, i64 0} +!42 = !{!"Simple C++ TBAA"} +!43 = !{!44} +!44 = distinct !{!44, !45, !"air-alias-scope-arg(0)"} +!45 = distinct !{!45, !"air-alias-scopes(dio_reset)"} +!46 = !{!47} +!47 = distinct !{!47, !48, !"air-alias-scope-arg(5)"} +!48 = distinct !{!48, !"air-alias-scopes(dio_encode)"} +!49 = !{!50, !51, !52, !53, !54} +!50 = distinct !{!50, !48, !"air-alias-scope-arg(0)"} +!51 = distinct !{!51, !48, !"air-alias-scope-arg(1)"} +!52 = distinct !{!52, !48, !"air-alias-scope-arg(2)"} +!53 = distinct !{!53, !48, !"air-alias-scope-arg(3)"} +!54 = distinct !{!54, !48, !"air-alias-scope-arg(4)"} +!55 = !{i64 0, i64 8, !56} +!56 = !{!57, !57, i64 0} +!57 = !{!"__metal_command_buffer_t", !41, i64 0} +!58 = !{!50} +!59 = !{!51, !52, !53, !54, !47} +!60 = !{!61} +!61 = distinct !{!61, !62, !"air-alias-scope-arg(5)"} +!62 = distinct !{!62, !"air-alias-scopes(dio_encode_strided)"} +!63 = !{!64, !65, !66, !67, !68} +!64 = distinct !{!64, !62, !"air-alias-scope-arg(0)"} +!65 = distinct !{!65, !62, !"air-alias-scope-arg(1)"} +!66 = distinct !{!66, !62, !"air-alias-scope-arg(2)"} +!67 = distinct !{!67, !62, !"air-alias-scope-arg(3)"} +!68 = distinct !{!68, !62, !"air-alias-scope-arg(4)"} +!69 = !{!64} +!70 = !{!65, !66, !67, !68, !61} +!71 = !{!72, !73, i64 0} +!72 = !{!"_ZTS6Camera", !73, i64 0, !73, i64 4} +!73 = !{!"float", !41, i64 0} +!74 = !{!75} +!75 = distinct !{!75, !76, !"air-alias-scope-arg(1)"} +!76 = distinct !{!76, !"air-alias-scopes(dio_vertex)"} +!77 = !{!72, !73, i64 4} diff --git a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.metal b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.metal index 44083048d..c36104dc4 100644 --- a/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.metal +++ b/crates/metal2vulkan-validation/fixtures/public/engine/icb_depth_indexed_encode.metal @@ -4,7 +4,9 @@ // by an atomic increment of the range's length (buffer 4, the range's second word) and encodes // there, onto the executing encoder's pipeline state, which the commands inherit, its own vertex // buffers (the `positions` buffer at 0, the address of the constant `camera` argument at 1) and -// an indexed triangle-list draw of 6 16-bit indices from 6 * tid, once. `dio_vertex` reads its +// an indexed triangle-list draw of 6 16-bit indices from 6 * tid, once. `dio_encode_strided` does +// the same, binding the positions with a stride of 12 bytes of its own, for a state whose layout +// takes its stride per bind (`MTLBufferLayoutStrideDynamic`). `dio_vertex` reads its // position as attribute 0 (a float3 from buffer 0) and places it at depth // position.z * camera.depth_scale + camera.depth_offset. There is no fragment function. Written // for this repository; no third-party source. Loop-free. @@ -44,6 +46,24 @@ kernel void dio_encode(device const Encoder &encoder [[buffer(0)]], command.draw_indexed_primitives(primitive_type::triangle, 6, indices + 6 * tid, 1); } +kernel void dio_encode_strided(device const Encoder &encoder [[buffer(0)]], + const device float *positions [[buffer(1)]], + const device ushort *indices [[buffer(2)]], + constant Camera &camera [[buffer(3)]], + device atomic_uint *length [[buffer(4)]], + constant uint &visible [[buffer(5)]], + uint tid [[thread_position_in_grid]]) +{ + if (((visible >> tid) & 1) == 0) { + return; + } + uint slot = atomic_fetch_add_explicit(length, 1, memory_order_relaxed); + render_command command(encoder.commands, slot); + command.set_vertex_buffer(positions, 12, 0); + command.set_vertex_buffer(&camera, 1); + command.draw_indexed_primitives(primitive_type::triangle, 6, indices + 6 * tid, 1); +} + struct DioIn { float3 position [[attribute(0)]]; }; diff --git a/crates/metal2vulkan-validation/src/runtime_lane/depth_executions.rs b/crates/metal2vulkan-validation/src/runtime_lane/depth_executions.rs index 2d9077dfa..b610cd115 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/depth_executions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/depth_executions.rs @@ -10,9 +10,11 @@ //! (`execute_commands_in_buffer_indirect`). The depth the pass stores is each visible chunk's quad //! at its depth, and the clear depth elsewhere: into a 2D texture, into slice 1 of a 2D array, //! beside a later encoder of the command buffer that makes a shadowed texture resident, and over a -//! rasterization rate map. And a frame's two passes that execute the commands after the culling -//! wait for the culling alone: not for another queue's unrelated work submitted before them, nor -//! the second for the first. +//! rasterization rate map. And a frame's two passes that execute the commands after the culling, +//! where each command binds the positions at a stride of its own (`dio_encode_strided`, a state +//! whose layout takes its stride per bind), which keeps their executions on the host step, wait +//! for the culling alone: not for another queue's unrelated work submitted before them, nor the +//! second for the first. At the state's fixed stride the frame's executions run on the GPU alone. use super::degrade_notices::on_device; use super::rate_maps::descriptor as descriptor_of_rate_map; @@ -74,15 +76,17 @@ fn kernel(device: &Device, library: &Library, name: &str) -> ComputePipelineStat } /// The depth-only state of `dio_vertex`: attribute 0 a `float3` of buffer 0's 12-byte layout, -/// no fragment function, a `Depth32Float` attachment, made for indirect command buffers. -fn depth_only(device: &Device, library: &Library) -> RenderPipelineState { +/// or of a layout whose stride each bind sets where `per_bind`, no fragment function, a +/// `Depth32Float` attachment, made for indirect command buffers. +fn depth_only(device: &Device, library: &Library, per_bind: bool) -> RenderPipelineState { let function = library.new_function_with_name("dio_vertex").unwrap(); let mut descriptor = RenderPipelineDescriptor::new(StageSource::Function(function.function_descriptor())); let mut vertex = VertexDescriptor::new(); - vertex - .layouts - .insert(0, VertexBufferLayoutDescriptor::new(Some(12))); + vertex.layouts.insert( + 0, + VertexBufferLayoutDescriptor::new((!per_bind).then_some(12)), + ); vertex.attributes.insert( 0, VertexAttributeDescriptor::new(VertexFormat::Float3, 0, 0), @@ -221,10 +225,19 @@ struct Workload { } impl Workload { + /// The workload at the state's fixed stride. fn new(device: &Device) -> Workload { + Workload::with_strides(device, false) + } + + /// The workload, each command binding the positions at a stride of its own where `per_bind` + /// (`dio_encode_strided`, the state's layout taking its stride per bind), else at the state's + /// fixed stride (`dio_encode`). + fn with_strides(device: &Device, per_bind: bool) -> Workload { let library = library(device); let mut descriptor = IndirectCommandBufferDescriptor::new(); descriptor.command_types = IndirectCommandType::DRAW_INDEXED; + descriptor.support_dynamic_attribute_stride = per_bind; descriptor.inherit_pipeline_state = true; descriptor.inherit_buffers = false; descriptor.max_vertex_buffer_bind_count = 2; @@ -235,8 +248,11 @@ impl Workload { let camera: Vec = CAMERA.iter().flat_map(|f| f.to_le_bytes()).collect(); Workload { reset: kernel(device, &library, "dio_reset"), - encode: kernel(device, &library, "dio_encode"), - state: depth_only(device, &library), + encode: match per_bind { + true => kernel(device, &library, "dio_encode_strided"), + false => kernel(device, &library, "dio_encode"), + }, + state: depth_only(device, &library, per_bind), less: less(device), icb, encoder_args, @@ -370,55 +386,77 @@ fn a_kernel_s_depth_only_commands_draw_through_the_range_it_wrote() { }); } -/// A frame's executions after the culling wait for the culling alone. One queue commits the -/// culling; another commits unrelated work (a fill of a buffer of its own), submitted before the -/// frame is committed; then the first commits the frame: two passes, each into a target of its -/// own, that execute the commands through the range. Each pass's host step waits for the batches -/// that wrote the records and the range, so both gates leave work submitted before them out (the -/// unrelated fill, and for the second the first pass's batch): `host_step_gates_narrowed` counts -/// two, against none where a gate is every queue's last submission. Both targets hold each kept -/// chunk's depth. +/// A frame's executions after the culling wait for the culling alone ([`frame`]). Each command +/// binds the positions at a stride of its own, which keeps the executions on the host step (a +/// command's stride lives in its record alone: `DrawReplayRule::VertexAttributes`). Each pass's +/// host step waits for the batches that wrote the records and the range, so both gates leave +/// work submitted before them out (the unrelated fill, and for the second the first pass's +/// batch): `host_step_gates_narrowed` counts two, against none where a gate is every queue's last +/// submission. Both targets hold each kept chunk's depth. #[test] fn a_frame_s_executions_wait_for_the_culling_alone() { on_device(|device, _| { - let work = Workload::new(device); - let queue = device.new_command_queue().unwrap(); - let other = device.new_command_queue().unwrap(); - let culling = queue.command_buffer().unwrap(); - work.cull(&culling); - culling.commit().unwrap(); - let filled = buffer_with(device, &[0; 1 << 16]).unwrap(); - let unrelated = other.command_buffer().unwrap(); - let mut fill = unrelated.blit_command_encoder().unwrap(); - fill.fill_buffer(&filled, 0..1 << 16, 0x5A).unwrap(); - fill.end_encoding().unwrap(); - unrelated.commit().unwrap(); - unrelated.wait_until_scheduled().unwrap(); - // The culling's own host steps (a dispatch whose pipeline was still being made) are - // behind it once it is scheduled. - culling.wait_until_scheduled().unwrap(); - let before = device.work_counts(); - let targets = [target(device, None), target(device, None)]; - let frame = queue.command_buffer().unwrap(); - for target in &targets { - work.draw(&frame, &depth_pass(target, 0)); - } - frame.commit().unwrap(); - frame.wait_until_completed_for(WAIT).unwrap(); - let after = device.work_counts(); - unrelated.wait_until_completed_for(WAIT).unwrap(); - let narrowed = after.host_step_gates_narrowed - before.host_step_gates_narrowed; - let steps = after.host_steps_recorded - before.host_steps_recorded; - let drawn: Vec> = targets.iter().map(|target| depths(target, 0)).collect(); - let statuses = [culling.status(), unrelated.status(), frame.status()]; - eprintln!( - "statuses {statuses:?}, refusal {:?}, steps {steps}, gates narrowed {narrowed}, \ - depths {drawn:?}", - frame.refusal() - ); - assert_eq!(statuses, [CommandBufferStatus::Completed; 3]); - assert_eq!(frame.refusal(), None); - assert_eq!(drawn, [expected(), expected()]); - assert_eq!((steps, narrowed), (2, 2), "host steps, gates narrowed"); + let counts = frame(device, true); + assert_eq!(counts, (2, 2), "host steps, gates narrowed"); }); } + +/// [`frame`] at the state's fixed stride: the executions take the GPU form (the replay pulls the +/// state's `float3` attribute from its 12-byte layout), so no host step runs and no gate is +/// taken, and both targets hold each kept chunk's depth. +#[test] +fn a_frame_s_executions_at_a_fixed_stride_run_on_the_gpu() { + on_device(|device, _| { + let counts = frame(device, false); + assert_eq!(counts, (0, 0), "host steps, gates narrowed"); + }); +} + +/// A frame after the culling on `device`. One queue commits the culling; another commits +/// unrelated work (a fill of a buffer of its own), submitted before the frame is committed; then +/// the first commits the frame: two passes, each into a target of its own, that execute the +/// commands through the range, each command binding the positions at a stride of its own where +/// `per_bind` ([`Workload::with_strides`]). Every command buffer completes, the frame refuses +/// nothing, and both targets hold each kept chunk's depth. Returns the host steps the frame +/// recorded and the gates it narrowed. +fn frame(device: &Device, per_bind: bool) -> (u64, u64) { + let work = Workload::with_strides(device, per_bind); + let queue = device.new_command_queue().unwrap(); + let other = device.new_command_queue().unwrap(); + let culling = queue.command_buffer().unwrap(); + work.cull(&culling); + culling.commit().unwrap(); + let filled = buffer_with(device, &[0; 1 << 16]).unwrap(); + let unrelated = other.command_buffer().unwrap(); + let mut fill = unrelated.blit_command_encoder().unwrap(); + fill.fill_buffer(&filled, 0..1 << 16, 0x5A).unwrap(); + fill.end_encoding().unwrap(); + unrelated.commit().unwrap(); + unrelated.wait_until_scheduled().unwrap(); + // The culling's own host steps (a dispatch whose pipeline was still being made) are + // behind it once it is scheduled. + culling.wait_until_scheduled().unwrap(); + let before = device.work_counts(); + let targets = [target(device, None), target(device, None)]; + let frame = queue.command_buffer().unwrap(); + for target in &targets { + work.draw(&frame, &depth_pass(target, 0)); + } + frame.commit().unwrap(); + frame.wait_until_completed_for(WAIT).unwrap(); + let after = device.work_counts(); + unrelated.wait_until_completed_for(WAIT).unwrap(); + let narrowed = after.host_step_gates_narrowed - before.host_step_gates_narrowed; + let steps = after.host_steps_recorded - before.host_steps_recorded; + let drawn: Vec> = targets.iter().map(|target| depths(target, 0)).collect(); + let statuses = [culling.status(), unrelated.status(), frame.status()]; + eprintln!( + "per bind {per_bind}: statuses {statuses:?}, refusal {:?}, steps {steps}, gates \ + narrowed {narrowed}, depths {drawn:?}", + frame.refusal() + ); + assert_eq!(statuses, [CommandBufferStatus::Completed; 3]); + assert_eq!(frame.refusal(), None); + assert_eq!(drawn, [expected(), expected()]); + (steps, narrowed) +}