diff --git a/crates/metal2vulkan/src/lower/simd/group.rs b/crates/metal2vulkan/src/lower/simd/group.rs index 6261a7c2b..71cd584e8 100644 --- a/crates/metal2vulkan/src/lower/simd/group.rs +++ b/crates/metal2vulkan/src/lower/simd/group.rs @@ -114,17 +114,16 @@ pub fn cluster_ops(g: &GroupPlan) -> OpBits { /// Quads are the same in every tier: aligned groups of four subgroup lanes. A reduction, a /// prefix sum or product and the first active lane's broadcast read the quad's lanes by shuffles -/// under the ballot of its active lanes, in Metal's order, except a fragment's reduction, which -/// reads them by quad instructions ([`quad_reduce`]); a vote is the quad's bits of the ballot, -/// except a fragment's, which is the quad vote instruction. A broadcast or shuffle-xor by a -/// constant lane or mask is Vulkan's quad class where the stage takes it, and a shuffle elsewhere. +/// under the ballot of its active lanes, in Metal's order, and a vote, `is_first`, the ballot and +/// the active mask are the quad's bits of the ballot; a fragment's are quad instructions alone +/// ([`quad_votes`], [`quad_lanes`]). A broadcast or shuffle-xor by a constant lane or mask is +/// Vulkan's quad class where the stage takes it, and a shuffle elsewhere. fn quad_ops(g: &GroupPlan) -> OpBits { use GroupOp as G; match (g.op, g.konst) { (G::ShuffleXor, Some(0)) => OpBits::NONE, (G::Broadcast | G::ShuffleXor, Some(_)) if g.quad_instructions => OpBits::QUAD, - (op, _) if g.helpers && combines(op) && !scan(op) => OpBits::QUAD, - (G::All | G::Any, _) if g.helpers => OpBits::QUAD, + _ if g.helpers && !reads_lanes(g) => OpBits::QUAD, (G::BroadcastFirst, _) => OpBits::BALLOT.union(OpBits::SHUFFLE), (op, _) if combines(op) => OpBits::BALLOT.union(OpBits::SHUFFLE), (G::All | G::Any | G::Ballot | G::IsFirst | G::ActiveThreadsMask, _) => OpBits::BALLOT, @@ -243,6 +242,13 @@ pub(super) fn words(g: &GroupPlan) -> u32 { // ([`quad_tree`]). op if combines(op) && g.scope == GroupScope::Quad => 512 + 4 * round, op if combines(op) => 64 + round, + // A fragment's quad's four votes and their bits, or four reads and their selects + // ([`quad_votes`], [`quad_lanes`]). + G::Ballot | G::IsFirst | G::ActiveThreadsMask | G::BroadcastFirst + if g.helpers && g.scope == GroupScope::Quad => + { + 512 + } _ => 128, } } @@ -837,20 +843,68 @@ pub(super) fn fill_source( Ok((src, inside)) } +/// A fragment's quad holds helpers, which take part in Metal's quad operations (`full_quads`). +/// Vulkan runs its quad instructions over a quad's helpers, and its other group operations, a +/// ballot and a shuffle among them, over them only where the entry maximally reconverges (section +/// 9.31, "Helper Invocations"), and AMD's open Linux driver leaves the helpers out of a ballot even +/// there. So a fragment reads its quad through quad instructions alone: quad lane `j` takes part +/// when `OpGroupNonUniformQuadAnyKHR` of "the quad lane is `j`" (and of `pred`, where given) +/// holds. +fn quad_votes( + fb: &mut FunctionBuilder<'_>, + ln: &Lane, + pred: Option, +) -> BResult> { + let q = bin_k(fb, BinOp::BitwiseAnd, ln.l, 3)?; + (0..4) + .map(|j| { + let me = bin_k(fb, BinOp::IEqual, q, j)?; + let me = match pred { + Some(p) => fb.binary(BinOp::LogicalAnd, me, p)?, + None => me, + }; + fb.group_quad_vote(false, me) + }) + .collect() +} + +/// The quad's four bits of a fragment's quad `votes` ([`quad_votes`]): bit `j` is quad lane `j`'s. +fn quad_word(fb: &mut FunctionBuilder<'_>, votes: &[SValue]) -> BResult { + let zero = k(fb, 0)?; + let mut word = zero; + for (j, &on) in (0..).zip(votes) { + let bit = k(fb, 1 << j)?; + let bit = fb.select(on, bit, zero)?; + word = fb.binary(BinOp::BitwiseOr, word, bit)?; + } + Ok(word) +} + +/// A fragment's quad's four lanes of `x` ([`quad_votes`]): for each quad lane `j`, its value, +/// `OpGroupNonUniformQuadBroadcast` from the constant lane `j`, and whether it takes part. A lane +/// that takes no part is read as poison, which no caller's select takes (SPIR-V's `OpSelect` +/// keeps an operand it does not select from its result). +fn quad_lanes( + fb: &mut FunctionBuilder<'_>, + ln: &Lane, + x: SValue, +) -> BResult> { + let q = bin_k(fb, BinOp::BitwiseAnd, ln.l, 3)?; + (0..4) + .map(|j| { + let me = bin_k(fb, BinOp::IEqual, q, j)?; + let on = fb.group_quad_vote(false, me)?; + let i = k(fb, j)?; + Ok((fb.group_quad_broadcast(x, i)?, on)) + }) + .collect() +} + /// A quad reduction in every subgroup tier: the quad's four lanes, each with whether it takes -/// part, combined in Metal's order ([`quad_tree`]). -/// -/// A fragment's quad holds helpers, which take part in Metal's reductions (`full_quads`). Vulkan -/// runs its quad instructions over a quad's helpers, and its other group operations, a ballot and -/// a shuffle among them, over them only where the entry maximally reconverges (section 9.31, -/// "Helper Invocations"). So there lane `j` takes part when `OpGroupNonUniformQuadAnyKHR` of -/// "the quad lane is `j`" holds, and its value is `OpGroupNonUniformQuadBroadcast` from quad lane -/// `j`: every lane folds the same four values in the same order, bit for bit. A lane that takes -/// no part is read as poison, which [`quad_tree`]'s selects never take (SPIR-V's `OpSelect` keeps -/// an operand it does not select from its result). -/// -/// Elsewhere lane `j` takes part when its bit of the ballot of the active lanes is set, and its -/// value is the shuffle from it, or from the calling lane when it takes no part. +/// part, combined in Metal's order ([`quad_tree`]). A fragment's lanes are its [`quad_lanes`]: +/// every lane folds the same four values in the same order, bit for bit. Elsewhere lane `j` takes +/// part when its bit of the ballot of the active lanes is set, and its value is the shuffle from +/// it, or from the calling lane when it takes no part. fn quad_reduce( fb: &mut FunctionBuilder<'_>, ln: &Lane, @@ -858,20 +912,14 @@ fn quad_reduce( v: SValue, ) -> BResult { let x = pre(fb, g, v)?; - let mut lanes = Vec::with_capacity(4); - if g.helpers { - let q = bin_k(fb, BinOp::BitwiseAnd, ln.l, 3)?; - for j in 0..4 { - let me = bin_k(fb, BinOp::IEqual, q, j)?; - let on = fb.group_quad_vote(false, me)?; - let i = k(fb, j)?; - lanes.push((fb.group_quad_broadcast(x, i)?, on)); - } + let lanes = if g.helpers { + quad_lanes(fb, ln, x)? } else { let word = ln.active(fb)?; let act = ln.quad_bits(fb, word)?; let base = bin_k(fb, BinOp::BitwiseAnd, ln.l, !3)?; let zero = k(fb, 0)?; + let mut lanes = Vec::with_capacity(4); for j in 0..4 { let bit = bin_k(fb, BinOp::ShiftRightLogical, act, j)?; let bit = bin_k(fb, BinOp::BitwiseAnd, bit, 1)?; @@ -879,7 +927,8 @@ fn quad_reduce( let from = bin_k(fb, BinOp::BitwiseOr, base, j)?; lanes.push((ln.read_or_own(fb, None, x, on, from)?, on)); } - } + lanes + }; let r = quad_tree(fb, g, &lanes)?; post(fb, g, r) } @@ -918,14 +967,22 @@ fn nth_lane(lanes: &[(SValue, SValue)], i: usize) -> BResult<(SValue, SValue)> { } /// The lanes a [`lane_scan`] reads: the word of the group's active lanes (bit `j` is the group's -/// lane `j`), the calling lane's index in the group, the group's first lane, and its width. +/// lane `j`), the calling lane's index in the group, how it reads them, and the group's width. struct Span { act: SValue, m: SValue, - base: SValue, + reads: LaneReads, width: u32, } +/// How a [`lane_scan`] reads the group's lane `j`: by a shuffle from the group's first lane +/// `base` plus `j`, or the calling lane where `j` takes no part; or, in a fragment's quad, as its +/// [`quad_lanes`] value. +enum LaneReads { + Shuffle { base: SValue }, + Quad(Vec), +} + /// A `Cluster32` prefix sum or product: [`lane_scan`] over the 32-lane cluster. An exclusive float /// sum with no contribution is `+0`, the identity of Vulkan's scans. fn cluster_scan( @@ -941,7 +998,7 @@ fn cluster_scan( let span = Span { act, m, - base, + reads: LaneReads::Shuffle { base }, width: SIMD_WIDTH, }; lane_scan(fb, ln, g, v, span, true) @@ -949,16 +1006,35 @@ fn cluster_scan( /// A quad prefix sum or product in every subgroup tier: [`lane_scan`] over the quad, Metal's order /// (measured, in lane order, the exclusive sum of the first active lane `-0` and its product 1, a -/// `half` rounded at every step). +/// `half` rounded at every step). A fragment's quad is its [`quad_lanes`], their votes the active +/// lanes; elsewhere the active lanes are the quad's bits of the ballot. fn quad_scan(fb: &mut FunctionBuilder<'_>, ln: &Lane, g: &GroupPlan, v: SValue) -> BResult { - let word = ln.active(fb)?; - let act = ln.quad_bits(fb, word)?; + let lanes = if g.helpers { + Some(quad_lanes(fb, ln, v)?) + } else { + None + }; + let act = match &lanes { + Some(lanes) => { + let votes: Vec = lanes.iter().map(|&(_, on)| on).collect(); + quad_word(fb, &votes)? + } + None => { + let word = ln.active(fb)?; + ln.quad_bits(fb, word)? + } + }; let m = bin_k(fb, BinOp::BitwiseAnd, ln.l, 3)?; - let base = bin_k(fb, BinOp::BitwiseAnd, ln.l, !3)?; + let reads = match lanes { + Some(lanes) => LaneReads::Quad(lanes.into_iter().map(|(x, _)| x).collect()), + None => LaneReads::Shuffle { + base: bin_k(fb, BinOp::BitwiseAnd, ln.l, !3)?, + }, + }; let span = Span { act, m, - base, + reads, width: 4, }; lane_scan(fb, ln, g, v, span, false) @@ -967,10 +1043,11 @@ fn quad_scan(fb: &mut FunctionBuilder<'_>, ln: &Lane, g: &GroupPlan, v: SValue) /// A prefix sum or product over a group of lanes read one by one: the sum or product, in /// increasing lane order, of the values of the group's active lanes below the calling lane (and /// the calling lane itself when inclusive). Each step reads one lane when it is included and the -/// calling lane otherwise, so no step reads an inactive lane, and inactive lanes contribute -/// nothing whatever the divergence. The empty answer is the operation's identity: `-0` for a float -/// sum, or `+0` where `plus_zero` (the cluster's), and 1 for a product, Metal's answer for the -/// first active lane (measured). +/// calling lane otherwise, so no step reads an inactive lane (a fragment's quad step takes its +/// lane's [`quad_lanes`] value, which the step's select keeps only where the lane is included), +/// and inactive lanes contribute nothing whatever the divergence. The empty answer is the +/// operation's identity: `-0` for a float sum, or `+0` where `plus_zero` (the cluster's), and 1 +/// for a product, Metal's answer for the first active lane (measured). fn lane_scan( fb: &mut FunctionBuilder<'_>, ln: &Lane, @@ -1010,8 +1087,13 @@ fn lane_scan( let bit = bin_k(fb, BinOp::ShiftRightLogical, include, j)?; let bit = bin_k(fb, BinOp::BitwiseAnd, bit, 1)?; let on = fb.binary(BinOp::INotEqual, bit, zero)?; - let from = bin_k(fb, BinOp::BitwiseOr, span.base, j)?; - let x = ln.read_or_own(fb, None, v, on, from)?; + let x = match &span.reads { + LaneReads::Shuffle { base } => { + let from = bin_k(fb, BinOp::BitwiseOr, *base, j)?; + ln.read_or_own(fb, None, v, on, from)? + } + LaneReads::Quad(values) => nth(values, j as usize)?, + }; let sum = fb.binary(add, acc, x)?; let sum = if float { narrow::round(fb, g.form, sum)? @@ -1150,6 +1232,16 @@ fn quad_op( ln.read_from(fb, live, v()?, src) } (G::ShuffleAndFillUp | G::ShuffleAndFillDown, _) => fill(fb, ln, live, g, a), + // A fragment's quad's first lane that takes part ([`quad_lanes`]), the calling lane at the + // latest. + (G::BroadcastFirst, _) if g.helpers => { + let lanes = quad_lanes(fb, ln, v()?)?; + let mut first = v()?; + for &(x, on) in lanes.iter().rev() { + first = fb.select(on, x, first)?; + } + Ok(first) + } // The quad's first active lane, by the ballot of `true` (the calling lane is one). (G::BroadcastFirst, _) => { let word = ln.active(fb)?; @@ -1185,15 +1277,22 @@ fn quad_op( fb.binary(BinOp::IEqual, held, act) } (G::Ballot | G::IsFirst | G::ActiveThreadsMask, _) => { - // The quad's four bits of the simdgroup's ballot word (of the predicate for a - // ballot, of `true` otherwise; inactive lanes give 0): quads never straddle a word. - // Bit `j` is quad lane `j`, as Metal's `quad_vote` numbers them. - let word = if g.op == G::Ballot { - ln.word(fb, v()?)? + // The quad's four bits of the lanes that take part (and whose predicate holds, for a + // ballot): a fragment's quad votes ([`quad_votes`]), elsewhere the quad's bits of the + // simdgroup's ballot word (of the predicate for a ballot, of `true` otherwise; + // inactive lanes give 0), as quads never straddle a word. Bit `j` is quad lane `j`, + // as Metal's `quad_vote` numbers them. + let pred = if g.op == G::Ballot { Some(v()?) } else { None }; + let bits = if g.helpers { + let votes = quad_votes(fb, ln, pred)?; + quad_word(fb, &votes)? } else { - ln.active(fb)? + let word = match pred { + Some(p) => ln.word(fb, p)?, + None => ln.active(fb)?, + }; + ln.quad_bits(fb, word)? }; - let bits = ln.quad_bits(fb, word)?; if g.op != G::IsFirst { return Ok(bits); } diff --git a/crates/metal2vulkan/src/lower/simd/mod.rs b/crates/metal2vulkan/src/lower/simd/mod.rs index 4f7cf957a..b8e8047f6 100644 --- a/crates/metal2vulkan/src/lower/simd/mod.rs +++ b/crates/metal2vulkan/src/lower/simd/mod.rs @@ -102,9 +102,10 @@ //! declares `MaximallyReconvergesKHR` (section 9.31, "Helper Invocations"), as every such entry //! does (**Reconvergence**, below): a profile with `shaderQuadControl` has maximal reconvergence, //! which every device with the one supports (`Device::from_queries` refuses a description listing -//! the one without the other). A quad reduction or vote -//! there reads its quad through quad instructions alone. A profile without `shaderQuadControl` -//! refuses such a function. +//! the one without the other). A quad reduction, prefix sum or product, vote, ballot, active mask, +//! `is_first` or first active lane's broadcast there reads its quad through quad instructions +//! alone: AMD's open Linux driver leaves a partly covered quad's helper out of a ballot even under +//! `MaximallyReconvergesKHR`. A profile without `shaderQuadControl` refuses such a function. //! //! In compute the flattened tiers derive every Metal thread builtin from the subgroup partition //! (`lower::dispatch`), so simdgroup `k` is exactly the linear threads `[32k, 32k + 32)` and a @@ -119,14 +120,15 @@ //! `OpGroupNonUniformQuadBroadcast` for constant masks and lanes where the stage takes Vulkan's //! quad instructions (else the shuffles from the same lanes), reductions, prefix sums and products //! over the quad's four lanes in Metal's order, each lane read by a shuffle under its bit of the -//! ballot of the active lanes (a fragment's reduction by `OpGroupNonUniformQuadBroadcast` under -//! `OpGroupNonUniformQuadAnyKHR` of its quad lane, **Helpers** above), and the ballot word for -//! votes (a fragment's by `OpGroupNonUniformQuadAllKHR` and `…AnyKHR`), `is_first`, the active -//! mask, and `quad_ballot` (the quad's four bits, quad lane `j` at bit `j`). A simdgroup's -//! `simd_active_threads_mask` is -//! the ballot of `true`, held as the 64-bit ballot is. A fragment's `[[thread_index_in_quadgroup]]` -//! is the subgroup lane's low two bits ([`select_param`]), or, on a profile without the fragment -//! quad tier, the pixel's place in its aligned 2x2 block, read from `FragCoord`. +//! ballot of the active lanes, and the ballot word for votes, `is_first`, the first active lane's +//! broadcast, the active mask, and `quad_ballot` (the quad's four bits, quad lane `j` at bit `j`). +//! A fragment's (**Helpers** above) read lane `j` by `OpGroupNonUniformQuadBroadcast` under +//! `OpGroupNonUniformQuadAnyKHR` of "the quad lane is `j`", take the active lanes' bits from those +//! votes (of the predicate too, for `quad_ballot`), and vote by `OpGroupNonUniformQuadAllKHR` and +//! `…AnyKHR`. A simdgroup's `simd_active_threads_mask` is the ballot of `true`, held as the 64-bit +//! ballot is. A fragment's `[[thread_index_in_quadgroup]]` is the subgroup lane's low two bits +//! ([`select_param`]), or, on a profile without the fragment quad tier, the pixel's place in its +//! aligned 2x2 block, read from `FragCoord`. //! //! **Values** are held as `lower::arith` holds them: narrow integers zero-extended in a `u32`, //! `half` and `bfloat` widened in an `f32`. Moving lanes is exact on the container. A signed @@ -215,8 +217,9 @@ pub struct GroupPlan { /// outside kernels, where a quad may lack a lane. pub quad_instructions: bool, /// The function is a fragment function, whose quads hold helpers ([`full_quads`]): a quad - /// reduction or vote reads its quad through quad instructions, which Vulkan runs over the - /// quad's helpers whatever the entry's reconvergence (section 9.31, "Helper Invocations"). + /// operation that counts or combines its quad's lanes reads its quad through quad + /// instructions, which Vulkan runs over the quad's helpers whatever the entry's reconvergence + /// (section 9.31, "Helper Invocations"). pub helpers: bool, } diff --git a/crates/metal2vulkan/src/lower/simd/tests.rs b/crates/metal2vulkan/src/lower/simd/tests.rs index fb86397bf..d2428e08c 100644 --- a/crates/metal2vulkan/src/lower/simd/tests.rs +++ b/crates/metal2vulkan/src/lower/simd/tests.rs @@ -65,10 +65,11 @@ fn in_fragment(g: GroupPlan) -> GroupPlan { /// is arithmetic, a `Cluster32` one clustered; a `Cluster32` prefix sum or product is ballot and /// shuffles; a rotate up is a shuffle; a quad broadcast or shuffle-xor by a constant is the quad /// class where the stage takes quad instructions and a shuffle elsewhere; a fragment's quad -/// reduction and vote are the quad class, its prefix sum ballot and shuffles. +/// reduction, prefix sum, vote, first active lane's broadcast, ballot, active mask and `is_first` +/// are the quad class. #[test] fn operation_classes_follow_the_tier() { - let cases: [(GroupPlan, OpBits, OpBits); 26] = [ + let cases: [(GroupPlan, OpBits, OpBits); 30] = [ ( plan(S::Simdgroup, G::Shuffle, F32, None), OpBits::SHUFFLE, @@ -196,8 +197,28 @@ fn operation_classes_follow_the_tier() { ), ( in_fragment(plan(S::Quad, G::PrefixInclusiveSum, F32, None)), - OpBits::BALLOT.union(OpBits::SHUFFLE), - OpBits::BALLOT.union(OpBits::SHUFFLE), + OpBits::QUAD, + OpBits::QUAD, + ), + ( + in_fragment(plan(S::Quad, G::BroadcastFirst, F32, None)), + OpBits::QUAD, + OpBits::QUAD, + ), + ( + in_fragment(plan(S::Quad, G::Ballot, LANE_FORM, None)), + OpBits::QUAD, + OpBits::QUAD, + ), + ( + in_fragment(plan(S::Quad, G::ActiveThreadsMask, LANE_FORM, None)), + OpBits::QUAD, + OpBits::QUAD, + ), + ( + in_fragment(plan(S::Quad, G::IsFirst, BOOL, None)), + OpBits::QUAD, + OpBits::QUAD, ), ]; for (g, d, c) in cases { diff --git a/crates/metal2vulkan/src/tests/engine_simd.rs b/crates/metal2vulkan/src/tests/engine_simd.rs index 89f1c746a..fa916c471 100644 --- a/crates/metal2vulkan/src/tests/engine_simd.rs +++ b/crates/metal2vulkan/src/tests/engine_simd.rs @@ -1543,10 +1543,11 @@ fn vertex_lane_reads_past_the_draw_name_no_inactive_lane() { /// A fragment function whose group operations read, combine, or count other lanes declares /// `RequireFullQuadsKHR` on every committed profile with `shaderQuadControl`, needs that feature, /// and reads every lane by a shuffle through the ballot of the live lanes (a quad may lack a -/// lane), except a reduction (`fragment_quad_scans`' product), whose one quad broadcast per lane -/// follows that lane's quad vote; a profile without it (`moltenvk-m3`) whose stage has the -/// classes refuses the function, needing the feature. A fragment that only reads its quad lane -/// (`simd_masks`' `quad_lane_f`) declares no such mode and needs no such feature anywhere. +/// lane), except a quad reduction, prefix scan or first lane's broadcast (`fragment_quad_scans`), +/// whose one quad broadcast per lane follows that lane's quad vote; a profile without it +/// (`moltenvk-m3`) whose stage has the classes refuses the function, needing the feature. A +/// fragment that only reads its quad lane (`simd_masks`' `quad_lane_f`) declares no such mode +/// and needs no such feature anywhere. #[test] fn fragment_group_operations_run_full_quads() { use crate::error::{Capability, Unsupported}; @@ -1621,9 +1622,9 @@ fn fragment_group_operations_run_full_quads() { /// over the quad's helpers in every entry (section 9.31): on every committed profile with /// `shaderQuadControl`, `fragment_quad_bitwise`'s AND, OR and XOR each ask /// `OpGroupNonUniformQuadAnyKHR` whether each of the four quad lanes takes part and read each by -/// `OpGroupNonUniformQuadBroadcast` (4 of each per reduction, 12 in all), and its quad prefix sum -/// alone reads by a ballot and shuffles. A kernel's quad reductions (`quad_bitwise`), whose quads -/// hold no helpers, keep the active lanes' word and take no quad vote. +/// `OpGroupNonUniformQuadBroadcast` (4 of each per reduction), and so does its quad prefix sum: +/// 16 of each in all, and no ballot. A kernel's quad reductions (`quad_bitwise`), whose quads hold +/// no helpers, keep the active lanes' word and take no quad vote. #[test] fn fragment_quad_reductions_read_their_quads_by_quad_instructions() { let Some(quads) = assemble("gsm2_quad_ops") else { @@ -1639,20 +1640,14 @@ fn fragment_quad_reductions_read_their_quads_by_quad_instructions() { let (_, words) = link_and_validate(&a, &p).expect("valid"); let text = spirv_dis(&words); assert_eq!( - count(&text, "OpGroupNonUniformQuadAnyKHR"), - 12, + [ + count(&text, "OpGroupNonUniformQuadAnyKHR"), + count(&text, "OpGroupNonUniformQuadBroadcast"), + count(&text, "OpGroupNonUniformBallot "), + ], + [16, 16, 0], "fragment_quad_bitwise on {name}" ); - assert_eq!( - count(&text, "OpGroupNonUniformQuadBroadcast"), - 12, - "fragment_quad_bitwise on {name}" - ); - assert_eq!( - count(&text, "OpGroupNonUniformBallot "), - 1, - "fragment_quad_bitwise on {name}: the prefix sum's ballot alone" - ); fragments += 1; if let Ok(a) = compile(&quads, "quad_bitwise", &p) { let (_, words) = link_and_validate(&a, &p).expect("valid"); @@ -1753,6 +1748,57 @@ fn fragment_quad_votes_are_quad_vote_instructions() { assert!(fragments > 0, "no committed profile has quad control"); } +/// A fragment's quad prefix sums and products, first active lane's broadcast, `quad_ballot` and +/// `quad_active_threads_mask` read their quad through quad instructions alone, which Vulkan runs +/// over the quad's helpers in every entry (section 9.31): on every committed profile with +/// `shaderQuadControl`, `fragment_quad_scans` (a product, two prefix scans and a first lane's +/// broadcast) asks `OpGroupNonUniformQuadAnyKHR` 16 times and reads 16 lanes by +/// `OpGroupNonUniformQuadBroadcast`, and `fragment_helper_threads` (two ballots and the active +/// mask) votes 12 times and reads none; neither holds a ballot. A kernel's quad scans +/// (`quad_float_scans`), whose quads hold no helpers, take no quad vote. +#[test] +fn fragment_quad_scans_and_masks_read_their_quads_by_quad_instructions() { + let (Some(quads), Some(helpers)) = (assemble("gsm2_quad_ops"), assemble("gsm2_helper_threads")) + else { + return; + }; + let count = |text: &str, op: &str| text.lines().filter(|l| l.contains(op)).count(); + let mut fragments = 0; + for (name, p) in committed_profiles().expect("profiles") { + if !p.fields().subgroup.quad_control { + continue; + } + for (bc, entry, votes, reads) in [ + (&quads, "fragment_quad_scans", 16, 16), + (&helpers, "fragment_helper_threads", 12, 0), + ] { + let a = compile(bc, entry, &p).expect("compile"); + let (_, words) = link_and_validate(&a, &p).expect("valid"); + let text = spirv_dis(&words); + assert_eq!( + [ + count(&text, "OpGroupNonUniformQuadAnyKHR"), + count(&text, "OpGroupNonUniformQuadBroadcast"), + count(&text, "OpGroupNonUniformBallot "), + ], + [votes, reads, 0], + "{entry} on {name}" + ); + } + fragments += 1; + if let Ok(a) = compile(&quads, "quad_float_scans", &p) { + let (_, words) = link_and_validate(&a, &p).expect("valid"); + let text = spirv_dis(&words); + assert_eq!( + count(&text, "OpGroupNonUniformQuadAnyKHR"), + 0, + "quad_float_scans on {name}" + ); + } + } + assert!(fragments > 0, "no committed profile has quad control"); +} + /// MoltenVK 1.4.2 drops `VK_KHR_shader_quad_control` (`shaderQuadControl`) on Apple's GPUs. The /// committed `moltenvk-m3` description, a capture of 1.4.2 that states neither the feature nor /// its extension, refuses each fragment function whose group operations observe other lanes,